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 |
Daniel Kiper | 080e2be | 2011-07-25 17:12:06 -0700 | [diff] [blame] | 7 | * Copyright (c) 2010 Daniel Kiper |
| 8 | * |
| 9 | * Memory hotplug support was written by Daniel Kiper. Work on |
| 10 | * it was sponsored by Google under Google Summer of Code 2010 |
| 11 | * program. Jeremy Fitzhardinge from Citrix was the mentor for |
| 12 | * this project. |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 13 | * |
| 14 | * This program is free software; you can redistribute it and/or |
| 15 | * modify it under the terms of the GNU General Public License version 2 |
| 16 | * as published by the Free Software Foundation; or, when distributed |
| 17 | * separately from the Linux kernel or incorporated into other |
| 18 | * software packages, subject to the following license: |
| 19 | * |
| 20 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 21 | * of this source file (the "Software"), to deal in the Software without |
| 22 | * restriction, including without limitation the rights to use, copy, modify, |
| 23 | * merge, publish, distribute, sublicense, and/or sell copies of the Software, |
| 24 | * and to permit persons to whom the Software is furnished to do so, subject to |
| 25 | * the following conditions: |
| 26 | * |
| 27 | * The above copyright notice and this permission notice shall be included in |
| 28 | * all copies or substantial portions of the Software. |
| 29 | * |
| 30 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 31 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 32 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 33 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 34 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 35 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 36 | * IN THE SOFTWARE. |
| 37 | */ |
| 38 | |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 39 | #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt |
| 40 | |
Stefano Stabellini | cd9151e | 2013-08-04 15:39:40 +0100 | [diff] [blame] | 41 | #include <linux/cpu.h> |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 42 | #include <linux/kernel.h> |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 43 | #include <linux/sched.h> |
| 44 | #include <linux/errno.h> |
Paul Gortmaker | 72ee511 | 2011-07-03 16:20:57 -0400 | [diff] [blame] | 45 | #include <linux/module.h> |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 46 | #include <linux/mm.h> |
| 47 | #include <linux/bootmem.h> |
| 48 | #include <linux/pagemap.h> |
| 49 | #include <linux/highmem.h> |
| 50 | #include <linux/mutex.h> |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 51 | #include <linux/list.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 52 | #include <linux/gfp.h> |
Daniel Kiper | 080e2be | 2011-07-25 17:12:06 -0700 | [diff] [blame] | 53 | #include <linux/notifier.h> |
| 54 | #include <linux/memory.h> |
| 55 | #include <linux/memory_hotplug.h> |
Stefano Stabellini | cd9151e | 2013-08-04 15:39:40 +0100 | [diff] [blame] | 56 | #include <linux/percpu-defs.h> |
David Vrabel | 55b3da9 | 2015-06-24 15:58:42 +0100 | [diff] [blame] | 57 | #include <linux/slab.h> |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 58 | |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 59 | #include <asm/page.h> |
| 60 | #include <asm/pgalloc.h> |
| 61 | #include <asm/pgtable.h> |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 62 | #include <asm/tlb.h> |
| 63 | |
Jeremy Fitzhardinge | ecbf29c | 2008-12-16 12:37:07 -0800 | [diff] [blame] | 64 | #include <asm/xen/hypervisor.h> |
| 65 | #include <asm/xen/hypercall.h> |
Jeremy Fitzhardinge | 1ccbf53 | 2009-10-06 15:11:14 -0700 | [diff] [blame] | 66 | |
| 67 | #include <xen/xen.h> |
Jeremy Fitzhardinge | ecbf29c | 2008-12-16 12:37:07 -0800 | [diff] [blame] | 68 | #include <xen/interface/xen.h> |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 69 | #include <xen/interface/memory.h> |
Daniel De Graaf | 803eb04 | 2011-03-14 11:29:37 -0400 | [diff] [blame] | 70 | #include <xen/balloon.h> |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 71 | #include <xen/features.h> |
| 72 | #include <xen/page.h> |
| 73 | |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 74 | /* |
| 75 | * balloon_process() state: |
| 76 | * |
| 77 | * BP_DONE: done or nothing to do, |
David Vrabel | b2ac6aa | 2015-06-25 12:10:28 +0100 | [diff] [blame^] | 78 | * BP_WAIT: wait to be rescheduled, |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 79 | * BP_EAGAIN: error, go to sleep, |
| 80 | * BP_ECANCELED: error, balloon operation canceled. |
| 81 | */ |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 82 | |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 83 | enum bp_state { |
| 84 | BP_DONE, |
David Vrabel | b2ac6aa | 2015-06-25 12:10:28 +0100 | [diff] [blame^] | 85 | BP_WAIT, |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 86 | BP_EAGAIN, |
| 87 | BP_ECANCELED |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 88 | }; |
| 89 | |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 90 | |
| 91 | static DEFINE_MUTEX(balloon_mutex); |
| 92 | |
Daniel De Graaf | 803eb04 | 2011-03-14 11:29:37 -0400 | [diff] [blame] | 93 | struct balloon_stats balloon_stats; |
| 94 | EXPORT_SYMBOL_GPL(balloon_stats); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 95 | |
| 96 | /* We increase/decrease in batches which fit in a page */ |
Ian Campbell | 965c0aa | 2012-10-17 09:39:16 +0100 | [diff] [blame] | 97 | static xen_pfn_t frame_list[PAGE_SIZE / sizeof(unsigned long)]; |
Stefano Stabellini | cd9151e | 2013-08-04 15:39:40 +0100 | [diff] [blame] | 98 | |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 99 | |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 100 | /* List of ballooned pages, threaded through the mem_map array. */ |
| 101 | static LIST_HEAD(ballooned_pages); |
| 102 | |
| 103 | /* Main work function, always executed in process context. */ |
| 104 | static void balloon_process(struct work_struct *work); |
Daniel Kiper | 95170b2 | 2011-03-08 22:47:39 +0100 | [diff] [blame] | 105 | static DECLARE_DELAYED_WORK(balloon_worker, balloon_process); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 106 | |
| 107 | /* When ballooning out (allocating memory to return to Xen) we don't really |
| 108 | want the kernel to try too hard since that can trigger the oom killer. */ |
| 109 | #define GFP_BALLOON \ |
| 110 | (GFP_HIGHUSER | __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC) |
| 111 | |
| 112 | static void scrub_page(struct page *page) |
| 113 | { |
| 114 | #ifdef CONFIG_XEN_SCRUB_PAGES |
Jeremy Fitzhardinge | 26a3e99 | 2008-11-17 09:35:00 -0800 | [diff] [blame] | 115 | clear_highpage(page); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 116 | #endif |
| 117 | } |
| 118 | |
| 119 | /* balloon_append: add the given page to the balloon. */ |
Jeremy Fitzhardinge | 9be4d45 | 2010-08-31 15:01:16 -0700 | [diff] [blame] | 120 | static void __balloon_append(struct page *page) |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 121 | { |
| 122 | /* Lowmem is re-populated first, so highmem pages go at list tail. */ |
| 123 | if (PageHighMem(page)) { |
| 124 | list_add_tail(&page->lru, &ballooned_pages); |
| 125 | balloon_stats.balloon_high++; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 126 | } else { |
| 127 | list_add(&page->lru, &ballooned_pages); |
| 128 | balloon_stats.balloon_low++; |
| 129 | } |
Jeremy Fitzhardinge | 9be4d45 | 2010-08-31 15:01:16 -0700 | [diff] [blame] | 130 | } |
Gianluca Guida | 3d65c94 | 2009-07-30 22:54:36 +0100 | [diff] [blame] | 131 | |
Jeremy Fitzhardinge | 9be4d45 | 2010-08-31 15:01:16 -0700 | [diff] [blame] | 132 | static void balloon_append(struct page *page) |
| 133 | { |
| 134 | __balloon_append(page); |
Jiang Liu | 3dcc057 | 2013-07-03 15:03:21 -0700 | [diff] [blame] | 135 | adjust_managed_page_count(page, -1); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /* 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] | 139 | static struct page *balloon_retrieve(bool prefer_highmem) |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 140 | { |
| 141 | struct page *page; |
| 142 | |
| 143 | if (list_empty(&ballooned_pages)) |
| 144 | return NULL; |
| 145 | |
Konrad Rzeszutek Wilk | b6f3067 | 2011-03-15 10:23:57 -0400 | [diff] [blame] | 146 | if (prefer_highmem) |
| 147 | page = list_entry(ballooned_pages.prev, struct page, lru); |
| 148 | else |
| 149 | page = list_entry(ballooned_pages.next, struct page, lru); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 150 | list_del(&page->lru); |
| 151 | |
Jiang Liu | 3dcc057 | 2013-07-03 15:03:21 -0700 | [diff] [blame] | 152 | if (PageHighMem(page)) |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 153 | balloon_stats.balloon_high--; |
Jiang Liu | 3dcc057 | 2013-07-03 15:03:21 -0700 | [diff] [blame] | 154 | else |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 155 | balloon_stats.balloon_low--; |
| 156 | |
Jiang Liu | 3dcc057 | 2013-07-03 15:03:21 -0700 | [diff] [blame] | 157 | adjust_managed_page_count(page, 1); |
Gianluca Guida | 3d65c94 | 2009-07-30 22:54:36 +0100 | [diff] [blame] | 158 | |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 159 | return page; |
| 160 | } |
| 161 | |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 162 | static struct page *balloon_next_page(struct page *page) |
| 163 | { |
| 164 | struct list_head *next = page->lru.next; |
| 165 | if (next == &ballooned_pages) |
| 166 | return NULL; |
| 167 | return list_entry(next, struct page, lru); |
| 168 | } |
| 169 | |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 170 | static enum bp_state update_schedule(enum bp_state state) |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 171 | { |
David Vrabel | b2ac6aa | 2015-06-25 12:10:28 +0100 | [diff] [blame^] | 172 | if (state == BP_WAIT) |
| 173 | return BP_WAIT; |
| 174 | |
Boris Ostrovsky | fd8b795 | 2014-10-07 17:00:07 -0400 | [diff] [blame] | 175 | if (state == BP_ECANCELED) |
| 176 | return BP_ECANCELED; |
| 177 | |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 178 | if (state == BP_DONE) { |
| 179 | balloon_stats.schedule_delay = 1; |
| 180 | balloon_stats.retry_count = 1; |
| 181 | return BP_DONE; |
| 182 | } |
| 183 | |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 184 | ++balloon_stats.retry_count; |
| 185 | |
| 186 | if (balloon_stats.max_retry_count != RETRY_UNLIMITED && |
| 187 | balloon_stats.retry_count > balloon_stats.max_retry_count) { |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 188 | balloon_stats.schedule_delay = 1; |
| 189 | balloon_stats.retry_count = 1; |
| 190 | return BP_ECANCELED; |
| 191 | } |
| 192 | |
| 193 | balloon_stats.schedule_delay <<= 1; |
| 194 | |
| 195 | if (balloon_stats.schedule_delay > balloon_stats.max_schedule_delay) |
| 196 | balloon_stats.schedule_delay = balloon_stats.max_schedule_delay; |
| 197 | |
| 198 | return BP_EAGAIN; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Daniel Kiper | 080e2be | 2011-07-25 17:12:06 -0700 | [diff] [blame] | 201 | #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG |
David Vrabel | 55b3da9 | 2015-06-24 15:58:42 +0100 | [diff] [blame] | 202 | static struct resource *additional_memory_resource(phys_addr_t size) |
| 203 | { |
| 204 | struct resource *res; |
| 205 | int ret; |
| 206 | |
| 207 | res = kzalloc(sizeof(*res), GFP_KERNEL); |
| 208 | if (!res) |
| 209 | return NULL; |
| 210 | |
| 211 | res->name = "System RAM"; |
| 212 | res->flags = IORESOURCE_MEM | IORESOURCE_BUSY; |
| 213 | |
| 214 | ret = allocate_resource(&iomem_resource, res, |
| 215 | size, 0, -1, |
| 216 | PAGES_PER_SECTION * PAGE_SIZE, NULL, NULL); |
| 217 | if (ret < 0) { |
| 218 | pr_err("Cannot allocate new System RAM resource\n"); |
| 219 | kfree(res); |
| 220 | return NULL; |
| 221 | } |
| 222 | |
| 223 | return res; |
| 224 | } |
| 225 | |
| 226 | static void release_memory_resource(struct resource *resource) |
| 227 | { |
| 228 | if (!resource) |
| 229 | return; |
| 230 | |
| 231 | /* |
| 232 | * No need to reset region to identity mapped since we now |
| 233 | * know that no I/O can be in this region |
| 234 | */ |
| 235 | release_resource(resource); |
| 236 | kfree(resource); |
| 237 | } |
Daniel Kiper | 080e2be | 2011-07-25 17:12:06 -0700 | [diff] [blame] | 238 | |
David Vrabel | b2ac6aa | 2015-06-25 12:10:28 +0100 | [diff] [blame^] | 239 | static enum bp_state reserve_additional_memory(void) |
Daniel Kiper | 080e2be | 2011-07-25 17:12:06 -0700 | [diff] [blame] | 240 | { |
David Vrabel | b2ac6aa | 2015-06-25 12:10:28 +0100 | [diff] [blame^] | 241 | long credit; |
David Vrabel | 55b3da9 | 2015-06-24 15:58:42 +0100 | [diff] [blame] | 242 | struct resource *resource; |
Daniel Kiper | 080e2be | 2011-07-25 17:12:06 -0700 | [diff] [blame] | 243 | int nid, rc; |
David Vrabel | 55b3da9 | 2015-06-24 15:58:42 +0100 | [diff] [blame] | 244 | unsigned long balloon_hotplug; |
Daniel Kiper | 080e2be | 2011-07-25 17:12:06 -0700 | [diff] [blame] | 245 | |
David Vrabel | b2ac6aa | 2015-06-25 12:10:28 +0100 | [diff] [blame^] | 246 | credit = balloon_stats.target_pages - balloon_stats.total_pages; |
| 247 | |
| 248 | /* |
| 249 | * Already hotplugged enough pages? Wait for them to be |
| 250 | * onlined. |
| 251 | */ |
| 252 | if (credit <= 0) |
| 253 | return BP_WAIT; |
| 254 | |
David Vrabel | 55b3da9 | 2015-06-24 15:58:42 +0100 | [diff] [blame] | 255 | balloon_hotplug = round_up(credit, PAGES_PER_SECTION); |
| 256 | |
| 257 | resource = additional_memory_resource(balloon_hotplug * PAGE_SIZE); |
| 258 | if (!resource) |
| 259 | goto err; |
| 260 | |
| 261 | nid = memory_add_physaddr_to_nid(resource->start); |
Daniel Kiper | 080e2be | 2011-07-25 17:12:06 -0700 | [diff] [blame] | 262 | |
Juergen Gross | 3c56b3a1 | 2015-03-20 13:55:39 +0100 | [diff] [blame] | 263 | #ifdef CONFIG_XEN_HAVE_PVMMU |
| 264 | /* |
| 265 | * add_memory() will build page tables for the new memory so |
| 266 | * the p2m must contain invalid entries so the correct |
| 267 | * non-present PTEs will be written. |
| 268 | * |
| 269 | * If a failure occurs, the original (identity) p2m entries |
| 270 | * are not restored since this region is now known not to |
| 271 | * conflict with any devices. |
| 272 | */ |
| 273 | if (!xen_feature(XENFEAT_auto_translated_physmap)) { |
| 274 | unsigned long pfn, i; |
| 275 | |
David Vrabel | 55b3da9 | 2015-06-24 15:58:42 +0100 | [diff] [blame] | 276 | pfn = PFN_DOWN(resource->start); |
Juergen Gross | 3c56b3a1 | 2015-03-20 13:55:39 +0100 | [diff] [blame] | 277 | for (i = 0; i < balloon_hotplug; i++) { |
| 278 | if (!set_phys_to_machine(pfn + i, INVALID_P2M_ENTRY)) { |
| 279 | pr_warn("set_phys_to_machine() failed, no memory added\n"); |
David Vrabel | 55b3da9 | 2015-06-24 15:58:42 +0100 | [diff] [blame] | 280 | goto err; |
Juergen Gross | 3c56b3a1 | 2015-03-20 13:55:39 +0100 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | } |
| 284 | #endif |
| 285 | |
David Vrabel | 55b3da9 | 2015-06-24 15:58:42 +0100 | [diff] [blame] | 286 | rc = add_memory_resource(nid, resource); |
Daniel Kiper | 080e2be | 2011-07-25 17:12:06 -0700 | [diff] [blame] | 287 | if (rc) { |
David Vrabel | 3dcf636 | 2014-09-01 18:52:44 +0100 | [diff] [blame] | 288 | pr_warn("Cannot add additional memory (%i)\n", rc); |
David Vrabel | 55b3da9 | 2015-06-24 15:58:42 +0100 | [diff] [blame] | 289 | goto err; |
Daniel Kiper | 080e2be | 2011-07-25 17:12:06 -0700 | [diff] [blame] | 290 | } |
| 291 | |
David Vrabel | de5a77d | 2015-06-25 12:08:20 +0100 | [diff] [blame] | 292 | balloon_stats.total_pages += balloon_hotplug; |
Daniel Kiper | 080e2be | 2011-07-25 17:12:06 -0700 | [diff] [blame] | 293 | |
David Vrabel | b2ac6aa | 2015-06-25 12:10:28 +0100 | [diff] [blame^] | 294 | return BP_WAIT; |
David Vrabel | 55b3da9 | 2015-06-24 15:58:42 +0100 | [diff] [blame] | 295 | err: |
| 296 | release_memory_resource(resource); |
| 297 | return BP_ECANCELED; |
Daniel Kiper | 080e2be | 2011-07-25 17:12:06 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | static void xen_online_page(struct page *page) |
| 301 | { |
| 302 | __online_page_set_limits(page); |
| 303 | |
| 304 | mutex_lock(&balloon_mutex); |
| 305 | |
| 306 | __balloon_append(page); |
| 307 | |
Daniel Kiper | 080e2be | 2011-07-25 17:12:06 -0700 | [diff] [blame] | 308 | mutex_unlock(&balloon_mutex); |
| 309 | } |
| 310 | |
| 311 | static int xen_memory_notifier(struct notifier_block *nb, unsigned long val, void *v) |
| 312 | { |
| 313 | if (val == MEM_ONLINE) |
| 314 | schedule_delayed_work(&balloon_worker, 0); |
| 315 | |
| 316 | return NOTIFY_OK; |
| 317 | } |
| 318 | |
| 319 | static struct notifier_block xen_memory_nb = { |
| 320 | .notifier_call = xen_memory_notifier, |
| 321 | .priority = 0 |
| 322 | }; |
| 323 | #else |
David Vrabel | b2ac6aa | 2015-06-25 12:10:28 +0100 | [diff] [blame^] | 324 | static enum bp_state reserve_additional_memory(void) |
Daniel Kiper | 080e2be | 2011-07-25 17:12:06 -0700 | [diff] [blame] | 325 | { |
| 326 | balloon_stats.target_pages = balloon_stats.current_pages; |
| 327 | return BP_DONE; |
| 328 | } |
| 329 | #endif /* CONFIG_XEN_BALLOON_MEMORY_HOTPLUG */ |
| 330 | |
David Vrabel | de5a77d | 2015-06-25 12:08:20 +0100 | [diff] [blame] | 331 | static long current_credit(void) |
| 332 | { |
| 333 | return balloon_stats.target_pages - balloon_stats.current_pages; |
| 334 | } |
| 335 | |
| 336 | static bool balloon_is_inflated(void) |
| 337 | { |
| 338 | return balloon_stats.balloon_low || balloon_stats.balloon_high; |
| 339 | } |
| 340 | |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 341 | static enum bp_state increase_reservation(unsigned long nr_pages) |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 342 | { |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 343 | int rc; |
Jeremy Fitzhardinge | 2f70e0a | 2010-09-02 23:11:17 -0700 | [diff] [blame] | 344 | unsigned long pfn, i; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 345 | struct page *page; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 346 | struct xen_memory_reservation reservation = { |
| 347 | .address_bits = 0, |
| 348 | .extent_order = 0, |
| 349 | .domid = DOMID_SELF |
| 350 | }; |
| 351 | |
| 352 | if (nr_pages > ARRAY_SIZE(frame_list)) |
| 353 | nr_pages = ARRAY_SIZE(frame_list); |
| 354 | |
Jie Liu | 9346c2a | 2013-11-13 20:59:58 +0800 | [diff] [blame] | 355 | page = list_first_entry_or_null(&ballooned_pages, struct page, lru); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 356 | for (i = 0; i < nr_pages; i++) { |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 357 | if (!page) { |
| 358 | nr_pages = i; |
| 359 | break; |
| 360 | } |
Joe Perches | a419aef | 2009-08-18 11:18:35 -0700 | [diff] [blame] | 361 | frame_list[i] = page_to_pfn(page); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 362 | page = balloon_next_page(page); |
| 363 | } |
| 364 | |
Isaku Yamahata | a90971e | 2008-05-26 23:31:14 +0100 | [diff] [blame] | 365 | set_xen_guest_handle(reservation.extent_start, frame_list); |
Jeremy Fitzhardinge | fde28e8 | 2008-07-24 16:28:00 -0700 | [diff] [blame] | 366 | reservation.nr_extents = nr_pages; |
| 367 | rc = HYPERVISOR_memory_op(XENMEM_populate_physmap, &reservation); |
Konrad Rzeszutek Wilk | 40095de | 2011-03-14 11:42:40 -0400 | [diff] [blame] | 368 | if (rc <= 0) |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 369 | return BP_EAGAIN; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 370 | |
Ian Campbell | bc2c030 | 2009-06-05 11:58:37 +0100 | [diff] [blame] | 371 | for (i = 0; i < rc; i++) { |
Konrad Rzeszutek Wilk | b6f3067 | 2011-03-15 10:23:57 -0400 | [diff] [blame] | 372 | page = balloon_retrieve(false); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 373 | BUG_ON(page == NULL); |
| 374 | |
| 375 | pfn = page_to_pfn(page); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 376 | |
Ian Campbell | c2374bf | 2012-10-03 12:17:50 +0100 | [diff] [blame] | 377 | #ifdef CONFIG_XEN_HAVE_PVMMU |
Stefano Stabellini | c1d15f5 | 2013-12-11 16:58:42 +0000 | [diff] [blame] | 378 | if (!xen_feature(XENFEAT_auto_translated_physmap)) { |
| 379 | set_phys_to_machine(pfn, frame_list[i]); |
| 380 | |
| 381 | /* Link back into the page tables if not highmem. */ |
| 382 | if (!PageHighMem(page)) { |
| 383 | int ret; |
| 384 | ret = HYPERVISOR_update_va_mapping( |
| 385 | (unsigned long)__va(pfn << PAGE_SHIFT), |
| 386 | mfn_pte(frame_list[i], PAGE_KERNEL), |
| 387 | 0); |
| 388 | BUG_ON(ret); |
| 389 | } |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 390 | } |
Ian Campbell | c2374bf | 2012-10-03 12:17:50 +0100 | [diff] [blame] | 391 | #endif |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 392 | |
| 393 | /* Relinquish the page back to the allocator. */ |
Jiang Liu | 3dcc057 | 2013-07-03 15:03:21 -0700 | [diff] [blame] | 394 | __free_reserved_page(page); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Ian Campbell | bc2c030 | 2009-06-05 11:58:37 +0100 | [diff] [blame] | 397 | balloon_stats.current_pages += rc; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 398 | |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 399 | return BP_DONE; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Konrad Rzeszutek Wilk | b6f3067 | 2011-03-15 10:23:57 -0400 | [diff] [blame] | 402 | 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] | 403 | { |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 404 | enum bp_state state = BP_DONE; |
Jeremy Fitzhardinge | 2f70e0a | 2010-09-02 23:11:17 -0700 | [diff] [blame] | 405 | unsigned long pfn, i; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 406 | struct page *page; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 407 | int ret; |
| 408 | struct xen_memory_reservation reservation = { |
| 409 | .address_bits = 0, |
| 410 | .extent_order = 0, |
| 411 | .domid = DOMID_SELF |
| 412 | }; |
| 413 | |
| 414 | if (nr_pages > ARRAY_SIZE(frame_list)) |
| 415 | nr_pages = ARRAY_SIZE(frame_list); |
| 416 | |
| 417 | for (i = 0; i < nr_pages; i++) { |
Lisa Nguyen | fce9268 | 2013-05-15 22:59:40 -0700 | [diff] [blame] | 418 | page = alloc_page(gfp); |
| 419 | if (page == NULL) { |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 420 | nr_pages = i; |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 421 | state = BP_EAGAIN; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 422 | break; |
| 423 | } |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 424 | scrub_page(page); |
Dan Magenheimer | 1058a75 | 2009-01-22 14:36:08 -0800 | [diff] [blame] | 425 | |
Wei Liu | 09ed3d5 | 2014-03-15 16:11:47 +0000 | [diff] [blame] | 426 | frame_list[i] = page_to_pfn(page); |
| 427 | } |
| 428 | |
| 429 | /* |
| 430 | * Ensure that ballooned highmem pages don't have kmaps. |
| 431 | * |
| 432 | * Do this before changing the p2m as kmap_flush_unused() |
| 433 | * reads PTEs to obtain pages (and hence needs the original |
| 434 | * p2m entry). |
| 435 | */ |
| 436 | kmap_flush_unused(); |
| 437 | |
| 438 | /* Update direct mapping, invalidate P2M, and add to balloon. */ |
| 439 | for (i = 0; i < nr_pages; i++) { |
| 440 | pfn = frame_list[i]; |
Julien Grall | 0df4f26 | 2015-08-07 17:34:37 +0100 | [diff] [blame] | 441 | frame_list[i] = pfn_to_gfn(pfn); |
Wei Liu | 09ed3d5 | 2014-03-15 16:11:47 +0000 | [diff] [blame] | 442 | page = pfn_to_page(pfn); |
| 443 | |
Stefano Stabellini | c1d15f5 | 2013-12-11 16:58:42 +0000 | [diff] [blame] | 444 | #ifdef CONFIG_XEN_HAVE_PVMMU |
Wei Liu | 04660bb | 2013-08-27 16:17:25 +0100 | [diff] [blame] | 445 | if (!xen_feature(XENFEAT_auto_translated_physmap)) { |
Stefano Stabellini | c1d15f5 | 2013-12-11 16:58:42 +0000 | [diff] [blame] | 446 | if (!PageHighMem(page)) { |
| 447 | ret = HYPERVISOR_update_va_mapping( |
| 448 | (unsigned long)__va(pfn << PAGE_SHIFT), |
David Vrabel | 0bb599f | 2015-01-05 17:06:01 +0000 | [diff] [blame] | 449 | __pte_ma(0), 0); |
Stefano Stabellini | c1d15f5 | 2013-12-11 16:58:42 +0000 | [diff] [blame] | 450 | BUG_ON(ret); |
David Vrabel | fb9a0c4 | 2014-06-27 10:42:03 +0100 | [diff] [blame] | 451 | } |
| 452 | __set_phys_to_machine(pfn, INVALID_P2M_ENTRY); |
Wei Liu | 04660bb | 2013-08-27 16:17:25 +0100 | [diff] [blame] | 453 | } |
Stefano Stabellini | c1d15f5 | 2013-12-11 16:58:42 +0000 | [diff] [blame] | 454 | #endif |
David Vrabel | 24f6937 | 2013-09-19 17:14:53 +0100 | [diff] [blame] | 455 | |
Wei Liu | 09ed3d5 | 2014-03-15 16:11:47 +0000 | [diff] [blame] | 456 | balloon_append(page); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 457 | } |
| 458 | |
David Vrabel | 24f6937 | 2013-09-19 17:14:53 +0100 | [diff] [blame] | 459 | flush_tlb_all(); |
David Vrabel | 2bad07c | 2013-09-11 17:45:44 +0000 | [diff] [blame] | 460 | |
Isaku Yamahata | a90971e | 2008-05-26 23:31:14 +0100 | [diff] [blame] | 461 | set_xen_guest_handle(reservation.extent_start, frame_list); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 462 | reservation.nr_extents = nr_pages; |
| 463 | ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation); |
| 464 | BUG_ON(ret != nr_pages); |
| 465 | |
| 466 | balloon_stats.current_pages -= nr_pages; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 467 | |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 468 | return state; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | /* |
Juergen Gross | 929423f | 2015-07-20 13:49:39 +0200 | [diff] [blame] | 472 | * As this is a work item it is guaranteed to run as a single instance only. |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 473 | * We may of course race updates of the target counts (which are protected |
| 474 | * by the balloon lock), or with changes to the Xen hard limit, but we will |
| 475 | * recover from these in time. |
| 476 | */ |
| 477 | static void balloon_process(struct work_struct *work) |
| 478 | { |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 479 | enum bp_state state = BP_DONE; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 480 | long credit; |
| 481 | |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 482 | |
| 483 | do { |
Juergen Gross | 929423f | 2015-07-20 13:49:39 +0200 | [diff] [blame] | 484 | mutex_lock(&balloon_mutex); |
| 485 | |
Daniel Kiper | 83be7e5 | 2011-03-28 11:34:10 +0200 | [diff] [blame] | 486 | credit = current_credit(); |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 487 | |
Daniel Kiper | 080e2be | 2011-07-25 17:12:06 -0700 | [diff] [blame] | 488 | if (credit > 0) { |
| 489 | if (balloon_is_inflated()) |
| 490 | state = increase_reservation(credit); |
| 491 | else |
David Vrabel | b2ac6aa | 2015-06-25 12:10:28 +0100 | [diff] [blame^] | 492 | state = reserve_additional_memory(); |
Daniel Kiper | 080e2be | 2011-07-25 17:12:06 -0700 | [diff] [blame] | 493 | } |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 494 | |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 495 | if (credit < 0) |
Konrad Rzeszutek Wilk | b6f3067 | 2011-03-15 10:23:57 -0400 | [diff] [blame] | 496 | state = decrease_reservation(-credit, GFP_BALLOON); |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 497 | |
| 498 | state = update_schedule(state); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 499 | |
Juergen Gross | 929423f | 2015-07-20 13:49:39 +0200 | [diff] [blame] | 500 | mutex_unlock(&balloon_mutex); |
| 501 | |
| 502 | cond_resched(); |
| 503 | |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 504 | } while (credit && state == BP_DONE); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 505 | |
| 506 | /* Schedule more work if there is some still to be done. */ |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 507 | if (state == BP_EAGAIN) |
| 508 | schedule_delayed_work(&balloon_worker, balloon_stats.schedule_delay * HZ); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | /* Resets the Xen limit, sets new target, and kicks off processing. */ |
Daniel De Graaf | 803eb04 | 2011-03-14 11:29:37 -0400 | [diff] [blame] | 512 | void balloon_set_new_target(unsigned long target) |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 513 | { |
| 514 | /* No need for lock. Not read-modify-write updates. */ |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 515 | balloon_stats.target_pages = target; |
Daniel Kiper | 95170b2 | 2011-03-08 22:47:39 +0100 | [diff] [blame] | 516 | schedule_delayed_work(&balloon_worker, 0); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 517 | } |
Daniel De Graaf | 803eb04 | 2011-03-14 11:29:37 -0400 | [diff] [blame] | 518 | EXPORT_SYMBOL_GPL(balloon_set_new_target); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 519 | |
Konrad Rzeszutek Wilk | b6f3067 | 2011-03-15 10:23:57 -0400 | [diff] [blame] | 520 | /** |
| 521 | * alloc_xenballooned_pages - get pages that have been ballooned out |
| 522 | * @nr_pages: Number of pages to get |
| 523 | * @pages: pages returned |
Daniel De Graaf | 72e9cf2 | 2011-10-19 17:59:37 -0400 | [diff] [blame] | 524 | * @highmem: allow highmem pages |
Konrad Rzeszutek Wilk | b6f3067 | 2011-03-15 10:23:57 -0400 | [diff] [blame] | 525 | * @return 0 on success, error otherwise |
| 526 | */ |
Stefano Stabellini | 693394b | 2011-09-29 11:57:55 +0100 | [diff] [blame] | 527 | int alloc_xenballooned_pages(int nr_pages, struct page **pages, bool highmem) |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 528 | { |
Konrad Rzeszutek Wilk | b6f3067 | 2011-03-15 10:23:57 -0400 | [diff] [blame] | 529 | int pgno = 0; |
Ruslan Pisarev | e882dc9 | 2011-07-26 14:15:59 +0300 | [diff] [blame] | 530 | struct page *page; |
Konrad Rzeszutek Wilk | b6f3067 | 2011-03-15 10:23:57 -0400 | [diff] [blame] | 531 | mutex_lock(&balloon_mutex); |
| 532 | while (pgno < nr_pages) { |
Stefano Stabellini | 693394b | 2011-09-29 11:57:55 +0100 | [diff] [blame] | 533 | page = balloon_retrieve(highmem); |
Daniel De Graaf | 72e9cf2 | 2011-10-19 17:59:37 -0400 | [diff] [blame] | 534 | if (page && (highmem || !PageHighMem(page))) { |
Konrad Rzeszutek Wilk | b6f3067 | 2011-03-15 10:23:57 -0400 | [diff] [blame] | 535 | pages[pgno++] = page; |
| 536 | } else { |
| 537 | enum bp_state st; |
Stefano Stabellini | 693394b | 2011-09-29 11:57:55 +0100 | [diff] [blame] | 538 | if (page) |
| 539 | balloon_append(page); |
| 540 | st = decrease_reservation(nr_pages - pgno, |
| 541 | highmem ? GFP_HIGHUSER : GFP_USER); |
Konrad Rzeszutek Wilk | b6f3067 | 2011-03-15 10:23:57 -0400 | [diff] [blame] | 542 | if (st != BP_DONE) |
| 543 | goto out_undo; |
| 544 | } |
| 545 | } |
| 546 | mutex_unlock(&balloon_mutex); |
| 547 | return 0; |
| 548 | out_undo: |
| 549 | while (pgno) |
| 550 | balloon_append(pages[--pgno]); |
| 551 | /* Free the memory back to the kernel soon */ |
| 552 | schedule_delayed_work(&balloon_worker, 0); |
| 553 | mutex_unlock(&balloon_mutex); |
| 554 | return -ENOMEM; |
| 555 | } |
| 556 | EXPORT_SYMBOL(alloc_xenballooned_pages); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 557 | |
Konrad Rzeszutek Wilk | b6f3067 | 2011-03-15 10:23:57 -0400 | [diff] [blame] | 558 | /** |
| 559 | * free_xenballooned_pages - return pages retrieved with get_ballooned_pages |
| 560 | * @nr_pages: Number of pages |
| 561 | * @pages: pages to return |
| 562 | */ |
Ruslan Pisarev | e882dc9 | 2011-07-26 14:15:59 +0300 | [diff] [blame] | 563 | void free_xenballooned_pages(int nr_pages, struct page **pages) |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 564 | { |
Konrad Rzeszutek Wilk | b6f3067 | 2011-03-15 10:23:57 -0400 | [diff] [blame] | 565 | int i; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 566 | |
Konrad Rzeszutek Wilk | b6f3067 | 2011-03-15 10:23:57 -0400 | [diff] [blame] | 567 | mutex_lock(&balloon_mutex); |
| 568 | |
| 569 | for (i = 0; i < nr_pages; i++) { |
| 570 | if (pages[i]) |
| 571 | balloon_append(pages[i]); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Konrad Rzeszutek Wilk | b6f3067 | 2011-03-15 10:23:57 -0400 | [diff] [blame] | 574 | /* The balloon may be too large now. Shrink it if needed. */ |
Daniel Kiper | 83be7e5 | 2011-03-28 11:34:10 +0200 | [diff] [blame] | 575 | if (current_credit()) |
Konrad Rzeszutek Wilk | b6f3067 | 2011-03-15 10:23:57 -0400 | [diff] [blame] | 576 | schedule_delayed_work(&balloon_worker, 0); |
| 577 | |
| 578 | mutex_unlock(&balloon_mutex); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 579 | } |
Konrad Rzeszutek Wilk | b6f3067 | 2011-03-15 10:23:57 -0400 | [diff] [blame] | 580 | EXPORT_SYMBOL(free_xenballooned_pages); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 581 | |
David Vrabel | 8b5d44a | 2011-09-28 17:46:34 +0100 | [diff] [blame] | 582 | static void __init balloon_add_region(unsigned long start_pfn, |
| 583 | unsigned long pages) |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 584 | { |
Daniel Kiper | 4dfe22f | 2011-03-28 11:33:18 +0200 | [diff] [blame] | 585 | unsigned long pfn, extra_pfn_end; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 586 | struct page *page; |
| 587 | |
David Vrabel | 8b5d44a | 2011-09-28 17:46:34 +0100 | [diff] [blame] | 588 | /* |
| 589 | * If the amount of usable memory has been limited (e.g., with |
| 590 | * the 'mem' command line parameter), don't add pages beyond |
| 591 | * this limit. |
| 592 | */ |
| 593 | extra_pfn_end = min(max_pfn, start_pfn + pages); |
| 594 | |
| 595 | for (pfn = start_pfn; pfn < extra_pfn_end; pfn++) { |
| 596 | page = pfn_to_page(pfn); |
| 597 | /* totalram_pages and totalhigh_pages do not |
| 598 | include the boot-time balloon extension, so |
| 599 | don't subtract from it. */ |
| 600 | __balloon_append(page); |
| 601 | } |
David Vrabel | de5a77d | 2015-06-25 12:08:20 +0100 | [diff] [blame] | 602 | |
| 603 | balloon_stats.total_pages += extra_pfn_end - start_pfn; |
David Vrabel | 8b5d44a | 2011-09-28 17:46:34 +0100 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | static int __init balloon_init(void) |
| 607 | { |
David Vrabel | 0bb599f | 2015-01-05 17:06:01 +0000 | [diff] [blame] | 608 | int i; |
David Vrabel | 8b5d44a | 2011-09-28 17:46:34 +0100 | [diff] [blame] | 609 | |
Stefano Stabellini | 53d5522 | 2010-12-02 17:55:05 +0000 | [diff] [blame] | 610 | if (!xen_domain()) |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 611 | return -ENODEV; |
| 612 | |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 613 | pr_info("Initialising balloon driver\n"); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 614 | |
David Vrabel | aa24411 | 2011-09-28 17:46:32 +0100 | [diff] [blame] | 615 | balloon_stats.current_pages = xen_pv_domain() |
| 616 | ? min(xen_start_info->nr_pages - xen_released_pages, max_pfn) |
Boris Ostrovsky | c275a57 | 2013-11-06 15:37:40 -0500 | [diff] [blame] | 617 | : get_num_physpages(); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 618 | balloon_stats.target_pages = balloon_stats.current_pages; |
| 619 | balloon_stats.balloon_low = 0; |
| 620 | balloon_stats.balloon_high = 0; |
David Vrabel | de5a77d | 2015-06-25 12:08:20 +0100 | [diff] [blame] | 621 | balloon_stats.total_pages = balloon_stats.current_pages; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 622 | |
Daniel Kiper | 95d2ac4 | 2011-03-08 22:48:24 +0100 | [diff] [blame] | 623 | balloon_stats.schedule_delay = 1; |
| 624 | balloon_stats.max_schedule_delay = 32; |
| 625 | balloon_stats.retry_count = 1; |
Konrad Rzeszutek Wilk | 40095de | 2011-03-14 11:42:40 -0400 | [diff] [blame] | 626 | balloon_stats.max_retry_count = RETRY_UNLIMITED; |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 627 | |
Daniel Kiper | 080e2be | 2011-07-25 17:12:06 -0700 | [diff] [blame] | 628 | #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG |
Daniel Kiper | 080e2be | 2011-07-25 17:12:06 -0700 | [diff] [blame] | 629 | set_online_page_callback(&xen_online_page); |
| 630 | register_memory_notifier(&xen_memory_nb); |
| 631 | #endif |
| 632 | |
Jeremy Fitzhardinge | 2a4c92f | 2010-12-02 15:30:06 -0800 | [diff] [blame] | 633 | /* |
David Vrabel | b1cbf9b | 2011-09-28 17:46:33 +0100 | [diff] [blame] | 634 | * Initialize the balloon with pages from the extra memory |
David Vrabel | 8b5d44a | 2011-09-28 17:46:34 +0100 | [diff] [blame] | 635 | * regions (see arch/x86/xen/setup.c). |
Jeremy Fitzhardinge | 2a4c92f | 2010-12-02 15:30:06 -0800 | [diff] [blame] | 636 | */ |
David Vrabel | 8b5d44a | 2011-09-28 17:46:34 +0100 | [diff] [blame] | 637 | for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) |
Juergen Gross | 626d750 | 2015-09-04 14:05:51 +0200 | [diff] [blame] | 638 | if (xen_extra_mem[i].n_pfns) |
| 639 | balloon_add_region(xen_extra_mem[i].start_pfn, |
| 640 | xen_extra_mem[i].n_pfns); |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 641 | |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | subsys_initcall(balloon_init); |
| 646 | |
Jeremy Fitzhardinge | 1775826 | 2008-04-02 10:54:13 -0700 | [diff] [blame] | 647 | MODULE_LICENSE("GPL"); |