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