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>
39 * --------------------------------------------------------------------
40 * irqfd: Allows an fd to be used to inject an interrupt to the guest
42 * Credit goes to Avi Kivity for the original idea.
43 * --------------------------------------------------------------------
48 struct eventfd_ctx
*eventfd
;
50 struct list_head list
;
53 struct work_struct inject
;
54 struct work_struct shutdown
;
57 static struct workqueue_struct
*irqfd_cleanup_wq
;
60 irqfd_inject(struct work_struct
*work
)
62 struct _irqfd
*irqfd
= container_of(work
, struct _irqfd
, inject
);
63 struct kvm
*kvm
= irqfd
->kvm
;
65 kvm_set_irq(kvm
, KVM_USERSPACE_IRQ_SOURCE_ID
, irqfd
->gsi
, 1);
66 kvm_set_irq(kvm
, KVM_USERSPACE_IRQ_SOURCE_ID
, irqfd
->gsi
, 0);
70 * Race-free decouple logic (ordering is critical)
73 irqfd_shutdown(struct work_struct
*work
)
75 struct _irqfd
*irqfd
= container_of(work
, struct _irqfd
, shutdown
);
79 * Synchronize with the wait-queue and unhook ourselves to prevent
82 eventfd_ctx_remove_wait_queue(irqfd
->eventfd
, &irqfd
->wait
, &cnt
);
85 * We know no new events will be scheduled at this point, so block
86 * until all previously outstanding events have completed
88 flush_work(&irqfd
->inject
);
91 * It is now safe to release the object's resources
93 eventfd_ctx_put(irqfd
->eventfd
);
98 /* assumes kvm->irqfds.lock is held */
100 irqfd_is_active(struct _irqfd
*irqfd
)
102 return list_empty(&irqfd
->list
) ? false : true;
106 * Mark the irqfd as inactive and schedule it for removal
108 * assumes kvm->irqfds.lock is held
111 irqfd_deactivate(struct _irqfd
*irqfd
)
113 BUG_ON(!irqfd_is_active(irqfd
));
115 list_del_init(&irqfd
->list
);
117 queue_work(irqfd_cleanup_wq
, &irqfd
->shutdown
);
121 * Called with wqh->lock held and interrupts disabled
124 irqfd_wakeup(wait_queue_t
*wait
, unsigned mode
, int sync
, void *key
)
126 struct _irqfd
*irqfd
= container_of(wait
, struct _irqfd
, wait
);
127 unsigned long flags
= (unsigned long)key
;
130 /* An event has been signaled, inject an interrupt */
131 schedule_work(&irqfd
->inject
);
133 if (flags
& POLLHUP
) {
134 /* The eventfd is closing, detach from KVM */
135 struct kvm
*kvm
= irqfd
->kvm
;
138 spin_lock_irqsave(&kvm
->irqfds
.lock
, flags
);
141 * We must check if someone deactivated the irqfd before
142 * we could acquire the irqfds.lock since the item is
143 * deactivated from the KVM side before it is unhooked from
144 * the wait-queue. If it is already deactivated, we can
145 * simply return knowing the other side will cleanup for us.
146 * We cannot race against the irqfd going away since the
147 * other side is required to acquire wqh->lock, which we hold
149 if (irqfd_is_active(irqfd
))
150 irqfd_deactivate(irqfd
);
152 spin_unlock_irqrestore(&kvm
->irqfds
.lock
, flags
);
159 irqfd_ptable_queue_proc(struct file
*file
, wait_queue_head_t
*wqh
,
162 struct _irqfd
*irqfd
= container_of(pt
, struct _irqfd
, pt
);
163 add_wait_queue(wqh
, &irqfd
->wait
);
167 kvm_irqfd_assign(struct kvm
*kvm
, int fd
, int gsi
)
169 struct _irqfd
*irqfd
, *tmp
;
170 struct file
*file
= NULL
;
171 struct eventfd_ctx
*eventfd
= NULL
;
175 irqfd
= kzalloc(sizeof(*irqfd
), GFP_KERNEL
);
181 INIT_LIST_HEAD(&irqfd
->list
);
182 INIT_WORK(&irqfd
->inject
, irqfd_inject
);
183 INIT_WORK(&irqfd
->shutdown
, irqfd_shutdown
);
185 file
= eventfd_fget(fd
);
191 eventfd
= eventfd_ctx_fileget(file
);
192 if (IS_ERR(eventfd
)) {
193 ret
= PTR_ERR(eventfd
);
197 irqfd
->eventfd
= eventfd
;
200 * Install our own custom wake-up handling so we are notified via
201 * a callback whenever someone signals the underlying eventfd
203 init_waitqueue_func_entry(&irqfd
->wait
, irqfd_wakeup
);
204 init_poll_funcptr(&irqfd
->pt
, irqfd_ptable_queue_proc
);
206 spin_lock_irq(&kvm
->irqfds
.lock
);
209 list_for_each_entry(tmp
, &kvm
->irqfds
.items
, list
) {
210 if (irqfd
->eventfd
!= tmp
->eventfd
)
212 /* This fd is used for another irq already. */
214 spin_unlock_irq(&kvm
->irqfds
.lock
);
218 events
= file
->f_op
->poll(file
, &irqfd
->pt
);
220 list_add_tail(&irqfd
->list
, &kvm
->irqfds
.items
);
221 spin_unlock_irq(&kvm
->irqfds
.lock
);
224 * Check if there was an event already pending on the eventfd
225 * before we registered, and trigger it as if we didn't miss it.
228 schedule_work(&irqfd
->inject
);
231 * do not drop the file until the irqfd is fully initialized, otherwise
232 * we might race against the POLLHUP
239 if (eventfd
&& !IS_ERR(eventfd
))
240 eventfd_ctx_put(eventfd
);
250 kvm_eventfd_init(struct kvm
*kvm
)
252 spin_lock_init(&kvm
->irqfds
.lock
);
253 INIT_LIST_HEAD(&kvm
->irqfds
.items
);
254 INIT_LIST_HEAD(&kvm
->ioeventfds
);
258 * shutdown any irqfd's that match fd+gsi
261 kvm_irqfd_deassign(struct kvm
*kvm
, int fd
, int gsi
)
263 struct _irqfd
*irqfd
, *tmp
;
264 struct eventfd_ctx
*eventfd
;
266 eventfd
= eventfd_ctx_fdget(fd
);
268 return PTR_ERR(eventfd
);
270 spin_lock_irq(&kvm
->irqfds
.lock
);
272 list_for_each_entry_safe(irqfd
, tmp
, &kvm
->irqfds
.items
, list
) {
273 if (irqfd
->eventfd
== eventfd
&& irqfd
->gsi
== gsi
)
274 irqfd_deactivate(irqfd
);
277 spin_unlock_irq(&kvm
->irqfds
.lock
);
278 eventfd_ctx_put(eventfd
);
281 * Block until we know all outstanding shutdown jobs have completed
282 * so that we guarantee there will not be any more interrupts on this
283 * gsi once this deassign function returns.
285 flush_workqueue(irqfd_cleanup_wq
);
291 kvm_irqfd(struct kvm
*kvm
, int fd
, int gsi
, int flags
)
293 if (flags
& KVM_IRQFD_FLAG_DEASSIGN
)
294 return kvm_irqfd_deassign(kvm
, fd
, gsi
);
296 return kvm_irqfd_assign(kvm
, fd
, gsi
);
300 * This function is called as the kvm VM fd is being released. Shutdown all
301 * irqfds that still remain open
304 kvm_irqfd_release(struct kvm
*kvm
)
306 struct _irqfd
*irqfd
, *tmp
;
308 spin_lock_irq(&kvm
->irqfds
.lock
);
310 list_for_each_entry_safe(irqfd
, tmp
, &kvm
->irqfds
.items
, list
)
311 irqfd_deactivate(irqfd
);
313 spin_unlock_irq(&kvm
->irqfds
.lock
);
316 * Block until we know all outstanding shutdown jobs have completed
317 * since we do not take a kvm* reference.
319 flush_workqueue(irqfd_cleanup_wq
);
324 * create a host-wide workqueue for issuing deferred shutdown requests
325 * aggregated from all vm* instances. We need our own isolated single-thread
326 * queue to prevent deadlock against flushing the normal work-queue.
328 static int __init
irqfd_module_init(void)
330 irqfd_cleanup_wq
= create_singlethread_workqueue("kvm-irqfd-cleanup");
331 if (!irqfd_cleanup_wq
)
337 static void __exit
irqfd_module_exit(void)
339 destroy_workqueue(irqfd_cleanup_wq
);
342 module_init(irqfd_module_init
);
343 module_exit(irqfd_module_exit
);
346 * --------------------------------------------------------------------
347 * ioeventfd: translate a PIO/MMIO memory write to an eventfd signal.
349 * userspace can register a PIO/MMIO address with an eventfd for receiving
350 * notification when the memory has been touched.
351 * --------------------------------------------------------------------
355 struct list_head list
;
358 struct eventfd_ctx
*eventfd
;
360 struct kvm_io_device dev
;
364 static inline struct _ioeventfd
*
365 to_ioeventfd(struct kvm_io_device
*dev
)
367 return container_of(dev
, struct _ioeventfd
, dev
);
371 ioeventfd_release(struct _ioeventfd
*p
)
373 eventfd_ctx_put(p
->eventfd
);
379 ioeventfd_in_range(struct _ioeventfd
*p
, gpa_t addr
, int len
, const void *val
)
383 if (!(addr
== p
->addr
&& len
== p
->length
))
384 /* address-range must be precise for a hit */
388 /* all else equal, wildcard is always a hit */
391 /* otherwise, we have to actually compare the data */
393 BUG_ON(!IS_ALIGNED((unsigned long)val
, len
));
412 return _val
== p
->datamatch
? true : false;
415 /* MMIO/PIO writes trigger an event if the addr/val match */
417 ioeventfd_write(struct kvm_io_device
*this, gpa_t addr
, int len
,
420 struct _ioeventfd
*p
= to_ioeventfd(this);
422 if (!ioeventfd_in_range(p
, addr
, len
, val
))
425 eventfd_signal(p
->eventfd
, 1);
430 * This function is called as KVM is completely shutting down. We do not
431 * need to worry about locking just nuke anything we have as quickly as possible
434 ioeventfd_destructor(struct kvm_io_device
*this)
436 struct _ioeventfd
*p
= to_ioeventfd(this);
438 ioeventfd_release(p
);
441 static const struct kvm_io_device_ops ioeventfd_ops
= {
442 .write
= ioeventfd_write
,
443 .destructor
= ioeventfd_destructor
,
446 /* assumes kvm->slots_lock held */
448 ioeventfd_check_collision(struct kvm
*kvm
, struct _ioeventfd
*p
)
450 struct _ioeventfd
*_p
;
452 list_for_each_entry(_p
, &kvm
->ioeventfds
, list
)
453 if (_p
->addr
== p
->addr
&& _p
->length
== p
->length
&&
454 (_p
->wildcard
|| p
->wildcard
||
455 _p
->datamatch
== p
->datamatch
))
462 kvm_assign_ioeventfd(struct kvm
*kvm
, struct kvm_ioeventfd
*args
)
464 int pio
= args
->flags
& KVM_IOEVENTFD_FLAG_PIO
;
465 enum kvm_bus bus_idx
= pio
? KVM_PIO_BUS
: KVM_MMIO_BUS
;
466 struct _ioeventfd
*p
;
467 struct eventfd_ctx
*eventfd
;
470 /* must be natural-word sized */
481 /* check for range overflow */
482 if (args
->addr
+ args
->len
< args
->addr
)
485 /* check for extra flags that we don't understand */
486 if (args
->flags
& ~KVM_IOEVENTFD_VALID_FLAG_MASK
)
489 eventfd
= eventfd_ctx_fdget(args
->fd
);
491 return PTR_ERR(eventfd
);
493 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
499 INIT_LIST_HEAD(&p
->list
);
500 p
->addr
= args
->addr
;
501 p
->length
= args
->len
;
502 p
->eventfd
= eventfd
;
504 /* The datamatch feature is optional, otherwise this is a wildcard */
505 if (args
->flags
& KVM_IOEVENTFD_FLAG_DATAMATCH
)
506 p
->datamatch
= args
->datamatch
;
510 mutex_lock(&kvm
->slots_lock
);
512 /* Verify that there isnt a match already */
513 if (ioeventfd_check_collision(kvm
, p
)) {
518 kvm_iodevice_init(&p
->dev
, &ioeventfd_ops
);
520 ret
= kvm_io_bus_register_dev(kvm
, bus_idx
, &p
->dev
);
524 list_add_tail(&p
->list
, &kvm
->ioeventfds
);
526 mutex_unlock(&kvm
->slots_lock
);
531 mutex_unlock(&kvm
->slots_lock
);
535 eventfd_ctx_put(eventfd
);
541 kvm_deassign_ioeventfd(struct kvm
*kvm
, struct kvm_ioeventfd
*args
)
543 int pio
= args
->flags
& KVM_IOEVENTFD_FLAG_PIO
;
544 enum kvm_bus bus_idx
= pio
? KVM_PIO_BUS
: KVM_MMIO_BUS
;
545 struct _ioeventfd
*p
, *tmp
;
546 struct eventfd_ctx
*eventfd
;
549 eventfd
= eventfd_ctx_fdget(args
->fd
);
551 return PTR_ERR(eventfd
);
553 mutex_lock(&kvm
->slots_lock
);
555 list_for_each_entry_safe(p
, tmp
, &kvm
->ioeventfds
, list
) {
556 bool wildcard
= !(args
->flags
& KVM_IOEVENTFD_FLAG_DATAMATCH
);
558 if (p
->eventfd
!= eventfd
||
559 p
->addr
!= args
->addr
||
560 p
->length
!= args
->len
||
561 p
->wildcard
!= wildcard
)
564 if (!p
->wildcard
&& p
->datamatch
!= args
->datamatch
)
567 kvm_io_bus_unregister_dev(kvm
, bus_idx
, &p
->dev
);
568 ioeventfd_release(p
);
573 mutex_unlock(&kvm
->slots_lock
);
575 eventfd_ctx_put(eventfd
);
581 kvm_ioeventfd(struct kvm
*kvm
, struct kvm_ioeventfd
*args
)
583 if (args
->flags
& KVM_IOEVENTFD_FLAG_DEASSIGN
)
584 return kvm_deassign_ioeventfd(kvm
, args
);
586 return kvm_assign_ioeventfd(kvm
, args
);