xen-balloon.c 6.7 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/capability.h>
  35. #include <xen/xen.h>
  36. #include <xen/interface/xen.h>
  37. #include <xen/balloon.h>
  38. #include <xen/xenbus.h>
  39. #include <xen/features.h>
  40. #include <xen/page.h>
  41. #define PAGES2KB(_p) ((_p)<<(PAGE_SHIFT-10))
  42. #define BALLOON_CLASS_NAME "xen_memory"
  43. static struct device balloon_dev;
  44. static int register_balloon(struct device *dev);
  45. /* React to a change in the target key */
  46. static void watch_target(struct xenbus_watch *watch,
  47. const char **vec, unsigned int len)
  48. {
  49. unsigned long long new_target;
  50. int err;
  51. err = xenbus_scanf(XBT_NIL, "memory", "target", "%llu", &new_target);
  52. if (err != 1) {
  53. /* This is ok (for domain0 at least) - so just return */
  54. return;
  55. }
  56. /* The given memory/target value is in KiB, so it needs converting to
  57. * pages. PAGE_SHIFT converts bytes to pages, hence PAGE_SHIFT - 10.
  58. */
  59. balloon_set_new_target(new_target >> (PAGE_SHIFT - 10));
  60. }
  61. static struct xenbus_watch target_watch = {
  62. .node = "memory/target",
  63. .callback = watch_target,
  64. };
  65. static int balloon_init_watcher(struct notifier_block *notifier,
  66. unsigned long event,
  67. void *data)
  68. {
  69. int err;
  70. err = register_xenbus_watch(&target_watch);
  71. if (err)
  72. printk(KERN_ERR "Failed to set balloon watcher\n");
  73. return NOTIFY_DONE;
  74. }
  75. static struct notifier_block xenstore_notifier = {
  76. .notifier_call = balloon_init_watcher,
  77. };
  78. static int __init balloon_init(void)
  79. {
  80. if (!xen_domain())
  81. return -ENODEV;
  82. pr_info("xen-balloon: Initialising balloon driver.\n");
  83. register_balloon(&balloon_dev);
  84. register_xen_selfballooning(&balloon_dev);
  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 device *dev, \
  97. struct device_attribute *attr, \
  98. char *buf) \
  99. { \
  100. return sprintf(buf, format, ##args); \
  101. } \
  102. static DEVICE_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 DEVICE_ULONG_ATTR(schedule_delay, 0444, balloon_stats.schedule_delay);
  107. static DEVICE_ULONG_ATTR(max_schedule_delay, 0644, balloon_stats.max_schedule_delay);
  108. static DEVICE_ULONG_ATTR(retry_count, 0444, balloon_stats.retry_count);
  109. static DEVICE_ULONG_ATTR(max_retry_count, 0644, balloon_stats.max_retry_count);
  110. static ssize_t show_target_kb(struct device *dev, struct device_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 device *dev,
  116. struct device_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 DEVICE_ATTR(target_kb, S_IRUGO | S_IWUSR,
  129. show_target_kb, store_target_kb);
  130. static ssize_t show_target(struct device *dev, struct device_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 device *dev,
  138. struct device_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 DEVICE_ATTR(target, S_IRUGO | S_IWUSR,
  151. show_target, store_target);
  152. static struct device_attribute *balloon_attrs[] = {
  153. &dev_attr_target_kb,
  154. &dev_attr_target,
  155. &dev_attr_schedule_delay.attr,
  156. &dev_attr_max_schedule_delay.attr,
  157. &dev_attr_retry_count.attr,
  158. &dev_attr_max_retry_count.attr
  159. };
  160. static struct attribute *balloon_info_attrs[] = {
  161. &dev_attr_current_kb.attr,
  162. &dev_attr_low_kb.attr,
  163. &dev_attr_high_kb.attr,
  164. NULL
  165. };
  166. static const struct attribute_group balloon_info_group = {
  167. .name = "info",
  168. .attrs = balloon_info_attrs
  169. };
  170. static struct bus_type balloon_subsys = {
  171. .name = BALLOON_CLASS_NAME,
  172. .dev_name = BALLOON_CLASS_NAME,
  173. };
  174. static int register_balloon(struct device *dev)
  175. {
  176. int i, error;
  177. error = subsys_system_register(&balloon_subsys, NULL);
  178. if (error)
  179. return error;
  180. dev->id = 0;
  181. dev->bus = &balloon_subsys;
  182. error = device_register(dev);
  183. if (error) {
  184. bus_unregister(&balloon_subsys);
  185. return error;
  186. }
  187. for (i = 0; i < ARRAY_SIZE(balloon_attrs); i++) {
  188. error = device_create_file(dev, balloon_attrs[i]);
  189. if (error)
  190. goto fail;
  191. }
  192. error = sysfs_create_group(&dev->kobj, &balloon_info_group);
  193. if (error)
  194. goto fail;
  195. return 0;
  196. fail:
  197. while (--i >= 0)
  198. device_remove_file(dev, balloon_attrs[i]);
  199. device_unregister(dev);
  200. bus_unregister(&balloon_subsys);
  201. return error;
  202. }
  203. MODULE_LICENSE("GPL");