Skip to contentSkip to conversation

xen: privcmd: Add ioeventfd and fix irqfd support

PATCH In review Viresh Kumar 2 participants 9 messages Oct 16, 2023 Xen hypervisor interface

lore
Share & tools
lore
#0 lore raw

v4 cover xen: privcmd: Add ioeventfd and fix irqfd support

Hello,

Now that irqfd support (backend to guest interrupt) is already merged, this series solves the other part of the problem, i.e. ioeventfd (guest to backend interrupt).

More details inside the commits.

Arnd reported few issues with the ioctl macro usage and argument's layout, fixed them for irqfd too, which was added recently.

--
Viresh

V3->V4:
- Use __u64 for indirect pointers in an ioctl command's arguments.
- Use u64_to_user_ptr() in kernel driver to access the same.
- Use _IOW() macro instead of the internal one: _IOC().

V2->V3:
- Remove explicit barriers and depend on spin lock instead to take care of it.
- Move check for empty ioeventfds list to privcmd_ioeventfd_deassign(), which could earlier call ioreq_free() even when the list wasn't empty and so we returned without printing a warning in v1 earlier. V2 implemented it incorrectly.

V1->V2:
- Increment irq_info refcnt only for valid info.
- Use u64 type for addr.
- Add comments for use of barriers.
- Use spin lock instead of mutex as we need to use them in irq handler.
- Add a warning when kioreq is getting freed and ioeventfds list isn't empty.
- Use struct_size().
- Validate number of vcpus as well.

Viresh Kumar (4):
xen: Make struct privcmd_irqfd's layout architecture independent
xen: irqfd: Use _IOW instead of the internal _IOC() macro
xen: evtchn: Allow shared registration of IRQ handers
xen: privcmd: Add support for ioeventfd

 drivers/xen/Kconfig               |   8 +-
 drivers/xen/events/events_base.c  |   3 +-
 drivers/xen/evtchn.c              |   2 +-
 drivers/xen/privcmd.c             | 407 +++++++++++++++++++++++++++++-
 include/uapi/xen/privcmd.h        |  22 +-
 include/xen/interface/hvm/ioreq.h |  51 ++++
 6 files changed, 482 insertions(+), 11 deletions(-)
 create mode 100644 include/xen/interface/hvm/ioreq.h
Signature · 1 line
2.31.1.272.g89b43f80a514
#1 lore raw

PATCH v4 1/4 xen: Make struct privcmd_irqfd's layout architecture independent

Using indirect pointers in an ioctl command argument means that the layout is architecture specific, in particular we can't use the same one from 32-bit compat tasks. The general recommendation is to have __u64 members and use u64_to_user_ptr() to access it from the kernel if we are unable to avoid the pointers altogether.

Fixes f8941e6c4c71 ("xen: privcmd: Add support for irqfd")
---
 drivers/xen/privcmd.c      | 2 +-
 include/uapi/xen/privcmd.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/xen/privcmd.c +1 -1
index 120af57999fc..5095bd1abea5 100644
@@ -935,7 +935,7 @@ static int privcmd_irqfd_assign(struct privcmd_irqfd *irqfd)
 		return -ENOMEM;
 	dm_op = kirqfd + 1;
 
