blob: ac6391bd8029c5836906f9cd8be1ecb2b6061896 [file] [log] [blame]
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -07001/******************************************************************************
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -07002 * 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 Kiper080e2be2011-07-25 17:12:06 -07007 * 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 Fitzhardinge17758262008-04-02 10:54:13 -070013 *
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 Perches283c0972013-06-28 03:21:41 -070039#define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
40
Stefano Stabellinicd9151e2013-08-04 15:39:40 +010041#include <linux/cpu.h>
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070042#include <linux/kernel.h>
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070043#include <linux/sched.h>
44#include <linux/errno.h>
Paul Gortmaker72ee5112011-07-03 16:20:57 -040045#include <linux/module.h>
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070046#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 Fitzhardinge17758262008-04-02 10:54:13 -070051#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090052#include <linux/gfp.h>
Daniel Kiper080e2be2011-07-25 17:12:06 -070053#include <linux/notifier.h>
54#include <linux/memory.h>
55#include <linux/memory_hotplug.h>
Stefano Stabellinicd9151e2013-08-04 15:39:40 +010056#include <linux/percpu-defs.h>
David Vrabel55b3da92015-06-24 15:58:42 +010057#include <linux/slab.h>
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070058
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070059#include <asm/page.h>
60#include <asm/pgalloc.h>
61#include <asm/pgtable.h>
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070062#include <asm/tlb.h>
63
Jeremy Fitzhardingeecbf29c2008-12-16 12:37:07 -080064#include <asm/xen/hypervisor.h>
65#include <asm/xen/hypercall.h>
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070066
67#include <xen/xen.h>
Jeremy Fitzhardingeecbf29c2008-12-16 12:37:07 -080068#include <xen/interface/xen.h>
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070069#include <xen/interface/memory.h>
Daniel De Graaf803eb042011-03-14 11:29:37 -040070#include <xen/balloon.h>
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070071#include <xen/features.h>
72#include <xen/page.h>
73
Daniel Kiper95d2ac42011-03-08 22:48:24 +010074/*
75 * balloon_process() state:
76 *
77 * BP_DONE: done or nothing to do,
David Vrabelb2ac6aa2015-06-25 12:10:28 +010078 * BP_WAIT: wait to be rescheduled,
Daniel Kiper95d2ac42011-03-08 22:48:24 +010079 * BP_EAGAIN: error, go to sleep,
80 * BP_ECANCELED: error, balloon operation canceled.
81 */
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070082
Daniel Kiper95d2ac42011-03-08 22:48:24 +010083enum bp_state {
84 BP_DONE,
David Vrabelb2ac6aa2015-06-25 12:10:28 +010085 BP_WAIT,
Daniel Kiper95d2ac42011-03-08 22:48:24 +010086 BP_EAGAIN,
87 BP_ECANCELED
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070088};
89
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070090
91static DEFINE_MUTEX(balloon_mutex);
92
Daniel De Graaf803eb042011-03-14 11:29:37 -040093struct balloon_stats balloon_stats;
94EXPORT_SYMBOL_GPL(balloon_stats);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070095
96/* We increase/decrease in batches which fit in a page */
Ian Campbell965c0aa2012-10-17 09:39:16 +010097static xen_pfn_t frame_list[PAGE_SIZE / sizeof(unsigned long)];
Stefano Stabellinicd9151e2013-08-04 15:39:40 +010098
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070099
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700100/* List of ballooned pages, threaded through the mem_map array. */
101static LIST_HEAD(ballooned_pages);
102
103/* Main work function, always executed in process context. */
104static void balloon_process(struct work_struct *work);
Daniel Kiper95170b22011-03-08 22:47:39 +0100105static DECLARE_DELAYED_WORK(balloon_worker, balloon_process);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700106
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
112static void scrub_page(struct page *page)
113{
114#ifdef CONFIG_XEN_SCRUB_PAGES
Jeremy Fitzhardinge26a3e992008-11-17 09:35:00 -0800115 clear_highpage(page);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700116#endif
117}
118
119/* balloon_append: add the given page to the balloon. */
Jeremy Fitzhardinge9be4d452010-08-31 15:01:16 -0700120static void __balloon_append(struct page *page)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700121{
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 Fitzhardinge17758262008-04-02 10:54:13 -0700126 } else {
127 list_add(&page->lru, &ballooned_pages);
128 balloon_stats.balloon_low++;
129 }
Jeremy Fitzhardinge9be4d452010-08-31 15:01:16 -0700130}
Gianluca Guida3d65c942009-07-30 22:54:36 +0100131
Jeremy Fitzhardinge9be4d452010-08-31 15:01:16 -0700132static void balloon_append(struct page *page)
133{
134 __balloon_append(page);
Jiang Liu3dcc0572013-07-03 15:03:21 -0700135 adjust_managed_page_count(page, -1);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700136}
137
138/* balloon_retrieve: rescue a page from the balloon, if it is not empty. */
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400139static struct page *balloon_retrieve(bool prefer_highmem)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700140{
141 struct page *page;
142
143 if (list_empty(&ballooned_pages))
144 return NULL;
145
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400146 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 Fitzhardinge17758262008-04-02 10:54:13 -0700150 list_del(&page->lru);
151
Jiang Liu3dcc0572013-07-03 15:03:21 -0700152 if (PageHighMem(page))
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700153 balloon_stats.balloon_high--;
Jiang Liu3dcc0572013-07-03 15:03:21 -0700154 else
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700155 balloon_stats.balloon_low--;
156
Jiang Liu3dcc0572013-07-03 15:03:21 -0700157 adjust_managed_page_count(page, 1);
Gianluca Guida3d65c942009-07-30 22:54:36 +0100158
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700159 return page;
160}
161
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700162static 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 Kiper95d2ac42011-03-08 22:48:24 +0100170static enum bp_state update_schedule(enum bp_state state)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700171{
David Vrabelb2ac6aa2015-06-25 12:10:28 +0100172 if (state == BP_WAIT)
173 return BP_WAIT;
174
Boris Ostrovskyfd8b7952014-10-07 17:00:07 -0400175 if (state == BP_ECANCELED)
176 return BP_ECANCELED;
177
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100178 if (state == BP_DONE) {
179 balloon_stats.schedule_delay = 1;
180 balloon_stats.retry_count = 1;
181 return BP_DONE;
182 }
183
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100184 ++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 Kiper95d2ac42011-03-08 22:48:24 +0100188 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 Fitzhardinge17758262008-04-02 10:54:13 -0700199}
200
Daniel Kiper080e2be2011-07-25 17:12:06 -0700201#ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
David Vrabel55b3da92015-06-24 15:58:42 +0100202static 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
226static 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 Kiper080e2be2011-07-25 17:12:06 -0700238
David Vrabelb2ac6aa2015-06-25 12:10:28 +0100239static enum bp_state reserve_additional_memory(void)
Daniel Kiper080e2be2011-07-25 17:12:06 -0700240{
David Vrabelb2ac6aa2015-06-25 12:10:28 +0100241 long credit;
David Vrabel55b3da92015-06-24 15:58:42 +0100242 struct resource *resource;
Daniel Kiper080e2be2011-07-25 17:12:06 -0700243 int nid, rc;
David Vrabel55b3da92015-06-24 15:58:42 +0100244 unsigned long balloon_hotplug;
Daniel Kiper080e2be2011-07-25 17:12:06 -0700245
David Vrabelb2ac6aa2015-06-25 12:10:28 +0100246 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 Vrabel55b3da92015-06-24 15:58:42 +0100255 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 Kiper080e2be2011-07-25 17:12:06 -0700262
Juergen Gross3c56b3a12015-03-20 13:55:39 +0100263#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 Vrabel55b3da92015-06-24 15:58:42 +0100276 pfn = PFN_DOWN(resource->start);
Juergen Gross3c56b3a12015-03-20 13:55:39 +0100277 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 Vrabel55b3da92015-06-24 15:58:42 +0100280 goto err;
Juergen Gross3c56b3a12015-03-20 13:55:39 +0100281 }
282 }
283 }
284#endif
285
David Vrabel55b3da92015-06-24 15:58:42 +0100286 rc = add_memory_resource(nid, resource);
Daniel Kiper080e2be2011-07-25 17:12:06 -0700287 if (rc) {
David Vrabel3dcf6362014-09-01 18:52:44 +0100288 pr_warn("Cannot add additional memory (%i)\n", rc);
David Vrabel55b3da92015-06-24 15:58:42 +0100289 goto err;
Daniel Kiper080e2be2011-07-25 17:12:06 -0700290 }
291
David Vrabelde5a77d2015-06-25 12:08:20 +0100292 balloon_stats.total_pages += balloon_hotplug;
Daniel Kiper080e2be2011-07-25 17:12:06 -0700293
David Vrabelb2ac6aa2015-06-25 12:10:28 +0100294 return BP_WAIT;
David Vrabel55b3da92015-06-24 15:58:42 +0100295 err:
296 release_memory_resource(resource);
297 return BP_ECANCELED;
Daniel Kiper080e2be2011-07-25 17:12:06 -0700298}
299
300static 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 Kiper080e2be2011-07-25 17:12:06 -0700308 mutex_unlock(&balloon_mutex);
309}
310
311static 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
319static struct notifier_block xen_memory_nb = {
320 .notifier_call = xen_memory_notifier,
321 .priority = 0
322};
323#else
David Vrabelb2ac6aa2015-06-25 12:10:28 +0100324static enum bp_state reserve_additional_memory(void)
Daniel Kiper080e2be2011-07-25 17:12:06 -0700325{
326 balloon_stats.target_pages = balloon_stats.current_pages;
327 return BP_DONE;
328}
329#endif /* CONFIG_XEN_BALLOON_MEMORY_HOTPLUG */
330
David Vrabelde5a77d2015-06-25 12:08:20 +0100331static long current_credit(void)
332{
333 return balloon_stats.target_pages - balloon_stats.current_pages;
334}
335
336static bool balloon_is_inflated(void)
337{
338 return balloon_stats.balloon_low || balloon_stats.balloon_high;
339}
340
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100341static enum bp_state increase_reservation(unsigned long nr_pages)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700342{
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100343 int rc;
Jeremy Fitzhardinge2f70e0a2010-09-02 23:11:17 -0700344 unsigned long pfn, i;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700345 struct page *page;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700346 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 Liu9346c2a2013-11-13 20:59:58 +0800355 page = list_first_entry_or_null(&ballooned_pages, struct page, lru);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700356 for (i = 0; i < nr_pages; i++) {
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100357 if (!page) {
358 nr_pages = i;
359 break;
360 }
Joe Perchesa419aef2009-08-18 11:18:35 -0700361 frame_list[i] = page_to_pfn(page);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700362 page = balloon_next_page(page);
363 }
364
Isaku Yamahataa90971e2008-05-26 23:31:14 +0100365 set_xen_guest_handle(reservation.extent_start, frame_list);
Jeremy Fitzhardingefde28e82008-07-24 16:28:00 -0700366 reservation.nr_extents = nr_pages;
367 rc = HYPERVISOR_memory_op(XENMEM_populate_physmap, &reservation);
Konrad Rzeszutek Wilk40095de2011-03-14 11:42:40 -0400368 if (rc <= 0)
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100369 return BP_EAGAIN;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700370
Ian Campbellbc2c0302009-06-05 11:58:37 +0100371 for (i = 0; i < rc; i++) {
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400372 page = balloon_retrieve(false);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700373 BUG_ON(page == NULL);
374
375 pfn = page_to_pfn(page);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700376
Ian Campbellc2374bf2012-10-03 12:17:50 +0100377#ifdef CONFIG_XEN_HAVE_PVMMU
Stefano Stabellinic1d15f52013-12-11 16:58:42 +0000378 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 Fitzhardinge17758262008-04-02 10:54:13 -0700390 }
Ian Campbellc2374bf2012-10-03 12:17:50 +0100391#endif
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700392
393 /* Relinquish the page back to the allocator. */
Jiang Liu3dcc0572013-07-03 15:03:21 -0700394 __free_reserved_page(page);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700395 }
396
Ian Campbellbc2c0302009-06-05 11:58:37 +0100397 balloon_stats.current_pages += rc;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700398
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100399 return BP_DONE;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700400}
401
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400402static enum bp_state decrease_reservation(unsigned long nr_pages, gfp_t gfp)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700403{
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100404 enum bp_state state = BP_DONE;
Jeremy Fitzhardinge2f70e0a2010-09-02 23:11:17 -0700405 unsigned long pfn, i;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700406 struct page *page;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700407 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 Nguyenfce92682013-05-15 22:59:40 -0700418 page = alloc_page(gfp);
419 if (page == NULL) {
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700420 nr_pages = i;
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100421 state = BP_EAGAIN;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700422 break;
423 }
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700424 scrub_page(page);
Dan Magenheimer1058a752009-01-22 14:36:08 -0800425
Wei Liu09ed3d52014-03-15 16:11:47 +0000426 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 Grall0df4f262015-08-07 17:34:37 +0100441 frame_list[i] = pfn_to_gfn(pfn);
Wei Liu09ed3d52014-03-15 16:11:47 +0000442 page = pfn_to_page(pfn);
443
Stefano Stabellinic1d15f52013-12-11 16:58:42 +0000444#ifdef CONFIG_XEN_HAVE_PVMMU
Wei Liu04660bb2013-08-27 16:17:25 +0100445 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
Stefano Stabellinic1d15f52013-12-11 16:58:42 +0000446 if (!PageHighMem(page)) {
447 ret = HYPERVISOR_update_va_mapping(
448 (unsigned long)__va(pfn << PAGE_SHIFT),
David Vrabel0bb599f2015-01-05 17:06:01 +0000449 __pte_ma(0), 0);
Stefano Stabellinic1d15f52013-12-11 16:58:42 +0000450 BUG_ON(ret);
David Vrabelfb9a0c42014-06-27 10:42:03 +0100451 }
452 __set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
Wei Liu04660bb2013-08-27 16:17:25 +0100453 }
Stefano Stabellinic1d15f52013-12-11 16:58:42 +0000454#endif
David Vrabel24f69372013-09-19 17:14:53 +0100455
Wei Liu09ed3d52014-03-15 16:11:47 +0000456 balloon_append(page);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700457 }
458
David Vrabel24f69372013-09-19 17:14:53 +0100459 flush_tlb_all();
David Vrabel2bad07c2013-09-11 17:45:44 +0000460
Isaku Yamahataa90971e2008-05-26 23:31:14 +0100461 set_xen_guest_handle(reservation.extent_start, frame_list);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700462 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 Fitzhardinge17758262008-04-02 10:54:13 -0700467
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100468 return state;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700469}
470
471/*
Juergen Gross929423f2015-07-20 13:49:39 +0200472 * As this is a work item it is guaranteed to run as a single instance only.
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700473 * 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 */
477static void balloon_process(struct work_struct *work)
478{
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100479 enum bp_state state = BP_DONE;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700480 long credit;
481
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700482
483 do {
Juergen Gross929423f2015-07-20 13:49:39 +0200484 mutex_lock(&balloon_mutex);
485
Daniel Kiper83be7e52011-03-28 11:34:10 +0200486 credit = current_credit();
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100487
Daniel Kiper080e2be2011-07-25 17:12:06 -0700488 if (credit > 0) {
489 if (balloon_is_inflated())
490 state = increase_reservation(credit);
491 else
David Vrabelb2ac6aa2015-06-25 12:10:28 +0100492 state = reserve_additional_memory();
Daniel Kiper080e2be2011-07-25 17:12:06 -0700493 }
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100494
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700495 if (credit < 0)
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400496 state = decrease_reservation(-credit, GFP_BALLOON);
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100497
498 state = update_schedule(state);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700499
Juergen Gross929423f2015-07-20 13:49:39 +0200500 mutex_unlock(&balloon_mutex);
501
502 cond_resched();
503
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100504 } while (credit && state == BP_DONE);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700505
506 /* Schedule more work if there is some still to be done. */
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100507 if (state == BP_EAGAIN)
508 schedule_delayed_work(&balloon_worker, balloon_stats.schedule_delay * HZ);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700509}
510
511/* Resets the Xen limit, sets new target, and kicks off processing. */
Daniel De Graaf803eb042011-03-14 11:29:37 -0400512void balloon_set_new_target(unsigned long target)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700513{
514 /* No need for lock. Not read-modify-write updates. */
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700515 balloon_stats.target_pages = target;
Daniel Kiper95170b22011-03-08 22:47:39 +0100516 schedule_delayed_work(&balloon_worker, 0);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700517}
Daniel De Graaf803eb042011-03-14 11:29:37 -0400518EXPORT_SYMBOL_GPL(balloon_set_new_target);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700519
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400520/**
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 Graaf72e9cf22011-10-19 17:59:37 -0400524 * @highmem: allow highmem pages
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400525 * @return 0 on success, error otherwise
526 */
Stefano Stabellini693394b2011-09-29 11:57:55 +0100527int alloc_xenballooned_pages(int nr_pages, struct page **pages, bool highmem)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700528{
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400529 int pgno = 0;
Ruslan Pisareve882dc92011-07-26 14:15:59 +0300530 struct page *page;
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400531 mutex_lock(&balloon_mutex);
532 while (pgno < nr_pages) {
Stefano Stabellini693394b2011-09-29 11:57:55 +0100533 page = balloon_retrieve(highmem);
Daniel De Graaf72e9cf22011-10-19 17:59:37 -0400534 if (page && (highmem || !PageHighMem(page))) {
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400535 pages[pgno++] = page;
536 } else {
537 enum bp_state st;
Stefano Stabellini693394b2011-09-29 11:57:55 +0100538 if (page)
539 balloon_append(page);
540 st = decrease_reservation(nr_pages - pgno,
541 highmem ? GFP_HIGHUSER : GFP_USER);
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400542 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}
556EXPORT_SYMBOL(alloc_xenballooned_pages);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700557
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400558/**
559 * free_xenballooned_pages - return pages retrieved with get_ballooned_pages
560 * @nr_pages: Number of pages
561 * @pages: pages to return
562 */
Ruslan Pisareve882dc92011-07-26 14:15:59 +0300563void free_xenballooned_pages(int nr_pages, struct page **pages)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700564{
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400565 int i;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700566
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400567 mutex_lock(&balloon_mutex);
568
569 for (i = 0; i < nr_pages; i++) {
570 if (pages[i])
571 balloon_append(pages[i]);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700572 }
573
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400574 /* The balloon may be too large now. Shrink it if needed. */
Daniel Kiper83be7e52011-03-28 11:34:10 +0200575 if (current_credit())
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400576 schedule_delayed_work(&balloon_worker, 0);
577
578 mutex_unlock(&balloon_mutex);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700579}
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400580EXPORT_SYMBOL(free_xenballooned_pages);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700581
David Vrabel8b5d44a2011-09-28 17:46:34 +0100582static void __init balloon_add_region(unsigned long start_pfn,
583 unsigned long pages)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700584{
Daniel Kiper4dfe22f2011-03-28 11:33:18 +0200585 unsigned long pfn, extra_pfn_end;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700586 struct page *page;
587
David Vrabel8b5d44a2011-09-28 17:46:34 +0100588 /*
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 Vrabelde5a77d2015-06-25 12:08:20 +0100602
603 balloon_stats.total_pages += extra_pfn_end - start_pfn;
David Vrabel8b5d44a2011-09-28 17:46:34 +0100604}
605
606static int __init balloon_init(void)
607{
David Vrabel0bb599f2015-01-05 17:06:01 +0000608 int i;
David Vrabel8b5d44a2011-09-28 17:46:34 +0100609
Stefano Stabellini53d55222010-12-02 17:55:05 +0000610 if (!xen_domain())
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700611 return -ENODEV;
612
Joe Perches283c0972013-06-28 03:21:41 -0700613 pr_info("Initialising balloon driver\n");
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700614
David Vrabelaa244112011-09-28 17:46:32 +0100615 balloon_stats.current_pages = xen_pv_domain()
616 ? min(xen_start_info->nr_pages - xen_released_pages, max_pfn)
Boris Ostrovskyc275a572013-11-06 15:37:40 -0500617 : get_num_physpages();
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700618 balloon_stats.target_pages = balloon_stats.current_pages;
619 balloon_stats.balloon_low = 0;
620 balloon_stats.balloon_high = 0;
David Vrabelde5a77d2015-06-25 12:08:20 +0100621 balloon_stats.total_pages = balloon_stats.current_pages;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700622
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100623 balloon_stats.schedule_delay = 1;
624 balloon_stats.max_schedule_delay = 32;
625 balloon_stats.retry_count = 1;
Konrad Rzeszutek Wilk40095de2011-03-14 11:42:40 -0400626 balloon_stats.max_retry_count = RETRY_UNLIMITED;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700627
Daniel Kiper080e2be2011-07-25 17:12:06 -0700628#ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
Daniel Kiper080e2be2011-07-25 17:12:06 -0700629 set_online_page_callback(&xen_online_page);
630 register_memory_notifier(&xen_memory_nb);
631#endif
632
Jeremy Fitzhardinge2a4c92f2010-12-02 15:30:06 -0800633 /*
David Vrabelb1cbf9b2011-09-28 17:46:33 +0100634 * Initialize the balloon with pages from the extra memory
David Vrabel8b5d44a2011-09-28 17:46:34 +0100635 * regions (see arch/x86/xen/setup.c).
Jeremy Fitzhardinge2a4c92f2010-12-02 15:30:06 -0800636 */
David Vrabel8b5d44a2011-09-28 17:46:34 +0100637 for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++)
Juergen Gross626d7502015-09-04 14:05:51 +0200638 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 Fitzhardinge17758262008-04-02 10:54:13 -0700641
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700642 return 0;
643}
644
645subsys_initcall(balloon_init);
646
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700647MODULE_LICENSE("GPL");