blob: 095bb67897311728dfe1ae645e8bb89c202f4b63 [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,
78 * BP_EAGAIN: error, go to sleep,
79 * BP_ECANCELED: error, balloon operation canceled.
80 */
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070081
Daniel Kiper95d2ac42011-03-08 22:48:24 +010082enum bp_state {
83 BP_DONE,
84 BP_EAGAIN,
85 BP_ECANCELED
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070086};
87
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070088
89static DEFINE_MUTEX(balloon_mutex);
90
Daniel De Graaf803eb042011-03-14 11:29:37 -040091struct balloon_stats balloon_stats;
92EXPORT_SYMBOL_GPL(balloon_stats);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070093
94/* We increase/decrease in batches which fit in a page */
Ian Campbell965c0aa2012-10-17 09:39:16 +010095static xen_pfn_t frame_list[PAGE_SIZE / sizeof(unsigned long)];
Stefano Stabellinicd9151e2013-08-04 15:39:40 +010096
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070097
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070098/* List of ballooned pages, threaded through the mem_map array. */
99static LIST_HEAD(ballooned_pages);
100
101/* Main work function, always executed in process context. */
102static void balloon_process(struct work_struct *work);
Daniel Kiper95170b22011-03-08 22:47:39 +0100103static DECLARE_DELAYED_WORK(balloon_worker, balloon_process);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700104
105/* When ballooning out (allocating memory to return to Xen) we don't really
106 want the kernel to try too hard since that can trigger the oom killer. */
107#define GFP_BALLOON \
108 (GFP_HIGHUSER | __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC)
109
110static void scrub_page(struct page *page)
111{
112#ifdef CONFIG_XEN_SCRUB_PAGES
Jeremy Fitzhardinge26a3e992008-11-17 09:35:00 -0800113 clear_highpage(page);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700114#endif
115}
116
117/* balloon_append: add the given page to the balloon. */
Jeremy Fitzhardinge9be4d452010-08-31 15:01:16 -0700118static void __balloon_append(struct page *page)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700119{
120 /* Lowmem is re-populated first, so highmem pages go at list tail. */
121 if (PageHighMem(page)) {
122 list_add_tail(&page->lru, &ballooned_pages);
123 balloon_stats.balloon_high++;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700124 } else {
125 list_add(&page->lru, &ballooned_pages);
126 balloon_stats.balloon_low++;
127 }
Jeremy Fitzhardinge9be4d452010-08-31 15:01:16 -0700128}
Gianluca Guida3d65c942009-07-30 22:54:36 +0100129
Jeremy Fitzhardinge9be4d452010-08-31 15:01:16 -0700130static void balloon_append(struct page *page)
131{
132 __balloon_append(page);
Jiang Liu3dcc0572013-07-03 15:03:21 -0700133 adjust_managed_page_count(page, -1);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700134}
135
136/* balloon_retrieve: rescue a page from the balloon, if it is not empty. */
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400137static struct page *balloon_retrieve(bool prefer_highmem)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700138{
139 struct page *page;
140
141 if (list_empty(&ballooned_pages))
142 return NULL;
143
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400144 if (prefer_highmem)
145 page = list_entry(ballooned_pages.prev, struct page, lru);
146 else
147 page = list_entry(ballooned_pages.next, struct page, lru);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700148 list_del(&page->lru);
149
Jiang Liu3dcc0572013-07-03 15:03:21 -0700150 if (PageHighMem(page))
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700151 balloon_stats.balloon_high--;
Jiang Liu3dcc0572013-07-03 15:03:21 -0700152 else
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700153 balloon_stats.balloon_low--;
154
Jiang Liu3dcc0572013-07-03 15:03:21 -0700155 adjust_managed_page_count(page, 1);
Gianluca Guida3d65c942009-07-30 22:54:36 +0100156
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700157 return page;
158}
159
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700160static struct page *balloon_next_page(struct page *page)
161{
162 struct list_head *next = page->lru.next;
163 if (next == &ballooned_pages)
164 return NULL;
165 return list_entry(next, struct page, lru);
166}
167
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100168static enum bp_state update_schedule(enum bp_state state)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700169{
Boris Ostrovskyfd8b7952014-10-07 17:00:07 -0400170 if (state == BP_ECANCELED)
171 return BP_ECANCELED;
172
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100173 if (state == BP_DONE) {
174 balloon_stats.schedule_delay = 1;
175 balloon_stats.retry_count = 1;
176 return BP_DONE;
177 }
178
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100179 ++balloon_stats.retry_count;
180
181 if (balloon_stats.max_retry_count != RETRY_UNLIMITED &&
182 balloon_stats.retry_count > balloon_stats.max_retry_count) {
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100183 balloon_stats.schedule_delay = 1;
184 balloon_stats.retry_count = 1;
185 return BP_ECANCELED;
186 }
187
188 balloon_stats.schedule_delay <<= 1;
189
190 if (balloon_stats.schedule_delay > balloon_stats.max_schedule_delay)
191 balloon_stats.schedule_delay = balloon_stats.max_schedule_delay;
192
193 return BP_EAGAIN;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700194}
195
Daniel Kiper080e2be2011-07-25 17:12:06 -0700196#ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
197static long current_credit(void)
198{
199 return balloon_stats.target_pages - balloon_stats.current_pages -
200 balloon_stats.hotplug_pages;
201}
202
203static bool balloon_is_inflated(void)
204{
205 if (balloon_stats.balloon_low || balloon_stats.balloon_high ||
206 balloon_stats.balloon_hotplug)
207 return true;
208 else
209 return false;
210}
211
David Vrabel55b3da92015-06-24 15:58:42 +0100212static struct resource *additional_memory_resource(phys_addr_t size)
213{
214 struct resource *res;
215 int ret;
216
217 res = kzalloc(sizeof(*res), GFP_KERNEL);
218 if (!res)
219 return NULL;
220
221 res->name = "System RAM";
222 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
223
224 ret = allocate_resource(&iomem_resource, res,
225 size, 0, -1,
226 PAGES_PER_SECTION * PAGE_SIZE, NULL, NULL);
227 if (ret < 0) {
228 pr_err("Cannot allocate new System RAM resource\n");
229 kfree(res);
230 return NULL;
231 }
232
233 return res;
234}
235
236static void release_memory_resource(struct resource *resource)
237{
238 if (!resource)
239 return;
240
241 /*
242 * No need to reset region to identity mapped since we now
243 * know that no I/O can be in this region
244 */
245 release_resource(resource);
246 kfree(resource);
247}
Daniel Kiper080e2be2011-07-25 17:12:06 -0700248
249static enum bp_state reserve_additional_memory(long credit)
250{
David Vrabel55b3da92015-06-24 15:58:42 +0100251 struct resource *resource;
Daniel Kiper080e2be2011-07-25 17:12:06 -0700252 int nid, rc;
David Vrabel55b3da92015-06-24 15:58:42 +0100253 unsigned long balloon_hotplug;
Daniel Kiper080e2be2011-07-25 17:12:06 -0700254
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 Gross3c56b3a2015-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 Gross3c56b3a2015-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 Gross3c56b3a2015-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
292 balloon_hotplug -= credit;
293
294 balloon_stats.hotplug_pages += credit;
295 balloon_stats.balloon_hotplug = balloon_hotplug;
296
297 return BP_DONE;
David Vrabel55b3da92015-06-24 15:58:42 +0100298 err:
299 release_memory_resource(resource);
300 return BP_ECANCELED;
Daniel Kiper080e2be2011-07-25 17:12:06 -0700301}
302
303static void xen_online_page(struct page *page)
304{
305 __online_page_set_limits(page);
306
307 mutex_lock(&balloon_mutex);
308
309 __balloon_append(page);
310
311 if (balloon_stats.hotplug_pages)
312 --balloon_stats.hotplug_pages;
313 else
314 --balloon_stats.balloon_hotplug;
315
316 mutex_unlock(&balloon_mutex);
317}
318
319static int xen_memory_notifier(struct notifier_block *nb, unsigned long val, void *v)
320{
321 if (val == MEM_ONLINE)
322 schedule_delayed_work(&balloon_worker, 0);
323
324 return NOTIFY_OK;
325}
326
327static struct notifier_block xen_memory_nb = {
328 .notifier_call = xen_memory_notifier,
329 .priority = 0
330};
331#else
Daniel Kiper83be7e52011-03-28 11:34:10 +0200332static long current_credit(void)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700333{
Ian Campbellbc2c0302009-06-05 11:58:37 +0100334 unsigned long target = balloon_stats.target_pages;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700335
336 target = min(target,
337 balloon_stats.current_pages +
338 balloon_stats.balloon_low +
339 balloon_stats.balloon_high);
340
Daniel Kiper83be7e52011-03-28 11:34:10 +0200341 return target - balloon_stats.current_pages;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700342}
343
Daniel Kiper080e2be2011-07-25 17:12:06 -0700344static bool balloon_is_inflated(void)
345{
346 if (balloon_stats.balloon_low || balloon_stats.balloon_high)
347 return true;
348 else
349 return false;
350}
351
352static enum bp_state reserve_additional_memory(long credit)
353{
354 balloon_stats.target_pages = balloon_stats.current_pages;
355 return BP_DONE;
356}
357#endif /* CONFIG_XEN_BALLOON_MEMORY_HOTPLUG */
358
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100359static enum bp_state increase_reservation(unsigned long nr_pages)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700360{
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100361 int rc;
Jeremy Fitzhardinge2f70e0a2010-09-02 23:11:17 -0700362 unsigned long pfn, i;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700363 struct page *page;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700364 struct xen_memory_reservation reservation = {
365 .address_bits = 0,
366 .extent_order = 0,
367 .domid = DOMID_SELF
368 };
369
Daniel Kiper080e2be2011-07-25 17:12:06 -0700370#ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
371 if (!balloon_stats.balloon_low && !balloon_stats.balloon_high) {
372 nr_pages = min(nr_pages, balloon_stats.balloon_hotplug);
373 balloon_stats.hotplug_pages += nr_pages;
374 balloon_stats.balloon_hotplug -= nr_pages;
375 return BP_DONE;
376 }
377#endif
378
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700379 if (nr_pages > ARRAY_SIZE(frame_list))
380 nr_pages = ARRAY_SIZE(frame_list);
381
Jie Liu9346c2a2013-11-13 20:59:58 +0800382 page = list_first_entry_or_null(&ballooned_pages, struct page, lru);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700383 for (i = 0; i < nr_pages; i++) {
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100384 if (!page) {
385 nr_pages = i;
386 break;
387 }
Joe Perchesa419aef2009-08-18 11:18:35 -0700388 frame_list[i] = page_to_pfn(page);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700389 page = balloon_next_page(page);
390 }
391
Isaku Yamahataa90971e2008-05-26 23:31:14 +0100392 set_xen_guest_handle(reservation.extent_start, frame_list);
Jeremy Fitzhardingefde28e82008-07-24 16:28:00 -0700393 reservation.nr_extents = nr_pages;
394 rc = HYPERVISOR_memory_op(XENMEM_populate_physmap, &reservation);
Konrad Rzeszutek Wilk40095de2011-03-14 11:42:40 -0400395 if (rc <= 0)
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100396 return BP_EAGAIN;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700397
Ian Campbellbc2c0302009-06-05 11:58:37 +0100398 for (i = 0; i < rc; i++) {
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400399 page = balloon_retrieve(false);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700400 BUG_ON(page == NULL);
401
402 pfn = page_to_pfn(page);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700403
Ian Campbellc2374bf2012-10-03 12:17:50 +0100404#ifdef CONFIG_XEN_HAVE_PVMMU
Stefano Stabellinic1d15f52013-12-11 16:58:42 +0000405 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
406 set_phys_to_machine(pfn, frame_list[i]);
407
408 /* Link back into the page tables if not highmem. */
409 if (!PageHighMem(page)) {
410 int ret;
411 ret = HYPERVISOR_update_va_mapping(
412 (unsigned long)__va(pfn << PAGE_SHIFT),
413 mfn_pte(frame_list[i], PAGE_KERNEL),
414 0);
415 BUG_ON(ret);
416 }
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700417 }
Ian Campbellc2374bf2012-10-03 12:17:50 +0100418#endif
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700419
420 /* Relinquish the page back to the allocator. */
Jiang Liu3dcc0572013-07-03 15:03:21 -0700421 __free_reserved_page(page);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700422 }
423
Ian Campbellbc2c0302009-06-05 11:58:37 +0100424 balloon_stats.current_pages += rc;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700425
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100426 return BP_DONE;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700427}
428
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400429static enum bp_state decrease_reservation(unsigned long nr_pages, gfp_t gfp)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700430{
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100431 enum bp_state state = BP_DONE;
Jeremy Fitzhardinge2f70e0a2010-09-02 23:11:17 -0700432 unsigned long pfn, i;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700433 struct page *page;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700434 int ret;
435 struct xen_memory_reservation reservation = {
436 .address_bits = 0,
437 .extent_order = 0,
438 .domid = DOMID_SELF
439 };
440
Daniel Kiper080e2be2011-07-25 17:12:06 -0700441#ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
442 if (balloon_stats.hotplug_pages) {
443 nr_pages = min(nr_pages, balloon_stats.hotplug_pages);
444 balloon_stats.hotplug_pages -= nr_pages;
445 balloon_stats.balloon_hotplug += nr_pages;
446 return BP_DONE;
447 }
448#endif
449
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700450 if (nr_pages > ARRAY_SIZE(frame_list))
451 nr_pages = ARRAY_SIZE(frame_list);
452
453 for (i = 0; i < nr_pages; i++) {
Lisa Nguyenfce92682013-05-15 22:59:40 -0700454 page = alloc_page(gfp);
455 if (page == NULL) {
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700456 nr_pages = i;
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100457 state = BP_EAGAIN;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700458 break;
459 }
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700460 scrub_page(page);
Dan Magenheimer1058a752009-01-22 14:36:08 -0800461
Wei Liu09ed3d52014-03-15 16:11:47 +0000462 frame_list[i] = page_to_pfn(page);
463 }
464
465 /*
466 * Ensure that ballooned highmem pages don't have kmaps.
467 *
468 * Do this before changing the p2m as kmap_flush_unused()
469 * reads PTEs to obtain pages (and hence needs the original
470 * p2m entry).
471 */
472 kmap_flush_unused();
473
474 /* Update direct mapping, invalidate P2M, and add to balloon. */
475 for (i = 0; i < nr_pages; i++) {
476 pfn = frame_list[i];
Julien Grall0df4f262015-08-07 17:34:37 +0100477 frame_list[i] = pfn_to_gfn(pfn);
Wei Liu09ed3d52014-03-15 16:11:47 +0000478 page = pfn_to_page(pfn);
479
Stefano Stabellinic1d15f52013-12-11 16:58:42 +0000480#ifdef CONFIG_XEN_HAVE_PVMMU
Wei Liu04660bb2013-08-27 16:17:25 +0100481 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
Stefano Stabellinic1d15f52013-12-11 16:58:42 +0000482 if (!PageHighMem(page)) {
483 ret = HYPERVISOR_update_va_mapping(
484 (unsigned long)__va(pfn << PAGE_SHIFT),
David Vrabel0bb599f2015-01-05 17:06:01 +0000485 __pte_ma(0), 0);
Stefano Stabellinic1d15f52013-12-11 16:58:42 +0000486 BUG_ON(ret);
David Vrabelfb9a0c42014-06-27 10:42:03 +0100487 }
488 __set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
Wei Liu04660bb2013-08-27 16:17:25 +0100489 }
Stefano Stabellinic1d15f52013-12-11 16:58:42 +0000490#endif
David Vrabel24f69372013-09-19 17:14:53 +0100491
Wei Liu09ed3d52014-03-15 16:11:47 +0000492 balloon_append(page);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700493 }
494
David Vrabel24f69372013-09-19 17:14:53 +0100495 flush_tlb_all();
David Vrabel2bad07c2013-09-11 17:45:44 +0000496
Isaku Yamahataa90971e2008-05-26 23:31:14 +0100497 set_xen_guest_handle(reservation.extent_start, frame_list);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700498 reservation.nr_extents = nr_pages;
499 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
500 BUG_ON(ret != nr_pages);
501
502 balloon_stats.current_pages -= nr_pages;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700503
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100504 return state;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700505}
506
507/*
Juergen Gross929423f2015-07-20 13:49:39 +0200508 * As this is a work item it is guaranteed to run as a single instance only.
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700509 * We may of course race updates of the target counts (which are protected
510 * by the balloon lock), or with changes to the Xen hard limit, but we will
511 * recover from these in time.
512 */
513static void balloon_process(struct work_struct *work)
514{
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100515 enum bp_state state = BP_DONE;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700516 long credit;
517
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700518
519 do {
Juergen Gross929423f2015-07-20 13:49:39 +0200520 mutex_lock(&balloon_mutex);
521
Daniel Kiper83be7e52011-03-28 11:34:10 +0200522 credit = current_credit();
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100523
Daniel Kiper080e2be2011-07-25 17:12:06 -0700524 if (credit > 0) {
525 if (balloon_is_inflated())
526 state = increase_reservation(credit);
527 else
528 state = reserve_additional_memory(credit);
529 }
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100530
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700531 if (credit < 0)
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400532 state = decrease_reservation(-credit, GFP_BALLOON);
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100533
534 state = update_schedule(state);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700535
Juergen Gross929423f2015-07-20 13:49:39 +0200536 mutex_unlock(&balloon_mutex);
537
538 cond_resched();
539
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100540 } while (credit && state == BP_DONE);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700541
542 /* Schedule more work if there is some still to be done. */
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100543 if (state == BP_EAGAIN)
544 schedule_delayed_work(&balloon_worker, balloon_stats.schedule_delay * HZ);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700545}
546
547/* Resets the Xen limit, sets new target, and kicks off processing. */
Daniel De Graaf803eb042011-03-14 11:29:37 -0400548void balloon_set_new_target(unsigned long target)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700549{
550 /* No need for lock. Not read-modify-write updates. */
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700551 balloon_stats.target_pages = target;
Daniel Kiper95170b22011-03-08 22:47:39 +0100552 schedule_delayed_work(&balloon_worker, 0);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700553}
Daniel De Graaf803eb042011-03-14 11:29:37 -0400554EXPORT_SYMBOL_GPL(balloon_set_new_target);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700555
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400556/**
557 * alloc_xenballooned_pages - get pages that have been ballooned out
558 * @nr_pages: Number of pages to get
559 * @pages: pages returned
Daniel De Graaf72e9cf22011-10-19 17:59:37 -0400560 * @highmem: allow highmem pages
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400561 * @return 0 on success, error otherwise
562 */
Stefano Stabellini693394b2011-09-29 11:57:55 +0100563int alloc_xenballooned_pages(int nr_pages, struct page **pages, bool highmem)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700564{
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400565 int pgno = 0;
Ruslan Pisareve882dc92011-07-26 14:15:59 +0300566 struct page *page;
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400567 mutex_lock(&balloon_mutex);
568 while (pgno < nr_pages) {
Stefano Stabellini693394b2011-09-29 11:57:55 +0100569 page = balloon_retrieve(highmem);
Daniel De Graaf72e9cf22011-10-19 17:59:37 -0400570 if (page && (highmem || !PageHighMem(page))) {
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400571 pages[pgno++] = page;
572 } else {
573 enum bp_state st;
Stefano Stabellini693394b2011-09-29 11:57:55 +0100574 if (page)
575 balloon_append(page);
576 st = decrease_reservation(nr_pages - pgno,
577 highmem ? GFP_HIGHUSER : GFP_USER);
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400578 if (st != BP_DONE)
579 goto out_undo;
580 }
581 }
582 mutex_unlock(&balloon_mutex);
583 return 0;
584 out_undo:
585 while (pgno)
586 balloon_append(pages[--pgno]);
587 /* Free the memory back to the kernel soon */
588 schedule_delayed_work(&balloon_worker, 0);
589 mutex_unlock(&balloon_mutex);
590 return -ENOMEM;
591}
592EXPORT_SYMBOL(alloc_xenballooned_pages);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700593
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400594/**
595 * free_xenballooned_pages - return pages retrieved with get_ballooned_pages
596 * @nr_pages: Number of pages
597 * @pages: pages to return
598 */
Ruslan Pisareve882dc92011-07-26 14:15:59 +0300599void free_xenballooned_pages(int nr_pages, struct page **pages)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700600{
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400601 int i;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700602
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400603 mutex_lock(&balloon_mutex);
604
605 for (i = 0; i < nr_pages; i++) {
606 if (pages[i])
607 balloon_append(pages[i]);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700608 }
609
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400610 /* The balloon may be too large now. Shrink it if needed. */
Daniel Kiper83be7e52011-03-28 11:34:10 +0200611 if (current_credit())
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400612 schedule_delayed_work(&balloon_worker, 0);
613
614 mutex_unlock(&balloon_mutex);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700615}
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400616EXPORT_SYMBOL(free_xenballooned_pages);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700617
David Vrabel8b5d44a2011-09-28 17:46:34 +0100618static void __init balloon_add_region(unsigned long start_pfn,
619 unsigned long pages)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700620{
Daniel Kiper4dfe22f2011-03-28 11:33:18 +0200621 unsigned long pfn, extra_pfn_end;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700622 struct page *page;
623
David Vrabel8b5d44a2011-09-28 17:46:34 +0100624 /*
625 * If the amount of usable memory has been limited (e.g., with
626 * the 'mem' command line parameter), don't add pages beyond
627 * this limit.
628 */
629 extra_pfn_end = min(max_pfn, start_pfn + pages);
630
631 for (pfn = start_pfn; pfn < extra_pfn_end; pfn++) {
632 page = pfn_to_page(pfn);
633 /* totalram_pages and totalhigh_pages do not
634 include the boot-time balloon extension, so
635 don't subtract from it. */
636 __balloon_append(page);
637 }
638}
639
640static int __init balloon_init(void)
641{
David Vrabel0bb599f2015-01-05 17:06:01 +0000642 int i;
David Vrabel8b5d44a2011-09-28 17:46:34 +0100643
Stefano Stabellini53d55222010-12-02 17:55:05 +0000644 if (!xen_domain())
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700645 return -ENODEV;
646
Joe Perches283c0972013-06-28 03:21:41 -0700647 pr_info("Initialising balloon driver\n");
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700648
David Vrabelaa244112011-09-28 17:46:32 +0100649 balloon_stats.current_pages = xen_pv_domain()
650 ? min(xen_start_info->nr_pages - xen_released_pages, max_pfn)
Boris Ostrovskyc275a572013-11-06 15:37:40 -0500651 : get_num_physpages();
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700652 balloon_stats.target_pages = balloon_stats.current_pages;
653 balloon_stats.balloon_low = 0;
654 balloon_stats.balloon_high = 0;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700655
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100656 balloon_stats.schedule_delay = 1;
657 balloon_stats.max_schedule_delay = 32;
658 balloon_stats.retry_count = 1;
Konrad Rzeszutek Wilk40095de2011-03-14 11:42:40 -0400659 balloon_stats.max_retry_count = RETRY_UNLIMITED;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700660
Daniel Kiper080e2be2011-07-25 17:12:06 -0700661#ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
662 balloon_stats.hotplug_pages = 0;
663 balloon_stats.balloon_hotplug = 0;
664
665 set_online_page_callback(&xen_online_page);
666 register_memory_notifier(&xen_memory_nb);
667#endif
668
Jeremy Fitzhardinge2a4c92f2010-12-02 15:30:06 -0800669 /*
David Vrabelb1cbf9b2011-09-28 17:46:33 +0100670 * Initialize the balloon with pages from the extra memory
David Vrabel8b5d44a2011-09-28 17:46:34 +0100671 * regions (see arch/x86/xen/setup.c).
Jeremy Fitzhardinge2a4c92f2010-12-02 15:30:06 -0800672 */
David Vrabel8b5d44a2011-09-28 17:46:34 +0100673 for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++)
Juergen Gross626d7502015-09-04 14:05:51 +0200674 if (xen_extra_mem[i].n_pfns)
675 balloon_add_region(xen_extra_mem[i].start_pfn,
676 xen_extra_mem[i].n_pfns);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700677
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700678 return 0;
679}
680
681subsys_initcall(balloon_init);
682
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700683MODULE_LICENSE("GPL");