-	if (copy_from_user(dm_op, irqfd->dm_op, irqfd->size)) {
+	if (copy_from_user(dm_op, u64_to_user_ptr(irqfd->dm_op), irqfd->size)) {
 		ret = -EFAULT;
 		goto error_kfree;
 	}
include/uapi/xen/privcmd.h +1 -1
index 375718ba4ab6..b143fafce84d 100644
@@ -102,7 +102,7 @@ struct privcmd_mmap_resource {
 #define PRIVCMD_IRQFD_FLAG_DEASSIGN (1 << 0)
 
 struct privcmd_irqfd {
-	void __user *dm_op;
+	__u64 dm_op;
 	__u32 size; /* Size of structure pointed by dm_op */
 	__u32 fd;
 	__u32 flags;
Signature · 1 line
2.31.1.272.g89b43f80a514
maintainer Reviewed-by on v4 1/4
#2 lore raw

Re: [PATCH V4 1/4] xen: Make struct privcmd_irqfd's layout architecture independent

Quoted 10 lines · Viresh Kumar

Using indirect pointers in an ioctl command argument means that the layout is architecture specific, in particular we can't use the same one from 32-bit compat tasks. The general recommendation is to have __u64 members and use u64_to_user_ptr() to access it from the kernel if we are unable to avoid the pointers altogether.

Fixes f8941e6c4c71 ("xen: privcmd: Add support for irqfd")

Juergen

#3 lore raw

PATCH v4 2/4 xen: irqfd: Use _IOW instead of the internal _IOC() macro

In reply to Viresh Kumar

_IOC() an internal helper that we should not use in driver code. In particular, we got the data direction wrong here, which breaks a number of tools, as having "_IOC_NONE" should never be paired with a nonzero size.

Use _IOW() instead.

Fixes f8941e6c4c71 ("xen: privcmd: Add support for irqfd")
---
 include/uapi/xen/privcmd.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/uapi/xen/privcmd.h +1 -1
index b143fafce84d..e145bca5105c 100644
@@ -138,6 +138,6 @@ struct privcmd_irqfd {
 #define IOCTL_PRIVCMD_MMAP_RESOURCE				\
 	_IOC(_IOC_NONE, 'P', 7, sizeof(struct privcmd_mmap_resource))
 #define IOCTL_PRIVCMD_IRQFD					\
-	_IOC(_IOC_NONE, 'P', 8, sizeof(struct privcmd_irqfd))
+	_IOW('P', 8, struct privcmd_irqfd)
 
 #endif /* __LINUX_PUBLIC_PRIVCMD_H__ */
Signature · 1 line
2.31.1.272.g89b43f80a514
maintainer Reviewed-by on v4 2/4
#4 lore raw

Re: [PATCH V4 2/4] xen: irqfd: Use _IOW instead of the internal _IOC() macro

Quoted 11 lines · Viresh Kumar

_IOC() an internal helper that we should not use in driver code. In particular, we got the data direction wrong here, which breaks a number of tools, as having "_IOC_NONE" should never be paired with a nonzero size.

Use _IOW() instead.

Fixes f8941e6c4c71 ("xen: privcmd: Add support for irqfd")

Juergen

#5 lore raw

PATCH v4 3/4 xen: evtchn: Allow shared registration of IRQ handers

In reply to Viresh Kumar

Currently the handling of events is supported either in the kernel or userspace, but not both.

In order to support fast delivery of interrupts from the guest to the backend, we need to handle the Queue notify part of Virtio protocol in kernel and the rest in userspace.

Update the interrupt handler registration flag to IRQF_SHARED for event channels, which would allow multiple entities to bind their interrupt handler for the same event channel port.

Also increment the reference count of irq_info when multiple entities try to bind event channel to irqchip, so the unbinding happens only after all the users are gone.

---
 drivers/xen/events/events_base.c | 3 ++-
 drivers/xen/evtchn.c             | 2 +-
 2 files changed, 3 insertions(+), 2 deletions(-)
drivers/xen/events/events_base.c +2 -1
index c7715f8bd452..d72fb26cc051 100644
@@ -1238,7 +1238,8 @@ static int bind_evtchn_to_irq_chip(evtchn_port_t evtchn, struct irq_chip *chip,
 		bind_evtchn_to_cpu(evtchn, 0, false);
 	} else {
 		struct irq_info *info = info_for_irq(irq);
-		WARN_ON(info == NULL || info->type != IRQT_EVTCHN);
+		if (!WARN_ON(!info || info->type != IRQT_EVTCHN))
+			info->refcnt++;
 	}
 
 out:
drivers/xen/evtchn.c +1 -1
index c99415a70051..43f77915feb5 100644
@@ -397,7 +397,7 @@ static int evtchn_bind_to_user(struct per_user_data *u, evtchn_port_t port)
 	if (rc < 0)
 		goto err;
 
-	rc = bind_evtchn_to_irqhandler_lateeoi(port, evtchn_interrupt, 0,
+	rc = bind_evtchn_to_irqhandler_lateeoi(port, evtchn_interrupt, IRQF_SHARED,
 					       u->name, evtchn);
 	if (rc < 0)
 		goto err;
Signature · 1 line
2.31.1.272.g89b43f80a514
maintainer Reviewed-by on v4 3/4
#6 lore raw

Re: [PATCH V4 3/4] xen: evtchn: Allow shared registration of IRQ handers

Quoted 11 lines · Viresh Kumar

Currently the handling of events is supported either in the kernel or userspace, but not both.

In order to support fast delivery of interrupts from the guest to the backend, we need to handle the Queue notify part of Virtio protocol in kernel and the rest in userspace.

Update the interrupt handler registration flag to IRQF_SHARED for event channels, which would allow multiple entities to bind their interrupt handler for the same event channel port.

Quoted 5 lines

Also increment the reference count of irq_info when multiple entities try to bind event channel to irqchip, so the unbinding happens only after all the users are gone.

Juergen

#7 lore raw

PATCH v4 4/4 xen: privcmd: Add support for ioeventfd

In reply to Viresh Kumar

Virtio guests send VIRTIO_MMIO_QUEUE_NOTIFY notification when they need to notify the backend of an update to the status of the virtqueue. The backend or another entity, polls the MMIO address for updates to know when the notification is sent.

It works well if the backend does this polling by itself. But as we move towards generic backend implementations, we end up implementing this in a separate user-space program.

Generally, the Virtio backends are implemented to work with the Eventfd based mechanism. In order to make such backends work with Xen, another software layer needs to do the polling and send an event via eventfd to the backend once the notification from guest is received. This results in an extra context switch.

This is not a new problem in Linux though. It is present with other hypervisors like KVM, etc. as well. The generic solution implemented in the kernel for them is to provide an IOCTL call to pass the address to poll and eventfd, which lets the kernel take care of polling and raise an event on the eventfd, instead of handling this in user space (which involves an extra context switch).

This patch adds similar support for xen.

Inspired by existing implementations for KVM, etc..

This also copies ioreq.h header file (only struct ioreq and related macros) from Xen's source tree (Top commit 5d84f07fe6bf ("xen/pci: drop remaining uses of bool_t")).

---
 drivers/xen/Kconfig               |   8 +-
 drivers/xen/privcmd.c             | 405 +++++++++++++++++++++++++++++-
 include/uapi/xen/privcmd.h        |  18 ++
 include/xen/interface/hvm/ioreq.h |  51 ++++
 4 files changed, 476 insertions(+), 6 deletions(-)
 create mode 100644 include/xen/interface/hvm/ioreq.h
drivers/xen/Kconfig +4 -4
index d43153fec18e..d5989871dd5d 100644
@@ -269,12 +269,12 @@ config XEN_PRIVCMD
 	  disaggregated Xen setups this driver might be needed for other
 	  domains, too.
 
-config XEN_PRIVCMD_IRQFD
-	bool "Xen irqfd support"
+config XEN_PRIVCMD_EVENTFD
+	bool "Xen Ioeventfd and irqfd support"
 	depends on XEN_PRIVCMD && XEN_VIRTIO && EVENTFD
 	help
-	  Using the irqfd mechanism a virtio backend running in a daemon can
-	  speed up interrupt injection into a guest.
+	  Using the ioeventfd / irqfd mechanism a virtio backend running in a
+	  daemon can speed up interrupt delivery from / to a guest.
 
 config XEN_ACPI_PROCESSOR
 	tristate "Xen ACPI processor"
drivers/xen/privcmd.c +403 -2

456 lines not on this page · raw message

include/uapi/xen/privcmd.h +18 -0
index e145bca5105c..8b8c5d1420fe 100644
@@ -110,6 +110,22 @@ struct privcmd_irqfd {
 	__u8 pad[2];
 };
 
+/* For privcmd_ioeventfd::flags */
+#define PRIVCMD_IOEVENTFD_FLAG_DEASSIGN (1 << 0)
+
+struct privcmd_ioeventfd {
+	__u64 ioreq;
+	__u64 ports;
+	__u64 addr;
+	__u32 addr_len;
+	__u32 event_fd;
+	__u32 vcpus;
+	__u32 vq;
+	__u32 flags;
+	domid_t dom;
+	__u8 pad[2];
+};
+
 /*
  * @cmd: IOCTL_PRIVCMD_HYPERCALL
  * @arg: &privcmd_hypercall_t
@@ -139,5 +155,7 @@ struct privcmd_irqfd {
 	_IOC(_IOC_NONE, 'P', 7, sizeof(struct privcmd_mmap_resource))
 #define IOCTL_PRIVCMD_IRQFD					\
 	_IOW('P', 8, struct privcmd_irqfd)
+#define IOCTL_PRIVCMD_IOEVENTFD					\
+	_IOW('P', 9, struct privcmd_ioeventfd)
 
 #endif /* __LINUX_PUBLIC_PRIVCMD_H__ */
include/xen/interface/hvm/ioreq.h new file +51 -0

51 lines not on this page · raw message

Signature · 1 line
2.31.1.272.g89b43f80a514
maintainer Reviewed-by on v4 4/4
#8 lore raw

Re: [PATCH V4 4/4] xen: privcmd: Add support for ioeventfd

Quoted 24 lines · Viresh Kumar

Virtio guests send VIRTIO_MMIO_QUEUE_NOTIFY notification when they need to notify the backend of an update to the status of the virtqueue. The backend or another entity, polls the MMIO address for updates to know when the notification is sent.

It works well if the backend does this polling by itself. But as we move towards generic backend implementations, we end up implementing this in a separate user-space program.

Generally, the Virtio backends are implemented to work with the Eventfd based mechanism. In order to make such backends work with Xen, another software layer needs to do the polling and send an event via eventfd to the backend once the notification from guest is received. This results in an extra context switch.

This is not a new problem in Linux though. It is present with other hypervisors like KVM, etc. as well. The generic solution implemented in the kernel for them is to provide an IOCTL call to pass the address to poll and eventfd, which lets the kernel take care of polling and raise an event on the eventfd, instead of handling this in user space (which involves an extra context switch).

This patch adds similar support for xen.

Quoted 7 lines

Inspired by existing implementations for KVM, etc..

This also copies ioreq.h header file (only struct ioreq and related macros) from Xen's source tree (Top commit 5d84f07fe6bf ("xen/pci: drop remaining uses of bool_t")).

Juergen