xen-balloon.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  33. #include <linux/kernel.h>
  34. #include <linux/module.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 device balloon_dev;
  45. static int register_balloon(struct device *dev);
  46. /* React to a change in the target key */
  47. static void watch_target(struct xenbus_watch *watch,
  48. const char **vec, unsigned int len)
  49. {
  50. unsigned long long new_target;
  51. int err;
  52. err = xenbus_scanf(XBT_NIL, "memory", "target", "%llu", &new_target);
  53. if (err != 1) {
  54. /* This is ok (for domain0 at least) - so just return */
  55. return;
  56. }
  57. /* The given memory/target value is in KiB, so it needs converting to
  58. * pages. PAGE_SHIFT converts bytes to pages, hence PAGE_SHIFT - 10.
  59. */
  60. balloon_set_new_target(new_target >> (PAGE_SHIFT - 10));
  61. }
  62. static struct xenbus_watch target_watch = {
  63. .node = "memory/target",
  64. .callback = watch_target,
  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. pr_err("Failed to set balloon watcher\n");
  74. return NOTIFY_DONE;
  75. }
  76. static struct notifier_block xenstore_notifier = {
  77. .notifier_call = balloon_init_watcher,
  78. };
  79. static int __init balloon_init(void)
  80. {
  81. if (!xen_domain())
  82. return -ENODEV;
  83. pr_info("Initialising balloon driver\n");
  84. register_balloon(&balloon_dev);
  85. register_xen_selfballooning(&balloon_dev);
  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 device *dev, \
  98. struct device_attribute *attr, \
  99. char *buf) \
  100. { \
  101. return sprintf(buf, format, ##args); \
  102. } \
  103. static DEVICE_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 DEVICE_ULONG_ATTR(schedule_delay, 0444, balloon_stats.schedule_delay);
  108. static DEVICE_ULONG_ATTR(max_schedule_delay, 0644, balloon_stats.max_schedule_delay);
  109. static DEVICE_ULONG_ATTR(retry_count, 0444, balloon_stats.retry_count);
  110. static DEVICE_ULONG_ATTR(max_retry_count, 0644, balloon_stats.max_retry_count);
  111. static ssize_t show_target_kb(struct device *dev, struct device_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 device *dev,
  117. struct device_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 DEVICE_ATTR(target_kb, S_IRUGO | S_IWUSR,
  130. show_target_kb, store_target_kb);
  131. static ssize_t show_target(struct device *dev, struct device_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 device *dev,
  139. struct device_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 DEVICE_ATTR(target, S_IRUGO | S_IWUSR,
  152. show_target, store_target);
  153. static struct attribute *balloon_attrs[] = {
  154. &dev_attr_target_kb.attr,
  155. &dev_attr_target.attr,
  156. &dev_attr_schedule_delay.attr.attr,
  157. &dev_attr_max_schedule_delay.attr.attr,
  158. &dev_attr_retry_count.attr.attr,
  159. &dev_attr_max_retry_count.attr.attr,
  160. NULL
  161. };
  162. static const struct attribute_group balloon_group = {
  163. .attrs = balloon_attrs
  164. };
  165. static struct attribute *balloon_info_attrs[] = {
  166. &dev_attr_current_kb.attr,
  167. &dev_attr_low_kb.attr,
  168. &dev_attr_high_kb.attr,
  169. NULL
  170. };
  171. static const struct attribute_group balloon_info_group = {
  172. .name = "info",
  173. .attrs = balloon_info_attrs
  174. };
  175. static const struct attribute_group *balloon_groups[] = {
  176. &balloon_group,
  177. &balloon_info_group,
  178. NULL
  179. };
  180. static struct bus_type balloon_subsys = {
  181. .name = BALLOON_CLASS_NAME,
  182. .dev_name = BALLOON_CLASS_NAME,
  183. };
  184. static int register_balloon(struct device *dev)
  185. {
  186. int error;
  187. error = subsys_system_register(&balloon_subsys, NULL);
  188. if (error)
  189. return error;
  190. dev->id = 0;
  191. dev->bus = &balloon_subsys;
  192. dev->groups = balloon_groups;
  193. error = device_register(dev);
  194. if (error) {
  195. bus_unregister(&balloon_subsys);
  196. return error;
  197. }
  198. return 0;
  199. }
  200. MODULE_LICENSE("GPL");