]>
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 | |
7 | * Documentation/lguest/lguest.c, by Rusty Russell | |
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> | |
16 | #include <linux/virtio_net.h> | |
17 | #include <linux/mm.h> | |
18 | #include <linux/miscdevice.h> | |
19 | #include <linux/mutex.h> | |
3a4d5c94 MT |
20 | #include <linux/rcupdate.h> |
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> |
3a4d5c94 MT |
27 | |
28 | #include <linux/net.h> | |
29 | #include <linux/if_packet.h> | |
30 | #include <linux/if_arp.h> | |
31 | ||
32 | #include <net/sock.h> | |
33 | ||
34 | #include "vhost.h" | |
35 | ||
36 | enum { | |
37 | VHOST_MEMORY_MAX_NREGIONS = 64, | |
38 | VHOST_MEMORY_F_LOG = 0x1, | |
39 | }; | |
40 | ||
3a4d5c94 MT |
41 | static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh, |
42 | poll_table *pt) | |
43 | { | |
44 | struct vhost_poll *poll; | |
45 | poll = container_of(pt, struct vhost_poll, table); | |
46 | ||
47 | poll->wqh = wqh; | |
48 | add_wait_queue(wqh, &poll->wait); | |
49 | } | |
50 | ||
51 | static int vhost_poll_wakeup(wait_queue_t *wait, unsigned mode, int sync, | |
52 | void *key) | |
53 | { | |
c23f3445 TH |
54 | struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait); |
55 | ||
3a4d5c94 MT |
56 | if (!((unsigned long)key & poll->mask)) |
57 | return 0; | |
58 | ||
c23f3445 | 59 | vhost_poll_queue(poll); |
3a4d5c94 MT |
60 | return 0; |
61 | } | |
62 | ||
87d6a412 MT |
63 | static void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn) |
64 | { | |
65 | INIT_LIST_HEAD(&work->node); | |
66 | work->fn = fn; | |
67 | init_waitqueue_head(&work->done); | |
68 | work->flushing = 0; | |
69 | work->queue_seq = work->done_seq = 0; | |
70 | } | |
71 | ||
3a4d5c94 | 72 | /* Init poll structure */ |
c23f3445 TH |
73 | void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn, |
74 | unsigned long mask, struct vhost_dev *dev) | |
3a4d5c94 | 75 | { |
3a4d5c94 MT |
76 | init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup); |
77 | init_poll_funcptr(&poll->table, vhost_poll_func); | |
78 | poll->mask = mask; | |
c23f3445 TH |
79 | poll->dev = dev; |
80 | ||
87d6a412 | 81 | vhost_work_init(&poll->work, fn); |
3a4d5c94 MT |
82 | } |
83 | ||
84 | /* Start polling a file. We add ourselves to file's wait queue. The caller must | |
85 | * keep a reference to a file until after vhost_poll_stop is called. */ | |
86 | void vhost_poll_start(struct vhost_poll *poll, struct file *file) | |
87 | { | |
88 | unsigned long mask; | |
89 | mask = file->f_op->poll(file, &poll->table); | |
90 | if (mask) | |
91 | vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask); | |
92 | } | |
93 | ||
94 | /* Stop polling a file. After this function returns, it becomes safe to drop the | |
95 | * file reference. You must also flush afterwards. */ | |
96 | void vhost_poll_stop(struct vhost_poll *poll) | |
97 | { | |
98 | remove_wait_queue(poll->wqh, &poll->wait); | |
99 | } | |
100 | ||
87d6a412 | 101 | static void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work) |
3a4d5c94 | 102 | { |
c23f3445 TH |
103 | unsigned seq; |
104 | int left; | |
105 | int flushing; | |
106 | ||
87d6a412 | 107 | spin_lock_irq(&dev->work_lock); |
c23f3445 TH |
108 | seq = work->queue_seq; |
109 | work->flushing++; | |
87d6a412 | 110 | spin_unlock_irq(&dev->work_lock); |
c23f3445 | 111 | wait_event(work->done, ({ |
87d6a412 | 112 | spin_lock_irq(&dev->work_lock); |
c23f3445 | 113 | left = seq - work->done_seq <= 0; |
87d6a412 | 114 | spin_unlock_irq(&dev->work_lock); |
c23f3445 TH |
115 | left; |
116 | })); | |
87d6a412 | 117 | spin_lock_irq(&dev->work_lock); |
c23f3445 | 118 | flushing = --work->flushing; |
87d6a412 | 119 | spin_unlock_irq(&dev->work_lock); |
c23f3445 | 120 | BUG_ON(flushing < 0); |
3a4d5c94 MT |
121 | } |
122 | ||
87d6a412 MT |
123 | /* Flush any work that has been scheduled. When calling this, don't hold any |
124 | * locks that are also used by the callback. */ | |
125 | void vhost_poll_flush(struct vhost_poll *poll) | |
126 | { | |
127 | vhost_work_flush(poll->dev, &poll->work); | |
128 | } | |
129 | ||
130 | static inline void vhost_work_queue(struct vhost_dev *dev, | |
131 | struct vhost_work *work) | |
3a4d5c94 | 132 | { |
c23f3445 TH |
133 | unsigned long flags; |
134 | ||
135 | spin_lock_irqsave(&dev->work_lock, flags); | |
136 | if (list_empty(&work->node)) { | |
137 | list_add_tail(&work->node, &dev->work_list); | |
138 | work->queue_seq++; | |
139 | wake_up_process(dev->worker); | |
140 | } | |
141 | spin_unlock_irqrestore(&dev->work_lock, flags); | |
3a4d5c94 MT |
142 | } |
143 | ||
87d6a412 MT |
144 | void vhost_poll_queue(struct vhost_poll *poll) |
145 | { | |
146 | vhost_work_queue(poll->dev, &poll->work); | |
147 | } | |
148 | ||
3a4d5c94 MT |
149 | static void vhost_vq_reset(struct vhost_dev *dev, |
150 | struct vhost_virtqueue *vq) | |
151 | { | |
152 | vq->num = 1; | |
153 | vq->desc = NULL; | |
154 | vq->avail = NULL; | |
155 | vq->used = NULL; | |
156 | vq->last_avail_idx = 0; | |
157 | vq->avail_idx = 0; | |
158 | vq->last_used_idx = 0; | |
159 | vq->used_flags = 0; | |
160 | vq->used_flags = 0; | |
161 | vq->log_used = false; | |
162 | vq->log_addr = -1ull; | |
8dd014ad DS |
163 | vq->vhost_hlen = 0; |
164 | vq->sock_hlen = 0; | |
3a4d5c94 MT |
165 | vq->private_data = NULL; |
166 | vq->log_base = NULL; | |
167 | vq->error_ctx = NULL; | |
168 | vq->error = NULL; | |
169 | vq->kick = NULL; | |
170 | vq->call_ctx = NULL; | |
171 | vq->call = NULL; | |
73a99f08 | 172 | vq->log_ctx = NULL; |
3a4d5c94 MT |
173 | } |
174 | ||
c23f3445 TH |
175 | static int vhost_worker(void *data) |
176 | { | |
177 | struct vhost_dev *dev = data; | |
178 | struct vhost_work *work = NULL; | |
179 | unsigned uninitialized_var(seq); | |
180 | ||
181 | for (;;) { | |
182 | /* mb paired w/ kthread_stop */ | |
183 | set_current_state(TASK_INTERRUPTIBLE); | |
184 | ||
185 | spin_lock_irq(&dev->work_lock); | |
186 | if (work) { | |
187 | work->done_seq = seq; | |
188 | if (work->flushing) | |
189 | wake_up_all(&work->done); | |
190 | } | |
191 | ||
192 | if (kthread_should_stop()) { | |
193 | spin_unlock_irq(&dev->work_lock); | |
194 | __set_current_state(TASK_RUNNING); | |
195 | return 0; | |
196 | } | |
197 | if (!list_empty(&dev->work_list)) { | |
198 | work = list_first_entry(&dev->work_list, | |
199 | struct vhost_work, node); | |
200 | list_del_init(&work->node); | |
201 | seq = work->queue_seq; | |
202 | } else | |
203 | work = NULL; | |
204 | spin_unlock_irq(&dev->work_lock); | |
205 | ||
206 | if (work) { | |
207 | __set_current_state(TASK_RUNNING); | |
208 | work->fn(work); | |
209 | } else | |
210 | schedule(); | |
211 | ||
212 | } | |
213 | } | |
214 | ||
3a4d5c94 MT |
215 | long vhost_dev_init(struct vhost_dev *dev, |
216 | struct vhost_virtqueue *vqs, int nvqs) | |
217 | { | |
218 | int i; | |
c23f3445 | 219 | |
3a4d5c94 MT |
220 | dev->vqs = vqs; |
221 | dev->nvqs = nvqs; | |
222 | mutex_init(&dev->mutex); | |
223 | dev->log_ctx = NULL; | |
224 | dev->log_file = NULL; | |
225 | dev->memory = NULL; | |
226 | dev->mm = NULL; | |
c23f3445 TH |
227 | spin_lock_init(&dev->work_lock); |
228 | INIT_LIST_HEAD(&dev->work_list); | |
229 | dev->worker = NULL; | |
3a4d5c94 MT |
230 | |
231 | for (i = 0; i < dev->nvqs; ++i) { | |
232 | dev->vqs[i].dev = dev; | |
233 | mutex_init(&dev->vqs[i].mutex); | |
234 | vhost_vq_reset(dev, dev->vqs + i); | |
235 | if (dev->vqs[i].handle_kick) | |
236 | vhost_poll_init(&dev->vqs[i].poll, | |
c23f3445 | 237 | dev->vqs[i].handle_kick, POLLIN, dev); |
3a4d5c94 | 238 | } |
c23f3445 | 239 | |
3a4d5c94 MT |
240 | return 0; |
241 | } | |
242 | ||
243 | /* Caller should have device mutex */ | |
244 | long vhost_dev_check_owner(struct vhost_dev *dev) | |
245 | { | |
246 | /* Are you the owner? If not, I don't think you mean to do that */ | |
247 | return dev->mm == current->mm ? 0 : -EPERM; | |
248 | } | |
249 | ||
87d6a412 MT |
250 | struct vhost_attach_cgroups_struct { |
251 | struct vhost_work work; | |
252 | struct task_struct *owner; | |
253 | int ret; | |
254 | }; | |
255 | ||
256 | static void vhost_attach_cgroups_work(struct vhost_work *work) | |
257 | { | |
258 | struct vhost_attach_cgroups_struct *s; | |
259 | s = container_of(work, struct vhost_attach_cgroups_struct, work); | |
260 | s->ret = cgroup_attach_task_all(s->owner, current); | |
261 | } | |
262 | ||
263 | static int vhost_attach_cgroups(struct vhost_dev *dev) | |
264 | { | |
265 | struct vhost_attach_cgroups_struct attach; | |
266 | attach.owner = current; | |
267 | vhost_work_init(&attach.work, vhost_attach_cgroups_work); | |
268 | vhost_work_queue(dev, &attach.work); | |
269 | vhost_work_flush(dev, &attach.work); | |
270 | return attach.ret; | |
271 | } | |
272 | ||
3a4d5c94 MT |
273 | /* Caller should have device mutex */ |
274 | static long vhost_dev_set_owner(struct vhost_dev *dev) | |
275 | { | |
c23f3445 TH |
276 | struct task_struct *worker; |
277 | int err; | |
3a4d5c94 | 278 | /* Is there an owner already? */ |
c23f3445 TH |
279 | if (dev->mm) { |
280 | err = -EBUSY; | |
281 | goto err_mm; | |
282 | } | |
3a4d5c94 MT |
283 | /* No owner, become one */ |
284 | dev->mm = get_task_mm(current); | |
c23f3445 TH |
285 | worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid); |
286 | if (IS_ERR(worker)) { | |
287 | err = PTR_ERR(worker); | |
288 | goto err_worker; | |
289 | } | |
290 | ||
291 | dev->worker = worker; | |
87d6a412 MT |
292 | wake_up_process(worker); /* avoid contributing to loadavg */ |
293 | ||
294 | err = vhost_attach_cgroups(dev); | |
9e3d1957 MT |
295 | if (err) |
296 | goto err_cgroup; | |
c23f3445 | 297 | |
3a4d5c94 | 298 | return 0; |
9e3d1957 MT |
299 | err_cgroup: |
300 | kthread_stop(worker); | |
c23f3445 TH |
301 | err_worker: |
302 | if (dev->mm) | |
303 | mmput(dev->mm); | |
304 | dev->mm = NULL; | |
305 | err_mm: | |
306 | return err; | |
3a4d5c94 MT |
307 | } |
308 | ||
309 | /* Caller should have device mutex */ | |
310 | long vhost_dev_reset_owner(struct vhost_dev *dev) | |
311 | { | |
312 | struct vhost_memory *memory; | |
313 | ||
314 | /* Restore memory to default empty mapping. */ | |
315 | memory = kmalloc(offsetof(struct vhost_memory, regions), GFP_KERNEL); | |
316 | if (!memory) | |
317 | return -ENOMEM; | |
318 | ||
319 | vhost_dev_cleanup(dev); | |
320 | ||
321 | memory->nregions = 0; | |
322 | dev->memory = memory; | |
323 | return 0; | |
324 | } | |
325 | ||
326 | /* Caller should have device mutex */ | |
327 | void vhost_dev_cleanup(struct vhost_dev *dev) | |
328 | { | |
329 | int i; | |
330 | for (i = 0; i < dev->nvqs; ++i) { | |
331 | if (dev->vqs[i].kick && dev->vqs[i].handle_kick) { | |
332 | vhost_poll_stop(&dev->vqs[i].poll); | |
333 | vhost_poll_flush(&dev->vqs[i].poll); | |
334 | } | |
335 | if (dev->vqs[i].error_ctx) | |
336 | eventfd_ctx_put(dev->vqs[i].error_ctx); | |
337 | if (dev->vqs[i].error) | |
338 | fput(dev->vqs[i].error); | |
339 | if (dev->vqs[i].kick) | |
340 | fput(dev->vqs[i].kick); | |
341 | if (dev->vqs[i].call_ctx) | |
342 | eventfd_ctx_put(dev->vqs[i].call_ctx); | |
343 | if (dev->vqs[i].call) | |
344 | fput(dev->vqs[i].call); | |
345 | vhost_vq_reset(dev, dev->vqs + i); | |
346 | } | |
347 | if (dev->log_ctx) | |
348 | eventfd_ctx_put(dev->log_ctx); | |
349 | dev->log_ctx = NULL; | |
350 | if (dev->log_file) | |
351 | fput(dev->log_file); | |
352 | dev->log_file = NULL; | |
353 | /* No one will access memory at this point */ | |
354 | kfree(dev->memory); | |
355 | dev->memory = NULL; | |
356 | if (dev->mm) | |
357 | mmput(dev->mm); | |
358 | dev->mm = NULL; | |
c23f3445 TH |
359 | |
360 | WARN_ON(!list_empty(&dev->work_list)); | |
78b620ce ED |
361 | if (dev->worker) { |
362 | kthread_stop(dev->worker); | |
363 | dev->worker = NULL; | |
364 | } | |
3a4d5c94 MT |
365 | } |
366 | ||
367 | static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz) | |
368 | { | |
369 | u64 a = addr / VHOST_PAGE_SIZE / 8; | |
370 | /* Make sure 64 bit math will not overflow. */ | |
371 | if (a > ULONG_MAX - (unsigned long)log_base || | |
372 | a + (unsigned long)log_base > ULONG_MAX) | |
373 | return -EFAULT; | |
374 | ||
375 | return access_ok(VERIFY_WRITE, log_base + a, | |
376 | (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8); | |
377 | } | |
378 | ||
379 | /* Caller should have vq mutex and device mutex. */ | |
380 | static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem, | |
381 | int log_all) | |
382 | { | |
383 | int i; | |
179b284e | 384 | |
f8322fbe MT |
385 | if (!mem) |
386 | return 0; | |
179b284e | 387 | |
3a4d5c94 MT |
388 | for (i = 0; i < mem->nregions; ++i) { |
389 | struct vhost_memory_region *m = mem->regions + i; | |
390 | unsigned long a = m->userspace_addr; | |
391 | if (m->memory_size > ULONG_MAX) | |
392 | return 0; | |
393 | else if (!access_ok(VERIFY_WRITE, (void __user *)a, | |
394 | m->memory_size)) | |
395 | return 0; | |
396 | else if (log_all && !log_access_ok(log_base, | |
397 | m->guest_phys_addr, | |
398 | m->memory_size)) | |
399 | return 0; | |
400 | } | |
401 | return 1; | |
402 | } | |
403 | ||
404 | /* Can we switch to this memory table? */ | |
405 | /* Caller should have device mutex but not vq mutex */ | |
406 | static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem, | |
407 | int log_all) | |
408 | { | |
409 | int i; | |
410 | for (i = 0; i < d->nvqs; ++i) { | |
411 | int ok; | |
412 | mutex_lock(&d->vqs[i].mutex); | |
413 | /* If ring is inactive, will check when it's enabled. */ | |
414 | if (d->vqs[i].private_data) | |
415 | ok = vq_memory_access_ok(d->vqs[i].log_base, mem, | |
416 | log_all); | |
417 | else | |
418 | ok = 1; | |
419 | mutex_unlock(&d->vqs[i].mutex); | |
420 | if (!ok) | |
421 | return 0; | |
422 | } | |
423 | return 1; | |
424 | } | |
425 | ||
426 | static int vq_access_ok(unsigned int num, | |
427 | struct vring_desc __user *desc, | |
428 | struct vring_avail __user *avail, | |
429 | struct vring_used __user *used) | |
430 | { | |
431 | return access_ok(VERIFY_READ, desc, num * sizeof *desc) && | |
432 | access_ok(VERIFY_READ, avail, | |
433 | sizeof *avail + num * sizeof *avail->ring) && | |
434 | access_ok(VERIFY_WRITE, used, | |
435 | sizeof *used + num * sizeof *used->ring); | |
436 | } | |
437 | ||
438 | /* Can we log writes? */ | |
439 | /* Caller should have device mutex but not vq mutex */ | |
440 | int vhost_log_access_ok(struct vhost_dev *dev) | |
441 | { | |
442 | return memory_access_ok(dev, dev->memory, 1); | |
443 | } | |
444 | ||
445 | /* Verify access for write logging. */ | |
446 | /* Caller should have vq mutex and device mutex */ | |
447 | static int vq_log_access_ok(struct vhost_virtqueue *vq, void __user *log_base) | |
448 | { | |
449 | return vq_memory_access_ok(log_base, vq->dev->memory, | |
450 | vhost_has_feature(vq->dev, VHOST_F_LOG_ALL)) && | |
451 | (!vq->log_used || log_access_ok(log_base, vq->log_addr, | |
452 | sizeof *vq->used + | |
453 | vq->num * sizeof *vq->used->ring)); | |
454 | } | |
455 | ||
456 | /* Can we start vq? */ | |
457 | /* Caller should have vq mutex and device mutex */ | |
458 | int vhost_vq_access_ok(struct vhost_virtqueue *vq) | |
459 | { | |
460 | return vq_access_ok(vq->num, vq->desc, vq->avail, vq->used) && | |
461 | vq_log_access_ok(vq, vq->log_base); | |
462 | } | |
463 | ||
464 | static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m) | |
465 | { | |
466 | struct vhost_memory mem, *newmem, *oldmem; | |
467 | unsigned long size = offsetof(struct vhost_memory, regions); | |
7ad9c9d2 TY |
468 | if (copy_from_user(&mem, m, size)) |
469 | return -EFAULT; | |
3a4d5c94 MT |
470 | if (mem.padding) |
471 | return -EOPNOTSUPP; | |
472 | if (mem.nregions > VHOST_MEMORY_MAX_NREGIONS) | |
473 | return -E2BIG; | |
474 | newmem = kmalloc(size + mem.nregions * sizeof *m->regions, GFP_KERNEL); | |
475 | if (!newmem) | |
476 | return -ENOMEM; | |
477 | ||
478 | memcpy(newmem, &mem, size); | |
7ad9c9d2 TY |
479 | if (copy_from_user(newmem->regions, m->regions, |
480 | mem.nregions * sizeof *m->regions)) { | |
3a4d5c94 | 481 | kfree(newmem); |
7ad9c9d2 | 482 | return -EFAULT; |
3a4d5c94 MT |
483 | } |
484 | ||
a02c3789 TY |
485 | if (!memory_access_ok(d, newmem, vhost_has_feature(d, VHOST_F_LOG_ALL))) { |
486 | kfree(newmem); | |
3a4d5c94 | 487 | return -EFAULT; |
a02c3789 | 488 | } |
3a4d5c94 MT |
489 | oldmem = d->memory; |
490 | rcu_assign_pointer(d->memory, newmem); | |
491 | synchronize_rcu(); | |
492 | kfree(oldmem); | |
493 | return 0; | |
494 | } | |
495 | ||
496 | static int init_used(struct vhost_virtqueue *vq, | |
497 | struct vring_used __user *used) | |
498 | { | |
499 | int r = put_user(vq->used_flags, &used->flags); | |
500 | if (r) | |
501 | return r; | |
502 | return get_user(vq->last_used_idx, &used->idx); | |
503 | } | |
504 | ||
505 | static long vhost_set_vring(struct vhost_dev *d, int ioctl, void __user *argp) | |
506 | { | |
507 | struct file *eventfp, *filep = NULL, | |
508 | *pollstart = NULL, *pollstop = NULL; | |
509 | struct eventfd_ctx *ctx = NULL; | |
510 | u32 __user *idxp = argp; | |
511 | struct vhost_virtqueue *vq; | |
512 | struct vhost_vring_state s; | |
513 | struct vhost_vring_file f; | |
514 | struct vhost_vring_addr a; | |
515 | u32 idx; | |
516 | long r; | |
517 | ||
518 | r = get_user(idx, idxp); | |
519 | if (r < 0) | |
520 | return r; | |
0f3d9a17 | 521 | if (idx >= d->nvqs) |
3a4d5c94 MT |
522 | return -ENOBUFS; |
523 | ||
524 | vq = d->vqs + idx; | |
525 | ||
526 | mutex_lock(&vq->mutex); | |
527 | ||
528 | switch (ioctl) { | |
529 | case VHOST_SET_VRING_NUM: | |
530 | /* Resizing ring with an active backend? | |
531 | * You don't want to do that. */ | |
532 | if (vq->private_data) { | |
533 | r = -EBUSY; | |
534 | break; | |
535 | } | |
7ad9c9d2 TY |
536 | if (copy_from_user(&s, argp, sizeof s)) { |
537 | r = -EFAULT; | |
3a4d5c94 | 538 | break; |
7ad9c9d2 | 539 | } |
3a4d5c94 MT |
540 | if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) { |
541 | r = -EINVAL; | |
542 | break; | |
543 | } | |
544 | vq->num = s.num; | |
545 | break; | |
546 | case VHOST_SET_VRING_BASE: | |
547 | /* Moving base with an active backend? | |
548 | * You don't want to do that. */ | |
549 | if (vq->private_data) { | |
550 | r = -EBUSY; | |
551 | break; | |
552 | } | |
7ad9c9d2 TY |
553 | if (copy_from_user(&s, argp, sizeof s)) { |
554 | r = -EFAULT; | |
3a4d5c94 | 555 | break; |
7ad9c9d2 | 556 | } |
3a4d5c94 MT |
557 | if (s.num > 0xffff) { |
558 | r = -EINVAL; | |
559 | break; | |
560 | } | |
561 | vq->last_avail_idx = s.num; | |
562 | /* Forget the cached index value. */ | |
563 | vq->avail_idx = vq->last_avail_idx; | |
564 | break; | |
565 | case VHOST_GET_VRING_BASE: | |
566 | s.index = idx; | |
567 | s.num = vq->last_avail_idx; | |
7ad9c9d2 TY |
568 | if (copy_to_user(argp, &s, sizeof s)) |
569 | r = -EFAULT; | |
3a4d5c94 MT |
570 | break; |
571 | case VHOST_SET_VRING_ADDR: | |
7ad9c9d2 TY |
572 | if (copy_from_user(&a, argp, sizeof a)) { |
573 | r = -EFAULT; | |
3a4d5c94 | 574 | break; |
7ad9c9d2 | 575 | } |
3a4d5c94 MT |
576 | if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) { |
577 | r = -EOPNOTSUPP; | |
578 | break; | |
579 | } | |
580 | /* For 32bit, verify that the top 32bits of the user | |
581 | data are set to zero. */ | |
582 | if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr || | |
583 | (u64)(unsigned long)a.used_user_addr != a.used_user_addr || | |
584 | (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) { | |
585 | r = -EFAULT; | |
586 | break; | |
587 | } | |
588 | if ((a.avail_user_addr & (sizeof *vq->avail->ring - 1)) || | |
589 | (a.used_user_addr & (sizeof *vq->used->ring - 1)) || | |
590 | (a.log_guest_addr & (sizeof *vq->used->ring - 1))) { | |
591 | r = -EINVAL; | |
592 | break; | |
593 | } | |
594 | ||
595 | /* We only verify access here if backend is configured. | |
596 | * If it is not, we don't as size might not have been setup. | |
597 | * We will verify when backend is configured. */ | |
598 | if (vq->private_data) { | |
599 | if (!vq_access_ok(vq->num, | |
600 | (void __user *)(unsigned long)a.desc_user_addr, | |
601 | (void __user *)(unsigned long)a.avail_user_addr, | |
602 | (void __user *)(unsigned long)a.used_user_addr)) { | |
603 | r = -EINVAL; | |
604 | break; | |
605 | } | |
606 | ||
607 | /* Also validate log access for used ring if enabled. */ | |
608 | if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) && | |
609 | !log_access_ok(vq->log_base, a.log_guest_addr, | |
610 | sizeof *vq->used + | |
611 | vq->num * sizeof *vq->used->ring)) { | |
612 | r = -EINVAL; | |
613 | break; | |
614 | } | |
615 | } | |
616 | ||
617 | r = init_used(vq, (struct vring_used __user *)(unsigned long) | |
618 | a.used_user_addr); | |
619 | if (r) | |
620 | break; | |
621 | vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG)); | |
622 | vq->desc = (void __user *)(unsigned long)a.desc_user_addr; | |
623 | vq->avail = (void __user *)(unsigned long)a.avail_user_addr; | |
624 | vq->log_addr = a.log_guest_addr; | |
625 | vq->used = (void __user *)(unsigned long)a.used_user_addr; | |
626 | break; | |
627 | case VHOST_SET_VRING_KICK: | |
7ad9c9d2 TY |
628 | if (copy_from_user(&f, argp, sizeof f)) { |
629 | r = -EFAULT; | |
3a4d5c94 | 630 | break; |
7ad9c9d2 | 631 | } |
3a4d5c94 | 632 | eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd); |
535297a6 MT |
633 | if (IS_ERR(eventfp)) { |
634 | r = PTR_ERR(eventfp); | |
635 | break; | |
636 | } | |
3a4d5c94 MT |
637 | if (eventfp != vq->kick) { |
638 | pollstop = filep = vq->kick; | |
639 | pollstart = vq->kick = eventfp; | |
640 | } else | |
641 | filep = eventfp; | |
642 | break; | |
643 | case VHOST_SET_VRING_CALL: | |
7ad9c9d2 TY |
644 | if (copy_from_user(&f, argp, sizeof f)) { |
645 | r = -EFAULT; | |
3a4d5c94 | 646 | break; |
7ad9c9d2 | 647 | } |
3a4d5c94 | 648 | eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd); |
535297a6 MT |
649 | if (IS_ERR(eventfp)) { |
650 | r = PTR_ERR(eventfp); | |
651 | break; | |
652 | } | |
3a4d5c94 MT |
653 | if (eventfp != vq->call) { |
654 | filep = vq->call; | |
655 | ctx = vq->call_ctx; | |
656 | vq->call = eventfp; | |
657 | vq->call_ctx = eventfp ? | |
658 | eventfd_ctx_fileget(eventfp) : NULL; | |
659 | } else | |
660 | filep = eventfp; | |
661 | break; | |
662 | case VHOST_SET_VRING_ERR: | |
7ad9c9d2 TY |
663 | if (copy_from_user(&f, argp, sizeof f)) { |
664 | r = -EFAULT; | |
3a4d5c94 | 665 | break; |
7ad9c9d2 | 666 | } |
3a4d5c94 | 667 | eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd); |
535297a6 MT |
668 | if (IS_ERR(eventfp)) { |
669 | r = PTR_ERR(eventfp); | |
670 | break; | |
671 | } | |
3a4d5c94 MT |
672 | if (eventfp != vq->error) { |
673 | filep = vq->error; | |
674 | vq->error = eventfp; | |
675 | ctx = vq->error_ctx; | |
676 | vq->error_ctx = eventfp ? | |
677 | eventfd_ctx_fileget(eventfp) : NULL; | |
678 | } else | |
679 | filep = eventfp; | |
680 | break; | |
681 | default: | |
682 | r = -ENOIOCTLCMD; | |
683 | } | |
684 | ||
685 | if (pollstop && vq->handle_kick) | |
686 | vhost_poll_stop(&vq->poll); | |
687 | ||
688 | if (ctx) | |
689 | eventfd_ctx_put(ctx); | |
690 | if (filep) | |
691 | fput(filep); | |
692 | ||
693 | if (pollstart && vq->handle_kick) | |
694 | vhost_poll_start(&vq->poll, vq->kick); | |
695 | ||
696 | mutex_unlock(&vq->mutex); | |
697 | ||
698 | if (pollstop && vq->handle_kick) | |
699 | vhost_poll_flush(&vq->poll); | |
700 | return r; | |
701 | } | |
702 | ||
703 | /* Caller must have device mutex */ | |
704 | long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, unsigned long arg) | |
705 | { | |
706 | void __user *argp = (void __user *)arg; | |
707 | struct file *eventfp, *filep = NULL; | |
708 | struct eventfd_ctx *ctx = NULL; | |
709 | u64 p; | |
710 | long r; | |
711 | int i, fd; | |
712 | ||
713 | /* If you are not the owner, you can become one */ | |
714 | if (ioctl == VHOST_SET_OWNER) { | |
715 | r = vhost_dev_set_owner(d); | |
716 | goto done; | |
717 | } | |
718 | ||
719 | /* You must be the owner to do anything else */ | |
720 | r = vhost_dev_check_owner(d); | |
721 | if (r) | |
722 | goto done; | |
723 | ||
724 | switch (ioctl) { | |
725 | case VHOST_SET_MEM_TABLE: | |
726 | r = vhost_set_memory(d, argp); | |
727 | break; | |
728 | case VHOST_SET_LOG_BASE: | |
7ad9c9d2 TY |
729 | if (copy_from_user(&p, argp, sizeof p)) { |
730 | r = -EFAULT; | |
3a4d5c94 | 731 | break; |
7ad9c9d2 | 732 | } |
3a4d5c94 MT |
733 | if ((u64)(unsigned long)p != p) { |
734 | r = -EFAULT; | |
735 | break; | |
736 | } | |
737 | for (i = 0; i < d->nvqs; ++i) { | |
738 | struct vhost_virtqueue *vq; | |
739 | void __user *base = (void __user *)(unsigned long)p; | |
740 | vq = d->vqs + i; | |
741 | mutex_lock(&vq->mutex); | |
742 | /* If ring is inactive, will check when it's enabled. */ | |
743 | if (vq->private_data && !vq_log_access_ok(vq, base)) | |
744 | r = -EFAULT; | |
745 | else | |
746 | vq->log_base = base; | |
747 | mutex_unlock(&vq->mutex); | |
748 | } | |
749 | break; | |
750 | case VHOST_SET_LOG_FD: | |
751 | r = get_user(fd, (int __user *)argp); | |
752 | if (r < 0) | |
753 | break; | |
754 | eventfp = fd == -1 ? NULL : eventfd_fget(fd); | |
755 | if (IS_ERR(eventfp)) { | |
756 | r = PTR_ERR(eventfp); | |
757 | break; | |
758 | } | |
759 | if (eventfp != d->log_file) { | |
760 | filep = d->log_file; | |
761 | ctx = d->log_ctx; | |
762 | d->log_ctx = eventfp ? | |
763 | eventfd_ctx_fileget(eventfp) : NULL; | |
764 | } else | |
765 | filep = eventfp; | |
766 | for (i = 0; i < d->nvqs; ++i) { | |
767 | mutex_lock(&d->vqs[i].mutex); | |
768 | d->vqs[i].log_ctx = d->log_ctx; | |
769 | mutex_unlock(&d->vqs[i].mutex); | |
770 | } | |
771 | if (ctx) | |
772 | eventfd_ctx_put(ctx); | |
773 | if (filep) | |
774 | fput(filep); | |
775 | break; | |
776 | default: | |
777 | r = vhost_set_vring(d, ioctl, argp); | |
778 | break; | |
779 | } | |
780 | done: | |
781 | return r; | |
782 | } | |
783 | ||
784 | static const struct vhost_memory_region *find_region(struct vhost_memory *mem, | |
785 | __u64 addr, __u32 len) | |
786 | { | |
787 | struct vhost_memory_region *reg; | |
788 | int i; | |
789 | /* linear search is not brilliant, but we really have on the order of 6 | |
790 | * regions in practice */ | |
791 | for (i = 0; i < mem->nregions; ++i) { | |
792 | reg = mem->regions + i; | |
793 | if (reg->guest_phys_addr <= addr && | |
794 | reg->guest_phys_addr + reg->memory_size - 1 >= addr) | |
795 | return reg; | |
796 | } | |
797 | return NULL; | |
798 | } | |
799 | ||
800 | /* TODO: This is really inefficient. We need something like get_user() | |
801 | * (instruction directly accesses the data, with an exception table entry | |
802 | * returning -EFAULT). See Documentation/x86/exception-tables.txt. | |
803 | */ | |
804 | static int set_bit_to_user(int nr, void __user *addr) | |
805 | { | |
806 | unsigned long log = (unsigned long)addr; | |
807 | struct page *page; | |
808 | void *base; | |
809 | int bit = nr + (log % PAGE_SIZE) * 8; | |
810 | int r; | |
811 | r = get_user_pages_fast(log, 1, 1, &page); | |
d6db3f5c | 812 | if (r < 0) |
3a4d5c94 | 813 | return r; |
d6db3f5c | 814 | BUG_ON(r != 1); |
3a4d5c94 MT |
815 | base = kmap_atomic(page, KM_USER0); |
816 | set_bit(bit, base); | |
817 | kunmap_atomic(base, KM_USER0); | |
818 | set_page_dirty_lock(page); | |
819 | put_page(page); | |
820 | return 0; | |
821 | } | |
822 | ||
823 | static int log_write(void __user *log_base, | |
824 | u64 write_address, u64 write_length) | |
825 | { | |
826 | int r; | |
827 | if (!write_length) | |
828 | return 0; | |
829 | write_address /= VHOST_PAGE_SIZE; | |
830 | for (;;) { | |
831 | u64 base = (u64)(unsigned long)log_base; | |
832 | u64 log = base + write_address / 8; | |
833 | int bit = write_address % 8; | |
834 | if ((u64)(unsigned long)log != log) | |
835 | return -EFAULT; | |
836 | r = set_bit_to_user(bit, (void __user *)(unsigned long)log); | |
837 | if (r < 0) | |
838 | return r; | |
839 | if (write_length <= VHOST_PAGE_SIZE) | |
840 | break; | |
841 | write_length -= VHOST_PAGE_SIZE; | |
842 | write_address += VHOST_PAGE_SIZE; | |
843 | } | |
844 | return r; | |
845 | } | |
846 | ||
847 | int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log, | |
848 | unsigned int log_num, u64 len) | |
849 | { | |
850 | int i, r; | |
851 | ||
852 | /* Make sure data written is seen before log. */ | |
5659338c | 853 | smp_wmb(); |
3a4d5c94 MT |
854 | for (i = 0; i < log_num; ++i) { |
855 | u64 l = min(log[i].len, len); | |
856 | r = log_write(vq->log_base, log[i].addr, l); | |
857 | if (r < 0) | |
858 | return r; | |
859 | len -= l; | |
860 | if (!len) | |
861 | return 0; | |
862 | } | |
863 | if (vq->log_ctx) | |
864 | eventfd_signal(vq->log_ctx, 1); | |
865 | /* Length written exceeds what we have stored. This is a bug. */ | |
866 | BUG(); | |
867 | return 0; | |
868 | } | |
869 | ||
a8d3782f CH |
870 | static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len, |
871 | struct iovec iov[], int iov_size) | |
3a4d5c94 MT |
872 | { |
873 | const struct vhost_memory_region *reg; | |
874 | struct vhost_memory *mem; | |
875 | struct iovec *_iov; | |
876 | u64 s = 0; | |
877 | int ret = 0; | |
878 | ||
879 | rcu_read_lock(); | |
880 | ||
881 | mem = rcu_dereference(dev->memory); | |
882 | while ((u64)len > s) { | |
883 | u64 size; | |
7b3384fc | 884 | if (unlikely(ret >= iov_size)) { |
3a4d5c94 MT |
885 | ret = -ENOBUFS; |
886 | break; | |
887 | } | |
888 | reg = find_region(mem, addr, len); | |
7b3384fc | 889 | if (unlikely(!reg)) { |
3a4d5c94 MT |
890 | ret = -EFAULT; |
891 | break; | |
892 | } | |
893 | _iov = iov + ret; | |
894 | size = reg->memory_size - addr + reg->guest_phys_addr; | |
895 | _iov->iov_len = min((u64)len, size); | |
a8d3782f | 896 | _iov->iov_base = (void __user *)(unsigned long) |
3a4d5c94 MT |
897 | (reg->userspace_addr + addr - reg->guest_phys_addr); |
898 | s += size; | |
899 | addr += size; | |
900 | ++ret; | |
901 | } | |
902 | ||
903 | rcu_read_unlock(); | |
904 | return ret; | |
905 | } | |
906 | ||
907 | /* Each buffer in the virtqueues is actually a chain of descriptors. This | |
908 | * function returns the next descriptor in the chain, | |
909 | * or -1U if we're at the end. */ | |
910 | static unsigned next_desc(struct vring_desc *desc) | |
911 | { | |
912 | unsigned int next; | |
913 | ||
914 | /* If this descriptor says it doesn't chain, we're done. */ | |
915 | if (!(desc->flags & VRING_DESC_F_NEXT)) | |
916 | return -1U; | |
917 | ||
918 | /* Check they're not leading us off end of descriptors. */ | |
919 | next = desc->next; | |
920 | /* Make sure compiler knows to grab that: we don't want it changing! */ | |
921 | /* We will use the result as an index in an array, so most | |
922 | * architectures only need a compiler barrier here. */ | |
923 | read_barrier_depends(); | |
924 | ||
925 | return next; | |
926 | } | |
927 | ||
7b3384fc MT |
928 | static int get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq, |
929 | struct iovec iov[], unsigned int iov_size, | |
930 | unsigned int *out_num, unsigned int *in_num, | |
931 | struct vhost_log *log, unsigned int *log_num, | |
932 | struct vring_desc *indirect) | |
3a4d5c94 MT |
933 | { |
934 | struct vring_desc desc; | |
935 | unsigned int i = 0, count, found = 0; | |
936 | int ret; | |
937 | ||
938 | /* Sanity check */ | |
7b3384fc | 939 | if (unlikely(indirect->len % sizeof desc)) { |
3a4d5c94 MT |
940 | vq_err(vq, "Invalid length in indirect descriptor: " |
941 | "len 0x%llx not multiple of 0x%zx\n", | |
942 | (unsigned long long)indirect->len, | |
943 | sizeof desc); | |
944 | return -EINVAL; | |
945 | } | |
946 | ||
947 | ret = translate_desc(dev, indirect->addr, indirect->len, vq->indirect, | |
948 | ARRAY_SIZE(vq->indirect)); | |
7b3384fc | 949 | if (unlikely(ret < 0)) { |
3a4d5c94 MT |
950 | vq_err(vq, "Translation failure %d in indirect.\n", ret); |
951 | return ret; | |
952 | } | |
953 | ||
954 | /* We will use the result as an address to read from, so most | |
955 | * architectures only need a compiler barrier here. */ | |
956 | read_barrier_depends(); | |
957 | ||
958 | count = indirect->len / sizeof desc; | |
959 | /* Buffers are chained via a 16 bit next field, so | |
960 | * we can have at most 2^16 of these. */ | |
7b3384fc | 961 | if (unlikely(count > USHRT_MAX + 1)) { |
3a4d5c94 MT |
962 | vq_err(vq, "Indirect buffer length too big: %d\n", |
963 | indirect->len); | |
964 | return -E2BIG; | |
965 | } | |
966 | ||
967 | do { | |
968 | unsigned iov_count = *in_num + *out_num; | |
7b3384fc | 969 | if (unlikely(++found > count)) { |
3a4d5c94 MT |
970 | vq_err(vq, "Loop detected: last one at %u " |
971 | "indirect size %u\n", | |
972 | i, count); | |
973 | return -EINVAL; | |
974 | } | |
7b3384fc MT |
975 | if (unlikely(memcpy_fromiovec((unsigned char *)&desc, vq->indirect, |
976 | sizeof desc))) { | |
3a4d5c94 MT |
977 | vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n", |
978 | i, (size_t)indirect->addr + i * sizeof desc); | |
979 | return -EINVAL; | |
980 | } | |
7b3384fc | 981 | if (unlikely(desc.flags & VRING_DESC_F_INDIRECT)) { |
3a4d5c94 MT |
982 | vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n", |
983 | i, (size_t)indirect->addr + i * sizeof desc); | |
984 | return -EINVAL; | |
985 | } | |
986 | ||
987 | ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count, | |
988 | iov_size - iov_count); | |
7b3384fc | 989 | if (unlikely(ret < 0)) { |
3a4d5c94 MT |
990 | vq_err(vq, "Translation failure %d indirect idx %d\n", |
991 | ret, i); | |
992 | return ret; | |
993 | } | |
994 | /* If this is an input descriptor, increment that count. */ | |
995 | if (desc.flags & VRING_DESC_F_WRITE) { | |
996 | *in_num += ret; | |
997 | if (unlikely(log)) { | |
998 | log[*log_num].addr = desc.addr; | |
999 | log[*log_num].len = desc.len; | |
1000 | ++*log_num; | |
1001 | } | |
1002 | } else { | |
1003 | /* If it's an output descriptor, they're all supposed | |
1004 | * to come before any input descriptors. */ | |
7b3384fc | 1005 | if (unlikely(*in_num)) { |
3a4d5c94 MT |
1006 | vq_err(vq, "Indirect descriptor " |
1007 | "has out after in: idx %d\n", i); | |
1008 | return -EINVAL; | |
1009 | } | |
1010 | *out_num += ret; | |
1011 | } | |
1012 | } while ((i = next_desc(&desc)) != -1); | |
1013 | return 0; | |
1014 | } | |
1015 | ||
1016 | /* This looks in the virtqueue and for the first available buffer, and converts | |
1017 | * it to an iovec for convenient access. Since descriptors consist of some | |
1018 | * number of output then some number of input descriptors, it's actually two | |
1019 | * iovecs, but we pack them into one and note how many of each there were. | |
1020 | * | |
d5675bd2 MT |
1021 | * This function returns the descriptor number found, or vq->num (which is |
1022 | * never a valid descriptor number) if none was found. A negative code is | |
1023 | * returned on error. */ | |
1024 | int vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq, | |
1025 | struct iovec iov[], unsigned int iov_size, | |
1026 | unsigned int *out_num, unsigned int *in_num, | |
1027 | struct vhost_log *log, unsigned int *log_num) | |
3a4d5c94 MT |
1028 | { |
1029 | struct vring_desc desc; | |
1030 | unsigned int i, head, found = 0; | |
1031 | u16 last_avail_idx; | |
1032 | int ret; | |
1033 | ||
1034 | /* Check it isn't doing very strange things with descriptor numbers. */ | |
1035 | last_avail_idx = vq->last_avail_idx; | |
7b3384fc | 1036 | if (unlikely(get_user(vq->avail_idx, &vq->avail->idx))) { |
3a4d5c94 MT |
1037 | vq_err(vq, "Failed to access avail idx at %p\n", |
1038 | &vq->avail->idx); | |
d5675bd2 | 1039 | return -EFAULT; |
3a4d5c94 MT |
1040 | } |
1041 | ||
7b3384fc | 1042 | if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) { |
3a4d5c94 MT |
1043 | vq_err(vq, "Guest moved used index from %u to %u", |
1044 | last_avail_idx, vq->avail_idx); | |
d5675bd2 | 1045 | return -EFAULT; |
3a4d5c94 MT |
1046 | } |
1047 | ||
1048 | /* If there's nothing new since last we looked, return invalid. */ | |
1049 | if (vq->avail_idx == last_avail_idx) | |
1050 | return vq->num; | |
1051 | ||
1052 | /* Only get avail ring entries after they have been exposed by guest. */ | |
5659338c | 1053 | smp_rmb(); |
3a4d5c94 MT |
1054 | |
1055 | /* Grab the next descriptor number they're advertising, and increment | |
1056 | * the index we've seen. */ | |
7b3384fc MT |
1057 | if (unlikely(get_user(head, |
1058 | &vq->avail->ring[last_avail_idx % vq->num]))) { | |
3a4d5c94 MT |
1059 | vq_err(vq, "Failed to read head: idx %d address %p\n", |
1060 | last_avail_idx, | |
1061 | &vq->avail->ring[last_avail_idx % vq->num]); | |
d5675bd2 | 1062 | return -EFAULT; |
3a4d5c94 MT |
1063 | } |
1064 | ||
1065 | /* If their number is silly, that's an error. */ | |
7b3384fc | 1066 | if (unlikely(head >= vq->num)) { |
3a4d5c94 MT |
1067 | vq_err(vq, "Guest says index %u > %u is available", |
1068 | head, vq->num); | |
d5675bd2 | 1069 | return -EINVAL; |
3a4d5c94 MT |
1070 | } |
1071 | ||
1072 | /* When we start there are none of either input nor output. */ | |
1073 | *out_num = *in_num = 0; | |
1074 | if (unlikely(log)) | |
1075 | *log_num = 0; | |
1076 | ||
1077 | i = head; | |
1078 | do { | |
1079 | unsigned iov_count = *in_num + *out_num; | |
7b3384fc | 1080 | if (unlikely(i >= vq->num)) { |
3a4d5c94 MT |
1081 | vq_err(vq, "Desc index is %u > %u, head = %u", |
1082 | i, vq->num, head); | |
d5675bd2 | 1083 | return -EINVAL; |
3a4d5c94 | 1084 | } |
7b3384fc | 1085 | if (unlikely(++found > vq->num)) { |
3a4d5c94 MT |
1086 | vq_err(vq, "Loop detected: last one at %u " |
1087 | "vq size %u head %u\n", | |
1088 | i, vq->num, head); | |
d5675bd2 | 1089 | return -EINVAL; |
3a4d5c94 MT |
1090 | } |
1091 | ret = copy_from_user(&desc, vq->desc + i, sizeof desc); | |
7b3384fc | 1092 | if (unlikely(ret)) { |
3a4d5c94 MT |
1093 | vq_err(vq, "Failed to get descriptor: idx %d addr %p\n", |
1094 | i, vq->desc + i); | |
d5675bd2 | 1095 | return -EFAULT; |
3a4d5c94 MT |
1096 | } |
1097 | if (desc.flags & VRING_DESC_F_INDIRECT) { | |
1098 | ret = get_indirect(dev, vq, iov, iov_size, | |
1099 | out_num, in_num, | |
1100 | log, log_num, &desc); | |
7b3384fc | 1101 | if (unlikely(ret < 0)) { |
3a4d5c94 MT |
1102 | vq_err(vq, "Failure detected " |
1103 | "in indirect descriptor at idx %d\n", i); | |
d5675bd2 | 1104 | return ret; |
3a4d5c94 MT |
1105 | } |
1106 | continue; | |
1107 | } | |
1108 | ||
1109 | ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count, | |
1110 | iov_size - iov_count); | |
7b3384fc | 1111 | if (unlikely(ret < 0)) { |
3a4d5c94 MT |
1112 | vq_err(vq, "Translation failure %d descriptor idx %d\n", |
1113 | ret, i); | |
d5675bd2 | 1114 | return ret; |
3a4d5c94 MT |
1115 | } |
1116 | if (desc.flags & VRING_DESC_F_WRITE) { | |
1117 | /* If this is an input descriptor, | |
1118 | * increment that count. */ | |
1119 | *in_num += ret; | |
1120 | if (unlikely(log)) { | |
1121 | log[*log_num].addr = desc.addr; | |
1122 | log[*log_num].len = desc.len; | |
1123 | ++*log_num; | |
1124 | } | |
1125 | } else { | |
1126 | /* If it's an output descriptor, they're all supposed | |
1127 | * to come before any input descriptors. */ | |
7b3384fc | 1128 | if (unlikely(*in_num)) { |
3a4d5c94 MT |
1129 | vq_err(vq, "Descriptor has out after in: " |
1130 | "idx %d\n", i); | |
d5675bd2 | 1131 | return -EINVAL; |
3a4d5c94 MT |
1132 | } |
1133 | *out_num += ret; | |
1134 | } | |
1135 | } while ((i = next_desc(&desc)) != -1); | |
1136 | ||
1137 | /* On success, increment avail index. */ | |
1138 | vq->last_avail_idx++; | |
1139 | return head; | |
1140 | } | |
1141 | ||
1142 | /* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */ | |
8dd014ad | 1143 | void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n) |
3a4d5c94 | 1144 | { |
8dd014ad | 1145 | vq->last_avail_idx -= n; |
3a4d5c94 MT |
1146 | } |
1147 | ||
1148 | /* After we've used one of their buffers, we tell them about it. We'll then | |
1149 | * want to notify the guest, using eventfd. */ | |
1150 | int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len) | |
1151 | { | |
a8d3782f | 1152 | struct vring_used_elem __user *used; |
3a4d5c94 MT |
1153 | |
1154 | /* The virtqueue contains a ring of used buffers. Get a pointer to the | |
1155 | * next entry in that used ring. */ | |
1156 | used = &vq->used->ring[vq->last_used_idx % vq->num]; | |
1157 | if (put_user(head, &used->id)) { | |
1158 | vq_err(vq, "Failed to write used id"); | |
1159 | return -EFAULT; | |
1160 | } | |
1161 | if (put_user(len, &used->len)) { | |
1162 | vq_err(vq, "Failed to write used len"); | |
1163 | return -EFAULT; | |
1164 | } | |
1165 | /* Make sure buffer is written before we update index. */ | |
5659338c | 1166 | smp_wmb(); |
3a4d5c94 MT |
1167 | if (put_user(vq->last_used_idx + 1, &vq->used->idx)) { |
1168 | vq_err(vq, "Failed to increment used idx"); | |
1169 | return -EFAULT; | |
1170 | } | |
1171 | if (unlikely(vq->log_used)) { | |
1172 | /* Make sure data is seen before log. */ | |
5659338c | 1173 | smp_wmb(); |
86e9424d MT |
1174 | /* Log used ring entry write. */ |
1175 | log_write(vq->log_base, | |
a8d3782f CH |
1176 | vq->log_addr + |
1177 | ((void __user *)used - (void __user *)vq->used), | |
86e9424d MT |
1178 | sizeof *used); |
1179 | /* Log used index update. */ | |
1180 | log_write(vq->log_base, | |
1181 | vq->log_addr + offsetof(struct vring_used, idx), | |
1182 | sizeof vq->used->idx); | |
3a4d5c94 MT |
1183 | if (vq->log_ctx) |
1184 | eventfd_signal(vq->log_ctx, 1); | |
1185 | } | |
1186 | vq->last_used_idx++; | |
1187 | return 0; | |
1188 | } | |
1189 | ||
8dd014ad DS |
1190 | static int __vhost_add_used_n(struct vhost_virtqueue *vq, |
1191 | struct vring_used_elem *heads, | |
1192 | unsigned count) | |
1193 | { | |
1194 | struct vring_used_elem __user *used; | |
1195 | int start; | |
1196 | ||
1197 | start = vq->last_used_idx % vq->num; | |
1198 | used = vq->used->ring + start; | |
1199 | if (copy_to_user(used, heads, count * sizeof *used)) { | |
1200 | vq_err(vq, "Failed to write used"); | |
1201 | return -EFAULT; | |
1202 | } | |
1203 | if (unlikely(vq->log_used)) { | |
1204 | /* Make sure data is seen before log. */ | |
1205 | smp_wmb(); | |
1206 | /* Log used ring entry write. */ | |
1207 | log_write(vq->log_base, | |
1208 | vq->log_addr + | |
1209 | ((void __user *)used - (void __user *)vq->used), | |
1210 | count * sizeof *used); | |
1211 | } | |
1212 | vq->last_used_idx += count; | |
1213 | return 0; | |
1214 | } | |
1215 | ||
1216 | /* After we've used one of their buffers, we tell them about it. We'll then | |
1217 | * want to notify the guest, using eventfd. */ | |
1218 | int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads, | |
1219 | unsigned count) | |
1220 | { | |
1221 | int start, n, r; | |
1222 | ||
1223 | start = vq->last_used_idx % vq->num; | |
1224 | n = vq->num - start; | |
1225 | if (n < count) { | |
1226 | r = __vhost_add_used_n(vq, heads, n); | |
1227 | if (r < 0) | |
1228 | return r; | |
1229 | heads += n; | |
1230 | count -= n; | |
1231 | } | |
1232 | r = __vhost_add_used_n(vq, heads, count); | |
1233 | ||
1234 | /* Make sure buffer is written before we update index. */ | |
1235 | smp_wmb(); | |
1236 | if (put_user(vq->last_used_idx, &vq->used->idx)) { | |
1237 | vq_err(vq, "Failed to increment used idx"); | |
1238 | return -EFAULT; | |
1239 | } | |
1240 | if (unlikely(vq->log_used)) { | |
1241 | /* Log used index update. */ | |
1242 | log_write(vq->log_base, | |
1243 | vq->log_addr + offsetof(struct vring_used, idx), | |
1244 | sizeof vq->used->idx); | |
1245 | if (vq->log_ctx) | |
1246 | eventfd_signal(vq->log_ctx, 1); | |
1247 | } | |
1248 | return r; | |
1249 | } | |
1250 | ||
3a4d5c94 MT |
1251 | /* This actually signals the guest, using eventfd. */ |
1252 | void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq) | |
1253 | { | |
0d499356 MT |
1254 | __u16 flags; |
1255 | /* Flush out used index updates. This is paired | |
1256 | * with the barrier that the Guest executes when enabling | |
1257 | * interrupts. */ | |
1258 | smp_mb(); | |
1259 | ||
3a4d5c94 MT |
1260 | if (get_user(flags, &vq->avail->flags)) { |
1261 | vq_err(vq, "Failed to get flags"); | |
1262 | return; | |
1263 | } | |
1264 | ||
1265 | /* If they don't want an interrupt, don't signal, unless empty. */ | |
1266 | if ((flags & VRING_AVAIL_F_NO_INTERRUPT) && | |
1267 | (vq->avail_idx != vq->last_avail_idx || | |
1268 | !vhost_has_feature(dev, VIRTIO_F_NOTIFY_ON_EMPTY))) | |
1269 | return; | |
1270 | ||
1271 | /* Signal the Guest tell them we used something up. */ | |
1272 | if (vq->call_ctx) | |
1273 | eventfd_signal(vq->call_ctx, 1); | |
1274 | } | |
1275 | ||
1276 | /* And here's the combo meal deal. Supersize me! */ | |
1277 | void vhost_add_used_and_signal(struct vhost_dev *dev, | |
1278 | struct vhost_virtqueue *vq, | |
1279 | unsigned int head, int len) | |
1280 | { | |
1281 | vhost_add_used(vq, head, len); | |
1282 | vhost_signal(dev, vq); | |
1283 | } | |
1284 | ||
8dd014ad DS |
1285 | /* multi-buffer version of vhost_add_used_and_signal */ |
1286 | void vhost_add_used_and_signal_n(struct vhost_dev *dev, | |
1287 | struct vhost_virtqueue *vq, | |
1288 | struct vring_used_elem *heads, unsigned count) | |
1289 | { | |
1290 | vhost_add_used_n(vq, heads, count); | |
1291 | vhost_signal(dev, vq); | |
1292 | } | |
1293 | ||
3a4d5c94 MT |
1294 | /* OK, now we need to know about added descriptors. */ |
1295 | bool vhost_enable_notify(struct vhost_virtqueue *vq) | |
1296 | { | |
1297 | u16 avail_idx; | |
1298 | int r; | |
1299 | if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY)) | |
1300 | return false; | |
1301 | vq->used_flags &= ~VRING_USED_F_NO_NOTIFY; | |
1302 | r = put_user(vq->used_flags, &vq->used->flags); | |
1303 | if (r) { | |
1304 | vq_err(vq, "Failed to enable notification at %p: %d\n", | |
1305 | &vq->used->flags, r); | |
1306 | return false; | |
1307 | } | |
1308 | /* They could have slipped one in as we were doing that: make | |
1309 | * sure it's written, then check again. */ | |
5659338c | 1310 | smp_mb(); |
3a4d5c94 MT |
1311 | r = get_user(avail_idx, &vq->avail->idx); |
1312 | if (r) { | |
1313 | vq_err(vq, "Failed to check avail idx at %p: %d\n", | |
1314 | &vq->avail->idx, r); | |
1315 | return false; | |
1316 | } | |
1317 | ||
8dd014ad | 1318 | return avail_idx != vq->avail_idx; |
3a4d5c94 MT |
1319 | } |
1320 | ||
1321 | /* We don't need to be notified again. */ | |
1322 | void vhost_disable_notify(struct vhost_virtqueue *vq) | |
1323 | { | |
1324 | int r; | |
1325 | if (vq->used_flags & VRING_USED_F_NO_NOTIFY) | |
1326 | return; | |
1327 | vq->used_flags |= VRING_USED_F_NO_NOTIFY; | |
1328 | r = put_user(vq->used_flags, &vq->used->flags); | |
1329 | if (r) | |
1330 | vq_err(vq, "Failed to enable notification at %p: %d\n", | |
1331 | &vq->used->flags, r); | |
1332 | } |