Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 1 | /* |
| 2 | * kvm eventfd support - use eventfd objects to signal various KVM events |
| 3 | * |
| 4 | * Copyright 2009 Novell. All Rights Reserved. |
Avi Kivity | 221d059 | 2010-05-23 18:37:00 +0300 | [diff] [blame] | 5 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 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> |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 25 | #include <linux/kvm.h> |
Eric Auger | 166c977 | 2015-09-18 22:29:42 +0800 | [diff] [blame] | 26 | #include <linux/kvm_irqfd.h> |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 27 | #include <linux/workqueue.h> |
| 28 | #include <linux/syscalls.h> |
| 29 | #include <linux/wait.h> |
| 30 | #include <linux/poll.h> |
| 31 | #include <linux/file.h> |
| 32 | #include <linux/list.h> |
| 33 | #include <linux/eventfd.h> |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 34 | #include <linux/kernel.h> |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 35 | #include <linux/srcu.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 36 | #include <linux/slab.h> |
Paul Mackerras | 56f89f36 | 2014-06-30 20:51:09 +1000 | [diff] [blame] | 37 | #include <linux/seqlock.h> |
Eric Auger | 9016cfb | 2015-09-18 22:29:44 +0800 | [diff] [blame] | 38 | #include <linux/irqbypass.h> |
Paul Mackerras | e4d57e1 | 2014-06-30 20:51:12 +1000 | [diff] [blame] | 39 | #include <trace/events/kvm.h> |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 40 | |
Andre Przywara | af669ac | 2015-03-26 14:39:29 +0000 | [diff] [blame] | 41 | #include <kvm/iodev.h> |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 42 | |
Paul Mackerras | 297e210 | 2014-06-30 20:51:13 +1000 | [diff] [blame] | 43 | #ifdef CONFIG_HAVE_KVM_IRQFD |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 44 | |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 45 | |
| 46 | static void |
| 47 | irqfd_inject(struct work_struct *work) |
| 48 | { |
Eric Auger | 166c977 | 2015-09-18 22:29:42 +0800 | [diff] [blame] | 49 | struct kvm_kernel_irqfd *irqfd = |
| 50 | container_of(work, struct kvm_kernel_irqfd, inject); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 51 | struct kvm *kvm = irqfd->kvm; |
| 52 | |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 53 | if (!irqfd->resampler) { |
Yang Zhang | aa2fbe6 | 2013-04-11 19:21:40 +0800 | [diff] [blame] | 54 | kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 1, |
| 55 | false); |
| 56 | kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 0, |
| 57 | false); |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 58 | } else |
| 59 | kvm_set_irq(kvm, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID, |
Yang Zhang | aa2fbe6 | 2013-04-11 19:21:40 +0800 | [diff] [blame] | 60 | irqfd->gsi, 1, false); |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /* |
| 64 | * Since resampler irqfds share an IRQ source ID, we de-assert once |
| 65 | * then notify all of the resampler irqfds using this GSI. We can't |
| 66 | * do multiple de-asserts or we risk racing with incoming re-asserts. |
| 67 | */ |
| 68 | static void |
| 69 | irqfd_resampler_ack(struct kvm_irq_ack_notifier *kian) |
| 70 | { |
Eric Auger | 166c977 | 2015-09-18 22:29:42 +0800 | [diff] [blame] | 71 | struct kvm_kernel_irqfd_resampler *resampler; |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 72 | struct kvm *kvm; |
Eric Auger | 166c977 | 2015-09-18 22:29:42 +0800 | [diff] [blame] | 73 | struct kvm_kernel_irqfd *irqfd; |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 74 | int idx; |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 75 | |
Eric Auger | 166c977 | 2015-09-18 22:29:42 +0800 | [diff] [blame] | 76 | resampler = container_of(kian, |
| 77 | struct kvm_kernel_irqfd_resampler, notifier); |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 78 | kvm = resampler->kvm; |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 79 | |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 80 | kvm_set_irq(kvm, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID, |
Yang Zhang | aa2fbe6 | 2013-04-11 19:21:40 +0800 | [diff] [blame] | 81 | resampler->notifier.gsi, 0, false); |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 82 | |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 83 | idx = srcu_read_lock(&kvm->irq_srcu); |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 84 | |
| 85 | list_for_each_entry_rcu(irqfd, &resampler->list, resampler_link) |
| 86 | eventfd_signal(irqfd->resamplefd, 1); |
| 87 | |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 88 | srcu_read_unlock(&kvm->irq_srcu, idx); |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static void |
Eric Auger | 166c977 | 2015-09-18 22:29:42 +0800 | [diff] [blame] | 92 | irqfd_resampler_shutdown(struct kvm_kernel_irqfd *irqfd) |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 93 | { |
Eric Auger | 166c977 | 2015-09-18 22:29:42 +0800 | [diff] [blame] | 94 | struct kvm_kernel_irqfd_resampler *resampler = irqfd->resampler; |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 95 | struct kvm *kvm = resampler->kvm; |
| 96 | |
| 97 | mutex_lock(&kvm->irqfds.resampler_lock); |
| 98 | |
| 99 | list_del_rcu(&irqfd->resampler_link); |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 100 | synchronize_srcu(&kvm->irq_srcu); |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 101 | |
| 102 | if (list_empty(&resampler->list)) { |
| 103 | list_del(&resampler->link); |
| 104 | kvm_unregister_irq_ack_notifier(kvm, &resampler->notifier); |
| 105 | kvm_set_irq(kvm, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID, |
Yang Zhang | aa2fbe6 | 2013-04-11 19:21:40 +0800 | [diff] [blame] | 106 | resampler->notifier.gsi, 0, false); |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 107 | kfree(resampler); |
| 108 | } |
| 109 | |
| 110 | mutex_unlock(&kvm->irqfds.resampler_lock); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* |
| 114 | * Race-free decouple logic (ordering is critical) |
| 115 | */ |
| 116 | static void |
| 117 | irqfd_shutdown(struct work_struct *work) |
| 118 | { |
Eric Auger | 166c977 | 2015-09-18 22:29:42 +0800 | [diff] [blame] | 119 | struct kvm_kernel_irqfd *irqfd = |
| 120 | container_of(work, struct kvm_kernel_irqfd, shutdown); |
Michael S. Tsirkin | b6a114d | 2010-01-13 19:12:30 +0200 | [diff] [blame] | 121 | u64 cnt; |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 122 | |
| 123 | /* |
| 124 | * Synchronize with the wait-queue and unhook ourselves to prevent |
| 125 | * further events. |
| 126 | */ |
Michael S. Tsirkin | b6a114d | 2010-01-13 19:12:30 +0200 | [diff] [blame] | 127 | eventfd_ctx_remove_wait_queue(irqfd->eventfd, &irqfd->wait, &cnt); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 128 | |
| 129 | /* |
| 130 | * We know no new events will be scheduled at this point, so block |
| 131 | * until all previously outstanding events have completed |
| 132 | */ |
Tejun Heo | 4382973 | 2012-08-20 14:51:24 -0700 | [diff] [blame] | 133 | flush_work(&irqfd->inject); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 134 | |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 135 | if (irqfd->resampler) { |
| 136 | irqfd_resampler_shutdown(irqfd); |
| 137 | eventfd_ctx_put(irqfd->resamplefd); |
| 138 | } |
| 139 | |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 140 | /* |
| 141 | * It is now safe to release the object's resources |
| 142 | */ |
Eric Auger | 9016cfb | 2015-09-18 22:29:44 +0800 | [diff] [blame] | 143 | #ifdef CONFIG_HAVE_KVM_IRQ_BYPASS |
| 144 | irq_bypass_unregister_consumer(&irqfd->consumer); |
| 145 | #endif |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 146 | eventfd_ctx_put(irqfd->eventfd); |
| 147 | kfree(irqfd); |
| 148 | } |
| 149 | |
| 150 | |
| 151 | /* assumes kvm->irqfds.lock is held */ |
| 152 | static bool |
Eric Auger | 166c977 | 2015-09-18 22:29:42 +0800 | [diff] [blame] | 153 | irqfd_is_active(struct kvm_kernel_irqfd *irqfd) |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 154 | { |
| 155 | return list_empty(&irqfd->list) ? false : true; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Mark the irqfd as inactive and schedule it for removal |
| 160 | * |
| 161 | * assumes kvm->irqfds.lock is held |
| 162 | */ |
| 163 | static void |
Eric Auger | 166c977 | 2015-09-18 22:29:42 +0800 | [diff] [blame] | 164 | irqfd_deactivate(struct kvm_kernel_irqfd *irqfd) |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 165 | { |
| 166 | BUG_ON(!irqfd_is_active(irqfd)); |
| 167 | |
| 168 | list_del_init(&irqfd->list); |
| 169 | |
Bhaktipriya Shridhar | 3706fea | 2016-08-30 23:29:51 +0530 | [diff] [blame] | 170 | schedule_work(&irqfd->shutdown); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 171 | } |
| 172 | |
Paolo Bonzini | b97e6de | 2015-10-28 19:16:47 +0100 | [diff] [blame] | 173 | int __attribute__((weak)) kvm_arch_set_irq_inatomic( |
Andrey Smetanin | c9a5ecc | 2015-10-16 10:07:47 +0300 | [diff] [blame] | 174 | struct kvm_kernel_irq_routing_entry *irq, |
| 175 | struct kvm *kvm, int irq_source_id, |
| 176 | int level, |
| 177 | bool line_status) |
| 178 | { |
| 179 | return -EWOULDBLOCK; |
| 180 | } |
| 181 | |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 182 | /* |
| 183 | * Called with wqh->lock held and interrupts disabled |
| 184 | */ |
| 185 | static int |
| 186 | irqfd_wakeup(wait_queue_t *wait, unsigned mode, int sync, void *key) |
| 187 | { |
Eric Auger | 166c977 | 2015-09-18 22:29:42 +0800 | [diff] [blame] | 188 | struct kvm_kernel_irqfd *irqfd = |
| 189 | container_of(wait, struct kvm_kernel_irqfd, wait); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 190 | unsigned long flags = (unsigned long)key; |
Paul Mackerras | 56f89f36 | 2014-06-30 20:51:09 +1000 | [diff] [blame] | 191 | struct kvm_kernel_irq_routing_entry irq; |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 192 | struct kvm *kvm = irqfd->kvm; |
Paul Mackerras | 56f89f36 | 2014-06-30 20:51:09 +1000 | [diff] [blame] | 193 | unsigned seq; |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 194 | int idx; |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 195 | |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 196 | if (flags & POLLIN) { |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 197 | idx = srcu_read_lock(&kvm->irq_srcu); |
Paul Mackerras | 56f89f36 | 2014-06-30 20:51:09 +1000 | [diff] [blame] | 198 | do { |
| 199 | seq = read_seqcount_begin(&irqfd->irq_entry_sc); |
| 200 | irq = irqfd->irq_entry; |
| 201 | } while (read_seqcount_retry(&irqfd->irq_entry_sc, seq)); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 202 | /* An event has been signaled, inject an interrupt */ |
Paolo Bonzini | b97e6de | 2015-10-28 19:16:47 +0100 | [diff] [blame] | 203 | if (kvm_arch_set_irq_inatomic(&irq, kvm, |
| 204 | KVM_USERSPACE_IRQ_SOURCE_ID, 1, |
| 205 | false) == -EWOULDBLOCK) |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 206 | schedule_work(&irqfd->inject); |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 207 | srcu_read_unlock(&kvm->irq_srcu, idx); |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 208 | } |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 209 | |
| 210 | if (flags & POLLHUP) { |
| 211 | /* The eventfd is closing, detach from KVM */ |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 212 | unsigned long flags; |
| 213 | |
| 214 | spin_lock_irqsave(&kvm->irqfds.lock, flags); |
| 215 | |
| 216 | /* |
| 217 | * We must check if someone deactivated the irqfd before |
| 218 | * we could acquire the irqfds.lock since the item is |
| 219 | * deactivated from the KVM side before it is unhooked from |
| 220 | * the wait-queue. If it is already deactivated, we can |
| 221 | * simply return knowing the other side will cleanup for us. |
| 222 | * We cannot race against the irqfd going away since the |
| 223 | * other side is required to acquire wqh->lock, which we hold |
| 224 | */ |
| 225 | if (irqfd_is_active(irqfd)) |
| 226 | irqfd_deactivate(irqfd); |
| 227 | |
| 228 | spin_unlock_irqrestore(&kvm->irqfds.lock, flags); |
| 229 | } |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | static void |
| 235 | irqfd_ptable_queue_proc(struct file *file, wait_queue_head_t *wqh, |
| 236 | poll_table *pt) |
| 237 | { |
Eric Auger | 166c977 | 2015-09-18 22:29:42 +0800 | [diff] [blame] | 238 | struct kvm_kernel_irqfd *irqfd = |
| 239 | container_of(pt, struct kvm_kernel_irqfd, pt); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 240 | add_wait_queue(wqh, &irqfd->wait); |
| 241 | } |
| 242 | |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 243 | /* Must be called under irqfds.lock */ |
Eric Auger | 166c977 | 2015-09-18 22:29:42 +0800 | [diff] [blame] | 244 | static void irqfd_update(struct kvm *kvm, struct kvm_kernel_irqfd *irqfd) |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 245 | { |
| 246 | struct kvm_kernel_irq_routing_entry *e; |
Paul Mackerras | 8ba918d | 2014-06-30 20:51:10 +1000 | [diff] [blame] | 247 | struct kvm_kernel_irq_routing_entry entries[KVM_NR_IRQCHIPS]; |
Andrey Smetanin | 351dc647 | 2015-10-16 10:07:45 +0300 | [diff] [blame] | 248 | int n_entries; |
Paul Mackerras | 8ba918d | 2014-06-30 20:51:10 +1000 | [diff] [blame] | 249 | |
Paul Mackerras | 9957c86 | 2014-06-30 20:51:11 +1000 | [diff] [blame] | 250 | n_entries = kvm_irq_map_gsi(kvm, entries, irqfd->gsi); |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 251 | |
Paul Mackerras | 56f89f36 | 2014-06-30 20:51:09 +1000 | [diff] [blame] | 252 | write_seqcount_begin(&irqfd->irq_entry_sc); |
| 253 | |
Paul Mackerras | 8ba918d | 2014-06-30 20:51:10 +1000 | [diff] [blame] | 254 | e = entries; |
Andrey Smetanin | 351dc647 | 2015-10-16 10:07:45 +0300 | [diff] [blame] | 255 | if (n_entries == 1) |
| 256 | irqfd->irq_entry = *e; |
| 257 | else |
| 258 | irqfd->irq_entry.type = 0; |
Paul Mackerras | 56f89f36 | 2014-06-30 20:51:09 +1000 | [diff] [blame] | 259 | |
Paul Mackerras | 56f89f36 | 2014-06-30 20:51:09 +1000 | [diff] [blame] | 260 | write_seqcount_end(&irqfd->irq_entry_sc); |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 261 | } |
| 262 | |
Eric Auger | 1a02b27 | 2015-09-18 22:29:43 +0800 | [diff] [blame] | 263 | #ifdef CONFIG_HAVE_KVM_IRQ_BYPASS |
| 264 | void __attribute__((weak)) kvm_arch_irq_bypass_stop( |
| 265 | struct irq_bypass_consumer *cons) |
| 266 | { |
| 267 | } |
| 268 | |
| 269 | void __attribute__((weak)) kvm_arch_irq_bypass_start( |
| 270 | struct irq_bypass_consumer *cons) |
| 271 | { |
| 272 | } |
Feng Wu | f70c20a | 2015-09-18 22:29:53 +0800 | [diff] [blame] | 273 | |
| 274 | int __attribute__((weak)) kvm_arch_update_irqfd_routing( |
| 275 | struct kvm *kvm, unsigned int host_irq, |
| 276 | uint32_t guest_irq, bool set) |
| 277 | { |
| 278 | return 0; |
| 279 | } |
Eric Auger | 1a02b27 | 2015-09-18 22:29:43 +0800 | [diff] [blame] | 280 | #endif |
| 281 | |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 282 | static int |
Alex Williamson | d4db293 | 2012-06-29 09:56:08 -0600 | [diff] [blame] | 283 | kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *args) |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 284 | { |
Eric Auger | 166c977 | 2015-09-18 22:29:42 +0800 | [diff] [blame] | 285 | struct kvm_kernel_irqfd *irqfd, *tmp; |
Al Viro | cffe78d | 2013-08-30 15:47:17 -0400 | [diff] [blame] | 286 | struct fd f; |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 287 | struct eventfd_ctx *eventfd = NULL, *resamplefd = NULL; |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 288 | int ret; |
| 289 | unsigned int events; |
Paul Mackerras | 9957c86 | 2014-06-30 20:51:11 +1000 | [diff] [blame] | 290 | int idx; |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 291 | |
Eric Auger | 01c94e6 | 2015-03-04 11:14:33 +0100 | [diff] [blame] | 292 | if (!kvm_arch_intc_initialized(kvm)) |
| 293 | return -EAGAIN; |
| 294 | |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 295 | irqfd = kzalloc(sizeof(*irqfd), GFP_KERNEL); |
| 296 | if (!irqfd) |
| 297 | return -ENOMEM; |
| 298 | |
| 299 | irqfd->kvm = kvm; |
Alex Williamson | d4db293 | 2012-06-29 09:56:08 -0600 | [diff] [blame] | 300 | irqfd->gsi = args->gsi; |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 301 | INIT_LIST_HEAD(&irqfd->list); |
| 302 | INIT_WORK(&irqfd->inject, irqfd_inject); |
| 303 | INIT_WORK(&irqfd->shutdown, irqfd_shutdown); |
Paul Mackerras | 56f89f36 | 2014-06-30 20:51:09 +1000 | [diff] [blame] | 304 | seqcount_init(&irqfd->irq_entry_sc); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 305 | |
Al Viro | cffe78d | 2013-08-30 15:47:17 -0400 | [diff] [blame] | 306 | f = fdget(args->fd); |
| 307 | if (!f.file) { |
| 308 | ret = -EBADF; |
| 309 | goto out; |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 310 | } |
| 311 | |
Al Viro | cffe78d | 2013-08-30 15:47:17 -0400 | [diff] [blame] | 312 | eventfd = eventfd_ctx_fileget(f.file); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 313 | if (IS_ERR(eventfd)) { |
| 314 | ret = PTR_ERR(eventfd); |
| 315 | goto fail; |
| 316 | } |
| 317 | |
| 318 | irqfd->eventfd = eventfd; |
| 319 | |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 320 | if (args->flags & KVM_IRQFD_FLAG_RESAMPLE) { |
Eric Auger | 166c977 | 2015-09-18 22:29:42 +0800 | [diff] [blame] | 321 | struct kvm_kernel_irqfd_resampler *resampler; |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 322 | |
| 323 | resamplefd = eventfd_ctx_fdget(args->resamplefd); |
| 324 | if (IS_ERR(resamplefd)) { |
| 325 | ret = PTR_ERR(resamplefd); |
| 326 | goto fail; |
| 327 | } |
| 328 | |
| 329 | irqfd->resamplefd = resamplefd; |
| 330 | INIT_LIST_HEAD(&irqfd->resampler_link); |
| 331 | |
| 332 | mutex_lock(&kvm->irqfds.resampler_lock); |
| 333 | |
| 334 | list_for_each_entry(resampler, |
Alex Williamson | 49f8a1a | 2012-12-06 14:44:59 -0700 | [diff] [blame] | 335 | &kvm->irqfds.resampler_list, link) { |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 336 | if (resampler->notifier.gsi == irqfd->gsi) { |
| 337 | irqfd->resampler = resampler; |
| 338 | break; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | if (!irqfd->resampler) { |
| 343 | resampler = kzalloc(sizeof(*resampler), GFP_KERNEL); |
| 344 | if (!resampler) { |
| 345 | ret = -ENOMEM; |
| 346 | mutex_unlock(&kvm->irqfds.resampler_lock); |
| 347 | goto fail; |
| 348 | } |
| 349 | |
| 350 | resampler->kvm = kvm; |
| 351 | INIT_LIST_HEAD(&resampler->list); |
| 352 | resampler->notifier.gsi = irqfd->gsi; |
| 353 | resampler->notifier.irq_acked = irqfd_resampler_ack; |
| 354 | INIT_LIST_HEAD(&resampler->link); |
| 355 | |
| 356 | list_add(&resampler->link, &kvm->irqfds.resampler_list); |
| 357 | kvm_register_irq_ack_notifier(kvm, |
| 358 | &resampler->notifier); |
| 359 | irqfd->resampler = resampler; |
| 360 | } |
| 361 | |
| 362 | list_add_rcu(&irqfd->resampler_link, &irqfd->resampler->list); |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 363 | synchronize_srcu(&kvm->irq_srcu); |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 364 | |
| 365 | mutex_unlock(&kvm->irqfds.resampler_lock); |
| 366 | } |
| 367 | |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 368 | /* |
| 369 | * Install our own custom wake-up handling so we are notified via |
| 370 | * a callback whenever someone signals the underlying eventfd |
| 371 | */ |
| 372 | init_waitqueue_func_entry(&irqfd->wait, irqfd_wakeup); |
| 373 | init_poll_funcptr(&irqfd->pt, irqfd_ptable_queue_proc); |
| 374 | |
Michael S. Tsirkin | f1d1c30 | 2010-01-13 18:58:09 +0200 | [diff] [blame] | 375 | spin_lock_irq(&kvm->irqfds.lock); |
| 376 | |
| 377 | ret = 0; |
| 378 | list_for_each_entry(tmp, &kvm->irqfds.items, list) { |
| 379 | if (irqfd->eventfd != tmp->eventfd) |
| 380 | continue; |
| 381 | /* This fd is used for another irq already. */ |
| 382 | ret = -EBUSY; |
| 383 | spin_unlock_irq(&kvm->irqfds.lock); |
| 384 | goto fail; |
| 385 | } |
| 386 | |
Paul Mackerras | 9957c86 | 2014-06-30 20:51:11 +1000 | [diff] [blame] | 387 | idx = srcu_read_lock(&kvm->irq_srcu); |
| 388 | irqfd_update(kvm, irqfd); |
| 389 | srcu_read_unlock(&kvm->irq_srcu, idx); |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 390 | |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 391 | list_add_tail(&irqfd->list, &kvm->irqfds.items); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 392 | |
Cornelia Huck | 684a0b7 | 2014-03-17 19:11:35 +0100 | [diff] [blame] | 393 | spin_unlock_irq(&kvm->irqfds.lock); |
| 394 | |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 395 | /* |
| 396 | * Check if there was an event already pending on the eventfd |
| 397 | * before we registered, and trigger it as if we didn't miss it. |
| 398 | */ |
Cornelia Huck | 684a0b7 | 2014-03-17 19:11:35 +0100 | [diff] [blame] | 399 | events = f.file->f_op->poll(f.file, &irqfd->pt); |
| 400 | |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 401 | if (events & POLLIN) |
| 402 | schedule_work(&irqfd->inject); |
| 403 | |
| 404 | /* |
| 405 | * do not drop the file until the irqfd is fully initialized, otherwise |
| 406 | * we might race against the POLLHUP |
| 407 | */ |
Al Viro | cffe78d | 2013-08-30 15:47:17 -0400 | [diff] [blame] | 408 | fdput(f); |
Eric Auger | 9016cfb | 2015-09-18 22:29:44 +0800 | [diff] [blame] | 409 | #ifdef CONFIG_HAVE_KVM_IRQ_BYPASS |
Alex Williamson | 14717e2 | 2016-05-05 11:58:35 -0600 | [diff] [blame] | 410 | if (kvm_arch_has_irq_bypass()) { |
| 411 | irqfd->consumer.token = (void *)irqfd->eventfd; |
| 412 | irqfd->consumer.add_producer = kvm_arch_irq_bypass_add_producer; |
| 413 | irqfd->consumer.del_producer = kvm_arch_irq_bypass_del_producer; |
| 414 | irqfd->consumer.stop = kvm_arch_irq_bypass_stop; |
| 415 | irqfd->consumer.start = kvm_arch_irq_bypass_start; |
| 416 | ret = irq_bypass_register_consumer(&irqfd->consumer); |
| 417 | if (ret) |
| 418 | pr_info("irq bypass consumer (token %p) registration fails: %d\n", |
Eric Auger | 9016cfb | 2015-09-18 22:29:44 +0800 | [diff] [blame] | 419 | irqfd->consumer.token, ret); |
Alex Williamson | 14717e2 | 2016-05-05 11:58:35 -0600 | [diff] [blame] | 420 | } |
Eric Auger | 9016cfb | 2015-09-18 22:29:44 +0800 | [diff] [blame] | 421 | #endif |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 422 | |
| 423 | return 0; |
| 424 | |
| 425 | fail: |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 426 | if (irqfd->resampler) |
| 427 | irqfd_resampler_shutdown(irqfd); |
| 428 | |
| 429 | if (resamplefd && !IS_ERR(resamplefd)) |
| 430 | eventfd_ctx_put(resamplefd); |
| 431 | |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 432 | if (eventfd && !IS_ERR(eventfd)) |
| 433 | eventfd_ctx_put(eventfd); |
| 434 | |
Al Viro | cffe78d | 2013-08-30 15:47:17 -0400 | [diff] [blame] | 435 | fdput(f); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 436 | |
Al Viro | cffe78d | 2013-08-30 15:47:17 -0400 | [diff] [blame] | 437 | out: |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 438 | kfree(irqfd); |
| 439 | return ret; |
| 440 | } |
Paolo Bonzini | c77dcac | 2014-08-06 14:24:45 +0200 | [diff] [blame] | 441 | |
| 442 | bool kvm_irq_has_notifier(struct kvm *kvm, unsigned irqchip, unsigned pin) |
| 443 | { |
| 444 | struct kvm_irq_ack_notifier *kian; |
| 445 | int gsi, idx; |
| 446 | |
| 447 | idx = srcu_read_lock(&kvm->irq_srcu); |
| 448 | gsi = kvm_irq_map_chip_pin(kvm, irqchip, pin); |
| 449 | if (gsi != -1) |
| 450 | hlist_for_each_entry_rcu(kian, &kvm->irq_ack_notifier_list, |
| 451 | link) |
| 452 | if (kian->gsi == gsi) { |
| 453 | srcu_read_unlock(&kvm->irq_srcu, idx); |
| 454 | return true; |
| 455 | } |
| 456 | |
| 457 | srcu_read_unlock(&kvm->irq_srcu, idx); |
| 458 | |
| 459 | return false; |
| 460 | } |
| 461 | EXPORT_SYMBOL_GPL(kvm_irq_has_notifier); |
| 462 | |
Andrey Smetanin | ba1aefc | 2015-10-16 10:07:46 +0300 | [diff] [blame] | 463 | void kvm_notify_acked_gsi(struct kvm *kvm, int gsi) |
Paolo Bonzini | c77dcac | 2014-08-06 14:24:45 +0200 | [diff] [blame] | 464 | { |
| 465 | struct kvm_irq_ack_notifier *kian; |
Andrey Smetanin | ba1aefc | 2015-10-16 10:07:46 +0300 | [diff] [blame] | 466 | |
| 467 | hlist_for_each_entry_rcu(kian, &kvm->irq_ack_notifier_list, |
| 468 | link) |
| 469 | if (kian->gsi == gsi) |
| 470 | kian->irq_acked(kian); |
| 471 | } |
| 472 | |
| 473 | void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin) |
| 474 | { |
Paolo Bonzini | c77dcac | 2014-08-06 14:24:45 +0200 | [diff] [blame] | 475 | int gsi, idx; |
| 476 | |
| 477 | trace_kvm_ack_irq(irqchip, pin); |
| 478 | |
| 479 | idx = srcu_read_lock(&kvm->irq_srcu); |
| 480 | gsi = kvm_irq_map_chip_pin(kvm, irqchip, pin); |
| 481 | if (gsi != -1) |
Andrey Smetanin | ba1aefc | 2015-10-16 10:07:46 +0300 | [diff] [blame] | 482 | kvm_notify_acked_gsi(kvm, gsi); |
Paolo Bonzini | c77dcac | 2014-08-06 14:24:45 +0200 | [diff] [blame] | 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); |
Paolo Bonzini | c77dcac | 2014-08-06 14:24:45 +0200 | [diff] [blame] | 492 | kvm_vcpu_request_scan_ioapic(kvm); |
Paolo Bonzini | c77dcac | 2014-08-06 14:24:45 +0200 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | void kvm_unregister_irq_ack_notifier(struct kvm *kvm, |
| 496 | struct kvm_irq_ack_notifier *kian) |
| 497 | { |
| 498 | mutex_lock(&kvm->irq_lock); |
| 499 | hlist_del_init_rcu(&kian->link); |
| 500 | mutex_unlock(&kvm->irq_lock); |
| 501 | synchronize_srcu(&kvm->irq_srcu); |
Paolo Bonzini | c77dcac | 2014-08-06 14:24:45 +0200 | [diff] [blame] | 502 | kvm_vcpu_request_scan_ioapic(kvm); |
Paolo Bonzini | c77dcac | 2014-08-06 14:24:45 +0200 | [diff] [blame] | 503 | } |
Alexander Graf | 914daba | 2012-10-09 00:22:59 +0200 | [diff] [blame] | 504 | #endif |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 505 | |
| 506 | void |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 507 | kvm_eventfd_init(struct kvm *kvm) |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 508 | { |
Paul Mackerras | 297e210 | 2014-06-30 20:51:13 +1000 | [diff] [blame] | 509 | #ifdef CONFIG_HAVE_KVM_IRQFD |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 510 | spin_lock_init(&kvm->irqfds.lock); |
| 511 | INIT_LIST_HEAD(&kvm->irqfds.items); |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 512 | INIT_LIST_HEAD(&kvm->irqfds.resampler_list); |
| 513 | mutex_init(&kvm->irqfds.resampler_lock); |
Alexander Graf | 914daba | 2012-10-09 00:22:59 +0200 | [diff] [blame] | 514 | #endif |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 515 | INIT_LIST_HEAD(&kvm->ioeventfds); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 516 | } |
| 517 | |
Paul Mackerras | 297e210 | 2014-06-30 20:51:13 +1000 | [diff] [blame] | 518 | #ifdef CONFIG_HAVE_KVM_IRQFD |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 519 | /* |
| 520 | * shutdown any irqfd's that match fd+gsi |
| 521 | */ |
| 522 | static int |
Alex Williamson | d4db293 | 2012-06-29 09:56:08 -0600 | [diff] [blame] | 523 | kvm_irqfd_deassign(struct kvm *kvm, struct kvm_irqfd *args) |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 524 | { |
Eric Auger | 166c977 | 2015-09-18 22:29:42 +0800 | [diff] [blame] | 525 | struct kvm_kernel_irqfd *irqfd, *tmp; |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 526 | struct eventfd_ctx *eventfd; |
| 527 | |
Alex Williamson | d4db293 | 2012-06-29 09:56:08 -0600 | [diff] [blame] | 528 | eventfd = eventfd_ctx_fdget(args->fd); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 529 | if (IS_ERR(eventfd)) |
| 530 | return PTR_ERR(eventfd); |
| 531 | |
| 532 | spin_lock_irq(&kvm->irqfds.lock); |
| 533 | |
| 534 | list_for_each_entry_safe(irqfd, tmp, &kvm->irqfds.items, list) { |
Alex Williamson | d4db293 | 2012-06-29 09:56:08 -0600 | [diff] [blame] | 535 | if (irqfd->eventfd == eventfd && irqfd->gsi == args->gsi) { |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 536 | /* |
Paul Mackerras | 56f89f36 | 2014-06-30 20:51:09 +1000 | [diff] [blame] | 537 | * This clearing of irq_entry.type is needed for when |
Michael S. Tsirkin | c8ce057 | 2011-03-06 13:03:26 +0200 | [diff] [blame] | 538 | * another thread calls kvm_irq_routing_update before |
| 539 | * we flush workqueue below (we synchronize with |
| 540 | * kvm_irq_routing_update using irqfds.lock). |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 541 | */ |
Paul Mackerras | 56f89f36 | 2014-06-30 20:51:09 +1000 | [diff] [blame] | 542 | write_seqcount_begin(&irqfd->irq_entry_sc); |
| 543 | irqfd->irq_entry.type = 0; |
| 544 | write_seqcount_end(&irqfd->irq_entry_sc); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 545 | irqfd_deactivate(irqfd); |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 546 | } |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | spin_unlock_irq(&kvm->irqfds.lock); |
| 550 | eventfd_ctx_put(eventfd); |
| 551 | |
| 552 | /* |
| 553 | * Block until we know all outstanding shutdown jobs have completed |
| 554 | * so that we guarantee there will not be any more interrupts on this |
| 555 | * gsi once this deassign function returns. |
| 556 | */ |
Bhaktipriya Shridhar | 3706fea | 2016-08-30 23:29:51 +0530 | [diff] [blame] | 557 | flush_work(&irqfd->shutdown); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 558 | |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | int |
Alex Williamson | d4db293 | 2012-06-29 09:56:08 -0600 | [diff] [blame] | 563 | kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args) |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 564 | { |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 565 | if (args->flags & ~(KVM_IRQFD_FLAG_DEASSIGN | KVM_IRQFD_FLAG_RESAMPLE)) |
Alex Williamson | 326cf03 | 2012-06-29 09:56:24 -0600 | [diff] [blame] | 566 | return -EINVAL; |
| 567 | |
Alex Williamson | d4db293 | 2012-06-29 09:56:08 -0600 | [diff] [blame] | 568 | if (args->flags & KVM_IRQFD_FLAG_DEASSIGN) |
| 569 | return kvm_irqfd_deassign(kvm, args); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 570 | |
Alex Williamson | d4db293 | 2012-06-29 09:56:08 -0600 | [diff] [blame] | 571 | return kvm_irqfd_assign(kvm, args); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /* |
| 575 | * This function is called as the kvm VM fd is being released. Shutdown all |
| 576 | * irqfds that still remain open |
| 577 | */ |
| 578 | void |
| 579 | kvm_irqfd_release(struct kvm *kvm) |
| 580 | { |
Eric Auger | 166c977 | 2015-09-18 22:29:42 +0800 | [diff] [blame] | 581 | struct kvm_kernel_irqfd *irqfd, *tmp; |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 582 | |
| 583 | spin_lock_irq(&kvm->irqfds.lock); |
| 584 | |
| 585 | list_for_each_entry_safe(irqfd, tmp, &kvm->irqfds.items, list) |
| 586 | irqfd_deactivate(irqfd); |
| 587 | |
| 588 | spin_unlock_irq(&kvm->irqfds.lock); |
| 589 | |
| 590 | /* |
| 591 | * Block until we know all outstanding shutdown jobs have completed |
| 592 | * since we do not take a kvm* reference. |
| 593 | */ |
Bhaktipriya Shridhar | 3706fea | 2016-08-30 23:29:51 +0530 | [diff] [blame] | 594 | flush_work(&irqfd->shutdown); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 595 | |
| 596 | } |
| 597 | |
| 598 | /* |
Paul Mackerras | 9957c86 | 2014-06-30 20:51:11 +1000 | [diff] [blame] | 599 | * Take note of a change in irq routing. |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 600 | * Caller must invoke synchronize_srcu(&kvm->irq_srcu) afterwards. |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 601 | */ |
Paul Mackerras | 9957c86 | 2014-06-30 20:51:11 +1000 | [diff] [blame] | 602 | void kvm_irq_routing_update(struct kvm *kvm) |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 603 | { |
Eric Auger | 166c977 | 2015-09-18 22:29:42 +0800 | [diff] [blame] | 604 | struct kvm_kernel_irqfd *irqfd; |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 605 | |
| 606 | spin_lock_irq(&kvm->irqfds.lock); |
| 607 | |
Feng Wu | f70c20a | 2015-09-18 22:29:53 +0800 | [diff] [blame] | 608 | list_for_each_entry(irqfd, &kvm->irqfds.items, list) { |
Paul Mackerras | 9957c86 | 2014-06-30 20:51:11 +1000 | [diff] [blame] | 609 | irqfd_update(kvm, irqfd); |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 610 | |
Feng Wu | f70c20a | 2015-09-18 22:29:53 +0800 | [diff] [blame] | 611 | #ifdef CONFIG_HAVE_KVM_IRQ_BYPASS |
| 612 | if (irqfd->producer) { |
| 613 | int ret = kvm_arch_update_irqfd_routing( |
| 614 | irqfd->kvm, irqfd->producer->irq, |
| 615 | irqfd->gsi, 1); |
| 616 | WARN_ON(ret); |
| 617 | } |
| 618 | #endif |
| 619 | } |
| 620 | |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 621 | spin_unlock_irq(&kvm->irqfds.lock); |
| 622 | } |
| 623 | |
Cornelia Huck | a0f155e | 2013-02-28 12:33:18 +0100 | [diff] [blame] | 624 | void kvm_irqfd_exit(void) |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 625 | { |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 626 | } |
Alexander Graf | 914daba | 2012-10-09 00:22:59 +0200 | [diff] [blame] | 627 | #endif |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 628 | |
| 629 | /* |
| 630 | * -------------------------------------------------------------------- |
| 631 | * ioeventfd: translate a PIO/MMIO memory write to an eventfd signal. |
| 632 | * |
| 633 | * userspace can register a PIO/MMIO address with an eventfd for receiving |
| 634 | * notification when the memory has been touched. |
| 635 | * -------------------------------------------------------------------- |
| 636 | */ |
| 637 | |
| 638 | struct _ioeventfd { |
| 639 | struct list_head list; |
| 640 | u64 addr; |
| 641 | int length; |
| 642 | struct eventfd_ctx *eventfd; |
| 643 | u64 datamatch; |
| 644 | struct kvm_io_device dev; |
Michael S. Tsirkin | 05e07f9 | 2013-04-04 13:27:21 +0300 | [diff] [blame] | 645 | u8 bus_idx; |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 646 | bool wildcard; |
| 647 | }; |
| 648 | |
| 649 | static inline struct _ioeventfd * |
| 650 | to_ioeventfd(struct kvm_io_device *dev) |
| 651 | { |
| 652 | return container_of(dev, struct _ioeventfd, dev); |
| 653 | } |
| 654 | |
| 655 | static void |
| 656 | ioeventfd_release(struct _ioeventfd *p) |
| 657 | { |
| 658 | eventfd_ctx_put(p->eventfd); |
| 659 | list_del(&p->list); |
| 660 | kfree(p); |
| 661 | } |
| 662 | |
| 663 | static bool |
| 664 | ioeventfd_in_range(struct _ioeventfd *p, gpa_t addr, int len, const void *val) |
| 665 | { |
| 666 | u64 _val; |
| 667 | |
Michael S. Tsirkin | f848a5a | 2014-03-31 21:50:38 +0300 | [diff] [blame] | 668 | if (addr != p->addr) |
| 669 | /* address must be precise for a hit */ |
| 670 | return false; |
| 671 | |
| 672 | if (!p->length) |
| 673 | /* length = 0 means only look at the address, so always a hit */ |
| 674 | return true; |
| 675 | |
| 676 | if (len != p->length) |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 677 | /* address-range must be precise for a hit */ |
| 678 | return false; |
| 679 | |
| 680 | if (p->wildcard) |
| 681 | /* all else equal, wildcard is always a hit */ |
| 682 | return true; |
| 683 | |
| 684 | /* otherwise, we have to actually compare the data */ |
| 685 | |
| 686 | BUG_ON(!IS_ALIGNED((unsigned long)val, len)); |
| 687 | |
| 688 | switch (len) { |
| 689 | case 1: |
| 690 | _val = *(u8 *)val; |
| 691 | break; |
| 692 | case 2: |
| 693 | _val = *(u16 *)val; |
| 694 | break; |
| 695 | case 4: |
| 696 | _val = *(u32 *)val; |
| 697 | break; |
| 698 | case 8: |
| 699 | _val = *(u64 *)val; |
| 700 | break; |
| 701 | default: |
| 702 | return false; |
| 703 | } |
| 704 | |
| 705 | return _val == p->datamatch ? true : false; |
| 706 | } |
| 707 | |
| 708 | /* MMIO/PIO writes trigger an event if the addr/val match */ |
| 709 | static int |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 710 | ioeventfd_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this, gpa_t addr, |
| 711 | int len, const void *val) |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 712 | { |
| 713 | struct _ioeventfd *p = to_ioeventfd(this); |
| 714 | |
| 715 | if (!ioeventfd_in_range(p, addr, len, val)) |
| 716 | return -EOPNOTSUPP; |
| 717 | |
| 718 | eventfd_signal(p->eventfd, 1); |
| 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | /* |
| 723 | * This function is called as KVM is completely shutting down. We do not |
| 724 | * need to worry about locking just nuke anything we have as quickly as possible |
| 725 | */ |
| 726 | static void |
| 727 | ioeventfd_destructor(struct kvm_io_device *this) |
| 728 | { |
| 729 | struct _ioeventfd *p = to_ioeventfd(this); |
| 730 | |
| 731 | ioeventfd_release(p); |
| 732 | } |
| 733 | |
| 734 | static const struct kvm_io_device_ops ioeventfd_ops = { |
| 735 | .write = ioeventfd_write, |
| 736 | .destructor = ioeventfd_destructor, |
| 737 | }; |
| 738 | |
| 739 | /* assumes kvm->slots_lock held */ |
| 740 | static bool |
| 741 | ioeventfd_check_collision(struct kvm *kvm, struct _ioeventfd *p) |
| 742 | { |
| 743 | struct _ioeventfd *_p; |
| 744 | |
| 745 | list_for_each_entry(_p, &kvm->ioeventfds, list) |
Michael S. Tsirkin | 05e07f9 | 2013-04-04 13:27:21 +0300 | [diff] [blame] | 746 | if (_p->bus_idx == p->bus_idx && |
Michael S. Tsirkin | f848a5a | 2014-03-31 21:50:38 +0300 | [diff] [blame] | 747 | _p->addr == p->addr && |
| 748 | (!_p->length || !p->length || |
| 749 | (_p->length == p->length && |
| 750 | (_p->wildcard || p->wildcard || |
| 751 | _p->datamatch == p->datamatch)))) |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 752 | return true; |
| 753 | |
| 754 | return false; |
| 755 | } |
| 756 | |
Cornelia Huck | 2b83451 | 2013-02-28 12:33:20 +0100 | [diff] [blame] | 757 | static enum kvm_bus ioeventfd_bus_from_flags(__u32 flags) |
| 758 | { |
| 759 | if (flags & KVM_IOEVENTFD_FLAG_PIO) |
| 760 | return KVM_PIO_BUS; |
| 761 | if (flags & KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY) |
| 762 | return KVM_VIRTIO_CCW_NOTIFY_BUS; |
| 763 | return KVM_MMIO_BUS; |
| 764 | } |
| 765 | |
Jason Wang | 85da11c | 2015-09-15 14:41:55 +0800 | [diff] [blame] | 766 | static int kvm_assign_ioeventfd_idx(struct kvm *kvm, |
| 767 | enum kvm_bus bus_idx, |
| 768 | struct kvm_ioeventfd *args) |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 769 | { |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 770 | |
Jason Wang | 85da11c | 2015-09-15 14:41:55 +0800 | [diff] [blame] | 771 | struct eventfd_ctx *eventfd; |
| 772 | struct _ioeventfd *p; |
| 773 | int ret; |
Michael S. Tsirkin | f848a5a | 2014-03-31 21:50:38 +0300 | [diff] [blame] | 774 | |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 775 | eventfd = eventfd_ctx_fdget(args->fd); |
| 776 | if (IS_ERR(eventfd)) |
| 777 | return PTR_ERR(eventfd); |
| 778 | |
| 779 | p = kzalloc(sizeof(*p), GFP_KERNEL); |
| 780 | if (!p) { |
| 781 | ret = -ENOMEM; |
| 782 | goto fail; |
| 783 | } |
| 784 | |
| 785 | INIT_LIST_HEAD(&p->list); |
| 786 | p->addr = args->addr; |
Michael S. Tsirkin | 05e07f9 | 2013-04-04 13:27:21 +0300 | [diff] [blame] | 787 | p->bus_idx = bus_idx; |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 788 | p->length = args->len; |
| 789 | p->eventfd = eventfd; |
| 790 | |
| 791 | /* The datamatch feature is optional, otherwise this is a wildcard */ |
| 792 | if (args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH) |
| 793 | p->datamatch = args->datamatch; |
| 794 | else |
| 795 | p->wildcard = true; |
| 796 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 797 | mutex_lock(&kvm->slots_lock); |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 798 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 799 | /* Verify that there isn't a match already */ |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 800 | if (ioeventfd_check_collision(kvm, p)) { |
| 801 | ret = -EEXIST; |
| 802 | goto unlock_fail; |
| 803 | } |
| 804 | |
| 805 | kvm_iodevice_init(&p->dev, &ioeventfd_ops); |
| 806 | |
Sasha Levin | 743eeb0 | 2011-07-27 16:00:48 +0300 | [diff] [blame] | 807 | ret = kvm_io_bus_register_dev(kvm, bus_idx, p->addr, p->length, |
| 808 | &p->dev); |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 809 | if (ret < 0) |
| 810 | goto unlock_fail; |
| 811 | |
Amos Kong | 6ea34c9 | 2013-05-25 06:44:15 +0800 | [diff] [blame] | 812 | kvm->buses[bus_idx]->ioeventfd_count++; |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 813 | list_add_tail(&p->list, &kvm->ioeventfds); |
| 814 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 815 | mutex_unlock(&kvm->slots_lock); |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 816 | |
| 817 | return 0; |
| 818 | |
| 819 | unlock_fail: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 820 | mutex_unlock(&kvm->slots_lock); |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 821 | |
| 822 | fail: |
| 823 | kfree(p); |
| 824 | eventfd_ctx_put(eventfd); |
| 825 | |
| 826 | return ret; |
| 827 | } |
| 828 | |
| 829 | static int |
Jason Wang | 85da11c | 2015-09-15 14:41:55 +0800 | [diff] [blame] | 830 | kvm_deassign_ioeventfd_idx(struct kvm *kvm, enum kvm_bus bus_idx, |
| 831 | struct kvm_ioeventfd *args) |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 832 | { |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 833 | struct _ioeventfd *p, *tmp; |
| 834 | struct eventfd_ctx *eventfd; |
| 835 | int ret = -ENOENT; |
| 836 | |
| 837 | eventfd = eventfd_ctx_fdget(args->fd); |
| 838 | if (IS_ERR(eventfd)) |
| 839 | return PTR_ERR(eventfd); |
| 840 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 841 | mutex_lock(&kvm->slots_lock); |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 842 | |
| 843 | list_for_each_entry_safe(p, tmp, &kvm->ioeventfds, list) { |
| 844 | bool wildcard = !(args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH); |
| 845 | |
Michael S. Tsirkin | 05e07f9 | 2013-04-04 13:27:21 +0300 | [diff] [blame] | 846 | if (p->bus_idx != bus_idx || |
| 847 | p->eventfd != eventfd || |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 848 | p->addr != args->addr || |
| 849 | p->length != args->len || |
| 850 | p->wildcard != wildcard) |
| 851 | continue; |
| 852 | |
| 853 | if (!p->wildcard && p->datamatch != args->datamatch) |
| 854 | continue; |
| 855 | |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 856 | kvm_io_bus_unregister_dev(kvm, bus_idx, &p->dev); |
Amos Kong | 6ea34c9 | 2013-05-25 06:44:15 +0800 | [diff] [blame] | 857 | kvm->buses[bus_idx]->ioeventfd_count--; |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 858 | ioeventfd_release(p); |
| 859 | ret = 0; |
| 860 | break; |
| 861 | } |
| 862 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 863 | mutex_unlock(&kvm->slots_lock); |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 864 | |
| 865 | eventfd_ctx_put(eventfd); |
| 866 | |
| 867 | return ret; |
| 868 | } |
| 869 | |
Jason Wang | 85da11c | 2015-09-15 14:41:55 +0800 | [diff] [blame] | 870 | static int kvm_deassign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) |
| 871 | { |
| 872 | enum kvm_bus bus_idx = ioeventfd_bus_from_flags(args->flags); |
Jason Wang | eefd6b0 | 2015-09-15 14:41:56 +0800 | [diff] [blame] | 873 | int ret = kvm_deassign_ioeventfd_idx(kvm, bus_idx, args); |
Jason Wang | 85da11c | 2015-09-15 14:41:55 +0800 | [diff] [blame] | 874 | |
Jason Wang | eefd6b0 | 2015-09-15 14:41:56 +0800 | [diff] [blame] | 875 | if (!args->len && bus_idx == KVM_MMIO_BUS) |
| 876 | kvm_deassign_ioeventfd_idx(kvm, KVM_FAST_MMIO_BUS, args); |
| 877 | |
| 878 | return ret; |
Jason Wang | 85da11c | 2015-09-15 14:41:55 +0800 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | static int |
| 882 | kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) |
| 883 | { |
| 884 | enum kvm_bus bus_idx; |
Jason Wang | eefd6b0 | 2015-09-15 14:41:56 +0800 | [diff] [blame] | 885 | int ret; |
Jason Wang | 85da11c | 2015-09-15 14:41:55 +0800 | [diff] [blame] | 886 | |
| 887 | bus_idx = ioeventfd_bus_from_flags(args->flags); |
| 888 | /* must be natural-word sized, or 0 to ignore length */ |
| 889 | switch (args->len) { |
| 890 | case 0: |
| 891 | case 1: |
| 892 | case 2: |
| 893 | case 4: |
| 894 | case 8: |
| 895 | break; |
| 896 | default: |
| 897 | return -EINVAL; |
| 898 | } |
| 899 | |
| 900 | /* check for range overflow */ |
| 901 | if (args->addr + args->len < args->addr) |
| 902 | return -EINVAL; |
| 903 | |
| 904 | /* check for extra flags that we don't understand */ |
| 905 | if (args->flags & ~KVM_IOEVENTFD_VALID_FLAG_MASK) |
| 906 | return -EINVAL; |
| 907 | |
| 908 | /* ioeventfd with no length can't be combined with DATAMATCH */ |
Jason Wang | e9ea506 | 2015-09-15 14:41:59 +0800 | [diff] [blame] | 909 | if (!args->len && (args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH)) |
Jason Wang | 85da11c | 2015-09-15 14:41:55 +0800 | [diff] [blame] | 910 | return -EINVAL; |
| 911 | |
Jason Wang | eefd6b0 | 2015-09-15 14:41:56 +0800 | [diff] [blame] | 912 | ret = kvm_assign_ioeventfd_idx(kvm, bus_idx, args); |
| 913 | if (ret) |
| 914 | goto fail; |
| 915 | |
| 916 | /* When length is ignored, MMIO is also put on a separate bus, for |
| 917 | * faster lookups. |
| 918 | */ |
| 919 | if (!args->len && bus_idx == KVM_MMIO_BUS) { |
| 920 | ret = kvm_assign_ioeventfd_idx(kvm, KVM_FAST_MMIO_BUS, args); |
| 921 | if (ret < 0) |
| 922 | goto fast_fail; |
| 923 | } |
| 924 | |
| 925 | return 0; |
| 926 | |
| 927 | fast_fail: |
| 928 | kvm_deassign_ioeventfd_idx(kvm, bus_idx, args); |
| 929 | fail: |
| 930 | return ret; |
Jason Wang | 85da11c | 2015-09-15 14:41:55 +0800 | [diff] [blame] | 931 | } |
| 932 | |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 933 | int |
| 934 | kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) |
| 935 | { |
| 936 | if (args->flags & KVM_IOEVENTFD_FLAG_DEASSIGN) |
| 937 | return kvm_deassign_ioeventfd(kvm, args); |
| 938 | |
| 939 | return kvm_assign_ioeventfd(kvm, args); |
| 940 | } |