blob: ac8054765127fe5d95bfa74f0f25586bab0d9df3 [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
David Vrabel55b3da92015-06-24 15:58:42 +0100197static struct resource *additional_memory_resource(phys_addr_t size)
198{
199 struct resource *res;
200 int ret;
201
202 res = kzalloc(sizeof(*res), GFP_KERNEL);
203 if (!res)
204 return NULL;
205
206 res->name = "System RAM";
207 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
208
209 ret = allocate_resource(&iomem_resource, res,
210 size, 0, -1,
211 PAGES_PER_SECTION * PAGE_SIZE, NULL, NULL);
212 if (ret < 0) {
213 pr_err("Cannot allocate new System RAM resource\n");
214 kfree(res);
215 return NULL;
216 }
217
218 return res;
219}
220
221static void release_memory_resource(struct resource *resource)
222{
223 if (!resource)
224 return;
225
226 /*
227 * No need to reset region to identity mapped since we now
228 * know that no I/O can be in this region
229 */
230 release_resource(resource);
231 kfree(resource);
232}
Daniel Kiper080e2be2011-07-25 17:12:06 -0700233
234static enum bp_state reserve_additional_memory(long credit)
235{
David Vrabel55b3da92015-06-24 15:58:42 +0100236 struct resource *resource;
Daniel Kiper080e2be2011-07-25 17:12:06 -0700237 int nid, rc;
David Vrabel55b3da92015-06-24 15:58:42 +0100238 unsigned long balloon_hotplug;
Daniel Kiper080e2be2011-07-25 17:12:06 -0700239
David Vrabel55b3da92015-06-24 15:58:42 +0100240 balloon_hotplug = round_up(credit, PAGES_PER_SECTION);
241
242 resource = additional_memory_resource(balloon_hotplug * PAGE_SIZE);
243 if (!resource)
244 goto err;
245
246 nid = memory_add_physaddr_to_nid(resource->start);
Daniel Kiper080e2be2011-07-25 17:12:06 -0700247
Juergen Gross3c56b3a2015-03-20 13:55:39 +0100248#ifdef CONFIG_XEN_HAVE_PVMMU
249 /*
250 * add_memory() will build page tables for the new memory so
251 * the p2m must contain invalid entries so the correct
252 * non-present PTEs will be written.
253 *
254 * If a failure occurs, the original (identity) p2m entries
255 * are not restored since this region is now known not to
256 * conflict with any devices.
257 */
258 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
259 unsigned long pfn, i;
260
David Vrabel55b3da92015-06-24 15:58:42 +0100261 pfn = PFN_DOWN(resource->start);
Juergen Gross3c56b3a2015-03-20 13:55:39 +0100262 for (i = 0; i < balloon_hotplug; i++) {
263 if (!set_phys_to_machine(pfn + i, INVALID_P2M_ENTRY)) {
264 pr_warn("set_phys_to_machine() failed, no memory added\n");
David Vrabel55b3da92015-06-24 15:58:42 +0100265 goto err;
Juergen Gross3c56b3a2015-03-20 13:55:39 +0100266 }
267 }
268 }
269#endif
270
David Vrabel55b3da92015-06-24 15:58:42 +0100271 rc = add_memory_resource(nid, resource);
Daniel Kiper080e2be2011-07-25 17:12:06 -0700272 if (rc) {
David Vrabel3dcf6362014-09-01 18:52:44 +0100273 pr_warn("Cannot add additional memory (%i)\n", rc);
David Vrabel55b3da92015-06-24 15:58:42 +0100274 goto err;
Daniel Kiper080e2be2011-07-25 17:12:06 -0700275 }
276
David Vrabelde5a77d2015-06-25 12:08:20 +0100277 balloon_stats.total_pages += balloon_hotplug;
Daniel Kiper080e2be2011-07-25 17:12:06 -0700278
279 return BP_DONE;
David Vrabel55b3da92015-06-24 15:58:42 +0100280 err:
281 release_memory_resource(resource);
282 return BP_ECANCELED;
Daniel Kiper080e2be2011-07-25 17:12:06 -0700283}
284
285static void xen_online_page(struct page *page)
286{
287 __online_page_set_limits(page);
288
289 mutex_lock(&balloon_mutex);
290
291 __balloon_append(page);
292
Daniel Kiper080e2be2011-07-25 17:12:06 -0700293 mutex_unlock(&balloon_mutex);
294}
295
296static int xen_memory_notifier(struct notifier_block *nb, unsigned long val, void *v)
297{
298 if (val == MEM_ONLINE)
299 schedule_delayed_work(&balloon_worker, 0);
300
301 return NOTIFY_OK;
302}
303
304static struct notifier_block xen_memory_nb = {
305 .notifier_call = xen_memory_notifier,
306 .priority = 0
307};
308#else
Daniel Kiper080e2be2011-07-25 17:12:06 -0700309static enum bp_state reserve_additional_memory(long credit)
310{
311 balloon_stats.target_pages = balloon_stats.current_pages;
312 return BP_DONE;
313}
314#endif /* CONFIG_XEN_BALLOON_MEMORY_HOTPLUG */
315
David Vrabelde5a77d2015-06-25 12:08:20 +0100316static long current_credit(void)
317{
318 return balloon_stats.target_pages - balloon_stats.current_pages;
319}
320
321static bool balloon_is_inflated(void)
322{
323 return balloon_stats.balloon_low || balloon_stats.balloon_high;
324}
325
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100326static enum bp_state increase_reservation(unsigned long nr_pages)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700327{
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100328 int rc;
Jeremy Fitzhardinge2f70e0a2010-09-02 23:11:17 -0700329 unsigned long pfn, i;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700330 struct page *page;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700331 struct xen_memory_reservation reservation = {
332 .address_bits = 0,
333 .extent_order = 0,
334 .domid = DOMID_SELF
335 };
336
337 if (nr_pages > ARRAY_SIZE(frame_list))
338 nr_pages = ARRAY_SIZE(frame_list);
339
Jie Liu9346c2a2013-11-13 20:59:58 +0800340 page = list_first_entry_or_null(&ballooned_pages, struct page, lru);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700341 for (i = 0; i < nr_pages; i++) {
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100342 if (!page) {
343 nr_pages = i;
344 break;
345 }
Joe Perchesa419aef2009-08-18 11:18:35 -0700346 frame_list[i] = page_to_pfn(page);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700347 page = balloon_next_page(page);
348 }
349
Isaku Yamahataa90971e2008-05-26 23:31:14 +0100350 set_xen_guest_handle(reservation.extent_start, frame_list);
Jeremy Fitzhardingefde28e82008-07-24 16:28:00 -0700351 reservation.nr_extents = nr_pages;
352 rc = HYPERVISOR_memory_op(XENMEM_populate_physmap, &reservation);
Konrad Rzeszutek Wilk40095de2011-03-14 11:42:40 -0400353 if (rc <= 0)
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100354 return BP_EAGAIN;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700355
Ian Campbellbc2c0302009-06-05 11:58:37 +0100356 for (i = 0; i < rc; i++) {
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400357 page = balloon_retrieve(false);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700358 BUG_ON(page == NULL);
359
360 pfn = page_to_pfn(page);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700361
Ian Campbellc2374bf2012-10-03 12:17:50 +0100362#ifdef CONFIG_XEN_HAVE_PVMMU
Stefano Stabellinic1d15f52013-12-11 16:58:42 +0000363 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
364 set_phys_to_machine(pfn, frame_list[i]);
365
366 /* Link back into the page tables if not highmem. */
367 if (!PageHighMem(page)) {
368 int ret;
369 ret = HYPERVISOR_update_va_mapping(
370 (unsigned long)__va(pfn << PAGE_SHIFT),
371 mfn_pte(frame_list[i], PAGE_KERNEL),
372 0);
373 BUG_ON(ret);
374 }
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700375 }
Ian Campbellc2374bf2012-10-03 12:17:50 +0100376#endif
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700377
378 /* Relinquish the page back to the allocator. */
Jiang Liu3dcc0572013-07-03 15:03:21 -0700379 __free_reserved_page(page);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700380 }
381
Ian Campbellbc2c0302009-06-05 11:58:37 +0100382 balloon_stats.current_pages += rc;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700383
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100384 return BP_DONE;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700385}
386
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400387static enum bp_state decrease_reservation(unsigned long nr_pages, gfp_t gfp)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700388{
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100389 enum bp_state state = BP_DONE;
Jeremy Fitzhardinge2f70e0a2010-09-02 23:11:17 -0700390 unsigned long pfn, i;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700391 struct page *page;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700392 int ret;
393 struct xen_memory_reservation reservation = {
394 .address_bits = 0,
395 .extent_order = 0,
396 .domid = DOMID_SELF
397 };
398
399 if (nr_pages > ARRAY_SIZE(frame_list))
400 nr_pages = ARRAY_SIZE(frame_list);
401
402 for (i = 0; i < nr_pages; i++) {
Lisa Nguyenfce92682013-05-15 22:59:40 -0700403 page = alloc_page(gfp);
404 if (page == NULL) {
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700405 nr_pages = i;
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100406 state = BP_EAGAIN;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700407 break;
408 }
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700409 scrub_page(page);
Dan Magenheimer1058a752009-01-22 14:36:08 -0800410
Wei Liu09ed3d52014-03-15 16:11:47 +0000411 frame_list[i] = page_to_pfn(page);
412 }
413
414 /*
415 * Ensure that ballooned highmem pages don't have kmaps.
416 *
417 * Do this before changing the p2m as kmap_flush_unused()
418 * reads PTEs to obtain pages (and hence needs the original
419 * p2m entry).
420 */
421 kmap_flush_unused();
422
423 /* Update direct mapping, invalidate P2M, and add to balloon. */
424 for (i = 0; i < nr_pages; i++) {
425 pfn = frame_list[i];
Julien Grall0df4f262015-08-07 17:34:37 +0100426 frame_list[i] = pfn_to_gfn(pfn);
Wei Liu09ed3d52014-03-15 16:11:47 +0000427 page = pfn_to_page(pfn);
428
Stefano Stabellinic1d15f52013-12-11 16:58:42 +0000429#ifdef CONFIG_XEN_HAVE_PVMMU
Wei Liu04660bb2013-08-27 16:17:25 +0100430 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
Stefano Stabellinic1d15f52013-12-11 16:58:42 +0000431 if (!PageHighMem(page)) {
432 ret = HYPERVISOR_update_va_mapping(
433 (unsigned long)__va(pfn << PAGE_SHIFT),
David Vrabel0bb599f2015-01-05 17:06:01 +0000434 __pte_ma(0), 0);
Stefano Stabellinic1d15f52013-12-11 16:58:42 +0000435 BUG_ON(ret);
David Vrabelfb9a0c42014-06-27 10:42:03 +0100436 }
437 __set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
Wei Liu04660bb2013-08-27 16:17:25 +0100438 }
Stefano Stabellinic1d15f52013-12-11 16:58:42 +0000439#endif
David Vrabel24f69372013-09-19 17:14:53 +0100440
Wei Liu09ed3d52014-03-15 16:11:47 +0000441 balloon_append(page);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700442 }
443
David Vrabel24f69372013-09-19 17:14:53 +0100444 flush_tlb_all();
David Vrabel2bad07c2013-09-11 17:45:44 +0000445
Isaku Yamahataa90971e2008-05-26 23:31:14 +0100446 set_xen_guest_handle(reservation.extent_start, frame_list);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700447 reservation.nr_extents = nr_pages;
448 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
449 BUG_ON(ret != nr_pages);
450
451 balloon_stats.current_pages -= nr_pages;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700452
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100453 return state;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700454}
455
456/*
Juergen Gross929423f2015-07-20 13:49:39 +0200457 * As this is a work item it is guaranteed to run as a single instance only.
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700458 * We may of course race updates of the target counts (which are protected
459 * by the balloon lock), or with changes to the Xen hard limit, but we will
460 * recover from these in time.
461 */
462static void balloon_process(struct work_struct *work)
463{
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100464 enum bp_state state = BP_DONE;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700465 long credit;
466
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700467
468 do {
Juergen Gross929423f2015-07-20 13:49:39 +0200469 mutex_lock(&balloon_mutex);
470
Daniel Kiper83be7e52011-03-28 11:34:10 +0200471 credit = current_credit();
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100472
Daniel Kiper080e2be2011-07-25 17:12:06 -0700473 if (credit > 0) {
474 if (balloon_is_inflated())
475 state = increase_reservation(credit);
476 else
477 state = reserve_additional_memory(credit);
478 }
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100479
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700480 if (credit < 0)
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400481 state = decrease_reservation(-credit, GFP_BALLOON);
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100482
483 state = update_schedule(state);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700484
Juergen Gross929423f2015-07-20 13:49:39 +0200485 mutex_unlock(&balloon_mutex);
486
487 cond_resched();
488
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100489 } while (credit && state == BP_DONE);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700490
491 /* Schedule more work if there is some still to be done. */
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100492 if (state == BP_EAGAIN)
493 schedule_delayed_work(&balloon_worker, balloon_stats.schedule_delay * HZ);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700494}
495
496/* Resets the Xen limit, sets new target, and kicks off processing. */
Daniel De Graaf803eb042011-03-14 11:29:37 -0400497void balloon_set_new_target(unsigned long target)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700498{
499 /* No need for lock. Not read-modify-write updates. */
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700500 balloon_stats.target_pages = target;
Daniel Kiper95170b22011-03-08 22:47:39 +0100501 schedule_delayed_work(&balloon_worker, 0);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700502}
Daniel De Graaf803eb042011-03-14 11:29:37 -0400503EXPORT_SYMBOL_GPL(balloon_set_new_target);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700504
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400505/**
506 * alloc_xenballooned_pages - get pages that have been ballooned out
507 * @nr_pages: Number of pages to get
508 * @pages: pages returned
Daniel De Graaf72e9cf22011-10-19 17:59:37 -0400509 * @highmem: allow highmem pages
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400510 * @return 0 on success, error otherwise
511 */
Stefano Stabellini693394b2011-09-29 11:57:55 +0100512int alloc_xenballooned_pages(int nr_pages, struct page **pages, bool highmem)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700513{
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400514 int pgno = 0;
Ruslan Pisareve882dc92011-07-26 14:15:59 +0300515 struct page *page;
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400516 mutex_lock(&balloon_mutex);
517 while (pgno < nr_pages) {
Stefano Stabellini693394b2011-09-29 11:57:55 +0100518 page = balloon_retrieve(highmem);
Daniel De Graaf72e9cf22011-10-19 17:59:37 -0400519 if (page && (highmem || !PageHighMem(page))) {
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400520 pages[pgno++] = page;
521 } else {
522 enum bp_state st;
Stefano Stabellini693394b2011-09-29 11:57:55 +0100523 if (page)
524 balloon_append(page);
525 st = decrease_reservation(nr_pages - pgno,
526 highmem ? GFP_HIGHUSER : GFP_USER);
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400527 if (st != BP_DONE)
528 goto out_undo;
529 }
530 }
531 mutex_unlock(&balloon_mutex);
532 return 0;
533 out_undo:
534 while (pgno)
535 balloon_append(pages[--pgno]);
536 /* Free the memory back to the kernel soon */
537 schedule_delayed_work(&balloon_worker, 0);
538 mutex_unlock(&balloon_mutex);
539 return -ENOMEM;
540}
541EXPORT_SYMBOL(alloc_xenballooned_pages);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700542
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400543/**
544 * free_xenballooned_pages - return pages retrieved with get_ballooned_pages
545 * @nr_pages: Number of pages
546 * @pages: pages to return
547 */
Ruslan Pisareve882dc92011-07-26 14:15:59 +0300548void free_xenballooned_pages(int nr_pages, struct page **pages)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700549{
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400550 int i;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700551
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400552 mutex_lock(&balloon_mutex);
553
554 for (i = 0; i < nr_pages; i++) {
555 if (pages[i])
556 balloon_append(pages[i]);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700557 }
558
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400559 /* The balloon may be too large now. Shrink it if needed. */
Daniel Kiper83be7e52011-03-28 11:34:10 +0200560 if (current_credit())
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400561 schedule_delayed_work(&balloon_worker, 0);
562
563 mutex_unlock(&balloon_mutex);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700564}
Konrad Rzeszutek Wilkb6f30672011-03-15 10:23:57 -0400565EXPORT_SYMBOL(free_xenballooned_pages);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700566
David Vrabel8b5d44a2011-09-28 17:46:34 +0100567static void __init balloon_add_region(unsigned long start_pfn,
568 unsigned long pages)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700569{
Daniel Kiper4dfe22f2011-03-28 11:33:18 +0200570 unsigned long pfn, extra_pfn_end;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700571 struct page *page;
572
David Vrabel8b5d44a2011-09-28 17:46:34 +0100573 /*
574 * If the amount of usable memory has been limited (e.g., with
575 * the 'mem' command line parameter), don't add pages beyond
576 * this limit.
577 */
578 extra_pfn_end = min(max_pfn, start_pfn + pages);
579
580 for (pfn = start_pfn; pfn < extra_pfn_end; pfn++) {
581 page = pfn_to_page(pfn);
582 /* totalram_pages and totalhigh_pages do not
583 include the boot-time balloon extension, so
584 don't subtract from it. */
585 __balloon_append(page);
586 }
David Vrabelde5a77d2015-06-25 12:08:20 +0100587
588 balloon_stats.total_pages += extra_pfn_end - start_pfn;
David Vrabel8b5d44a2011-09-28 17:46:34 +0100589}
590
591static int __init balloon_init(void)
592{
David Vrabel0bb599f2015-01-05 17:06:01 +0000593 int i;
David Vrabel8b5d44a2011-09-28 17:46:34 +0100594
Stefano Stabellini53d55222010-12-02 17:55:05 +0000595 if (!xen_domain())
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700596 return -ENODEV;
597
Joe Perches283c0972013-06-28 03:21:41 -0700598 pr_info("Initialising balloon driver\n");
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700599
David Vrabelaa244112011-09-28 17:46:32 +0100600 balloon_stats.current_pages = xen_pv_domain()
601 ? min(xen_start_info->nr_pages - xen_released_pages, max_pfn)
Boris Ostrovskyc275a572013-11-06 15:37:40 -0500602 : get_num_physpages();
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700603 balloon_stats.target_pages = balloon_stats.current_pages;
604 balloon_stats.balloon_low = 0;
605 balloon_stats.balloon_high = 0;
David Vrabelde5a77d2015-06-25 12:08:20 +0100606 balloon_stats.total_pages = balloon_stats.current_pages;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700607
Daniel Kiper95d2ac42011-03-08 22:48:24 +0100608 balloon_stats.schedule_delay = 1;
609 balloon_stats.max_schedule_delay = 32;
610 balloon_stats.retry_count = 1;
Konrad Rzeszutek Wilk40095de2011-03-14 11:42:40 -0400611 balloon_stats.max_retry_count = RETRY_UNLIMITED;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700612
Daniel Kiper080e2be2011-07-25 17:12:06 -0700613#ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
Daniel Kiper080e2be2011-07-25 17:12:06 -0700614 set_online_page_callback(&xen_online_page);
615 register_memory_notifier(&xen_memory_nb);
616#endif
617
Jeremy Fitzhardinge2a4c92f2010-12-02 15:30:06 -0800618 /*
David Vrabelb1cbf9b2011-09-28 17:46:33 +0100619 * Initialize the balloon with pages from the extra memory
David Vrabel8b5d44a2011-09-28 17:46:34 +0100620 * regions (see arch/x86/xen/setup.c).
Jeremy Fitzhardinge2a4c92f2010-12-02 15:30:06 -0800621 */
David Vrabel8b5d44a2011-09-28 17:46:34 +0100622 for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++)
Juergen Gross626d7502015-09-04 14:05:51 +0200623 if (xen_extra_mem[i].n_pfns)
624 balloon_add_region(xen_extra_mem[i].start_pfn,
625 xen_extra_mem[i].n_pfns);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700626
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700627 return 0;
628}
629
630subsys_initcall(balloon_init);
631
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700632MODULE_LICENSE("GPL");