aboutsummaryrefslogtreecommitdiffstats
path: root/virt
diff options
context:
space:
mode:
authorGregory Haskins <ghaskins@novell.com>2009-05-20 10:30:49 -0400
committerAvi Kivity <avi@redhat.com>2009-09-10 01:32:41 -0400
commit721eecbf4fe995ca94a9edec0c9843b1cc0eaaf3 (patch)
treec759fe98a4ea5e2e2a7694e4b44a558407077290 /virt
parent0ba12d10817a8db1fd7d96d3283ec6c0b294aeab (diff)
KVM: irqfd
KVM provides a complete virtual system environment for guests, including support for injecting interrupts modeled after the real exception/interrupt facilities present on the native platform (such as the IDT on x86). Virtual interrupts can come from a variety of sources (emulated devices, pass-through devices, etc) but all must be injected to the guest via the KVM infrastructure. This patch adds a new mechanism to inject a specific interrupt to a guest using a decoupled eventfd mechnanism: Any legal signal on the irqfd (using eventfd semantics from either userspace or kernel) will translate into an injected interrupt in the guest at the next available interrupt window. Signed-off-by: Gregory Haskins <ghaskins@novell.com> Signed-off-by: Avi Kivity <avi@redhat.com>
Diffstat (limited to 'virt')
-rw-r--r--virt/kvm/Kconfig4
-rw-r--r--virt/kvm/eventfd.c329
-rw-r--r--virt/kvm/kvm_main.c12
3 files changed, 345 insertions, 0 deletions
diff --git a/virt/kvm/Kconfig b/virt/kvm/Kconfig
index 79e83fc00dc7..56c6848d2df8 100644
--- a/virt/kvm/Kconfig
+++ b/virt/kvm/Kconfig
@@ -5,3 +5,7 @@ config HAVE_KVM
5 5
6config HAVE_KVM_IRQCHIP 6config HAVE_KVM_IRQCHIP
7 bool 7 bool
8
9config HAVE_KVM_EVENTFD
10 bool
11 select EVENTFD
diff --git a/virt/kvm/eventfd.c b/virt/kvm/eventfd.c
new file mode 100644
index 000000000000..314012323afe
--- /dev/null
+++ b/virt/kvm/eventfd.c
@@ -0,0 +1,329 @@
1/*
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>
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>
31
32/*
33 * --------------------------------------------------------------------
34 * irqfd: Allows an fd to be used to inject an interrupt to the guest
35 *
36 * Credit goes to Avi Kivity for the original idea.
37 * --------------------------------------------------------------------
38 */
39
40struct _irqfd {
41 struct kvm *kvm;
42 struct eventfd_ctx *eventfd;
43 int gsi;
44 struct list_head list;
45 poll_table pt;
46 wait_queue_head_t *wqh;
47 wait_queue_t wait;
48 struct work_struct inject;
49 struct work_struct shutdown;
50};
51
52static struct workqueue_struct *irqfd_cleanup_wq;
53
54static void
55irqfd_inject(struct work_struct *work)
56{
57 struct _irqfd *irqfd = container_of(work, struct _irqfd, inject);
58 struct kvm *kvm = irqfd->kvm;
59
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);
64}
65
66/*
67 * Race-free decouple logic (ordering is critical)
68 */
69static void
70irqfd_shutdown(struct work_struct *work)
71{
72 struct _irqfd *irqfd = container_of(work, struct _irqfd, shutdown);
73
74 /*
75 * Synchronize with the wait-queue and unhook ourselves to prevent
76 * further events.
77 */
78 remove_wait_queue(irqfd->wqh, &irqfd->wait);
79
80 /*
81 * We know no new events will be scheduled at this point, so block
82 * until all previously outstanding events have completed
83 */
84 flush_work(&irqfd->inject);
85
86 /*
87 * It is now safe to release the object's resources
88 */
89 eventfd_ctx_put(irqfd->eventfd);
90 kfree(irqfd);
91}
92
93
94/* assumes kvm->irqfds.lock is held */
95static bool
96irqfd_is_active(struct _irqfd *irqfd)
97{
98 return list_empty(&irqfd->list) ? false : true;
99}
100
101/*
102 * Mark the irqfd as inactive and schedule it for removal
103 *
104 * assumes kvm->irqfds.lock is held
105 */
106static void
107irqfd_deactivate(struct _irqfd *irqfd)
108{
109 BUG_ON(!irqfd_is_active(irqfd));
110
111 list_del_init(&irqfd->list);
112
113 queue_work(irqfd_cleanup_wq, &irqfd->shutdown);
114}
115
116/*
117 * Called with wqh->lock held and interrupts disabled
118 */
119static int
120irqfd_wakeup(wait_queue_t *wait, unsigned mode, int sync, void *key)
121{
122 struct _irqfd *irqfd = container_of(wait, struct _irqfd, wait);
123 unsigned long flags = (unsigned long)key;
124
125 if (flags & POLLIN)
126 /* An event has been signaled, inject an interrupt */
127 schedule_work(&irqfd->inject);
128
129 if (flags & POLLHUP) {
130 /* The eventfd is closing, detach from KVM */
131 struct kvm *kvm = irqfd->kvm;
132 unsigned long flags;
133
134 spin_lock_irqsave(&kvm->irqfds.lock, flags);
135
136 /*
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
144 */
145 if (irqfd_is_active(irqfd))
146 irqfd_deactivate(irqfd);
147
148 spin_unlock_irqrestore(&kvm->irqfds.lock, flags);
149 }
150
151 return 0;
152}
153
154static void
155irqfd_ptable_queue_proc(struct file *file, wait_queue_head_t *wqh,
156 poll_table *pt)
157{
158 struct _irqfd *irqfd = container_of(pt, struct _irqfd, pt);
159
160 irqfd->wqh = wqh;
161 add_wait_queue(wqh, &irqfd->wait);
162}
163
164static int
165kvm_irqfd_assign(struct kvm *kvm, int fd, int gsi)
166{
167 struct _irqfd *irqfd;
168 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
204 events = file->f_op->poll(file, &irqfd->pt);
205
206 spin_lock_irq(&kvm->irqfds.lock);
207 list_add_tail(&irqfd->list, &kvm->irqfds.items);
208 spin_unlock_irq(&kvm->irqfds.lock);
209
210 /*
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.
213 */
214 if (events & POLLIN)
215 schedule_work(&irqfd->inject);
216
217 /*
218 * do not drop the file until the irqfd is fully initialized, otherwise
219 * we might race against the POLLHUP
220 */
221 fput(file);
222
223 return 0;
224
225fail:
226 if (eventfd && !IS_ERR(eventfd))
227 eventfd_ctx_put(eventfd);
228
229 if (file && !IS_ERR(file))
230 fput(file);
231
232 kfree(irqfd);
233 return ret;
234}
235
236void
237kvm_irqfd_init(struct kvm *kvm)
238{
239 spin_lock_init(&kvm->irqfds.lock);
240 INIT_LIST_HEAD(&kvm->irqfds.items);
241}
242
243/*
244 * shutdown any irqfd's that match fd+gsi
245 */
246static int
247kvm_irqfd_deassign(struct kvm *kvm, int fd, int gsi)
248{
249 struct _irqfd *irqfd, *tmp;
250 struct eventfd_ctx *eventfd;
251
252 eventfd = eventfd_ctx_fdget(fd);
253 if (IS_ERR(eventfd))
254 return PTR_ERR(eventfd);
255
256 spin_lock_irq(&kvm->irqfds.lock);
257
258 list_for_each_entry_safe(irqfd, tmp, &kvm->irqfds.items, list) {
259 if (irqfd->eventfd == eventfd && irqfd->gsi == gsi)
260 irqfd_deactivate(irqfd);
261 }
262
263 spin_unlock_irq(&kvm->irqfds.lock);
264 eventfd_ctx_put(eventfd);
265
266 /*
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.
270 */
271 flush_workqueue(irqfd_cleanup_wq);
272
273 return 0;
274}
275
276int
277kvm_irqfd(struct kvm *kvm, int fd, int gsi, int flags)
278{
279 if (flags & KVM_IRQFD_FLAG_DEASSIGN)
280 return kvm_irqfd_deassign(kvm, fd, gsi);
281
282 return kvm_irqfd_assign(kvm, fd, gsi);
283}
284
285/*
286 * This function is called as the kvm VM fd is being released. Shutdown all
287 * irqfds that still remain open
288 */
289void
290kvm_irqfd_release(struct kvm *kvm)
291{
292 struct _irqfd *irqfd, *tmp;
293
294 spin_lock_irq(&kvm->irqfds.lock);
295
296 list_for_each_entry_safe(irqfd, tmp, &kvm->irqfds.items, list)
297 irqfd_deactivate(irqfd);
298
299 spin_unlock_irq(&kvm->irqfds.lock);
300
301 /*
302 * Block until we know all outstanding shutdown jobs have completed
303 * since we do not take a kvm* reference.
304 */
305 flush_workqueue(irqfd_cleanup_wq);
306
307}
308
309/*
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.
313 */
314static int __init irqfd_module_init(void)
315{
316 irqfd_cleanup_wq = create_singlethread_workqueue("kvm-irqfd-cleanup");
317 if (!irqfd_cleanup_wq)
318 return -ENOMEM;
319
320 return 0;
321}
322
323static void __exit irqfd_module_exit(void)
324{
325 destroy_workqueue(irqfd_cleanup_wq);
326}
327
328module_init(irqfd_module_init);
329module_exit(irqfd_module_exit);
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 2884baf1d5f9..dee321e58894 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -986,6 +986,7 @@ static struct kvm *kvm_create_vm(void)
986 spin_lock_init(&kvm->mmu_lock); 986 spin_lock_init(&kvm->mmu_lock);
987 spin_lock_init(&kvm->requests_lock); 987 spin_lock_init(&kvm->requests_lock);
988 kvm_io_bus_init(&kvm->pio_bus); 988 kvm_io_bus_init(&kvm->pio_bus);
989 kvm_irqfd_init(kvm);
989 mutex_init(&kvm->lock); 990 mutex_init(&kvm->lock);
990 kvm_io_bus_init(&kvm->mmio_bus); 991 kvm_io_bus_init(&kvm->mmio_bus);
991 init_rwsem(&kvm->slots_lock); 992 init_rwsem(&kvm->slots_lock);
@@ -1071,6 +1072,8 @@ static int kvm_vm_release(struct inode *inode, struct file *filp)
1071{ 1072{
1072 struct kvm *kvm = filp->private_data; 1073 struct kvm *kvm = filp->private_data;
1073 1074
1075 kvm_irqfd_release(kvm);
1076
1074 kvm_put_kvm(kvm); 1077 kvm_put_kvm(kvm);
1075 return 0; 1078 return 0;
1076} 1079}
@@ -2222,6 +2225,15 @@ static long kvm_vm_ioctl(struct file *filp,
2222 } 2225 }
2223#endif 2226#endif
2224#endif /* KVM_CAP_IRQ_ROUTING */ 2227#endif /* KVM_CAP_IRQ_ROUTING */
2228 case KVM_IRQFD: {
2229 struct kvm_irqfd data;
2230
2231 r = -EFAULT;
2232 if (copy_from_user(&data, argp, sizeof data))
2233 goto out;
2234 r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
2235 break;
2236 }
2225 default: 2237 default:
2226 r = kvm_arch_vm_ioctl(filp, ioctl, arg); 2238 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
2227 } 2239 }