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