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