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 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:
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:
23 * The above copyright notice and this permission notice shall be included in
24 * all copies or substantial portions of the Software.
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
35 #include <linux/kernel.h>
36 #include <linux/module.h>
37 #include <linux/sched.h>
38 #include <linux/errno.h>
40 #include <linux/bootmem.h>
41 #include <linux/pagemap.h>
42 #include <linux/highmem.h>
43 #include <linux/mutex.h>
44 #include <linux/list.h>
45 #include <linux/sysdev.h>
46 #include <linux/gfp.h>
49 #include <asm/pgalloc.h>
50 #include <asm/pgtable.h>
51 #include <asm/uaccess.h>
54 #include <asm/xen/hypervisor.h>
55 #include <asm/xen/hypercall.h>
58 #include <xen/interface/xen.h>
59 #include <xen/interface/memory.h>
60 #include <xen/xenbus.h>
61 #include <xen/features.h>
64 #define PAGES2KB(_p) ((_p)<<(PAGE_SHIFT-10))
66 #define BALLOON_CLASS_NAME "xen_memory"
68 struct balloon_stats {
69 /* We aim for 'current allocation' == 'target allocation'. */
70 unsigned long current_pages;
71 unsigned long target_pages;
73 * Drivers may alter the memory reservation independently, but they
74 * must inform the balloon driver so we avoid hitting the hard limit.
76 unsigned long driver_pages;
77 /* Number of pages in high- and low-memory balloons. */
78 unsigned long balloon_low;
79 unsigned long balloon_high;
82 static DEFINE_MUTEX(balloon_mutex);
84 static struct sys_device balloon_sysdev;
86 static int register_balloon(struct sys_device *sysdev);
88 static struct balloon_stats balloon_stats;
90 /* We increase/decrease in batches which fit in a page */
91 static unsigned long frame_list[PAGE_SIZE / sizeof(unsigned long)];
94 #define inc_totalhigh_pages() (totalhigh_pages++)
95 #define dec_totalhigh_pages() (totalhigh_pages--)
97 #define inc_totalhigh_pages() do {} while(0)
98 #define dec_totalhigh_pages() do {} while(0)
101 /* List of ballooned pages, threaded through the mem_map array. */
102 static LIST_HEAD(ballooned_pages);
104 /* Main work function, always executed in process context. */
105 static void balloon_process(struct work_struct *work);
106 static DECLARE_WORK(balloon_worker, balloon_process);
107 static struct timer_list balloon_timer;
109 /* When ballooning out (allocating memory to return to Xen) we don't really
110 want the kernel to try too hard since that can trigger the oom killer. */
111 #define GFP_BALLOON \
112 (GFP_HIGHUSER | __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC)
114 static void scrub_page(struct page *page)
116 #ifdef CONFIG_XEN_SCRUB_PAGES
117 clear_highpage(page);
121 /* balloon_append: add the given page to the balloon. */
122 static void __balloon_append(struct page *page)
124 /* Lowmem is re-populated first, so highmem pages go at list tail. */
125 if (PageHighMem(page)) {
126 list_add_tail(&page->lru, &ballooned_pages);
127 balloon_stats.balloon_high++;
128 dec_totalhigh_pages();
130 list_add(&page->lru, &ballooned_pages);
131 balloon_stats.balloon_low++;
135 static void balloon_append(struct page *page)
137 __balloon_append(page);
141 /* balloon_retrieve: rescue a page from the balloon, if it is not empty. */
142 static struct page *balloon_retrieve(void)
146 if (list_empty(&ballooned_pages))
149 page = list_entry(ballooned_pages.next, struct page, lru);
150 list_del(&page->lru);
152 if (PageHighMem(page)) {
153 balloon_stats.balloon_high--;
154 inc_totalhigh_pages();
157 balloon_stats.balloon_low--;
164 static struct page *balloon_first_page(void)
166 if (list_empty(&ballooned_pages))
168 return list_entry(ballooned_pages.next, struct page, lru);
171 static struct page *balloon_next_page(struct page *page)
173 struct list_head *next = page->lru.next;
174 if (next == &ballooned_pages)
176 return list_entry(next, struct page, lru);
179 static void balloon_alarm(unsigned long unused)
181 schedule_work(&balloon_worker);
184 static unsigned long current_target(void)
186 unsigned long target = balloon_stats.target_pages;
189 balloon_stats.current_pages +
190 balloon_stats.balloon_low +
191 balloon_stats.balloon_high);
196 static int increase_reservation(unsigned long nr_pages)
198 unsigned long pfn, i;
201 struct xen_memory_reservation reservation = {
207 if (nr_pages > ARRAY_SIZE(frame_list))
208 nr_pages = ARRAY_SIZE(frame_list);
210 page = balloon_first_page();
211 for (i = 0; i < nr_pages; i++) {
212 BUG_ON(page == NULL);
213 frame_list[i] = page_to_pfn(page);
214 page = balloon_next_page(page);
217 set_xen_guest_handle(reservation.extent_start, frame_list);
218 reservation.nr_extents = nr_pages;
219 rc = HYPERVISOR_memory_op(XENMEM_populate_physmap, &reservation);
223 for (i = 0; i < rc; i++) {
224 page = balloon_retrieve();
225 BUG_ON(page == NULL);
227 pfn = page_to_pfn(page);
228 BUG_ON(!xen_feature(XENFEAT_auto_translated_physmap) &&
229 phys_to_machine_mapping_valid(pfn));
231 set_phys_to_machine(pfn, frame_list[i]);
233 /* Link back into the page tables if not highmem. */
234 if (pfn < max_low_pfn) {
236 ret = HYPERVISOR_update_va_mapping(
237 (unsigned long)__va(pfn << PAGE_SHIFT),
238 mfn_pte(frame_list[i], PAGE_KERNEL),
243 /* Relinquish the page back to the allocator. */
244 ClearPageReserved(page);
245 init_page_count(page);
249 balloon_stats.current_pages += rc;
252 return rc < 0 ? rc : rc != nr_pages;
255 static int decrease_reservation(unsigned long nr_pages)
257 unsigned long pfn, i;
261 struct xen_memory_reservation reservation = {
267 if (nr_pages > ARRAY_SIZE(frame_list))
268 nr_pages = ARRAY_SIZE(frame_list);
270 for (i = 0; i < nr_pages; i++) {
271 if ((page = alloc_page(GFP_BALLOON)) == NULL) {
277 pfn = page_to_pfn(page);
278 frame_list[i] = pfn_to_mfn(pfn);
282 if (!PageHighMem(page)) {
283 ret = HYPERVISOR_update_va_mapping(
284 (unsigned long)__va(pfn << PAGE_SHIFT),
291 /* Ensure that ballooned highmem pages don't have kmaps. */
295 /* No more mappings: invalidate P2M and add to balloon. */
296 for (i = 0; i < nr_pages; i++) {
297 pfn = mfn_to_pfn(frame_list[i]);
298 set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
299 balloon_append(pfn_to_page(pfn));
302 set_xen_guest_handle(reservation.extent_start, frame_list);
303 reservation.nr_extents = nr_pages;
304 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
305 BUG_ON(ret != nr_pages);
307 balloon_stats.current_pages -= nr_pages;
313 * We avoid multiple worker processes conflicting via the balloon mutex.
314 * We may of course race updates of the target counts (which are protected
315 * by the balloon lock), or with changes to the Xen hard limit, but we will
316 * recover from these in time.
318 static void balloon_process(struct work_struct *work)
323 mutex_lock(&balloon_mutex);
326 credit = current_target() - balloon_stats.current_pages;
328 need_sleep = (increase_reservation(credit) != 0);
330 need_sleep = (decrease_reservation(-credit) != 0);
332 #ifndef CONFIG_PREEMPT
336 } while ((credit != 0) && !need_sleep);
338 /* Schedule more work if there is some still to be done. */
339 if (current_target() != balloon_stats.current_pages)
340 mod_timer(&balloon_timer, jiffies + HZ);
342 mutex_unlock(&balloon_mutex);
345 /* Resets the Xen limit, sets new target, and kicks off processing. */
346 static void balloon_set_new_target(unsigned long target)
348 /* No need for lock. Not read-modify-write updates. */
349 balloon_stats.target_pages = target;
350 schedule_work(&balloon_worker);
353 static struct xenbus_watch target_watch =
355 .node = "memory/target"
358 /* React to a change in the target key */
359 static void watch_target(struct xenbus_watch *watch,
360 const char **vec, unsigned int len)
362 unsigned long long new_target;
365 err = xenbus_scanf(XBT_NIL, "memory", "target", "%llu", &new_target);
367 /* This is ok (for domain0 at least) - so just return */
371 /* The given memory/target value is in KiB, so it needs converting to
372 * pages. PAGE_SHIFT converts bytes to pages, hence PAGE_SHIFT - 10.
374 balloon_set_new_target(new_target >> (PAGE_SHIFT - 10));
377 static int balloon_init_watcher(struct notifier_block *notifier,
383 err = register_xenbus_watch(&target_watch);
385 printk(KERN_ERR "Failed to set balloon watcher\n");
390 static struct notifier_block xenstore_notifier;
392 static int __init balloon_init(void)
397 if (!xen_pv_domain())
400 pr_info("xen_balloon: Initialising balloon driver.\n");
402 balloon_stats.current_pages = min(xen_start_info->nr_pages, max_pfn);
403 balloon_stats.target_pages = balloon_stats.current_pages;
404 balloon_stats.balloon_low = 0;
405 balloon_stats.balloon_high = 0;
406 balloon_stats.driver_pages = 0UL;
408 init_timer(&balloon_timer);
409 balloon_timer.data = 0;
410 balloon_timer.function = balloon_alarm;
412 register_balloon(&balloon_sysdev);
414 /* Initialise the balloon with excess memory space. */
415 for (pfn = PFN_UP(xen_extra_mem_start);
416 pfn < PFN_DOWN(xen_extra_mem_start + xen_extra_mem_size);
418 page = pfn_to_page(pfn);
419 /* totalram_pages doesn't include the boot-time
420 balloon extension, so don't subtract from it. */
421 __balloon_append(page);
424 target_watch.callback = watch_target;
425 xenstore_notifier.notifier_call = balloon_init_watcher;
427 register_xenstore_notifier(&xenstore_notifier);
432 subsys_initcall(balloon_init);
434 static void balloon_exit(void)
436 /* XXX - release balloon here */
440 module_exit(balloon_exit);
442 #define BALLOON_SHOW(name, format, args...) \
443 static ssize_t show_##name(struct sys_device *dev, \
444 struct sysdev_attribute *attr, \
447 return sprintf(buf, format, ##args); \
449 static SYSDEV_ATTR(name, S_IRUGO, show_##name, NULL)
451 BALLOON_SHOW(current_kb, "%lu\n", PAGES2KB(balloon_stats.current_pages));
452 BALLOON_SHOW(low_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_low));
453 BALLOON_SHOW(high_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_high));
454 BALLOON_SHOW(driver_kb, "%lu\n", PAGES2KB(balloon_stats.driver_pages));
456 static ssize_t show_target_kb(struct sys_device *dev, struct sysdev_attribute *attr,
459 return sprintf(buf, "%lu\n", PAGES2KB(balloon_stats.target_pages));
462 static ssize_t store_target_kb(struct sys_device *dev,
463 struct sysdev_attribute *attr,
468 unsigned long long target_bytes;
470 if (!capable(CAP_SYS_ADMIN))
473 target_bytes = simple_strtoull(buf, &endchar, 0) * 1024;
475 balloon_set_new_target(target_bytes >> PAGE_SHIFT);
480 static SYSDEV_ATTR(target_kb, S_IRUGO | S_IWUSR,
481 show_target_kb, store_target_kb);
484 static ssize_t show_target(struct sys_device *dev, struct sysdev_attribute *attr,
487 return sprintf(buf, "%llu\n",
488 (unsigned long long)balloon_stats.target_pages
492 static ssize_t store_target(struct sys_device *dev,
493 struct sysdev_attribute *attr,
498 unsigned long long target_bytes;
500 if (!capable(CAP_SYS_ADMIN))
503 target_bytes = memparse(buf, &endchar);
505 balloon_set_new_target(target_bytes >> PAGE_SHIFT);
510 static SYSDEV_ATTR(target, S_IRUGO | S_IWUSR,
511 show_target, store_target);
514 static struct sysdev_attribute *balloon_attrs[] = {
519 static struct attribute *balloon_info_attrs[] = {
520 &attr_current_kb.attr,
523 &attr_driver_kb.attr,
527 static struct attribute_group balloon_info_group = {
529 .attrs = balloon_info_attrs,
532 static struct sysdev_class balloon_sysdev_class = {
533 .name = BALLOON_CLASS_NAME,
536 static int register_balloon(struct sys_device *sysdev)
540 error = sysdev_class_register(&balloon_sysdev_class);
545 sysdev->cls = &balloon_sysdev_class;
547 error = sysdev_register(sysdev);
549 sysdev_class_unregister(&balloon_sysdev_class);
553 for (i = 0; i < ARRAY_SIZE(balloon_attrs); i++) {
554 error = sysdev_create_file(sysdev, balloon_attrs[i]);
559 error = sysfs_create_group(&sysdev->kobj, &balloon_info_group);
567 sysdev_remove_file(sysdev, balloon_attrs[i]);
568 sysdev_unregister(sysdev);
569 sysdev_class_unregister(&balloon_sysdev_class);
573 MODULE_LICENSE("GPL");