memory_hotplug.c 56 KB

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