memory_hotplug.c 47 KB

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