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 struct balloon_entry {
99 STAILQ_ENTRY(balloon_entry) list;
102 /* List of ballooned pages, threaded through the mem_map array. */
103 static STAILQ_HEAD(,balloon_entry) ballooned_pages;
105 /* Main work function, always executed in process context. */
106 static void balloon_process(void *unused);
108 #define IPRINTK(fmt, args...) \
109 printk(KERN_INFO "xen_mem: " fmt, ##args)
110 #define WPRINTK(fmt, args...) \
111 printk(KERN_WARNING "xen_mem: " fmt, ##args)
113 /* balloon_append: add the given page to the balloon. */
115 balloon_append(vm_page_t page)
117 struct balloon_entry *entry;
119 mtx_assert(&balloon_mutex, MA_OWNED);
121 entry = malloc(sizeof(struct balloon_entry), M_BALLOON, M_NOWAIT);
125 STAILQ_INSERT_HEAD(&ballooned_pages, entry, list);
131 /* balloon_retrieve: rescue a page from the balloon, if it is not empty. */
133 balloon_retrieve(void)
136 struct balloon_entry *entry;
138 mtx_assert(&balloon_mutex, MA_OWNED);
140 if (STAILQ_EMPTY(&ballooned_pages))
143 entry = STAILQ_FIRST(&ballooned_pages);
144 STAILQ_REMOVE_HEAD(&ballooned_pages, list);
147 free(entry, M_BALLOON);
157 unsigned long target = min(bs.target_pages, bs.hard_limit);
158 if (target > (bs.current_pages + bs.balloon_low + bs.balloon_high))
159 target = bs.current_pages + bs.balloon_low + bs.balloon_high;
167 #define max_pfn realmem
169 #define max_pfn HYPERVISOR_shared_info->arch.max_pfn
171 unsigned long min_pages, curr_pages = current_target();
173 #define MB2PAGES(mb) ((mb) << (20 - PAGE_SHIFT))
175 * Simple continuous piecewiese linear function:
176 * max MiB -> min MiB gradient
187 if (max_pfn < MB2PAGES(128))
188 min_pages = MB2PAGES(8) + (max_pfn >> 1);
189 else if (max_pfn < MB2PAGES(512))
190 min_pages = MB2PAGES(40) + (max_pfn >> 2);
191 else if (max_pfn < MB2PAGES(2048))
192 min_pages = MB2PAGES(104) + (max_pfn >> 3);
194 min_pages = MB2PAGES(296) + (max_pfn >> 5);
198 /* Don't enforce growth */
199 return (min(min_pages, curr_pages));
203 increase_reservation(unsigned long nr_pages)
205 unsigned long pfn, i;
206 struct balloon_entry *entry;
209 struct xen_memory_reservation reservation = {
215 mtx_assert(&balloon_mutex, MA_OWNED);
217 if (nr_pages > nitems(frame_list))
218 nr_pages = nitems(frame_list);
220 for (entry = STAILQ_FIRST(&ballooned_pages), i = 0;
221 i < nr_pages; i++, entry = STAILQ_NEXT(entry, list)) {
222 KASSERT(entry, ("ballooned_pages list corrupt"));
224 frame_list[i] = (VM_PAGE_TO_PHYS(page) >> PAGE_SHIFT);
227 set_xen_guest_handle(reservation.extent_start, frame_list);
228 reservation.nr_extents = nr_pages;
229 rc = HYPERVISOR_memory_op(
230 XENMEM_populate_physmap, &reservation);
235 /* We hit the Xen hard limit: reprobe. */
236 reservation.nr_extents = rc;
237 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
239 KASSERT(ret == rc, ("HYPERVISOR_memory_op failed"));
242 bs.hard_limit = (bs.current_pages + rc -
247 for (i = 0; i < nr_pages; i++) {
248 page = balloon_retrieve();
249 KASSERT(page, ("balloon_retrieve failed"));
251 pfn = (VM_PAGE_TO_PHYS(page) >> PAGE_SHIFT);
252 KASSERT((xen_feature(XENFEAT_auto_translated_physmap) ||
253 !phys_to_machine_mapping_valid(pfn)),
254 ("auto translated physmap but mapping is valid"));
256 set_phys_to_machine(pfn, frame_list[i]);
258 vm_page_unwire(page, 0);
262 bs.current_pages += nr_pages;
269 decrease_reservation(unsigned long nr_pages)
271 unsigned long pfn, i;
275 struct xen_memory_reservation reservation = {
281 mtx_assert(&balloon_mutex, MA_OWNED);
283 if (nr_pages > nitems(frame_list))
284 nr_pages = nitems(frame_list);
286 for (i = 0; i < nr_pages; i++) {
287 if ((page = vm_page_alloc(NULL, 0,
288 VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ |
289 VM_ALLOC_WIRED | VM_ALLOC_ZERO)) == NULL) {
295 pfn = (VM_PAGE_TO_PHYS(page) >> PAGE_SHIFT);
296 frame_list[i] = PFNTOMFN(pfn);
298 set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
299 if (balloon_append(page) != 0) {
300 vm_page_unwire(page, 0);
309 set_xen_guest_handle(reservation.extent_start, frame_list);
310 reservation.nr_extents = nr_pages;
311 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
312 KASSERT(ret == nr_pages, ("HYPERVISOR_memory_op failed"));
314 bs.current_pages -= nr_pages;
320 * We avoid multiple worker processes conflicting via the balloon mutex.
321 * We may of course race updates of the target counts (which are protected
322 * by the balloon lock), or with changes to the Xen hard limit, but we will
323 * recover from these in time.
326 balloon_process(void *unused)
331 mtx_lock(&balloon_mutex);
336 credit = current_target() - bs.current_pages;
338 need_sleep = (increase_reservation(credit) != 0);
340 need_sleep = (decrease_reservation(-credit) != 0);
342 } while ((credit != 0) && !need_sleep);
344 /* Schedule more work if there is some still to be done. */
345 if (current_target() != bs.current_pages)
350 msleep(balloon_process, &balloon_mutex, 0, "balloon",
353 mtx_unlock(&balloon_mutex);
356 /* Resets the Xen limit, sets new target, and kicks off processing. */
358 set_new_target(unsigned long target)
360 /* No need for lock. Not read-modify-write updates. */
361 bs.hard_limit = ~0UL;
362 bs.target_pages = max(target, minimum_target());
363 wakeup(balloon_process);
366 static struct xs_watch target_watch =
368 .node = "memory/target"
371 /* React to a change in the target key */
373 watch_target(struct xs_watch *watch,
374 const char **vec, unsigned int len)
376 unsigned long long new_target;
379 err = xs_scanf(XST_NIL, "memory", "target", NULL,
380 "%llu", &new_target);
382 /* This is ok (for domain0 at least) - so just return */
387 * The given memory/target value is in KiB, so it needs converting to
388 * pages. PAGE_SHIFT converts bytes to pages, hence PAGE_SHIFT - 10.
390 set_new_target(new_target >> KB_TO_PAGE_SHIFT);
394 balloon_init_watcher(void *arg)
398 if (!is_running_on_xen())
401 err = xs_register_watch(&target_watch);
403 printf("Failed to set balloon watcher\n");
406 SYSINIT(balloon_init_watcher, SI_SUB_PSEUDO, SI_ORDER_ANY,
407 balloon_init_watcher, NULL);
410 balloon_init(void *arg)
416 #define max_pfn HYPERVISOR_shared_info->arch.max_pfn
419 if (!is_running_on_xen())
422 mtx_init(&balloon_mutex, "balloon_mutex", NULL, MTX_DEF);
425 bs.current_pages = min(xen_start_info->nr_pages, max_pfn);
427 bs.current_pages = realmem;
429 bs.target_pages = bs.current_pages;
432 bs.driver_pages = 0UL;
433 bs.hard_limit = ~0UL;
435 kproc_create(balloon_process, NULL, NULL, 0, 0, "balloon");
438 /* Initialise the balloon with excess memory space. */
439 for (pfn = xen_start_info->nr_pages; pfn < max_pfn; pfn++) {
440 page = PHYS_TO_VM_PAGE(pfn << PAGE_SHIFT);
441 balloon_append(page);
446 target_watch.callback = watch_target;
450 SYSINIT(balloon_init, SI_SUB_PSEUDO, SI_ORDER_ANY, balloon_init, NULL);
452 void balloon_update_driver_allowance(long delta);
455 balloon_update_driver_allowance(long delta)
457 mtx_lock(&balloon_mutex);
458 bs.driver_pages += delta;
459 mtx_unlock(&balloon_mutex);