memory_hotplug.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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 <asm/tlbflush.h>
  32. #include "internal.h"
  33. /*
  34. * online_page_callback contains pointer to current page onlining function.
  35. * Initially it is generic_online_page(). If it is required it could be
  36. * changed by calling set_online_page_callback() for callback registration
  37. * and restore_online_page_callback() for generic callback restore.
  38. */
  39. static void generic_online_page(struct page *page);
  40. static online_page_callback_t online_page_callback = generic_online_page;
  41. DEFINE_MUTEX(mem_hotplug_mutex);
  42. void lock_memory_hotplug(void)
  43. {
  44. mutex_lock(&mem_hotplug_mutex);
  45. /* for exclusive hibernation if CONFIG_HIBERNATION=y */
  46. lock_system_sleep();
  47. }
  48. void unlock_memory_hotplug(void)
  49. {
  50. unlock_system_sleep();
  51. mutex_unlock(&mem_hotplug_mutex);
  52. }
  53. /* add this memory to iomem resource */
  54. static struct resource *register_memory_resource(u64 start, u64 size)
  55. {
  56. struct resource *res;
  57. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  58. BUG_ON(!res);
  59. res->name = "System RAM";
  60. res->start = start;
  61. res->end = start + size - 1;
  62. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  63. if (request_resource(&iomem_resource, res) < 0) {
  64. printk("System RAM resource %pR cannot be added\n", res);
  65. kfree(res);
  66. res = NULL;
  67. }
  68. return res;
  69. }
  70. static void release_memory_resource(struct resource *res)
  71. {
  72. if (!res)
  73. return;
  74. release_resource(res);
  75. kfree(res);
  76. return;
  77. }
  78. #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
  79. #ifndef CONFIG_SPARSEMEM_VMEMMAP
  80. static 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. type = (unsigned long) page->lru.next;
  94. BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE ||
  95. type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE);
  96. if (atomic_dec_return(&page->_count) == 1) {
  97. ClearPagePrivate(page);
  98. set_page_private(page, 0);
  99. INIT_LIST_HEAD(&page->lru);
  100. __free_pages_bootmem(page, 0);
  101. }
  102. }
  103. static void register_page_bootmem_info_section(unsigned long start_pfn)
  104. {
  105. unsigned long *usemap, mapsize, section_nr, i;
  106. struct mem_section *ms;
  107. struct page *page, *memmap;
  108. section_nr = pfn_to_section_nr(start_pfn);
  109. ms = __nr_to_section(section_nr);
  110. /* Get section's memmap address */
  111. memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
  112. /*
  113. * Get page for the memmap's phys address
  114. * XXX: need more consideration for sparse_vmemmap...
  115. */
  116. page = virt_to_page(memmap);
  117. mapsize = sizeof(struct page) * PAGES_PER_SECTION;
  118. mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT;
  119. /* remember memmap's page */
  120. for (i = 0; i < mapsize; i++, page++)
  121. get_page_bootmem(section_nr, page, SECTION_INFO);
  122. usemap = __nr_to_section(section_nr)->pageblock_flags;
  123. page = virt_to_page(usemap);
  124. mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT;
  125. for (i = 0; i < mapsize; i++, page++)
  126. get_page_bootmem(section_nr, page, MIX_SECTION_INFO);
  127. }
  128. void register_page_bootmem_info_node(struct pglist_data *pgdat)
  129. {
  130. unsigned long i, pfn, end_pfn, nr_pages;
  131. int node = pgdat->node_id;
  132. struct page *page;
  133. struct zone *zone;
  134. nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT;
  135. page = virt_to_page(pgdat);
  136. for (i = 0; i < nr_pages; i++, page++)
  137. get_page_bootmem(node, page, NODE_INFO);
  138. zone = &pgdat->node_zones[0];
  139. for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) {
  140. if (zone->wait_table) {
  141. nr_pages = zone->wait_table_hash_nr_entries
  142. * sizeof(wait_queue_head_t);
  143. nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT;
  144. page = virt_to_page(zone->wait_table);
  145. for (i = 0; i < nr_pages; i++, page++)
  146. get_page_bootmem(node, page, NODE_INFO);
  147. }
  148. }
  149. pfn = pgdat->node_start_pfn;
  150. end_pfn = pfn + pgdat->node_spanned_pages;
  151. /* register_section info */
  152. for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  153. /*
  154. * Some platforms can assign the same pfn to multiple nodes - on
  155. * node0 as well as nodeN. To avoid registering a pfn against
  156. * multiple nodes we check that this pfn does not already
  157. * reside in some other node.
  158. */
  159. if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node))
  160. register_page_bootmem_info_section(pfn);
  161. }
  162. }
  163. #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
  164. static void grow_zone_span(struct zone *zone, unsigned long start_pfn,
  165. unsigned long end_pfn)
  166. {
  167. unsigned long old_zone_end_pfn;
  168. zone_span_writelock(zone);
  169. old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
  170. if (start_pfn < zone->zone_start_pfn)
  171. zone->zone_start_pfn = start_pfn;
  172. zone->spanned_pages = max(old_zone_end_pfn, end_pfn) -
  173. zone->zone_start_pfn;
  174. zone_span_writeunlock(zone);
  175. }
  176. static void grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn,
  177. unsigned long end_pfn)
  178. {
  179. unsigned long old_pgdat_end_pfn =
  180. pgdat->node_start_pfn + pgdat->node_spanned_pages;
  181. if (start_pfn < pgdat->node_start_pfn)
  182. pgdat->node_start_pfn = start_pfn;
  183. pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) -
  184. pgdat->node_start_pfn;
  185. }
  186. static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn)
  187. {
  188. struct pglist_data *pgdat = zone->zone_pgdat;
  189. int nr_pages = PAGES_PER_SECTION;
  190. int nid = pgdat->node_id;
  191. int zone_type;
  192. unsigned long flags;
  193. zone_type = zone - pgdat->node_zones;
  194. if (!zone->wait_table) {
  195. int ret;
  196. ret = init_currently_empty_zone(zone, phys_start_pfn,
  197. nr_pages, MEMMAP_HOTPLUG);
  198. if (ret)
  199. return ret;
  200. }
  201. pgdat_resize_lock(zone->zone_pgdat, &flags);
  202. grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages);
  203. grow_pgdat_span(zone->zone_pgdat, phys_start_pfn,
  204. phys_start_pfn + nr_pages);
  205. pgdat_resize_unlock(zone->zone_pgdat, &flags);
  206. memmap_init_zone(nr_pages, nid, zone_type,
  207. phys_start_pfn, MEMMAP_HOTPLUG);
  208. return 0;
  209. }
  210. static int __meminit __add_section(int nid, struct zone *zone,
  211. unsigned long phys_start_pfn)
  212. {
  213. int nr_pages = PAGES_PER_SECTION;
  214. int ret;
  215. if (pfn_valid(phys_start_pfn))
  216. return -EEXIST;
  217. ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
  218. if (ret < 0)
  219. return ret;
  220. ret = __add_zone(zone, phys_start_pfn);
  221. if (ret < 0)
  222. return ret;
  223. return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
  224. }
  225. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  226. static int __remove_section(struct zone *zone, struct mem_section *ms)
  227. {
  228. /*
  229. * XXX: Freeing memmap with vmemmap is not implement yet.
  230. * This should be removed later.
  231. */
  232. return -EBUSY;
  233. }
  234. #else
  235. static int __remove_section(struct zone *zone, struct mem_section *ms)
  236. {
  237. unsigned long flags;
  238. struct pglist_data *pgdat = zone->zone_pgdat;
  239. int ret = -EINVAL;
  240. if (!valid_section(ms))
  241. return ret;
  242. ret = unregister_memory_section(ms);
  243. if (ret)
  244. return ret;
  245. pgdat_resize_lock(pgdat, &flags);
  246. sparse_remove_one_section(zone, ms);
  247. pgdat_resize_unlock(pgdat, &flags);
  248. return 0;
  249. }
  250. #endif
  251. /*
  252. * Reasonably generic function for adding memory. It is
  253. * expected that archs that support memory hotplug will
  254. * call this function after deciding the zone to which to
  255. * add the new pages.
  256. */
  257. int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn,
  258. unsigned long nr_pages)
  259. {
  260. unsigned long i;
  261. int err = 0;
  262. int start_sec, end_sec;
  263. /* during initialize mem_map, align hot-added range to section */
  264. start_sec = pfn_to_section_nr(phys_start_pfn);
  265. end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1);
  266. for (i = start_sec; i <= end_sec; i++) {
  267. err = __add_section(nid, zone, i << PFN_SECTION_SHIFT);
  268. /*
  269. * EEXIST is finally dealt with by ioresource collision
  270. * check. see add_memory() => register_memory_resource()
  271. * Warning will be printed if there is collision.
  272. */
  273. if (err && (err != -EEXIST))
  274. break;
  275. err = 0;
  276. }
  277. return err;
  278. }
  279. EXPORT_SYMBOL_GPL(__add_pages);
  280. /**
  281. * __remove_pages() - remove sections of pages from a zone
  282. * @zone: zone from which pages need to be removed
  283. * @phys_start_pfn: starting pageframe (must be aligned to start of a section)
  284. * @nr_pages: number of pages to remove (must be multiple of section size)
  285. *
  286. * Generic helper function to remove section mappings and sysfs entries
  287. * for the section of the memory we are removing. Caller needs to make
  288. * sure that pages are marked reserved and zones are adjust properly by
  289. * calling offline_pages().
  290. */
  291. int __remove_pages(struct zone *zone, unsigned long phys_start_pfn,
  292. unsigned long nr_pages)
  293. {
  294. unsigned long i, ret = 0;
  295. int sections_to_remove;
  296. /*
  297. * We can only remove entire sections
  298. */
  299. BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK);
  300. BUG_ON(nr_pages % PAGES_PER_SECTION);
  301. release_mem_region(phys_start_pfn << PAGE_SHIFT, nr_pages * PAGE_SIZE);
  302. sections_to_remove = nr_pages / PAGES_PER_SECTION;
  303. for (i = 0; i < sections_to_remove; i++) {
  304. unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;
  305. ret = __remove_section(zone, __pfn_to_section(pfn));
  306. if (ret)
  307. break;
  308. }
  309. return ret;
  310. }
  311. EXPORT_SYMBOL_GPL(__remove_pages);
  312. int set_online_page_callback(online_page_callback_t callback)
  313. {
  314. int rc = -EINVAL;
  315. lock_memory_hotplug();
  316. if (online_page_callback == generic_online_page) {
  317. online_page_callback = callback;
  318. rc = 0;
  319. }
  320. unlock_memory_hotplug();
  321. return rc;
  322. }
  323. EXPORT_SYMBOL_GPL(set_online_page_callback);
  324. int restore_online_page_callback(online_page_callback_t callback)
  325. {
  326. int rc = -EINVAL;
  327. lock_memory_hotplug();
  328. if (online_page_callback == callback) {
  329. online_page_callback = generic_online_page;
  330. rc = 0;
  331. }
  332. unlock_memory_hotplug();
  333. return rc;
  334. }
  335. EXPORT_SYMBOL_GPL(restore_online_page_callback);
  336. void __online_page_set_limits(struct page *page)
  337. {
  338. unsigned long pfn = page_to_pfn(page);
  339. if (pfn >= num_physpages)
  340. num_physpages = pfn + 1;
  341. }
  342. EXPORT_SYMBOL_GPL(__online_page_set_limits);
  343. void __online_page_increment_counters(struct page *page)
  344. {
  345. totalram_pages++;
  346. #ifdef CONFIG_HIGHMEM
  347. if (PageHighMem(page))
  348. totalhigh_pages++;
  349. #endif
  350. }
  351. EXPORT_SYMBOL_GPL(__online_page_increment_counters);
  352. void __online_page_free(struct page *page)
  353. {
  354. ClearPageReserved(page);
  355. init_page_count(page);
  356. __free_page(page);
  357. }
  358. EXPORT_SYMBOL_GPL(__online_page_free);
  359. static void generic_online_page(struct page *page)
  360. {
  361. __online_page_set_limits(page);
  362. __online_page_increment_counters(page);
  363. __online_page_free(page);
  364. }
  365. static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages,
  366. void *arg)
  367. {
  368. unsigned long i;
  369. unsigned long onlined_pages = *(unsigned long *)arg;
  370. struct page *page;
  371. if (PageReserved(pfn_to_page(start_pfn)))
  372. for (i = 0; i < nr_pages; i++) {
  373. page = pfn_to_page(start_pfn + i);
  374. (*online_page_callback)(page);
  375. onlined_pages++;
  376. }
  377. *(unsigned long *)arg = onlined_pages;
  378. return 0;
  379. }
  380. int __ref online_pages(unsigned long pfn, unsigned long nr_pages)
  381. {
  382. unsigned long onlined_pages = 0;
  383. struct zone *zone;
  384. int need_zonelists_rebuild = 0;
  385. int nid;
  386. int ret;
  387. struct memory_notify arg;
  388. lock_memory_hotplug();
  389. arg.start_pfn = pfn;
  390. arg.nr_pages = nr_pages;
  391. arg.status_change_nid = -1;
  392. nid = page_to_nid(pfn_to_page(pfn));
  393. if (node_present_pages(nid) == 0)
  394. arg.status_change_nid = nid;
  395. ret = memory_notify(MEM_GOING_ONLINE, &arg);
  396. ret = notifier_to_errno(ret);
  397. if (ret) {
  398. memory_notify(MEM_CANCEL_ONLINE, &arg);
  399. unlock_memory_hotplug();
  400. return ret;
  401. }
  402. /*
  403. * This doesn't need a lock to do pfn_to_page().
  404. * The section can't be removed here because of the
  405. * memory_block->state_mutex.
  406. */
  407. zone = page_zone(pfn_to_page(pfn));
  408. /*
  409. * If this zone is not populated, then it is not in zonelist.
  410. * This means the page allocator ignores this zone.
  411. * So, zonelist must be updated after online.
  412. */
  413. mutex_lock(&zonelists_mutex);
  414. if (!populated_zone(zone))
  415. need_zonelists_rebuild = 1;
  416. ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
  417. online_pages_range);
  418. if (ret) {
  419. mutex_unlock(&zonelists_mutex);
  420. printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n",
  421. (unsigned long long) pfn << PAGE_SHIFT,
  422. (((unsigned long long) pfn + nr_pages)
  423. << PAGE_SHIFT) - 1);
  424. memory_notify(MEM_CANCEL_ONLINE, &arg);
  425. unlock_memory_hotplug();
  426. return ret;
  427. }
  428. zone->present_pages += onlined_pages;
  429. zone->zone_pgdat->node_present_pages += onlined_pages;
  430. if (onlined_pages) {
  431. node_set_state(zone_to_nid(zone), N_HIGH_MEMORY);
  432. if (need_zonelists_rebuild)
  433. build_all_zonelists(NULL, zone);
  434. else
  435. zone_pcp_update(zone);
  436. }
  437. mutex_unlock(&zonelists_mutex);
  438. init_per_zone_wmark_min();
  439. if (onlined_pages)
  440. kswapd_run(zone_to_nid(zone));
  441. vm_total_pages = nr_free_pagecache_pages();
  442. writeback_set_ratelimit();
  443. if (onlined_pages)
  444. memory_notify(MEM_ONLINE, &arg);
  445. unlock_memory_hotplug();
  446. return 0;
  447. }
  448. #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
  449. /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
  450. static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start)
  451. {
  452. struct pglist_data *pgdat;
  453. unsigned long zones_size[MAX_NR_ZONES] = {0};
  454. unsigned long zholes_size[MAX_NR_ZONES] = {0};
  455. unsigned long start_pfn = start >> PAGE_SHIFT;
  456. pgdat = arch_alloc_nodedata(nid);
  457. if (!pgdat)
  458. return NULL;
  459. arch_refresh_nodedata(nid, pgdat);
  460. /* we can use NODE_DATA(nid) from here */
  461. /* init node's zones as empty zones, we don't have any present pages.*/
  462. free_area_init_node(nid, zones_size, start_pfn, zholes_size);
  463. /*
  464. * The node we allocated has no zone fallback lists. For avoiding
  465. * to access not-initialized zonelist, build here.
  466. */
  467. mutex_lock(&zonelists_mutex);
  468. build_all_zonelists(pgdat, NULL);
  469. mutex_unlock(&zonelists_mutex);
  470. return pgdat;
  471. }
  472. static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
  473. {
  474. arch_refresh_nodedata(nid, NULL);
  475. arch_free_nodedata(pgdat);
  476. return;
  477. }
  478. /*
  479. * called by cpu_up() to online a node without onlined memory.
  480. */
  481. int mem_online_node(int nid)
  482. {
  483. pg_data_t *pgdat;
  484. int ret;
  485. lock_memory_hotplug();
  486. pgdat = hotadd_new_pgdat(nid, 0);
  487. if (!pgdat) {
  488. ret = -ENOMEM;
  489. goto out;
  490. }
  491. node_set_online(nid);
  492. ret = register_one_node(nid);
  493. BUG_ON(ret);
  494. out:
  495. unlock_memory_hotplug();
  496. return ret;
  497. }
  498. /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */
  499. int __ref add_memory(int nid, u64 start, u64 size)
  500. {
  501. pg_data_t *pgdat = NULL;
  502. int new_pgdat = 0;
  503. struct resource *res;
  504. int ret;
  505. lock_memory_hotplug();
  506. res = register_memory_resource(start, size);
  507. ret = -EEXIST;
  508. if (!res)
  509. goto out;
  510. if (!node_online(nid)) {
  511. pgdat = hotadd_new_pgdat(nid, start);
  512. ret = -ENOMEM;
  513. if (!pgdat)
  514. goto error;
  515. new_pgdat = 1;
  516. }
  517. /* call arch's memory hotadd */
  518. ret = arch_add_memory(nid, start, size);
  519. if (ret < 0)
  520. goto error;
  521. /* we online node here. we can't roll back from here. */
  522. node_set_online(nid);
  523. if (new_pgdat) {
  524. ret = register_one_node(nid);
  525. /*
  526. * If sysfs file of new node can't create, cpu on the node
  527. * can't be hot-added. There is no rollback way now.
  528. * So, check by BUG_ON() to catch it reluctantly..
  529. */
  530. BUG_ON(ret);
  531. }
  532. /* create new memmap entry */
  533. firmware_map_add_hotplug(start, start + size, "System RAM");
  534. goto out;
  535. error:
  536. /* rollback pgdat allocation and others */
  537. if (new_pgdat)
  538. rollback_node_hotadd(nid, pgdat);
  539. if (res)
  540. release_memory_resource(res);
  541. out:
  542. unlock_memory_hotplug();
  543. return ret;
  544. }
  545. EXPORT_SYMBOL_GPL(add_memory);
  546. #ifdef CONFIG_MEMORY_HOTREMOVE
  547. /*
  548. * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy
  549. * set and the size of the free page is given by page_order(). Using this,
  550. * the function determines if the pageblock contains only free pages.
  551. * Due to buddy contraints, a free page at least the size of a pageblock will
  552. * be located at the start of the pageblock
  553. */
  554. static inline int pageblock_free(struct page *page)
  555. {
  556. return PageBuddy(page) && page_order(page) >= pageblock_order;
  557. }
  558. /* Return the start of the next active pageblock after a given page */
  559. static struct page *next_active_pageblock(struct page *page)
  560. {
  561. /* Ensure the starting page is pageblock-aligned */
  562. BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1));
  563. /* If the entire pageblock is free, move to the end of free page */
  564. if (pageblock_free(page)) {
  565. int order;
  566. /* be careful. we don't have locks, page_order can be changed.*/
  567. order = page_order(page);
  568. if ((order < MAX_ORDER) && (order >= pageblock_order))
  569. return page + (1 << order);
  570. }
  571. return page + pageblock_nr_pages;
  572. }
  573. /* Checks if this range of memory is likely to be hot-removable. */
  574. int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
  575. {
  576. struct page *page = pfn_to_page(start_pfn);
  577. struct page *end_page = page + nr_pages;
  578. /* Check the starting page of each pageblock within the range */
  579. for (; page < end_page; page = next_active_pageblock(page)) {
  580. if (!is_pageblock_removable_nolock(page))
  581. return 0;
  582. cond_resched();
  583. }
  584. /* All pageblocks in the memory block are likely to be hot-removable */
  585. return 1;
  586. }
  587. /*
  588. * Confirm all pages in a range [start, end) is belongs to the same zone.
  589. */
  590. static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
  591. {
  592. unsigned long pfn;
  593. struct zone *zone = NULL;
  594. struct page *page;
  595. int i;
  596. for (pfn = start_pfn;
  597. pfn < end_pfn;
  598. pfn += MAX_ORDER_NR_PAGES) {
  599. i = 0;
  600. /* This is just a CONFIG_HOLES_IN_ZONE check.*/
  601. while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i))
  602. i++;
  603. if (i == MAX_ORDER_NR_PAGES)
  604. continue;
  605. page = pfn_to_page(pfn + i);
  606. if (zone && page_zone(page) != zone)
  607. return 0;
  608. zone = page_zone(page);
  609. }
  610. return 1;
  611. }
  612. /*
  613. * Scanning pfn is much easier than scanning lru list.
  614. * Scan pfn from start to end and Find LRU page.
  615. */
  616. static unsigned long scan_lru_pages(unsigned long start, unsigned long end)
  617. {
  618. unsigned long pfn;
  619. struct page *page;
  620. for (pfn = start; pfn < end; pfn++) {
  621. if (pfn_valid(pfn)) {
  622. page = pfn_to_page(pfn);
  623. if (PageLRU(page))
  624. return pfn;
  625. }
  626. }
  627. return 0;
  628. }
  629. #define NR_OFFLINE_AT_ONCE_PAGES (256)
  630. static int
  631. do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
  632. {
  633. unsigned long pfn;
  634. struct page *page;
  635. int move_pages = NR_OFFLINE_AT_ONCE_PAGES;
  636. int not_managed = 0;
  637. int ret = 0;
  638. LIST_HEAD(source);
  639. for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) {
  640. if (!pfn_valid(pfn))
  641. continue;
  642. page = pfn_to_page(pfn);
  643. if (!get_page_unless_zero(page))
  644. continue;
  645. /*
  646. * We can skip free pages. And we can only deal with pages on
  647. * LRU.
  648. */
  649. ret = isolate_lru_page(page);
  650. if (!ret) { /* Success */
  651. put_page(page);
  652. list_add_tail(&page->lru, &source);
  653. move_pages--;
  654. inc_zone_page_state(page, NR_ISOLATED_ANON +
  655. page_is_file_cache(page));
  656. } else {
  657. #ifdef CONFIG_DEBUG_VM
  658. printk(KERN_ALERT "removing pfn %lx from LRU failed\n",
  659. pfn);
  660. dump_page(page);
  661. #endif
  662. put_page(page);
  663. /* Because we don't have big zone->lock. we should
  664. check this again here. */
  665. if (page_count(page)) {
  666. not_managed++;
  667. ret = -EBUSY;
  668. break;
  669. }
  670. }
  671. }
  672. if (!list_empty(&source)) {
  673. if (not_managed) {
  674. putback_lru_pages(&source);
  675. goto out;
  676. }
  677. /*
  678. * alloc_migrate_target should be improooooved!!
  679. * migrate_pages returns # of failed pages.
  680. */
  681. ret = migrate_pages(&source, alloc_migrate_target, 0,
  682. true, MIGRATE_SYNC);
  683. if (ret)
  684. putback_lru_pages(&source);
  685. }
  686. out:
  687. return ret;
  688. }
  689. /*
  690. * remove from free_area[] and mark all as Reserved.
  691. */
  692. static int
  693. offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages,
  694. void *data)
  695. {
  696. __offline_isolated_pages(start, start + nr_pages);
  697. return 0;
  698. }
  699. static void
  700. offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
  701. {
  702. walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL,
  703. offline_isolated_pages_cb);
  704. }
  705. /*
  706. * Check all pages in range, recoreded as memory resource, are isolated.
  707. */
  708. static int
  709. check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages,
  710. void *data)
  711. {
  712. int ret;
  713. long offlined = *(long *)data;
  714. ret = test_pages_isolated(start_pfn, start_pfn + nr_pages);
  715. offlined = nr_pages;
  716. if (!ret)
  717. *(long *)data += offlined;
  718. return ret;
  719. }
  720. static long
  721. check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
  722. {
  723. long offlined = 0;
  724. int ret;
  725. ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined,
  726. check_pages_isolated_cb);
  727. if (ret < 0)
  728. offlined = (long)ret;
  729. return offlined;
  730. }
  731. static int __ref __offline_pages(unsigned long start_pfn,
  732. unsigned long end_pfn, unsigned long timeout)
  733. {
  734. unsigned long pfn, nr_pages, expire;
  735. long offlined_pages;
  736. int ret, drain, retry_max, node;
  737. struct zone *zone;
  738. struct memory_notify arg;
  739. BUG_ON(start_pfn >= end_pfn);
  740. /* at least, alignment against pageblock is necessary */
  741. if (!IS_ALIGNED(start_pfn, pageblock_nr_pages))
  742. return -EINVAL;
  743. if (!IS_ALIGNED(end_pfn, pageblock_nr_pages))
  744. return -EINVAL;
  745. /* This makes hotplug much easier...and readable.
  746. we assume this for now. .*/
  747. if (!test_pages_in_a_zone(start_pfn, end_pfn))
  748. return -EINVAL;
  749. lock_memory_hotplug();
  750. zone = page_zone(pfn_to_page(start_pfn));
  751. node = zone_to_nid(zone);
  752. nr_pages = end_pfn - start_pfn;
  753. /* set above range as isolated */
  754. ret = start_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
  755. if (ret)
  756. goto out;
  757. arg.start_pfn = start_pfn;
  758. arg.nr_pages = nr_pages;
  759. arg.status_change_nid = -1;
  760. if (nr_pages >= node_present_pages(node))
  761. arg.status_change_nid = node;
  762. ret = memory_notify(MEM_GOING_OFFLINE, &arg);
  763. ret = notifier_to_errno(ret);
  764. if (ret)
  765. goto failed_removal;
  766. pfn = start_pfn;
  767. expire = jiffies + timeout;
  768. drain = 0;
  769. retry_max = 5;
  770. repeat:
  771. /* start memory hot removal */
  772. ret = -EAGAIN;
  773. if (time_after(jiffies, expire))
  774. goto failed_removal;
  775. ret = -EINTR;
  776. if (signal_pending(current))
  777. goto failed_removal;
  778. ret = 0;
  779. if (drain) {
  780. lru_add_drain_all();
  781. cond_resched();
  782. drain_all_pages();
  783. }
  784. pfn = scan_lru_pages(start_pfn, end_pfn);
  785. if (pfn) { /* We have page on LRU */
  786. ret = do_migrate_range(pfn, end_pfn);
  787. if (!ret) {
  788. drain = 1;
  789. goto repeat;
  790. } else {
  791. if (ret < 0)
  792. if (--retry_max == 0)
  793. goto failed_removal;
  794. yield();
  795. drain = 1;
  796. goto repeat;
  797. }
  798. }
  799. /* drain all zone's lru pagevec, this is asyncronous... */
  800. lru_add_drain_all();
  801. yield();
  802. /* drain pcp pages , this is synchrouns. */
  803. drain_all_pages();
  804. /* check again */
  805. offlined_pages = check_pages_isolated(start_pfn, end_pfn);
  806. if (offlined_pages < 0) {
  807. ret = -EBUSY;
  808. goto failed_removal;
  809. }
  810. printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages);
  811. /* Ok, all of our target is islaoted.
  812. We cannot do rollback at this point. */
  813. offline_isolated_pages(start_pfn, end_pfn);
  814. /* reset pagetype flags and makes migrate type to be MOVABLE */
  815. undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
  816. /* removal success */
  817. zone->present_pages -= offlined_pages;
  818. zone->zone_pgdat->node_present_pages -= offlined_pages;
  819. totalram_pages -= offlined_pages;
  820. init_per_zone_wmark_min();
  821. if (!populated_zone(zone)) {
  822. zone_pcp_reset(zone);
  823. mutex_lock(&zonelists_mutex);
  824. build_all_zonelists(NULL, NULL);
  825. mutex_unlock(&zonelists_mutex);
  826. } else
  827. zone_pcp_update(zone);
  828. if (!node_present_pages(node)) {
  829. node_clear_state(node, N_HIGH_MEMORY);
  830. kswapd_stop(node);
  831. }
  832. vm_total_pages = nr_free_pagecache_pages();
  833. writeback_set_ratelimit();
  834. memory_notify(MEM_OFFLINE, &arg);
  835. unlock_memory_hotplug();
  836. return 0;
  837. failed_removal:
  838. printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n",
  839. (unsigned long long) start_pfn << PAGE_SHIFT,
  840. ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
  841. memory_notify(MEM_CANCEL_OFFLINE, &arg);
  842. /* pushback to free area */
  843. undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
  844. out:
  845. unlock_memory_hotplug();
  846. return ret;
  847. }
  848. int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
  849. {
  850. return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);
  851. }
  852. int remove_memory(u64 start, u64 size)
  853. {
  854. struct memory_block *mem = NULL;
  855. struct mem_section *section;
  856. unsigned long start_pfn, end_pfn;
  857. unsigned long pfn, section_nr;
  858. int ret;
  859. start_pfn = PFN_DOWN(start);
  860. end_pfn = start_pfn + PFN_DOWN(size);
  861. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  862. section_nr = pfn_to_section_nr(pfn);
  863. if (!present_section_nr(section_nr))
  864. continue;
  865. section = __nr_to_section(section_nr);
  866. /* same memblock? */
  867. if (mem)
  868. if ((section_nr >= mem->start_section_nr) &&
  869. (section_nr <= mem->end_section_nr))
  870. continue;
  871. mem = find_memory_block_hinted(section, mem);
  872. if (!mem)
  873. continue;
  874. ret = offline_memory_block(mem);
  875. if (ret) {
  876. kobject_put(&mem->dev.kobj);
  877. return ret;
  878. }
  879. }
  880. if (mem)
  881. kobject_put(&mem->dev.kobj);
  882. return 0;
  883. }
  884. #else
  885. int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
  886. {
  887. return -EINVAL;
  888. }
  889. int remove_memory(u64 start, u64 size)
  890. {
  891. return -EINVAL;
  892. }
  893. #endif /* CONFIG_MEMORY_HOTREMOVE */
  894. EXPORT_SYMBOL_GPL(remove_memory);