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