blob: 9206ff7514e7b294e2e734a0d08496313136830d [file] [log] [blame]
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -07001/******************************************************************************
2 * balloon.c
3 *
4 * Xen balloon driver - enables returning/claiming memory to/from Xen.
5 *
6 * Copyright (c) 2003, B Dragovic
7 * Copyright (c) 2003-2004, M Williamson, K Fraser
8 * Copyright (c) 2005 Dan M. Smith, IBM Corporation
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation; or, when distributed
13 * separately from the Linux kernel or incorporated into other
14 * software packages, subject to the following license:
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a copy
17 * of this source file (the "Software"), to deal in the Software without
18 * restriction, including without limitation the rights to use, copy, modify,
19 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
20 * and to permit persons to whom the Software is furnished to do so, subject to
21 * the following conditions:
22 *
23 * The above copyright notice and this permission notice shall be included in
24 * all copies or substantial portions of the Software.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
32 * IN THE SOFTWARE.
33 */
34
35#include <linux/kernel.h>
36#include <linux/module.h>
37#include <linux/sched.h>
38#include <linux/errno.h>
39#include <linux/mm.h>
40#include <linux/bootmem.h>
41#include <linux/pagemap.h>
42#include <linux/highmem.h>
43#include <linux/mutex.h>
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070044#include <linux/list.h>
45#include <linux/sysdev.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/gfp.h>
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070047
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070048#include <asm/page.h>
49#include <asm/pgalloc.h>
50#include <asm/pgtable.h>
51#include <asm/uaccess.h>
52#include <asm/tlb.h>
Jeremy Fitzhardinge66946f62010-09-14 10:32:32 -070053#include <asm/e820.h>
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070054
Jeremy Fitzhardingeecbf29c2008-12-16 12:37:07 -080055#include <asm/xen/hypervisor.h>
56#include <asm/xen/hypercall.h>
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070057
58#include <xen/xen.h>
Jeremy Fitzhardingeecbf29c2008-12-16 12:37:07 -080059#include <xen/interface/xen.h>
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070060#include <xen/interface/memory.h>
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070061#include <xen/xenbus.h>
62#include <xen/features.h>
63#include <xen/page.h>
64
65#define PAGES2KB(_p) ((_p)<<(PAGE_SHIFT-10))
66
Jeremy Fitzhardinge167e6cf2008-07-24 16:27:52 -070067#define BALLOON_CLASS_NAME "xen_memory"
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070068
69struct balloon_stats {
70 /* We aim for 'current allocation' == 'target allocation'. */
71 unsigned long current_pages;
72 unsigned long target_pages;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070073 /* Number of pages in high- and low-memory balloons. */
74 unsigned long balloon_low;
75 unsigned long balloon_high;
76};
77
78static DEFINE_MUTEX(balloon_mutex);
79
80static struct sys_device balloon_sysdev;
81
82static int register_balloon(struct sys_device *sysdev);
83
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070084static struct balloon_stats balloon_stats;
85
86/* We increase/decrease in batches which fit in a page */
87static unsigned long frame_list[PAGE_SIZE / sizeof(unsigned long)];
88
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070089#ifdef CONFIG_HIGHMEM
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -070090#define inc_totalhigh_pages() (totalhigh_pages++)
91#define dec_totalhigh_pages() (totalhigh_pages--)
92#else
93#define inc_totalhigh_pages() do {} while(0)
94#define dec_totalhigh_pages() do {} while(0)
95#endif
96
97/* List of ballooned pages, threaded through the mem_map array. */
98static LIST_HEAD(ballooned_pages);
99
100/* Main work function, always executed in process context. */
101static void balloon_process(struct work_struct *work);
Daniel Kiper95170b22011-03-08 22:47:39 +0100102static DECLARE_DELAYED_WORK(balloon_worker, balloon_process);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700103
104/* When ballooning out (allocating memory to return to Xen) we don't really
105 want the kernel to try too hard since that can trigger the oom killer. */
106#define GFP_BALLOON \
107 (GFP_HIGHUSER | __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC)
108
109static void scrub_page(struct page *page)
110{
111#ifdef CONFIG_XEN_SCRUB_PAGES
Jeremy Fitzhardinge26a3e992008-11-17 09:35:00 -0800112 clear_highpage(page);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700113#endif
114}
115
116/* balloon_append: add the given page to the balloon. */
Jeremy Fitzhardinge9be4d452010-08-31 15:01:16 -0700117static void __balloon_append(struct page *page)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700118{
119 /* Lowmem is re-populated first, so highmem pages go at list tail. */
120 if (PageHighMem(page)) {
121 list_add_tail(&page->lru, &ballooned_pages);
122 balloon_stats.balloon_high++;
123 dec_totalhigh_pages();
124 } 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);
Gianluca Guida3d65c942009-07-30 22:54:36 +0100133 totalram_pages--;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700134}
135
136/* balloon_retrieve: rescue a page from the balloon, if it is not empty. */
137static struct page *balloon_retrieve(void)
138{
139 struct page *page;
140
141 if (list_empty(&ballooned_pages))
142 return NULL;
143
144 page = list_entry(ballooned_pages.next, struct page, lru);
145 list_del(&page->lru);
146
147 if (PageHighMem(page)) {
148 balloon_stats.balloon_high--;
149 inc_totalhigh_pages();
150 }
151 else
152 balloon_stats.balloon_low--;
153
Gianluca Guida3d65c942009-07-30 22:54:36 +0100154 totalram_pages++;
155
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700156 return page;
157}
158
159static struct page *balloon_first_page(void)
160{
161 if (list_empty(&ballooned_pages))
162 return NULL;
163 return list_entry(ballooned_pages.next, struct page, lru);
164}
165
166static struct page *balloon_next_page(struct page *page)
167{
168 struct list_head *next = page->lru.next;
169 if (next == &ballooned_pages)
170 return NULL;
171 return list_entry(next, struct page, lru);
172}
173
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700174static unsigned long current_target(void)
175{
Ian Campbellbc2c0302009-06-05 11:58:37 +0100176 unsigned long target = balloon_stats.target_pages;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700177
178 target = min(target,
179 balloon_stats.current_pages +
180 balloon_stats.balloon_low +
181 balloon_stats.balloon_high);
182
183 return target;
184}
185
186static int increase_reservation(unsigned long nr_pages)
187{
Jeremy Fitzhardinge2f70e0a2010-09-02 23:11:17 -0700188 unsigned long pfn, i;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700189 struct page *page;
190 long rc;
191 struct xen_memory_reservation reservation = {
192 .address_bits = 0,
193 .extent_order = 0,
194 .domid = DOMID_SELF
195 };
196
197 if (nr_pages > ARRAY_SIZE(frame_list))
198 nr_pages = ARRAY_SIZE(frame_list);
199
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700200 page = balloon_first_page();
201 for (i = 0; i < nr_pages; i++) {
202 BUG_ON(page == NULL);
Joe Perchesa419aef2009-08-18 11:18:35 -0700203 frame_list[i] = page_to_pfn(page);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700204 page = balloon_next_page(page);
205 }
206
Isaku Yamahataa90971e2008-05-26 23:31:14 +0100207 set_xen_guest_handle(reservation.extent_start, frame_list);
Jeremy Fitzhardingefde28e82008-07-24 16:28:00 -0700208 reservation.nr_extents = nr_pages;
209 rc = HYPERVISOR_memory_op(XENMEM_populate_physmap, &reservation);
Ian Campbellbc2c0302009-06-05 11:58:37 +0100210 if (rc < 0)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700211 goto out;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700212
Ian Campbellbc2c0302009-06-05 11:58:37 +0100213 for (i = 0; i < rc; i++) {
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700214 page = balloon_retrieve();
215 BUG_ON(page == NULL);
216
217 pfn = page_to_pfn(page);
218 BUG_ON(!xen_feature(XENFEAT_auto_translated_physmap) &&
219 phys_to_machine_mapping_valid(pfn));
220
221 set_phys_to_machine(pfn, frame_list[i]);
222
223 /* Link back into the page tables if not highmem. */
224 if (pfn < max_low_pfn) {
225 int ret;
226 ret = HYPERVISOR_update_va_mapping(
227 (unsigned long)__va(pfn << PAGE_SHIFT),
228 mfn_pte(frame_list[i], PAGE_KERNEL),
229 0);
230 BUG_ON(ret);
231 }
232
233 /* Relinquish the page back to the allocator. */
234 ClearPageReserved(page);
235 init_page_count(page);
236 __free_page(page);
237 }
238
Ian Campbellbc2c0302009-06-05 11:58:37 +0100239 balloon_stats.current_pages += rc;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700240
241 out:
Ian Campbellbc2c0302009-06-05 11:58:37 +0100242 return rc < 0 ? rc : rc != nr_pages;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700243}
244
245static int decrease_reservation(unsigned long nr_pages)
246{
Jeremy Fitzhardinge2f70e0a2010-09-02 23:11:17 -0700247 unsigned long pfn, i;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700248 struct page *page;
249 int need_sleep = 0;
250 int ret;
251 struct xen_memory_reservation reservation = {
252 .address_bits = 0,
253 .extent_order = 0,
254 .domid = DOMID_SELF
255 };
256
257 if (nr_pages > ARRAY_SIZE(frame_list))
258 nr_pages = ARRAY_SIZE(frame_list);
259
260 for (i = 0; i < nr_pages; i++) {
261 if ((page = alloc_page(GFP_BALLOON)) == NULL) {
262 nr_pages = i;
263 need_sleep = 1;
264 break;
265 }
266
267 pfn = page_to_pfn(page);
268 frame_list[i] = pfn_to_mfn(pfn);
269
270 scrub_page(page);
Dan Magenheimer1058a752009-01-22 14:36:08 -0800271
Ian Campbellff4ce8c2009-01-23 16:26:21 +0000272 if (!PageHighMem(page)) {
273 ret = HYPERVISOR_update_va_mapping(
274 (unsigned long)__va(pfn << PAGE_SHIFT),
275 __pte_ma(0), 0);
276 BUG_ON(ret);
277 }
278
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700279 }
280
281 /* Ensure that ballooned highmem pages don't have kmaps. */
282 kmap_flush_unused();
283 flush_tlb_all();
284
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700285 /* No more mappings: invalidate P2M and add to balloon. */
286 for (i = 0; i < nr_pages; i++) {
287 pfn = mfn_to_pfn(frame_list[i]);
288 set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
289 balloon_append(pfn_to_page(pfn));
290 }
291
Isaku Yamahataa90971e2008-05-26 23:31:14 +0100292 set_xen_guest_handle(reservation.extent_start, frame_list);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700293 reservation.nr_extents = nr_pages;
294 ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
295 BUG_ON(ret != nr_pages);
296
297 balloon_stats.current_pages -= nr_pages;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700298
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700299 return need_sleep;
300}
301
302/*
303 * We avoid multiple worker processes conflicting via the balloon mutex.
304 * We may of course race updates of the target counts (which are protected
305 * by the balloon lock), or with changes to the Xen hard limit, but we will
306 * recover from these in time.
307 */
308static void balloon_process(struct work_struct *work)
309{
310 int need_sleep = 0;
311 long credit;
312
313 mutex_lock(&balloon_mutex);
314
315 do {
316 credit = current_target() - balloon_stats.current_pages;
317 if (credit > 0)
318 need_sleep = (increase_reservation(credit) != 0);
319 if (credit < 0)
320 need_sleep = (decrease_reservation(-credit) != 0);
321
322#ifndef CONFIG_PREEMPT
323 if (need_resched())
324 schedule();
325#endif
326 } while ((credit != 0) && !need_sleep);
327
328 /* Schedule more work if there is some still to be done. */
329 if (current_target() != balloon_stats.current_pages)
Daniel Kiper95170b22011-03-08 22:47:39 +0100330 schedule_delayed_work(&balloon_worker, HZ);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700331
332 mutex_unlock(&balloon_mutex);
333}
334
335/* Resets the Xen limit, sets new target, and kicks off processing. */
Adrian Bunk955d6f12008-05-26 23:31:17 +0100336static void balloon_set_new_target(unsigned long target)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700337{
338 /* No need for lock. Not read-modify-write updates. */
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700339 balloon_stats.target_pages = target;
Daniel Kiper95170b22011-03-08 22:47:39 +0100340 schedule_delayed_work(&balloon_worker, 0);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700341}
342
343static struct xenbus_watch target_watch =
344{
345 .node = "memory/target"
346};
347
348/* React to a change in the target key */
349static void watch_target(struct xenbus_watch *watch,
350 const char **vec, unsigned int len)
351{
352 unsigned long long new_target;
353 int err;
354
355 err = xenbus_scanf(XBT_NIL, "memory", "target", "%llu", &new_target);
356 if (err != 1) {
357 /* This is ok (for domain0 at least) - so just return */
358 return;
359 }
360
361 /* The given memory/target value is in KiB, so it needs converting to
362 * pages. PAGE_SHIFT converts bytes to pages, hence PAGE_SHIFT - 10.
363 */
364 balloon_set_new_target(new_target >> (PAGE_SHIFT - 10));
365}
366
367static int balloon_init_watcher(struct notifier_block *notifier,
368 unsigned long event,
369 void *data)
370{
371 int err;
372
373 err = register_xenbus_watch(&target_watch);
374 if (err)
375 printk(KERN_ERR "Failed to set balloon watcher\n");
376
377 return NOTIFY_DONE;
378}
379
380static struct notifier_block xenstore_notifier;
381
382static int __init balloon_init(void)
383{
Jeremy Fitzhardinge66946f62010-09-14 10:32:32 -0700384 unsigned long pfn, extra_pfn_end;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700385 struct page *page;
386
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -0700387 if (!xen_pv_domain())
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700388 return -ENODEV;
389
390 pr_info("xen_balloon: Initialising balloon driver.\n");
391
392 balloon_stats.current_pages = min(xen_start_info->nr_pages, max_pfn);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700393 balloon_stats.target_pages = balloon_stats.current_pages;
394 balloon_stats.balloon_low = 0;
395 balloon_stats.balloon_high = 0;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700396
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700397 register_balloon(&balloon_sysdev);
398
Jeremy Fitzhardinge2a4c92f2010-12-02 15:30:06 -0800399 /*
400 * Initialise the balloon with excess memory space. We need
401 * to make sure we don't add memory which doesn't exist or
402 * logically exist. The E820 map can be trimmed to be smaller
403 * than the amount of physical memory due to the mem= command
404 * line parameter. And if this is a 32-bit non-HIGHMEM kernel
405 * on a system with memory which requires highmem to access,
406 * don't try to use it.
407 */
408 extra_pfn_end = min(min(max_pfn, e820_end_of_ram_pfn()),
Jeremy Fitzhardinge66946f62010-09-14 10:32:32 -0700409 (unsigned long)PFN_DOWN(xen_extra_mem_start + xen_extra_mem_size));
Jeremy Fitzhardinge9be4d452010-08-31 15:01:16 -0700410 for (pfn = PFN_UP(xen_extra_mem_start);
Jeremy Fitzhardinge66946f62010-09-14 10:32:32 -0700411 pfn < extra_pfn_end;
Jeremy Fitzhardinge9be4d452010-08-31 15:01:16 -0700412 pfn++) {
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700413 page = pfn_to_page(pfn);
Jeremy Fitzhardinge9be4d452010-08-31 15:01:16 -0700414 /* totalram_pages doesn't include the boot-time
415 balloon extension, so don't subtract from it. */
416 __balloon_append(page);
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700417 }
418
419 target_watch.callback = watch_target;
420 xenstore_notifier.notifier_call = balloon_init_watcher;
421
422 register_xenstore_notifier(&xenstore_notifier);
423
424 return 0;
425}
426
427subsys_initcall(balloon_init);
428
429static void balloon_exit(void)
430{
431 /* XXX - release balloon here */
432 return;
433}
434
435module_exit(balloon_exit);
436
Jeremy Fitzhardinge167e6cf2008-07-24 16:27:52 -0700437#define BALLOON_SHOW(name, format, args...) \
438 static ssize_t show_##name(struct sys_device *dev, \
439 struct sysdev_attribute *attr, \
440 char *buf) \
441 { \
442 return sprintf(buf, format, ##args); \
443 } \
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700444 static SYSDEV_ATTR(name, S_IRUGO, show_##name, NULL)
445
446BALLOON_SHOW(current_kb, "%lu\n", PAGES2KB(balloon_stats.current_pages));
447BALLOON_SHOW(low_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_low));
448BALLOON_SHOW(high_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_high));
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700449
Jeremy Fitzhardinge167e6cf2008-07-24 16:27:52 -0700450static ssize_t show_target_kb(struct sys_device *dev, struct sysdev_attribute *attr,
451 char *buf)
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700452{
453 return sprintf(buf, "%lu\n", PAGES2KB(balloon_stats.target_pages));
454}
455
456static ssize_t store_target_kb(struct sys_device *dev,
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200457 struct sysdev_attribute *attr,
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700458 const char *buf,
459 size_t count)
460{
Jeremy Fitzhardinge167e6cf2008-07-24 16:27:52 -0700461 char *endchar;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700462 unsigned long long target_bytes;
463
464 if (!capable(CAP_SYS_ADMIN))
465 return -EPERM;
466
Jeremy Fitzhardinge618b2c82009-01-28 16:50:20 -0800467 target_bytes = simple_strtoull(buf, &endchar, 0) * 1024;
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700468
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700469 balloon_set_new_target(target_bytes >> PAGE_SHIFT);
470
471 return count;
472}
473
474static SYSDEV_ATTR(target_kb, S_IRUGO | S_IWUSR,
475 show_target_kb, store_target_kb);
476
Jeremy Fitzhardinge618b2c82009-01-28 16:50:20 -0800477
478static ssize_t show_target(struct sys_device *dev, struct sysdev_attribute *attr,
479 char *buf)
480{
481 return sprintf(buf, "%llu\n",
Jan Beulich06926982009-05-05 13:57:52 +0100482 (unsigned long long)balloon_stats.target_pages
483 << PAGE_SHIFT);
Jeremy Fitzhardinge618b2c82009-01-28 16:50:20 -0800484}
485
486static ssize_t store_target(struct sys_device *dev,
487 struct sysdev_attribute *attr,
488 const char *buf,
489 size_t count)
490{
491 char *endchar;
492 unsigned long long target_bytes;
493
494 if (!capable(CAP_SYS_ADMIN))
495 return -EPERM;
496
497 target_bytes = memparse(buf, &endchar);
498
499 balloon_set_new_target(target_bytes >> PAGE_SHIFT);
500
501 return count;
502}
503
504static SYSDEV_ATTR(target, S_IRUGO | S_IWUSR,
505 show_target, store_target);
506
507
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700508static struct sysdev_attribute *balloon_attrs[] = {
509 &attr_target_kb,
Jeremy Fitzhardinge618b2c82009-01-28 16:50:20 -0800510 &attr_target,
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700511};
512
513static struct attribute *balloon_info_attrs[] = {
514 &attr_current_kb.attr,
515 &attr_low_kb.attr,
516 &attr_high_kb.attr,
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700517 NULL
518};
519
520static struct attribute_group balloon_info_group = {
521 .name = "info",
522 .attrs = balloon_info_attrs,
523};
524
525static struct sysdev_class balloon_sysdev_class = {
526 .name = BALLOON_CLASS_NAME,
527};
528
529static int register_balloon(struct sys_device *sysdev)
530{
531 int i, error;
532
533 error = sysdev_class_register(&balloon_sysdev_class);
534 if (error)
535 return error;
536
537 sysdev->id = 0;
538 sysdev->cls = &balloon_sysdev_class;
539
540 error = sysdev_register(sysdev);
541 if (error) {
542 sysdev_class_unregister(&balloon_sysdev_class);
543 return error;
544 }
545
546 for (i = 0; i < ARRAY_SIZE(balloon_attrs); i++) {
547 error = sysdev_create_file(sysdev, balloon_attrs[i]);
548 if (error)
549 goto fail;
550 }
551
552 error = sysfs_create_group(&sysdev->kobj, &balloon_info_group);
553 if (error)
554 goto fail;
555
556 return 0;
557
558 fail:
559 while (--i >= 0)
560 sysdev_remove_file(sysdev, balloon_attrs[i]);
561 sysdev_unregister(sysdev);
562 sysdev_class_unregister(&balloon_sysdev_class);
563 return error;
564}
565
Jeremy Fitzhardinge17758262008-04-02 10:54:13 -0700566MODULE_LICENSE("GPL");