memory_hotplug.c 47 KB

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