virtio_balloon.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. struct virtio_balloon
  27. {
  28. struct virtio_device *vdev;
  29. struct virtqueue *inflate_vq, *deflate_vq;
  30. /* Where the ballooning thread waits for config to change. */
  31. wait_queue_head_t config_change;
  32. /* The thread servicing the balloon. */
  33. struct task_struct *thread;
  34. /* Waiting for host to ack the pages we released. */
  35. struct completion acked;
  36. /* Do we have to tell Host *before* we reuse pages? */
  37. bool tell_host_first;
  38. /* The pages we've told the Host we're not using. */
  39. unsigned int num_pages;
  40. struct list_head pages;
  41. /* The array of pfns we tell the Host about. */
  42. unsigned int num_pfns;
  43. u32 pfns[256];
  44. };
  45. static struct virtio_device_id id_table[] = {
  46. { VIRTIO_ID_BALLOON, VIRTIO_DEV_ANY_ID },
  47. { 0 },
  48. };
  49. static void balloon_ack(struct virtqueue *vq)
  50. {
  51. struct virtio_balloon *vb;
  52. unsigned int len;
  53. vb = vq->vq_ops->get_buf(vq, &len);
  54. if (vb)
  55. complete(&vb->acked);
  56. }
  57. static void tell_host(struct virtio_balloon *vb, struct virtqueue *vq)
  58. {
  59. struct scatterlist sg;
  60. sg_init_one(&sg, vb->pfns, sizeof(vb->pfns[0]) * vb->num_pfns);
  61. init_completion(&vb->acked);
  62. /* We should always be able to add one buffer to an empty queue. */
  63. if (vq->vq_ops->add_buf(vq, &sg, 1, 0, vb) != 0)
  64. BUG();
  65. vq->vq_ops->kick(vq);
  66. /* When host has read buffer, this completes via balloon_ack */
  67. wait_for_completion(&vb->acked);
  68. }
  69. static void fill_balloon(struct virtio_balloon *vb, size_t num)
  70. {
  71. /* We can only do one array worth at a time. */
  72. num = min(num, ARRAY_SIZE(vb->pfns));
  73. for (vb->num_pfns = 0; vb->num_pfns < num; vb->num_pfns++) {
  74. struct page *page = alloc_page(GFP_HIGHUSER | __GFP_NORETRY);
  75. if (!page) {
  76. if (printk_ratelimit())
  77. dev_printk(KERN_INFO, &vb->vdev->dev,
  78. "Out of puff! Can't get %zu pages\n",
  79. num);
  80. /* Sleep for at least 1/5 of a second before retry. */
  81. msleep(200);
  82. break;
  83. }
  84. vb->pfns[vb->num_pfns] = page_to_pfn(page);
  85. totalram_pages--;
  86. vb->num_pages++;
  87. list_add(&page->lru, &vb->pages);
  88. }
  89. /* Didn't get any? Oh well. */
  90. if (vb->num_pfns == 0)
  91. return;
  92. tell_host(vb, vb->inflate_vq);
  93. }
  94. static void release_pages_by_pfn(const u32 pfns[], unsigned int num)
  95. {
  96. unsigned int i;
  97. for (i = 0; i < num; i++) {
  98. __free_page(pfn_to_page(pfns[i]));
  99. totalram_pages++;
  100. }
  101. }
  102. static void leak_balloon(struct virtio_balloon *vb, size_t num)
  103. {
  104. struct page *page;
  105. /* We can only do one array worth at a time. */
  106. num = min(num, ARRAY_SIZE(vb->pfns));
  107. for (vb->num_pfns = 0; vb->num_pfns < num; vb->num_pfns++) {
  108. page = list_first_entry(&vb->pages, struct page, lru);
  109. list_del(&page->lru);
  110. vb->pfns[vb->num_pfns] = page_to_pfn(page);
  111. vb->num_pages--;
  112. }
  113. if (vb->tell_host_first) {
  114. tell_host(vb, vb->deflate_vq);
  115. release_pages_by_pfn(vb->pfns, vb->num_pfns);
  116. } else {
  117. release_pages_by_pfn(vb->pfns, vb->num_pfns);
  118. tell_host(vb, vb->deflate_vq);
  119. }
  120. }
  121. static void virtballoon_changed(struct virtio_device *vdev)
  122. {
  123. struct virtio_balloon *vb = vdev->priv;
  124. wake_up(&vb->config_change);
  125. }
  126. static inline int towards_target(struct virtio_balloon *vb)
  127. {
  128. u32 v;
  129. __virtio_config_val(vb->vdev,
  130. offsetof(struct virtio_balloon_config, num_pages),
  131. &v);
  132. return v - vb->num_pages;
  133. }
  134. static void update_balloon_size(struct virtio_balloon *vb)
  135. {
  136. __le32 actual = cpu_to_le32(vb->num_pages);
  137. vb->vdev->config->set(vb->vdev,
  138. offsetof(struct virtio_balloon_config, actual),
  139. &actual, sizeof(actual));
  140. }
  141. static int balloon(void *_vballoon)
  142. {
  143. struct virtio_balloon *vb = _vballoon;
  144. set_freezable();
  145. while (!kthread_should_stop()) {
  146. int diff;
  147. try_to_freeze();
  148. wait_event_interruptible(vb->config_change,
  149. (diff = towards_target(vb)) != 0
  150. || kthread_should_stop());
  151. if (diff > 0)
  152. fill_balloon(vb, diff);
  153. else if (diff < 0)
  154. leak_balloon(vb, -diff);
  155. update_balloon_size(vb);
  156. }
  157. return 0;
  158. }
  159. static int virtballoon_probe(struct virtio_device *vdev)
  160. {
  161. struct virtio_balloon *vb;
  162. int err;
  163. vdev->priv = vb = kmalloc(sizeof(*vb), GFP_KERNEL);
  164. if (!vb) {
  165. err = -ENOMEM;
  166. goto out;
  167. }
  168. INIT_LIST_HEAD(&vb->pages);
  169. vb->num_pages = 0;
  170. init_waitqueue_head(&vb->config_change);
  171. vb->vdev = vdev;
  172. /* We expect two virtqueues. */
  173. vb->inflate_vq = vdev->config->find_vq(vdev, 0, balloon_ack);
  174. if (IS_ERR(vb->inflate_vq)) {
  175. err = PTR_ERR(vb->inflate_vq);
  176. goto out_free_vb;
  177. }
  178. vb->deflate_vq = vdev->config->find_vq(vdev, 1, balloon_ack);
  179. if (IS_ERR(vb->deflate_vq)) {
  180. err = PTR_ERR(vb->deflate_vq);
  181. goto out_del_inflate_vq;
  182. }
  183. vb->thread = kthread_run(balloon, vb, "vballoon");
  184. if (IS_ERR(vb->thread)) {
  185. err = PTR_ERR(vb->thread);
  186. goto out_del_deflate_vq;
  187. }
  188. vb->tell_host_first
  189. = vdev->config->feature(vdev, VIRTIO_BALLOON_F_MUST_TELL_HOST);
  190. return 0;
  191. out_del_deflate_vq:
  192. vdev->config->del_vq(vb->deflate_vq);
  193. out_del_inflate_vq:
  194. vdev->config->del_vq(vb->inflate_vq);
  195. out_free_vb:
  196. kfree(vb);
  197. out:
  198. return err;
  199. }
  200. static void virtballoon_remove(struct virtio_device *vdev)
  201. {
  202. struct virtio_balloon *vb = vdev->priv;
  203. kthread_stop(vb->thread);
  204. /* There might be pages left in the balloon: free them. */
  205. while (vb->num_pages)
  206. leak_balloon(vb, vb->num_pages);
  207. /* Now we reset the device so we can clean up the queues. */
  208. vdev->config->reset(vdev);
  209. vdev->config->del_vq(vb->deflate_vq);
  210. vdev->config->del_vq(vb->inflate_vq);
  211. kfree(vb);
  212. }
  213. static struct virtio_driver virtio_balloon = {
  214. .driver.name = KBUILD_MODNAME,
  215. .driver.owner = THIS_MODULE,
  216. .id_table = id_table,
  217. .probe = virtballoon_probe,
  218. .remove = __devexit_p(virtballoon_remove),
  219. .config_changed = virtballoon_changed,
  220. };
  221. static int __init init(void)
  222. {
  223. return register_virtio_driver(&virtio_balloon);
  224. }
  225. static void __exit fini(void)
  226. {
  227. unregister_virtio_driver(&virtio_balloon);
  228. }
  229. module_init(init);
  230. module_exit(fini);
  231. MODULE_DEVICE_TABLE(virtio, id_table);
  232. MODULE_DESCRIPTION("Virtio balloon driver");
  233. MODULE_LICENSE("GPL");