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