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_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>
28 
29 struct 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 
55 static struct virtio_device_id id_table[] = {
56 	{ VIRTIO_ID_BALLOON, VIRTIO_DEV_ANY_ID },
57 	{ 0 },
58 };
59 
60 static 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 
69 static 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 
79 static 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. */
88 	if (vq->vq_ops->add_buf(vq, &sg, 1, 0, vb) < 0)
89 		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 
96 static 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 		}
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);
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 
125 static 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 
135 static 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);
145 		vb->pfns[vb->num_pfns] = page_to_balloon_pfn(page);
146 		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 
158 static void virtballoon_changed(struct virtio_device *vdev)
159 {
160 	struct virtio_balloon *vb = vdev->priv;
161 
162 	wake_up(&vb->config_change);
163 }
164 
165 static inline s64 towards_target(struct virtio_balloon *vb)
166 {
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;
172 }
173 
174 static 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 
183 static int balloon(void *_vballoon)
184 {
185 	struct virtio_balloon *vb = _vballoon;
186 
187 	set_freezable();
188 	while (!kthread_should_stop()) {
189 		s64 diff;
190 
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);
201 	}
202 	return 0;
203 }
204 
205 static int virtballoon_probe(struct virtio_device *vdev)
206 {
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;
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. */
225 	err = vdev->config->find_vqs(vdev, 2, vqs, callbacks, names);
226 	if (err)
227 		goto out_free_vb;
228 
229 	vb->inflate_vq = vqs[0];
230 	vb->deflate_vq = vqs[1];
231 
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;
236 	}
237 
238 	vb->tell_host_first
239 		= virtio_has_feature(vdev, VIRTIO_BALLOON_F_MUST_TELL_HOST);
240 
241 	return 0;
242 
243 out_del_vqs:
244 	vdev->config->del_vqs(vdev);
245 out_free_vb:
246 	kfree(vb);
247 out:
248 	return err;
249 }
250 
251 static 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 
264 	vdev->config->del_vqs(vdev);
265 	kfree(vb);
266 }
267 
268 static unsigned int features[] = { VIRTIO_BALLOON_F_MUST_TELL_HOST };
269 
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,
279 };
280 
281 static int __init init(void)
282 {
283 	return register_virtio_driver(&virtio_balloon);
284 }
285 
286 static void __exit fini(void)
287 {
288 	unregister_virtio_driver(&virtio_balloon);
289 }
290 module_init(init);
291 module_exit(fini);
292 
293 MODULE_DEVICE_TABLE(virtio, id_table);
294 MODULE_DESCRIPTION("Virtio balloon driver");
295 MODULE_LICENSE("GPL");
296