virtio_balloon.c 9.7 KB

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