Skip to contentSkip to conversation

KVM: Add module for platform IRQ forwarding

RFC PATCH New Micah Morton 2 participants 2 messages Feb 20, 2020 – Feb 22, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 KVM: Add module for platform IRQ forwarding

Currently, KVM/VFIO offers no way to forward interrupts into a guest VM when those interrupts are generated by a platform device that sits behind a PCI bus controller which is given to the VM. This module allows for forwarding arbitrary interrupts on the host system into the guest, supporting this platform-device-behind-PCI-controller scenario.

This code is mostly inspired/taken from the equivalent code in VFIO. I don't necessarily expect this exact code or code that closely resembles this to be merged -- just sharing this patch that I've written/tested to show an example of the kind of functionality I'm looking to gain support for in KVM. (At the very least some of the code duplication from VFIO should probably be eliminated).

One obvious question would be why not just add this support to VFIO? See https://www.redhat.com/archives/vfio-users/2019-December/msg00008.html
and the encompassing thread for a discussion as to why this probably isn't the way to go.

Forwarding arbitrary IRQs to a guest VM does require the VMM to "tell" the guest about the interrupt (e.g. through ACPI), since such info is not discoverable by the guest like it is for PCI devices. So separate logic is needed in the VMM to set this up -- this isn't something done by the module shared here.

What do people think? Forwarding platform IRQs can have a big payoff for getting platform devices to work in a guest, especially when the platform devices sit behind a PCI bus controller that can be easily passed through to the guest. On an Intel device I'm using for development, this module allowed me to get multiple devices (keyboard, touchscreen, touchpad) working in a VM guest on the device that wouldn't have worked otherwise -- straight out of the box after passing through the PCI bus controller with vfio-pci (plus constructing some AML for the guest in the VMM).

NOTE: This code works for forwarding IRQs to a guest (with the VMM calling the appropriate ioctls with the appropriate args), although it's missing some code and testing related to shutdown/irq disable/reboot. Works well enough to demonstrate the feasibility though.

---
 include/linux/miscdevice.h          |   1 +
 include/linux/plat_irqfd.h          |  20 ++
 include/uapi/linux/platirqforward.h |  55 ++++++
 virt/lib/Kconfig                    |   3 +
 virt/lib/Makefile                   |   1 +
 virt/lib/plat_irqfd.c               | 146 ++++++++++++++
 virt/lib/platirqforward.c           | 289 ++++++++++++++++++++++++++++
 7 files changed, 515 insertions(+)
 create mode 100644 include/linux/plat_irqfd.h
 create mode 100644 include/uapi/linux/platirqforward.h
 create mode 100644 virt/lib/plat_irqfd.c
 create mode 100644 virt/lib/platirqforward.c
include/linux/miscdevice.h +1 -0
index becde6981a95..a9866dc7e10a 100644
@@ -39,6 +39,7 @@
 #define IRNET_MINOR		187
 #define D7S_MINOR		193
 #define VFIO_MINOR		196
+#define PLAT_IRQ_FORWARD_MINOR	197
 #define TUN_MINOR		200
 #define CUSE_MINOR		203
 #define MWAVE_MINOR		219	/* ACP/Mwave Modem */
include/linux/plat_irqfd.h new file +20 -0
new file mode 100644
index 000000000000..936a79cc7f5e
@@ -0,0 +1,20 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+#ifndef PLAT_IRQFD_H
+#define PLAT_IRQFD_H
+
+struct plat_irq_forward_irqfd {
+	struct eventfd_ctx	*eventfd;
+	int			(*handler)(void *, void *);
+	void			*data;
+	wait_queue_entry_t	wait;
+	poll_table		pt;
+	struct work_struct	shutdown;
+	struct plat_irq_forward_irqfd		**pirqfd;
+};
+
+int plat_irq_forward_irqfd_enable(int (*handler)(void *, void *), void *data, struct plat_irq_forward_irqfd **pirqfd, int fd);
+#endif /* PLAT_IRQFD_H */
include/uapi/linux/platirqforward.h new file +55 -0

55 lines not on this page · raw message

virt/lib/Kconfig +3 -0
index 2d9523b7155e..68d911ec54cc 100644
@@ -1,3 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-only
 config IRQ_BYPASS_MANAGER
 	tristate
+
+config PLAT_IRQ_FORWARD
+	tristate "Enable forwarding arbitrary platform IRQs to guest in KVM"
virt/lib/Makefile +1 -0
index bd7f9a78bb6b..633e793b19a5 100644
@@ -1,2 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0-only
 obj-$(CONFIG_IRQ_BYPASS_MANAGER) += irqbypass.o
+obj-$(CONFIG_PLAT_IRQ_FORWARD) += platirqforward.o plat_irqfd.o
virt/lib/plat_irqfd.c new file +146 -0

146 lines not on this page · raw message

virt/lib/platirqforward.c new file +289 -0

289 lines not on this page · raw message

Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
#1 lore raw
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please…

Hi Micah,

