xen-balloon.c (e4da3fbfbd1de56d2367653e3823e6445e49f8a9) xen-balloon.c (070680218379e15c1901f4bf21b98e3cbf12b527)
1/******************************************************************************
2 * 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
7 *
8 * This program is free software; you can redistribute it and/or

--- 18 unchanged lines hidden (view full) ---

27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30 * IN THE SOFTWARE.
31 */
32
33#include <linux/kernel.h>
34#include <linux/module.h>
1/******************************************************************************
2 * 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
7 *
8 * This program is free software; you can redistribute it and/or

--- 18 unchanged lines hidden (view full) ---

27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30 * IN THE SOFTWARE.
31 */
32
33#include <linux/kernel.h>
34#include <linux/module.h>
35#include <linux/sysdev.h>
36#include <linux/capability.h>
37
38#include <xen/xen.h>
39#include <xen/interface/xen.h>
40#include <xen/balloon.h>
41#include <xen/xenbus.h>
42#include <xen/features.h>
43#include <xen/page.h>
44
45#define PAGES2KB(_p) ((_p)<<(PAGE_SHIFT-10))
46
47#define BALLOON_CLASS_NAME "xen_memory"
48
35#include <linux/capability.h>
36
37#include <xen/xen.h>
38#include <xen/interface/xen.h>
39#include <xen/balloon.h>
40#include <xen/xenbus.h>
41#include <xen/features.h>
42#include <xen/page.h>
43
44#define PAGES2KB(_p) ((_p)<<(PAGE_SHIFT-10))
45
46#define BALLOON_CLASS_NAME "xen_memory"
47
49static struct sys_device balloon_sysdev;
48static struct device balloon_dev;
50
49
51static int register_balloon(struct sys_device *sysdev);
50static int register_balloon(struct device *dev);
52
53/* React to a change in the target key */
54static void watch_target(struct xenbus_watch *watch,
55 const char **vec, unsigned int len)
56{
57 unsigned long long new_target;
58 int err;
59

--- 33 unchanged lines hidden (view full) ---

93
94static int __init balloon_init(void)
95{
96 if (!xen_domain())
97 return -ENODEV;
98
99 pr_info("xen-balloon: Initialising balloon driver.\n");
100
51
52/* React to a change in the target key */
53static void watch_target(struct xenbus_watch *watch,
54 const char **vec, unsigned int len)
55{
56 unsigned long long new_target;
57 int err;
58

--- 33 unchanged lines hidden (view full) ---

92
93static int __init balloon_init(void)
94{
95 if (!xen_domain())
96 return -ENODEV;
97
98 pr_info("xen-balloon: Initialising balloon driver.\n");
99
101 register_balloon(&balloon_sysdev);
100 register_balloon(&balloon_dev);
102
101
103 register_xen_selfballooning(&balloon_sysdev);
102 register_xen_selfballooning(&balloon_dev);
104
105 register_xenstore_notifier(&xenstore_notifier);
106
107 return 0;
108}
109subsys_initcall(balloon_init);
110
111static void balloon_exit(void)
112{
113 /* XXX - release balloon here */
114 return;
115}
116
117module_exit(balloon_exit);
118
119#define BALLOON_SHOW(name, format, args...) \
103
104 register_xenstore_notifier(&xenstore_notifier);
105
106 return 0;
107}
108subsys_initcall(balloon_init);
109
110static void balloon_exit(void)
111{
112 /* XXX - release balloon here */
113 return;
114}
115
116module_exit(balloon_exit);
117
118#define BALLOON_SHOW(name, format, args...) \
120 static ssize_t show_##name(struct sys_device *dev, \
121 struct sysdev_attribute *attr, \
119 static ssize_t show_##name(struct device *dev, \
120 struct device_attribute *attr, \
122 char *buf) \
123 { \
124 return sprintf(buf, format, ##args); \
125 } \
121 char *buf) \
122 { \
123 return sprintf(buf, format, ##args); \
124 } \
126 static SYSDEV_ATTR(name, S_IRUGO, show_##name, NULL)
125 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
127
128BALLOON_SHOW(current_kb, "%lu\n", PAGES2KB(balloon_stats.current_pages));
129BALLOON_SHOW(low_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_low));
130BALLOON_SHOW(high_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_high));
131
126
127BALLOON_SHOW(current_kb, "%lu\n", PAGES2KB(balloon_stats.current_pages));
128BALLOON_SHOW(low_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_low));
129BALLOON_SHOW(high_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_high));
130
132static SYSDEV_ULONG_ATTR(schedule_delay, 0444, balloon_stats.schedule_delay);
133static SYSDEV_ULONG_ATTR(max_schedule_delay, 0644, balloon_stats.max_schedule_delay);
134static SYSDEV_ULONG_ATTR(retry_count, 0444, balloon_stats.retry_count);
135static SYSDEV_ULONG_ATTR(max_retry_count, 0644, balloon_stats.max_retry_count);
131static DEVICE_ULONG_ATTR(schedule_delay, 0444, balloon_stats.schedule_delay);
132static DEVICE_ULONG_ATTR(max_schedule_delay, 0644, balloon_stats.max_schedule_delay);
133static DEVICE_ULONG_ATTR(retry_count, 0444, balloon_stats.retry_count);
134static DEVICE_ULONG_ATTR(max_retry_count, 0644, balloon_stats.max_retry_count);
136
135
137static ssize_t show_target_kb(struct sys_device *dev, struct sysdev_attribute *attr,
136static ssize_t show_target_kb(struct device *dev, struct device_attribute *attr,
138 char *buf)
139{
140 return sprintf(buf, "%lu\n", PAGES2KB(balloon_stats.target_pages));
141}
142
137 char *buf)
138{
139 return sprintf(buf, "%lu\n", PAGES2KB(balloon_stats.target_pages));
140}
141
143static ssize_t store_target_kb(struct sys_device *dev,
144 struct sysdev_attribute *attr,
142static ssize_t store_target_kb(struct device *dev,
143 struct device_attribute *attr,
145 const char *buf,
146 size_t count)
147{
148 char *endchar;
149 unsigned long long target_bytes;
150
151 if (!capable(CAP_SYS_ADMIN))
152 return -EPERM;
153
154 target_bytes = simple_strtoull(buf, &endchar, 0) * 1024;
155
156 balloon_set_new_target(target_bytes >> PAGE_SHIFT);
157
158 return count;
159}
160
144 const char *buf,
145 size_t count)
146{
147 char *endchar;
148 unsigned long long target_bytes;
149
150 if (!capable(CAP_SYS_ADMIN))
151 return -EPERM;
152
153 target_bytes = simple_strtoull(buf, &endchar, 0) * 1024;
154
155 balloon_set_new_target(target_bytes >> PAGE_SHIFT);
156
157 return count;
158}
159
161static SYSDEV_ATTR(target_kb, S_IRUGO | S_IWUSR,
160static DEVICE_ATTR(target_kb, S_IRUGO | S_IWUSR,
162 show_target_kb, store_target_kb);
163
164
161 show_target_kb, store_target_kb);
162
163
165static ssize_t show_target(struct sys_device *dev, struct sysdev_attribute *attr,
164static ssize_t show_target(struct device *dev, struct device_attribute *attr,
166 char *buf)
167{
168 return sprintf(buf, "%llu\n",
169 (unsigned long long)balloon_stats.target_pages
170 << PAGE_SHIFT);
171}
172
165 char *buf)
166{
167 return sprintf(buf, "%llu\n",
168 (unsigned long long)balloon_stats.target_pages
169 << PAGE_SHIFT);
170}
171
173static ssize_t store_target(struct sys_device *dev,
174 struct sysdev_attribute *attr,
172static ssize_t store_target(struct device *dev,
173 struct device_attribute *attr,
175 const char *buf,
176 size_t count)
177{
178 char *endchar;
179 unsigned long long target_bytes;
180
181 if (!capable(CAP_SYS_ADMIN))
182 return -EPERM;
183
184 target_bytes = memparse(buf, &endchar);
185
186 balloon_set_new_target(target_bytes >> PAGE_SHIFT);
187
188 return count;
189}
190
174 const char *buf,
175 size_t count)
176{
177 char *endchar;
178 unsigned long long target_bytes;
179
180 if (!capable(CAP_SYS_ADMIN))
181 return -EPERM;
182
183 target_bytes = memparse(buf, &endchar);
184
185 balloon_set_new_target(target_bytes >> PAGE_SHIFT);
186
187 return count;
188}
189
191static SYSDEV_ATTR(target, S_IRUGO | S_IWUSR,
190static DEVICE_ATTR(target, S_IRUGO | S_IWUSR,
192 show_target, store_target);
193
194
191 show_target, store_target);
192
193
195static struct sysdev_attribute *balloon_attrs[] = {
196 &attr_target_kb,
197 &attr_target,
198 &attr_schedule_delay.attr,
199 &attr_max_schedule_delay.attr,
200 &attr_retry_count.attr,
201 &attr_max_retry_count.attr
194static struct device_attribute *balloon_attrs[] = {
195 &dev_attr_target_kb,
196 &dev_attr_target,
197 &dev_attr_schedule_delay.attr,
198 &dev_attr_max_schedule_delay.attr,
199 &dev_attr_retry_count.attr,
200 &dev_attr_max_retry_count.attr
202};
203
204static struct attribute *balloon_info_attrs[] = {
201};
202
203static struct attribute *balloon_info_attrs[] = {
205 &attr_current_kb.attr,
206 &attr_low_kb.attr,
207 &attr_high_kb.attr,
204 &dev_attr_current_kb.attr,
205 &dev_attr_low_kb.attr,
206 &dev_attr_high_kb.attr,
208 NULL
209};
210
211static struct attribute_group balloon_info_group = {
212 .name = "info",
213 .attrs = balloon_info_attrs
214};
215
207 NULL
208};
209
210static struct attribute_group balloon_info_group = {
211 .name = "info",
212 .attrs = balloon_info_attrs
213};
214
216static struct sysdev_class balloon_sysdev_class = {
217 .name = BALLOON_CLASS_NAME
215static struct bus_type balloon_subsys = {
216 .name = BALLOON_CLASS_NAME,
217 .dev_name = BALLOON_CLASS_NAME,
218};
219
218};
219
220static int register_balloon(struct sys_device *sysdev)
220static int register_balloon(struct device *dev)
221{
222 int i, error;
223
221{
222 int i, error;
223
224 error = sysdev_class_register(&balloon_sysdev_class);
224 error = bus_register(&balloon_subsys);
225 if (error)
226 return error;
227
225 if (error)
226 return error;
227
228 sysdev->id = 0;
229 sysdev->cls = &balloon_sysdev_class;
228 dev->id = 0;
229 dev->bus = &balloon_subsys;
230
230
231 error = sysdev_register(sysdev);
231 error = device_register(dev);
232 if (error) {
232 if (error) {
233 sysdev_class_unregister(&balloon_sysdev_class);
233 bus_unregister(&balloon_subsys);
234 return error;
235 }
236
237 for (i = 0; i < ARRAY_SIZE(balloon_attrs); i++) {
234 return error;
235 }
236
237 for (i = 0; i < ARRAY_SIZE(balloon_attrs); i++) {
238 error = sysdev_create_file(sysdev, balloon_attrs[i]);
238 error = device_create_file(dev, balloon_attrs[i]);
239 if (error)
240 goto fail;
241 }
242
239 if (error)
240 goto fail;
241 }
242
243 error = sysfs_create_group(&sysdev->kobj, &balloon_info_group);
243 error = sysfs_create_group(&dev->kobj, &balloon_info_group);
244 if (error)
245 goto fail;
246
247 return 0;
248
249 fail:
250 while (--i >= 0)
244 if (error)
245 goto fail;
246
247 return 0;
248
249 fail:
250 while (--i >= 0)
251 sysdev_remove_file(sysdev, balloon_attrs[i]);
252 sysdev_unregister(sysdev);
253 sysdev_class_unregister(&balloon_sysdev_class);
251 device_remove_file(dev, balloon_attrs[i]);
252 device_unregister(dev);
253 bus_unregister(&balloon_subsys);
254 return error;
255}
256
257MODULE_LICENSE("GPL");
254 return error;
255}
256
257MODULE_LICENSE("GPL");