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