memory_hotplug.c 50 KB

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