virtio_balloon.c 12 KB

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