blob: 200c22f55130ba890d37a73a0e565460e8240315 [file] [log] [blame]
Rusty Russell6b35e402008-02-04 23:50:12 -05001/* 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>
Fernando Luis Vazquez Cao3ca4f5c2009-07-31 15:25:56 +090022#include <linux/virtio_ids.h>
Rusty Russell6b35e402008-02-04 23:50:12 -050023#include <linux/virtio_balloon.h>
24#include <linux/swap.h>
25#include <linux/kthread.h>
26#include <linux/freezer.h>
Johann Felix Soden6659a0f2008-02-06 01:40:22 -080027#include <linux/delay.h>
Rusty Russell6b35e402008-02-04 23:50:12 -050028
29struct virtio_balloon
30{
31 struct virtio_device *vdev;
32 struct virtqueue *inflate_vq, *deflate_vq;
33
34 /* Where the ballooning thread waits for config to change. */
35 wait_queue_head_t config_change;
36
37 /* The thread servicing the balloon. */
38 struct task_struct *thread;
39
40 /* Waiting for host to ack the pages we released. */
41 struct completion acked;
42
43 /* Do we have to tell Host *before* we reuse pages? */
44 bool tell_host_first;
45
46 /* The pages we've told the Host we're not using. */
47 unsigned int num_pages;
48 struct list_head pages;
49
50 /* The array of pfns we tell the Host about. */
51 unsigned int num_pfns;
52 u32 pfns[256];
53};
54
55static struct virtio_device_id id_table[] = {
56 { VIRTIO_ID_BALLOON, VIRTIO_DEV_ANY_ID },
57 { 0 },
58};
59
Hollis Blanchard1b4aa2f2008-11-13 15:48:33 -060060static u32 page_to_balloon_pfn(struct page *page)
61{
62 unsigned long pfn = page_to_pfn(page);
63
64 BUILD_BUG_ON(PAGE_SHIFT < VIRTIO_BALLOON_PFN_SHIFT);
65 /* Convert pfn from Linux page size to balloon page size. */
66 return pfn >> (PAGE_SHIFT - VIRTIO_BALLOON_PFN_SHIFT);
67}
68
Rusty Russell6b35e402008-02-04 23:50:12 -050069static void balloon_ack(struct virtqueue *vq)
70{
71 struct virtio_balloon *vb;
72 unsigned int len;
73
74 vb = vq->vq_ops->get_buf(vq, &len);
75 if (vb)
76 complete(&vb->acked);
77}
78
79static void tell_host(struct virtio_balloon *vb, struct virtqueue *vq)
80{
81 struct scatterlist sg;
82
83 sg_init_one(&sg, vb->pfns, sizeof(vb->pfns[0]) * vb->num_pfns);
84
85 init_completion(&vb->acked);
86
87 /* We should always be able to add one buffer to an empty queue. */
Rusty Russell3c1b27d2009-09-23 22:26:31 -060088 if (vq->vq_ops->add_buf(vq, &sg, 1, 0, vb) < 0)
Rusty Russell6b35e402008-02-04 23:50:12 -050089 BUG();
90 vq->vq_ops->kick(vq);
91
92 /* When host has read buffer, this completes via balloon_ack */
93 wait_for_completion(&vb->acked);
94}
95
96static void fill_balloon(struct virtio_balloon *vb, size_t num)
97{
98 /* We can only do one array worth at a time. */
99 num = min(num, ARRAY_SIZE(vb->pfns));
100
101 for (vb->num_pfns = 0; vb->num_pfns < num; vb->num_pfns++) {
102 struct page *page = alloc_page(GFP_HIGHUSER | __GFP_NORETRY);
103 if (!page) {
104 if (printk_ratelimit())
105 dev_printk(KERN_INFO, &vb->vdev->dev,
106 "Out of puff! Can't get %zu pages\n",
107 num);
108 /* Sleep for at least 1/5 of a second before retry. */
109 msleep(200);
110 break;
111 }
Hollis Blanchard1b4aa2f2008-11-13 15:48:33 -0600112 vb->pfns[vb->num_pfns] = page_to_balloon_pfn(page);
Rusty Russell6b35e402008-02-04 23:50:12 -0500113 totalram_pages--;
114 vb->num_pages++;
115 list_add(&page->lru, &vb->pages);
116 }
117
118 /* Didn't get any? Oh well. */
119 if (vb->num_pfns == 0)
120 return;
121
122 tell_host(vb, vb->inflate_vq);
123}
124
125static void release_pages_by_pfn(const u32 pfns[], unsigned int num)
126{
127 unsigned int i;
128
129 for (i = 0; i < num; i++) {
130 __free_page(pfn_to_page(pfns[i]));
131 totalram_pages++;
132 }
133}
134
135static void leak_balloon(struct virtio_balloon *vb, size_t num)
136{
137 struct page *page;
138
139 /* We can only do one array worth at a time. */
140 num = min(num, ARRAY_SIZE(vb->pfns));
141
142 for (vb->num_pfns = 0; vb->num_pfns < num; vb->num_pfns++) {
143 page = list_first_entry(&vb->pages, struct page, lru);
144 list_del(&page->lru);
Hollis Blanchard1b4aa2f2008-11-13 15:48:33 -0600145 vb->pfns[vb->num_pfns] = page_to_balloon_pfn(page);
Rusty Russell6b35e402008-02-04 23:50:12 -0500146 vb->num_pages--;
147 }
148
149 if (vb->tell_host_first) {
150 tell_host(vb, vb->deflate_vq);
151 release_pages_by_pfn(vb->pfns, vb->num_pfns);
152 } else {
153 release_pages_by_pfn(vb->pfns, vb->num_pfns);
154 tell_host(vb, vb->deflate_vq);
155 }
156}
157
158static void virtballoon_changed(struct virtio_device *vdev)
159{
160 struct virtio_balloon *vb = vdev->priv;
161
162 wake_up(&vb->config_change);
163}
164
Rusty Russellbdc16812008-03-17 22:58:15 -0500165static inline s64 towards_target(struct virtio_balloon *vb)
Rusty Russell6b35e402008-02-04 23:50:12 -0500166{
167 u32 v;
Rusty Russell72e61eb2008-05-02 21:50:49 -0500168 vb->vdev->config->get(vb->vdev,
169 offsetof(struct virtio_balloon_config, num_pages),
170 &v, sizeof(v));
Anthony Liguori532a6082008-08-18 17:15:31 -0500171 return (s64)v - vb->num_pages;
Rusty Russell6b35e402008-02-04 23:50:12 -0500172}
173
174static void update_balloon_size(struct virtio_balloon *vb)
175{
176 __le32 actual = cpu_to_le32(vb->num_pages);
177
178 vb->vdev->config->set(vb->vdev,
179 offsetof(struct virtio_balloon_config, actual),
180 &actual, sizeof(actual));
181}
182
183static int balloon(void *_vballoon)
184{
185 struct virtio_balloon *vb = _vballoon;
186
187 set_freezable();
188 while (!kthread_should_stop()) {
Rusty Russellbdc16812008-03-17 22:58:15 -0500189 s64 diff;
Rusty Russell6b35e402008-02-04 23:50:12 -0500190
191 try_to_freeze();
192 wait_event_interruptible(vb->config_change,
193 (diff = towards_target(vb)) != 0
Marcelo Tosatti84a139a2009-04-16 21:14:04 -0300194 || kthread_should_stop()
195 || freezing(current));
Rusty Russell6b35e402008-02-04 23:50:12 -0500196 if (diff > 0)
197 fill_balloon(vb, diff);
198 else if (diff < 0)
199 leak_balloon(vb, -diff);
200 update_balloon_size(vb);
201 }
202 return 0;
203}
204
205static int virtballoon_probe(struct virtio_device *vdev)
206{
207 struct virtio_balloon *vb;
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600208 struct virtqueue *vqs[2];
209 vq_callback_t *callbacks[] = { balloon_ack, balloon_ack };
210 const char *names[] = { "inflate", "deflate" };
Rusty Russell6b35e402008-02-04 23:50:12 -0500211 int err;
212
213 vdev->priv = vb = kmalloc(sizeof(*vb), GFP_KERNEL);
214 if (!vb) {
215 err = -ENOMEM;
216 goto out;
217 }
218
219 INIT_LIST_HEAD(&vb->pages);
220 vb->num_pages = 0;
221 init_waitqueue_head(&vb->config_change);
222 vb->vdev = vdev;
223
224 /* We expect two virtqueues. */
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600225 err = vdev->config->find_vqs(vdev, 2, vqs, callbacks, names);
226 if (err)
Rusty Russell6b35e402008-02-04 23:50:12 -0500227 goto out_free_vb;
Rusty Russell6b35e402008-02-04 23:50:12 -0500228
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600229 vb->inflate_vq = vqs[0];
230 vb->deflate_vq = vqs[1];
Rusty Russell6b35e402008-02-04 23:50:12 -0500231
232 vb->thread = kthread_run(balloon, vb, "vballoon");
233 if (IS_ERR(vb->thread)) {
234 err = PTR_ERR(vb->thread);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600235 goto out_del_vqs;
Rusty Russell6b35e402008-02-04 23:50:12 -0500236 }
237
238 vb->tell_host_first
Rusty Russellc45a6812008-05-02 21:50:50 -0500239 = virtio_has_feature(vdev, VIRTIO_BALLOON_F_MUST_TELL_HOST);
Rusty Russell6b35e402008-02-04 23:50:12 -0500240
241 return 0;
242
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600243out_del_vqs:
244 vdev->config->del_vqs(vdev);
Rusty Russell6b35e402008-02-04 23:50:12 -0500245out_free_vb:
246 kfree(vb);
247out:
248 return err;
249}
250
251static void virtballoon_remove(struct virtio_device *vdev)
252{
253 struct virtio_balloon *vb = vdev->priv;
254
255 kthread_stop(vb->thread);
256
257 /* There might be pages left in the balloon: free them. */
258 while (vb->num_pages)
259 leak_balloon(vb, vb->num_pages);
260
261 /* Now we reset the device so we can clean up the queues. */
262 vdev->config->reset(vdev);
263
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600264 vdev->config->del_vqs(vdev);
Rusty Russell6b35e402008-02-04 23:50:12 -0500265 kfree(vb);
266}
267
Rusty Russellc45a6812008-05-02 21:50:50 -0500268static unsigned int features[] = { VIRTIO_BALLOON_F_MUST_TELL_HOST };
269
Rusty Russell6b35e402008-02-04 23:50:12 -0500270static struct virtio_driver virtio_balloon = {
Rusty Russellc45a6812008-05-02 21:50:50 -0500271 .feature_table = features,
272 .feature_table_size = ARRAY_SIZE(features),
Rusty Russell6b35e402008-02-04 23:50:12 -0500273 .driver.name = KBUILD_MODNAME,
274 .driver.owner = THIS_MODULE,
275 .id_table = id_table,
276 .probe = virtballoon_probe,
277 .remove = __devexit_p(virtballoon_remove),
278 .config_changed = virtballoon_changed,
279};
280
281static int __init init(void)
282{
283 return register_virtio_driver(&virtio_balloon);
284}
285
286static void __exit fini(void)
287{
288 unregister_virtio_driver(&virtio_balloon);
289}
290module_init(init);
291module_exit(fini);
292
293MODULE_DEVICE_TABLE(virtio, id_table);
294MODULE_DESCRIPTION("Virtio balloon driver");
295MODULE_LICENSE("GPL");