memory_hotplug.c 48 KB

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