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>
55 #include <asm/xen/hypervisor.h>
56 #include <asm/xen/hypercall.h>
59 #include <xen/interface/xen.h>
60 #include <xen/interface/memory.h>
61 #include <xen/xenbus.h>
62 #include <xen/features.h>
65 #define PAGES2KB(_p) ((_p)<<(PAGE_SHIFT-10))
67 #define BALLOON_CLASS_NAME "xen_memory"
69 struct balloon_stats {
70 /* We aim for 'current allocation' == 'target allocation'. */
71 unsigned long current_pages;
72 unsigned long target_pages;
73 /* Number of pages in high- and low-memory balloons. */
74 unsigned long balloon_low;
75 unsigned long balloon_high;
78 static DEFINE_MUTEX(balloon_mutex);
80 static struct sys_device balloon_sysdev;
82 static int register_balloon(struct sys_device *sysdev);
84 static struct balloon_stats balloon_stats;
86 /* We increase/decrease in batches which fit in a page */
87 static unsigned long frame_list[PAGE_SIZE / sizeof(unsigned long)];
90 #define inc_totalhigh_pages() (totalhigh_pages++)
91 #define dec_totalhigh_pages() (totalhigh_pages--)
93 #define inc_totalhigh_pages() do {} while(0)
94 #define dec_totalhigh_pages() do {} while(0)
97 /* List of ballooned pages, threaded through the mem_map array. */
98 static LIST_HEAD(ballooned_pages);
100 /* Main work function, always executed in process context. */
101 static void balloon_process(struct work_struct *work);
102 static DECLARE_DELAYED_WORK(balloon_worker, balloon_process);
104 /* When ballooning out (allocating memory to return to Xen) we don't really
105 want the kernel to try too hard since that can trigger the oom killer. */
106 #define GFP_BALLOON \
107 (GFP_HIGHUSER | __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC)
109 static void scrub_page(struct page *page)
111 #ifdef CONFIG_XEN_SCRUB_PAGES
112 clear_highpage(page);
116 /* balloon_append: add the given page to the balloon. */
117 static void __balloon_append(struct page *page)
119 /* Lowmem is re-populated first, so highmem pages go at list tail. */
120 if (PageHighMem(page)) {
121 list_add_tail(&page->lru, &ballooned_pages);
122 balloon_stats.balloon_high++;
123 dec_totalhigh_pages();
125 list_add(&page->lru, &ballooned_pages);
126 balloon_stats.balloon_low++;
130 static void balloon_append(struct page *page)
132 __balloon_append(page);
136 /* balloon_retrieve: rescue a page from the balloon, if it is not empty. */
137 static struct page *balloon_retrieve(void)
141 if (list_empty(&ballooned_pages))
144 page = list_entry(ballooned_pages.next, struct page, lru);
145 list_del(&page->lru);
147 if (PageHighMem(page)) {
148 balloon_stats.balloon_high--;
149 inc_totalhigh_pages();
152 balloon_stats.balloon_low--;
159 static struct page *balloon_first_page(void)
161 if (list_empty(&ballooned_pages))
163 return list_entry(ballooned_pages.next, struct page, lru);
166 static struct page *balloon_next_page(struct page *page)
168 struct list_head *next = page->lru.next;
169 if (next == &ballooned_pages)
171 return list_entry(next, struct page, lru);
174 static unsigned long current_target(void)
176 unsigned long target = balloon_stats.target_pages;
179 balloon_stats.current_pages +
180 balloon_stats.balloon_low +
181 balloon_stats.balloon_high);
186 static int increase_reservation(unsigned long nr_pages)
188 unsigned long pfn, i;
191 struct xen_memory_reservation reservation = {
197 if (nr_pages > ARRAY_SIZE(frame_list))
198 nr_pages = ARRAY_SIZE(frame_list);
200 page = balloon_first_page();
201 for (i = 0; i < nr_pages; i++) {
202 BUG_ON(page == NULL);
203 frame_list[i] = page_to_pfn(page);
204 page = balloon_next_page(page);
207 set_xen_guest_handle(reservation.extent_start, frame_list);
208 reservation.nr_extents = nr_pages;
209 rc = HYPERVISOR_memory_op(XENMEM_populate_physmap, &reservation);
213 for (i = 0; i < rc; i++) {
214 page = balloon_retrieve();
215 BUG_ON(page == NULL);
217 pfn = page_to_pfn(page);
218 BUG_ON(!xen_feature(XENFEAT_auto_translated_physmap) &&
219 phys_to_machine_mapping_valid(pfn));
221 set_phys_to_machine(pfn, frame_list[i]);
223 /* Link back into the page tables if not highmem. */
224 if (pfn < max_low_pfn) {
226 ret = HYPERVISOR_update_va_mapping(
227 (unsigned long)__va(pfn << PAGE_SHIFT),
228 mfn_pte(frame_list[i], PAGE_KERNEL),
233 /* Relinquish the page back to the allocator. */
234 ClearPageReserved(page);
235 init_page_count(page);
239 balloon_stats.current_pages += rc;
242 return rc < 0 ? rc : rc != nr_pages;
245 static int decrease_reservation(unsigned long nr_pages)
247 unsigned long pfn, i;
251 struct xen_memory_reservation reservation = {
257 if (nr_pages > ARRAY_SIZE(frame_list))
258 nr_pages = ARRAY_SIZE(frame_list);
260 for (i = 0; i < nr_pages; i++) {
261 if ((page = alloc_page(GFP_BALLOON)) == NULL) {
267 pfn = page_to_pfn(page);
268 frame_list[i] = pfn_to_mfn(pfn);
272 if (!PageHighMem(page)) {
273 ret = HYPERVISOR_update_va_mapping(
274 (unsigned long)__va(pfn << PAGE_SHIFT),
281 /* Ensure that ballooned highmem pages don't have kmaps. */
285 /* No more mappings: invalidate P2M and add to balloon. */
286 for (i = 0; i < nr_pages; i++) {
287 pfn = mfn_to_pfn(frame_list[i]);
288 set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
289 balloon_append(pfn_to_page(pfn));
292 set_xen_guest_handle(reservation.extent_start, frame_list);
293 reservation.nr_extents = nr_pages;
294 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
295 BUG_ON(ret != nr_pages);
297 balloon_stats.current_pages -= nr_pages;
303 * We avoid multiple worker processes conflicting via the balloon mutex.
304 * We may of course race updates of the target counts (which are protected
305 * by the balloon lock), or with changes to the Xen hard limit, but we will
306 * recover from these in time.
308 static void balloon_process(struct work_struct *work)
313 mutex_lock(&balloon_mutex);
316 credit = current_target() - balloon_stats.current_pages;
318 need_sleep = (increase_reservation(credit) != 0);
320 need_sleep = (decrease_reservation(-credit) != 0);
322 #ifndef CONFIG_PREEMPT
326 } while ((credit != 0) && !need_sleep);
328 /* Schedule more work if there is some still to be done. */
329 if (current_target() != balloon_stats.current_pages)
330 schedule_delayed_work(&balloon_worker, HZ);
332 mutex_unlock(&balloon_mutex);
335 /* Resets the Xen limit, sets new target, and kicks off processing. */
336 static void balloon_set_new_target(unsigned long target)
338 /* No need for lock. Not read-modify-write updates. */
339 balloon_stats.target_pages = target;
340 schedule_delayed_work(&balloon_worker, 0);
343 static struct xenbus_watch target_watch =
345 .node = "memory/target"
348 /* React to a change in the target key */
349 static void watch_target(struct xenbus_watch *watch,
350 const char **vec, unsigned int len)
352 unsigned long long new_target;
355 err = xenbus_scanf(XBT_NIL, "memory", "target", "%llu", &new_target);
357 /* This is ok (for domain0 at least) - so just return */
361 /* The given memory/target value is in KiB, so it needs converting to
362 * pages. PAGE_SHIFT converts bytes to pages, hence PAGE_SHIFT - 10.
364 balloon_set_new_target(new_target >> (PAGE_SHIFT - 10));
367 static int balloon_init_watcher(struct notifier_block *notifier,
373 err = register_xenbus_watch(&target_watch);
375 printk(KERN_ERR "Failed to set balloon watcher\n");
380 static struct notifier_block xenstore_notifier;
382 static int __init balloon_init(void)
384 unsigned long pfn, extra_pfn_end;
387 if (!xen_pv_domain())
390 pr_info("xen_balloon: Initialising balloon driver.\n");
392 balloon_stats.current_pages = min(xen_start_info->nr_pages, max_pfn);
393 balloon_stats.target_pages = balloon_stats.current_pages;
394 balloon_stats.balloon_low = 0;
395 balloon_stats.balloon_high = 0;
397 register_balloon(&balloon_sysdev);
400 * Initialise the balloon with excess memory space. We need
401 * to make sure we don't add memory which doesn't exist or
402 * logically exist. The E820 map can be trimmed to be smaller
403 * than the amount of physical memory due to the mem= command
404 * line parameter. And if this is a 32-bit non-HIGHMEM kernel
405 * on a system with memory which requires highmem to access,
406 * don't try to use it.
408 extra_pfn_end = min(min(max_pfn, e820_end_of_ram_pfn()),
409 (unsigned long)PFN_DOWN(xen_extra_mem_start + xen_extra_mem_size));
410 for (pfn = PFN_UP(xen_extra_mem_start);
413 page = pfn_to_page(pfn);
414 /* totalram_pages doesn't include the boot-time
415 balloon extension, so don't subtract from it. */
416 __balloon_append(page);
419 target_watch.callback = watch_target;
420 xenstore_notifier.notifier_call = balloon_init_watcher;
422 register_xenstore_notifier(&xenstore_notifier);
427 subsys_initcall(balloon_init);
429 static void balloon_exit(void)
431 /* XXX - release balloon here */
435 module_exit(balloon_exit);
437 #define BALLOON_SHOW(name, format, args...) \
438 static ssize_t show_##name(struct sys_device *dev, \
439 struct sysdev_attribute *attr, \
442 return sprintf(buf, format, ##args); \
444 static SYSDEV_ATTR(name, S_IRUGO, show_##name, NULL)
446 BALLOON_SHOW(current_kb, "%lu\n", PAGES2KB(balloon_stats.current_pages));
447 BALLOON_SHOW(low_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_low));
448 BALLOON_SHOW(high_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_high));
450 static ssize_t show_target_kb(struct sys_device *dev, struct sysdev_attribute *attr,
453 return sprintf(buf, "%lu\n", PAGES2KB(balloon_stats.target_pages));
456 static ssize_t store_target_kb(struct sys_device *dev,
457 struct sysdev_attribute *attr,
462 unsigned long long target_bytes;
464 if (!capable(CAP_SYS_ADMIN))
467 target_bytes = simple_strtoull(buf, &endchar, 0) * 1024;
469 balloon_set_new_target(target_bytes >> PAGE_SHIFT);
474 static SYSDEV_ATTR(target_kb, S_IRUGO | S_IWUSR,
475 show_target_kb, store_target_kb);
478 static ssize_t show_target(struct sys_device *dev, struct sysdev_attribute *attr,
481 return sprintf(buf, "%llu\n",
482 (unsigned long long)balloon_stats.target_pages
486 static ssize_t store_target(struct sys_device *dev,
487 struct sysdev_attribute *attr,
492 unsigned long long target_bytes;
494 if (!capable(CAP_SYS_ADMIN))
497 target_bytes = memparse(buf, &endchar);
499 balloon_set_new_target(target_bytes >> PAGE_SHIFT);
504 static SYSDEV_ATTR(target, S_IRUGO | S_IWUSR,
505 show_target, store_target);
508 static struct sysdev_attribute *balloon_attrs[] = {
513 static struct attribute *balloon_info_attrs[] = {
514 &attr_current_kb.attr,
520 static struct attribute_group balloon_info_group = {
522 .attrs = balloon_info_attrs,
525 static struct sysdev_class balloon_sysdev_class = {
526 .name = BALLOON_CLASS_NAME,
529 static int register_balloon(struct sys_device *sysdev)
533 error = sysdev_class_register(&balloon_sysdev_class);
538 sysdev->cls = &balloon_sysdev_class;
540 error = sysdev_register(sysdev);
542 sysdev_class_unregister(&balloon_sysdev_class);
546 for (i = 0; i < ARRAY_SIZE(balloon_attrs); i++) {
547 error = sysdev_create_file(sysdev, balloon_attrs[i]);
552 error = sysfs_create_group(&sysdev->kobj, &balloon_info_group);
560 sysdev_remove_file(sysdev, balloon_attrs[i]);
561 sysdev_unregister(sysdev);
562 sysdev_class_unregister(&balloon_sysdev_class);
566 MODULE_LICENSE("GPL");