memory_hotplug.c 49 KB

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