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