xen-balloon.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. target_watch.callback = watch_target;
  84. xenstore_notifier.notifier_call = balloon_init_watcher;
  85. register_xenstore_notifier(&xenstore_notifier);
  86. return 0;
  87. }
  88. subsys_initcall(balloon_init);
  89. static void balloon_exit(void)
  90. {
  91. /* XXX - release balloon here */
  92. return;
  93. }
  94. module_exit(balloon_exit);
  95. #define BALLOON_SHOW(name, format, args...) \
  96. static ssize_t show_##name(struct sys_device *dev, \
  97. struct sysdev_attribute *attr, \
  98. char *buf) \
  99. { \
  100. return sprintf(buf, format, ##args); \
  101. } \
  102. static SYSDEV_ATTR(name, S_IRUGO, show_##name, NULL)
  103. BALLOON_SHOW(current_kb, "%lu\n", PAGES2KB(balloon_stats.current_pages));
  104. BALLOON_SHOW(low_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_low));
  105. BALLOON_SHOW(high_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_high));
  106. static SYSDEV_ULONG_ATTR(schedule_delay, 0444, balloon_stats.schedule_delay);
  107. static SYSDEV_ULONG_ATTR(max_schedule_delay, 0644, balloon_stats.max_schedule_delay);
  108. static SYSDEV_ULONG_ATTR(retry_count, 0444, balloon_stats.retry_count);
  109. static SYSDEV_ULONG_ATTR(max_retry_count, 0644, balloon_stats.max_retry_count);
  110. static ssize_t show_target_kb(struct sys_device *dev, struct sysdev_attribute *attr,
  111. char *buf)
  112. {
  113. return sprintf(buf, "%lu\n", PAGES2KB(balloon_stats.target_pages));
  114. }
  115. static ssize_t store_target_kb(struct sys_device *dev,
  116. struct sysdev_attribute *attr,
  117. const char *buf,
  118. size_t count)
  119. {
  120. char *endchar;
  121. unsigned long long target_bytes;
  122. if (!capable(CAP_SYS_ADMIN))
  123. return -EPERM;
  124. target_bytes = simple_strtoull(buf, &endchar, 0) * 1024;
  125. balloon_set_new_target(target_bytes >> PAGE_SHIFT);
  126. return count;
  127. }
  128. static SYSDEV_ATTR(target_kb, S_IRUGO | S_IWUSR,
  129. show_target_kb, store_target_kb);
  130. static ssize_t show_target(struct sys_device *dev, struct sysdev_attribute *attr,
  131. char *buf)
  132. {
  133. return sprintf(buf, "%llu\n",
  134. (unsigned long long)balloon_stats.target_pages
  135. << PAGE_SHIFT);
  136. }
  137. static ssize_t store_target(struct sys_device *dev,
  138. struct sysdev_attribute *attr,
  139. const char *buf,
  140. size_t count)
  141. {
  142. char *endchar;
  143. unsigned long long target_bytes;
  144. if (!capable(CAP_SYS_ADMIN))
  145. return -EPERM;
  146. target_bytes = memparse(buf, &endchar);
  147. balloon_set_new_target(target_bytes >> PAGE_SHIFT);
  148. return count;
  149. }
  150. static SYSDEV_ATTR(target, S_IRUGO | S_IWUSR,
  151. show_target, store_target);
  152. static struct sysdev_attribute *balloon_attrs[] = {
  153. &attr_target_kb,
  154. &attr_target,
  155. &attr_schedule_delay.attr,
  156. &attr_max_schedule_delay.attr,
  157. &attr_retry_count.attr,
  158. &attr_max_retry_count.attr
  159. };
  160. static struct attribute *balloon_info_attrs[] = {
  161. &attr_current_kb.attr,
  162. &attr_low_kb.attr,
  163. &attr_high_kb.attr,
  164. NULL
  165. };
  166. static struct attribute_group balloon_info_group = {
  167. .name = "info",
  168. .attrs = balloon_info_attrs
  169. };
  170. static struct sysdev_class balloon_sysdev_class = {
  171. .name = BALLOON_CLASS_NAME
  172. };
  173. static int register_balloon(struct sys_device *sysdev)
  174. {
  175. int i, error;
  176. error = sysdev_class_register(&balloon_sysdev_class);
  177. if (error)
  178. return error;
  179. sysdev->id = 0;
  180. sysdev->cls = &balloon_sysdev_class;
  181. error = sysdev_register(sysdev);
  182. if (error) {
  183. sysdev_class_unregister(&balloon_sysdev_class);
  184. return error;
  185. }
  186. for (i = 0; i < ARRAY_SIZE(balloon_attrs); i++) {
  187. error = sysdev_create_file(sysdev, balloon_attrs[i]);
  188. if (error)
  189. goto fail;
  190. }
  191. error = sysfs_create_group(&sysdev->kobj, &balloon_info_group);
  192. if (error)
  193. goto fail;
  194. return 0;
  195. fail:
  196. while (--i >= 0)
  197. sysdev_remove_file(sysdev, balloon_attrs[i]);
  198. sysdev_unregister(sysdev);
  199. sysdev_class_unregister(&balloon_sysdev_class);
  200. return error;
  201. }
  202. MODULE_LICENSE("GPL");