xfs: remove block number from inode lookup code
[linux/fpc-iii.git] / virt / kvm / eventfd.c
blob251282c43ac87434ffe93ffd43c32389af80b27f
1 /*
2 * kvm eventfd support - use eventfd objects to signal various KVM events
4 * Copyright 2009 Novell. All Rights Reserved.
6 * Author:
7 * Gregory Haskins <ghaskins@novell.com>
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.
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.
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.
23 #include <linux/kvm_host.h>
24 #include <linux/kvm.h>
25 #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>
32 #include <linux/kernel.h>
34 #include "iodev.h"
37 * --------------------------------------------------------------------
38 * irqfd: Allows an fd to be used to inject an interrupt to the guest
40 * Credit goes to Avi Kivity for the original idea.
41 * --------------------------------------------------------------------
44 struct _irqfd {
45 struct kvm *kvm;
46 struct eventfd_ctx *eventfd;
47 int gsi;
48 struct list_head list;
49 poll_table pt;
50 wait_queue_head_t *wqh;
51 wait_queue_t wait;
52 struct work_struct inject;
53 struct work_struct shutdown;
56 static struct workqueue_struct *irqfd_cleanup_wq;
58 static void
59 irqfd_inject(struct work_struct *work)
61 struct _irqfd *irqfd = container_of(work, struct _irqfd, inject);
62 struct kvm *kvm = irqfd->kvm;
64 mutex_lock(&kvm->irq_lock);
65 kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 1);
66 kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 0);
67 mutex_unlock(&kvm->irq_lock);
71 * Race-free decouple logic (ordering is critical)
73 static void
74 irqfd_shutdown(struct work_struct *work)
76 struct _irqfd *irqfd = container_of(work, struct _irqfd, shutdown);
79 * Synchronize with the wait-queue and unhook ourselves to prevent
80 * further events.
82 remove_wait_queue(irqfd->wqh, &irqfd->wait);
85 * We know no new events will be scheduled at this point, so block
86 * until all previously outstanding events have completed
88 flush_work(&irqfd->inject);
91 * It is now safe to release the object's resources
93 eventfd_ctx_put(irqfd->eventfd);
94 kfree(irqfd);
98 /* assumes kvm->irqfds.lock is held */
99 static bool
100 irqfd_is_active(struct _irqfd *irqfd)
102 return list_empty(&irqfd->list) ? false : true;
106 * Mark the irqfd as inactive and schedule it for removal
108 * assumes kvm->irqfds.lock is held
110 static void
111 irqfd_deactivate(struct _irqfd *irqfd)
113 BUG_ON(!irqfd_is_active(irqfd));
115 list_del_init(&irqfd->list);
117 queue_work(irqfd_cleanup_wq, &irqfd->shutdown);
121 * Called with wqh->lock held and interrupts disabled
123 static int
124 irqfd_wakeup(wait_queue_t *wait, unsigned mode, int sync, void *key)
126 struct _irqfd *irqfd = container_of(wait, struct _irqfd, wait);
127 unsigned long flags = (unsigned long)key;
129 if (flags & POLLIN)
130 /* An event has been signaled, inject an interrupt */
131 schedule_work(&irqfd->inject);
133 if (flags & POLLHUP) {
134 /* The eventfd is closing, detach from KVM */
135 struct kvm *kvm = irqfd->kvm;
136 unsigned long flags;
138 spin_lock_irqsave(&kvm->irqfds.lock, flags);
141 * We must check if someone deactivated the irqfd before
142 * we could acquire the irqfds.lock since the item is
143 * deactivated from the KVM side before it is unhooked from
144 * the wait-queue. If it is already deactivated, we can
145 * simply return knowing the other side will cleanup for us.
146 * We cannot race against the irqfd going away since the
147 * other side is required to acquire wqh->lock, which we hold
149 if (irqfd_is_active(irqfd))
150 irqfd_deactivate(irqfd);
152 spin_unlock_irqrestore(&kvm->irqfds.lock, flags);
155 return 0;
158 static void
159 irqfd_ptable_queue_proc(struct file *file, wait_queue_head_t *wqh,
160 poll_table *pt)
162 struct _irqfd *irqfd = container_of(pt, struct _irqfd, pt);
164 irqfd->wqh = wqh;
165 add_wait_queue(wqh, &irqfd->wait);
168 static int
169 kvm_irqfd_assign(struct kvm *kvm, int fd, int gsi)
171 struct _irqfd *irqfd, *tmp;
172 struct file *file = NULL;
173 struct eventfd_ctx *eventfd = NULL;
174 int ret;
175 unsigned int events;
177 irqfd = kzalloc(sizeof(*irqfd), GFP_KERNEL);
178 if (!irqfd)
179 return -ENOMEM;
181 irqfd->kvm = kvm;
182 irqfd->gsi = gsi;
183 INIT_LIST_HEAD(&irqfd->list);
184 INIT_WORK(&irqfd->inject, irqfd_inject);
185 INIT_WORK(&irqfd->shutdown, irqfd_shutdown);
187 file = eventfd_fget(fd);
188 if (IS_ERR(file)) {
189 ret = PTR_ERR(file);
190 goto fail;
193 eventfd = eventfd_ctx_fileget(file);
194 if (IS_ERR(eventfd)) {
195 ret = PTR_ERR(eventfd);
196 goto fail;
199 irqfd->eventfd = eventfd;
202 * Install our own custom wake-up handling so we are notified via
203 * a callback whenever someone signals the underlying eventfd
205 init_waitqueue_func_entry(&irqfd->wait, irqfd_wakeup);
206 init_poll_funcptr(&irqfd->pt, irqfd_ptable_queue_proc);
208 spin_lock_irq(&kvm->irqfds.lock);
210 ret = 0;
211 list_for_each_entry(tmp, &kvm->irqfds.items, list) {
212 if (irqfd->eventfd != tmp->eventfd)
213 continue;
214 /* This fd is used for another irq already. */
215 ret = -EBUSY;
216 spin_unlock_irq(&kvm->irqfds.lock);
217 goto fail;
220 events = file->f_op->poll(file, &irqfd->pt);
222 list_add_tail(&irqfd->list, &kvm->irqfds.items);
223 spin_unlock_irq(&kvm->irqfds.lock);
226 * Check if there was an event already pending on the eventfd
227 * before we registered, and trigger it as if we didn't miss it.
229 if (events & POLLIN)
230 schedule_work(&irqfd->inject);
233 * do not drop the file until the irqfd is fully initialized, otherwise
234 * we might race against the POLLHUP
236 fput(file);
238 return 0;
240 fail:
241 if (eventfd && !IS_ERR(eventfd))
242 eventfd_ctx_put(eventfd);
244 if (!IS_ERR(file))
245 fput(file);
247 kfree(irqfd);
248 return ret;
251 void
252 kvm_eventfd_init(struct kvm *kvm)
254 spin_lock_init(&kvm->irqfds.lock);
255 INIT_LIST_HEAD(&kvm->irqfds.items);
256 INIT_LIST_HEAD(&kvm->ioeventfds);
260 * shutdown any irqfd's that match fd+gsi
262 static int
263 kvm_irqfd_deassign(struct kvm *kvm, int fd, int gsi)
265 struct _irqfd *irqfd, *tmp;
266 struct eventfd_ctx *eventfd;
268 eventfd = eventfd_ctx_fdget(fd);
269 if (IS_ERR(eventfd))
270 return PTR_ERR(eventfd);
272 spin_lock_irq(&kvm->irqfds.lock);
274 list_for_each_entry_safe(irqfd, tmp, &kvm->irqfds.items, list) {
275 if (irqfd->eventfd == eventfd && irqfd->gsi == gsi)
276 irqfd_deactivate(irqfd);
279 spin_unlock_irq(&kvm->irqfds.lock);
280 eventfd_ctx_put(eventfd);
283 * Block until we know all outstanding shutdown jobs have completed
284 * so that we guarantee there will not be any more interrupts on this
285 * gsi once this deassign function returns.
287 flush_workqueue(irqfd_cleanup_wq);
289 return 0;
293 kvm_irqfd(struct kvm *kvm, int fd, int gsi, int flags)
295 if (flags & KVM_IRQFD_FLAG_DEASSIGN)
296 return kvm_irqfd_deassign(kvm, fd, gsi);
298 return kvm_irqfd_assign(kvm, fd, gsi);
302 * This function is called as the kvm VM fd is being released. Shutdown all
303 * irqfds that still remain open
305 void
306 kvm_irqfd_release(struct kvm *kvm)
308 struct _irqfd *irqfd, *tmp;
310 spin_lock_irq(&kvm->irqfds.lock);
312 list_for_each_entry_safe(irqfd, tmp, &kvm->irqfds.items, list)
313 irqfd_deactivate(irqfd);
315 spin_unlock_irq(&kvm->irqfds.lock);
318 * Block until we know all outstanding shutdown jobs have completed
319 * since we do not take a kvm* reference.
321 flush_workqueue(irqfd_cleanup_wq);
326 * create a host-wide workqueue for issuing deferred shutdown requests
327 * aggregated from all vm* instances. We need our own isolated single-thread
328 * queue to prevent deadlock against flushing the normal work-queue.
330 static int __init irqfd_module_init(void)
332 irqfd_cleanup_wq = create_singlethread_workqueue("kvm-irqfd-cleanup");
333 if (!irqfd_cleanup_wq)
334 return -ENOMEM;
336 return 0;
339 static void __exit irqfd_module_exit(void)
341 destroy_workqueue(irqfd_cleanup_wq);
344 module_init(irqfd_module_init);
345 module_exit(irqfd_module_exit);
348 * --------------------------------------------------------------------
349 * ioeventfd: translate a PIO/MMIO memory write to an eventfd signal.
351 * userspace can register a PIO/MMIO address with an eventfd for receiving
352 * notification when the memory has been touched.
353 * --------------------------------------------------------------------
356 struct _ioeventfd {
357 struct list_head list;
358 u64 addr;
359 int length;
360 struct eventfd_ctx *eventfd;
361 u64 datamatch;
362 struct kvm_io_device dev;
363 bool wildcard;
366 static inline struct _ioeventfd *
367 to_ioeventfd(struct kvm_io_device *dev)
369 return container_of(dev, struct _ioeventfd, dev);
372 static void
373 ioeventfd_release(struct _ioeventfd *p)
375 eventfd_ctx_put(p->eventfd);
376 list_del(&p->list);
377 kfree(p);
380 static bool
381 ioeventfd_in_range(struct _ioeventfd *p, gpa_t addr, int len, const void *val)
383 u64 _val;
385 if (!(addr == p->addr && len == p->length))
386 /* address-range must be precise for a hit */
387 return false;
389 if (p->wildcard)
390 /* all else equal, wildcard is always a hit */
391 return true;
393 /* otherwise, we have to actually compare the data */
395 BUG_ON(!IS_ALIGNED((unsigned long)val, len));
397 switch (len) {
398 case 1:
399 _val = *(u8 *)val;
400 break;
401 case 2:
402 _val = *(u16 *)val;
403 break;
404 case 4:
405 _val = *(u32 *)val;
406 break;
407 case 8:
408 _val = *(u64 *)val;
409 break;
410 default:
411 return false;
414 return _val == p->datamatch ? true : false;
417 /* MMIO/PIO writes trigger an event if the addr/val match */
418 static int
419 ioeventfd_write(struct kvm_io_device *this, gpa_t addr, int len,
420 const void *val)
422 struct _ioeventfd *p = to_ioeventfd(this);
424 if (!ioeventfd_in_range(p, addr, len, val))
425 return -EOPNOTSUPP;
427 eventfd_signal(p->eventfd, 1);
428 return 0;
432 * This function is called as KVM is completely shutting down. We do not
433 * need to worry about locking just nuke anything we have as quickly as possible
435 static void
436 ioeventfd_destructor(struct kvm_io_device *this)
438 struct _ioeventfd *p = to_ioeventfd(this);
440 ioeventfd_release(p);
443 static const struct kvm_io_device_ops ioeventfd_ops = {
444 .write = ioeventfd_write,
445 .destructor = ioeventfd_destructor,
448 /* assumes kvm->slots_lock held */
449 static bool
450 ioeventfd_check_collision(struct kvm *kvm, struct _ioeventfd *p)
452 struct _ioeventfd *_p;
454 list_for_each_entry(_p, &kvm->ioeventfds, list)
455 if (_p->addr == p->addr && _p->length == p->length &&
456 (_p->wildcard || p->wildcard ||
457 _p->datamatch == p->datamatch))
458 return true;
460 return false;
463 static int
464 kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
466 int pio = args->flags & KVM_IOEVENTFD_FLAG_PIO;
467 struct kvm_io_bus *bus = pio ? &kvm->pio_bus : &kvm->mmio_bus;
468 struct _ioeventfd *p;
469 struct eventfd_ctx *eventfd;
470 int ret;
472 /* must be natural-word sized */
473 switch (args->len) {
474 case 1:
475 case 2:
476 case 4:
477 case 8:
478 break;
479 default:
480 return -EINVAL;
483 /* check for range overflow */
484 if (args->addr + args->len < args->addr)
485 return -EINVAL;
487 /* check for extra flags that we don't understand */
488 if (args->flags & ~KVM_IOEVENTFD_VALID_FLAG_MASK)
489 return -EINVAL;
491 eventfd = eventfd_ctx_fdget(args->fd);
492 if (IS_ERR(eventfd))
493 return PTR_ERR(eventfd);
495 p = kzalloc(sizeof(*p), GFP_KERNEL);
496 if (!p) {
497 ret = -ENOMEM;
498 goto fail;
501 INIT_LIST_HEAD(&p->list);
502 p->addr = args->addr;
503 p->length = args->len;
504 p->eventfd = eventfd;
506 /* The datamatch feature is optional, otherwise this is a wildcard */
507 if (args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH)
508 p->datamatch = args->datamatch;
509 else
510 p->wildcard = true;
512 down_write(&kvm->slots_lock);
514 /* Verify that there isnt a match already */
515 if (ioeventfd_check_collision(kvm, p)) {
516 ret = -EEXIST;
517 goto unlock_fail;
520 kvm_iodevice_init(&p->dev, &ioeventfd_ops);
522 ret = __kvm_io_bus_register_dev(bus, &p->dev);
523 if (ret < 0)
524 goto unlock_fail;
526 list_add_tail(&p->list, &kvm->ioeventfds);
528 up_write(&kvm->slots_lock);
530 return 0;
532 unlock_fail:
533 up_write(&kvm->slots_lock);
535 fail:
536 kfree(p);
537 eventfd_ctx_put(eventfd);
539 return ret;
542 static int
543 kvm_deassign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
545 int pio = args->flags & KVM_IOEVENTFD_FLAG_PIO;
546 struct kvm_io_bus *bus = pio ? &kvm->pio_bus : &kvm->mmio_bus;
547 struct _ioeventfd *p, *tmp;
548 struct eventfd_ctx *eventfd;
549 int ret = -ENOENT;
551 eventfd = eventfd_ctx_fdget(args->fd);
552 if (IS_ERR(eventfd))
553 return PTR_ERR(eventfd);
555 down_write(&kvm->slots_lock);
557 list_for_each_entry_safe(p, tmp, &kvm->ioeventfds, list) {
558 bool wildcard = !(args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH);
560 if (p->eventfd != eventfd ||
561 p->addr != args->addr ||
562 p->length != args->len ||
563 p->wildcard != wildcard)
564 continue;
566 if (!p->wildcard && p->datamatch != args->datamatch)
567 continue;
569 __kvm_io_bus_unregister_dev(bus, &p->dev);
570 ioeventfd_release(p);
571 ret = 0;
572 break;
575 up_write(&kvm->slots_lock);
577 eventfd_ctx_put(eventfd);
579 return ret;
583 kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
585 if (args->flags & KVM_IOEVENTFD_FLAG_DEASSIGN)
586 return kvm_deassign_ioeventfd(kvm, args);
588 return kvm_assign_ioeventfd(kvm, args);