memory_hotplug.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063
  1. /*
  2. * linux/mm/memory_hotplug.c
  3. *
  4. * Copyright (C)
  5. */
  6. #include <linux/stddef.h>
  7. #include <linux/mm.h>
  8. #include <linux/swap.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/compiler.h>
  12. #include <linux/export.h>
  13. #include <linux/pagevec.h>
  14. #include <linux/writeback.h>
  15. #include <linux/slab.h>
  16. #include <linux/sysctl.h>
  17. #include <linux/cpu.h>
  18. #include <linux/memory.h>
  19. #include <linux/memory_hotplug.h>
  20. #include <linux/highmem.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/ioport.h>
  23. #include <linux/delay.h>
  24. #include <linux/migrate.h>
  25. #include <linux/page-isolation.h>
  26. #include <linux/pfn.h>
  27. #include <linux/suspend.h>
  28. #include <linux/mm_inline.h>
  29. #include <linux/firmware-map.h>
  30. #include <linux/stop_machine.h>
  31. #include <linux/hugetlb.h>
  32. #include <linux/memblock.h>
  33. #include <linux/bootmem.h>
  34. #include <asm/tlbflush.h>
  35. #include "internal.h"
  36. /*
  37. * online_page_callback contains pointer to current page onlining function.
  38. * Initially it is generic_online_page(). If it is required it could be
  39. * changed by calling set_online_page_callback() for callback registration
  40. * and restore_online_page_callback() for generic callback restore.
  41. */
  42. static void generic_online_page(struct page *page);
  43. static online_page_callback_t online_page_callback = generic_online_page;
  44. static DEFINE_MUTEX(online_page_callback_lock);
  45. /* The same as the cpu_hotplug lock, but for memory hotplug. */
  46. static struct {
  47. struct task_struct *active_writer;
  48. struct mutex lock; /* Synchronizes accesses to refcount, */
  49. /*
  50. * Also blocks the new readers during
  51. * an ongoing mem hotplug operation.
  52. */
  53. int refcount;
  54. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  55. struct lockdep_map dep_map;
  56. #endif
  57. } mem_hotplug = {
  58. .active_writer = NULL,
  59. .lock = __MUTEX_INITIALIZER(mem_hotplug.lock),
  60. .refcount = 0,
  61. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  62. .dep_map = {.name = "mem_hotplug.lock" },
  63. #endif
  64. };
  65. /* Lockdep annotations for get/put_online_mems() and mem_hotplug_begin/end() */
  66. #define memhp_lock_acquire_read() lock_map_acquire_read(&mem_hotplug.dep_map)
  67. #define memhp_lock_acquire() lock_map_acquire(&mem_hotplug.dep_map)
  68. #define memhp_lock_release() lock_map_release(&mem_hotplug.dep_map)
  69. void get_online_mems(void)
  70. {
  71. might_sleep();
  72. if (mem_hotplug.active_writer == current)
  73. return;
  74. memhp_lock_acquire_read();
  75. mutex_lock(&mem_hotplug.lock);
  76. mem_hotplug.refcount++;
  77. mutex_unlock(&mem_hotplug.lock);
  78. }
  79. void put_online_mems(void)
  80. {
  81. if (mem_hotplug.active_writer == current)
  82. return;
  83. mutex_lock(&mem_hotplug.lock);
  84. if (WARN_ON(!mem_hotplug.refcount))
  85. mem_hotplug.refcount++; /* try to fix things up */
  86. if (!--mem_hotplug.refcount && unlikely(mem_hotplug.active_writer))
  87. wake_up_process(mem_hotplug.active_writer);
  88. mutex_unlock(&mem_hotplug.lock);
  89. memhp_lock_release();
  90. }
  91. void mem_hotplug_begin(void)
  92. {
  93. mem_hotplug.active_writer = current;
  94. memhp_lock_acquire();
  95. for (;;) {
  96. mutex_lock(&mem_hotplug.lock);
  97. if (likely(!mem_hotplug.refcount))
  98. break;
  99. __set_current_state(TASK_UNINTERRUPTIBLE);
  100. mutex_unlock(&mem_hotplug.lock);
  101. schedule();
  102. }
  103. }
  104. void mem_hotplug_done(void)
  105. {
  106. mem_hotplug.active_writer = NULL;
  107. mutex_unlock(&mem_hotplug.lock);
  108. memhp_lock_release();
  109. }
  110. /* add this memory to iomem resource */
  111. static struct resource *register_memory_resource(u64 start, u64 size)
  112. {
  113. struct resource *res;
  114. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  115. BUG_ON(!res);
  116. res->name = "System RAM";
  117. res->start = start;
  118. res->end = start + size - 1;
  119. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  120. if (request_resource(&iomem_resource, res) < 0) {
  121. pr_debug("System RAM resource %pR cannot be added\n", res);
  122. kfree(res);
  123. res = NULL;
  124. }
  125. return res;
  126. }
  127. static void release_memory_resource(struct resource *res)
  128. {
  129. if (!res)
  130. return;
  131. release_resource(res);
  132. kfree(res);
  133. return;
  134. }
  135. #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
  136. void get_page_bootmem(unsigned long info, struct page *page,
  137. unsigned long type)
  138. {
  139. page->lru.next = (struct list_head *) type;
  140. SetPagePrivate(page);
  141. set_page_private(page, info);
  142. atomic_inc(&page->_count);
  143. }
  144. void put_page_bootmem(struct page *page)
  145. {
  146. unsigned long type;
  147. type = (unsigned long) page->lru.next;
  148. BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
  149. type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
  150. if (atomic_dec_return(&page->_count) == 1) {
  151. ClearPagePrivate(page);
  152. set_page_private(page, 0);
  153. INIT_LIST_HEAD(&page->lru);
  154. free_reserved_page(page);
  155. }
  156. }
  157. #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE
  158. #ifndef CONFIG_SPARSEMEM_VMEMMAP
  159. static void register_page_bootmem_info_section(unsigned long start_pfn)
  160. {
  161. unsigned long *usemap, mapsize, section_nr, i;
  162. struct mem_section *ms;
  163. struct page *page, *memmap;
  164. section_nr = pfn_to_section_nr(start_pfn);
  165. ms = __nr_to_section(section_nr);
  166. /* Get section's memmap address */
  167. memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
  168. /*
  169. * Get page for the memmap's phys address
  170. * XXX: need more consideration for sparse_vmemmap...
  171. */
  172. page = virt_to_page(memmap);
  173. mapsize = sizeof(struct page) * PAGES_PER_SECTION;
  174. mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
  175. /* remember memmap's page */
  176. for (i = 0; i < mapsize; i++, page++)
  177. get_page_bootmem(section_nr, page, SECTION_INFO);
  178. usemap = __nr_to_section(section_nr)->pageblock_flags;
  179. page = virt_to_page(usemap);
  180. mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
  181. for (i = 0; i < mapsize; i++, page++)
  182. get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
  183. }
  184. #else /* CONFIG_SPARSEMEM_VMEMMAP */
  185. static void register_page_bootmem_info_section(unsigned long start_pfn)
  186. {
  187. unsigned long *usemap, mapsize, section_nr, i;
  188. struct mem_section *ms;
  189. struct page *page, *memmap;
  190. if (!pfn_valid(start_pfn))
  191. return;
  192. section_nr = pfn_to_section_nr(start_pfn);
  193. ms = __nr_to_section(section_nr);
  194. memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
  195. register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION);
  196. usemap = __nr_to_section(section_nr)->pageblock_flags;
  197. page = virt_to_page(usemap);
  198. mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
  199. for (i = 0; i < mapsize; i++, page++)
  200. get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
  201. }
  202. #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
  203. void register_page_bootmem_info_node(struct pglist_data *pgdat)
  204. {
  205. unsigned long i, pfn, end_pfn, nr_pages;
  206. int node = pgdat->node_id;
  207. struct page *page;
  208. struct zone *zone;
  209. nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
  210. page = virt_to_page(pgdat);
  211. for (i = 0; i < nr_pages; i++, page++)
  212. get_page_bootmem(node, page, NODE_INFO);
  213. zone = &pgdat->node_zones[0];
  214. for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
  215. if (zone_is_initialized(zone)) {
  216. nr_pages = zone->wait_table_hash_nr_entries
  217. * sizeof(wait_queue_head_t);
  218. nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
  219. page = virt_to_page(zone->wait_table);
  220. for (i = 0; i < nr_pages; i++, page++)
  221. get_page_bootmem(node, page, NODE_INFO);
  222. }
  223. }
  224. pfn = pgdat->node_start_pfn;
  225. end_pfn = pgdat_end_pfn(pgdat);
  226. /* register section info */
  227. for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  228. /*
  229. * Some platforms can assign the same pfn to multiple nodes - on
  230. * node0 as well as nodeN. To avoid registering a pfn against
  231. * multiple nodes we check that this pfn does not already
  232. * reside in some other nodes.
  233. */
  234. if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node))
  235. register_page_bootmem_info_section(pfn);
  236. }
  237. }
  238. #endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */
  239. static void __meminit grow_zone_span(struct zone *zone, unsigned long start_pfn,
  240. unsigned long end_pfn)
  241. {
  242. unsigned long old_zone_end_pfn;
  243. zone_span_writelock(zone);
  244. old_zone_end_pfn = zone_end_pfn(zone);
  245. if (zone_is_empty(zone) || start_pfn < zone->zone_start_pfn)
  246. zone->zone_start_pfn = start_pfn;
  247. zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
  248. zone->zone_start_pfn;
  249. zone_span_writeunlock(zone);
  250. }
  251. static void resize_zone(struct zone *zone, unsigned long start_pfn,
  252. unsigned long end_pfn)
  253. {
  254. zone_span_writelock(zone);
  255. if (end_pfn - start_pfn) {
  256. zone->zone_start_pfn = start_pfn;
  257. zone->spanned_pages = end_pfn - start_pfn;
  258. } else {
  259. /*
  260. * make it consist as free_area_init_core(),
  261. * if spanned_pages = 0, then keep start_pfn = 0
  262. */
  263. zone->zone_start_pfn = 0;
  264. zone->spanned_pages = 0;
  265. }
  266. zone_span_writeunlock(zone);
  267. }
  268. static void fix_zone_id(struct zone *zone, unsigned long start_pfn,
  269. unsigned long end_pfn)
  270. {
  271. enum zone_type zid = zone_idx(zone);
  272. int nid = zone->zone_pgdat->node_id;
  273. unsigned long pfn;
  274. for (pfn = start_pfn; pfn < end_pfn; pfn++)
  275. set_page_links(pfn_to_page(pfn), zid, nid, pfn);
  276. }
  277. /* Can fail with -ENOMEM from allocating a wait table with vmalloc() or
  278. * alloc_bootmem_node_nopanic()/memblock_virt_alloc_node_nopanic() */
  279. static int __ref ensure_zone_is_initialized(struct zone *zone,
  280. unsigned long start_pfn, unsigned long num_pages)
  281. {
  282. if (!zone_is_initialized(zone))
  283. return init_currently_empty_zone(zone, start_pfn, num_pages);
  284. return 0;
  285. }
  286. static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2,
  287. unsigned long start_pfn, unsigned long end_pfn)
  288. {
  289. int ret;
  290. unsigned long flags;
  291. unsigned long z1_start_pfn;
  292. ret = ensure_zone_is_initialized(z1, start_pfn, end_pfn - start_pfn);
  293. if (ret)
  294. return ret;
  295. pgdat_resize_lock(z1->zone_pgdat, &flags);
  296. /* can't move pfns which are higher than @z2 */
  297. if (end_pfn > zone_end_pfn(z2))
  298. goto out_fail;
  299. /* the move out part must be at the left most of @z2 */
  300. if (start_pfn > z2->zone_start_pfn)
  301. goto out_fail;
  302. /* must included/overlap */
  303. if (end_pfn <= z2->zone_start_pfn)
  304. goto out_fail;
  305. /* use start_pfn for z1's start_pfn if z1 is empty */
  306. if (!zone_is_empty(z1))
  307. z1_start_pfn = z1->zone_start_pfn;
  308. else
  309. z1_start_pfn = start_pfn;
  310. resize_zone(z1, z1_start_pfn, end_pfn);
  311. resize_zone(z2, end_pfn, zone_end_pfn(z2));
  312. pgdat_resize_unlock(z1->zone_pgdat, &flags);
  313. fix_zone_id(z1, start_pfn, end_pfn);
  314. return 0;
  315. out_fail:
  316. pgdat_resize_unlock(z1->zone_pgdat, &flags);
  317. return -1;
  318. }
  319. static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2,
  320. unsigned long start_pfn, unsigned long end_pfn)
  321. {
  322. int ret;
  323. unsigned long flags;
  324. unsigned long z2_end_pfn;
  325. ret = ensure_zone_is_initialized(z2, start_pfn, end_pfn - start_pfn);
  326. if (ret)
  327. return ret;
  328. pgdat_resize_lock(z1->zone_pgdat, &flags);
  329. /* can't move pfns which are lower than @z1 */
  330. if (z1->zone_start_pfn > start_pfn)
  331. goto out_fail;
  332. /* the move out part mast at the right most of @z1 */
  333. if (zone_end_pfn(z1) > end_pfn)
  334. goto out_fail;
  335. /* must included/overlap */
  336. if (start_pfn >= zone_end_pfn(z1))
  337. goto out_fail;
  338. /* use end_pfn for z2's end_pfn if z2 is empty */
  339. if (!zone_is_empty(z2))
  340. z2_end_pfn = zone_end_pfn(z2);
  341. else
  342. z2_end_pfn = end_pfn;
  343. resize_zone(z1, z1->zone_start_pfn, start_pfn);
  344. resize_zone(z2, start_pfn, z2_end_pfn);
  345. pgdat_resize_unlock(z1->zone_pgdat, &flags);
  346. fix_zone_id(z2, start_pfn, end_pfn);
  347. return 0;
  348. out_fail:
  349. pgdat_resize_unlock(z1->zone_pgdat, &flags);
  350. return -1;
  351. }
  352. static void __meminit grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
  353. unsigned long end_pfn)
  354. {
  355. unsigned long old_pgdat_end_pfn = pgdat_end_pfn(pgdat);
  356. if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn)
  357. pgdat->node_start_pfn = start_pfn;
  358. pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
  359. pgdat->node_start_pfn;
  360. }
  361. static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
  362. {
  363. struct pglist_data *pgdat = zone->zone_pgdat;
  364. int nr_pages = PAGES_PER_SECTION;
  365. int nid = pgdat->node_id;
  366. int zone_type;
  367. unsigned long flags, pfn;
  368. int ret;
  369. zone_type = zone - pgdat->node_zones;
  370. ret = ensure_zone_is_initialized(zone, phys_start_pfn, nr_pages);
  371. if (ret)
  372. return ret;
  373. pgdat_resize_lock(zone->zone_pgdat, &flags);
  374. grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
  375. grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
  376. phys_start_pfn + nr_pages);
  377. pgdat_resize_unlock(zone->zone_pgdat, &flags);
  378. memmap_init_zone(nr_pages, nid, zone_type,
  379. phys_start_pfn, MEMMAP_HOTPLUG);
  380. /* online_page_range is called later and expects pages reserved */
  381. for (pfn = phys_start_pfn; pfn < phys_start_pfn + nr_pages; pfn++) {
  382. if (!pfn_valid(pfn))
  383. continue;
  384. SetPageReserved(pfn_to_page(pfn));
  385. }
  386. return 0;
  387. }
  388. static int __meminit __add_section(int nid, struct zone *zone,
  389. unsigned long phys_start_pfn)
  390. {
  391. int ret;
  392. if (pfn_valid(phys_start_pfn))
  393. return -EEXIST;
  394. ret = sparse_add_one_section(zone, phys_start_pfn);
  395. if (ret < 0)
  396. return ret;
  397. ret = __add_zone(zone, phys_start_pfn);
  398. if (ret < 0)
  399. return ret;
  400. return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
  401. }
  402. /*
  403. * Reasonably generic function for adding memory. It is
  404. * expected that archs that support memory hotplug will
  405. * call this function after deciding the zone to which to
  406. * add the new pages.
  407. */
  408. int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
  409. unsigned long nr_pages)
  410. {
  411. unsigned long i;
  412. int err = 0;
  413. int start_sec, end_sec;
  414. /* during initialize mem_map, align hot-added range to section */
  415. start_sec = pfn_to_section_nr(phys_start_pfn);
  416. end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
  417. for (i = start_sec; i <= end_sec; i++) {
  418. err = __add_section(nid, zone, section_nr_to_pfn(i));
  419. /*
  420. * EEXIST is finally dealt with by ioresource collision
  421. * check. see add_memory() => register_memory_resource()
  422. * Warning will be printed if there is collision.
  423. */
  424. if (err && (err != -EEXIST))
  425. break;
  426. err = 0;
  427. }
  428. vmemmap_populate_print_last();
  429. return err;
  430. }
  431. EXPORT_SYMBOL_GPL(__add_pages);
  432. #ifdef CONFIG_MEMORY_HOTREMOVE
  433. /* find the smallest valid pfn in the range [start_pfn, end_pfn) */
  434. static int find_smallest_section_pfn(int nid, struct zone *zone,
  435. unsigned long start_pfn,
  436. unsigned long end_pfn)
  437. {
  438. struct mem_section *ms;
  439. for (; start_pfn < end_pfn; start_pfn += PAGES_PER_SECTION) {
  440. ms = __pfn_to_section(start_pfn);
  441. if (unlikely(!valid_section(ms)))
  442. continue;
  443. if (unlikely(pfn_to_nid(start_pfn) != nid))
  444. continue;
  445. if (zone && zone != page_zone(pfn_to_page(start_pfn)))
  446. continue;
  447. return start_pfn;
  448. }
  449. return 0;
  450. }
  451. /* find the biggest valid pfn in the range [start_pfn, end_pfn). */
  452. static int find_biggest_section_pfn(int nid, struct zone *zone,
  453. unsigned long start_pfn,
  454. unsigned long end_pfn)
  455. {
  456. struct mem_section *ms;
  457. unsigned long pfn;
  458. /* pfn is the end pfn of a memory section. */
  459. pfn = end_pfn - 1;
  460. for (; pfn >= start_pfn; pfn -= PAGES_PER_SECTION) {
  461. ms = __pfn_to_section(pfn);
  462. if (unlikely(!valid_section(ms)))
  463. continue;
  464. if (unlikely(pfn_to_nid(pfn) != nid))
  465. continue;
  466. if (zone && zone != page_zone(pfn_to_page(pfn)))
  467. continue;
  468. return pfn;
  469. }
  470. return 0;
  471. }
  472. static void shrink_zone_span(struct zone *zone, unsigned long start_pfn,
  473. unsigned long end_pfn)
  474. {
  475. unsigned long zone_start_pfn = zone->zone_start_pfn;
  476. unsigned long z = zone_end_pfn(zone); /* zone_end_pfn namespace clash */
  477. unsigned long zone_end_pfn = z;
  478. unsigned long pfn;
  479. struct mem_section *ms;
  480. int nid = zone_to_nid(zone);
  481. zone_span_writelock(zone);
  482. if (zone_start_pfn == start_pfn) {
  483. /*
  484. * If the section is smallest section in the zone, it need
  485. * shrink zone->zone_start_pfn and zone->zone_spanned_pages.
  486. * In this case, we find second smallest valid mem_section
  487. * for shrinking zone.
  488. */
  489. pfn = find_smallest_section_pfn(nid, zone, end_pfn,
  490. zone_end_pfn);
  491. if (pfn) {
  492. zone->zone_start_pfn = pfn;
  493. zone->spanned_pages = zone_end_pfn - pfn;
  494. }
  495. } else if (zone_end_pfn == end_pfn) {
  496. /*
  497. * If the section is biggest section in the zone, it need
  498. * shrink zone->spanned_pages.
  499. * In this case, we find second biggest valid mem_section for
  500. * shrinking zone.
  501. */
  502. pfn = find_biggest_section_pfn(nid, zone, zone_start_pfn,
  503. start_pfn);
  504. if (pfn)
  505. zone->spanned_pages = pfn - zone_start_pfn + 1;
  506. }
  507. /*
  508. * The section is not biggest or smallest mem_section in the zone, it
  509. * only creates a hole in the zone. So in this case, we need not
  510. * change the zone. But perhaps, the zone has only hole data. Thus
  511. * it check the zone has only hole or not.
  512. */
  513. pfn = zone_start_pfn;
  514. for (; pfn < zone_end_pfn; pfn += PAGES_PER_SECTION) {
  515. ms = __pfn_to_section(pfn);
  516. if (unlikely(!valid_section(ms)))
  517. continue;
  518. if (page_zone(pfn_to_page(pfn)) != zone)
  519. continue;
  520. /* If the section is current section, it continues the loop */
  521. if (start_pfn == pfn)
  522. continue;
  523. /* If we find valid section, we have nothing to do */
  524. zone_span_writeunlock(zone);
  525. return;
  526. }
  527. /* The zone has no valid section */
  528. zone->zone_start_pfn = 0;
  529. zone->spanned_pages = 0;
  530. zone_span_writeunlock(zone);
  531. }
  532. static void shrink_pgdat_span(struct pglist_data *pgdat,
  533. unsigned long start_pfn, unsigned long end_pfn)
  534. {
  535. unsigned long pgdat_start_pfn = pgdat->node_start_pfn;
  536. unsigned long p = pgdat_end_pfn(pgdat); /* pgdat_end_pfn namespace clash */
  537. unsigned long pgdat_end_pfn = p;
  538. unsigned long pfn;
  539. struct mem_section *ms;
  540. int nid = pgdat->node_id;
  541. if (pgdat_start_pfn == start_pfn) {
  542. /*
  543. * If the section is smallest section in the pgdat, it need
  544. * shrink pgdat->node_start_pfn and pgdat->node_spanned_pages.
  545. * In this case, we find second smallest valid mem_section
  546. * for shrinking zone.
  547. */
  548. pfn = find_smallest_section_pfn(nid, NULL, end_pfn,
  549. pgdat_end_pfn);
  550. if (pfn) {
  551. pgdat->node_start_pfn = pfn;
  552. pgdat->node_spanned_pages = pgdat_end_pfn - pfn;
  553. }
  554. } else if (pgdat_end_pfn == end_pfn) {
  555. /*
  556. * If the section is biggest section in the pgdat, it need
  557. * shrink pgdat->node_spanned_pages.
  558. * In this case, we find second biggest valid mem_section for
  559. * shrinking zone.
  560. */
  561. pfn = find_biggest_section_pfn(nid, NULL, pgdat_start_pfn,
  562. start_pfn);
  563. if (pfn)
  564. pgdat->node_spanned_pages = pfn - pgdat_start_pfn + 1;
  565. }
  566. /*
  567. * If the section is not biggest or smallest mem_section in the pgdat,
  568. * it only creates a hole in the pgdat. So in this case, we need not
  569. * change the pgdat.
  570. * But perhaps, the pgdat has only hole data. Thus it check the pgdat
  571. * has only hole or not.
  572. */
  573. pfn = pgdat_start_pfn;
  574. for (; pfn < pgdat_end_pfn; pfn += PAGES_PER_SECTION) {
  575. ms = __pfn_to_section(pfn);
  576. if (unlikely(!valid_section(ms)))
  577. continue;
  578. if (pfn_to_nid(pfn) != nid)
  579. continue;
  580. /* If the section is current section, it continues the loop */
  581. if (start_pfn == pfn)
  582. continue;
  583. /* If we find valid section, we have nothing to do */
  584. return;
  585. }
  586. /* The pgdat has no valid section */
  587. pgdat->node_start_pfn = 0;
  588. pgdat->node_spanned_pages = 0;
  589. }
  590. static void __remove_zone(struct zone *zone, unsigned long start_pfn)
  591. {
  592. struct pglist_data *pgdat = zone->zone_pgdat;
  593. int nr_pages = PAGES_PER_SECTION;
  594. int zone_type;
  595. unsigned long flags;
  596. zone_type = zone - pgdat->node_zones;
  597. pgdat_resize_lock(zone->zone_pgdat, &flags);
  598. shrink_zone_span(zone, start_pfn, start_pfn + nr_pages);
  599. shrink_pgdat_span(pgdat, start_pfn, start_pfn + nr_pages);
  600. pgdat_resize_unlock(zone->zone_pgdat, &flags);
  601. }
  602. static int __remove_section(struct zone *zone, struct mem_section *ms)
  603. {
  604. unsigned long start_pfn;
  605. int scn_nr;
  606. int ret = -EINVAL;
  607. if (!valid_section(ms))
  608. return ret;
  609. ret = unregister_memory_section(ms);
  610. if (ret)
  611. return ret;
  612. scn_nr = __section_nr(ms);
  613. start_pfn = section_nr_to_pfn(scn_nr);
  614. __remove_zone(zone, start_pfn);
  615. sparse_remove_one_section(zone, ms);
  616. return 0;
  617. }
  618. /**
  619. * __remove_pages() - remove sections of pages from a zone
  620. * @zone: zone from which pages need to be removed
  621. * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
  622. * @nr_pages: number of pages to remove (must be multiple of section size)
  623. *
  624. * Generic helper function to remove section mappings and sysfs entries
  625. * for the section of the memory we are removing. Caller needs to make
  626. * sure that pages are marked reserved and zones are adjust properly by
  627. * calling offline_pages().
  628. */
  629. int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
  630. unsigned long nr_pages)
  631. {
  632. unsigned long i;
  633. int sections_to_remove;
  634. resource_size_t start, size;
  635. int ret = 0;
  636. /*
  637. * We can only remove entire sections
  638. */
  639. BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
  640. BUG_ON(nr_pages % PAGES_PER_SECTION);
  641. start = phys_start_pfn << PAGE_SHIFT;
  642. size = nr_pages * PAGE_SIZE;
  643. /* in the ZONE_DEVICE case device driver owns the memory region */
  644. if (!is_dev_zone(zone))
  645. ret = release_mem_region_adjustable(&iomem_resource, start, size);
  646. if (ret) {
  647. resource_size_t endres = start + size - 1;
  648. pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
  649. &start, &endres, ret);
  650. }
  651. sections_to_remove = nr_pages / PAGES_PER_SECTION;
  652. for (i = 0; i < sections_to_remove; i++) {
  653. unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
  654. ret = __remove_section(zone, __pfn_to_section(pfn));
  655. if (ret)
  656. break;
  657. }
  658. return ret;
  659. }
  660. EXPORT_SYMBOL_GPL(__remove_pages);
  661. #endif /* CONFIG_MEMORY_HOTREMOVE */
  662. int set_online_page_callback(online_page_callback_t callback)
  663. {
  664. int rc = -EINVAL;
  665. get_online_mems();
  666. mutex_lock(&online_page_callback_lock);
  667. if (online_page_callback == generic_online_page) {
  668. online_page_callback = callback;
  669. rc = 0;
  670. }
  671. mutex_unlock(&online_page_callback_lock);
  672. put_online_mems();
  673. return rc;
  674. }
  675. EXPORT_SYMBOL_GPL(set_online_page_callback);
  676. int restore_online_page_callback(online_page_callback_t callback)
  677. {
  678. int rc = -EINVAL;
  679. get_online_mems();
  680. mutex_lock(&online_page_callback_lock);
  681. if (online_page_callback == callback) {
  682. online_page_callback = generic_online_page;
  683. rc = 0;
  684. }
  685. mutex_unlock(&online_page_callback_lock);
  686. put_online_mems();
  687. return rc;
  688. }
  689. EXPORT_SYMBOL_GPL(restore_online_page_callback);
  690. void __online_page_set_limits(struct page *page)
  691. {
  692. }
  693. EXPORT_SYMBOL_GPL(__online_page_set_limits);
  694. void __online_page_increment_counters(struct page *page)
  695. {
  696. adjust_managed_page_count(page, 1);
  697. }
  698. EXPORT_SYMBOL_GPL(__online_page_increment_counters);
  699. void __online_page_free(struct page *page)
  700. {
  701. __free_reserved_page(page);
  702. }
  703. EXPORT_SYMBOL_GPL(__online_page_free);
  704. static void generic_online_page(struct page *page)
  705. {
  706. __online_page_set_limits(page);
  707. __online_page_increment_counters(page);
  708. __online_page_free(page);
  709. }
  710. static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
  711. void *arg)
  712. {
  713. unsigned long i;
  714. unsigned long onlined_pages = *(unsigned long *)arg;
  715. struct page *page;
  716. if (PageReserved(pfn_to_page(start_pfn)))
  717. for (i = 0; i < nr_pages; i++) {
  718. page = pfn_to_page(start_pfn + i);
  719. (*online_page_callback)(page);
  720. onlined_pages++;
  721. }
  722. *(unsigned long *)arg = onlined_pages;
  723. return 0;
  724. }
  725. #ifdef CONFIG_MOVABLE_NODE
  726. /*
  727. * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have
  728. * normal memory.
  729. */
  730. static bool can_online_high_movable(struct zone *zone)
  731. {
  732. return true;
  733. }
  734. #else /* CONFIG_MOVABLE_NODE */
  735. /* ensure every online node has NORMAL memory */
  736. static bool can_online_high_movable(struct zone *zone)
  737. {
  738. return node_state(zone_to_nid(zone), N_NORMAL_MEMORY);
  739. }
  740. #endif /* CONFIG_MOVABLE_NODE */
  741. /* check which state of node_states will be changed when online memory */
  742. static void node_states_check_changes_online(unsigned long nr_pages,
  743. struct zone *zone, struct memory_notify *arg)
  744. {
  745. int nid = zone_to_nid(zone);
  746. enum zone_type zone_last = ZONE_NORMAL;
  747. /*
  748. * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
  749. * contains nodes which have zones of 0...ZONE_NORMAL,
  750. * set zone_last to ZONE_NORMAL.
  751. *
  752. * If we don't have HIGHMEM nor movable node,
  753. * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
  754. * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
  755. */
  756. if (N_MEMORY == N_NORMAL_MEMORY)
  757. zone_last = ZONE_MOVABLE;
  758. /*
  759. * if the memory to be online is in a zone of 0...zone_last, and
  760. * the zones of 0...zone_last don't have memory before online, we will
  761. * need to set the node to node_states[N_NORMAL_MEMORY] after
  762. * the memory is online.
  763. */
  764. if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY))
  765. arg->status_change_nid_normal = nid;
  766. else
  767. arg->status_change_nid_normal = -1;
  768. #ifdef CONFIG_HIGHMEM
  769. /*
  770. * If we have movable node, node_states[N_HIGH_MEMORY]
  771. * contains nodes which have zones of 0...ZONE_HIGHMEM,
  772. * set zone_last to ZONE_HIGHMEM.
  773. *
  774. * If we don't have movable node, node_states[N_NORMAL_MEMORY]
  775. * contains nodes which have zones of 0...ZONE_MOVABLE,
  776. * set zone_last to ZONE_MOVABLE.
  777. */
  778. zone_last = ZONE_HIGHMEM;
  779. if (N_MEMORY == N_HIGH_MEMORY)
  780. zone_last = ZONE_MOVABLE;
  781. if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY))
  782. arg->status_change_nid_high = nid;
  783. else
  784. arg->status_change_nid_high = -1;
  785. #else
  786. arg->status_change_nid_high = arg->status_change_nid_normal;
  787. #endif
  788. /*
  789. * if the node don't have memory befor online, we will need to
  790. * set the node to node_states[N_MEMORY] after the memory
  791. * is online.
  792. */
  793. if (!node_state(nid, N_MEMORY))
  794. arg->status_change_nid = nid;
  795. else
  796. arg->status_change_nid = -1;
  797. }
  798. static void node_states_set_node(int node, struct memory_notify *arg)
  799. {
  800. if (arg->status_change_nid_normal >= 0)
  801. node_set_state(node, N_NORMAL_MEMORY);
  802. if (arg->status_change_nid_high >= 0)
  803. node_set_state(node, N_HIGH_MEMORY);
  804. node_set_state(node, N_MEMORY);
  805. }
  806. /* Must be protected by mem_hotplug_begin() */
  807. int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
  808. {
  809. unsigned long flags;
  810. unsigned long onlined_pages = 0;
  811. struct zone *zone;
  812. int need_zonelists_rebuild = 0;
  813. int nid;
  814. int ret;
  815. struct memory_notify arg;
  816. /*
  817. * This doesn't need a lock to do pfn_to_page().
  818. * The section can't be removed here because of the
  819. * memory_block->state_mutex.
  820. */
  821. zone = page_zone(pfn_to_page(pfn));
  822. if ((zone_idx(zone) > ZONE_NORMAL ||
  823. online_type == MMOP_ONLINE_MOVABLE) &&
  824. !can_online_high_movable(zone))
  825. return -EINVAL;
  826. if (online_type == MMOP_ONLINE_KERNEL &&
  827. zone_idx(zone) == ZONE_MOVABLE) {
  828. if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages))
  829. return -EINVAL;
  830. }
  831. if (online_type == MMOP_ONLINE_MOVABLE &&
  832. zone_idx(zone) == ZONE_MOVABLE - 1) {
  833. if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages))
  834. return -EINVAL;
  835. }
  836. /* Previous code may changed the zone of the pfn range */
  837. zone = page_zone(pfn_to_page(pfn));
  838. arg.start_pfn = pfn;
  839. arg.nr_pages = nr_pages;
  840. node_states_check_changes_online(nr_pages, zone, &arg);
  841. nid = pfn_to_nid(pfn);
  842. ret = memory_notify(MEM_GOING_ONLINE, &arg);
  843. ret = notifier_to_errno(ret);
  844. if (ret) {
  845. memory_notify(MEM_CANCEL_ONLINE, &arg);
  846. return ret;
  847. }
  848. /*
  849. * If this zone is not populated, then it is not in zonelist.
  850. * This means the page allocator ignores this zone.
  851. * So, zonelist must be updated after online.
  852. */
  853. mutex_lock(&zonelists_mutex);
  854. if (!populated_zone(zone)) {
  855. need_zonelists_rebuild = 1;
  856. build_all_zonelists(NULL, zone);
  857. }
  858. ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
  859. online_pages_range);
  860. if (ret) {
  861. if (need_zonelists_rebuild)
  862. zone_pcp_reset(zone);
  863. mutex_unlock(&zonelists_mutex);
  864. printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
  865. (unsigned long long) pfn << PAGE_SHIFT,
  866. (((unsigned long long) pfn + nr_pages)
  867. << PAGE_SHIFT) - 1);
  868. memory_notify(MEM_CANCEL_ONLINE, &arg);
  869. return ret;
  870. }
  871. zone->present_pages += onlined_pages;
  872. pgdat_resize_lock(zone->zone_pgdat, &flags);
  873. zone->zone_pgdat->node_present_pages += onlined_pages;
  874. pgdat_resize_unlock(zone->zone_pgdat, &flags);
  875. if (onlined_pages) {
  876. node_states_set_node(zone_to_nid(zone), &arg);
  877. if (need_zonelists_rebuild)
  878. build_all_zonelists(NULL, NULL);
  879. else
  880. zone_pcp_update(zone);
  881. }
  882. mutex_unlock(&zonelists_mutex);
  883. init_per_zone_wmark_min();
  884. if (onlined_pages)
  885. kswapd_run(zone_to_nid(zone));
  886. vm_total_pages = nr_free_pagecache_pages();
  887. writeback_set_ratelimit();
  888. if (onlined_pages)
  889. memory_notify(MEM_ONLINE, &arg);
  890. return 0;
  891. }
  892. #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
  893. static void reset_node_present_pages(pg_data_t *pgdat)
  894. {
  895. struct zone *z;
  896. for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
  897. z->present_pages = 0;
  898. pgdat->node_present_pages = 0;
  899. }
  900. /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
  901. static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
  902. {
  903. struct pglist_data *pgdat;
  904. unsigned long zones_size[MAX_NR_ZONES] = {0};
  905. unsigned long zholes_size[MAX_NR_ZONES] = {0};
  906. unsigned long start_pfn = PFN_DOWN(start);
  907. pgdat = NODE_DATA(nid);
  908. if (!pgdat) {
  909. pgdat = arch_alloc_nodedata(nid);
  910. if (!pgdat)
  911. return NULL;
  912. arch_refresh_nodedata(nid, pgdat);
  913. } else {
  914. /* Reset the nr_zones and classzone_idx to 0 before reuse */
  915. pgdat->nr_zones = 0;
  916. pgdat->classzone_idx = 0;
  917. }
  918. /* we can use NODE_DATA(nid) from here */
  919. /* init node's zones as empty zones, we don't have any present pages.*/
  920. free_area_init_node(nid, zones_size, start_pfn, zholes_size);
  921. /*
  922. * The node we allocated has no zone fallback lists. For avoiding
  923. * to access not-initialized zonelist, build here.
  924. */
  925. mutex_lock(&zonelists_mutex);
  926. build_all_zonelists(pgdat, NULL);
  927. mutex_unlock(&zonelists_mutex);
  928. /*
  929. * zone->managed_pages is set to an approximate value in
  930. * free_area_init_core(), which will cause
  931. * /sys/device/system/node/nodeX/meminfo has wrong data.
  932. * So reset it to 0 before any memory is onlined.
  933. */
  934. reset_node_managed_pages(pgdat);
  935. /*
  936. * When memory is hot-added, all the memory is in offline state. So
  937. * clear all zones' present_pages because they will be updated in
  938. * online_pages() and offline_pages().
  939. */
  940. reset_node_present_pages(pgdat);
  941. return pgdat;
  942. }
  943. static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
  944. {
  945. arch_refresh_nodedata(nid, NULL);
  946. arch_free_nodedata(pgdat);
  947. return;
  948. }
  949. /**
  950. * try_online_node - online a node if offlined
  951. *
  952. * called by cpu_up() to online a node without onlined memory.
  953. */
  954. int try_online_node(int nid)
  955. {
  956. pg_data_t *pgdat;
  957. int ret;
  958. if (node_online(nid))
  959. return 0;
  960. mem_hotplug_begin();
  961. pgdat = hotadd_new_pgdat(nid, 0);
  962. if (!pgdat) {
  963. pr_err("Cannot online node %d due to NULL pgdat\n", nid);
  964. ret = -ENOMEM;
  965. goto out;
  966. }
  967. node_set_online(nid);
  968. ret = register_one_node(nid);
  969. BUG_ON(ret);
  970. if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
  971. mutex_lock(&zonelists_mutex);
  972. build_all_zonelists(NULL, NULL);
  973. mutex_unlock(&zonelists_mutex);
  974. }
  975. out:
  976. mem_hotplug_done();
  977. return ret;
  978. }
  979. static int check_hotplug_memory_range(u64 start, u64 size)
  980. {
  981. u64 start_pfn = PFN_DOWN(start);
  982. u64 nr_pages = size >> PAGE_SHIFT;
  983. /* Memory range must be aligned with section */
  984. if ((start_pfn & ~PAGE_SECTION_MASK) ||
  985. (nr_pages % PAGES_PER_SECTION) || (!nr_pages)) {
  986. pr_err("Section-unaligned hotplug range: start 0x%llx, size 0x%llx\n",
  987. (unsigned long long)start,
  988. (unsigned long long)size);
  989. return -EINVAL;
  990. }
  991. return 0;
  992. }
  993. /*
  994. * If movable zone has already been setup, newly added memory should be check.
  995. * If its address is higher than movable zone, it should be added as movable.
  996. * Without this check, movable zone may overlap with other zone.
  997. */
  998. static int should_add_memory_movable(int nid, u64 start, u64 size)
  999. {
  1000. unsigned long start_pfn = start >> PAGE_SHIFT;
  1001. pg_data_t *pgdat = NODE_DATA(nid);
  1002. struct zone *movable_zone = pgdat->node_zones + ZONE_MOVABLE;
  1003. if (zone_is_empty(movable_zone))
  1004. return 0;
  1005. if (movable_zone->zone_start_pfn <= start_pfn)
  1006. return 1;
  1007. return 0;
  1008. }
  1009. int zone_for_memory(int nid, u64 start, u64 size, int zone_default,
  1010. bool for_device)
  1011. {
  1012. #ifdef CONFIG_ZONE_DEVICE
  1013. if (for_device)
  1014. return ZONE_DEVICE;
  1015. #endif
  1016. if (should_add_memory_movable(nid, start, size))
  1017. return ZONE_MOVABLE;
  1018. return zone_default;
  1019. }
  1020. /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
  1021. int __ref add_memory_resource(int nid, struct resource *res)
  1022. {
  1023. u64 start, size;
  1024. pg_data_t *pgdat = NULL;
  1025. bool new_pgdat;
  1026. bool new_node;
  1027. int ret;
  1028. start = res->start;
  1029. size = resource_size(res);
  1030. ret = check_hotplug_memory_range(start, size);
  1031. if (ret)
  1032. return ret;
  1033. { /* Stupid hack to suppress address-never-null warning */
  1034. void *p = NODE_DATA(nid);
  1035. new_pgdat = !p;
  1036. }
  1037. mem_hotplug_begin();
  1038. /*
  1039. * Add new range to memblock so that when hotadd_new_pgdat() is called
  1040. * to allocate new pgdat, get_pfn_range_for_nid() will be able to find
  1041. * this new range and calculate total pages correctly. The range will
  1042. * be removed at hot-remove time.
  1043. */
  1044. memblock_add_node(start, size, nid);
  1045. new_node = !node_online(nid);
  1046. if (new_node) {
  1047. pgdat = hotadd_new_pgdat(nid, start);
  1048. ret = -ENOMEM;
  1049. if (!pgdat)
  1050. goto error;
  1051. }
  1052. /* call arch's memory hotadd */
  1053. ret = arch_add_memory(nid, start, size, false);
  1054. if (ret < 0)
  1055. goto error;
  1056. /* we online node here. we can't roll back from here. */
  1057. node_set_online(nid);
  1058. if (new_node) {
  1059. ret = register_one_node(nid);
  1060. /*
  1061. * If sysfs file of new node can't create, cpu on the node
  1062. * can't be hot-added. There is no rollback way now.
  1063. * So, check by BUG_ON() to catch it reluctantly..
  1064. */
  1065. BUG_ON(ret);
  1066. }
  1067. /* create new memmap entry */
  1068. firmware_map_add_hotplug(start, start + size, "System RAM");
  1069. goto out;
  1070. error:
  1071. /* rollback pgdat allocation and others */
  1072. if (new_pgdat)
  1073. rollback_node_hotadd(nid, pgdat);
  1074. memblock_remove(start, size);
  1075. out:
  1076. mem_hotplug_done();
  1077. return ret;
  1078. }
  1079. EXPORT_SYMBOL_GPL(add_memory_resource);
  1080. int __ref add_memory(int nid, u64 start, u64 size)
  1081. {
  1082. struct resource *res;
  1083. int ret;
  1084. res = register_memory_resource(start, size);
  1085. if (!res)
  1086. return -EEXIST;
  1087. ret = add_memory_resource(nid, res);
  1088. if (ret < 0)
  1089. release_memory_resource(res);
  1090. return ret;
  1091. }
  1092. EXPORT_SYMBOL_GPL(add_memory);
  1093. #ifdef CONFIG_MEMORY_HOTREMOVE
  1094. /*
  1095. * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
  1096. * set and the size of the free page is given by page_order(). Using this,
  1097. * the function determines if the pageblock contains only free pages.
  1098. * Due to buddy contraints, a free page at least the size of a pageblock will
  1099. * be located at the start of the pageblock
  1100. */
  1101. static inline int pageblock_free(struct page *page)
  1102. {
  1103. return PageBuddy(page) && page_order(page) >= pageblock_order;
  1104. }
  1105. /* Return the start of the next active pageblock after a given page */
  1106. static struct page *next_active_pageblock(struct page *page)
  1107. {
  1108. /* Ensure the starting page is pageblock-aligned */
  1109. BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
  1110. /* If the entire pageblock is free, move to the end of free page */
  1111. if (pageblock_free(page)) {
  1112. int order;
  1113. /* be careful. we don't have locks, page_order can be changed.*/
  1114. order = page_order(page);
  1115. if ((order < MAX_ORDER) && (order >= pageblock_order))
  1116. return page + (1 << order);
  1117. }
  1118. return page + pageblock_nr_pages;
  1119. }
  1120. /* Checks if this range of memory is likely to be hot-removable. */
  1121. int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
  1122. {
  1123. struct page *page = pfn_to_page(start_pfn);
  1124. struct page *end_page = page + nr_pages;
  1125. /* Check the starting page of each pageblock within the range */
  1126. for (; page < end_page; page = next_active_pageblock(page)) {
  1127. if (!is_pageblock_removable_nolock(page))
  1128. return 0;
  1129. cond_resched();
  1130. }
  1131. /* All pageblocks in the memory block are likely to be hot-removable */
  1132. return 1;
  1133. }
  1134. /*
  1135. * Confirm all pages in a range [start, end) is belongs to the same zone.
  1136. */
  1137. int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
  1138. {
  1139. unsigned long pfn, sec_end_pfn;
  1140. struct zone *zone = NULL;
  1141. struct page *page;
  1142. int i;
  1143. for (pfn = start_pfn, sec_end_pfn = SECTION_ALIGN_UP(start_pfn);
  1144. pfn < end_pfn;
  1145. pfn = sec_end_pfn + 1, sec_end_pfn += PAGES_PER_SECTION) {
  1146. /* Make sure the memory section is present first */
  1147. if (!present_section_nr(pfn_to_section_nr(pfn)))
  1148. continue;
  1149. for (; pfn < sec_end_pfn && pfn < end_pfn;
  1150. pfn += MAX_ORDER_NR_PAGES) {
  1151. i = 0;
  1152. /* This is just a CONFIG_HOLES_IN_ZONE check.*/
  1153. while ((i < MAX_ORDER_NR_PAGES) &&
  1154. !pfn_valid_within(pfn + i))
  1155. i++;
  1156. if (i == MAX_ORDER_NR_PAGES)
  1157. continue;
  1158. page = pfn_to_page(pfn + i);
  1159. if (zone && page_zone(page) != zone)
  1160. return 0;
  1161. zone = page_zone(page);
  1162. }
  1163. }
  1164. return 1;
  1165. }
  1166. /*
  1167. * Scan pfn range [start,end) to find movable/migratable pages (LRU pages
  1168. * and hugepages). We scan pfn because it's much easier than scanning over
  1169. * linked list. This function returns the pfn of the first found movable
  1170. * page if it's found, otherwise 0.
  1171. */
  1172. static unsigned long scan_movable_pages(unsigned long start, unsigned long end)
  1173. {
  1174. unsigned long pfn;
  1175. struct page *page;
  1176. for (pfn = start; pfn < end; pfn++) {
  1177. if (pfn_valid(pfn)) {
  1178. page = pfn_to_page(pfn);
  1179. if (PageLRU(page))
  1180. return pfn;
  1181. if (PageHuge(page)) {
  1182. if (page_huge_active(page))
  1183. return pfn;
  1184. else
  1185. pfn = round_up(pfn + 1,
  1186. 1 << compound_order(page)) - 1;
  1187. }
  1188. }
  1189. }
  1190. return 0;
  1191. }
  1192. #define NR_OFFLINE_AT_ONCE_PAGES (256)
  1193. static int
  1194. do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
  1195. {
  1196. unsigned long pfn;
  1197. struct page *page;
  1198. int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
  1199. int not_managed = 0;
  1200. int ret = 0;
  1201. LIST_HEAD(source);
  1202. for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
  1203. if (!pfn_valid(pfn))
  1204. continue;
  1205. page = pfn_to_page(pfn);
  1206. if (PageHuge(page)) {
  1207. struct page *head = compound_head(page);
  1208. pfn = page_to_pfn(head) + (1<<compound_order(head)) - 1;
  1209. if (compound_order(head) > PFN_SECTION_SHIFT) {
  1210. ret = -EBUSY;
  1211. break;
  1212. }
  1213. if (isolate_huge_page(page, &source))
  1214. move_pages -= 1 << compound_order(head);
  1215. continue;
  1216. }
  1217. if (!get_page_unless_zero(page))
  1218. continue;
  1219. /*
  1220. * We can skip free pages. And we can only deal with pages on
  1221. * LRU.
  1222. */
  1223. ret = isolate_lru_page(page);
  1224. if (!ret) { /* Success */
  1225. put_page(page);
  1226. list_add_tail(&page->lru, &source);
  1227. move_pages--;
  1228. inc_zone_page_state(page, NR_ISOLATED_ANON +
  1229. page_is_file_cache(page));
  1230. } else {
  1231. #ifdef CONFIG_DEBUG_VM
  1232. printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
  1233. pfn);
  1234. dump_page(page, "failed to remove from LRU");
  1235. #endif
  1236. put_page(page);
  1237. /* Because we don't have big zone->lock. we should
  1238. check this again here. */
  1239. if (page_count(page)) {
  1240. not_managed++;
  1241. ret = -EBUSY;
  1242. break;
  1243. }
  1244. }
  1245. }
  1246. if (!list_empty(&source)) {
  1247. if (not_managed) {
  1248. putback_movable_pages(&source);
  1249. goto out;
  1250. }
  1251. /*
  1252. * alloc_migrate_target should be improooooved!!
  1253. * migrate_pages returns # of failed pages.
  1254. */
  1255. ret = migrate_pages(&source, alloc_migrate_target, NULL, 0,
  1256. MIGRATE_SYNC, MR_MEMORY_HOTPLUG);
  1257. if (ret)
  1258. putback_movable_pages(&source);
  1259. }
  1260. out:
  1261. return ret;
  1262. }
  1263. /*
  1264. * remove from free_area[] and mark all as Reserved.
  1265. */
  1266. static int
  1267. offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
  1268. void *data)
  1269. {
  1270. __offline_isolated_pages(start, start + nr_pages);
  1271. return 0;
  1272. }
  1273. static void
  1274. offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
  1275. {
  1276. walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
  1277. offline_isolated_pages_cb);
  1278. }
  1279. /*
  1280. * Check all pages in range, recoreded as memory resource, are isolated.
  1281. */
  1282. static int
  1283. check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
  1284. void *data)
  1285. {
  1286. int ret;
  1287. long offlined = *(long *)data;
  1288. ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true);
  1289. offlined = nr_pages;
  1290. if (!ret)
  1291. *(long *)data += offlined;
  1292. return ret;
  1293. }
  1294. static long
  1295. check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
  1296. {
  1297. long offlined = 0;
  1298. int ret;
  1299. ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
  1300. check_pages_isolated_cb);
  1301. if (ret < 0)
  1302. offlined = (long)ret;
  1303. return offlined;
  1304. }
  1305. #ifdef CONFIG_MOVABLE_NODE
  1306. /*
  1307. * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have
  1308. * normal memory.
  1309. */
  1310. static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
  1311. {
  1312. return true;
  1313. }
  1314. #else /* CONFIG_MOVABLE_NODE */
  1315. /* ensure the node has NORMAL memory if it is still online */
  1316. static bool can_offline_normal(struct zone *zone, unsigned long nr_pages)
  1317. {
  1318. struct pglist_data *pgdat = zone->zone_pgdat;
  1319. unsigned long present_pages = 0;
  1320. enum zone_type zt;
  1321. for (zt = 0; zt <= ZONE_NORMAL; zt++)
  1322. present_pages += pgdat->node_zones[zt].present_pages;
  1323. if (present_pages > nr_pages)
  1324. return true;
  1325. present_pages = 0;
  1326. for (; zt <= ZONE_MOVABLE; zt++)
  1327. present_pages += pgdat->node_zones[zt].present_pages;
  1328. /*
  1329. * we can't offline the last normal memory until all
  1330. * higher memory is offlined.
  1331. */
  1332. return present_pages == 0;
  1333. }
  1334. #endif /* CONFIG_MOVABLE_NODE */
  1335. static int __init cmdline_parse_movable_node(char *p)
  1336. {
  1337. #ifdef CONFIG_MOVABLE_NODE
  1338. /*
  1339. * Memory used by the kernel cannot be hot-removed because Linux
  1340. * cannot migrate the kernel pages. When memory hotplug is
  1341. * enabled, we should prevent memblock from allocating memory
  1342. * for the kernel.
  1343. *
  1344. * ACPI SRAT records all hotpluggable memory ranges. But before
  1345. * SRAT is parsed, we don't know about it.
  1346. *
  1347. * The kernel image is loaded into memory at very early time. We
  1348. * cannot prevent this anyway. So on NUMA system, we set any
  1349. * node the kernel resides in as un-hotpluggable.
  1350. *
  1351. * Since on modern servers, one node could have double-digit
  1352. * gigabytes memory, we can assume the memory around the kernel
  1353. * image is also un-hotpluggable. So before SRAT is parsed, just
  1354. * allocate memory near the kernel image to try the best to keep
  1355. * the kernel away from hotpluggable memory.
  1356. */
  1357. memblock_set_bottom_up(true);
  1358. movable_node_enabled = true;
  1359. #else
  1360. pr_warn("movable_node option not supported\n");
  1361. #endif
  1362. return 0;
  1363. }
  1364. early_param("movable_node", cmdline_parse_movable_node);
  1365. /* check which state of node_states will be changed when offline memory */
  1366. static void node_states_check_changes_offline(unsigned long nr_pages,
  1367. struct zone *zone, struct memory_notify *arg)
  1368. {
  1369. struct pglist_data *pgdat = zone->zone_pgdat;
  1370. unsigned long present_pages = 0;
  1371. enum zone_type zt, zone_last = ZONE_NORMAL;
  1372. /*
  1373. * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY]
  1374. * contains nodes which have zones of 0...ZONE_NORMAL,
  1375. * set zone_last to ZONE_NORMAL.
  1376. *
  1377. * If we don't have HIGHMEM nor movable node,
  1378. * node_states[N_NORMAL_MEMORY] contains nodes which have zones of
  1379. * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE.
  1380. */
  1381. if (N_MEMORY == N_NORMAL_MEMORY)
  1382. zone_last = ZONE_MOVABLE;
  1383. /*
  1384. * check whether node_states[N_NORMAL_MEMORY] will be changed.
  1385. * If the memory to be offline is in a zone of 0...zone_last,
  1386. * and it is the last present memory, 0...zone_last will
  1387. * become empty after offline , thus we can determind we will
  1388. * need to clear the node from node_states[N_NORMAL_MEMORY].
  1389. */
  1390. for (zt = 0; zt <= zone_last; zt++)
  1391. present_pages += pgdat->node_zones[zt].present_pages;
  1392. if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
  1393. arg->status_change_nid_normal = zone_to_nid(zone);
  1394. else
  1395. arg->status_change_nid_normal = -1;
  1396. #ifdef CONFIG_HIGHMEM
  1397. /*
  1398. * If we have movable node, node_states[N_HIGH_MEMORY]
  1399. * contains nodes which have zones of 0...ZONE_HIGHMEM,
  1400. * set zone_last to ZONE_HIGHMEM.
  1401. *
  1402. * If we don't have movable node, node_states[N_NORMAL_MEMORY]
  1403. * contains nodes which have zones of 0...ZONE_MOVABLE,
  1404. * set zone_last to ZONE_MOVABLE.
  1405. */
  1406. zone_last = ZONE_HIGHMEM;
  1407. if (N_MEMORY == N_HIGH_MEMORY)
  1408. zone_last = ZONE_MOVABLE;
  1409. for (; zt <= zone_last; zt++)
  1410. present_pages += pgdat->node_zones[zt].present_pages;
  1411. if (zone_idx(zone) <= zone_last && nr_pages >= present_pages)
  1412. arg->status_change_nid_high = zone_to_nid(zone);
  1413. else
  1414. arg->status_change_nid_high = -1;
  1415. #else
  1416. arg->status_change_nid_high = arg->status_change_nid_normal;
  1417. #endif
  1418. /*
  1419. * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE
  1420. */
  1421. zone_last = ZONE_MOVABLE;
  1422. /*
  1423. * check whether node_states[N_HIGH_MEMORY] will be changed
  1424. * If we try to offline the last present @nr_pages from the node,
  1425. * we can determind we will need to clear the node from
  1426. * node_states[N_HIGH_MEMORY].
  1427. */
  1428. for (; zt <= zone_last; zt++)
  1429. present_pages += pgdat->node_zones[zt].present_pages;
  1430. if (nr_pages >= present_pages)
  1431. arg->status_change_nid = zone_to_nid(zone);
  1432. else
  1433. arg->status_change_nid = -1;
  1434. }
  1435. static void node_states_clear_node(int node, struct memory_notify *arg)
  1436. {
  1437. if (arg->status_change_nid_normal >= 0)
  1438. node_clear_state(node, N_NORMAL_MEMORY);
  1439. if ((N_MEMORY != N_NORMAL_MEMORY) &&
  1440. (arg->status_change_nid_high >= 0))
  1441. node_clear_state(node, N_HIGH_MEMORY);
  1442. if ((N_MEMORY != N_HIGH_MEMORY) &&
  1443. (arg->status_change_nid >= 0))
  1444. node_clear_state(node, N_MEMORY);
  1445. }
  1446. static int __ref __offline_pages(unsigned long start_pfn,
  1447. unsigned long end_pfn, unsigned long timeout)
  1448. {
  1449. unsigned long pfn, nr_pages, expire;
  1450. long offlined_pages;
  1451. int ret, drain, retry_max, node;
  1452. unsigned long flags;
  1453. struct zone *zone;
  1454. struct memory_notify arg;
  1455. /* at least, alignment against pageblock is necessary */
  1456. if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
  1457. return -EINVAL;
  1458. if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
  1459. return -EINVAL;
  1460. /* This makes hotplug much easier...and readable.
  1461. we assume this for now. .*/
  1462. if (!test_pages_in_a_zone(start_pfn, end_pfn))
  1463. return -EINVAL;
  1464. zone = page_zone(pfn_to_page(start_pfn));
  1465. node = zone_to_nid(zone);
  1466. nr_pages = end_pfn - start_pfn;
  1467. if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
  1468. return -EINVAL;
  1469. /* set above range as isolated */
  1470. ret = start_isolate_page_range(start_pfn, end_pfn,
  1471. MIGRATE_MOVABLE, true);
  1472. if (ret)
  1473. return ret;
  1474. arg.start_pfn = start_pfn;
  1475. arg.nr_pages = nr_pages;
  1476. node_states_check_changes_offline(nr_pages, zone, &arg);
  1477. ret = memory_notify(MEM_GOING_OFFLINE, &arg);
  1478. ret = notifier_to_errno(ret);
  1479. if (ret)
  1480. goto failed_removal;
  1481. pfn = start_pfn;
  1482. expire = jiffies + timeout;
  1483. drain = 0;
  1484. retry_max = 5;
  1485. repeat:
  1486. /* start memory hot removal */
  1487. ret = -EAGAIN;
  1488. if (time_after(jiffies, expire))
  1489. goto failed_removal;
  1490. ret = -EINTR;
  1491. if (signal_pending(current))
  1492. goto failed_removal;
  1493. ret = 0;
  1494. if (drain) {
  1495. lru_add_drain_all();
  1496. cond_resched();
  1497. drain_all_pages(zone);
  1498. }
  1499. pfn = scan_movable_pages(start_pfn, end_pfn);
  1500. if (pfn) { /* We have movable pages */
  1501. ret = do_migrate_range(pfn, end_pfn);
  1502. if (!ret) {
  1503. drain = 1;
  1504. goto repeat;
  1505. } else {
  1506. if (ret < 0)
  1507. if (--retry_max == 0)
  1508. goto failed_removal;
  1509. yield();
  1510. drain = 1;
  1511. goto repeat;
  1512. }
  1513. }
  1514. /* drain all zone's lru pagevec, this is asynchronous... */
  1515. lru_add_drain_all();
  1516. yield();
  1517. /* drain pcp pages, this is synchronous. */
  1518. drain_all_pages(zone);
  1519. /*
  1520. * dissolve free hugepages in the memory block before doing offlining
  1521. * actually in order to make hugetlbfs's object counting consistent.
  1522. */
  1523. dissolve_free_huge_pages(start_pfn, end_pfn);
  1524. /* check again */
  1525. offlined_pages = check_pages_isolated(start_pfn, end_pfn);
  1526. if (offlined_pages < 0) {
  1527. ret = -EBUSY;
  1528. goto failed_removal;
  1529. }
  1530. printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
  1531. /* Ok, all of our target is isolated.
  1532. We cannot do rollback at this point. */
  1533. offline_isolated_pages(start_pfn, end_pfn);
  1534. /* reset pagetype flags and makes migrate type to be MOVABLE */
  1535. undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
  1536. /* removal success */
  1537. adjust_managed_page_count(pfn_to_page(start_pfn), -offlined_pages);
  1538. zone->present_pages -= offlined_pages;
  1539. pgdat_resize_lock(zone->zone_pgdat, &flags);
  1540. zone->zone_pgdat->node_present_pages -= offlined_pages;
  1541. pgdat_resize_unlock(zone->zone_pgdat, &flags);
  1542. init_per_zone_wmark_min();
  1543. if (!populated_zone(zone)) {
  1544. zone_pcp_reset(zone);
  1545. mutex_lock(&zonelists_mutex);
  1546. build_all_zonelists(NULL, NULL);
  1547. mutex_unlock(&zonelists_mutex);
  1548. } else
  1549. zone_pcp_update(zone);
  1550. node_states_clear_node(node, &arg);
  1551. if (arg.status_change_nid >= 0)
  1552. kswapd_stop(node);
  1553. vm_total_pages = nr_free_pagecache_pages();
  1554. writeback_set_ratelimit();
  1555. memory_notify(MEM_OFFLINE, &arg);
  1556. return 0;
  1557. failed_removal:
  1558. printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
  1559. (unsigned long long) start_pfn << PAGE_SHIFT,
  1560. ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
  1561. memory_notify(MEM_CANCEL_OFFLINE, &arg);
  1562. /* pushback to free area */
  1563. undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
  1564. return ret;
  1565. }
  1566. /* Must be protected by mem_hotplug_begin() */
  1567. int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
  1568. {
  1569. return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
  1570. }
  1571. #endif /* CONFIG_MEMORY_HOTREMOVE */
  1572. /**
  1573. * walk_memory_range - walks through all mem sections in [start_pfn, end_pfn)
  1574. * @start_pfn: start pfn of the memory range
  1575. * @end_pfn: end pfn of the memory range
  1576. * @arg: argument passed to func
  1577. * @func: callback for each memory section walked
  1578. *
  1579. * This function walks through all present mem sections in range
  1580. * [start_pfn, end_pfn) and call func on each mem section.
  1581. *
  1582. * Returns the return value of func.
  1583. */
  1584. int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn,
  1585. void *arg, int (*func)(struct memory_block *, void *))
  1586. {
  1587. struct memory_block *mem = NULL;
  1588. struct mem_section *section;
  1589. unsigned long pfn, section_nr;
  1590. int ret;
  1591. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  1592. section_nr = pfn_to_section_nr(pfn);
  1593. if (!present_section_nr(section_nr))
  1594. continue;
  1595. section = __nr_to_section(section_nr);
  1596. /* same memblock? */
  1597. if (mem)
  1598. if ((section_nr >= mem->start_section_nr) &&
  1599. (section_nr <= mem->end_section_nr))
  1600. continue;
  1601. mem = find_memory_block_hinted(section, mem);
  1602. if (!mem)
  1603. continue;
  1604. ret = func(mem, arg);
  1605. if (ret) {
  1606. kobject_put(&mem->dev.kobj);
  1607. return ret;
  1608. }
  1609. }
  1610. if (mem)
  1611. kobject_put(&mem->dev.kobj);
  1612. return 0;
  1613. }
  1614. #ifdef CONFIG_MEMORY_HOTREMOVE
  1615. static int check_memblock_offlined_cb(struct memory_block *mem, void *arg)
  1616. {
  1617. int ret = !is_memblock_offlined(mem);
  1618. if (unlikely(ret)) {
  1619. phys_addr_t beginpa, endpa;
  1620. beginpa = PFN_PHYS(section_nr_to_pfn(mem->start_section_nr));
  1621. endpa = PFN_PHYS(section_nr_to_pfn(mem->end_section_nr + 1))-1;
  1622. pr_warn("removing memory fails, because memory "
  1623. "[%pa-%pa] is onlined\n",
  1624. &beginpa, &endpa);
  1625. }
  1626. return ret;
  1627. }
  1628. static int check_cpu_on_node(pg_data_t *pgdat)
  1629. {
  1630. int cpu;
  1631. for_each_present_cpu(cpu) {
  1632. if (cpu_to_node(cpu) == pgdat->node_id)
  1633. /*
  1634. * the cpu on this node isn't removed, and we can't
  1635. * offline this node.
  1636. */
  1637. return -EBUSY;
  1638. }
  1639. return 0;
  1640. }
  1641. static void unmap_cpu_on_node(pg_data_t *pgdat)
  1642. {
  1643. #ifdef CONFIG_ACPI_NUMA
  1644. int cpu;
  1645. for_each_possible_cpu(cpu)
  1646. if (cpu_to_node(cpu) == pgdat->node_id)
  1647. numa_clear_node(cpu);
  1648. #endif
  1649. }
  1650. static int check_and_unmap_cpu_on_node(pg_data_t *pgdat)
  1651. {
  1652. int ret;
  1653. ret = check_cpu_on_node(pgdat);
  1654. if (ret)
  1655. return ret;
  1656. /*
  1657. * the node will be offlined when we come here, so we can clear
  1658. * the cpu_to_node() now.
  1659. */
  1660. unmap_cpu_on_node(pgdat);
  1661. return 0;
  1662. }
  1663. /**
  1664. * try_offline_node
  1665. *
  1666. * Offline a node if all memory sections and cpus of the node are removed.
  1667. *
  1668. * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
  1669. * and online/offline operations before this call.
  1670. */
  1671. void try_offline_node(int nid)
  1672. {
  1673. pg_data_t *pgdat = NODE_DATA(nid);
  1674. unsigned long start_pfn = pgdat->node_start_pfn;
  1675. unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
  1676. unsigned long pfn;
  1677. int i;
  1678. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  1679. unsigned long section_nr = pfn_to_section_nr(pfn);
  1680. if (!present_section_nr(section_nr))
  1681. continue;
  1682. if (pfn_to_nid(pfn) != nid)
  1683. continue;
  1684. /*
  1685. * some memory sections of this node are not removed, and we
  1686. * can't offline node now.
  1687. */
  1688. return;
  1689. }
  1690. if (check_and_unmap_cpu_on_node(pgdat))
  1691. return;
  1692. /*
  1693. * all memory/cpu of this node are removed, we can offline this
  1694. * node now.
  1695. */
  1696. node_set_offline(nid);
  1697. unregister_one_node(nid);
  1698. /* free waittable in each zone */
  1699. for (i = 0; i < MAX_NR_ZONES; i++) {
  1700. struct zone *zone = pgdat->node_zones + i;
  1701. /*
  1702. * wait_table may be allocated from boot memory,
  1703. * here only free if it's allocated by vmalloc.
  1704. */
  1705. if (is_vmalloc_addr(zone->wait_table)) {
  1706. vfree(zone->wait_table);
  1707. zone->wait_table = NULL;
  1708. }
  1709. }
  1710. }
  1711. EXPORT_SYMBOL(try_offline_node);
  1712. /**
  1713. * remove_memory
  1714. *
  1715. * NOTE: The caller must call lock_device_hotplug() to serialize hotplug
  1716. * and online/offline operations before this call, as required by
  1717. * try_offline_node().
  1718. */
  1719. void __ref remove_memory(int nid, u64 start, u64 size)
  1720. {
  1721. int ret;
  1722. BUG_ON(check_hotplug_memory_range(start, size));
  1723. mem_hotplug_begin();
  1724. /*
  1725. * All memory blocks must be offlined before removing memory. Check
  1726. * whether all memory blocks in question are offline and trigger a BUG()
  1727. * if this is not the case.
  1728. */
  1729. ret = walk_memory_range(PFN_DOWN(start), PFN_UP(start + size - 1), NULL,
  1730. check_memblock_offlined_cb);
  1731. if (ret)
  1732. BUG();
  1733. /* remove memmap entry */
  1734. firmware_map_remove(start, start + size, "System RAM");
  1735. memblock_free(start, size);
  1736. memblock_remove(start, size);
  1737. arch_remove_memory(start, size);
  1738. try_offline_node(nid);
  1739. mem_hotplug_done();
  1740. }
  1741. EXPORT_SYMBOL_GPL(remove_memory);
  1742. #endif /* CONFIG_MEMORY_HOTREMOVE */