memory_hotplug.c 50 KB

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