diff options
Diffstat (limited to 'drivers/xen/balloon.c')
-rw-r--r-- | drivers/xen/balloon.c | 420 |
1 files changed, 161 insertions, 259 deletions
diff --git a/drivers/xen/balloon.c b/drivers/xen/balloon.c index 500290b150bb..f54290baa3db 100644 --- a/drivers/xen/balloon.c +++ b/drivers/xen/balloon.c | |||
@@ -1,6 +1,4 @@ | |||
1 | /****************************************************************************** | 1 | /****************************************************************************** |
2 | * balloon.c | ||
3 | * | ||
4 | * Xen balloon driver - enables returning/claiming memory to/from Xen. | 2 | * Xen balloon driver - enables returning/claiming memory to/from Xen. |
5 | * | 3 | * |
6 | * Copyright (c) 2003, B Dragovic | 4 | * Copyright (c) 2003, B Dragovic |
@@ -33,7 +31,6 @@ | |||
33 | */ | 31 | */ |
34 | 32 | ||
35 | #include <linux/kernel.h> | 33 | #include <linux/kernel.h> |
36 | #include <linux/module.h> | ||
37 | #include <linux/sched.h> | 34 | #include <linux/sched.h> |
38 | #include <linux/errno.h> | 35 | #include <linux/errno.h> |
39 | #include <linux/mm.h> | 36 | #include <linux/mm.h> |
@@ -42,14 +39,13 @@ | |||
42 | #include <linux/highmem.h> | 39 | #include <linux/highmem.h> |
43 | #include <linux/mutex.h> | 40 | #include <linux/mutex.h> |
44 | #include <linux/list.h> | 41 | #include <linux/list.h> |
45 | #include <linux/sysdev.h> | ||
46 | #include <linux/gfp.h> | 42 | #include <linux/gfp.h> |
47 | 43 | ||
48 | #include <asm/page.h> | 44 | #include <asm/page.h> |
49 | #include <asm/pgalloc.h> | 45 | #include <asm/pgalloc.h> |
50 | #include <asm/pgtable.h> | 46 | #include <asm/pgtable.h> |
51 | #include <asm/uaccess.h> | ||
52 | #include <asm/tlb.h> | 47 | #include <asm/tlb.h> |
48 | #include <asm/e820.h> | ||
53 | 49 | ||
54 | #include <asm/xen/hypervisor.h> | 50 | #include <asm/xen/hypervisor.h> |
55 | #include <asm/xen/hypercall.h> | 51 | #include <asm/xen/hypercall.h> |
@@ -57,35 +53,29 @@ | |||
57 | #include <xen/xen.h> | 53 | #include <xen/xen.h> |
58 | #include <xen/interface/xen.h> | 54 | #include <xen/interface/xen.h> |
59 | #include <xen/interface/memory.h> | 55 | #include <xen/interface/memory.h> |
60 | #include <xen/xenbus.h> | 56 | #include <xen/balloon.h> |
61 | #include <xen/features.h> | 57 | #include <xen/features.h> |
62 | #include <xen/page.h> | 58 | #include <xen/page.h> |
63 | 59 | ||
64 | #define PAGES2KB(_p) ((_p)<<(PAGE_SHIFT-10)) | 60 | /* |
65 | 61 | * balloon_process() state: | |
66 | #define BALLOON_CLASS_NAME "xen_memory" | 62 | * |
63 | * BP_DONE: done or nothing to do, | ||
64 | * BP_EAGAIN: error, go to sleep, | ||
65 | * BP_ECANCELED: error, balloon operation canceled. | ||
66 | */ | ||
67 | 67 | ||
68 | struct balloon_stats { | 68 | enum bp_state { |
69 | /* We aim for 'current allocation' == 'target allocation'. */ | 69 | BP_DONE, |
70 | unsigned long current_pages; | 70 | BP_EAGAIN, |
71 | unsigned long target_pages; | 71 | BP_ECANCELED |
72 | /* | ||
73 | * Drivers may alter the memory reservation independently, but they | ||
74 | * must inform the balloon driver so we avoid hitting the hard limit. | ||
75 | */ | ||
76 | unsigned long driver_pages; | ||
77 | /* Number of pages in high- and low-memory balloons. */ | ||
78 | unsigned long balloon_low; | ||
79 | unsigned long balloon_high; | ||
80 | }; | 72 | }; |
81 | 73 | ||
82 | static DEFINE_MUTEX(balloon_mutex); | ||
83 | |||
84 | static struct sys_device balloon_sysdev; | ||
85 | 74 | ||
86 | static int register_balloon(struct sys_device *sysdev); | 75 | static DEFINE_MUTEX(balloon_mutex); |
87 | 76 | ||
88 | static struct balloon_stats balloon_stats; | 77 | struct balloon_stats balloon_stats; |
78 | EXPORT_SYMBOL_GPL(balloon_stats); | ||
89 | 79 | ||
90 | /* We increase/decrease in batches which fit in a page */ | 80 | /* We increase/decrease in batches which fit in a page */ |
91 | static unsigned long frame_list[PAGE_SIZE / sizeof(unsigned long)]; | 81 | static unsigned long frame_list[PAGE_SIZE / sizeof(unsigned long)]; |
@@ -103,8 +93,7 @@ static LIST_HEAD(ballooned_pages); | |||
103 | 93 | ||
104 | /* Main work function, always executed in process context. */ | 94 | /* Main work function, always executed in process context. */ |
105 | static void balloon_process(struct work_struct *work); | 95 | static void balloon_process(struct work_struct *work); |
106 | static DECLARE_WORK(balloon_worker, balloon_process); | 96 | static DECLARE_DELAYED_WORK(balloon_worker, balloon_process); |
107 | static struct timer_list balloon_timer; | ||
108 | 97 | ||
109 | /* When ballooning out (allocating memory to return to Xen) we don't really | 98 | /* When ballooning out (allocating memory to return to Xen) we don't really |
110 | want the kernel to try too hard since that can trigger the oom killer. */ | 99 | want the kernel to try too hard since that can trigger the oom killer. */ |
@@ -119,30 +108,38 @@ static void scrub_page(struct page *page) | |||
119 | } | 108 | } |
120 | 109 | ||
121 | /* balloon_append: add the given page to the balloon. */ | 110 | /* balloon_append: add the given page to the balloon. */ |
122 | static void balloon_append(struct page *page) | 111 | static void __balloon_append(struct page *page) |
123 | { | 112 | { |
124 | /* Lowmem is re-populated first, so highmem pages go at list tail. */ | 113 | /* Lowmem is re-populated first, so highmem pages go at list tail. */ |
125 | if (PageHighMem(page)) { | 114 | if (PageHighMem(page)) { |
126 | list_add_tail(&page->lru, &ballooned_pages); | 115 | list_add_tail(&page->lru, &ballooned_pages); |
127 | balloon_stats.balloon_high++; | 116 | balloon_stats.balloon_high++; |
128 | dec_totalhigh_pages(); | ||
129 | } else { | 117 | } else { |
130 | list_add(&page->lru, &ballooned_pages); | 118 | list_add(&page->lru, &ballooned_pages); |
131 | balloon_stats.balloon_low++; | 119 | balloon_stats.balloon_low++; |
132 | } | 120 | } |
121 | } | ||
133 | 122 | ||
123 | static void balloon_append(struct page *page) | ||
124 | { | ||
125 | __balloon_append(page); | ||
126 | if (PageHighMem(page)) | ||
127 | dec_totalhigh_pages(); | ||
134 | totalram_pages--; | 128 | totalram_pages--; |
135 | } | 129 | } |
136 | 130 | ||
137 | /* balloon_retrieve: rescue a page from the balloon, if it is not empty. */ | 131 | /* balloon_retrieve: rescue a page from the balloon, if it is not empty. */ |
138 | static struct page *balloon_retrieve(void) | 132 | static struct page *balloon_retrieve(bool prefer_highmem) |
139 | { | 133 | { |
140 | struct page *page; | 134 | struct page *page; |
141 | 135 | ||
142 | if (list_empty(&ballooned_pages)) | 136 | if (list_empty(&ballooned_pages)) |
143 | return NULL; | 137 | return NULL; |
144 | 138 | ||
145 | page = list_entry(ballooned_pages.next, struct page, lru); | 139 | if (prefer_highmem) |
140 | page = list_entry(ballooned_pages.prev, struct page, lru); | ||
141 | else | ||
142 | page = list_entry(ballooned_pages.next, struct page, lru); | ||
146 | list_del(&page->lru); | 143 | list_del(&page->lru); |
147 | 144 | ||
148 | if (PageHighMem(page)) { | 145 | if (PageHighMem(page)) { |
@@ -172,12 +169,32 @@ static struct page *balloon_next_page(struct page *page) | |||
172 | return list_entry(next, struct page, lru); | 169 | return list_entry(next, struct page, lru); |
173 | } | 170 | } |
174 | 171 | ||
175 | static void balloon_alarm(unsigned long unused) | 172 | static enum bp_state update_schedule(enum bp_state state) |
176 | { | 173 | { |
177 | schedule_work(&balloon_worker); | 174 | if (state == BP_DONE) { |
175 | balloon_stats.schedule_delay = 1; | ||
176 | balloon_stats.retry_count = 1; | ||
177 | return BP_DONE; | ||
178 | } | ||
179 | |||
180 | ++balloon_stats.retry_count; | ||
181 | |||
182 | if (balloon_stats.max_retry_count != RETRY_UNLIMITED && | ||
183 | balloon_stats.retry_count > balloon_stats.max_retry_count) { | ||
184 | balloon_stats.schedule_delay = 1; | ||
185 | balloon_stats.retry_count = 1; | ||
186 | return BP_ECANCELED; | ||
187 | } | ||
188 | |||
189 | balloon_stats.schedule_delay <<= 1; | ||
190 | |||
191 | if (balloon_stats.schedule_delay > balloon_stats.max_schedule_delay) | ||
192 | balloon_stats.schedule_delay = balloon_stats.max_schedule_delay; | ||
193 | |||
194 | return BP_EAGAIN; | ||
178 | } | 195 | } |
179 | 196 | ||
180 | static unsigned long current_target(void) | 197 | static long current_credit(void) |
181 | { | 198 | { |
182 | unsigned long target = balloon_stats.target_pages; | 199 | unsigned long target = balloon_stats.target_pages; |
183 | 200 | ||
@@ -186,14 +203,14 @@ static unsigned long current_target(void) | |||
186 | balloon_stats.balloon_low + | 203 | balloon_stats.balloon_low + |
187 | balloon_stats.balloon_high); | 204 | balloon_stats.balloon_high); |
188 | 205 | ||
189 | return target; | 206 | return target - balloon_stats.current_pages; |
190 | } | 207 | } |
191 | 208 | ||
192 | static int increase_reservation(unsigned long nr_pages) | 209 | static enum bp_state increase_reservation(unsigned long nr_pages) |
193 | { | 210 | { |
194 | unsigned long pfn, i, flags; | 211 | int rc; |
212 | unsigned long pfn, i; | ||
195 | struct page *page; | 213 | struct page *page; |
196 | long rc; | ||
197 | struct xen_memory_reservation reservation = { | 214 | struct xen_memory_reservation reservation = { |
198 | .address_bits = 0, | 215 | .address_bits = 0, |
199 | .extent_order = 0, | 216 | .extent_order = 0, |
@@ -203,11 +220,12 @@ static int increase_reservation(unsigned long nr_pages) | |||
203 | if (nr_pages > ARRAY_SIZE(frame_list)) | 220 | if (nr_pages > ARRAY_SIZE(frame_list)) |
204 | nr_pages = ARRAY_SIZE(frame_list); | 221 | nr_pages = ARRAY_SIZE(frame_list); |
205 | 222 | ||
206 | spin_lock_irqsave(&xen_reservation_lock, flags); | ||
207 | |||
208 | page = balloon_first_page(); | 223 | page = balloon_first_page(); |
209 | for (i = 0; i < nr_pages; i++) { | 224 | for (i = 0; i < nr_pages; i++) { |
210 | BUG_ON(page == NULL); | 225 | if (!page) { |
226 | nr_pages = i; | ||
227 | break; | ||
228 | } | ||
211 | frame_list[i] = page_to_pfn(page); | 229 | frame_list[i] = page_to_pfn(page); |
212 | page = balloon_next_page(page); | 230 | page = balloon_next_page(page); |
213 | } | 231 | } |
@@ -215,11 +233,11 @@ static int increase_reservation(unsigned long nr_pages) | |||
215 | set_xen_guest_handle(reservation.extent_start, frame_list); | 233 | set_xen_guest_handle(reservation.extent_start, frame_list); |
216 | reservation.nr_extents = nr_pages; | 234 | reservation.nr_extents = nr_pages; |
217 | rc = HYPERVISOR_memory_op(XENMEM_populate_physmap, &reservation); | 235 | rc = HYPERVISOR_memory_op(XENMEM_populate_physmap, &reservation); |
218 | if (rc < 0) | 236 | if (rc <= 0) |
219 | goto out; | 237 | return BP_EAGAIN; |
220 | 238 | ||
221 | for (i = 0; i < rc; i++) { | 239 | for (i = 0; i < rc; i++) { |
222 | page = balloon_retrieve(); | 240 | page = balloon_retrieve(false); |
223 | BUG_ON(page == NULL); | 241 | BUG_ON(page == NULL); |
224 | 242 | ||
225 | pfn = page_to_pfn(page); | 243 | pfn = page_to_pfn(page); |
@@ -229,7 +247,7 @@ static int increase_reservation(unsigned long nr_pages) | |||
229 | set_phys_to_machine(pfn, frame_list[i]); | 247 | set_phys_to_machine(pfn, frame_list[i]); |
230 | 248 | ||
231 | /* Link back into the page tables if not highmem. */ | 249 | /* Link back into the page tables if not highmem. */ |
232 | if (pfn < max_low_pfn) { | 250 | if (xen_pv_domain() && !PageHighMem(page)) { |
233 | int ret; | 251 | int ret; |
234 | ret = HYPERVISOR_update_va_mapping( | 252 | ret = HYPERVISOR_update_va_mapping( |
235 | (unsigned long)__va(pfn << PAGE_SHIFT), | 253 | (unsigned long)__va(pfn << PAGE_SHIFT), |
@@ -246,17 +264,14 @@ static int increase_reservation(unsigned long nr_pages) | |||
246 | 264 | ||
247 | balloon_stats.current_pages += rc; | 265 | balloon_stats.current_pages += rc; |
248 | 266 | ||
249 | out: | 267 | return BP_DONE; |
250 | spin_unlock_irqrestore(&xen_reservation_lock, flags); | ||
251 | |||
252 | return rc < 0 ? rc : rc != nr_pages; | ||
253 | } | 268 | } |
254 | 269 | ||
255 | static int decrease_reservation(unsigned long nr_pages) | 270 | static enum bp_state decrease_reservation(unsigned long nr_pages, gfp_t gfp) |
256 | { | 271 | { |
257 | unsigned long pfn, i, flags; | 272 | enum bp_state state = BP_DONE; |
273 | unsigned long pfn, i; | ||
258 | struct page *page; | 274 | struct page *page; |
259 | int need_sleep = 0; | ||
260 | int ret; | 275 | int ret; |
261 | struct xen_memory_reservation reservation = { | 276 | struct xen_memory_reservation reservation = { |
262 | .address_bits = 0, | 277 | .address_bits = 0, |
@@ -268,9 +283,9 @@ static int decrease_reservation(unsigned long nr_pages) | |||
268 | nr_pages = ARRAY_SIZE(frame_list); | 283 | nr_pages = ARRAY_SIZE(frame_list); |
269 | 284 | ||
270 | for (i = 0; i < nr_pages; i++) { | 285 | for (i = 0; i < nr_pages; i++) { |
271 | if ((page = alloc_page(GFP_BALLOON)) == NULL) { | 286 | if ((page = alloc_page(gfp)) == NULL) { |
272 | nr_pages = i; | 287 | nr_pages = i; |
273 | need_sleep = 1; | 288 | state = BP_EAGAIN; |
274 | break; | 289 | break; |
275 | } | 290 | } |
276 | 291 | ||
@@ -279,7 +294,7 @@ static int decrease_reservation(unsigned long nr_pages) | |||
279 | 294 | ||
280 | scrub_page(page); | 295 | scrub_page(page); |
281 | 296 | ||
282 | if (!PageHighMem(page)) { | 297 | if (xen_pv_domain() && !PageHighMem(page)) { |
283 | ret = HYPERVISOR_update_va_mapping( | 298 | ret = HYPERVISOR_update_va_mapping( |
284 | (unsigned long)__va(pfn << PAGE_SHIFT), | 299 | (unsigned long)__va(pfn << PAGE_SHIFT), |
285 | __pte_ma(0), 0); | 300 | __pte_ma(0), 0); |
@@ -292,12 +307,10 @@ static int decrease_reservation(unsigned long nr_pages) | |||
292 | kmap_flush_unused(); | 307 | kmap_flush_unused(); |
293 | flush_tlb_all(); | 308 | flush_tlb_all(); |
294 | 309 | ||
295 | spin_lock_irqsave(&xen_reservation_lock, flags); | ||
296 | |||
297 | /* No more mappings: invalidate P2M and add to balloon. */ | 310 | /* No more mappings: invalidate P2M and add to balloon. */ |
298 | for (i = 0; i < nr_pages; i++) { | 311 | for (i = 0; i < nr_pages; i++) { |
299 | pfn = mfn_to_pfn(frame_list[i]); | 312 | pfn = mfn_to_pfn(frame_list[i]); |
300 | set_phys_to_machine(pfn, INVALID_P2M_ENTRY); | 313 | __set_phys_to_machine(pfn, INVALID_P2M_ENTRY); |
301 | balloon_append(pfn_to_page(pfn)); | 314 | balloon_append(pfn_to_page(pfn)); |
302 | } | 315 | } |
303 | 316 | ||
@@ -308,9 +321,7 @@ static int decrease_reservation(unsigned long nr_pages) | |||
308 | 321 | ||
309 | balloon_stats.current_pages -= nr_pages; | 322 | balloon_stats.current_pages -= nr_pages; |
310 | 323 | ||
311 | spin_unlock_irqrestore(&xen_reservation_lock, flags); | 324 | return state; |
312 | |||
313 | return need_sleep; | ||
314 | } | 325 | } |
315 | 326 | ||
316 | /* | 327 | /* |
@@ -321,254 +332,145 @@ static int decrease_reservation(unsigned long nr_pages) | |||
321 | */ | 332 | */ |
322 | static void balloon_process(struct work_struct *work) | 333 | static void balloon_process(struct work_struct *work) |
323 | { | 334 | { |
324 | int need_sleep = 0; | 335 | enum bp_state state = BP_DONE; |
325 | long credit; | 336 | long credit; |
326 | 337 | ||
327 | mutex_lock(&balloon_mutex); | 338 | mutex_lock(&balloon_mutex); |
328 | 339 | ||
329 | do { | 340 | do { |
330 | credit = current_target() - balloon_stats.current_pages; | 341 | credit = current_credit(); |
342 | |||
331 | if (credit > 0) | 343 | if (credit > 0) |
332 | need_sleep = (increase_reservation(credit) != 0); | 344 | state = increase_reservation(credit); |
345 | |||
333 | if (credit < 0) | 346 | if (credit < 0) |
334 | need_sleep = (decrease_reservation(-credit) != 0); | 347 | state = decrease_reservation(-credit, GFP_BALLOON); |
348 | |||
349 | state = update_schedule(state); | ||
335 | 350 | ||
336 | #ifndef CONFIG_PREEMPT | 351 | #ifndef CONFIG_PREEMPT |
337 | if (need_resched()) | 352 | if (need_resched()) |
338 | schedule(); | 353 | schedule(); |
339 | #endif | 354 | #endif |
340 | } while ((credit != 0) && !need_sleep); | 355 | } while (credit && state == BP_DONE); |
341 | 356 | ||
342 | /* Schedule more work if there is some still to be done. */ | 357 | /* Schedule more work if there is some still to be done. */ |
343 | if (current_target() != balloon_stats.current_pages) | 358 | if (state == BP_EAGAIN) |
344 | mod_timer(&balloon_timer, jiffies + HZ); | 359 | schedule_delayed_work(&balloon_worker, balloon_stats.schedule_delay * HZ); |
345 | 360 | ||
346 | mutex_unlock(&balloon_mutex); | 361 | mutex_unlock(&balloon_mutex); |
347 | } | 362 | } |
348 | 363 | ||
349 | /* Resets the Xen limit, sets new target, and kicks off processing. */ | 364 | /* Resets the Xen limit, sets new target, and kicks off processing. */ |
350 | static void balloon_set_new_target(unsigned long target) | 365 | void balloon_set_new_target(unsigned long target) |
351 | { | 366 | { |
352 | /* No need for lock. Not read-modify-write updates. */ | 367 | /* No need for lock. Not read-modify-write updates. */ |
353 | balloon_stats.target_pages = target; | 368 | balloon_stats.target_pages = target; |
354 | schedule_work(&balloon_worker); | 369 | schedule_delayed_work(&balloon_worker, 0); |
355 | } | 370 | } |
371 | EXPORT_SYMBOL_GPL(balloon_set_new_target); | ||
356 | 372 | ||
357 | static struct xenbus_watch target_watch = | 373 | /** |
358 | { | 374 | * alloc_xenballooned_pages - get pages that have been ballooned out |
359 | .node = "memory/target" | 375 | * @nr_pages: Number of pages to get |
360 | }; | 376 | * @pages: pages returned |
361 | 377 | * @return 0 on success, error otherwise | |
362 | /* React to a change in the target key */ | 378 | */ |
363 | static void watch_target(struct xenbus_watch *watch, | 379 | int alloc_xenballooned_pages(int nr_pages, struct page** pages) |
364 | const char **vec, unsigned int len) | ||
365 | { | 380 | { |
366 | unsigned long long new_target; | 381 | int pgno = 0; |
367 | int err; | 382 | struct page* page; |
368 | 383 | mutex_lock(&balloon_mutex); | |
369 | err = xenbus_scanf(XBT_NIL, "memory", "target", "%llu", &new_target); | 384 | while (pgno < nr_pages) { |
370 | if (err != 1) { | 385 | page = balloon_retrieve(true); |
371 | /* This is ok (for domain0 at least) - so just return */ | 386 | if (page) { |
372 | return; | 387 | pages[pgno++] = page; |
388 | } else { | ||
389 | enum bp_state st; | ||
390 | st = decrease_reservation(nr_pages - pgno, GFP_HIGHUSER); | ||
391 | if (st != BP_DONE) | ||
392 | goto out_undo; | ||
393 | } | ||
373 | } | 394 | } |
374 | 395 | mutex_unlock(&balloon_mutex); | |
375 | /* The given memory/target value is in KiB, so it needs converting to | 396 | return 0; |
376 | * pages. PAGE_SHIFT converts bytes to pages, hence PAGE_SHIFT - 10. | 397 | out_undo: |
377 | */ | 398 | while (pgno) |
378 | balloon_set_new_target(new_target >> (PAGE_SHIFT - 10)); | 399 | balloon_append(pages[--pgno]); |
400 | /* Free the memory back to the kernel soon */ | ||
401 | schedule_delayed_work(&balloon_worker, 0); | ||
402 | mutex_unlock(&balloon_mutex); | ||
403 | return -ENOMEM; | ||
379 | } | 404 | } |
405 | EXPORT_SYMBOL(alloc_xenballooned_pages); | ||
380 | 406 | ||
381 | static int balloon_init_watcher(struct notifier_block *notifier, | 407 | /** |
382 | unsigned long event, | 408 | * free_xenballooned_pages - return pages retrieved with get_ballooned_pages |
383 | void *data) | 409 | * @nr_pages: Number of pages |
410 | * @pages: pages to return | ||
411 | */ | ||
412 | void free_xenballooned_pages(int nr_pages, struct page** pages) | ||
384 | { | 413 | { |
385 | int err; | 414 | int i; |
415 | |||
416 | mutex_lock(&balloon_mutex); | ||
386 | 417 | ||
387 | err = register_xenbus_watch(&target_watch); | 418 | for (i = 0; i < nr_pages; i++) { |
388 | if (err) | 419 | if (pages[i]) |
389 | printk(KERN_ERR "Failed to set balloon watcher\n"); | 420 | balloon_append(pages[i]); |
421 | } | ||
390 | 422 | ||
391 | return NOTIFY_DONE; | 423 | /* The balloon may be too large now. Shrink it if needed. */ |
392 | } | 424 | if (current_credit()) |
425 | schedule_delayed_work(&balloon_worker, 0); | ||
393 | 426 | ||
394 | static struct notifier_block xenstore_notifier; | 427 | mutex_unlock(&balloon_mutex); |
428 | } | ||
429 | EXPORT_SYMBOL(free_xenballooned_pages); | ||
395 | 430 | ||
396 | static int __init balloon_init(void) | 431 | static int __init balloon_init(void) |
397 | { | 432 | { |
398 | unsigned long pfn; | 433 | unsigned long pfn, extra_pfn_end; |
399 | struct page *page; | 434 | struct page *page; |
400 | 435 | ||
401 | if (!xen_pv_domain()) | 436 | if (!xen_domain()) |
402 | return -ENODEV; | 437 | return -ENODEV; |
403 | 438 | ||
404 | pr_info("xen_balloon: Initialising balloon driver.\n"); | 439 | pr_info("xen/balloon: Initialising balloon driver.\n"); |
405 | 440 | ||
406 | balloon_stats.current_pages = min(xen_start_info->nr_pages, max_pfn); | 441 | balloon_stats.current_pages = xen_pv_domain() ? min(xen_start_info->nr_pages, max_pfn) : max_pfn; |
407 | balloon_stats.target_pages = balloon_stats.current_pages; | 442 | balloon_stats.target_pages = balloon_stats.current_pages; |
408 | balloon_stats.balloon_low = 0; | 443 | balloon_stats.balloon_low = 0; |
409 | balloon_stats.balloon_high = 0; | 444 | balloon_stats.balloon_high = 0; |
410 | balloon_stats.driver_pages = 0UL; | ||
411 | |||
412 | init_timer(&balloon_timer); | ||
413 | balloon_timer.data = 0; | ||
414 | balloon_timer.function = balloon_alarm; | ||
415 | 445 | ||
416 | register_balloon(&balloon_sysdev); | 446 | balloon_stats.schedule_delay = 1; |
447 | balloon_stats.max_schedule_delay = 32; | ||
448 | balloon_stats.retry_count = 1; | ||
449 | balloon_stats.max_retry_count = RETRY_UNLIMITED; | ||
417 | 450 | ||
418 | /* Initialise the balloon with excess memory space. */ | 451 | /* |
419 | for (pfn = xen_start_info->nr_pages; pfn < max_pfn; pfn++) { | 452 | * Initialise the balloon with excess memory space. We need |
453 | * to make sure we don't add memory which doesn't exist or | ||
454 | * logically exist. The E820 map can be trimmed to be smaller | ||
455 | * than the amount of physical memory due to the mem= command | ||
456 | * line parameter. And if this is a 32-bit non-HIGHMEM kernel | ||
457 | * on a system with memory which requires highmem to access, | ||
458 | * don't try to use it. | ||
459 | */ | ||
460 | extra_pfn_end = min(min(max_pfn, e820_end_of_ram_pfn()), | ||
461 | (unsigned long)PFN_DOWN(xen_extra_mem_start + xen_extra_mem_size)); | ||
462 | for (pfn = PFN_UP(xen_extra_mem_start); | ||
463 | pfn < extra_pfn_end; | ||
464 | pfn++) { | ||
420 | page = pfn_to_page(pfn); | 465 | page = pfn_to_page(pfn); |
421 | if (!PageReserved(page)) | 466 | /* totalram_pages and totalhigh_pages do not include the boot-time |
422 | balloon_append(page); | 467 | balloon extension, so don't subtract from it. */ |
468 | __balloon_append(page); | ||
423 | } | 469 | } |
424 | 470 | ||
425 | target_watch.callback = watch_target; | ||
426 | xenstore_notifier.notifier_call = balloon_init_watcher; | ||
427 | |||
428 | register_xenstore_notifier(&xenstore_notifier); | ||
429 | |||
430 | return 0; | 471 | return 0; |
431 | } | 472 | } |
432 | 473 | ||
433 | subsys_initcall(balloon_init); | 474 | subsys_initcall(balloon_init); |
434 | 475 | ||
435 | static void balloon_exit(void) | ||
436 | { | ||
437 | /* XXX - release balloon here */ | ||
438 | return; | ||
439 | } | ||
440 | |||
441 | module_exit(balloon_exit); | ||
442 | |||
443 | #define BALLOON_SHOW(name, format, args...) \ | ||
444 | static ssize_t show_##name(struct sys_device *dev, \ | ||
445 | struct sysdev_attribute *attr, \ | ||
446 | char *buf) \ | ||
447 | { \ | ||
448 | return sprintf(buf, format, ##args); \ | ||
449 | } \ | ||
450 | static SYSDEV_ATTR(name, S_IRUGO, show_##name, NULL) | ||
451 | |||
452 | BALLOON_SHOW(current_kb, "%lu\n", PAGES2KB(balloon_stats.current_pages)); | ||
453 | BALLOON_SHOW(low_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_low)); | ||
454 | BALLOON_SHOW(high_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_high)); | ||
455 | BALLOON_SHOW(driver_kb, "%lu\n", PAGES2KB(balloon_stats.driver_pages)); | ||
456 | |||
457 | static ssize_t show_target_kb(struct sys_device *dev, struct sysdev_attribute *attr, | ||
458 | char *buf) | ||
459 | { | ||
460 | return sprintf(buf, "%lu\n", PAGES2KB(balloon_stats.target_pages)); | ||
461 | } | ||
462 | |||
463 | static ssize_t store_target_kb(struct sys_device *dev, | ||
464 | struct sysdev_attribute *attr, | ||
465 | const char *buf, | ||
466 | size_t count) | ||
467 | { | ||
468 | char *endchar; | ||
469 | unsigned long long target_bytes; | ||
470 | |||
471 | if (!capable(CAP_SYS_ADMIN)) | ||
472 | return -EPERM; | ||
473 | |||
474 | target_bytes = simple_strtoull(buf, &endchar, 0) * 1024; | ||
475 | |||
476 | balloon_set_new_target(target_bytes >> PAGE_SHIFT); | ||
477 | |||
478 | return count; | ||
479 | } | ||
480 | |||
481 | static SYSDEV_ATTR(target_kb, S_IRUGO | S_IWUSR, | ||
482 | show_target_kb, store_target_kb); | ||
483 | |||
484 | |||
485 | static ssize_t show_target(struct sys_device *dev, struct sysdev_attribute *attr, | ||
486 | char *buf) | ||
487 | { | ||
488 | return sprintf(buf, "%llu\n", | ||
489 | (unsigned long long)balloon_stats.target_pages | ||
490 | << PAGE_SHIFT); | ||
491 | } | ||
492 | |||
493 | static ssize_t store_target(struct sys_device *dev, | ||
494 | struct sysdev_attribute *attr, | ||
495 | const char *buf, | ||
496 | size_t count) | ||
497 | { | ||
498 | char *endchar; | ||
499 | unsigned long long target_bytes; | ||
500 | |||
501 | if (!capable(CAP_SYS_ADMIN)) | ||
502 | return -EPERM; | ||
503 | |||
504 | target_bytes = memparse(buf, &endchar); | ||
505 | |||
506 | balloon_set_new_target(target_bytes >> PAGE_SHIFT); | ||
507 | |||
508 | return count; | ||
509 | } | ||
510 | |||
511 | static SYSDEV_ATTR(target, S_IRUGO | S_IWUSR, | ||
512 | show_target, store_target); | ||
513 | |||
514 | |||
515 | static struct sysdev_attribute *balloon_attrs[] = { | ||
516 | &attr_target_kb, | ||
517 | &attr_target, | ||
518 | }; | ||
519 | |||
520 | static struct attribute *balloon_info_attrs[] = { | ||
521 | &attr_current_kb.attr, | ||
522 | &attr_low_kb.attr, | ||
523 | &attr_high_kb.attr, | ||
524 | &attr_driver_kb.attr, | ||
525 | NULL | ||
526 | }; | ||
527 | |||
528 | static struct attribute_group balloon_info_group = { | ||
529 | .name = "info", | ||
530 | .attrs = balloon_info_attrs, | ||
531 | }; | ||
532 | |||
533 | static struct sysdev_class balloon_sysdev_class = { | ||
534 | .name = BALLOON_CLASS_NAME, | ||
535 | }; | ||
536 | |||
537 | static int register_balloon(struct sys_device *sysdev) | ||
538 | { | ||
539 | int i, error; | ||
540 | |||
541 | error = sysdev_class_register(&balloon_sysdev_class); | ||
542 | if (error) | ||
543 | return error; | ||
544 | |||
545 | sysdev->id = 0; | ||
546 | sysdev->cls = &balloon_sysdev_class; | ||
547 | |||
548 | error = sysdev_register(sysdev); | ||
549 | if (error) { | ||
550 | sysdev_class_unregister(&balloon_sysdev_class); | ||
551 | return error; | ||
552 | } | ||
553 | |||
554 | for (i = 0; i < ARRAY_SIZE(balloon_attrs); i++) { | ||
555 | error = sysdev_create_file(sysdev, balloon_attrs[i]); | ||
556 | if (error) | ||
557 | goto fail; | ||
558 | } | ||
559 | |||
560 | error = sysfs_create_group(&sysdev->kobj, &balloon_info_group); | ||
561 | if (error) | ||
562 | goto fail; | ||
563 | |||
564 | return 0; | ||
565 | |||
566 | fail: | ||
567 | while (--i >= 0) | ||
568 | sysdev_remove_file(sysdev, balloon_attrs[i]); | ||
569 | sysdev_unregister(sysdev); | ||
570 | sysdev_class_unregister(&balloon_sysdev_class); | ||
571 | return error; | ||
572 | } | ||
573 | |||
574 | MODULE_LICENSE("GPL"); | 476 | MODULE_LICENSE("GPL"); |