Skip to contentSkip to conversation

xen: privcmd: Add ioeventfd support

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

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 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

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

PATCH v2 1/2 xen: evtchn: Allow shared registration of IRQ handers

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
#2 lore raw

PATCH v2 2/2 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             | 398 +++++++++++++++++++++++++++++-
 include/uapi/xen/privcmd.h        |  18 ++
 include/xen/interface/hvm/ioreq.h |  51 ++++
 4 files changed, 469 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 +396 -2

449 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
#3 lore raw

Re: [PATCH V2 2/2] xen: privcmd: Add support for ioeventfd

kernel test robot noticed the following build warnings:

Hi Viresh,

kernel test robot noticed the following build warnings:

[auto build test WARNING on xen-tip/linux-next]
[also build test WARNING on linus/master v6.6-rc6]
[cannot apply to next-20231018]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Viresh-Kumar/xen-privcmd-Add-support-for-ioeventfd/20231005-215814
base:   https://git.kernel.org/pub/scm/linux/kernel/git/xen/tip.git linux-next
patch link:    https://lore.kernel.org/r/6e43e092e35a56619b0a132e78fac01b68ded94a.1696500578.git.viresh.kumar%40linaro.org
patch subject: [PATCH V2 2/2] xen: privcmd: Add support for ioeventfd
config: arm64-allyesconfig (https://download.01.org/0day-ci/archive/20231019/[email protected]/config)
compiler: aarch64-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231019/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

drivers/xen/privcmd.c: In function 'alloc_ioreq':

>> drivers/xen/privcmd.c:1233:30: warning: array subscript i is outside array bounds of 'struct ioreq_port[0]' [-Warray-bounds=]
    1233 |                 kioreq->ports[i].vcpu = i;
         |                 ~~~~~~~~~~~~~^~~
   drivers/xen/privcmd.c:1118:27: note: while referencing 'ports'
    1118 |         struct ioreq_port ports[0];
         |                           ^~~~~
   drivers/xen/privcmd.c:1234:30: warning: array subscript i is outside array bounds of 'struct ioreq_port[0]' [-Warray-bounds=]
    1234 |                 kioreq->ports[i].port = ports[i];
         |                 ~~~~~~~~~~~~~^~~
   drivers/xen/privcmd.c:1118:27: note: while referencing 'ports'
    1118 |         struct ioreq_port ports[0];
         |                           ^~~~~
   drivers/xen/privcmd.c:1235:30: warning: array subscript i is outside array bounds of 'struct ioreq_port[0]' [-Warray-bounds=]
    1235 |                 kioreq->ports[i].kioreq = kioreq;
         |                 ~~~~~~~~~~~~~^~~
   drivers/xen/privcmd.c:1118:27: note: while referencing 'ports'
    1118 |         struct ioreq_port ports[0];
         |                           ^~~~~
   drivers/xen/privcmd.c:1239:33: warning: array subscript i is outside array bounds of 'struct ioreq_port[0]' [-Warray-bounds=]
    1239 |                                 &kioreq->ports[i]);
         |                                 ^~~~~~~~~~~~~~~~~
   drivers/xen/privcmd.c:1118:27: note: while referencing 'ports'
    1118 |         struct ioreq_port ports[0];
         |                           ^~~~~
   drivers/xen/privcmd.c:1252:67: warning: array subscript i is outside array bounds of 'struct ioreq_port[0]' [-Warray-bounds=]
    1252 |                 unbind_from_irqhandler(irq_from_evtchn(ports[i]), &kioreq->ports[i]);
         |                                                                   ^~~~~~~~~~~~~~~~~
   drivers/xen/privcmd.c:1118:27: note: while referencing 'ports'
    1118 |         struct ioreq_port ports[0];
         |                           ^~~~~

vim +1233 drivers/xen/privcmd.c

