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> |
6e84f315 | 30 | #include <linux/sched/mm.h> |
174cd4b1 | 31 | #include <linux/sched/signal.h> |
a9709d68 | 32 | #include <linux/interval_tree_generic.h> |
3a4d5c94 | 33 | |
3a4d5c94 MT |
34 | #include "vhost.h" |
35 | ||
c9ce42f7 IM |
36 | static ushort max_mem_regions = 64; |
37 | module_param(max_mem_regions, ushort, 0444); | |
38 | MODULE_PARM_DESC(max_mem_regions, | |
39 | "Maximum number of memory regions in memory map. (default: 64)"); | |
6b1e6cc7 JW |
40 | static int max_iotlb_entries = 2048; |
41 | module_param(max_iotlb_entries, int, 0444); | |
42 | MODULE_PARM_DESC(max_iotlb_entries, | |
43 | "Maximum number of iotlb entries. (default: 2048)"); | |
c9ce42f7 | 44 | |
3a4d5c94 | 45 | enum { |
3a4d5c94 MT |
46 | VHOST_MEMORY_F_LOG = 0x1, |
47 | }; | |
48 | ||
3b1bbe89 MT |
49 | #define vhost_used_event(vq) ((__virtio16 __user *)&vq->avail->ring[vq->num]) |
50 | #define vhost_avail_event(vq) ((__virtio16 __user *)&vq->used->ring[vq->num]) | |
8ea8cf89 | 51 | |
a9709d68 JW |
52 | INTERVAL_TREE_DEFINE(struct vhost_umem_node, |
53 | rb, __u64, __subtree_last, | |
2f952c01 | 54 | START, LAST, static inline, vhost_umem_interval_tree); |
a9709d68 | 55 | |
2751c988 | 56 | #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY |
c5072037 | 57 | static void vhost_disable_cross_endian(struct vhost_virtqueue *vq) |
2751c988 GK |
58 | { |
59 | vq->user_be = !virtio_legacy_is_little_endian(); | |
60 | } | |
61 | ||
c5072037 GK |
62 | static void vhost_enable_cross_endian_big(struct vhost_virtqueue *vq) |
63 | { | |
64 | vq->user_be = true; | |
65 | } | |
66 | ||
67 | static void vhost_enable_cross_endian_little(struct vhost_virtqueue *vq) | |
68 | { | |
69 | vq->user_be = false; | |
70 | } | |
71 | ||
2751c988 GK |
72 | static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp) |
73 | { | |
74 | struct vhost_vring_state s; | |
75 | ||
76 | if (vq->private_data) | |
77 | return -EBUSY; | |
78 | ||
79 | if (copy_from_user(&s, argp, sizeof(s))) | |
80 | return -EFAULT; | |
81 | ||
82 | if (s.num != VHOST_VRING_LITTLE_ENDIAN && | |
83 | s.num != VHOST_VRING_BIG_ENDIAN) | |
84 | return -EINVAL; | |
85 | ||
c5072037 GK |
86 | if (s.num == VHOST_VRING_BIG_ENDIAN) |
87 | vhost_enable_cross_endian_big(vq); | |
88 | else | |
89 | vhost_enable_cross_endian_little(vq); | |
2751c988 GK |
90 | |
91 | return 0; | |
92 | } | |
93 | ||
94 | static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx, | |
95 | int __user *argp) | |
96 | { | |
97 | struct vhost_vring_state s = { | |
98 | .index = idx, | |
99 | .num = vq->user_be | |
100 | }; | |
101 | ||
102 | if (copy_to_user(argp, &s, sizeof(s))) | |
103 | return -EFAULT; | |
104 | ||
105 | return 0; | |
106 | } | |
107 | ||
108 | static void vhost_init_is_le(struct vhost_virtqueue *vq) | |
109 | { | |
110 | /* Note for legacy virtio: user_be is initialized at reset time | |
111 | * according to the host endianness. If userspace does not set an | |
112 | * explicit endianness, the default behavior is native endian, as | |
113 | * expected by legacy virtio. | |
114 | */ | |
115 | vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1) || !vq->user_be; | |
116 | } | |
117 | #else | |
c5072037 | 118 | static void vhost_disable_cross_endian(struct vhost_virtqueue *vq) |
2751c988 GK |
119 | { |
120 | } | |
121 | ||
122 | static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp) | |
123 | { | |
124 | return -ENOIOCTLCMD; | |
125 | } | |
126 | ||
127 | static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx, | |
128 | int __user *argp) | |
129 | { | |
130 | return -ENOIOCTLCMD; | |
131 | } | |
132 | ||
133 | static void vhost_init_is_le(struct vhost_virtqueue *vq) | |
134 | { | |
cda8bba0 HP |
135 | vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1) |
136 | || virtio_legacy_is_little_endian(); | |
2751c988 GK |
137 | } |
138 | #endif /* CONFIG_VHOST_CROSS_ENDIAN_LEGACY */ | |
139 | ||
c5072037 GK |
140 | static void vhost_reset_is_le(struct vhost_virtqueue *vq) |
141 | { | |
cda8bba0 | 142 | vhost_init_is_le(vq); |
c5072037 GK |
143 | } |
144 | ||
7235acdb JW |
145 | struct vhost_flush_struct { |
146 | struct vhost_work work; | |
147 | struct completion wait_event; | |
148 | }; | |
149 | ||
150 | static void vhost_flush_work(struct vhost_work *work) | |
151 | { | |
152 | struct vhost_flush_struct *s; | |
153 | ||
154 | s = container_of(work, struct vhost_flush_struct, work); | |
155 | complete(&s->wait_event); | |
156 | } | |
157 | ||
3a4d5c94 MT |
158 | static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh, |
159 | poll_table *pt) | |
160 | { | |
161 | struct vhost_poll *poll; | |
3a4d5c94 | 162 | |
d47effe1 | 163 | poll = container_of(pt, struct vhost_poll, table); |
3a4d5c94 MT |
164 | poll->wqh = wqh; |
165 | add_wait_queue(wqh, &poll->wait); | |
166 | } | |
167 | ||
ac6424b9 | 168 | static int vhost_poll_wakeup(wait_queue_entry_t *wait, unsigned mode, int sync, |
3a4d5c94 MT |
169 | void *key) |
170 | { | |
c23f3445 TH |
171 | struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait); |
172 | ||
3ad6f93e | 173 | if (!(key_to_poll(key) & poll->mask)) |
3a4d5c94 MT |
174 | return 0; |
175 | ||
c23f3445 | 176 | vhost_poll_queue(poll); |
3a4d5c94 MT |
177 | return 0; |
178 | } | |
179 | ||
163049ae | 180 | void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn) |
87d6a412 | 181 | { |
04b96e55 | 182 | clear_bit(VHOST_WORK_QUEUED, &work->flags); |
87d6a412 | 183 | work->fn = fn; |
87d6a412 | 184 | } |
6ac1afbf | 185 | EXPORT_SYMBOL_GPL(vhost_work_init); |
87d6a412 | 186 | |
3a4d5c94 | 187 | /* Init poll structure */ |
c23f3445 | 188 | void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn, |
58e3b602 | 189 | __poll_t mask, struct vhost_dev *dev) |
3a4d5c94 | 190 | { |
3a4d5c94 MT |
191 | init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup); |
192 | init_poll_funcptr(&poll->table, vhost_poll_func); | |
193 | poll->mask = mask; | |
c23f3445 | 194 | poll->dev = dev; |
2b8b328b | 195 | poll->wqh = NULL; |
c23f3445 | 196 | |
87d6a412 | 197 | vhost_work_init(&poll->work, fn); |
3a4d5c94 | 198 | } |
6ac1afbf | 199 | EXPORT_SYMBOL_GPL(vhost_poll_init); |
3a4d5c94 MT |
200 | |
201 | /* Start polling a file. We add ourselves to file's wait queue. The caller must | |
202 | * keep a reference to a file until after vhost_poll_stop is called. */ | |
2b8b328b | 203 | int vhost_poll_start(struct vhost_poll *poll, struct file *file) |
3a4d5c94 | 204 | { |
e6c8adca | 205 | __poll_t mask; |
2b8b328b | 206 | int ret = 0; |
d47effe1 | 207 | |
70181d51 JW |
208 | if (poll->wqh) |
209 | return 0; | |
210 | ||
9965ed17 | 211 | mask = vfs_poll(file, &poll->table); |
3a4d5c94 | 212 | if (mask) |
3ad6f93e | 213 | vhost_poll_wakeup(&poll->wait, 0, 0, poll_to_key(mask)); |
a9a08845 | 214 | if (mask & EPOLLERR) { |
dc6455a7 | 215 | vhost_poll_stop(poll); |
2b8b328b JW |
216 | ret = -EINVAL; |
217 | } | |
218 | ||
219 | return ret; | |
3a4d5c94 | 220 | } |
6ac1afbf | 221 | EXPORT_SYMBOL_GPL(vhost_poll_start); |
3a4d5c94 MT |
222 | |
223 | /* Stop polling a file. After this function returns, it becomes safe to drop the | |
224 | * file reference. You must also flush afterwards. */ | |
225 | void vhost_poll_stop(struct vhost_poll *poll) | |
226 | { | |
2b8b328b JW |
227 | if (poll->wqh) { |
228 | remove_wait_queue(poll->wqh, &poll->wait); | |
229 | poll->wqh = NULL; | |
230 | } | |
3a4d5c94 | 231 | } |
6ac1afbf | 232 | EXPORT_SYMBOL_GPL(vhost_poll_stop); |
3a4d5c94 | 233 | |
7235acdb | 234 | void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work) |
0174b0c3 | 235 | { |
7235acdb | 236 | struct vhost_flush_struct flush; |
d47effe1 | 237 | |
7235acdb JW |
238 | if (dev->worker) { |
239 | init_completion(&flush.wait_event); | |
240 | vhost_work_init(&flush.work, vhost_flush_work); | |
0174b0c3 | 241 | |
7235acdb JW |
242 | vhost_work_queue(dev, &flush.work); |
243 | wait_for_completion(&flush.wait_event); | |
244 | } | |
3a4d5c94 | 245 | } |
6ac1afbf | 246 | EXPORT_SYMBOL_GPL(vhost_work_flush); |
3a4d5c94 | 247 | |
87d6a412 MT |
248 | /* Flush any work that has been scheduled. When calling this, don't hold any |
249 | * locks that are also used by the callback. */ | |
250 | void vhost_poll_flush(struct vhost_poll *poll) | |
251 | { | |
252 | vhost_work_flush(poll->dev, &poll->work); | |
253 | } | |
6ac1afbf | 254 | EXPORT_SYMBOL_GPL(vhost_poll_flush); |
87d6a412 | 255 | |
163049ae | 256 | void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work) |
3a4d5c94 | 257 | { |
04b96e55 JW |
258 | if (!dev->worker) |
259 | return; | |
c23f3445 | 260 | |
04b96e55 JW |
261 | if (!test_and_set_bit(VHOST_WORK_QUEUED, &work->flags)) { |
262 | /* We can only add the work to the list after we're | |
263 | * sure it was not in the list. | |
635abf01 | 264 | * test_and_set_bit() implies a memory barrier. |
04b96e55 | 265 | */ |
04b96e55 | 266 | llist_add(&work->node, &dev->work_list); |
c23f3445 TH |
267 | wake_up_process(dev->worker); |
268 | } | |
3a4d5c94 | 269 | } |
6ac1afbf | 270 | EXPORT_SYMBOL_GPL(vhost_work_queue); |
3a4d5c94 | 271 | |
526d3e7f JW |
272 | /* A lockless hint for busy polling code to exit the loop */ |
273 | bool vhost_has_work(struct vhost_dev *dev) | |
274 | { | |
04b96e55 | 275 | return !llist_empty(&dev->work_list); |
526d3e7f JW |
276 | } |
277 | EXPORT_SYMBOL_GPL(vhost_has_work); | |
278 | ||
87d6a412 MT |
279 | void vhost_poll_queue(struct vhost_poll *poll) |
280 | { | |
281 | vhost_work_queue(poll->dev, &poll->work); | |
282 | } | |
6ac1afbf | 283 | EXPORT_SYMBOL_GPL(vhost_poll_queue); |
87d6a412 | 284 | |
f8894913 JW |
285 | static void __vhost_vq_meta_reset(struct vhost_virtqueue *vq) |
286 | { | |
287 | int j; | |
288 | ||
289 | for (j = 0; j < VHOST_NUM_ADDRS; j++) | |
290 | vq->meta_iotlb[j] = NULL; | |
291 | } | |
292 | ||
293 | static void vhost_vq_meta_reset(struct vhost_dev *d) | |
294 | { | |
295 | int i; | |
296 | ||
297 | for (i = 0; i < d->nvqs; ++i) | |
298 | __vhost_vq_meta_reset(d->vqs[i]); | |
299 | } | |
300 | ||
3a4d5c94 MT |
301 | static void vhost_vq_reset(struct vhost_dev *dev, |
302 | struct vhost_virtqueue *vq) | |
303 | { | |
304 | vq->num = 1; | |
305 | vq->desc = NULL; | |
306 | vq->avail = NULL; | |
307 | vq->used = NULL; | |
308 | vq->last_avail_idx = 0; | |
309 | vq->avail_idx = 0; | |
310 | vq->last_used_idx = 0; | |
8ea8cf89 MT |
311 | vq->signalled_used = 0; |
312 | vq->signalled_used_valid = false; | |
3a4d5c94 | 313 | vq->used_flags = 0; |
3a4d5c94 MT |
314 | vq->log_used = false; |
315 | vq->log_addr = -1ull; | |
3a4d5c94 | 316 | vq->private_data = NULL; |
ea16c514 | 317 | vq->acked_features = 0; |
3a4d5c94 MT |
318 | vq->log_base = NULL; |
319 | vq->error_ctx = NULL; | |
3a4d5c94 MT |
320 | vq->kick = NULL; |
321 | vq->call_ctx = NULL; | |
73a99f08 | 322 | vq->log_ctx = NULL; |
c5072037 GK |
323 | vhost_reset_is_le(vq); |
324 | vhost_disable_cross_endian(vq); | |
03088137 | 325 | vq->busyloop_timeout = 0; |
a9709d68 | 326 | vq->umem = NULL; |
6b1e6cc7 | 327 | vq->iotlb = NULL; |
f8894913 | 328 | __vhost_vq_meta_reset(vq); |
3a4d5c94 MT |
329 | } |
330 | ||
c23f3445 TH |
331 | static int vhost_worker(void *data) |
332 | { | |
333 | struct vhost_dev *dev = data; | |
04b96e55 JW |
334 | struct vhost_work *work, *work_next; |
335 | struct llist_node *node; | |
d7ffde35 | 336 | mm_segment_t oldfs = get_fs(); |
c23f3445 | 337 | |
d7ffde35 | 338 | set_fs(USER_DS); |
64e1c807 MT |
339 | use_mm(dev->mm); |
340 | ||
c23f3445 TH |
341 | for (;;) { |
342 | /* mb paired w/ kthread_stop */ | |
343 | set_current_state(TASK_INTERRUPTIBLE); | |
344 | ||
c23f3445 | 345 | if (kthread_should_stop()) { |
c23f3445 | 346 | __set_current_state(TASK_RUNNING); |
64e1c807 | 347 | break; |
c23f3445 | 348 | } |
04b96e55 JW |
349 | |
350 | node = llist_del_all(&dev->work_list); | |
351 | if (!node) | |
352 | schedule(); | |
353 | ||
354 | node = llist_reverse_order(node); | |
355 | /* make sure flag is seen after deletion */ | |
356 | smp_wmb(); | |
357 | llist_for_each_entry_safe(work, work_next, node, node) { | |
358 | clear_bit(VHOST_WORK_QUEUED, &work->flags); | |
c23f3445 TH |
359 | __set_current_state(TASK_RUNNING); |
360 | work->fn(work); | |
d550dda1 NHE |
361 | if (need_resched()) |
362 | schedule(); | |
04b96e55 | 363 | } |
c23f3445 | 364 | } |
64e1c807 | 365 | unuse_mm(dev->mm); |
d7ffde35 | 366 | set_fs(oldfs); |
64e1c807 | 367 | return 0; |
c23f3445 TH |
368 | } |
369 | ||
bab632d6 MT |
370 | static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq) |
371 | { | |
372 | kfree(vq->indirect); | |
373 | vq->indirect = NULL; | |
374 | kfree(vq->log); | |
375 | vq->log = NULL; | |
376 | kfree(vq->heads); | |
377 | vq->heads = NULL; | |
bab632d6 MT |
378 | } |
379 | ||
e0e9b406 JW |
380 | /* Helper to allocate iovec buffers for all vqs. */ |
381 | static long vhost_dev_alloc_iovecs(struct vhost_dev *dev) | |
382 | { | |
6d5e6aa8 | 383 | struct vhost_virtqueue *vq; |
e0e9b406 | 384 | int i; |
d47effe1 | 385 | |
e0e9b406 | 386 | for (i = 0; i < dev->nvqs; ++i) { |
6d5e6aa8 | 387 | vq = dev->vqs[i]; |
6da2ec56 KC |
388 | vq->indirect = kmalloc_array(UIO_MAXIOV, |
389 | sizeof(*vq->indirect), | |
390 | GFP_KERNEL); | |
391 | vq->log = kmalloc_array(UIO_MAXIOV, sizeof(*vq->log), | |
392 | GFP_KERNEL); | |
393 | vq->heads = kmalloc_array(UIO_MAXIOV, sizeof(*vq->heads), | |
394 | GFP_KERNEL); | |
6d5e6aa8 | 395 | if (!vq->indirect || !vq->log || !vq->heads) |
e0e9b406 JW |
396 | goto err_nomem; |
397 | } | |
398 | return 0; | |
d47effe1 | 399 | |
e0e9b406 | 400 | err_nomem: |
bab632d6 | 401 | for (; i >= 0; --i) |
3ab2e420 | 402 | vhost_vq_free_iovecs(dev->vqs[i]); |
e0e9b406 JW |
403 | return -ENOMEM; |
404 | } | |
405 | ||
406 | static void vhost_dev_free_iovecs(struct vhost_dev *dev) | |
407 | { | |
408 | int i; | |
d47effe1 | 409 | |
bab632d6 | 410 | for (i = 0; i < dev->nvqs; ++i) |
3ab2e420 | 411 | vhost_vq_free_iovecs(dev->vqs[i]); |
e0e9b406 JW |
412 | } |
413 | ||
59566b6e | 414 | void vhost_dev_init(struct vhost_dev *dev, |
3ab2e420 | 415 | struct vhost_virtqueue **vqs, int nvqs) |
3a4d5c94 | 416 | { |
6d5e6aa8 | 417 | struct vhost_virtqueue *vq; |
3a4d5c94 | 418 | int i; |
c23f3445 | 419 | |
3a4d5c94 MT |
420 | dev->vqs = vqs; |
421 | dev->nvqs = nvqs; | |
422 | mutex_init(&dev->mutex); | |
423 | dev->log_ctx = NULL; | |
a9709d68 | 424 | dev->umem = NULL; |
6b1e6cc7 | 425 | dev->iotlb = NULL; |
3a4d5c94 | 426 | dev->mm = NULL; |
c23f3445 | 427 | dev->worker = NULL; |
04b96e55 | 428 | init_llist_head(&dev->work_list); |
6b1e6cc7 JW |
429 | init_waitqueue_head(&dev->wait); |
430 | INIT_LIST_HEAD(&dev->read_list); | |
431 | INIT_LIST_HEAD(&dev->pending_list); | |
432 | spin_lock_init(&dev->iotlb_lock); | |
04b96e55 | 433 | |
3a4d5c94 MT |
434 | |
435 | for (i = 0; i < dev->nvqs; ++i) { | |
6d5e6aa8 AH |
436 | vq = dev->vqs[i]; |
437 | vq->log = NULL; | |
438 | vq->indirect = NULL; | |
439 | vq->heads = NULL; | |
440 | vq->dev = dev; | |
441 | mutex_init(&vq->mutex); | |
442 | vhost_vq_reset(dev, vq); | |
443 | if (vq->handle_kick) | |
444 | vhost_poll_init(&vq->poll, vq->handle_kick, | |
a9a08845 | 445 | EPOLLIN, dev); |
3a4d5c94 | 446 | } |
3a4d5c94 | 447 | } |
6ac1afbf | 448 | EXPORT_SYMBOL_GPL(vhost_dev_init); |
3a4d5c94 MT |
449 | |
450 | /* Caller should have device mutex */ | |
451 | long vhost_dev_check_owner(struct vhost_dev *dev) | |
452 | { | |
453 | /* Are you the owner? If not, I don't think you mean to do that */ | |
454 | return dev->mm == current->mm ? 0 : -EPERM; | |
455 | } | |
6ac1afbf | 456 | EXPORT_SYMBOL_GPL(vhost_dev_check_owner); |
3a4d5c94 | 457 | |
87d6a412 | 458 | struct vhost_attach_cgroups_struct { |
d47effe1 KK |
459 | struct vhost_work work; |
460 | struct task_struct *owner; | |
461 | int ret; | |
87d6a412 MT |
462 | }; |
463 | ||
464 | static void vhost_attach_cgroups_work(struct vhost_work *work) | |
465 | { | |
d47effe1 KK |
466 | struct vhost_attach_cgroups_struct *s; |
467 | ||
468 | s = container_of(work, struct vhost_attach_cgroups_struct, work); | |
469 | s->ret = cgroup_attach_task_all(s->owner, current); | |
87d6a412 MT |
470 | } |
471 | ||
472 | static int vhost_attach_cgroups(struct vhost_dev *dev) | |
473 | { | |
d47effe1 KK |
474 | struct vhost_attach_cgroups_struct attach; |
475 | ||
476 | attach.owner = current; | |
477 | vhost_work_init(&attach.work, vhost_attach_cgroups_work); | |
478 | vhost_work_queue(dev, &attach.work); | |
479 | vhost_work_flush(dev, &attach.work); | |
480 | return attach.ret; | |
87d6a412 MT |
481 | } |
482 | ||
05c05351 MT |
483 | /* Caller should have device mutex */ |
484 | bool vhost_dev_has_owner(struct vhost_dev *dev) | |
485 | { | |
486 | return dev->mm; | |
487 | } | |
6ac1afbf | 488 | EXPORT_SYMBOL_GPL(vhost_dev_has_owner); |
05c05351 | 489 | |
3a4d5c94 | 490 | /* Caller should have device mutex */ |
54db63c2 | 491 | long vhost_dev_set_owner(struct vhost_dev *dev) |
3a4d5c94 | 492 | { |
c23f3445 TH |
493 | struct task_struct *worker; |
494 | int err; | |
d47effe1 | 495 | |
3a4d5c94 | 496 | /* Is there an owner already? */ |
05c05351 | 497 | if (vhost_dev_has_owner(dev)) { |
c23f3445 TH |
498 | err = -EBUSY; |
499 | goto err_mm; | |
500 | } | |
d47effe1 | 501 | |
3a4d5c94 MT |
502 | /* No owner, become one */ |
503 | dev->mm = get_task_mm(current); | |
c23f3445 TH |
504 | worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid); |
505 | if (IS_ERR(worker)) { | |
506 | err = PTR_ERR(worker); | |
507 | goto err_worker; | |
508 | } | |
509 | ||
510 | dev->worker = worker; | |
87d6a412 MT |
511 | wake_up_process(worker); /* avoid contributing to loadavg */ |
512 | ||
513 | err = vhost_attach_cgroups(dev); | |
9e3d1957 MT |
514 | if (err) |
515 | goto err_cgroup; | |
c23f3445 | 516 | |
e0e9b406 JW |
517 | err = vhost_dev_alloc_iovecs(dev); |
518 | if (err) | |
519 | goto err_cgroup; | |
520 | ||
3a4d5c94 | 521 | return 0; |
9e3d1957 MT |
522 | err_cgroup: |
523 | kthread_stop(worker); | |
615cc221 | 524 | dev->worker = NULL; |
c23f3445 TH |
525 | err_worker: |
526 | if (dev->mm) | |
527 | mmput(dev->mm); | |
528 | dev->mm = NULL; | |
529 | err_mm: | |
530 | return err; | |
3a4d5c94 | 531 | } |
6ac1afbf | 532 | EXPORT_SYMBOL_GPL(vhost_dev_set_owner); |
3a4d5c94 | 533 | |
a9709d68 JW |
534 | struct vhost_umem *vhost_dev_reset_owner_prepare(void) |
535 | { | |
6c5ab651 | 536 | return kvzalloc(sizeof(struct vhost_umem), GFP_KERNEL); |
150b9e51 | 537 | } |
6ac1afbf | 538 | EXPORT_SYMBOL_GPL(vhost_dev_reset_owner_prepare); |
3a4d5c94 | 539 | |
150b9e51 | 540 | /* Caller should have device mutex */ |
a9709d68 | 541 | void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_umem *umem) |
150b9e51 | 542 | { |
47283bef MT |
543 | int i; |
544 | ||
f6f93f75 | 545 | vhost_dev_cleanup(dev); |
3a4d5c94 | 546 | |
150b9e51 | 547 | /* Restore memory to default empty mapping. */ |
a9709d68 JW |
548 | INIT_LIST_HEAD(&umem->umem_list); |
549 | dev->umem = umem; | |
47283bef MT |
550 | /* We don't need VQ locks below since vhost_dev_cleanup makes sure |
551 | * VQs aren't running. | |
552 | */ | |
553 | for (i = 0; i < dev->nvqs; ++i) | |
a9709d68 | 554 | dev->vqs[i]->umem = umem; |
3a4d5c94 | 555 | } |
6ac1afbf | 556 | EXPORT_SYMBOL_GPL(vhost_dev_reset_owner); |
3a4d5c94 | 557 | |
b211616d | 558 | void vhost_dev_stop(struct vhost_dev *dev) |
bab632d6 MT |
559 | { |
560 | int i; | |
b211616d MT |
561 | |
562 | for (i = 0; i < dev->nvqs; ++i) { | |
3ab2e420 AH |
563 | if (dev->vqs[i]->kick && dev->vqs[i]->handle_kick) { |
564 | vhost_poll_stop(&dev->vqs[i]->poll); | |
565 | vhost_poll_flush(&dev->vqs[i]->poll); | |
b211616d | 566 | } |
bab632d6 | 567 | } |
bab632d6 | 568 | } |
6ac1afbf | 569 | EXPORT_SYMBOL_GPL(vhost_dev_stop); |
bab632d6 | 570 | |
6b1e6cc7 JW |
571 | static void vhost_umem_free(struct vhost_umem *umem, |
572 | struct vhost_umem_node *node) | |
573 | { | |
574 | vhost_umem_interval_tree_remove(node, &umem->umem_tree); | |
575 | list_del(&node->link); | |
576 | kfree(node); | |
577 | umem->numem--; | |
578 | } | |
579 | ||
a9709d68 JW |
580 | static void vhost_umem_clean(struct vhost_umem *umem) |
581 | { | |
582 | struct vhost_umem_node *node, *tmp; | |
583 | ||
584 | if (!umem) | |
585 | return; | |
586 | ||
6b1e6cc7 JW |
587 | list_for_each_entry_safe(node, tmp, &umem->umem_list, link) |
588 | vhost_umem_free(umem, node); | |
589 | ||
a9709d68 JW |
590 | kvfree(umem); |
591 | } | |
592 | ||
6b1e6cc7 JW |
593 | static void vhost_clear_msg(struct vhost_dev *dev) |
594 | { | |
595 | struct vhost_msg_node *node, *n; | |
596 | ||
597 | spin_lock(&dev->iotlb_lock); | |
598 | ||
599 | list_for_each_entry_safe(node, n, &dev->read_list, node) { | |
600 | list_del(&node->node); | |
601 | kfree(node); | |
602 | } | |
603 | ||
604 | list_for_each_entry_safe(node, n, &dev->pending_list, node) { | |
605 | list_del(&node->node); | |
606 | kfree(node); | |
607 | } | |
608 | ||
609 | spin_unlock(&dev->iotlb_lock); | |
610 | } | |
611 | ||
f6f93f75 | 612 | void vhost_dev_cleanup(struct vhost_dev *dev) |
3a4d5c94 MT |
613 | { |
614 | int i; | |
d47effe1 | 615 | |
3a4d5c94 | 616 | for (i = 0; i < dev->nvqs; ++i) { |
3ab2e420 AH |
617 | if (dev->vqs[i]->error_ctx) |
618 | eventfd_ctx_put(dev->vqs[i]->error_ctx); | |
3ab2e420 AH |
619 | if (dev->vqs[i]->kick) |
620 | fput(dev->vqs[i]->kick); | |
621 | if (dev->vqs[i]->call_ctx) | |
622 | eventfd_ctx_put(dev->vqs[i]->call_ctx); | |
3ab2e420 | 623 | vhost_vq_reset(dev, dev->vqs[i]); |
3a4d5c94 | 624 | } |
e0e9b406 | 625 | vhost_dev_free_iovecs(dev); |
3a4d5c94 MT |
626 | if (dev->log_ctx) |
627 | eventfd_ctx_put(dev->log_ctx); | |
628 | dev->log_ctx = NULL; | |
3a4d5c94 | 629 | /* No one will access memory at this point */ |
a9709d68 JW |
630 | vhost_umem_clean(dev->umem); |
631 | dev->umem = NULL; | |
6b1e6cc7 JW |
632 | vhost_umem_clean(dev->iotlb); |
633 | dev->iotlb = NULL; | |
634 | vhost_clear_msg(dev); | |
a9a08845 | 635 | wake_up_interruptible_poll(&dev->wait, EPOLLIN | EPOLLRDNORM); |
04b96e55 | 636 | WARN_ON(!llist_empty(&dev->work_list)); |
78b620ce ED |
637 | if (dev->worker) { |
638 | kthread_stop(dev->worker); | |
639 | dev->worker = NULL; | |
640 | } | |
533a19b4 MT |
641 | if (dev->mm) |
642 | mmput(dev->mm); | |
643 | dev->mm = NULL; | |
3a4d5c94 | 644 | } |
6ac1afbf | 645 | EXPORT_SYMBOL_GPL(vhost_dev_cleanup); |
3a4d5c94 | 646 | |
ddd3d408 | 647 | static bool log_access_ok(void __user *log_base, u64 addr, unsigned long sz) |
3a4d5c94 MT |
648 | { |
649 | u64 a = addr / VHOST_PAGE_SIZE / 8; | |
d47effe1 | 650 | |
3a4d5c94 MT |
651 | /* Make sure 64 bit math will not overflow. */ |
652 | if (a > ULONG_MAX - (unsigned long)log_base || | |
653 | a + (unsigned long)log_base > ULONG_MAX) | |
ddd3d408 | 654 | return false; |
3a4d5c94 MT |
655 | |
656 | return access_ok(VERIFY_WRITE, log_base + a, | |
657 | (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8); | |
658 | } | |
659 | ||
ec33d031 MT |
660 | static bool vhost_overflow(u64 uaddr, u64 size) |
661 | { | |
662 | /* Make sure 64 bit math will not overflow. */ | |
663 | return uaddr > ULONG_MAX || size > ULONG_MAX || uaddr > ULONG_MAX - size; | |
664 | } | |
665 | ||
3a4d5c94 | 666 | /* Caller should have vq mutex and device mutex. */ |
ddd3d408 SH |
667 | static bool vq_memory_access_ok(void __user *log_base, struct vhost_umem *umem, |
668 | int log_all) | |
3a4d5c94 | 669 | { |
a9709d68 | 670 | struct vhost_umem_node *node; |
179b284e | 671 | |
a9709d68 | 672 | if (!umem) |
ddd3d408 | 673 | return false; |
179b284e | 674 | |
a9709d68 JW |
675 | list_for_each_entry(node, &umem->umem_list, link) { |
676 | unsigned long a = node->userspace_addr; | |
677 | ||
ec33d031 | 678 | if (vhost_overflow(node->userspace_addr, node->size)) |
ddd3d408 | 679 | return false; |
ec33d031 MT |
680 | |
681 | ||
682 | if (!access_ok(VERIFY_WRITE, (void __user *)a, | |
a9709d68 | 683 | node->size)) |
ddd3d408 | 684 | return false; |
3a4d5c94 | 685 | else if (log_all && !log_access_ok(log_base, |
a9709d68 JW |
686 | node->start, |
687 | node->size)) | |
ddd3d408 | 688 | return false; |
3a4d5c94 | 689 | } |
ddd3d408 | 690 | return true; |
3a4d5c94 MT |
691 | } |
692 | ||
f8894913 JW |
693 | static inline void __user *vhost_vq_meta_fetch(struct vhost_virtqueue *vq, |
694 | u64 addr, unsigned int size, | |
695 | int type) | |
696 | { | |
697 | const struct vhost_umem_node *node = vq->meta_iotlb[type]; | |
698 | ||
699 | if (!node) | |
700 | return NULL; | |
701 | ||
702 | return (void *)(uintptr_t)(node->userspace_addr + addr - node->start); | |
703 | } | |
704 | ||
3a4d5c94 MT |
705 | /* Can we switch to this memory table? */ |
706 | /* Caller should have device mutex but not vq mutex */ | |
ddd3d408 SH |
707 | static bool memory_access_ok(struct vhost_dev *d, struct vhost_umem *umem, |
708 | int log_all) | |
3a4d5c94 MT |
709 | { |
710 | int i; | |
d47effe1 | 711 | |
3a4d5c94 | 712 | for (i = 0; i < d->nvqs; ++i) { |
ddd3d408 | 713 | bool ok; |
ea16c514 MT |
714 | bool log; |
715 | ||
3ab2e420 | 716 | mutex_lock(&d->vqs[i]->mutex); |
ea16c514 | 717 | log = log_all || vhost_has_feature(d->vqs[i], VHOST_F_LOG_ALL); |
3a4d5c94 | 718 | /* If ring is inactive, will check when it's enabled. */ |
3ab2e420 | 719 | if (d->vqs[i]->private_data) |
a9709d68 JW |
720 | ok = vq_memory_access_ok(d->vqs[i]->log_base, |
721 | umem, log); | |
3a4d5c94 | 722 | else |
ddd3d408 | 723 | ok = true; |
3ab2e420 | 724 | mutex_unlock(&d->vqs[i]->mutex); |
3a4d5c94 | 725 | if (!ok) |
ddd3d408 | 726 | return false; |
3a4d5c94 | 727 | } |
ddd3d408 | 728 | return true; |
3a4d5c94 MT |
729 | } |
730 | ||
6b1e6cc7 JW |
731 | static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len, |
732 | struct iovec iov[], int iov_size, int access); | |
bfe2bc51 | 733 | |
72952cc0 | 734 | static int vhost_copy_to_user(struct vhost_virtqueue *vq, void __user *to, |
bfe2bc51 JW |
735 | const void *from, unsigned size) |
736 | { | |
6b1e6cc7 | 737 | int ret; |
bfe2bc51 | 738 | |
6b1e6cc7 JW |
739 | if (!vq->iotlb) |
740 | return __copy_to_user(to, from, size); | |
741 | else { | |
742 | /* This function should be called after iotlb | |
743 | * prefetch, which means we're sure that all vq | |
744 | * could be access through iotlb. So -EAGAIN should | |
745 | * not happen in this case. | |
746 | */ | |
6b1e6cc7 | 747 | struct iov_iter t; |
f8894913 JW |
748 | void __user *uaddr = vhost_vq_meta_fetch(vq, |
749 | (u64)(uintptr_t)to, size, | |
7ced6c98 | 750 | VHOST_ADDR_USED); |
f8894913 JW |
751 | |
752 | if (uaddr) | |
753 | return __copy_to_user(uaddr, from, size); | |
754 | ||
6b1e6cc7 JW |
755 | ret = translate_desc(vq, (u64)(uintptr_t)to, size, vq->iotlb_iov, |
756 | ARRAY_SIZE(vq->iotlb_iov), | |
757 | VHOST_ACCESS_WO); | |
758 | if (ret < 0) | |
759 | goto out; | |
760 | iov_iter_init(&t, WRITE, vq->iotlb_iov, ret, size); | |
761 | ret = copy_to_iter(from, size, &t); | |
762 | if (ret == size) | |
763 | ret = 0; | |
764 | } | |
765 | out: | |
766 | return ret; | |
767 | } | |
bfe2bc51 JW |
768 | |
769 | static int vhost_copy_from_user(struct vhost_virtqueue *vq, void *to, | |
72952cc0 | 770 | void __user *from, unsigned size) |
bfe2bc51 | 771 | { |
6b1e6cc7 JW |
772 | int ret; |
773 | ||
774 | if (!vq->iotlb) | |
775 | return __copy_from_user(to, from, size); | |
776 | else { | |
777 | /* This function should be called after iotlb | |
778 | * prefetch, which means we're sure that vq | |
779 | * could be access through iotlb. So -EAGAIN should | |
780 | * not happen in this case. | |
781 | */ | |
f8894913 JW |
782 | void __user *uaddr = vhost_vq_meta_fetch(vq, |
783 | (u64)(uintptr_t)from, size, | |
784 | VHOST_ADDR_DESC); | |
6b1e6cc7 | 785 | struct iov_iter f; |
f8894913 JW |
786 | |
787 | if (uaddr) | |
788 | return __copy_from_user(to, uaddr, size); | |
789 | ||
6b1e6cc7 JW |
790 | ret = translate_desc(vq, (u64)(uintptr_t)from, size, vq->iotlb_iov, |
791 | ARRAY_SIZE(vq->iotlb_iov), | |
792 | VHOST_ACCESS_RO); | |
793 | if (ret < 0) { | |
794 | vq_err(vq, "IOTLB translation failure: uaddr " | |
795 | "%p size 0x%llx\n", from, | |
796 | (unsigned long long) size); | |
797 | goto out; | |
798 | } | |
799 | iov_iter_init(&f, READ, vq->iotlb_iov, ret, size); | |
800 | ret = copy_from_iter(to, size, &f); | |
801 | if (ret == size) | |
802 | ret = 0; | |
803 | } | |
804 | ||
805 | out: | |
806 | return ret; | |
807 | } | |
808 | ||
f8894913 JW |
809 | static void __user *__vhost_get_user_slow(struct vhost_virtqueue *vq, |
810 | void __user *addr, unsigned int size, | |
811 | int type) | |
6b1e6cc7 JW |
812 | { |
813 | int ret; | |
814 | ||
6b1e6cc7 JW |
815 | ret = translate_desc(vq, (u64)(uintptr_t)addr, size, vq->iotlb_iov, |
816 | ARRAY_SIZE(vq->iotlb_iov), | |
817 | VHOST_ACCESS_RO); | |
818 | if (ret < 0) { | |
819 | vq_err(vq, "IOTLB translation failure: uaddr " | |
820 | "%p size 0x%llx\n", addr, | |
821 | (unsigned long long) size); | |
822 | return NULL; | |
823 | } | |
824 | ||
825 | if (ret != 1 || vq->iotlb_iov[0].iov_len != size) { | |
826 | vq_err(vq, "Non atomic userspace memory access: uaddr " | |
827 | "%p size 0x%llx\n", addr, | |
828 | (unsigned long long) size); | |
829 | return NULL; | |
830 | } | |
831 | ||
832 | return vq->iotlb_iov[0].iov_base; | |
833 | } | |
834 | ||
f8894913 JW |
835 | /* This function should be called after iotlb |
836 | * prefetch, which means we're sure that vq | |
837 | * could be access through iotlb. So -EAGAIN should | |
838 | * not happen in this case. | |
839 | */ | |
840 | static inline void __user *__vhost_get_user(struct vhost_virtqueue *vq, | |
841 | void *addr, unsigned int size, | |
842 | int type) | |
843 | { | |
844 | void __user *uaddr = vhost_vq_meta_fetch(vq, | |
845 | (u64)(uintptr_t)addr, size, type); | |
846 | if (uaddr) | |
847 | return uaddr; | |
848 | ||
849 | return __vhost_get_user_slow(vq, addr, size, type); | |
850 | } | |
851 | ||
852 | #define vhost_put_user(vq, x, ptr) \ | |
6b1e6cc7 JW |
853 | ({ \ |
854 | int ret = -EFAULT; \ | |
855 | if (!vq->iotlb) { \ | |
856 | ret = __put_user(x, ptr); \ | |
857 | } else { \ | |
858 | __typeof__(ptr) to = \ | |
f8894913 JW |
859 | (__typeof__(ptr)) __vhost_get_user(vq, ptr, \ |
860 | sizeof(*ptr), VHOST_ADDR_USED); \ | |
6b1e6cc7 JW |
861 | if (to != NULL) \ |
862 | ret = __put_user(x, to); \ | |
863 | else \ | |
864 | ret = -EFAULT; \ | |
865 | } \ | |
866 | ret; \ | |
867 | }) | |
868 | ||
f8894913 | 869 | #define vhost_get_user(vq, x, ptr, type) \ |
6b1e6cc7 JW |
870 | ({ \ |
871 | int ret; \ | |
872 | if (!vq->iotlb) { \ | |
873 | ret = __get_user(x, ptr); \ | |
874 | } else { \ | |
875 | __typeof__(ptr) from = \ | |
f8894913 JW |
876 | (__typeof__(ptr)) __vhost_get_user(vq, ptr, \ |
877 | sizeof(*ptr), \ | |
878 | type); \ | |
6b1e6cc7 JW |
879 | if (from != NULL) \ |
880 | ret = __get_user(x, from); \ | |
881 | else \ | |
882 | ret = -EFAULT; \ | |
883 | } \ | |
884 | ret; \ | |
885 | }) | |
886 | ||
f8894913 JW |
887 | #define vhost_get_avail(vq, x, ptr) \ |
888 | vhost_get_user(vq, x, ptr, VHOST_ADDR_AVAIL) | |
889 | ||
890 | #define vhost_get_used(vq, x, ptr) \ | |
891 | vhost_get_user(vq, x, ptr, VHOST_ADDR_USED) | |
892 | ||
6b1e6cc7 JW |
893 | static void vhost_dev_lock_vqs(struct vhost_dev *d) |
894 | { | |
895 | int i = 0; | |
896 | for (i = 0; i < d->nvqs; ++i) | |
e9cb4239 | 897 | mutex_lock_nested(&d->vqs[i]->mutex, i); |
6b1e6cc7 JW |
898 | } |
899 | ||
900 | static void vhost_dev_unlock_vqs(struct vhost_dev *d) | |
901 | { | |
902 | int i = 0; | |
903 | for (i = 0; i < d->nvqs; ++i) | |
904 | mutex_unlock(&d->vqs[i]->mutex); | |
905 | } | |
906 | ||
907 | static int vhost_new_umem_range(struct vhost_umem *umem, | |
908 | u64 start, u64 size, u64 end, | |
909 | u64 userspace_addr, int perm) | |
910 | { | |
911 | struct vhost_umem_node *tmp, *node = kmalloc(sizeof(*node), GFP_ATOMIC); | |
912 | ||
913 | if (!node) | |
914 | return -ENOMEM; | |
915 | ||
916 | if (umem->numem == max_iotlb_entries) { | |
917 | tmp = list_first_entry(&umem->umem_list, typeof(*tmp), link); | |
918 | vhost_umem_free(umem, tmp); | |
919 | } | |
920 | ||
921 | node->start = start; | |
922 | node->size = size; | |
923 | node->last = end; | |
924 | node->userspace_addr = userspace_addr; | |
925 | node->perm = perm; | |
926 | INIT_LIST_HEAD(&node->link); | |
927 | list_add_tail(&node->link, &umem->umem_list); | |
928 | vhost_umem_interval_tree_insert(node, &umem->umem_tree); | |
929 | umem->numem++; | |
930 | ||
931 | return 0; | |
932 | } | |
933 | ||
934 | static void vhost_del_umem_range(struct vhost_umem *umem, | |
935 | u64 start, u64 end) | |
936 | { | |
937 | struct vhost_umem_node *node; | |
938 | ||
939 | while ((node = vhost_umem_interval_tree_iter_first(&umem->umem_tree, | |
940 | start, end))) | |
941 | vhost_umem_free(umem, node); | |
942 | } | |
943 | ||
944 | static void vhost_iotlb_notify_vq(struct vhost_dev *d, | |
945 | struct vhost_iotlb_msg *msg) | |
946 | { | |
947 | struct vhost_msg_node *node, *n; | |
948 | ||
949 | spin_lock(&d->iotlb_lock); | |
950 | ||
951 | list_for_each_entry_safe(node, n, &d->pending_list, node) { | |
952 | struct vhost_iotlb_msg *vq_msg = &node->msg.iotlb; | |
953 | if (msg->iova <= vq_msg->iova && | |
954 | msg->iova + msg->size - 1 > vq_msg->iova && | |
955 | vq_msg->type == VHOST_IOTLB_MISS) { | |
956 | vhost_poll_queue(&node->vq->poll); | |
957 | list_del(&node->node); | |
958 | kfree(node); | |
959 | } | |
960 | } | |
961 | ||
962 | spin_unlock(&d->iotlb_lock); | |
963 | } | |
964 | ||
ddd3d408 | 965 | static bool umem_access_ok(u64 uaddr, u64 size, int access) |
6b1e6cc7 JW |
966 | { |
967 | unsigned long a = uaddr; | |
968 | ||
ec33d031 MT |
969 | /* Make sure 64 bit math will not overflow. */ |
970 | if (vhost_overflow(uaddr, size)) | |
ddd3d408 | 971 | return false; |
ec33d031 | 972 | |
6b1e6cc7 JW |
973 | if ((access & VHOST_ACCESS_RO) && |
974 | !access_ok(VERIFY_READ, (void __user *)a, size)) | |
ddd3d408 | 975 | return false; |
6b1e6cc7 JW |
976 | if ((access & VHOST_ACCESS_WO) && |
977 | !access_ok(VERIFY_WRITE, (void __user *)a, size)) | |
ddd3d408 SH |
978 | return false; |
979 | return true; | |
6b1e6cc7 JW |
980 | } |
981 | ||
72952cc0 MT |
982 | static int vhost_process_iotlb_msg(struct vhost_dev *dev, |
983 | struct vhost_iotlb_msg *msg) | |
6b1e6cc7 JW |
984 | { |
985 | int ret = 0; | |
986 | ||
1b15ad68 | 987 | mutex_lock(&dev->mutex); |
6b1e6cc7 JW |
988 | vhost_dev_lock_vqs(dev); |
989 | switch (msg->type) { | |
990 | case VHOST_IOTLB_UPDATE: | |
991 | if (!dev->iotlb) { | |
992 | ret = -EFAULT; | |
993 | break; | |
994 | } | |
ddd3d408 | 995 | if (!umem_access_ok(msg->uaddr, msg->size, msg->perm)) { |
6b1e6cc7 JW |
996 | ret = -EFAULT; |
997 | break; | |
998 | } | |
f8894913 | 999 | vhost_vq_meta_reset(dev); |
6b1e6cc7 JW |
1000 | if (vhost_new_umem_range(dev->iotlb, msg->iova, msg->size, |
1001 | msg->iova + msg->size - 1, | |
1002 | msg->uaddr, msg->perm)) { | |
1003 | ret = -ENOMEM; | |
1004 | break; | |
1005 | } | |
1006 | vhost_iotlb_notify_vq(dev, msg); | |
1007 | break; | |
1008 | case VHOST_IOTLB_INVALIDATE: | |
6f3180af JW |
1009 | if (!dev->iotlb) { |
1010 | ret = -EFAULT; | |
1011 | break; | |
1012 | } | |
f8894913 | 1013 | vhost_vq_meta_reset(dev); |
6b1e6cc7 JW |
1014 | vhost_del_umem_range(dev->iotlb, msg->iova, |
1015 | msg->iova + msg->size - 1); | |
1016 | break; | |
1017 | default: | |
1018 | ret = -EINVAL; | |
1019 | break; | |
1020 | } | |
1021 | ||
1022 | vhost_dev_unlock_vqs(dev); | |
1b15ad68 JW |
1023 | mutex_unlock(&dev->mutex); |
1024 | ||
6b1e6cc7 JW |
1025 | return ret; |
1026 | } | |
1027 | ssize_t vhost_chr_write_iter(struct vhost_dev *dev, | |
1028 | struct iov_iter *from) | |
1029 | { | |
1030 | struct vhost_msg_node node; | |
1031 | unsigned size = sizeof(struct vhost_msg); | |
1032 | size_t ret; | |
1033 | int err; | |
1034 | ||
1035 | if (iov_iter_count(from) < size) | |
1036 | return 0; | |
1037 | ret = copy_from_iter(&node.msg, size, from); | |
1038 | if (ret != size) | |
1039 | goto done; | |
1040 | ||
1041 | switch (node.msg.type) { | |
1042 | case VHOST_IOTLB_MSG: | |
1043 | err = vhost_process_iotlb_msg(dev, &node.msg.iotlb); | |
1044 | if (err) | |
1045 | ret = err; | |
1046 | break; | |
1047 | default: | |
1048 | ret = -EINVAL; | |
1049 | break; | |
1050 | } | |
1051 | ||
1052 | done: | |
1053 | return ret; | |
1054 | } | |
1055 | EXPORT_SYMBOL(vhost_chr_write_iter); | |
1056 | ||
afc9a42b | 1057 | __poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev, |
6b1e6cc7 JW |
1058 | poll_table *wait) |
1059 | { | |
afc9a42b | 1060 | __poll_t mask = 0; |
6b1e6cc7 JW |
1061 | |
1062 | poll_wait(file, &dev->wait, wait); | |
1063 | ||
1064 | if (!list_empty(&dev->read_list)) | |
a9a08845 | 1065 | mask |= EPOLLIN | EPOLLRDNORM; |
6b1e6cc7 JW |
1066 | |
1067 | return mask; | |
1068 | } | |
1069 | EXPORT_SYMBOL(vhost_chr_poll); | |
1070 | ||
1071 | ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to, | |
1072 | int noblock) | |
1073 | { | |
1074 | DEFINE_WAIT(wait); | |
1075 | struct vhost_msg_node *node; | |
1076 | ssize_t ret = 0; | |
1077 | unsigned size = sizeof(struct vhost_msg); | |
1078 | ||
1079 | if (iov_iter_count(to) < size) | |
1080 | return 0; | |
1081 | ||
1082 | while (1) { | |
1083 | if (!noblock) | |
1084 | prepare_to_wait(&dev->wait, &wait, | |
1085 | TASK_INTERRUPTIBLE); | |
1086 | ||
1087 | node = vhost_dequeue_msg(dev, &dev->read_list); | |
1088 | if (node) | |
1089 | break; | |
1090 | if (noblock) { | |
1091 | ret = -EAGAIN; | |
1092 | break; | |
1093 | } | |
1094 | if (signal_pending(current)) { | |
1095 | ret = -ERESTARTSYS; | |
1096 | break; | |
1097 | } | |
1098 | if (!dev->iotlb) { | |
1099 | ret = -EBADFD; | |
1100 | break; | |
1101 | } | |
1102 | ||
1103 | schedule(); | |
1104 | } | |
1105 | ||
1106 | if (!noblock) | |
1107 | finish_wait(&dev->wait, &wait); | |
1108 | ||
1109 | if (node) { | |
1110 | ret = copy_to_iter(&node->msg, size, to); | |
1111 | ||
1112 | if (ret != size || node->msg.type != VHOST_IOTLB_MISS) { | |
1113 | kfree(node); | |
1114 | return ret; | |
1115 | } | |
1116 | ||
1117 | vhost_enqueue_msg(dev, &dev->pending_list, node); | |
1118 | } | |
1119 | ||
1120 | return ret; | |
1121 | } | |
1122 | EXPORT_SYMBOL_GPL(vhost_chr_read_iter); | |
1123 | ||
1124 | static int vhost_iotlb_miss(struct vhost_virtqueue *vq, u64 iova, int access) | |
1125 | { | |
1126 | struct vhost_dev *dev = vq->dev; | |
1127 | struct vhost_msg_node *node; | |
1128 | struct vhost_iotlb_msg *msg; | |
1129 | ||
1130 | node = vhost_new_msg(vq, VHOST_IOTLB_MISS); | |
1131 | if (!node) | |
1132 | return -ENOMEM; | |
1133 | ||
1134 | msg = &node->msg.iotlb; | |
1135 | msg->type = VHOST_IOTLB_MISS; | |
1136 | msg->iova = iova; | |
1137 | msg->perm = access; | |
1138 | ||
1139 | vhost_enqueue_msg(dev, &dev->read_list, node); | |
1140 | ||
1141 | return 0; | |
bfe2bc51 JW |
1142 | } |
1143 | ||
ddd3d408 SH |
1144 | static bool vq_access_ok(struct vhost_virtqueue *vq, unsigned int num, |
1145 | struct vring_desc __user *desc, | |
1146 | struct vring_avail __user *avail, | |
1147 | struct vring_used __user *used) | |
6b1e6cc7 | 1148 | |
3a4d5c94 | 1149 | { |
ea16c514 | 1150 | size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0; |
6b1e6cc7 | 1151 | |
3a4d5c94 MT |
1152 | return access_ok(VERIFY_READ, desc, num * sizeof *desc) && |
1153 | access_ok(VERIFY_READ, avail, | |
8ea8cf89 | 1154 | sizeof *avail + num * sizeof *avail->ring + s) && |
3a4d5c94 | 1155 | access_ok(VERIFY_WRITE, used, |
8ea8cf89 | 1156 | sizeof *used + num * sizeof *used->ring + s); |
3a4d5c94 MT |
1157 | } |
1158 | ||
f8894913 JW |
1159 | static void vhost_vq_meta_update(struct vhost_virtqueue *vq, |
1160 | const struct vhost_umem_node *node, | |
1161 | int type) | |
1162 | { | |
1163 | int access = (type == VHOST_ADDR_USED) ? | |
1164 | VHOST_ACCESS_WO : VHOST_ACCESS_RO; | |
1165 | ||
1166 | if (likely(node->perm & access)) | |
1167 | vq->meta_iotlb[type] = node; | |
1168 | } | |
1169 | ||
ddd3d408 SH |
1170 | static bool iotlb_access_ok(struct vhost_virtqueue *vq, |
1171 | int access, u64 addr, u64 len, int type) | |
6b1e6cc7 JW |
1172 | { |
1173 | const struct vhost_umem_node *node; | |
1174 | struct vhost_umem *umem = vq->iotlb; | |
ca2c5b33 | 1175 | u64 s = 0, size, orig_addr = addr, last = addr + len - 1; |
f8894913 JW |
1176 | |
1177 | if (vhost_vq_meta_fetch(vq, addr, len, type)) | |
1178 | return true; | |
6b1e6cc7 JW |
1179 | |
1180 | while (len > s) { | |
1181 | node = vhost_umem_interval_tree_iter_first(&umem->umem_tree, | |
1182 | addr, | |
ca2c5b33 | 1183 | last); |
6b1e6cc7 JW |
1184 | if (node == NULL || node->start > addr) { |
1185 | vhost_iotlb_miss(vq, addr, access); | |
1186 | return false; | |
1187 | } else if (!(node->perm & access)) { | |
1188 | /* Report the possible access violation by | |
1189 | * request another translation from userspace. | |
1190 | */ | |
1191 | return false; | |
1192 | } | |
1193 | ||
1194 | size = node->size - addr + node->start; | |
f8894913 JW |
1195 | |
1196 | if (orig_addr == addr && size >= len) | |
1197 | vhost_vq_meta_update(vq, node, type); | |
1198 | ||
6b1e6cc7 JW |
1199 | s += size; |
1200 | addr += size; | |
1201 | } | |
1202 | ||
1203 | return true; | |
1204 | } | |
1205 | ||
1206 | int vq_iotlb_prefetch(struct vhost_virtqueue *vq) | |
1207 | { | |
1208 | size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0; | |
1209 | unsigned int num = vq->num; | |
1210 | ||
1211 | if (!vq->iotlb) | |
1212 | return 1; | |
1213 | ||
1214 | return iotlb_access_ok(vq, VHOST_ACCESS_RO, (u64)(uintptr_t)vq->desc, | |
f8894913 | 1215 | num * sizeof(*vq->desc), VHOST_ADDR_DESC) && |
6b1e6cc7 JW |
1216 | iotlb_access_ok(vq, VHOST_ACCESS_RO, (u64)(uintptr_t)vq->avail, |
1217 | sizeof *vq->avail + | |
f8894913 JW |
1218 | num * sizeof(*vq->avail->ring) + s, |
1219 | VHOST_ADDR_AVAIL) && | |
6b1e6cc7 JW |
1220 | iotlb_access_ok(vq, VHOST_ACCESS_WO, (u64)(uintptr_t)vq->used, |
1221 | sizeof *vq->used + | |
f8894913 JW |
1222 | num * sizeof(*vq->used->ring) + s, |
1223 | VHOST_ADDR_USED); | |
6b1e6cc7 JW |
1224 | } |
1225 | EXPORT_SYMBOL_GPL(vq_iotlb_prefetch); | |
1226 | ||
3a4d5c94 MT |
1227 | /* Can we log writes? */ |
1228 | /* Caller should have device mutex but not vq mutex */ | |
ddd3d408 | 1229 | bool vhost_log_access_ok(struct vhost_dev *dev) |
3a4d5c94 | 1230 | { |
a9709d68 | 1231 | return memory_access_ok(dev, dev->umem, 1); |
3a4d5c94 | 1232 | } |
6ac1afbf | 1233 | EXPORT_SYMBOL_GPL(vhost_log_access_ok); |
3a4d5c94 MT |
1234 | |
1235 | /* Verify access for write logging. */ | |
1236 | /* Caller should have vq mutex and device mutex */ | |
ddd3d408 SH |
1237 | static bool vq_log_access_ok(struct vhost_virtqueue *vq, |
1238 | void __user *log_base) | |
3a4d5c94 | 1239 | { |
ea16c514 | 1240 | size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0; |
28457ee6 | 1241 | |
a9709d68 | 1242 | return vq_memory_access_ok(log_base, vq->umem, |
ea16c514 | 1243 | vhost_has_feature(vq, VHOST_F_LOG_ALL)) && |
3a4d5c94 MT |
1244 | (!vq->log_used || log_access_ok(log_base, vq->log_addr, |
1245 | sizeof *vq->used + | |
8ea8cf89 | 1246 | vq->num * sizeof *vq->used->ring + s)); |
3a4d5c94 MT |
1247 | } |
1248 | ||
1249 | /* Can we start vq? */ | |
1250 | /* Caller should have vq mutex and device mutex */ | |
ddd3d408 | 1251 | bool vhost_vq_access_ok(struct vhost_virtqueue *vq) |
3a4d5c94 | 1252 | { |
d14d2b78 | 1253 | if (!vq_log_access_ok(vq, vq->log_base)) |
ddd3d408 | 1254 | return false; |
d65026c6 | 1255 | |
d14d2b78 SH |
1256 | /* Access validation occurs at prefetch time with IOTLB */ |
1257 | if (vq->iotlb) | |
ddd3d408 | 1258 | return true; |
d65026c6 JW |
1259 | |
1260 | return vq_access_ok(vq, vq->num, vq->desc, vq->avail, vq->used); | |
3a4d5c94 | 1261 | } |
6ac1afbf | 1262 | EXPORT_SYMBOL_GPL(vhost_vq_access_ok); |
3a4d5c94 | 1263 | |
6b1e6cc7 JW |
1264 | static struct vhost_umem *vhost_umem_alloc(void) |
1265 | { | |
6c5ab651 | 1266 | struct vhost_umem *umem = kvzalloc(sizeof(*umem), GFP_KERNEL); |
6b1e6cc7 JW |
1267 | |
1268 | if (!umem) | |
1269 | return NULL; | |
1270 | ||
f808c13f | 1271 | umem->umem_tree = RB_ROOT_CACHED; |
6b1e6cc7 JW |
1272 | umem->numem = 0; |
1273 | INIT_LIST_HEAD(&umem->umem_list); | |
1274 | ||
1275 | return umem; | |
1276 | } | |
1277 | ||
3a4d5c94 MT |
1278 | static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m) |
1279 | { | |
a9709d68 JW |
1280 | struct vhost_memory mem, *newmem; |
1281 | struct vhost_memory_region *region; | |
a9709d68 | 1282 | struct vhost_umem *newumem, *oldumem; |
3a4d5c94 | 1283 | unsigned long size = offsetof(struct vhost_memory, regions); |
98f9ca0a | 1284 | int i; |
d47effe1 | 1285 | |
7ad9c9d2 TY |
1286 | if (copy_from_user(&mem, m, size)) |
1287 | return -EFAULT; | |
3a4d5c94 MT |
1288 | if (mem.padding) |
1289 | return -EOPNOTSUPP; | |
c9ce42f7 | 1290 | if (mem.nregions > max_mem_regions) |
3a4d5c94 | 1291 | return -E2BIG; |
b2303d7b MW |
1292 | newmem = kvzalloc(struct_size(newmem, regions, mem.nregions), |
1293 | GFP_KERNEL); | |
3a4d5c94 MT |
1294 | if (!newmem) |
1295 | return -ENOMEM; | |
1296 | ||
1297 | memcpy(newmem, &mem, size); | |
7ad9c9d2 TY |
1298 | if (copy_from_user(newmem->regions, m->regions, |
1299 | mem.nregions * sizeof *m->regions)) { | |
bcfeacab | 1300 | kvfree(newmem); |
7ad9c9d2 | 1301 | return -EFAULT; |
3a4d5c94 MT |
1302 | } |
1303 | ||
6b1e6cc7 | 1304 | newumem = vhost_umem_alloc(); |
a9709d68 | 1305 | if (!newumem) { |
4de7255f | 1306 | kvfree(newmem); |
a9709d68 JW |
1307 | return -ENOMEM; |
1308 | } | |
1309 | ||
a9709d68 JW |
1310 | for (region = newmem->regions; |
1311 | region < newmem->regions + mem.nregions; | |
1312 | region++) { | |
6b1e6cc7 JW |
1313 | if (vhost_new_umem_range(newumem, |
1314 | region->guest_phys_addr, | |
1315 | region->memory_size, | |
1316 | region->guest_phys_addr + | |
1317 | region->memory_size - 1, | |
1318 | region->userspace_addr, | |
1319 | VHOST_ACCESS_RW)) | |
a9709d68 | 1320 | goto err; |
a02c3789 | 1321 | } |
a9709d68 JW |
1322 | |
1323 | if (!memory_access_ok(d, newumem, 0)) | |
1324 | goto err; | |
1325 | ||
1326 | oldumem = d->umem; | |
1327 | d->umem = newumem; | |
98f9ca0a | 1328 | |
47283bef | 1329 | /* All memory accesses are done under some VQ mutex. */ |
98f9ca0a MT |
1330 | for (i = 0; i < d->nvqs; ++i) { |
1331 | mutex_lock(&d->vqs[i]->mutex); | |
a9709d68 | 1332 | d->vqs[i]->umem = newumem; |
98f9ca0a MT |
1333 | mutex_unlock(&d->vqs[i]->mutex); |
1334 | } | |
a9709d68 JW |
1335 | |
1336 | kvfree(newmem); | |
1337 | vhost_umem_clean(oldumem); | |
3a4d5c94 | 1338 | return 0; |
a9709d68 JW |
1339 | |
1340 | err: | |
1341 | vhost_umem_clean(newumem); | |
1342 | kvfree(newmem); | |
1343 | return -EFAULT; | |
3a4d5c94 MT |
1344 | } |
1345 | ||
26b36604 | 1346 | long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) |
3a4d5c94 | 1347 | { |
cecb46f1 AV |
1348 | struct file *eventfp, *filep = NULL; |
1349 | bool pollstart = false, pollstop = false; | |
3a4d5c94 MT |
1350 | struct eventfd_ctx *ctx = NULL; |
1351 | u32 __user *idxp = argp; | |
1352 | struct vhost_virtqueue *vq; | |
1353 | struct vhost_vring_state s; | |
1354 | struct vhost_vring_file f; | |
1355 | struct vhost_vring_addr a; | |
1356 | u32 idx; | |
1357 | long r; | |
1358 | ||
1359 | r = get_user(idx, idxp); | |
1360 | if (r < 0) | |
1361 | return r; | |
0f3d9a17 | 1362 | if (idx >= d->nvqs) |
3a4d5c94 MT |
1363 | return -ENOBUFS; |
1364 | ||
3ab2e420 | 1365 | vq = d->vqs[idx]; |
3a4d5c94 MT |
1366 | |
1367 | mutex_lock(&vq->mutex); | |
1368 | ||
1369 | switch (ioctl) { | |
1370 | case VHOST_SET_VRING_NUM: | |
1371 | /* Resizing ring with an active backend? | |
1372 | * You don't want to do that. */ | |
1373 | if (vq->private_data) { | |
1374 | r = -EBUSY; | |
1375 | break; | |
1376 | } | |
7ad9c9d2 TY |
1377 | if (copy_from_user(&s, argp, sizeof s)) { |
1378 | r = -EFAULT; | |
3a4d5c94 | 1379 | break; |
7ad9c9d2 | 1380 | } |
3a4d5c94 MT |
1381 | if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) { |
1382 | r = -EINVAL; | |
1383 | break; | |
1384 | } | |
1385 | vq->num = s.num; | |
1386 | break; | |
1387 | case VHOST_SET_VRING_BASE: | |
1388 | /* Moving base with an active backend? | |
1389 | * You don't want to do that. */ | |
1390 | if (vq->private_data) { | |
1391 | r = -EBUSY; | |
1392 | break; | |
1393 | } | |
7ad9c9d2 TY |
1394 | if (copy_from_user(&s, argp, sizeof s)) { |
1395 | r = -EFAULT; | |
3a4d5c94 | 1396 | break; |
7ad9c9d2 | 1397 | } |
3a4d5c94 MT |
1398 | if (s.num > 0xffff) { |
1399 | r = -EINVAL; | |
1400 | break; | |
1401 | } | |
8d65843c | 1402 | vq->last_avail_idx = s.num; |
3a4d5c94 MT |
1403 | /* Forget the cached index value. */ |
1404 | vq->avail_idx = vq->last_avail_idx; | |
1405 | break; | |
1406 | case VHOST_GET_VRING_BASE: | |
1407 | s.index = idx; | |
1408 | s.num = vq->last_avail_idx; | |
7ad9c9d2 TY |
1409 | if (copy_to_user(argp, &s, sizeof s)) |
1410 | r = -EFAULT; | |
3a4d5c94 MT |
1411 | break; |
1412 | case VHOST_SET_VRING_ADDR: | |
7ad9c9d2 TY |
1413 | if (copy_from_user(&a, argp, sizeof a)) { |
1414 | r = -EFAULT; | |
3a4d5c94 | 1415 | break; |
7ad9c9d2 | 1416 | } |
3a4d5c94 MT |
1417 | if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) { |
1418 | r = -EOPNOTSUPP; | |
1419 | break; | |
1420 | } | |
1421 | /* For 32bit, verify that the top 32bits of the user | |
1422 | data are set to zero. */ | |
1423 | if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr || | |
1424 | (u64)(unsigned long)a.used_user_addr != a.used_user_addr || | |
1425 | (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) { | |
1426 | r = -EFAULT; | |
1427 | break; | |
1428 | } | |
5d9a07b0 MT |
1429 | |
1430 | /* Make sure it's safe to cast pointers to vring types. */ | |
1431 | BUILD_BUG_ON(__alignof__ *vq->avail > VRING_AVAIL_ALIGN_SIZE); | |
1432 | BUILD_BUG_ON(__alignof__ *vq->used > VRING_USED_ALIGN_SIZE); | |
1433 | if ((a.avail_user_addr & (VRING_AVAIL_ALIGN_SIZE - 1)) || | |
1434 | (a.used_user_addr & (VRING_USED_ALIGN_SIZE - 1)) || | |
d5424838 | 1435 | (a.log_guest_addr & (VRING_USED_ALIGN_SIZE - 1))) { |
3a4d5c94 MT |
1436 | r = -EINVAL; |
1437 | break; | |
1438 | } | |
1439 | ||
1440 | /* We only verify access here if backend is configured. | |
1441 | * If it is not, we don't as size might not have been setup. | |
1442 | * We will verify when backend is configured. */ | |
1443 | if (vq->private_data) { | |
ea16c514 | 1444 | if (!vq_access_ok(vq, vq->num, |
3a4d5c94 MT |
1445 | (void __user *)(unsigned long)a.desc_user_addr, |
1446 | (void __user *)(unsigned long)a.avail_user_addr, | |
1447 | (void __user *)(unsigned long)a.used_user_addr)) { | |
1448 | r = -EINVAL; | |
1449 | break; | |
1450 | } | |
1451 | ||
1452 | /* Also validate log access for used ring if enabled. */ | |
1453 | if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) && | |
1454 | !log_access_ok(vq->log_base, a.log_guest_addr, | |
1455 | sizeof *vq->used + | |
1456 | vq->num * sizeof *vq->used->ring)) { | |
1457 | r = -EINVAL; | |
1458 | break; | |
1459 | } | |
1460 | } | |
1461 | ||
3a4d5c94 MT |
1462 | vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG)); |
1463 | vq->desc = (void __user *)(unsigned long)a.desc_user_addr; | |
1464 | vq->avail = (void __user *)(unsigned long)a.avail_user_addr; | |
1465 | vq->log_addr = a.log_guest_addr; | |
1466 | vq->used = (void __user *)(unsigned long)a.used_user_addr; | |
1467 | break; | |
1468 | case VHOST_SET_VRING_KICK: | |
7ad9c9d2 TY |
1469 | if (copy_from_user(&f, argp, sizeof f)) { |
1470 | r = -EFAULT; | |
3a4d5c94 | 1471 | break; |
7ad9c9d2 | 1472 | } |
3a4d5c94 | 1473 | eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd); |
535297a6 MT |
1474 | if (IS_ERR(eventfp)) { |
1475 | r = PTR_ERR(eventfp); | |
1476 | break; | |
1477 | } | |
3a4d5c94 | 1478 | if (eventfp != vq->kick) { |
cecb46f1 AV |
1479 | pollstop = (filep = vq->kick) != NULL; |
1480 | pollstart = (vq->kick = eventfp) != NULL; | |
3a4d5c94 MT |
1481 | } else |
1482 | filep = eventfp; | |
1483 | break; | |
1484 | case VHOST_SET_VRING_CALL: | |
7ad9c9d2 TY |
1485 | if (copy_from_user(&f, argp, sizeof f)) { |
1486 | r = -EFAULT; | |
3a4d5c94 | 1487 | break; |
7ad9c9d2 | 1488 | } |
e050c7d9 EB |
1489 | ctx = f.fd == -1 ? NULL : eventfd_ctx_fdget(f.fd); |
1490 | if (IS_ERR(ctx)) { | |
1491 | r = PTR_ERR(ctx); | |
535297a6 MT |
1492 | break; |
1493 | } | |
e050c7d9 | 1494 | swap(ctx, vq->call_ctx); |
3a4d5c94 MT |
1495 | break; |
1496 | case VHOST_SET_VRING_ERR: | |
7ad9c9d2 TY |
1497 | if (copy_from_user(&f, argp, sizeof f)) { |
1498 | r = -EFAULT; | |
3a4d5c94 | 1499 | break; |
7ad9c9d2 | 1500 | } |
09f332a5 EB |
1501 | ctx = f.fd == -1 ? NULL : eventfd_ctx_fdget(f.fd); |
1502 | if (IS_ERR(ctx)) { | |
1503 | r = PTR_ERR(ctx); | |
535297a6 MT |
1504 | break; |
1505 | } | |
09f332a5 | 1506 | swap(ctx, vq->error_ctx); |
3a4d5c94 | 1507 | break; |
2751c988 GK |
1508 | case VHOST_SET_VRING_ENDIAN: |
1509 | r = vhost_set_vring_endian(vq, argp); | |
1510 | break; | |
1511 | case VHOST_GET_VRING_ENDIAN: | |
1512 | r = vhost_get_vring_endian(vq, idx, argp); | |
1513 | break; | |
03088137 JW |
1514 | case VHOST_SET_VRING_BUSYLOOP_TIMEOUT: |
1515 | if (copy_from_user(&s, argp, sizeof(s))) { | |
1516 | r = -EFAULT; | |
1517 | break; | |
1518 | } | |
1519 | vq->busyloop_timeout = s.num; | |
1520 | break; | |
1521 | case VHOST_GET_VRING_BUSYLOOP_TIMEOUT: | |
1522 | s.index = idx; | |
1523 | s.num = vq->busyloop_timeout; | |
1524 | if (copy_to_user(argp, &s, sizeof(s))) | |
1525 | r = -EFAULT; | |
1526 | break; | |
3a4d5c94 MT |
1527 | default: |
1528 | r = -ENOIOCTLCMD; | |
1529 | } | |
1530 | ||
1531 | if (pollstop && vq->handle_kick) | |
1532 | vhost_poll_stop(&vq->poll); | |
1533 | ||
e050c7d9 | 1534 | if (!IS_ERR_OR_NULL(ctx)) |
3a4d5c94 MT |
1535 | eventfd_ctx_put(ctx); |
1536 | if (filep) | |
1537 | fput(filep); | |
1538 | ||
1539 | if (pollstart && vq->handle_kick) | |
2b8b328b | 1540 | r = vhost_poll_start(&vq->poll, vq->kick); |
3a4d5c94 MT |
1541 | |
1542 | mutex_unlock(&vq->mutex); | |
1543 | ||
1544 | if (pollstop && vq->handle_kick) | |
1545 | vhost_poll_flush(&vq->poll); | |
1546 | return r; | |
1547 | } | |
6ac1afbf | 1548 | EXPORT_SYMBOL_GPL(vhost_vring_ioctl); |
3a4d5c94 | 1549 | |
6b1e6cc7 JW |
1550 | int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled) |
1551 | { | |
1552 | struct vhost_umem *niotlb, *oiotlb; | |
1553 | int i; | |
1554 | ||
1555 | niotlb = vhost_umem_alloc(); | |
1556 | if (!niotlb) | |
1557 | return -ENOMEM; | |
1558 | ||
1559 | oiotlb = d->iotlb; | |
1560 | d->iotlb = niotlb; | |
1561 | ||
1562 | for (i = 0; i < d->nvqs; ++i) { | |
1563 | mutex_lock(&d->vqs[i]->mutex); | |
1564 | d->vqs[i]->iotlb = niotlb; | |
1565 | mutex_unlock(&d->vqs[i]->mutex); | |
1566 | } | |
1567 | ||
1568 | vhost_umem_clean(oiotlb); | |
1569 | ||
1570 | return 0; | |
1571 | } | |
1572 | EXPORT_SYMBOL_GPL(vhost_init_device_iotlb); | |
1573 | ||
3a4d5c94 | 1574 | /* Caller must have device mutex */ |
935cdee7 | 1575 | long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) |
3a4d5c94 | 1576 | { |
d25cc43c | 1577 | struct eventfd_ctx *ctx; |
3a4d5c94 MT |
1578 | u64 p; |
1579 | long r; | |
1580 | int i, fd; | |
1581 | ||
1582 | /* If you are not the owner, you can become one */ | |
1583 | if (ioctl == VHOST_SET_OWNER) { | |
1584 | r = vhost_dev_set_owner(d); | |
1585 | goto done; | |
1586 | } | |
1587 | ||
1588 | /* You must be the owner to do anything else */ | |
1589 | r = vhost_dev_check_owner(d); | |
1590 | if (r) | |
1591 | goto done; | |
1592 | ||
1593 | switch (ioctl) { | |
1594 | case VHOST_SET_MEM_TABLE: | |
1595 | r = vhost_set_memory(d, argp); | |
1596 | break; | |
1597 | case VHOST_SET_LOG_BASE: | |
7ad9c9d2 TY |
1598 | if (copy_from_user(&p, argp, sizeof p)) { |
1599 | r = -EFAULT; | |
3a4d5c94 | 1600 | break; |
7ad9c9d2 | 1601 | } |
3a4d5c94 MT |
1602 | if ((u64)(unsigned long)p != p) { |
1603 | r = -EFAULT; | |
1604 | break; | |
1605 | } | |
1606 | for (i = 0; i < d->nvqs; ++i) { | |
1607 | struct vhost_virtqueue *vq; | |
1608 | void __user *base = (void __user *)(unsigned long)p; | |
3ab2e420 | 1609 | vq = d->vqs[i]; |
3a4d5c94 MT |
1610 | mutex_lock(&vq->mutex); |
1611 | /* If ring is inactive, will check when it's enabled. */ | |
ea16c514 | 1612 | if (vq->private_data && !vq_log_access_ok(vq, base)) |
3a4d5c94 MT |
1613 | r = -EFAULT; |
1614 | else | |
1615 | vq->log_base = base; | |
1616 | mutex_unlock(&vq->mutex); | |
1617 | } | |
1618 | break; | |
1619 | case VHOST_SET_LOG_FD: | |
1620 | r = get_user(fd, (int __user *)argp); | |
1621 | if (r < 0) | |
1622 | break; | |
d25cc43c EB |
1623 | ctx = fd == -1 ? NULL : eventfd_ctx_fdget(fd); |
1624 | if (IS_ERR(ctx)) { | |
1625 | r = PTR_ERR(ctx); | |
3a4d5c94 MT |
1626 | break; |
1627 | } | |
d25cc43c | 1628 | swap(ctx, d->log_ctx); |
3a4d5c94 | 1629 | for (i = 0; i < d->nvqs; ++i) { |
3ab2e420 AH |
1630 | mutex_lock(&d->vqs[i]->mutex); |
1631 | d->vqs[i]->log_ctx = d->log_ctx; | |
1632 | mutex_unlock(&d->vqs[i]->mutex); | |
3a4d5c94 MT |
1633 | } |
1634 | if (ctx) | |
1635 | eventfd_ctx_put(ctx); | |
3a4d5c94 MT |
1636 | break; |
1637 | default: | |
935cdee7 | 1638 | r = -ENOIOCTLCMD; |
3a4d5c94 MT |
1639 | break; |
1640 | } | |
1641 | done: | |
1642 | return r; | |
1643 | } | |
6ac1afbf | 1644 | EXPORT_SYMBOL_GPL(vhost_dev_ioctl); |
3a4d5c94 | 1645 | |
3a4d5c94 MT |
1646 | /* TODO: This is really inefficient. We need something like get_user() |
1647 | * (instruction directly accesses the data, with an exception table entry | |
1648 | * returning -EFAULT). See Documentation/x86/exception-tables.txt. | |
1649 | */ | |
1650 | static int set_bit_to_user(int nr, void __user *addr) | |
1651 | { | |
1652 | unsigned long log = (unsigned long)addr; | |
1653 | struct page *page; | |
1654 | void *base; | |
1655 | int bit = nr + (log % PAGE_SIZE) * 8; | |
1656 | int r; | |
d47effe1 | 1657 | |
3a4d5c94 | 1658 | r = get_user_pages_fast(log, 1, 1, &page); |
d6db3f5c | 1659 | if (r < 0) |
3a4d5c94 | 1660 | return r; |
d6db3f5c | 1661 | BUG_ON(r != 1); |
c6daa7ff | 1662 | base = kmap_atomic(page); |
3a4d5c94 | 1663 | set_bit(bit, base); |
c6daa7ff | 1664 | kunmap_atomic(base); |
3a4d5c94 MT |
1665 | set_page_dirty_lock(page); |
1666 | put_page(page); | |
1667 | return 0; | |
1668 | } | |
1669 | ||
1670 | static int log_write(void __user *log_base, | |
1671 | u64 write_address, u64 write_length) | |
1672 | { | |
28831ee6 | 1673 | u64 write_page = write_address / VHOST_PAGE_SIZE; |
3a4d5c94 | 1674 | int r; |
d47effe1 | 1675 | |
3a4d5c94 MT |
1676 | if (!write_length) |
1677 | return 0; | |
3bf9be40 | 1678 | write_length += write_address % VHOST_PAGE_SIZE; |
3a4d5c94 MT |
1679 | for (;;) { |
1680 | u64 base = (u64)(unsigned long)log_base; | |
28831ee6 MT |
1681 | u64 log = base + write_page / 8; |
1682 | int bit = write_page % 8; | |
3a4d5c94 MT |
1683 | if ((u64)(unsigned long)log != log) |
1684 | return -EFAULT; | |
1685 | r = set_bit_to_user(bit, (void __user *)(unsigned long)log); | |
1686 | if (r < 0) | |
1687 | return r; | |
1688 | if (write_length <= VHOST_PAGE_SIZE) | |
1689 | break; | |
1690 | write_length -= VHOST_PAGE_SIZE; | |
28831ee6 | 1691 | write_page += 1; |
3a4d5c94 MT |
1692 | } |
1693 | return r; | |
1694 | } | |
1695 | ||
1696 | int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log, | |
1697 | unsigned int log_num, u64 len) | |
1698 | { | |
1699 | int i, r; | |
1700 | ||
1701 | /* Make sure data written is seen before log. */ | |
5659338c | 1702 | smp_wmb(); |
3a4d5c94 MT |
1703 | for (i = 0; i < log_num; ++i) { |
1704 | u64 l = min(log[i].len, len); | |
1705 | r = log_write(vq->log_base, log[i].addr, l); | |
1706 | if (r < 0) | |
1707 | return r; | |
1708 | len -= l; | |
5786aee8 MT |
1709 | if (!len) { |
1710 | if (vq->log_ctx) | |
1711 | eventfd_signal(vq->log_ctx, 1); | |
3a4d5c94 | 1712 | return 0; |
5786aee8 | 1713 | } |
3a4d5c94 | 1714 | } |
3a4d5c94 MT |
1715 | /* Length written exceeds what we have stored. This is a bug. */ |
1716 | BUG(); | |
1717 | return 0; | |
1718 | } | |
6ac1afbf | 1719 | EXPORT_SYMBOL_GPL(vhost_log_write); |
3a4d5c94 | 1720 | |
2723feaa JW |
1721 | static int vhost_update_used_flags(struct vhost_virtqueue *vq) |
1722 | { | |
1723 | void __user *used; | |
bfe2bc51 JW |
1724 | if (vhost_put_user(vq, cpu_to_vhost16(vq, vq->used_flags), |
1725 | &vq->used->flags) < 0) | |
2723feaa JW |
1726 | return -EFAULT; |
1727 | if (unlikely(vq->log_used)) { | |
1728 | /* Make sure the flag is seen before log. */ | |
1729 | smp_wmb(); | |
1730 | /* Log used flag write. */ | |
1731 | used = &vq->used->flags; | |
1732 | log_write(vq->log_base, vq->log_addr + | |
1733 | (used - (void __user *)vq->used), | |
1734 | sizeof vq->used->flags); | |
1735 | if (vq->log_ctx) | |
1736 | eventfd_signal(vq->log_ctx, 1); | |
1737 | } | |
1738 | return 0; | |
1739 | } | |
1740 | ||
1741 | static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event) | |
1742 | { | |
bfe2bc51 JW |
1743 | if (vhost_put_user(vq, cpu_to_vhost16(vq, vq->avail_idx), |
1744 | vhost_avail_event(vq))) | |
2723feaa JW |
1745 | return -EFAULT; |
1746 | if (unlikely(vq->log_used)) { | |
1747 | void __user *used; | |
1748 | /* Make sure the event is seen before log. */ | |
1749 | smp_wmb(); | |
1750 | /* Log avail event write */ | |
1751 | used = vhost_avail_event(vq); | |
1752 | log_write(vq->log_base, vq->log_addr + | |
1753 | (used - (void __user *)vq->used), | |
1754 | sizeof *vhost_avail_event(vq)); | |
1755 | if (vq->log_ctx) | |
1756 | eventfd_signal(vq->log_ctx, 1); | |
1757 | } | |
1758 | return 0; | |
1759 | } | |
1760 | ||
80f7d030 | 1761 | int vhost_vq_init_access(struct vhost_virtqueue *vq) |
2723feaa | 1762 | { |
3b1bbe89 | 1763 | __virtio16 last_used_idx; |
2723feaa | 1764 | int r; |
e1f33be9 GK |
1765 | bool is_le = vq->is_le; |
1766 | ||
cda8bba0 | 1767 | if (!vq->private_data) |
2723feaa | 1768 | return 0; |
2751c988 GK |
1769 | |
1770 | vhost_init_is_le(vq); | |
2723feaa JW |
1771 | |
1772 | r = vhost_update_used_flags(vq); | |
1773 | if (r) | |
e1f33be9 | 1774 | goto err; |
2723feaa | 1775 | vq->signalled_used_valid = false; |
6b1e6cc7 JW |
1776 | if (!vq->iotlb && |
1777 | !access_ok(VERIFY_READ, &vq->used->idx, sizeof vq->used->idx)) { | |
e1f33be9 GK |
1778 | r = -EFAULT; |
1779 | goto err; | |
1780 | } | |
f8894913 | 1781 | r = vhost_get_used(vq, last_used_idx, &vq->used->idx); |
6b1e6cc7 JW |
1782 | if (r) { |
1783 | vq_err(vq, "Can't access used idx at %p\n", | |
1784 | &vq->used->idx); | |
e1f33be9 | 1785 | goto err; |
6b1e6cc7 | 1786 | } |
3b1bbe89 | 1787 | vq->last_used_idx = vhost16_to_cpu(vq, last_used_idx); |
64f7f051 | 1788 | return 0; |
6b1e6cc7 | 1789 | |
e1f33be9 GK |
1790 | err: |
1791 | vq->is_le = is_le; | |
1792 | return r; | |
2723feaa | 1793 | } |
80f7d030 | 1794 | EXPORT_SYMBOL_GPL(vhost_vq_init_access); |
2723feaa | 1795 | |
47283bef | 1796 | static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len, |
6b1e6cc7 | 1797 | struct iovec iov[], int iov_size, int access) |
3a4d5c94 | 1798 | { |
a9709d68 | 1799 | const struct vhost_umem_node *node; |
6b1e6cc7 JW |
1800 | struct vhost_dev *dev = vq->dev; |
1801 | struct vhost_umem *umem = dev->iotlb ? dev->iotlb : dev->umem; | |
3a4d5c94 MT |
1802 | struct iovec *_iov; |
1803 | u64 s = 0; | |
1804 | int ret = 0; | |
1805 | ||
3a4d5c94 MT |
1806 | while ((u64)len > s) { |
1807 | u64 size; | |
7b3384fc | 1808 | if (unlikely(ret >= iov_size)) { |
3a4d5c94 MT |
1809 | ret = -ENOBUFS; |
1810 | break; | |
1811 | } | |
6b1e6cc7 | 1812 | |
a9709d68 JW |
1813 | node = vhost_umem_interval_tree_iter_first(&umem->umem_tree, |
1814 | addr, addr + len - 1); | |
1815 | if (node == NULL || node->start > addr) { | |
6b1e6cc7 JW |
1816 | if (umem != dev->iotlb) { |
1817 | ret = -EFAULT; | |
1818 | break; | |
1819 | } | |
1820 | ret = -EAGAIN; | |
1821 | break; | |
1822 | } else if (!(node->perm & access)) { | |
1823 | ret = -EPERM; | |
3a4d5c94 MT |
1824 | break; |
1825 | } | |
6b1e6cc7 | 1826 | |
3a4d5c94 | 1827 | _iov = iov + ret; |
a9709d68 | 1828 | size = node->size - addr + node->start; |
bd97120f | 1829 | _iov->iov_len = min((u64)len - s, size); |
a8d3782f | 1830 | _iov->iov_base = (void __user *)(unsigned long) |
a9709d68 | 1831 | (node->userspace_addr + addr - node->start); |
3a4d5c94 MT |
1832 | s += size; |
1833 | addr += size; | |
1834 | ++ret; | |
1835 | } | |
1836 | ||
6b1e6cc7 JW |
1837 | if (ret == -EAGAIN) |
1838 | vhost_iotlb_miss(vq, addr, access); | |
3a4d5c94 MT |
1839 | return ret; |
1840 | } | |
1841 | ||
1842 | /* Each buffer in the virtqueues is actually a chain of descriptors. This | |
1843 | * function returns the next descriptor in the chain, | |
1844 | * or -1U if we're at the end. */ | |
3b1bbe89 | 1845 | static unsigned next_desc(struct vhost_virtqueue *vq, struct vring_desc *desc) |
3a4d5c94 MT |
1846 | { |
1847 | unsigned int next; | |
1848 | ||
1849 | /* If this descriptor says it doesn't chain, we're done. */ | |
3b1bbe89 | 1850 | if (!(desc->flags & cpu_to_vhost16(vq, VRING_DESC_F_NEXT))) |
3a4d5c94 MT |
1851 | return -1U; |
1852 | ||
1853 | /* Check they're not leading us off end of descriptors. */ | |
3a5db0b1 | 1854 | next = vhost16_to_cpu(vq, READ_ONCE(desc->next)); |
3a4d5c94 MT |
1855 | return next; |
1856 | } | |
1857 | ||
47283bef | 1858 | static int get_indirect(struct vhost_virtqueue *vq, |
7b3384fc MT |
1859 | struct iovec iov[], unsigned int iov_size, |
1860 | unsigned int *out_num, unsigned int *in_num, | |
1861 | struct vhost_log *log, unsigned int *log_num, | |
1862 | struct vring_desc *indirect) | |
3a4d5c94 MT |
1863 | { |
1864 | struct vring_desc desc; | |
1865 | unsigned int i = 0, count, found = 0; | |
3b1bbe89 | 1866 | u32 len = vhost32_to_cpu(vq, indirect->len); |
aad9a1ce | 1867 | struct iov_iter from; |
6b1e6cc7 | 1868 | int ret, access; |
3a4d5c94 MT |
1869 | |
1870 | /* Sanity check */ | |
3b1bbe89 | 1871 | if (unlikely(len % sizeof desc)) { |
3a4d5c94 MT |
1872 | vq_err(vq, "Invalid length in indirect descriptor: " |
1873 | "len 0x%llx not multiple of 0x%zx\n", | |
3b1bbe89 | 1874 | (unsigned long long)len, |
3a4d5c94 MT |
1875 | sizeof desc); |
1876 | return -EINVAL; | |
1877 | } | |
1878 | ||
3b1bbe89 | 1879 | ret = translate_desc(vq, vhost64_to_cpu(vq, indirect->addr), len, vq->indirect, |
6b1e6cc7 | 1880 | UIO_MAXIOV, VHOST_ACCESS_RO); |
7b3384fc | 1881 | if (unlikely(ret < 0)) { |
6b1e6cc7 JW |
1882 | if (ret != -EAGAIN) |
1883 | vq_err(vq, "Translation failure %d in indirect.\n", ret); | |
3a4d5c94 MT |
1884 | return ret; |
1885 | } | |
aad9a1ce | 1886 | iov_iter_init(&from, READ, vq->indirect, ret, len); |
3a4d5c94 MT |
1887 | |
1888 | /* We will use the result as an address to read from, so most | |
1889 | * architectures only need a compiler barrier here. */ | |
1890 | read_barrier_depends(); | |
1891 | ||
3b1bbe89 | 1892 | count = len / sizeof desc; |
3a4d5c94 MT |
1893 | /* Buffers are chained via a 16 bit next field, so |
1894 | * we can have at most 2^16 of these. */ | |
7b3384fc | 1895 | if (unlikely(count > USHRT_MAX + 1)) { |
3a4d5c94 MT |
1896 | vq_err(vq, "Indirect buffer length too big: %d\n", |
1897 | indirect->len); | |
1898 | return -E2BIG; | |
1899 | } | |
1900 | ||
1901 | do { | |
1902 | unsigned iov_count = *in_num + *out_num; | |
7b3384fc | 1903 | if (unlikely(++found > count)) { |
3a4d5c94 MT |
1904 | vq_err(vq, "Loop detected: last one at %u " |
1905 | "indirect size %u\n", | |
1906 | i, count); | |
1907 | return -EINVAL; | |
1908 | } | |
cbbd26b8 | 1909 | if (unlikely(!copy_from_iter_full(&desc, sizeof(desc), &from))) { |
3a4d5c94 | 1910 | vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n", |
3b1bbe89 | 1911 | i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc); |
3a4d5c94 MT |
1912 | return -EINVAL; |
1913 | } | |
3b1bbe89 | 1914 | if (unlikely(desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT))) { |
3a4d5c94 | 1915 | vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n", |
3b1bbe89 | 1916 | i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc); |
3a4d5c94 MT |
1917 | return -EINVAL; |
1918 | } | |
1919 | ||
6b1e6cc7 JW |
1920 | if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE)) |
1921 | access = VHOST_ACCESS_WO; | |
1922 | else | |
1923 | access = VHOST_ACCESS_RO; | |
1924 | ||
3b1bbe89 MT |
1925 | ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr), |
1926 | vhost32_to_cpu(vq, desc.len), iov + iov_count, | |
6b1e6cc7 | 1927 | iov_size - iov_count, access); |
7b3384fc | 1928 | if (unlikely(ret < 0)) { |
6b1e6cc7 JW |
1929 | if (ret != -EAGAIN) |
1930 | vq_err(vq, "Translation failure %d indirect idx %d\n", | |
1931 | ret, i); | |
3a4d5c94 MT |
1932 | return ret; |
1933 | } | |
1934 | /* If this is an input descriptor, increment that count. */ | |
6b1e6cc7 | 1935 | if (access == VHOST_ACCESS_WO) { |
3a4d5c94 MT |
1936 | *in_num += ret; |
1937 | if (unlikely(log)) { | |
3b1bbe89 MT |
1938 | log[*log_num].addr = vhost64_to_cpu(vq, desc.addr); |
1939 | log[*log_num].len = vhost32_to_cpu(vq, desc.len); | |
3a4d5c94 MT |
1940 | ++*log_num; |
1941 | } | |
1942 | } else { | |
1943 | /* If it's an output descriptor, they're all supposed | |
1944 | * to come before any input descriptors. */ | |
7b3384fc | 1945 | if (unlikely(*in_num)) { |
3a4d5c94 MT |
1946 | vq_err(vq, "Indirect descriptor " |
1947 | "has out after in: idx %d\n", i); | |
1948 | return -EINVAL; | |
1949 | } | |
1950 | *out_num += ret; | |
1951 | } | |
3b1bbe89 | 1952 | } while ((i = next_desc(vq, &desc)) != -1); |
3a4d5c94 MT |
1953 | return 0; |
1954 | } | |
1955 | ||
1956 | /* This looks in the virtqueue and for the first available buffer, and converts | |
1957 | * it to an iovec for convenient access. Since descriptors consist of some | |
1958 | * number of output then some number of input descriptors, it's actually two | |
1959 | * iovecs, but we pack them into one and note how many of each there were. | |
1960 | * | |
d5675bd2 MT |
1961 | * This function returns the descriptor number found, or vq->num (which is |
1962 | * never a valid descriptor number) if none was found. A negative code is | |
1963 | * returned on error. */ | |
47283bef | 1964 | int vhost_get_vq_desc(struct vhost_virtqueue *vq, |
d5675bd2 MT |
1965 | struct iovec iov[], unsigned int iov_size, |
1966 | unsigned int *out_num, unsigned int *in_num, | |
1967 | struct vhost_log *log, unsigned int *log_num) | |
3a4d5c94 MT |
1968 | { |
1969 | struct vring_desc desc; | |
1970 | unsigned int i, head, found = 0; | |
1971 | u16 last_avail_idx; | |
3b1bbe89 MT |
1972 | __virtio16 avail_idx; |
1973 | __virtio16 ring_head; | |
6b1e6cc7 | 1974 | int ret, access; |
3a4d5c94 MT |
1975 | |
1976 | /* Check it isn't doing very strange things with descriptor numbers. */ | |
1977 | last_avail_idx = vq->last_avail_idx; | |
3a4d5c94 | 1978 | |
e3b56cdd | 1979 | if (vq->avail_idx == vq->last_avail_idx) { |
f8894913 | 1980 | if (unlikely(vhost_get_avail(vq, avail_idx, &vq->avail->idx))) { |
e3b56cdd JW |
1981 | vq_err(vq, "Failed to access avail idx at %p\n", |
1982 | &vq->avail->idx); | |
1983 | return -EFAULT; | |
1984 | } | |
1985 | vq->avail_idx = vhost16_to_cpu(vq, avail_idx); | |
3a4d5c94 | 1986 | |
e3b56cdd JW |
1987 | if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) { |
1988 | vq_err(vq, "Guest moved used index from %u to %u", | |
1989 | last_avail_idx, vq->avail_idx); | |
1990 | return -EFAULT; | |
1991 | } | |
1992 | ||
1993 | /* If there's nothing new since last we looked, return | |
1994 | * invalid. | |
1995 | */ | |
1996 | if (vq->avail_idx == last_avail_idx) | |
1997 | return vq->num; | |
3a4d5c94 | 1998 | |
e3b56cdd JW |
1999 | /* Only get avail ring entries after they have been |
2000 | * exposed by guest. | |
2001 | */ | |
2002 | smp_rmb(); | |
2003 | } | |
3a4d5c94 MT |
2004 | |
2005 | /* Grab the next descriptor number they're advertising, and increment | |
2006 | * the index we've seen. */ | |
f8894913 | 2007 | if (unlikely(vhost_get_avail(vq, ring_head, |
bfe2bc51 | 2008 | &vq->avail->ring[last_avail_idx & (vq->num - 1)]))) { |
3a4d5c94 MT |
2009 | vq_err(vq, "Failed to read head: idx %d address %p\n", |
2010 | last_avail_idx, | |
2011 | &vq->avail->ring[last_avail_idx % vq->num]); | |
d5675bd2 | 2012 | return -EFAULT; |
3a4d5c94 MT |
2013 | } |
2014 | ||
3b1bbe89 MT |
2015 | head = vhost16_to_cpu(vq, ring_head); |
2016 | ||
3a4d5c94 | 2017 | /* If their number is silly, that's an error. */ |
7b3384fc | 2018 | if (unlikely(head >= vq->num)) { |
3a4d5c94 MT |
2019 | vq_err(vq, "Guest says index %u > %u is available", |
2020 | head, vq->num); | |
d5675bd2 | 2021 | return -EINVAL; |
3a4d5c94 MT |
2022 | } |
2023 | ||
2024 | /* When we start there are none of either input nor output. */ | |
2025 | *out_num = *in_num = 0; | |
2026 | if (unlikely(log)) | |
2027 | *log_num = 0; | |
2028 | ||
2029 | i = head; | |
2030 | do { | |
2031 | unsigned iov_count = *in_num + *out_num; | |
7b3384fc | 2032 | if (unlikely(i >= vq->num)) { |
3a4d5c94 MT |
2033 | vq_err(vq, "Desc index is %u > %u, head = %u", |
2034 | i, vq->num, head); | |
d5675bd2 | 2035 | return -EINVAL; |
3a4d5c94 | 2036 | } |
7b3384fc | 2037 | if (unlikely(++found > vq->num)) { |
3a4d5c94 MT |
2038 | vq_err(vq, "Loop detected: last one at %u " |
2039 | "vq size %u head %u\n", | |
2040 | i, vq->num, head); | |
d5675bd2 | 2041 | return -EINVAL; |
3a4d5c94 | 2042 | } |
bfe2bc51 JW |
2043 | ret = vhost_copy_from_user(vq, &desc, vq->desc + i, |
2044 | sizeof desc); | |
7b3384fc | 2045 | if (unlikely(ret)) { |
3a4d5c94 MT |
2046 | vq_err(vq, "Failed to get descriptor: idx %d addr %p\n", |
2047 | i, vq->desc + i); | |
d5675bd2 | 2048 | return -EFAULT; |
3a4d5c94 | 2049 | } |
3b1bbe89 | 2050 | if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT)) { |
47283bef | 2051 | ret = get_indirect(vq, iov, iov_size, |
3a4d5c94 MT |
2052 | out_num, in_num, |
2053 | log, log_num, &desc); | |
7b3384fc | 2054 | if (unlikely(ret < 0)) { |
6b1e6cc7 JW |
2055 | if (ret != -EAGAIN) |
2056 | vq_err(vq, "Failure detected " | |
2057 | "in indirect descriptor at idx %d\n", i); | |
d5675bd2 | 2058 | return ret; |
3a4d5c94 MT |
2059 | } |
2060 | continue; | |
2061 | } | |
2062 | ||
6b1e6cc7 JW |
2063 | if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE)) |
2064 | access = VHOST_ACCESS_WO; | |
2065 | else | |
2066 | access = VHOST_ACCESS_RO; | |
3b1bbe89 MT |
2067 | ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr), |
2068 | vhost32_to_cpu(vq, desc.len), iov + iov_count, | |
6b1e6cc7 | 2069 | iov_size - iov_count, access); |
7b3384fc | 2070 | if (unlikely(ret < 0)) { |
6b1e6cc7 JW |
2071 | if (ret != -EAGAIN) |
2072 | vq_err(vq, "Translation failure %d descriptor idx %d\n", | |
2073 | ret, i); | |
d5675bd2 | 2074 | return ret; |
3a4d5c94 | 2075 | } |
6b1e6cc7 | 2076 | if (access == VHOST_ACCESS_WO) { |
3a4d5c94 MT |
2077 | /* If this is an input descriptor, |
2078 | * increment that count. */ | |
2079 | *in_num += ret; | |
2080 | if (unlikely(log)) { | |
3b1bbe89 MT |
2081 | log[*log_num].addr = vhost64_to_cpu(vq, desc.addr); |
2082 | log[*log_num].len = vhost32_to_cpu(vq, desc.len); | |
3a4d5c94 MT |
2083 | ++*log_num; |
2084 | } | |
2085 | } else { | |
2086 | /* If it's an output descriptor, they're all supposed | |
2087 | * to come before any input descriptors. */ | |
7b3384fc | 2088 | if (unlikely(*in_num)) { |
3a4d5c94 MT |
2089 | vq_err(vq, "Descriptor has out after in: " |
2090 | "idx %d\n", i); | |
d5675bd2 | 2091 | return -EINVAL; |
3a4d5c94 MT |
2092 | } |
2093 | *out_num += ret; | |
2094 | } | |
3b1bbe89 | 2095 | } while ((i = next_desc(vq, &desc)) != -1); |
3a4d5c94 MT |
2096 | |
2097 | /* On success, increment avail index. */ | |
2098 | vq->last_avail_idx++; | |
8ea8cf89 MT |
2099 | |
2100 | /* Assume notifications from guest are disabled at this point, | |
2101 | * if they aren't we would need to update avail_event index. */ | |
2102 | BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY)); | |
3a4d5c94 MT |
2103 | return head; |
2104 | } | |
6ac1afbf | 2105 | EXPORT_SYMBOL_GPL(vhost_get_vq_desc); |
3a4d5c94 MT |
2106 | |
2107 | /* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */ | |
8dd014ad | 2108 | void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n) |
3a4d5c94 | 2109 | { |
8dd014ad | 2110 | vq->last_avail_idx -= n; |
3a4d5c94 | 2111 | } |
6ac1afbf | 2112 | EXPORT_SYMBOL_GPL(vhost_discard_vq_desc); |
3a4d5c94 MT |
2113 | |
2114 | /* After we've used one of their buffers, we tell them about it. We'll then | |
2115 | * want to notify the guest, using eventfd. */ | |
2116 | int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len) | |
2117 | { | |
3b1bbe89 MT |
2118 | struct vring_used_elem heads = { |
2119 | cpu_to_vhost32(vq, head), | |
2120 | cpu_to_vhost32(vq, len) | |
2121 | }; | |
3a4d5c94 | 2122 | |
c49e4e57 | 2123 | return vhost_add_used_n(vq, &heads, 1); |
3a4d5c94 | 2124 | } |
6ac1afbf | 2125 | EXPORT_SYMBOL_GPL(vhost_add_used); |
3a4d5c94 | 2126 | |
8dd014ad DS |
2127 | static int __vhost_add_used_n(struct vhost_virtqueue *vq, |
2128 | struct vring_used_elem *heads, | |
2129 | unsigned count) | |
2130 | { | |
2131 | struct vring_used_elem __user *used; | |
8ea8cf89 | 2132 | u16 old, new; |
8dd014ad DS |
2133 | int start; |
2134 | ||
5fba13b5 | 2135 | start = vq->last_used_idx & (vq->num - 1); |
8dd014ad | 2136 | used = vq->used->ring + start; |
c49e4e57 | 2137 | if (count == 1) { |
bfe2bc51 | 2138 | if (vhost_put_user(vq, heads[0].id, &used->id)) { |
c49e4e57 JW |
2139 | vq_err(vq, "Failed to write used id"); |
2140 | return -EFAULT; | |
2141 | } | |
bfe2bc51 | 2142 | if (vhost_put_user(vq, heads[0].len, &used->len)) { |
c49e4e57 JW |
2143 | vq_err(vq, "Failed to write used len"); |
2144 | return -EFAULT; | |
2145 | } | |
bfe2bc51 | 2146 | } else if (vhost_copy_to_user(vq, used, heads, count * sizeof *used)) { |
8dd014ad DS |
2147 | vq_err(vq, "Failed to write used"); |
2148 | return -EFAULT; | |
2149 | } | |
2150 | if (unlikely(vq->log_used)) { | |
2151 | /* Make sure data is seen before log. */ | |
2152 | smp_wmb(); | |
2153 | /* Log used ring entry write. */ | |
2154 | log_write(vq->log_base, | |
2155 | vq->log_addr + | |
2156 | ((void __user *)used - (void __user *)vq->used), | |
2157 | count * sizeof *used); | |
2158 | } | |
8ea8cf89 MT |
2159 | old = vq->last_used_idx; |
2160 | new = (vq->last_used_idx += count); | |
2161 | /* If the driver never bothers to signal in a very long while, | |
2162 | * used index might wrap around. If that happens, invalidate | |
2163 | * signalled_used index we stored. TODO: make sure driver | |
2164 | * signals at least once in 2^16 and remove this. */ | |
2165 | if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old))) | |
2166 | vq->signalled_used_valid = false; | |
8dd014ad DS |
2167 | return 0; |
2168 | } | |
2169 | ||
2170 | /* After we've used one of their buffers, we tell them about it. We'll then | |
2171 | * want to notify the guest, using eventfd. */ | |
2172 | int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads, | |
2173 | unsigned count) | |
2174 | { | |
2175 | int start, n, r; | |
2176 | ||
5fba13b5 | 2177 | start = vq->last_used_idx & (vq->num - 1); |
8dd014ad DS |
2178 | n = vq->num - start; |
2179 | if (n < count) { | |
2180 | r = __vhost_add_used_n(vq, heads, n); | |
2181 | if (r < 0) | |
2182 | return r; | |
2183 | heads += n; | |
2184 | count -= n; | |
2185 | } | |
2186 | r = __vhost_add_used_n(vq, heads, count); | |
2187 | ||
2188 | /* Make sure buffer is written before we update index. */ | |
2189 | smp_wmb(); | |
bfe2bc51 JW |
2190 | if (vhost_put_user(vq, cpu_to_vhost16(vq, vq->last_used_idx), |
2191 | &vq->used->idx)) { | |
8dd014ad DS |
2192 | vq_err(vq, "Failed to increment used idx"); |
2193 | return -EFAULT; | |
2194 | } | |
2195 | if (unlikely(vq->log_used)) { | |
2196 | /* Log used index update. */ | |
2197 | log_write(vq->log_base, | |
2198 | vq->log_addr + offsetof(struct vring_used, idx), | |
2199 | sizeof vq->used->idx); | |
2200 | if (vq->log_ctx) | |
2201 | eventfd_signal(vq->log_ctx, 1); | |
2202 | } | |
2203 | return r; | |
2204 | } | |
6ac1afbf | 2205 | EXPORT_SYMBOL_GPL(vhost_add_used_n); |
8dd014ad | 2206 | |
8ea8cf89 | 2207 | static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq) |
3a4d5c94 | 2208 | { |
3b1bbe89 MT |
2209 | __u16 old, new; |
2210 | __virtio16 event; | |
8ea8cf89 | 2211 | bool v; |
8d65843c JW |
2212 | /* Flush out used index updates. This is paired |
2213 | * with the barrier that the Guest executes when enabling | |
2214 | * interrupts. */ | |
2215 | smp_mb(); | |
0d499356 | 2216 | |
ea16c514 | 2217 | if (vhost_has_feature(vq, VIRTIO_F_NOTIFY_ON_EMPTY) && |
8ea8cf89 MT |
2218 | unlikely(vq->avail_idx == vq->last_avail_idx)) |
2219 | return true; | |
2220 | ||
ea16c514 | 2221 | if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) { |
3b1bbe89 | 2222 | __virtio16 flags; |
f8894913 | 2223 | if (vhost_get_avail(vq, flags, &vq->avail->flags)) { |
8ea8cf89 MT |
2224 | vq_err(vq, "Failed to get flags"); |
2225 | return true; | |
2226 | } | |
3b1bbe89 | 2227 | return !(flags & cpu_to_vhost16(vq, VRING_AVAIL_F_NO_INTERRUPT)); |
3a4d5c94 | 2228 | } |
8ea8cf89 MT |
2229 | old = vq->signalled_used; |
2230 | v = vq->signalled_used_valid; | |
2231 | new = vq->signalled_used = vq->last_used_idx; | |
2232 | vq->signalled_used_valid = true; | |
3a4d5c94 | 2233 | |
8ea8cf89 MT |
2234 | if (unlikely(!v)) |
2235 | return true; | |
3a4d5c94 | 2236 | |
f8894913 | 2237 | if (vhost_get_avail(vq, event, vhost_used_event(vq))) { |
8ea8cf89 MT |
2238 | vq_err(vq, "Failed to get used event idx"); |
2239 | return true; | |
2240 | } | |
8d65843c | 2241 | return vring_need_event(vhost16_to_cpu(vq, event), new, old); |
8ea8cf89 MT |
2242 | } |
2243 | ||
2244 | /* This actually signals the guest, using eventfd. */ | |
2245 | void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq) | |
2246 | { | |
3a4d5c94 | 2247 | /* Signal the Guest tell them we used something up. */ |
8ea8cf89 | 2248 | if (vq->call_ctx && vhost_notify(dev, vq)) |
3a4d5c94 MT |
2249 | eventfd_signal(vq->call_ctx, 1); |
2250 | } | |
6ac1afbf | 2251 | EXPORT_SYMBOL_GPL(vhost_signal); |
3a4d5c94 MT |
2252 | |
2253 | /* And here's the combo meal deal. Supersize me! */ | |
2254 | void vhost_add_used_and_signal(struct vhost_dev *dev, | |
2255 | struct vhost_virtqueue *vq, | |
2256 | unsigned int head, int len) | |
2257 | { | |
2258 | vhost_add_used(vq, head, len); | |
2259 | vhost_signal(dev, vq); | |
2260 | } | |
6ac1afbf | 2261 | EXPORT_SYMBOL_GPL(vhost_add_used_and_signal); |
3a4d5c94 | 2262 | |
8dd014ad DS |
2263 | /* multi-buffer version of vhost_add_used_and_signal */ |
2264 | void vhost_add_used_and_signal_n(struct vhost_dev *dev, | |
2265 | struct vhost_virtqueue *vq, | |
2266 | struct vring_used_elem *heads, unsigned count) | |
2267 | { | |
2268 | vhost_add_used_n(vq, heads, count); | |
2269 | vhost_signal(dev, vq); | |
2270 | } | |
6ac1afbf | 2271 | EXPORT_SYMBOL_GPL(vhost_add_used_and_signal_n); |
8dd014ad | 2272 | |
d4a60603 JW |
2273 | /* return true if we're sure that avaiable ring is empty */ |
2274 | bool vhost_vq_avail_empty(struct vhost_dev *dev, struct vhost_virtqueue *vq) | |
2275 | { | |
2276 | __virtio16 avail_idx; | |
2277 | int r; | |
2278 | ||
275bf960 JW |
2279 | if (vq->avail_idx != vq->last_avail_idx) |
2280 | return false; | |
2281 | ||
f8894913 | 2282 | r = vhost_get_avail(vq, avail_idx, &vq->avail->idx); |
275bf960 | 2283 | if (unlikely(r)) |
d4a60603 | 2284 | return false; |
275bf960 | 2285 | vq->avail_idx = vhost16_to_cpu(vq, avail_idx); |
d4a60603 | 2286 | |
275bf960 | 2287 | return vq->avail_idx == vq->last_avail_idx; |
d4a60603 JW |
2288 | } |
2289 | EXPORT_SYMBOL_GPL(vhost_vq_avail_empty); | |
2290 | ||
3a4d5c94 | 2291 | /* OK, now we need to know about added descriptors. */ |
8ea8cf89 | 2292 | bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq) |
3a4d5c94 | 2293 | { |
3b1bbe89 | 2294 | __virtio16 avail_idx; |
3a4d5c94 | 2295 | int r; |
d47effe1 | 2296 | |
3a4d5c94 MT |
2297 | if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY)) |
2298 | return false; | |
2299 | vq->used_flags &= ~VRING_USED_F_NO_NOTIFY; | |
ea16c514 | 2300 | if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) { |
2723feaa | 2301 | r = vhost_update_used_flags(vq); |
8ea8cf89 MT |
2302 | if (r) { |
2303 | vq_err(vq, "Failed to enable notification at %p: %d\n", | |
2304 | &vq->used->flags, r); | |
2305 | return false; | |
2306 | } | |
2307 | } else { | |
2723feaa | 2308 | r = vhost_update_avail_event(vq, vq->avail_idx); |
8ea8cf89 MT |
2309 | if (r) { |
2310 | vq_err(vq, "Failed to update avail event index at %p: %d\n", | |
2311 | vhost_avail_event(vq), r); | |
2312 | return false; | |
2313 | } | |
2314 | } | |
3a4d5c94 MT |
2315 | /* They could have slipped one in as we were doing that: make |
2316 | * sure it's written, then check again. */ | |
5659338c | 2317 | smp_mb(); |
f8894913 | 2318 | r = vhost_get_avail(vq, avail_idx, &vq->avail->idx); |
3a4d5c94 MT |
2319 | if (r) { |
2320 | vq_err(vq, "Failed to check avail idx at %p: %d\n", | |
2321 | &vq->avail->idx, r); | |
2322 | return false; | |
2323 | } | |
2324 | ||
3b1bbe89 | 2325 | return vhost16_to_cpu(vq, avail_idx) != vq->avail_idx; |
3a4d5c94 | 2326 | } |
6ac1afbf | 2327 | EXPORT_SYMBOL_GPL(vhost_enable_notify); |
3a4d5c94 MT |
2328 | |
2329 | /* We don't need to be notified again. */ | |
8ea8cf89 | 2330 | void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq) |
3a4d5c94 MT |
2331 | { |
2332 | int r; | |
d47effe1 | 2333 | |
3a4d5c94 MT |
2334 | if (vq->used_flags & VRING_USED_F_NO_NOTIFY) |
2335 | return; | |
2336 | vq->used_flags |= VRING_USED_F_NO_NOTIFY; | |
ea16c514 | 2337 | if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) { |
2723feaa | 2338 | r = vhost_update_used_flags(vq); |
8ea8cf89 MT |
2339 | if (r) |
2340 | vq_err(vq, "Failed to enable notification at %p: %d\n", | |
2341 | &vq->used->flags, r); | |
2342 | } | |
3a4d5c94 | 2343 | } |
6ac1afbf AH |
2344 | EXPORT_SYMBOL_GPL(vhost_disable_notify); |
2345 | ||
6b1e6cc7 JW |
2346 | /* Create a new message. */ |
2347 | struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type) | |
2348 | { | |
2349 | struct vhost_msg_node *node = kmalloc(sizeof *node, GFP_KERNEL); | |
2350 | if (!node) | |
2351 | return NULL; | |
670ae9ca MT |
2352 | |
2353 | /* Make sure all padding within the structure is initialized. */ | |
2354 | memset(&node->msg, 0, sizeof node->msg); | |
6b1e6cc7 JW |
2355 | node->vq = vq; |
2356 | node->msg.type = type; | |
2357 | return node; | |
2358 | } | |
2359 | EXPORT_SYMBOL_GPL(vhost_new_msg); | |
2360 | ||
2361 | void vhost_enqueue_msg(struct vhost_dev *dev, struct list_head *head, | |
2362 | struct vhost_msg_node *node) | |
2363 | { | |
2364 | spin_lock(&dev->iotlb_lock); | |
2365 | list_add_tail(&node->node, head); | |
2366 | spin_unlock(&dev->iotlb_lock); | |
2367 | ||
a9a08845 | 2368 | wake_up_interruptible_poll(&dev->wait, EPOLLIN | EPOLLRDNORM); |
6b1e6cc7 JW |
2369 | } |
2370 | EXPORT_SYMBOL_GPL(vhost_enqueue_msg); | |
2371 | ||
2372 | struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev, | |
2373 | struct list_head *head) | |
2374 | { | |
2375 | struct vhost_msg_node *node = NULL; | |
2376 | ||
2377 | spin_lock(&dev->iotlb_lock); | |
2378 | if (!list_empty(head)) { | |
2379 | node = list_first_entry(head, struct vhost_msg_node, | |
2380 | node); | |
2381 | list_del(&node->node); | |
2382 | } | |
2383 | spin_unlock(&dev->iotlb_lock); | |
2384 | ||
2385 | return node; | |
2386 | } | |
2387 | EXPORT_SYMBOL_GPL(vhost_dequeue_msg); | |
2388 | ||
2389 | ||
6ac1afbf AH |
2390 | static int __init vhost_init(void) |
2391 | { | |
2392 | return 0; | |
2393 | } | |
2394 | ||
2395 | static void __exit vhost_exit(void) | |
2396 | { | |
2397 | } | |
2398 | ||
2399 | module_init(vhost_init); | |
2400 | module_exit(vhost_exit); | |
2401 | ||
2402 | MODULE_VERSION("0.0.1"); | |
2403 | MODULE_LICENSE("GPL v2"); | |
2404 | MODULE_AUTHOR("Michael S. Tsirkin"); | |
2405 | MODULE_DESCRIPTION("Host kernel accelerator for virtio"); |