ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/balloon/balloon.c @ 10590:9b35fada9e65

Use a static page for the frame_list in the balloon driver.
Simplifies code and robustifies against oom situations.

Signed-off-by: Steven Hand <steven@xensource.com>
author shand@kneesaa.uk.xensource.com
date Thu Jun 29 11:17:24 2006 +0100 (2006-06-29)
parents b4e328334392
children 8242c0c24db7
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 #ifdef CONFIG_PROC_FS
62 static struct proc_dir_entry *balloon_pde;
63 #endif
65 static DECLARE_MUTEX(balloon_mutex);
67 /*
68 * Protects atomic reservation decrease/increase against concurrent increases.
69 * Also protects non-atomic updates of current_pages and driver_pages, and
70 * balloon lists.
71 */
72 DEFINE_SPINLOCK(balloon_lock);
74 /* We aim for 'current allocation' == 'target allocation'. */
75 static unsigned long current_pages;
76 static unsigned long target_pages;
78 /* We increase/decrease in batches which fit in a page */
79 static unsigned long frame_list[PAGE_SIZE / sizeof(unsigned long)];
81 /* VM /proc information for memory */
82 extern unsigned long totalram_pages;
84 /* We may hit the hard limit in Xen. If we do then we remember it. */
85 static unsigned long hard_limit;
87 /*
88 * Drivers may alter the memory reservation independently, but they must
89 * inform the balloon driver so that we can avoid hitting the hard limit.
90 */
91 static unsigned long driver_pages;
93 /* List of ballooned pages, threaded through the mem_map array. */
94 static LIST_HEAD(ballooned_pages);
95 static unsigned long balloon_low, balloon_high;
97 /* Main work function, always executed in process context. */
98 static void balloon_process(void *unused);
99 static DECLARE_WORK(balloon_worker, balloon_process, NULL);
100 static struct timer_list balloon_timer;
102 #define PAGE_TO_LIST(p) (&(p)->lru)
103 #define LIST_TO_PAGE(l) list_entry((l), struct page, lru)
104 #define UNLIST_PAGE(p) \
105 do { \
106 list_del(PAGE_TO_LIST(p)); \
107 PAGE_TO_LIST(p)->next = NULL; \
108 PAGE_TO_LIST(p)->prev = NULL; \
109 } while(0)
111 #define IPRINTK(fmt, args...) \
112 printk(KERN_INFO "xen_mem: " fmt, ##args)
113 #define WPRINTK(fmt, args...) \
114 printk(KERN_WARNING "xen_mem: " fmt, ##args)
116 /* balloon_append: add the given page to the balloon. */
117 static void balloon_append(struct page *page)
118 {
119 /* Lowmem is re-populated first, so highmem pages go at list tail. */
120 if (PageHighMem(page)) {
121 list_add_tail(PAGE_TO_LIST(page), &ballooned_pages);
122 balloon_high++;
123 } else {
124 list_add(PAGE_TO_LIST(page), &ballooned_pages);
125 balloon_low++;
126 }
127 }
129 /* balloon_retrieve: rescue a page from the balloon, if it is not empty. */
130 static struct page *balloon_retrieve(void)
131 {
132 struct page *page;
134 if (list_empty(&ballooned_pages))
135 return NULL;
137 page = LIST_TO_PAGE(ballooned_pages.next);
138 UNLIST_PAGE(page);
140 if (PageHighMem(page))
141 balloon_high--;
142 else
143 balloon_low--;
145 return page;
146 }
148 static struct page *balloon_first_page(void)
149 {
150 if (list_empty(&ballooned_pages))
151 return NULL;
152 return LIST_TO_PAGE(ballooned_pages.next);
153 }
155 static struct page *balloon_next_page(struct page *page)
156 {
157 struct list_head *next = PAGE_TO_LIST(page)->next;
158 if (next == &ballooned_pages)
159 return NULL;
160 return LIST_TO_PAGE(next);
161 }
163 static void balloon_alarm(unsigned long unused)
164 {
165 schedule_work(&balloon_worker);
166 }
168 static unsigned long current_target(void)
169 {
170 unsigned long target = min(target_pages, hard_limit);
171 if (target > (current_pages + balloon_low + balloon_high))
172 target = current_pages + balloon_low + balloon_high;
173 return target;
174 }
176 static int increase_reservation(unsigned long nr_pages)
177 {
178 unsigned long pfn, i, flags;
179 struct page *page;
180 long rc;
181 struct xen_memory_reservation reservation = {
182 .address_bits = 0,
183 .extent_order = 0,
184 .domid = DOMID_SELF
185 };
187 if (nr_pages > ARRAY_SIZE(frame_list))
188 nr_pages = ARRAY_SIZE(frame_list);
190 balloon_lock(flags);
192 page = balloon_first_page();
193 for (i = 0; i < nr_pages; i++) {
194 BUG_ON(page == NULL);
195 frame_list[i] = page_to_pfn(page);;
196 page = balloon_next_page(page);
197 }
199 set_xen_guest_handle(reservation.extent_start, frame_list);
200 reservation.nr_extents = nr_pages;
201 rc = HYPERVISOR_memory_op(
202 XENMEM_populate_physmap, &reservation);
203 if (rc < nr_pages) {
204 if (rc > 0) {
205 int ret;
207 /* We hit the Xen hard limit: reprobe. */
208 reservation.nr_extents = rc;
209 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
210 &reservation);
211 BUG_ON(ret != rc);
212 }
213 if (rc >= 0)
214 hard_limit = current_pages + rc - driver_pages;
215 goto out;
216 }
218 for (i = 0; i < nr_pages; i++) {
219 page = balloon_retrieve();
220 BUG_ON(page == NULL);
222 pfn = page_to_pfn(page);
223 BUG_ON(!xen_feature(XENFEAT_auto_translated_physmap) &&
224 phys_to_machine_mapping_valid(pfn));
226 /* Update P->M and M->P tables. */
227 set_phys_to_machine(pfn, frame_list[i]);
228 xen_machphys_update(frame_list[i], pfn);
230 /* Link back into the page tables if not highmem. */
231 if (pfn < max_low_pfn) {
232 int ret;
233 ret = HYPERVISOR_update_va_mapping(
234 (unsigned long)__va(pfn << PAGE_SHIFT),
235 pfn_pte_ma(frame_list[i], PAGE_KERNEL),
236 0);
237 BUG_ON(ret);
238 }
240 /* Relinquish the page back to the allocator. */
241 ClearPageReserved(page);
242 set_page_count(page, 1);
243 __free_page(page);
244 }
246 current_pages += nr_pages;
247 totalram_pages = current_pages;
249 out:
250 balloon_unlock(flags);
252 return 0;
253 }
255 static int decrease_reservation(unsigned long nr_pages)
256 {
257 unsigned long pfn, i, flags;
258 struct page *page;
259 void *v;
260 int need_sleep = 0;
261 int ret;
262 struct xen_memory_reservation reservation = {
263 .address_bits = 0,
264 .extent_order = 0,
265 .domid = DOMID_SELF
266 };
268 if (nr_pages > ARRAY_SIZE(frame_list))
269 nr_pages = ARRAY_SIZE(frame_list);
271 for (i = 0; i < nr_pages; i++) {
272 if ((page = alloc_page(GFP_HIGHUSER)) == NULL) {
273 nr_pages = i;
274 need_sleep = 1;
275 break;
276 }
278 pfn = page_to_pfn(page);
279 frame_list[i] = pfn_to_mfn(pfn);
281 if (!PageHighMem(page)) {
282 v = phys_to_virt(pfn << PAGE_SHIFT);
283 scrub_pages(v, 1);
284 ret = HYPERVISOR_update_va_mapping(
285 (unsigned long)v, __pte_ma(0), 0);
286 BUG_ON(ret);
287 }
288 #ifdef CONFIG_XEN_SCRUB_PAGES
289 else {
290 v = kmap(page);
291 scrub_pages(v, 1);
292 kunmap(page);
293 }
294 #endif
295 }
297 /* Ensure that ballooned highmem pages don't have kmaps. */
298 kmap_flush_unused();
299 flush_tlb_all();
301 balloon_lock(flags);
303 /* No more mappings: invalidate P2M and add to balloon. */
304 for (i = 0; i < nr_pages; i++) {
305 pfn = mfn_to_pfn(frame_list[i]);
306 set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
307 balloon_append(pfn_to_page(pfn));
308 }
310 set_xen_guest_handle(reservation.extent_start, frame_list);
311 reservation.nr_extents = nr_pages;
312 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
313 BUG_ON(ret != nr_pages);
315 current_pages -= nr_pages;
316 totalram_pages = current_pages;
318 balloon_unlock(flags);
320 return need_sleep;
321 }
323 /*
324 * We avoid multiple worker processes conflicting via the balloon mutex.
325 * We may of course race updates of the target counts (which are protected
326 * by the balloon lock), or with changes to the Xen hard limit, but we will
327 * recover from these in time.
328 */
329 static void balloon_process(void *unused)
330 {
331 int need_sleep = 0;
332 long credit;
334 down(&balloon_mutex);
336 do {
337 credit = current_target() - current_pages;
338 if (credit > 0)
339 need_sleep = (increase_reservation(credit) != 0);
340 if (credit < 0)
341 need_sleep = (decrease_reservation(-credit) != 0);
343 #ifndef CONFIG_PREEMPT
344 if (need_resched())
345 schedule();
346 #endif
347 } while ((credit != 0) && !need_sleep);
349 /* Schedule more work if there is some still to be done. */
350 if (current_target() != current_pages)
351 mod_timer(&balloon_timer, jiffies + HZ);
353 up(&balloon_mutex);
354 }
356 /* Resets the Xen limit, sets new target, and kicks off processing. */
357 static void set_new_target(unsigned long target)
358 {
359 /* No need for lock. Not read-modify-write updates. */
360 hard_limit = ~0UL;
361 target_pages = target;
362 schedule_work(&balloon_worker);
363 }
365 static struct xenbus_watch target_watch =
366 {
367 .node = "memory/target"
368 };
370 /* React to a change in the target key */
371 static void watch_target(struct xenbus_watch *watch,
372 const char **vec, unsigned int len)
373 {
374 unsigned long long new_target;
375 int err;
377 err = xenbus_scanf(XBT_NIL, "memory", "target", "%llu", &new_target);
378 if (err != 1) {
379 /* This is ok (for domain0 at least) - so just return */
380 return;
381 }
383 /* The given memory/target value is in KiB, so it needs converting to
384 * pages. PAGE_SHIFT converts bytes to pages, hence PAGE_SHIFT - 10.
385 */
386 set_new_target(new_target >> (PAGE_SHIFT - 10));
387 }
389 static int balloon_init_watcher(struct notifier_block *notifier,
390 unsigned long event,
391 void *data)
392 {
393 int err;
395 err = register_xenbus_watch(&target_watch);
396 if (err)
397 printk(KERN_ERR "Failed to set balloon watcher\n");
399 return NOTIFY_DONE;
400 }
402 #ifdef CONFIG_PROC_FS
403 static int balloon_write(struct file *file, const char __user *buffer,
404 unsigned long count, void *data)
405 {
406 char memstring[64], *endchar;
407 unsigned long long target_bytes;
409 if (!capable(CAP_SYS_ADMIN))
410 return -EPERM;
412 if (count <= 1)
413 return -EBADMSG; /* runt */
414 if (count > sizeof(memstring))
415 return -EFBIG; /* too long */
417 if (copy_from_user(memstring, buffer, count))
418 return -EFAULT;
419 memstring[sizeof(memstring)-1] = '\0';
421 target_bytes = memparse(memstring, &endchar);
422 set_new_target(target_bytes >> PAGE_SHIFT);
424 return count;
425 }
427 static int balloon_read(char *page, char **start, off_t off,
428 int count, int *eof, void *data)
429 {
430 int len;
432 len = sprintf(
433 page,
434 "Current allocation: %8lu kB\n"
435 "Requested target: %8lu kB\n"
436 "Low-mem balloon: %8lu kB\n"
437 "High-mem balloon: %8lu kB\n"
438 "Xen hard limit: ",
439 PAGES2KB(current_pages), PAGES2KB(target_pages),
440 PAGES2KB(balloon_low), PAGES2KB(balloon_high));
442 if (hard_limit != ~0UL) {
443 len += sprintf(
444 page + len,
445 "%8lu kB (inc. %8lu kB driver headroom)\n",
446 PAGES2KB(hard_limit), PAGES2KB(driver_pages));
447 } else {
448 len += sprintf(
449 page + len,
450 " ??? kB\n");
451 }
453 *eof = 1;
454 return len;
455 }
456 #endif
458 static struct notifier_block xenstore_notifier;
460 static int __init balloon_init(void)
461 {
462 unsigned long pfn;
463 struct page *page;
465 if (!is_running_on_xen())
466 return -ENODEV;
468 IPRINTK("Initialising balloon driver.\n");
470 current_pages = min(xen_start_info->nr_pages, max_pfn);
471 totalram_pages = current_pages;
472 target_pages = current_pages;
473 balloon_low = 0;
474 balloon_high = 0;
475 driver_pages = 0UL;
476 hard_limit = ~0UL;
478 init_timer(&balloon_timer);
479 balloon_timer.data = 0;
480 balloon_timer.function = balloon_alarm;
482 #ifdef CONFIG_PROC_FS
483 if ((balloon_pde = create_xen_proc_entry("balloon", 0644)) == NULL) {
484 WPRINTK("Unable to create /proc/xen/balloon.\n");
485 return -1;
486 }
488 balloon_pde->read_proc = balloon_read;
489 balloon_pde->write_proc = balloon_write;
490 #endif
492 /* Initialise the balloon with excess memory space. */
493 for (pfn = xen_start_info->nr_pages; pfn < max_pfn; pfn++) {
494 page = pfn_to_page(pfn);
495 if (!PageReserved(page))
496 balloon_append(page);
497 }
499 target_watch.callback = watch_target;
500 xenstore_notifier.notifier_call = balloon_init_watcher;
502 register_xenstore_notifier(&xenstore_notifier);
504 return 0;
505 }
507 subsys_initcall(balloon_init);
509 void balloon_update_driver_allowance(long delta)
510 {
511 unsigned long flags;
513 balloon_lock(flags);
514 driver_pages += delta;
515 balloon_unlock(flags);
516 }
518 static int dealloc_pte_fn(
519 pte_t *pte, struct page *pmd_page, unsigned long addr, void *data)
520 {
521 unsigned long mfn = pte_mfn(*pte);
522 int ret;
523 struct xen_memory_reservation reservation = {
524 .nr_extents = 1,
525 .extent_order = 0,
526 .domid = DOMID_SELF
527 };
528 set_xen_guest_handle(reservation.extent_start, &mfn);
529 set_pte_at(&init_mm, addr, pte, __pte_ma(0));
530 set_phys_to_machine(__pa(addr) >> PAGE_SHIFT, INVALID_P2M_ENTRY);
531 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
532 BUG_ON(ret != 1);
533 return 0;
534 }
536 struct page *balloon_alloc_empty_page_range(unsigned long nr_pages)
537 {
538 unsigned long vstart, flags;
539 unsigned int order = get_order(nr_pages * PAGE_SIZE);
540 int ret;
541 unsigned long i;
542 struct page *page;
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 if (xen_feature(XENFEAT_auto_translated_physmap)) {
552 unsigned long gmfn = __pa(vstart) >> PAGE_SHIFT;
553 struct xen_memory_reservation reservation = {
554 .nr_extents = 1,
555 .extent_order = order,
556 .domid = DOMID_SELF
557 };
558 set_xen_guest_handle(reservation.extent_start, &gmfn);
559 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
560 &reservation);
561 BUG_ON(ret != 1);
562 } else {
563 ret = apply_to_page_range(&init_mm, vstart, PAGE_SIZE << order,
564 dealloc_pte_fn, NULL);
565 BUG_ON(ret);
566 }
567 current_pages -= 1UL << order;
568 totalram_pages = current_pages;
569 balloon_unlock(flags);
571 schedule_work(&balloon_worker);
573 flush_tlb_all();
575 page = virt_to_page(vstart);
577 for (i = 0; i < (1UL << order); i++)
578 set_page_count(page + i, 1);
580 return page;
581 }
583 void balloon_dealloc_empty_page_range(
584 struct page *page, unsigned long nr_pages)
585 {
586 unsigned long i, flags;
587 unsigned int order = get_order(nr_pages * PAGE_SIZE);
589 balloon_lock(flags);
590 for (i = 0; i < (1UL << order); i++) {
591 BUG_ON(page_count(page + i) != 1);
592 balloon_append(page + i);
593 }
594 balloon_unlock(flags);
596 schedule_work(&balloon_worker);
597 }
599 EXPORT_SYMBOL_GPL(balloon_update_driver_allowance);
600 EXPORT_SYMBOL_GPL(balloon_alloc_empty_page_range);
601 EXPORT_SYMBOL_GPL(balloon_dealloc_empty_page_range);
603 MODULE_LICENSE("Dual BSD/GPL");