memory_hotplug.c 47 KB

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