virtio_balloon.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * Virtio balloon implementation, inspired by Dor Laor and Marcelo
  3. * Tosatti's implementations.
  4. *
  5. * Copyright 2008 Rusty Russell IBM Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  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, GFP_KERNEL) < 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, GFP_KERNEL) < 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 init_vqs(struct virtio_balloon *vb)
  232. {
  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. /*
  238. * We expect two virtqueues: inflate and deflate, and
  239. * optionally stat.
  240. */
  241. nvqs = virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ) ? 3 : 2;
  242. err = vb->vdev->config->find_vqs(vb->vdev, nvqs, vqs, callbacks, names);
  243. if (err)
  244. return err;
  245. vb->inflate_vq = vqs[0];
  246. vb->deflate_vq = vqs[1];
  247. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) {
  248. struct scatterlist sg;
  249. vb->stats_vq = vqs[2];
  250. /*
  251. * Prime this virtqueue with one buffer so the hypervisor can
  252. * use it to signal us later.
  253. */
  254. sg_init_one(&sg, vb->stats, sizeof vb->stats);
  255. if (virtqueue_add_buf(vb->stats_vq, &sg, 1, 0, vb, GFP_KERNEL)
  256. < 0)
  257. BUG();
  258. virtqueue_kick(vb->stats_vq);
  259. }
  260. return 0;
  261. }
  262. static int virtballoon_probe(struct virtio_device *vdev)
  263. {
  264. struct virtio_balloon *vb;
  265. int err;
  266. vdev->priv = vb = kmalloc(sizeof(*vb), GFP_KERNEL);
  267. if (!vb) {
  268. err = -ENOMEM;
  269. goto out;
  270. }
  271. INIT_LIST_HEAD(&vb->pages);
  272. vb->num_pages = 0;
  273. init_waitqueue_head(&vb->config_change);
  274. vb->vdev = vdev;
  275. vb->need_stats_update = 0;
  276. err = init_vqs(vb);
  277. if (err)
  278. goto out_free_vb;
  279. vb->thread = kthread_run(balloon, vb, "vballoon");
  280. if (IS_ERR(vb->thread)) {
  281. err = PTR_ERR(vb->thread);
  282. goto out_del_vqs;
  283. }
  284. return 0;
  285. out_del_vqs:
  286. vdev->config->del_vqs(vdev);
  287. out_free_vb:
  288. kfree(vb);
  289. out:
  290. return err;
  291. }
  292. static void __devexit virtballoon_remove(struct virtio_device *vdev)
  293. {
  294. struct virtio_balloon *vb = vdev->priv;
  295. kthread_stop(vb->thread);
  296. /* There might be pages left in the balloon: free them. */
  297. while (vb->num_pages)
  298. leak_balloon(vb, vb->num_pages);
  299. /* Now we reset the device so we can clean up the queues. */
  300. vdev->config->reset(vdev);
  301. vdev->config->del_vqs(vdev);
  302. kfree(vb);
  303. }
  304. #ifdef CONFIG_PM
  305. static int virtballoon_freeze(struct virtio_device *vdev)
  306. {
  307. struct virtio_balloon *vb = vdev->priv;
  308. /*
  309. * The kthread is already frozen by the PM core before this
  310. * function is called.
  311. */
  312. while (vb->num_pages)
  313. leak_balloon(vb, vb->num_pages);
  314. update_balloon_size(vb);
  315. /* Ensure we don't get any more requests from the host */
  316. vdev->config->reset(vdev);
  317. vdev->config->del_vqs(vdev);
  318. return 0;
  319. }
  320. static int restore_common(struct virtio_device *vdev)
  321. {
  322. struct virtio_balloon *vb = vdev->priv;
  323. int ret;
  324. ret = init_vqs(vdev->priv);
  325. if (ret)
  326. return ret;
  327. fill_balloon(vb, towards_target(vb));
  328. update_balloon_size(vb);
  329. return 0;
  330. }
  331. static int virtballoon_thaw(struct virtio_device *vdev)
  332. {
  333. return restore_common(vdev);
  334. }
  335. static int virtballoon_restore(struct virtio_device *vdev)
  336. {
  337. struct virtio_balloon *vb = vdev->priv;
  338. /*
  339. * If a request wasn't complete at the time of freezing, this
  340. * could have been set.
  341. */
  342. vb->need_stats_update = 0;
  343. return restore_common(vdev);
  344. }
  345. #endif
  346. static unsigned int features[] = {
  347. VIRTIO_BALLOON_F_MUST_TELL_HOST,
  348. VIRTIO_BALLOON_F_STATS_VQ,
  349. };
  350. static struct virtio_driver virtio_balloon_driver = {
  351. .feature_table = features,
  352. .feature_table_size = ARRAY_SIZE(features),
  353. .driver.name = KBUILD_MODNAME,
  354. .driver.owner = THIS_MODULE,
  355. .id_table = id_table,
  356. .probe = virtballoon_probe,
  357. .remove = __devexit_p(virtballoon_remove),
  358. .config_changed = virtballoon_changed,
  359. #ifdef CONFIG_PM
  360. .freeze = virtballoon_freeze,
  361. .restore = virtballoon_restore,
  362. .thaw = virtballoon_thaw,
  363. #endif
  364. };
  365. static int __init init(void)
  366. {
  367. return register_virtio_driver(&virtio_balloon_driver);
  368. }
  369. static void __exit fini(void)
  370. {
  371. unregister_virtio_driver(&virtio_balloon_driver);
  372. }
  373. module_init(init);
  374. module_exit(fini);
  375. MODULE_DEVICE_TABLE(virtio, id_table);
  376. MODULE_DESCRIPTION("Virtio balloon driver");
  377. MODULE_LICENSE("GPL");