blob: f56662324a47176a5184c6703e788951469d2d54 [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>
David Vrabel1cf6a6c2015-06-25 16:29:18 +010058#include <linux/sysctl.h>
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070059
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070060#include <asm/page.h>
61#include <asm/pgalloc.h>
62#include <asm/pgtable.h>
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070063#include <asm/tlb.h>
64
Jeremy Fitzhardingeecbf29c2008-12-16 12:37:07 -080065#include <asm/xen/hypervisor.h>
66#include <asm/xen/hypercall.h>
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070067
68#include <xen/xen.h>
Jeremy Fitzhardingeecbf29c2008-12-16 12:37:07 -080069#include <xen/interface/xen.h>
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070070#include <xen/interface/memory.h>
Daniel De Graaf803eb042011-03-14 11:29:37 -040071#include <xen/balloon.h>
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070072#include <xen/features.h>
73#include <xen/page.h>
74
David Vrabel1cf6a6c2015-06-25 16:29:18 +010075static int xen_hotplug_unpopulated;
76
77#ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
78
79static int zero;
80static int one = 1;
81
82static struct ctl_table balloon_table[] = {
83 {
84 .procname = "hotplug_unpopulated",
85 .data = &xen_hotplug_unpopulated,
86 .maxlen = sizeof(int),
87 .mode = 0644,
88 .proc_handler = proc_dointvec_minmax,
89 .extra1 = &zero,
90 .extra2 = &one,
91 },
92 { }
93};
94
95static struct ctl_table balloon_root[] = {
96 {
97 .procname = "balloon",
98 .mode = 0555,
99 .child = balloon_table,
100 },
101 { }
102};
103
104static struct ctl_table xen_root[] = {
105 {
106 .procname = "xen",
107 .mode = 0555,
108 .child = balloon_root,
109 },
110 { }
111};
112
113#endif
114
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100115/*
116 * balloon_process() state:
117 *
118 * BP_DONE: done or nothing to do,
David Vrabelb2ac6aa2015-06-25 12:10:28 +0100119 * BP_WAIT: wait to be rescheduled,
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100120 * BP_EAGAIN: error, go to sleep,
121 * BP_ECANCELED: error, balloon operation canceled.
122 */
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700123
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100124enum bp_state {
125 BP_DONE,
David Vrabelb2ac6aa2015-06-25 12:10:28 +0100126 BP_WAIT,
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100127 BP_EAGAIN,
128 BP_ECANCELED
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700129};
130
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700131
132static DEFINE_MUTEX(balloon_mutex);
133
Daniel De Graaf803eb042011-03-14 11:29:37 -0400134struct balloon_stats balloon_stats;
135EXPORT_SYMBOL_GPL(balloon_stats);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700136
137/* We increase/decrease in batches which fit in a page */
Ian Campbell965c0aa2012-10-17 09:39:16 +0100138static xen_pfn_t frame_list[PAGE_SIZE / sizeof(unsigned long)];
Stefano Stabellinicd9151e2013-08-04 15:39:40 +0100139
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700140
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700141/* List of ballooned pages, threaded through the mem_map array. */
142static LIST_HEAD(ballooned_pages);
David Vrabel1cf6a6c2015-06-25 16:29:18 +0100143static DECLARE_WAIT_QUEUE_HEAD(balloon_wq);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700144
145/* Main work function, always executed in process context. */
146static void balloon_process(struct work_struct *work);
Daniel Kiper95170b22011-03-08 22:47:39 +0100147static DECLARE_DELAYED_WORK(balloon_worker, balloon_process);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700148
149/* When ballooning out (allocating memory to return to Xen) we don't really
150 want the kernel to try too hard since that can trigger the oom killer. */
151#define GFP_BALLOON \
152 (GFP_HIGHUSER | __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC)
153
154static void scrub_page(struct page *page)
155{
156#ifdef CONFIG_XEN_SCRUB_PAGES
Jeremy Fitzhardinge26a3e992008-11-17 09:35:00 -0800157 clear_highpage(page);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700158#endif
159}
160
161/* balloon_append: add the given page to the balloon. */
Jeremy Fitzhardinge9be4d452010-08-31 15:01:16 -0700162static void __balloon_append(struct page *page)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700163{
164 /* Lowmem is re-populated first, so highmem pages go at list tail. */
165 if (PageHighMem(page)) {
166 list_add_tail(&page->lru, &ballooned_pages);
167 balloon_stats.balloon_high++;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700168 } else {
169 list_add(&page->lru, &ballooned_pages);
170 balloon_stats.balloon_low++;
171 }
David Vrabel1cf6a6c2015-06-25 16:29:18 +0100172 wake_up(&balloon_wq);
Jeremy Fitzhardinge9be4d452010-08-31 15:01:16 -0700173}
Gianluca Guida3d65c942009-07-30 22:54:36 +0100174
Jeremy Fitzhardinge9be4d452010-08-31 15:01:16 -0700175static void balloon_append(struct page *page)
176{
177 __balloon_append(page);
Jiang Liu3dcc0572013-07-03 15:03:21 -0700178 adjust_managed_page_count(page, -1);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700179}
180
181/* balloon_retrieve: rescue a page from the balloon, if it is not empty. */
David Vrabel81b286e2015-06-25 13:12:46 +0100182static struct page *balloon_retrieve(bool require_lowmem)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700183{
184 struct page *page;
185
186 if (list_empty(&ballooned_pages))
187 return NULL;
188
David Vrabel81b286e2015-06-25 13:12:46 +0100189 page = list_entry(ballooned_pages.next, struct page, lru);
190 if (require_lowmem && PageHighMem(page))
191 return NULL;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700192 list_del(&page->lru);
193
Jiang Liu3dcc0572013-07-03 15:03:21 -0700194 if (PageHighMem(page))
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700195 balloon_stats.balloon_high--;
Jiang Liu3dcc0572013-07-03 15:03:21 -0700196 else
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700197 balloon_stats.balloon_low--;
198
Jiang Liu3dcc0572013-07-03 15:03:21 -0700199 adjust_managed_page_count(page, 1);
Gianluca Guida3d65c942009-07-30 22:54:36 +0100200
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700201 return page;
202}
203
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700204static struct page *balloon_next_page(struct page *page)
205{
206 struct list_head *next = page->lru.next;
207 if (next == &ballooned_pages)
208 return NULL;
209 return list_entry(next, struct page, lru);
210}
211
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100212static enum bp_state update_schedule(enum bp_state state)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700213{
David Vrabelb2ac6aa2015-06-25 12:10:28 +0100214 if (state == BP_WAIT)
215 return BP_WAIT;
216
Boris Ostrovskyfd8b7952014-10-07 17:00:07 -0400217 if (state == BP_ECANCELED)
218 return BP_ECANCELED;
219
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100220 if (state == BP_DONE) {
221 balloon_stats.schedule_delay = 1;
222 balloon_stats.retry_count = 1;
223 return BP_DONE;
224 }
225
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100226 ++balloon_stats.retry_count;
227
228 if (balloon_stats.max_retry_count != RETRY_UNLIMITED &&
229 balloon_stats.retry_count > balloon_stats.max_retry_count) {
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100230 balloon_stats.schedule_delay = 1;
231 balloon_stats.retry_count = 1;
232 return BP_ECANCELED;
233 }
234
235 balloon_stats.schedule_delay <<= 1;
236
237 if (balloon_stats.schedule_delay > balloon_stats.max_schedule_delay)
238 balloon_stats.schedule_delay = balloon_stats.max_schedule_delay;
239
240 return BP_EAGAIN;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700241}
242
Daniel Kiper080e2be2011-07-25 17:12:06 -0700243#ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
David Vrabel55b3da92015-06-24 15:58:42 +0100244static struct resource *additional_memory_resource(phys_addr_t size)
245{
246 struct resource *res;
247 int ret;
248
249 res = kzalloc(sizeof(*res), GFP_KERNEL);
250 if (!res)
251 return NULL;
252
253 res->name = "System RAM";
254 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
255
256 ret = allocate_resource(&iomem_resource, res,
257 size, 0, -1,
258 PAGES_PER_SECTION * PAGE_SIZE, NULL, NULL);
259 if (ret < 0) {
260 pr_err("Cannot allocate new System RAM resource\n");
261 kfree(res);
262 return NULL;
263 }
264
265 return res;
266}
267
268static void release_memory_resource(struct resource *resource)
269{
270 if (!resource)
271 return;
272
273 /*
274 * No need to reset region to identity mapped since we now
275 * know that no I/O can be in this region
276 */
277 release_resource(resource);
278 kfree(resource);
279}
Daniel Kiper080e2be2011-07-25 17:12:06 -0700280
David Vrabelb2ac6aa2015-06-25 12:10:28 +0100281static enum bp_state reserve_additional_memory(void)
Daniel Kiper080e2be2011-07-25 17:12:06 -0700282{
David Vrabelb2ac6aa2015-06-25 12:10:28 +0100283 long credit;
David Vrabel55b3da92015-06-24 15:58:42 +0100284 struct resource *resource;
Daniel Kiper080e2be2011-07-25 17:12:06 -0700285 int nid, rc;
David Vrabel55b3da92015-06-24 15:58:42 +0100286 unsigned long balloon_hotplug;
Daniel Kiper080e2be2011-07-25 17:12:06 -0700287
David Vrabel1cf6a6c2015-06-25 16:29:18 +0100288 credit = balloon_stats.target_pages + balloon_stats.target_unpopulated
289 - balloon_stats.total_pages;
David Vrabelb2ac6aa2015-06-25 12:10:28 +0100290
291 /*
292 * Already hotplugged enough pages? Wait for them to be
293 * onlined.
294 */
295 if (credit <= 0)
296 return BP_WAIT;
297
David Vrabel55b3da92015-06-24 15:58:42 +0100298 balloon_hotplug = round_up(credit, PAGES_PER_SECTION);
299
300 resource = additional_memory_resource(balloon_hotplug * PAGE_SIZE);
301 if (!resource)
302 goto err;
303
304 nid = memory_add_physaddr_to_nid(resource->start);
Daniel Kiper080e2be2011-07-25 17:12:06 -0700305
Juergen Gross3c56b3a12015-03-20 13:55:39 +0100306#ifdef CONFIG_XEN_HAVE_PVMMU
307 /*
308 * add_memory() will build page tables for the new memory so
309 * the p2m must contain invalid entries so the correct
310 * non-present PTEs will be written.
311 *
312 * If a failure occurs, the original (identity) p2m entries
313 * are not restored since this region is now known not to
314 * conflict with any devices.
315 */
316 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
317 unsigned long pfn, i;
318
David Vrabel55b3da92015-06-24 15:58:42 +0100319 pfn = PFN_DOWN(resource->start);
Juergen Gross3c56b3a12015-03-20 13:55:39 +0100320 for (i = 0; i < balloon_hotplug; i++) {
321 if (!set_phys_to_machine(pfn + i, INVALID_P2M_ENTRY)) {
322 pr_warn("set_phys_to_machine() failed, no memory added\n");
David Vrabel55b3da92015-06-24 15:58:42 +0100323 goto err;
Juergen Gross3c56b3a12015-03-20 13:55:39 +0100324 }
325 }
326 }
327#endif
328
David Vrabel55b3da92015-06-24 15:58:42 +0100329 rc = add_memory_resource(nid, resource);
Daniel Kiper080e2be2011-07-25 17:12:06 -0700330 if (rc) {
David Vrabel3dcf6362014-09-01 18:52:44 +0100331 pr_warn("Cannot add additional memory (%i)\n", rc);
David Vrabel55b3da92015-06-24 15:58:42 +0100332 goto err;
Daniel Kiper080e2be2011-07-25 17:12:06 -0700333 }
334
David Vrabelde5a77d2015-06-25 12:08:20 +0100335 balloon_stats.total_pages += balloon_hotplug;
Daniel Kiper080e2be2011-07-25 17:12:06 -0700336
David Vrabelb2ac6aa2015-06-25 12:10:28 +0100337 return BP_WAIT;
David Vrabel55b3da92015-06-24 15:58:42 +0100338 err:
339 release_memory_resource(resource);
340 return BP_ECANCELED;
Daniel Kiper080e2be2011-07-25 17:12:06 -0700341}
342
343static void xen_online_page(struct page *page)
344{
345 __online_page_set_limits(page);
346
347 mutex_lock(&balloon_mutex);
348
349 __balloon_append(page);
350
Daniel Kiper080e2be2011-07-25 17:12:06 -0700351 mutex_unlock(&balloon_mutex);
352}
353
354static int xen_memory_notifier(struct notifier_block *nb, unsigned long val, void *v)
355{
356 if (val == MEM_ONLINE)
357 schedule_delayed_work(&balloon_worker, 0);
358
359 return NOTIFY_OK;
360}
361
362static struct notifier_block xen_memory_nb = {
363 .notifier_call = xen_memory_notifier,
364 .priority = 0
365};
366#else
David Vrabelb2ac6aa2015-06-25 12:10:28 +0100367static enum bp_state reserve_additional_memory(void)
Daniel Kiper080e2be2011-07-25 17:12:06 -0700368{
369 balloon_stats.target_pages = balloon_stats.current_pages;
David Vrabel1cf6a6c2015-06-25 16:29:18 +0100370 return BP_ECANCELED;
Daniel Kiper080e2be2011-07-25 17:12:06 -0700371}
372#endif /* CONFIG_XEN_BALLOON_MEMORY_HOTPLUG */
373
David Vrabelde5a77d2015-06-25 12:08:20 +0100374static long current_credit(void)
375{
376 return balloon_stats.target_pages - balloon_stats.current_pages;
377}
378
379static bool balloon_is_inflated(void)
380{
381 return balloon_stats.balloon_low || balloon_stats.balloon_high;
382}
383
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100384static enum bp_state increase_reservation(unsigned long nr_pages)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700385{
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100386 int rc;
Jeremy Fitzhardinge2f70e0a2010-09-02 23:11:17 -0700387 unsigned long pfn, i;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700388 struct page *page;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700389 struct xen_memory_reservation reservation = {
390 .address_bits = 0,
391 .extent_order = 0,
392 .domid = DOMID_SELF
393 };
394
395 if (nr_pages > ARRAY_SIZE(frame_list))
396 nr_pages = ARRAY_SIZE(frame_list);
397
Jie Liu9346c2a2013-11-13 20:59:58 +0800398 page = list_first_entry_or_null(&ballooned_pages, struct page, lru);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700399 for (i = 0; i < nr_pages; i++) {
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100400 if (!page) {
401 nr_pages = i;
402 break;
403 }
Joe Perchesa419aef2009-08-18 11:18:35 -0700404 frame_list[i] = page_to_pfn(page);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700405 page = balloon_next_page(page);
406 }
407
Isaku Yamahataa90971e2008-05-26 23:31:14 +0100408 set_xen_guest_handle(reservation.extent_start, frame_list);
Jeremy Fitzhardingefde28e82008-07-24 16:28:00 -0700409 reservation.nr_extents = nr_pages;
410 rc = HYPERVISOR_memory_op(XENMEM_populate_physmap, &reservation);
Konrad Rzeszutek Wilk40095de2011-03-14 11:42:40 -0400411 if (rc <= 0)
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100412 return BP_EAGAIN;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700413
Ian Campbellbc2c0302009-06-05 11:58:37 +0100414 for (i = 0; i < rc; i++) {
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400415 page = balloon_retrieve(false);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700416 BUG_ON(page == NULL);
417
418 pfn = page_to_pfn(page);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700419
Ian Campbellc2374bf2012-10-03 12:17:50 +0100420#ifdef CONFIG_XEN_HAVE_PVMMU
Stefano Stabellinic1d15f52013-12-11 16:58:42 +0000421 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
422 set_phys_to_machine(pfn, frame_list[i]);
423
424 /* Link back into the page tables if not highmem. */
425 if (!PageHighMem(page)) {
426 int ret;
427 ret = HYPERVISOR_update_va_mapping(
428 (unsigned long)__va(pfn << PAGE_SHIFT),
429 mfn_pte(frame_list[i], PAGE_KERNEL),
430 0);
431 BUG_ON(ret);
432 }
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700433 }
Ian Campbellc2374bf2012-10-03 12:17:50 +0100434#endif
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700435
436 /* Relinquish the page back to the allocator. */
Jiang Liu3dcc0572013-07-03 15:03:21 -0700437 __free_reserved_page(page);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700438 }
439
Ian Campbellbc2c0302009-06-05 11:58:37 +0100440 balloon_stats.current_pages += rc;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700441
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100442 return BP_DONE;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700443}
444
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400445static enum bp_state decrease_reservation(unsigned long nr_pages, gfp_t gfp)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700446{
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100447 enum bp_state state = BP_DONE;
Jeremy Fitzhardinge2f70e0a2010-09-02 23:11:17 -0700448 unsigned long pfn, i;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700449 struct page *page;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700450 int ret;
451 struct xen_memory_reservation reservation = {
452 .address_bits = 0,
453 .extent_order = 0,
454 .domid = DOMID_SELF
455 };
456
457 if (nr_pages > ARRAY_SIZE(frame_list))
458 nr_pages = ARRAY_SIZE(frame_list);
459
460 for (i = 0; i < nr_pages; i++) {
Lisa Nguyenfce92682013-05-15 22:59:40 -0700461 page = alloc_page(gfp);
462 if (page == NULL) {
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700463 nr_pages = i;
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100464 state = BP_EAGAIN;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700465 break;
466 }
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700467 scrub_page(page);
Dan Magenheimer1058a752009-01-22 14:36:08 -0800468
Wei Liu09ed3d52014-03-15 16:11:47 +0000469 frame_list[i] = page_to_pfn(page);
470 }
471
472 /*
473 * Ensure that ballooned highmem pages don't have kmaps.
474 *
475 * Do this before changing the p2m as kmap_flush_unused()
476 * reads PTEs to obtain pages (and hence needs the original
477 * p2m entry).
478 */
479 kmap_flush_unused();
480
481 /* Update direct mapping, invalidate P2M, and add to balloon. */
482 for (i = 0; i < nr_pages; i++) {
483 pfn = frame_list[i];
Julien Grall0df4f262015-08-07 17:34:37 +0100484 frame_list[i] = pfn_to_gfn(pfn);
Wei Liu09ed3d52014-03-15 16:11:47 +0000485 page = pfn_to_page(pfn);
486
Stefano Stabellinic1d15f52013-12-11 16:58:42 +0000487#ifdef CONFIG_XEN_HAVE_PVMMU
Wei Liu04660bb2013-08-27 16:17:25 +0100488 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
Stefano Stabellinic1d15f52013-12-11 16:58:42 +0000489 if (!PageHighMem(page)) {
490 ret = HYPERVISOR_update_va_mapping(
491 (unsigned long)__va(pfn << PAGE_SHIFT),
David Vrabel0bb599f2015-01-05 17:06:01 +0000492 __pte_ma(0), 0);
Stefano Stabellinic1d15f52013-12-11 16:58:42 +0000493 BUG_ON(ret);
David Vrabelfb9a0c42014-06-27 10:42:03 +0100494 }
495 __set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
Wei Liu04660bb2013-08-27 16:17:25 +0100496 }
Stefano Stabellinic1d15f52013-12-11 16:58:42 +0000497#endif
David Vrabel24f69372013-09-19 17:14:53 +0100498
Wei Liu09ed3d52014-03-15 16:11:47 +0000499 balloon_append(page);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700500 }
501
David Vrabel24f69372013-09-19 17:14:53 +0100502 flush_tlb_all();
David Vrabel2bad07c2013-09-11 17:45:44 +0000503
Isaku Yamahataa90971e2008-05-26 23:31:14 +0100504 set_xen_guest_handle(reservation.extent_start, frame_list);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700505 reservation.nr_extents = nr_pages;
506 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
507 BUG_ON(ret != nr_pages);
508
509 balloon_stats.current_pages -= nr_pages;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700510
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100511 return state;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700512}
513
514/*
Juergen Gross929423f2015-07-20 13:49:39 +0200515 * As this is a work item it is guaranteed to run as a single instance only.
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700516 * We may of course race updates of the target counts (which are protected
517 * by the balloon lock), or with changes to the Xen hard limit, but we will
518 * recover from these in time.
519 */
520static void balloon_process(struct work_struct *work)
521{
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100522 enum bp_state state = BP_DONE;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700523 long credit;
524
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700525
526 do {
Juergen Gross929423f2015-07-20 13:49:39 +0200527 mutex_lock(&balloon_mutex);
528
Daniel Kiper83be7e52011-03-28 11:34:10 +0200529 credit = current_credit();
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100530
Daniel Kiper080e2be2011-07-25 17:12:06 -0700531 if (credit > 0) {
532 if (balloon_is_inflated())
533 state = increase_reservation(credit);
534 else
David Vrabelb2ac6aa2015-06-25 12:10:28 +0100535 state = reserve_additional_memory();
Daniel Kiper080e2be2011-07-25 17:12:06 -0700536 }
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100537
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700538 if (credit < 0)
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400539 state = decrease_reservation(-credit, GFP_BALLOON);
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100540
541 state = update_schedule(state);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700542
Juergen Gross929423f2015-07-20 13:49:39 +0200543 mutex_unlock(&balloon_mutex);
544
545 cond_resched();
546
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100547 } while (credit && state == BP_DONE);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700548
549 /* Schedule more work if there is some still to be done. */
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100550 if (state == BP_EAGAIN)
551 schedule_delayed_work(&balloon_worker, balloon_stats.schedule_delay * HZ);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700552}
553
554/* Resets the Xen limit, sets new target, and kicks off processing. */
Daniel De Graaf803eb042011-03-14 11:29:37 -0400555void balloon_set_new_target(unsigned long target)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700556{
557 /* No need for lock. Not read-modify-write updates. */
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700558 balloon_stats.target_pages = target;
Daniel Kiper95170b22011-03-08 22:47:39 +0100559 schedule_delayed_work(&balloon_worker, 0);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700560}
Daniel De Graaf803eb042011-03-14 11:29:37 -0400561EXPORT_SYMBOL_GPL(balloon_set_new_target);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700562
David Vrabel1cf6a6c2015-06-25 16:29:18 +0100563static int add_ballooned_pages(int nr_pages)
564{
565 enum bp_state st;
566
567 if (xen_hotplug_unpopulated) {
568 st = reserve_additional_memory();
569 if (st != BP_ECANCELED) {
570 mutex_unlock(&balloon_mutex);
571 wait_event(balloon_wq,
572 !list_empty(&ballooned_pages));
573 mutex_lock(&balloon_mutex);
574 return 0;
575 }
576 }
577
578 st = decrease_reservation(nr_pages, GFP_USER);
579 if (st != BP_DONE)
580 return -ENOMEM;
581
582 return 0;
583}
584
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400585/**
586 * alloc_xenballooned_pages - get pages that have been ballooned out
587 * @nr_pages: Number of pages to get
588 * @pages: pages returned
589 * @return 0 on success, error otherwise
590 */
David Vrabel81b286e2015-06-25 13:12:46 +0100591int alloc_xenballooned_pages(int nr_pages, struct page **pages)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700592{
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400593 int pgno = 0;
Ruslan Pisareve882dc92011-07-26 14:15:59 +0300594 struct page *page;
David Vrabel1cf6a6c2015-06-25 16:29:18 +0100595 int ret;
596
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400597 mutex_lock(&balloon_mutex);
David Vrabel1cf6a6c2015-06-25 16:29:18 +0100598
599 balloon_stats.target_unpopulated += nr_pages;
600
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400601 while (pgno < nr_pages) {
David Vrabel81b286e2015-06-25 13:12:46 +0100602 page = balloon_retrieve(true);
603 if (page) {
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400604 pages[pgno++] = page;
David Vrabel4a69c902015-07-22 14:50:37 +0100605#ifdef CONFIG_XEN_HAVE_PVMMU
606 ret = xen_alloc_p2m_entry(page_to_pfn(page));
607 if (ret < 0)
608 goto out_undo;
609#endif
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400610 } else {
David Vrabel1cf6a6c2015-06-25 16:29:18 +0100611 ret = add_ballooned_pages(nr_pages - pgno);
612 if (ret < 0)
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400613 goto out_undo;
614 }
615 }
616 mutex_unlock(&balloon_mutex);
617 return 0;
618 out_undo:
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400619 mutex_unlock(&balloon_mutex);
David Vrabel1cf6a6c2015-06-25 16:29:18 +0100620 free_xenballooned_pages(pgno, pages);
621 return ret;
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400622}
623EXPORT_SYMBOL(alloc_xenballooned_pages);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700624
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400625/**
626 * free_xenballooned_pages - return pages retrieved with get_ballooned_pages
627 * @nr_pages: Number of pages
628 * @pages: pages to return
629 */
Ruslan Pisareve882dc92011-07-26 14:15:59 +0300630void free_xenballooned_pages(int nr_pages, struct page **pages)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700631{
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400632 int i;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700633
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400634 mutex_lock(&balloon_mutex);
635
636 for (i = 0; i < nr_pages; i++) {
637 if (pages[i])
638 balloon_append(pages[i]);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700639 }
640
David Vrabel1cf6a6c2015-06-25 16:29:18 +0100641 balloon_stats.target_unpopulated -= nr_pages;
642
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400643 /* The balloon may be too large now. Shrink it if needed. */
Daniel Kiper83be7e52011-03-28 11:34:10 +0200644 if (current_credit())
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400645 schedule_delayed_work(&balloon_worker, 0);
646
647 mutex_unlock(&balloon_mutex);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700648}
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400649EXPORT_SYMBOL(free_xenballooned_pages);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700650
David Vrabel8b5d44a2011-09-28 17:46:34 +0100651static void __init balloon_add_region(unsigned long start_pfn,
652 unsigned long pages)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700653{
Daniel Kiper4dfe22f2011-03-28 11:33:18 +0200654 unsigned long pfn, extra_pfn_end;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700655 struct page *page;
656
David Vrabel8b5d44a2011-09-28 17:46:34 +0100657 /*
658 * If the amount of usable memory has been limited (e.g., with
659 * the 'mem' command line parameter), don't add pages beyond
660 * this limit.
661 */
662 extra_pfn_end = min(max_pfn, start_pfn + pages);
663
664 for (pfn = start_pfn; pfn < extra_pfn_end; pfn++) {
665 page = pfn_to_page(pfn);
666 /* totalram_pages and totalhigh_pages do not
667 include the boot-time balloon extension, so
668 don't subtract from it. */
669 __balloon_append(page);
670 }
David Vrabelde5a77d2015-06-25 12:08:20 +0100671
672 balloon_stats.total_pages += extra_pfn_end - start_pfn;
David Vrabel8b5d44a2011-09-28 17:46:34 +0100673}
674
675static int __init balloon_init(void)
676{
David Vrabel0bb599f2015-01-05 17:06:01 +0000677 int i;
David Vrabel8b5d44a2011-09-28 17:46:34 +0100678
Stefano Stabellini53d55222010-12-02 17:55:05 +0000679 if (!xen_domain())
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700680 return -ENODEV;
681
Joe Perches283c0972013-06-28 03:21:41 -0700682 pr_info("Initialising balloon driver\n");
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700683
David Vrabelaa244112011-09-28 17:46:32 +0100684 balloon_stats.current_pages = xen_pv_domain()
685 ? min(xen_start_info->nr_pages - xen_released_pages, max_pfn)
Boris Ostrovskyc275a572013-11-06 15:37:40 -0500686 : get_num_physpages();
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700687 balloon_stats.target_pages = balloon_stats.current_pages;
688 balloon_stats.balloon_low = 0;
689 balloon_stats.balloon_high = 0;
David Vrabelde5a77d2015-06-25 12:08:20 +0100690 balloon_stats.total_pages = balloon_stats.current_pages;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700691
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100692 balloon_stats.schedule_delay = 1;
693 balloon_stats.max_schedule_delay = 32;
694 balloon_stats.retry_count = 1;
Konrad Rzeszutek Wilk40095de2011-03-14 11:42:40 -0400695 balloon_stats.max_retry_count = RETRY_UNLIMITED;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700696
Daniel Kiper080e2be2011-07-25 17:12:06 -0700697#ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
Daniel Kiper080e2be2011-07-25 17:12:06 -0700698 set_online_page_callback(&xen_online_page);
699 register_memory_notifier(&xen_memory_nb);
David Vrabel1cf6a6c2015-06-25 16:29:18 +0100700 register_sysctl_table(xen_root);
Daniel Kiper080e2be2011-07-25 17:12:06 -0700701#endif
702
Jeremy Fitzhardinge2a4c92f2010-12-02 15:30:06 -0800703 /*
David Vrabelb1cbf9b2011-09-28 17:46:33 +0100704 * Initialize the balloon with pages from the extra memory
David Vrabel8b5d44a2011-09-28 17:46:34 +0100705 * regions (see arch/x86/xen/setup.c).
Jeremy Fitzhardinge2a4c92f2010-12-02 15:30:06 -0800706 */
David Vrabel8b5d44a2011-09-28 17:46:34 +0100707 for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++)
Juergen Gross626d7502015-09-04 14:05:51 +0200708 if (xen_extra_mem[i].n_pfns)
709 balloon_add_region(xen_extra_mem[i].start_pfn,
710 xen_extra_mem[i].n_pfns);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700711
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700712 return 0;
713}
714
715subsys_initcall(balloon_init);
716
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700717MODULE_LICENSE("GPL");