blob: 7016319b1ec0590513ba1886ede37982c39e71b4 [file] [log] [blame]
Gregory Haskins721eecb2009-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.
5 *
6 * Author:
7 * Gregory Haskins <ghaskins@novell.com>
8 *
9 * This file is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License
11 * as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22
23#include <linux/kvm_host.h>
Gregory Haskinsd34e6b12009-07-07 17:08:49 -040024#include <linux/kvm.h>
Gregory Haskins721eecb2009-05-20 10:30:49 -040025#include <linux/workqueue.h>
26#include <linux/syscalls.h>
27#include <linux/wait.h>
28#include <linux/poll.h>
29#include <linux/file.h>
30#include <linux/list.h>
31#include <linux/eventfd.h>
Gregory Haskinsd34e6b12009-07-07 17:08:49 -040032#include <linux/kernel.h>
33
34#include "iodev.h"
Gregory Haskins721eecb2009-05-20 10:30:49 -040035
36/*
37 * --------------------------------------------------------------------
38 * irqfd: Allows an fd to be used to inject an interrupt to the guest
39 *
40 * Credit goes to Avi Kivity for the original idea.
41 * --------------------------------------------------------------------
42 */
43
44struct _irqfd {
45 struct kvm *kvm;
46 struct eventfd_ctx *eventfd;
47 int gsi;
48 struct list_head list;
49 poll_table pt;
Gregory Haskins721eecb2009-05-20 10:30:49 -040050 wait_queue_t wait;
51 struct work_struct inject;
52 struct work_struct shutdown;
53};
54
55static struct workqueue_struct *irqfd_cleanup_wq;
56
57static void
58irqfd_inject(struct work_struct *work)
59{
60 struct _irqfd *irqfd = container_of(work, struct _irqfd, inject);
61 struct kvm *kvm = irqfd->kvm;
62
Gregory Haskins721eecb2009-05-20 10:30:49 -040063 kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 1);
64 kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 0);
Gregory Haskins721eecb2009-05-20 10:30:49 -040065}
66
67/*
68 * Race-free decouple logic (ordering is critical)
69 */
70static void
71irqfd_shutdown(struct work_struct *work)
72{
73 struct _irqfd *irqfd = container_of(work, struct _irqfd, shutdown);
Michael S. Tsirkinb6a114d2010-01-13 19:12:30 +020074 u64 cnt;
Gregory Haskins721eecb2009-05-20 10:30:49 -040075
76 /*
77 * Synchronize with the wait-queue and unhook ourselves to prevent
78 * further events.
79 */
Michael S. Tsirkinb6a114d2010-01-13 19:12:30 +020080 eventfd_ctx_remove_wait_queue(irqfd->eventfd, &irqfd->wait, &cnt);
Gregory Haskins721eecb2009-05-20 10:30:49 -040081
82 /*
83 * We know no new events will be scheduled at this point, so block
84 * until all previously outstanding events have completed
85 */
86 flush_work(&irqfd->inject);
87
88 /*
89 * It is now safe to release the object's resources
90 */
91 eventfd_ctx_put(irqfd->eventfd);
92 kfree(irqfd);
93}
94
95
96/* assumes kvm->irqfds.lock is held */
97static bool
98irqfd_is_active(struct _irqfd *irqfd)
99{
100 return list_empty(&irqfd->list) ? false : true;
101}
102
103/*
104 * Mark the irqfd as inactive and schedule it for removal
105 *
106 * assumes kvm->irqfds.lock is held
107 */
108static void
109irqfd_deactivate(struct _irqfd *irqfd)
110{
111 BUG_ON(!irqfd_is_active(irqfd));
112
113 list_del_init(&irqfd->list);
114
115 queue_work(irqfd_cleanup_wq, &irqfd->shutdown);
116}
117
118/*
119 * Called with wqh->lock held and interrupts disabled
120 */
121static int
122irqfd_wakeup(wait_queue_t *wait, unsigned mode, int sync, void *key)
123{
124 struct _irqfd *irqfd = container_of(wait, struct _irqfd, wait);
125 unsigned long flags = (unsigned long)key;
126
127 if (flags & POLLIN)
128 /* An event has been signaled, inject an interrupt */
129 schedule_work(&irqfd->inject);
130
131 if (flags & POLLHUP) {
132 /* The eventfd is closing, detach from KVM */
133 struct kvm *kvm = irqfd->kvm;
134 unsigned long flags;
135
136 spin_lock_irqsave(&kvm->irqfds.lock, flags);
137
138 /*
139 * We must check if someone deactivated the irqfd before
140 * we could acquire the irqfds.lock since the item is
141 * deactivated from the KVM side before it is unhooked from
142 * the wait-queue. If it is already deactivated, we can
143 * simply return knowing the other side will cleanup for us.
144 * We cannot race against the irqfd going away since the
145 * other side is required to acquire wqh->lock, which we hold
146 */
147 if (irqfd_is_active(irqfd))
148 irqfd_deactivate(irqfd);
149
150 spin_unlock_irqrestore(&kvm->irqfds.lock, flags);
151 }
152
153 return 0;
154}
155
156static void
157irqfd_ptable_queue_proc(struct file *file, wait_queue_head_t *wqh,
158 poll_table *pt)
159{
160 struct _irqfd *irqfd = container_of(pt, struct _irqfd, pt);
Gregory Haskins721eecb2009-05-20 10:30:49 -0400161 add_wait_queue(wqh, &irqfd->wait);
162}
163
164static int
165kvm_irqfd_assign(struct kvm *kvm, int fd, int gsi)
166{
Michael S. Tsirkinf1d1c302010-01-13 18:58:09 +0200167 struct _irqfd *irqfd, *tmp;
Gregory Haskins721eecb2009-05-20 10:30:49 -0400168 struct file *file = NULL;
169 struct eventfd_ctx *eventfd = NULL;
170 int ret;
171 unsigned int events;
172
173 irqfd = kzalloc(sizeof(*irqfd), GFP_KERNEL);
174 if (!irqfd)
175 return -ENOMEM;
176
177 irqfd->kvm = kvm;
178 irqfd->gsi = gsi;
179 INIT_LIST_HEAD(&irqfd->list);
180 INIT_WORK(&irqfd->inject, irqfd_inject);
181 INIT_WORK(&irqfd->shutdown, irqfd_shutdown);
182
183 file = eventfd_fget(fd);
184 if (IS_ERR(file)) {
185 ret = PTR_ERR(file);
186 goto fail;
187 }
188
189 eventfd = eventfd_ctx_fileget(file);
190 if (IS_ERR(eventfd)) {
191 ret = PTR_ERR(eventfd);
192 goto fail;
193 }
194
195 irqfd->eventfd = eventfd;
196
197 /*
198 * Install our own custom wake-up handling so we are notified via
199 * a callback whenever someone signals the underlying eventfd
200 */
201 init_waitqueue_func_entry(&irqfd->wait, irqfd_wakeup);
202 init_poll_funcptr(&irqfd->pt, irqfd_ptable_queue_proc);
203
Michael S. Tsirkinf1d1c302010-01-13 18:58:09 +0200204 spin_lock_irq(&kvm->irqfds.lock);
205
206 ret = 0;
207 list_for_each_entry(tmp, &kvm->irqfds.items, list) {
208 if (irqfd->eventfd != tmp->eventfd)
209 continue;
210 /* This fd is used for another irq already. */
211 ret = -EBUSY;
212 spin_unlock_irq(&kvm->irqfds.lock);
213 goto fail;
214 }
215
Gregory Haskins721eecb2009-05-20 10:30:49 -0400216 events = file->f_op->poll(file, &irqfd->pt);
217
Gregory Haskins721eecb2009-05-20 10:30:49 -0400218 list_add_tail(&irqfd->list, &kvm->irqfds.items);
219 spin_unlock_irq(&kvm->irqfds.lock);
220
221 /*
222 * Check if there was an event already pending on the eventfd
223 * before we registered, and trigger it as if we didn't miss it.
224 */
225 if (events & POLLIN)
226 schedule_work(&irqfd->inject);
227
228 /*
229 * do not drop the file until the irqfd is fully initialized, otherwise
230 * we might race against the POLLHUP
231 */
232 fput(file);
233
234 return 0;
235
236fail:
237 if (eventfd && !IS_ERR(eventfd))
238 eventfd_ctx_put(eventfd);
239
Julia Lawall62230112009-07-28 17:53:24 +0200240 if (!IS_ERR(file))
Gregory Haskins721eecb2009-05-20 10:30:49 -0400241 fput(file);
242
243 kfree(irqfd);
244 return ret;
245}
246
247void
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400248kvm_eventfd_init(struct kvm *kvm)
Gregory Haskins721eecb2009-05-20 10:30:49 -0400249{
250 spin_lock_init(&kvm->irqfds.lock);
251 INIT_LIST_HEAD(&kvm->irqfds.items);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400252 INIT_LIST_HEAD(&kvm->ioeventfds);
Gregory Haskins721eecb2009-05-20 10:30:49 -0400253}
254
255/*
256 * shutdown any irqfd's that match fd+gsi
257 */
258static int
259kvm_irqfd_deassign(struct kvm *kvm, int fd, int gsi)
260{
261 struct _irqfd *irqfd, *tmp;
262 struct eventfd_ctx *eventfd;
263
264 eventfd = eventfd_ctx_fdget(fd);
265 if (IS_ERR(eventfd))
266 return PTR_ERR(eventfd);
267
268 spin_lock_irq(&kvm->irqfds.lock);
269
270 list_for_each_entry_safe(irqfd, tmp, &kvm->irqfds.items, list) {
271 if (irqfd->eventfd == eventfd && irqfd->gsi == gsi)
272 irqfd_deactivate(irqfd);
273 }
274
275 spin_unlock_irq(&kvm->irqfds.lock);
276 eventfd_ctx_put(eventfd);
277
278 /*
279 * Block until we know all outstanding shutdown jobs have completed
280 * so that we guarantee there will not be any more interrupts on this
281 * gsi once this deassign function returns.
282 */
283 flush_workqueue(irqfd_cleanup_wq);
284
285 return 0;
286}
287
288int
289kvm_irqfd(struct kvm *kvm, int fd, int gsi, int flags)
290{
291 if (flags & KVM_IRQFD_FLAG_DEASSIGN)
292 return kvm_irqfd_deassign(kvm, fd, gsi);
293
294 return kvm_irqfd_assign(kvm, fd, gsi);
295}
296
297/*
298 * This function is called as the kvm VM fd is being released. Shutdown all
299 * irqfds that still remain open
300 */
301void
302kvm_irqfd_release(struct kvm *kvm)
303{
304 struct _irqfd *irqfd, *tmp;
305
306 spin_lock_irq(&kvm->irqfds.lock);
307
308 list_for_each_entry_safe(irqfd, tmp, &kvm->irqfds.items, list)
309 irqfd_deactivate(irqfd);
310
311 spin_unlock_irq(&kvm->irqfds.lock);
312
313 /*
314 * Block until we know all outstanding shutdown jobs have completed
315 * since we do not take a kvm* reference.
316 */
317 flush_workqueue(irqfd_cleanup_wq);
318
319}
320
321/*
322 * create a host-wide workqueue for issuing deferred shutdown requests
323 * aggregated from all vm* instances. We need our own isolated single-thread
324 * queue to prevent deadlock against flushing the normal work-queue.
325 */
326static int __init irqfd_module_init(void)
327{
328 irqfd_cleanup_wq = create_singlethread_workqueue("kvm-irqfd-cleanup");
329 if (!irqfd_cleanup_wq)
330 return -ENOMEM;
331
332 return 0;
333}
334
335static void __exit irqfd_module_exit(void)
336{
337 destroy_workqueue(irqfd_cleanup_wq);
338}
339
340module_init(irqfd_module_init);
341module_exit(irqfd_module_exit);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400342
343/*
344 * --------------------------------------------------------------------
345 * ioeventfd: translate a PIO/MMIO memory write to an eventfd signal.
346 *
347 * userspace can register a PIO/MMIO address with an eventfd for receiving
348 * notification when the memory has been touched.
349 * --------------------------------------------------------------------
350 */
351
352struct _ioeventfd {
353 struct list_head list;
354 u64 addr;
355 int length;
356 struct eventfd_ctx *eventfd;
357 u64 datamatch;
358 struct kvm_io_device dev;
359 bool wildcard;
360};
361
362static inline struct _ioeventfd *
363to_ioeventfd(struct kvm_io_device *dev)
364{
365 return container_of(dev, struct _ioeventfd, dev);
366}
367
368static void
369ioeventfd_release(struct _ioeventfd *p)
370{
371 eventfd_ctx_put(p->eventfd);
372 list_del(&p->list);
373 kfree(p);
374}
375
376static bool
377ioeventfd_in_range(struct _ioeventfd *p, gpa_t addr, int len, const void *val)
378{
379 u64 _val;
380
381 if (!(addr == p->addr && len == p->length))
382 /* address-range must be precise for a hit */
383 return false;
384
385 if (p->wildcard)
386 /* all else equal, wildcard is always a hit */
387 return true;
388
389 /* otherwise, we have to actually compare the data */
390
391 BUG_ON(!IS_ALIGNED((unsigned long)val, len));
392
393 switch (len) {
394 case 1:
395 _val = *(u8 *)val;
396 break;
397 case 2:
398 _val = *(u16 *)val;
399 break;
400 case 4:
401 _val = *(u32 *)val;
402 break;
403 case 8:
404 _val = *(u64 *)val;
405 break;
406 default:
407 return false;
408 }
409
410 return _val == p->datamatch ? true : false;
411}
412
413/* MMIO/PIO writes trigger an event if the addr/val match */
414static int
415ioeventfd_write(struct kvm_io_device *this, gpa_t addr, int len,
416 const void *val)
417{
418 struct _ioeventfd *p = to_ioeventfd(this);
419
420 if (!ioeventfd_in_range(p, addr, len, val))
421 return -EOPNOTSUPP;
422
423 eventfd_signal(p->eventfd, 1);
424 return 0;
425}
426
427/*
428 * This function is called as KVM is completely shutting down. We do not
429 * need to worry about locking just nuke anything we have as quickly as possible
430 */
431static void
432ioeventfd_destructor(struct kvm_io_device *this)
433{
434 struct _ioeventfd *p = to_ioeventfd(this);
435
436 ioeventfd_release(p);
437}
438
439static const struct kvm_io_device_ops ioeventfd_ops = {
440 .write = ioeventfd_write,
441 .destructor = ioeventfd_destructor,
442};
443
444/* assumes kvm->slots_lock held */
445static bool
446ioeventfd_check_collision(struct kvm *kvm, struct _ioeventfd *p)
447{
448 struct _ioeventfd *_p;
449
450 list_for_each_entry(_p, &kvm->ioeventfds, list)
451 if (_p->addr == p->addr && _p->length == p->length &&
452 (_p->wildcard || p->wildcard ||
453 _p->datamatch == p->datamatch))
454 return true;
455
456 return false;
457}
458
459static int
460kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
461{
462 int pio = args->flags & KVM_IOEVENTFD_FLAG_PIO;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200463 enum kvm_bus bus_idx = pio ? KVM_PIO_BUS : KVM_MMIO_BUS;
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400464 struct _ioeventfd *p;
465 struct eventfd_ctx *eventfd;
466 int ret;
467
468 /* must be natural-word sized */
469 switch (args->len) {
470 case 1:
471 case 2:
472 case 4:
473 case 8:
474 break;
475 default:
476 return -EINVAL;
477 }
478
479 /* check for range overflow */
480 if (args->addr + args->len < args->addr)
481 return -EINVAL;
482
483 /* check for extra flags that we don't understand */
484 if (args->flags & ~KVM_IOEVENTFD_VALID_FLAG_MASK)
485 return -EINVAL;
486
487 eventfd = eventfd_ctx_fdget(args->fd);
488 if (IS_ERR(eventfd))
489 return PTR_ERR(eventfd);
490
491 p = kzalloc(sizeof(*p), GFP_KERNEL);
492 if (!p) {
493 ret = -ENOMEM;
494 goto fail;
495 }
496
497 INIT_LIST_HEAD(&p->list);
498 p->addr = args->addr;
499 p->length = args->len;
500 p->eventfd = eventfd;
501
502 /* The datamatch feature is optional, otherwise this is a wildcard */
503 if (args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH)
504 p->datamatch = args->datamatch;
505 else
506 p->wildcard = true;
507
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200508 mutex_lock(&kvm->slots_lock);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400509
510 /* Verify that there isnt a match already */
511 if (ioeventfd_check_collision(kvm, p)) {
512 ret = -EEXIST;
513 goto unlock_fail;
514 }
515
516 kvm_iodevice_init(&p->dev, &ioeventfd_ops);
517
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200518 ret = kvm_io_bus_register_dev(kvm, bus_idx, &p->dev);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400519 if (ret < 0)
520 goto unlock_fail;
521
522 list_add_tail(&p->list, &kvm->ioeventfds);
523
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200524 mutex_unlock(&kvm->slots_lock);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400525
526 return 0;
527
528unlock_fail:
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200529 mutex_unlock(&kvm->slots_lock);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400530
531fail:
532 kfree(p);
533 eventfd_ctx_put(eventfd);
534
535 return ret;
536}
537
538static int
539kvm_deassign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
540{
541 int pio = args->flags & KVM_IOEVENTFD_FLAG_PIO;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200542 enum kvm_bus bus_idx = pio ? KVM_PIO_BUS : KVM_MMIO_BUS;
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400543 struct _ioeventfd *p, *tmp;
544 struct eventfd_ctx *eventfd;
545 int ret = -ENOENT;
546
547 eventfd = eventfd_ctx_fdget(args->fd);
548 if (IS_ERR(eventfd))
549 return PTR_ERR(eventfd);
550
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200551 mutex_lock(&kvm->slots_lock);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400552
553 list_for_each_entry_safe(p, tmp, &kvm->ioeventfds, list) {
554 bool wildcard = !(args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH);
555
556 if (p->eventfd != eventfd ||
557 p->addr != args->addr ||
558 p->length != args->len ||
559 p->wildcard != wildcard)
560 continue;
561
562 if (!p->wildcard && p->datamatch != args->datamatch)
563 continue;
564
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200565 kvm_io_bus_unregister_dev(kvm, bus_idx, &p->dev);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400566 ioeventfd_release(p);
567 ret = 0;
568 break;
569 }
570
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200571 mutex_unlock(&kvm->slots_lock);
Gregory Haskinsd34e6b12009-07-07 17:08:49 -0400572
573 eventfd_ctx_put(eventfd);
574
575 return ret;
576}
577
578int
579kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
580{
581 if (args->flags & KVM_IOEVENTFD_FLAG_DEASSIGN)
582 return kvm_deassign_ioeventfd(kvm, args);
583
584 return kvm_assign_ioeventfd(kvm, args);
585}