memory_hotplug.c 55 KB

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