1182
1183 static
1184 struct privcmd_kernel_ioreq *alloc_ioreq(struct privcmd_ioeventfd *ioeventfd)
1185 {
1186 struct privcmd_kernel_ioreq *kioreq;
1187 struct mm_struct *mm = current->mm;
1188 struct vm_area_struct *vma;
1189 struct page **pages;
1190 unsigned int *ports;
1191 int ret, size, i;
1192
1193 lockdep_assert_held(&ioreq_lock);
1194
1195 size = struct_size(kioreq, ports, ioeventfd->vcpus);
1196 kioreq = kzalloc(size, GFP_KERNEL);
1197 if (!kioreq)
1198 return ERR_PTR(-ENOMEM);
1199
1200 kioreq->dom = ioeventfd->dom;
1201 kioreq->vcpus = ioeventfd->vcpus;
1202 kioreq->uioreq = ioeventfd->ioreq;
1203 spin_lock_init(&kioreq->lock);
1204 INIT_LIST_HEAD(&kioreq->ioeventfds);
1205
1206 /* The memory for ioreq server must have been mapped earlier */
1207 mmap_write_lock(mm);
1208 vma = find_vma(mm, (unsigned long)ioeventfd->ioreq);
1209 if (!vma) {
1210 pr_err("Failed to find vma for ioreq page!\n");
1211 mmap_write_unlock(mm);
1212 ret = -EFAULT;
1213 goto error_kfree;
1214 }
1215
1216 pages = vma->vm_private_data;
1217 kioreq->ioreq = (struct ioreq *)(page_to_virt(pages[0]));
1218 mmap_write_unlock(mm);
1219
1220 size = sizeof(*ports) * kioreq->vcpus;
1221 ports = kzalloc(size, GFP_KERNEL);
1222 if (!ports) {
1223 ret = -ENOMEM;
1224 goto error_kfree;
1225 }
1226
1227 if (copy_from_user(ports, ioeventfd->ports, size)) {
1228 ret = -EFAULT;
1229 goto error_kfree_ports;
1230 }
1231
1232 for (i = 0; i < kioreq->vcpus; i++) {

> 1233			kioreq->ports[i].vcpu = i;

1234 kioreq->ports[i].port = ports[i];
1235 kioreq->ports[i].kioreq = kioreq;
1236
1237 ret = bind_evtchn_to_irqhandler_lateeoi(ports[i],
1238 ioeventfd_interrupt, IRQF_SHARED, "ioeventfd",
1239 &kioreq->ports[i]);
1240 if (ret < 0)
1241 goto error_unbind;
1242 }
1243
1244 kfree(ports);
1245
1246 list_add_tail(&kioreq->list, &ioreq_list);
1247
1248 return kioreq;
1249
1250 error_unbind:
1251 while (--i >= 0)
1252 unbind_from_irqhandler(irq_from_evtchn(ports[i]), &kioreq->ports[i]);
1253 error_kfree_ports:
1254 kfree(ports);
1255 error_kfree:
1256 kfree(kioreq);
1257 return ERR_PTR(ret);
1258 }
1259

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#4 lore raw

Re: [PATCH V2 2/2] xen: privcmd: Add support for ioeventfd

Quoted 24 lines · kernel test robot

Hi Viresh,

kernel test robot noticed the following build warnings:

[auto build test WARNING on xen-tip/linux-next]
[also build test WARNING on linus/master v6.6-rc6]
[cannot apply to next-20231018]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Viresh-Kumar/xen-privcmd-Add-support-for-ioeventfd/20231005-215814
base:   https://git.kernel.org/pub/scm/linux/kernel/git/xen/tip.git linux-next
patch link:    https://lore.kernel.org/r/6e43e092e35a56619b0a132e78fac01b68ded94a.1696500578.git.viresh.kumar%40linaro.org
patch subject: [PATCH V2 2/2] xen: privcmd: Add support for ioeventfd
config: arm64-allyesconfig (https://download.01.org/0day-ci/archive/20231019/[email protected]/config)
compiler: aarch64-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231019/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

Quoted 5 lines

All warnings (new ones prefixed by >>):

drivers/xen/privcmd.c: In function 'alloc_ioreq':

Quoted 1 line
Quoted 1 line

drivers/xen/privcmd.c:1233:30: warning: array subscript i is outside array bounds of 'struct ioreq_port[0]' [-Warray-bounds=]

    1233 |                 kioreq->ports[i].vcpu = i;

Looks like a false negative.

Signature · 1 line
viresh