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