Skip to contentSkip to conversation

xen: privcmd: Add ioeventfd support

PATCH Superseded Viresh Kumar 2 participants 5 messages Oct 9, 2023 – Oct 10, 2023 Xen hypervisor interface

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover xen: privcmd: Add ioeventfd 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.

--
Viresh

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 (2):
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             | 405 +++++++++++++++++++++++++++++-
 include/uapi/xen/privcmd.h        |  18 ++
 include/xen/interface/hvm/ioreq.h |  51 ++++
 6 files changed, 479 insertions(+), 8 deletions(-)
 create mode 100644 include/xen/interface/hvm/ioreq.h
Signature · 1 line
2.31.1.272.g89b43f80a514
#2 lore raw

PATCH v3 2/2 xen: privcmd: Add support for ioeventfd

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 375718ba4ab6..ce71390f0024 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 {
+	void __user *ioreq;
+	unsigned int __user *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					\
 	_IOC(_IOC_NONE, 'P', 8, sizeof(struct privcmd_irqfd))
+#define IOCTL_PRIVCMD_IOEVENTFD					\
+	_IOC(_IOC_NONE, 'P', 9, sizeof(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
#1 lore raw

PATCH v3 1/2 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
#3 lore raw

Re: [Stratos-dev] [PATCH V3 2/2] xen: privcmd: Add support for ioeventfd

In reply to Viresh Kumar

Hi Viresh,

Quoted 44 lines · Viresh Kumar via Stratos-dev

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 +0 -0
index d43153fec18e..d5989871dd5d 100644

drivers/xen/Kconfig config XEN_PRIVCMD · line 273

Quoted 7 lines
drivers/xen/Kconfig +2 -2
@@ -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"

You certainly know about kernel policies better than I but why the CONGIG_ name change?

With this change all downstream distros need to find this and re-enable it if they want to keep a working Xen system.

I presume the kernel can still work in the old way and not use your new mode correct?

I would certainly change the description but I wonder about keeping the CONFIG_ name the same.

No issue if I am obviously wrong and this is done all the time.

Thanks,
Bill

#4 lore raw

Re: [Stratos-dev] [PATCH V3 2/2] xen: privcmd: Add support for ioeventfd

Hi Bill,

Quoted 13 lines · Bill Mills

drivers/xen/Kconfig config XEN_PRIVCMD · line 273

Quoted 4 lines · Viresh Kumar via Stratos-dev
drivers/xen/Kconfig +2 -2
-config XEN_PRIVCMD_IRQFD
-     bool "Xen irqfd support"
+config XEN_PRIVCMD_EVENTFD
+     bool "Xen Ioeventfd and irqfd support"

You certainly know about kernel policies better than I but why the CONGIG_ name change?

With this change all downstream distros need to find this and re-enable it if they want to keep a working Xen system.

Quoted 7 lines

I presume the kernel can still work in the old way and not use your new mode correct?

I would certainly change the description but I wonder about keeping the CONFIG_ name the same.

No issue if I am obviously wrong and this is done all the time.

You are absolutely right Bill, but this is a special case. The CONFIG option here (XEN_PRIVCMD_IRQFD) is recently added (by me) and is currently present in linux-next only. Hopefully this and the other commit will make it to Linus's master together.

No one using this currently downstream and so life won't be that complicated for anyone and so I wanted to put this under the same config option, since they are related too.

Hope it makes sense.

--
Viresh