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