xen-balloon.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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/errno.h>
  35. #include <linux/mm_types.h>
  36. #include <linux/init.h>
  37. #include <linux/capability.h>
  38. #include <xen/xen.h>
  39. #include <xen/interface/xen.h>
  40. #include <xen/balloon.h>
  41. #include <xen/xenbus.h>
  42. #include <xen/features.h>
  43. #include <xen/page.h>
  44. #define PAGES2KB(_p) ((_p)<<(PAGE_SHIFT-10))
  45. #define BALLOON_CLASS_NAME "xen_memory"
  46. static struct device balloon_dev;
  47. static int register_balloon(struct device *dev);
  48. /* React to a change in the target key */
  49. static void watch_target(struct xenbus_watch *watch,
  50. const char **vec, unsigned int len)
  51. {
  52. unsigned long long new_target;
  53. int err;
  54. err = xenbus_scanf(XBT_NIL, "memory", "target", "%llu", &new_target);
  55. if (err != 1) {
  56. /* This is ok (for domain0 at least) - so just return */
  57. return;
  58. }
  59. /* The given memory/target value is in KiB, so it needs converting to
  60. * pages. PAGE_SHIFT converts bytes to pages, hence PAGE_SHIFT - 10.
  61. */
  62. balloon_set_new_target(new_target >> (PAGE_SHIFT - 10));
  63. }
  64. static struct xenbus_watch target_watch = {
  65. .node = "memory/target",
  66. .callback = watch_target,
  67. };
  68. static int balloon_init_watcher(struct notifier_block *notifier,
  69. unsigned long event,
  70. void *data)
  71. {
  72. int err;
  73. err = register_xenbus_watch(&target_watch);
  74. if (err)
  75. pr_err("Failed to set balloon watcher\n");
  76. return NOTIFY_DONE;
  77. }
  78. static struct notifier_block xenstore_notifier = {
  79. .notifier_call = balloon_init_watcher,
  80. };
  81. static int __init balloon_init(void)
  82. {
  83. if (!xen_domain())
  84. return -ENODEV;
  85. pr_info("Initialising balloon driver\n");
  86. register_balloon(&balloon_dev);
  87. register_xen_selfballooning(&balloon_dev);
  88. register_xenstore_notifier(&xenstore_notifier);
  89. return 0;
  90. }
  91. subsys_initcall(balloon_init);
  92. #define BALLOON_SHOW(name, format, args...) \
  93. static ssize_t show_##name(struct device *dev, \
  94. struct device_attribute *attr, \
  95. char *buf) \
  96. { \
  97. return sprintf(buf, format, ##args); \
  98. } \
  99. static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
  100. BALLOON_SHOW(current_kb, "%lu\n", PAGES2KB(balloon_stats.current_pages));
  101. BALLOON_SHOW(low_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_low));
  102. BALLOON_SHOW(high_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_high));
  103. static DEVICE_ULONG_ATTR(schedule_delay, 0444, balloon_stats.schedule_delay);
  104. static DEVICE_ULONG_ATTR(max_schedule_delay, 0644, balloon_stats.max_schedule_delay);
  105. static DEVICE_ULONG_ATTR(retry_count, 0444, balloon_stats.retry_count);
  106. static DEVICE_ULONG_ATTR(max_retry_count, 0644, balloon_stats.max_retry_count);
  107. static ssize_t show_target_kb(struct device *dev, struct device_attribute *attr,
  108. char *buf)
  109. {
  110. return sprintf(buf, "%lu\n", PAGES2KB(balloon_stats.target_pages));
  111. }
  112. static ssize_t store_target_kb(struct device *dev,
  113. struct device_attribute *attr,
  114. const char *buf,
  115. size_t count)
  116. {
  117. char *endchar;
  118. unsigned long long target_bytes;
  119. if (!capable(CAP_SYS_ADMIN))
  120. return -EPERM;
  121. target_bytes = simple_strtoull(buf, &endchar, 0) * 1024;
  122. balloon_set_new_target(target_bytes >> PAGE_SHIFT);
  123. return count;
  124. }
  125. static DEVICE_ATTR(target_kb, S_IRUGO | S_IWUSR,
  126. show_target_kb, store_target_kb);
  127. static ssize_t show_target(struct device *dev, struct device_attribute *attr,
  128. char *buf)
  129. {
  130. return sprintf(buf, "%llu\n",
  131. (unsigned long long)balloon_stats.target_pages
  132. << PAGE_SHIFT);
  133. }
  134. static ssize_t store_target(struct device *dev,
  135. struct device_attribute *attr,
  136. const char *buf,
  137. size_t count)
  138. {
  139. char *endchar;
  140. unsigned long long target_bytes;
  141. if (!capable(CAP_SYS_ADMIN))
  142. return -EPERM;
  143. target_bytes = memparse(buf, &endchar);
  144. balloon_set_new_target(target_bytes >> PAGE_SHIFT);
  145. return count;
  146. }
  147. static DEVICE_ATTR(target, S_IRUGO | S_IWUSR,
  148. show_target, store_target);
  149. static struct attribute *balloon_attrs[] = {
  150. &dev_attr_target_kb.attr,
  151. &dev_attr_target.attr,
  152. &dev_attr_schedule_delay.attr.attr,
  153. &dev_attr_max_schedule_delay.attr.attr,
  154. &dev_attr_retry_count.attr.attr,
  155. &dev_attr_max_retry_count.attr.attr,
  156. NULL
  157. };
  158. static const struct attribute_group balloon_group = {
  159. .attrs = balloon_attrs
  160. };
  161. static struct attribute *balloon_info_attrs[] = {
  162. &dev_attr_current_kb.attr,
  163. &dev_attr_low_kb.attr,
  164. &dev_attr_high_kb.attr,
  165. NULL
  166. };
  167. static const struct attribute_group balloon_info_group = {
  168. .name = "info",
  169. .attrs = balloon_info_attrs
  170. };
  171. static const struct attribute_group *balloon_groups[] = {
  172. &balloon_group,
  173. &balloon_info_group,
  174. NULL
  175. };
  176. static struct bus_type balloon_subsys = {
  177. .name = BALLOON_CLASS_NAME,
  178. .dev_name = BALLOON_CLASS_NAME,
  179. };
  180. static int register_balloon(struct device *dev)
  181. {
  182. int error;
  183. error = subsys_system_register(&balloon_subsys, NULL);
  184. if (error)
  185. return error;
  186. dev->id = 0;
  187. dev->bus = &balloon_subsys;
  188. dev->groups = balloon_groups;
  189. error = device_register(dev);
  190. if (error) {
  191. bus_unregister(&balloon_subsys);
  192. return error;
  193. }
  194. return 0;
  195. }