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 *
8*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or
9*4882a593Smuzhiyun * modify it under the terms of the GNU General Public License version 2
10*4882a593Smuzhiyun * as published by the Free Software Foundation; or, when distributed
11*4882a593Smuzhiyun * separately from the Linux kernel or incorporated into other
12*4882a593Smuzhiyun * software packages, subject to the following license:
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * Permission is hereby granted, free of charge, to any person obtaining a copy
15*4882a593Smuzhiyun * of this source file (the "Software"), to deal in the Software without
16*4882a593Smuzhiyun * restriction, including without limitation the rights to use, copy, modify,
17*4882a593Smuzhiyun * merge, publish, distribute, sublicense, and/or sell copies of the Software,
18*4882a593Smuzhiyun * and to permit persons to whom the Software is furnished to do so, subject to
19*4882a593Smuzhiyun * the following conditions:
20*4882a593Smuzhiyun *
21*4882a593Smuzhiyun * The above copyright notice and this permission notice shall be included in
22*4882a593Smuzhiyun * all copies or substantial portions of the Software.
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25*4882a593Smuzhiyun * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26*4882a593Smuzhiyun * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27*4882a593Smuzhiyun * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28*4882a593Smuzhiyun * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29*4882a593Smuzhiyun * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30*4882a593Smuzhiyun * IN THE SOFTWARE.
31*4882a593Smuzhiyun */
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun #include <linux/kernel.h>
36*4882a593Smuzhiyun #include <linux/errno.h>
37*4882a593Smuzhiyun #include <linux/mm_types.h>
38*4882a593Smuzhiyun #include <linux/init.h>
39*4882a593Smuzhiyun #include <linux/capability.h>
40*4882a593Smuzhiyun #include <linux/memory_hotplug.h>
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun #include <xen/xen.h>
43*4882a593Smuzhiyun #include <xen/interface/xen.h>
44*4882a593Smuzhiyun #include <xen/balloon.h>
45*4882a593Smuzhiyun #include <xen/xenbus.h>
46*4882a593Smuzhiyun #include <xen/features.h>
47*4882a593Smuzhiyun #include <xen/page.h>
48*4882a593Smuzhiyun #include <xen/mem-reservation.h>
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun #define PAGES2KB(_p) ((_p)<<(PAGE_SHIFT-10))
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun #define BALLOON_CLASS_NAME "xen_memory"
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun #ifdef CONFIG_MEMORY_HOTPLUG
55*4882a593Smuzhiyun u64 xen_saved_max_mem_size = 0;
56*4882a593Smuzhiyun #endif
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun static struct device balloon_dev;
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun static int register_balloon(struct device *dev);
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun /* React to a change in the target key */
watch_target(struct xenbus_watch * watch,const char * path,const char * token)63*4882a593Smuzhiyun static void watch_target(struct xenbus_watch *watch,
64*4882a593Smuzhiyun const char *path, const char *token)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun unsigned long long new_target, static_max;
67*4882a593Smuzhiyun int err;
68*4882a593Smuzhiyun static bool watch_fired;
69*4882a593Smuzhiyun static long target_diff;
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun #ifdef CONFIG_MEMORY_HOTPLUG
72*4882a593Smuzhiyun /* The balloon driver will take care of adding memory now. */
73*4882a593Smuzhiyun if (xen_saved_max_mem_size)
74*4882a593Smuzhiyun max_mem_size = xen_saved_max_mem_size;
75*4882a593Smuzhiyun #endif
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun err = xenbus_scanf(XBT_NIL, "memory", "target", "%llu", &new_target);
78*4882a593Smuzhiyun if (err != 1) {
79*4882a593Smuzhiyun /* This is ok (for domain0 at least) - so just return */
80*4882a593Smuzhiyun return;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun /* The given memory/target value is in KiB, so it needs converting to
84*4882a593Smuzhiyun * pages. PAGE_SHIFT converts bytes to pages, hence PAGE_SHIFT - 10.
85*4882a593Smuzhiyun */
86*4882a593Smuzhiyun new_target >>= PAGE_SHIFT - 10;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun if (!watch_fired) {
89*4882a593Smuzhiyun watch_fired = true;
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun if ((xenbus_scanf(XBT_NIL, "memory", "static-max",
92*4882a593Smuzhiyun "%llu", &static_max) == 1) ||
93*4882a593Smuzhiyun (xenbus_scanf(XBT_NIL, "memory", "memory_static_max",
94*4882a593Smuzhiyun "%llu", &static_max) == 1))
95*4882a593Smuzhiyun static_max >>= PAGE_SHIFT - 10;
96*4882a593Smuzhiyun else
97*4882a593Smuzhiyun static_max = balloon_stats.current_pages;
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun target_diff = (xen_pv_domain() || xen_initial_domain()) ? 0
100*4882a593Smuzhiyun : static_max - balloon_stats.target_pages;
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun balloon_set_new_target(new_target - target_diff);
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun static struct xenbus_watch target_watch = {
106*4882a593Smuzhiyun .node = "memory/target",
107*4882a593Smuzhiyun .callback = watch_target,
108*4882a593Smuzhiyun };
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun
balloon_init_watcher(struct notifier_block * notifier,unsigned long event,void * data)111*4882a593Smuzhiyun static int balloon_init_watcher(struct notifier_block *notifier,
112*4882a593Smuzhiyun unsigned long event,
113*4882a593Smuzhiyun void *data)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun int err;
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun err = register_xenbus_watch(&target_watch);
118*4882a593Smuzhiyun if (err)
119*4882a593Smuzhiyun pr_err("Failed to set balloon watcher\n");
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun return NOTIFY_DONE;
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun static struct notifier_block xenstore_notifier = {
125*4882a593Smuzhiyun .notifier_call = balloon_init_watcher,
126*4882a593Smuzhiyun };
127*4882a593Smuzhiyun
xen_balloon_init(void)128*4882a593Smuzhiyun void xen_balloon_init(void)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun register_balloon(&balloon_dev);
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun register_xenstore_notifier(&xenstore_notifier);
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xen_balloon_init);
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun #define BALLOON_SHOW(name, format, args...) \
137*4882a593Smuzhiyun static ssize_t show_##name(struct device *dev, \
138*4882a593Smuzhiyun struct device_attribute *attr, \
139*4882a593Smuzhiyun char *buf) \
140*4882a593Smuzhiyun { \
141*4882a593Smuzhiyun return sprintf(buf, format, ##args); \
142*4882a593Smuzhiyun } \
143*4882a593Smuzhiyun static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun BALLOON_SHOW(current_kb, "%lu\n", PAGES2KB(balloon_stats.current_pages));
146*4882a593Smuzhiyun BALLOON_SHOW(low_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_low));
147*4882a593Smuzhiyun BALLOON_SHOW(high_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_high));
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun static DEVICE_ULONG_ATTR(schedule_delay, 0444, balloon_stats.schedule_delay);
150*4882a593Smuzhiyun static DEVICE_ULONG_ATTR(max_schedule_delay, 0644, balloon_stats.max_schedule_delay);
151*4882a593Smuzhiyun static DEVICE_ULONG_ATTR(retry_count, 0444, balloon_stats.retry_count);
152*4882a593Smuzhiyun static DEVICE_ULONG_ATTR(max_retry_count, 0644, balloon_stats.max_retry_count);
153*4882a593Smuzhiyun static DEVICE_BOOL_ATTR(scrub_pages, 0644, xen_scrub_pages);
154*4882a593Smuzhiyun
show_target_kb(struct device * dev,struct device_attribute * attr,char * buf)155*4882a593Smuzhiyun static ssize_t show_target_kb(struct device *dev, struct device_attribute *attr,
156*4882a593Smuzhiyun char *buf)
157*4882a593Smuzhiyun {
158*4882a593Smuzhiyun return sprintf(buf, "%lu\n", PAGES2KB(balloon_stats.target_pages));
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun
store_target_kb(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)161*4882a593Smuzhiyun static ssize_t store_target_kb(struct device *dev,
162*4882a593Smuzhiyun struct device_attribute *attr,
163*4882a593Smuzhiyun const char *buf,
164*4882a593Smuzhiyun size_t count)
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun char *endchar;
167*4882a593Smuzhiyun unsigned long long target_bytes;
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun if (!capable(CAP_SYS_ADMIN))
170*4882a593Smuzhiyun return -EPERM;
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun target_bytes = simple_strtoull(buf, &endchar, 0) * 1024;
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun balloon_set_new_target(target_bytes >> PAGE_SHIFT);
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun return count;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun static DEVICE_ATTR(target_kb, S_IRUGO | S_IWUSR,
180*4882a593Smuzhiyun show_target_kb, store_target_kb);
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun
show_target(struct device * dev,struct device_attribute * attr,char * buf)183*4882a593Smuzhiyun static ssize_t show_target(struct device *dev, struct device_attribute *attr,
184*4882a593Smuzhiyun char *buf)
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun return sprintf(buf, "%llu\n",
187*4882a593Smuzhiyun (unsigned long long)balloon_stats.target_pages
188*4882a593Smuzhiyun << PAGE_SHIFT);
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun
store_target(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)191*4882a593Smuzhiyun static ssize_t store_target(struct device *dev,
192*4882a593Smuzhiyun struct device_attribute *attr,
193*4882a593Smuzhiyun const char *buf,
194*4882a593Smuzhiyun size_t count)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun char *endchar;
197*4882a593Smuzhiyun unsigned long long target_bytes;
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun if (!capable(CAP_SYS_ADMIN))
200*4882a593Smuzhiyun return -EPERM;
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun target_bytes = memparse(buf, &endchar);
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun balloon_set_new_target(target_bytes >> PAGE_SHIFT);
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun return count;
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun static DEVICE_ATTR(target, S_IRUGO | S_IWUSR,
210*4882a593Smuzhiyun show_target, store_target);
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun static struct attribute *balloon_attrs[] = {
214*4882a593Smuzhiyun &dev_attr_target_kb.attr,
215*4882a593Smuzhiyun &dev_attr_target.attr,
216*4882a593Smuzhiyun &dev_attr_schedule_delay.attr.attr,
217*4882a593Smuzhiyun &dev_attr_max_schedule_delay.attr.attr,
218*4882a593Smuzhiyun &dev_attr_retry_count.attr.attr,
219*4882a593Smuzhiyun &dev_attr_max_retry_count.attr.attr,
220*4882a593Smuzhiyun &dev_attr_scrub_pages.attr.attr,
221*4882a593Smuzhiyun NULL
222*4882a593Smuzhiyun };
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun static const struct attribute_group balloon_group = {
225*4882a593Smuzhiyun .attrs = balloon_attrs
226*4882a593Smuzhiyun };
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun static struct attribute *balloon_info_attrs[] = {
229*4882a593Smuzhiyun &dev_attr_current_kb.attr,
230*4882a593Smuzhiyun &dev_attr_low_kb.attr,
231*4882a593Smuzhiyun &dev_attr_high_kb.attr,
232*4882a593Smuzhiyun NULL
233*4882a593Smuzhiyun };
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun static const struct attribute_group balloon_info_group = {
236*4882a593Smuzhiyun .name = "info",
237*4882a593Smuzhiyun .attrs = balloon_info_attrs
238*4882a593Smuzhiyun };
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun static const struct attribute_group *balloon_groups[] = {
241*4882a593Smuzhiyun &balloon_group,
242*4882a593Smuzhiyun &balloon_info_group,
243*4882a593Smuzhiyun NULL
244*4882a593Smuzhiyun };
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun static struct bus_type balloon_subsys = {
247*4882a593Smuzhiyun .name = BALLOON_CLASS_NAME,
248*4882a593Smuzhiyun .dev_name = BALLOON_CLASS_NAME,
249*4882a593Smuzhiyun };
250*4882a593Smuzhiyun
register_balloon(struct device * dev)251*4882a593Smuzhiyun static int register_balloon(struct device *dev)
252*4882a593Smuzhiyun {
253*4882a593Smuzhiyun int error;
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun error = subsys_system_register(&balloon_subsys, NULL);
256*4882a593Smuzhiyun if (error)
257*4882a593Smuzhiyun return error;
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun dev->id = 0;
260*4882a593Smuzhiyun dev->bus = &balloon_subsys;
261*4882a593Smuzhiyun dev->groups = balloon_groups;
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun error = device_register(dev);
264*4882a593Smuzhiyun if (error) {
265*4882a593Smuzhiyun bus_unregister(&balloon_subsys);
266*4882a593Smuzhiyun return error;
267*4882a593Smuzhiyun }
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun return 0;
270*4882a593Smuzhiyun }
271