189e0f4d2SKip Macy /******************************************************************************
289e0f4d2SKip Macy * balloon.c
389e0f4d2SKip Macy *
489e0f4d2SKip Macy * Xen balloon driver - enables returning/claiming memory to/from Xen.
589e0f4d2SKip Macy *
689e0f4d2SKip Macy * Copyright (c) 2003, B Dragovic
789e0f4d2SKip Macy * Copyright (c) 2003-2004, M Williamson, K Fraser
889e0f4d2SKip Macy * Copyright (c) 2005 Dan M. Smith, IBM Corporation
989e0f4d2SKip Macy *
1089e0f4d2SKip Macy * This file may be distributed separately from the Linux kernel, or
1189e0f4d2SKip Macy * incorporated into other software packages, subject to the following license:
1289e0f4d2SKip Macy *
1389e0f4d2SKip Macy * Permission is hereby granted, free of charge, to any person obtaining a copy
1489e0f4d2SKip Macy * of this source file (the "Software"), to deal in the Software without
1589e0f4d2SKip Macy * restriction, including without limitation the rights to use, copy, modify,
1689e0f4d2SKip Macy * merge, publish, distribute, sublicense, and/or sell copies of the Software,
1789e0f4d2SKip Macy * and to permit persons to whom the Software is furnished to do so, subject to
1889e0f4d2SKip Macy * the following conditions:
1989e0f4d2SKip Macy *
2089e0f4d2SKip Macy * The above copyright notice and this permission notice shall be included in
2189e0f4d2SKip Macy * all copies or substantial portions of the Software.
2289e0f4d2SKip Macy *
2389e0f4d2SKip Macy * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2489e0f4d2SKip Macy * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2589e0f4d2SKip Macy * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2689e0f4d2SKip Macy * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2789e0f4d2SKip Macy * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2889e0f4d2SKip Macy * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2989e0f4d2SKip Macy * IN THE SOFTWARE.
3089e0f4d2SKip Macy */
3189e0f4d2SKip Macy
3289e0f4d2SKip Macy #include <sys/param.h>
3389e0f4d2SKip Macy #include <sys/lock.h>
3412678024SDoug Rabson #include <sys/kernel.h>
3512678024SDoug Rabson #include <sys/kthread.h>
3612678024SDoug Rabson #include <sys/malloc.h>
3789e0f4d2SKip Macy #include <sys/mutex.h>
3812678024SDoug Rabson #include <sys/sysctl.h>
39ae3078d9SRoger Pau Monné #include <sys/module.h>
4089e0f4d2SKip Macy
4112678024SDoug Rabson #include <vm/vm.h>
4212678024SDoug Rabson #include <vm/vm_page.h>
4312678024SDoug Rabson
4476acc41fSJustin T. Gibbs #include <xen/xen-os.h>
4576acc41fSJustin T. Gibbs #include <xen/hypervisor.h>
4676acc41fSJustin T. Gibbs #include <xen/features.h>
4776acc41fSJustin T. Gibbs #include <xen/xenstore/xenstorevar.h>
4876acc41fSJustin T. Gibbs
49d745c852SEd Schouten static MALLOC_DEFINE(M_BALLOON, "Balloon", "Xen Balloon Driver");
5012678024SDoug Rabson
5170046ce6SJustin T. Gibbs /* Convert from KB (as fetched from xenstore) to number of PAGES */
5270046ce6SJustin T. Gibbs #define KB_TO_PAGE_SHIFT (PAGE_SHIFT - 10)
5389e0f4d2SKip Macy
5470046ce6SJustin T. Gibbs struct mtx balloon_mutex;
5589e0f4d2SKip Macy
5612678024SDoug Rabson /* We increase/decrease in batches which fit in a page */
572f9ec994SRoger Pau Monné static xen_pfn_t frame_list[PAGE_SIZE / sizeof(xen_pfn_t)];
5812678024SDoug Rabson
5912678024SDoug Rabson struct balloon_stats {
6089e0f4d2SKip Macy /* We aim for 'current allocation' == 'target allocation'. */
6112678024SDoug Rabson unsigned long current_pages;
6212678024SDoug Rabson unsigned long target_pages;
6389e0f4d2SKip Macy /* We may hit the hard limit in Xen. If we do then we remember it. */
6412678024SDoug Rabson unsigned long hard_limit;
6589e0f4d2SKip Macy /*
6612678024SDoug Rabson * Drivers may alter the memory reservation independently, but they
6712678024SDoug Rabson * must inform the balloon driver so we avoid hitting the hard limit.
6889e0f4d2SKip Macy */
6912678024SDoug Rabson unsigned long driver_pages;
7012678024SDoug Rabson /* Number of pages in high- and low-memory balloons. */
7112678024SDoug Rabson unsigned long balloon_low;
7212678024SDoug Rabson unsigned long balloon_high;
7312678024SDoug Rabson };
7412678024SDoug Rabson
7512678024SDoug Rabson static struct balloon_stats balloon_stats;
7612678024SDoug Rabson #define bs balloon_stats
7712678024SDoug Rabson
7812678024SDoug Rabson SYSCTL_DECL(_dev_xen);
7941fc1ce1SPawel Biernacki static SYSCTL_NODE(_dev_xen, OID_AUTO, balloon,
8041fc1ce1SPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
8141fc1ce1SPawel Biernacki "Balloon");
8212678024SDoug Rabson SYSCTL_ULONG(_dev_xen_balloon, OID_AUTO, current, CTLFLAG_RD,
8312678024SDoug Rabson &bs.current_pages, 0, "Current allocation");
8412678024SDoug Rabson SYSCTL_ULONG(_dev_xen_balloon, OID_AUTO, target, CTLFLAG_RD,
8512678024SDoug Rabson &bs.target_pages, 0, "Target allocation");
8612678024SDoug Rabson SYSCTL_ULONG(_dev_xen_balloon, OID_AUTO, driver_pages, CTLFLAG_RD,
8712678024SDoug Rabson &bs.driver_pages, 0, "Driver pages");
8812678024SDoug Rabson SYSCTL_ULONG(_dev_xen_balloon, OID_AUTO, hard_limit, CTLFLAG_RD,
8912678024SDoug Rabson &bs.hard_limit, 0, "Xen hard limit");
9012678024SDoug Rabson SYSCTL_ULONG(_dev_xen_balloon, OID_AUTO, low_mem, CTLFLAG_RD,
9112678024SDoug Rabson &bs.balloon_low, 0, "Low-mem balloon");
9212678024SDoug Rabson SYSCTL_ULONG(_dev_xen_balloon, OID_AUTO, high_mem, CTLFLAG_RD,
9312678024SDoug Rabson &bs.balloon_high, 0, "High-mem balloon");
9489e0f4d2SKip Macy
9589e0f4d2SKip Macy /* List of ballooned pages, threaded through the mem_map array. */
9668e58ea7SRoger Pau Monné static TAILQ_HEAD(,vm_page) ballooned_pages;
9789e0f4d2SKip Macy
9889e0f4d2SKip Macy /* Main work function, always executed in process context. */
9989e0f4d2SKip Macy static void balloon_process(void *unused);
10089e0f4d2SKip Macy
10189e0f4d2SKip Macy #define IPRINTK(fmt, args...) \
10289e0f4d2SKip Macy printk(KERN_INFO "xen_mem: " fmt, ##args)
10389e0f4d2SKip Macy #define WPRINTK(fmt, args...) \
10489e0f4d2SKip Macy printk(KERN_WARNING "xen_mem: " fmt, ##args)
10589e0f4d2SKip Macy
10689e0f4d2SKip Macy static unsigned long
current_target(void)10789e0f4d2SKip Macy current_target(void)
10889e0f4d2SKip Macy {
10912678024SDoug Rabson unsigned long target = min(bs.target_pages, bs.hard_limit);
11012678024SDoug Rabson if (target > (bs.current_pages + bs.balloon_low + bs.balloon_high))
11112678024SDoug Rabson target = bs.current_pages + bs.balloon_low + bs.balloon_high;
11270046ce6SJustin T. Gibbs return (target);
11389e0f4d2SKip Macy }
11489e0f4d2SKip Macy
11512678024SDoug Rabson static unsigned long
minimum_target(void)11612678024SDoug Rabson minimum_target(void)
11712678024SDoug Rabson {
11812678024SDoug Rabson unsigned long min_pages, curr_pages = current_target();
11912678024SDoug Rabson
12012678024SDoug Rabson #define MB2PAGES(mb) ((mb) << (20 - PAGE_SHIFT))
12170046ce6SJustin T. Gibbs /*
12270046ce6SJustin T. Gibbs * Simple continuous piecewiese linear function:
12312678024SDoug Rabson * max MiB -> min MiB gradient
12412678024SDoug Rabson * 0 0
12512678024SDoug Rabson * 16 16
12612678024SDoug Rabson * 32 24
12712678024SDoug Rabson * 128 72 (1/2)
12812678024SDoug Rabson * 512 168 (1/4)
12912678024SDoug Rabson * 2048 360 (1/8)
13012678024SDoug Rabson * 8192 552 (1/32)
13112678024SDoug Rabson * 32768 1320
13212678024SDoug Rabson * 131072 4392
13312678024SDoug Rabson */
134ed95805eSJohn Baldwin if (realmem < MB2PAGES(128))
135ed95805eSJohn Baldwin min_pages = MB2PAGES(8) + (realmem >> 1);
136ed95805eSJohn Baldwin else if (realmem < MB2PAGES(512))
137ed95805eSJohn Baldwin min_pages = MB2PAGES(40) + (realmem >> 2);
138ed95805eSJohn Baldwin else if (realmem < MB2PAGES(2048))
139ed95805eSJohn Baldwin min_pages = MB2PAGES(104) + (realmem >> 3);
14012678024SDoug Rabson else
141ed95805eSJohn Baldwin min_pages = MB2PAGES(296) + (realmem >> 5);
14212678024SDoug Rabson #undef MB2PAGES
14312678024SDoug Rabson
14412678024SDoug Rabson /* Don't enforce growth */
14570046ce6SJustin T. Gibbs return (min(min_pages, curr_pages));
14612678024SDoug Rabson }
14712678024SDoug Rabson
14889e0f4d2SKip Macy static int
increase_reservation(unsigned long nr_pages)14989e0f4d2SKip Macy increase_reservation(unsigned long nr_pages)
15089e0f4d2SKip Macy {
1512f9ec994SRoger Pau Monné unsigned long i;
15212678024SDoug Rabson vm_page_t page;
15389e0f4d2SKip Macy long rc;
15489e0f4d2SKip Macy struct xen_memory_reservation reservation = {
15589e0f4d2SKip Macy .extent_order = 0,
15689e0f4d2SKip Macy .domid = DOMID_SELF
15789e0f4d2SKip Macy };
15889e0f4d2SKip Macy
15970046ce6SJustin T. Gibbs mtx_assert(&balloon_mutex, MA_OWNED);
16089e0f4d2SKip Macy
16170046ce6SJustin T. Gibbs if (nr_pages > nitems(frame_list))
16270046ce6SJustin T. Gibbs nr_pages = nitems(frame_list);
16389e0f4d2SKip Macy
16468e58ea7SRoger Pau Monné for (page = TAILQ_FIRST(&ballooned_pages), i = 0;
16568e58ea7SRoger Pau Monné i < nr_pages; i++, page = TAILQ_NEXT(page, plinks.q)) {
16668e58ea7SRoger Pau Monné KASSERT(page != NULL, ("ballooned_pages list corrupt"));
16712678024SDoug Rabson frame_list[i] = (VM_PAGE_TO_PHYS(page) >> PAGE_SHIFT);
16812678024SDoug Rabson }
16989e0f4d2SKip Macy
17012678024SDoug Rabson set_xen_guest_handle(reservation.extent_start, frame_list);
17189e0f4d2SKip Macy reservation.nr_extents = nr_pages;
17289e0f4d2SKip Macy rc = HYPERVISOR_memory_op(
17312678024SDoug Rabson XENMEM_populate_physmap, &reservation);
17489e0f4d2SKip Macy if (rc < nr_pages) {
17512678024SDoug Rabson if (rc > 0) {
176e7236a7dSMateusz Guzik int ret __diagused;
17712678024SDoug Rabson
17889e0f4d2SKip Macy /* We hit the Xen hard limit: reprobe. */
17989e0f4d2SKip Macy reservation.nr_extents = rc;
18089e0f4d2SKip Macy ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
18189e0f4d2SKip Macy &reservation);
18212678024SDoug Rabson KASSERT(ret == rc, ("HYPERVISOR_memory_op failed"));
18312678024SDoug Rabson }
18412678024SDoug Rabson if (rc >= 0)
18512678024SDoug Rabson bs.hard_limit = (bs.current_pages + rc -
18612678024SDoug Rabson bs.driver_pages);
18789e0f4d2SKip Macy goto out;
18889e0f4d2SKip Macy }
18989e0f4d2SKip Macy
19089e0f4d2SKip Macy for (i = 0; i < nr_pages; i++) {
19168e58ea7SRoger Pau Monné page = TAILQ_FIRST(&ballooned_pages);
19268e58ea7SRoger Pau Monné KASSERT(page != NULL, ("Unable to get ballooned page"));
19368e58ea7SRoger Pau Monné TAILQ_REMOVE(&ballooned_pages, page, plinks.q);
19468e58ea7SRoger Pau Monné bs.balloon_low--;
19589e0f4d2SKip Macy
196ed95805eSJohn Baldwin KASSERT(xen_feature(XENFEAT_auto_translated_physmap),
19712678024SDoug Rabson ("auto translated physmap but mapping is valid"));
19889e0f4d2SKip Macy
19989e0f4d2SKip Macy vm_page_free(page);
20089e0f4d2SKip Macy }
20189e0f4d2SKip Macy
20212678024SDoug Rabson bs.current_pages += nr_pages;
20389e0f4d2SKip Macy
20489e0f4d2SKip Macy out:
20570046ce6SJustin T. Gibbs return (0);
20689e0f4d2SKip Macy }
20789e0f4d2SKip Macy
20889e0f4d2SKip Macy static int
decrease_reservation(unsigned long nr_pages)20989e0f4d2SKip Macy decrease_reservation(unsigned long nr_pages)
21089e0f4d2SKip Macy {
2112f9ec994SRoger Pau Monné unsigned long i;
21212678024SDoug Rabson vm_page_t page;
21389e0f4d2SKip Macy int need_sleep = 0;
214e7236a7dSMateusz Guzik int ret __diagused;
21589e0f4d2SKip Macy struct xen_memory_reservation reservation = {
21689e0f4d2SKip Macy .extent_order = 0,
21789e0f4d2SKip Macy .domid = DOMID_SELF
21889e0f4d2SKip Macy };
21989e0f4d2SKip Macy
22070046ce6SJustin T. Gibbs mtx_assert(&balloon_mutex, MA_OWNED);
22170046ce6SJustin T. Gibbs
22270046ce6SJustin T. Gibbs if (nr_pages > nitems(frame_list))
22370046ce6SJustin T. Gibbs nr_pages = nitems(frame_list);
22489e0f4d2SKip Macy
22589e0f4d2SKip Macy for (i = 0; i < nr_pages; i++) {
226a4667e09SMark Johnston /*
227a4667e09SMark Johnston * Zero the page, or else we might be leaking important data to
228a4667e09SMark Johnston * other domains on the same host. Xen doesn't scrub ballooned
229a4667e09SMark Johnston * out memory pages, the guest is in charge of making sure that
230a4667e09SMark Johnston * no information is leaked.
231a4667e09SMark Johnston */
232a4667e09SMark Johnston if ((page = vm_page_alloc_noobj(VM_ALLOC_ZERO)) == NULL) {
23389e0f4d2SKip Macy nr_pages = i;
23489e0f4d2SKip Macy need_sleep = 1;
23589e0f4d2SKip Macy break;
23689e0f4d2SKip Macy }
23712678024SDoug Rabson
2382f9ec994SRoger Pau Monné frame_list[i] = (VM_PAGE_TO_PHYS(page) >> PAGE_SHIFT);
23912678024SDoug Rabson
24068e58ea7SRoger Pau Monné TAILQ_INSERT_HEAD(&ballooned_pages, page, plinks.q);
24168e58ea7SRoger Pau Monné bs.balloon_low++;
24289e0f4d2SKip Macy }
24389e0f4d2SKip Macy
24412678024SDoug Rabson set_xen_guest_handle(reservation.extent_start, frame_list);
24589e0f4d2SKip Macy reservation.nr_extents = nr_pages;
24689e0f4d2SKip Macy ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
24712678024SDoug Rabson KASSERT(ret == nr_pages, ("HYPERVISOR_memory_op failed"));
24889e0f4d2SKip Macy
24912678024SDoug Rabson bs.current_pages -= nr_pages;
25089e0f4d2SKip Macy
25112678024SDoug Rabson return (need_sleep);
25289e0f4d2SKip Macy }
25389e0f4d2SKip Macy
25489e0f4d2SKip Macy /*
25589e0f4d2SKip Macy * We avoid multiple worker processes conflicting via the balloon mutex.
25689e0f4d2SKip Macy * We may of course race updates of the target counts (which are protected
25789e0f4d2SKip Macy * by the balloon lock), or with changes to the Xen hard limit, but we will
25889e0f4d2SKip Macy * recover from these in time.
25989e0f4d2SKip Macy */
26089e0f4d2SKip Macy static void
balloon_process(void * unused)26189e0f4d2SKip Macy balloon_process(void *unused)
26289e0f4d2SKip Macy {
26389e0f4d2SKip Macy int need_sleep = 0;
26489e0f4d2SKip Macy long credit;
26589e0f4d2SKip Macy
26612678024SDoug Rabson mtx_lock(&balloon_mutex);
26789e0f4d2SKip Macy for (;;) {
268c4f9a105SAlexander Motin int sleep_time;
269c4f9a105SAlexander Motin
27089e0f4d2SKip Macy do {
27112678024SDoug Rabson credit = current_target() - bs.current_pages;
27289e0f4d2SKip Macy if (credit > 0)
27389e0f4d2SKip Macy need_sleep = (increase_reservation(credit) != 0);
27489e0f4d2SKip Macy if (credit < 0)
27589e0f4d2SKip Macy need_sleep = (decrease_reservation(-credit) != 0);
27689e0f4d2SKip Macy
27789e0f4d2SKip Macy } while ((credit != 0) && !need_sleep);
27889e0f4d2SKip Macy
27989e0f4d2SKip Macy /* Schedule more work if there is some still to be done. */
28012678024SDoug Rabson if (current_target() != bs.current_pages)
281c4f9a105SAlexander Motin sleep_time = hz;
282c4f9a105SAlexander Motin else
283c4f9a105SAlexander Motin sleep_time = 0;
28489e0f4d2SKip Macy
285c4f9a105SAlexander Motin msleep(balloon_process, &balloon_mutex, 0, "balloon",
286c4f9a105SAlexander Motin sleep_time);
28789e0f4d2SKip Macy }
28812678024SDoug Rabson mtx_unlock(&balloon_mutex);
28989e0f4d2SKip Macy }
29089e0f4d2SKip Macy
29189e0f4d2SKip Macy /* Resets the Xen limit, sets new target, and kicks off processing. */
29289e0f4d2SKip Macy static void
set_new_target(unsigned long target)29389e0f4d2SKip Macy set_new_target(unsigned long target)
29489e0f4d2SKip Macy {
29589e0f4d2SKip Macy /* No need for lock. Not read-modify-write updates. */
29612678024SDoug Rabson bs.hard_limit = ~0UL;
29712678024SDoug Rabson bs.target_pages = max(target, minimum_target());
29889e0f4d2SKip Macy wakeup(balloon_process);
29989e0f4d2SKip Macy }
30089e0f4d2SKip Macy
301ff662b5cSJustin T. Gibbs static struct xs_watch target_watch =
30289e0f4d2SKip Macy {
3034e4e43dcSRoger Pau Monné .node = "memory/target",
3044e4e43dcSRoger Pau Monné .max_pending = 1,
30589e0f4d2SKip Macy };
30689e0f4d2SKip Macy
30789e0f4d2SKip Macy /* React to a change in the target key */
30889e0f4d2SKip Macy static void
watch_target(struct xs_watch * watch,const char ** vec,unsigned int len)309ff662b5cSJustin T. Gibbs watch_target(struct xs_watch *watch,
31089e0f4d2SKip Macy const char **vec, unsigned int len)
31189e0f4d2SKip Macy {
31289e0f4d2SKip Macy unsigned long long new_target;
31389e0f4d2SKip Macy int err;
31489e0f4d2SKip Macy
315ff662b5cSJustin T. Gibbs err = xs_scanf(XST_NIL, "memory", "target", NULL,
31612678024SDoug Rabson "%llu", &new_target);
31712678024SDoug Rabson if (err) {
31889e0f4d2SKip Macy /* This is ok (for domain0 at least) - so just return */
31989e0f4d2SKip Macy return;
32089e0f4d2SKip Macy }
32189e0f4d2SKip Macy
32270046ce6SJustin T. Gibbs /*
32370046ce6SJustin T. Gibbs * The given memory/target value is in KiB, so it needs converting to
32470046ce6SJustin T. Gibbs * pages. PAGE_SHIFT converts bytes to pages, hence PAGE_SHIFT - 10.
32589e0f4d2SKip Macy */
32670046ce6SJustin T. Gibbs set_new_target(new_target >> KB_TO_PAGE_SHIFT);
32789e0f4d2SKip Macy }
32889e0f4d2SKip Macy
329ae3078d9SRoger Pau Monné /*------------------ Private Device Attachment Functions --------------------*/
330ae3078d9SRoger Pau Monné /**
331ae3078d9SRoger Pau Monné * \brief Identify instances of this device type in the system.
332ae3078d9SRoger Pau Monné *
333ae3078d9SRoger Pau Monné * \param driver The driver performing this identify action.
334ae3078d9SRoger Pau Monné * \param parent The NewBus parent device for any devices this method adds.
335ae3078d9SRoger Pau Monné */
33689e0f4d2SKip Macy static void
xenballoon_identify(driver_t * driver,device_t parent)337*d48760ffSElliott Mitchell xenballoon_identify(driver_t *driver, device_t parent)
338ae3078d9SRoger Pau Monné {
339ae3078d9SRoger Pau Monné /*
340ae3078d9SRoger Pau Monné * A single device instance for our driver is always present
341ae3078d9SRoger Pau Monné * in a system operating under Xen.
342ae3078d9SRoger Pau Monné */
343ae3078d9SRoger Pau Monné BUS_ADD_CHILD(parent, 0, driver->name, 0);
344ae3078d9SRoger Pau Monné }
345ae3078d9SRoger Pau Monné
346ae3078d9SRoger Pau Monné /**
347453130d9SPedro F. Giffuni * \brief Probe for the existence of the Xen Balloon device
348ae3078d9SRoger Pau Monné *
349ae3078d9SRoger Pau Monné * \param dev NewBus device_t for this Xen control instance.
350ae3078d9SRoger Pau Monné *
351ae3078d9SRoger Pau Monné * \return Always returns 0 indicating success.
352ae3078d9SRoger Pau Monné */
353ae3078d9SRoger Pau Monné static int
xenballoon_probe(device_t dev)354ae3078d9SRoger Pau Monné xenballoon_probe(device_t dev)
355ae3078d9SRoger Pau Monné {
356ae3078d9SRoger Pau Monné
357ae3078d9SRoger Pau Monné device_set_desc(dev, "Xen Balloon Device");
358ae3078d9SRoger Pau Monné return (0);
359ae3078d9SRoger Pau Monné }
360ae3078d9SRoger Pau Monné
361ae3078d9SRoger Pau Monné /**
362ae3078d9SRoger Pau Monné * \brief Attach the Xen Balloon device.
363ae3078d9SRoger Pau Monné *
364ae3078d9SRoger Pau Monné * \param dev NewBus device_t for this Xen control instance.
365ae3078d9SRoger Pau Monné *
366ae3078d9SRoger Pau Monné * \return On success, 0. Otherwise an errno value indicating the
367ae3078d9SRoger Pau Monné * type of failure.
368ae3078d9SRoger Pau Monné */
369ae3078d9SRoger Pau Monné static int
xenballoon_attach(device_t dev)370ae3078d9SRoger Pau Monné xenballoon_attach(device_t dev)
37189e0f4d2SKip Macy {
37289e0f4d2SKip Macy int err;
37389e0f4d2SKip Macy
37412678024SDoug Rabson mtx_init(&balloon_mutex, "balloon_mutex", NULL, MTX_DEF);
37589e0f4d2SKip Macy
376cfa0b7b8SRoger Pau Monné bs.current_pages = realmem;
37712678024SDoug Rabson bs.target_pages = bs.current_pages;
37812678024SDoug Rabson bs.balloon_low = 0;
37912678024SDoug Rabson bs.balloon_high = 0;
38012678024SDoug Rabson bs.driver_pages = 0UL;
38112678024SDoug Rabson bs.hard_limit = ~0UL;
38289e0f4d2SKip Macy
38312678024SDoug Rabson kproc_create(balloon_process, NULL, NULL, 0, 0, "balloon");
38489e0f4d2SKip Macy
38589e0f4d2SKip Macy target_watch.callback = watch_target;
38689e0f4d2SKip Macy
387ae3078d9SRoger Pau Monné err = xs_register_watch(&target_watch);
388ae3078d9SRoger Pau Monné if (err)
389ae3078d9SRoger Pau Monné device_printf(dev,
390ae3078d9SRoger Pau Monné "xenballon: failed to set balloon watcher\n");
39112678024SDoug Rabson
392ae3078d9SRoger Pau Monné return (err);
39389e0f4d2SKip Macy }
394ae3078d9SRoger Pau Monné
395ae3078d9SRoger Pau Monné /*-------------------- Private Device Attachment Data -----------------------*/
396ae3078d9SRoger Pau Monné static device_method_t xenballoon_methods[] = {
397ae3078d9SRoger Pau Monné /* Device interface */
398ae3078d9SRoger Pau Monné DEVMETHOD(device_identify, xenballoon_identify),
399ae3078d9SRoger Pau Monné DEVMETHOD(device_probe, xenballoon_probe),
400ae3078d9SRoger Pau Monné DEVMETHOD(device_attach, xenballoon_attach),
401ae3078d9SRoger Pau Monné
402ae3078d9SRoger Pau Monné DEVMETHOD_END
403ae3078d9SRoger Pau Monné };
404ae3078d9SRoger Pau Monné
405ae3078d9SRoger Pau Monné DEFINE_CLASS_0(xenballoon, xenballoon_driver, xenballoon_methods, 0);
406ae3078d9SRoger Pau Monné
407f929eb1eSJohn Baldwin DRIVER_MODULE(xenballoon, xenstore, xenballoon_driver, NULL, NULL);
408