virtio_balloon.c 9.8 KB

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