xref: /OK3568_Linux_fs/kernel/drivers/xen/balloon.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /******************************************************************************
2*4882a593Smuzhiyun  * Xen balloon driver - enables returning/claiming memory to/from Xen.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (c) 2003, B Dragovic
5*4882a593Smuzhiyun  * Copyright (c) 2003-2004, M Williamson, K Fraser
6*4882a593Smuzhiyun  * Copyright (c) 2005 Dan M. Smith, IBM Corporation
7*4882a593Smuzhiyun  * Copyright (c) 2010 Daniel Kiper
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Memory hotplug support was written by Daniel Kiper. Work on
10*4882a593Smuzhiyun  * it was sponsored by Google under Google Summer of Code 2010
11*4882a593Smuzhiyun  * program. Jeremy Fitzhardinge from Citrix was the mentor for
12*4882a593Smuzhiyun  * this project.
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or
15*4882a593Smuzhiyun  * modify it under the terms of the GNU General Public License version 2
16*4882a593Smuzhiyun  * as published by the Free Software Foundation; or, when distributed
17*4882a593Smuzhiyun  * separately from the Linux kernel or incorporated into other
18*4882a593Smuzhiyun  * software packages, subject to the following license:
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person obtaining a copy
21*4882a593Smuzhiyun  * of this source file (the "Software"), to deal in the Software without
22*4882a593Smuzhiyun  * restriction, including without limitation the rights to use, copy, modify,
23*4882a593Smuzhiyun  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
24*4882a593Smuzhiyun  * and to permit persons to whom the Software is furnished to do so, subject to
25*4882a593Smuzhiyun  * the following conditions:
26*4882a593Smuzhiyun  *
27*4882a593Smuzhiyun  * The above copyright notice and this permission notice shall be included in
28*4882a593Smuzhiyun  * all copies or substantial portions of the Software.
29*4882a593Smuzhiyun  *
30*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31*4882a593Smuzhiyun  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32*4882a593Smuzhiyun  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33*4882a593Smuzhiyun  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34*4882a593Smuzhiyun  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
35*4882a593Smuzhiyun  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
36*4882a593Smuzhiyun  * IN THE SOFTWARE.
37*4882a593Smuzhiyun  */
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun #include <linux/cpu.h>
42*4882a593Smuzhiyun #include <linux/kernel.h>
43*4882a593Smuzhiyun #include <linux/sched.h>
44*4882a593Smuzhiyun #include <linux/cred.h>
45*4882a593Smuzhiyun #include <linux/errno.h>
46*4882a593Smuzhiyun #include <linux/freezer.h>
47*4882a593Smuzhiyun #include <linux/kthread.h>
48*4882a593Smuzhiyun #include <linux/mm.h>
49*4882a593Smuzhiyun #include <linux/memblock.h>
50*4882a593Smuzhiyun #include <linux/pagemap.h>
51*4882a593Smuzhiyun #include <linux/highmem.h>
52*4882a593Smuzhiyun #include <linux/mutex.h>
53*4882a593Smuzhiyun #include <linux/list.h>
54*4882a593Smuzhiyun #include <linux/gfp.h>
55*4882a593Smuzhiyun #include <linux/notifier.h>
56*4882a593Smuzhiyun #include <linux/memory.h>
57*4882a593Smuzhiyun #include <linux/memory_hotplug.h>
58*4882a593Smuzhiyun #include <linux/percpu-defs.h>
59*4882a593Smuzhiyun #include <linux/slab.h>
60*4882a593Smuzhiyun #include <linux/sysctl.h>
61*4882a593Smuzhiyun #include <linux/moduleparam.h>
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun #include <asm/page.h>
64*4882a593Smuzhiyun #include <asm/tlb.h>
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun #include <asm/xen/hypervisor.h>
67*4882a593Smuzhiyun #include <asm/xen/hypercall.h>
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun #include <xen/xen.h>
70*4882a593Smuzhiyun #include <xen/interface/xen.h>
71*4882a593Smuzhiyun #include <xen/interface/memory.h>
72*4882a593Smuzhiyun #include <xen/balloon.h>
73*4882a593Smuzhiyun #include <xen/features.h>
74*4882a593Smuzhiyun #include <xen/page.h>
75*4882a593Smuzhiyun #include <xen/mem-reservation.h>
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun #undef MODULE_PARAM_PREFIX
78*4882a593Smuzhiyun #define MODULE_PARAM_PREFIX "xen."
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun static uint __read_mostly balloon_boot_timeout = 180;
81*4882a593Smuzhiyun module_param(balloon_boot_timeout, uint, 0444);
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun static int xen_hotplug_unpopulated;
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun static struct ctl_table balloon_table[] = {
88*4882a593Smuzhiyun 	{
89*4882a593Smuzhiyun 		.procname	= "hotplug_unpopulated",
90*4882a593Smuzhiyun 		.data		= &xen_hotplug_unpopulated,
91*4882a593Smuzhiyun 		.maxlen		= sizeof(int),
92*4882a593Smuzhiyun 		.mode		= 0644,
93*4882a593Smuzhiyun 		.proc_handler	= proc_dointvec_minmax,
94*4882a593Smuzhiyun 		.extra1         = SYSCTL_ZERO,
95*4882a593Smuzhiyun 		.extra2         = SYSCTL_ONE,
96*4882a593Smuzhiyun 	},
97*4882a593Smuzhiyun 	{ }
98*4882a593Smuzhiyun };
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun static struct ctl_table balloon_root[] = {
101*4882a593Smuzhiyun 	{
102*4882a593Smuzhiyun 		.procname	= "balloon",
103*4882a593Smuzhiyun 		.mode		= 0555,
104*4882a593Smuzhiyun 		.child		= balloon_table,
105*4882a593Smuzhiyun 	},
106*4882a593Smuzhiyun 	{ }
107*4882a593Smuzhiyun };
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun static struct ctl_table xen_root[] = {
110*4882a593Smuzhiyun 	{
111*4882a593Smuzhiyun 		.procname	= "xen",
112*4882a593Smuzhiyun 		.mode		= 0555,
113*4882a593Smuzhiyun 		.child		= balloon_root,
114*4882a593Smuzhiyun 	},
115*4882a593Smuzhiyun 	{ }
116*4882a593Smuzhiyun };
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun #endif
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun /*
121*4882a593Smuzhiyun  * Use one extent per PAGE_SIZE to avoid to break down the page into
122*4882a593Smuzhiyun  * multiple frame.
123*4882a593Smuzhiyun  */
124*4882a593Smuzhiyun #define EXTENT_ORDER (fls(XEN_PFN_PER_PAGE) - 1)
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun /*
127*4882a593Smuzhiyun  * balloon_thread() state:
128*4882a593Smuzhiyun  *
129*4882a593Smuzhiyun  * BP_DONE: done or nothing to do,
130*4882a593Smuzhiyun  * BP_WAIT: wait to be rescheduled,
131*4882a593Smuzhiyun  * BP_EAGAIN: error, go to sleep,
132*4882a593Smuzhiyun  * BP_ECANCELED: error, balloon operation canceled.
133*4882a593Smuzhiyun  */
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun static enum bp_state {
136*4882a593Smuzhiyun 	BP_DONE,
137*4882a593Smuzhiyun 	BP_WAIT,
138*4882a593Smuzhiyun 	BP_EAGAIN,
139*4882a593Smuzhiyun 	BP_ECANCELED
140*4882a593Smuzhiyun } balloon_state = BP_DONE;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun /* Main waiting point for xen-balloon thread. */
143*4882a593Smuzhiyun static DECLARE_WAIT_QUEUE_HEAD(balloon_thread_wq);
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun static DEFINE_MUTEX(balloon_mutex);
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun struct balloon_stats balloon_stats;
148*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(balloon_stats);
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun /* We increase/decrease in batches which fit in a page */
151*4882a593Smuzhiyun static xen_pfn_t frame_list[PAGE_SIZE / sizeof(xen_pfn_t)];
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun /* List of ballooned pages, threaded through the mem_map array. */
155*4882a593Smuzhiyun static LIST_HEAD(ballooned_pages);
156*4882a593Smuzhiyun static DECLARE_WAIT_QUEUE_HEAD(balloon_wq);
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun /* When ballooning out (allocating memory to return to Xen) we don't really
159*4882a593Smuzhiyun    want the kernel to try too hard since that can trigger the oom killer. */
160*4882a593Smuzhiyun #define GFP_BALLOON \
161*4882a593Smuzhiyun 	(GFP_HIGHUSER | __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC)
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun /* balloon_append: add the given page to the balloon. */
balloon_append(struct page * page)164*4882a593Smuzhiyun static void balloon_append(struct page *page)
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun 	__SetPageOffline(page);
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	/* Lowmem is re-populated first, so highmem pages go at list tail. */
169*4882a593Smuzhiyun 	if (PageHighMem(page)) {
170*4882a593Smuzhiyun 		list_add_tail(&page->lru, &ballooned_pages);
171*4882a593Smuzhiyun 		balloon_stats.balloon_high++;
172*4882a593Smuzhiyun 	} else {
173*4882a593Smuzhiyun 		list_add(&page->lru, &ballooned_pages);
174*4882a593Smuzhiyun 		balloon_stats.balloon_low++;
175*4882a593Smuzhiyun 	}
176*4882a593Smuzhiyun 	wake_up(&balloon_wq);
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun /* balloon_retrieve: rescue a page from the balloon, if it is not empty. */
balloon_retrieve(bool require_lowmem)180*4882a593Smuzhiyun static struct page *balloon_retrieve(bool require_lowmem)
181*4882a593Smuzhiyun {
182*4882a593Smuzhiyun 	struct page *page;
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	if (list_empty(&ballooned_pages))
185*4882a593Smuzhiyun 		return NULL;
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun 	page = list_entry(ballooned_pages.next, struct page, lru);
188*4882a593Smuzhiyun 	if (require_lowmem && PageHighMem(page))
189*4882a593Smuzhiyun 		return NULL;
190*4882a593Smuzhiyun 	list_del(&page->lru);
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	if (PageHighMem(page))
193*4882a593Smuzhiyun 		balloon_stats.balloon_high--;
194*4882a593Smuzhiyun 	else
195*4882a593Smuzhiyun 		balloon_stats.balloon_low--;
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	__ClearPageOffline(page);
198*4882a593Smuzhiyun 	return page;
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun 
balloon_next_page(struct page * page)201*4882a593Smuzhiyun static struct page *balloon_next_page(struct page *page)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun 	struct list_head *next = page->lru.next;
204*4882a593Smuzhiyun 	if (next == &ballooned_pages)
205*4882a593Smuzhiyun 		return NULL;
206*4882a593Smuzhiyun 	return list_entry(next, struct page, lru);
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun 
update_schedule(void)209*4882a593Smuzhiyun static void update_schedule(void)
210*4882a593Smuzhiyun {
211*4882a593Smuzhiyun 	if (balloon_state == BP_WAIT || balloon_state == BP_ECANCELED)
212*4882a593Smuzhiyun 		return;
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	if (balloon_state == BP_DONE) {
215*4882a593Smuzhiyun 		balloon_stats.schedule_delay = 1;
216*4882a593Smuzhiyun 		balloon_stats.retry_count = 1;
217*4882a593Smuzhiyun 		return;
218*4882a593Smuzhiyun 	}
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 	++balloon_stats.retry_count;
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	if (balloon_stats.max_retry_count != RETRY_UNLIMITED &&
223*4882a593Smuzhiyun 			balloon_stats.retry_count > balloon_stats.max_retry_count) {
224*4882a593Smuzhiyun 		balloon_stats.schedule_delay = 1;
225*4882a593Smuzhiyun 		balloon_stats.retry_count = 1;
226*4882a593Smuzhiyun 		balloon_state = BP_ECANCELED;
227*4882a593Smuzhiyun 		return;
228*4882a593Smuzhiyun 	}
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	balloon_stats.schedule_delay <<= 1;
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	if (balloon_stats.schedule_delay > balloon_stats.max_schedule_delay)
233*4882a593Smuzhiyun 		balloon_stats.schedule_delay = balloon_stats.max_schedule_delay;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	balloon_state = BP_EAGAIN;
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
release_memory_resource(struct resource * resource)239*4882a593Smuzhiyun static void release_memory_resource(struct resource *resource)
240*4882a593Smuzhiyun {
241*4882a593Smuzhiyun 	if (!resource)
242*4882a593Smuzhiyun 		return;
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	/*
245*4882a593Smuzhiyun 	 * No need to reset region to identity mapped since we now
246*4882a593Smuzhiyun 	 * know that no I/O can be in this region
247*4882a593Smuzhiyun 	 */
248*4882a593Smuzhiyun 	release_resource(resource);
249*4882a593Smuzhiyun 	kfree(resource);
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun 
additional_memory_resource(phys_addr_t size)252*4882a593Smuzhiyun static struct resource *additional_memory_resource(phys_addr_t size)
253*4882a593Smuzhiyun {
254*4882a593Smuzhiyun 	struct resource *res;
255*4882a593Smuzhiyun 	int ret;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	res = kzalloc(sizeof(*res), GFP_KERNEL);
258*4882a593Smuzhiyun 	if (!res)
259*4882a593Smuzhiyun 		return NULL;
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	res->name = "System RAM";
262*4882a593Smuzhiyun 	res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	ret = allocate_resource(&iomem_resource, res,
265*4882a593Smuzhiyun 				size, 0, -1,
266*4882a593Smuzhiyun 				PAGES_PER_SECTION * PAGE_SIZE, NULL, NULL);
267*4882a593Smuzhiyun 	if (ret < 0) {
268*4882a593Smuzhiyun 		pr_err("Cannot allocate new System RAM resource\n");
269*4882a593Smuzhiyun 		kfree(res);
270*4882a593Smuzhiyun 		return NULL;
271*4882a593Smuzhiyun 	}
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	return res;
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun 
reserve_additional_memory(void)276*4882a593Smuzhiyun static enum bp_state reserve_additional_memory(void)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun 	long credit;
279*4882a593Smuzhiyun 	struct resource *resource;
280*4882a593Smuzhiyun 	int nid, rc;
281*4882a593Smuzhiyun 	unsigned long balloon_hotplug;
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 	credit = balloon_stats.target_pages + balloon_stats.target_unpopulated
284*4882a593Smuzhiyun 		- balloon_stats.total_pages;
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 	/*
287*4882a593Smuzhiyun 	 * Already hotplugged enough pages?  Wait for them to be
288*4882a593Smuzhiyun 	 * onlined.
289*4882a593Smuzhiyun 	 */
290*4882a593Smuzhiyun 	if (credit <= 0)
291*4882a593Smuzhiyun 		return BP_WAIT;
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 	balloon_hotplug = round_up(credit, PAGES_PER_SECTION);
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	resource = additional_memory_resource(balloon_hotplug * PAGE_SIZE);
296*4882a593Smuzhiyun 	if (!resource)
297*4882a593Smuzhiyun 		goto err;
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 	nid = memory_add_physaddr_to_nid(resource->start);
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun #ifdef CONFIG_XEN_HAVE_PVMMU
302*4882a593Smuzhiyun 	/*
303*4882a593Smuzhiyun 	 * We don't support PV MMU when Linux and Xen is using
304*4882a593Smuzhiyun 	 * different page granularity.
305*4882a593Smuzhiyun 	 */
306*4882a593Smuzhiyun 	BUILD_BUG_ON(XEN_PAGE_SIZE != PAGE_SIZE);
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun         /*
309*4882a593Smuzhiyun          * add_memory() will build page tables for the new memory so
310*4882a593Smuzhiyun          * the p2m must contain invalid entries so the correct
311*4882a593Smuzhiyun          * non-present PTEs will be written.
312*4882a593Smuzhiyun          *
313*4882a593Smuzhiyun          * If a failure occurs, the original (identity) p2m entries
314*4882a593Smuzhiyun          * are not restored since this region is now known not to
315*4882a593Smuzhiyun          * conflict with any devices.
316*4882a593Smuzhiyun          */
317*4882a593Smuzhiyun 	if (!xen_feature(XENFEAT_auto_translated_physmap)) {
318*4882a593Smuzhiyun 		unsigned long pfn, i;
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 		pfn = PFN_DOWN(resource->start);
321*4882a593Smuzhiyun 		for (i = 0; i < balloon_hotplug; i++) {
322*4882a593Smuzhiyun 			if (!set_phys_to_machine(pfn + i, INVALID_P2M_ENTRY)) {
323*4882a593Smuzhiyun 				pr_warn("set_phys_to_machine() failed, no memory added\n");
324*4882a593Smuzhiyun 				goto err;
325*4882a593Smuzhiyun 			}
326*4882a593Smuzhiyun                 }
327*4882a593Smuzhiyun 	}
328*4882a593Smuzhiyun #endif
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	/*
331*4882a593Smuzhiyun 	 * add_memory_resource() will call online_pages() which in its turn
332*4882a593Smuzhiyun 	 * will call xen_online_page() callback causing deadlock if we don't
333*4882a593Smuzhiyun 	 * release balloon_mutex here. Unlocking here is safe because the
334*4882a593Smuzhiyun 	 * callers drop the mutex before trying again.
335*4882a593Smuzhiyun 	 */
336*4882a593Smuzhiyun 	mutex_unlock(&balloon_mutex);
337*4882a593Smuzhiyun 	/* add_memory_resource() requires the device_hotplug lock */
338*4882a593Smuzhiyun 	lock_device_hotplug();
339*4882a593Smuzhiyun 	rc = add_memory_resource(nid, resource, MEMHP_MERGE_RESOURCE);
340*4882a593Smuzhiyun 	unlock_device_hotplug();
341*4882a593Smuzhiyun 	mutex_lock(&balloon_mutex);
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 	if (rc) {
344*4882a593Smuzhiyun 		pr_warn("Cannot add additional memory (%i)\n", rc);
345*4882a593Smuzhiyun 		goto err;
346*4882a593Smuzhiyun 	}
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	balloon_stats.total_pages += balloon_hotplug;
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 	return BP_WAIT;
351*4882a593Smuzhiyun   err:
352*4882a593Smuzhiyun 	release_memory_resource(resource);
353*4882a593Smuzhiyun 	return BP_ECANCELED;
354*4882a593Smuzhiyun }
355*4882a593Smuzhiyun 
xen_online_page(struct page * page,unsigned int order)356*4882a593Smuzhiyun static void xen_online_page(struct page *page, unsigned int order)
357*4882a593Smuzhiyun {
358*4882a593Smuzhiyun 	unsigned long i, size = (1 << order);
359*4882a593Smuzhiyun 	unsigned long start_pfn = page_to_pfn(page);
360*4882a593Smuzhiyun 	struct page *p;
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun 	pr_debug("Online %lu pages starting at pfn 0x%lx\n", size, start_pfn);
363*4882a593Smuzhiyun 	mutex_lock(&balloon_mutex);
364*4882a593Smuzhiyun 	for (i = 0; i < size; i++) {
365*4882a593Smuzhiyun 		p = pfn_to_page(start_pfn + i);
366*4882a593Smuzhiyun 		balloon_append(p);
367*4882a593Smuzhiyun 	}
368*4882a593Smuzhiyun 	mutex_unlock(&balloon_mutex);
369*4882a593Smuzhiyun }
370*4882a593Smuzhiyun 
xen_memory_notifier(struct notifier_block * nb,unsigned long val,void * v)371*4882a593Smuzhiyun static int xen_memory_notifier(struct notifier_block *nb, unsigned long val, void *v)
372*4882a593Smuzhiyun {
373*4882a593Smuzhiyun 	if (val == MEM_ONLINE)
374*4882a593Smuzhiyun 		wake_up(&balloon_thread_wq);
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	return NOTIFY_OK;
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun static struct notifier_block xen_memory_nb = {
380*4882a593Smuzhiyun 	.notifier_call = xen_memory_notifier,
381*4882a593Smuzhiyun 	.priority = 0
382*4882a593Smuzhiyun };
383*4882a593Smuzhiyun #else
reserve_additional_memory(void)384*4882a593Smuzhiyun static enum bp_state reserve_additional_memory(void)
385*4882a593Smuzhiyun {
386*4882a593Smuzhiyun 	balloon_stats.target_pages = balloon_stats.current_pages +
387*4882a593Smuzhiyun 				     balloon_stats.target_unpopulated;
388*4882a593Smuzhiyun 	return BP_ECANCELED;
389*4882a593Smuzhiyun }
390*4882a593Smuzhiyun #endif /* CONFIG_XEN_BALLOON_MEMORY_HOTPLUG */
391*4882a593Smuzhiyun 
current_credit(void)392*4882a593Smuzhiyun static long current_credit(void)
393*4882a593Smuzhiyun {
394*4882a593Smuzhiyun 	return balloon_stats.target_pages - balloon_stats.current_pages;
395*4882a593Smuzhiyun }
396*4882a593Smuzhiyun 
balloon_is_inflated(void)397*4882a593Smuzhiyun static bool balloon_is_inflated(void)
398*4882a593Smuzhiyun {
399*4882a593Smuzhiyun 	return balloon_stats.balloon_low || balloon_stats.balloon_high;
400*4882a593Smuzhiyun }
401*4882a593Smuzhiyun 
increase_reservation(unsigned long nr_pages)402*4882a593Smuzhiyun static enum bp_state increase_reservation(unsigned long nr_pages)
403*4882a593Smuzhiyun {
404*4882a593Smuzhiyun 	int rc;
405*4882a593Smuzhiyun 	unsigned long i;
406*4882a593Smuzhiyun 	struct page   *page;
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 	if (nr_pages > ARRAY_SIZE(frame_list))
409*4882a593Smuzhiyun 		nr_pages = ARRAY_SIZE(frame_list);
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun 	page = list_first_entry_or_null(&ballooned_pages, struct page, lru);
412*4882a593Smuzhiyun 	for (i = 0; i < nr_pages; i++) {
413*4882a593Smuzhiyun 		if (!page) {
414*4882a593Smuzhiyun 			nr_pages = i;
415*4882a593Smuzhiyun 			break;
416*4882a593Smuzhiyun 		}
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 		frame_list[i] = page_to_xen_pfn(page);
419*4882a593Smuzhiyun 		page = balloon_next_page(page);
420*4882a593Smuzhiyun 	}
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 	rc = xenmem_reservation_increase(nr_pages, frame_list);
423*4882a593Smuzhiyun 	if (rc <= 0)
424*4882a593Smuzhiyun 		return BP_EAGAIN;
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun 	for (i = 0; i < rc; i++) {
427*4882a593Smuzhiyun 		page = balloon_retrieve(false);
428*4882a593Smuzhiyun 		BUG_ON(page == NULL);
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 		xenmem_reservation_va_mapping_update(1, &page, &frame_list[i]);
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 		/* Relinquish the page back to the allocator. */
433*4882a593Smuzhiyun 		free_reserved_page(page);
434*4882a593Smuzhiyun 	}
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	balloon_stats.current_pages += rc;
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun 	return BP_DONE;
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun 
decrease_reservation(unsigned long nr_pages,gfp_t gfp)441*4882a593Smuzhiyun static enum bp_state decrease_reservation(unsigned long nr_pages, gfp_t gfp)
442*4882a593Smuzhiyun {
443*4882a593Smuzhiyun 	enum bp_state state = BP_DONE;
444*4882a593Smuzhiyun 	unsigned long i;
445*4882a593Smuzhiyun 	struct page *page, *tmp;
446*4882a593Smuzhiyun 	int ret;
447*4882a593Smuzhiyun 	LIST_HEAD(pages);
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun 	if (nr_pages > ARRAY_SIZE(frame_list))
450*4882a593Smuzhiyun 		nr_pages = ARRAY_SIZE(frame_list);
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 	for (i = 0; i < nr_pages; i++) {
453*4882a593Smuzhiyun 		page = alloc_page(gfp);
454*4882a593Smuzhiyun 		if (page == NULL) {
455*4882a593Smuzhiyun 			nr_pages = i;
456*4882a593Smuzhiyun 			state = BP_EAGAIN;
457*4882a593Smuzhiyun 			break;
458*4882a593Smuzhiyun 		}
459*4882a593Smuzhiyun 		adjust_managed_page_count(page, -1);
460*4882a593Smuzhiyun 		xenmem_reservation_scrub_page(page);
461*4882a593Smuzhiyun 		list_add(&page->lru, &pages);
462*4882a593Smuzhiyun 	}
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun 	/*
465*4882a593Smuzhiyun 	 * Ensure that ballooned highmem pages don't have kmaps.
466*4882a593Smuzhiyun 	 *
467*4882a593Smuzhiyun 	 * Do this before changing the p2m as kmap_flush_unused()
468*4882a593Smuzhiyun 	 * reads PTEs to obtain pages (and hence needs the original
469*4882a593Smuzhiyun 	 * p2m entry).
470*4882a593Smuzhiyun 	 */
471*4882a593Smuzhiyun 	kmap_flush_unused();
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 	/*
474*4882a593Smuzhiyun 	 * Setup the frame, update direct mapping, invalidate P2M,
475*4882a593Smuzhiyun 	 * and add to balloon.
476*4882a593Smuzhiyun 	 */
477*4882a593Smuzhiyun 	i = 0;
478*4882a593Smuzhiyun 	list_for_each_entry_safe(page, tmp, &pages, lru) {
479*4882a593Smuzhiyun 		frame_list[i++] = xen_page_to_gfn(page);
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun 		xenmem_reservation_va_mapping_reset(1, &page);
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 		list_del(&page->lru);
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 		balloon_append(page);
486*4882a593Smuzhiyun 	}
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun 	flush_tlb_all();
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 	ret = xenmem_reservation_decrease(nr_pages, frame_list);
491*4882a593Smuzhiyun 	BUG_ON(ret != nr_pages);
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun 	balloon_stats.current_pages -= nr_pages;
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 	return state;
496*4882a593Smuzhiyun }
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun /*
499*4882a593Smuzhiyun  * Stop waiting if either state is BP_DONE and ballooning action is
500*4882a593Smuzhiyun  * needed, or if the credit has changed while state is not BP_DONE.
501*4882a593Smuzhiyun  */
balloon_thread_cond(long credit)502*4882a593Smuzhiyun static bool balloon_thread_cond(long credit)
503*4882a593Smuzhiyun {
504*4882a593Smuzhiyun 	if (balloon_state == BP_DONE)
505*4882a593Smuzhiyun 		credit = 0;
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 	return current_credit() != credit || kthread_should_stop();
508*4882a593Smuzhiyun }
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun /*
511*4882a593Smuzhiyun  * As this is a kthread it is guaranteed to run as a single instance only.
512*4882a593Smuzhiyun  * We may of course race updates of the target counts (which are protected
513*4882a593Smuzhiyun  * by the balloon lock), or with changes to the Xen hard limit, but we will
514*4882a593Smuzhiyun  * recover from these in time.
515*4882a593Smuzhiyun  */
balloon_thread(void * unused)516*4882a593Smuzhiyun static int balloon_thread(void *unused)
517*4882a593Smuzhiyun {
518*4882a593Smuzhiyun 	long credit;
519*4882a593Smuzhiyun 	unsigned long timeout;
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 	set_freezable();
522*4882a593Smuzhiyun 	for (;;) {
523*4882a593Smuzhiyun 		switch (balloon_state) {
524*4882a593Smuzhiyun 		case BP_DONE:
525*4882a593Smuzhiyun 		case BP_ECANCELED:
526*4882a593Smuzhiyun 			timeout = 3600 * HZ;
527*4882a593Smuzhiyun 			break;
528*4882a593Smuzhiyun 		case BP_EAGAIN:
529*4882a593Smuzhiyun 			timeout = balloon_stats.schedule_delay * HZ;
530*4882a593Smuzhiyun 			break;
531*4882a593Smuzhiyun 		case BP_WAIT:
532*4882a593Smuzhiyun 			timeout = HZ;
533*4882a593Smuzhiyun 			break;
534*4882a593Smuzhiyun 		}
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 		credit = current_credit();
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 		wait_event_freezable_timeout(balloon_thread_wq,
539*4882a593Smuzhiyun 			balloon_thread_cond(credit), timeout);
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 		if (kthread_should_stop())
542*4882a593Smuzhiyun 			return 0;
543*4882a593Smuzhiyun 
544*4882a593Smuzhiyun 		mutex_lock(&balloon_mutex);
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun 		credit = current_credit();
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 		if (credit > 0) {
549*4882a593Smuzhiyun 			if (balloon_is_inflated())
550*4882a593Smuzhiyun 				balloon_state = increase_reservation(credit);
551*4882a593Smuzhiyun 			else
552*4882a593Smuzhiyun 				balloon_state = reserve_additional_memory();
553*4882a593Smuzhiyun 		}
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun 		if (credit < 0) {
556*4882a593Smuzhiyun 			long n_pages;
557*4882a593Smuzhiyun 
558*4882a593Smuzhiyun 			n_pages = min(-credit, si_mem_available());
559*4882a593Smuzhiyun 			balloon_state = decrease_reservation(n_pages,
560*4882a593Smuzhiyun 							     GFP_BALLOON);
561*4882a593Smuzhiyun 			if (balloon_state == BP_DONE && n_pages != -credit &&
562*4882a593Smuzhiyun 			    n_pages < totalreserve_pages)
563*4882a593Smuzhiyun 				balloon_state = BP_EAGAIN;
564*4882a593Smuzhiyun 		}
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun 		update_schedule();
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun 		mutex_unlock(&balloon_mutex);
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun 		cond_resched();
571*4882a593Smuzhiyun 	}
572*4882a593Smuzhiyun }
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun /* Resets the Xen limit, sets new target, and kicks off processing. */
balloon_set_new_target(unsigned long target)575*4882a593Smuzhiyun void balloon_set_new_target(unsigned long target)
576*4882a593Smuzhiyun {
577*4882a593Smuzhiyun 	/* No need for lock. Not read-modify-write updates. */
578*4882a593Smuzhiyun 	balloon_stats.target_pages = target;
579*4882a593Smuzhiyun 	wake_up(&balloon_thread_wq);
580*4882a593Smuzhiyun }
581*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(balloon_set_new_target);
582*4882a593Smuzhiyun 
add_ballooned_pages(int nr_pages)583*4882a593Smuzhiyun static int add_ballooned_pages(int nr_pages)
584*4882a593Smuzhiyun {
585*4882a593Smuzhiyun 	enum bp_state st;
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun 	if (xen_hotplug_unpopulated) {
588*4882a593Smuzhiyun 		st = reserve_additional_memory();
589*4882a593Smuzhiyun 		if (st != BP_ECANCELED) {
590*4882a593Smuzhiyun 			int rc;
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun 			mutex_unlock(&balloon_mutex);
593*4882a593Smuzhiyun 			rc = wait_event_interruptible(balloon_wq,
594*4882a593Smuzhiyun 				   !list_empty(&ballooned_pages));
595*4882a593Smuzhiyun 			mutex_lock(&balloon_mutex);
596*4882a593Smuzhiyun 			return rc ? -ENOMEM : 0;
597*4882a593Smuzhiyun 		}
598*4882a593Smuzhiyun 	}
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 	if (si_mem_available() < nr_pages)
601*4882a593Smuzhiyun 		return -ENOMEM;
602*4882a593Smuzhiyun 
603*4882a593Smuzhiyun 	st = decrease_reservation(nr_pages, GFP_USER);
604*4882a593Smuzhiyun 	if (st != BP_DONE)
605*4882a593Smuzhiyun 		return -ENOMEM;
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun 	return 0;
608*4882a593Smuzhiyun }
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun /**
611*4882a593Smuzhiyun  * alloc_xenballooned_pages - get pages that have been ballooned out
612*4882a593Smuzhiyun  * @nr_pages: Number of pages to get
613*4882a593Smuzhiyun  * @pages: pages returned
614*4882a593Smuzhiyun  * @return 0 on success, error otherwise
615*4882a593Smuzhiyun  */
alloc_xenballooned_pages(int nr_pages,struct page ** pages)616*4882a593Smuzhiyun int alloc_xenballooned_pages(int nr_pages, struct page **pages)
617*4882a593Smuzhiyun {
618*4882a593Smuzhiyun 	int pgno = 0;
619*4882a593Smuzhiyun 	struct page *page;
620*4882a593Smuzhiyun 	int ret;
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 	mutex_lock(&balloon_mutex);
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 	balloon_stats.target_unpopulated += nr_pages;
625*4882a593Smuzhiyun 
626*4882a593Smuzhiyun 	while (pgno < nr_pages) {
627*4882a593Smuzhiyun 		page = balloon_retrieve(true);
628*4882a593Smuzhiyun 		if (page) {
629*4882a593Smuzhiyun 			pages[pgno++] = page;
630*4882a593Smuzhiyun #ifdef CONFIG_XEN_HAVE_PVMMU
631*4882a593Smuzhiyun 			/*
632*4882a593Smuzhiyun 			 * We don't support PV MMU when Linux and Xen is using
633*4882a593Smuzhiyun 			 * different page granularity.
634*4882a593Smuzhiyun 			 */
635*4882a593Smuzhiyun 			BUILD_BUG_ON(XEN_PAGE_SIZE != PAGE_SIZE);
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun 			if (!xen_feature(XENFEAT_auto_translated_physmap)) {
638*4882a593Smuzhiyun 				ret = xen_alloc_p2m_entry(page_to_pfn(page));
639*4882a593Smuzhiyun 				if (ret < 0)
640*4882a593Smuzhiyun 					goto out_undo;
641*4882a593Smuzhiyun 			}
642*4882a593Smuzhiyun #endif
643*4882a593Smuzhiyun 		} else {
644*4882a593Smuzhiyun 			ret = add_ballooned_pages(nr_pages - pgno);
645*4882a593Smuzhiyun 			if (ret < 0)
646*4882a593Smuzhiyun 				goto out_undo;
647*4882a593Smuzhiyun 		}
648*4882a593Smuzhiyun 	}
649*4882a593Smuzhiyun 	mutex_unlock(&balloon_mutex);
650*4882a593Smuzhiyun 	return 0;
651*4882a593Smuzhiyun  out_undo:
652*4882a593Smuzhiyun 	mutex_unlock(&balloon_mutex);
653*4882a593Smuzhiyun 	free_xenballooned_pages(pgno, pages);
654*4882a593Smuzhiyun 	/*
655*4882a593Smuzhiyun 	 * NB: free_xenballooned_pages will only subtract pgno pages, but since
656*4882a593Smuzhiyun 	 * target_unpopulated is incremented with nr_pages at the start we need
657*4882a593Smuzhiyun 	 * to remove the remaining ones also, or accounting will be screwed.
658*4882a593Smuzhiyun 	 */
659*4882a593Smuzhiyun 	balloon_stats.target_unpopulated -= nr_pages - pgno;
660*4882a593Smuzhiyun 	return ret;
661*4882a593Smuzhiyun }
662*4882a593Smuzhiyun EXPORT_SYMBOL(alloc_xenballooned_pages);
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun /**
665*4882a593Smuzhiyun  * free_xenballooned_pages - return pages retrieved with get_ballooned_pages
666*4882a593Smuzhiyun  * @nr_pages: Number of pages
667*4882a593Smuzhiyun  * @pages: pages to return
668*4882a593Smuzhiyun  */
free_xenballooned_pages(int nr_pages,struct page ** pages)669*4882a593Smuzhiyun void free_xenballooned_pages(int nr_pages, struct page **pages)
670*4882a593Smuzhiyun {
671*4882a593Smuzhiyun 	int i;
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun 	mutex_lock(&balloon_mutex);
674*4882a593Smuzhiyun 
675*4882a593Smuzhiyun 	for (i = 0; i < nr_pages; i++) {
676*4882a593Smuzhiyun 		if (pages[i])
677*4882a593Smuzhiyun 			balloon_append(pages[i]);
678*4882a593Smuzhiyun 	}
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	balloon_stats.target_unpopulated -= nr_pages;
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun 	/* The balloon may be too large now. Shrink it if needed. */
683*4882a593Smuzhiyun 	if (current_credit())
684*4882a593Smuzhiyun 		wake_up(&balloon_thread_wq);
685*4882a593Smuzhiyun 
686*4882a593Smuzhiyun 	mutex_unlock(&balloon_mutex);
687*4882a593Smuzhiyun }
688*4882a593Smuzhiyun EXPORT_SYMBOL(free_xenballooned_pages);
689*4882a593Smuzhiyun 
690*4882a593Smuzhiyun #if defined(CONFIG_XEN_PV) && !defined(CONFIG_XEN_UNPOPULATED_ALLOC)
balloon_add_region(unsigned long start_pfn,unsigned long pages)691*4882a593Smuzhiyun static void __init balloon_add_region(unsigned long start_pfn,
692*4882a593Smuzhiyun 				      unsigned long pages)
693*4882a593Smuzhiyun {
694*4882a593Smuzhiyun 	unsigned long pfn, extra_pfn_end;
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 	/*
697*4882a593Smuzhiyun 	 * If the amount of usable memory has been limited (e.g., with
698*4882a593Smuzhiyun 	 * the 'mem' command line parameter), don't add pages beyond
699*4882a593Smuzhiyun 	 * this limit.
700*4882a593Smuzhiyun 	 */
701*4882a593Smuzhiyun 	extra_pfn_end = min(max_pfn, start_pfn + pages);
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun 	for (pfn = start_pfn; pfn < extra_pfn_end; pfn++) {
704*4882a593Smuzhiyun 		/* totalram_pages and totalhigh_pages do not
705*4882a593Smuzhiyun 		   include the boot-time balloon extension, so
706*4882a593Smuzhiyun 		   don't subtract from it. */
707*4882a593Smuzhiyun 		balloon_append(pfn_to_page(pfn));
708*4882a593Smuzhiyun 	}
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun 	balloon_stats.total_pages += extra_pfn_end - start_pfn;
711*4882a593Smuzhiyun }
712*4882a593Smuzhiyun #endif
713*4882a593Smuzhiyun 
balloon_init(void)714*4882a593Smuzhiyun static int __init balloon_init(void)
715*4882a593Smuzhiyun {
716*4882a593Smuzhiyun 	struct task_struct *task;
717*4882a593Smuzhiyun 
718*4882a593Smuzhiyun 	if (!xen_domain())
719*4882a593Smuzhiyun 		return -ENODEV;
720*4882a593Smuzhiyun 
721*4882a593Smuzhiyun 	pr_info("Initialising balloon driver\n");
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun #ifdef CONFIG_XEN_PV
724*4882a593Smuzhiyun 	balloon_stats.current_pages = xen_pv_domain()
725*4882a593Smuzhiyun 		? min(xen_start_info->nr_pages - xen_released_pages, max_pfn)
726*4882a593Smuzhiyun 		: get_num_physpages();
727*4882a593Smuzhiyun #else
728*4882a593Smuzhiyun 	balloon_stats.current_pages = get_num_physpages();
729*4882a593Smuzhiyun #endif
730*4882a593Smuzhiyun 	balloon_stats.target_pages  = balloon_stats.current_pages;
731*4882a593Smuzhiyun 	balloon_stats.balloon_low   = 0;
732*4882a593Smuzhiyun 	balloon_stats.balloon_high  = 0;
733*4882a593Smuzhiyun 	balloon_stats.total_pages   = balloon_stats.current_pages;
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 	balloon_stats.schedule_delay = 1;
736*4882a593Smuzhiyun 	balloon_stats.max_schedule_delay = 32;
737*4882a593Smuzhiyun 	balloon_stats.retry_count = 1;
738*4882a593Smuzhiyun 	balloon_stats.max_retry_count = 4;
739*4882a593Smuzhiyun 
740*4882a593Smuzhiyun #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
741*4882a593Smuzhiyun 	set_online_page_callback(&xen_online_page);
742*4882a593Smuzhiyun 	register_memory_notifier(&xen_memory_nb);
743*4882a593Smuzhiyun 	register_sysctl_table(xen_root);
744*4882a593Smuzhiyun #endif
745*4882a593Smuzhiyun 
746*4882a593Smuzhiyun #if defined(CONFIG_XEN_PV) && !defined(CONFIG_XEN_UNPOPULATED_ALLOC)
747*4882a593Smuzhiyun 	{
748*4882a593Smuzhiyun 		int i;
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 		/*
751*4882a593Smuzhiyun 		 * Initialize the balloon with pages from the extra memory
752*4882a593Smuzhiyun 		 * regions (see arch/x86/xen/setup.c).
753*4882a593Smuzhiyun 		 */
754*4882a593Smuzhiyun 		for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++)
755*4882a593Smuzhiyun 			if (xen_extra_mem[i].n_pfns)
756*4882a593Smuzhiyun 				balloon_add_region(xen_extra_mem[i].start_pfn,
757*4882a593Smuzhiyun 						   xen_extra_mem[i].n_pfns);
758*4882a593Smuzhiyun 	}
759*4882a593Smuzhiyun #endif
760*4882a593Smuzhiyun 
761*4882a593Smuzhiyun 	task = kthread_run(balloon_thread, NULL, "xen-balloon");
762*4882a593Smuzhiyun 	if (IS_ERR(task)) {
763*4882a593Smuzhiyun 		pr_err("xen-balloon thread could not be started, ballooning will not work!\n");
764*4882a593Smuzhiyun 		return PTR_ERR(task);
765*4882a593Smuzhiyun 	}
766*4882a593Smuzhiyun 
767*4882a593Smuzhiyun 	/* Init the xen-balloon driver. */
768*4882a593Smuzhiyun 	xen_balloon_init();
769*4882a593Smuzhiyun 
770*4882a593Smuzhiyun 	return 0;
771*4882a593Smuzhiyun }
772*4882a593Smuzhiyun subsys_initcall(balloon_init);
773*4882a593Smuzhiyun 
balloon_wait_finish(void)774*4882a593Smuzhiyun static int __init balloon_wait_finish(void)
775*4882a593Smuzhiyun {
776*4882a593Smuzhiyun 	long credit, last_credit = 0;
777*4882a593Smuzhiyun 	unsigned long last_changed = 0;
778*4882a593Smuzhiyun 
779*4882a593Smuzhiyun 	if (!xen_domain())
780*4882a593Smuzhiyun 		return -ENODEV;
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 	/* PV guests don't need to wait. */
783*4882a593Smuzhiyun 	if (xen_pv_domain() || !current_credit())
784*4882a593Smuzhiyun 		return 0;
785*4882a593Smuzhiyun 
786*4882a593Smuzhiyun 	pr_notice("Waiting for initial ballooning down having finished.\n");
787*4882a593Smuzhiyun 
788*4882a593Smuzhiyun 	while ((credit = current_credit()) < 0) {
789*4882a593Smuzhiyun 		if (credit != last_credit) {
790*4882a593Smuzhiyun 			last_changed = jiffies;
791*4882a593Smuzhiyun 			last_credit = credit;
792*4882a593Smuzhiyun 		}
793*4882a593Smuzhiyun 		if (balloon_state == BP_ECANCELED) {
794*4882a593Smuzhiyun 			pr_warn_once("Initial ballooning failed, %ld pages need to be freed.\n",
795*4882a593Smuzhiyun 				     -credit);
796*4882a593Smuzhiyun 			if (jiffies - last_changed >= HZ * balloon_boot_timeout)
797*4882a593Smuzhiyun 				panic("Initial ballooning failed!\n");
798*4882a593Smuzhiyun 		}
799*4882a593Smuzhiyun 
800*4882a593Smuzhiyun 		schedule_timeout_interruptible(HZ / 10);
801*4882a593Smuzhiyun 	}
802*4882a593Smuzhiyun 
803*4882a593Smuzhiyun 	pr_notice("Initial ballooning down finished.\n");
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun 	return 0;
806*4882a593Smuzhiyun }
807*4882a593Smuzhiyun late_initcall_sync(balloon_wait_finish);
808