virtio_balloon.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /* Virtio balloon implementation, inspired by Dor Loar and Marcelo
  2. * Tosatti's implementations.
  3. *
  4. * Copyright 2008 Rusty Russell IBM Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. //#define DEBUG
  21. #include <linux/virtio.h>
  22. #include <linux/virtio_balloon.h>
  23. #include <linux/swap.h>
  24. #include <linux/kthread.h>
  25. #include <linux/freezer.h>
  26. #include <linux/delay.h>
  27. struct virtio_balloon
  28. {
  29. struct virtio_device *vdev;
  30. struct virtqueue *inflate_vq, *deflate_vq, *stats_vq;
  31. /* Where the ballooning thread waits for config to change. */
  32. wait_queue_head_t config_change;
  33. /* The thread servicing the balloon. */
  34. struct task_struct *thread;
  35. /* Waiting for host to ack the pages we released. */
  36. struct completion acked;
  37. /* Do we have to tell Host *before* we reuse pages? */
  38. bool tell_host_first;
  39. /* The pages we've told the Host we're not using. */
  40. unsigned int num_pages;
  41. struct list_head pages;
  42. /* The array of pfns we tell the Host about. */
  43. unsigned int num_pfns;
  44. u32 pfns[256];
  45. /* Memory statistics */
  46. struct virtio_balloon_stat stats[VIRTIO_BALLOON_S_NR];
  47. };
  48. static struct virtio_device_id id_table[] = {
  49. { VIRTIO_ID_BALLOON, VIRTIO_DEV_ANY_ID },
  50. { 0 },
  51. };
  52. static u32 page_to_balloon_pfn(struct page *page)
  53. {
  54. unsigned long pfn = page_to_pfn(page);
  55. BUILD_BUG_ON(PAGE_SHIFT < VIRTIO_BALLOON_PFN_SHIFT);
  56. /* Convert pfn from Linux page size to balloon page size. */
  57. return pfn >> (PAGE_SHIFT - VIRTIO_BALLOON_PFN_SHIFT);
  58. }
  59. static void balloon_ack(struct virtqueue *vq)
  60. {
  61. struct virtio_balloon *vb;
  62. unsigned int len;
  63. vb = vq->vq_ops->get_buf(vq, &len);
  64. if (vb)
  65. complete(&vb->acked);
  66. }
  67. static void tell_host(struct virtio_balloon *vb, struct virtqueue *vq)
  68. {
  69. struct scatterlist sg;
  70. sg_init_one(&sg, vb->pfns, sizeof(vb->pfns[0]) * vb->num_pfns);
  71. init_completion(&vb->acked);
  72. /* We should always be able to add one buffer to an empty queue. */
  73. if (vq->vq_ops->add_buf(vq, &sg, 1, 0, vb) < 0)
  74. BUG();
  75. vq->vq_ops->kick(vq);
  76. /* When host has read buffer, this completes via balloon_ack */
  77. wait_for_completion(&vb->acked);
  78. }
  79. static void fill_balloon(struct virtio_balloon *vb, size_t num)
  80. {
  81. /* We can only do one array worth at a time. */
  82. num = min(num, ARRAY_SIZE(vb->pfns));
  83. for (vb->num_pfns = 0; vb->num_pfns < num; vb->num_pfns++) {
  84. struct page *page = alloc_page(GFP_HIGHUSER | __GFP_NORETRY);
  85. if (!page) {
  86. if (printk_ratelimit())
  87. dev_printk(KERN_INFO, &vb->vdev->dev,
  88. "Out of puff! Can't get %zu pages\n",
  89. num);
  90. /* Sleep for at least 1/5 of a second before retry. */
  91. msleep(200);
  92. break;
  93. }
  94. vb->pfns[vb->num_pfns] = page_to_balloon_pfn(page);
  95. totalram_pages--;
  96. vb->num_pages++;
  97. list_add(&page->lru, &vb->pages);
  98. }
  99. /* Didn't get any? Oh well. */
  100. if (vb->num_pfns == 0)
  101. return;
  102. tell_host(vb, vb->inflate_vq);
  103. }
  104. static void release_pages_by_pfn(const u32 pfns[], unsigned int num)
  105. {
  106. unsigned int i;
  107. for (i = 0; i < num; i++) {
  108. __free_page(pfn_to_page(pfns[i]));
  109. totalram_pages++;
  110. }
  111. }
  112. static void leak_balloon(struct virtio_balloon *vb, size_t num)
  113. {
  114. struct page *page;
  115. /* We can only do one array worth at a time. */
  116. num = min(num, ARRAY_SIZE(vb->pfns));
  117. for (vb->num_pfns = 0; vb->num_pfns < num; vb->num_pfns++) {
  118. page = list_first_entry(&vb->pages, struct page, lru);
  119. list_del(&page->lru);
  120. vb->pfns[vb->num_pfns] = page_to_balloon_pfn(page);
  121. vb->num_pages--;
  122. }
  123. if (vb->tell_host_first) {
  124. tell_host(vb, vb->deflate_vq);
  125. release_pages_by_pfn(vb->pfns, vb->num_pfns);
  126. } else {
  127. release_pages_by_pfn(vb->pfns, vb->num_pfns);
  128. tell_host(vb, vb->deflate_vq);
  129. }
  130. }
  131. static inline void update_stat(struct virtio_balloon *vb, int idx,
  132. u16 tag, u64 val)
  133. {
  134. BUG_ON(idx >= VIRTIO_BALLOON_S_NR);
  135. vb->stats[idx].tag = tag;
  136. vb->stats[idx].val = val;
  137. }
  138. #define pages_to_bytes(x) ((u64)(x) << PAGE_SHIFT)
  139. static void update_balloon_stats(struct virtio_balloon *vb)
  140. {
  141. unsigned long events[NR_VM_EVENT_ITEMS];
  142. struct sysinfo i;
  143. int idx = 0;
  144. all_vm_events(events);
  145. si_meminfo(&i);
  146. update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_IN,
  147. pages_to_bytes(events[PSWPIN]));
  148. update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_OUT,
  149. pages_to_bytes(events[PSWPOUT]));
  150. update_stat(vb, idx++, VIRTIO_BALLOON_S_MAJFLT, events[PGMAJFAULT]);
  151. update_stat(vb, idx++, VIRTIO_BALLOON_S_MINFLT, events[PGFAULT]);
  152. update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMFREE,
  153. pages_to_bytes(i.freeram));
  154. update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMTOT,
  155. pages_to_bytes(i.totalram));
  156. }
  157. /*
  158. * While most virtqueues communicate guest-initiated requests to the hypervisor,
  159. * the stats queue operates in reverse. The driver initializes the virtqueue
  160. * with a single buffer. From that point forward, all conversations consist of
  161. * a hypervisor request (a call to this function) which directs us to refill
  162. * the virtqueue with a fresh stats buffer.
  163. */
  164. static void stats_ack(struct virtqueue *vq)
  165. {
  166. struct virtio_balloon *vb;
  167. unsigned int len;
  168. struct scatterlist sg;
  169. vb = vq->vq_ops->get_buf(vq, &len);
  170. if (!vb)
  171. return;
  172. update_balloon_stats(vb);
  173. sg_init_one(&sg, vb->stats, sizeof(vb->stats));
  174. if (vq->vq_ops->add_buf(vq, &sg, 1, 0, vb) < 0)
  175. BUG();
  176. vq->vq_ops->kick(vq);
  177. }
  178. static void virtballoon_changed(struct virtio_device *vdev)
  179. {
  180. struct virtio_balloon *vb = vdev->priv;
  181. wake_up(&vb->config_change);
  182. }
  183. static inline s64 towards_target(struct virtio_balloon *vb)
  184. {
  185. u32 v;
  186. vb->vdev->config->get(vb->vdev,
  187. offsetof(struct virtio_balloon_config, num_pages),
  188. &v, sizeof(v));
  189. return (s64)v - vb->num_pages;
  190. }
  191. static void update_balloon_size(struct virtio_balloon *vb)
  192. {
  193. __le32 actual = cpu_to_le32(vb->num_pages);
  194. vb->vdev->config->set(vb->vdev,
  195. offsetof(struct virtio_balloon_config, actual),
  196. &actual, sizeof(actual));
  197. }
  198. static int balloon(void *_vballoon)
  199. {
  200. struct virtio_balloon *vb = _vballoon;
  201. set_freezable();
  202. while (!kthread_should_stop()) {
  203. s64 diff;
  204. try_to_freeze();
  205. wait_event_interruptible(vb->config_change,
  206. (diff = towards_target(vb)) != 0
  207. || kthread_should_stop()
  208. || freezing(current));
  209. if (diff > 0)
  210. fill_balloon(vb, diff);
  211. else if (diff < 0)
  212. leak_balloon(vb, -diff);
  213. update_balloon_size(vb);
  214. }
  215. return 0;
  216. }
  217. static int virtballoon_probe(struct virtio_device *vdev)
  218. {
  219. struct virtio_balloon *vb;
  220. struct virtqueue *vqs[3];
  221. vq_callback_t *callbacks[] = { balloon_ack, balloon_ack, stats_ack };
  222. const char *names[] = { "inflate", "deflate", "stats" };
  223. int err, nvqs;
  224. vdev->priv = vb = kmalloc(sizeof(*vb), GFP_KERNEL);
  225. if (!vb) {
  226. err = -ENOMEM;
  227. goto out;
  228. }
  229. INIT_LIST_HEAD(&vb->pages);
  230. vb->num_pages = 0;
  231. init_waitqueue_head(&vb->config_change);
  232. vb->vdev = vdev;
  233. /* We expect two virtqueues: inflate and deflate,
  234. * and optionally stat. */
  235. nvqs = virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ) ? 3 : 2;
  236. err = vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names);
  237. if (err)
  238. goto out_free_vb;
  239. vb->inflate_vq = vqs[0];
  240. vb->deflate_vq = vqs[1];
  241. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) {
  242. struct scatterlist sg;
  243. vb->stats_vq = vqs[2];
  244. /*
  245. * Prime this virtqueue with one buffer so the hypervisor can
  246. * use it to signal us later.
  247. */
  248. sg_init_one(&sg, vb->stats, sizeof vb->stats);
  249. if (vb->stats_vq->vq_ops->add_buf(vb->stats_vq,
  250. &sg, 1, 0, vb) < 0)
  251. BUG();
  252. vb->stats_vq->vq_ops->kick(vb->stats_vq);
  253. }
  254. vb->thread = kthread_run(balloon, vb, "vballoon");
  255. if (IS_ERR(vb->thread)) {
  256. err = PTR_ERR(vb->thread);
  257. goto out_del_vqs;
  258. }
  259. vb->tell_host_first
  260. = virtio_has_feature(vdev, VIRTIO_BALLOON_F_MUST_TELL_HOST);
  261. return 0;
  262. out_del_vqs:
  263. vdev->config->del_vqs(vdev);
  264. out_free_vb:
  265. kfree(vb);
  266. out:
  267. return err;
  268. }
  269. static void __devexit virtballoon_remove(struct virtio_device *vdev)
  270. {
  271. struct virtio_balloon *vb = vdev->priv;
  272. kthread_stop(vb->thread);
  273. /* There might be pages left in the balloon: free them. */
  274. while (vb->num_pages)
  275. leak_balloon(vb, vb->num_pages);
  276. /* Now we reset the device so we can clean up the queues. */
  277. vdev->config->reset(vdev);
  278. vdev->config->del_vqs(vdev);
  279. kfree(vb);
  280. }
  281. static unsigned int features[] = {
  282. VIRTIO_BALLOON_F_MUST_TELL_HOST,
  283. VIRTIO_BALLOON_F_STATS_VQ,
  284. };
  285. static struct virtio_driver virtio_balloon_driver = {
  286. .feature_table = features,
  287. .feature_table_size = ARRAY_SIZE(features),
  288. .driver.name = KBUILD_MODNAME,
  289. .driver.owner = THIS_MODULE,
  290. .id_table = id_table,
  291. .probe = virtballoon_probe,
  292. .remove = __devexit_p(virtballoon_remove),
  293. .config_changed = virtballoon_changed,
  294. };
  295. static int __init init(void)
  296. {
  297. return register_virtio_driver(&virtio_balloon_driver);
  298. }
  299. static void __exit fini(void)
  300. {
  301. unregister_virtio_driver(&virtio_balloon_driver);
  302. }
  303. module_init(init);
  304. module_exit(fini);
  305. MODULE_DEVICE_TABLE(virtio, id_table);
  306. MODULE_DESCRIPTION("Virtio balloon driver");
  307. MODULE_LICENSE("GPL");