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/srcu.h>
35 #include <linux/slab.h>
39 #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
41 * --------------------------------------------------------------------
42 * irqfd: Allows an fd to be used to inject an interrupt to the guest
44 * Credit goes to Avi Kivity for the original idea.
45 * --------------------------------------------------------------------
49 * Resampling irqfds are a special variety of irqfds used to emulate
50 * level triggered interrupts. The interrupt is asserted on eventfd
51 * trigger. On acknowledgement through the irq ack notifier, the
52 * interrupt is de-asserted and userspace is notified through the
53 * resamplefd. All resamplers on the same gsi are de-asserted
54 * together, so we don't need to track the state of each individual
55 * user. We can also therefore share the same irq source ID.
57 struct _irqfd_resampler
{
60 * List of resampling struct _irqfd objects sharing this gsi.
61 * RCU list modified under kvm->irqfds.resampler_lock
63 struct list_head list
;
64 struct kvm_irq_ack_notifier notifier
;
66 * Entry in list of kvm->irqfd.resampler_list. Use for sharing
67 * resamplers among irqfds on the same gsi.
68 * Accessed and modified under kvm->irqfds.resampler_lock
70 struct list_head link
;
74 /* Used for MSI fast-path */
77 /* Update side is protected by irqfds.lock */
78 struct kvm_kernel_irq_routing_entry __rcu
*irq_entry
;
79 /* Used for level IRQ fast-path */
81 struct work_struct inject
;
82 /* The resampler used by this irqfd (resampler-only) */
83 struct _irqfd_resampler
*resampler
;
84 /* Eventfd notified on resample (resampler-only) */
85 struct eventfd_ctx
*resamplefd
;
86 /* Entry in list of irqfds for a resampler (resampler-only) */
87 struct list_head resampler_link
;
88 /* Used for setup/shutdown */
89 struct eventfd_ctx
*eventfd
;
90 struct list_head list
;
92 struct work_struct shutdown
;
95 static struct workqueue_struct
*irqfd_cleanup_wq
;
98 irqfd_inject(struct work_struct
*work
)
100 struct _irqfd
*irqfd
= container_of(work
, struct _irqfd
, inject
);
101 struct kvm
*kvm
= irqfd
->kvm
;
103 if (!irqfd
->resampler
) {
104 kvm_set_irq(kvm
, KVM_USERSPACE_IRQ_SOURCE_ID
, irqfd
->gsi
, 1,
106 kvm_set_irq(kvm
, KVM_USERSPACE_IRQ_SOURCE_ID
, irqfd
->gsi
, 0,
109 kvm_set_irq(kvm
, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID
,
110 irqfd
->gsi
, 1, false);
114 * Since resampler irqfds share an IRQ source ID, we de-assert once
115 * then notify all of the resampler irqfds using this GSI. We can't
116 * do multiple de-asserts or we risk racing with incoming re-asserts.
119 irqfd_resampler_ack(struct kvm_irq_ack_notifier
*kian
)
121 struct _irqfd_resampler
*resampler
;
123 struct _irqfd
*irqfd
;
126 resampler
= container_of(kian
, struct _irqfd_resampler
, notifier
);
127 kvm
= resampler
->kvm
;
129 kvm_set_irq(kvm
, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID
,
130 resampler
->notifier
.gsi
, 0, false);
132 idx
= srcu_read_lock(&kvm
->irq_srcu
);
134 list_for_each_entry_rcu(irqfd
, &resampler
->list
, resampler_link
)
135 eventfd_signal(irqfd
->resamplefd
, 1);
137 srcu_read_unlock(&kvm
->irq_srcu
, idx
);
141 irqfd_resampler_shutdown(struct _irqfd
*irqfd
)
143 struct _irqfd_resampler
*resampler
= irqfd
->resampler
;
144 struct kvm
*kvm
= resampler
->kvm
;
146 mutex_lock(&kvm
->irqfds
.resampler_lock
);
148 list_del_rcu(&irqfd
->resampler_link
);
149 synchronize_srcu(&kvm
->irq_srcu
);
151 if (list_empty(&resampler
->list
)) {
152 list_del(&resampler
->link
);
153 kvm_unregister_irq_ack_notifier(kvm
, &resampler
->notifier
);
154 kvm_set_irq(kvm
, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID
,
155 resampler
->notifier
.gsi
, 0, false);
159 mutex_unlock(&kvm
->irqfds
.resampler_lock
);
163 * Race-free decouple logic (ordering is critical)
166 irqfd_shutdown(struct work_struct
*work
)
168 struct _irqfd
*irqfd
= container_of(work
, struct _irqfd
, shutdown
);
172 * Synchronize with the wait-queue and unhook ourselves to prevent
175 eventfd_ctx_remove_wait_queue(irqfd
->eventfd
, &irqfd
->wait
, &cnt
);
178 * We know no new events will be scheduled at this point, so block
179 * until all previously outstanding events have completed
181 flush_work(&irqfd
->inject
);
183 if (irqfd
->resampler
) {
184 irqfd_resampler_shutdown(irqfd
);
185 eventfd_ctx_put(irqfd
->resamplefd
);
189 * It is now safe to release the object's resources
191 eventfd_ctx_put(irqfd
->eventfd
);
196 /* assumes kvm->irqfds.lock is held */
198 irqfd_is_active(struct _irqfd
*irqfd
)
200 return list_empty(&irqfd
->list
) ? false : true;
204 * Mark the irqfd as inactive and schedule it for removal
206 * assumes kvm->irqfds.lock is held
209 irqfd_deactivate(struct _irqfd
*irqfd
)
211 BUG_ON(!irqfd_is_active(irqfd
));
213 list_del_init(&irqfd
->list
);
215 queue_work(irqfd_cleanup_wq
, &irqfd
->shutdown
);
219 * Called with wqh->lock held and interrupts disabled
222 irqfd_wakeup(wait_queue_t
*wait
, unsigned mode
, int sync
, void *key
)
224 struct _irqfd
*irqfd
= container_of(wait
, struct _irqfd
, wait
);
225 unsigned long flags
= (unsigned long)key
;
226 struct kvm_kernel_irq_routing_entry
*irq
;
227 struct kvm
*kvm
= irqfd
->kvm
;
230 if (flags
& POLLIN
) {
231 idx
= srcu_read_lock(&kvm
->irq_srcu
);
232 irq
= srcu_dereference(irqfd
->irq_entry
, &kvm
->irq_srcu
);
233 /* An event has been signaled, inject an interrupt */
235 kvm_set_msi(irq
, kvm
, KVM_USERSPACE_IRQ_SOURCE_ID
, 1,
238 schedule_work(&irqfd
->inject
);
239 srcu_read_unlock(&kvm
->irq_srcu
, idx
);
242 if (flags
& POLLHUP
) {
243 /* The eventfd is closing, detach from KVM */
246 spin_lock_irqsave(&kvm
->irqfds
.lock
, flags
);
249 * We must check if someone deactivated the irqfd before
250 * we could acquire the irqfds.lock since the item is
251 * deactivated from the KVM side before it is unhooked from
252 * the wait-queue. If it is already deactivated, we can
253 * simply return knowing the other side will cleanup for us.
254 * We cannot race against the irqfd going away since the
255 * other side is required to acquire wqh->lock, which we hold
257 if (irqfd_is_active(irqfd
))
258 irqfd_deactivate(irqfd
);
260 spin_unlock_irqrestore(&kvm
->irqfds
.lock
, flags
);
267 irqfd_ptable_queue_proc(struct file
*file
, wait_queue_head_t
*wqh
,
270 struct _irqfd
*irqfd
= container_of(pt
, struct _irqfd
, pt
);
271 add_wait_queue(wqh
, &irqfd
->wait
);
274 /* Must be called under irqfds.lock */
275 static void irqfd_update(struct kvm
*kvm
, struct _irqfd
*irqfd
,
276 struct kvm_irq_routing_table
*irq_rt
)
278 struct kvm_kernel_irq_routing_entry
*e
;
280 if (irqfd
->gsi
>= irq_rt
->nr_rt_entries
) {
281 rcu_assign_pointer(irqfd
->irq_entry
, NULL
);
285 hlist_for_each_entry(e
, &irq_rt
->map
[irqfd
->gsi
], link
) {
286 /* Only fast-path MSI. */
287 if (e
->type
== KVM_IRQ_ROUTING_MSI
)
288 rcu_assign_pointer(irqfd
->irq_entry
, e
);
290 rcu_assign_pointer(irqfd
->irq_entry
, NULL
);
295 kvm_irqfd_assign(struct kvm
*kvm
, struct kvm_irqfd
*args
)
297 struct kvm_irq_routing_table
*irq_rt
;
298 struct _irqfd
*irqfd
, *tmp
;
300 struct eventfd_ctx
*eventfd
= NULL
, *resamplefd
= NULL
;
304 irqfd
= kzalloc(sizeof(*irqfd
), GFP_KERNEL
);
309 irqfd
->gsi
= args
->gsi
;
310 INIT_LIST_HEAD(&irqfd
->list
);
311 INIT_WORK(&irqfd
->inject
, irqfd_inject
);
312 INIT_WORK(&irqfd
->shutdown
, irqfd_shutdown
);
320 eventfd
= eventfd_ctx_fileget(f
.file
);
321 if (IS_ERR(eventfd
)) {
322 ret
= PTR_ERR(eventfd
);
326 irqfd
->eventfd
= eventfd
;
328 if (args
->flags
& KVM_IRQFD_FLAG_RESAMPLE
) {
329 struct _irqfd_resampler
*resampler
;
331 resamplefd
= eventfd_ctx_fdget(args
->resamplefd
);
332 if (IS_ERR(resamplefd
)) {
333 ret
= PTR_ERR(resamplefd
);
337 irqfd
->resamplefd
= resamplefd
;
338 INIT_LIST_HEAD(&irqfd
->resampler_link
);
340 mutex_lock(&kvm
->irqfds
.resampler_lock
);
342 list_for_each_entry(resampler
,
343 &kvm
->irqfds
.resampler_list
, link
) {
344 if (resampler
->notifier
.gsi
== irqfd
->gsi
) {
345 irqfd
->resampler
= resampler
;
350 if (!irqfd
->resampler
) {
351 resampler
= kzalloc(sizeof(*resampler
), GFP_KERNEL
);
354 mutex_unlock(&kvm
->irqfds
.resampler_lock
);
358 resampler
->kvm
= kvm
;
359 INIT_LIST_HEAD(&resampler
->list
);
360 resampler
->notifier
.gsi
= irqfd
->gsi
;
361 resampler
->notifier
.irq_acked
= irqfd_resampler_ack
;
362 INIT_LIST_HEAD(&resampler
->link
);
364 list_add(&resampler
->link
, &kvm
->irqfds
.resampler_list
);
365 kvm_register_irq_ack_notifier(kvm
,
366 &resampler
->notifier
);
367 irqfd
->resampler
= resampler
;
370 list_add_rcu(&irqfd
->resampler_link
, &irqfd
->resampler
->list
);
371 synchronize_srcu(&kvm
->irq_srcu
);
373 mutex_unlock(&kvm
->irqfds
.resampler_lock
);
377 * Install our own custom wake-up handling so we are notified via
378 * a callback whenever someone signals the underlying eventfd
380 init_waitqueue_func_entry(&irqfd
->wait
, irqfd_wakeup
);
381 init_poll_funcptr(&irqfd
->pt
, irqfd_ptable_queue_proc
);
383 spin_lock_irq(&kvm
->irqfds
.lock
);
386 list_for_each_entry(tmp
, &kvm
->irqfds
.items
, list
) {
387 if (irqfd
->eventfd
!= tmp
->eventfd
)
389 /* This fd is used for another irq already. */
391 spin_unlock_irq(&kvm
->irqfds
.lock
);
395 irq_rt
= rcu_dereference_protected(kvm
->irq_routing
,
396 lockdep_is_held(&kvm
->irqfds
.lock
));
397 irqfd_update(kvm
, irqfd
, irq_rt
);
399 list_add_tail(&irqfd
->list
, &kvm
->irqfds
.items
);
401 spin_unlock_irq(&kvm
->irqfds
.lock
);
404 * Check if there was an event already pending on the eventfd
405 * before we registered, and trigger it as if we didn't miss it.
407 events
= f
.file
->f_op
->poll(f
.file
, &irqfd
->pt
);
410 schedule_work(&irqfd
->inject
);
413 * do not drop the file until the irqfd is fully initialized, otherwise
414 * we might race against the POLLHUP
421 if (irqfd
->resampler
)
422 irqfd_resampler_shutdown(irqfd
);
424 if (resamplefd
&& !IS_ERR(resamplefd
))
425 eventfd_ctx_put(resamplefd
);
427 if (eventfd
&& !IS_ERR(eventfd
))
428 eventfd_ctx_put(eventfd
);
439 kvm_eventfd_init(struct kvm
*kvm
)
441 #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
442 spin_lock_init(&kvm
->irqfds
.lock
);
443 INIT_LIST_HEAD(&kvm
->irqfds
.items
);
444 INIT_LIST_HEAD(&kvm
->irqfds
.resampler_list
);
445 mutex_init(&kvm
->irqfds
.resampler_lock
);
447 INIT_LIST_HEAD(&kvm
->ioeventfds
);
450 #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
452 * shutdown any irqfd's that match fd+gsi
455 kvm_irqfd_deassign(struct kvm
*kvm
, struct kvm_irqfd
*args
)
457 struct _irqfd
*irqfd
, *tmp
;
458 struct eventfd_ctx
*eventfd
;
460 eventfd
= eventfd_ctx_fdget(args
->fd
);
462 return PTR_ERR(eventfd
);
464 spin_lock_irq(&kvm
->irqfds
.lock
);
466 list_for_each_entry_safe(irqfd
, tmp
, &kvm
->irqfds
.items
, list
) {
467 if (irqfd
->eventfd
== eventfd
&& irqfd
->gsi
== args
->gsi
) {
469 * This rcu_assign_pointer is needed for when
470 * another thread calls kvm_irq_routing_update before
471 * we flush workqueue below (we synchronize with
472 * kvm_irq_routing_update using irqfds.lock).
473 * It is paired with synchronize_srcu done by caller
476 rcu_assign_pointer(irqfd
->irq_entry
, NULL
);
477 irqfd_deactivate(irqfd
);
481 spin_unlock_irq(&kvm
->irqfds
.lock
);
482 eventfd_ctx_put(eventfd
);
485 * Block until we know all outstanding shutdown jobs have completed
486 * so that we guarantee there will not be any more interrupts on this
487 * gsi once this deassign function returns.
489 flush_workqueue(irqfd_cleanup_wq
);
495 kvm_irqfd(struct kvm
*kvm
, struct kvm_irqfd
*args
)
497 if (args
->flags
& ~(KVM_IRQFD_FLAG_DEASSIGN
| KVM_IRQFD_FLAG_RESAMPLE
))
500 if (args
->flags
& KVM_IRQFD_FLAG_DEASSIGN
)
501 return kvm_irqfd_deassign(kvm
, args
);
503 return kvm_irqfd_assign(kvm
, args
);
507 * This function is called as the kvm VM fd is being released. Shutdown all
508 * irqfds that still remain open
511 kvm_irqfd_release(struct kvm
*kvm
)
513 struct _irqfd
*irqfd
, *tmp
;
515 spin_lock_irq(&kvm
->irqfds
.lock
);
517 list_for_each_entry_safe(irqfd
, tmp
, &kvm
->irqfds
.items
, list
)
518 irqfd_deactivate(irqfd
);
520 spin_unlock_irq(&kvm
->irqfds
.lock
);
523 * Block until we know all outstanding shutdown jobs have completed
524 * since we do not take a kvm* reference.
526 flush_workqueue(irqfd_cleanup_wq
);
531 * Change irq_routing and irqfd.
532 * Caller must invoke synchronize_srcu(&kvm->irq_srcu) afterwards.
534 void kvm_irq_routing_update(struct kvm
*kvm
,
535 struct kvm_irq_routing_table
*irq_rt
)
537 struct _irqfd
*irqfd
;
539 spin_lock_irq(&kvm
->irqfds
.lock
);
541 rcu_assign_pointer(kvm
->irq_routing
, irq_rt
);
543 list_for_each_entry(irqfd
, &kvm
->irqfds
.items
, list
)
544 irqfd_update(kvm
, irqfd
, irq_rt
);
546 spin_unlock_irq(&kvm
->irqfds
.lock
);
550 * create a host-wide workqueue for issuing deferred shutdown requests
551 * aggregated from all vm* instances. We need our own isolated single-thread
552 * queue to prevent deadlock against flushing the normal work-queue.
554 int kvm_irqfd_init(void)
556 irqfd_cleanup_wq
= create_singlethread_workqueue("kvm-irqfd-cleanup");
557 if (!irqfd_cleanup_wq
)
563 void kvm_irqfd_exit(void)
565 destroy_workqueue(irqfd_cleanup_wq
);
570 * --------------------------------------------------------------------
571 * ioeventfd: translate a PIO/MMIO memory write to an eventfd signal.
573 * userspace can register a PIO/MMIO address with an eventfd for receiving
574 * notification when the memory has been touched.
575 * --------------------------------------------------------------------
579 struct list_head list
;
582 struct eventfd_ctx
*eventfd
;
584 struct kvm_io_device dev
;
589 static inline struct _ioeventfd
*
590 to_ioeventfd(struct kvm_io_device
*dev
)
592 return container_of(dev
, struct _ioeventfd
, dev
);
596 ioeventfd_release(struct _ioeventfd
*p
)
598 eventfd_ctx_put(p
->eventfd
);
604 ioeventfd_in_range(struct _ioeventfd
*p
, gpa_t addr
, int len
, const void *val
)
609 /* address must be precise for a hit */
613 /* length = 0 means only look at the address, so always a hit */
616 if (len
!= p
->length
)
617 /* address-range must be precise for a hit */
621 /* all else equal, wildcard is always a hit */
624 /* otherwise, we have to actually compare the data */
626 BUG_ON(!IS_ALIGNED((unsigned long)val
, len
));
645 return _val
== p
->datamatch
? true : false;
648 /* MMIO/PIO writes trigger an event if the addr/val match */
650 ioeventfd_write(struct kvm_io_device
*this, gpa_t addr
, int len
,
653 struct _ioeventfd
*p
= to_ioeventfd(this);
655 if (!ioeventfd_in_range(p
, addr
, len
, val
))
658 eventfd_signal(p
->eventfd
, 1);
663 * This function is called as KVM is completely shutting down. We do not
664 * need to worry about locking just nuke anything we have as quickly as possible
667 ioeventfd_destructor(struct kvm_io_device
*this)
669 struct _ioeventfd
*p
= to_ioeventfd(this);
671 ioeventfd_release(p
);
674 static const struct kvm_io_device_ops ioeventfd_ops
= {
675 .write
= ioeventfd_write
,
676 .destructor
= ioeventfd_destructor
,
679 /* assumes kvm->slots_lock held */
681 ioeventfd_check_collision(struct kvm
*kvm
, struct _ioeventfd
*p
)
683 struct _ioeventfd
*_p
;
685 list_for_each_entry(_p
, &kvm
->ioeventfds
, list
)
686 if (_p
->bus_idx
== p
->bus_idx
&&
687 _p
->addr
== p
->addr
&&
688 (!_p
->length
|| !p
->length
||
689 (_p
->length
== p
->length
&&
690 (_p
->wildcard
|| p
->wildcard
||
691 _p
->datamatch
== p
->datamatch
))))
697 static enum kvm_bus
ioeventfd_bus_from_flags(__u32 flags
)
699 if (flags
& KVM_IOEVENTFD_FLAG_PIO
)
701 if (flags
& KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY
)
702 return KVM_VIRTIO_CCW_NOTIFY_BUS
;
706 static int kvm_assign_ioeventfd_idx(struct kvm
*kvm
,
707 enum kvm_bus bus_idx
,
708 struct kvm_ioeventfd
*args
)
711 struct eventfd_ctx
*eventfd
;
712 struct _ioeventfd
*p
;
715 eventfd
= eventfd_ctx_fdget(args
->fd
);
717 return PTR_ERR(eventfd
);
719 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
725 INIT_LIST_HEAD(&p
->list
);
726 p
->addr
= args
->addr
;
727 p
->bus_idx
= bus_idx
;
728 p
->length
= args
->len
;
729 p
->eventfd
= eventfd
;
731 /* The datamatch feature is optional, otherwise this is a wildcard */
732 if (args
->flags
& KVM_IOEVENTFD_FLAG_DATAMATCH
)
733 p
->datamatch
= args
->datamatch
;
737 mutex_lock(&kvm
->slots_lock
);
739 /* Verify that there isn't a match already */
740 if (ioeventfd_check_collision(kvm
, p
)) {
745 kvm_iodevice_init(&p
->dev
, &ioeventfd_ops
);
747 ret
= kvm_io_bus_register_dev(kvm
, bus_idx
, p
->addr
, p
->length
,
752 kvm
->buses
[bus_idx
]->ioeventfd_count
++;
753 list_add_tail(&p
->list
, &kvm
->ioeventfds
);
755 mutex_unlock(&kvm
->slots_lock
);
760 mutex_unlock(&kvm
->slots_lock
);
764 eventfd_ctx_put(eventfd
);
770 kvm_deassign_ioeventfd_idx(struct kvm
*kvm
, enum kvm_bus bus_idx
,
771 struct kvm_ioeventfd
*args
)
773 struct _ioeventfd
*p
, *tmp
;
774 struct eventfd_ctx
*eventfd
;
777 eventfd
= eventfd_ctx_fdget(args
->fd
);
779 return PTR_ERR(eventfd
);
781 mutex_lock(&kvm
->slots_lock
);
783 list_for_each_entry_safe(p
, tmp
, &kvm
->ioeventfds
, list
) {
784 bool wildcard
= !(args
->flags
& KVM_IOEVENTFD_FLAG_DATAMATCH
);
786 if (p
->bus_idx
!= bus_idx
||
787 p
->eventfd
!= eventfd
||
788 p
->addr
!= args
->addr
||
789 p
->length
!= args
->len
||
790 p
->wildcard
!= wildcard
)
793 if (!p
->wildcard
&& p
->datamatch
!= args
->datamatch
)
796 kvm_io_bus_unregister_dev(kvm
, bus_idx
, &p
->dev
);
797 kvm
->buses
[bus_idx
]->ioeventfd_count
--;
798 ioeventfd_release(p
);
803 mutex_unlock(&kvm
->slots_lock
);
805 eventfd_ctx_put(eventfd
);
810 static int kvm_deassign_ioeventfd(struct kvm
*kvm
, struct kvm_ioeventfd
*args
)
812 enum kvm_bus bus_idx
= ioeventfd_bus_from_flags(args
->flags
);
813 int ret
= kvm_deassign_ioeventfd_idx(kvm
, bus_idx
, args
);
815 if (!args
->len
&& bus_idx
== KVM_MMIO_BUS
)
816 kvm_deassign_ioeventfd_idx(kvm
, KVM_FAST_MMIO_BUS
, args
);
822 kvm_assign_ioeventfd(struct kvm
*kvm
, struct kvm_ioeventfd
*args
)
824 enum kvm_bus bus_idx
;
827 bus_idx
= ioeventfd_bus_from_flags(args
->flags
);
828 /* must be natural-word sized, or 0 to ignore length */
840 /* check for range overflow */
841 if (args
->addr
+ args
->len
< args
->addr
)
844 /* check for extra flags that we don't understand */
845 if (args
->flags
& ~KVM_IOEVENTFD_VALID_FLAG_MASK
)
848 /* ioeventfd with no length can't be combined with DATAMATCH */
850 args
->flags
& (KVM_IOEVENTFD_FLAG_PIO
|
851 KVM_IOEVENTFD_FLAG_DATAMATCH
))
854 ret
= kvm_assign_ioeventfd_idx(kvm
, bus_idx
, args
);
858 /* When length is ignored, MMIO is also put on a separate bus, for
861 if (!args
->len
&& bus_idx
== KVM_MMIO_BUS
) {
862 ret
= kvm_assign_ioeventfd_idx(kvm
, KVM_FAST_MMIO_BUS
, args
);
870 kvm_deassign_ioeventfd_idx(kvm
, bus_idx
, args
);
876 kvm_ioeventfd(struct kvm
*kvm
, struct kvm_ioeventfd
*args
)
878 if (args
->flags
& KVM_IOEVENTFD_FLAG_DEASSIGN
)
879 return kvm_deassign_ioeventfd(kvm
, args
);
881 return kvm_assign_ioeventfd(kvm
, args
);