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
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>
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? */
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
;
55 static struct virtio_device_id id_table
[] = {
56 { VIRTIO_ID_BALLOON
, VIRTIO_DEV_ANY_ID
},
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
;
74 vb
= vq
->vq_ops
->get_buf(vq
, &len
);
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)
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
);
104 if (printk_ratelimit())
105 dev_printk(KERN_INFO
, &vb
->vdev
->dev
,
106 "Out of puff! Can't get %zu pages\n",
108 /* Sleep for at least 1/5 of a second before retry. */
112 vb
->pfns
[vb
->num_pfns
] = page_to_balloon_pfn(page
);
115 list_add(&page
->lru
, &vb
->pages
);
118 /* Didn't get any? Oh well. */
119 if (vb
->num_pfns
== 0)
122 tell_host(vb
, vb
->inflate_vq
);
125 static void release_pages_by_pfn(const u32 pfns
[], unsigned int num
)
129 for (i
= 0; i
< num
; i
++) {
130 __free_page(pfn_to_page(pfns
[i
]));
135 static void leak_balloon(struct virtio_balloon
*vb
, size_t num
)
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
);
149 if (vb
->tell_host_first
) {
150 tell_host(vb
, vb
->deflate_vq
);
151 release_pages_by_pfn(vb
->pfns
, vb
->num_pfns
);
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
)
168 vb
->vdev
->config
->get(vb
->vdev
,
169 offsetof(struct virtio_balloon_config
, num_pages
),
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
;
188 while (!kthread_should_stop()) {
192 wait_event_interruptible(vb
->config_change
,
193 (diff
= towards_target(vb
)) != 0
194 || kthread_should_stop()
195 || freezing(current
));
197 fill_balloon(vb
, diff
);
199 leak_balloon(vb
, -diff
);
200 update_balloon_size(vb
);
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" };
213 vdev
->priv
= vb
= kmalloc(sizeof(*vb
), GFP_KERNEL
);
219 INIT_LIST_HEAD(&vb
->pages
);
221 init_waitqueue_head(&vb
->config_change
);
224 /* We expect two virtqueues. */
225 err
= vdev
->config
->find_vqs(vdev
, 2, vqs
, callbacks
, names
);
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
);
239 = virtio_has_feature(vdev
, VIRTIO_BALLOON_F_MUST_TELL_HOST
);
244 vdev
->config
->del_vqs(vdev
);
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
);
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
);
293 MODULE_DEVICE_TABLE(virtio
, id_table
);
294 MODULE_DESCRIPTION("Virtio balloon driver");
295 MODULE_LICENSE("GPL");