memory_hotplug.c 54 KB

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