2 * kvm eventfd support - use eventfd objects to signal various KVM events
4 * Copyright 2009 Novell. All Rights Reserved.
5 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
8 * Gregory Haskins <ghaskins@novell.com>
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.
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.
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.
24 #include <linux/kvm_host.h>
25 #include <linux/kvm.h>
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>
33 #include <linux/kernel.h>
34 #include <linux/slab.h>
38 #ifdef __KVM_HAVE_IOAPIC
40 * --------------------------------------------------------------------
41 * irqfd: Allows an fd to be used to inject an interrupt to the guest
43 * Credit goes to Avi Kivity for the original idea.
44 * --------------------------------------------------------------------
48 * Resampling irqfds are a special variety of irqfds used to emulate
49 * level triggered interrupts. The interrupt is asserted on eventfd
50 * trigger. On acknowledgement through the irq ack notifier, the
51 * interrupt is de-asserted and userspace is notified through the
52 * resamplefd. All resamplers on the same gsi are de-asserted
53 * together, so we don't need to track the state of each individual
54 * user. We can also therefore share the same irq source ID.
56 struct _irqfd_resampler
{
59 * List of resampling struct _irqfd objects sharing this gsi.
60 * RCU list modified under kvm->irqfds.resampler_lock
62 struct list_head list
;
63 struct kvm_irq_ack_notifier notifier
;
65 * Entry in list of kvm->irqfd.resampler_list. Use for sharing
66 * resamplers among irqfds on the same gsi.
67 * Accessed and modified under kvm->irqfds.resampler_lock
69 struct list_head link
;
73 /* Used for MSI fast-path */
76 /* Update side is protected by irqfds.lock */
77 struct kvm_kernel_irq_routing_entry __rcu
*irq_entry
;
78 /* Used for level IRQ fast-path */
80 struct work_struct inject
;
81 /* The resampler used by this irqfd (resampler-only) */
82 struct _irqfd_resampler
*resampler
;
83 /* Eventfd notified on resample (resampler-only) */
84 struct eventfd_ctx
*resamplefd
;
85 /* Entry in list of irqfds for a resampler (resampler-only) */
86 struct list_head resampler_link
;
87 /* Used for setup/shutdown */
88 struct eventfd_ctx
*eventfd
;
89 struct list_head list
;
91 struct work_struct shutdown
;
94 static struct workqueue_struct
*irqfd_cleanup_wq
;
97 irqfd_inject(struct work_struct
*work
)
99 struct _irqfd
*irqfd
= container_of(work
, struct _irqfd
, inject
);
100 struct kvm
*kvm
= irqfd
->kvm
;
102 if (!irqfd
->resampler
) {
103 kvm_set_irq(kvm
, KVM_USERSPACE_IRQ_SOURCE_ID
, irqfd
->gsi
, 1);
104 kvm_set_irq(kvm
, KVM_USERSPACE_IRQ_SOURCE_ID
, irqfd
->gsi
, 0);
106 kvm_set_irq(kvm
, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID
,
111 * Since resampler irqfds share an IRQ source ID, we de-assert once
112 * then notify all of the resampler irqfds using this GSI. We can't
113 * do multiple de-asserts or we risk racing with incoming re-asserts.
116 irqfd_resampler_ack(struct kvm_irq_ack_notifier
*kian
)
118 struct _irqfd_resampler
*resampler
;
119 struct _irqfd
*irqfd
;
121 resampler
= container_of(kian
, struct _irqfd_resampler
, notifier
);
123 kvm_set_irq(resampler
->kvm
, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID
,
124 resampler
->notifier
.gsi
, 0);
128 list_for_each_entry_rcu(irqfd
, &resampler
->list
, resampler_link
)
129 eventfd_signal(irqfd
->resamplefd
, 1);
135 irqfd_resampler_shutdown(struct _irqfd
*irqfd
)
137 struct _irqfd_resampler
*resampler
= irqfd
->resampler
;
138 struct kvm
*kvm
= resampler
->kvm
;
140 mutex_lock(&kvm
->irqfds
.resampler_lock
);
142 list_del_rcu(&irqfd
->resampler_link
);
145 if (list_empty(&resampler
->list
)) {
146 list_del(&resampler
->link
);
147 kvm_unregister_irq_ack_notifier(kvm
, &resampler
->notifier
);
148 kvm_set_irq(kvm
, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID
,
149 resampler
->notifier
.gsi
, 0);
153 mutex_unlock(&kvm
->irqfds
.resampler_lock
);
157 * Race-free decouple logic (ordering is critical)
160 irqfd_shutdown(struct work_struct
*work
)
162 struct _irqfd
*irqfd
= container_of(work
, struct _irqfd
, shutdown
);
166 * Synchronize with the wait-queue and unhook ourselves to prevent
169 eventfd_ctx_remove_wait_queue(irqfd
->eventfd
, &irqfd
->wait
, &cnt
);
172 * We know no new events will be scheduled at this point, so block
173 * until all previously outstanding events have completed
175 flush_work(&irqfd
->inject
);
177 if (irqfd
->resampler
) {
178 irqfd_resampler_shutdown(irqfd
);
179 eventfd_ctx_put(irqfd
->resamplefd
);
183 * It is now safe to release the object's resources
185 eventfd_ctx_put(irqfd
->eventfd
);
190 /* assumes kvm->irqfds.lock is held */
192 irqfd_is_active(struct _irqfd
*irqfd
)
194 return list_empty(&irqfd
->list
) ? false : true;
198 * Mark the irqfd as inactive and schedule it for removal
200 * assumes kvm->irqfds.lock is held
203 irqfd_deactivate(struct _irqfd
*irqfd
)
205 BUG_ON(!irqfd_is_active(irqfd
));
207 list_del_init(&irqfd
->list
);
209 queue_work(irqfd_cleanup_wq
, &irqfd
->shutdown
);
213 * Called with wqh->lock held and interrupts disabled
216 irqfd_wakeup(wait_queue_t
*wait
, unsigned mode
, int sync
, void *key
)
218 struct _irqfd
*irqfd
= container_of(wait
, struct _irqfd
, wait
);
219 unsigned long flags
= (unsigned long)key
;
220 struct kvm_kernel_irq_routing_entry
*irq
;
221 struct kvm
*kvm
= irqfd
->kvm
;
223 if (flags
& POLLIN
) {
225 irq
= rcu_dereference(irqfd
->irq_entry
);
226 /* An event has been signaled, inject an interrupt */
228 kvm_set_msi(irq
, kvm
, KVM_USERSPACE_IRQ_SOURCE_ID
, 1);
230 schedule_work(&irqfd
->inject
);
234 if (flags
& POLLHUP
) {
235 /* The eventfd is closing, detach from KVM */
238 spin_lock_irqsave(&kvm
->irqfds
.lock
, flags
);
241 * We must check if someone deactivated the irqfd before
242 * we could acquire the irqfds.lock since the item is
243 * deactivated from the KVM side before it is unhooked from
244 * the wait-queue. If it is already deactivated, we can
245 * simply return knowing the other side will cleanup for us.
246 * We cannot race against the irqfd going away since the
247 * other side is required to acquire wqh->lock, which we hold
249 if (irqfd_is_active(irqfd
))
250 irqfd_deactivate(irqfd
);
252 spin_unlock_irqrestore(&kvm
->irqfds
.lock
, flags
);
259 irqfd_ptable_queue_proc(struct file
*file
, wait_queue_head_t
*wqh
,
262 struct _irqfd
*irqfd
= container_of(pt
, struct _irqfd
, pt
);
263 add_wait_queue(wqh
, &irqfd
->wait
);
266 /* Must be called under irqfds.lock */
267 static void irqfd_update(struct kvm
*kvm
, struct _irqfd
*irqfd
,
268 struct kvm_irq_routing_table
*irq_rt
)
270 struct kvm_kernel_irq_routing_entry
*e
;
271 struct hlist_node
*n
;
273 if (irqfd
->gsi
>= irq_rt
->nr_rt_entries
) {
274 rcu_assign_pointer(irqfd
->irq_entry
, NULL
);
278 hlist_for_each_entry(e
, n
, &irq_rt
->map
[irqfd
->gsi
], link
) {
279 /* Only fast-path MSI. */
280 if (e
->type
== KVM_IRQ_ROUTING_MSI
)
281 rcu_assign_pointer(irqfd
->irq_entry
, e
);
283 rcu_assign_pointer(irqfd
->irq_entry
, NULL
);
288 kvm_irqfd_assign(struct kvm
*kvm
, struct kvm_irqfd
*args
)
290 struct kvm_irq_routing_table
*irq_rt
;
291 struct _irqfd
*irqfd
, *tmp
;
292 struct file
*file
= NULL
;
293 struct eventfd_ctx
*eventfd
= NULL
, *resamplefd
= NULL
;
297 irqfd
= kzalloc(sizeof(*irqfd
), GFP_KERNEL
);
302 irqfd
->gsi
= args
->gsi
;
303 INIT_LIST_HEAD(&irqfd
->list
);
304 INIT_WORK(&irqfd
->inject
, irqfd_inject
);
305 INIT_WORK(&irqfd
->shutdown
, irqfd_shutdown
);
307 file
= eventfd_fget(args
->fd
);
313 eventfd
= eventfd_ctx_fileget(file
);
314 if (IS_ERR(eventfd
)) {
315 ret
= PTR_ERR(eventfd
);
319 irqfd
->eventfd
= eventfd
;
321 if (args
->flags
& KVM_IRQFD_FLAG_RESAMPLE
) {
322 struct _irqfd_resampler
*resampler
;
324 resamplefd
= eventfd_ctx_fdget(args
->resamplefd
);
325 if (IS_ERR(resamplefd
)) {
326 ret
= PTR_ERR(resamplefd
);
330 irqfd
->resamplefd
= resamplefd
;
331 INIT_LIST_HEAD(&irqfd
->resampler_link
);
333 mutex_lock(&kvm
->irqfds
.resampler_lock
);
335 list_for_each_entry(resampler
,
336 &kvm
->irqfds
.resampler_list
, link
) {
337 if (resampler
->notifier
.gsi
== irqfd
->gsi
) {
338 irqfd
->resampler
= resampler
;
343 if (!irqfd
->resampler
) {
344 resampler
= kzalloc(sizeof(*resampler
), GFP_KERNEL
);
347 mutex_unlock(&kvm
->irqfds
.resampler_lock
);
351 resampler
->kvm
= kvm
;
352 INIT_LIST_HEAD(&resampler
->list
);
353 resampler
->notifier
.gsi
= irqfd
->gsi
;
354 resampler
->notifier
.irq_acked
= irqfd_resampler_ack
;
355 INIT_LIST_HEAD(&resampler
->link
);
357 list_add(&resampler
->link
, &kvm
->irqfds
.resampler_list
);
358 kvm_register_irq_ack_notifier(kvm
,
359 &resampler
->notifier
);
360 irqfd
->resampler
= resampler
;
363 list_add_rcu(&irqfd
->resampler_link
, &irqfd
->resampler
->list
);
366 mutex_unlock(&kvm
->irqfds
.resampler_lock
);
370 * Install our own custom wake-up handling so we are notified via
371 * a callback whenever someone signals the underlying eventfd
373 init_waitqueue_func_entry(&irqfd
->wait
, irqfd_wakeup
);
374 init_poll_funcptr(&irqfd
->pt
, irqfd_ptable_queue_proc
);
376 spin_lock_irq(&kvm
->irqfds
.lock
);
379 list_for_each_entry(tmp
, &kvm
->irqfds
.items
, list
) {
380 if (irqfd
->eventfd
!= tmp
->eventfd
)
382 /* This fd is used for another irq already. */
384 spin_unlock_irq(&kvm
->irqfds
.lock
);
388 irq_rt
= rcu_dereference_protected(kvm
->irq_routing
,
389 lockdep_is_held(&kvm
->irqfds
.lock
));
390 irqfd_update(kvm
, irqfd
, irq_rt
);
392 events
= file
->f_op
->poll(file
, &irqfd
->pt
);
394 list_add_tail(&irqfd
->list
, &kvm
->irqfds
.items
);
397 * Check if there was an event already pending on the eventfd
398 * before we registered, and trigger it as if we didn't miss it.
401 schedule_work(&irqfd
->inject
);
403 spin_unlock_irq(&kvm
->irqfds
.lock
);
406 * do not drop the file until the irqfd is fully initialized, otherwise
407 * we might race against the POLLHUP
414 if (irqfd
->resampler
)
415 irqfd_resampler_shutdown(irqfd
);
417 if (resamplefd
&& !IS_ERR(resamplefd
))
418 eventfd_ctx_put(resamplefd
);
420 if (eventfd
&& !IS_ERR(eventfd
))
421 eventfd_ctx_put(eventfd
);
432 kvm_eventfd_init(struct kvm
*kvm
)
434 #ifdef __KVM_HAVE_IOAPIC
435 spin_lock_init(&kvm
->irqfds
.lock
);
436 INIT_LIST_HEAD(&kvm
->irqfds
.items
);
437 INIT_LIST_HEAD(&kvm
->irqfds
.resampler_list
);
438 mutex_init(&kvm
->irqfds
.resampler_lock
);
440 INIT_LIST_HEAD(&kvm
->ioeventfds
);
443 #ifdef __KVM_HAVE_IOAPIC
445 * shutdown any irqfd's that match fd+gsi
448 kvm_irqfd_deassign(struct kvm
*kvm
, struct kvm_irqfd
*args
)
450 struct _irqfd
*irqfd
, *tmp
;
451 struct eventfd_ctx
*eventfd
;
453 eventfd
= eventfd_ctx_fdget(args
->fd
);
455 return PTR_ERR(eventfd
);
457 spin_lock_irq(&kvm
->irqfds
.lock
);
459 list_for_each_entry_safe(irqfd
, tmp
, &kvm
->irqfds
.items
, list
) {
460 if (irqfd
->eventfd
== eventfd
&& irqfd
->gsi
== args
->gsi
) {
462 * This rcu_assign_pointer is needed for when
463 * another thread calls kvm_irq_routing_update before
464 * we flush workqueue below (we synchronize with
465 * kvm_irq_routing_update using irqfds.lock).
466 * It is paired with synchronize_rcu done by caller
469 rcu_assign_pointer(irqfd
->irq_entry
, NULL
);
470 irqfd_deactivate(irqfd
);
474 spin_unlock_irq(&kvm
->irqfds
.lock
);
475 eventfd_ctx_put(eventfd
);
478 * Block until we know all outstanding shutdown jobs have completed
479 * so that we guarantee there will not be any more interrupts on this
480 * gsi once this deassign function returns.
482 flush_workqueue(irqfd_cleanup_wq
);
488 kvm_irqfd(struct kvm
*kvm
, struct kvm_irqfd
*args
)
490 if (args
->flags
& ~(KVM_IRQFD_FLAG_DEASSIGN
| KVM_IRQFD_FLAG_RESAMPLE
))
493 if (args
->flags
& KVM_IRQFD_FLAG_DEASSIGN
)
494 return kvm_irqfd_deassign(kvm
, args
);
496 return kvm_irqfd_assign(kvm
, args
);
500 * This function is called as the kvm VM fd is being released. Shutdown all
501 * irqfds that still remain open
504 kvm_irqfd_release(struct kvm
*kvm
)
506 struct _irqfd
*irqfd
, *tmp
;
508 spin_lock_irq(&kvm
->irqfds
.lock
);
510 list_for_each_entry_safe(irqfd
, tmp
, &kvm
->irqfds
.items
, list
)
511 irqfd_deactivate(irqfd
);
513 spin_unlock_irq(&kvm
->irqfds
.lock
);
516 * Block until we know all outstanding shutdown jobs have completed
517 * since we do not take a kvm* reference.
519 flush_workqueue(irqfd_cleanup_wq
);
524 * Change irq_routing and irqfd.
525 * Caller must invoke synchronize_rcu afterwards.
527 void kvm_irq_routing_update(struct kvm
*kvm
,
528 struct kvm_irq_routing_table
*irq_rt
)
530 struct _irqfd
*irqfd
;
532 spin_lock_irq(&kvm
->irqfds
.lock
);
534 rcu_assign_pointer(kvm
->irq_routing
, irq_rt
);
536 list_for_each_entry(irqfd
, &kvm
->irqfds
.items
, list
)
537 irqfd_update(kvm
, irqfd
, irq_rt
);
539 spin_unlock_irq(&kvm
->irqfds
.lock
);
543 * create a host-wide workqueue for issuing deferred shutdown requests
544 * aggregated from all vm* instances. We need our own isolated single-thread
545 * queue to prevent deadlock against flushing the normal work-queue.
547 static int __init
irqfd_module_init(void)
549 irqfd_cleanup_wq
= create_singlethread_workqueue("kvm-irqfd-cleanup");
550 if (!irqfd_cleanup_wq
)
556 static void __exit
irqfd_module_exit(void)
558 destroy_workqueue(irqfd_cleanup_wq
);
561 module_init(irqfd_module_init
);
562 module_exit(irqfd_module_exit
);
566 * --------------------------------------------------------------------
567 * ioeventfd: translate a PIO/MMIO memory write to an eventfd signal.
569 * userspace can register a PIO/MMIO address with an eventfd for receiving
570 * notification when the memory has been touched.
571 * --------------------------------------------------------------------
575 struct list_head list
;
578 struct eventfd_ctx
*eventfd
;
580 struct kvm_io_device dev
;
584 static inline struct _ioeventfd
*
585 to_ioeventfd(struct kvm_io_device
*dev
)
587 return container_of(dev
, struct _ioeventfd
, dev
);
591 ioeventfd_release(struct _ioeventfd
*p
)
593 eventfd_ctx_put(p
->eventfd
);
599 ioeventfd_in_range(struct _ioeventfd
*p
, gpa_t addr
, int len
, const void *val
)
603 if (!(addr
== p
->addr
&& len
== p
->length
))
604 /* address-range must be precise for a hit */
608 /* all else equal, wildcard is always a hit */
611 /* otherwise, we have to actually compare the data */
613 BUG_ON(!IS_ALIGNED((unsigned long)val
, len
));
632 return _val
== p
->datamatch
? true : false;
635 /* MMIO/PIO writes trigger an event if the addr/val match */
637 ioeventfd_write(struct kvm_io_device
*this, gpa_t addr
, int len
,
640 struct _ioeventfd
*p
= to_ioeventfd(this);
642 if (!ioeventfd_in_range(p
, addr
, len
, val
))
645 eventfd_signal(p
->eventfd
, 1);
650 * This function is called as KVM is completely shutting down. We do not
651 * need to worry about locking just nuke anything we have as quickly as possible
654 ioeventfd_destructor(struct kvm_io_device
*this)
656 struct _ioeventfd
*p
= to_ioeventfd(this);
658 ioeventfd_release(p
);
661 static const struct kvm_io_device_ops ioeventfd_ops
= {
662 .write
= ioeventfd_write
,
663 .destructor
= ioeventfd_destructor
,
666 /* assumes kvm->slots_lock held */
668 ioeventfd_check_collision(struct kvm
*kvm
, struct _ioeventfd
*p
)
670 struct _ioeventfd
*_p
;
672 list_for_each_entry(_p
, &kvm
->ioeventfds
, list
)
673 if (_p
->addr
== p
->addr
&& _p
->length
== p
->length
&&
674 (_p
->wildcard
|| p
->wildcard
||
675 _p
->datamatch
== p
->datamatch
))
682 kvm_assign_ioeventfd(struct kvm
*kvm
, struct kvm_ioeventfd
*args
)
684 int pio
= args
->flags
& KVM_IOEVENTFD_FLAG_PIO
;
685 enum kvm_bus bus_idx
= pio
? KVM_PIO_BUS
: KVM_MMIO_BUS
;
686 struct _ioeventfd
*p
;
687 struct eventfd_ctx
*eventfd
;
690 /* must be natural-word sized */
701 /* check for range overflow */
702 if (args
->addr
+ args
->len
< args
->addr
)
705 /* check for extra flags that we don't understand */
706 if (args
->flags
& ~KVM_IOEVENTFD_VALID_FLAG_MASK
)
709 eventfd
= eventfd_ctx_fdget(args
->fd
);
711 return PTR_ERR(eventfd
);
713 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
719 INIT_LIST_HEAD(&p
->list
);
720 p
->addr
= args
->addr
;
721 p
->length
= args
->len
;
722 p
->eventfd
= eventfd
;
724 /* The datamatch feature is optional, otherwise this is a wildcard */
725 if (args
->flags
& KVM_IOEVENTFD_FLAG_DATAMATCH
)
726 p
->datamatch
= args
->datamatch
;
730 mutex_lock(&kvm
->slots_lock
);
732 /* Verify that there isn't a match already */
733 if (ioeventfd_check_collision(kvm
, p
)) {
738 kvm_iodevice_init(&p
->dev
, &ioeventfd_ops
);
740 ret
= kvm_io_bus_register_dev(kvm
, bus_idx
, p
->addr
, p
->length
,
745 list_add_tail(&p
->list
, &kvm
->ioeventfds
);
747 mutex_unlock(&kvm
->slots_lock
);
752 mutex_unlock(&kvm
->slots_lock
);
756 eventfd_ctx_put(eventfd
);
762 kvm_deassign_ioeventfd(struct kvm
*kvm
, struct kvm_ioeventfd
*args
)
764 int pio
= args
->flags
& KVM_IOEVENTFD_FLAG_PIO
;
765 enum kvm_bus bus_idx
= pio
? KVM_PIO_BUS
: KVM_MMIO_BUS
;
766 struct _ioeventfd
*p
, *tmp
;
767 struct eventfd_ctx
*eventfd
;
770 eventfd
= eventfd_ctx_fdget(args
->fd
);
772 return PTR_ERR(eventfd
);
774 mutex_lock(&kvm
->slots_lock
);
776 list_for_each_entry_safe(p
, tmp
, &kvm
->ioeventfds
, list
) {
777 bool wildcard
= !(args
->flags
& KVM_IOEVENTFD_FLAG_DATAMATCH
);
779 if (p
->eventfd
!= eventfd
||
780 p
->addr
!= args
->addr
||
781 p
->length
!= args
->len
||
782 p
->wildcard
!= wildcard
)
785 if (!p
->wildcard
&& p
->datamatch
!= args
->datamatch
)
788 kvm_io_bus_unregister_dev(kvm
, bus_idx
, &p
->dev
);
789 ioeventfd_release(p
);
794 mutex_unlock(&kvm
->slots_lock
);
796 eventfd_ctx_put(eventfd
);
802 kvm_ioeventfd(struct kvm
*kvm
, struct kvm_ioeventfd
*args
)
804 if (args
->flags
& KVM_IOEVENTFD_FLAG_DEASSIGN
)
805 return kvm_deassign_ioeventfd(kvm
, args
);
807 return kvm_assign_ioeventfd(kvm
, args
);