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