]>
Commit | Line | Data |
---|---|---|
721eecbf GH |
1 | /* |
2 | * kvm eventfd support - use eventfd objects to signal various KVM events | |
3 | * | |
4 | * Copyright 2009 Novell. All Rights Reserved. | |
221d059d | 5 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
721eecbf GH |
6 | * |
7 | * Author: | |
8 | * Gregory Haskins <ghaskins@novell.com> | |
9 | * | |
10 | * This file is free software; you can redistribute it and/or modify | |
11 | * it under the terms of version 2 of the GNU General Public License | |
12 | * as published by the Free Software Foundation. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software Foundation, | |
21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. | |
22 | */ | |
23 | ||
24 | #include <linux/kvm_host.h> | |
d34e6b17 | 25 | #include <linux/kvm.h> |
721eecbf GH |
26 | #include <linux/workqueue.h> |
27 | #include <linux/syscalls.h> | |
28 | #include <linux/wait.h> | |
29 | #include <linux/poll.h> | |
30 | #include <linux/file.h> | |
31 | #include <linux/list.h> | |
32 | #include <linux/eventfd.h> | |
d34e6b17 | 33 | #include <linux/kernel.h> |
719d93cd | 34 | #include <linux/srcu.h> |
5a0e3ad6 | 35 | #include <linux/slab.h> |
56f89f36 | 36 | #include <linux/seqlock.h> |
d34e6b17 GH |
37 | |
38 | #include "iodev.h" | |
721eecbf | 39 | |
a725d56a | 40 | #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING |
721eecbf GH |
41 | /* |
42 | * -------------------------------------------------------------------- | |
43 | * irqfd: Allows an fd to be used to inject an interrupt to the guest | |
44 | * | |
45 | * Credit goes to Avi Kivity for the original idea. | |
46 | * -------------------------------------------------------------------- | |
47 | */ | |
48 | ||
7a84428a AW |
49 | /* |
50 | * Resampling irqfds are a special variety of irqfds used to emulate | |
51 | * level triggered interrupts. The interrupt is asserted on eventfd | |
52 | * trigger. On acknowledgement through the irq ack notifier, the | |
53 | * interrupt is de-asserted and userspace is notified through the | |
54 | * resamplefd. All resamplers on the same gsi are de-asserted | |
55 | * together, so we don't need to track the state of each individual | |
56 | * user. We can also therefore share the same irq source ID. | |
57 | */ | |
58 | struct _irqfd_resampler { | |
59 | struct kvm *kvm; | |
60 | /* | |
61 | * List of resampling struct _irqfd objects sharing this gsi. | |
62 | * RCU list modified under kvm->irqfds.resampler_lock | |
63 | */ | |
64 | struct list_head list; | |
65 | struct kvm_irq_ack_notifier notifier; | |
66 | /* | |
67 | * Entry in list of kvm->irqfd.resampler_list. Use for sharing | |
68 | * resamplers among irqfds on the same gsi. | |
69 | * Accessed and modified under kvm->irqfds.resampler_lock | |
70 | */ | |
71 | struct list_head link; | |
72 | }; | |
73 | ||
721eecbf | 74 | struct _irqfd { |
bd2b53b2 MT |
75 | /* Used for MSI fast-path */ |
76 | struct kvm *kvm; | |
77 | wait_queue_t wait; | |
78 | /* Update side is protected by irqfds.lock */ | |
56f89f36 PM |
79 | struct kvm_kernel_irq_routing_entry irq_entry; |
80 | seqcount_t irq_entry_sc; | |
bd2b53b2 MT |
81 | /* Used for level IRQ fast-path */ |
82 | int gsi; | |
83 | struct work_struct inject; | |
7a84428a AW |
84 | /* The resampler used by this irqfd (resampler-only) */ |
85 | struct _irqfd_resampler *resampler; | |
86 | /* Eventfd notified on resample (resampler-only) */ | |
87 | struct eventfd_ctx *resamplefd; | |
88 | /* Entry in list of irqfds for a resampler (resampler-only) */ | |
89 | struct list_head resampler_link; | |
bd2b53b2 MT |
90 | /* Used for setup/shutdown */ |
91 | struct eventfd_ctx *eventfd; | |
92 | struct list_head list; | |
93 | poll_table pt; | |
94 | struct work_struct shutdown; | |
721eecbf GH |
95 | }; |
96 | ||
97 | static struct workqueue_struct *irqfd_cleanup_wq; | |
98 | ||
99 | static void | |
100 | irqfd_inject(struct work_struct *work) | |
101 | { | |
102 | struct _irqfd *irqfd = container_of(work, struct _irqfd, inject); | |
103 | struct kvm *kvm = irqfd->kvm; | |
104 | ||
7a84428a | 105 | if (!irqfd->resampler) { |
aa2fbe6d YZ |
106 | kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 1, |
107 | false); | |
108 | kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 0, | |
109 | false); | |
7a84428a AW |
110 | } else |
111 | kvm_set_irq(kvm, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID, | |
aa2fbe6d | 112 | irqfd->gsi, 1, false); |
7a84428a AW |
113 | } |
114 | ||
115 | /* | |
116 | * Since resampler irqfds share an IRQ source ID, we de-assert once | |
117 | * then notify all of the resampler irqfds using this GSI. We can't | |
118 | * do multiple de-asserts or we risk racing with incoming re-asserts. | |
119 | */ | |
120 | static void | |
121 | irqfd_resampler_ack(struct kvm_irq_ack_notifier *kian) | |
122 | { | |
123 | struct _irqfd_resampler *resampler; | |
719d93cd | 124 | struct kvm *kvm; |
7a84428a | 125 | struct _irqfd *irqfd; |
719d93cd | 126 | int idx; |
7a84428a AW |
127 | |
128 | resampler = container_of(kian, struct _irqfd_resampler, notifier); | |
719d93cd | 129 | kvm = resampler->kvm; |
7a84428a | 130 | |
719d93cd | 131 | kvm_set_irq(kvm, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID, |
aa2fbe6d | 132 | resampler->notifier.gsi, 0, false); |
7a84428a | 133 | |
719d93cd | 134 | idx = srcu_read_lock(&kvm->irq_srcu); |
7a84428a AW |
135 | |
136 | list_for_each_entry_rcu(irqfd, &resampler->list, resampler_link) | |
137 | eventfd_signal(irqfd->resamplefd, 1); | |
138 | ||
719d93cd | 139 | srcu_read_unlock(&kvm->irq_srcu, idx); |
7a84428a AW |
140 | } |
141 | ||
142 | static void | |
143 | irqfd_resampler_shutdown(struct _irqfd *irqfd) | |
144 | { | |
145 | struct _irqfd_resampler *resampler = irqfd->resampler; | |
146 | struct kvm *kvm = resampler->kvm; | |
147 | ||
148 | mutex_lock(&kvm->irqfds.resampler_lock); | |
149 | ||
150 | list_del_rcu(&irqfd->resampler_link); | |
719d93cd | 151 | synchronize_srcu(&kvm->irq_srcu); |
7a84428a AW |
152 | |
153 | if (list_empty(&resampler->list)) { | |
154 | list_del(&resampler->link); | |
155 | kvm_unregister_irq_ack_notifier(kvm, &resampler->notifier); | |
156 | kvm_set_irq(kvm, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID, | |
aa2fbe6d | 157 | resampler->notifier.gsi, 0, false); |
7a84428a AW |
158 | kfree(resampler); |
159 | } | |
160 | ||
161 | mutex_unlock(&kvm->irqfds.resampler_lock); | |
721eecbf GH |
162 | } |
163 | ||
164 | /* | |
165 | * Race-free decouple logic (ordering is critical) | |
166 | */ | |
167 | static void | |
168 | irqfd_shutdown(struct work_struct *work) | |
169 | { | |
170 | struct _irqfd *irqfd = container_of(work, struct _irqfd, shutdown); | |
b6a114d2 | 171 | u64 cnt; |
721eecbf GH |
172 | |
173 | /* | |
174 | * Synchronize with the wait-queue and unhook ourselves to prevent | |
175 | * further events. | |
176 | */ | |
b6a114d2 | 177 | eventfd_ctx_remove_wait_queue(irqfd->eventfd, &irqfd->wait, &cnt); |
721eecbf GH |
178 | |
179 | /* | |
180 | * We know no new events will be scheduled at this point, so block | |
181 | * until all previously outstanding events have completed | |
182 | */ | |
43829731 | 183 | flush_work(&irqfd->inject); |
721eecbf | 184 | |
7a84428a AW |
185 | if (irqfd->resampler) { |
186 | irqfd_resampler_shutdown(irqfd); | |
187 | eventfd_ctx_put(irqfd->resamplefd); | |
188 | } | |
189 | ||
721eecbf GH |
190 | /* |
191 | * It is now safe to release the object's resources | |
192 | */ | |
193 | eventfd_ctx_put(irqfd->eventfd); | |
194 | kfree(irqfd); | |
195 | } | |
196 | ||
197 | ||
198 | /* assumes kvm->irqfds.lock is held */ | |
199 | static bool | |
200 | irqfd_is_active(struct _irqfd *irqfd) | |
201 | { | |
202 | return list_empty(&irqfd->list) ? false : true; | |
203 | } | |
204 | ||
205 | /* | |
206 | * Mark the irqfd as inactive and schedule it for removal | |
207 | * | |
208 | * assumes kvm->irqfds.lock is held | |
209 | */ | |
210 | static void | |
211 | irqfd_deactivate(struct _irqfd *irqfd) | |
212 | { | |
213 | BUG_ON(!irqfd_is_active(irqfd)); | |
214 | ||
215 | list_del_init(&irqfd->list); | |
216 | ||
217 | queue_work(irqfd_cleanup_wq, &irqfd->shutdown); | |
218 | } | |
219 | ||
220 | /* | |
221 | * Called with wqh->lock held and interrupts disabled | |
222 | */ | |
223 | static int | |
224 | irqfd_wakeup(wait_queue_t *wait, unsigned mode, int sync, void *key) | |
225 | { | |
226 | struct _irqfd *irqfd = container_of(wait, struct _irqfd, wait); | |
227 | unsigned long flags = (unsigned long)key; | |
56f89f36 | 228 | struct kvm_kernel_irq_routing_entry irq; |
bd2b53b2 | 229 | struct kvm *kvm = irqfd->kvm; |
56f89f36 | 230 | unsigned seq; |
719d93cd | 231 | int idx; |
721eecbf | 232 | |
bd2b53b2 | 233 | if (flags & POLLIN) { |
719d93cd | 234 | idx = srcu_read_lock(&kvm->irq_srcu); |
56f89f36 PM |
235 | do { |
236 | seq = read_seqcount_begin(&irqfd->irq_entry_sc); | |
237 | irq = irqfd->irq_entry; | |
238 | } while (read_seqcount_retry(&irqfd->irq_entry_sc, seq)); | |
721eecbf | 239 | /* An event has been signaled, inject an interrupt */ |
56f89f36 PM |
240 | if (irq.type == KVM_IRQ_ROUTING_MSI) |
241 | kvm_set_msi(&irq, kvm, KVM_USERSPACE_IRQ_SOURCE_ID, 1, | |
aa2fbe6d | 242 | false); |
bd2b53b2 MT |
243 | else |
244 | schedule_work(&irqfd->inject); | |
719d93cd | 245 | srcu_read_unlock(&kvm->irq_srcu, idx); |
bd2b53b2 | 246 | } |
721eecbf GH |
247 | |
248 | if (flags & POLLHUP) { | |
249 | /* The eventfd is closing, detach from KVM */ | |
721eecbf GH |
250 | unsigned long flags; |
251 | ||
252 | spin_lock_irqsave(&kvm->irqfds.lock, flags); | |
253 | ||
254 | /* | |
255 | * We must check if someone deactivated the irqfd before | |
256 | * we could acquire the irqfds.lock since the item is | |
257 | * deactivated from the KVM side before it is unhooked from | |
258 | * the wait-queue. If it is already deactivated, we can | |
259 | * simply return knowing the other side will cleanup for us. | |
260 | * We cannot race against the irqfd going away since the | |
261 | * other side is required to acquire wqh->lock, which we hold | |
262 | */ | |
263 | if (irqfd_is_active(irqfd)) | |
264 | irqfd_deactivate(irqfd); | |
265 | ||
266 | spin_unlock_irqrestore(&kvm->irqfds.lock, flags); | |
267 | } | |
268 | ||
269 | return 0; | |
270 | } | |
271 | ||
272 | static void | |
273 | irqfd_ptable_queue_proc(struct file *file, wait_queue_head_t *wqh, | |
274 | poll_table *pt) | |
275 | { | |
276 | struct _irqfd *irqfd = container_of(pt, struct _irqfd, pt); | |
721eecbf GH |
277 | add_wait_queue(wqh, &irqfd->wait); |
278 | } | |
279 | ||
bd2b53b2 MT |
280 | /* Must be called under irqfds.lock */ |
281 | static void irqfd_update(struct kvm *kvm, struct _irqfd *irqfd, | |
282 | struct kvm_irq_routing_table *irq_rt) | |
283 | { | |
284 | struct kvm_kernel_irq_routing_entry *e; | |
bd2b53b2 | 285 | |
56f89f36 PM |
286 | write_seqcount_begin(&irqfd->irq_entry_sc); |
287 | ||
288 | irqfd->irq_entry.type = 0; | |
289 | if (irqfd->gsi >= irq_rt->nr_rt_entries) | |
290 | goto out; | |
bd2b53b2 | 291 | |
b67bfe0d | 292 | hlist_for_each_entry(e, &irq_rt->map[irqfd->gsi], link) { |
bd2b53b2 MT |
293 | /* Only fast-path MSI. */ |
294 | if (e->type == KVM_IRQ_ROUTING_MSI) | |
56f89f36 | 295 | irqfd->irq_entry = *e; |
bd2b53b2 | 296 | } |
56f89f36 PM |
297 | |
298 | out: | |
299 | write_seqcount_end(&irqfd->irq_entry_sc); | |
bd2b53b2 MT |
300 | } |
301 | ||
721eecbf | 302 | static int |
d4db2935 | 303 | kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *args) |
721eecbf | 304 | { |
bd2b53b2 | 305 | struct kvm_irq_routing_table *irq_rt; |
f1d1c309 | 306 | struct _irqfd *irqfd, *tmp; |
cffe78d9 | 307 | struct fd f; |
7a84428a | 308 | struct eventfd_ctx *eventfd = NULL, *resamplefd = NULL; |
721eecbf GH |
309 | int ret; |
310 | unsigned int events; | |
311 | ||
312 | irqfd = kzalloc(sizeof(*irqfd), GFP_KERNEL); | |
313 | if (!irqfd) | |
314 | return -ENOMEM; | |
315 | ||
316 | irqfd->kvm = kvm; | |
d4db2935 | 317 | irqfd->gsi = args->gsi; |
721eecbf GH |
318 | INIT_LIST_HEAD(&irqfd->list); |
319 | INIT_WORK(&irqfd->inject, irqfd_inject); | |
320 | INIT_WORK(&irqfd->shutdown, irqfd_shutdown); | |
56f89f36 | 321 | seqcount_init(&irqfd->irq_entry_sc); |
721eecbf | 322 | |
cffe78d9 AV |
323 | f = fdget(args->fd); |
324 | if (!f.file) { | |
325 | ret = -EBADF; | |
326 | goto out; | |
721eecbf GH |
327 | } |
328 | ||
cffe78d9 | 329 | eventfd = eventfd_ctx_fileget(f.file); |
721eecbf GH |
330 | if (IS_ERR(eventfd)) { |
331 | ret = PTR_ERR(eventfd); | |
332 | goto fail; | |
333 | } | |
334 | ||
335 | irqfd->eventfd = eventfd; | |
336 | ||
7a84428a AW |
337 | if (args->flags & KVM_IRQFD_FLAG_RESAMPLE) { |
338 | struct _irqfd_resampler *resampler; | |
339 | ||
340 | resamplefd = eventfd_ctx_fdget(args->resamplefd); | |
341 | if (IS_ERR(resamplefd)) { | |
342 | ret = PTR_ERR(resamplefd); | |
343 | goto fail; | |
344 | } | |
345 | ||
346 | irqfd->resamplefd = resamplefd; | |
347 | INIT_LIST_HEAD(&irqfd->resampler_link); | |
348 | ||
349 | mutex_lock(&kvm->irqfds.resampler_lock); | |
350 | ||
351 | list_for_each_entry(resampler, | |
49f8a1a5 | 352 | &kvm->irqfds.resampler_list, link) { |
7a84428a AW |
353 | if (resampler->notifier.gsi == irqfd->gsi) { |
354 | irqfd->resampler = resampler; | |
355 | break; | |
356 | } | |
357 | } | |
358 | ||
359 | if (!irqfd->resampler) { | |
360 | resampler = kzalloc(sizeof(*resampler), GFP_KERNEL); | |
361 | if (!resampler) { | |
362 | ret = -ENOMEM; | |
363 | mutex_unlock(&kvm->irqfds.resampler_lock); | |
364 | goto fail; | |
365 | } | |
366 | ||
367 | resampler->kvm = kvm; | |
368 | INIT_LIST_HEAD(&resampler->list); | |
369 | resampler->notifier.gsi = irqfd->gsi; | |
370 | resampler->notifier.irq_acked = irqfd_resampler_ack; | |
371 | INIT_LIST_HEAD(&resampler->link); | |
372 | ||
373 | list_add(&resampler->link, &kvm->irqfds.resampler_list); | |
374 | kvm_register_irq_ack_notifier(kvm, | |
375 | &resampler->notifier); | |
376 | irqfd->resampler = resampler; | |
377 | } | |
378 | ||
379 | list_add_rcu(&irqfd->resampler_link, &irqfd->resampler->list); | |
719d93cd | 380 | synchronize_srcu(&kvm->irq_srcu); |
7a84428a AW |
381 | |
382 | mutex_unlock(&kvm->irqfds.resampler_lock); | |
383 | } | |
384 | ||
721eecbf GH |
385 | /* |
386 | * Install our own custom wake-up handling so we are notified via | |
387 | * a callback whenever someone signals the underlying eventfd | |
388 | */ | |
389 | init_waitqueue_func_entry(&irqfd->wait, irqfd_wakeup); | |
390 | init_poll_funcptr(&irqfd->pt, irqfd_ptable_queue_proc); | |
391 | ||
f1d1c309 MT |
392 | spin_lock_irq(&kvm->irqfds.lock); |
393 | ||
394 | ret = 0; | |
395 | list_for_each_entry(tmp, &kvm->irqfds.items, list) { | |
396 | if (irqfd->eventfd != tmp->eventfd) | |
397 | continue; | |
398 | /* This fd is used for another irq already. */ | |
399 | ret = -EBUSY; | |
400 | spin_unlock_irq(&kvm->irqfds.lock); | |
401 | goto fail; | |
402 | } | |
403 | ||
bd2b53b2 MT |
404 | irq_rt = rcu_dereference_protected(kvm->irq_routing, |
405 | lockdep_is_held(&kvm->irqfds.lock)); | |
406 | irqfd_update(kvm, irqfd, irq_rt); | |
407 | ||
721eecbf | 408 | list_add_tail(&irqfd->list, &kvm->irqfds.items); |
721eecbf | 409 | |
684a0b71 CH |
410 | spin_unlock_irq(&kvm->irqfds.lock); |
411 | ||
721eecbf GH |
412 | /* |
413 | * Check if there was an event already pending on the eventfd | |
414 | * before we registered, and trigger it as if we didn't miss it. | |
415 | */ | |
684a0b71 CH |
416 | events = f.file->f_op->poll(f.file, &irqfd->pt); |
417 | ||
721eecbf GH |
418 | if (events & POLLIN) |
419 | schedule_work(&irqfd->inject); | |
420 | ||
421 | /* | |
422 | * do not drop the file until the irqfd is fully initialized, otherwise | |
423 | * we might race against the POLLHUP | |
424 | */ | |
cffe78d9 | 425 | fdput(f); |
721eecbf GH |
426 | |
427 | return 0; | |
428 | ||
429 | fail: | |
7a84428a AW |
430 | if (irqfd->resampler) |
431 | irqfd_resampler_shutdown(irqfd); | |
432 | ||
433 | if (resamplefd && !IS_ERR(resamplefd)) | |
434 | eventfd_ctx_put(resamplefd); | |
435 | ||
721eecbf GH |
436 | if (eventfd && !IS_ERR(eventfd)) |
437 | eventfd_ctx_put(eventfd); | |
438 | ||
cffe78d9 | 439 | fdput(f); |
721eecbf | 440 | |
cffe78d9 | 441 | out: |
721eecbf GH |
442 | kfree(irqfd); |
443 | return ret; | |
444 | } | |
914daba8 | 445 | #endif |
721eecbf GH |
446 | |
447 | void | |
d34e6b17 | 448 | kvm_eventfd_init(struct kvm *kvm) |
721eecbf | 449 | { |
a725d56a | 450 | #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING |
721eecbf GH |
451 | spin_lock_init(&kvm->irqfds.lock); |
452 | INIT_LIST_HEAD(&kvm->irqfds.items); | |
7a84428a AW |
453 | INIT_LIST_HEAD(&kvm->irqfds.resampler_list); |
454 | mutex_init(&kvm->irqfds.resampler_lock); | |
914daba8 | 455 | #endif |
d34e6b17 | 456 | INIT_LIST_HEAD(&kvm->ioeventfds); |
721eecbf GH |
457 | } |
458 | ||
a725d56a | 459 | #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING |
721eecbf GH |
460 | /* |
461 | * shutdown any irqfd's that match fd+gsi | |
462 | */ | |
463 | static int | |
d4db2935 | 464 | kvm_irqfd_deassign(struct kvm *kvm, struct kvm_irqfd *args) |
721eecbf GH |
465 | { |
466 | struct _irqfd *irqfd, *tmp; | |
467 | struct eventfd_ctx *eventfd; | |
468 | ||
d4db2935 | 469 | eventfd = eventfd_ctx_fdget(args->fd); |
721eecbf GH |
470 | if (IS_ERR(eventfd)) |
471 | return PTR_ERR(eventfd); | |
472 | ||
473 | spin_lock_irq(&kvm->irqfds.lock); | |
474 | ||
475 | list_for_each_entry_safe(irqfd, tmp, &kvm->irqfds.items, list) { | |
d4db2935 | 476 | if (irqfd->eventfd == eventfd && irqfd->gsi == args->gsi) { |
bd2b53b2 | 477 | /* |
56f89f36 | 478 | * This clearing of irq_entry.type is needed for when |
c8ce057e MT |
479 | * another thread calls kvm_irq_routing_update before |
480 | * we flush workqueue below (we synchronize with | |
481 | * kvm_irq_routing_update using irqfds.lock). | |
bd2b53b2 | 482 | */ |
56f89f36 PM |
483 | write_seqcount_begin(&irqfd->irq_entry_sc); |
484 | irqfd->irq_entry.type = 0; | |
485 | write_seqcount_end(&irqfd->irq_entry_sc); | |
721eecbf | 486 | irqfd_deactivate(irqfd); |
bd2b53b2 | 487 | } |
721eecbf GH |
488 | } |
489 | ||
490 | spin_unlock_irq(&kvm->irqfds.lock); | |
491 | eventfd_ctx_put(eventfd); | |
492 | ||
493 | /* | |
494 | * Block until we know all outstanding shutdown jobs have completed | |
495 | * so that we guarantee there will not be any more interrupts on this | |
496 | * gsi once this deassign function returns. | |
497 | */ | |
498 | flush_workqueue(irqfd_cleanup_wq); | |
499 | ||
500 | return 0; | |
501 | } | |
502 | ||
503 | int | |
d4db2935 | 504 | kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args) |
721eecbf | 505 | { |
7a84428a | 506 | if (args->flags & ~(KVM_IRQFD_FLAG_DEASSIGN | KVM_IRQFD_FLAG_RESAMPLE)) |
326cf033 AW |
507 | return -EINVAL; |
508 | ||
d4db2935 AW |
509 | if (args->flags & KVM_IRQFD_FLAG_DEASSIGN) |
510 | return kvm_irqfd_deassign(kvm, args); | |
721eecbf | 511 | |
d4db2935 | 512 | return kvm_irqfd_assign(kvm, args); |
721eecbf GH |
513 | } |
514 | ||
515 | /* | |
516 | * This function is called as the kvm VM fd is being released. Shutdown all | |
517 | * irqfds that still remain open | |
518 | */ | |
519 | void | |
520 | kvm_irqfd_release(struct kvm *kvm) | |
521 | { | |
522 | struct _irqfd *irqfd, *tmp; | |
523 | ||
524 | spin_lock_irq(&kvm->irqfds.lock); | |
525 | ||
526 | list_for_each_entry_safe(irqfd, tmp, &kvm->irqfds.items, list) | |
527 | irqfd_deactivate(irqfd); | |
528 | ||
529 | spin_unlock_irq(&kvm->irqfds.lock); | |
530 | ||
531 | /* | |
532 | * Block until we know all outstanding shutdown jobs have completed | |
533 | * since we do not take a kvm* reference. | |
534 | */ | |
535 | flush_workqueue(irqfd_cleanup_wq); | |
536 | ||
537 | } | |
538 | ||
bd2b53b2 MT |
539 | /* |
540 | * Change irq_routing and irqfd. | |
719d93cd | 541 | * Caller must invoke synchronize_srcu(&kvm->irq_srcu) afterwards. |
bd2b53b2 MT |
542 | */ |
543 | void kvm_irq_routing_update(struct kvm *kvm, | |
544 | struct kvm_irq_routing_table *irq_rt) | |
545 | { | |
546 | struct _irqfd *irqfd; | |
547 | ||
548 | spin_lock_irq(&kvm->irqfds.lock); | |
549 | ||
550 | rcu_assign_pointer(kvm->irq_routing, irq_rt); | |
551 | ||
552 | list_for_each_entry(irqfd, &kvm->irqfds.items, list) | |
553 | irqfd_update(kvm, irqfd, irq_rt); | |
554 | ||
555 | spin_unlock_irq(&kvm->irqfds.lock); | |
556 | } | |
557 | ||
721eecbf GH |
558 | /* |
559 | * create a host-wide workqueue for issuing deferred shutdown requests | |
560 | * aggregated from all vm* instances. We need our own isolated single-thread | |
561 | * queue to prevent deadlock against flushing the normal work-queue. | |
562 | */ | |
a0f155e9 | 563 | int kvm_irqfd_init(void) |
721eecbf GH |
564 | { |
565 | irqfd_cleanup_wq = create_singlethread_workqueue("kvm-irqfd-cleanup"); | |
566 | if (!irqfd_cleanup_wq) | |
567 | return -ENOMEM; | |
568 | ||
569 | return 0; | |
570 | } | |
571 | ||
a0f155e9 | 572 | void kvm_irqfd_exit(void) |
721eecbf GH |
573 | { |
574 | destroy_workqueue(irqfd_cleanup_wq); | |
575 | } | |
914daba8 | 576 | #endif |
d34e6b17 GH |
577 | |
578 | /* | |
579 | * -------------------------------------------------------------------- | |
580 | * ioeventfd: translate a PIO/MMIO memory write to an eventfd signal. | |
581 | * | |
582 | * userspace can register a PIO/MMIO address with an eventfd for receiving | |
583 | * notification when the memory has been touched. | |
584 | * -------------------------------------------------------------------- | |
585 | */ | |
586 | ||
587 | struct _ioeventfd { | |
588 | struct list_head list; | |
589 | u64 addr; | |
590 | int length; | |
591 | struct eventfd_ctx *eventfd; | |
592 | u64 datamatch; | |
593 | struct kvm_io_device dev; | |
05e07f9b | 594 | u8 bus_idx; |
d34e6b17 GH |
595 | bool wildcard; |
596 | }; | |
597 | ||
598 | static inline struct _ioeventfd * | |
599 | to_ioeventfd(struct kvm_io_device *dev) | |
600 | { | |
601 | return container_of(dev, struct _ioeventfd, dev); | |
602 | } | |
603 | ||
604 | static void | |
605 | ioeventfd_release(struct _ioeventfd *p) | |
606 | { | |
607 | eventfd_ctx_put(p->eventfd); | |
608 | list_del(&p->list); | |
609 | kfree(p); | |
610 | } | |
611 | ||
612 | static bool | |
613 | ioeventfd_in_range(struct _ioeventfd *p, gpa_t addr, int len, const void *val) | |
614 | { | |
615 | u64 _val; | |
616 | ||
f848a5a8 MT |
617 | if (addr != p->addr) |
618 | /* address must be precise for a hit */ | |
619 | return false; | |
620 | ||
621 | if (!p->length) | |
622 | /* length = 0 means only look at the address, so always a hit */ | |
623 | return true; | |
624 | ||
625 | if (len != p->length) | |
d34e6b17 GH |
626 | /* address-range must be precise for a hit */ |
627 | return false; | |
628 | ||
629 | if (p->wildcard) | |
630 | /* all else equal, wildcard is always a hit */ | |
631 | return true; | |
632 | ||
633 | /* otherwise, we have to actually compare the data */ | |
634 | ||
635 | BUG_ON(!IS_ALIGNED((unsigned long)val, len)); | |
636 | ||
637 | switch (len) { | |
638 | case 1: | |
639 | _val = *(u8 *)val; | |
640 | break; | |
641 | case 2: | |
642 | _val = *(u16 *)val; | |
643 | break; | |
644 | case 4: | |
645 | _val = *(u32 *)val; | |
646 | break; | |
647 | case 8: | |
648 | _val = *(u64 *)val; | |
649 | break; | |
650 | default: | |
651 | return false; | |
652 | } | |
653 | ||
654 | return _val == p->datamatch ? true : false; | |
655 | } | |
656 | ||
657 | /* MMIO/PIO writes trigger an event if the addr/val match */ | |
658 | static int | |
659 | ioeventfd_write(struct kvm_io_device *this, gpa_t addr, int len, | |
660 | const void *val) | |
661 | { | |
662 | struct _ioeventfd *p = to_ioeventfd(this); | |
663 | ||
664 | if (!ioeventfd_in_range(p, addr, len, val)) | |
665 | return -EOPNOTSUPP; | |
666 | ||
667 | eventfd_signal(p->eventfd, 1); | |
668 | return 0; | |
669 | } | |
670 | ||
671 | /* | |
672 | * This function is called as KVM is completely shutting down. We do not | |
673 | * need to worry about locking just nuke anything we have as quickly as possible | |
674 | */ | |
675 | static void | |
676 | ioeventfd_destructor(struct kvm_io_device *this) | |
677 | { | |
678 | struct _ioeventfd *p = to_ioeventfd(this); | |
679 | ||
680 | ioeventfd_release(p); | |
681 | } | |
682 | ||
683 | static const struct kvm_io_device_ops ioeventfd_ops = { | |
684 | .write = ioeventfd_write, | |
685 | .destructor = ioeventfd_destructor, | |
686 | }; | |
687 | ||
688 | /* assumes kvm->slots_lock held */ | |
689 | static bool | |
690 | ioeventfd_check_collision(struct kvm *kvm, struct _ioeventfd *p) | |
691 | { | |
692 | struct _ioeventfd *_p; | |
693 | ||
694 | list_for_each_entry(_p, &kvm->ioeventfds, list) | |
05e07f9b | 695 | if (_p->bus_idx == p->bus_idx && |
f848a5a8 MT |
696 | _p->addr == p->addr && |
697 | (!_p->length || !p->length || | |
698 | (_p->length == p->length && | |
699 | (_p->wildcard || p->wildcard || | |
700 | _p->datamatch == p->datamatch)))) | |
d34e6b17 GH |
701 | return true; |
702 | ||
703 | return false; | |
704 | } | |
705 | ||
2b83451b CH |
706 | static enum kvm_bus ioeventfd_bus_from_flags(__u32 flags) |
707 | { | |
708 | if (flags & KVM_IOEVENTFD_FLAG_PIO) | |
709 | return KVM_PIO_BUS; | |
710 | if (flags & KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY) | |
711 | return KVM_VIRTIO_CCW_NOTIFY_BUS; | |
712 | return KVM_MMIO_BUS; | |
713 | } | |
714 | ||
d34e6b17 GH |
715 | static int |
716 | kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) | |
717 | { | |
2b83451b | 718 | enum kvm_bus bus_idx; |
d34e6b17 GH |
719 | struct _ioeventfd *p; |
720 | struct eventfd_ctx *eventfd; | |
721 | int ret; | |
722 | ||
2b83451b | 723 | bus_idx = ioeventfd_bus_from_flags(args->flags); |
f848a5a8 | 724 | /* must be natural-word sized, or 0 to ignore length */ |
d34e6b17 | 725 | switch (args->len) { |
f848a5a8 | 726 | case 0: |
d34e6b17 GH |
727 | case 1: |
728 | case 2: | |
729 | case 4: | |
730 | case 8: | |
731 | break; | |
732 | default: | |
733 | return -EINVAL; | |
734 | } | |
735 | ||
736 | /* check for range overflow */ | |
737 | if (args->addr + args->len < args->addr) | |
738 | return -EINVAL; | |
739 | ||
740 | /* check for extra flags that we don't understand */ | |
741 | if (args->flags & ~KVM_IOEVENTFD_VALID_FLAG_MASK) | |
742 | return -EINVAL; | |
743 | ||
f848a5a8 MT |
744 | /* ioeventfd with no length can't be combined with DATAMATCH */ |
745 | if (!args->len && | |
746 | args->flags & (KVM_IOEVENTFD_FLAG_PIO | | |
747 | KVM_IOEVENTFD_FLAG_DATAMATCH)) | |
748 | return -EINVAL; | |
749 | ||
d34e6b17 GH |
750 | eventfd = eventfd_ctx_fdget(args->fd); |
751 | if (IS_ERR(eventfd)) | |
752 | return PTR_ERR(eventfd); | |
753 | ||
754 | p = kzalloc(sizeof(*p), GFP_KERNEL); | |
755 | if (!p) { | |
756 | ret = -ENOMEM; | |
757 | goto fail; | |
758 | } | |
759 | ||
760 | INIT_LIST_HEAD(&p->list); | |
761 | p->addr = args->addr; | |
05e07f9b | 762 | p->bus_idx = bus_idx; |
d34e6b17 GH |
763 | p->length = args->len; |
764 | p->eventfd = eventfd; | |
765 | ||
766 | /* The datamatch feature is optional, otherwise this is a wildcard */ | |
767 | if (args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH) | |
768 | p->datamatch = args->datamatch; | |
769 | else | |
770 | p->wildcard = true; | |
771 | ||
79fac95e | 772 | mutex_lock(&kvm->slots_lock); |
d34e6b17 | 773 | |
25985edc | 774 | /* Verify that there isn't a match already */ |
d34e6b17 GH |
775 | if (ioeventfd_check_collision(kvm, p)) { |
776 | ret = -EEXIST; | |
777 | goto unlock_fail; | |
778 | } | |
779 | ||
780 | kvm_iodevice_init(&p->dev, &ioeventfd_ops); | |
781 | ||
743eeb0b SL |
782 | ret = kvm_io_bus_register_dev(kvm, bus_idx, p->addr, p->length, |
783 | &p->dev); | |
d34e6b17 GH |
784 | if (ret < 0) |
785 | goto unlock_fail; | |
786 | ||
68c3b4d1 MT |
787 | /* When length is ignored, MMIO is also put on a separate bus, for |
788 | * faster lookups. | |
789 | */ | |
790 | if (!args->len && !(args->flags & KVM_IOEVENTFD_FLAG_PIO)) { | |
791 | ret = kvm_io_bus_register_dev(kvm, KVM_FAST_MMIO_BUS, | |
792 | p->addr, 0, &p->dev); | |
793 | if (ret < 0) | |
794 | goto register_fail; | |
795 | } | |
796 | ||
6ea34c9b | 797 | kvm->buses[bus_idx]->ioeventfd_count++; |
d34e6b17 GH |
798 | list_add_tail(&p->list, &kvm->ioeventfds); |
799 | ||
79fac95e | 800 | mutex_unlock(&kvm->slots_lock); |
d34e6b17 GH |
801 | |
802 | return 0; | |
803 | ||
68c3b4d1 MT |
804 | register_fail: |
805 | kvm_io_bus_unregister_dev(kvm, bus_idx, &p->dev); | |
d34e6b17 | 806 | unlock_fail: |
79fac95e | 807 | mutex_unlock(&kvm->slots_lock); |
d34e6b17 GH |
808 | |
809 | fail: | |
810 | kfree(p); | |
811 | eventfd_ctx_put(eventfd); | |
812 | ||
813 | return ret; | |
814 | } | |
815 | ||
816 | static int | |
817 | kvm_deassign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) | |
818 | { | |
2b83451b | 819 | enum kvm_bus bus_idx; |
d34e6b17 GH |
820 | struct _ioeventfd *p, *tmp; |
821 | struct eventfd_ctx *eventfd; | |
822 | int ret = -ENOENT; | |
823 | ||
2b83451b | 824 | bus_idx = ioeventfd_bus_from_flags(args->flags); |
d34e6b17 GH |
825 | eventfd = eventfd_ctx_fdget(args->fd); |
826 | if (IS_ERR(eventfd)) | |
827 | return PTR_ERR(eventfd); | |
828 | ||
79fac95e | 829 | mutex_lock(&kvm->slots_lock); |
d34e6b17 GH |
830 | |
831 | list_for_each_entry_safe(p, tmp, &kvm->ioeventfds, list) { | |
832 | bool wildcard = !(args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH); | |
833 | ||
05e07f9b MT |
834 | if (p->bus_idx != bus_idx || |
835 | p->eventfd != eventfd || | |
d34e6b17 GH |
836 | p->addr != args->addr || |
837 | p->length != args->len || | |
838 | p->wildcard != wildcard) | |
839 | continue; | |
840 | ||
841 | if (!p->wildcard && p->datamatch != args->datamatch) | |
842 | continue; | |
843 | ||
e93f8a0f | 844 | kvm_io_bus_unregister_dev(kvm, bus_idx, &p->dev); |
68c3b4d1 MT |
845 | if (!p->length) { |
846 | kvm_io_bus_unregister_dev(kvm, KVM_FAST_MMIO_BUS, | |
847 | &p->dev); | |
848 | } | |
6ea34c9b | 849 | kvm->buses[bus_idx]->ioeventfd_count--; |
d34e6b17 GH |
850 | ioeventfd_release(p); |
851 | ret = 0; | |
852 | break; | |
853 | } | |
854 | ||
79fac95e | 855 | mutex_unlock(&kvm->slots_lock); |
d34e6b17 GH |
856 | |
857 | eventfd_ctx_put(eventfd); | |
858 | ||
859 | return ret; | |
860 | } | |
861 | ||
862 | int | |
863 | kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) | |
864 | { | |
865 | if (args->flags & KVM_IOEVENTFD_FLAG_DEASSIGN) | |
866 | return kvm_deassign_ioeventfd(kvm, args); | |
867 | ||
868 | return kvm_assign_ioeventfd(kvm, args); | |
869 | } |