[ARM] pxa: Gumstix Verdex PCMCIA support
[linux-2.6/verdex.git] / drivers / virtio / virtio_balloon.c
blob200c22f55130ba890d37a73a0e565460e8240315
1 /* Virtio balloon implementation, inspired by Dor Loar and Marcelo
2 * Tosatti's implementations.
4 * Copyright 2008 Rusty Russell IBM Corporation
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.
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.
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
20 //#define DEBUG
21 #include <linux/virtio.h>
22 #include <linux/virtio_ids.h>
23 #include <linux/virtio_balloon.h>
24 #include <linux/swap.h>
25 #include <linux/kthread.h>
26 #include <linux/freezer.h>
27 #include <linux/delay.h>
29 struct virtio_balloon
31 struct virtio_device *vdev;
32 struct virtqueue *inflate_vq, *deflate_vq;
34 /* Where the ballooning thread waits for config to change. */
35 wait_queue_head_t config_change;
37 /* The thread servicing the balloon. */
38 struct task_struct *thread;
40 /* Waiting for host to ack the pages we released. */
41 struct completion acked;
43 /* Do we have to tell Host *before* we reuse pages? */
44 bool tell_host_first;
46 /* The pages we've told the Host we're not using. */
47 unsigned int num_pages;
48 struct list_head pages;
50 /* The array of pfns we tell the Host about. */
51 unsigned int num_pfns;
52 u32 pfns[256];
55 static struct virtio_device_id id_table[] = {
56 { VIRTIO_ID_BALLOON, VIRTIO_DEV_ANY_ID },
57 { 0 },
60 static u32 page_to_balloon_pfn(struct page *page)
62 unsigned long pfn = page_to_pfn(page);
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);
69 static void balloon_ack(struct virtqueue *vq)
71 struct virtio_balloon *vb;
72 unsigned int len;
74 vb = vq->vq_ops->get_buf(vq, &len);
75 if (vb)
76 complete(&vb->acked);
79 static void tell_host(struct virtio_balloon *vb, struct virtqueue *vq)
81 struct scatterlist sg;
83 sg_init_one(&sg, vb->pfns, sizeof(vb->pfns[0]) * vb->num_pfns);
85 init_completion(&vb->acked);
87 /* We should always be able to add one buffer to an empty queue. */
88 if (vq->vq_ops->add_buf(vq, &sg, 1, 0, vb) < 0)
89 BUG();
90 vq->vq_ops->kick(vq);
92 /* When host has read buffer, this completes via balloon_ack */
93 wait_for_completion(&vb->acked);
96 static void fill_balloon(struct virtio_balloon *vb, size_t num)
98 /* We can only do one array worth at a time. */
99 num = min(num, ARRAY_SIZE(vb->pfns));
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;
112 vb->pfns[vb->num_pfns] = page_to_balloon_pfn(page);
113 totalram_pages--;
114 vb->num_pages++;
115 list_add(&page->lru, &vb->pages);
118 /* Didn't get any? Oh well. */
119 if (vb->num_pfns == 0)
120 return;
122 tell_host(vb, vb->inflate_vq);
125 static void release_pages_by_pfn(const u32 pfns[], unsigned int num)
127 unsigned int i;
129 for (i = 0; i < num; i++) {
130 __free_page(pfn_to_page(pfns[i]));
131 totalram_pages++;
135 static void leak_balloon(struct virtio_balloon *vb, size_t num)
137 struct page *page;
139 /* We can only do one array worth at a time. */
140 num = min(num, ARRAY_SIZE(vb->pfns));
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);
145 vb->pfns[vb->num_pfns] = page_to_balloon_pfn(page);
146 vb->num_pages--;
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);
158 static void virtballoon_changed(struct virtio_device *vdev)
160 struct virtio_balloon *vb = vdev->priv;
162 wake_up(&vb->config_change);
165 static inline s64 towards_target(struct virtio_balloon *vb)
167 u32 v;
168 vb->vdev->config->get(vb->vdev,
169 offsetof(struct virtio_balloon_config, num_pages),
170 &v, sizeof(v));
171 return (s64)v - vb->num_pages;
174 static void update_balloon_size(struct virtio_balloon *vb)
176 __le32 actual = cpu_to_le32(vb->num_pages);
178 vb->vdev->config->set(vb->vdev,
179 offsetof(struct virtio_balloon_config, actual),
180 &actual, sizeof(actual));
183 static int balloon(void *_vballoon)
185 struct virtio_balloon *vb = _vballoon;
187 set_freezable();
188 while (!kthread_should_stop()) {
189 s64 diff;
191 try_to_freeze();
192 wait_event_interruptible(vb->config_change,
193 (diff = towards_target(vb)) != 0
194 || kthread_should_stop()
195 || freezing(current));
196 if (diff > 0)
197 fill_balloon(vb, diff);
198 else if (diff < 0)
199 leak_balloon(vb, -diff);
200 update_balloon_size(vb);
202 return 0;
205 static int virtballoon_probe(struct virtio_device *vdev)
207 struct virtio_balloon *vb;
208 struct virtqueue *vqs[2];
209 vq_callback_t *callbacks[] = { balloon_ack, balloon_ack };
210 const char *names[] = { "inflate", "deflate" };
211 int err;
213 vdev->priv = vb = kmalloc(sizeof(*vb), GFP_KERNEL);
214 if (!vb) {
215 err = -ENOMEM;
216 goto out;
219 INIT_LIST_HEAD(&vb->pages);
220 vb->num_pages = 0;
221 init_waitqueue_head(&vb->config_change);
222 vb->vdev = vdev;
224 /* We expect two virtqueues. */
225 err = vdev->config->find_vqs(vdev, 2, vqs, callbacks, names);
226 if (err)
227 goto out_free_vb;
229 vb->inflate_vq = vqs[0];
230 vb->deflate_vq = vqs[1];
232 vb->thread = kthread_run(balloon, vb, "vballoon");
233 if (IS_ERR(vb->thread)) {
234 err = PTR_ERR(vb->thread);
235 goto out_del_vqs;
238 vb->tell_host_first
239 = virtio_has_feature(vdev, VIRTIO_BALLOON_F_MUST_TELL_HOST);
241 return 0;
243 out_del_vqs:
244 vdev->config->del_vqs(vdev);
245 out_free_vb:
246 kfree(vb);
247 out:
248 return err;
251 static void virtballoon_remove(struct virtio_device *vdev)
253 struct virtio_balloon *vb = vdev->priv;
255 kthread_stop(vb->thread);
257 /* There might be pages left in the balloon: free them. */
258 while (vb->num_pages)
259 leak_balloon(vb, vb->num_pages);
261 /* Now we reset the device so we can clean up the queues. */
262 vdev->config->reset(vdev);
264 vdev->config->del_vqs(vdev);
265 kfree(vb);
268 static unsigned int features[] = { VIRTIO_BALLOON_F_MUST_TELL_HOST };
270 static struct virtio_driver virtio_balloon = {
271 .feature_table = features,
272 .feature_table_size = ARRAY_SIZE(features),
273 .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,
281 static int __init init(void)
283 return register_virtio_driver(&virtio_balloon);
286 static void __exit fini(void)
288 unregister_virtio_driver(&virtio_balloon);
290 module_init(init);
291 module_exit(fini);
293 MODULE_DEVICE_TABLE(virtio, id_table);
294 MODULE_DESCRIPTION("Virtio balloon driver");
295 MODULE_LICENSE("GPL");