memory_hotplug.c 57 KB

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