blob: f397e9b20370a2fb547b04fe555802846b2e9aef [file] [log] [blame]
Gregory Haskins721eecbf2009-05-20 10:30:49 -04001/*
2 * kvm eventfd support - use eventfd objects to signal various KVM events
3 *
4 * Copyright 2009 Novell. All Rights Reserved.
Avi Kivity221d0592010-05-23 18:37:00 +03005 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Gregory Haskins721eecbf2009-05-20 10:30:49 -04006 *
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 Haskinsd34e6b12009-07-07 17:08:49 -040025#include <linux/kvm.h>
Eric Auger166c9772015-09-18 22:29:42 +080026#include <linux/kvm_irqfd.h>
Gregory Haskins721eecbf2009-05-20 10:30:49 -040027#include <linux/workqueue.h>
28#include <linux/syscalls.h>
29#include <linux/wait.h>
30#include <linux/poll.h>
31#include <linux/file.h>
32#include <linux/list.h>
33#include <linux/eventfd.h>
Gregory Haskinsd34e6b12009-07-07 17:08:49 -040034#include <linux/kernel.h>
Christian Borntraeger719d93c2014-01-16 13:44:20 +010035#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Paul Mackerras56f89f362014-06-30 20:51:09 +100037#include <linux/seqlock.h>
Eric Auger9016cfb2015-09-18 22:29:44 +080038#include <linux/irqbypass.h>
Paul Mackerrase4d57e12014-06-30 20:51:12 +100039#include <trace/events/kvm.h>
Gregory Haskinsd34e6b12009-07-07 17:08:49 -040040
Andre Przywaraaf669ac2015-03-26 14:39:29 +000041#include <kvm/iodev.h>
Gregory Haskins721eecbf2009-05-20 10:30:49 -040042
Paul Mackerras297e2102014-06-30 20:51:13 +100043#ifdef CONFIG_HAVE_KVM_IRQFD
Gregory Haskins721eecbf2009-05-20 10:30:49 -040044
Gregory Haskins721eecbf2009-05-20 10:30:49 -040045
46static void
47irqfd_inject(struct work_struct *work)
48{
Eric Auger166c9772015-09-18 22:29:42 +080049 struct kvm_kernel_irqfd *irqfd =
50 container_of(work, struct kvm_kernel_irqfd, inject);
Gregory Haskins721eecbf2009-05-20 10:30:49 -040051 struct kvm *kvm = irqfd->kvm;
52
Alex Williamson7a844282012-09-21 11:58:03 -060053 if (!irqfd->resampler) {
Yang Zhangaa2fbe62013-04-11 19:21:40 +080054 kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 1,
55 false);
56 kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 0,
57 false);
Alex Williamson7a844282012-09-21 11:58:03 -060058 } else
59 kvm_set_irq(kvm, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
Yang Zhangaa2fbe62013-04-11 19:21:40 +080060 irqfd->gsi, 1, false);
Alex Williamson7a844282012-09-21 11:58:03 -060061}
62
63/*
64 * Since resampler irqfds share an IRQ source ID, we de-assert once
65 * then notify all of the resampler irqfds using this GSI. We can't
66 * do multiple de-asserts or we risk racing with incoming re-asserts.
67 */
68static void
69irqfd_resampler_ack(struct kvm_irq_ack_notifier *kian)
70{
Eric Auger166c9772015-09-18 22:29:42 +080071 struct kvm_kernel_irqfd_resampler *resampler;
Christian Borntraeger719d93c2014-01-16 13:44:20 +010072 struct kvm *kvm;
Eric Auger166c9772015-09-18 22:29:42 +080073 struct kvm_kernel_irqfd *irqfd;
Christian Borntraeger719d93c2014-01-16 13:44:20 +010074 int idx;
Alex Williamson7a844282012-09-21 11:58:03 -060075
Eric Auger166c9772015-09-18 22:29:42 +080076 resampler = container_of(kian,
77 struct kvm_kernel_irqfd_resampler, notifier);
Christian Borntraeger719d93c2014-01-16 13:44:20 +010078 kvm = resampler->kvm;
Alex Williamson7a844282012-09-21 11:58:03 -060079
Christian Borntraeger719d93c2014-01-16 13:44:20 +010080 kvm_set_irq(kvm, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
Yang Zhangaa2fbe62013-04-11 19:21:40 +080081 resampler->notifier.gsi, 0, false);
Alex Williamson7a844282012-09-21 11:58:03 -060082
Christian Borntraeger719d93c2014-01-16 13:44:20 +010083 idx = srcu_read_lock(&kvm->irq_srcu);
Alex Williamson7a844282012-09-21 11:58:03 -060084
85 list_for_each_entry_rcu(irqfd, &resampler->list, resampler_link)
86 eventfd_signal(irqfd->resamplefd, 1);
87
Christian Borntraeger719d93c2014-01-16 13:44:20 +010088 srcu_read_unlock(&kvm->irq_srcu, idx);
Alex Williamson7a844282012-09-21 11:58:03 -060089}
90
91static void
Eric Auger166c9772015-09-18 22:29:42 +080092irqfd_resampler_shutdown(struct kvm_kernel_irqfd *irqfd)
Alex Williamson7a844282012-09-21 11:58:03 -060093{
Eric Auger166c9772015-09-18 22:29:42 +080094 struct kvm_kernel_irqfd_resampler *resampler = irqfd->resampler;
Alex Williamson7a844282012-09-21 11:58:03 -060095 struct kvm *kvm = resampler->kvm;
96
97 mutex_lock(&kvm->irqfds.resampler_lock);
98
99 list_del_rcu(&irqfd->resampler_link);
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100100 synchronize_srcu(&kvm->irq_srcu);
Alex Williamson7a844282012-09-21 11:58:03 -0600101
102 if (list_empty(&resampler->list)) {
103 list_del(&resampler->link);
104 kvm_unregister_irq_ack_notifier(kvm, &resampler->notifier);
105 kvm_set_irq(kvm, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
Yang Zhangaa2fbe62013-04-11 19:21:40 +0800106 resampler->notifier.gsi, 0, false);
Alex Williamson7a844282012-09-21 11:58:03 -0600107 kfree(resampler);
108 }
109
110 mutex_unlock(&kvm->irqfds.resampler_lock);
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400111}
112
113/*
114 * Race-free decouple logic (ordering is critical)
115 */
116static void
117irqfd_shutdown(struct work_struct *work)
118{
Eric Auger166c9772015-09-18 22:29:42 +0800119 struct kvm_kernel_irqfd *irqfd =
120 container_of(work, struct kvm_kernel_irqfd, shutdown);
Michael S. Tsirkinb6a114d2010-01-13 19:12:30 +0200121 u64 cnt;
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400122
123 /*
124 * Synchronize with the wait-queue and unhook ourselves to prevent
125 * further events.
126 */
Michael S. Tsirkinb6a114d2010-01-13 19:12:30 +0200127 eventfd_ctx_remove_wait_queue(irqfd->eventfd, &irqfd->wait, &cnt);
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400128
129 /*
130 * We know no new events will be scheduled at this point, so block
131 * until all previously outstanding events have completed
132 */
Tejun Heo43829732012-08-20 14:51:24 -0700133 flush_work(&irqfd->inject);
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400134
Alex Williamson7a844282012-09-21 11:58:03 -0600135 if (irqfd->resampler) {
136 irqfd_resampler_shutdown(irqfd);
137 eventfd_ctx_put(irqfd->resamplefd);
138 }
139
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400140 /*
141 * It is now safe to release the object's resources
142 */
Eric Auger9016cfb2015-09-18 22:29:44 +0800143#ifdef CONFIG_HAVE_KVM_IRQ_BYPASS
144 irq_bypass_unregister_consumer(&irqfd->consumer);
145#endif
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400146 eventfd_ctx_put(irqfd->eventfd);
147 kfree(irqfd);
148}
149
150
151/* assumes kvm->irqfds.lock is held */
152static bool
Eric Auger166c9772015-09-18 22:29:42 +0800153irqfd_is_active(struct kvm_kernel_irqfd *irqfd)
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400154{
155 return list_empty(&irqfd->list) ? false : true;
156}
157
158/*
159 * Mark the irqfd as inactive and schedule it for removal
160 *
161 * assumes kvm->irqfds.lock is held
162 */
163static void
Eric Auger166c9772015-09-18 22:29:42 +0800164irqfd_deactivate(struct kvm_kernel_irqfd *irqfd)
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400165{
166 BUG_ON(!irqfd_is_active(irqfd));
167
168 list_del_init(&irqfd->list);
169
Bhaktipriya Shridhar3706fea2016-08-30 23:29:51 +0530170 schedule_work(&irqfd->shutdown);
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400171}
172
Paolo Bonzinib97e6de2015-10-28 19:16:47 +0100173int __attribute__((weak)) kvm_arch_set_irq_inatomic(
Andrey Smetaninc9a5ecc2015-10-16 10:07:47 +0300174 struct kvm_kernel_irq_routing_entry *irq,
175 struct kvm *kvm, int irq_source_id,
176 int level,
177 bool line_status)
178{
179 return -EWOULDBLOCK;
180}
181
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400182/*
183 * Called with wqh->lock held and interrupts disabled
184 */
185static int
186irqfd_wakeup(wait_queue_t *wait, unsigned mode, int sync, void *key)
187{
Eric Auger166c9772015-09-18 22:29:42 +0800188 struct kvm_kernel_irqfd *irqfd =
189 container_of(wait, struct kvm_kernel_irqfd, wait);
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400190 unsigned long flags = (unsigned long)key;
Paul Mackerras56f89f362014-06-30 20:51:09 +1000191 struct kvm_kernel_irq_routing_entry irq;
Michael S. Tsirkinbd2b53b2010-11-18 19:09:08 +0200192 struct kvm *kvm = irqfd->kvm;
Paul Mackerras56f89f362014-06-30 20:51:09 +1000193 unsigned seq;
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100194 int idx;
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400195
Michael S. Tsirkinbd2b53b2010-11-18 19:09:08 +0200196 if (flags & POLLIN) {
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100197 idx = srcu_read_lock(&kvm->irq_srcu);
Paul Mackerras56f89f362014-06-30 20:51:09 +1000198 do {
199 seq = read_seqcount_begin(&irqfd->irq_entry_sc);
200 irq = irqfd->irq_entry;
201 } while (read_seqcount_retry(&irqfd->irq_entry_sc, seq));
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400202 /* An event has been signaled, inject an interrupt */
Paolo Bonzinib97e6de2015-10-28 19:16:47 +0100203 if (kvm_arch_set_irq_inatomic(&irq, kvm,
204 KVM_USERSPACE_IRQ_SOURCE_ID, 1,
205 false) == -EWOULDBLOCK)
Michael S. Tsirkinbd2b53b2010-11-18 19:09:08 +0200206 schedule_work(&irqfd->inject);
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100207 srcu_read_unlock(&kvm->irq_srcu, idx);
Michael S. Tsirkinbd2b53b2010-11-18 19:09:08 +0200208 }
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400209
210 if (flags & POLLHUP) {
211 /* The eventfd is closing, detach from KVM */
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400212 unsigned long flags;
213
214 spin_lock_irqsave(&kvm->irqfds.lock, flags);
215
216 /*
217 * We must check if someone deactivated the irqfd before
218 * we could acquire the irqfds.lock since the item is
219 * deactivated from the KVM side before it is unhooked from
220 * the wait-queue. If it is already deactivated, we can
221 * simply return knowing the other side will cleanup for us.
222 * We cannot race against the irqfd going away since the
223 * other side is required to acquire wqh->lock, which we hold
224 */
225 if (irqfd_is_active(irqfd))
226 irqfd_deactivate(irqfd);
227
228 spin_unlock_irqrestore(&kvm->irqfds.lock, flags);
229 }
230
231 return 0;
232}
233
234static void
235irqfd_ptable_queue_proc(struct file *file, wait_queue_head_t *wqh,
236 poll_table *pt)
237{
Eric Auger166c9772015-09-18 22:29:42 +0800238 struct kvm_kernel_irqfd *irqfd =
239 container_of(pt, struct kvm_kernel_irqfd, pt);
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400240 add_wait_queue(wqh, &irqfd->wait);
241}
242
Michael S. Tsirkinbd2b53b2010-11-18 19:09:08 +0200243/* Must be called under irqfds.lock */
Eric Auger166c9772015-09-18 22:29:42 +0800244static void irqfd_update(struct kvm *kvm, struct kvm_kernel_irqfd *irqfd)
Michael S. Tsirkinbd2b53b2010-11-18 19:09:08 +0200245{
246 struct kvm_kernel_irq_routing_entry *e;
Paul Mackerras8ba918d2014-06-30 20:51:10 +1000247 struct kvm_kernel_irq_routing_entry entries[KVM_NR_IRQCHIPS];
Andrey Smetanin351dc6472015-10-16 10:07:45 +0300248 int n_entries;
Paul Mackerras8ba918d2014-06-30 20:51:10 +1000249
Paul Mackerras9957c862014-06-30 20:51:11 +1000250 n_entries = kvm_irq_map_gsi(kvm, entries, irqfd->gsi);
Michael S. Tsirkinbd2b53b2010-11-18 19:09:08 +0200251
Paul Mackerras56f89f362014-06-30 20:51:09 +1000252 write_seqcount_begin(&irqfd->irq_entry_sc);
253
Paul Mackerras8ba918d2014-06-30 20:51:10 +1000254 e = entries;
Andrey Smetanin351dc6472015-10-16 10:07:45 +0300255 if (n_entries == 1)
256 irqfd->irq_entry = *e;
257 else
258 irqfd->irq_entry.type = 0;
Paul Mackerras56f89f362014-06-30 20:51:09 +1000259
Paul Mackerras56f89f362014-06-30 20:51:09 +1000260 write_seqcount_end(&irqfd->irq_entry_sc);
Michael S. Tsirkinbd2b53b2010-11-18 19:09:08 +0200261}
262
Eric Auger1a02b272015-09-18 22:29:43 +0800263#ifdef CONFIG_HAVE_KVM_IRQ_BYPASS
264void __attribute__((weak)) kvm_arch_irq_bypass_stop(
265 struct irq_bypass_consumer *cons)
266{
267}
268
269void __attribute__((weak)) kvm_arch_irq_bypass_start(
270 struct irq_bypass_consumer *cons)
271{
272}
Feng Wuf70c20a2015-09-18 22:29:53 +0800273
274int __attribute__((weak)) kvm_arch_update_irqfd_routing(
275 struct kvm *kvm, unsigned int host_irq,
276 uint32_t guest_irq, bool set)
277{
278 return 0;
279}
Eric Auger1a02b272015-09-18 22:29:43 +0800280#endif
281
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400282static int
Alex Williamsond4db2932012-06-29 09:56:08 -0600283kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *args)
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400284{
Eric Auger166c9772015-09-18 22:29:42 +0800285 struct kvm_kernel_irqfd *irqfd, *tmp;
Al Virocffe78d2013-08-30 15:47:17 -0400286 struct fd f;
Alex Williamson7a844282012-09-21 11:58:03 -0600287 struct eventfd_ctx *eventfd = NULL, *resamplefd = NULL;
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400288 int ret;
289 unsigned int events;
Paul Mackerras9957c862014-06-30 20:51:11 +1000290 int idx;
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400291
Eric Auger01c94e62015-03-04 11:14:33 +0100292 if (!kvm_arch_intc_initialized(kvm))
293 return -EAGAIN;
294
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400295 irqfd = kzalloc(sizeof(*irqfd), GFP_KERNEL);
296 if (!irqfd)
297 return -ENOMEM;
298
299 irqfd->kvm = kvm;
Alex Williamsond4db2932012-06-29 09:56:08 -0600300 irqfd->gsi = args->gsi;
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400301 INIT_LIST_HEAD(&irqfd->list);
302 INIT_WORK(&irqfd->inject, irqfd_inject);
303 INIT_WORK(&irqfd->shutdown, irqfd_shutdown);
Paul Mackerras56f89f362014-06-30 20:51:09 +1000304 seqcount_init(&irqfd->irq_entry_sc);
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400305
Al Virocffe78d2013-08-30 15:47:17 -0400306 f = fdget(args->fd);
307 if (!f.file) {
308 ret = -EBADF;
309 goto out;
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400310 }
311
Al Virocffe78d2013-08-30 15:47:17 -0400312 eventfd = eventfd_ctx_fileget(f.file);
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400313 if (IS_ERR(eventfd)) {
314 ret = PTR_ERR(eventfd);
315 goto fail;
316 }
317
318 irqfd->eventfd = eventfd;
319
Alex Williamson7a844282012-09-21 11:58:03 -0600320 if (args->flags & KVM_IRQFD_FLAG_RESAMPLE) {
Eric Auger166c9772015-09-18 22:29:42 +0800321 struct kvm_kernel_irqfd_resampler *resampler;
Alex Williamson7a844282012-09-21 11:58:03 -0600322
323 resamplefd = eventfd_ctx_fdget(args->resamplefd);
324 if (IS_ERR(resamplefd)) {
325 ret = PTR_ERR(resamplefd);
326 goto fail;
327 }
328
329 irqfd->resamplefd = resamplefd;
330 INIT_LIST_HEAD(&irqfd->resampler_link);
331
332 mutex_lock(&kvm->irqfds.resampler_lock);
333
334 list_for_each_entry(resampler,
Alex Williamson49f8a1a2012-12-06 14:44:59 -0700335 &kvm->irqfds.resampler_list, link) {
Alex Williamson7a844282012-09-21 11:58:03 -0600336 if (resampler->notifier.gsi == irqfd->gsi) {
337 irqfd->resampler = resampler;
338 break;
339 }
340 }
341
342 if (!irqfd->resampler) {
343 resampler = kzalloc(sizeof(*resampler), GFP_KERNEL);
344 if (!resampler) {
345 ret = -ENOMEM;
346 mutex_unlock(&kvm->irqfds.resampler_lock);
347 goto fail;
348 }
349
350 resampler->kvm = kvm;
351 INIT_LIST_HEAD(&resampler->list);
352 resampler->notifier.gsi = irqfd->gsi;
353 resampler->notifier.irq_acked = irqfd_resampler_ack;
354 INIT_LIST_HEAD(&resampler->link);
355
356 list_add(&resampler->link, &kvm->irqfds.resampler_list);
357 kvm_register_irq_ack_notifier(kvm,
358 &resampler->notifier);
359 irqfd->resampler = resampler;
360 }
361
362 list_add_rcu(&irqfd->resampler_link, &irqfd->resampler->list);
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100363 synchronize_srcu(&kvm->irq_srcu);
Alex Williamson7a844282012-09-21 11:58:03 -0600364
365 mutex_unlock(&kvm->irqfds.resampler_lock);
366 }
367
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400368 /*
369 * Install our own custom wake-up handling so we are notified via
370 * a callback whenever someone signals the underlying eventfd
371 */
372 init_waitqueue_func_entry(&irqfd->wait, irqfd_wakeup);
373 init_poll_funcptr(&irqfd->pt, irqfd_ptable_queue_proc);
374
Michael S. Tsirkinf1d1c302010-01-13 18:58:09 +0200375 spin_lock_irq(&kvm->irqfds.lock);
376
377 ret = 0;
378 list_for_each_entry(tmp, &kvm->irqfds.items, list) {
379 if (irqfd->eventfd != tmp->eventfd)
380 continue;
381 /* This fd is used for another irq already. */
382 ret = -EBUSY;
383 spin_unlock_irq(&kvm->irqfds.lock);
384 goto fail;
385 }
386
Paul Mackerras9957c862014-06-30 20:51:11 +1000387 idx = srcu_read_lock(&kvm->irq_srcu);
388 irqfd_update(kvm, irqfd);
389 srcu_read_unlock(&kvm->irq_srcu, idx);
Michael S. Tsirkinbd2b53b2010-11-18 19:09:08 +0200390
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400391 list_add_tail(&irqfd->list, &kvm->irqfds.items);
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400392
Cornelia Huck684a0b72014-03-17 19:11:35 +0100393 spin_unlock_irq(&kvm->irqfds.lock);
394
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400395 /*
396 * Check if there was an event already pending on the eventfd
397 * before we registered, and trigger it as if we didn't miss it.
398 */
Cornelia Huck684a0b72014-03-17 19:11:35 +0100399 events = f.file->f_op->poll(f.file, &irqfd->pt);
400
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400401 if (events & POLLIN)
402 schedule_work(&irqfd->inject);
403
404 /*
405 * do not drop the file until the irqfd is fully initialized, otherwise
406 * we might race against the POLLHUP
407 */
Al Virocffe78d2013-08-30 15:47:17 -0400408 fdput(f);
Eric Auger9016cfb2015-09-18 22:29:44 +0800409#ifdef CONFIG_HAVE_KVM_IRQ_BYPASS
Alex Williamson14717e22016-05-05 11:58:35 -0600410 if (kvm_arch_has_irq_bypass()) {
411 irqfd->consumer.token = (void *)irqfd->eventfd;
412 irqfd->consumer.add_producer = kvm_arch_irq_bypass_add_producer;
413 irqfd->consumer.del_producer = kvm_arch_irq_bypass_del_producer;
414 irqfd->consumer.stop = kvm_arch_irq_bypass_stop;
415 irqfd->consumer.start = kvm_arch_irq_bypass_start;
416 ret = irq_bypass_register_consumer(&irqfd->consumer);
417 if (ret)
418 pr_info("irq bypass consumer (token %p) registration fails: %d\n",
Eric Auger9016cfb2015-09-18 22:29:44 +0800419 irqfd->consumer.token, ret);
Alex Williamson14717e22016-05-05 11:58:35 -0600420 }
Eric Auger9016cfb2015-09-18 22:29:44 +0800421#endif
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400422
423 return 0;
424
425fail:
Alex Williamson7a844282012-09-21 11:58:03 -0600426 if (irqfd->resampler)
427 irqfd_resampler_shutdown(irqfd);
428
429 if (resamplefd && !IS_ERR(resamplefd))
430 eventfd_ctx_put(resamplefd);
431
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400432 if (eventfd && !IS_ERR(eventfd))
433 eventfd_ctx_put(eventfd);
434
Al Virocffe78d2013-08-30 15:47:17 -0400435 fdput(f);
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400436
Al Virocffe78d2013-08-30 15:47:17 -0400437out:
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400438 kfree(irqfd);
439 return ret;
440}
Paolo Bonzinic77dcac2014-08-06 14:24:45 +0200441
442bool kvm_irq_has_notifier(struct kvm *kvm, unsigned irqchip, unsigned pin)
443{
444 struct kvm_irq_ack_notifier *kian;
445 int gsi, idx;
446
447 idx = srcu_read_lock(&kvm->irq_srcu);
448 gsi = kvm_irq_map_chip_pin(kvm, irqchip, pin);
449 if (gsi != -1)
450 hlist_for_each_entry_rcu(kian, &kvm->irq_ack_notifier_list,
451 link)
452 if (kian->gsi == gsi) {
453 srcu_read_unlock(&kvm->irq_srcu, idx);
454 return true;
455 }
456
457 srcu_read_unlock(&kvm->irq_srcu, idx);
458
459 return false;
460}
461EXPORT_SYMBOL_GPL(kvm_irq_has_notifier);
462
Andrey Smetaninba1aefc2015-10-16 10:07:46 +0300463void kvm_notify_acked_gsi(struct kvm *kvm, int gsi)
Paolo Bonzinic77dcac2014-08-06 14:24:45 +0200464{
465 struct kvm_irq_ack_notifier *kian;
Andrey Smetaninba1aefc2015-10-16 10:07:46 +0300466
467 hlist_for_each_entry_rcu(kian, &kvm->irq_ack_notifier_list,
468 link)
469 if (kian->gsi == gsi)
470 kian->irq_acked(kian);
471}
472
473void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin)
474{
Paolo Bonzinic77dcac2014-08-06 14:24:45 +0200475 int gsi, idx;
476
477 trace_kvm_ack_irq(irqchip, pin);
478
479 idx = srcu_read_lock(&kvm->irq_srcu);
480 gsi = kvm_irq_map_chip_pin(kvm, irqchip, pin);
481 if (gsi != -1)
Andrey Smetaninba1aefc2015-10-16 10:07:46 +0300482 kvm_notify_acked_gsi(kvm, gsi);
Paolo Bonzinic77dcac2014-08-06 14:24:45 +0200483 srcu_read_unlock(&kvm->irq_srcu, idx);
484}
485
486void 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 Bonzinic77dcac2014-08-06 14:24:45 +0200492 kvm_vcpu_request_scan_ioapic(kvm);
Paolo Bonzinic77dcac2014-08-06 14:24:45 +0200493}
494
495void 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 Bonzinic77dcac2014-08-06 14:24:45 +0200502 kvm_vcpu_request_scan_ioapic(kvm);
Paolo Bonzinic77dcac2014-08-06 14:24:45 +0200503}
Alexander Graf914daba2012-10-09 00:22:59 +0200504#endif
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400505
506void
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400507kvm_eventfd_init(struct kvm *kvm)
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400508{
Paul Mackerras297e2102014-06-30 20:51:13 +1000509#ifdef CONFIG_HAVE_KVM_IRQFD
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400510 spin_lock_init(&kvm->irqfds.lock);
511 INIT_LIST_HEAD(&kvm->irqfds.items);
Alex Williamson7a844282012-09-21 11:58:03 -0600512 INIT_LIST_HEAD(&kvm->irqfds.resampler_list);
513 mutex_init(&kvm->irqfds.resampler_lock);
Alexander Graf914daba2012-10-09 00:22:59 +0200514#endif
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400515 INIT_LIST_HEAD(&kvm->ioeventfds);
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400516}
517
Paul Mackerras297e2102014-06-30 20:51:13 +1000518#ifdef CONFIG_HAVE_KVM_IRQFD
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400519/*
520 * shutdown any irqfd's that match fd+gsi
521 */
522static int
Alex Williamsond4db2932012-06-29 09:56:08 -0600523kvm_irqfd_deassign(struct kvm *kvm, struct kvm_irqfd *args)
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400524{
Eric Auger166c9772015-09-18 22:29:42 +0800525 struct kvm_kernel_irqfd *irqfd, *tmp;
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400526 struct eventfd_ctx *eventfd;
527
Alex Williamsond4db2932012-06-29 09:56:08 -0600528 eventfd = eventfd_ctx_fdget(args->fd);
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400529 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 Williamsond4db2932012-06-29 09:56:08 -0600535 if (irqfd->eventfd == eventfd && irqfd->gsi == args->gsi) {
Michael S. Tsirkinbd2b53b2010-11-18 19:09:08 +0200536 /*
Paul Mackerras56f89f362014-06-30 20:51:09 +1000537 * This clearing of irq_entry.type is needed for when
Michael S. Tsirkinc8ce0572011-03-06 13:03:26 +0200538 * 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. Tsirkinbd2b53b2010-11-18 19:09:08 +0200541 */
Paul Mackerras56f89f362014-06-30 20:51:09 +1000542 write_seqcount_begin(&irqfd->irq_entry_sc);
543 irqfd->irq_entry.type = 0;
544 write_seqcount_end(&irqfd->irq_entry_sc);
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400545 irqfd_deactivate(irqfd);
Michael S. Tsirkinbd2b53b2010-11-18 19:09:08 +0200546 }
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400547 }
548
549 spin_unlock_irq(&kvm->irqfds.lock);
550 eventfd_ctx_put(eventfd);
551
552 /*
553 * Block until we know all outstanding shutdown jobs have completed
554 * so that we guarantee there will not be any more interrupts on this
555 * gsi once this deassign function returns.
556 */
Bhaktipriya Shridhar3706fea2016-08-30 23:29:51 +0530557 flush_work(&irqfd->shutdown);
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400558
559 return 0;
560}
561
562int
Alex Williamsond4db2932012-06-29 09:56:08 -0600563kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args)
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400564{
Alex Williamson7a844282012-09-21 11:58:03 -0600565 if (args->flags & ~(KVM_IRQFD_FLAG_DEASSIGN | KVM_IRQFD_FLAG_RESAMPLE))
Alex Williamson326cf032012-06-29 09:56:24 -0600566 return -EINVAL;
567
Alex Williamsond4db2932012-06-29 09:56:08 -0600568 if (args->flags & KVM_IRQFD_FLAG_DEASSIGN)
569 return kvm_irqfd_deassign(kvm, args);
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400570
Alex Williamsond4db2932012-06-29 09:56:08 -0600571 return kvm_irqfd_assign(kvm, args);
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400572}
573
574/*
575 * This function is called as the kvm VM fd is being released. Shutdown all
576 * irqfds that still remain open
577 */
578void
579kvm_irqfd_release(struct kvm *kvm)
580{
Eric Auger166c9772015-09-18 22:29:42 +0800581 struct kvm_kernel_irqfd *irqfd, *tmp;
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400582
583 spin_lock_irq(&kvm->irqfds.lock);
584
585 list_for_each_entry_safe(irqfd, tmp, &kvm->irqfds.items, list)
586 irqfd_deactivate(irqfd);
587
588 spin_unlock_irq(&kvm->irqfds.lock);
589
590 /*
591 * Block until we know all outstanding shutdown jobs have completed
592 * since we do not take a kvm* reference.
593 */
Bhaktipriya Shridhar3706fea2016-08-30 23:29:51 +0530594 flush_work(&irqfd->shutdown);
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400595
596}
597
598/*
Paul Mackerras9957c862014-06-30 20:51:11 +1000599 * Take note of a change in irq routing.
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100600 * Caller must invoke synchronize_srcu(&kvm->irq_srcu) afterwards.
Michael S. Tsirkinbd2b53b2010-11-18 19:09:08 +0200601 */
Paul Mackerras9957c862014-06-30 20:51:11 +1000602void kvm_irq_routing_update(struct kvm *kvm)
Michael S. Tsirkinbd2b53b2010-11-18 19:09:08 +0200603{
Eric Auger166c9772015-09-18 22:29:42 +0800604 struct kvm_kernel_irqfd *irqfd;
Michael S. Tsirkinbd2b53b2010-11-18 19:09:08 +0200605
606 spin_lock_irq(&kvm->irqfds.lock);
607
Feng Wuf70c20a2015-09-18 22:29:53 +0800608 list_for_each_entry(irqfd, &kvm->irqfds.items, list) {
Paul Mackerras9957c862014-06-30 20:51:11 +1000609 irqfd_update(kvm, irqfd);
Michael S. Tsirkinbd2b53b2010-11-18 19:09:08 +0200610
Feng Wuf70c20a2015-09-18 22:29:53 +0800611#ifdef CONFIG_HAVE_KVM_IRQ_BYPASS
612 if (irqfd->producer) {
613 int ret = kvm_arch_update_irqfd_routing(
614 irqfd->kvm, irqfd->producer->irq,
615 irqfd->gsi, 1);
616 WARN_ON(ret);
617 }
618#endif
619 }
620
Michael S. Tsirkinbd2b53b2010-11-18 19:09:08 +0200621 spin_unlock_irq(&kvm->irqfds.lock);
622}
623
Cornelia Hucka0f155e2013-02-28 12:33:18 +0100624void kvm_irqfd_exit(void)
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400625{
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400626}
Alexander Graf914daba2012-10-09 00:22:59 +0200627#endif
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400628
629/*
630 * --------------------------------------------------------------------
631 * ioeventfd: translate a PIO/MMIO memory write to an eventfd signal.
632 *
633 * userspace can register a PIO/MMIO address with an eventfd for receiving
634 * notification when the memory has been touched.
635 * --------------------------------------------------------------------
636 */
637
638struct _ioeventfd {
639 struct list_head list;
640 u64 addr;
641 int length;
642 struct eventfd_ctx *eventfd;
643 u64 datamatch;
644 struct kvm_io_device dev;
Michael S. Tsirkin05e07f92013-04-04 13:27:21 +0300645 u8 bus_idx;
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400646 bool wildcard;
647};
648
649static inline struct _ioeventfd *
650to_ioeventfd(struct kvm_io_device *dev)
651{
652 return container_of(dev, struct _ioeventfd, dev);
653}
654
655static void
656ioeventfd_release(struct _ioeventfd *p)
657{
658 eventfd_ctx_put(p->eventfd);
659 list_del(&p->list);
660 kfree(p);
661}
662
663static bool
664ioeventfd_in_range(struct _ioeventfd *p, gpa_t addr, int len, const void *val)
665{
666 u64 _val;
667
Michael S. Tsirkinf848a5a2014-03-31 21:50:38 +0300668 if (addr != p->addr)
669 /* address must be precise for a hit */
670 return false;
671
672 if (!p->length)
673 /* length = 0 means only look at the address, so always a hit */
674 return true;
675
676 if (len != p->length)
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400677 /* address-range must be precise for a hit */
678 return false;
679
680 if (p->wildcard)
681 /* all else equal, wildcard is always a hit */
682 return true;
683
684 /* otherwise, we have to actually compare the data */
685
686 BUG_ON(!IS_ALIGNED((unsigned long)val, len));
687
688 switch (len) {
689 case 1:
690 _val = *(u8 *)val;
691 break;
692 case 2:
693 _val = *(u16 *)val;
694 break;
695 case 4:
696 _val = *(u32 *)val;
697 break;
698 case 8:
699 _val = *(u64 *)val;
700 break;
701 default:
702 return false;
703 }
704
705 return _val == p->datamatch ? true : false;
706}
707
708/* MMIO/PIO writes trigger an event if the addr/val match */
709static int
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +0000710ioeventfd_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this, gpa_t addr,
711 int len, const void *val)
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400712{
713 struct _ioeventfd *p = to_ioeventfd(this);
714
715 if (!ioeventfd_in_range(p, addr, len, val))
716 return -EOPNOTSUPP;
717
718 eventfd_signal(p->eventfd, 1);
719 return 0;
720}
721
722/*
723 * This function is called as KVM is completely shutting down. We do not
724 * need to worry about locking just nuke anything we have as quickly as possible
725 */
726static void
727ioeventfd_destructor(struct kvm_io_device *this)
728{
729 struct _ioeventfd *p = to_ioeventfd(this);
730
731 ioeventfd_release(p);
732}
733
734static const struct kvm_io_device_ops ioeventfd_ops = {
735 .write = ioeventfd_write,
736 .destructor = ioeventfd_destructor,
737};
738
739/* assumes kvm->slots_lock held */
740static bool
741ioeventfd_check_collision(struct kvm *kvm, struct _ioeventfd *p)
742{
743 struct _ioeventfd *_p;
744
745 list_for_each_entry(_p, &kvm->ioeventfds, list)
Michael S. Tsirkin05e07f92013-04-04 13:27:21 +0300746 if (_p->bus_idx == p->bus_idx &&
Michael S. Tsirkinf848a5a2014-03-31 21:50:38 +0300747 _p->addr == p->addr &&
748 (!_p->length || !p->length ||
749 (_p->length == p->length &&
750 (_p->wildcard || p->wildcard ||
751 _p->datamatch == p->datamatch))))
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400752 return true;
753
754 return false;
755}
756
Cornelia Huck2b834512013-02-28 12:33:20 +0100757static enum kvm_bus ioeventfd_bus_from_flags(__u32 flags)
758{
759 if (flags & KVM_IOEVENTFD_FLAG_PIO)
760 return KVM_PIO_BUS;
761 if (flags & KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY)
762 return KVM_VIRTIO_CCW_NOTIFY_BUS;
763 return KVM_MMIO_BUS;
764}
765
Jason Wang85da11c2015-09-15 14:41:55 +0800766static int kvm_assign_ioeventfd_idx(struct kvm *kvm,
767 enum kvm_bus bus_idx,
768 struct kvm_ioeventfd *args)
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400769{
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400770
Jason Wang85da11c2015-09-15 14:41:55 +0800771 struct eventfd_ctx *eventfd;
772 struct _ioeventfd *p;
773 int ret;
Michael S. Tsirkinf848a5a2014-03-31 21:50:38 +0300774
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400775 eventfd = eventfd_ctx_fdget(args->fd);
776 if (IS_ERR(eventfd))
777 return PTR_ERR(eventfd);
778
779 p = kzalloc(sizeof(*p), GFP_KERNEL);
780 if (!p) {
781 ret = -ENOMEM;
782 goto fail;
783 }
784
785 INIT_LIST_HEAD(&p->list);
786 p->addr = args->addr;
Michael S. Tsirkin05e07f92013-04-04 13:27:21 +0300787 p->bus_idx = bus_idx;
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400788 p->length = args->len;
789 p->eventfd = eventfd;
790
791 /* The datamatch feature is optional, otherwise this is a wildcard */
792 if (args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH)
793 p->datamatch = args->datamatch;
794 else
795 p->wildcard = true;
796
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200797 mutex_lock(&kvm->slots_lock);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400798
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300799 /* Verify that there isn't a match already */
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400800 if (ioeventfd_check_collision(kvm, p)) {
801 ret = -EEXIST;
802 goto unlock_fail;
803 }
804
805 kvm_iodevice_init(&p->dev, &ioeventfd_ops);
806
Sasha Levin743eeb02011-07-27 16:00:48 +0300807 ret = kvm_io_bus_register_dev(kvm, bus_idx, p->addr, p->length,
808 &p->dev);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400809 if (ret < 0)
810 goto unlock_fail;
811
Amos Kong6ea34c92013-05-25 06:44:15 +0800812 kvm->buses[bus_idx]->ioeventfd_count++;
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400813 list_add_tail(&p->list, &kvm->ioeventfds);
814
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200815 mutex_unlock(&kvm->slots_lock);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400816
817 return 0;
818
819unlock_fail:
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200820 mutex_unlock(&kvm->slots_lock);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400821
822fail:
823 kfree(p);
824 eventfd_ctx_put(eventfd);
825
826 return ret;
827}
828
829static int
Jason Wang85da11c2015-09-15 14:41:55 +0800830kvm_deassign_ioeventfd_idx(struct kvm *kvm, enum kvm_bus bus_idx,
831 struct kvm_ioeventfd *args)
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400832{
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400833 struct _ioeventfd *p, *tmp;
834 struct eventfd_ctx *eventfd;
835 int ret = -ENOENT;
836
837 eventfd = eventfd_ctx_fdget(args->fd);
838 if (IS_ERR(eventfd))
839 return PTR_ERR(eventfd);
840
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200841 mutex_lock(&kvm->slots_lock);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400842
843 list_for_each_entry_safe(p, tmp, &kvm->ioeventfds, list) {
844 bool wildcard = !(args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH);
845
Michael S. Tsirkin05e07f92013-04-04 13:27:21 +0300846 if (p->bus_idx != bus_idx ||
847 p->eventfd != eventfd ||
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400848 p->addr != args->addr ||
849 p->length != args->len ||
850 p->wildcard != wildcard)
851 continue;
852
853 if (!p->wildcard && p->datamatch != args->datamatch)
854 continue;
855
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200856 kvm_io_bus_unregister_dev(kvm, bus_idx, &p->dev);
Amos Kong6ea34c92013-05-25 06:44:15 +0800857 kvm->buses[bus_idx]->ioeventfd_count--;
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400858 ioeventfd_release(p);
859 ret = 0;
860 break;
861 }
862
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200863 mutex_unlock(&kvm->slots_lock);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400864
865 eventfd_ctx_put(eventfd);
866
867 return ret;
868}
869
Jason Wang85da11c2015-09-15 14:41:55 +0800870static int kvm_deassign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
871{
872 enum kvm_bus bus_idx = ioeventfd_bus_from_flags(args->flags);
Jason Wangeefd6b02015-09-15 14:41:56 +0800873 int ret = kvm_deassign_ioeventfd_idx(kvm, bus_idx, args);
Jason Wang85da11c2015-09-15 14:41:55 +0800874
Jason Wangeefd6b02015-09-15 14:41:56 +0800875 if (!args->len && bus_idx == KVM_MMIO_BUS)
876 kvm_deassign_ioeventfd_idx(kvm, KVM_FAST_MMIO_BUS, args);
877
878 return ret;
Jason Wang85da11c2015-09-15 14:41:55 +0800879}
880
881static int
882kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
883{
884 enum kvm_bus bus_idx;
Jason Wangeefd6b02015-09-15 14:41:56 +0800885 int ret;
Jason Wang85da11c2015-09-15 14:41:55 +0800886
887 bus_idx = ioeventfd_bus_from_flags(args->flags);
888 /* must be natural-word sized, or 0 to ignore length */
889 switch (args->len) {
890 case 0:
891 case 1:
892 case 2:
893 case 4:
894 case 8:
895 break;
896 default:
897 return -EINVAL;
898 }
899
900 /* check for range overflow */
901 if (args->addr + args->len < args->addr)
902 return -EINVAL;
903
904 /* check for extra flags that we don't understand */
905 if (args->flags & ~KVM_IOEVENTFD_VALID_FLAG_MASK)
906 return -EINVAL;
907
908 /* ioeventfd with no length can't be combined with DATAMATCH */
Jason Wange9ea5062015-09-15 14:41:59 +0800909 if (!args->len && (args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH))
Jason Wang85da11c2015-09-15 14:41:55 +0800910 return -EINVAL;
911
Jason Wangeefd6b02015-09-15 14:41:56 +0800912 ret = kvm_assign_ioeventfd_idx(kvm, bus_idx, args);
913 if (ret)
914 goto fail;
915
916 /* When length is ignored, MMIO is also put on a separate bus, for
917 * faster lookups.
918 */
919 if (!args->len && bus_idx == KVM_MMIO_BUS) {
920 ret = kvm_assign_ioeventfd_idx(kvm, KVM_FAST_MMIO_BUS, args);
921 if (ret < 0)
922 goto fast_fail;
923 }
924
925 return 0;
926
927fast_fail:
928 kvm_deassign_ioeventfd_idx(kvm, bus_idx, args);
929fail:
930 return ret;
Jason Wang85da11c2015-09-15 14:41:55 +0800931}
932
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400933int
934kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
935{
936 if (args->flags & KVM_IOEVENTFD_FLAG_DEASSIGN)
937 return kvm_deassign_ioeventfd(kvm, args);
938
939 return kvm_assign_ioeventfd(kvm, args);
940}