memory_hotplug.c 49 KB

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