blob: 4204336135849de16ffd5a9d9d010d5ed5231433 [file] [log] [blame]
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -07001/******************************************************************************
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 program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation; or, when distributed
13 * separately from the Linux kernel or incorporated into other
14 * software packages, subject to the following license:
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a copy
17 * of this source file (the "Software"), to deal in the Software without
18 * restriction, including without limitation the rights to use, copy, modify,
19 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
20 * and to permit persons to whom the Software is furnished to do so, subject to
21 * the following conditions:
22 *
23 * The above copyright notice and this permission notice shall be included in
24 * all copies or substantial portions of the Software.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
32 * IN THE SOFTWARE.
33 */
34
35#include <linux/kernel.h>
36#include <linux/module.h>
37#include <linux/sched.h>
38#include <linux/errno.h>
39#include <linux/mm.h>
40#include <linux/bootmem.h>
41#include <linux/pagemap.h>
42#include <linux/highmem.h>
43#include <linux/mutex.h>
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070044#include <linux/list.h>
45#include <linux/sysdev.h>
46
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070047#include <asm/page.h>
48#include <asm/pgalloc.h>
49#include <asm/pgtable.h>
50#include <asm/uaccess.h>
51#include <asm/tlb.h>
52
Jeremy Fitzhardingeecbf29c2008-12-16 12:37:07 -080053#include <asm/xen/hypervisor.h>
54#include <asm/xen/hypercall.h>
55#include <xen/interface/xen.h>
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070056#include <xen/interface/memory.h>
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070057#include <xen/xenbus.h>
58#include <xen/features.h>
59#include <xen/page.h>
60
61#define PAGES2KB(_p) ((_p)<<(PAGE_SHIFT-10))
62
Jeremy Fitzhardinge167e6cf2008-07-24 16:27:52 -070063#define BALLOON_CLASS_NAME "xen_memory"
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070064
65struct balloon_stats {
66 /* We aim for 'current allocation' == 'target allocation'. */
67 unsigned long current_pages;
68 unsigned long target_pages;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070069 /*
70 * Drivers may alter the memory reservation independently, but they
71 * must inform the balloon driver so we avoid hitting the hard limit.
72 */
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;
77};
78
79static DEFINE_MUTEX(balloon_mutex);
80
81static struct sys_device balloon_sysdev;
82
83static int register_balloon(struct sys_device *sysdev);
84
85/*
86 * Protects atomic reservation decrease/increase against concurrent increases.
87 * Also protects non-atomic updates of current_pages and driver_pages, and
88 * balloon lists.
89 */
90static DEFINE_SPINLOCK(balloon_lock);
91
92static struct balloon_stats balloon_stats;
93
94/* We increase/decrease in batches which fit in a page */
95static unsigned long frame_list[PAGE_SIZE / sizeof(unsigned long)];
96
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070097#ifdef CONFIG_HIGHMEM
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070098#define inc_totalhigh_pages() (totalhigh_pages++)
99#define dec_totalhigh_pages() (totalhigh_pages--)
100#else
101#define inc_totalhigh_pages() do {} while(0)
102#define dec_totalhigh_pages() do {} while(0)
103#endif
104
105/* List of ballooned pages, threaded through the mem_map array. */
106static LIST_HEAD(ballooned_pages);
107
108/* Main work function, always executed in process context. */
109static void balloon_process(struct work_struct *work);
110static DECLARE_WORK(balloon_worker, balloon_process);
111static struct timer_list balloon_timer;
112
113/* When ballooning out (allocating memory to return to Xen) we don't really
114 want the kernel to try too hard since that can trigger the oom killer. */
115#define GFP_BALLOON \
116 (GFP_HIGHUSER | __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC)
117
118static void scrub_page(struct page *page)
119{
120#ifdef CONFIG_XEN_SCRUB_PAGES
Jeremy Fitzhardinge26a3e992008-11-17 09:35:00 -0800121 clear_highpage(page);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700122#endif
123}
124
125/* balloon_append: add the given page to the balloon. */
126static void balloon_append(struct page *page)
127{
128 /* Lowmem is re-populated first, so highmem pages go at list tail. */
129 if (PageHighMem(page)) {
130 list_add_tail(&page->lru, &ballooned_pages);
131 balloon_stats.balloon_high++;
132 dec_totalhigh_pages();
133 } else {
134 list_add(&page->lru, &ballooned_pages);
135 balloon_stats.balloon_low++;
136 }
Gianluca Guida3d65c942009-07-30 22:54:36 +0100137
138 totalram_pages--;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700139}
140
141/* balloon_retrieve: rescue a page from the balloon, if it is not empty. */
142static struct page *balloon_retrieve(void)
143{
144 struct page *page;
145
146 if (list_empty(&ballooned_pages))
147 return NULL;
148
149 page = list_entry(ballooned_pages.next, struct page, lru);
150 list_del(&page->lru);
151
152 if (PageHighMem(page)) {
153 balloon_stats.balloon_high--;
154 inc_totalhigh_pages();
155 }
156 else
157 balloon_stats.balloon_low--;
158
Gianluca Guida3d65c942009-07-30 22:54:36 +0100159 totalram_pages++;
160
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700161 return page;
162}
163
164static struct page *balloon_first_page(void)
165{
166 if (list_empty(&ballooned_pages))
167 return NULL;
168 return list_entry(ballooned_pages.next, struct page, lru);
169}
170
171static struct page *balloon_next_page(struct page *page)
172{
173 struct list_head *next = page->lru.next;
174 if (next == &ballooned_pages)
175 return NULL;
176 return list_entry(next, struct page, lru);
177}
178
179static void balloon_alarm(unsigned long unused)
180{
181 schedule_work(&balloon_worker);
182}
183
184static unsigned long current_target(void)
185{
Ian Campbellbc2c0302009-06-05 11:58:37 +0100186 unsigned long target = balloon_stats.target_pages;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700187
188 target = min(target,
189 balloon_stats.current_pages +
190 balloon_stats.balloon_low +
191 balloon_stats.balloon_high);
192
193 return target;
194}
195
196static int increase_reservation(unsigned long nr_pages)
197{
198 unsigned long pfn, i, flags;
199 struct page *page;
200 long rc;
201 struct xen_memory_reservation reservation = {
202 .address_bits = 0,
203 .extent_order = 0,
204 .domid = DOMID_SELF
205 };
206
207 if (nr_pages > ARRAY_SIZE(frame_list))
208 nr_pages = ARRAY_SIZE(frame_list);
209
210 spin_lock_irqsave(&balloon_lock, flags);
211
212 page = balloon_first_page();
213 for (i = 0; i < nr_pages; i++) {
214 BUG_ON(page == NULL);
Joe Perchesa419aef2009-08-18 11:18:35 -0700215 frame_list[i] = page_to_pfn(page);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700216 page = balloon_next_page(page);
217 }
218
Isaku Yamahataa90971e2008-05-26 23:31:14 +0100219 set_xen_guest_handle(reservation.extent_start, frame_list);
Jeremy Fitzhardingefde28e82008-07-24 16:28:00 -0700220 reservation.nr_extents = nr_pages;
221 rc = HYPERVISOR_memory_op(XENMEM_populate_physmap, &reservation);
Ian Campbellbc2c0302009-06-05 11:58:37 +0100222 if (rc < 0)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700223 goto out;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700224
Ian Campbellbc2c0302009-06-05 11:58:37 +0100225 for (i = 0; i < rc; i++) {
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700226 page = balloon_retrieve();
227 BUG_ON(page == NULL);
228
229 pfn = page_to_pfn(page);
230 BUG_ON(!xen_feature(XENFEAT_auto_translated_physmap) &&
231 phys_to_machine_mapping_valid(pfn));
232
233 set_phys_to_machine(pfn, frame_list[i]);
234
235 /* Link back into the page tables if not highmem. */
236 if (pfn < max_low_pfn) {
237 int ret;
238 ret = HYPERVISOR_update_va_mapping(
239 (unsigned long)__va(pfn << PAGE_SHIFT),
240 mfn_pte(frame_list[i], PAGE_KERNEL),
241 0);
242 BUG_ON(ret);
243 }
244
245 /* Relinquish the page back to the allocator. */
246 ClearPageReserved(page);
247 init_page_count(page);
248 __free_page(page);
249 }
250
Ian Campbellbc2c0302009-06-05 11:58:37 +0100251 balloon_stats.current_pages += rc;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700252
253 out:
254 spin_unlock_irqrestore(&balloon_lock, flags);
255
Ian Campbellbc2c0302009-06-05 11:58:37 +0100256 return rc < 0 ? rc : rc != nr_pages;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700257}
258
259static int decrease_reservation(unsigned long nr_pages)
260{
261 unsigned long pfn, i, flags;
262 struct page *page;
263 int need_sleep = 0;
264 int ret;
265 struct xen_memory_reservation reservation = {
266 .address_bits = 0,
267 .extent_order = 0,
268 .domid = DOMID_SELF
269 };
270
271 if (nr_pages > ARRAY_SIZE(frame_list))
272 nr_pages = ARRAY_SIZE(frame_list);
273
274 for (i = 0; i < nr_pages; i++) {
275 if ((page = alloc_page(GFP_BALLOON)) == NULL) {
276 nr_pages = i;
277 need_sleep = 1;
278 break;
279 }
280
281 pfn = page_to_pfn(page);
282 frame_list[i] = pfn_to_mfn(pfn);
283
284 scrub_page(page);
Dan Magenheimer1058a752009-01-22 14:36:08 -0800285
Ian Campbellff4ce8c2009-01-23 16:26:21 +0000286 if (!PageHighMem(page)) {
287 ret = HYPERVISOR_update_va_mapping(
288 (unsigned long)__va(pfn << PAGE_SHIFT),
289 __pte_ma(0), 0);
290 BUG_ON(ret);
291 }
292
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700293 }
294
295 /* Ensure that ballooned highmem pages don't have kmaps. */
296 kmap_flush_unused();
297 flush_tlb_all();
298
299 spin_lock_irqsave(&balloon_lock, flags);
300
301 /* No more mappings: invalidate P2M and add to balloon. */
302 for (i = 0; i < nr_pages; i++) {
303 pfn = mfn_to_pfn(frame_list[i]);
304 set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
305 balloon_append(pfn_to_page(pfn));
306 }
307
Isaku Yamahataa90971e2008-05-26 23:31:14 +0100308 set_xen_guest_handle(reservation.extent_start, frame_list);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700309 reservation.nr_extents = nr_pages;
310 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
311 BUG_ON(ret != nr_pages);
312
313 balloon_stats.current_pages -= nr_pages;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700314
315 spin_unlock_irqrestore(&balloon_lock, flags);
316
317 return need_sleep;
318}
319
320/*
321 * We avoid multiple worker processes conflicting via the balloon mutex.
322 * We may of course race updates of the target counts (which are protected
323 * by the balloon lock), or with changes to the Xen hard limit, but we will
324 * recover from these in time.
325 */
326static void balloon_process(struct work_struct *work)
327{
328 int need_sleep = 0;
329 long credit;
330
331 mutex_lock(&balloon_mutex);
332
333 do {
334 credit = current_target() - balloon_stats.current_pages;
335 if (credit > 0)
336 need_sleep = (increase_reservation(credit) != 0);
337 if (credit < 0)
338 need_sleep = (decrease_reservation(-credit) != 0);
339
340#ifndef CONFIG_PREEMPT
341 if (need_resched())
342 schedule();
343#endif
344 } while ((credit != 0) && !need_sleep);
345
346 /* Schedule more work if there is some still to be done. */
347 if (current_target() != balloon_stats.current_pages)
348 mod_timer(&balloon_timer, jiffies + HZ);
349
350 mutex_unlock(&balloon_mutex);
351}
352
353/* Resets the Xen limit, sets new target, and kicks off processing. */
Adrian Bunk955d6f12008-05-26 23:31:17 +0100354static void balloon_set_new_target(unsigned long target)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700355{
356 /* No need for lock. Not read-modify-write updates. */
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700357 balloon_stats.target_pages = target;
358 schedule_work(&balloon_worker);
359}
360
361static struct xenbus_watch target_watch =
362{
363 .node = "memory/target"
364};
365
366/* React to a change in the target key */
367static void watch_target(struct xenbus_watch *watch,
368 const char **vec, unsigned int len)
369{
370 unsigned long long new_target;
371 int err;
372
373 err = xenbus_scanf(XBT_NIL, "memory", "target", "%llu", &new_target);
374 if (err != 1) {
375 /* This is ok (for domain0 at least) - so just return */
376 return;
377 }
378
379 /* The given memory/target value is in KiB, so it needs converting to
380 * pages. PAGE_SHIFT converts bytes to pages, hence PAGE_SHIFT - 10.
381 */
382 balloon_set_new_target(new_target >> (PAGE_SHIFT - 10));
383}
384
385static int balloon_init_watcher(struct notifier_block *notifier,
386 unsigned long event,
387 void *data)
388{
389 int err;
390
391 err = register_xenbus_watch(&target_watch);
392 if (err)
393 printk(KERN_ERR "Failed to set balloon watcher\n");
394
395 return NOTIFY_DONE;
396}
397
398static struct notifier_block xenstore_notifier;
399
400static int __init balloon_init(void)
401{
402 unsigned long pfn;
403 struct page *page;
404
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -0700405 if (!xen_pv_domain())
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700406 return -ENODEV;
407
408 pr_info("xen_balloon: Initialising balloon driver.\n");
409
410 balloon_stats.current_pages = min(xen_start_info->nr_pages, max_pfn);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700411 balloon_stats.target_pages = balloon_stats.current_pages;
412 balloon_stats.balloon_low = 0;
413 balloon_stats.balloon_high = 0;
414 balloon_stats.driver_pages = 0UL;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700415
416 init_timer(&balloon_timer);
417 balloon_timer.data = 0;
418 balloon_timer.function = balloon_alarm;
419
420 register_balloon(&balloon_sysdev);
421
422 /* Initialise the balloon with excess memory space. */
423 for (pfn = xen_start_info->nr_pages; pfn < max_pfn; pfn++) {
424 page = pfn_to_page(pfn);
425 if (!PageReserved(page))
426 balloon_append(page);
427 }
428
429 target_watch.callback = watch_target;
430 xenstore_notifier.notifier_call = balloon_init_watcher;
431
432 register_xenstore_notifier(&xenstore_notifier);
433
434 return 0;
435}
436
437subsys_initcall(balloon_init);
438
439static void balloon_exit(void)
440{
441 /* XXX - release balloon here */
442 return;
443}
444
445module_exit(balloon_exit);
446
Jeremy Fitzhardinge167e6cf2008-07-24 16:27:52 -0700447#define BALLOON_SHOW(name, format, args...) \
448 static ssize_t show_##name(struct sys_device *dev, \
449 struct sysdev_attribute *attr, \
450 char *buf) \
451 { \
452 return sprintf(buf, format, ##args); \
453 } \
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700454 static SYSDEV_ATTR(name, S_IRUGO, show_##name, NULL)
455
456BALLOON_SHOW(current_kb, "%lu\n", PAGES2KB(balloon_stats.current_pages));
457BALLOON_SHOW(low_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_low));
458BALLOON_SHOW(high_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_high));
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700459BALLOON_SHOW(driver_kb, "%lu\n", PAGES2KB(balloon_stats.driver_pages));
460
Jeremy Fitzhardinge167e6cf2008-07-24 16:27:52 -0700461static ssize_t show_target_kb(struct sys_device *dev, struct sysdev_attribute *attr,
462 char *buf)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700463{
464 return sprintf(buf, "%lu\n", PAGES2KB(balloon_stats.target_pages));
465}
466
467static ssize_t store_target_kb(struct sys_device *dev,
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200468 struct sysdev_attribute *attr,
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700469 const char *buf,
470 size_t count)
471{
Jeremy Fitzhardinge167e6cf2008-07-24 16:27:52 -0700472 char *endchar;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700473 unsigned long long target_bytes;
474
475 if (!capable(CAP_SYS_ADMIN))
476 return -EPERM;
477
Jeremy Fitzhardinge618b2c82009-01-28 16:50:20 -0800478 target_bytes = simple_strtoull(buf, &endchar, 0) * 1024;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700479
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700480 balloon_set_new_target(target_bytes >> PAGE_SHIFT);
481
482 return count;
483}
484
485static SYSDEV_ATTR(target_kb, S_IRUGO | S_IWUSR,
486 show_target_kb, store_target_kb);
487
Jeremy Fitzhardinge618b2c82009-01-28 16:50:20 -0800488
489static ssize_t show_target(struct sys_device *dev, struct sysdev_attribute *attr,
490 char *buf)
491{
492 return sprintf(buf, "%llu\n",
Jan Beulich06926982009-05-05 13:57:52 +0100493 (unsigned long long)balloon_stats.target_pages
494 << PAGE_SHIFT);
Jeremy Fitzhardinge618b2c82009-01-28 16:50:20 -0800495}
496
497static ssize_t store_target(struct sys_device *dev,
498 struct sysdev_attribute *attr,
499 const char *buf,
500 size_t count)
501{
502 char *endchar;
503 unsigned long long target_bytes;
504
505 if (!capable(CAP_SYS_ADMIN))
506 return -EPERM;
507
508 target_bytes = memparse(buf, &endchar);
509
510 balloon_set_new_target(target_bytes >> PAGE_SHIFT);
511
512 return count;
513}
514
515static SYSDEV_ATTR(target, S_IRUGO | S_IWUSR,
516 show_target, store_target);
517
518
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700519static struct sysdev_attribute *balloon_attrs[] = {
520 &attr_target_kb,
Jeremy Fitzhardinge618b2c82009-01-28 16:50:20 -0800521 &attr_target,
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700522};
523
524static struct attribute *balloon_info_attrs[] = {
525 &attr_current_kb.attr,
526 &attr_low_kb.attr,
527 &attr_high_kb.attr,
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700528 &attr_driver_kb.attr,
529 NULL
530};
531
532static struct attribute_group balloon_info_group = {
533 .name = "info",
534 .attrs = balloon_info_attrs,
535};
536
537static struct sysdev_class balloon_sysdev_class = {
538 .name = BALLOON_CLASS_NAME,
539};
540
541static int register_balloon(struct sys_device *sysdev)
542{
543 int i, error;
544
545 error = sysdev_class_register(&balloon_sysdev_class);
546 if (error)
547 return error;
548
549 sysdev->id = 0;
550 sysdev->cls = &balloon_sysdev_class;
551
552 error = sysdev_register(sysdev);
553 if (error) {
554 sysdev_class_unregister(&balloon_sysdev_class);
555 return error;
556 }
557
558 for (i = 0; i < ARRAY_SIZE(balloon_attrs); i++) {
559 error = sysdev_create_file(sysdev, balloon_attrs[i]);
560 if (error)
561 goto fail;
562 }
563
564 error = sysfs_create_group(&sysdev->kobj, &balloon_info_group);
565 if (error)
566 goto fail;
567
568 return 0;
569
570 fail:
571 while (--i >= 0)
572 sysdev_remove_file(sysdev, balloon_attrs[i]);
573 sysdev_unregister(sysdev);
574 sysdev_class_unregister(&balloon_sysdev_class);
575 return error;
576}
577
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700578MODULE_LICENSE("GPL");