xen-balloon: fix up sysfs issues
[linux-2.6/mini2440.git] / drivers / xen / balloon.c
blobcdec2d843e4c54e6f5a43e7964c2e8f9cc5ddd0b
1 /******************************************************************************
2 * balloon.c
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
32 * IN THE SOFTWARE.
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>
44 #include <linux/highmem.h>
45 #include <linux/list.h>
46 #include <linux/sysdev.h>
48 #include <asm/xen/hypervisor.h>
49 #include <asm/page.h>
50 #include <asm/pgalloc.h>
51 #include <asm/pgtable.h>
52 #include <asm/uaccess.h>
53 #include <asm/tlb.h>
55 #include <xen/interface/memory.h>
56 #include <xen/balloon.h>
57 #include <xen/xenbus.h>
58 #include <xen/features.h>
59 #include <xen/page.h>
61 #define PAGES2KB(_p) ((_p)<<(PAGE_SHIFT-10))
63 #define BALLOON_CLASS_NAME "xen_memory"
65 struct balloon_stats {
66 /* We aim for 'current allocation' == 'target allocation'. */
67 unsigned long current_pages;
68 unsigned long target_pages;
69 /* We may hit the hard limit in Xen. If we do then we remember it. */
70 unsigned long hard_limit;
72 * Drivers may alter the memory reservation independently, but they
73 * must inform the balloon driver so we avoid hitting the hard limit.
75 unsigned long driver_pages;
76 /* Number of pages in high- and low-memory balloons. */
77 unsigned long balloon_low;
78 unsigned long balloon_high;
81 static DEFINE_MUTEX(balloon_mutex);
83 static struct sys_device balloon_sysdev;
85 static int register_balloon(struct sys_device *sysdev);
88 * Protects atomic reservation decrease/increase against concurrent increases.
89 * Also protects non-atomic updates of current_pages and driver_pages, and
90 * balloon lists.
92 static DEFINE_SPINLOCK(balloon_lock);
94 static struct balloon_stats balloon_stats;
96 /* We increase/decrease in batches which fit in a page */
97 static unsigned long frame_list[PAGE_SIZE / sizeof(unsigned long)];
99 /* VM /proc information for memory */
100 extern unsigned long totalram_pages;
102 #ifdef CONFIG_HIGHMEM
103 extern unsigned long totalhigh_pages;
104 #define inc_totalhigh_pages() (totalhigh_pages++)
105 #define dec_totalhigh_pages() (totalhigh_pages--)
106 #else
107 #define inc_totalhigh_pages() do {} while(0)
108 #define dec_totalhigh_pages() do {} while(0)
109 #endif
111 /* List of ballooned pages, threaded through the mem_map array. */
112 static LIST_HEAD(ballooned_pages);
114 /* Main work function, always executed in process context. */
115 static void balloon_process(struct work_struct *work);
116 static DECLARE_WORK(balloon_worker, balloon_process);
117 static struct timer_list balloon_timer;
119 /* When ballooning out (allocating memory to return to Xen) we don't really
120 want the kernel to try too hard since that can trigger the oom killer. */
121 #define GFP_BALLOON \
122 (GFP_HIGHUSER | __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC)
124 static void scrub_page(struct page *page)
126 #ifdef CONFIG_XEN_SCRUB_PAGES
127 if (PageHighMem(page)) {
128 void *v = kmap(page);
129 clear_page(v);
130 kunmap(v);
131 } else {
132 void *v = page_address(page);
133 clear_page(v);
135 #endif
138 /* balloon_append: add the given page to the balloon. */
139 static void balloon_append(struct page *page)
141 /* Lowmem is re-populated first, so highmem pages go at list tail. */
142 if (PageHighMem(page)) {
143 list_add_tail(&page->lru, &ballooned_pages);
144 balloon_stats.balloon_high++;
145 dec_totalhigh_pages();
146 } else {
147 list_add(&page->lru, &ballooned_pages);
148 balloon_stats.balloon_low++;
152 /* balloon_retrieve: rescue a page from the balloon, if it is not empty. */
153 static struct page *balloon_retrieve(void)
155 struct page *page;
157 if (list_empty(&ballooned_pages))
158 return NULL;
160 page = list_entry(ballooned_pages.next, struct page, lru);
161 list_del(&page->lru);
163 if (PageHighMem(page)) {
164 balloon_stats.balloon_high--;
165 inc_totalhigh_pages();
167 else
168 balloon_stats.balloon_low--;
170 return page;
173 static struct page *balloon_first_page(void)
175 if (list_empty(&ballooned_pages))
176 return NULL;
177 return list_entry(ballooned_pages.next, struct page, lru);
180 static struct page *balloon_next_page(struct page *page)
182 struct list_head *next = page->lru.next;
183 if (next == &ballooned_pages)
184 return NULL;
185 return list_entry(next, struct page, lru);
188 static void balloon_alarm(unsigned long unused)
190 schedule_work(&balloon_worker);
193 static unsigned long current_target(void)
195 unsigned long target = min(balloon_stats.target_pages, balloon_stats.hard_limit);
197 target = min(target,
198 balloon_stats.current_pages +
199 balloon_stats.balloon_low +
200 balloon_stats.balloon_high);
202 return target;
205 static int increase_reservation(unsigned long nr_pages)
207 unsigned long pfn, i, flags;
208 struct page *page;
209 long rc;
210 struct xen_memory_reservation reservation = {
211 .address_bits = 0,
212 .extent_order = 0,
213 .domid = DOMID_SELF
216 if (nr_pages > ARRAY_SIZE(frame_list))
217 nr_pages = ARRAY_SIZE(frame_list);
219 spin_lock_irqsave(&balloon_lock, flags);
221 page = balloon_first_page();
222 for (i = 0; i < nr_pages; i++) {
223 BUG_ON(page == NULL);
224 frame_list[i] = page_to_pfn(page);;
225 page = balloon_next_page(page);
228 set_xen_guest_handle(reservation.extent_start, frame_list);
229 reservation.nr_extents = nr_pages;
230 rc = HYPERVISOR_memory_op(
231 XENMEM_populate_physmap, &reservation);
232 if (rc < nr_pages) {
233 if (rc > 0) {
234 int ret;
236 /* We hit the Xen hard limit: reprobe. */
237 reservation.nr_extents = rc;
238 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
239 &reservation);
240 BUG_ON(ret != rc);
242 if (rc >= 0)
243 balloon_stats.hard_limit = (balloon_stats.current_pages + rc -
244 balloon_stats.driver_pages);
245 goto out;
248 for (i = 0; i < nr_pages; i++) {
249 page = balloon_retrieve();
250 BUG_ON(page == NULL);
252 pfn = page_to_pfn(page);
253 BUG_ON(!xen_feature(XENFEAT_auto_translated_physmap) &&
254 phys_to_machine_mapping_valid(pfn));
256 set_phys_to_machine(pfn, frame_list[i]);
258 /* Link back into the page tables if not highmem. */
259 if (pfn < max_low_pfn) {
260 int ret;
261 ret = HYPERVISOR_update_va_mapping(
262 (unsigned long)__va(pfn << PAGE_SHIFT),
263 mfn_pte(frame_list[i], PAGE_KERNEL),
265 BUG_ON(ret);
268 /* Relinquish the page back to the allocator. */
269 ClearPageReserved(page);
270 init_page_count(page);
271 __free_page(page);
274 balloon_stats.current_pages += nr_pages;
275 totalram_pages = balloon_stats.current_pages;
277 out:
278 spin_unlock_irqrestore(&balloon_lock, flags);
280 return 0;
283 static int decrease_reservation(unsigned long nr_pages)
285 unsigned long pfn, i, flags;
286 struct page *page;
287 int need_sleep = 0;
288 int ret;
289 struct xen_memory_reservation reservation = {
290 .address_bits = 0,
291 .extent_order = 0,
292 .domid = DOMID_SELF
295 if (nr_pages > ARRAY_SIZE(frame_list))
296 nr_pages = ARRAY_SIZE(frame_list);
298 for (i = 0; i < nr_pages; i++) {
299 if ((page = alloc_page(GFP_BALLOON)) == NULL) {
300 nr_pages = i;
301 need_sleep = 1;
302 break;
305 pfn = page_to_pfn(page);
306 frame_list[i] = pfn_to_mfn(pfn);
308 scrub_page(page);
311 /* Ensure that ballooned highmem pages don't have kmaps. */
312 kmap_flush_unused();
313 flush_tlb_all();
315 spin_lock_irqsave(&balloon_lock, flags);
317 /* No more mappings: invalidate P2M and add to balloon. */
318 for (i = 0; i < nr_pages; i++) {
319 pfn = mfn_to_pfn(frame_list[i]);
320 set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
321 balloon_append(pfn_to_page(pfn));
324 set_xen_guest_handle(reservation.extent_start, frame_list);
325 reservation.nr_extents = nr_pages;
326 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
327 BUG_ON(ret != nr_pages);
329 balloon_stats.current_pages -= nr_pages;
330 totalram_pages = balloon_stats.current_pages;
332 spin_unlock_irqrestore(&balloon_lock, flags);
334 return need_sleep;
338 * We avoid multiple worker processes conflicting via the balloon mutex.
339 * We may of course race updates of the target counts (which are protected
340 * by the balloon lock), or with changes to the Xen hard limit, but we will
341 * recover from these in time.
343 static void balloon_process(struct work_struct *work)
345 int need_sleep = 0;
346 long credit;
348 mutex_lock(&balloon_mutex);
350 do {
351 credit = current_target() - balloon_stats.current_pages;
352 if (credit > 0)
353 need_sleep = (increase_reservation(credit) != 0);
354 if (credit < 0)
355 need_sleep = (decrease_reservation(-credit) != 0);
357 #ifndef CONFIG_PREEMPT
358 if (need_resched())
359 schedule();
360 #endif
361 } while ((credit != 0) && !need_sleep);
363 /* Schedule more work if there is some still to be done. */
364 if (current_target() != balloon_stats.current_pages)
365 mod_timer(&balloon_timer, jiffies + HZ);
367 mutex_unlock(&balloon_mutex);
370 /* Resets the Xen limit, sets new target, and kicks off processing. */
371 static void balloon_set_new_target(unsigned long target)
373 /* No need for lock. Not read-modify-write updates. */
374 balloon_stats.hard_limit = ~0UL;
375 balloon_stats.target_pages = target;
376 schedule_work(&balloon_worker);
379 static struct xenbus_watch target_watch =
381 .node = "memory/target"
384 /* React to a change in the target key */
385 static void watch_target(struct xenbus_watch *watch,
386 const char **vec, unsigned int len)
388 unsigned long long new_target;
389 int err;
391 err = xenbus_scanf(XBT_NIL, "memory", "target", "%llu", &new_target);
392 if (err != 1) {
393 /* This is ok (for domain0 at least) - so just return */
394 return;
397 /* The given memory/target value is in KiB, so it needs converting to
398 * pages. PAGE_SHIFT converts bytes to pages, hence PAGE_SHIFT - 10.
400 balloon_set_new_target(new_target >> (PAGE_SHIFT - 10));
403 static int balloon_init_watcher(struct notifier_block *notifier,
404 unsigned long event,
405 void *data)
407 int err;
409 err = register_xenbus_watch(&target_watch);
410 if (err)
411 printk(KERN_ERR "Failed to set balloon watcher\n");
413 return NOTIFY_DONE;
416 static struct notifier_block xenstore_notifier;
418 static int __init balloon_init(void)
420 unsigned long pfn;
421 struct page *page;
423 if (!is_running_on_xen())
424 return -ENODEV;
426 pr_info("xen_balloon: Initialising balloon driver.\n");
428 balloon_stats.current_pages = min(xen_start_info->nr_pages, max_pfn);
429 totalram_pages = balloon_stats.current_pages;
430 balloon_stats.target_pages = balloon_stats.current_pages;
431 balloon_stats.balloon_low = 0;
432 balloon_stats.balloon_high = 0;
433 balloon_stats.driver_pages = 0UL;
434 balloon_stats.hard_limit = ~0UL;
436 init_timer(&balloon_timer);
437 balloon_timer.data = 0;
438 balloon_timer.function = balloon_alarm;
440 register_balloon(&balloon_sysdev);
442 /* Initialise the balloon with excess memory space. */
443 for (pfn = xen_start_info->nr_pages; pfn < max_pfn; pfn++) {
444 page = pfn_to_page(pfn);
445 if (!PageReserved(page))
446 balloon_append(page);
449 target_watch.callback = watch_target;
450 xenstore_notifier.notifier_call = balloon_init_watcher;
452 register_xenstore_notifier(&xenstore_notifier);
454 return 0;
457 subsys_initcall(balloon_init);
459 static void balloon_exit(void)
461 /* XXX - release balloon here */
462 return;
465 module_exit(balloon_exit);
467 static void balloon_update_driver_allowance(long delta)
469 unsigned long flags;
471 spin_lock_irqsave(&balloon_lock, flags);
472 balloon_stats.driver_pages += delta;
473 spin_unlock_irqrestore(&balloon_lock, flags);
476 static int dealloc_pte_fn(
477 pte_t *pte, struct page *pmd_page, unsigned long addr, void *data)
479 unsigned long mfn = pte_mfn(*pte);
480 int ret;
481 struct xen_memory_reservation reservation = {
482 .nr_extents = 1,
483 .extent_order = 0,
484 .domid = DOMID_SELF
486 set_xen_guest_handle(reservation.extent_start, &mfn);
487 set_pte_at(&init_mm, addr, pte, __pte_ma(0ull));
488 set_phys_to_machine(__pa(addr) >> PAGE_SHIFT, INVALID_P2M_ENTRY);
489 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
490 BUG_ON(ret != 1);
491 return 0;
494 static struct page **alloc_empty_pages_and_pagevec(int nr_pages)
496 unsigned long vaddr, flags;
497 struct page *page, **pagevec;
498 int i, ret;
500 pagevec = kmalloc(sizeof(page) * nr_pages, GFP_KERNEL);
501 if (pagevec == NULL)
502 return NULL;
504 for (i = 0; i < nr_pages; i++) {
505 page = pagevec[i] = alloc_page(GFP_KERNEL);
506 if (page == NULL)
507 goto err;
509 vaddr = (unsigned long)page_address(page);
511 scrub_page(page);
513 spin_lock_irqsave(&balloon_lock, flags);
515 if (xen_feature(XENFEAT_auto_translated_physmap)) {
516 unsigned long gmfn = page_to_pfn(page);
517 struct xen_memory_reservation reservation = {
518 .nr_extents = 1,
519 .extent_order = 0,
520 .domid = DOMID_SELF
522 set_xen_guest_handle(reservation.extent_start, &gmfn);
523 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
524 &reservation);
525 if (ret == 1)
526 ret = 0; /* success */
527 } else {
528 ret = apply_to_page_range(&init_mm, vaddr, PAGE_SIZE,
529 dealloc_pte_fn, NULL);
532 if (ret != 0) {
533 spin_unlock_irqrestore(&balloon_lock, flags);
534 __free_page(page);
535 goto err;
538 totalram_pages = --balloon_stats.current_pages;
540 spin_unlock_irqrestore(&balloon_lock, flags);
543 out:
544 schedule_work(&balloon_worker);
545 flush_tlb_all();
546 return pagevec;
548 err:
549 spin_lock_irqsave(&balloon_lock, flags);
550 while (--i >= 0)
551 balloon_append(pagevec[i]);
552 spin_unlock_irqrestore(&balloon_lock, flags);
553 kfree(pagevec);
554 pagevec = NULL;
555 goto out;
558 static void free_empty_pages_and_pagevec(struct page **pagevec, int nr_pages)
560 unsigned long flags;
561 int i;
563 if (pagevec == NULL)
564 return;
566 spin_lock_irqsave(&balloon_lock, flags);
567 for (i = 0; i < nr_pages; i++) {
568 BUG_ON(page_count(pagevec[i]) != 1);
569 balloon_append(pagevec[i]);
571 spin_unlock_irqrestore(&balloon_lock, flags);
573 kfree(pagevec);
575 schedule_work(&balloon_worker);
578 static void balloon_release_driver_page(struct page *page)
580 unsigned long flags;
582 spin_lock_irqsave(&balloon_lock, flags);
583 balloon_append(page);
584 balloon_stats.driver_pages--;
585 spin_unlock_irqrestore(&balloon_lock, flags);
587 schedule_work(&balloon_worker);
591 #define BALLOON_SHOW(name, format, args...) \
592 static ssize_t show_##name(struct sys_device *dev, \
593 struct sysdev_attribute *attr, \
594 char *buf) \
596 return sprintf(buf, format, ##args); \
598 static SYSDEV_ATTR(name, S_IRUGO, show_##name, NULL)
600 BALLOON_SHOW(current_kb, "%lu\n", PAGES2KB(balloon_stats.current_pages));
601 BALLOON_SHOW(low_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_low));
602 BALLOON_SHOW(high_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_high));
603 BALLOON_SHOW(hard_limit_kb,
604 (balloon_stats.hard_limit!=~0UL) ? "%lu\n" : "???\n",
605 (balloon_stats.hard_limit!=~0UL) ? PAGES2KB(balloon_stats.hard_limit) : 0);
606 BALLOON_SHOW(driver_kb, "%lu\n", PAGES2KB(balloon_stats.driver_pages));
608 static ssize_t show_target_kb(struct sys_device *dev, struct sysdev_attribute *attr,
609 char *buf)
611 return sprintf(buf, "%lu\n", PAGES2KB(balloon_stats.target_pages));
614 static ssize_t store_target_kb(struct sys_device *dev,
615 struct sysdev_attribute *attr,
616 const char *buf,
617 size_t count)
619 char *endchar;
620 unsigned long long target_bytes;
622 if (!capable(CAP_SYS_ADMIN))
623 return -EPERM;
625 target_bytes = memparse(buf, &endchar);
627 balloon_set_new_target(target_bytes >> PAGE_SHIFT);
629 return count;
632 static SYSDEV_ATTR(target_kb, S_IRUGO | S_IWUSR,
633 show_target_kb, store_target_kb);
635 static struct sysdev_attribute *balloon_attrs[] = {
636 &attr_target_kb,
639 static struct attribute *balloon_info_attrs[] = {
640 &attr_current_kb.attr,
641 &attr_low_kb.attr,
642 &attr_high_kb.attr,
643 &attr_hard_limit_kb.attr,
644 &attr_driver_kb.attr,
645 NULL
648 static struct attribute_group balloon_info_group = {
649 .name = "info",
650 .attrs = balloon_info_attrs,
653 static struct sysdev_class balloon_sysdev_class = {
654 .name = BALLOON_CLASS_NAME,
657 static int register_balloon(struct sys_device *sysdev)
659 int i, error;
661 error = sysdev_class_register(&balloon_sysdev_class);
662 if (error)
663 return error;
665 sysdev->id = 0;
666 sysdev->cls = &balloon_sysdev_class;
668 error = sysdev_register(sysdev);
669 if (error) {
670 sysdev_class_unregister(&balloon_sysdev_class);
671 return error;
674 for (i = 0; i < ARRAY_SIZE(balloon_attrs); i++) {
675 error = sysdev_create_file(sysdev, balloon_attrs[i]);
676 if (error)
677 goto fail;
680 error = sysfs_create_group(&sysdev->kobj, &balloon_info_group);
681 if (error)
682 goto fail;
684 return 0;
686 fail:
687 while (--i >= 0)
688 sysdev_remove_file(sysdev, balloon_attrs[i]);
689 sysdev_unregister(sysdev);
690 sysdev_class_unregister(&balloon_sysdev_class);
691 return error;
694 static void unregister_balloon(struct sys_device *sysdev)
696 int i;
698 sysfs_remove_group(&sysdev->kobj, &balloon_info_group);
699 for (i = 0; i < ARRAY_SIZE(balloon_attrs); i++)
700 sysdev_remove_file(sysdev, balloon_attrs[i]);
701 sysdev_unregister(sysdev);
702 sysdev_class_unregister(&balloon_sysdev_class);
705 static void balloon_sysfs_exit(void)
707 unregister_balloon(&balloon_sysdev);
710 MODULE_LICENSE("GPL");