memory_hotplug.c 54 KB

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