1 /******************************************************************************
2 * balloon.c
3 *
4 * Xen balloon driver - enables returning/claiming memory to/from Xen.
5 *
6 * Copyright (c) 2003, B Dragovic
7 * Copyright (c) 2003-2004, M Williamson, K Fraser
8 * Copyright (c) 2005 Dan M. Smith, IBM Corporation
9 *
10 * This file may be distributed separately from the Linux kernel, or
11 * incorporated into other software packages, subject to the following license:
12 *
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:
19 *
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
22 *
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
29 * IN THE SOFTWARE.
30 */
31
32 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/lock.h>
35 #include <sys/kernel.h>
36 #include <sys/kthread.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/sysctl.h>
40 #include <sys/module.h>
41
42 #include <vm/vm.h>
43 #include <vm/vm_page.h>
44
45 #include <xen/xen-os.h>
46 #include <xen/hypervisor.h>
47 #include <xen/features.h>
48 #include <xen/xenstore/xenstorevar.h>
49
50 static MALLOC_DEFINE(M_BALLOON, "Balloon", "Xen Balloon Driver");
51
52 /* Convert from KB (as fetched from xenstore) to number of PAGES */
53 #define KB_TO_PAGE_SHIFT (PAGE_SHIFT - 10)
54
55 struct mtx balloon_mutex;
56
57 /* We increase/decrease in batches which fit in a page */
58 static xen_pfn_t frame_list[PAGE_SIZE / sizeof(xen_pfn_t)];
59
60 struct balloon_stats {
61 /* We aim for 'current allocation' == 'target allocation'. */
62 unsigned long current_pages;
63 unsigned long target_pages;
64 /* We may hit the hard limit in Xen. If we do then we remember it. */
65 unsigned long hard_limit;
66 /*
67 * Drivers may alter the memory reservation independently, but they
68 * must inform the balloon driver so we avoid hitting the hard limit.
69 */
70 unsigned long driver_pages;
71 /* Number of pages in high- and low-memory balloons. */
72 unsigned long balloon_low;
73 unsigned long balloon_high;
74 };
75
76 static struct balloon_stats balloon_stats;
77 #define bs balloon_stats
78
79 SYSCTL_DECL(_dev_xen);
80 static SYSCTL_NODE(_dev_xen, OID_AUTO, balloon,
81 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
82 "Balloon");
83 SYSCTL_ULONG(_dev_xen_balloon, OID_AUTO, current, CTLFLAG_RD,
84 &bs.current_pages, 0, "Current allocation");
85 SYSCTL_ULONG(_dev_xen_balloon, OID_AUTO, target, CTLFLAG_RD,
86 &bs.target_pages, 0, "Target allocation");
87 SYSCTL_ULONG(_dev_xen_balloon, OID_AUTO, driver_pages, CTLFLAG_RD,
88 &bs.driver_pages, 0, "Driver pages");
89 SYSCTL_ULONG(_dev_xen_balloon, OID_AUTO, hard_limit, CTLFLAG_RD,
90 &bs.hard_limit, 0, "Xen hard limit");
91 SYSCTL_ULONG(_dev_xen_balloon, OID_AUTO, low_mem, CTLFLAG_RD,
92 &bs.balloon_low, 0, "Low-mem balloon");
93 SYSCTL_ULONG(_dev_xen_balloon, OID_AUTO, high_mem, CTLFLAG_RD,
94 &bs.balloon_high, 0, "High-mem balloon");
95
96 /* List of ballooned pages, threaded through the mem_map array. */
97 static TAILQ_HEAD(,vm_page) ballooned_pages;
98
99 /* Main work function, always executed in process context. */
100 static void balloon_process(void *unused);
101
102 #define IPRINTK(fmt, args...) \
103 printk(KERN_INFO "xen_mem: " fmt, ##args)
104 #define WPRINTK(fmt, args...) \
105 printk(KERN_WARNING "xen_mem: " fmt, ##args)
106
107 static unsigned long
current_target(void)108 current_target(void)
109 {
110 unsigned long target = min(bs.target_pages, bs.hard_limit);
111 if (target > (bs.current_pages + bs.balloon_low + bs.balloon_high))
112 target = bs.current_pages + bs.balloon_low + bs.balloon_high;
113 return (target);
114 }
115
116 static unsigned long
minimum_target(void)117 minimum_target(void)
118 {
119 unsigned long min_pages, curr_pages = current_target();
120
121 #define MB2PAGES(mb) ((mb) << (20 - PAGE_SHIFT))
122 /*
123 * Simple continuous piecewiese linear function:
124 * max MiB -> min MiB gradient
125 * 0 0
126 * 16 16
127 * 32 24
128 * 128 72 (1/2)
129 * 512 168 (1/4)
130 * 2048 360 (1/8)
131 * 8192 552 (1/32)
132 * 32768 1320
133 * 131072 4392
134 */
135 if (realmem < MB2PAGES(128))
136 min_pages = MB2PAGES(8) + (realmem >> 1);
137 else if (realmem < MB2PAGES(512))
138 min_pages = MB2PAGES(40) + (realmem >> 2);
139 else if (realmem < MB2PAGES(2048))
140 min_pages = MB2PAGES(104) + (realmem >> 3);
141 else
142 min_pages = MB2PAGES(296) + (realmem >> 5);
143 #undef MB2PAGES
144
145 /* Don't enforce growth */
146 return (min(min_pages, curr_pages));
147 }
148
149 static int
increase_reservation(unsigned long nr_pages)150 increase_reservation(unsigned long nr_pages)
151 {
152 unsigned long i;
153 vm_page_t page;
154 long rc;
155 struct xen_memory_reservation reservation = {
156 .extent_order = 0,
157 .domid = DOMID_SELF
158 };
159
160 mtx_assert(&balloon_mutex, MA_OWNED);
161
162 if (nr_pages > nitems(frame_list))
163 nr_pages = nitems(frame_list);
164
165 for (page = TAILQ_FIRST(&ballooned_pages), i = 0;
166 i < nr_pages; i++, page = TAILQ_NEXT(page, plinks.q)) {
167 KASSERT(page != NULL, ("ballooned_pages list corrupt"));
168 frame_list[i] = (VM_PAGE_TO_PHYS(page) >> PAGE_SHIFT);
169 }
170
171 set_xen_guest_handle(reservation.extent_start, frame_list);
172 reservation.nr_extents = nr_pages;
173 rc = HYPERVISOR_memory_op(
174 XENMEM_populate_physmap, &reservation);
175 if (rc < nr_pages) {
176 if (rc > 0) {
177 int ret __diagused;
178
179 /* We hit the Xen hard limit: reprobe. */
180 reservation.nr_extents = rc;
181 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
182 &reservation);
183 KASSERT(ret == rc, ("HYPERVISOR_memory_op failed"));
184 }
185 if (rc >= 0)
186 bs.hard_limit = (bs.current_pages + rc -
187 bs.driver_pages);
188 goto out;
189 }
190
191 for (i = 0; i < nr_pages; i++) {
192 page = TAILQ_FIRST(&ballooned_pages);
193 KASSERT(page != NULL, ("Unable to get ballooned page"));
194 TAILQ_REMOVE(&ballooned_pages, page, plinks.q);
195 bs.balloon_low--;
196
197 KASSERT(xen_feature(XENFEAT_auto_translated_physmap),
198 ("auto translated physmap but mapping is valid"));
199
200 vm_page_free(page);
201 }
202
203 bs.current_pages += nr_pages;
204
205 out:
206 return (0);
207 }
208
209 static int
decrease_reservation(unsigned long nr_pages)210 decrease_reservation(unsigned long nr_pages)
211 {
212 unsigned long i;
213 vm_page_t page;
214 int need_sleep = 0;
215 int ret __diagused;
216 struct xen_memory_reservation reservation = {
217 .extent_order = 0,
218 .domid = DOMID_SELF
219 };
220
221 mtx_assert(&balloon_mutex, MA_OWNED);
222
223 if (nr_pages > nitems(frame_list))
224 nr_pages = nitems(frame_list);
225
226 for (i = 0; i < nr_pages; i++) {
227 /*
228 * Zero the page, or else we might be leaking important data to
229 * other domains on the same host. Xen doesn't scrub ballooned
230 * out memory pages, the guest is in charge of making sure that
231 * no information is leaked.
232 */
233 if ((page = vm_page_alloc_noobj(VM_ALLOC_ZERO)) == NULL) {
234 nr_pages = i;
235 need_sleep = 1;
236 break;
237 }
238
239 frame_list[i] = (VM_PAGE_TO_PHYS(page) >> PAGE_SHIFT);
240
241 TAILQ_INSERT_HEAD(&ballooned_pages, page, plinks.q);
242 bs.balloon_low++;
243 }
244
245 set_xen_guest_handle(reservation.extent_start, frame_list);
246 reservation.nr_extents = nr_pages;
247 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
248 KASSERT(ret == nr_pages, ("HYPERVISOR_memory_op failed"));
249
250 bs.current_pages -= nr_pages;
251
252 return (need_sleep);
253 }
254
255 /*
256 * We avoid multiple worker processes conflicting via the balloon mutex.
257 * We may of course race updates of the target counts (which are protected
258 * by the balloon lock), or with changes to the Xen hard limit, but we will
259 * recover from these in time.
260 */
261 static void
balloon_process(void * unused)262 balloon_process(void *unused)
263 {
264 int need_sleep = 0;
265 long credit;
266
267 mtx_lock(&balloon_mutex);
268 for (;;) {
269 int sleep_time;
270
271 do {
272 credit = current_target() - bs.current_pages;
273 if (credit > 0)
274 need_sleep = (increase_reservation(credit) != 0);
275 if (credit < 0)
276 need_sleep = (decrease_reservation(-credit) != 0);
277
278 } while ((credit != 0) && !need_sleep);
279
280 /* Schedule more work if there is some still to be done. */
281 if (current_target() != bs.current_pages)
282 sleep_time = hz;
283 else
284 sleep_time = 0;
285
286 msleep(balloon_process, &balloon_mutex, 0, "balloon",
287 sleep_time);
288 }
289 mtx_unlock(&balloon_mutex);
290 }
291
292 /* Resets the Xen limit, sets new target, and kicks off processing. */
293 static void
set_new_target(unsigned long target)294 set_new_target(unsigned long target)
295 {
296 /* No need for lock. Not read-modify-write updates. */
297 bs.hard_limit = ~0UL;
298 bs.target_pages = max(target, minimum_target());
299 wakeup(balloon_process);
300 }
301
302 static struct xs_watch target_watch =
303 {
304 .node = "memory/target",
305 .max_pending = 1,
306 };
307
308 /* React to a change in the target key */
309 static void
watch_target(struct xs_watch * watch,const char ** vec,unsigned int len)310 watch_target(struct xs_watch *watch,
311 const char **vec, unsigned int len)
312 {
313 unsigned long long new_target;
314 int err;
315
316 err = xs_scanf(XST_NIL, "memory", "target", NULL,
317 "%llu", &new_target);
318 if (err) {
319 /* This is ok (for domain0 at least) - so just return */
320 return;
321 }
322
323 /*
324 * The given memory/target value is in KiB, so it needs converting to
325 * pages. PAGE_SHIFT converts bytes to pages, hence PAGE_SHIFT - 10.
326 */
327 set_new_target(new_target >> KB_TO_PAGE_SHIFT);
328 }
329
330 /*------------------ Private Device Attachment Functions --------------------*/
331 /**
332 * \brief Identify instances of this device type in the system.
333 *
334 * \param driver The driver performing this identify action.
335 * \param parent The NewBus parent device for any devices this method adds.
336 */
337 static void
xenballoon_identify(driver_t * driver __unused,device_t parent)338 xenballoon_identify(driver_t *driver __unused, device_t parent)
339 {
340 /*
341 * A single device instance for our driver is always present
342 * in a system operating under Xen.
343 */
344 BUS_ADD_CHILD(parent, 0, driver->name, 0);
345 }
346
347 /**
348 * \brief Probe for the existence of the Xen Balloon device
349 *
350 * \param dev NewBus device_t for this Xen control instance.
351 *
352 * \return Always returns 0 indicating success.
353 */
354 static int
xenballoon_probe(device_t dev)355 xenballoon_probe(device_t dev)
356 {
357
358 device_set_desc(dev, "Xen Balloon Device");
359 return (0);
360 }
361
362 /**
363 * \brief Attach the Xen Balloon device.
364 *
365 * \param dev NewBus device_t for this Xen control instance.
366 *
367 * \return On success, 0. Otherwise an errno value indicating the
368 * type of failure.
369 */
370 static int
xenballoon_attach(device_t dev)371 xenballoon_attach(device_t dev)
372 {
373 int err;
374
375 mtx_init(&balloon_mutex, "balloon_mutex", NULL, MTX_DEF);
376
377 bs.current_pages = realmem;
378 bs.target_pages = bs.current_pages;
379 bs.balloon_low = 0;
380 bs.balloon_high = 0;
381 bs.driver_pages = 0UL;
382 bs.hard_limit = ~0UL;
383
384 kproc_create(balloon_process, NULL, NULL, 0, 0, "balloon");
385
386 target_watch.callback = watch_target;
387
388 err = xs_register_watch(&target_watch);
389 if (err)
390 device_printf(dev,
391 "xenballon: failed to set balloon watcher\n");
392
393 return (err);
394 }
395
396 /*-------------------- Private Device Attachment Data -----------------------*/
397 static device_method_t xenballoon_methods[] = {
398 /* Device interface */
399 DEVMETHOD(device_identify, xenballoon_identify),
400 DEVMETHOD(device_probe, xenballoon_probe),
401 DEVMETHOD(device_attach, xenballoon_attach),
402
403 DEVMETHOD_END
404 };
405
406 DEFINE_CLASS_0(xenballoon, xenballoon_driver, xenballoon_methods, 0);
407
408 DRIVER_MODULE(xenballoon, xenstore, xenballoon_driver, NULL, NULL);
409