xen-balloon.c 7.1 KB

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