virtio_balloon.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. dev_info_ratelimited(&vb->vdev->dev,
  111. "Out of puff! Can't get %zu pages\n",
  112. num);
  113. /* Sleep for at least 1/5 of a second before retry. */
  114. msleep(200);
  115. break;
  116. }
  117. set_page_pfns(vb->pfns + vb->num_pfns, page);
  118. vb->num_pages += VIRTIO_BALLOON_PAGES_PER_PAGE;
  119. totalram_pages--;
  120. list_add(&page->lru, &vb->pages);
  121. }
  122. /* Didn't get any? Oh well. */
  123. if (vb->num_pfns == 0)
  124. return;
  125. tell_host(vb, vb->inflate_vq);
  126. }
  127. static void release_pages_by_pfn(const u32 pfns[], unsigned int num)
  128. {
  129. unsigned int i;
  130. /* Find pfns pointing at start of each page, get pages and free them. */
  131. for (i = 0; i < num; i += VIRTIO_BALLOON_PAGES_PER_PAGE) {
  132. __free_page(balloon_pfn_to_page(pfns[i]));
  133. totalram_pages++;
  134. }
  135. }
  136. static void leak_balloon(struct virtio_balloon *vb, size_t num)
  137. {
  138. struct page *page;
  139. /* We can only do one array worth at a time. */
  140. num = min(num, ARRAY_SIZE(vb->pfns));
  141. for (vb->num_pfns = 0; vb->num_pfns < num;
  142. vb->num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE) {
  143. page = list_first_entry(&vb->pages, struct page, lru);
  144. list_del(&page->lru);
  145. set_page_pfns(vb->pfns + vb->num_pfns, page);
  146. vb->num_pages -= VIRTIO_BALLOON_PAGES_PER_PAGE;
  147. }
  148. /*
  149. * Note that if
  150. * virtio_has_feature(vdev, VIRTIO_BALLOON_F_MUST_TELL_HOST);
  151. * is true, we *have* to do it in this order
  152. */
  153. tell_host(vb, vb->deflate_vq);
  154. release_pages_by_pfn(vb->pfns, vb->num_pfns);
  155. }
  156. static inline void update_stat(struct virtio_balloon *vb, int idx,
  157. u16 tag, u64 val)
  158. {
  159. BUG_ON(idx >= VIRTIO_BALLOON_S_NR);
  160. vb->stats[idx].tag = tag;
  161. vb->stats[idx].val = val;
  162. }
  163. #define pages_to_bytes(x) ((u64)(x) << PAGE_SHIFT)
  164. static void update_balloon_stats(struct virtio_balloon *vb)
  165. {
  166. unsigned long events[NR_VM_EVENT_ITEMS];
  167. struct sysinfo i;
  168. int idx = 0;
  169. all_vm_events(events);
  170. si_meminfo(&i);
  171. update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_IN,
  172. pages_to_bytes(events[PSWPIN]));
  173. update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_OUT,
  174. pages_to_bytes(events[PSWPOUT]));
  175. update_stat(vb, idx++, VIRTIO_BALLOON_S_MAJFLT, events[PGMAJFAULT]);
  176. update_stat(vb, idx++, VIRTIO_BALLOON_S_MINFLT, events[PGFAULT]);
  177. update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMFREE,
  178. pages_to_bytes(i.freeram));
  179. update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMTOT,
  180. pages_to_bytes(i.totalram));
  181. }
  182. /*
  183. * While most virtqueues communicate guest-initiated requests to the hypervisor,
  184. * the stats queue operates in reverse. The driver initializes the virtqueue
  185. * with a single buffer. From that point forward, all conversations consist of
  186. * a hypervisor request (a call to this function) which directs us to refill
  187. * the virtqueue with a fresh stats buffer. Since stats collection can sleep,
  188. * we notify our kthread which does the actual work via stats_handle_request().
  189. */
  190. static void stats_request(struct virtqueue *vq)
  191. {
  192. struct virtio_balloon *vb = vq->vdev->priv;
  193. vb->need_stats_update = 1;
  194. wake_up(&vb->config_change);
  195. }
  196. static void stats_handle_request(struct virtio_balloon *vb)
  197. {
  198. struct virtqueue *vq;
  199. struct scatterlist sg;
  200. unsigned int len;
  201. vb->need_stats_update = 0;
  202. update_balloon_stats(vb);
  203. vq = vb->stats_vq;
  204. if (!virtqueue_get_buf(vq, &len))
  205. return;
  206. sg_init_one(&sg, vb->stats, sizeof(vb->stats));
  207. if (virtqueue_add_buf(vq, &sg, 1, 0, vb, GFP_KERNEL) < 0)
  208. BUG();
  209. virtqueue_kick(vq);
  210. }
  211. static void virtballoon_changed(struct virtio_device *vdev)
  212. {
  213. struct virtio_balloon *vb = vdev->priv;
  214. wake_up(&vb->config_change);
  215. }
  216. static inline s64 towards_target(struct virtio_balloon *vb)
  217. {
  218. __le32 v;
  219. s64 target;
  220. vb->vdev->config->get(vb->vdev,
  221. offsetof(struct virtio_balloon_config, num_pages),
  222. &v, sizeof(v));
  223. target = le32_to_cpu(v);
  224. return target - vb->num_pages;
  225. }
  226. static void update_balloon_size(struct virtio_balloon *vb)
  227. {
  228. __le32 actual = cpu_to_le32(vb->num_pages);
  229. vb->vdev->config->set(vb->vdev,
  230. offsetof(struct virtio_balloon_config, actual),
  231. &actual, sizeof(actual));
  232. }
  233. static int balloon(void *_vballoon)
  234. {
  235. struct virtio_balloon *vb = _vballoon;
  236. set_freezable();
  237. while (!kthread_should_stop()) {
  238. s64 diff;
  239. try_to_freeze();
  240. wait_event_interruptible(vb->config_change,
  241. (diff = towards_target(vb)) != 0
  242. || vb->need_stats_update
  243. || kthread_should_stop()
  244. || freezing(current));
  245. if (vb->need_stats_update)
  246. stats_handle_request(vb);
  247. if (diff > 0)
  248. fill_balloon(vb, diff);
  249. else if (diff < 0)
  250. leak_balloon(vb, -diff);
  251. update_balloon_size(vb);
  252. }
  253. return 0;
  254. }
  255. static int init_vqs(struct virtio_balloon *vb)
  256. {
  257. struct virtqueue *vqs[3];
  258. vq_callback_t *callbacks[] = { balloon_ack, balloon_ack, stats_request };
  259. const char *names[] = { "inflate", "deflate", "stats" };
  260. int err, nvqs;
  261. /*
  262. * We expect two virtqueues: inflate and deflate, and
  263. * optionally stat.
  264. */
  265. nvqs = virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ) ? 3 : 2;
  266. err = vb->vdev->config->find_vqs(vb->vdev, nvqs, vqs, callbacks, names);
  267. if (err)
  268. return err;
  269. vb->inflate_vq = vqs[0];
  270. vb->deflate_vq = vqs[1];
  271. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) {
  272. struct scatterlist sg;
  273. vb->stats_vq = vqs[2];
  274. /*
  275. * Prime this virtqueue with one buffer so the hypervisor can
  276. * use it to signal us later.
  277. */
  278. sg_init_one(&sg, vb->stats, sizeof vb->stats);
  279. if (virtqueue_add_buf(vb->stats_vq, &sg, 1, 0, vb, GFP_KERNEL)
  280. < 0)
  281. BUG();
  282. virtqueue_kick(vb->stats_vq);
  283. }
  284. return 0;
  285. }
  286. static int virtballoon_probe(struct virtio_device *vdev)
  287. {
  288. struct virtio_balloon *vb;
  289. int err;
  290. vdev->priv = vb = kmalloc(sizeof(*vb), GFP_KERNEL);
  291. if (!vb) {
  292. err = -ENOMEM;
  293. goto out;
  294. }
  295. INIT_LIST_HEAD(&vb->pages);
  296. vb->num_pages = 0;
  297. init_waitqueue_head(&vb->config_change);
  298. init_waitqueue_head(&vb->acked);
  299. vb->vdev = vdev;
  300. vb->need_stats_update = 0;
  301. err = init_vqs(vb);
  302. if (err)
  303. goto out_free_vb;
  304. vb->thread = kthread_run(balloon, vb, "vballoon");
  305. if (IS_ERR(vb->thread)) {
  306. err = PTR_ERR(vb->thread);
  307. goto out_del_vqs;
  308. }
  309. return 0;
  310. out_del_vqs:
  311. vdev->config->del_vqs(vdev);
  312. out_free_vb:
  313. kfree(vb);
  314. out:
  315. return err;
  316. }
  317. static void remove_common(struct virtio_balloon *vb)
  318. {
  319. /* There might be pages left in the balloon: free them. */
  320. while (vb->num_pages)
  321. leak_balloon(vb, vb->num_pages);
  322. update_balloon_size(vb);
  323. /* Now we reset the device so we can clean up the queues. */
  324. vb->vdev->config->reset(vb->vdev);
  325. vb->vdev->config->del_vqs(vb->vdev);
  326. }
  327. static void __devexit virtballoon_remove(struct virtio_device *vdev)
  328. {
  329. struct virtio_balloon *vb = vdev->priv;
  330. kthread_stop(vb->thread);
  331. remove_common(vb);
  332. kfree(vb);
  333. }
  334. #ifdef CONFIG_PM
  335. static int virtballoon_freeze(struct virtio_device *vdev)
  336. {
  337. struct virtio_balloon *vb = vdev->priv;
  338. /*
  339. * The kthread is already frozen by the PM core before this
  340. * function is called.
  341. */
  342. remove_common(vb);
  343. return 0;
  344. }
  345. static int virtballoon_restore(struct virtio_device *vdev)
  346. {
  347. struct virtio_balloon *vb = vdev->priv;
  348. int ret;
  349. ret = init_vqs(vdev->priv);
  350. if (ret)
  351. return ret;
  352. fill_balloon(vb, towards_target(vb));
  353. update_balloon_size(vb);
  354. return 0;
  355. }
  356. #endif
  357. static unsigned int features[] = {
  358. VIRTIO_BALLOON_F_MUST_TELL_HOST,
  359. VIRTIO_BALLOON_F_STATS_VQ,
  360. };
  361. static struct virtio_driver virtio_balloon_driver = {
  362. .feature_table = features,
  363. .feature_table_size = ARRAY_SIZE(features),
  364. .driver.name = KBUILD_MODNAME,
  365. .driver.owner = THIS_MODULE,
  366. .id_table = id_table,
  367. .probe = virtballoon_probe,
  368. .remove = __devexit_p(virtballoon_remove),
  369. .config_changed = virtballoon_changed,
  370. #ifdef CONFIG_PM
  371. .freeze = virtballoon_freeze,
  372. .restore = virtballoon_restore,
  373. #endif
  374. };
  375. static int __init init(void)
  376. {
  377. return register_virtio_driver(&virtio_balloon_driver);
  378. }
  379. static void __exit fini(void)
  380. {
  381. unregister_virtio_driver(&virtio_balloon_driver);
  382. }
  383. module_init(init);
  384. module_exit(fini);
  385. MODULE_DEVICE_TABLE(virtio, id_table);
  386. MODULE_DESCRIPTION("Virtio balloon driver");
  387. MODULE_LICENSE("GPL");