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> |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 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> |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 33 | #include <linux/kernel.h> |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 34 | #include <linux/srcu.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 35 | #include <linux/slab.h> |
Paul Mackerras | 56f89f3 | 2014-06-30 20:51:09 +1000 | [diff] [blame] | 36 | #include <linux/seqlock.h> |
Paul Mackerras | e4d57e1 | 2014-06-30 20:51:12 +1000 | [diff] [blame] | 37 | #include <trace/events/kvm.h> |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 38 | |
| 39 | #include "iodev.h" |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 40 | |
Paul Mackerras | 297e210 | 2014-06-30 20:51:13 +1000 | [diff] [blame] | 41 | #ifdef CONFIG_HAVE_KVM_IRQFD |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 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 | |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 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 | |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 75 | struct _irqfd { |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 76 | /* Used for MSI fast-path */ |
| 77 | struct kvm *kvm; |
| 78 | wait_queue_t wait; |
| 79 | /* Update side is protected by irqfds.lock */ |
Paul Mackerras | 56f89f3 | 2014-06-30 20:51:09 +1000 | [diff] [blame] | 80 | struct kvm_kernel_irq_routing_entry irq_entry; |
| 81 | seqcount_t irq_entry_sc; |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 82 | /* Used for level IRQ fast-path */ |
| 83 | int gsi; |
| 84 | struct work_struct inject; |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 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; |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 91 | /* Used for setup/shutdown */ |
| 92 | struct eventfd_ctx *eventfd; |
| 93 | struct list_head list; |
| 94 | poll_table pt; |
| 95 | struct work_struct shutdown; |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 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 | |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 106 | if (!irqfd->resampler) { |
Yang Zhang | aa2fbe6 | 2013-04-11 19:21:40 +0800 | [diff] [blame] | 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); |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 111 | } else |
| 112 | kvm_set_irq(kvm, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID, |
Yang Zhang | aa2fbe6 | 2013-04-11 19:21:40 +0800 | [diff] [blame] | 113 | irqfd->gsi, 1, false); |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 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; |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 125 | struct kvm *kvm; |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 126 | struct _irqfd *irqfd; |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 127 | int idx; |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 128 | |
| 129 | resampler = container_of(kian, struct _irqfd_resampler, notifier); |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 130 | kvm = resampler->kvm; |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 131 | |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 132 | kvm_set_irq(kvm, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID, |
Yang Zhang | aa2fbe6 | 2013-04-11 19:21:40 +0800 | [diff] [blame] | 133 | resampler->notifier.gsi, 0, false); |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 134 | |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 135 | idx = srcu_read_lock(&kvm->irq_srcu); |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 136 | |
| 137 | list_for_each_entry_rcu(irqfd, &resampler->list, resampler_link) |
| 138 | eventfd_signal(irqfd->resamplefd, 1); |
| 139 | |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 140 | srcu_read_unlock(&kvm->irq_srcu, idx); |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 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); |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 152 | synchronize_srcu(&kvm->irq_srcu); |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 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, |
Yang Zhang | aa2fbe6 | 2013-04-11 19:21:40 +0800 | [diff] [blame] | 158 | resampler->notifier.gsi, 0, false); |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 159 | kfree(resampler); |
| 160 | } |
| 161 | |
| 162 | mutex_unlock(&kvm->irqfds.resampler_lock); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 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); |
Michael S. Tsirkin | b6a114d | 2010-01-13 19:12:30 +0200 | [diff] [blame] | 172 | u64 cnt; |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 173 | |
| 174 | /* |
| 175 | * Synchronize with the wait-queue and unhook ourselves to prevent |
| 176 | * further events. |
| 177 | */ |
Michael S. Tsirkin | b6a114d | 2010-01-13 19:12:30 +0200 | [diff] [blame] | 178 | eventfd_ctx_remove_wait_queue(irqfd->eventfd, &irqfd->wait, &cnt); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 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 | */ |
Tejun Heo | 4382973 | 2012-08-20 14:51:24 -0700 | [diff] [blame] | 184 | flush_work(&irqfd->inject); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 185 | |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 186 | if (irqfd->resampler) { |
| 187 | irqfd_resampler_shutdown(irqfd); |
| 188 | eventfd_ctx_put(irqfd->resamplefd); |
| 189 | } |
| 190 | |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 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; |
Paul Mackerras | 56f89f3 | 2014-06-30 20:51:09 +1000 | [diff] [blame] | 229 | struct kvm_kernel_irq_routing_entry irq; |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 230 | struct kvm *kvm = irqfd->kvm; |
Paul Mackerras | 56f89f3 | 2014-06-30 20:51:09 +1000 | [diff] [blame] | 231 | unsigned seq; |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 232 | int idx; |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 233 | |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 234 | if (flags & POLLIN) { |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 235 | idx = srcu_read_lock(&kvm->irq_srcu); |
Paul Mackerras | 56f89f3 | 2014-06-30 20:51:09 +1000 | [diff] [blame] | 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)); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 240 | /* An event has been signaled, inject an interrupt */ |
Paul Mackerras | 56f89f3 | 2014-06-30 20:51:09 +1000 | [diff] [blame] | 241 | if (irq.type == KVM_IRQ_ROUTING_MSI) |
| 242 | kvm_set_msi(&irq, kvm, KVM_USERSPACE_IRQ_SOURCE_ID, 1, |
Yang Zhang | aa2fbe6 | 2013-04-11 19:21:40 +0800 | [diff] [blame] | 243 | false); |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 244 | else |
| 245 | schedule_work(&irqfd->inject); |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 246 | srcu_read_unlock(&kvm->irq_srcu, idx); |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 247 | } |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 248 | |
| 249 | if (flags & POLLHUP) { |
| 250 | /* The eventfd is closing, detach from KVM */ |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 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); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 278 | add_wait_queue(wqh, &irqfd->wait); |
| 279 | } |
| 280 | |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 281 | /* Must be called under irqfds.lock */ |
Paul Mackerras | 9957c86 | 2014-06-30 20:51:11 +1000 | [diff] [blame] | 282 | static void irqfd_update(struct kvm *kvm, struct _irqfd *irqfd) |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 283 | { |
| 284 | struct kvm_kernel_irq_routing_entry *e; |
Paul Mackerras | 8ba918d | 2014-06-30 20:51:10 +1000 | [diff] [blame] | 285 | struct kvm_kernel_irq_routing_entry entries[KVM_NR_IRQCHIPS]; |
| 286 | int i, n_entries; |
| 287 | |
Paul Mackerras | 9957c86 | 2014-06-30 20:51:11 +1000 | [diff] [blame] | 288 | n_entries = kvm_irq_map_gsi(kvm, entries, irqfd->gsi); |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 289 | |
Paul Mackerras | 56f89f3 | 2014-06-30 20:51:09 +1000 | [diff] [blame] | 290 | write_seqcount_begin(&irqfd->irq_entry_sc); |
| 291 | |
| 292 | irqfd->irq_entry.type = 0; |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 293 | |
Paul Mackerras | 8ba918d | 2014-06-30 20:51:10 +1000 | [diff] [blame] | 294 | e = entries; |
| 295 | for (i = 0; i < n_entries; ++i, ++e) { |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 296 | /* Only fast-path MSI. */ |
| 297 | if (e->type == KVM_IRQ_ROUTING_MSI) |
Paul Mackerras | 56f89f3 | 2014-06-30 20:51:09 +1000 | [diff] [blame] | 298 | irqfd->irq_entry = *e; |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 299 | } |
Paul Mackerras | 56f89f3 | 2014-06-30 20:51:09 +1000 | [diff] [blame] | 300 | |
Paul Mackerras | 56f89f3 | 2014-06-30 20:51:09 +1000 | [diff] [blame] | 301 | write_seqcount_end(&irqfd->irq_entry_sc); |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 302 | } |
| 303 | |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 304 | static int |
Alex Williamson | d4db293 | 2012-06-29 09:56:08 -0600 | [diff] [blame] | 305 | kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *args) |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 306 | { |
Michael S. Tsirkin | f1d1c30 | 2010-01-13 18:58:09 +0200 | [diff] [blame] | 307 | struct _irqfd *irqfd, *tmp; |
Al Viro | cffe78d | 2013-08-30 15:47:17 -0400 | [diff] [blame] | 308 | struct fd f; |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 309 | struct eventfd_ctx *eventfd = NULL, *resamplefd = NULL; |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 310 | int ret; |
| 311 | unsigned int events; |
Paul Mackerras | 9957c86 | 2014-06-30 20:51:11 +1000 | [diff] [blame] | 312 | int idx; |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 313 | |
| 314 | irqfd = kzalloc(sizeof(*irqfd), GFP_KERNEL); |
| 315 | if (!irqfd) |
| 316 | return -ENOMEM; |
| 317 | |
| 318 | irqfd->kvm = kvm; |
Alex Williamson | d4db293 | 2012-06-29 09:56:08 -0600 | [diff] [blame] | 319 | irqfd->gsi = args->gsi; |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 320 | INIT_LIST_HEAD(&irqfd->list); |
| 321 | INIT_WORK(&irqfd->inject, irqfd_inject); |
| 322 | INIT_WORK(&irqfd->shutdown, irqfd_shutdown); |
Paul Mackerras | 56f89f3 | 2014-06-30 20:51:09 +1000 | [diff] [blame] | 323 | seqcount_init(&irqfd->irq_entry_sc); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 324 | |
Al Viro | cffe78d | 2013-08-30 15:47:17 -0400 | [diff] [blame] | 325 | f = fdget(args->fd); |
| 326 | if (!f.file) { |
| 327 | ret = -EBADF; |
| 328 | goto out; |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 329 | } |
| 330 | |
Al Viro | cffe78d | 2013-08-30 15:47:17 -0400 | [diff] [blame] | 331 | eventfd = eventfd_ctx_fileget(f.file); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 332 | if (IS_ERR(eventfd)) { |
| 333 | ret = PTR_ERR(eventfd); |
| 334 | goto fail; |
| 335 | } |
| 336 | |
| 337 | irqfd->eventfd = eventfd; |
| 338 | |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 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, |
Alex Williamson | 49f8a1a | 2012-12-06 14:44:59 -0700 | [diff] [blame] | 354 | &kvm->irqfds.resampler_list, link) { |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 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); |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 382 | synchronize_srcu(&kvm->irq_srcu); |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 383 | |
| 384 | mutex_unlock(&kvm->irqfds.resampler_lock); |
| 385 | } |
| 386 | |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 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 | |
Michael S. Tsirkin | f1d1c30 | 2010-01-13 18:58:09 +0200 | [diff] [blame] | 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 | |
Paul Mackerras | 9957c86 | 2014-06-30 20:51:11 +1000 | [diff] [blame] | 406 | idx = srcu_read_lock(&kvm->irq_srcu); |
| 407 | irqfd_update(kvm, irqfd); |
| 408 | srcu_read_unlock(&kvm->irq_srcu, idx); |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 409 | |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 410 | list_add_tail(&irqfd->list, &kvm->irqfds.items); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 411 | |
Cornelia Huck | 684a0b7 | 2014-03-17 19:11:35 +0100 | [diff] [blame] | 412 | spin_unlock_irq(&kvm->irqfds.lock); |
| 413 | |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 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 | */ |
Cornelia Huck | 684a0b7 | 2014-03-17 19:11:35 +0100 | [diff] [blame] | 418 | events = f.file->f_op->poll(f.file, &irqfd->pt); |
| 419 | |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 420 | if (events & POLLIN) |
| 421 | schedule_work(&irqfd->inject); |
| 422 | |
| 423 | /* |
| 424 | * do not drop the file until the irqfd is fully initialized, otherwise |
| 425 | * we might race against the POLLHUP |
| 426 | */ |
Al Viro | cffe78d | 2013-08-30 15:47:17 -0400 | [diff] [blame] | 427 | fdput(f); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 428 | |
| 429 | return 0; |
| 430 | |
| 431 | fail: |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 432 | if (irqfd->resampler) |
| 433 | irqfd_resampler_shutdown(irqfd); |
| 434 | |
| 435 | if (resamplefd && !IS_ERR(resamplefd)) |
| 436 | eventfd_ctx_put(resamplefd); |
| 437 | |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 438 | if (eventfd && !IS_ERR(eventfd)) |
| 439 | eventfd_ctx_put(eventfd); |
| 440 | |
Al Viro | cffe78d | 2013-08-30 15:47:17 -0400 | [diff] [blame] | 441 | fdput(f); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 442 | |
Al Viro | cffe78d | 2013-08-30 15:47:17 -0400 | [diff] [blame] | 443 | out: |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 444 | kfree(irqfd); |
| 445 | return ret; |
| 446 | } |
Paolo Bonzini | c77dcac | 2014-08-06 14:24:45 +0200 | [diff] [blame] | 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); |
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 | { |
| 525 | struct _irqfd *irqfd, *tmp; |
| 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 | 56f89f3 | 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 | 56f89f3 | 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 | */ |
| 557 | flush_workqueue(irqfd_cleanup_wq); |
| 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 | { |
| 581 | struct _irqfd *irqfd, *tmp; |
| 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 | */ |
| 594 | flush_workqueue(irqfd_cleanup_wq); |
| 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 | { |
| 604 | struct _irqfd *irqfd; |
| 605 | |
| 606 | spin_lock_irq(&kvm->irqfds.lock); |
| 607 | |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [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 | |
| 611 | spin_unlock_irq(&kvm->irqfds.lock); |
| 612 | } |
| 613 | |
| 614 | /* |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 615 | * create a host-wide workqueue for issuing deferred shutdown requests |
| 616 | * aggregated from all vm* instances. We need our own isolated single-thread |
| 617 | * queue to prevent deadlock against flushing the normal work-queue. |
| 618 | */ |
Cornelia Huck | a0f155e | 2013-02-28 12:33:18 +0100 | [diff] [blame] | 619 | int kvm_irqfd_init(void) |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 620 | { |
| 621 | irqfd_cleanup_wq = create_singlethread_workqueue("kvm-irqfd-cleanup"); |
| 622 | if (!irqfd_cleanup_wq) |
| 623 | return -ENOMEM; |
| 624 | |
| 625 | return 0; |
| 626 | } |
| 627 | |
Cornelia Huck | a0f155e | 2013-02-28 12:33:18 +0100 | [diff] [blame] | 628 | void kvm_irqfd_exit(void) |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 629 | { |
| 630 | destroy_workqueue(irqfd_cleanup_wq); |
| 631 | } |
Alexander Graf | 914daba | 2012-10-09 00:22:59 +0200 | [diff] [blame] | 632 | #endif |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 633 | |
| 634 | /* |
| 635 | * -------------------------------------------------------------------- |
| 636 | * ioeventfd: translate a PIO/MMIO memory write to an eventfd signal. |
| 637 | * |
| 638 | * userspace can register a PIO/MMIO address with an eventfd for receiving |
| 639 | * notification when the memory has been touched. |
| 640 | * -------------------------------------------------------------------- |
| 641 | */ |
| 642 | |
| 643 | struct _ioeventfd { |
| 644 | struct list_head list; |
| 645 | u64 addr; |
| 646 | int length; |
| 647 | struct eventfd_ctx *eventfd; |
| 648 | u64 datamatch; |
| 649 | struct kvm_io_device dev; |
Michael S. Tsirkin | 05e07f9 | 2013-04-04 13:27:21 +0300 | [diff] [blame] | 650 | u8 bus_idx; |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 651 | bool wildcard; |
| 652 | }; |
| 653 | |
| 654 | static inline struct _ioeventfd * |
| 655 | to_ioeventfd(struct kvm_io_device *dev) |
| 656 | { |
| 657 | return container_of(dev, struct _ioeventfd, dev); |
| 658 | } |
| 659 | |
| 660 | static void |
| 661 | ioeventfd_release(struct _ioeventfd *p) |
| 662 | { |
| 663 | eventfd_ctx_put(p->eventfd); |
| 664 | list_del(&p->list); |
| 665 | kfree(p); |
| 666 | } |
| 667 | |
| 668 | static bool |
| 669 | ioeventfd_in_range(struct _ioeventfd *p, gpa_t addr, int len, const void *val) |
| 670 | { |
| 671 | u64 _val; |
| 672 | |
Michael S. Tsirkin | f848a5a | 2014-03-31 21:50:38 +0300 | [diff] [blame] | 673 | if (addr != p->addr) |
| 674 | /* address must be precise for a hit */ |
| 675 | return false; |
| 676 | |
| 677 | if (!p->length) |
| 678 | /* length = 0 means only look at the address, so always a hit */ |
| 679 | return true; |
| 680 | |
| 681 | if (len != p->length) |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 682 | /* address-range must be precise for a hit */ |
| 683 | return false; |
| 684 | |
| 685 | if (p->wildcard) |
| 686 | /* all else equal, wildcard is always a hit */ |
| 687 | return true; |
| 688 | |
| 689 | /* otherwise, we have to actually compare the data */ |
| 690 | |
| 691 | BUG_ON(!IS_ALIGNED((unsigned long)val, len)); |
| 692 | |
| 693 | switch (len) { |
| 694 | case 1: |
| 695 | _val = *(u8 *)val; |
| 696 | break; |
| 697 | case 2: |
| 698 | _val = *(u16 *)val; |
| 699 | break; |
| 700 | case 4: |
| 701 | _val = *(u32 *)val; |
| 702 | break; |
| 703 | case 8: |
| 704 | _val = *(u64 *)val; |
| 705 | break; |
| 706 | default: |
| 707 | return false; |
| 708 | } |
| 709 | |
| 710 | return _val == p->datamatch ? true : false; |
| 711 | } |
| 712 | |
| 713 | /* MMIO/PIO writes trigger an event if the addr/val match */ |
| 714 | static int |
| 715 | ioeventfd_write(struct kvm_io_device *this, gpa_t addr, int len, |
| 716 | const void *val) |
| 717 | { |
| 718 | struct _ioeventfd *p = to_ioeventfd(this); |
| 719 | |
| 720 | if (!ioeventfd_in_range(p, addr, len, val)) |
| 721 | return -EOPNOTSUPP; |
| 722 | |
| 723 | eventfd_signal(p->eventfd, 1); |
| 724 | return 0; |
| 725 | } |
| 726 | |
| 727 | /* |
| 728 | * This function is called as KVM is completely shutting down. We do not |
| 729 | * need to worry about locking just nuke anything we have as quickly as possible |
| 730 | */ |
| 731 | static void |
| 732 | ioeventfd_destructor(struct kvm_io_device *this) |
| 733 | { |
| 734 | struct _ioeventfd *p = to_ioeventfd(this); |
| 735 | |
| 736 | ioeventfd_release(p); |
| 737 | } |
| 738 | |
| 739 | static const struct kvm_io_device_ops ioeventfd_ops = { |
| 740 | .write = ioeventfd_write, |
| 741 | .destructor = ioeventfd_destructor, |
| 742 | }; |
| 743 | |
| 744 | /* assumes kvm->slots_lock held */ |
| 745 | static bool |
| 746 | ioeventfd_check_collision(struct kvm *kvm, struct _ioeventfd *p) |
| 747 | { |
| 748 | struct _ioeventfd *_p; |
| 749 | |
| 750 | list_for_each_entry(_p, &kvm->ioeventfds, list) |
Michael S. Tsirkin | 05e07f9 | 2013-04-04 13:27:21 +0300 | [diff] [blame] | 751 | if (_p->bus_idx == p->bus_idx && |
Michael S. Tsirkin | f848a5a | 2014-03-31 21:50:38 +0300 | [diff] [blame] | 752 | _p->addr == p->addr && |
| 753 | (!_p->length || !p->length || |
| 754 | (_p->length == p->length && |
| 755 | (_p->wildcard || p->wildcard || |
| 756 | _p->datamatch == p->datamatch)))) |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 757 | return true; |
| 758 | |
| 759 | return false; |
| 760 | } |
| 761 | |
Cornelia Huck | 2b83451 | 2013-02-28 12:33:20 +0100 | [diff] [blame] | 762 | static enum kvm_bus ioeventfd_bus_from_flags(__u32 flags) |
| 763 | { |
| 764 | if (flags & KVM_IOEVENTFD_FLAG_PIO) |
| 765 | return KVM_PIO_BUS; |
| 766 | if (flags & KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY) |
| 767 | return KVM_VIRTIO_CCW_NOTIFY_BUS; |
| 768 | return KVM_MMIO_BUS; |
| 769 | } |
| 770 | |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 771 | static int |
| 772 | kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) |
| 773 | { |
Cornelia Huck | 2b83451 | 2013-02-28 12:33:20 +0100 | [diff] [blame] | 774 | enum kvm_bus bus_idx; |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 775 | struct _ioeventfd *p; |
| 776 | struct eventfd_ctx *eventfd; |
| 777 | int ret; |
| 778 | |
Cornelia Huck | 2b83451 | 2013-02-28 12:33:20 +0100 | [diff] [blame] | 779 | bus_idx = ioeventfd_bus_from_flags(args->flags); |
Michael S. Tsirkin | f848a5a | 2014-03-31 21:50:38 +0300 | [diff] [blame] | 780 | /* must be natural-word sized, or 0 to ignore length */ |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 781 | switch (args->len) { |
Michael S. Tsirkin | f848a5a | 2014-03-31 21:50:38 +0300 | [diff] [blame] | 782 | case 0: |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 783 | case 1: |
| 784 | case 2: |
| 785 | case 4: |
| 786 | case 8: |
| 787 | break; |
| 788 | default: |
| 789 | return -EINVAL; |
| 790 | } |
| 791 | |
| 792 | /* check for range overflow */ |
| 793 | if (args->addr + args->len < args->addr) |
| 794 | return -EINVAL; |
| 795 | |
| 796 | /* check for extra flags that we don't understand */ |
| 797 | if (args->flags & ~KVM_IOEVENTFD_VALID_FLAG_MASK) |
| 798 | return -EINVAL; |
| 799 | |
Michael S. Tsirkin | f848a5a | 2014-03-31 21:50:38 +0300 | [diff] [blame] | 800 | /* ioeventfd with no length can't be combined with DATAMATCH */ |
| 801 | if (!args->len && |
| 802 | args->flags & (KVM_IOEVENTFD_FLAG_PIO | |
| 803 | KVM_IOEVENTFD_FLAG_DATAMATCH)) |
| 804 | return -EINVAL; |
| 805 | |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 806 | eventfd = eventfd_ctx_fdget(args->fd); |
| 807 | if (IS_ERR(eventfd)) |
| 808 | return PTR_ERR(eventfd); |
| 809 | |
| 810 | p = kzalloc(sizeof(*p), GFP_KERNEL); |
| 811 | if (!p) { |
| 812 | ret = -ENOMEM; |
| 813 | goto fail; |
| 814 | } |
| 815 | |
| 816 | INIT_LIST_HEAD(&p->list); |
| 817 | p->addr = args->addr; |
Michael S. Tsirkin | 05e07f9 | 2013-04-04 13:27:21 +0300 | [diff] [blame] | 818 | p->bus_idx = bus_idx; |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 819 | p->length = args->len; |
| 820 | p->eventfd = eventfd; |
| 821 | |
| 822 | /* The datamatch feature is optional, otherwise this is a wildcard */ |
| 823 | if (args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH) |
| 824 | p->datamatch = args->datamatch; |
| 825 | else |
| 826 | p->wildcard = true; |
| 827 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 828 | mutex_lock(&kvm->slots_lock); |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 829 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 830 | /* Verify that there isn't a match already */ |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 831 | if (ioeventfd_check_collision(kvm, p)) { |
| 832 | ret = -EEXIST; |
| 833 | goto unlock_fail; |
| 834 | } |
| 835 | |
| 836 | kvm_iodevice_init(&p->dev, &ioeventfd_ops); |
| 837 | |
Sasha Levin | 743eeb0 | 2011-07-27 16:00:48 +0300 | [diff] [blame] | 838 | ret = kvm_io_bus_register_dev(kvm, bus_idx, p->addr, p->length, |
| 839 | &p->dev); |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 840 | if (ret < 0) |
| 841 | goto unlock_fail; |
| 842 | |
Michael S. Tsirkin | 68c3b4d | 2014-03-31 21:50:44 +0300 | [diff] [blame] | 843 | /* When length is ignored, MMIO is also put on a separate bus, for |
| 844 | * faster lookups. |
| 845 | */ |
| 846 | if (!args->len && !(args->flags & KVM_IOEVENTFD_FLAG_PIO)) { |
| 847 | ret = kvm_io_bus_register_dev(kvm, KVM_FAST_MMIO_BUS, |
| 848 | p->addr, 0, &p->dev); |
| 849 | if (ret < 0) |
| 850 | goto register_fail; |
| 851 | } |
| 852 | |
Amos Kong | 6ea34c9 | 2013-05-25 06:44:15 +0800 | [diff] [blame] | 853 | kvm->buses[bus_idx]->ioeventfd_count++; |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 854 | list_add_tail(&p->list, &kvm->ioeventfds); |
| 855 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 856 | mutex_unlock(&kvm->slots_lock); |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 857 | |
| 858 | return 0; |
| 859 | |
Michael S. Tsirkin | 68c3b4d | 2014-03-31 21:50:44 +0300 | [diff] [blame] | 860 | register_fail: |
| 861 | kvm_io_bus_unregister_dev(kvm, bus_idx, &p->dev); |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 862 | unlock_fail: |
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 | fail: |
| 866 | kfree(p); |
| 867 | eventfd_ctx_put(eventfd); |
| 868 | |
| 869 | return ret; |
| 870 | } |
| 871 | |
| 872 | static int |
| 873 | kvm_deassign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) |
| 874 | { |
Cornelia Huck | 2b83451 | 2013-02-28 12:33:20 +0100 | [diff] [blame] | 875 | enum kvm_bus bus_idx; |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 876 | struct _ioeventfd *p, *tmp; |
| 877 | struct eventfd_ctx *eventfd; |
| 878 | int ret = -ENOENT; |
| 879 | |
Cornelia Huck | 2b83451 | 2013-02-28 12:33:20 +0100 | [diff] [blame] | 880 | bus_idx = ioeventfd_bus_from_flags(args->flags); |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 881 | eventfd = eventfd_ctx_fdget(args->fd); |
| 882 | if (IS_ERR(eventfd)) |
| 883 | return PTR_ERR(eventfd); |
| 884 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 885 | mutex_lock(&kvm->slots_lock); |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 886 | |
| 887 | list_for_each_entry_safe(p, tmp, &kvm->ioeventfds, list) { |
| 888 | bool wildcard = !(args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH); |
| 889 | |
Michael S. Tsirkin | 05e07f9 | 2013-04-04 13:27:21 +0300 | [diff] [blame] | 890 | if (p->bus_idx != bus_idx || |
| 891 | p->eventfd != eventfd || |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 892 | p->addr != args->addr || |
| 893 | p->length != args->len || |
| 894 | p->wildcard != wildcard) |
| 895 | continue; |
| 896 | |
| 897 | if (!p->wildcard && p->datamatch != args->datamatch) |
| 898 | continue; |
| 899 | |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 900 | kvm_io_bus_unregister_dev(kvm, bus_idx, &p->dev); |
Michael S. Tsirkin | 68c3b4d | 2014-03-31 21:50:44 +0300 | [diff] [blame] | 901 | if (!p->length) { |
| 902 | kvm_io_bus_unregister_dev(kvm, KVM_FAST_MMIO_BUS, |
| 903 | &p->dev); |
| 904 | } |
Amos Kong | 6ea34c9 | 2013-05-25 06:44:15 +0800 | [diff] [blame] | 905 | kvm->buses[bus_idx]->ioeventfd_count--; |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 906 | ioeventfd_release(p); |
| 907 | ret = 0; |
| 908 | break; |
| 909 | } |
| 910 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 911 | mutex_unlock(&kvm->slots_lock); |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 912 | |
| 913 | eventfd_ctx_put(eventfd); |
| 914 | |
| 915 | return ret; |
| 916 | } |
| 917 | |
| 918 | int |
| 919 | kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) |
| 920 | { |
| 921 | if (args->flags & KVM_IOEVENTFD_FLAG_DEASSIGN) |
| 922 | return kvm_deassign_ioeventfd(kvm, args); |
| 923 | |
| 924 | return kvm_assign_ioeventfd(kvm, args); |
| 925 | } |