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 | 5ab6e0e | 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 | 74afc9f | 2014-06-30 20:51:09 +1000 | [diff] [blame] | 36 | #include <linux/seqlock.h> |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 37 | |
| 38 | #include "iodev.h" |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 39 | |
Alexander Graf | a725d56 | 2013-04-17 13:29:30 +0200 | [diff] [blame] | 40 | #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 41 | /* |
| 42 | * -------------------------------------------------------------------- |
| 43 | * irqfd: Allows an fd to be used to inject an interrupt to the guest |
| 44 | * |
| 45 | * Credit goes to Avi Kivity for the original idea. |
| 46 | * -------------------------------------------------------------------- |
| 47 | */ |
| 48 | |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 49 | /* |
| 50 | * Resampling irqfds are a special variety of irqfds used to emulate |
| 51 | * level triggered interrupts. The interrupt is asserted on eventfd |
| 52 | * trigger. On acknowledgement through the irq ack notifier, the |
| 53 | * interrupt is de-asserted and userspace is notified through the |
| 54 | * resamplefd. All resamplers on the same gsi are de-asserted |
| 55 | * together, so we don't need to track the state of each individual |
| 56 | * user. We can also therefore share the same irq source ID. |
| 57 | */ |
| 58 | struct _irqfd_resampler { |
| 59 | struct kvm *kvm; |
| 60 | /* |
| 61 | * List of resampling struct _irqfd objects sharing this gsi. |
| 62 | * RCU list modified under kvm->irqfds.resampler_lock |
| 63 | */ |
| 64 | struct list_head list; |
| 65 | struct kvm_irq_ack_notifier notifier; |
| 66 | /* |
| 67 | * Entry in list of kvm->irqfd.resampler_list. Use for sharing |
| 68 | * resamplers among irqfds on the same gsi. |
| 69 | * Accessed and modified under kvm->irqfds.resampler_lock |
| 70 | */ |
| 71 | struct list_head link; |
| 72 | }; |
| 73 | |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 74 | struct _irqfd { |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 75 | /* Used for MSI fast-path */ |
| 76 | struct kvm *kvm; |
| 77 | wait_queue_t wait; |
| 78 | /* Update side is protected by irqfds.lock */ |
Paul Mackerras | 74afc9f | 2014-06-30 20:51:09 +1000 | [diff] [blame] | 79 | struct kvm_kernel_irq_routing_entry irq_entry; |
| 80 | seqcount_t irq_entry_sc; |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 81 | /* Used for level IRQ fast-path */ |
| 82 | int gsi; |
| 83 | struct work_struct inject; |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 84 | /* The resampler used by this irqfd (resampler-only) */ |
| 85 | struct _irqfd_resampler *resampler; |
| 86 | /* Eventfd notified on resample (resampler-only) */ |
| 87 | struct eventfd_ctx *resamplefd; |
| 88 | /* Entry in list of irqfds for a resampler (resampler-only) */ |
| 89 | struct list_head resampler_link; |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 90 | /* Used for setup/shutdown */ |
| 91 | struct eventfd_ctx *eventfd; |
| 92 | struct list_head list; |
| 93 | poll_table pt; |
| 94 | struct work_struct shutdown; |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | static struct workqueue_struct *irqfd_cleanup_wq; |
| 98 | |
| 99 | static void |
| 100 | irqfd_inject(struct work_struct *work) |
| 101 | { |
| 102 | struct _irqfd *irqfd = container_of(work, struct _irqfd, inject); |
| 103 | struct kvm *kvm = irqfd->kvm; |
| 104 | |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 105 | if (!irqfd->resampler) { |
Yang Zhang | aa2fbe6 | 2013-04-11 19:21:40 +0800 | [diff] [blame] | 106 | kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 1, |
| 107 | false); |
| 108 | kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 0, |
| 109 | false); |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 110 | } else |
| 111 | kvm_set_irq(kvm, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID, |
Yang Zhang | aa2fbe6 | 2013-04-11 19:21:40 +0800 | [diff] [blame] | 112 | irqfd->gsi, 1, false); |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /* |
| 116 | * Since resampler irqfds share an IRQ source ID, we de-assert once |
| 117 | * then notify all of the resampler irqfds using this GSI. We can't |
| 118 | * do multiple de-asserts or we risk racing with incoming re-asserts. |
| 119 | */ |
| 120 | static void |
| 121 | irqfd_resampler_ack(struct kvm_irq_ack_notifier *kian) |
| 122 | { |
| 123 | struct _irqfd_resampler *resampler; |
Christian Borntraeger | 5ab6e0e | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 124 | struct kvm *kvm; |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 125 | struct _irqfd *irqfd; |
Christian Borntraeger | 5ab6e0e | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 126 | int idx; |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 127 | |
| 128 | resampler = container_of(kian, struct _irqfd_resampler, notifier); |
Christian Borntraeger | 5ab6e0e | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 129 | kvm = resampler->kvm; |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 130 | |
Christian Borntraeger | 5ab6e0e | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 131 | kvm_set_irq(kvm, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID, |
Yang Zhang | aa2fbe6 | 2013-04-11 19:21:40 +0800 | [diff] [blame] | 132 | resampler->notifier.gsi, 0, false); |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 133 | |
Christian Borntraeger | 5ab6e0e | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 134 | idx = srcu_read_lock(&kvm->irq_srcu); |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 135 | |
| 136 | list_for_each_entry_rcu(irqfd, &resampler->list, resampler_link) |
| 137 | eventfd_signal(irqfd->resamplefd, 1); |
| 138 | |
Christian Borntraeger | 5ab6e0e | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 139 | srcu_read_unlock(&kvm->irq_srcu, idx); |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static void |
| 143 | irqfd_resampler_shutdown(struct _irqfd *irqfd) |
| 144 | { |
| 145 | struct _irqfd_resampler *resampler = irqfd->resampler; |
| 146 | struct kvm *kvm = resampler->kvm; |
| 147 | |
| 148 | mutex_lock(&kvm->irqfds.resampler_lock); |
| 149 | |
| 150 | list_del_rcu(&irqfd->resampler_link); |
Christian Borntraeger | 5ab6e0e | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 151 | synchronize_srcu(&kvm->irq_srcu); |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 152 | |
| 153 | if (list_empty(&resampler->list)) { |
| 154 | list_del(&resampler->link); |
| 155 | kvm_unregister_irq_ack_notifier(kvm, &resampler->notifier); |
| 156 | kvm_set_irq(kvm, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID, |
Yang Zhang | aa2fbe6 | 2013-04-11 19:21:40 +0800 | [diff] [blame] | 157 | resampler->notifier.gsi, 0, false); |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 158 | kfree(resampler); |
| 159 | } |
| 160 | |
| 161 | mutex_unlock(&kvm->irqfds.resampler_lock); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /* |
| 165 | * Race-free decouple logic (ordering is critical) |
| 166 | */ |
| 167 | static void |
| 168 | irqfd_shutdown(struct work_struct *work) |
| 169 | { |
| 170 | struct _irqfd *irqfd = container_of(work, struct _irqfd, shutdown); |
Michael S. Tsirkin | b6a114d | 2010-01-13 19:12:30 +0200 | [diff] [blame] | 171 | u64 cnt; |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 172 | |
| 173 | /* |
| 174 | * Synchronize with the wait-queue and unhook ourselves to prevent |
| 175 | * further events. |
| 176 | */ |
Michael S. Tsirkin | b6a114d | 2010-01-13 19:12:30 +0200 | [diff] [blame] | 177 | eventfd_ctx_remove_wait_queue(irqfd->eventfd, &irqfd->wait, &cnt); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 178 | |
| 179 | /* |
| 180 | * We know no new events will be scheduled at this point, so block |
| 181 | * until all previously outstanding events have completed |
| 182 | */ |
Tejun Heo | 4382973 | 2012-08-20 14:51:24 -0700 | [diff] [blame] | 183 | flush_work(&irqfd->inject); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 184 | |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 185 | if (irqfd->resampler) { |
| 186 | irqfd_resampler_shutdown(irqfd); |
| 187 | eventfd_ctx_put(irqfd->resamplefd); |
| 188 | } |
| 189 | |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 190 | /* |
| 191 | * It is now safe to release the object's resources |
| 192 | */ |
| 193 | eventfd_ctx_put(irqfd->eventfd); |
| 194 | kfree(irqfd); |
| 195 | } |
| 196 | |
| 197 | |
| 198 | /* assumes kvm->irqfds.lock is held */ |
| 199 | static bool |
| 200 | irqfd_is_active(struct _irqfd *irqfd) |
| 201 | { |
| 202 | return list_empty(&irqfd->list) ? false : true; |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * Mark the irqfd as inactive and schedule it for removal |
| 207 | * |
| 208 | * assumes kvm->irqfds.lock is held |
| 209 | */ |
| 210 | static void |
| 211 | irqfd_deactivate(struct _irqfd *irqfd) |
| 212 | { |
| 213 | BUG_ON(!irqfd_is_active(irqfd)); |
| 214 | |
| 215 | list_del_init(&irqfd->list); |
| 216 | |
| 217 | queue_work(irqfd_cleanup_wq, &irqfd->shutdown); |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | * Called with wqh->lock held and interrupts disabled |
| 222 | */ |
| 223 | static int |
| 224 | irqfd_wakeup(wait_queue_t *wait, unsigned mode, int sync, void *key) |
| 225 | { |
| 226 | struct _irqfd *irqfd = container_of(wait, struct _irqfd, wait); |
| 227 | unsigned long flags = (unsigned long)key; |
Paul Mackerras | 74afc9f | 2014-06-30 20:51:09 +1000 | [diff] [blame] | 228 | struct kvm_kernel_irq_routing_entry irq; |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 229 | struct kvm *kvm = irqfd->kvm; |
Paul Mackerras | 74afc9f | 2014-06-30 20:51:09 +1000 | [diff] [blame] | 230 | unsigned seq; |
Christian Borntraeger | 5ab6e0e | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 231 | int idx; |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 232 | |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 233 | if (flags & POLLIN) { |
Christian Borntraeger | 5ab6e0e | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 234 | idx = srcu_read_lock(&kvm->irq_srcu); |
Paul Mackerras | 74afc9f | 2014-06-30 20:51:09 +1000 | [diff] [blame] | 235 | do { |
| 236 | seq = read_seqcount_begin(&irqfd->irq_entry_sc); |
| 237 | irq = irqfd->irq_entry; |
| 238 | } while (read_seqcount_retry(&irqfd->irq_entry_sc, seq)); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 239 | /* An event has been signaled, inject an interrupt */ |
Paul Mackerras | 74afc9f | 2014-06-30 20:51:09 +1000 | [diff] [blame] | 240 | if (irq.type == KVM_IRQ_ROUTING_MSI) |
| 241 | kvm_set_msi(&irq, kvm, KVM_USERSPACE_IRQ_SOURCE_ID, 1, |
Yang Zhang | aa2fbe6 | 2013-04-11 19:21:40 +0800 | [diff] [blame] | 242 | false); |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 243 | else |
| 244 | schedule_work(&irqfd->inject); |
Christian Borntraeger | 5ab6e0e | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 245 | srcu_read_unlock(&kvm->irq_srcu, idx); |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 246 | } |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 247 | |
| 248 | if (flags & POLLHUP) { |
| 249 | /* The eventfd is closing, detach from KVM */ |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 250 | unsigned long flags; |
| 251 | |
| 252 | spin_lock_irqsave(&kvm->irqfds.lock, flags); |
| 253 | |
| 254 | /* |
| 255 | * We must check if someone deactivated the irqfd before |
| 256 | * we could acquire the irqfds.lock since the item is |
| 257 | * deactivated from the KVM side before it is unhooked from |
| 258 | * the wait-queue. If it is already deactivated, we can |
| 259 | * simply return knowing the other side will cleanup for us. |
| 260 | * We cannot race against the irqfd going away since the |
| 261 | * other side is required to acquire wqh->lock, which we hold |
| 262 | */ |
| 263 | if (irqfd_is_active(irqfd)) |
| 264 | irqfd_deactivate(irqfd); |
| 265 | |
| 266 | spin_unlock_irqrestore(&kvm->irqfds.lock, flags); |
| 267 | } |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | static void |
| 273 | irqfd_ptable_queue_proc(struct file *file, wait_queue_head_t *wqh, |
| 274 | poll_table *pt) |
| 275 | { |
| 276 | struct _irqfd *irqfd = container_of(pt, struct _irqfd, pt); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 277 | add_wait_queue(wqh, &irqfd->wait); |
| 278 | } |
| 279 | |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 280 | /* Must be called under irqfds.lock */ |
| 281 | static void irqfd_update(struct kvm *kvm, struct _irqfd *irqfd, |
| 282 | struct kvm_irq_routing_table *irq_rt) |
| 283 | { |
| 284 | struct kvm_kernel_irq_routing_entry *e; |
Paul Mackerras | 28bcfc2 | 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 | |
| 288 | n_entries = kvm_irq_map_gsi(entries, irq_rt, irqfd->gsi); |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 289 | |
Paul Mackerras | 74afc9f | 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 | 28bcfc2 | 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 | 74afc9f | 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 | 74afc9f | 2014-06-30 20:51:09 +1000 | [diff] [blame] | 300 | |
Paul Mackerras | 74afc9f | 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 | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 307 | struct kvm_irq_routing_table *irq_rt; |
Michael S. Tsirkin | f1d1c30 | 2010-01-13 18:58:09 +0200 | [diff] [blame] | 308 | struct _irqfd *irqfd, *tmp; |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 309 | struct file *file = NULL; |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 310 | struct eventfd_ctx *eventfd = NULL, *resamplefd = NULL; |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 311 | int ret; |
| 312 | unsigned int events; |
| 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 | 74afc9f | 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 | |
Alex Williamson | d4db293 | 2012-06-29 09:56:08 -0600 | [diff] [blame] | 325 | file = eventfd_fget(args->fd); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 326 | if (IS_ERR(file)) { |
| 327 | ret = PTR_ERR(file); |
| 328 | goto fail; |
| 329 | } |
| 330 | |
| 331 | eventfd = eventfd_ctx_fileget(file); |
| 332 | if (IS_ERR(eventfd)) { |
| 333 | ret = PTR_ERR(eventfd); |
| 334 | goto fail; |
| 335 | } |
| 336 | |
| 337 | irqfd->eventfd = eventfd; |
| 338 | |
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 | 5ab6e0e | 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 | |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 406 | irq_rt = rcu_dereference_protected(kvm->irq_routing, |
| 407 | lockdep_is_held(&kvm->irqfds.lock)); |
| 408 | irqfd_update(kvm, irqfd, irq_rt); |
| 409 | |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 410 | events = file->f_op->poll(file, &irqfd->pt); |
| 411 | |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 412 | list_add_tail(&irqfd->list, &kvm->irqfds.items); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 413 | |
| 414 | /* |
| 415 | * Check if there was an event already pending on the eventfd |
| 416 | * before we registered, and trigger it as if we didn't miss it. |
| 417 | */ |
| 418 | if (events & POLLIN) |
| 419 | schedule_work(&irqfd->inject); |
| 420 | |
Michael S. Tsirkin | 6bbfb26 | 2010-09-19 19:02:31 +0200 | [diff] [blame] | 421 | spin_unlock_irq(&kvm->irqfds.lock); |
| 422 | |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 423 | /* |
| 424 | * do not drop the file until the irqfd is fully initialized, otherwise |
| 425 | * we might race against the POLLHUP |
| 426 | */ |
| 427 | fput(file); |
| 428 | |
| 429 | return 0; |
| 430 | |
| 431 | fail: |
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 | |
Julia Lawall | 6223011 | 2009-07-28 17:53:24 +0200 | [diff] [blame] | 441 | if (!IS_ERR(file)) |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 442 | fput(file); |
| 443 | |
| 444 | kfree(irqfd); |
| 445 | return ret; |
| 446 | } |
Alexander Graf | 914daba | 2012-10-09 00:22:59 +0200 | [diff] [blame] | 447 | #endif |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 448 | |
| 449 | void |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 450 | kvm_eventfd_init(struct kvm *kvm) |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 451 | { |
Alexander Graf | a725d56 | 2013-04-17 13:29:30 +0200 | [diff] [blame] | 452 | #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 453 | spin_lock_init(&kvm->irqfds.lock); |
| 454 | INIT_LIST_HEAD(&kvm->irqfds.items); |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 455 | INIT_LIST_HEAD(&kvm->irqfds.resampler_list); |
| 456 | mutex_init(&kvm->irqfds.resampler_lock); |
Alexander Graf | 914daba | 2012-10-09 00:22:59 +0200 | [diff] [blame] | 457 | #endif |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 458 | INIT_LIST_HEAD(&kvm->ioeventfds); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 459 | } |
| 460 | |
Alexander Graf | a725d56 | 2013-04-17 13:29:30 +0200 | [diff] [blame] | 461 | #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 462 | /* |
| 463 | * shutdown any irqfd's that match fd+gsi |
| 464 | */ |
| 465 | static int |
Alex Williamson | d4db293 | 2012-06-29 09:56:08 -0600 | [diff] [blame] | 466 | kvm_irqfd_deassign(struct kvm *kvm, struct kvm_irqfd *args) |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 467 | { |
| 468 | struct _irqfd *irqfd, *tmp; |
| 469 | struct eventfd_ctx *eventfd; |
| 470 | |
Alex Williamson | d4db293 | 2012-06-29 09:56:08 -0600 | [diff] [blame] | 471 | eventfd = eventfd_ctx_fdget(args->fd); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 472 | if (IS_ERR(eventfd)) |
| 473 | return PTR_ERR(eventfd); |
| 474 | |
| 475 | spin_lock_irq(&kvm->irqfds.lock); |
| 476 | |
| 477 | list_for_each_entry_safe(irqfd, tmp, &kvm->irqfds.items, list) { |
Alex Williamson | d4db293 | 2012-06-29 09:56:08 -0600 | [diff] [blame] | 478 | if (irqfd->eventfd == eventfd && irqfd->gsi == args->gsi) { |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 479 | /* |
Paul Mackerras | 74afc9f | 2014-06-30 20:51:09 +1000 | [diff] [blame] | 480 | * This clearing of irq_entry.type is needed for when |
Michael S. Tsirkin | c8ce057 | 2011-03-06 13:03:26 +0200 | [diff] [blame] | 481 | * another thread calls kvm_irq_routing_update before |
| 482 | * we flush workqueue below (we synchronize with |
| 483 | * kvm_irq_routing_update using irqfds.lock). |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 484 | */ |
Paul Mackerras | 74afc9f | 2014-06-30 20:51:09 +1000 | [diff] [blame] | 485 | write_seqcount_begin(&irqfd->irq_entry_sc); |
| 486 | irqfd->irq_entry.type = 0; |
| 487 | write_seqcount_end(&irqfd->irq_entry_sc); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 488 | irqfd_deactivate(irqfd); |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 489 | } |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | spin_unlock_irq(&kvm->irqfds.lock); |
| 493 | eventfd_ctx_put(eventfd); |
| 494 | |
| 495 | /* |
| 496 | * Block until we know all outstanding shutdown jobs have completed |
| 497 | * so that we guarantee there will not be any more interrupts on this |
| 498 | * gsi once this deassign function returns. |
| 499 | */ |
| 500 | flush_workqueue(irqfd_cleanup_wq); |
| 501 | |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | int |
Alex Williamson | d4db293 | 2012-06-29 09:56:08 -0600 | [diff] [blame] | 506 | kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args) |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 507 | { |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 508 | if (args->flags & ~(KVM_IRQFD_FLAG_DEASSIGN | KVM_IRQFD_FLAG_RESAMPLE)) |
Alex Williamson | 326cf03 | 2012-06-29 09:56:24 -0600 | [diff] [blame] | 509 | return -EINVAL; |
| 510 | |
Alex Williamson | d4db293 | 2012-06-29 09:56:08 -0600 | [diff] [blame] | 511 | if (args->flags & KVM_IRQFD_FLAG_DEASSIGN) |
| 512 | return kvm_irqfd_deassign(kvm, args); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 513 | |
Alex Williamson | d4db293 | 2012-06-29 09:56:08 -0600 | [diff] [blame] | 514 | return kvm_irqfd_assign(kvm, args); |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | /* |
| 518 | * This function is called as the kvm VM fd is being released. Shutdown all |
| 519 | * irqfds that still remain open |
| 520 | */ |
| 521 | void |
| 522 | kvm_irqfd_release(struct kvm *kvm) |
| 523 | { |
| 524 | struct _irqfd *irqfd, *tmp; |
| 525 | |
| 526 | spin_lock_irq(&kvm->irqfds.lock); |
| 527 | |
| 528 | list_for_each_entry_safe(irqfd, tmp, &kvm->irqfds.items, list) |
| 529 | irqfd_deactivate(irqfd); |
| 530 | |
| 531 | spin_unlock_irq(&kvm->irqfds.lock); |
| 532 | |
| 533 | /* |
| 534 | * Block until we know all outstanding shutdown jobs have completed |
| 535 | * since we do not take a kvm* reference. |
| 536 | */ |
| 537 | flush_workqueue(irqfd_cleanup_wq); |
| 538 | |
| 539 | } |
| 540 | |
| 541 | /* |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 542 | * Change irq_routing and irqfd. |
Christian Borntraeger | 5ab6e0e | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 543 | * Caller must invoke synchronize_srcu(&kvm->irq_srcu) afterwards. |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 544 | */ |
| 545 | void kvm_irq_routing_update(struct kvm *kvm, |
| 546 | struct kvm_irq_routing_table *irq_rt) |
| 547 | { |
| 548 | struct _irqfd *irqfd; |
| 549 | |
| 550 | spin_lock_irq(&kvm->irqfds.lock); |
| 551 | |
| 552 | rcu_assign_pointer(kvm->irq_routing, irq_rt); |
| 553 | |
| 554 | list_for_each_entry(irqfd, &kvm->irqfds.items, list) |
| 555 | irqfd_update(kvm, irqfd, irq_rt); |
| 556 | |
| 557 | spin_unlock_irq(&kvm->irqfds.lock); |
| 558 | } |
| 559 | |
| 560 | /* |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 561 | * create a host-wide workqueue for issuing deferred shutdown requests |
| 562 | * aggregated from all vm* instances. We need our own isolated single-thread |
| 563 | * queue to prevent deadlock against flushing the normal work-queue. |
| 564 | */ |
Cornelia Huck | a0f155e | 2013-02-28 12:33:18 +0100 | [diff] [blame] | 565 | int kvm_irqfd_init(void) |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 566 | { |
| 567 | irqfd_cleanup_wq = create_singlethread_workqueue("kvm-irqfd-cleanup"); |
| 568 | if (!irqfd_cleanup_wq) |
| 569 | return -ENOMEM; |
| 570 | |
| 571 | return 0; |
| 572 | } |
| 573 | |
Cornelia Huck | a0f155e | 2013-02-28 12:33:18 +0100 | [diff] [blame] | 574 | void kvm_irqfd_exit(void) |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 575 | { |
| 576 | destroy_workqueue(irqfd_cleanup_wq); |
| 577 | } |
Alexander Graf | 914daba | 2012-10-09 00:22:59 +0200 | [diff] [blame] | 578 | #endif |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 579 | |
| 580 | /* |
| 581 | * -------------------------------------------------------------------- |
| 582 | * ioeventfd: translate a PIO/MMIO memory write to an eventfd signal. |
| 583 | * |
| 584 | * userspace can register a PIO/MMIO address with an eventfd for receiving |
| 585 | * notification when the memory has been touched. |
| 586 | * -------------------------------------------------------------------- |
| 587 | */ |
| 588 | |
| 589 | struct _ioeventfd { |
| 590 | struct list_head list; |
| 591 | u64 addr; |
| 592 | int length; |
| 593 | struct eventfd_ctx *eventfd; |
| 594 | u64 datamatch; |
| 595 | struct kvm_io_device dev; |
Michael S. Tsirkin | 05e07f9 | 2013-04-04 13:27:21 +0300 | [diff] [blame] | 596 | u8 bus_idx; |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 597 | bool wildcard; |
| 598 | }; |
| 599 | |
| 600 | static inline struct _ioeventfd * |
| 601 | to_ioeventfd(struct kvm_io_device *dev) |
| 602 | { |
| 603 | return container_of(dev, struct _ioeventfd, dev); |
| 604 | } |
| 605 | |
| 606 | static void |
| 607 | ioeventfd_release(struct _ioeventfd *p) |
| 608 | { |
| 609 | eventfd_ctx_put(p->eventfd); |
| 610 | list_del(&p->list); |
| 611 | kfree(p); |
| 612 | } |
| 613 | |
| 614 | static bool |
| 615 | ioeventfd_in_range(struct _ioeventfd *p, gpa_t addr, int len, const void *val) |
| 616 | { |
| 617 | u64 _val; |
| 618 | |
| 619 | if (!(addr == p->addr && len == p->length)) |
| 620 | /* address-range must be precise for a hit */ |
| 621 | return false; |
| 622 | |
| 623 | if (p->wildcard) |
| 624 | /* all else equal, wildcard is always a hit */ |
| 625 | return true; |
| 626 | |
| 627 | /* otherwise, we have to actually compare the data */ |
| 628 | |
| 629 | BUG_ON(!IS_ALIGNED((unsigned long)val, len)); |
| 630 | |
| 631 | switch (len) { |
| 632 | case 1: |
| 633 | _val = *(u8 *)val; |
| 634 | break; |
| 635 | case 2: |
| 636 | _val = *(u16 *)val; |
| 637 | break; |
| 638 | case 4: |
| 639 | _val = *(u32 *)val; |
| 640 | break; |
| 641 | case 8: |
| 642 | _val = *(u64 *)val; |
| 643 | break; |
| 644 | default: |
| 645 | return false; |
| 646 | } |
| 647 | |
| 648 | return _val == p->datamatch ? true : false; |
| 649 | } |
| 650 | |
| 651 | /* MMIO/PIO writes trigger an event if the addr/val match */ |
| 652 | static int |
| 653 | ioeventfd_write(struct kvm_io_device *this, gpa_t addr, int len, |
| 654 | const void *val) |
| 655 | { |
| 656 | struct _ioeventfd *p = to_ioeventfd(this); |
| 657 | |
| 658 | if (!ioeventfd_in_range(p, addr, len, val)) |
| 659 | return -EOPNOTSUPP; |
| 660 | |
| 661 | eventfd_signal(p->eventfd, 1); |
| 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | /* |
| 666 | * This function is called as KVM is completely shutting down. We do not |
| 667 | * need to worry about locking just nuke anything we have as quickly as possible |
| 668 | */ |
| 669 | static void |
| 670 | ioeventfd_destructor(struct kvm_io_device *this) |
| 671 | { |
| 672 | struct _ioeventfd *p = to_ioeventfd(this); |
| 673 | |
| 674 | ioeventfd_release(p); |
| 675 | } |
| 676 | |
| 677 | static const struct kvm_io_device_ops ioeventfd_ops = { |
| 678 | .write = ioeventfd_write, |
| 679 | .destructor = ioeventfd_destructor, |
| 680 | }; |
| 681 | |
| 682 | /* assumes kvm->slots_lock held */ |
| 683 | static bool |
| 684 | ioeventfd_check_collision(struct kvm *kvm, struct _ioeventfd *p) |
| 685 | { |
| 686 | struct _ioeventfd *_p; |
| 687 | |
| 688 | list_for_each_entry(_p, &kvm->ioeventfds, list) |
Michael S. Tsirkin | 05e07f9 | 2013-04-04 13:27:21 +0300 | [diff] [blame] | 689 | if (_p->bus_idx == p->bus_idx && |
| 690 | _p->addr == p->addr && _p->length == p->length && |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 691 | (_p->wildcard || p->wildcard || |
| 692 | _p->datamatch == p->datamatch)) |
| 693 | return true; |
| 694 | |
| 695 | return false; |
| 696 | } |
| 697 | |
Cornelia Huck | 2b83451 | 2013-02-28 12:33:20 +0100 | [diff] [blame] | 698 | static enum kvm_bus ioeventfd_bus_from_flags(__u32 flags) |
| 699 | { |
| 700 | if (flags & KVM_IOEVENTFD_FLAG_PIO) |
| 701 | return KVM_PIO_BUS; |
| 702 | if (flags & KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY) |
| 703 | return KVM_VIRTIO_CCW_NOTIFY_BUS; |
| 704 | return KVM_MMIO_BUS; |
| 705 | } |
| 706 | |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 707 | static int |
| 708 | kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) |
| 709 | { |
Cornelia Huck | 2b83451 | 2013-02-28 12:33:20 +0100 | [diff] [blame] | 710 | enum kvm_bus bus_idx; |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 711 | struct _ioeventfd *p; |
| 712 | struct eventfd_ctx *eventfd; |
| 713 | int ret; |
| 714 | |
Cornelia Huck | 2b83451 | 2013-02-28 12:33:20 +0100 | [diff] [blame] | 715 | bus_idx = ioeventfd_bus_from_flags(args->flags); |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 716 | /* must be natural-word sized */ |
| 717 | switch (args->len) { |
| 718 | case 1: |
| 719 | case 2: |
| 720 | case 4: |
| 721 | case 8: |
| 722 | break; |
| 723 | default: |
| 724 | return -EINVAL; |
| 725 | } |
| 726 | |
| 727 | /* check for range overflow */ |
| 728 | if (args->addr + args->len < args->addr) |
| 729 | return -EINVAL; |
| 730 | |
| 731 | /* check for extra flags that we don't understand */ |
| 732 | if (args->flags & ~KVM_IOEVENTFD_VALID_FLAG_MASK) |
| 733 | return -EINVAL; |
| 734 | |
| 735 | eventfd = eventfd_ctx_fdget(args->fd); |
| 736 | if (IS_ERR(eventfd)) |
| 737 | return PTR_ERR(eventfd); |
| 738 | |
| 739 | p = kzalloc(sizeof(*p), GFP_KERNEL); |
| 740 | if (!p) { |
| 741 | ret = -ENOMEM; |
| 742 | goto fail; |
| 743 | } |
| 744 | |
| 745 | INIT_LIST_HEAD(&p->list); |
| 746 | p->addr = args->addr; |
Michael S. Tsirkin | 05e07f9 | 2013-04-04 13:27:21 +0300 | [diff] [blame] | 747 | p->bus_idx = bus_idx; |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 748 | p->length = args->len; |
| 749 | p->eventfd = eventfd; |
| 750 | |
| 751 | /* The datamatch feature is optional, otherwise this is a wildcard */ |
| 752 | if (args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH) |
| 753 | p->datamatch = args->datamatch; |
| 754 | else |
| 755 | p->wildcard = true; |
| 756 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 757 | mutex_lock(&kvm->slots_lock); |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 758 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 759 | /* Verify that there isn't a match already */ |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 760 | if (ioeventfd_check_collision(kvm, p)) { |
| 761 | ret = -EEXIST; |
| 762 | goto unlock_fail; |
| 763 | } |
| 764 | |
| 765 | kvm_iodevice_init(&p->dev, &ioeventfd_ops); |
| 766 | |
Sasha Levin | 743eeb0 | 2011-07-27 16:00:48 +0300 | [diff] [blame] | 767 | ret = kvm_io_bus_register_dev(kvm, bus_idx, p->addr, p->length, |
| 768 | &p->dev); |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 769 | if (ret < 0) |
| 770 | goto unlock_fail; |
| 771 | |
| 772 | list_add_tail(&p->list, &kvm->ioeventfds); |
| 773 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 774 | mutex_unlock(&kvm->slots_lock); |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 775 | |
| 776 | return 0; |
| 777 | |
| 778 | unlock_fail: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 779 | mutex_unlock(&kvm->slots_lock); |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 780 | |
| 781 | fail: |
| 782 | kfree(p); |
| 783 | eventfd_ctx_put(eventfd); |
| 784 | |
| 785 | return ret; |
| 786 | } |
| 787 | |
| 788 | static int |
| 789 | kvm_deassign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) |
| 790 | { |
Cornelia Huck | 2b83451 | 2013-02-28 12:33:20 +0100 | [diff] [blame] | 791 | enum kvm_bus bus_idx; |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 792 | struct _ioeventfd *p, *tmp; |
| 793 | struct eventfd_ctx *eventfd; |
| 794 | int ret = -ENOENT; |
| 795 | |
Cornelia Huck | 2b83451 | 2013-02-28 12:33:20 +0100 | [diff] [blame] | 796 | bus_idx = ioeventfd_bus_from_flags(args->flags); |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 797 | eventfd = eventfd_ctx_fdget(args->fd); |
| 798 | if (IS_ERR(eventfd)) |
| 799 | return PTR_ERR(eventfd); |
| 800 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 801 | mutex_lock(&kvm->slots_lock); |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 802 | |
| 803 | list_for_each_entry_safe(p, tmp, &kvm->ioeventfds, list) { |
| 804 | bool wildcard = !(args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH); |
| 805 | |
Michael S. Tsirkin | 05e07f9 | 2013-04-04 13:27:21 +0300 | [diff] [blame] | 806 | if (p->bus_idx != bus_idx || |
| 807 | p->eventfd != eventfd || |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 808 | p->addr != args->addr || |
| 809 | p->length != args->len || |
| 810 | p->wildcard != wildcard) |
| 811 | continue; |
| 812 | |
| 813 | if (!p->wildcard && p->datamatch != args->datamatch) |
| 814 | continue; |
| 815 | |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 816 | kvm_io_bus_unregister_dev(kvm, bus_idx, &p->dev); |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 817 | ioeventfd_release(p); |
| 818 | ret = 0; |
| 819 | break; |
| 820 | } |
| 821 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 822 | mutex_unlock(&kvm->slots_lock); |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 823 | |
| 824 | eventfd_ctx_put(eventfd); |
| 825 | |
| 826 | return ret; |
| 827 | } |
| 828 | |
| 829 | int |
| 830 | kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) |
| 831 | { |
| 832 | if (args->flags & KVM_IOEVENTFD_FLAG_DEASSIGN) |
| 833 | return kvm_deassign_ioeventfd(kvm, args); |
| 834 | |
| 835 | return kvm_assign_ioeventfd(kvm, args); |
| 836 | } |