memory_hotplug.c 49 KB

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