memory_hotplug.c 50 KB

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