virtio_balloon.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. #include <linux/slab.h>
  28. struct virtio_balloon
  29. {
  30. struct virtio_device *vdev;
  31. struct virtqueue *inflate_vq, *deflate_vq, *stats_vq;
  32. /* Where the ballooning thread waits for config to change. */
  33. wait_queue_head_t config_change;
  34. /* The thread servicing the balloon. */
  35. struct task_struct *thread;
  36. /* Waiting for host to ack the pages we released. */
  37. struct completion acked;
  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. /* Memory statistics */
  45. int need_stats_update;
  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 = virtqueue_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 (virtqueue_add_buf(vq, &sg, 1, 0, vb) < 0)
  74. BUG();
  75. virtqueue_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. __GFP_NOMEMALLOC | __GFP_NOWARN);
  86. if (!page) {
  87. if (printk_ratelimit())
  88. dev_printk(KERN_INFO, &vb->vdev->dev,
  89. "Out of puff! Can't get %zu pages\n",
  90. num);
  91. /* Sleep for at least 1/5 of a second before retry. */
  92. msleep(200);
  93. break;
  94. }
  95. vb->pfns[vb->num_pfns] = page_to_balloon_pfn(page);
  96. totalram_pages--;
  97. vb->num_pages++;
  98. list_add(&page->lru, &vb->pages);
  99. }
  100. /* Didn't get any? Oh well. */
  101. if (vb->num_pfns == 0)
  102. return;
  103. tell_host(vb, vb->inflate_vq);
  104. }
  105. static void release_pages_by_pfn(const u32 pfns[], unsigned int num)
  106. {
  107. unsigned int i;
  108. for (i = 0; i < num; i++) {
  109. __free_page(pfn_to_page(pfns[i]));
  110. totalram_pages++;
  111. }
  112. }
  113. static void leak_balloon(struct virtio_balloon *vb, size_t num)
  114. {
  115. struct page *page;
  116. /* We can only do one array worth at a time. */
  117. num = min(num, ARRAY_SIZE(vb->pfns));
  118. for (vb->num_pfns = 0; vb->num_pfns < num; vb->num_pfns++) {
  119. page = list_first_entry(&vb->pages, struct page, lru);
  120. list_del(&page->lru);
  121. vb->pfns[vb->num_pfns] = page_to_balloon_pfn(page);
  122. vb->num_pages--;
  123. }
  124. /*
  125. * Note that if
  126. * virtio_has_feature(vdev, VIRTIO_BALLOON_F_MUST_TELL_HOST);
  127. * is true, we *have* to do it in this order
  128. */
  129. tell_host(vb, vb->deflate_vq);
  130. release_pages_by_pfn(vb->pfns, vb->num_pfns);
  131. }
  132. static inline void update_stat(struct virtio_balloon *vb, int idx,
  133. u16 tag, u64 val)
  134. {
  135. BUG_ON(idx >= VIRTIO_BALLOON_S_NR);
  136. vb->stats[idx].tag = tag;
  137. vb->stats[idx].val = val;
  138. }
  139. #define pages_to_bytes(x) ((u64)(x) << PAGE_SHIFT)
  140. static void update_balloon_stats(struct virtio_balloon *vb)
  141. {
  142. unsigned long events[NR_VM_EVENT_ITEMS];
  143. struct sysinfo i;
  144. int idx = 0;
  145. all_vm_events(events);
  146. si_meminfo(&i);
  147. update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_IN,
  148. pages_to_bytes(events[PSWPIN]));
  149. update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_OUT,
  150. pages_to_bytes(events[PSWPOUT]));
  151. update_stat(vb, idx++, VIRTIO_BALLOON_S_MAJFLT, events[PGMAJFAULT]);
  152. update_stat(vb, idx++, VIRTIO_BALLOON_S_MINFLT, events[PGFAULT]);
  153. update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMFREE,
  154. pages_to_bytes(i.freeram));
  155. update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMTOT,
  156. pages_to_bytes(i.totalram));
  157. }
  158. /*
  159. * While most virtqueues communicate guest-initiated requests to the hypervisor,
  160. * the stats queue operates in reverse. The driver initializes the virtqueue
  161. * with a single buffer. From that point forward, all conversations consist of
  162. * a hypervisor request (a call to this function) which directs us to refill
  163. * the virtqueue with a fresh stats buffer. Since stats collection can sleep,
  164. * we notify our kthread which does the actual work via stats_handle_request().
  165. */
  166. static void stats_request(struct virtqueue *vq)
  167. {
  168. struct virtio_balloon *vb;
  169. unsigned int len;
  170. vb = virtqueue_get_buf(vq, &len);
  171. if (!vb)
  172. return;
  173. vb->need_stats_update = 1;
  174. wake_up(&vb->config_change);
  175. }
  176. static void stats_handle_request(struct virtio_balloon *vb)
  177. {
  178. struct virtqueue *vq;
  179. struct scatterlist sg;
  180. vb->need_stats_update = 0;
  181. update_balloon_stats(vb);
  182. vq = vb->stats_vq;
  183. sg_init_one(&sg, vb->stats, sizeof(vb->stats));
  184. if (virtqueue_add_buf(vq, &sg, 1, 0, vb) < 0)
  185. BUG();
  186. virtqueue_kick(vq);
  187. }
  188. static void virtballoon_changed(struct virtio_device *vdev)
  189. {
  190. struct virtio_balloon *vb = vdev->priv;
  191. wake_up(&vb->config_change);
  192. }
  193. static inline s64 towards_target(struct virtio_balloon *vb)
  194. {
  195. u32 v;
  196. vb->vdev->config->get(vb->vdev,
  197. offsetof(struct virtio_balloon_config, num_pages),
  198. &v, sizeof(v));
  199. return (s64)v - vb->num_pages;
  200. }
  201. static void update_balloon_size(struct virtio_balloon *vb)
  202. {
  203. __le32 actual = cpu_to_le32(vb->num_pages);
  204. vb->vdev->config->set(vb->vdev,
  205. offsetof(struct virtio_balloon_config, actual),
  206. &actual, sizeof(actual));
  207. }
  208. static int balloon(void *_vballoon)
  209. {
  210. struct virtio_balloon *vb = _vballoon;
  211. set_freezable();
  212. while (!kthread_should_stop()) {
  213. s64 diff;
  214. try_to_freeze();
  215. wait_event_interruptible(vb->config_change,
  216. (diff = towards_target(vb)) != 0
  217. || vb->need_stats_update
  218. || kthread_should_stop()
  219. || freezing(current));
  220. if (vb->need_stats_update)
  221. stats_handle_request(vb);
  222. if (diff > 0)
  223. fill_balloon(vb, diff);
  224. else if (diff < 0)
  225. leak_balloon(vb, -diff);
  226. update_balloon_size(vb);
  227. }
  228. return 0;
  229. }
  230. static int virtballoon_probe(struct virtio_device *vdev)
  231. {
  232. struct virtio_balloon *vb;
  233. struct virtqueue *vqs[3];
  234. vq_callback_t *callbacks[] = { balloon_ack, balloon_ack, stats_request };
  235. const char *names[] = { "inflate", "deflate", "stats" };
  236. int err, nvqs;
  237. vdev->priv = vb = kmalloc(sizeof(*vb), GFP_KERNEL);
  238. if (!vb) {
  239. err = -ENOMEM;
  240. goto out;
  241. }
  242. INIT_LIST_HEAD(&vb->pages);
  243. vb->num_pages = 0;
  244. init_waitqueue_head(&vb->config_change);
  245. vb->vdev = vdev;
  246. vb->need_stats_update = 0;
  247. /* We expect two virtqueues: inflate and deflate,
  248. * and optionally stat. */
  249. nvqs = virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ) ? 3 : 2;
  250. err = vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names);
  251. if (err)
  252. goto out_free_vb;
  253. vb->inflate_vq = vqs[0];
  254. vb->deflate_vq = vqs[1];
  255. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) {
  256. struct scatterlist sg;
  257. vb->stats_vq = vqs[2];
  258. /*
  259. * Prime this virtqueue with one buffer so the hypervisor can
  260. * use it to signal us later.
  261. */
  262. sg_init_one(&sg, vb->stats, sizeof vb->stats);
  263. if (virtqueue_add_buf(vb->stats_vq, &sg, 1, 0, vb) < 0)
  264. BUG();
  265. virtqueue_kick(vb->stats_vq);
  266. }
  267. vb->thread = kthread_run(balloon, vb, "vballoon");
  268. if (IS_ERR(vb->thread)) {
  269. err = PTR_ERR(vb->thread);
  270. goto out_del_vqs;
  271. }
  272. return 0;
  273. out_del_vqs:
  274. vdev->config->del_vqs(vdev);
  275. out_free_vb:
  276. kfree(vb);
  277. out:
  278. return err;
  279. }
  280. static void __devexit virtballoon_remove(struct virtio_device *vdev)
  281. {
  282. struct virtio_balloon *vb = vdev->priv;
  283. kthread_stop(vb->thread);
  284. /* There might be pages left in the balloon: free them. */
  285. while (vb->num_pages)
  286. leak_balloon(vb, vb->num_pages);
  287. /* Now we reset the device so we can clean up the queues. */
  288. vdev->config->reset(vdev);
  289. vdev->config->del_vqs(vdev);
  290. kfree(vb);
  291. }
  292. static unsigned int features[] = {
  293. VIRTIO_BALLOON_F_MUST_TELL_HOST,
  294. VIRTIO_BALLOON_F_STATS_VQ,
  295. };
  296. static struct virtio_driver virtio_balloon_driver = {
  297. .feature_table = features,
  298. .feature_table_size = ARRAY_SIZE(features),
  299. .driver.name = KBUILD_MODNAME,
  300. .driver.owner = THIS_MODULE,
  301. .id_table = id_table,
  302. .probe = virtballoon_probe,
  303. .remove = __devexit_p(virtballoon_remove),
  304. .config_changed = virtballoon_changed,
  305. };
  306. static int __init init(void)
  307. {
  308. return register_virtio_driver(&virtio_balloon_driver);
  309. }
  310. static void __exit fini(void)
  311. {
  312. unregister_virtio_driver(&virtio_balloon_driver);
  313. }
  314. module_init(init);
  315. module_exit(fini);
  316. MODULE_DEVICE_TABLE(virtio, id_table);
  317. MODULE_DESCRIPTION("Virtio balloon driver");
  318. MODULE_LICENSE("GPL");