1 /******************************************************************************
4 * Xen balloon driver - enables returning/claiming memory to/from Xen.
6 * Copyright (c) 2003, B Dragovic
7 * Copyright (c) 2003-2004, M Williamson, K Fraser
8 * Copyright (c) 2005 Dan M. Smith, IBM Corporation
10 * This file may be distributed separately from the Linux kernel, or
11 * incorporated into other software packages, subject to the following license:
13 * Permission is hereby granted, free of charge, to any person obtaining a copy
14 * of this source file (the "Software"), to deal in the Software without
15 * restriction, including without limitation the rights to use, copy, modify,
16 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
17 * and to permit persons to whom the Software is furnished to do so, subject to
18 * the following conditions:
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/kthread.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
41 #include <sys/sysctl.h>
44 #include <vm/vm_page.h>
46 #include <xen/xen-os.h>
47 #include <xen/hypervisor.h>
48 #include <xen/features.h>
49 #include <xen/xenstore/xenstorevar.h>
51 #include <machine/xen/xenvar.h>
53 static MALLOC_DEFINE(M_BALLOON, "Balloon", "Xen Balloon Driver");
55 /* Convert from KB (as fetched from xenstore) to number of PAGES */
56 #define KB_TO_PAGE_SHIFT (PAGE_SHIFT - 10)
58 struct mtx balloon_mutex;
60 /* We increase/decrease in batches which fit in a page */
61 static unsigned long frame_list[PAGE_SIZE / sizeof(unsigned long)];
63 struct balloon_stats {
64 /* We aim for 'current allocation' == 'target allocation'. */
65 unsigned long current_pages;
66 unsigned long target_pages;
67 /* We may hit the hard limit in Xen. If we do then we remember it. */
68 unsigned long hard_limit;
70 * Drivers may alter the memory reservation independently, but they
71 * must inform the balloon driver so we avoid hitting the hard limit.
73 unsigned long driver_pages;
74 /* Number of pages in high- and low-memory balloons. */
75 unsigned long balloon_low;
76 unsigned long balloon_high;
79 static struct balloon_stats balloon_stats;
80 #define bs balloon_stats
82 SYSCTL_DECL(_dev_xen);
83 static SYSCTL_NODE(_dev_xen, OID_AUTO, balloon, CTLFLAG_RD, NULL, "Balloon");
84 SYSCTL_ULONG(_dev_xen_balloon, OID_AUTO, current, CTLFLAG_RD,
85 &bs.current_pages, 0, "Current allocation");
86 SYSCTL_ULONG(_dev_xen_balloon, OID_AUTO, target, CTLFLAG_RD,
87 &bs.target_pages, 0, "Target allocation");
88 SYSCTL_ULONG(_dev_xen_balloon, OID_AUTO, driver_pages, CTLFLAG_RD,
89 &bs.driver_pages, 0, "Driver pages");
90 SYSCTL_ULONG(_dev_xen_balloon, OID_AUTO, hard_limit, CTLFLAG_RD,
91 &bs.hard_limit, 0, "Xen hard limit");
92 SYSCTL_ULONG(_dev_xen_balloon, OID_AUTO, low_mem, CTLFLAG_RD,
93 &bs.balloon_low, 0, "Low-mem balloon");
94 SYSCTL_ULONG(_dev_xen_balloon, OID_AUTO, high_mem, CTLFLAG_RD,
95 &bs.balloon_high, 0, "High-mem balloon");
97 /* List of ballooned pages, threaded through the mem_map array. */
98 static TAILQ_HEAD(,vm_page) ballooned_pages;
100 /* Main work function, always executed in process context. */
101 static void balloon_process(void *unused);
103 #define IPRINTK(fmt, args...) \
104 printk(KERN_INFO "xen_mem: " fmt, ##args)
105 #define WPRINTK(fmt, args...) \
106 printk(KERN_WARNING "xen_mem: " fmt, ##args)
111 unsigned long target = min(bs.target_pages, bs.hard_limit);
112 if (target > (bs.current_pages + bs.balloon_low + bs.balloon_high))
113 target = bs.current_pages + bs.balloon_low + bs.balloon_high;
121 #define max_pfn realmem
123 #define max_pfn HYPERVISOR_shared_info->arch.max_pfn
125 unsigned long min_pages, curr_pages = current_target();
127 #define MB2PAGES(mb) ((mb) << (20 - PAGE_SHIFT))
129 * Simple continuous piecewiese linear function:
130 * max MiB -> min MiB gradient
141 if (max_pfn < MB2PAGES(128))
142 min_pages = MB2PAGES(8) + (max_pfn >> 1);
143 else if (max_pfn < MB2PAGES(512))
144 min_pages = MB2PAGES(40) + (max_pfn >> 2);
145 else if (max_pfn < MB2PAGES(2048))
146 min_pages = MB2PAGES(104) + (max_pfn >> 3);
148 min_pages = MB2PAGES(296) + (max_pfn >> 5);
152 /* Don't enforce growth */
153 return (min(min_pages, curr_pages));
157 increase_reservation(unsigned long nr_pages)
159 unsigned long pfn, i;
162 struct xen_memory_reservation reservation = {
168 mtx_assert(&balloon_mutex, MA_OWNED);
170 if (nr_pages > nitems(frame_list))
171 nr_pages = nitems(frame_list);
173 for (page = TAILQ_FIRST(&ballooned_pages), i = 0;
174 i < nr_pages; i++, page = TAILQ_NEXT(page, plinks.q)) {
175 KASSERT(page != NULL, ("ballooned_pages list corrupt"));
176 frame_list[i] = (VM_PAGE_TO_PHYS(page) >> PAGE_SHIFT);
179 set_xen_guest_handle(reservation.extent_start, frame_list);
180 reservation.nr_extents = nr_pages;
181 rc = HYPERVISOR_memory_op(
182 XENMEM_populate_physmap, &reservation);
187 /* We hit the Xen hard limit: reprobe. */
188 reservation.nr_extents = rc;
189 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
191 KASSERT(ret == rc, ("HYPERVISOR_memory_op failed"));
194 bs.hard_limit = (bs.current_pages + rc -
199 for (i = 0; i < nr_pages; i++) {
200 page = TAILQ_FIRST(&ballooned_pages);
201 KASSERT(page != NULL, ("Unable to get ballooned page"));
202 TAILQ_REMOVE(&ballooned_pages, page, plinks.q);
205 pfn = (VM_PAGE_TO_PHYS(page) >> PAGE_SHIFT);
206 KASSERT((xen_feature(XENFEAT_auto_translated_physmap) ||
207 !phys_to_machine_mapping_valid(pfn)),
208 ("auto translated physmap but mapping is valid"));
210 set_phys_to_machine(pfn, frame_list[i]);
215 bs.current_pages += nr_pages;
222 decrease_reservation(unsigned long nr_pages)
224 unsigned long pfn, i;
228 struct xen_memory_reservation reservation = {
234 mtx_assert(&balloon_mutex, MA_OWNED);
236 if (nr_pages > nitems(frame_list))
237 nr_pages = nitems(frame_list);
239 for (i = 0; i < nr_pages; i++) {
240 if ((page = vm_page_alloc(NULL, 0,
241 VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ |
242 VM_ALLOC_ZERO)) == NULL) {
248 if ((page->flags & PG_ZERO) == 0) {
250 * Zero the page, or else we might be leaking
251 * important data to other domains on the same
252 * host. Xen doesn't scrub ballooned out memory
253 * pages, the guest is in charge of making
254 * sure that no information is leaked.
256 pmap_zero_page(page);
259 pfn = (VM_PAGE_TO_PHYS(page) >> PAGE_SHIFT);
260 frame_list[i] = PFNTOMFN(pfn);
262 set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
263 TAILQ_INSERT_HEAD(&ballooned_pages, page, plinks.q);
267 set_xen_guest_handle(reservation.extent_start, frame_list);
268 reservation.nr_extents = nr_pages;
269 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
270 KASSERT(ret == nr_pages, ("HYPERVISOR_memory_op failed"));
272 bs.current_pages -= nr_pages;
278 * We avoid multiple worker processes conflicting via the balloon mutex.
279 * We may of course race updates of the target counts (which are protected
280 * by the balloon lock), or with changes to the Xen hard limit, but we will
281 * recover from these in time.
284 balloon_process(void *unused)
289 mtx_lock(&balloon_mutex);
294 credit = current_target() - bs.current_pages;
296 need_sleep = (increase_reservation(credit) != 0);
298 need_sleep = (decrease_reservation(-credit) != 0);
300 } while ((credit != 0) && !need_sleep);
302 /* Schedule more work if there is some still to be done. */
303 if (current_target() != bs.current_pages)
308 msleep(balloon_process, &balloon_mutex, 0, "balloon",
311 mtx_unlock(&balloon_mutex);
314 /* Resets the Xen limit, sets new target, and kicks off processing. */
316 set_new_target(unsigned long target)
318 /* No need for lock. Not read-modify-write updates. */
319 bs.hard_limit = ~0UL;
320 bs.target_pages = max(target, minimum_target());
321 wakeup(balloon_process);
324 static struct xs_watch target_watch =
326 .node = "memory/target"
329 /* React to a change in the target key */
331 watch_target(struct xs_watch *watch,
332 const char **vec, unsigned int len)
334 unsigned long long new_target;
337 err = xs_scanf(XST_NIL, "memory", "target", NULL,
338 "%llu", &new_target);
340 /* This is ok (for domain0 at least) - so just return */
345 * The given memory/target value is in KiB, so it needs converting to
346 * pages. PAGE_SHIFT converts bytes to pages, hence PAGE_SHIFT - 10.
348 set_new_target(new_target >> KB_TO_PAGE_SHIFT);
352 balloon_init_watcher(void *arg)
356 if (!is_running_on_xen())
359 err = xs_register_watch(&target_watch);
361 printf("Failed to set balloon watcher\n");
364 SYSINIT(balloon_init_watcher, SI_SUB_PSEUDO, SI_ORDER_ANY,
365 balloon_init_watcher, NULL);
368 balloon_init(void *arg)
374 #define max_pfn HYPERVISOR_shared_info->arch.max_pfn
377 if (!is_running_on_xen())
380 mtx_init(&balloon_mutex, "balloon_mutex", NULL, MTX_DEF);
383 bs.current_pages = min(xen_start_info->nr_pages, max_pfn);
385 bs.current_pages = realmem;
387 bs.target_pages = bs.current_pages;
390 bs.driver_pages = 0UL;
391 bs.hard_limit = ~0UL;
393 kproc_create(balloon_process, NULL, NULL, 0, 0, "balloon");
396 /* Initialise the balloon with excess memory space. */
397 for (pfn = xen_start_info->nr_pages; pfn < max_pfn; pfn++) {
398 page = PHYS_TO_VM_PAGE(pfn << PAGE_SHIFT);
399 TAILQ_INSERT_HEAD(&ballooned_pages, page, plinks.q);
405 target_watch.callback = watch_target;
409 SYSINIT(balloon_init, SI_SUB_PSEUDO, SI_ORDER_ANY, balloon_init, NULL);
411 void balloon_update_driver_allowance(long delta);
414 balloon_update_driver_allowance(long delta)
416 mtx_lock(&balloon_mutex);
417 bs.driver_pages += delta;
418 mtx_unlock(&balloon_mutex);