memory_hotplug.c 55 KB

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