[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on kvm/linux-next]
[also build test ERROR on vhost/linux-next char-misc/char-misc-testing linus/master v5.6-rc2 next-20200221] [if your patch is applied to the wrong git tree, please drop us a note to help
improve the system. BTW, we also suggest to use '--base' option to specify the
base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Micah-Morton/KVM-Add-module-for-platform-IRQ-forwarding/20200222-074100
base:   https://git.kernel.org/pub/scm/virt/kvm/kvm.git linux-next
config: c6x-allyesconfig (attached as .config)
compiler: c6x-elf-gcc (GCC) 7.5.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.5.0 make.cross ARCH=c6x

If you fix the issue, kindly add following tag

All error/warnings (new ones prefixed by >>):

In file included from include/linux/linkage.h:7:0,

                    from arch/c6x/include/asm/string.h:12,
                    from include/linux/string.h:20,
                    from include/linux/scatterlist.h:5,
                    from include/linux/iommu.h:10,
                    from include/linux/vfio.h:12,
                    from virt/lib/plat_irqfd.c:2:
>> virt/lib/plat_irqfd.c:146:19: error: 'plat_irq_forward_plat_irqfd_enable' undeclared here (not in a function); did you mean 'plat_irq_forward_irqfd_enable'?
    EXPORT_SYMBOL_GPL(plat_irq_forward_plat_irqfd_enable);
                      ^
   include/linux/export.h:98:16: note: in definition of macro '___EXPORT_SYMBOL'
     extern typeof(sym) sym;       \
                   ^~~
   include/linux/export.h:155:34: note: in expansion of macro '__EXPORT_SYMBOL'
    #define _EXPORT_SYMBOL(sym, sec) __EXPORT_SYMBOL(sym, sec, "")
                                     ^~~~~~~~~~~~~~~
   include/linux/export.h:159:33: note: in expansion of macro '_EXPORT_SYMBOL'
    #define EXPORT_SYMBOL_GPL(sym)  _EXPORT_SYMBOL(sym, "_gpl")
                                    ^~~~~~~~~~~~~~
>> virt/lib/plat_irqfd.c:146:1: note: in expansion of macro 'EXPORT_SYMBOL_GPL'
    EXPORT_SYMBOL_GPL(plat_irq_forward_plat_irqfd_enable);
    ^~~~~~~~~~~~~~~~~

vim +146 virt/lib/plat_irqfd.c

    68	
    69	int plat_irq_forward_irqfd_enable(int (*handler)(void *, void *), void *data, struct plat_irq_forward_irqfd **pirqfd, int fd)
    70	{
    71	        struct fd irqfd;
    72	        struct eventfd_ctx *ctx;
    73	        struct plat_irq_forward_irqfd *plat_irqfd;
    74	        int ret = 0;
    75	        unsigned int events;
    76	
    77	        plat_irqfd = kzalloc(sizeof(*plat_irqfd), GFP_KERNEL);
    78	        if (!plat_irqfd)
    79	                return -ENOMEM;
    80	
    81	        plat_irqfd->pirqfd = pirqfd;
    82	        plat_irqfd->handler = handler;
    83	        plat_irqfd->data = data;
    84	
    85	        // shutdown causes crash
    86	        INIT_WORK(&plat_irqfd->shutdown, plat_irqfd_shutdown);
    87	
    88	        irqfd = fdget(fd);
    89	        if (!irqfd.file) {
    90	                ret = -EBADF;
    91	                goto err_fd;
    92	        }
    93	
    94	        ctx = eventfd_ctx_fileget(irqfd.file);
    95	        if (IS_ERR(ctx)) {
    96	                ret = PTR_ERR(ctx);
    97	                goto err_ctx;
    98	        }
    99	
   100	        plat_irqfd->eventfd = ctx;
   101	
   102	         // plat_irqfds can be released by closing the eventfd or directly
   103	         // through ioctl.  These are both done through a workqueue, so
   104	         // we update the pointer to the plat_irqfd under lock to avoid
   105	         // pushing multiple jobs to release the same plat_irqfd.
   106	        spin_lock_irq(&plat_irqfd_lock);
   107	
   108	        if (*pirqfd) {
   109	                printk(KERN_EMERG "pirqfd should be NULL. BUG!\n");
   110	                spin_unlock_irq(&plat_irqfd_lock);
   111	                ret = -EBUSY;
   112	                goto err_busy;
   113	        }
   114	        *pirqfd = plat_irqfd;
   115	
   116	        spin_unlock_irq(&plat_irqfd_lock);
   117	
   118	         // Install our own custom wake-up handling so we are notified via
   119	         // a callback whenever someone signals the underlying eventfd.
   120	        init_waitqueue_func_entry(&plat_irqfd->wait, plat_irqfd_wakeup);
   121	        init_poll_funcptr(&plat_irqfd->pt, plat_irqfd_ptable_queue_proc);
   122	
   123	        events = irqfd.file->f_op->poll(irqfd.file, &plat_irqfd->pt);
   124	
   125	         // Check if there was an event already pending on the eventfd
   126	         // before we registered and trigger it as if we didn't miss it.
   127	        if (events & POLLIN) {
   128	                if (!handler || handler(NULL, data))
   129	                        printk(KERN_EMERG "handler failed\n");
   130	        }
   131	
   132	         // Do not drop the file until the irqfd is fully initialized,
   133	         // otherwise we might race against the POLLHUP.
   134	        fdput(irqfd);
   135	
   136	        return 0;
   137	err_busy:
   138	        eventfd_ctx_put(ctx);
   139	err_ctx:
   140	        fdput(irqfd);
   141	err_fd:
   142	        kfree(plat_irqfd);
   143	
   144	        return ret;
   145	}
 > 146	EXPORT_SYMBOL_GPL(plat_irq_forward_plat_irqfd_enable);

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org