rapidio/rionet: fix deadlock on SMP
[linux/fpc-iii.git] / virt / kvm / eventfd.c
blob2d4291b877924341e91021197b3b2fd63d2ff229
1 /*
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.
7 * Author:
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>
37 #include "iodev.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 {
58 struct kvm *kvm;
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;
73 struct _irqfd {
74 /* Used for MSI fast-path */
75 struct kvm *kvm;
76 wait_queue_t wait;
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 */
80 int gsi;
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;
91 poll_table pt;
92 struct work_struct shutdown;
95 static struct workqueue_struct *irqfd_cleanup_wq;
97 static void
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,
105 false);
106 kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 0,
107 false);
108 } else
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.
118 static void
119 irqfd_resampler_ack(struct kvm_irq_ack_notifier *kian)
121 struct _irqfd_resampler *resampler;
122 struct kvm *kvm;
123 struct _irqfd *irqfd;
124 int idx;
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);
140 static void
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);
156 kfree(resampler);
159 mutex_unlock(&kvm->irqfds.resampler_lock);
163 * Race-free decouple logic (ordering is critical)
165 static void
166 irqfd_shutdown(struct work_struct *work)
168 struct _irqfd *irqfd = container_of(work, struct _irqfd, shutdown);
169 u64 cnt;
172 * Synchronize with the wait-queue and unhook ourselves to prevent
173 * further events.
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);
192 kfree(irqfd);
196 /* assumes kvm->irqfds.lock is held */
197 static bool
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
208 static void
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
221 static int
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;
228 int idx;
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 */
234 if (irq)
235 kvm_set_msi(irq, kvm, KVM_USERSPACE_IRQ_SOURCE_ID, 1,
236 false);
237 else
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 */
244 unsigned long flags;
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);
263 return 0;
266 static void
267 irqfd_ptable_queue_proc(struct file *file, wait_queue_head_t *wqh,
268 poll_table *pt)
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);
282 return;
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);
289 else
290 rcu_assign_pointer(irqfd->irq_entry, NULL);
294 static int
295 kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *args)
297 struct kvm_irq_routing_table *irq_rt;
298 struct _irqfd *irqfd, *tmp;
299 struct fd f;
300 struct eventfd_ctx *eventfd = NULL, *resamplefd = NULL;
301 int ret;
302 unsigned int events;
304 irqfd = kzalloc(sizeof(*irqfd), GFP_KERNEL);
305 if (!irqfd)
306 return -ENOMEM;
308 irqfd->kvm = kvm;
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);
314 f = fdget(args->fd);
315 if (!f.file) {
316 ret = -EBADF;
317 goto out;
320 eventfd = eventfd_ctx_fileget(f.file);
321 if (IS_ERR(eventfd)) {
322 ret = PTR_ERR(eventfd);
323 goto fail;
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);
334 goto fail;
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;
346 break;
350 if (!irqfd->resampler) {
351 resampler = kzalloc(sizeof(*resampler), GFP_KERNEL);
352 if (!resampler) {
353 ret = -ENOMEM;
354 mutex_unlock(&kvm->irqfds.resampler_lock);
355 goto fail;
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);
385 ret = 0;
386 list_for_each_entry(tmp, &kvm->irqfds.items, list) {
387 if (irqfd->eventfd != tmp->eventfd)
388 continue;
389 /* This fd is used for another irq already. */
390 ret = -EBUSY;
391 spin_unlock_irq(&kvm->irqfds.lock);
392 goto fail;
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);
409 if (events & POLLIN)
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
416 fdput(f);
418 return 0;
420 fail:
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);
430 fdput(f);
432 out:
433 kfree(irqfd);
434 return ret;
436 #endif
438 void
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);
446 #endif
447 INIT_LIST_HEAD(&kvm->ioeventfds);
450 #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
452 * shutdown any irqfd's that match fd+gsi
454 static int
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);
461 if (IS_ERR(eventfd))
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
474 * of that function.
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);
491 return 0;
495 kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args)
497 if (args->flags & ~(KVM_IRQFD_FLAG_DEASSIGN | KVM_IRQFD_FLAG_RESAMPLE))
498 return -EINVAL;
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
510 void
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)
558 return -ENOMEM;
560 return 0;
563 void kvm_irqfd_exit(void)
565 destroy_workqueue(irqfd_cleanup_wq);
567 #endif
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 * --------------------------------------------------------------------
578 struct _ioeventfd {
579 struct list_head list;
580 u64 addr;
581 int length;
582 struct eventfd_ctx *eventfd;
583 u64 datamatch;
584 struct kvm_io_device dev;
585 u8 bus_idx;
586 bool wildcard;
589 static inline struct _ioeventfd *
590 to_ioeventfd(struct kvm_io_device *dev)
592 return container_of(dev, struct _ioeventfd, dev);
595 static void
596 ioeventfd_release(struct _ioeventfd *p)
598 eventfd_ctx_put(p->eventfd);
599 list_del(&p->list);
600 kfree(p);
603 static bool
604 ioeventfd_in_range(struct _ioeventfd *p, gpa_t addr, int len, const void *val)
606 u64 _val;
608 if (addr != p->addr)
609 /* address must be precise for a hit */
610 return false;
612 if (!p->length)
613 /* length = 0 means only look at the address, so always a hit */
614 return true;
616 if (len != p->length)
617 /* address-range must be precise for a hit */
618 return false;
620 if (p->wildcard)
621 /* all else equal, wildcard is always a hit */
622 return true;
624 /* otherwise, we have to actually compare the data */
626 BUG_ON(!IS_ALIGNED((unsigned long)val, len));
628 switch (len) {
629 case 1:
630 _val = *(u8 *)val;
631 break;
632 case 2:
633 _val = *(u16 *)val;
634 break;
635 case 4:
636 _val = *(u32 *)val;
637 break;
638 case 8:
639 _val = *(u64 *)val;
640 break;
641 default:
642 return false;
645 return _val == p->datamatch ? true : false;
648 /* MMIO/PIO writes trigger an event if the addr/val match */
649 static int
650 ioeventfd_write(struct kvm_io_device *this, gpa_t addr, int len,
651 const void *val)
653 struct _ioeventfd *p = to_ioeventfd(this);
655 if (!ioeventfd_in_range(p, addr, len, val))
656 return -EOPNOTSUPP;
658 eventfd_signal(p->eventfd, 1);
659 return 0;
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
666 static void
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 */
680 static bool
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))))
692 return true;
694 return false;
697 static enum kvm_bus ioeventfd_bus_from_flags(__u32 flags)
699 if (flags & KVM_IOEVENTFD_FLAG_PIO)
700 return KVM_PIO_BUS;
701 if (flags & KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY)
702 return KVM_VIRTIO_CCW_NOTIFY_BUS;
703 return KVM_MMIO_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;
713 int ret;
715 eventfd = eventfd_ctx_fdget(args->fd);
716 if (IS_ERR(eventfd))
717 return PTR_ERR(eventfd);
719 p = kzalloc(sizeof(*p), GFP_KERNEL);
720 if (!p) {
721 ret = -ENOMEM;
722 goto fail;
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;
734 else
735 p->wildcard = true;
737 mutex_lock(&kvm->slots_lock);
739 /* Verify that there isn't a match already */
740 if (ioeventfd_check_collision(kvm, p)) {
741 ret = -EEXIST;
742 goto unlock_fail;
745 kvm_iodevice_init(&p->dev, &ioeventfd_ops);
747 ret = kvm_io_bus_register_dev(kvm, bus_idx, p->addr, p->length,
748 &p->dev);
749 if (ret < 0)
750 goto unlock_fail;
752 kvm->buses[bus_idx]->ioeventfd_count++;
753 list_add_tail(&p->list, &kvm->ioeventfds);
755 mutex_unlock(&kvm->slots_lock);
757 return 0;
759 unlock_fail:
760 mutex_unlock(&kvm->slots_lock);
762 fail:
763 kfree(p);
764 eventfd_ctx_put(eventfd);
766 return ret;
769 static int
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;
775 int ret = -ENOENT;
777 eventfd = eventfd_ctx_fdget(args->fd);
778 if (IS_ERR(eventfd))
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)
791 continue;
793 if (!p->wildcard && p->datamatch != args->datamatch)
794 continue;
796 kvm_io_bus_unregister_dev(kvm, bus_idx, &p->dev);
797 kvm->buses[bus_idx]->ioeventfd_count--;
798 ioeventfd_release(p);
799 ret = 0;
800 break;
803 mutex_unlock(&kvm->slots_lock);
805 eventfd_ctx_put(eventfd);
807 return ret;
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);
818 return ret;
821 static int
822 kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
824 enum kvm_bus bus_idx;
825 int ret;
827 bus_idx = ioeventfd_bus_from_flags(args->flags);
828 /* must be natural-word sized, or 0 to ignore length */
829 switch (args->len) {
830 case 0:
831 case 1:
832 case 2:
833 case 4:
834 case 8:
835 break;
836 default:
837 return -EINVAL;
840 /* check for range overflow */
841 if (args->addr + args->len < args->addr)
842 return -EINVAL;
844 /* check for extra flags that we don't understand */
845 if (args->flags & ~KVM_IOEVENTFD_VALID_FLAG_MASK)
846 return -EINVAL;
848 /* ioeventfd with no length can't be combined with DATAMATCH */
849 if (!args->len &&
850 args->flags & (KVM_IOEVENTFD_FLAG_PIO |
851 KVM_IOEVENTFD_FLAG_DATAMATCH))
852 return -EINVAL;
854 ret = kvm_assign_ioeventfd_idx(kvm, bus_idx, args);
855 if (ret)
856 goto fail;
858 /* When length is ignored, MMIO is also put on a separate bus, for
859 * faster lookups.
861 if (!args->len && bus_idx == KVM_MMIO_BUS) {
862 ret = kvm_assign_ioeventfd_idx(kvm, KVM_FAST_MMIO_BUS, args);
863 if (ret < 0)
864 goto fast_fail;
867 return 0;
869 fast_fail:
870 kvm_deassign_ioeventfd_idx(kvm, bus_idx, args);
871 fail:
872 return ret;
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);