balloon.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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/kernel.h>
  40. #include <linux/sched.h>
  41. #include <linux/errno.h>
  42. #include <linux/module.h>
  43. #include <linux/mm.h>
  44. #include <linux/bootmem.h>
  45. #include <linux/pagemap.h>
  46. #include <linux/highmem.h>
  47. #include <linux/mutex.h>
  48. #include <linux/list.h>
  49. #include <linux/gfp.h>
  50. #include <linux/notifier.h>
  51. #include <linux/memory.h>
  52. #include <linux/memory_hotplug.h>
  53. #include <asm/page.h>
  54. #include <asm/pgalloc.h>
  55. #include <asm/pgtable.h>
  56. #include <asm/tlb.h>
  57. #include <asm/xen/hypervisor.h>
  58. #include <asm/xen/hypercall.h>
  59. #include <xen/xen.h>
  60. #include <xen/interface/xen.h>
  61. #include <xen/interface/memory.h>
  62. #include <xen/balloon.h>
  63. #include <xen/features.h>
  64. #include <xen/page.h>
  65. /*
  66. * balloon_process() state:
  67. *
  68. * BP_DONE: done or nothing to do,
  69. * BP_EAGAIN: error, go to sleep,
  70. * BP_ECANCELED: error, balloon operation canceled.
  71. */
  72. enum bp_state {
  73. BP_DONE,
  74. BP_EAGAIN,
  75. BP_ECANCELED
  76. };
  77. static DEFINE_MUTEX(balloon_mutex);
  78. struct balloon_stats balloon_stats;
  79. EXPORT_SYMBOL_GPL(balloon_stats);
  80. /* We increase/decrease in batches which fit in a page */
  81. static xen_pfn_t frame_list[PAGE_SIZE / sizeof(unsigned long)];
  82. /* List of ballooned pages, threaded through the mem_map array. */
  83. static LIST_HEAD(ballooned_pages);
  84. /* Main work function, always executed in process context. */
  85. static void balloon_process(struct work_struct *work);
  86. static DECLARE_DELAYED_WORK(balloon_worker, balloon_process);
  87. /* When ballooning out (allocating memory to return to Xen) we don't really
  88. want the kernel to try too hard since that can trigger the oom killer. */
  89. #define GFP_BALLOON \
  90. (GFP_HIGHUSER | __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC)
  91. static void scrub_page(struct page *page)
  92. {
  93. #ifdef CONFIG_XEN_SCRUB_PAGES
  94. clear_highpage(page);
  95. #endif
  96. }
  97. /* balloon_append: add the given page to the balloon. */
  98. static void __balloon_append(struct page *page)
  99. {
  100. /* Lowmem is re-populated first, so highmem pages go at list tail. */
  101. if (PageHighMem(page)) {
  102. list_add_tail(&page->lru, &ballooned_pages);
  103. balloon_stats.balloon_high++;
  104. } else {
  105. list_add(&page->lru, &ballooned_pages);
  106. balloon_stats.balloon_low++;
  107. }
  108. }
  109. static void balloon_append(struct page *page)
  110. {
  111. __balloon_append(page);
  112. adjust_managed_page_count(page, -1);
  113. }
  114. /* balloon_retrieve: rescue a page from the balloon, if it is not empty. */
  115. static struct page *balloon_retrieve(bool prefer_highmem)
  116. {
  117. struct page *page;
  118. if (list_empty(&ballooned_pages))
  119. return NULL;
  120. if (prefer_highmem)
  121. page = list_entry(ballooned_pages.prev, struct page, lru);
  122. else
  123. page = list_entry(ballooned_pages.next, struct page, lru);
  124. list_del(&page->lru);
  125. if (PageHighMem(page))
  126. balloon_stats.balloon_high--;
  127. else
  128. balloon_stats.balloon_low--;
  129. adjust_managed_page_count(page, 1);
  130. return page;
  131. }
  132. static struct page *balloon_first_page(void)
  133. {
  134. if (list_empty(&ballooned_pages))
  135. return NULL;
  136. return list_entry(ballooned_pages.next, struct page, lru);
  137. }
  138. static struct page *balloon_next_page(struct page *page)
  139. {
  140. struct list_head *next = page->lru.next;
  141. if (next == &ballooned_pages)
  142. return NULL;
  143. return list_entry(next, struct page, lru);
  144. }
  145. static enum bp_state update_schedule(enum bp_state state)
  146. {
  147. if (state == BP_DONE) {
  148. balloon_stats.schedule_delay = 1;
  149. balloon_stats.retry_count = 1;
  150. return BP_DONE;
  151. }
  152. ++balloon_stats.retry_count;
  153. if (balloon_stats.max_retry_count != RETRY_UNLIMITED &&
  154. balloon_stats.retry_count > balloon_stats.max_retry_count) {
  155. balloon_stats.schedule_delay = 1;
  156. balloon_stats.retry_count = 1;
  157. return BP_ECANCELED;
  158. }
  159. balloon_stats.schedule_delay <<= 1;
  160. if (balloon_stats.schedule_delay > balloon_stats.max_schedule_delay)
  161. balloon_stats.schedule_delay = balloon_stats.max_schedule_delay;
  162. return BP_EAGAIN;
  163. }
  164. #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
  165. static long current_credit(void)
  166. {
  167. return balloon_stats.target_pages - balloon_stats.current_pages -
  168. balloon_stats.hotplug_pages;
  169. }
  170. static bool balloon_is_inflated(void)
  171. {
  172. if (balloon_stats.balloon_low || balloon_stats.balloon_high ||
  173. balloon_stats.balloon_hotplug)
  174. return true;
  175. else
  176. return false;
  177. }
  178. /*
  179. * reserve_additional_memory() adds memory region of size >= credit above
  180. * max_pfn. New region is section aligned and size is modified to be multiple
  181. * of section size. Those features allow optimal use of address space and
  182. * establish proper alignment when this function is called first time after
  183. * boot (last section not fully populated at boot time contains unused memory
  184. * pages with PG_reserved bit not set; online_pages_range() does not allow page
  185. * onlining in whole range if first onlined page does not have PG_reserved
  186. * bit set). Real size of added memory is established at page onlining stage.
  187. */
  188. static enum bp_state reserve_additional_memory(long credit)
  189. {
  190. int nid, rc;
  191. u64 hotplug_start_paddr;
  192. unsigned long balloon_hotplug = credit;
  193. hotplug_start_paddr = PFN_PHYS(SECTION_ALIGN_UP(max_pfn));
  194. balloon_hotplug = round_up(balloon_hotplug, PAGES_PER_SECTION);
  195. nid = memory_add_physaddr_to_nid(hotplug_start_paddr);
  196. rc = add_memory(nid, hotplug_start_paddr, balloon_hotplug << PAGE_SHIFT);
  197. if (rc) {
  198. pr_info("%s: add_memory() failed: %i\n", __func__, rc);
  199. return BP_EAGAIN;
  200. }
  201. balloon_hotplug -= credit;
  202. balloon_stats.hotplug_pages += credit;
  203. balloon_stats.balloon_hotplug = balloon_hotplug;
  204. return BP_DONE;
  205. }
  206. static void xen_online_page(struct page *page)
  207. {
  208. __online_page_set_limits(page);
  209. mutex_lock(&balloon_mutex);
  210. __balloon_append(page);
  211. if (balloon_stats.hotplug_pages)
  212. --balloon_stats.hotplug_pages;
  213. else
  214. --balloon_stats.balloon_hotplug;
  215. mutex_unlock(&balloon_mutex);
  216. }
  217. static int xen_memory_notifier(struct notifier_block *nb, unsigned long val, void *v)
  218. {
  219. if (val == MEM_ONLINE)
  220. schedule_delayed_work(&balloon_worker, 0);
  221. return NOTIFY_OK;
  222. }
  223. static struct notifier_block xen_memory_nb = {
  224. .notifier_call = xen_memory_notifier,
  225. .priority = 0
  226. };
  227. #else
  228. static long current_credit(void)
  229. {
  230. unsigned long target = balloon_stats.target_pages;
  231. target = min(target,
  232. balloon_stats.current_pages +
  233. balloon_stats.balloon_low +
  234. balloon_stats.balloon_high);
  235. return target - balloon_stats.current_pages;
  236. }
  237. static bool balloon_is_inflated(void)
  238. {
  239. if (balloon_stats.balloon_low || balloon_stats.balloon_high)
  240. return true;
  241. else
  242. return false;
  243. }
  244. static enum bp_state reserve_additional_memory(long credit)
  245. {
  246. balloon_stats.target_pages = balloon_stats.current_pages;
  247. return BP_DONE;
  248. }
  249. #endif /* CONFIG_XEN_BALLOON_MEMORY_HOTPLUG */
  250. static enum bp_state increase_reservation(unsigned long nr_pages)
  251. {
  252. int rc;
  253. unsigned long pfn, i;
  254. struct page *page;
  255. struct xen_memory_reservation reservation = {
  256. .address_bits = 0,
  257. .extent_order = 0,
  258. .domid = DOMID_SELF
  259. };
  260. #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
  261. if (!balloon_stats.balloon_low && !balloon_stats.balloon_high) {
  262. nr_pages = min(nr_pages, balloon_stats.balloon_hotplug);
  263. balloon_stats.hotplug_pages += nr_pages;
  264. balloon_stats.balloon_hotplug -= nr_pages;
  265. return BP_DONE;
  266. }
  267. #endif
  268. if (nr_pages > ARRAY_SIZE(frame_list))
  269. nr_pages = ARRAY_SIZE(frame_list);
  270. page = balloon_first_page();
  271. for (i = 0; i < nr_pages; i++) {
  272. if (!page) {
  273. nr_pages = i;
  274. break;
  275. }
  276. frame_list[i] = page_to_pfn(page);
  277. page = balloon_next_page(page);
  278. }
  279. set_xen_guest_handle(reservation.extent_start, frame_list);
  280. reservation.nr_extents = nr_pages;
  281. rc = HYPERVISOR_memory_op(XENMEM_populate_physmap, &reservation);
  282. if (rc <= 0)
  283. return BP_EAGAIN;
  284. for (i = 0; i < rc; i++) {
  285. page = balloon_retrieve(false);
  286. BUG_ON(page == NULL);
  287. pfn = page_to_pfn(page);
  288. BUG_ON(!xen_feature(XENFEAT_auto_translated_physmap) &&
  289. phys_to_machine_mapping_valid(pfn));
  290. set_phys_to_machine(pfn, frame_list[i]);
  291. #ifdef CONFIG_XEN_HAVE_PVMMU
  292. /* Link back into the page tables if not highmem. */
  293. if (xen_pv_domain() && !PageHighMem(page)) {
  294. int ret;
  295. ret = HYPERVISOR_update_va_mapping(
  296. (unsigned long)__va(pfn << PAGE_SHIFT),
  297. mfn_pte(frame_list[i], PAGE_KERNEL),
  298. 0);
  299. BUG_ON(ret);
  300. }
  301. #endif
  302. /* Relinquish the page back to the allocator. */
  303. __free_reserved_page(page);
  304. }
  305. balloon_stats.current_pages += rc;
  306. return BP_DONE;
  307. }
  308. static enum bp_state decrease_reservation(unsigned long nr_pages, gfp_t gfp)
  309. {
  310. enum bp_state state = BP_DONE;
  311. unsigned long pfn, i;
  312. struct page *page;
  313. int ret;
  314. struct xen_memory_reservation reservation = {
  315. .address_bits = 0,
  316. .extent_order = 0,
  317. .domid = DOMID_SELF
  318. };
  319. #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
  320. if (balloon_stats.hotplug_pages) {
  321. nr_pages = min(nr_pages, balloon_stats.hotplug_pages);
  322. balloon_stats.hotplug_pages -= nr_pages;
  323. balloon_stats.balloon_hotplug += nr_pages;
  324. return BP_DONE;
  325. }
  326. #endif
  327. if (nr_pages > ARRAY_SIZE(frame_list))
  328. nr_pages = ARRAY_SIZE(frame_list);
  329. for (i = 0; i < nr_pages; i++) {
  330. page = alloc_page(gfp);
  331. if (page == NULL) {
  332. nr_pages = i;
  333. state = BP_EAGAIN;
  334. break;
  335. }
  336. pfn = page_to_pfn(page);
  337. frame_list[i] = pfn_to_mfn(pfn);
  338. scrub_page(page);
  339. #ifdef CONFIG_XEN_HAVE_PVMMU
  340. if (xen_pv_domain() && !PageHighMem(page)) {
  341. ret = HYPERVISOR_update_va_mapping(
  342. (unsigned long)__va(pfn << PAGE_SHIFT),
  343. __pte_ma(0), 0);
  344. BUG_ON(ret);
  345. }
  346. #endif
  347. }
  348. /* Ensure that ballooned highmem pages don't have kmaps. */
  349. kmap_flush_unused();
  350. flush_tlb_all();
  351. /* No more mappings: invalidate P2M and add to balloon. */
  352. for (i = 0; i < nr_pages; i++) {
  353. pfn = mfn_to_pfn(frame_list[i]);
  354. __set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
  355. balloon_append(pfn_to_page(pfn));
  356. }
  357. set_xen_guest_handle(reservation.extent_start, frame_list);
  358. reservation.nr_extents = nr_pages;
  359. ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
  360. BUG_ON(ret != nr_pages);
  361. balloon_stats.current_pages -= nr_pages;
  362. return state;
  363. }
  364. /*
  365. * We avoid multiple worker processes conflicting via the balloon mutex.
  366. * We may of course race updates of the target counts (which are protected
  367. * by the balloon lock), or with changes to the Xen hard limit, but we will
  368. * recover from these in time.
  369. */
  370. static void balloon_process(struct work_struct *work)
  371. {
  372. enum bp_state state = BP_DONE;
  373. long credit;
  374. mutex_lock(&balloon_mutex);
  375. do {
  376. credit = current_credit();
  377. if (credit > 0) {
  378. if (balloon_is_inflated())
  379. state = increase_reservation(credit);
  380. else
  381. state = reserve_additional_memory(credit);
  382. }
  383. if (credit < 0)
  384. state = decrease_reservation(-credit, GFP_BALLOON);
  385. state = update_schedule(state);
  386. #ifndef CONFIG_PREEMPT
  387. if (need_resched())
  388. schedule();
  389. #endif
  390. } while (credit && state == BP_DONE);
  391. /* Schedule more work if there is some still to be done. */
  392. if (state == BP_EAGAIN)
  393. schedule_delayed_work(&balloon_worker, balloon_stats.schedule_delay * HZ);
  394. mutex_unlock(&balloon_mutex);
  395. }
  396. /* Resets the Xen limit, sets new target, and kicks off processing. */
  397. void balloon_set_new_target(unsigned long target)
  398. {
  399. /* No need for lock. Not read-modify-write updates. */
  400. balloon_stats.target_pages = target;
  401. schedule_delayed_work(&balloon_worker, 0);
  402. }
  403. EXPORT_SYMBOL_GPL(balloon_set_new_target);
  404. /**
  405. * alloc_xenballooned_pages - get pages that have been ballooned out
  406. * @nr_pages: Number of pages to get
  407. * @pages: pages returned
  408. * @highmem: allow highmem pages
  409. * @return 0 on success, error otherwise
  410. */
  411. int alloc_xenballooned_pages(int nr_pages, struct page **pages, bool highmem)
  412. {
  413. int pgno = 0;
  414. struct page *page;
  415. mutex_lock(&balloon_mutex);
  416. while (pgno < nr_pages) {
  417. page = balloon_retrieve(highmem);
  418. if (page && (highmem || !PageHighMem(page))) {
  419. pages[pgno++] = page;
  420. } else {
  421. enum bp_state st;
  422. if (page)
  423. balloon_append(page);
  424. st = decrease_reservation(nr_pages - pgno,
  425. highmem ? GFP_HIGHUSER : GFP_USER);
  426. if (st != BP_DONE)
  427. goto out_undo;
  428. }
  429. }
  430. mutex_unlock(&balloon_mutex);
  431. return 0;
  432. out_undo:
  433. while (pgno)
  434. balloon_append(pages[--pgno]);
  435. /* Free the memory back to the kernel soon */
  436. schedule_delayed_work(&balloon_worker, 0);
  437. mutex_unlock(&balloon_mutex);
  438. return -ENOMEM;
  439. }
  440. EXPORT_SYMBOL(alloc_xenballooned_pages);
  441. /**
  442. * free_xenballooned_pages - return pages retrieved with get_ballooned_pages
  443. * @nr_pages: Number of pages
  444. * @pages: pages to return
  445. */
  446. void free_xenballooned_pages(int nr_pages, struct page **pages)
  447. {
  448. int i;
  449. mutex_lock(&balloon_mutex);
  450. for (i = 0; i < nr_pages; i++) {
  451. if (pages[i])
  452. balloon_append(pages[i]);
  453. }
  454. /* The balloon may be too large now. Shrink it if needed. */
  455. if (current_credit())
  456. schedule_delayed_work(&balloon_worker, 0);
  457. mutex_unlock(&balloon_mutex);
  458. }
  459. EXPORT_SYMBOL(free_xenballooned_pages);
  460. static void __init balloon_add_region(unsigned long start_pfn,
  461. unsigned long pages)
  462. {
  463. unsigned long pfn, extra_pfn_end;
  464. struct page *page;
  465. /*
  466. * If the amount of usable memory has been limited (e.g., with
  467. * the 'mem' command line parameter), don't add pages beyond
  468. * this limit.
  469. */
  470. extra_pfn_end = min(max_pfn, start_pfn + pages);
  471. for (pfn = start_pfn; pfn < extra_pfn_end; pfn++) {
  472. page = pfn_to_page(pfn);
  473. /* totalram_pages and totalhigh_pages do not
  474. include the boot-time balloon extension, so
  475. don't subtract from it. */
  476. __balloon_append(page);
  477. }
  478. }
  479. static int __init balloon_init(void)
  480. {
  481. int i;
  482. if (!xen_domain())
  483. return -ENODEV;
  484. pr_info("Initialising balloon driver\n");
  485. balloon_stats.current_pages = xen_pv_domain()
  486. ? min(xen_start_info->nr_pages - xen_released_pages, max_pfn)
  487. : max_pfn;
  488. balloon_stats.target_pages = balloon_stats.current_pages;
  489. balloon_stats.balloon_low = 0;
  490. balloon_stats.balloon_high = 0;
  491. balloon_stats.schedule_delay = 1;
  492. balloon_stats.max_schedule_delay = 32;
  493. balloon_stats.retry_count = 1;
  494. balloon_stats.max_retry_count = RETRY_UNLIMITED;
  495. #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
  496. balloon_stats.hotplug_pages = 0;
  497. balloon_stats.balloon_hotplug = 0;
  498. set_online_page_callback(&xen_online_page);
  499. register_memory_notifier(&xen_memory_nb);
  500. #endif
  501. /*
  502. * Initialize the balloon with pages from the extra memory
  503. * regions (see arch/x86/xen/setup.c).
  504. */
  505. for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++)
  506. if (xen_extra_mem[i].size)
  507. balloon_add_region(PFN_UP(xen_extra_mem[i].start),
  508. PFN_DOWN(xen_extra_mem[i].size));
  509. return 0;
  510. }
  511. subsys_initcall(balloon_init);
  512. MODULE_LICENSE("GPL");