memory_hotplug.c 55 KB

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