2 * kvm eventfd support - use eventfd objects to signal various KVM events
4 * Copyright 2009 Novell. All Rights Reserved.
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/workqueue.h>
25 #include <linux/syscalls.h>
26 #include <linux/wait.h>
27 #include <linux/poll.h>
28 #include <linux/file.h>
29 #include <linux/list.h>
30 #include <linux/eventfd.h>
33 * --------------------------------------------------------------------
34 * irqfd: Allows an fd to be used to inject an interrupt to the guest
36 * Credit goes to Avi Kivity for the original idea.
37 * --------------------------------------------------------------------
42 struct eventfd_ctx *eventfd;
44 struct list_head list;
46 wait_queue_head_t *wqh;
48 struct work_struct inject;
49 struct work_struct shutdown;
52 static struct workqueue_struct *irqfd_cleanup_wq;
55 irqfd_inject(struct work_struct *work)
57 struct _irqfd *irqfd = container_of(work, struct _irqfd, inject);
58 struct kvm *kvm = irqfd->kvm;
60 mutex_lock(&kvm->lock);
61 kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 1);
62 kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 0);
63 mutex_unlock(&kvm->lock);
67 * Race-free decouple logic (ordering is critical)
70 irqfd_shutdown(struct work_struct *work)
72 struct _irqfd *irqfd = container_of(work, struct _irqfd, shutdown);
75 * Synchronize with the wait-queue and unhook ourselves to prevent
78 remove_wait_queue(irqfd->wqh, &irqfd->wait);
81 * We know no new events will be scheduled at this point, so block
82 * until all previously outstanding events have completed
84 flush_work(&irqfd->inject);
87 * It is now safe to release the object's resources
89 eventfd_ctx_put(irqfd->eventfd);
94 /* assumes kvm->irqfds.lock is held */
96 irqfd_is_active(struct _irqfd *irqfd)
98 return list_empty(&irqfd->list) ? false : true;
102 * Mark the irqfd as inactive and schedule it for removal
104 * assumes kvm->irqfds.lock is held
107 irqfd_deactivate(struct _irqfd *irqfd)
109 BUG_ON(!irqfd_is_active(irqfd));
111 list_del_init(&irqfd->list);
113 queue_work(irqfd_cleanup_wq, &irqfd->shutdown);
117 * Called with wqh->lock held and interrupts disabled
120 irqfd_wakeup(wait_queue_t *wait, unsigned mode, int sync, void *key)
122 struct _irqfd *irqfd = container_of(wait, struct _irqfd, wait);
123 unsigned long flags = (unsigned long)key;
126 /* An event has been signaled, inject an interrupt */
127 schedule_work(&irqfd->inject);
129 if (flags & POLLHUP) {
130 /* The eventfd is closing, detach from KVM */
131 struct kvm *kvm = irqfd->kvm;
134 spin_lock_irqsave(&kvm->irqfds.lock, flags);
137 * We must check if someone deactivated the irqfd before
138 * we could acquire the irqfds.lock since the item is
139 * deactivated from the KVM side before it is unhooked from
140 * the wait-queue. If it is already deactivated, we can
141 * simply return knowing the other side will cleanup for us.
142 * We cannot race against the irqfd going away since the
143 * other side is required to acquire wqh->lock, which we hold
145 if (irqfd_is_active(irqfd))
146 irqfd_deactivate(irqfd);
148 spin_unlock_irqrestore(&kvm->irqfds.lock, flags);
155 irqfd_ptable_queue_proc(struct file *file, wait_queue_head_t *wqh,
158 struct _irqfd *irqfd = container_of(pt, struct _irqfd, pt);
161 add_wait_queue(wqh, &irqfd->wait);
165 kvm_irqfd_assign(struct kvm *kvm, int fd, int gsi)
167 struct _irqfd *irqfd;
168 struct file *file = NULL;
169 struct eventfd_ctx *eventfd = NULL;
173 irqfd = kzalloc(sizeof(*irqfd), GFP_KERNEL);
179 INIT_LIST_HEAD(&irqfd->list);
180 INIT_WORK(&irqfd->inject, irqfd_inject);
181 INIT_WORK(&irqfd->shutdown, irqfd_shutdown);
183 file = eventfd_fget(fd);
189 eventfd = eventfd_ctx_fileget(file);
190 if (IS_ERR(eventfd)) {
191 ret = PTR_ERR(eventfd);
195 irqfd->eventfd = eventfd;
198 * Install our own custom wake-up handling so we are notified via
199 * a callback whenever someone signals the underlying eventfd
201 init_waitqueue_func_entry(&irqfd->wait, irqfd_wakeup);
202 init_poll_funcptr(&irqfd->pt, irqfd_ptable_queue_proc);
204 events = file->f_op->poll(file, &irqfd->pt);
206 spin_lock_irq(&kvm->irqfds.lock);
207 list_add_tail(&irqfd->list, &kvm->irqfds.items);
208 spin_unlock_irq(&kvm->irqfds.lock);
211 * Check if there was an event already pending on the eventfd
212 * before we registered, and trigger it as if we didn't miss it.
215 schedule_work(&irqfd->inject);
218 * do not drop the file until the irqfd is fully initialized, otherwise
219 * we might race against the POLLHUP
226 if (eventfd && !IS_ERR(eventfd))
227 eventfd_ctx_put(eventfd);
229 if (file && !IS_ERR(file))
237 kvm_irqfd_init(struct kvm *kvm)
239 spin_lock_init(&kvm->irqfds.lock);
240 INIT_LIST_HEAD(&kvm->irqfds.items);
244 * shutdown any irqfd's that match fd+gsi
247 kvm_irqfd_deassign(struct kvm *kvm, int fd, int gsi)
249 struct _irqfd *irqfd, *tmp;
250 struct eventfd_ctx *eventfd;
252 eventfd = eventfd_ctx_fdget(fd);
254 return PTR_ERR(eventfd);
256 spin_lock_irq(&kvm->irqfds.lock);
258 list_for_each_entry_safe(irqfd, tmp, &kvm->irqfds.items, list) {
259 if (irqfd->eventfd == eventfd && irqfd->gsi == gsi)
260 irqfd_deactivate(irqfd);
263 spin_unlock_irq(&kvm->irqfds.lock);
264 eventfd_ctx_put(eventfd);
267 * Block until we know all outstanding shutdown jobs have completed
268 * so that we guarantee there will not be any more interrupts on this
269 * gsi once this deassign function returns.
271 flush_workqueue(irqfd_cleanup_wq);
277 kvm_irqfd(struct kvm *kvm, int fd, int gsi, int flags)
279 if (flags & KVM_IRQFD_FLAG_DEASSIGN)
280 return kvm_irqfd_deassign(kvm, fd, gsi);
282 return kvm_irqfd_assign(kvm, fd, gsi);
286 * This function is called as the kvm VM fd is being released. Shutdown all
287 * irqfds that still remain open
290 kvm_irqfd_release(struct kvm *kvm)
292 struct _irqfd *irqfd, *tmp;
294 spin_lock_irq(&kvm->irqfds.lock);
296 list_for_each_entry_safe(irqfd, tmp, &kvm->irqfds.items, list)
297 irqfd_deactivate(irqfd);
299 spin_unlock_irq(&kvm->irqfds.lock);
302 * Block until we know all outstanding shutdown jobs have completed
303 * since we do not take a kvm* reference.
305 flush_workqueue(irqfd_cleanup_wq);
310 * create a host-wide workqueue for issuing deferred shutdown requests
311 * aggregated from all vm* instances. We need our own isolated single-thread
312 * queue to prevent deadlock against flushing the normal work-queue.
314 static int __init irqfd_module_init(void)
316 irqfd_cleanup_wq = create_singlethread_workqueue("kvm-irqfd-cleanup");
317 if (!irqfd_cleanup_wq)
323 static void __exit irqfd_module_exit(void)
325 destroy_workqueue(irqfd_cleanup_wq);
328 module_init(irqfd_module_init);
329 module_exit(irqfd_module_exit);