virtio_balloon.c 7.1 KB

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