memory_hotplug.c 49 KB

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