Commit | Line | Data |
---|---|---|
3a4d5c94 MT |
1 | /* Copyright (C) 2009 Red Hat, Inc. |
2 | * Copyright (C) 2006 Rusty Russell IBM Corporation | |
3 | * | |
4 | * Author: Michael S. Tsirkin <mst@redhat.com> | |
5 | * | |
6 | * Inspiration, some code, and most witty comments come from | |
61516587 | 7 | * Documentation/virtual/lguest/lguest.c, by Rusty Russell |
3a4d5c94 MT |
8 | * |
9 | * This work is licensed under the terms of the GNU GPL, version 2. | |
10 | * | |
11 | * Generic code for virtio server in host kernel. | |
12 | */ | |
13 | ||
14 | #include <linux/eventfd.h> | |
15 | #include <linux/vhost.h> | |
35596b27 | 16 | #include <linux/uio.h> |
3a4d5c94 | 17 | #include <linux/mm.h> |
64e1c807 | 18 | #include <linux/mmu_context.h> |
3a4d5c94 MT |
19 | #include <linux/miscdevice.h> |
20 | #include <linux/mutex.h> | |
3a4d5c94 MT |
21 | #include <linux/poll.h> |
22 | #include <linux/file.h> | |
23 | #include <linux/highmem.h> | |
5a0e3ad6 | 24 | #include <linux/slab.h> |
c23f3445 | 25 | #include <linux/kthread.h> |
9e3d1957 | 26 | #include <linux/cgroup.h> |
6ac1afbf | 27 | #include <linux/module.h> |
3a4d5c94 | 28 | |
3a4d5c94 MT |
29 | #include "vhost.h" |
30 | ||
31 | enum { | |
32 | VHOST_MEMORY_MAX_NREGIONS = 64, | |
33 | VHOST_MEMORY_F_LOG = 0x1, | |
34 | }; | |
35 | ||
3b1bbe89 MT |
36 | #define vhost_used_event(vq) ((__virtio16 __user *)&vq->avail->ring[vq->num]) |
37 | #define vhost_avail_event(vq) ((__virtio16 __user *)&vq->used->ring[vq->num]) | |
8ea8cf89 | 38 | |
2751c988 GK |
39 | #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY |
40 | static void vhost_vq_reset_user_be(struct vhost_virtqueue *vq) | |
41 | { | |
42 | vq->user_be = !virtio_legacy_is_little_endian(); | |
43 | } | |
44 | ||
45 | static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp) | |
46 | { | |
47 | struct vhost_vring_state s; | |
48 | ||
49 | if (vq->private_data) | |
50 | return -EBUSY; | |
51 | ||
52 | if (copy_from_user(&s, argp, sizeof(s))) | |
53 | return -EFAULT; | |
54 | ||
55 | if (s.num != VHOST_VRING_LITTLE_ENDIAN && | |
56 | s.num != VHOST_VRING_BIG_ENDIAN) | |
57 | return -EINVAL; | |
58 | ||
59 | vq->user_be = s.num; | |
60 | ||
61 | return 0; | |
62 | } | |
63 | ||
64 | static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx, | |
65 | int __user *argp) | |
66 | { | |
67 | struct vhost_vring_state s = { | |
68 | .index = idx, | |
69 | .num = vq->user_be | |
70 | }; | |
71 | ||
72 | if (copy_to_user(argp, &s, sizeof(s))) | |
73 | return -EFAULT; | |
74 | ||
75 | return 0; | |
76 | } | |
77 | ||
78 | static void vhost_init_is_le(struct vhost_virtqueue *vq) | |
79 | { | |
80 | /* Note for legacy virtio: user_be is initialized at reset time | |
81 | * according to the host endianness. If userspace does not set an | |
82 | * explicit endianness, the default behavior is native endian, as | |
83 | * expected by legacy virtio. | |
84 | */ | |
85 | vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1) || !vq->user_be; | |
86 | } | |
87 | #else | |
88 | static void vhost_vq_reset_user_be(struct vhost_virtqueue *vq) | |
89 | { | |
90 | } | |
91 | ||
92 | static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp) | |
93 | { | |
94 | return -ENOIOCTLCMD; | |
95 | } | |
96 | ||
97 | static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx, | |
98 | int __user *argp) | |
99 | { | |
100 | return -ENOIOCTLCMD; | |
101 | } | |
102 | ||
103 | static void vhost_init_is_le(struct vhost_virtqueue *vq) | |
104 | { | |
105 | if (vhost_has_feature(vq, VIRTIO_F_VERSION_1)) | |
106 | vq->is_le = true; | |
107 | } | |
108 | #endif /* CONFIG_VHOST_CROSS_ENDIAN_LEGACY */ | |
109 | ||
3a4d5c94 MT |
110 | static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh, |
111 | poll_table *pt) | |
112 | { | |
113 | struct vhost_poll *poll; | |
3a4d5c94 | 114 | |
d47effe1 | 115 | poll = container_of(pt, struct vhost_poll, table); |
3a4d5c94 MT |
116 | poll->wqh = wqh; |
117 | add_wait_queue(wqh, &poll->wait); | |
118 | } | |
119 | ||
120 | static int vhost_poll_wakeup(wait_queue_t *wait, unsigned mode, int sync, | |
121 | void *key) | |
122 | { | |
c23f3445 TH |
123 | struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait); |
124 | ||
3a4d5c94 MT |
125 | if (!((unsigned long)key & poll->mask)) |
126 | return 0; | |
127 | ||
c23f3445 | 128 | vhost_poll_queue(poll); |
3a4d5c94 MT |
129 | return 0; |
130 | } | |
131 | ||
163049ae | 132 | void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn) |
87d6a412 MT |
133 | { |
134 | INIT_LIST_HEAD(&work->node); | |
135 | work->fn = fn; | |
136 | init_waitqueue_head(&work->done); | |
137 | work->flushing = 0; | |
138 | work->queue_seq = work->done_seq = 0; | |
139 | } | |
6ac1afbf | 140 | EXPORT_SYMBOL_GPL(vhost_work_init); |
87d6a412 | 141 | |
3a4d5c94 | 142 | /* Init poll structure */ |
c23f3445 TH |
143 | void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn, |
144 | unsigned long mask, struct vhost_dev *dev) | |
3a4d5c94 | 145 | { |
3a4d5c94 MT |
146 | init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup); |
147 | init_poll_funcptr(&poll->table, vhost_poll_func); | |
148 | poll->mask = mask; | |
c23f3445 | 149 | poll->dev = dev; |
2b8b328b | 150 | poll->wqh = NULL; |
c23f3445 | 151 | |
87d6a412 | 152 | vhost_work_init(&poll->work, fn); |
3a4d5c94 | 153 | } |
6ac1afbf | 154 | EXPORT_SYMBOL_GPL(vhost_poll_init); |
3a4d5c94 MT |
155 | |
156 | /* Start polling a file. We add ourselves to file's wait queue. The caller must | |
157 | * keep a reference to a file until after vhost_poll_stop is called. */ | |
2b8b328b | 158 | int vhost_poll_start(struct vhost_poll *poll, struct file *file) |
3a4d5c94 MT |
159 | { |
160 | unsigned long mask; | |
2b8b328b | 161 | int ret = 0; |
d47effe1 | 162 | |
70181d51 JW |
163 | if (poll->wqh) |
164 | return 0; | |
165 | ||
3a4d5c94 MT |
166 | mask = file->f_op->poll(file, &poll->table); |
167 | if (mask) | |
168 | vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask); | |
2b8b328b JW |
169 | if (mask & POLLERR) { |
170 | if (poll->wqh) | |
171 | remove_wait_queue(poll->wqh, &poll->wait); | |
172 | ret = -EINVAL; | |
173 | } | |
174 | ||
175 | return ret; | |
3a4d5c94 | 176 | } |
6ac1afbf | 177 | EXPORT_SYMBOL_GPL(vhost_poll_start); |
3a4d5c94 MT |
178 | |
179 | /* Stop polling a file. After this function returns, it becomes safe to drop the | |
180 | * file reference. You must also flush afterwards. */ | |
181 | void vhost_poll_stop(struct vhost_poll *poll) | |
182 | { | |
2b8b328b JW |
183 | if (poll->wqh) { |
184 | remove_wait_queue(poll->wqh, &poll->wait); | |
185 | poll->wqh = NULL; | |
186 | } | |
3a4d5c94 | 187 | } |
6ac1afbf | 188 | EXPORT_SYMBOL_GPL(vhost_poll_stop); |
3a4d5c94 | 189 | |
0174b0c3 MT |
190 | static bool vhost_work_seq_done(struct vhost_dev *dev, struct vhost_work *work, |
191 | unsigned seq) | |
192 | { | |
193 | int left; | |
d47effe1 | 194 | |
0174b0c3 MT |
195 | spin_lock_irq(&dev->work_lock); |
196 | left = seq - work->done_seq; | |
197 | spin_unlock_irq(&dev->work_lock); | |
198 | return left <= 0; | |
199 | } | |
200 | ||
6ac1afbf | 201 | void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work) |
3a4d5c94 | 202 | { |
c23f3445 | 203 | unsigned seq; |
c23f3445 TH |
204 | int flushing; |
205 | ||
87d6a412 | 206 | spin_lock_irq(&dev->work_lock); |
c23f3445 TH |
207 | seq = work->queue_seq; |
208 | work->flushing++; | |
87d6a412 | 209 | spin_unlock_irq(&dev->work_lock); |
0174b0c3 | 210 | wait_event(work->done, vhost_work_seq_done(dev, work, seq)); |
87d6a412 | 211 | spin_lock_irq(&dev->work_lock); |
c23f3445 | 212 | flushing = --work->flushing; |
87d6a412 | 213 | spin_unlock_irq(&dev->work_lock); |
c23f3445 | 214 | BUG_ON(flushing < 0); |
3a4d5c94 | 215 | } |
6ac1afbf | 216 | EXPORT_SYMBOL_GPL(vhost_work_flush); |
3a4d5c94 | 217 | |
87d6a412 MT |
218 | /* Flush any work that has been scheduled. When calling this, don't hold any |
219 | * locks that are also used by the callback. */ | |
220 | void vhost_poll_flush(struct vhost_poll *poll) | |
221 | { | |
222 | vhost_work_flush(poll->dev, &poll->work); | |
223 | } | |
6ac1afbf | 224 | EXPORT_SYMBOL_GPL(vhost_poll_flush); |
87d6a412 | 225 | |
163049ae | 226 | void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work) |
3a4d5c94 | 227 | { |
c23f3445 TH |
228 | unsigned long flags; |
229 | ||
230 | spin_lock_irqsave(&dev->work_lock, flags); | |
231 | if (list_empty(&work->node)) { | |
232 | list_add_tail(&work->node, &dev->work_list); | |
233 | work->queue_seq++; | |
ac9fde24 | 234 | spin_unlock_irqrestore(&dev->work_lock, flags); |
c23f3445 | 235 | wake_up_process(dev->worker); |
ac9fde24 QC |
236 | } else { |
237 | spin_unlock_irqrestore(&dev->work_lock, flags); | |
c23f3445 | 238 | } |
3a4d5c94 | 239 | } |
6ac1afbf | 240 | EXPORT_SYMBOL_GPL(vhost_work_queue); |
3a4d5c94 | 241 | |
87d6a412 MT |
242 | void vhost_poll_queue(struct vhost_poll *poll) |
243 | { | |
244 | vhost_work_queue(poll->dev, &poll->work); | |
245 | } | |
6ac1afbf | 246 | EXPORT_SYMBOL_GPL(vhost_poll_queue); |
87d6a412 | 247 | |
3a4d5c94 MT |
248 | static void vhost_vq_reset(struct vhost_dev *dev, |
249 | struct vhost_virtqueue *vq) | |
250 | { | |
251 | vq->num = 1; | |
252 | vq->desc = NULL; | |
253 | vq->avail = NULL; | |
254 | vq->used = NULL; | |
255 | vq->last_avail_idx = 0; | |
256 | vq->avail_idx = 0; | |
257 | vq->last_used_idx = 0; | |
8ea8cf89 MT |
258 | vq->signalled_used = 0; |
259 | vq->signalled_used_valid = false; | |
3a4d5c94 | 260 | vq->used_flags = 0; |
3a4d5c94 MT |
261 | vq->log_used = false; |
262 | vq->log_addr = -1ull; | |
3a4d5c94 | 263 | vq->private_data = NULL; |
ea16c514 | 264 | vq->acked_features = 0; |
3a4d5c94 MT |
265 | vq->log_base = NULL; |
266 | vq->error_ctx = NULL; | |
267 | vq->error = NULL; | |
268 | vq->kick = NULL; | |
269 | vq->call_ctx = NULL; | |
270 | vq->call = NULL; | |
73a99f08 | 271 | vq->log_ctx = NULL; |
47283bef | 272 | vq->memory = NULL; |
2751c988 GK |
273 | vq->is_le = virtio_legacy_is_little_endian(); |
274 | vhost_vq_reset_user_be(vq); | |
3a4d5c94 MT |
275 | } |
276 | ||
c23f3445 TH |
277 | static int vhost_worker(void *data) |
278 | { | |
279 | struct vhost_dev *dev = data; | |
280 | struct vhost_work *work = NULL; | |
281 | unsigned uninitialized_var(seq); | |
d7ffde35 | 282 | mm_segment_t oldfs = get_fs(); |
c23f3445 | 283 | |
d7ffde35 | 284 | set_fs(USER_DS); |
64e1c807 MT |
285 | use_mm(dev->mm); |
286 | ||
c23f3445 TH |
287 | for (;;) { |
288 | /* mb paired w/ kthread_stop */ | |
289 | set_current_state(TASK_INTERRUPTIBLE); | |
290 | ||
291 | spin_lock_irq(&dev->work_lock); | |
292 | if (work) { | |
293 | work->done_seq = seq; | |
294 | if (work->flushing) | |
295 | wake_up_all(&work->done); | |
296 | } | |
297 | ||
298 | if (kthread_should_stop()) { | |
299 | spin_unlock_irq(&dev->work_lock); | |
300 | __set_current_state(TASK_RUNNING); | |
64e1c807 | 301 | break; |
c23f3445 TH |
302 | } |
303 | if (!list_empty(&dev->work_list)) { | |
304 | work = list_first_entry(&dev->work_list, | |
305 | struct vhost_work, node); | |
306 | list_del_init(&work->node); | |
307 | seq = work->queue_seq; | |
308 | } else | |
309 | work = NULL; | |
310 | spin_unlock_irq(&dev->work_lock); | |
311 | ||
312 | if (work) { | |
313 | __set_current_state(TASK_RUNNING); | |
314 | work->fn(work); | |
d550dda1 NHE |
315 | if (need_resched()) |
316 | schedule(); | |
c23f3445 TH |
317 | } else |
318 | schedule(); | |
319 | ||
320 | } | |
64e1c807 | 321 | unuse_mm(dev->mm); |
d7ffde35 | 322 | set_fs(oldfs); |
64e1c807 | 323 | return 0; |
c23f3445 TH |
324 | } |
325 | ||
bab632d6 MT |
326 | static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq) |
327 | { | |
328 | kfree(vq->indirect); | |
329 | vq->indirect = NULL; | |
330 | kfree(vq->log); | |
331 | vq->log = NULL; | |
332 | kfree(vq->heads); | |
333 | vq->heads = NULL; | |
bab632d6 MT |
334 | } |
335 | ||
e0e9b406 JW |
336 | /* Helper to allocate iovec buffers for all vqs. */ |
337 | static long vhost_dev_alloc_iovecs(struct vhost_dev *dev) | |
338 | { | |
6d5e6aa8 | 339 | struct vhost_virtqueue *vq; |
e0e9b406 | 340 | int i; |
d47effe1 | 341 | |
e0e9b406 | 342 | for (i = 0; i < dev->nvqs; ++i) { |
6d5e6aa8 AH |
343 | vq = dev->vqs[i]; |
344 | vq->indirect = kmalloc(sizeof *vq->indirect * UIO_MAXIOV, | |
345 | GFP_KERNEL); | |
346 | vq->log = kmalloc(sizeof *vq->log * UIO_MAXIOV, GFP_KERNEL); | |
347 | vq->heads = kmalloc(sizeof *vq->heads * UIO_MAXIOV, GFP_KERNEL); | |
348 | if (!vq->indirect || !vq->log || !vq->heads) | |
e0e9b406 JW |
349 | goto err_nomem; |
350 | } | |
351 | return 0; | |
d47effe1 | 352 | |
e0e9b406 | 353 | err_nomem: |
bab632d6 | 354 | for (; i >= 0; --i) |
3ab2e420 | 355 | vhost_vq_free_iovecs(dev->vqs[i]); |
e0e9b406 JW |
356 | return -ENOMEM; |
357 | } | |
358 | ||
359 | static void vhost_dev_free_iovecs(struct vhost_dev *dev) | |
360 | { | |
361 | int i; | |
d47effe1 | 362 | |
bab632d6 | 363 | for (i = 0; i < dev->nvqs; ++i) |
3ab2e420 | 364 | vhost_vq_free_iovecs(dev->vqs[i]); |
e0e9b406 JW |
365 | } |
366 | ||
59566b6e | 367 | void vhost_dev_init(struct vhost_dev *dev, |
3ab2e420 | 368 | struct vhost_virtqueue **vqs, int nvqs) |
3a4d5c94 | 369 | { |
6d5e6aa8 | 370 | struct vhost_virtqueue *vq; |
3a4d5c94 | 371 | int i; |
c23f3445 | 372 | |
3a4d5c94 MT |
373 | dev->vqs = vqs; |
374 | dev->nvqs = nvqs; | |
375 | mutex_init(&dev->mutex); | |
376 | dev->log_ctx = NULL; | |
377 | dev->log_file = NULL; | |
378 | dev->memory = NULL; | |
379 | dev->mm = NULL; | |
c23f3445 TH |
380 | spin_lock_init(&dev->work_lock); |
381 | INIT_LIST_HEAD(&dev->work_list); | |
382 | dev->worker = NULL; | |
3a4d5c94 MT |
383 | |
384 | for (i = 0; i < dev->nvqs; ++i) { | |
6d5e6aa8 AH |
385 | vq = dev->vqs[i]; |
386 | vq->log = NULL; | |
387 | vq->indirect = NULL; | |
388 | vq->heads = NULL; | |
389 | vq->dev = dev; | |
390 | mutex_init(&vq->mutex); | |
391 | vhost_vq_reset(dev, vq); | |
392 | if (vq->handle_kick) | |
393 | vhost_poll_init(&vq->poll, vq->handle_kick, | |
394 | POLLIN, dev); | |
3a4d5c94 | 395 | } |
3a4d5c94 | 396 | } |
6ac1afbf | 397 | EXPORT_SYMBOL_GPL(vhost_dev_init); |
3a4d5c94 MT |
398 | |
399 | /* Caller should have device mutex */ | |
400 | long vhost_dev_check_owner(struct vhost_dev *dev) | |
401 | { | |
402 | /* Are you the owner? If not, I don't think you mean to do that */ | |
403 | return dev->mm == current->mm ? 0 : -EPERM; | |
404 | } | |
6ac1afbf | 405 | EXPORT_SYMBOL_GPL(vhost_dev_check_owner); |
3a4d5c94 | 406 | |
87d6a412 | 407 | struct vhost_attach_cgroups_struct { |
d47effe1 KK |
408 | struct vhost_work work; |
409 | struct task_struct *owner; | |
410 | int ret; | |
87d6a412 MT |
411 | }; |
412 | ||
413 | static void vhost_attach_cgroups_work(struct vhost_work *work) | |
414 | { | |
d47effe1 KK |
415 | struct vhost_attach_cgroups_struct *s; |
416 | ||
417 | s = container_of(work, struct vhost_attach_cgroups_struct, work); | |
418 | s->ret = cgroup_attach_task_all(s->owner, current); | |
87d6a412 MT |
419 | } |
420 | ||
421 | static int vhost_attach_cgroups(struct vhost_dev *dev) | |
422 | { | |
d47effe1 KK |
423 | struct vhost_attach_cgroups_struct attach; |
424 | ||
425 | attach.owner = current; | |
426 | vhost_work_init(&attach.work, vhost_attach_cgroups_work); | |
427 | vhost_work_queue(dev, &attach.work); | |
428 | vhost_work_flush(dev, &attach.work); | |
429 | return attach.ret; | |
87d6a412 MT |
430 | } |
431 | ||
05c05351 MT |
432 | /* Caller should have device mutex */ |
433 | bool vhost_dev_has_owner(struct vhost_dev *dev) | |
434 | { | |
435 | return dev->mm; | |
436 | } | |
6ac1afbf | 437 | EXPORT_SYMBOL_GPL(vhost_dev_has_owner); |
05c05351 | 438 | |
3a4d5c94 | 439 | /* Caller should have device mutex */ |
54db63c2 | 440 | long vhost_dev_set_owner(struct vhost_dev *dev) |
3a4d5c94 | 441 | { |
c23f3445 TH |
442 | struct task_struct *worker; |
443 | int err; | |
d47effe1 | 444 | |
3a4d5c94 | 445 | /* Is there an owner already? */ |
05c05351 | 446 | if (vhost_dev_has_owner(dev)) { |
c23f3445 TH |
447 | err = -EBUSY; |
448 | goto err_mm; | |
449 | } | |
d47effe1 | 450 | |
3a4d5c94 MT |
451 | /* No owner, become one */ |
452 | dev->mm = get_task_mm(current); | |
c23f3445 TH |
453 | worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid); |
454 | if (IS_ERR(worker)) { | |
455 | err = PTR_ERR(worker); | |
456 | goto err_worker; | |
457 | } | |
458 | ||
459 | dev->worker = worker; | |
87d6a412 MT |
460 | wake_up_process(worker); /* avoid contributing to loadavg */ |
461 | ||
462 | err = vhost_attach_cgroups(dev); | |
9e3d1957 MT |
463 | if (err) |
464 | goto err_cgroup; | |
c23f3445 | 465 | |
e0e9b406 JW |
466 | err = vhost_dev_alloc_iovecs(dev); |
467 | if (err) | |
468 | goto err_cgroup; | |
469 | ||
3a4d5c94 | 470 | return 0; |
9e3d1957 MT |
471 | err_cgroup: |
472 | kthread_stop(worker); | |
615cc221 | 473 | dev->worker = NULL; |
c23f3445 TH |
474 | err_worker: |
475 | if (dev->mm) | |
476 | mmput(dev->mm); | |
477 | dev->mm = NULL; | |
478 | err_mm: | |
479 | return err; | |
3a4d5c94 | 480 | } |
6ac1afbf | 481 | EXPORT_SYMBOL_GPL(vhost_dev_set_owner); |
3a4d5c94 | 482 | |
150b9e51 | 483 | struct vhost_memory *vhost_dev_reset_owner_prepare(void) |
3a4d5c94 | 484 | { |
150b9e51 MT |
485 | return kmalloc(offsetof(struct vhost_memory, regions), GFP_KERNEL); |
486 | } | |
6ac1afbf | 487 | EXPORT_SYMBOL_GPL(vhost_dev_reset_owner_prepare); |
3a4d5c94 | 488 | |
150b9e51 MT |
489 | /* Caller should have device mutex */ |
490 | void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_memory *memory) | |
491 | { | |
47283bef MT |
492 | int i; |
493 | ||
ea5d4046 | 494 | vhost_dev_cleanup(dev, true); |
3a4d5c94 | 495 | |
150b9e51 | 496 | /* Restore memory to default empty mapping. */ |
3a4d5c94 | 497 | memory->nregions = 0; |
47283bef MT |
498 | dev->memory = memory; |
499 | /* We don't need VQ locks below since vhost_dev_cleanup makes sure | |
500 | * VQs aren't running. | |
501 | */ | |
502 | for (i = 0; i < dev->nvqs; ++i) | |
503 | dev->vqs[i]->memory = memory; | |
3a4d5c94 | 504 | } |
6ac1afbf | 505 | EXPORT_SYMBOL_GPL(vhost_dev_reset_owner); |
3a4d5c94 | 506 | |
b211616d | 507 | void vhost_dev_stop(struct vhost_dev *dev) |
bab632d6 MT |
508 | { |
509 | int i; | |
b211616d MT |
510 | |
511 | for (i = 0; i < dev->nvqs; ++i) { | |
3ab2e420 AH |
512 | if (dev->vqs[i]->kick && dev->vqs[i]->handle_kick) { |
513 | vhost_poll_stop(&dev->vqs[i]->poll); | |
514 | vhost_poll_flush(&dev->vqs[i]->poll); | |
b211616d | 515 | } |
bab632d6 | 516 | } |
bab632d6 | 517 | } |
6ac1afbf | 518 | EXPORT_SYMBOL_GPL(vhost_dev_stop); |
bab632d6 | 519 | |
ea5d4046 MT |
520 | /* Caller should have device mutex if and only if locked is set */ |
521 | void vhost_dev_cleanup(struct vhost_dev *dev, bool locked) | |
3a4d5c94 MT |
522 | { |
523 | int i; | |
d47effe1 | 524 | |
3a4d5c94 | 525 | for (i = 0; i < dev->nvqs; ++i) { |
3ab2e420 AH |
526 | if (dev->vqs[i]->error_ctx) |
527 | eventfd_ctx_put(dev->vqs[i]->error_ctx); | |
528 | if (dev->vqs[i]->error) | |
529 | fput(dev->vqs[i]->error); | |
530 | if (dev->vqs[i]->kick) | |
531 | fput(dev->vqs[i]->kick); | |
532 | if (dev->vqs[i]->call_ctx) | |
533 | eventfd_ctx_put(dev->vqs[i]->call_ctx); | |
534 | if (dev->vqs[i]->call) | |
535 | fput(dev->vqs[i]->call); | |
536 | vhost_vq_reset(dev, dev->vqs[i]); | |
3a4d5c94 | 537 | } |
e0e9b406 | 538 | vhost_dev_free_iovecs(dev); |
3a4d5c94 MT |
539 | if (dev->log_ctx) |
540 | eventfd_ctx_put(dev->log_ctx); | |
541 | dev->log_ctx = NULL; | |
542 | if (dev->log_file) | |
543 | fput(dev->log_file); | |
544 | dev->log_file = NULL; | |
545 | /* No one will access memory at this point */ | |
47283bef MT |
546 | kfree(dev->memory); |
547 | dev->memory = NULL; | |
c23f3445 | 548 | WARN_ON(!list_empty(&dev->work_list)); |
78b620ce ED |
549 | if (dev->worker) { |
550 | kthread_stop(dev->worker); | |
551 | dev->worker = NULL; | |
552 | } | |
533a19b4 MT |
553 | if (dev->mm) |
554 | mmput(dev->mm); | |
555 | dev->mm = NULL; | |
3a4d5c94 | 556 | } |
6ac1afbf | 557 | EXPORT_SYMBOL_GPL(vhost_dev_cleanup); |
3a4d5c94 MT |
558 | |
559 | static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz) | |
560 | { | |
561 | u64 a = addr / VHOST_PAGE_SIZE / 8; | |
d47effe1 | 562 | |
3a4d5c94 MT |
563 | /* Make sure 64 bit math will not overflow. */ |
564 | if (a > ULONG_MAX - (unsigned long)log_base || | |
565 | a + (unsigned long)log_base > ULONG_MAX) | |
6d97e55f | 566 | return 0; |
3a4d5c94 MT |
567 | |
568 | return access_ok(VERIFY_WRITE, log_base + a, | |
569 | (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8); | |
570 | } | |
571 | ||
572 | /* Caller should have vq mutex and device mutex. */ | |
573 | static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem, | |
574 | int log_all) | |
575 | { | |
576 | int i; | |
179b284e | 577 | |
f8322fbe MT |
578 | if (!mem) |
579 | return 0; | |
179b284e | 580 | |
3a4d5c94 MT |
581 | for (i = 0; i < mem->nregions; ++i) { |
582 | struct vhost_memory_region *m = mem->regions + i; | |
583 | unsigned long a = m->userspace_addr; | |
584 | if (m->memory_size > ULONG_MAX) | |
585 | return 0; | |
586 | else if (!access_ok(VERIFY_WRITE, (void __user *)a, | |
587 | m->memory_size)) | |
588 | return 0; | |
589 | else if (log_all && !log_access_ok(log_base, | |
590 | m->guest_phys_addr, | |
591 | m->memory_size)) | |
592 | return 0; | |
593 | } | |
594 | return 1; | |
595 | } | |
596 | ||
597 | /* Can we switch to this memory table? */ | |
598 | /* Caller should have device mutex but not vq mutex */ | |
599 | static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem, | |
600 | int log_all) | |
601 | { | |
602 | int i; | |
d47effe1 | 603 | |
3a4d5c94 MT |
604 | for (i = 0; i < d->nvqs; ++i) { |
605 | int ok; | |
ea16c514 MT |
606 | bool log; |
607 | ||
3ab2e420 | 608 | mutex_lock(&d->vqs[i]->mutex); |
ea16c514 | 609 | log = log_all || vhost_has_feature(d->vqs[i], VHOST_F_LOG_ALL); |
3a4d5c94 | 610 | /* If ring is inactive, will check when it's enabled. */ |
3ab2e420 | 611 | if (d->vqs[i]->private_data) |
ea16c514 | 612 | ok = vq_memory_access_ok(d->vqs[i]->log_base, mem, log); |
3a4d5c94 MT |
613 | else |
614 | ok = 1; | |
3ab2e420 | 615 | mutex_unlock(&d->vqs[i]->mutex); |
3a4d5c94 MT |
616 | if (!ok) |
617 | return 0; | |
618 | } | |
619 | return 1; | |
620 | } | |
621 | ||
ea16c514 | 622 | static int vq_access_ok(struct vhost_virtqueue *vq, unsigned int num, |
3a4d5c94 MT |
623 | struct vring_desc __user *desc, |
624 | struct vring_avail __user *avail, | |
625 | struct vring_used __user *used) | |
626 | { | |
ea16c514 | 627 | size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0; |
3a4d5c94 MT |
628 | return access_ok(VERIFY_READ, desc, num * sizeof *desc) && |
629 | access_ok(VERIFY_READ, avail, | |
8ea8cf89 | 630 | sizeof *avail + num * sizeof *avail->ring + s) && |
3a4d5c94 | 631 | access_ok(VERIFY_WRITE, used, |
8ea8cf89 | 632 | sizeof *used + num * sizeof *used->ring + s); |
3a4d5c94 MT |
633 | } |
634 | ||
635 | /* Can we log writes? */ | |
636 | /* Caller should have device mutex but not vq mutex */ | |
637 | int vhost_log_access_ok(struct vhost_dev *dev) | |
638 | { | |
47283bef | 639 | return memory_access_ok(dev, dev->memory, 1); |
3a4d5c94 | 640 | } |
6ac1afbf | 641 | EXPORT_SYMBOL_GPL(vhost_log_access_ok); |
3a4d5c94 MT |
642 | |
643 | /* Verify access for write logging. */ | |
644 | /* Caller should have vq mutex and device mutex */ | |
ea16c514 | 645 | static int vq_log_access_ok(struct vhost_virtqueue *vq, |
8ea8cf89 | 646 | void __user *log_base) |
3a4d5c94 | 647 | { |
ea16c514 | 648 | size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0; |
28457ee6 | 649 | |
47283bef | 650 | return vq_memory_access_ok(log_base, vq->memory, |
ea16c514 | 651 | vhost_has_feature(vq, VHOST_F_LOG_ALL)) && |
3a4d5c94 MT |
652 | (!vq->log_used || log_access_ok(log_base, vq->log_addr, |
653 | sizeof *vq->used + | |
8ea8cf89 | 654 | vq->num * sizeof *vq->used->ring + s)); |
3a4d5c94 MT |
655 | } |
656 | ||
657 | /* Can we start vq? */ | |
658 | /* Caller should have vq mutex and device mutex */ | |
659 | int vhost_vq_access_ok(struct vhost_virtqueue *vq) | |
660 | { | |
ea16c514 MT |
661 | return vq_access_ok(vq, vq->num, vq->desc, vq->avail, vq->used) && |
662 | vq_log_access_ok(vq, vq->log_base); | |
3a4d5c94 | 663 | } |
6ac1afbf | 664 | EXPORT_SYMBOL_GPL(vhost_vq_access_ok); |
3a4d5c94 MT |
665 | |
666 | static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m) | |
667 | { | |
668 | struct vhost_memory mem, *newmem, *oldmem; | |
669 | unsigned long size = offsetof(struct vhost_memory, regions); | |
98f9ca0a | 670 | int i; |
d47effe1 | 671 | |
7ad9c9d2 TY |
672 | if (copy_from_user(&mem, m, size)) |
673 | return -EFAULT; | |
3a4d5c94 MT |
674 | if (mem.padding) |
675 | return -EOPNOTSUPP; | |
676 | if (mem.nregions > VHOST_MEMORY_MAX_NREGIONS) | |
677 | return -E2BIG; | |
678 | newmem = kmalloc(size + mem.nregions * sizeof *m->regions, GFP_KERNEL); | |
679 | if (!newmem) | |
680 | return -ENOMEM; | |
681 | ||
682 | memcpy(newmem, &mem, size); | |
7ad9c9d2 TY |
683 | if (copy_from_user(newmem->regions, m->regions, |
684 | mem.nregions * sizeof *m->regions)) { | |
3a4d5c94 | 685 | kfree(newmem); |
7ad9c9d2 | 686 | return -EFAULT; |
3a4d5c94 MT |
687 | } |
688 | ||
ea16c514 | 689 | if (!memory_access_ok(d, newmem, 0)) { |
a02c3789 | 690 | kfree(newmem); |
3a4d5c94 | 691 | return -EFAULT; |
a02c3789 | 692 | } |
47283bef MT |
693 | oldmem = d->memory; |
694 | d->memory = newmem; | |
98f9ca0a | 695 | |
47283bef | 696 | /* All memory accesses are done under some VQ mutex. */ |
98f9ca0a MT |
697 | for (i = 0; i < d->nvqs; ++i) { |
698 | mutex_lock(&d->vqs[i]->mutex); | |
47283bef | 699 | d->vqs[i]->memory = newmem; |
98f9ca0a MT |
700 | mutex_unlock(&d->vqs[i]->mutex); |
701 | } | |
3a4d5c94 MT |
702 | kfree(oldmem); |
703 | return 0; | |
704 | } | |
705 | ||
935cdee7 | 706 | long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp) |
3a4d5c94 | 707 | { |
cecb46f1 AV |
708 | struct file *eventfp, *filep = NULL; |
709 | bool pollstart = false, pollstop = false; | |
3a4d5c94 MT |
710 | struct eventfd_ctx *ctx = NULL; |
711 | u32 __user *idxp = argp; | |
712 | struct vhost_virtqueue *vq; | |
713 | struct vhost_vring_state s; | |
714 | struct vhost_vring_file f; | |
715 | struct vhost_vring_addr a; | |
716 | u32 idx; | |
717 | long r; | |
718 | ||
719 | r = get_user(idx, idxp); | |
720 | if (r < 0) | |
721 | return r; | |
0f3d9a17 | 722 | if (idx >= d->nvqs) |
3a4d5c94 MT |
723 | return -ENOBUFS; |
724 | ||
3ab2e420 | 725 | vq = d->vqs[idx]; |
3a4d5c94 MT |
726 | |
727 | mutex_lock(&vq->mutex); | |
728 | ||
729 | switch (ioctl) { | |
730 | case VHOST_SET_VRING_NUM: | |
731 | /* Resizing ring with an active backend? | |
732 | * You don't want to do that. */ | |
733 | if (vq->private_data) { | |
734 | r = -EBUSY; | |
735 | break; | |
736 | } | |
7ad9c9d2 TY |
737 | if (copy_from_user(&s, argp, sizeof s)) { |
738 | r = -EFAULT; | |
3a4d5c94 | 739 | break; |
7ad9c9d2 | 740 | } |
3a4d5c94 MT |
741 | if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) { |
742 | r = -EINVAL; | |
743 | break; | |
744 | } | |
745 | vq->num = s.num; | |
746 | break; | |
747 | case VHOST_SET_VRING_BASE: | |
748 | /* Moving base with an active backend? | |
749 | * You don't want to do that. */ | |
750 | if (vq->private_data) { | |
751 | r = -EBUSY; | |
752 | break; | |
753 | } | |
7ad9c9d2 TY |
754 | if (copy_from_user(&s, argp, sizeof s)) { |
755 | r = -EFAULT; | |
3a4d5c94 | 756 | break; |
7ad9c9d2 | 757 | } |
3a4d5c94 MT |
758 | if (s.num > 0xffff) { |
759 | r = -EINVAL; | |
760 | break; | |
761 | } | |
762 | vq->last_avail_idx = s.num; | |
763 | /* Forget the cached index value. */ | |
764 | vq->avail_idx = vq->last_avail_idx; | |
765 | break; | |
766 | case VHOST_GET_VRING_BASE: | |
767 | s.index = idx; | |
768 | s.num = vq->last_avail_idx; | |
7ad9c9d2 TY |
769 | if (copy_to_user(argp, &s, sizeof s)) |
770 | r = -EFAULT; | |
3a4d5c94 MT |
771 | break; |
772 | case VHOST_SET_VRING_ADDR: | |
7ad9c9d2 TY |
773 | if (copy_from_user(&a, argp, sizeof a)) { |
774 | r = -EFAULT; | |
3a4d5c94 | 775 | break; |
7ad9c9d2 | 776 | } |
3a4d5c94 MT |
777 | if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) { |
778 | r = -EOPNOTSUPP; | |
779 | break; | |
780 | } | |
781 | /* For 32bit, verify that the top 32bits of the user | |
782 | data are set to zero. */ | |
783 | if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr || | |
784 | (u64)(unsigned long)a.used_user_addr != a.used_user_addr || | |
785 | (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) { | |
786 | r = -EFAULT; | |
787 | break; | |
788 | } | |
5d9a07b0 MT |
789 | |
790 | /* Make sure it's safe to cast pointers to vring types. */ | |
791 | BUILD_BUG_ON(__alignof__ *vq->avail > VRING_AVAIL_ALIGN_SIZE); | |
792 | BUILD_BUG_ON(__alignof__ *vq->used > VRING_USED_ALIGN_SIZE); | |
793 | if ((a.avail_user_addr & (VRING_AVAIL_ALIGN_SIZE - 1)) || | |
794 | (a.used_user_addr & (VRING_USED_ALIGN_SIZE - 1)) || | |
795 | (a.log_guest_addr & (sizeof(u64) - 1))) { | |
3a4d5c94 MT |
796 | r = -EINVAL; |
797 | break; | |
798 | } | |
799 | ||
800 | /* We only verify access here if backend is configured. | |
801 | * If it is not, we don't as size might not have been setup. | |
802 | * We will verify when backend is configured. */ | |
803 | if (vq->private_data) { | |
ea16c514 | 804 | if (!vq_access_ok(vq, vq->num, |
3a4d5c94 MT |
805 | (void __user *)(unsigned long)a.desc_user_addr, |
806 | (void __user *)(unsigned long)a.avail_user_addr, | |
807 | (void __user *)(unsigned long)a.used_user_addr)) { | |
808 | r = -EINVAL; | |
809 | break; | |
810 | } | |
811 | ||
812 | /* Also validate log access for used ring if enabled. */ | |
813 | if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) && | |
814 | !log_access_ok(vq->log_base, a.log_guest_addr, | |
815 | sizeof *vq->used + | |
816 | vq->num * sizeof *vq->used->ring)) { | |
817 | r = -EINVAL; | |
818 | break; | |
819 | } | |
820 | } | |
821 | ||
3a4d5c94 MT |
822 | vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG)); |
823 | vq->desc = (void __user *)(unsigned long)a.desc_user_addr; | |
824 | vq->avail = (void __user *)(unsigned long)a.avail_user_addr; | |
825 | vq->log_addr = a.log_guest_addr; | |
826 | vq->used = (void __user *)(unsigned long)a.used_user_addr; | |
827 | break; | |
828 | case VHOST_SET_VRING_KICK: | |
7ad9c9d2 TY |
829 | if (copy_from_user(&f, argp, sizeof f)) { |
830 | r = -EFAULT; | |
3a4d5c94 | 831 | break; |
7ad9c9d2 | 832 | } |
3a4d5c94 | 833 | eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd); |
535297a6 MT |
834 | if (IS_ERR(eventfp)) { |
835 | r = PTR_ERR(eventfp); | |
836 | break; | |
837 | } | |
3a4d5c94 | 838 | if (eventfp != vq->kick) { |
cecb46f1 AV |
839 | pollstop = (filep = vq->kick) != NULL; |
840 | pollstart = (vq->kick = eventfp) != NULL; | |
3a4d5c94 MT |
841 | } else |
842 | filep = eventfp; | |
843 | break; | |
844 | case VHOST_SET_VRING_CALL: | |
7ad9c9d2 TY |
845 | if (copy_from_user(&f, argp, sizeof f)) { |
846 | r = -EFAULT; | |
3a4d5c94 | 847 | break; |
7ad9c9d2 | 848 | } |
3a4d5c94 | 849 | eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd); |
535297a6 MT |
850 | if (IS_ERR(eventfp)) { |
851 | r = PTR_ERR(eventfp); | |
852 | break; | |
853 | } | |
3a4d5c94 MT |
854 | if (eventfp != vq->call) { |
855 | filep = vq->call; | |
856 | ctx = vq->call_ctx; | |
857 | vq->call = eventfp; | |
858 | vq->call_ctx = eventfp ? | |
859 | eventfd_ctx_fileget(eventfp) : NULL; | |
860 | } else | |
861 | filep = eventfp; | |
862 | break; | |
863 | case VHOST_SET_VRING_ERR: | |
7ad9c9d2 TY |
864 | if (copy_from_user(&f, argp, sizeof f)) { |
865 | r = -EFAULT; | |
3a4d5c94 | 866 | break; |
7ad9c9d2 | 867 | } |
3a4d5c94 | 868 | eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd); |
535297a6 MT |
869 | if (IS_ERR(eventfp)) { |
870 | r = PTR_ERR(eventfp); | |
871 | break; | |
872 | } | |
3a4d5c94 MT |
873 | if (eventfp != vq->error) { |
874 | filep = vq->error; | |
875 | vq->error = eventfp; | |
876 | ctx = vq->error_ctx; | |
877 | vq->error_ctx = eventfp ? | |
878 | eventfd_ctx_fileget(eventfp) : NULL; | |
879 | } else | |
880 | filep = eventfp; | |
881 | break; | |
2751c988 GK |
882 | case VHOST_SET_VRING_ENDIAN: |
883 | r = vhost_set_vring_endian(vq, argp); | |
884 | break; | |
885 | case VHOST_GET_VRING_ENDIAN: | |
886 | r = vhost_get_vring_endian(vq, idx, argp); | |
887 | break; | |
3a4d5c94 MT |
888 | default: |
889 | r = -ENOIOCTLCMD; | |
890 | } | |
891 | ||
892 | if (pollstop && vq->handle_kick) | |
893 | vhost_poll_stop(&vq->poll); | |
894 | ||
895 | if (ctx) | |
896 | eventfd_ctx_put(ctx); | |
897 | if (filep) | |
898 | fput(filep); | |
899 | ||
900 | if (pollstart && vq->handle_kick) | |
2b8b328b | 901 | r = vhost_poll_start(&vq->poll, vq->kick); |
3a4d5c94 MT |
902 | |
903 | mutex_unlock(&vq->mutex); | |
904 | ||
905 | if (pollstop && vq->handle_kick) | |
906 | vhost_poll_flush(&vq->poll); | |
907 | return r; | |
908 | } | |
6ac1afbf | 909 | EXPORT_SYMBOL_GPL(vhost_vring_ioctl); |
3a4d5c94 MT |
910 | |
911 | /* Caller must have device mutex */ | |
935cdee7 | 912 | long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) |
3a4d5c94 | 913 | { |
3a4d5c94 MT |
914 | struct file *eventfp, *filep = NULL; |
915 | struct eventfd_ctx *ctx = NULL; | |
916 | u64 p; | |
917 | long r; | |
918 | int i, fd; | |
919 | ||
920 | /* If you are not the owner, you can become one */ | |
921 | if (ioctl == VHOST_SET_OWNER) { | |
922 | r = vhost_dev_set_owner(d); | |
923 | goto done; | |
924 | } | |
925 | ||
926 | /* You must be the owner to do anything else */ | |
927 | r = vhost_dev_check_owner(d); | |
928 | if (r) | |
929 | goto done; | |
930 | ||
931 | switch (ioctl) { | |
932 | case VHOST_SET_MEM_TABLE: | |
933 | r = vhost_set_memory(d, argp); | |
934 | break; | |
935 | case VHOST_SET_LOG_BASE: | |
7ad9c9d2 TY |
936 | if (copy_from_user(&p, argp, sizeof p)) { |
937 | r = -EFAULT; | |
3a4d5c94 | 938 | break; |
7ad9c9d2 | 939 | } |
3a4d5c94 MT |
940 | if ((u64)(unsigned long)p != p) { |
941 | r = -EFAULT; | |
942 | break; | |
943 | } | |
944 | for (i = 0; i < d->nvqs; ++i) { | |
945 | struct vhost_virtqueue *vq; | |
946 | void __user *base = (void __user *)(unsigned long)p; | |
3ab2e420 | 947 | vq = d->vqs[i]; |
3a4d5c94 MT |
948 | mutex_lock(&vq->mutex); |
949 | /* If ring is inactive, will check when it's enabled. */ | |
ea16c514 | 950 | if (vq->private_data && !vq_log_access_ok(vq, base)) |
3a4d5c94 MT |
951 | r = -EFAULT; |
952 | else | |
953 | vq->log_base = base; | |
954 | mutex_unlock(&vq->mutex); | |
955 | } | |
956 | break; | |
957 | case VHOST_SET_LOG_FD: | |
958 | r = get_user(fd, (int __user *)argp); | |
959 | if (r < 0) | |
960 | break; | |
961 | eventfp = fd == -1 ? NULL : eventfd_fget(fd); | |
962 | if (IS_ERR(eventfp)) { | |
963 | r = PTR_ERR(eventfp); | |
964 | break; | |
965 | } | |
966 | if (eventfp != d->log_file) { | |
967 | filep = d->log_file; | |
968 | ctx = d->log_ctx; | |
969 | d->log_ctx = eventfp ? | |
970 | eventfd_ctx_fileget(eventfp) : NULL; | |
971 | } else | |
972 | filep = eventfp; | |
973 | for (i = 0; i < d->nvqs; ++i) { | |
3ab2e420 AH |
974 | mutex_lock(&d->vqs[i]->mutex); |
975 | d->vqs[i]->log_ctx = d->log_ctx; | |
976 | mutex_unlock(&d->vqs[i]->mutex); | |
3a4d5c94 MT |
977 | } |
978 | if (ctx) | |
979 | eventfd_ctx_put(ctx); | |
980 | if (filep) | |
981 | fput(filep); | |
982 | break; | |
983 | default: | |
935cdee7 | 984 | r = -ENOIOCTLCMD; |
3a4d5c94 MT |
985 | break; |
986 | } | |
987 | done: | |
988 | return r; | |
989 | } | |
6ac1afbf | 990 | EXPORT_SYMBOL_GPL(vhost_dev_ioctl); |
3a4d5c94 MT |
991 | |
992 | static const struct vhost_memory_region *find_region(struct vhost_memory *mem, | |
993 | __u64 addr, __u32 len) | |
994 | { | |
995 | struct vhost_memory_region *reg; | |
996 | int i; | |
d47effe1 | 997 | |
3a4d5c94 MT |
998 | /* linear search is not brilliant, but we really have on the order of 6 |
999 | * regions in practice */ | |
1000 | for (i = 0; i < mem->nregions; ++i) { | |
1001 | reg = mem->regions + i; | |
1002 | if (reg->guest_phys_addr <= addr && | |
1003 | reg->guest_phys_addr + reg->memory_size - 1 >= addr) | |
1004 | return reg; | |
1005 | } | |
1006 | return NULL; | |
1007 | } | |
1008 | ||
1009 | /* TODO: This is really inefficient. We need something like get_user() | |
1010 | * (instruction directly accesses the data, with an exception table entry | |
1011 | * returning -EFAULT). See Documentation/x86/exception-tables.txt. | |
1012 | */ | |
1013 | static int set_bit_to_user(int nr, void __user *addr) | |
1014 | { | |
1015 | unsigned long log = (unsigned long)addr; | |
1016 | struct page *page; | |
1017 | void *base; | |
1018 | int bit = nr + (log % PAGE_SIZE) * 8; | |
1019 | int r; | |
d47effe1 | 1020 | |
3a4d5c94 | 1021 | r = get_user_pages_fast(log, 1, 1, &page); |
d6db3f5c | 1022 | if (r < 0) |
3a4d5c94 | 1023 | return r; |
d6db3f5c | 1024 | BUG_ON(r != 1); |
c6daa7ff | 1025 | base = kmap_atomic(page); |
3a4d5c94 | 1026 | set_bit(bit, base); |
c6daa7ff | 1027 | kunmap_atomic(base); |
3a4d5c94 MT |
1028 | set_page_dirty_lock(page); |
1029 | put_page(page); | |
1030 | return 0; | |
1031 | } | |
1032 | ||
1033 | static int log_write(void __user *log_base, | |
1034 | u64 write_address, u64 write_length) | |
1035 | { | |
28831ee6 | 1036 | u64 write_page = write_address / VHOST_PAGE_SIZE; |
3a4d5c94 | 1037 | int r; |
d47effe1 | 1038 | |
3a4d5c94 MT |
1039 | if (!write_length) |
1040 | return 0; | |
3bf9be40 | 1041 | write_length += write_address % VHOST_PAGE_SIZE; |
3a4d5c94 MT |
1042 | for (;;) { |
1043 | u64 base = (u64)(unsigned long)log_base; | |
28831ee6 MT |
1044 | u64 log = base + write_page / 8; |
1045 | int bit = write_page % 8; | |
3a4d5c94 MT |
1046 | if ((u64)(unsigned long)log != log) |
1047 | return -EFAULT; | |
1048 | r = set_bit_to_user(bit, (void __user *)(unsigned long)log); | |
1049 | if (r < 0) | |
1050 | return r; | |
1051 | if (write_length <= VHOST_PAGE_SIZE) | |
1052 | break; | |
1053 | write_length -= VHOST_PAGE_SIZE; | |
28831ee6 | 1054 | write_page += 1; |
3a4d5c94 MT |
1055 | } |
1056 | return r; | |
1057 | } | |
1058 | ||
1059 | int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log, | |
1060 | unsigned int log_num, u64 len) | |
1061 | { | |
1062 | int i, r; | |
1063 | ||
1064 | /* Make sure data written is seen before log. */ | |
5659338c | 1065 | smp_wmb(); |
3a4d5c94 MT |
1066 | for (i = 0; i < log_num; ++i) { |
1067 | u64 l = min(log[i].len, len); | |
1068 | r = log_write(vq->log_base, log[i].addr, l); | |
1069 | if (r < 0) | |
1070 | return r; | |
1071 | len -= l; | |
5786aee8 MT |
1072 | if (!len) { |
1073 | if (vq->log_ctx) | |
1074 | eventfd_signal(vq->log_ctx, 1); | |
3a4d5c94 | 1075 | return 0; |
5786aee8 | 1076 | } |
3a4d5c94 | 1077 | } |
3a4d5c94 MT |
1078 | /* Length written exceeds what we have stored. This is a bug. */ |
1079 | BUG(); | |
1080 | return 0; | |
1081 | } | |
6ac1afbf | 1082 | EXPORT_SYMBOL_GPL(vhost_log_write); |
3a4d5c94 | 1083 | |
2723feaa JW |
1084 | static int vhost_update_used_flags(struct vhost_virtqueue *vq) |
1085 | { | |
1086 | void __user *used; | |
3b1bbe89 | 1087 | if (__put_user(cpu_to_vhost16(vq, vq->used_flags), &vq->used->flags) < 0) |
2723feaa JW |
1088 | return -EFAULT; |
1089 | if (unlikely(vq->log_used)) { | |
1090 | /* Make sure the flag is seen before log. */ | |
1091 | smp_wmb(); | |
1092 | /* Log used flag write. */ | |
1093 | used = &vq->used->flags; | |
1094 | log_write(vq->log_base, vq->log_addr + | |
1095 | (used - (void __user *)vq->used), | |
1096 | sizeof vq->used->flags); | |
1097 | if (vq->log_ctx) | |
1098 | eventfd_signal(vq->log_ctx, 1); | |
1099 | } | |
1100 | return 0; | |
1101 | } | |
1102 | ||
1103 | static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event) | |
1104 | { | |
3b1bbe89 | 1105 | if (__put_user(cpu_to_vhost16(vq, vq->avail_idx), vhost_avail_event(vq))) |
2723feaa JW |
1106 | return -EFAULT; |
1107 | if (unlikely(vq->log_used)) { | |
1108 | void __user *used; | |
1109 | /* Make sure the event is seen before log. */ | |
1110 | smp_wmb(); | |
1111 | /* Log avail event write */ | |
1112 | used = vhost_avail_event(vq); | |
1113 | log_write(vq->log_base, vq->log_addr + | |
1114 | (used - (void __user *)vq->used), | |
1115 | sizeof *vhost_avail_event(vq)); | |
1116 | if (vq->log_ctx) | |
1117 | eventfd_signal(vq->log_ctx, 1); | |
1118 | } | |
1119 | return 0; | |
1120 | } | |
1121 | ||
1122 | int vhost_init_used(struct vhost_virtqueue *vq) | |
1123 | { | |
3b1bbe89 | 1124 | __virtio16 last_used_idx; |
2723feaa | 1125 | int r; |
2751c988 GK |
1126 | if (!vq->private_data) { |
1127 | vq->is_le = virtio_legacy_is_little_endian(); | |
2723feaa | 1128 | return 0; |
2751c988 GK |
1129 | } |
1130 | ||
1131 | vhost_init_is_le(vq); | |
2723feaa JW |
1132 | |
1133 | r = vhost_update_used_flags(vq); | |
1134 | if (r) | |
1135 | return r; | |
1136 | vq->signalled_used_valid = false; | |
64f7f051 MT |
1137 | if (!access_ok(VERIFY_READ, &vq->used->idx, sizeof vq->used->idx)) |
1138 | return -EFAULT; | |
1139 | r = __get_user(last_used_idx, &vq->used->idx); | |
1140 | if (r) | |
1141 | return r; | |
3b1bbe89 | 1142 | vq->last_used_idx = vhost16_to_cpu(vq, last_used_idx); |
64f7f051 | 1143 | return 0; |
2723feaa | 1144 | } |
6ac1afbf | 1145 | EXPORT_SYMBOL_GPL(vhost_init_used); |
2723feaa | 1146 | |
47283bef | 1147 | static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len, |
a8d3782f | 1148 | struct iovec iov[], int iov_size) |
3a4d5c94 MT |
1149 | { |
1150 | const struct vhost_memory_region *reg; | |
1151 | struct vhost_memory *mem; | |
1152 | struct iovec *_iov; | |
1153 | u64 s = 0; | |
1154 | int ret = 0; | |
1155 | ||
47283bef | 1156 | mem = vq->memory; |
3a4d5c94 MT |
1157 | while ((u64)len > s) { |
1158 | u64 size; | |
7b3384fc | 1159 | if (unlikely(ret >= iov_size)) { |
3a4d5c94 MT |
1160 | ret = -ENOBUFS; |
1161 | break; | |
1162 | } | |
1163 | reg = find_region(mem, addr, len); | |
7b3384fc | 1164 | if (unlikely(!reg)) { |
3a4d5c94 MT |
1165 | ret = -EFAULT; |
1166 | break; | |
1167 | } | |
1168 | _iov = iov + ret; | |
1169 | size = reg->memory_size - addr + reg->guest_phys_addr; | |
bd97120f | 1170 | _iov->iov_len = min((u64)len - s, size); |
a8d3782f | 1171 | _iov->iov_base = (void __user *)(unsigned long) |
3a4d5c94 MT |
1172 | (reg->userspace_addr + addr - reg->guest_phys_addr); |
1173 | s += size; | |
1174 | addr += size; | |
1175 | ++ret; | |
1176 | } | |
1177 | ||
3a4d5c94 MT |
1178 | return ret; |
1179 | } | |
1180 | ||
1181 | /* Each buffer in the virtqueues is actually a chain of descriptors. This | |
1182 | * function returns the next descriptor in the chain, | |
1183 | * or -1U if we're at the end. */ | |
3b1bbe89 | 1184 | static unsigned next_desc(struct vhost_virtqueue *vq, struct vring_desc *desc) |
3a4d5c94 MT |
1185 | { |
1186 | unsigned int next; | |
1187 | ||
1188 | /* If this descriptor says it doesn't chain, we're done. */ | |
3b1bbe89 | 1189 | if (!(desc->flags & cpu_to_vhost16(vq, VRING_DESC_F_NEXT))) |
3a4d5c94 MT |
1190 | return -1U; |
1191 | ||
1192 | /* Check they're not leading us off end of descriptors. */ | |
3b1bbe89 | 1193 | next = vhost16_to_cpu(vq, desc->next); |
3a4d5c94 MT |
1194 | /* Make sure compiler knows to grab that: we don't want it changing! */ |
1195 | /* We will use the result as an index in an array, so most | |
1196 | * architectures only need a compiler barrier here. */ | |
1197 | read_barrier_depends(); | |
1198 | ||
1199 | return next; | |
1200 | } | |
1201 | ||
47283bef | 1202 | static int get_indirect(struct vhost_virtqueue *vq, |
7b3384fc MT |
1203 | struct iovec iov[], unsigned int iov_size, |
1204 | unsigned int *out_num, unsigned int *in_num, | |
1205 | struct vhost_log *log, unsigned int *log_num, | |
1206 | struct vring_desc *indirect) | |
3a4d5c94 MT |
1207 | { |
1208 | struct vring_desc desc; | |
1209 | unsigned int i = 0, count, found = 0; | |
3b1bbe89 | 1210 | u32 len = vhost32_to_cpu(vq, indirect->len); |
aad9a1ce | 1211 | struct iov_iter from; |
3a4d5c94 MT |
1212 | int ret; |
1213 | ||
1214 | /* Sanity check */ | |
3b1bbe89 | 1215 | if (unlikely(len % sizeof desc)) { |
3a4d5c94 MT |
1216 | vq_err(vq, "Invalid length in indirect descriptor: " |
1217 | "len 0x%llx not multiple of 0x%zx\n", | |
3b1bbe89 | 1218 | (unsigned long long)len, |
3a4d5c94 MT |
1219 | sizeof desc); |
1220 | return -EINVAL; | |
1221 | } | |
1222 | ||
3b1bbe89 | 1223 | ret = translate_desc(vq, vhost64_to_cpu(vq, indirect->addr), len, vq->indirect, |
e0e9b406 | 1224 | UIO_MAXIOV); |
7b3384fc | 1225 | if (unlikely(ret < 0)) { |
3a4d5c94 MT |
1226 | vq_err(vq, "Translation failure %d in indirect.\n", ret); |
1227 | return ret; | |
1228 | } | |
aad9a1ce | 1229 | iov_iter_init(&from, READ, vq->indirect, ret, len); |
3a4d5c94 MT |
1230 | |
1231 | /* We will use the result as an address to read from, so most | |
1232 | * architectures only need a compiler barrier here. */ | |
1233 | read_barrier_depends(); | |
1234 | ||
3b1bbe89 | 1235 | count = len / sizeof desc; |
3a4d5c94 MT |
1236 | /* Buffers are chained via a 16 bit next field, so |
1237 | * we can have at most 2^16 of these. */ | |
7b3384fc | 1238 | if (unlikely(count > USHRT_MAX + 1)) { |
3a4d5c94 MT |
1239 | vq_err(vq, "Indirect buffer length too big: %d\n", |
1240 | indirect->len); | |
1241 | return -E2BIG; | |
1242 | } | |
1243 | ||
1244 | do { | |
1245 | unsigned iov_count = *in_num + *out_num; | |
7b3384fc | 1246 | if (unlikely(++found > count)) { |
3a4d5c94 MT |
1247 | vq_err(vq, "Loop detected: last one at %u " |
1248 | "indirect size %u\n", | |
1249 | i, count); | |
1250 | return -EINVAL; | |
1251 | } | |
aad9a1ce AV |
1252 | if (unlikely(copy_from_iter(&desc, sizeof(desc), &from) != |
1253 | sizeof(desc))) { | |
3a4d5c94 | 1254 | vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n", |
3b1bbe89 | 1255 | i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc); |
3a4d5c94 MT |
1256 | return -EINVAL; |
1257 | } | |
3b1bbe89 | 1258 | if (unlikely(desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT))) { |
3a4d5c94 | 1259 | vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n", |
3b1bbe89 | 1260 | i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc); |
3a4d5c94 MT |
1261 | return -EINVAL; |
1262 | } | |
1263 | ||
3b1bbe89 MT |
1264 | ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr), |
1265 | vhost32_to_cpu(vq, desc.len), iov + iov_count, | |
3a4d5c94 | 1266 | iov_size - iov_count); |
7b3384fc | 1267 | if (unlikely(ret < 0)) { |
3a4d5c94 MT |
1268 | vq_err(vq, "Translation failure %d indirect idx %d\n", |
1269 | ret, i); | |
1270 | return ret; | |
1271 | } | |
1272 | /* If this is an input descriptor, increment that count. */ | |
3b1bbe89 | 1273 | if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE)) { |
3a4d5c94 MT |
1274 | *in_num += ret; |
1275 | if (unlikely(log)) { | |
3b1bbe89 MT |
1276 | log[*log_num].addr = vhost64_to_cpu(vq, desc.addr); |
1277 | log[*log_num].len = vhost32_to_cpu(vq, desc.len); | |
3a4d5c94 MT |
1278 | ++*log_num; |
1279 | } | |
1280 | } else { | |
1281 | /* If it's an output descriptor, they're all supposed | |
1282 | * to come before any input descriptors. */ | |
7b3384fc | 1283 | if (unlikely(*in_num)) { |
3a4d5c94 MT |
1284 | vq_err(vq, "Indirect descriptor " |
1285 | "has out after in: idx %d\n", i); | |
1286 | return -EINVAL; | |
1287 | } | |
1288 | *out_num += ret; | |
1289 | } | |
3b1bbe89 | 1290 | } while ((i = next_desc(vq, &desc)) != -1); |
3a4d5c94 MT |
1291 | return 0; |
1292 | } | |
1293 | ||
1294 | /* This looks in the virtqueue and for the first available buffer, and converts | |
1295 | * it to an iovec for convenient access. Since descriptors consist of some | |
1296 | * number of output then some number of input descriptors, it's actually two | |
1297 | * iovecs, but we pack them into one and note how many of each there were. | |
1298 | * | |
d5675bd2 MT |
1299 | * This function returns the descriptor number found, or vq->num (which is |
1300 | * never a valid descriptor number) if none was found. A negative code is | |
1301 | * returned on error. */ | |
47283bef | 1302 | int vhost_get_vq_desc(struct vhost_virtqueue *vq, |
d5675bd2 MT |
1303 | struct iovec iov[], unsigned int iov_size, |
1304 | unsigned int *out_num, unsigned int *in_num, | |
1305 | struct vhost_log *log, unsigned int *log_num) | |
3a4d5c94 MT |
1306 | { |
1307 | struct vring_desc desc; | |
1308 | unsigned int i, head, found = 0; | |
1309 | u16 last_avail_idx; | |
3b1bbe89 MT |
1310 | __virtio16 avail_idx; |
1311 | __virtio16 ring_head; | |
3a4d5c94 MT |
1312 | int ret; |
1313 | ||
1314 | /* Check it isn't doing very strange things with descriptor numbers. */ | |
1315 | last_avail_idx = vq->last_avail_idx; | |
3b1bbe89 | 1316 | if (unlikely(__get_user(avail_idx, &vq->avail->idx))) { |
3a4d5c94 MT |
1317 | vq_err(vq, "Failed to access avail idx at %p\n", |
1318 | &vq->avail->idx); | |
d5675bd2 | 1319 | return -EFAULT; |
3a4d5c94 | 1320 | } |
3b1bbe89 | 1321 | vq->avail_idx = vhost16_to_cpu(vq, avail_idx); |
3a4d5c94 | 1322 | |
7b3384fc | 1323 | if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) { |
3a4d5c94 MT |
1324 | vq_err(vq, "Guest moved used index from %u to %u", |
1325 | last_avail_idx, vq->avail_idx); | |
d5675bd2 | 1326 | return -EFAULT; |
3a4d5c94 MT |
1327 | } |
1328 | ||
1329 | /* If there's nothing new since last we looked, return invalid. */ | |
1330 | if (vq->avail_idx == last_avail_idx) | |
1331 | return vq->num; | |
1332 | ||
1333 | /* Only get avail ring entries after they have been exposed by guest. */ | |
5659338c | 1334 | smp_rmb(); |
3a4d5c94 MT |
1335 | |
1336 | /* Grab the next descriptor number they're advertising, and increment | |
1337 | * the index we've seen. */ | |
3b1bbe89 | 1338 | if (unlikely(__get_user(ring_head, |
8b7347aa | 1339 | &vq->avail->ring[last_avail_idx % vq->num]))) { |
3a4d5c94 MT |
1340 | vq_err(vq, "Failed to read head: idx %d address %p\n", |
1341 | last_avail_idx, | |
1342 | &vq->avail->ring[last_avail_idx % vq->num]); | |
d5675bd2 | 1343 | return -EFAULT; |
3a4d5c94 MT |
1344 | } |
1345 | ||
3b1bbe89 MT |
1346 | head = vhost16_to_cpu(vq, ring_head); |
1347 | ||
3a4d5c94 | 1348 | /* If their number is silly, that's an error. */ |
7b3384fc | 1349 | if (unlikely(head >= vq->num)) { |
3a4d5c94 MT |
1350 | vq_err(vq, "Guest says index %u > %u is available", |
1351 | head, vq->num); | |
d5675bd2 | 1352 | return -EINVAL; |
3a4d5c94 MT |
1353 | } |
1354 | ||
1355 | /* When we start there are none of either input nor output. */ | |
1356 | *out_num = *in_num = 0; | |
1357 | if (unlikely(log)) | |
1358 | *log_num = 0; | |
1359 | ||
1360 | i = head; | |
1361 | do { | |
1362 | unsigned iov_count = *in_num + *out_num; | |
7b3384fc | 1363 | if (unlikely(i >= vq->num)) { |
3a4d5c94 MT |
1364 | vq_err(vq, "Desc index is %u > %u, head = %u", |
1365 | i, vq->num, head); | |
d5675bd2 | 1366 | return -EINVAL; |
3a4d5c94 | 1367 | } |
7b3384fc | 1368 | if (unlikely(++found > vq->num)) { |
3a4d5c94 MT |
1369 | vq_err(vq, "Loop detected: last one at %u " |
1370 | "vq size %u head %u\n", | |
1371 | i, vq->num, head); | |
d5675bd2 | 1372 | return -EINVAL; |
3a4d5c94 | 1373 | } |
fcc042a2 | 1374 | ret = __copy_from_user(&desc, vq->desc + i, sizeof desc); |
7b3384fc | 1375 | if (unlikely(ret)) { |
3a4d5c94 MT |
1376 | vq_err(vq, "Failed to get descriptor: idx %d addr %p\n", |
1377 | i, vq->desc + i); | |
d5675bd2 | 1378 | return -EFAULT; |
3a4d5c94 | 1379 | } |
3b1bbe89 | 1380 | if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT)) { |
47283bef | 1381 | ret = get_indirect(vq, iov, iov_size, |
3a4d5c94 MT |
1382 | out_num, in_num, |
1383 | log, log_num, &desc); | |
7b3384fc | 1384 | if (unlikely(ret < 0)) { |
3a4d5c94 MT |
1385 | vq_err(vq, "Failure detected " |
1386 | "in indirect descriptor at idx %d\n", i); | |
d5675bd2 | 1387 | return ret; |
3a4d5c94 MT |
1388 | } |
1389 | continue; | |
1390 | } | |
1391 | ||
3b1bbe89 MT |
1392 | ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr), |
1393 | vhost32_to_cpu(vq, desc.len), iov + iov_count, | |
3a4d5c94 | 1394 | iov_size - iov_count); |
7b3384fc | 1395 | if (unlikely(ret < 0)) { |
3a4d5c94 MT |
1396 | vq_err(vq, "Translation failure %d descriptor idx %d\n", |
1397 | ret, i); | |
d5675bd2 | 1398 | return ret; |
3a4d5c94 | 1399 | } |
3b1bbe89 | 1400 | if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE)) { |
3a4d5c94 MT |
1401 | /* If this is an input descriptor, |
1402 | * increment that count. */ | |
1403 | *in_num += ret; | |
1404 | if (unlikely(log)) { | |
3b1bbe89 MT |
1405 | log[*log_num].addr = vhost64_to_cpu(vq, desc.addr); |
1406 | log[*log_num].len = vhost32_to_cpu(vq, desc.len); | |
3a4d5c94 MT |
1407 | ++*log_num; |
1408 | } | |
1409 | } else { | |
1410 | /* If it's an output descriptor, they're all supposed | |
1411 | * to come before any input descriptors. */ | |
7b3384fc | 1412 | if (unlikely(*in_num)) { |
3a4d5c94 MT |
1413 | vq_err(vq, "Descriptor has out after in: " |
1414 | "idx %d\n", i); | |
d5675bd2 | 1415 | return -EINVAL; |
3a4d5c94 MT |
1416 | } |
1417 | *out_num += ret; | |
1418 | } | |
3b1bbe89 | 1419 | } while ((i = next_desc(vq, &desc)) != -1); |
3a4d5c94 MT |
1420 | |
1421 | /* On success, increment avail index. */ | |
1422 | vq->last_avail_idx++; | |
8ea8cf89 MT |
1423 | |
1424 | /* Assume notifications from guest are disabled at this point, | |
1425 | * if they aren't we would need to update avail_event index. */ | |
1426 | BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY)); | |
3a4d5c94 MT |
1427 | return head; |
1428 | } | |
6ac1afbf | 1429 | EXPORT_SYMBOL_GPL(vhost_get_vq_desc); |
3a4d5c94 MT |
1430 | |
1431 | /* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */ | |
8dd014ad | 1432 | void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n) |
3a4d5c94 | 1433 | { |
8dd014ad | 1434 | vq->last_avail_idx -= n; |
3a4d5c94 | 1435 | } |
6ac1afbf | 1436 | EXPORT_SYMBOL_GPL(vhost_discard_vq_desc); |
3a4d5c94 MT |
1437 | |
1438 | /* After we've used one of their buffers, we tell them about it. We'll then | |
1439 | * want to notify the guest, using eventfd. */ | |
1440 | int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len) | |
1441 | { | |
3b1bbe89 MT |
1442 | struct vring_used_elem heads = { |
1443 | cpu_to_vhost32(vq, head), | |
1444 | cpu_to_vhost32(vq, len) | |
1445 | }; | |
3a4d5c94 | 1446 | |
c49e4e57 | 1447 | return vhost_add_used_n(vq, &heads, 1); |
3a4d5c94 | 1448 | } |
6ac1afbf | 1449 | EXPORT_SYMBOL_GPL(vhost_add_used); |
3a4d5c94 | 1450 | |
8dd014ad DS |
1451 | static int __vhost_add_used_n(struct vhost_virtqueue *vq, |
1452 | struct vring_used_elem *heads, | |
1453 | unsigned count) | |
1454 | { | |
1455 | struct vring_used_elem __user *used; | |
8ea8cf89 | 1456 | u16 old, new; |
8dd014ad DS |
1457 | int start; |
1458 | ||
1459 | start = vq->last_used_idx % vq->num; | |
1460 | used = vq->used->ring + start; | |
c49e4e57 JW |
1461 | if (count == 1) { |
1462 | if (__put_user(heads[0].id, &used->id)) { | |
1463 | vq_err(vq, "Failed to write used id"); | |
1464 | return -EFAULT; | |
1465 | } | |
1466 | if (__put_user(heads[0].len, &used->len)) { | |
1467 | vq_err(vq, "Failed to write used len"); | |
1468 | return -EFAULT; | |
1469 | } | |
1470 | } else if (__copy_to_user(used, heads, count * sizeof *used)) { | |
8dd014ad DS |
1471 | vq_err(vq, "Failed to write used"); |
1472 | return -EFAULT; | |
1473 | } | |
1474 | if (unlikely(vq->log_used)) { | |
1475 | /* Make sure data is seen before log. */ | |
1476 | smp_wmb(); | |
1477 | /* Log used ring entry write. */ | |
1478 | log_write(vq->log_base, | |
1479 | vq->log_addr + | |
1480 | ((void __user *)used - (void __user *)vq->used), | |
1481 | count * sizeof *used); | |
1482 | } | |
8ea8cf89 MT |
1483 | old = vq->last_used_idx; |
1484 | new = (vq->last_used_idx += count); | |
1485 | /* If the driver never bothers to signal in a very long while, | |
1486 | * used index might wrap around. If that happens, invalidate | |
1487 | * signalled_used index we stored. TODO: make sure driver | |
1488 | * signals at least once in 2^16 and remove this. */ | |
1489 | if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old))) | |
1490 | vq->signalled_used_valid = false; | |
8dd014ad DS |
1491 | return 0; |
1492 | } | |
1493 | ||
1494 | /* After we've used one of their buffers, we tell them about it. We'll then | |
1495 | * want to notify the guest, using eventfd. */ | |
1496 | int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads, | |
1497 | unsigned count) | |
1498 | { | |
1499 | int start, n, r; | |
1500 | ||
1501 | start = vq->last_used_idx % vq->num; | |
1502 | n = vq->num - start; | |
1503 | if (n < count) { | |
1504 | r = __vhost_add_used_n(vq, heads, n); | |
1505 | if (r < 0) | |
1506 | return r; | |
1507 | heads += n; | |
1508 | count -= n; | |
1509 | } | |
1510 | r = __vhost_add_used_n(vq, heads, count); | |
1511 | ||
1512 | /* Make sure buffer is written before we update index. */ | |
1513 | smp_wmb(); | |
3b1bbe89 | 1514 | if (__put_user(cpu_to_vhost16(vq, vq->last_used_idx), &vq->used->idx)) { |
8dd014ad DS |
1515 | vq_err(vq, "Failed to increment used idx"); |
1516 | return -EFAULT; | |
1517 | } | |
1518 | if (unlikely(vq->log_used)) { | |
1519 | /* Log used index update. */ | |
1520 | log_write(vq->log_base, | |
1521 | vq->log_addr + offsetof(struct vring_used, idx), | |
1522 | sizeof vq->used->idx); | |
1523 | if (vq->log_ctx) | |
1524 | eventfd_signal(vq->log_ctx, 1); | |
1525 | } | |
1526 | return r; | |
1527 | } | |
6ac1afbf | 1528 | EXPORT_SYMBOL_GPL(vhost_add_used_n); |
8dd014ad | 1529 | |
8ea8cf89 | 1530 | static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq) |
3a4d5c94 | 1531 | { |
3b1bbe89 MT |
1532 | __u16 old, new; |
1533 | __virtio16 event; | |
8ea8cf89 | 1534 | bool v; |
0d499356 MT |
1535 | /* Flush out used index updates. This is paired |
1536 | * with the barrier that the Guest executes when enabling | |
1537 | * interrupts. */ | |
1538 | smp_mb(); | |
1539 | ||
ea16c514 | 1540 | if (vhost_has_feature(vq, VIRTIO_F_NOTIFY_ON_EMPTY) && |
8ea8cf89 MT |
1541 | unlikely(vq->avail_idx == vq->last_avail_idx)) |
1542 | return true; | |
1543 | ||
ea16c514 | 1544 | if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) { |
3b1bbe89 | 1545 | __virtio16 flags; |
8ea8cf89 MT |
1546 | if (__get_user(flags, &vq->avail->flags)) { |
1547 | vq_err(vq, "Failed to get flags"); | |
1548 | return true; | |
1549 | } | |
3b1bbe89 | 1550 | return !(flags & cpu_to_vhost16(vq, VRING_AVAIL_F_NO_INTERRUPT)); |
3a4d5c94 | 1551 | } |
8ea8cf89 MT |
1552 | old = vq->signalled_used; |
1553 | v = vq->signalled_used_valid; | |
1554 | new = vq->signalled_used = vq->last_used_idx; | |
1555 | vq->signalled_used_valid = true; | |
3a4d5c94 | 1556 | |
8ea8cf89 MT |
1557 | if (unlikely(!v)) |
1558 | return true; | |
3a4d5c94 | 1559 | |
64f7f051 | 1560 | if (__get_user(event, vhost_used_event(vq))) { |
8ea8cf89 MT |
1561 | vq_err(vq, "Failed to get used event idx"); |
1562 | return true; | |
1563 | } | |
3b1bbe89 | 1564 | return vring_need_event(vhost16_to_cpu(vq, event), new, old); |
8ea8cf89 MT |
1565 | } |
1566 | ||
1567 | /* This actually signals the guest, using eventfd. */ | |
1568 | void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq) | |
1569 | { | |
3a4d5c94 | 1570 | /* Signal the Guest tell them we used something up. */ |
8ea8cf89 | 1571 | if (vq->call_ctx && vhost_notify(dev, vq)) |
3a4d5c94 MT |
1572 | eventfd_signal(vq->call_ctx, 1); |
1573 | } | |
6ac1afbf | 1574 | EXPORT_SYMBOL_GPL(vhost_signal); |
3a4d5c94 MT |
1575 | |
1576 | /* And here's the combo meal deal. Supersize me! */ | |
1577 | void vhost_add_used_and_signal(struct vhost_dev *dev, | |
1578 | struct vhost_virtqueue *vq, | |
1579 | unsigned int head, int len) | |
1580 | { | |
1581 | vhost_add_used(vq, head, len); | |
1582 | vhost_signal(dev, vq); | |
1583 | } | |
6ac1afbf | 1584 | EXPORT_SYMBOL_GPL(vhost_add_used_and_signal); |
3a4d5c94 | 1585 | |
8dd014ad DS |
1586 | /* multi-buffer version of vhost_add_used_and_signal */ |
1587 | void vhost_add_used_and_signal_n(struct vhost_dev *dev, | |
1588 | struct vhost_virtqueue *vq, | |
1589 | struct vring_used_elem *heads, unsigned count) | |
1590 | { | |
1591 | vhost_add_used_n(vq, heads, count); | |
1592 | vhost_signal(dev, vq); | |
1593 | } | |
6ac1afbf | 1594 | EXPORT_SYMBOL_GPL(vhost_add_used_and_signal_n); |
8dd014ad | 1595 | |
3a4d5c94 | 1596 | /* OK, now we need to know about added descriptors. */ |
8ea8cf89 | 1597 | bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq) |
3a4d5c94 | 1598 | { |
3b1bbe89 | 1599 | __virtio16 avail_idx; |
3a4d5c94 | 1600 | int r; |
d47effe1 | 1601 | |
3a4d5c94 MT |
1602 | if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY)) |
1603 | return false; | |
1604 | vq->used_flags &= ~VRING_USED_F_NO_NOTIFY; | |
ea16c514 | 1605 | if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) { |
2723feaa | 1606 | r = vhost_update_used_flags(vq); |
8ea8cf89 MT |
1607 | if (r) { |
1608 | vq_err(vq, "Failed to enable notification at %p: %d\n", | |
1609 | &vq->used->flags, r); | |
1610 | return false; | |
1611 | } | |
1612 | } else { | |
2723feaa | 1613 | r = vhost_update_avail_event(vq, vq->avail_idx); |
8ea8cf89 MT |
1614 | if (r) { |
1615 | vq_err(vq, "Failed to update avail event index at %p: %d\n", | |
1616 | vhost_avail_event(vq), r); | |
1617 | return false; | |
1618 | } | |
1619 | } | |
3a4d5c94 MT |
1620 | /* They could have slipped one in as we were doing that: make |
1621 | * sure it's written, then check again. */ | |
5659338c | 1622 | smp_mb(); |
8b7347aa | 1623 | r = __get_user(avail_idx, &vq->avail->idx); |
3a4d5c94 MT |
1624 | if (r) { |
1625 | vq_err(vq, "Failed to check avail idx at %p: %d\n", | |
1626 | &vq->avail->idx, r); | |
1627 | return false; | |
1628 | } | |
1629 | ||
3b1bbe89 | 1630 | return vhost16_to_cpu(vq, avail_idx) != vq->avail_idx; |
3a4d5c94 | 1631 | } |
6ac1afbf | 1632 | EXPORT_SYMBOL_GPL(vhost_enable_notify); |
3a4d5c94 MT |
1633 | |
1634 | /* We don't need to be notified again. */ | |
8ea8cf89 | 1635 | void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq) |
3a4d5c94 MT |
1636 | { |
1637 | int r; | |
d47effe1 | 1638 | |
3a4d5c94 MT |
1639 | if (vq->used_flags & VRING_USED_F_NO_NOTIFY) |
1640 | return; | |
1641 | vq->used_flags |= VRING_USED_F_NO_NOTIFY; | |
ea16c514 | 1642 | if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) { |
2723feaa | 1643 | r = vhost_update_used_flags(vq); |
8ea8cf89 MT |
1644 | if (r) |
1645 | vq_err(vq, "Failed to enable notification at %p: %d\n", | |
1646 | &vq->used->flags, r); | |
1647 | } | |
3a4d5c94 | 1648 | } |
6ac1afbf AH |
1649 | EXPORT_SYMBOL_GPL(vhost_disable_notify); |
1650 | ||
1651 | static int __init vhost_init(void) | |
1652 | { | |
1653 | return 0; | |
1654 | } | |
1655 | ||
1656 | static void __exit vhost_exit(void) | |
1657 | { | |
1658 | } | |
1659 | ||
1660 | module_init(vhost_init); | |
1661 | module_exit(vhost_exit); | |
1662 | ||
1663 | MODULE_VERSION("0.0.1"); | |
1664 | MODULE_LICENSE("GPL v2"); | |
1665 | MODULE_AUTHOR("Michael S. Tsirkin"); | |
1666 | MODULE_DESCRIPTION("Host kernel accelerator for virtio"); |