virtio_balloon.c 12 KB

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