]>
Commit | Line | Data |
---|---|---|
1775826c | 1 | /****************************************************************************** |
1775826c JF |
2 | * Xen balloon driver - enables returning/claiming memory to/from Xen. |
3 | * | |
4 | * Copyright (c) 2003, B Dragovic | |
5 | * Copyright (c) 2003-2004, M Williamson, K Fraser | |
6 | * Copyright (c) 2005 Dan M. Smith, IBM Corporation | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public License version 2 | |
10 | * as published by the Free Software Foundation; or, when distributed | |
11 | * separately from the Linux kernel or incorporated into other | |
12 | * software packages, subject to the following license: | |
13 | * | |
14 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
15 | * of this source file (the "Software"), to deal in the Software without | |
16 | * restriction, including without limitation the rights to use, copy, modify, | |
17 | * merge, publish, distribute, sublicense, and/or sell copies of the Software, | |
18 | * and to permit persons to whom the Software is furnished to do so, subject to | |
19 | * the following conditions: | |
20 | * | |
21 | * The above copyright notice and this permission notice shall be included in | |
22 | * all copies or substantial portions of the Software. | |
23 | * | |
24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
25 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
26 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
27 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
28 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
29 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
30 | * IN THE SOFTWARE. | |
31 | */ | |
32 | ||
33 | #include <linux/kernel.h> | |
1775826c JF |
34 | #include <linux/sched.h> |
35 | #include <linux/errno.h> | |
36 | #include <linux/mm.h> | |
37 | #include <linux/bootmem.h> | |
38 | #include <linux/pagemap.h> | |
39 | #include <linux/highmem.h> | |
40 | #include <linux/mutex.h> | |
1775826c | 41 | #include <linux/list.h> |
5a0e3ad6 | 42 | #include <linux/gfp.h> |
1775826c | 43 | |
1775826c JF |
44 | #include <asm/page.h> |
45 | #include <asm/pgalloc.h> | |
46 | #include <asm/pgtable.h> | |
1775826c | 47 | #include <asm/tlb.h> |
66946f67 | 48 | #include <asm/e820.h> |
1775826c | 49 | |
ecbf29cd JF |
50 | #include <asm/xen/hypervisor.h> |
51 | #include <asm/xen/hypercall.h> | |
1ccbf534 JF |
52 | |
53 | #include <xen/xen.h> | |
ecbf29cd | 54 | #include <xen/interface/xen.h> |
1775826c | 55 | #include <xen/interface/memory.h> |
803eb047 | 56 | #include <xen/balloon.h> |
1775826c JF |
57 | #include <xen/features.h> |
58 | #include <xen/page.h> | |
59 | ||
95d2ac4a DK |
60 | /* |
61 | * balloon_process() state: | |
62 | * | |
63 | * BP_DONE: done or nothing to do, | |
64 | * BP_EAGAIN: error, go to sleep, | |
65 | * BP_ECANCELED: error, balloon operation canceled. | |
66 | */ | |
1775826c | 67 | |
95d2ac4a DK |
68 | enum bp_state { |
69 | BP_DONE, | |
70 | BP_EAGAIN, | |
71 | BP_ECANCELED | |
1775826c JF |
72 | }; |
73 | ||
1775826c | 74 | |
1775826c | 75 | static DEFINE_MUTEX(balloon_mutex); |
1775826c | 76 | |
803eb047 DDG |
77 | struct balloon_stats balloon_stats; |
78 | EXPORT_SYMBOL_GPL(balloon_stats); | |
1775826c JF |
79 | |
80 | /* We increase/decrease in batches which fit in a page */ | |
81 | static unsigned long frame_list[PAGE_SIZE / sizeof(unsigned long)]; | |
82 | ||
1775826c | 83 | #ifdef CONFIG_HIGHMEM |
1775826c JF |
84 | #define inc_totalhigh_pages() (totalhigh_pages++) |
85 | #define dec_totalhigh_pages() (totalhigh_pages--) | |
86 | #else | |
87 | #define inc_totalhigh_pages() do {} while(0) | |
88 | #define dec_totalhigh_pages() do {} while(0) | |
89 | #endif | |
90 | ||
91 | /* List of ballooned pages, threaded through the mem_map array. */ | |
92 | static LIST_HEAD(ballooned_pages); | |
93 | ||
94 | /* Main work function, always executed in process context. */ | |
95 | static void balloon_process(struct work_struct *work); | |
95170b2e | 96 | static DECLARE_DELAYED_WORK(balloon_worker, balloon_process); |
1775826c JF |
97 | |
98 | /* When ballooning out (allocating memory to return to Xen) we don't really | |
99 | want the kernel to try too hard since that can trigger the oom killer. */ | |
100 | #define GFP_BALLOON \ | |
101 | (GFP_HIGHUSER | __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC) | |
102 | ||
103 | static void scrub_page(struct page *page) | |
104 | { | |
105 | #ifdef CONFIG_XEN_SCRUB_PAGES | |
26a3e991 | 106 | clear_highpage(page); |
1775826c JF |
107 | #endif |
108 | } | |
109 | ||
110 | /* balloon_append: add the given page to the balloon. */ | |
9be4d457 | 111 | static void __balloon_append(struct page *page) |
1775826c JF |
112 | { |
113 | /* Lowmem is re-populated first, so highmem pages go at list tail. */ | |
114 | if (PageHighMem(page)) { | |
115 | list_add_tail(&page->lru, &ballooned_pages); | |
116 | balloon_stats.balloon_high++; | |
117 | dec_totalhigh_pages(); | |
118 | } else { | |
119 | list_add(&page->lru, &ballooned_pages); | |
120 | balloon_stats.balloon_low++; | |
121 | } | |
9be4d457 | 122 | } |
3d65c948 | 123 | |
9be4d457 JF |
124 | static void balloon_append(struct page *page) |
125 | { | |
126 | __balloon_append(page); | |
3d65c948 | 127 | totalram_pages--; |
1775826c JF |
128 | } |
129 | ||
130 | /* balloon_retrieve: rescue a page from the balloon, if it is not empty. */ | |
b6f30679 | 131 | static struct page *balloon_retrieve(bool prefer_highmem) |
1775826c JF |
132 | { |
133 | struct page *page; | |
134 | ||
135 | if (list_empty(&ballooned_pages)) | |
136 | return NULL; | |
137 | ||
b6f30679 KRW |
138 | if (prefer_highmem) |
139 | page = list_entry(ballooned_pages.prev, struct page, lru); | |
140 | else | |
141 | page = list_entry(ballooned_pages.next, struct page, lru); | |
1775826c JF |
142 | list_del(&page->lru); |
143 | ||
144 | if (PageHighMem(page)) { | |
145 | balloon_stats.balloon_high--; | |
146 | inc_totalhigh_pages(); | |
147 | } | |
148 | else | |
149 | balloon_stats.balloon_low--; | |
150 | ||
3d65c948 GG |
151 | totalram_pages++; |
152 | ||
1775826c JF |
153 | return page; |
154 | } | |
155 | ||
156 | static struct page *balloon_first_page(void) | |
157 | { | |
158 | if (list_empty(&ballooned_pages)) | |
159 | return NULL; | |
160 | return list_entry(ballooned_pages.next, struct page, lru); | |
161 | } | |
162 | ||
163 | static struct page *balloon_next_page(struct page *page) | |
164 | { | |
165 | struct list_head *next = page->lru.next; | |
166 | if (next == &ballooned_pages) | |
167 | return NULL; | |
168 | return list_entry(next, struct page, lru); | |
169 | } | |
170 | ||
95d2ac4a | 171 | static enum bp_state update_schedule(enum bp_state state) |
1775826c | 172 | { |
95d2ac4a DK |
173 | if (state == BP_DONE) { |
174 | balloon_stats.schedule_delay = 1; | |
175 | balloon_stats.retry_count = 1; | |
176 | return BP_DONE; | |
177 | } | |
178 | ||
95d2ac4a DK |
179 | ++balloon_stats.retry_count; |
180 | ||
181 | if (balloon_stats.max_retry_count != RETRY_UNLIMITED && | |
182 | balloon_stats.retry_count > balloon_stats.max_retry_count) { | |
95d2ac4a DK |
183 | balloon_stats.schedule_delay = 1; |
184 | balloon_stats.retry_count = 1; | |
185 | return BP_ECANCELED; | |
186 | } | |
187 | ||
188 | balloon_stats.schedule_delay <<= 1; | |
189 | ||
190 | if (balloon_stats.schedule_delay > balloon_stats.max_schedule_delay) | |
191 | balloon_stats.schedule_delay = balloon_stats.max_schedule_delay; | |
192 | ||
193 | return BP_EAGAIN; | |
1775826c JF |
194 | } |
195 | ||
196 | static unsigned long current_target(void) | |
197 | { | |
bc2c0303 | 198 | unsigned long target = balloon_stats.target_pages; |
1775826c JF |
199 | |
200 | target = min(target, | |
201 | balloon_stats.current_pages + | |
202 | balloon_stats.balloon_low + | |
203 | balloon_stats.balloon_high); | |
204 | ||
205 | return target; | |
206 | } | |
207 | ||
95d2ac4a | 208 | static enum bp_state increase_reservation(unsigned long nr_pages) |
1775826c | 209 | { |
95d2ac4a | 210 | int rc; |
2f70e0ac | 211 | unsigned long pfn, i; |
1775826c | 212 | struct page *page; |
1775826c JF |
213 | struct xen_memory_reservation reservation = { |
214 | .address_bits = 0, | |
215 | .extent_order = 0, | |
216 | .domid = DOMID_SELF | |
217 | }; | |
218 | ||
219 | if (nr_pages > ARRAY_SIZE(frame_list)) | |
220 | nr_pages = ARRAY_SIZE(frame_list); | |
221 | ||
1775826c JF |
222 | page = balloon_first_page(); |
223 | for (i = 0; i < nr_pages; i++) { | |
95d2ac4a DK |
224 | if (!page) { |
225 | nr_pages = i; | |
226 | break; | |
227 | } | |
a419aef8 | 228 | frame_list[i] = page_to_pfn(page); |
1775826c JF |
229 | page = balloon_next_page(page); |
230 | } | |
231 | ||
a90971eb | 232 | set_xen_guest_handle(reservation.extent_start, frame_list); |
fde28e8f JF |
233 | reservation.nr_extents = nr_pages; |
234 | rc = HYPERVISOR_memory_op(XENMEM_populate_physmap, &reservation); | |
40095de1 | 235 | if (rc <= 0) |
95d2ac4a | 236 | return BP_EAGAIN; |
1775826c | 237 | |
bc2c0303 | 238 | for (i = 0; i < rc; i++) { |
b6f30679 | 239 | page = balloon_retrieve(false); |
1775826c JF |
240 | BUG_ON(page == NULL); |
241 | ||
242 | pfn = page_to_pfn(page); | |
243 | BUG_ON(!xen_feature(XENFEAT_auto_translated_physmap) && | |
244 | phys_to_machine_mapping_valid(pfn)); | |
245 | ||
246 | set_phys_to_machine(pfn, frame_list[i]); | |
247 | ||
248 | /* Link back into the page tables if not highmem. */ | |
53d5522c | 249 | if (!xen_hvm_domain() && pfn < max_low_pfn) { |
1775826c JF |
250 | int ret; |
251 | ret = HYPERVISOR_update_va_mapping( | |
252 | (unsigned long)__va(pfn << PAGE_SHIFT), | |
253 | mfn_pte(frame_list[i], PAGE_KERNEL), | |
254 | 0); | |
255 | BUG_ON(ret); | |
256 | } | |
257 | ||
258 | /* Relinquish the page back to the allocator. */ | |
259 | ClearPageReserved(page); | |
260 | init_page_count(page); | |
261 | __free_page(page); | |
262 | } | |
263 | ||
bc2c0303 | 264 | balloon_stats.current_pages += rc; |
1775826c | 265 | |
95d2ac4a | 266 | return BP_DONE; |
1775826c JF |
267 | } |
268 | ||
b6f30679 | 269 | static enum bp_state decrease_reservation(unsigned long nr_pages, gfp_t gfp) |
1775826c | 270 | { |
95d2ac4a | 271 | enum bp_state state = BP_DONE; |
2f70e0ac | 272 | unsigned long pfn, i; |
1775826c | 273 | struct page *page; |
1775826c JF |
274 | int ret; |
275 | struct xen_memory_reservation reservation = { | |
276 | .address_bits = 0, | |
277 | .extent_order = 0, | |
278 | .domid = DOMID_SELF | |
279 | }; | |
280 | ||
281 | if (nr_pages > ARRAY_SIZE(frame_list)) | |
282 | nr_pages = ARRAY_SIZE(frame_list); | |
283 | ||
284 | for (i = 0; i < nr_pages; i++) { | |
b6f30679 | 285 | if ((page = alloc_page(gfp)) == NULL) { |
1775826c | 286 | nr_pages = i; |
95d2ac4a | 287 | state = BP_EAGAIN; |
1775826c JF |
288 | break; |
289 | } | |
290 | ||
291 | pfn = page_to_pfn(page); | |
292 | frame_list[i] = pfn_to_mfn(pfn); | |
293 | ||
294 | scrub_page(page); | |
1058a75f | 295 | |
53d5522c | 296 | if (!xen_hvm_domain() && !PageHighMem(page)) { |
ff4ce8c3 IC |
297 | ret = HYPERVISOR_update_va_mapping( |
298 | (unsigned long)__va(pfn << PAGE_SHIFT), | |
299 | __pte_ma(0), 0); | |
300 | BUG_ON(ret); | |
301 | } | |
302 | ||
1775826c JF |
303 | } |
304 | ||
305 | /* Ensure that ballooned highmem pages don't have kmaps. */ | |
306 | kmap_flush_unused(); | |
307 | flush_tlb_all(); | |
308 | ||
1775826c JF |
309 | /* No more mappings: invalidate P2M and add to balloon. */ |
310 | for (i = 0; i < nr_pages; i++) { | |
311 | pfn = mfn_to_pfn(frame_list[i]); | |
6eaa412f | 312 | __set_phys_to_machine(pfn, INVALID_P2M_ENTRY); |
1775826c JF |
313 | balloon_append(pfn_to_page(pfn)); |
314 | } | |
315 | ||
a90971eb | 316 | set_xen_guest_handle(reservation.extent_start, frame_list); |
1775826c JF |
317 | reservation.nr_extents = nr_pages; |
318 | ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation); | |
319 | BUG_ON(ret != nr_pages); | |
320 | ||
321 | balloon_stats.current_pages -= nr_pages; | |
1775826c | 322 | |
95d2ac4a | 323 | return state; |
1775826c JF |
324 | } |
325 | ||
326 | /* | |
327 | * We avoid multiple worker processes conflicting via the balloon mutex. | |
328 | * We may of course race updates of the target counts (which are protected | |
329 | * by the balloon lock), or with changes to the Xen hard limit, but we will | |
330 | * recover from these in time. | |
331 | */ | |
332 | static void balloon_process(struct work_struct *work) | |
333 | { | |
95d2ac4a | 334 | enum bp_state state = BP_DONE; |
1775826c JF |
335 | long credit; |
336 | ||
337 | mutex_lock(&balloon_mutex); | |
338 | ||
339 | do { | |
340 | credit = current_target() - balloon_stats.current_pages; | |
95d2ac4a | 341 | |
1775826c | 342 | if (credit > 0) |
95d2ac4a DK |
343 | state = increase_reservation(credit); |
344 | ||
1775826c | 345 | if (credit < 0) |
b6f30679 | 346 | state = decrease_reservation(-credit, GFP_BALLOON); |
95d2ac4a DK |
347 | |
348 | state = update_schedule(state); | |
1775826c JF |
349 | |
350 | #ifndef CONFIG_PREEMPT | |
351 | if (need_resched()) | |
352 | schedule(); | |
353 | #endif | |
95d2ac4a | 354 | } while (credit && state == BP_DONE); |
1775826c JF |
355 | |
356 | /* Schedule more work if there is some still to be done. */ | |
95d2ac4a DK |
357 | if (state == BP_EAGAIN) |
358 | schedule_delayed_work(&balloon_worker, balloon_stats.schedule_delay * HZ); | |
1775826c JF |
359 | |
360 | mutex_unlock(&balloon_mutex); | |
361 | } | |
362 | ||
363 | /* Resets the Xen limit, sets new target, and kicks off processing. */ | |
803eb047 | 364 | void balloon_set_new_target(unsigned long target) |
1775826c JF |
365 | { |
366 | /* No need for lock. Not read-modify-write updates. */ | |
1775826c | 367 | balloon_stats.target_pages = target; |
95170b2e | 368 | schedule_delayed_work(&balloon_worker, 0); |
1775826c | 369 | } |
803eb047 | 370 | EXPORT_SYMBOL_GPL(balloon_set_new_target); |
1775826c | 371 | |
b6f30679 KRW |
372 | /** |
373 | * alloc_xenballooned_pages - get pages that have been ballooned out | |
374 | * @nr_pages: Number of pages to get | |
375 | * @pages: pages returned | |
376 | * @return 0 on success, error otherwise | |
377 | */ | |
378 | int alloc_xenballooned_pages(int nr_pages, struct page** pages) | |
1775826c | 379 | { |
b6f30679 KRW |
380 | int pgno = 0; |
381 | struct page* page; | |
382 | mutex_lock(&balloon_mutex); | |
383 | while (pgno < nr_pages) { | |
384 | page = balloon_retrieve(true); | |
385 | if (page) { | |
386 | pages[pgno++] = page; | |
387 | } else { | |
388 | enum bp_state st; | |
389 | st = decrease_reservation(nr_pages - pgno, GFP_HIGHUSER); | |
390 | if (st != BP_DONE) | |
391 | goto out_undo; | |
392 | } | |
1775826c | 393 | } |
b6f30679 KRW |
394 | mutex_unlock(&balloon_mutex); |
395 | return 0; | |
396 | out_undo: | |
397 | while (pgno) | |
398 | balloon_append(pages[--pgno]); | |
399 | /* Free the memory back to the kernel soon */ | |
400 | schedule_delayed_work(&balloon_worker, 0); | |
401 | mutex_unlock(&balloon_mutex); | |
402 | return -ENOMEM; | |
1775826c | 403 | } |
b6f30679 | 404 | EXPORT_SYMBOL(alloc_xenballooned_pages); |
1775826c | 405 | |
b6f30679 KRW |
406 | /** |
407 | * free_xenballooned_pages - return pages retrieved with get_ballooned_pages | |
408 | * @nr_pages: Number of pages | |
409 | * @pages: pages to return | |
410 | */ | |
411 | void free_xenballooned_pages(int nr_pages, struct page** pages) | |
1775826c | 412 | { |
b6f30679 | 413 | int i; |
1775826c | 414 | |
b6f30679 | 415 | mutex_lock(&balloon_mutex); |
1775826c | 416 | |
b6f30679 KRW |
417 | for (i = 0; i < nr_pages; i++) { |
418 | if (pages[i]) | |
419 | balloon_append(pages[i]); | |
420 | } | |
421 | ||
422 | /* The balloon may be too large now. Shrink it if needed. */ | |
423 | if (current_target() != balloon_stats.current_pages) | |
424 | schedule_delayed_work(&balloon_worker, 0); | |
1775826c | 425 | |
b6f30679 KRW |
426 | mutex_unlock(&balloon_mutex); |
427 | } | |
428 | EXPORT_SYMBOL(free_xenballooned_pages); | |
1775826c JF |
429 | |
430 | static int __init balloon_init(void) | |
431 | { | |
53d5522c | 432 | unsigned long pfn, nr_pages, extra_pfn_end; |
1775826c JF |
433 | struct page *page; |
434 | ||
53d5522c | 435 | if (!xen_domain()) |
1775826c JF |
436 | return -ENODEV; |
437 | ||
803eb047 | 438 | pr_info("xen/balloon: Initialising balloon driver.\n"); |
1775826c | 439 | |
53d5522c SS |
440 | if (xen_pv_domain()) |
441 | nr_pages = xen_start_info->nr_pages; | |
442 | else | |
443 | nr_pages = max_pfn; | |
444 | balloon_stats.current_pages = min(nr_pages, max_pfn); | |
1775826c JF |
445 | balloon_stats.target_pages = balloon_stats.current_pages; |
446 | balloon_stats.balloon_low = 0; | |
447 | balloon_stats.balloon_high = 0; | |
1775826c | 448 | |
95d2ac4a DK |
449 | balloon_stats.schedule_delay = 1; |
450 | balloon_stats.max_schedule_delay = 32; | |
451 | balloon_stats.retry_count = 1; | |
40095de1 | 452 | balloon_stats.max_retry_count = RETRY_UNLIMITED; |
1775826c | 453 | |
2a4c92fa JF |
454 | /* |
455 | * Initialise the balloon with excess memory space. We need | |
456 | * to make sure we don't add memory which doesn't exist or | |
457 | * logically exist. The E820 map can be trimmed to be smaller | |
458 | * than the amount of physical memory due to the mem= command | |
459 | * line parameter. And if this is a 32-bit non-HIGHMEM kernel | |
460 | * on a system with memory which requires highmem to access, | |
461 | * don't try to use it. | |
462 | */ | |
463 | extra_pfn_end = min(min(max_pfn, e820_end_of_ram_pfn()), | |
66946f67 | 464 | (unsigned long)PFN_DOWN(xen_extra_mem_start + xen_extra_mem_size)); |
9be4d457 | 465 | for (pfn = PFN_UP(xen_extra_mem_start); |
66946f67 | 466 | pfn < extra_pfn_end; |
9be4d457 | 467 | pfn++) { |
1775826c | 468 | page = pfn_to_page(pfn); |
9be4d457 JF |
469 | /* totalram_pages doesn't include the boot-time |
470 | balloon extension, so don't subtract from it. */ | |
471 | __balloon_append(page); | |
1775826c JF |
472 | } |
473 | ||
1775826c JF |
474 | return 0; |
475 | } | |
476 | ||
477 | subsys_initcall(balloon_init); | |
478 | ||
1775826c | 479 | MODULE_LICENSE("GPL"); |