balloon.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. /******************************************************************************
  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. #include <linux/kernel.h>
  35. #include <linux/module.h>
  36. #include <linux/sched.h>
  37. #include <linux/errno.h>
  38. #include <linux/mm.h>
  39. #include <linux/bootmem.h>
  40. #include <linux/pagemap.h>
  41. #include <linux/highmem.h>
  42. #include <linux/mutex.h>
  43. #include <linux/highmem.h>
  44. #include <linux/list.h>
  45. #include <linux/sysdev.h>
  46. #include <asm/xen/hypervisor.h>
  47. #include <asm/page.h>
  48. #include <asm/pgalloc.h>
  49. #include <asm/pgtable.h>
  50. #include <asm/uaccess.h>
  51. #include <asm/tlb.h>
  52. #include <xen/interface/memory.h>
  53. #include <xen/balloon.h>
  54. #include <xen/xenbus.h>
  55. #include <xen/features.h>
  56. #include <xen/page.h>
  57. #define PAGES2KB(_p) ((_p)<<(PAGE_SHIFT-10))
  58. #define BALLOON_CLASS_NAME "xen_memory"
  59. struct balloon_stats {
  60. /* We aim for 'current allocation' == 'target allocation'. */
  61. unsigned long current_pages;
  62. unsigned long target_pages;
  63. /* We may hit the hard limit in Xen. If we do then we remember it. */
  64. unsigned long hard_limit;
  65. /*
  66. * Drivers may alter the memory reservation independently, but they
  67. * must inform the balloon driver so we avoid hitting the hard limit.
  68. */
  69. unsigned long driver_pages;
  70. /* Number of pages in high- and low-memory balloons. */
  71. unsigned long balloon_low;
  72. unsigned long balloon_high;
  73. };
  74. static DEFINE_MUTEX(balloon_mutex);
  75. static struct sys_device balloon_sysdev;
  76. static int register_balloon(struct sys_device *sysdev);
  77. /*
  78. * Protects atomic reservation decrease/increase against concurrent increases.
  79. * Also protects non-atomic updates of current_pages and driver_pages, and
  80. * balloon lists.
  81. */
  82. static DEFINE_SPINLOCK(balloon_lock);
  83. static struct balloon_stats balloon_stats;
  84. /* We increase/decrease in batches which fit in a page */
  85. static unsigned long frame_list[PAGE_SIZE / sizeof(unsigned long)];
  86. /* VM /proc information for memory */
  87. extern unsigned long totalram_pages;
  88. #ifdef CONFIG_HIGHMEM
  89. extern unsigned long totalhigh_pages;
  90. #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. /* List of ballooned pages, threaded through the mem_map array. */
  97. static LIST_HEAD(ballooned_pages);
  98. /* Main work function, always executed in process context. */
  99. static void balloon_process(struct work_struct *work);
  100. static DECLARE_WORK(balloon_worker, balloon_process);
  101. static struct timer_list balloon_timer;
  102. /* When ballooning out (allocating memory to return to Xen) we don't really
  103. want the kernel to try too hard since that can trigger the oom killer. */
  104. #define GFP_BALLOON \
  105. (GFP_HIGHUSER | __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC)
  106. static void scrub_page(struct page *page)
  107. {
  108. #ifdef CONFIG_XEN_SCRUB_PAGES
  109. if (PageHighMem(page)) {
  110. void *v = kmap(page);
  111. clear_page(v);
  112. kunmap(v);
  113. } else {
  114. void *v = page_address(page);
  115. clear_page(v);
  116. }
  117. #endif
  118. }
  119. /* balloon_append: add the given page to the balloon. */
  120. static void balloon_append(struct page *page)
  121. {
  122. /* Lowmem is re-populated first, so highmem pages go at list tail. */
  123. if (PageHighMem(page)) {
  124. list_add_tail(&page->lru, &ballooned_pages);
  125. balloon_stats.balloon_high++;
  126. dec_totalhigh_pages();
  127. } else {
  128. list_add(&page->lru, &ballooned_pages);
  129. balloon_stats.balloon_low++;
  130. }
  131. }
  132. /* balloon_retrieve: rescue a page from the balloon, if it is not empty. */
  133. static struct page *balloon_retrieve(void)
  134. {
  135. struct page *page;
  136. if (list_empty(&ballooned_pages))
  137. return NULL;
  138. page = list_entry(ballooned_pages.next, struct page, lru);
  139. list_del(&page->lru);
  140. if (PageHighMem(page)) {
  141. balloon_stats.balloon_high--;
  142. inc_totalhigh_pages();
  143. }
  144. else
  145. balloon_stats.balloon_low--;
  146. return page;
  147. }
  148. static struct page *balloon_first_page(void)
  149. {
  150. if (list_empty(&ballooned_pages))
  151. return NULL;
  152. return list_entry(ballooned_pages.next, struct page, lru);
  153. }
  154. static struct page *balloon_next_page(struct page *page)
  155. {
  156. struct list_head *next = page->lru.next;
  157. if (next == &ballooned_pages)
  158. return NULL;
  159. return list_entry(next, struct page, lru);
  160. }
  161. static void balloon_alarm(unsigned long unused)
  162. {
  163. schedule_work(&balloon_worker);
  164. }
  165. static unsigned long current_target(void)
  166. {
  167. unsigned long target = min(balloon_stats.target_pages, balloon_stats.hard_limit);
  168. target = min(target,
  169. balloon_stats.current_pages +
  170. balloon_stats.balloon_low +
  171. balloon_stats.balloon_high);
  172. return target;
  173. }
  174. static int increase_reservation(unsigned long nr_pages)
  175. {
  176. unsigned long pfn, i, flags;
  177. struct page *page;
  178. long rc;
  179. struct xen_memory_reservation reservation = {
  180. .address_bits = 0,
  181. .extent_order = 0,
  182. .domid = DOMID_SELF
  183. };
  184. if (nr_pages > ARRAY_SIZE(frame_list))
  185. nr_pages = ARRAY_SIZE(frame_list);
  186. spin_lock_irqsave(&balloon_lock, flags);
  187. page = balloon_first_page();
  188. for (i = 0; i < nr_pages; i++) {
  189. BUG_ON(page == NULL);
  190. frame_list[i] = page_to_pfn(page);;
  191. page = balloon_next_page(page);
  192. }
  193. set_xen_guest_handle(reservation.extent_start, frame_list);
  194. reservation.nr_extents = nr_pages;
  195. rc = HYPERVISOR_memory_op(
  196. XENMEM_populate_physmap, &reservation);
  197. if (rc < nr_pages) {
  198. if (rc > 0) {
  199. int ret;
  200. /* We hit the Xen hard limit: reprobe. */
  201. reservation.nr_extents = rc;
  202. ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
  203. &reservation);
  204. BUG_ON(ret != rc);
  205. }
  206. if (rc >= 0)
  207. balloon_stats.hard_limit = (balloon_stats.current_pages + rc -
  208. balloon_stats.driver_pages);
  209. goto out;
  210. }
  211. for (i = 0; i < nr_pages; i++) {
  212. page = balloon_retrieve();
  213. BUG_ON(page == NULL);
  214. pfn = page_to_pfn(page);
  215. BUG_ON(!xen_feature(XENFEAT_auto_translated_physmap) &&
  216. phys_to_machine_mapping_valid(pfn));
  217. set_phys_to_machine(pfn, frame_list[i]);
  218. /* Link back into the page tables if not highmem. */
  219. if (pfn < max_low_pfn) {
  220. int ret;
  221. ret = HYPERVISOR_update_va_mapping(
  222. (unsigned long)__va(pfn << PAGE_SHIFT),
  223. mfn_pte(frame_list[i], PAGE_KERNEL),
  224. 0);
  225. BUG_ON(ret);
  226. }
  227. /* Relinquish the page back to the allocator. */
  228. ClearPageReserved(page);
  229. init_page_count(page);
  230. __free_page(page);
  231. }
  232. balloon_stats.current_pages += nr_pages;
  233. totalram_pages = balloon_stats.current_pages;
  234. out:
  235. spin_unlock_irqrestore(&balloon_lock, flags);
  236. return 0;
  237. }
  238. static int decrease_reservation(unsigned long nr_pages)
  239. {
  240. unsigned long pfn, i, flags;
  241. struct page *page;
  242. int need_sleep = 0;
  243. int ret;
  244. struct xen_memory_reservation reservation = {
  245. .address_bits = 0,
  246. .extent_order = 0,
  247. .domid = DOMID_SELF
  248. };
  249. if (nr_pages > ARRAY_SIZE(frame_list))
  250. nr_pages = ARRAY_SIZE(frame_list);
  251. for (i = 0; i < nr_pages; i++) {
  252. if ((page = alloc_page(GFP_BALLOON)) == NULL) {
  253. nr_pages = i;
  254. need_sleep = 1;
  255. break;
  256. }
  257. pfn = page_to_pfn(page);
  258. frame_list[i] = pfn_to_mfn(pfn);
  259. scrub_page(page);
  260. }
  261. /* Ensure that ballooned highmem pages don't have kmaps. */
  262. kmap_flush_unused();
  263. flush_tlb_all();
  264. spin_lock_irqsave(&balloon_lock, flags);
  265. /* No more mappings: invalidate P2M and add to balloon. */
  266. for (i = 0; i < nr_pages; i++) {
  267. pfn = mfn_to_pfn(frame_list[i]);
  268. set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
  269. balloon_append(pfn_to_page(pfn));
  270. }
  271. set_xen_guest_handle(reservation.extent_start, frame_list);
  272. reservation.nr_extents = nr_pages;
  273. ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
  274. BUG_ON(ret != nr_pages);
  275. balloon_stats.current_pages -= nr_pages;
  276. totalram_pages = balloon_stats.current_pages;
  277. spin_unlock_irqrestore(&balloon_lock, flags);
  278. return need_sleep;
  279. }
  280. /*
  281. * We avoid multiple worker processes conflicting via the balloon mutex.
  282. * We may of course race updates of the target counts (which are protected
  283. * by the balloon lock), or with changes to the Xen hard limit, but we will
  284. * recover from these in time.
  285. */
  286. static void balloon_process(struct work_struct *work)
  287. {
  288. int need_sleep = 0;
  289. long credit;
  290. mutex_lock(&balloon_mutex);
  291. do {
  292. credit = current_target() - balloon_stats.current_pages;
  293. if (credit > 0)
  294. need_sleep = (increase_reservation(credit) != 0);
  295. if (credit < 0)
  296. need_sleep = (decrease_reservation(-credit) != 0);
  297. #ifndef CONFIG_PREEMPT
  298. if (need_resched())
  299. schedule();
  300. #endif
  301. } while ((credit != 0) && !need_sleep);
  302. /* Schedule more work if there is some still to be done. */
  303. if (current_target() != balloon_stats.current_pages)
  304. mod_timer(&balloon_timer, jiffies + HZ);
  305. mutex_unlock(&balloon_mutex);
  306. }
  307. /* Resets the Xen limit, sets new target, and kicks off processing. */
  308. static void balloon_set_new_target(unsigned long target)
  309. {
  310. /* No need for lock. Not read-modify-write updates. */
  311. balloon_stats.hard_limit = ~0UL;
  312. balloon_stats.target_pages = target;
  313. schedule_work(&balloon_worker);
  314. }
  315. static struct xenbus_watch target_watch =
  316. {
  317. .node = "memory/target"
  318. };
  319. /* React to a change in the target key */
  320. static void watch_target(struct xenbus_watch *watch,
  321. const char **vec, unsigned int len)
  322. {
  323. unsigned long long new_target;
  324. int err;
  325. err = xenbus_scanf(XBT_NIL, "memory", "target", "%llu", &new_target);
  326. if (err != 1) {
  327. /* This is ok (for domain0 at least) - so just return */
  328. return;
  329. }
  330. /* The given memory/target value is in KiB, so it needs converting to
  331. * pages. PAGE_SHIFT converts bytes to pages, hence PAGE_SHIFT - 10.
  332. */
  333. balloon_set_new_target(new_target >> (PAGE_SHIFT - 10));
  334. }
  335. static int balloon_init_watcher(struct notifier_block *notifier,
  336. unsigned long event,
  337. void *data)
  338. {
  339. int err;
  340. err = register_xenbus_watch(&target_watch);
  341. if (err)
  342. printk(KERN_ERR "Failed to set balloon watcher\n");
  343. return NOTIFY_DONE;
  344. }
  345. static struct notifier_block xenstore_notifier;
  346. static int __init balloon_init(void)
  347. {
  348. unsigned long pfn;
  349. struct page *page;
  350. if (!is_running_on_xen())
  351. return -ENODEV;
  352. pr_info("xen_balloon: Initialising balloon driver.\n");
  353. balloon_stats.current_pages = min(xen_start_info->nr_pages, max_pfn);
  354. totalram_pages = balloon_stats.current_pages;
  355. balloon_stats.target_pages = balloon_stats.current_pages;
  356. balloon_stats.balloon_low = 0;
  357. balloon_stats.balloon_high = 0;
  358. balloon_stats.driver_pages = 0UL;
  359. balloon_stats.hard_limit = ~0UL;
  360. init_timer(&balloon_timer);
  361. balloon_timer.data = 0;
  362. balloon_timer.function = balloon_alarm;
  363. register_balloon(&balloon_sysdev);
  364. /* Initialise the balloon with excess memory space. */
  365. for (pfn = xen_start_info->nr_pages; pfn < max_pfn; pfn++) {
  366. page = pfn_to_page(pfn);
  367. if (!PageReserved(page))
  368. balloon_append(page);
  369. }
  370. target_watch.callback = watch_target;
  371. xenstore_notifier.notifier_call = balloon_init_watcher;
  372. register_xenstore_notifier(&xenstore_notifier);
  373. return 0;
  374. }
  375. subsys_initcall(balloon_init);
  376. static void balloon_exit(void)
  377. {
  378. /* XXX - release balloon here */
  379. return;
  380. }
  381. module_exit(balloon_exit);
  382. static void balloon_update_driver_allowance(long delta)
  383. {
  384. unsigned long flags;
  385. spin_lock_irqsave(&balloon_lock, flags);
  386. balloon_stats.driver_pages += delta;
  387. spin_unlock_irqrestore(&balloon_lock, flags);
  388. }
  389. static int dealloc_pte_fn(
  390. pte_t *pte, struct page *pmd_page, unsigned long addr, void *data)
  391. {
  392. unsigned long mfn = pte_mfn(*pte);
  393. int ret;
  394. struct xen_memory_reservation reservation = {
  395. .nr_extents = 1,
  396. .extent_order = 0,
  397. .domid = DOMID_SELF
  398. };
  399. set_xen_guest_handle(reservation.extent_start, &mfn);
  400. set_pte_at(&init_mm, addr, pte, __pte_ma(0ull));
  401. set_phys_to_machine(__pa(addr) >> PAGE_SHIFT, INVALID_P2M_ENTRY);
  402. ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, &reservation);
  403. BUG_ON(ret != 1);
  404. return 0;
  405. }
  406. static struct page **alloc_empty_pages_and_pagevec(int nr_pages)
  407. {
  408. unsigned long vaddr, flags;
  409. struct page *page, **pagevec;
  410. int i, ret;
  411. pagevec = kmalloc(sizeof(page) * nr_pages, GFP_KERNEL);
  412. if (pagevec == NULL)
  413. return NULL;
  414. for (i = 0; i < nr_pages; i++) {
  415. page = pagevec[i] = alloc_page(GFP_KERNEL);
  416. if (page == NULL)
  417. goto err;
  418. vaddr = (unsigned long)page_address(page);
  419. scrub_page(page);
  420. spin_lock_irqsave(&balloon_lock, flags);
  421. if (xen_feature(XENFEAT_auto_translated_physmap)) {
  422. unsigned long gmfn = page_to_pfn(page);
  423. struct xen_memory_reservation reservation = {
  424. .nr_extents = 1,
  425. .extent_order = 0,
  426. .domid = DOMID_SELF
  427. };
  428. set_xen_guest_handle(reservation.extent_start, &gmfn);
  429. ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
  430. &reservation);
  431. if (ret == 1)
  432. ret = 0; /* success */
  433. } else {
  434. ret = apply_to_page_range(&init_mm, vaddr, PAGE_SIZE,
  435. dealloc_pte_fn, NULL);
  436. }
  437. if (ret != 0) {
  438. spin_unlock_irqrestore(&balloon_lock, flags);
  439. __free_page(page);
  440. goto err;
  441. }
  442. totalram_pages = --balloon_stats.current_pages;
  443. spin_unlock_irqrestore(&balloon_lock, flags);
  444. }
  445. out:
  446. schedule_work(&balloon_worker);
  447. flush_tlb_all();
  448. return pagevec;
  449. err:
  450. spin_lock_irqsave(&balloon_lock, flags);
  451. while (--i >= 0)
  452. balloon_append(pagevec[i]);
  453. spin_unlock_irqrestore(&balloon_lock, flags);
  454. kfree(pagevec);
  455. pagevec = NULL;
  456. goto out;
  457. }
  458. static void free_empty_pages_and_pagevec(struct page **pagevec, int nr_pages)
  459. {
  460. unsigned long flags;
  461. int i;
  462. if (pagevec == NULL)
  463. return;
  464. spin_lock_irqsave(&balloon_lock, flags);
  465. for (i = 0; i < nr_pages; i++) {
  466. BUG_ON(page_count(pagevec[i]) != 1);
  467. balloon_append(pagevec[i]);
  468. }
  469. spin_unlock_irqrestore(&balloon_lock, flags);
  470. kfree(pagevec);
  471. schedule_work(&balloon_worker);
  472. }
  473. static void balloon_release_driver_page(struct page *page)
  474. {
  475. unsigned long flags;
  476. spin_lock_irqsave(&balloon_lock, flags);
  477. balloon_append(page);
  478. balloon_stats.driver_pages--;
  479. spin_unlock_irqrestore(&balloon_lock, flags);
  480. schedule_work(&balloon_worker);
  481. }
  482. #define BALLOON_SHOW(name, format, args...) \
  483. static ssize_t show_##name(struct sys_device *dev, \
  484. char *buf) \
  485. { \
  486. return sprintf(buf, format, ##args); \
  487. } \
  488. static SYSDEV_ATTR(name, S_IRUGO, show_##name, NULL)
  489. BALLOON_SHOW(current_kb, "%lu\n", PAGES2KB(balloon_stats.current_pages));
  490. BALLOON_SHOW(low_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_low));
  491. BALLOON_SHOW(high_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_high));
  492. BALLOON_SHOW(hard_limit_kb,
  493. (balloon_stats.hard_limit!=~0UL) ? "%lu\n" : "???\n",
  494. (balloon_stats.hard_limit!=~0UL) ? PAGES2KB(balloon_stats.hard_limit) : 0);
  495. BALLOON_SHOW(driver_kb, "%lu\n", PAGES2KB(balloon_stats.driver_pages));
  496. static ssize_t show_target_kb(struct sys_device *dev, char *buf)
  497. {
  498. return sprintf(buf, "%lu\n", PAGES2KB(balloon_stats.target_pages));
  499. }
  500. static ssize_t store_target_kb(struct sys_device *dev,
  501. struct sysdev_attribute *attr,
  502. const char *buf,
  503. size_t count)
  504. {
  505. char memstring[64], *endchar;
  506. unsigned long long target_bytes;
  507. if (!capable(CAP_SYS_ADMIN))
  508. return -EPERM;
  509. if (count <= 1)
  510. return -EBADMSG; /* runt */
  511. if (count > sizeof(memstring))
  512. return -EFBIG; /* too long */
  513. strcpy(memstring, buf);
  514. target_bytes = memparse(memstring, &endchar);
  515. balloon_set_new_target(target_bytes >> PAGE_SHIFT);
  516. return count;
  517. }
  518. static SYSDEV_ATTR(target_kb, S_IRUGO | S_IWUSR,
  519. show_target_kb, store_target_kb);
  520. static struct sysdev_attribute *balloon_attrs[] = {
  521. &attr_target_kb,
  522. };
  523. static struct attribute *balloon_info_attrs[] = {
  524. &attr_current_kb.attr,
  525. &attr_low_kb.attr,
  526. &attr_high_kb.attr,
  527. &attr_hard_limit_kb.attr,
  528. &attr_driver_kb.attr,
  529. NULL
  530. };
  531. static struct attribute_group balloon_info_group = {
  532. .name = "info",
  533. .attrs = balloon_info_attrs,
  534. };
  535. static struct sysdev_class balloon_sysdev_class = {
  536. .name = BALLOON_CLASS_NAME,
  537. };
  538. static int register_balloon(struct sys_device *sysdev)
  539. {
  540. int i, error;
  541. error = sysdev_class_register(&balloon_sysdev_class);
  542. if (error)
  543. return error;
  544. sysdev->id = 0;
  545. sysdev->cls = &balloon_sysdev_class;
  546. error = sysdev_register(sysdev);
  547. if (error) {
  548. sysdev_class_unregister(&balloon_sysdev_class);
  549. return error;
  550. }
  551. for (i = 0; i < ARRAY_SIZE(balloon_attrs); i++) {
  552. error = sysdev_create_file(sysdev, balloon_attrs[i]);
  553. if (error)
  554. goto fail;
  555. }
  556. error = sysfs_create_group(&sysdev->kobj, &balloon_info_group);
  557. if (error)
  558. goto fail;
  559. return 0;
  560. fail:
  561. while (--i >= 0)
  562. sysdev_remove_file(sysdev, balloon_attrs[i]);
  563. sysdev_unregister(sysdev);
  564. sysdev_class_unregister(&balloon_sysdev_class);
  565. return error;
  566. }
  567. static void unregister_balloon(struct sys_device *sysdev)
  568. {
  569. int i;
  570. sysfs_remove_group(&sysdev->kobj, &balloon_info_group);
  571. for (i = 0; i < ARRAY_SIZE(balloon_attrs); i++)
  572. sysdev_remove_file(sysdev, balloon_attrs[i]);
  573. sysdev_unregister(sysdev);
  574. sysdev_class_unregister(&balloon_sysdev_class);
  575. }
  576. static void balloon_sysfs_exit(void)
  577. {
  578. unregister_balloon(&balloon_sysdev);
  579. }
  580. MODULE_LICENSE("GPL");