memory_hotplug.c 55 KB

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