xref: /linux/drivers/vfio/virqfd.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
27e992d69SAntonios Motakis /*
37e992d69SAntonios Motakis  * VFIO generic eventfd code for IRQFD support.
47e992d69SAntonios Motakis  * Derived from drivers/vfio/pci/vfio_pci_intrs.c
57e992d69SAntonios Motakis  *
67e992d69SAntonios Motakis  * Copyright (C) 2012 Red Hat, Inc.  All rights reserved.
77e992d69SAntonios Motakis  *     Author: Alex Williamson <alex.williamson@redhat.com>
87e992d69SAntonios Motakis  */
97e992d69SAntonios Motakis 
107e992d69SAntonios Motakis #include <linux/vfio.h>
117e992d69SAntonios Motakis #include <linux/eventfd.h>
127e992d69SAntonios Motakis #include <linux/file.h>
1371be3423SAlex Williamson #include <linux/module.h>
147e992d69SAntonios Motakis #include <linux/slab.h>
15e2d55709SJason Gunthorpe #include "vfio.h"
1671be3423SAlex Williamson 
177e992d69SAntonios Motakis static struct workqueue_struct *vfio_irqfd_cleanup_wq;
1866fdc052Skbuild test robot static DEFINE_SPINLOCK(virqfd_lock);
197e992d69SAntonios Motakis 
vfio_virqfd_init(void)20e2d55709SJason Gunthorpe int __init vfio_virqfd_init(void)
217e992d69SAntonios Motakis {
227e992d69SAntonios Motakis 	vfio_irqfd_cleanup_wq =
237e992d69SAntonios Motakis 		create_singlethread_workqueue("vfio-irqfd-cleanup");
247e992d69SAntonios Motakis 	if (!vfio_irqfd_cleanup_wq)
257e992d69SAntonios Motakis 		return -ENOMEM;
267e992d69SAntonios Motakis 
277e992d69SAntonios Motakis 	return 0;
287e992d69SAntonios Motakis }
297e992d69SAntonios Motakis 
vfio_virqfd_exit(void)30e2d55709SJason Gunthorpe void vfio_virqfd_exit(void)
317e992d69SAntonios Motakis {
327e992d69SAntonios Motakis 	destroy_workqueue(vfio_irqfd_cleanup_wq);
337e992d69SAntonios Motakis }
347e992d69SAntonios Motakis 
virqfd_deactivate(struct virqfd * virqfd)357e992d69SAntonios Motakis static void virqfd_deactivate(struct virqfd *virqfd)
367e992d69SAntonios Motakis {
377e992d69SAntonios Motakis 	queue_work(vfio_irqfd_cleanup_wq, &virqfd->shutdown);
387e992d69SAntonios Motakis }
397e992d69SAntonios Motakis 
virqfd_wakeup(wait_queue_entry_t * wait,unsigned mode,int sync,void * key)40ac6424b9SIngo Molnar static int virqfd_wakeup(wait_queue_entry_t *wait, unsigned mode, int sync, void *key)
417e992d69SAntonios Motakis {
427e992d69SAntonios Motakis 	struct virqfd *virqfd = container_of(wait, struct virqfd, wait);
433ad6f93eSAl Viro 	__poll_t flags = key_to_poll(key);
447e992d69SAntonios Motakis 
45a9a08845SLinus Torvalds 	if (flags & EPOLLIN) {
46b1b397aeSDavid Woodhouse 		u64 cnt;
47b1b397aeSDavid Woodhouse 		eventfd_ctx_do_read(virqfd->eventfd, &cnt);
48b1b397aeSDavid Woodhouse 
497e992d69SAntonios Motakis 		/* An event has been signaled, call function */
507e992d69SAntonios Motakis 		if ((!virqfd->handler ||
517e992d69SAntonios Motakis 		     virqfd->handler(virqfd->opaque, virqfd->data)) &&
527e992d69SAntonios Motakis 		    virqfd->thread)
537e992d69SAntonios Motakis 			schedule_work(&virqfd->inject);
547e992d69SAntonios Motakis 	}
557e992d69SAntonios Motakis 
56a9a08845SLinus Torvalds 	if (flags & EPOLLHUP) {
577e992d69SAntonios Motakis 		unsigned long flags;
587e992d69SAntonios Motakis 		spin_lock_irqsave(&virqfd_lock, flags);
597e992d69SAntonios Motakis 
607e992d69SAntonios Motakis 		/*
617e992d69SAntonios Motakis 		 * The eventfd is closing, if the virqfd has not yet been
627e992d69SAntonios Motakis 		 * queued for release, as determined by testing whether the
637e992d69SAntonios Motakis 		 * virqfd pointer to it is still valid, queue it now.  As
647e992d69SAntonios Motakis 		 * with kvm irqfds, we know we won't race against the virqfd
657e992d69SAntonios Motakis 		 * going away because we hold the lock to get here.
667e992d69SAntonios Motakis 		 */
677e992d69SAntonios Motakis 		if (*(virqfd->pvirqfd) == virqfd) {
687e992d69SAntonios Motakis 			*(virqfd->pvirqfd) = NULL;
697e992d69SAntonios Motakis 			virqfd_deactivate(virqfd);
707e992d69SAntonios Motakis 		}
717e992d69SAntonios Motakis 
727e992d69SAntonios Motakis 		spin_unlock_irqrestore(&virqfd_lock, flags);
737e992d69SAntonios Motakis 	}
747e992d69SAntonios Motakis 
757e992d69SAntonios Motakis 	return 0;
767e992d69SAntonios Motakis }
777e992d69SAntonios Motakis 
virqfd_ptable_queue_proc(struct file * file,wait_queue_head_t * wqh,poll_table * pt)787e992d69SAntonios Motakis static void virqfd_ptable_queue_proc(struct file *file,
797e992d69SAntonios Motakis 				     wait_queue_head_t *wqh, poll_table *pt)
807e992d69SAntonios Motakis {
817e992d69SAntonios Motakis 	struct virqfd *virqfd = container_of(pt, struct virqfd, pt);
827e992d69SAntonios Motakis 	add_wait_queue(wqh, &virqfd->wait);
837e992d69SAntonios Motakis }
847e992d69SAntonios Motakis 
virqfd_shutdown(struct work_struct * work)857e992d69SAntonios Motakis static void virqfd_shutdown(struct work_struct *work)
867e992d69SAntonios Motakis {
877e992d69SAntonios Motakis 	struct virqfd *virqfd = container_of(work, struct virqfd, shutdown);
887e992d69SAntonios Motakis 	u64 cnt;
897e992d69SAntonios Motakis 
907e992d69SAntonios Motakis 	eventfd_ctx_remove_wait_queue(virqfd->eventfd, &virqfd->wait, &cnt);
917e992d69SAntonios Motakis 	flush_work(&virqfd->inject);
927e992d69SAntonios Motakis 	eventfd_ctx_put(virqfd->eventfd);
937e992d69SAntonios Motakis 
947e992d69SAntonios Motakis 	kfree(virqfd);
957e992d69SAntonios Motakis }
967e992d69SAntonios Motakis 
virqfd_inject(struct work_struct * work)977e992d69SAntonios Motakis static void virqfd_inject(struct work_struct *work)
987e992d69SAntonios Motakis {
997e992d69SAntonios Motakis 	struct virqfd *virqfd = container_of(work, struct virqfd, inject);
1007e992d69SAntonios Motakis 	if (virqfd->thread)
1017e992d69SAntonios Motakis 		virqfd->thread(virqfd->opaque, virqfd->data);
1027e992d69SAntonios Motakis }
1037e992d69SAntonios Motakis 
virqfd_flush_inject(struct work_struct * work)104b620ecbdSAlex Williamson static void virqfd_flush_inject(struct work_struct *work)
105b620ecbdSAlex Williamson {
106b620ecbdSAlex Williamson 	struct virqfd *virqfd = container_of(work, struct virqfd, flush_inject);
107b620ecbdSAlex Williamson 
108b620ecbdSAlex Williamson 	flush_work(&virqfd->inject);
109b620ecbdSAlex Williamson }
110b620ecbdSAlex Williamson 
vfio_virqfd_enable(void * opaque,int (* handler)(void *,void *),void (* thread)(void *,void *),void * data,struct virqfd ** pvirqfd,int fd)1117e992d69SAntonios Motakis int vfio_virqfd_enable(void *opaque,
1127e992d69SAntonios Motakis 		       int (*handler)(void *, void *),
1137e992d69SAntonios Motakis 		       void (*thread)(void *, void *),
1147e992d69SAntonios Motakis 		       void *data, struct virqfd **pvirqfd, int fd)
1157e992d69SAntonios Motakis {
1167e992d69SAntonios Motakis 	struct fd irqfd;
1177e992d69SAntonios Motakis 	struct eventfd_ctx *ctx;
1187e992d69SAntonios Motakis 	struct virqfd *virqfd;
1197e992d69SAntonios Motakis 	int ret = 0;
120e6c8adcaSAl Viro 	__poll_t events;
1217e992d69SAntonios Motakis 
1220886196cSJason Gunthorpe 	virqfd = kzalloc(sizeof(*virqfd), GFP_KERNEL_ACCOUNT);
1237e992d69SAntonios Motakis 	if (!virqfd)
1247e992d69SAntonios Motakis 		return -ENOMEM;
1257e992d69SAntonios Motakis 
1267e992d69SAntonios Motakis 	virqfd->pvirqfd = pvirqfd;
1277e992d69SAntonios Motakis 	virqfd->opaque = opaque;
1287e992d69SAntonios Motakis 	virqfd->handler = handler;
1297e992d69SAntonios Motakis 	virqfd->thread = thread;
1307e992d69SAntonios Motakis 	virqfd->data = data;
1317e992d69SAntonios Motakis 
1327e992d69SAntonios Motakis 	INIT_WORK(&virqfd->shutdown, virqfd_shutdown);
1337e992d69SAntonios Motakis 	INIT_WORK(&virqfd->inject, virqfd_inject);
134b620ecbdSAlex Williamson 	INIT_WORK(&virqfd->flush_inject, virqfd_flush_inject);
1357e992d69SAntonios Motakis 
1367e992d69SAntonios Motakis 	irqfd = fdget(fd);
137*1da91ea8SAl Viro 	if (!fd_file(irqfd)) {
1387e992d69SAntonios Motakis 		ret = -EBADF;
1397e992d69SAntonios Motakis 		goto err_fd;
1407e992d69SAntonios Motakis 	}
1417e992d69SAntonios Motakis 
142*1da91ea8SAl Viro 	ctx = eventfd_ctx_fileget(fd_file(irqfd));
1437e992d69SAntonios Motakis 	if (IS_ERR(ctx)) {
1447e992d69SAntonios Motakis 		ret = PTR_ERR(ctx);
1457e992d69SAntonios Motakis 		goto err_ctx;
1467e992d69SAntonios Motakis 	}
1477e992d69SAntonios Motakis 
1487e992d69SAntonios Motakis 	virqfd->eventfd = ctx;
1497e992d69SAntonios Motakis 
1507e992d69SAntonios Motakis 	/*
1517e992d69SAntonios Motakis 	 * virqfds can be released by closing the eventfd or directly
1527e992d69SAntonios Motakis 	 * through ioctl.  These are both done through a workqueue, so
1537e992d69SAntonios Motakis 	 * we update the pointer to the virqfd under lock to avoid
1547e992d69SAntonios Motakis 	 * pushing multiple jobs to release the same virqfd.
1557e992d69SAntonios Motakis 	 */
1567e992d69SAntonios Motakis 	spin_lock_irq(&virqfd_lock);
1577e992d69SAntonios Motakis 
1587e992d69SAntonios Motakis 	if (*pvirqfd) {
1597e992d69SAntonios Motakis 		spin_unlock_irq(&virqfd_lock);
1607e992d69SAntonios Motakis 		ret = -EBUSY;
1617e992d69SAntonios Motakis 		goto err_busy;
1627e992d69SAntonios Motakis 	}
1637e992d69SAntonios Motakis 	*pvirqfd = virqfd;
1647e992d69SAntonios Motakis 
1657e992d69SAntonios Motakis 	spin_unlock_irq(&virqfd_lock);
1667e992d69SAntonios Motakis 
1677e992d69SAntonios Motakis 	/*
1687e992d69SAntonios Motakis 	 * Install our own custom wake-up handling so we are notified via
1697e992d69SAntonios Motakis 	 * a callback whenever someone signals the underlying eventfd.
1707e992d69SAntonios Motakis 	 */
1717e992d69SAntonios Motakis 	init_waitqueue_func_entry(&virqfd->wait, virqfd_wakeup);
1727e992d69SAntonios Motakis 	init_poll_funcptr(&virqfd->pt, virqfd_ptable_queue_proc);
1737e992d69SAntonios Motakis 
174*1da91ea8SAl Viro 	events = vfs_poll(fd_file(irqfd), &virqfd->pt);
1757e992d69SAntonios Motakis 
1767e992d69SAntonios Motakis 	/*
1777e992d69SAntonios Motakis 	 * Check if there was an event already pending on the eventfd
1787e992d69SAntonios Motakis 	 * before we registered and trigger it as if we didn't miss it.
1797e992d69SAntonios Motakis 	 */
180a9a08845SLinus Torvalds 	if (events & EPOLLIN) {
1817e992d69SAntonios Motakis 		if ((!handler || handler(opaque, data)) && thread)
1827e992d69SAntonios Motakis 			schedule_work(&virqfd->inject);
1837e992d69SAntonios Motakis 	}
1847e992d69SAntonios Motakis 
1857e992d69SAntonios Motakis 	/*
1867e992d69SAntonios Motakis 	 * Do not drop the file until the irqfd is fully initialized,
187a9a08845SLinus Torvalds 	 * otherwise we might race against the EPOLLHUP.
1887e992d69SAntonios Motakis 	 */
1897e992d69SAntonios Motakis 	fdput(irqfd);
1907e992d69SAntonios Motakis 
1917e992d69SAntonios Motakis 	return 0;
1927e992d69SAntonios Motakis err_busy:
1937e992d69SAntonios Motakis 	eventfd_ctx_put(ctx);
1947e992d69SAntonios Motakis err_ctx:
1957e992d69SAntonios Motakis 	fdput(irqfd);
1967e992d69SAntonios Motakis err_fd:
1977e992d69SAntonios Motakis 	kfree(virqfd);
1987e992d69SAntonios Motakis 
1997e992d69SAntonios Motakis 	return ret;
2007e992d69SAntonios Motakis }
2017e992d69SAntonios Motakis EXPORT_SYMBOL_GPL(vfio_virqfd_enable);
2027e992d69SAntonios Motakis 
vfio_virqfd_disable(struct virqfd ** pvirqfd)2037e992d69SAntonios Motakis void vfio_virqfd_disable(struct virqfd **pvirqfd)
2047e992d69SAntonios Motakis {
2057e992d69SAntonios Motakis 	unsigned long flags;
2067e992d69SAntonios Motakis 
2077e992d69SAntonios Motakis 	spin_lock_irqsave(&virqfd_lock, flags);
2087e992d69SAntonios Motakis 
2097e992d69SAntonios Motakis 	if (*pvirqfd) {
2107e992d69SAntonios Motakis 		virqfd_deactivate(*pvirqfd);
2117e992d69SAntonios Motakis 		*pvirqfd = NULL;
2127e992d69SAntonios Motakis 	}
2137e992d69SAntonios Motakis 
2147e992d69SAntonios Motakis 	spin_unlock_irqrestore(&virqfd_lock, flags);
2157e992d69SAntonios Motakis 
2167e992d69SAntonios Motakis 	/*
2177e992d69SAntonios Motakis 	 * Block until we know all outstanding shutdown jobs have completed.
2187e992d69SAntonios Motakis 	 * Even if we don't queue the job, flush the wq to be sure it's
2197e992d69SAntonios Motakis 	 * been released.
2207e992d69SAntonios Motakis 	 */
2217e992d69SAntonios Motakis 	flush_workqueue(vfio_irqfd_cleanup_wq);
2227e992d69SAntonios Motakis }
2237e992d69SAntonios Motakis EXPORT_SYMBOL_GPL(vfio_virqfd_disable);
224b620ecbdSAlex Williamson 
vfio_virqfd_flush_thread(struct virqfd ** pvirqfd)225b620ecbdSAlex Williamson void vfio_virqfd_flush_thread(struct virqfd **pvirqfd)
226b620ecbdSAlex Williamson {
227b620ecbdSAlex Williamson 	unsigned long flags;
228b620ecbdSAlex Williamson 
229b620ecbdSAlex Williamson 	spin_lock_irqsave(&virqfd_lock, flags);
230b620ecbdSAlex Williamson 	if (*pvirqfd && (*pvirqfd)->thread)
231b620ecbdSAlex Williamson 		queue_work(vfio_irqfd_cleanup_wq, &(*pvirqfd)->flush_inject);
232b620ecbdSAlex Williamson 	spin_unlock_irqrestore(&virqfd_lock, flags);
233b620ecbdSAlex Williamson 
234b620ecbdSAlex Williamson 	flush_workqueue(vfio_irqfd_cleanup_wq);
235b620ecbdSAlex Williamson }
236b620ecbdSAlex Williamson EXPORT_SYMBOL_GPL(vfio_virqfd_flush_thread);
237