ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/balloon/balloon.c @ 9880:8bf5637cb269

Relax BUG_ON() state ment in increase_reservation() in balloon.c.
When xen_feature(XENFEAT_auto_translated_physmap) = 1,
phys_to_machine_mapping_valid(pfn) always returns true.
Relaxed BUG_ON() for xen_feature(XENFEAT_auto_translated_physmap) = 1 case.

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
author kaf24@firebug.cl.cam.ac.uk
date Fri Apr 28 08:30:52 2006 +0100 (2006-04-28)
parents ec61a8c25429
children dfdc32a9814f
line source
1 /******************************************************************************
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 */
35 #include <linux/config.h>
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/sched.h>
39 #include <linux/errno.h>
40 #include <linux/mm.h>
41 #include <linux/mman.h>
42 #include <linux/smp_lock.h>
43 #include <linux/pagemap.h>
44 #include <linux/bootmem.h>
45 #include <linux/highmem.h>
46 #include <linux/vmalloc.h>
47 #include <xen/xen_proc.h>
48 #include <asm/hypervisor.h>
49 #include <xen/balloon.h>
50 #include <xen/interface/memory.h>
51 #include <asm/pgalloc.h>
52 #include <asm/pgtable.h>
53 #include <asm/uaccess.h>
54 #include <asm/tlb.h>
55 #include <linux/list.h>
57 #include <xen/xenbus.h>
59 #define PAGES2KB(_p) ((_p)<<(PAGE_SHIFT-10))
61 static struct proc_dir_entry *balloon_pde;
63 static DECLARE_MUTEX(balloon_mutex);
65 /*
66 * Protects atomic reservation decrease/increase against concurrent increases.
67 * Also protects non-atomic updates of current_pages and driver_pages, and
68 * balloon lists.
69 */
70 spinlock_t balloon_lock = SPIN_LOCK_UNLOCKED;
72 /* We aim for 'current allocation' == 'target allocation'. */
73 static unsigned long current_pages;
74 static unsigned long target_pages;
76 /* VM /proc information for memory */
77 extern unsigned long totalram_pages;
79 /* We may hit the hard limit in Xen. If we do then we remember it. */
80 static unsigned long hard_limit;
82 /*
83 * Drivers may alter the memory reservation independently, but they must
84 * inform the balloon driver so that we can avoid hitting the hard limit.
85 */
86 static unsigned long driver_pages;
88 /* List of ballooned pages, threaded through the mem_map array. */
89 static LIST_HEAD(ballooned_pages);
90 static unsigned long balloon_low, balloon_high;
92 /* Main work function, always executed in process context. */
93 static void balloon_process(void *unused);
94 static DECLARE_WORK(balloon_worker, balloon_process, NULL);
95 static struct timer_list balloon_timer;
97 #define PAGE_TO_LIST(p) (&(p)->ballooned)
98 #define LIST_TO_PAGE(l) list_entry((l), struct page, ballooned)
99 #define UNLIST_PAGE(p) \
100 do { \
101 list_del(PAGE_TO_LIST(p)); \
102 PAGE_TO_LIST(p)->next = NULL; \
103 PAGE_TO_LIST(p)->prev = NULL; \
104 } while(0)
106 #define IPRINTK(fmt, args...) \
107 printk(KERN_INFO "xen_mem: " fmt, ##args)
108 #define WPRINTK(fmt, args...) \
109 printk(KERN_WARNING "xen_mem: " fmt, ##args)
111 /* balloon_append: add the given page to the balloon. */
112 static void balloon_append(struct page *page)
113 {
114 /* Lowmem is re-populated first, so highmem pages go at list tail. */
115 if (PageHighMem(page)) {
116 list_add_tail(PAGE_TO_LIST(page), &ballooned_pages);
117 balloon_high++;
118 } else {
119 list_add(PAGE_TO_LIST(page), &ballooned_pages);
120 balloon_low++;
121 }
122 }
124 /* balloon_retrieve: rescue a page from the balloon, if it is not empty. */
125 static struct page *balloon_retrieve(void)
126 {
127 struct page *page;
129 if (list_empty(&ballooned_pages))
130 return NULL;
132 page = LIST_TO_PAGE(ballooned_pages.next);
133 UNLIST_PAGE(page);
135 if (PageHighMem(page))
136 balloon_high--;
137 else
138 balloon_low--;
140 return page;
141 }
143 static struct page *balloon_first_page(void)
144 {
145 if (list_empty(&ballooned_pages))
146 return NULL;
147 return LIST_TO_PAGE(ballooned_pages.next);
148 }
150 static struct page *balloon_next_page(struct page *page)
151 {
152 struct list_head *next = PAGE_TO_LIST(page)->next;
153 if (next == &ballooned_pages)
154 return NULL;
155 return LIST_TO_PAGE(next);
156 }
158 static void balloon_alarm(unsigned long unused)
159 {
160 schedule_work(&balloon_worker);
161 }
163 static unsigned long current_target(void)
164 {
165 unsigned long target = min(target_pages, hard_limit);
166 if (target > (current_pages + balloon_low + balloon_high))
167 target = current_pages + balloon_low + balloon_high;
168 return target;
169 }
171 static int increase_reservation(unsigned long nr_pages)
172 {
173 unsigned long *frame_list, pfn, i, flags;
174 struct page *page;
175 long rc;
176 struct xen_memory_reservation reservation = {
177 .address_bits = 0,
178 .extent_order = 0,
179 .domid = DOMID_SELF
180 };
182 if (nr_pages > (PAGE_SIZE / sizeof(unsigned long)))
183 nr_pages = PAGE_SIZE / sizeof(unsigned long);
185 frame_list = (unsigned long *)__get_free_page(GFP_KERNEL);
186 if (frame_list == NULL)
187 return -ENOMEM;
189 balloon_lock(flags);
191 page = balloon_first_page();
192 for (i = 0; i < nr_pages; i++) {
193 BUG_ON(page == NULL);
194 frame_list[i] = page_to_pfn(page);;
195 page = balloon_next_page(page);
196 }
198 SET_XEN_GUEST_HANDLE(reservation.extent_start, frame_list);
199 reservation.nr_extents = nr_pages;
200 rc = HYPERVISOR_memory_op(
201 XENMEM_populate_physmap, &reservation);
202 if (rc < nr_pages) {
203 int ret;
204 /* We hit the Xen hard limit: reprobe. */
205 SET_XEN_GUEST_HANDLE(reservation.extent_start, frame_list);
206 reservation.nr_extents = rc;
207 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
208 &reservation);
209 BUG_ON(ret != rc);
210 hard_limit = current_pages + rc - driver_pages;
211 goto out;
212 }
214 for (i = 0; i < nr_pages; i++) {
215 page = balloon_retrieve();
216 BUG_ON(page == NULL);
218 pfn = page_to_pfn(page);
219 BUG_ON(!xen_feature(XENFEAT_auto_translated_physmap) &&
220 phys_to_machine_mapping_valid(pfn));
222 /* Update P->M and M->P tables. */
223 set_phys_to_machine(pfn, frame_list[i]);
224 xen_machphys_update(frame_list[i], pfn);
226 /* Link back into the page tables if not highmem. */
227 if (pfn < max_low_pfn) {
228 int ret;
229 ret = HYPERVISOR_update_va_mapping(
230 (unsigned long)__va(pfn << PAGE_SHIFT),
231 pfn_pte_ma(frame_list[i], PAGE_KERNEL),
232 0);
233 BUG_ON(ret);
234 }
236 /* Relinquish the page back to the allocator. */
237 ClearPageReserved(page);
238 set_page_count(page, 1);
239 __free_page(page);
240 }
242 current_pages += nr_pages;
243 totalram_pages = current_pages;
245 out:
246 balloon_unlock(flags);
248 free_page((unsigned long)frame_list);
250 return 0;
251 }
253 static int decrease_reservation(unsigned long nr_pages)
254 {
255 unsigned long *frame_list, pfn, i, flags;
256 struct page *page;
257 void *v;
258 int need_sleep = 0;
259 int ret;
260 struct xen_memory_reservation reservation = {
261 .address_bits = 0,
262 .extent_order = 0,
263 .domid = DOMID_SELF
264 };
266 if (nr_pages > (PAGE_SIZE / sizeof(unsigned long)))
267 nr_pages = PAGE_SIZE / sizeof(unsigned long);
269 frame_list = (unsigned long *)__get_free_page(GFP_KERNEL);
270 if (frame_list == NULL)
271 return -ENOMEM;
273 for (i = 0; i < nr_pages; i++) {
274 if ((page = alloc_page(GFP_HIGHUSER)) == NULL) {
275 nr_pages = i;
276 need_sleep = 1;
277 break;
278 }
280 pfn = page_to_pfn(page);
281 frame_list[i] = pfn_to_mfn(pfn);
283 if (!PageHighMem(page)) {
284 v = phys_to_virt(pfn << PAGE_SHIFT);
285 scrub_pages(v, 1);
286 ret = HYPERVISOR_update_va_mapping(
287 (unsigned long)v, __pte_ma(0), 0);
288 BUG_ON(ret);
289 }
290 #ifdef CONFIG_XEN_SCRUB_PAGES
291 else {
292 v = kmap(page);
293 scrub_pages(v, 1);
294 kunmap(page);
295 }
296 #endif
297 }
299 /* Ensure that ballooned highmem pages don't have kmaps. */
300 kmap_flush_unused();
301 flush_tlb_all();
303 balloon_lock(flags);
305 /* No more mappings: invalidate P2M and add to balloon. */
306 for (i = 0; i < nr_pages; i++) {
307 pfn = mfn_to_pfn(frame_list[i]);
308 set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
309 balloon_append(pfn_to_page(pfn));
310 }
312 SET_XEN_GUEST_HANDLE(reservation.extent_start, frame_list);
313 reservation.nr_extents = nr_pages;
314 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
315 BUG_ON(ret != nr_pages);
317 current_pages -= nr_pages;
318 totalram_pages = current_pages;
320 balloon_unlock(flags);
322 free_page((unsigned long)frame_list);
324 return need_sleep;
325 }
327 /*
328 * We avoid multiple worker processes conflicting via the balloon mutex.
329 * We may of course race updates of the target counts (which are protected
330 * by the balloon lock), or with changes to the Xen hard limit, but we will
331 * recover from these in time.
332 */
333 static void balloon_process(void *unused)
334 {
335 int need_sleep = 0;
336 long credit;
338 down(&balloon_mutex);
340 do {
341 credit = current_target() - current_pages;
342 if (credit > 0)
343 need_sleep = (increase_reservation(credit) != 0);
344 if (credit < 0)
345 need_sleep = (decrease_reservation(-credit) != 0);
347 #ifndef CONFIG_PREEMPT
348 if (need_resched())
349 schedule();
350 #endif
351 } while ((credit != 0) && !need_sleep);
353 /* Schedule more work if there is some still to be done. */
354 if (current_target() != current_pages)
355 mod_timer(&balloon_timer, jiffies + HZ);
357 up(&balloon_mutex);
358 }
360 /* Resets the Xen limit, sets new target, and kicks off processing. */
361 static void set_new_target(unsigned long target)
362 {
363 /* No need for lock. Not read-modify-write updates. */
364 hard_limit = ~0UL;
365 target_pages = target;
366 schedule_work(&balloon_worker);
367 }
369 static struct xenbus_watch target_watch =
370 {
371 .node = "memory/target"
372 };
374 /* React to a change in the target key */
375 static void watch_target(struct xenbus_watch *watch,
376 const char **vec, unsigned int len)
377 {
378 unsigned long long new_target;
379 int err;
381 err = xenbus_scanf(XBT_NULL, "memory", "target", "%llu", &new_target);
382 if (err != 1) {
383 /* This is ok (for domain0 at least) - so just return */
384 return;
385 }
387 /* The given memory/target value is in KiB, so it needs converting to
388 pages. PAGE_SHIFT converts bytes to pages, hence PAGE_SHIFT - 10.
389 */
390 set_new_target(new_target >> (PAGE_SHIFT - 10));
392 }
394 static int balloon_init_watcher(struct notifier_block *notifier,
395 unsigned long event,
396 void *data)
397 {
398 int err;
400 err = register_xenbus_watch(&target_watch);
401 if (err)
402 printk(KERN_ERR "Failed to set balloon watcher\n");
404 return NOTIFY_DONE;
406 }
408 static int balloon_write(struct file *file, const char __user *buffer,
409 unsigned long count, void *data)
410 {
411 char memstring[64], *endchar;
412 unsigned long long target_bytes;
414 if (!capable(CAP_SYS_ADMIN))
415 return -EPERM;
417 if (count <= 1)
418 return -EBADMSG; /* runt */
419 if (count > sizeof(memstring))
420 return -EFBIG; /* too long */
422 if (copy_from_user(memstring, buffer, count))
423 return -EFAULT;
424 memstring[sizeof(memstring)-1] = '\0';
426 target_bytes = memparse(memstring, &endchar);
427 set_new_target(target_bytes >> PAGE_SHIFT);
429 return count;
430 }
432 static int balloon_read(char *page, char **start, off_t off,
433 int count, int *eof, void *data)
434 {
435 int len;
437 len = sprintf(
438 page,
439 "Current allocation: %8lu kB\n"
440 "Requested target: %8lu kB\n"
441 "Low-mem balloon: %8lu kB\n"
442 "High-mem balloon: %8lu kB\n"
443 "Xen hard limit: ",
444 PAGES2KB(current_pages), PAGES2KB(target_pages),
445 PAGES2KB(balloon_low), PAGES2KB(balloon_high));
447 if (hard_limit != ~0UL) {
448 len += sprintf(
449 page + len,
450 "%8lu kB (inc. %8lu kB driver headroom)\n",
451 PAGES2KB(hard_limit), PAGES2KB(driver_pages));
452 } else {
453 len += sprintf(
454 page + len,
455 " ??? kB\n");
456 }
458 *eof = 1;
459 return len;
460 }
462 static struct notifier_block xenstore_notifier;
464 static int __init balloon_init(void)
465 {
466 unsigned long pfn;
467 struct page *page;
469 IPRINTK("Initialising balloon driver.\n");
471 if (xen_init() < 0)
472 return -1;
474 current_pages = min(xen_start_info->nr_pages, max_pfn);
475 totalram_pages = current_pages;
476 target_pages = current_pages;
477 balloon_low = 0;
478 balloon_high = 0;
479 driver_pages = 0UL;
480 hard_limit = ~0UL;
482 init_timer(&balloon_timer);
483 balloon_timer.data = 0;
484 balloon_timer.function = balloon_alarm;
486 if ((balloon_pde = create_xen_proc_entry("balloon", 0644)) == NULL) {
487 WPRINTK("Unable to create /proc/xen/balloon.\n");
488 return -1;
489 }
491 balloon_pde->read_proc = balloon_read;
492 balloon_pde->write_proc = balloon_write;
494 /* Initialise the balloon with excess memory space. */
495 for (pfn = xen_start_info->nr_pages; pfn < max_pfn; pfn++) {
496 page = pfn_to_page(pfn);
497 if (!PageReserved(page))
498 balloon_append(page);
499 }
501 target_watch.callback = watch_target;
502 xenstore_notifier.notifier_call = balloon_init_watcher;
504 register_xenstore_notifier(&xenstore_notifier);
506 return 0;
507 }
509 subsys_initcall(balloon_init);
511 void balloon_update_driver_allowance(long delta)
512 {
513 unsigned long flags;
515 balloon_lock(flags);
516 driver_pages += delta;
517 balloon_unlock(flags);
518 }
520 static int dealloc_pte_fn(
521 pte_t *pte, struct page *pmd_page, unsigned long addr, void *data)
522 {
523 unsigned long mfn = pte_mfn(*pte);
524 int ret;
525 struct xen_memory_reservation reservation = {
526 .nr_extents = 1,
527 .extent_order = 0,
528 .domid = DOMID_SELF
529 };
530 SET_XEN_GUEST_HANDLE(reservation.extent_start, &mfn);
531 set_pte_at(&init_mm, addr, pte, __pte_ma(0));
532 set_phys_to_machine(__pa(addr) >> PAGE_SHIFT, INVALID_P2M_ENTRY);
533 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
534 BUG_ON(ret != 1);
535 return 0;
536 }
538 struct page *balloon_alloc_empty_page_range(unsigned long nr_pages)
539 {
540 unsigned long vstart, flags;
541 unsigned int order = get_order(nr_pages * PAGE_SIZE);
542 int ret;
544 vstart = __get_free_pages(GFP_KERNEL, order);
545 if (vstart == 0)
546 return NULL;
548 scrub_pages(vstart, 1 << order);
550 balloon_lock(flags);
551 ret = apply_to_page_range(&init_mm, vstart,
552 PAGE_SIZE << order, dealloc_pte_fn, NULL);
553 BUG_ON(ret);
554 current_pages -= 1UL << order;
555 totalram_pages = current_pages;
556 balloon_unlock(flags);
558 schedule_work(&balloon_worker);
560 flush_tlb_all();
562 return virt_to_page(vstart);
563 }
565 void balloon_dealloc_empty_page_range(
566 struct page *page, unsigned long nr_pages)
567 {
568 unsigned long i, flags;
569 unsigned int order = get_order(nr_pages * PAGE_SIZE);
571 balloon_lock(flags);
572 for (i = 0; i < (1UL << order); i++)
573 balloon_append(page + i);
574 balloon_unlock(flags);
576 schedule_work(&balloon_worker);
577 }
579 EXPORT_SYMBOL_GPL(balloon_update_driver_allowance);
580 EXPORT_SYMBOL_GPL(balloon_alloc_empty_page_range);
581 EXPORT_SYMBOL_GPL(balloon_dealloc_empty_page_range);
583 MODULE_LICENSE("Dual BSD/GPL");
585 /*
586 * Local variables:
587 * c-file-style: "linux"
588 * indent-tabs-mode: t
589 * c-indent-level: 8
590 * c-basic-offset: 8
591 * tab-width: 8
592 * End:
593 */