Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 1 | /****************************************************************************** |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 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> |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 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> |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 41 | #include <linux/list.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 42 | #include <linux/gfp.h> |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 43 | |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 44 | #include <asm/page.h> |
| 45 | #include <asm/pgalloc.h> |
| 46 | #include <asm/pgtable.h> |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 47 | #include <asm/tlb.h> |
Jeremy Fitzhardinge | 66946f6 | 2010-09-14 10:32:32 -0700 | [diff] [blame] | 48 | #include <asm/e820.h> |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 49 | |
Jeremy Fitzhardinge | ecbf29c | 2008-12-16 12:37:07 -0800 | [diff] [blame] | 50 | #include <asm/xen/hypervisor.h> |
| 51 | #include <asm/xen/hypercall.h> |
Jeremy Fitzhardinge | 1ccbf53 | 2009-10-06 15:11:14 -0700 | [diff] [blame] | 52 | |
| 53 | #include <xen/xen.h> |
Jeremy Fitzhardinge | ecbf29c | 2008-12-16 12:37:07 -0800 | [diff] [blame] | 54 | #include <xen/interface/xen.h> |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 55 | #include <xen/interface/memory.h> |
Daniel De Graaf | 803eb04 | 2011-03-14 11:29:37 -0400 | [diff] [blame] | 56 | #include <xen/balloon.h> |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 57 | #include <xen/features.h> |
| 58 | #include <xen/page.h> |
| 59 | |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 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 | */ |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 67 | |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 68 | enum bp_state { |
| 69 | BP_DONE, |
| 70 | BP_EAGAIN, |
| 71 | BP_ECANCELED |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 74 | |
| 75 | static DEFINE_MUTEX(balloon_mutex); |
| 76 | |
Daniel De Graaf | 803eb04 | 2011-03-14 11:29:37 -0400 | [diff] [blame] | 77 | struct balloon_stats balloon_stats; |
| 78 | EXPORT_SYMBOL_GPL(balloon_stats); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 79 | |
| 80 | /* We increase/decrease in batches which fit in a page */ |
| 81 | static unsigned long frame_list[PAGE_SIZE / sizeof(unsigned long)]; |
| 82 | |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 83 | #ifdef CONFIG_HIGHMEM |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 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); |
Daniel Kiper | 95170b2 | 2011-03-08 22:47:39 +0100 | [diff] [blame] | 96 | static DECLARE_DELAYED_WORK(balloon_worker, balloon_process); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 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 |
Jeremy Fitzhardinge | 26a3e99 | 2008-11-17 09:35:00 -0800 | [diff] [blame] | 106 | clear_highpage(page); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 107 | #endif |
| 108 | } |
| 109 | |
| 110 | /* balloon_append: add the given page to the balloon. */ |
Jeremy Fitzhardinge | 9be4d45 | 2010-08-31 15:01:16 -0700 | [diff] [blame] | 111 | static void __balloon_append(struct page *page) |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 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++; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 117 | } else { |
| 118 | list_add(&page->lru, &ballooned_pages); |
| 119 | balloon_stats.balloon_low++; |
| 120 | } |
Jeremy Fitzhardinge | 9be4d45 | 2010-08-31 15:01:16 -0700 | [diff] [blame] | 121 | } |
Gianluca Guida | 3d65c94 | 2009-07-30 22:54:36 +0100 | [diff] [blame] | 122 | |
Jeremy Fitzhardinge | 9be4d45 | 2010-08-31 15:01:16 -0700 | [diff] [blame] | 123 | static void balloon_append(struct page *page) |
| 124 | { |
| 125 | __balloon_append(page); |
Daniel Kiper | 09ca132 | 2011-03-28 11:35:59 +0200 | [diff] [blame] | 126 | if (PageHighMem(page)) |
| 127 | dec_totalhigh_pages(); |
Gianluca Guida | 3d65c94 | 2009-07-30 22:54:36 +0100 | [diff] [blame] | 128 | totalram_pages--; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* balloon_retrieve: rescue a page from the balloon, if it is not empty. */ |
Konrad Rzeszutek Wilk | b6f3067 | 2011-03-15 10:23:57 -0400 | [diff] [blame] | 132 | static struct page *balloon_retrieve(bool prefer_highmem) |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 133 | { |
| 134 | struct page *page; |
| 135 | |
| 136 | if (list_empty(&ballooned_pages)) |
| 137 | return NULL; |
| 138 | |
Konrad Rzeszutek Wilk | b6f3067 | 2011-03-15 10:23:57 -0400 | [diff] [blame] | 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); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 143 | list_del(&page->lru); |
| 144 | |
| 145 | if (PageHighMem(page)) { |
| 146 | balloon_stats.balloon_high--; |
| 147 | inc_totalhigh_pages(); |
| 148 | } |
| 149 | else |
| 150 | balloon_stats.balloon_low--; |
| 151 | |
Gianluca Guida | 3d65c94 | 2009-07-30 22:54:36 +0100 | [diff] [blame] | 152 | totalram_pages++; |
| 153 | |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 154 | return page; |
| 155 | } |
| 156 | |
| 157 | static struct page *balloon_first_page(void) |
| 158 | { |
| 159 | if (list_empty(&ballooned_pages)) |
| 160 | return NULL; |
| 161 | return list_entry(ballooned_pages.next, struct page, lru); |
| 162 | } |
| 163 | |
| 164 | static struct page *balloon_next_page(struct page *page) |
| 165 | { |
| 166 | struct list_head *next = page->lru.next; |
| 167 | if (next == &ballooned_pages) |
| 168 | return NULL; |
| 169 | return list_entry(next, struct page, lru); |
| 170 | } |
| 171 | |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 172 | static enum bp_state update_schedule(enum bp_state state) |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 173 | { |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 174 | if (state == BP_DONE) { |
| 175 | balloon_stats.schedule_delay = 1; |
| 176 | balloon_stats.retry_count = 1; |
| 177 | return BP_DONE; |
| 178 | } |
| 179 | |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 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) { |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 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; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Daniel Kiper | 83be7e5 | 2011-03-28 11:34:10 +0200 | [diff] [blame] | 197 | static long current_credit(void) |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 198 | { |
Ian Campbell | bc2c030 | 2009-06-05 11:58:37 +0100 | [diff] [blame] | 199 | unsigned long target = balloon_stats.target_pages; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 200 | |
| 201 | target = min(target, |
| 202 | balloon_stats.current_pages + |
| 203 | balloon_stats.balloon_low + |
| 204 | balloon_stats.balloon_high); |
| 205 | |
Daniel Kiper | 83be7e5 | 2011-03-28 11:34:10 +0200 | [diff] [blame] | 206 | return target - balloon_stats.current_pages; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 209 | static enum bp_state increase_reservation(unsigned long nr_pages) |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 210 | { |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 211 | int rc; |
Jeremy Fitzhardinge | 2f70e0a | 2010-09-02 23:11:17 -0700 | [diff] [blame] | 212 | unsigned long pfn, i; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 213 | struct page *page; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 214 | struct xen_memory_reservation reservation = { |
| 215 | .address_bits = 0, |
| 216 | .extent_order = 0, |
| 217 | .domid = DOMID_SELF |
| 218 | }; |
| 219 | |
| 220 | if (nr_pages > ARRAY_SIZE(frame_list)) |
| 221 | nr_pages = ARRAY_SIZE(frame_list); |
| 222 | |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 223 | page = balloon_first_page(); |
| 224 | for (i = 0; i < nr_pages; i++) { |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 225 | if (!page) { |
| 226 | nr_pages = i; |
| 227 | break; |
| 228 | } |
Joe Perches | a419aef | 2009-08-18 11:18:35 -0700 | [diff] [blame] | 229 | frame_list[i] = page_to_pfn(page); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 230 | page = balloon_next_page(page); |
| 231 | } |
| 232 | |
Isaku Yamahata | a90971e | 2008-05-26 23:31:14 +0100 | [diff] [blame] | 233 | set_xen_guest_handle(reservation.extent_start, frame_list); |
Jeremy Fitzhardinge | fde28e8 | 2008-07-24 16:28:00 -0700 | [diff] [blame] | 234 | reservation.nr_extents = nr_pages; |
| 235 | rc = HYPERVISOR_memory_op(XENMEM_populate_physmap, &reservation); |
Konrad Rzeszutek Wilk | 40095de | 2011-03-14 11:42:40 -0400 | [diff] [blame] | 236 | if (rc <= 0) |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 237 | return BP_EAGAIN; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 238 | |
Ian Campbell | bc2c030 | 2009-06-05 11:58:37 +0100 | [diff] [blame] | 239 | for (i = 0; i < rc; i++) { |
Konrad Rzeszutek Wilk | b6f3067 | 2011-03-15 10:23:57 -0400 | [diff] [blame] | 240 | page = balloon_retrieve(false); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 241 | BUG_ON(page == NULL); |
| 242 | |
| 243 | pfn = page_to_pfn(page); |
| 244 | BUG_ON(!xen_feature(XENFEAT_auto_translated_physmap) && |
| 245 | phys_to_machine_mapping_valid(pfn)); |
| 246 | |
| 247 | set_phys_to_machine(pfn, frame_list[i]); |
| 248 | |
| 249 | /* Link back into the page tables if not highmem. */ |
Daniel Kiper | 4dfe22f | 2011-03-28 11:33:18 +0200 | [diff] [blame] | 250 | if (xen_pv_domain() && !PageHighMem(page)) { |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 251 | int ret; |
| 252 | ret = HYPERVISOR_update_va_mapping( |
| 253 | (unsigned long)__va(pfn << PAGE_SHIFT), |
| 254 | mfn_pte(frame_list[i], PAGE_KERNEL), |
| 255 | 0); |
| 256 | BUG_ON(ret); |
| 257 | } |
| 258 | |
| 259 | /* Relinquish the page back to the allocator. */ |
| 260 | ClearPageReserved(page); |
| 261 | init_page_count(page); |
| 262 | __free_page(page); |
| 263 | } |
| 264 | |
Ian Campbell | bc2c030 | 2009-06-05 11:58:37 +0100 | [diff] [blame] | 265 | balloon_stats.current_pages += rc; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 266 | |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 267 | return BP_DONE; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Konrad Rzeszutek Wilk | b6f3067 | 2011-03-15 10:23:57 -0400 | [diff] [blame] | 270 | static enum bp_state decrease_reservation(unsigned long nr_pages, gfp_t gfp) |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 271 | { |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 272 | enum bp_state state = BP_DONE; |
Jeremy Fitzhardinge | 2f70e0a | 2010-09-02 23:11:17 -0700 | [diff] [blame] | 273 | unsigned long pfn, i; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 274 | struct page *page; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 275 | int ret; |
| 276 | struct xen_memory_reservation reservation = { |
| 277 | .address_bits = 0, |
| 278 | .extent_order = 0, |
| 279 | .domid = DOMID_SELF |
| 280 | }; |
| 281 | |
| 282 | if (nr_pages > ARRAY_SIZE(frame_list)) |
| 283 | nr_pages = ARRAY_SIZE(frame_list); |
| 284 | |
| 285 | for (i = 0; i < nr_pages; i++) { |
Konrad Rzeszutek Wilk | b6f3067 | 2011-03-15 10:23:57 -0400 | [diff] [blame] | 286 | if ((page = alloc_page(gfp)) == NULL) { |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 287 | nr_pages = i; |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 288 | state = BP_EAGAIN; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 289 | break; |
| 290 | } |
| 291 | |
| 292 | pfn = page_to_pfn(page); |
| 293 | frame_list[i] = pfn_to_mfn(pfn); |
| 294 | |
| 295 | scrub_page(page); |
Dan Magenheimer | 1058a75 | 2009-01-22 14:36:08 -0800 | [diff] [blame] | 296 | |
Daniel Kiper | 4dfe22f | 2011-03-28 11:33:18 +0200 | [diff] [blame] | 297 | if (xen_pv_domain() && !PageHighMem(page)) { |
Ian Campbell | ff4ce8c | 2009-01-23 16:26:21 +0000 | [diff] [blame] | 298 | ret = HYPERVISOR_update_va_mapping( |
| 299 | (unsigned long)__va(pfn << PAGE_SHIFT), |
| 300 | __pte_ma(0), 0); |
| 301 | BUG_ON(ret); |
| 302 | } |
| 303 | |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /* Ensure that ballooned highmem pages don't have kmaps. */ |
| 307 | kmap_flush_unused(); |
| 308 | flush_tlb_all(); |
| 309 | |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 310 | /* No more mappings: invalidate P2M and add to balloon. */ |
| 311 | for (i = 0; i < nr_pages; i++) { |
| 312 | pfn = mfn_to_pfn(frame_list[i]); |
Konrad Rzeszutek Wilk | 6eaa412 | 2011-01-18 20:09:41 -0500 | [diff] [blame] | 313 | __set_phys_to_machine(pfn, INVALID_P2M_ENTRY); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 314 | balloon_append(pfn_to_page(pfn)); |
| 315 | } |
| 316 | |
Isaku Yamahata | a90971e | 2008-05-26 23:31:14 +0100 | [diff] [blame] | 317 | set_xen_guest_handle(reservation.extent_start, frame_list); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 318 | reservation.nr_extents = nr_pages; |
| 319 | ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation); |
| 320 | BUG_ON(ret != nr_pages); |
| 321 | |
| 322 | balloon_stats.current_pages -= nr_pages; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 323 | |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 324 | return state; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 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(struct work_struct *work) |
| 334 | { |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 335 | enum bp_state state = BP_DONE; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 336 | long credit; |
| 337 | |
| 338 | mutex_lock(&balloon_mutex); |
| 339 | |
| 340 | do { |
Daniel Kiper | 83be7e5 | 2011-03-28 11:34:10 +0200 | [diff] [blame] | 341 | credit = current_credit(); |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 342 | |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 343 | if (credit > 0) |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 344 | state = increase_reservation(credit); |
| 345 | |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 346 | if (credit < 0) |
Konrad Rzeszutek Wilk | b6f3067 | 2011-03-15 10:23:57 -0400 | [diff] [blame] | 347 | state = decrease_reservation(-credit, GFP_BALLOON); |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 348 | |
| 349 | state = update_schedule(state); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 350 | |
| 351 | #ifndef CONFIG_PREEMPT |
| 352 | if (need_resched()) |
| 353 | schedule(); |
| 354 | #endif |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 355 | } while (credit && state == BP_DONE); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 356 | |
| 357 | /* Schedule more work if there is some still to be done. */ |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 358 | if (state == BP_EAGAIN) |
| 359 | schedule_delayed_work(&balloon_worker, balloon_stats.schedule_delay * HZ); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 360 | |
| 361 | mutex_unlock(&balloon_mutex); |
| 362 | } |
| 363 | |
| 364 | /* Resets the Xen limit, sets new target, and kicks off processing. */ |
Daniel De Graaf | 803eb04 | 2011-03-14 11:29:37 -0400 | [diff] [blame] | 365 | void balloon_set_new_target(unsigned long target) |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 366 | { |
| 367 | /* No need for lock. Not read-modify-write updates. */ |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 368 | balloon_stats.target_pages = target; |
Daniel Kiper | 95170b2 | 2011-03-08 22:47:39 +0100 | [diff] [blame] | 369 | schedule_delayed_work(&balloon_worker, 0); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 370 | } |
Daniel De Graaf | 803eb04 | 2011-03-14 11:29:37 -0400 | [diff] [blame] | 371 | EXPORT_SYMBOL_GPL(balloon_set_new_target); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 372 | |
Konrad Rzeszutek Wilk | b6f3067 | 2011-03-15 10:23:57 -0400 | [diff] [blame] | 373 | /** |
| 374 | * alloc_xenballooned_pages - get pages that have been ballooned out |
| 375 | * @nr_pages: Number of pages to get |
| 376 | * @pages: pages returned |
| 377 | * @return 0 on success, error otherwise |
| 378 | */ |
| 379 | int alloc_xenballooned_pages(int nr_pages, struct page** pages) |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 380 | { |
Konrad Rzeszutek Wilk | b6f3067 | 2011-03-15 10:23:57 -0400 | [diff] [blame] | 381 | int pgno = 0; |
| 382 | struct page* page; |
| 383 | mutex_lock(&balloon_mutex); |
| 384 | while (pgno < nr_pages) { |
| 385 | page = balloon_retrieve(true); |
| 386 | if (page) { |
| 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 | } |
| 394 | } |
| 395 | mutex_unlock(&balloon_mutex); |
| 396 | return 0; |
| 397 | out_undo: |
| 398 | while (pgno) |
| 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; |
| 404 | } |
| 405 | EXPORT_SYMBOL(alloc_xenballooned_pages); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 406 | |
Konrad Rzeszutek Wilk | b6f3067 | 2011-03-15 10:23:57 -0400 | [diff] [blame] | 407 | /** |
| 408 | * free_xenballooned_pages - return pages retrieved with get_ballooned_pages |
| 409 | * @nr_pages: Number of pages |
| 410 | * @pages: pages to return |
| 411 | */ |
| 412 | void free_xenballooned_pages(int nr_pages, struct page** pages) |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 413 | { |
Konrad Rzeszutek Wilk | b6f3067 | 2011-03-15 10:23:57 -0400 | [diff] [blame] | 414 | int i; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 415 | |
Konrad Rzeszutek Wilk | b6f3067 | 2011-03-15 10:23:57 -0400 | [diff] [blame] | 416 | mutex_lock(&balloon_mutex); |
| 417 | |
| 418 | for (i = 0; i < nr_pages; i++) { |
| 419 | if (pages[i]) |
| 420 | balloon_append(pages[i]); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Konrad Rzeszutek Wilk | b6f3067 | 2011-03-15 10:23:57 -0400 | [diff] [blame] | 423 | /* The balloon may be too large now. Shrink it if needed. */ |
Daniel Kiper | 83be7e5 | 2011-03-28 11:34:10 +0200 | [diff] [blame] | 424 | if (current_credit()) |
Konrad Rzeszutek Wilk | b6f3067 | 2011-03-15 10:23:57 -0400 | [diff] [blame] | 425 | schedule_delayed_work(&balloon_worker, 0); |
| 426 | |
| 427 | mutex_unlock(&balloon_mutex); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 428 | } |
Konrad Rzeszutek Wilk | b6f3067 | 2011-03-15 10:23:57 -0400 | [diff] [blame] | 429 | EXPORT_SYMBOL(free_xenballooned_pages); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 430 | |
| 431 | static int __init balloon_init(void) |
| 432 | { |
Daniel Kiper | 4dfe22f | 2011-03-28 11:33:18 +0200 | [diff] [blame] | 433 | unsigned long pfn, extra_pfn_end; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 434 | struct page *page; |
| 435 | |
Stefano Stabellini | 53d5522 | 2010-12-02 17:55:05 +0000 | [diff] [blame] | 436 | if (!xen_domain()) |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 437 | return -ENODEV; |
| 438 | |
Daniel De Graaf | 803eb04 | 2011-03-14 11:29:37 -0400 | [diff] [blame] | 439 | pr_info("xen/balloon: Initialising balloon driver.\n"); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 440 | |
Daniel Kiper | 4dfe22f | 2011-03-28 11:33:18 +0200 | [diff] [blame] | 441 | balloon_stats.current_pages = xen_pv_domain() ? min(xen_start_info->nr_pages, max_pfn) : max_pfn; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 442 | balloon_stats.target_pages = balloon_stats.current_pages; |
| 443 | balloon_stats.balloon_low = 0; |
| 444 | balloon_stats.balloon_high = 0; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 445 | |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 446 | balloon_stats.schedule_delay = 1; |
| 447 | balloon_stats.max_schedule_delay = 32; |
| 448 | balloon_stats.retry_count = 1; |
Konrad Rzeszutek Wilk | 40095de | 2011-03-14 11:42:40 -0400 | [diff] [blame] | 449 | balloon_stats.max_retry_count = RETRY_UNLIMITED; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 450 | |
Jeremy Fitzhardinge | 2a4c92f | 2010-12-02 15:30:06 -0800 | [diff] [blame] | 451 | /* |
| 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()), |
Jeremy Fitzhardinge | 66946f6 | 2010-09-14 10:32:32 -0700 | [diff] [blame] | 461 | (unsigned long)PFN_DOWN(xen_extra_mem_start + xen_extra_mem_size)); |
Jeremy Fitzhardinge | 9be4d45 | 2010-08-31 15:01:16 -0700 | [diff] [blame] | 462 | for (pfn = PFN_UP(xen_extra_mem_start); |
Jeremy Fitzhardinge | 66946f6 | 2010-09-14 10:32:32 -0700 | [diff] [blame] | 463 | pfn < extra_pfn_end; |
Jeremy Fitzhardinge | 9be4d45 | 2010-08-31 15:01:16 -0700 | [diff] [blame] | 464 | pfn++) { |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 465 | page = pfn_to_page(pfn); |
Daniel Kiper | 09ca132 | 2011-03-28 11:35:59 +0200 | [diff] [blame] | 466 | /* totalram_pages and totalhigh_pages do not include the boot-time |
Jeremy Fitzhardinge | 9be4d45 | 2010-08-31 15:01:16 -0700 | [diff] [blame] | 467 | balloon extension, so don't subtract from it. */ |
| 468 | __balloon_append(page); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | subsys_initcall(balloon_init); |
| 475 | |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 476 | MODULE_LICENSE("GPL"); |