balloon.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. /******************************************************************************
  2. * Xen balloon driver - enables returning/claiming memory to/from Xen.
  3. *
  4. * Copyright (c) 2003, B Dragovic
  5. * Copyright (c) 2003-2004, M Williamson, K Fraser
  6. * Copyright (c) 2005 Dan M. Smith, IBM Corporation
  7. * Copyright (c) 2010 Daniel Kiper
  8. *
  9. * Memory hotplug support was written by Daniel Kiper. Work on
  10. * it was sponsored by Google under Google Summer of Code 2010
  11. * program. Jeremy Fitzhardinge from Citrix was the mentor for
  12. * this project.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License version 2
  16. * as published by the Free Software Foundation; or, when distributed
  17. * separately from the Linux kernel or incorporated into other
  18. * software packages, subject to the following license:
  19. *
  20. * Permission is hereby granted, free of charge, to any person obtaining a copy
  21. * of this source file (the "Software"), to deal in the Software without
  22. * restriction, including without limitation the rights to use, copy, modify,
  23. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  24. * and to permit persons to whom the Software is furnished to do so, subject to
  25. * the following conditions:
  26. *
  27. * The above copyright notice and this permission notice shall be included in
  28. * all copies or substantial portions of the Software.
  29. *
  30. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  31. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  32. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  33. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  34. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  35. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  36. * IN THE SOFTWARE.
  37. */
  38. #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
  39. #include <linux/cpu.h>
  40. #include <linux/kernel.h>
  41. #include <linux/sched.h>
  42. #include <linux/errno.h>
  43. #include <linux/module.h>
  44. #include <linux/mm.h>
  45. #include <linux/bootmem.h>
  46. #include <linux/pagemap.h>
  47. #include <linux/highmem.h>
  48. #include <linux/mutex.h>
  49. #include <linux/list.h>
  50. #include <linux/gfp.h>
  51. #include <linux/notifier.h>
  52. #include <linux/memory.h>
  53. #include <linux/memory_hotplug.h>
  54. #include <linux/percpu-defs.h>
  55. #include <asm/page.h>
  56. #include <asm/pgalloc.h>
  57. #include <asm/pgtable.h>
  58. #include <asm/tlb.h>
  59. #include <asm/xen/hypervisor.h>
  60. #include <asm/xen/hypercall.h>
  61. #include <xen/xen.h>
  62. #include <xen/interface/xen.h>
  63. #include <xen/interface/memory.h>
  64. #include <xen/balloon.h>
  65. #include <xen/features.h>
  66. #include <xen/page.h>
  67. /*
  68. * balloon_process() state:
  69. *
  70. * BP_DONE: done or nothing to do,
  71. * BP_EAGAIN: error, go to sleep,
  72. * BP_ECANCELED: error, balloon operation canceled.
  73. */
  74. enum bp_state {
  75. BP_DONE,
  76. BP_EAGAIN,
  77. BP_ECANCELED
  78. };
  79. static DEFINE_MUTEX(balloon_mutex);
  80. struct balloon_stats balloon_stats;
  81. EXPORT_SYMBOL_GPL(balloon_stats);
  82. /* We increase/decrease in batches which fit in a page */
  83. static xen_pfn_t frame_list[PAGE_SIZE / sizeof(unsigned long)];
  84. /* List of ballooned pages, threaded through the mem_map array. */
  85. static LIST_HEAD(ballooned_pages);
  86. /* Main work function, always executed in process context. */
  87. static void balloon_process(struct work_struct *work);
  88. static DECLARE_DELAYED_WORK(balloon_worker, balloon_process);
  89. /* When ballooning out (allocating memory to return to Xen) we don't really
  90. want the kernel to try too hard since that can trigger the oom killer. */
  91. #define GFP_BALLOON \
  92. (GFP_HIGHUSER | __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC)
  93. static void scrub_page(struct page *page)
  94. {
  95. #ifdef CONFIG_XEN_SCRUB_PAGES
  96. clear_highpage(page);
  97. #endif
  98. }
  99. /* balloon_append: add the given page to the balloon. */
  100. static void __balloon_append(struct page *page)
  101. {
  102. /* Lowmem is re-populated first, so highmem pages go at list tail. */
  103. if (PageHighMem(page)) {
  104. list_add_tail(&page->lru, &ballooned_pages);
  105. balloon_stats.balloon_high++;
  106. } else {
  107. list_add(&page->lru, &ballooned_pages);
  108. balloon_stats.balloon_low++;
  109. }
  110. }
  111. static void balloon_append(struct page *page)
  112. {
  113. __balloon_append(page);
  114. adjust_managed_page_count(page, -1);
  115. }
  116. /* balloon_retrieve: rescue a page from the balloon, if it is not empty. */
  117. static struct page *balloon_retrieve(bool prefer_highmem)
  118. {
  119. struct page *page;
  120. if (list_empty(&ballooned_pages))
  121. return NULL;
  122. if (prefer_highmem)
  123. page = list_entry(ballooned_pages.prev, struct page, lru);
  124. else
  125. page = list_entry(ballooned_pages.next, struct page, lru);
  126. list_del(&page->lru);
  127. if (PageHighMem(page))
  128. balloon_stats.balloon_high--;
  129. else
  130. balloon_stats.balloon_low--;
  131. adjust_managed_page_count(page, 1);
  132. return page;
  133. }
  134. static struct page *balloon_next_page(struct page *page)
  135. {
  136. struct list_head *next = page->lru.next;
  137. if (next == &ballooned_pages)
  138. return NULL;
  139. return list_entry(next, struct page, lru);
  140. }
  141. static enum bp_state update_schedule(enum bp_state state)
  142. {
  143. if (state == BP_ECANCELED)
  144. return BP_ECANCELED;
  145. if (state == BP_DONE) {
  146. balloon_stats.schedule_delay = 1;
  147. balloon_stats.retry_count = 1;
  148. return BP_DONE;
  149. }
  150. ++balloon_stats.retry_count;
  151. if (balloon_stats.max_retry_count != RETRY_UNLIMITED &&
  152. balloon_stats.retry_count > balloon_stats.max_retry_count) {
  153. balloon_stats.schedule_delay = 1;
  154. balloon_stats.retry_count = 1;
  155. return BP_ECANCELED;
  156. }
  157. balloon_stats.schedule_delay <<= 1;
  158. if (balloon_stats.schedule_delay > balloon_stats.max_schedule_delay)
  159. balloon_stats.schedule_delay = balloon_stats.max_schedule_delay;
  160. return BP_EAGAIN;
  161. }
  162. #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
  163. static long current_credit(void)
  164. {
  165. return balloon_stats.target_pages - balloon_stats.current_pages -
  166. balloon_stats.hotplug_pages;
  167. }
  168. static bool balloon_is_inflated(void)
  169. {
  170. if (balloon_stats.balloon_low || balloon_stats.balloon_high ||
  171. balloon_stats.balloon_hotplug)
  172. return true;
  173. else
  174. return false;
  175. }
  176. /*
  177. * reserve_additional_memory() adds memory region of size >= credit above
  178. * max_pfn. New region is section aligned and size is modified to be multiple
  179. * of section size. Those features allow optimal use of address space and
  180. * establish proper alignment when this function is called first time after
  181. * boot (last section not fully populated at boot time contains unused memory
  182. * pages with PG_reserved bit not set; online_pages_range() does not allow page
  183. * onlining in whole range if first onlined page does not have PG_reserved
  184. * bit set). Real size of added memory is established at page onlining stage.
  185. */
  186. static enum bp_state reserve_additional_memory(long credit)
  187. {
  188. int nid, rc;
  189. u64 hotplug_start_paddr;
  190. unsigned long balloon_hotplug = credit;
  191. hotplug_start_paddr = PFN_PHYS(SECTION_ALIGN_UP(max_pfn));
  192. balloon_hotplug = round_up(balloon_hotplug, PAGES_PER_SECTION);
  193. nid = memory_add_physaddr_to_nid(hotplug_start_paddr);
  194. #ifdef CONFIG_XEN_HAVE_PVMMU
  195. /*
  196. * add_memory() will build page tables for the new memory so
  197. * the p2m must contain invalid entries so the correct
  198. * non-present PTEs will be written.
  199. *
  200. * If a failure occurs, the original (identity) p2m entries
  201. * are not restored since this region is now known not to
  202. * conflict with any devices.
  203. */
  204. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  205. unsigned long pfn, i;
  206. pfn = PFN_DOWN(hotplug_start_paddr);
  207. for (i = 0; i < balloon_hotplug; i++) {
  208. if (!set_phys_to_machine(pfn + i, INVALID_P2M_ENTRY)) {
  209. pr_warn("set_phys_to_machine() failed, no memory added\n");
  210. return BP_ECANCELED;
  211. }
  212. }
  213. }
  214. #endif
  215. rc = add_memory(nid, hotplug_start_paddr, balloon_hotplug << PAGE_SHIFT);
  216. if (rc) {
  217. pr_warn("Cannot add additional memory (%i)\n", rc);
  218. return BP_ECANCELED;
  219. }
  220. balloon_hotplug -= credit;
  221. balloon_stats.hotplug_pages += credit;
  222. balloon_stats.balloon_hotplug = balloon_hotplug;
  223. return BP_DONE;
  224. }
  225. static void xen_online_page(struct page *page)
  226. {
  227. __online_page_set_limits(page);
  228. mutex_lock(&balloon_mutex);
  229. __balloon_append(page);
  230. if (balloon_stats.hotplug_pages)
  231. --balloon_stats.hotplug_pages;
  232. else
  233. --balloon_stats.balloon_hotplug;
  234. mutex_unlock(&balloon_mutex);
  235. }
  236. static int xen_memory_notifier(struct notifier_block *nb, unsigned long val, void *v)
  237. {
  238. if (val == MEM_ONLINE)
  239. schedule_delayed_work(&balloon_worker, 0);
  240. return NOTIFY_OK;
  241. }
  242. static struct notifier_block xen_memory_nb = {
  243. .notifier_call = xen_memory_notifier,
  244. .priority = 0
  245. };
  246. #else
  247. static long current_credit(void)
  248. {
  249. unsigned long target = balloon_stats.target_pages;
  250. target = min(target,
  251. balloon_stats.current_pages +
  252. balloon_stats.balloon_low +
  253. balloon_stats.balloon_high);
  254. return target - balloon_stats.current_pages;
  255. }
  256. static bool balloon_is_inflated(void)
  257. {
  258. if (balloon_stats.balloon_low || balloon_stats.balloon_high)
  259. return true;
  260. else
  261. return false;
  262. }
  263. static enum bp_state reserve_additional_memory(long credit)
  264. {
  265. balloon_stats.target_pages = balloon_stats.current_pages;
  266. return BP_DONE;
  267. }
  268. #endif /* CONFIG_XEN_BALLOON_MEMORY_HOTPLUG */
  269. static enum bp_state increase_reservation(unsigned long nr_pages)
  270. {
  271. int rc;
  272. unsigned long pfn, i;
  273. struct page *page;
  274. struct xen_memory_reservation reservation = {
  275. .address_bits = 0,
  276. .extent_order = 0,
  277. .domid = DOMID_SELF
  278. };
  279. #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
  280. if (!balloon_stats.balloon_low && !balloon_stats.balloon_high) {
  281. nr_pages = min(nr_pages, balloon_stats.balloon_hotplug);
  282. balloon_stats.hotplug_pages += nr_pages;
  283. balloon_stats.balloon_hotplug -= nr_pages;
  284. return BP_DONE;
  285. }
  286. #endif
  287. if (nr_pages > ARRAY_SIZE(frame_list))
  288. nr_pages = ARRAY_SIZE(frame_list);
  289. page = list_first_entry_or_null(&ballooned_pages, struct page, lru);
  290. for (i = 0; i < nr_pages; i++) {
  291. if (!page) {
  292. nr_pages = i;
  293. break;
  294. }
  295. frame_list[i] = page_to_pfn(page);
  296. page = balloon_next_page(page);
  297. }
  298. set_xen_guest_handle(reservation.extent_start, frame_list);
  299. reservation.nr_extents = nr_pages;
  300. rc = HYPERVISOR_memory_op(XENMEM_populate_physmap, &reservation);
  301. if (rc <= 0)
  302. return BP_EAGAIN;
  303. for (i = 0; i < rc; i++) {
  304. page = balloon_retrieve(false);
  305. BUG_ON(page == NULL);
  306. pfn = page_to_pfn(page);
  307. #ifdef CONFIG_XEN_HAVE_PVMMU
  308. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  309. set_phys_to_machine(pfn, frame_list[i]);
  310. /* Link back into the page tables if not highmem. */
  311. if (!PageHighMem(page)) {
  312. int ret;
  313. ret = HYPERVISOR_update_va_mapping(
  314. (unsigned long)__va(pfn << PAGE_SHIFT),
  315. mfn_pte(frame_list[i], PAGE_KERNEL),
  316. 0);
  317. BUG_ON(ret);
  318. }
  319. }
  320. #endif
  321. /* Relinquish the page back to the allocator. */
  322. __free_reserved_page(page);
  323. }
  324. balloon_stats.current_pages += rc;
  325. return BP_DONE;
  326. }
  327. static enum bp_state decrease_reservation(unsigned long nr_pages, gfp_t gfp)
  328. {
  329. enum bp_state state = BP_DONE;
  330. unsigned long pfn, i;
  331. struct page *page;
  332. int ret;
  333. struct xen_memory_reservation reservation = {
  334. .address_bits = 0,
  335. .extent_order = 0,
  336. .domid = DOMID_SELF
  337. };
  338. #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
  339. if (balloon_stats.hotplug_pages) {
  340. nr_pages = min(nr_pages, balloon_stats.hotplug_pages);
  341. balloon_stats.hotplug_pages -= nr_pages;
  342. balloon_stats.balloon_hotplug += nr_pages;
  343. return BP_DONE;
  344. }
  345. #endif
  346. if (nr_pages > ARRAY_SIZE(frame_list))
  347. nr_pages = ARRAY_SIZE(frame_list);
  348. for (i = 0; i < nr_pages; i++) {
  349. page = alloc_page(gfp);
  350. if (page == NULL) {
  351. nr_pages = i;
  352. state = BP_EAGAIN;
  353. break;
  354. }
  355. scrub_page(page);
  356. frame_list[i] = page_to_pfn(page);
  357. }
  358. /*
  359. * Ensure that ballooned highmem pages don't have kmaps.
  360. *
  361. * Do this before changing the p2m as kmap_flush_unused()
  362. * reads PTEs to obtain pages (and hence needs the original
  363. * p2m entry).
  364. */
  365. kmap_flush_unused();
  366. /* Update direct mapping, invalidate P2M, and add to balloon. */
  367. for (i = 0; i < nr_pages; i++) {
  368. pfn = frame_list[i];
  369. frame_list[i] = pfn_to_gfn(pfn);
  370. page = pfn_to_page(pfn);
  371. #ifdef CONFIG_XEN_HAVE_PVMMU
  372. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  373. if (!PageHighMem(page)) {
  374. ret = HYPERVISOR_update_va_mapping(
  375. (unsigned long)__va(pfn << PAGE_SHIFT),
  376. __pte_ma(0), 0);
  377. BUG_ON(ret);
  378. }
  379. __set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
  380. }
  381. #endif
  382. balloon_append(page);
  383. }
  384. flush_tlb_all();
  385. set_xen_guest_handle(reservation.extent_start, frame_list);
  386. reservation.nr_extents = nr_pages;
  387. ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
  388. BUG_ON(ret != nr_pages);
  389. balloon_stats.current_pages -= nr_pages;
  390. return state;
  391. }
  392. /*
  393. * As this is a work item it is guaranteed to run as a single instance only.
  394. * We may of course race updates of the target counts (which are protected
  395. * by the balloon lock), or with changes to the Xen hard limit, but we will
  396. * recover from these in time.
  397. */
  398. static void balloon_process(struct work_struct *work)
  399. {
  400. enum bp_state state = BP_DONE;
  401. long credit;
  402. do {
  403. mutex_lock(&balloon_mutex);
  404. credit = current_credit();
  405. if (credit > 0) {
  406. if (balloon_is_inflated())
  407. state = increase_reservation(credit);
  408. else
  409. state = reserve_additional_memory(credit);
  410. }
  411. if (credit < 0)
  412. state = decrease_reservation(-credit, GFP_BALLOON);
  413. state = update_schedule(state);
  414. mutex_unlock(&balloon_mutex);
  415. cond_resched();
  416. } while (credit && state == BP_DONE);
  417. /* Schedule more work if there is some still to be done. */
  418. if (state == BP_EAGAIN)
  419. schedule_delayed_work(&balloon_worker, balloon_stats.schedule_delay * HZ);
  420. }
  421. /* Resets the Xen limit, sets new target, and kicks off processing. */
  422. void balloon_set_new_target(unsigned long target)
  423. {
  424. /* No need for lock. Not read-modify-write updates. */
  425. balloon_stats.target_pages = target;
  426. schedule_delayed_work(&balloon_worker, 0);
  427. }
  428. EXPORT_SYMBOL_GPL(balloon_set_new_target);
  429. /**
  430. * alloc_xenballooned_pages - get pages that have been ballooned out
  431. * @nr_pages: Number of pages to get
  432. * @pages: pages returned
  433. * @highmem: allow highmem pages
  434. * @return 0 on success, error otherwise
  435. */
  436. int alloc_xenballooned_pages(int nr_pages, struct page **pages, bool highmem)
  437. {
  438. int pgno = 0;
  439. struct page *page;
  440. mutex_lock(&balloon_mutex);
  441. while (pgno < nr_pages) {
  442. page = balloon_retrieve(highmem);
  443. if (page && (highmem || !PageHighMem(page))) {
  444. pages[pgno++] = page;
  445. } else {
  446. enum bp_state st;
  447. if (page)
  448. balloon_append(page);
  449. st = decrease_reservation(nr_pages - pgno,
  450. highmem ? GFP_HIGHUSER : GFP_USER);
  451. if (st != BP_DONE)
  452. goto out_undo;
  453. }
  454. }
  455. mutex_unlock(&balloon_mutex);
  456. return 0;
  457. out_undo:
  458. while (pgno)
  459. balloon_append(pages[--pgno]);
  460. /* Free the memory back to the kernel soon */
  461. schedule_delayed_work(&balloon_worker, 0);
  462. mutex_unlock(&balloon_mutex);
  463. return -ENOMEM;
  464. }
  465. EXPORT_SYMBOL(alloc_xenballooned_pages);
  466. /**
  467. * free_xenballooned_pages - return pages retrieved with get_ballooned_pages
  468. * @nr_pages: Number of pages
  469. * @pages: pages to return
  470. */
  471. void free_xenballooned_pages(int nr_pages, struct page **pages)
  472. {
  473. int i;
  474. mutex_lock(&balloon_mutex);
  475. for (i = 0; i < nr_pages; i++) {
  476. if (pages[i])
  477. balloon_append(pages[i]);
  478. }
  479. /* The balloon may be too large now. Shrink it if needed. */
  480. if (current_credit())
  481. schedule_delayed_work(&balloon_worker, 0);
  482. mutex_unlock(&balloon_mutex);
  483. }
  484. EXPORT_SYMBOL(free_xenballooned_pages);
  485. static void __init balloon_add_region(unsigned long start_pfn,
  486. unsigned long pages)
  487. {
  488. unsigned long pfn, extra_pfn_end;
  489. struct page *page;
  490. /*
  491. * If the amount of usable memory has been limited (e.g., with
  492. * the 'mem' command line parameter), don't add pages beyond
  493. * this limit.
  494. */
  495. extra_pfn_end = min(max_pfn, start_pfn + pages);
  496. for (pfn = start_pfn; pfn < extra_pfn_end; pfn++) {
  497. page = pfn_to_page(pfn);
  498. /* totalram_pages and totalhigh_pages do not
  499. include the boot-time balloon extension, so
  500. don't subtract from it. */
  501. __balloon_append(page);
  502. }
  503. }
  504. static int __init balloon_init(void)
  505. {
  506. int i;
  507. if (!xen_domain())
  508. return -ENODEV;
  509. pr_info("Initialising balloon driver\n");
  510. balloon_stats.current_pages = xen_pv_domain()
  511. ? min(xen_start_info->nr_pages - xen_released_pages, max_pfn)
  512. : get_num_physpages();
  513. balloon_stats.target_pages = balloon_stats.current_pages;
  514. balloon_stats.balloon_low = 0;
  515. balloon_stats.balloon_high = 0;
  516. balloon_stats.schedule_delay = 1;
  517. balloon_stats.max_schedule_delay = 32;
  518. balloon_stats.retry_count = 1;
  519. balloon_stats.max_retry_count = RETRY_UNLIMITED;
  520. #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
  521. balloon_stats.hotplug_pages = 0;
  522. balloon_stats.balloon_hotplug = 0;
  523. set_online_page_callback(&xen_online_page);
  524. register_memory_notifier(&xen_memory_nb);
  525. #endif
  526. /*
  527. * Initialize the balloon with pages from the extra memory
  528. * regions (see arch/x86/xen/setup.c).
  529. */
  530. for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++)
  531. if (xen_extra_mem[i].n_pfns)
  532. balloon_add_region(xen_extra_mem[i].start_pfn,
  533. xen_extra_mem[i].n_pfns);
  534. return 0;
  535. }
  536. subsys_initcall(balloon_init);
  537. MODULE_LICENSE("GPL");