xen-balloon.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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
  9. * modify it under the terms of the GNU General Public License version 2
  10. * as published by the Free Software Foundation; or, when distributed
  11. * separately from the Linux kernel or incorporated into other
  12. * software packages, subject to the following license:
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining a copy
  15. * of this source file (the "Software"), to deal in the Software without
  16. * restriction, including without limitation the rights to use, copy, modify,
  17. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  18. * and to permit persons to whom the Software is furnished to do so, subject to
  19. * the following conditions:
  20. *
  21. * The above copyright notice and this permission notice shall be included in
  22. * all copies or substantial portions of the Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  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. #include <linux/kernel.h>
  33. #include <linux/module.h>
  34. #include <linux/sysdev.h>
  35. #include <linux/capability.h>
  36. #include <xen/xen.h>
  37. #include <xen/interface/xen.h>
  38. #include <xen/balloon.h>
  39. #include <xen/xenbus.h>
  40. #include <xen/features.h>
  41. #include <xen/page.h>
  42. #define PAGES2KB(_p) ((_p)<<(PAGE_SHIFT-10))
  43. #define BALLOON_CLASS_NAME "xen_memory"
  44. static struct sys_device balloon_sysdev;
  45. static int register_balloon(struct sys_device *sysdev);
  46. static struct xenbus_watch target_watch =
  47. {
  48. .node = "memory/target"
  49. };
  50. /* React to a change in the target key */
  51. static void watch_target(struct xenbus_watch *watch,
  52. const char **vec, unsigned int len)
  53. {
  54. unsigned long long new_target;
  55. int err;
  56. err = xenbus_scanf(XBT_NIL, "memory", "target", "%llu", &new_target);
  57. if (err != 1) {
  58. /* This is ok (for domain0 at least) - so just return */
  59. return;
  60. }
  61. /* The given memory/target value is in KiB, so it needs converting to
  62. * pages. PAGE_SHIFT converts bytes to pages, hence PAGE_SHIFT - 10.
  63. */
  64. balloon_set_new_target(new_target >> (PAGE_SHIFT - 10));
  65. }
  66. static int balloon_init_watcher(struct notifier_block *notifier,
  67. unsigned long event,
  68. void *data)
  69. {
  70. int err;
  71. err = register_xenbus_watch(&target_watch);
  72. if (err)
  73. printk(KERN_ERR "Failed to set balloon watcher\n");
  74. return NOTIFY_DONE;
  75. }
  76. static struct notifier_block xenstore_notifier;
  77. static int __init balloon_init(void)
  78. {
  79. if (!xen_domain())
  80. return -ENODEV;
  81. pr_info("xen-balloon: Initialising balloon driver.\n");
  82. register_balloon(&balloon_sysdev);
  83. register_xen_selfballooning(&balloon_sysdev);
  84. target_watch.callback = watch_target;
  85. xenstore_notifier.notifier_call = balloon_init_watcher;
  86. register_xenstore_notifier(&xenstore_notifier);
  87. return 0;
  88. }
  89. subsys_initcall(balloon_init);
  90. static void balloon_exit(void)
  91. {
  92. /* XXX - release balloon here */
  93. return;
  94. }
  95. module_exit(balloon_exit);
  96. #define BALLOON_SHOW(name, format, args...) \
  97. static ssize_t show_##name(struct sys_device *dev, \
  98. struct sysdev_attribute *attr, \
  99. char *buf) \
  100. { \
  101. return sprintf(buf, format, ##args); \
  102. } \
  103. static SYSDEV_ATTR(name, S_IRUGO, show_##name, NULL)
  104. BALLOON_SHOW(current_kb, "%lu\n", PAGES2KB(balloon_stats.current_pages));
  105. BALLOON_SHOW(low_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_low));
  106. BALLOON_SHOW(high_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_high));
  107. static SYSDEV_ULONG_ATTR(schedule_delay, 0444, balloon_stats.schedule_delay);
  108. static SYSDEV_ULONG_ATTR(max_schedule_delay, 0644, balloon_stats.max_schedule_delay);
  109. static SYSDEV_ULONG_ATTR(retry_count, 0444, balloon_stats.retry_count);
  110. static SYSDEV_ULONG_ATTR(max_retry_count, 0644, balloon_stats.max_retry_count);
  111. static ssize_t show_target_kb(struct sys_device *dev, struct sysdev_attribute *attr,
  112. char *buf)
  113. {
  114. return sprintf(buf, "%lu\n", PAGES2KB(balloon_stats.target_pages));
  115. }
  116. static ssize_t store_target_kb(struct sys_device *dev,
  117. struct sysdev_attribute *attr,
  118. const char *buf,
  119. size_t count)
  120. {
  121. char *endchar;
  122. unsigned long long target_bytes;
  123. if (!capable(CAP_SYS_ADMIN))
  124. return -EPERM;
  125. target_bytes = simple_strtoull(buf, &endchar, 0) * 1024;
  126. balloon_set_new_target(target_bytes >> PAGE_SHIFT);
  127. return count;
  128. }
  129. static SYSDEV_ATTR(target_kb, S_IRUGO | S_IWUSR,
  130. show_target_kb, store_target_kb);
  131. static ssize_t show_target(struct sys_device *dev, struct sysdev_attribute *attr,
  132. char *buf)
  133. {
  134. return sprintf(buf, "%llu\n",
  135. (unsigned long long)balloon_stats.target_pages
  136. << PAGE_SHIFT);
  137. }
  138. static ssize_t store_target(struct sys_device *dev,
  139. struct sysdev_attribute *attr,
  140. const char *buf,
  141. size_t count)
  142. {
  143. char *endchar;
  144. unsigned long long target_bytes;
  145. if (!capable(CAP_SYS_ADMIN))
  146. return -EPERM;
  147. target_bytes = memparse(buf, &endchar);
  148. balloon_set_new_target(target_bytes >> PAGE_SHIFT);
  149. return count;
  150. }
  151. static SYSDEV_ATTR(target, S_IRUGO | S_IWUSR,
  152. show_target, store_target);
  153. static struct sysdev_attribute *balloon_attrs[] = {
  154. &attr_target_kb,
  155. &attr_target,
  156. &attr_schedule_delay.attr,
  157. &attr_max_schedule_delay.attr,
  158. &attr_retry_count.attr,
  159. &attr_max_retry_count.attr
  160. };
  161. static struct attribute *balloon_info_attrs[] = {
  162. &attr_current_kb.attr,
  163. &attr_low_kb.attr,
  164. &attr_high_kb.attr,
  165. NULL
  166. };
  167. static struct attribute_group balloon_info_group = {
  168. .name = "info",
  169. .attrs = balloon_info_attrs
  170. };
  171. static struct sysdev_class balloon_sysdev_class = {
  172. .name = BALLOON_CLASS_NAME
  173. };
  174. static int register_balloon(struct sys_device *sysdev)
  175. {
  176. int i, error;
  177. error = sysdev_class_register(&balloon_sysdev_class);
  178. if (error)
  179. return error;
  180. sysdev->id = 0;
  181. sysdev->cls = &balloon_sysdev_class;
  182. error = sysdev_register(sysdev);
  183. if (error) {
  184. sysdev_class_unregister(&balloon_sysdev_class);
  185. return error;
  186. }
  187. for (i = 0; i < ARRAY_SIZE(balloon_attrs); i++) {
  188. error = sysdev_create_file(sysdev, balloon_attrs[i]);
  189. if (error)
  190. goto fail;
  191. }
  192. error = sysfs_create_group(&sysdev->kobj, &balloon_info_group);
  193. if (error)
  194. goto fail;
  195. return 0;
  196. fail:
  197. while (--i >= 0)
  198. sysdev_remove_file(sysdev, balloon_attrs[i]);
  199. sysdev_unregister(sysdev);
  200. sysdev_class_unregister(&balloon_sysdev_class);
  201. return error;
  202. }
  203. MODULE_LICENSE("GPL");