memory_hotplug.c 56 KB

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