memory_hotplug.c 49 KB

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