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_WORK(balloon_worker
, balloon_process
);
103 static struct timer_list balloon_timer
;
105 /* When ballooning out (allocating memory to return to Xen) we don't really
106 want the kernel to try too hard since that can trigger the oom killer. */
107 #define GFP_BALLOON \
108 (GFP_HIGHUSER | __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC)
110 static void scrub_page(struct page
*page
)
112 #ifdef CONFIG_XEN_SCRUB_PAGES
113 clear_highpage(page
);
117 /* balloon_append: add the given page to the balloon. */
118 static void __balloon_append(struct page
*page
)
120 /* Lowmem is re-populated first, so highmem pages go at list tail. */
121 if (PageHighMem(page
)) {
122 list_add_tail(&page
->lru
, &ballooned_pages
);
123 balloon_stats
.balloon_high
++;
124 dec_totalhigh_pages();
126 list_add(&page
->lru
, &ballooned_pages
);
127 balloon_stats
.balloon_low
++;
131 static void balloon_append(struct page
*page
)
133 __balloon_append(page
);
137 /* balloon_retrieve: rescue a page from the balloon, if it is not empty. */
138 static struct page
*balloon_retrieve(void)
142 if (list_empty(&ballooned_pages
))
145 page
= list_entry(ballooned_pages
.next
, struct page
, lru
);
146 list_del(&page
->lru
);
148 if (PageHighMem(page
)) {
149 balloon_stats
.balloon_high
--;
150 inc_totalhigh_pages();
153 balloon_stats
.balloon_low
--;
160 static struct page
*balloon_first_page(void)
162 if (list_empty(&ballooned_pages
))
164 return list_entry(ballooned_pages
.next
, struct page
, lru
);
167 static struct page
*balloon_next_page(struct page
*page
)
169 struct list_head
*next
= page
->lru
.next
;
170 if (next
== &ballooned_pages
)
172 return list_entry(next
, struct page
, lru
);
175 static void balloon_alarm(unsigned long unused
)
177 schedule_work(&balloon_worker
);
180 static unsigned long current_target(void)
182 unsigned long target
= balloon_stats
.target_pages
;
185 balloon_stats
.current_pages
+
186 balloon_stats
.balloon_low
+
187 balloon_stats
.balloon_high
);
192 static int increase_reservation(unsigned long nr_pages
)
194 unsigned long pfn
, i
;
197 struct xen_memory_reservation reservation
= {
203 if (nr_pages
> ARRAY_SIZE(frame_list
))
204 nr_pages
= ARRAY_SIZE(frame_list
);
206 page
= balloon_first_page();
207 for (i
= 0; i
< nr_pages
; i
++) {
208 BUG_ON(page
== NULL
);
209 frame_list
[i
] = page_to_pfn(page
);
210 page
= balloon_next_page(page
);
213 set_xen_guest_handle(reservation
.extent_start
, frame_list
);
214 reservation
.nr_extents
= nr_pages
;
215 rc
= HYPERVISOR_memory_op(XENMEM_populate_physmap
, &reservation
);
219 for (i
= 0; i
< rc
; i
++) {
220 page
= balloon_retrieve();
221 BUG_ON(page
== NULL
);
223 pfn
= page_to_pfn(page
);
224 BUG_ON(!xen_feature(XENFEAT_auto_translated_physmap
) &&
225 phys_to_machine_mapping_valid(pfn
));
227 set_phys_to_machine(pfn
, frame_list
[i
]);
229 /* Link back into the page tables if not highmem. */
230 if (pfn
< max_low_pfn
) {
232 ret
= HYPERVISOR_update_va_mapping(
233 (unsigned long)__va(pfn
<< PAGE_SHIFT
),
234 mfn_pte(frame_list
[i
], PAGE_KERNEL
),
239 /* Relinquish the page back to the allocator. */
240 ClearPageReserved(page
);
241 init_page_count(page
);
245 balloon_stats
.current_pages
+= rc
;
248 return rc
< 0 ? rc
: rc
!= nr_pages
;
251 static int decrease_reservation(unsigned long nr_pages
)
253 unsigned long pfn
, i
;
257 struct xen_memory_reservation reservation
= {
263 if (nr_pages
> ARRAY_SIZE(frame_list
))
264 nr_pages
= ARRAY_SIZE(frame_list
);
266 for (i
= 0; i
< nr_pages
; i
++) {
267 if ((page
= alloc_page(GFP_BALLOON
)) == NULL
) {
273 pfn
= page_to_pfn(page
);
274 frame_list
[i
] = pfn_to_mfn(pfn
);
278 if (!PageHighMem(page
)) {
279 ret
= HYPERVISOR_update_va_mapping(
280 (unsigned long)__va(pfn
<< PAGE_SHIFT
),
287 /* Ensure that ballooned highmem pages don't have kmaps. */
291 /* No more mappings: invalidate P2M and add to balloon. */
292 for (i
= 0; i
< nr_pages
; i
++) {
293 pfn
= mfn_to_pfn(frame_list
[i
]);
294 set_phys_to_machine(pfn
, INVALID_P2M_ENTRY
);
295 balloon_append(pfn_to_page(pfn
));
298 set_xen_guest_handle(reservation
.extent_start
, frame_list
);
299 reservation
.nr_extents
= nr_pages
;
300 ret
= HYPERVISOR_memory_op(XENMEM_decrease_reservation
, &reservation
);
301 BUG_ON(ret
!= nr_pages
);
303 balloon_stats
.current_pages
-= nr_pages
;
309 * We avoid multiple worker processes conflicting via the balloon mutex.
310 * We may of course race updates of the target counts (which are protected
311 * by the balloon lock), or with changes to the Xen hard limit, but we will
312 * recover from these in time.
314 static void balloon_process(struct work_struct
*work
)
319 mutex_lock(&balloon_mutex
);
322 credit
= current_target() - balloon_stats
.current_pages
;
324 need_sleep
= (increase_reservation(credit
) != 0);
326 need_sleep
= (decrease_reservation(-credit
) != 0);
328 #ifndef CONFIG_PREEMPT
332 } while ((credit
!= 0) && !need_sleep
);
334 /* Schedule more work if there is some still to be done. */
335 if (current_target() != balloon_stats
.current_pages
)
336 mod_timer(&balloon_timer
, jiffies
+ HZ
);
338 mutex_unlock(&balloon_mutex
);
341 /* Resets the Xen limit, sets new target, and kicks off processing. */
342 static void balloon_set_new_target(unsigned long target
)
344 /* No need for lock. Not read-modify-write updates. */
345 balloon_stats
.target_pages
= target
;
346 schedule_work(&balloon_worker
);
349 static struct xenbus_watch target_watch
=
351 .node
= "memory/target"
354 /* React to a change in the target key */
355 static void watch_target(struct xenbus_watch
*watch
,
356 const char **vec
, unsigned int len
)
358 unsigned long long new_target
;
361 err
= xenbus_scanf(XBT_NIL
, "memory", "target", "%llu", &new_target
);
363 /* This is ok (for domain0 at least) - so just return */
367 /* The given memory/target value is in KiB, so it needs converting to
368 * pages. PAGE_SHIFT converts bytes to pages, hence PAGE_SHIFT - 10.
370 balloon_set_new_target(new_target
>> (PAGE_SHIFT
- 10));
373 static int balloon_init_watcher(struct notifier_block
*notifier
,
379 err
= register_xenbus_watch(&target_watch
);
381 printk(KERN_ERR
"Failed to set balloon watcher\n");
386 static struct notifier_block xenstore_notifier
;
388 static int __init
balloon_init(void)
390 unsigned long pfn
, extra_pfn_end
;
393 if (!xen_pv_domain())
396 pr_info("xen_balloon: Initialising balloon driver.\n");
398 balloon_stats
.current_pages
= min(xen_start_info
->nr_pages
, max_pfn
);
399 balloon_stats
.target_pages
= balloon_stats
.current_pages
;
400 balloon_stats
.balloon_low
= 0;
401 balloon_stats
.balloon_high
= 0;
403 init_timer(&balloon_timer
);
404 balloon_timer
.data
= 0;
405 balloon_timer
.function
= balloon_alarm
;
407 register_balloon(&balloon_sysdev
);
410 * Initialise the balloon with excess memory space. We need
411 * to make sure we don't add memory which doesn't exist or
412 * logically exist. The E820 map can be trimmed to be smaller
413 * than the amount of physical memory due to the mem= command
414 * line parameter. And if this is a 32-bit non-HIGHMEM kernel
415 * on a system with memory which requires highmem to access,
416 * don't try to use it.
418 extra_pfn_end
= min(min(max_pfn
, e820_end_of_ram_pfn()),
419 (unsigned long)PFN_DOWN(xen_extra_mem_start
+ xen_extra_mem_size
));
420 for (pfn
= PFN_UP(xen_extra_mem_start
);
423 page
= pfn_to_page(pfn
);
424 /* totalram_pages doesn't include the boot-time
425 balloon extension, so don't subtract from it. */
426 __balloon_append(page
);
429 target_watch
.callback
= watch_target
;
430 xenstore_notifier
.notifier_call
= balloon_init_watcher
;
432 register_xenstore_notifier(&xenstore_notifier
);
437 subsys_initcall(balloon_init
);
439 static void balloon_exit(void)
441 /* XXX - release balloon here */
445 module_exit(balloon_exit
);
447 #define BALLOON_SHOW(name, format, args...) \
448 static ssize_t show_##name(struct sys_device *dev, \
449 struct sysdev_attribute *attr, \
452 return sprintf(buf, format, ##args); \
454 static SYSDEV_ATTR(name, S_IRUGO, show_##name, NULL)
456 BALLOON_SHOW(current_kb
, "%lu\n", PAGES2KB(balloon_stats
.current_pages
));
457 BALLOON_SHOW(low_kb
, "%lu\n", PAGES2KB(balloon_stats
.balloon_low
));
458 BALLOON_SHOW(high_kb
, "%lu\n", PAGES2KB(balloon_stats
.balloon_high
));
460 static ssize_t
show_target_kb(struct sys_device
*dev
, struct sysdev_attribute
*attr
,
463 return sprintf(buf
, "%lu\n", PAGES2KB(balloon_stats
.target_pages
));
466 static ssize_t
store_target_kb(struct sys_device
*dev
,
467 struct sysdev_attribute
*attr
,
472 unsigned long long target_bytes
;
474 if (!capable(CAP_SYS_ADMIN
))
477 target_bytes
= simple_strtoull(buf
, &endchar
, 0) * 1024;
479 balloon_set_new_target(target_bytes
>> PAGE_SHIFT
);
484 static SYSDEV_ATTR(target_kb
, S_IRUGO
| S_IWUSR
,
485 show_target_kb
, store_target_kb
);
488 static ssize_t
show_target(struct sys_device
*dev
, struct sysdev_attribute
*attr
,
491 return sprintf(buf
, "%llu\n",
492 (unsigned long long)balloon_stats
.target_pages
496 static ssize_t
store_target(struct sys_device
*dev
,
497 struct sysdev_attribute
*attr
,
502 unsigned long long target_bytes
;
504 if (!capable(CAP_SYS_ADMIN
))
507 target_bytes
= memparse(buf
, &endchar
);
509 balloon_set_new_target(target_bytes
>> PAGE_SHIFT
);
514 static SYSDEV_ATTR(target
, S_IRUGO
| S_IWUSR
,
515 show_target
, store_target
);
518 static struct sysdev_attribute
*balloon_attrs
[] = {
523 static struct attribute
*balloon_info_attrs
[] = {
524 &attr_current_kb
.attr
,
530 static struct attribute_group balloon_info_group
= {
532 .attrs
= balloon_info_attrs
,
535 static struct sysdev_class balloon_sysdev_class
= {
536 .name
= BALLOON_CLASS_NAME
,
539 static int register_balloon(struct sys_device
*sysdev
)
543 error
= sysdev_class_register(&balloon_sysdev_class
);
548 sysdev
->cls
= &balloon_sysdev_class
;
550 error
= sysdev_register(sysdev
);
552 sysdev_class_unregister(&balloon_sysdev_class
);
556 for (i
= 0; i
< ARRAY_SIZE(balloon_attrs
); i
++) {
557 error
= sysdev_create_file(sysdev
, balloon_attrs
[i
]);
562 error
= sysfs_create_group(&sysdev
->kobj
, &balloon_info_group
);
570 sysdev_remove_file(sysdev
, balloon_attrs
[i
]);
571 sysdev_unregister(sysdev
);
572 sysdev_class_unregister(&balloon_sysdev_class
);
576 MODULE_LICENSE("GPL");