Skip to contentSkip to conversation

vfio/pci: Introduce vfio_pci driver for ISM devices

PATCH Superseded Julian Ruess 3 participants 7 messages Mar 23

Subsystems (6)
KVM PCI s390 VFIO driver +2 more subsystems

v7 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Julian Ruess

author maintainer
#0 lore raw

v7 cover vfio/pci: Introduce vfio_pci driver for ISM devices

Hi all,

This series adds a vfio_pci variant driver for the s390-specific Internal Shared Memory (ISM) devices used for inter-VM communication including SMC-D.

This is a prerequisite for an in-development open-source user space driver stack that will allow to use ISM devices to provide remote console and block device functionality. This stack will be part of s390-tools.

This driver would also allow QEMU to mediate access to an ISM device, enabling a form of PCI pass-through even for guests whose hardware cannot directly execute PCI accesses, such as nested guests.

On s390, kernel primitives such as ioread() and iowrite() are switched over from function handle based PCI load/stores instructions to PCI memory-I/O (MIO) loads/stores when these are available and not explicitly disabled. Since these instructions cannot be used with ISM devices, ensure that classic function handle-based PCI instructions are used instead.

The driver is still required even when MIO instructions are disabled, as the ISM device relies on the PCI store‑block (PCISTB) instruction to perform write operations.

Thank you,
Julian

---
Changes in v7:
- Add 4k alignment and to kmem_cache_create() to do not cross the integral boundary.
- Add SLAB_RECLAIM_ACCOUNT ans SLAB_ACCOUNT to kmem_cache_create() for memcg accounting.
- Link to v6: https://lore.kernel.org/r/[email protected]

Changes in v6:
- Introduce ism_vfio_pci_init_dev() and ism_vfio_pci_release_dev().
- Add early return for __zpci_load().
- Use kmem_cache_alloc() instead of kmem_cache_zalloc().
- Rename ivdev to ivpcd for consistency.
- Rename ism_pci_open_device() to ism_vfio_pci_open_device().
- Link to v5: https://lore.kernel.org/r/[email protected]

Changes in v5:
- Support reads < 8 bytes.
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Fix bug with < 8 byte reads. For code simplicity, only support 8 byte reads.
- Fix leak of ivpcd.
- Fix cache replacement by implementing a per-device kmem_cache.
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Add comments to ism_vfio_pci_do_io_r() and ism_vfio_pci_do_io_w().
- Format Kconfig.
- Add 4k boundary check to ism_vfio_pci_do_io_w().
- Use kmem_cache instead of kzalloc in ism_vfio_pci_do_io_w().
- Add error handler to struct ism_vfio_pci_driver.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Remove common code patch that sets VFIO_PCI_OFFSET_SHIFT to 48.
- Implement ism_vfio_pci_ioctl_get_region_info() to have own region
offsets.
- For config space accesses, rename vfio_config_do_rw() to vfio_pci_config_rw_single() and export it.
- Use zdev->maxstbl instead of ZPCI_BOUNDARY_SIZE.
- Add comment that zPCI must not use MIO instructions for config space
access.
- Rework patch descriptions.
- Update license info.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Julian Ruess (3):
vfio/pci: Rename vfio_config_do_rw() to vfio_pci_config_rw_single() and export it vfio/ism: Implement vfio_pci driver for ISM devices
MAINTAINERS: add VFIO ISM PCI DRIVER section

 MAINTAINERS                        |   6 +
 drivers/vfio/pci/Kconfig           |   2 +
 drivers/vfio/pci/Makefile          |   2 +
 drivers/vfio/pci/ism/Kconfig       |  10 +
 drivers/vfio/pci/ism/Makefile      |   3 +
 drivers/vfio/pci/ism/main.c        | 401 +++++++++++++++++++++++++++++++++++++
 drivers/vfio/pci/vfio_pci_config.c |   8 +-
 drivers/vfio/pci/vfio_pci_priv.h   |   4 +
 8 files changed, 433 insertions(+), 3 deletions(-)

---
base-commit: c369299895a591d96745d6492d4888259b004a9e
change-id: 20250227-vfio_pci_ism-0ccc2e472247

Best regards,

Signature · 1 line
Julian Ruess <[email protected]>
author maintainer
#1 lore raw

PATCH v7 1/3 vfio/pci: Rename vfio_config_do_rw() to vfio_pci_config_rw_single() and export it

A follow-up patch adds a new variant driver for s390 ISM devices. Since this device uses a 256 TiB BAR 0 that is never mapped, the variant driver needs its own ISM_VFIO_PCI_OFFSET_MASK. To minimally mirror the functionality of vfio_pci_config_rw() with such a custom mask, export vfio_config_do_rw(). To better distinguish the now exported function from vfio_pci_config_rw(), rename it to vfio_pci_config_rw_single() emphasizing that it does a single config space read or write.

---
 drivers/vfio/pci/vfio_pci_config.c | 8 +++++---
 drivers/vfio/pci/vfio_pci_priv.h   | 4 ++++
 2 files changed, 9 insertions(+), 3 deletions(-)
drivers/vfio/pci/vfio_pci_config.c +5 -3
index b4e39253f98da61a5e2b6dd0089b2f6aef4b85a0..fbb47b4ddb43d42b758b16778e6e701379d7e7db 100644
@@ -1880,8 +1880,9 @@ static size_t vfio_pci_cap_remaining_dword(struct vfio_pci_core_device *vdev,
 	return i;
 }
 
-static ssize_t vfio_config_do_rw(struct vfio_pci_core_device *vdev, char __user *buf,
-				 size_t count, loff_t *ppos, bool iswrite)
+ssize_t vfio_pci_config_rw_single(struct vfio_pci_core_device *vdev,
+			      char __user *buf, size_t count, loff_t *ppos,
+			      bool iswrite)
 {
 	struct pci_dev *pdev = vdev->pdev;
 	struct perm_bits *perm;
@@ -1970,6 +1971,7 @@ static ssize_t vfio_config_do_rw(struct vfio_pci_core_device *vdev, char __user
 
 	return ret;
 }
+EXPORT_SYMBOL_GPL(vfio_pci_config_rw_single);
 
 ssize_t vfio_pci_config_rw(struct vfio_pci_core_device *vdev, char __user *buf,
 			   size_t count, loff_t *ppos, bool iswrite)
@@ -1981,7 +1983,7 @@ ssize_t vfio_pci_config_rw(struct vfio_pci_core_device *vdev, char __user *buf,
 	pos &= VFIO_PCI_OFFSET_MASK;
 
 	while (count) {
-		ret = vfio_config_do_rw(vdev, buf, count, &pos, iswrite);
+		ret = vfio_pci_config_rw_single(vdev, buf, count, &pos, iswrite);
 		if (ret < 0)
 			return ret;
 
drivers/vfio/pci/vfio_pci_priv.h +4 -0
index 27ac280f00b975989f6cbc02c11aaca01f9badf3..28a3edf65aeecfa06cd1856637cd33eec1fa3006 100644
@@ -37,6 +37,10 @@ int vfio_pci_set_irqs_ioctl(struct vfio_pci_core_device *vdev, uint32_t flags,
 ssize_t vfio_pci_config_rw(struct vfio_pci_core_device *vdev, char __user *buf,
 			   size_t count, loff_t *ppos, bool iswrite);
 
+ssize_t vfio_pci_config_rw_single(struct vfio_pci_core_device *vdev,
+			      char __user *buf, size_t count, loff_t *ppos,
+			      bool iswrite);
+
 ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf,
 			size_t count, loff_t *ppos, bool iswrite);
 
Signature · 1 line
2.51.0
#2 lore raw

Re: [PATCH v7 1/3] vfio/pci: Rename vfio_config_do_rw() to vfio_pci_config_rw_single() and export it

Quoted 32 lines · Julian Ruess

A follow-up patch adds a new variant driver for s390 ISM devices. Since this device uses a 256 TiB BAR 0 that is never mapped, the variant driver needs its own ISM_VFIO_PCI_OFFSET_MASK. To minimally mirror the functionality of vfio_pci_config_rw() with such a custom mask, export vfio_config_do_rw(). To better distinguish the now exported function from vfio_pci_config_rw(), rename it to vfio_pci_config_rw_single() emphasizing that it does a single config space read or write.

---
 drivers/vfio/pci/vfio_pci_config.c | 8 +++++---
 drivers/vfio/pci/vfio_pci_priv.h   | 4 ++++
 2 files changed, 9 insertions(+), 3 deletions(-)
drivers/vfio/pci/vfio_pci_config.c +3 -2
index b4e39253f98da61a5e2b6dd0089b2f6aef4b85a0..fbb47b4ddb43d42b758b16778e6e701379d7e7db 100644
@@ -1880,8 +1880,9 @@ static size_t vfio_pci_cap_remaining_dword(struct vfio_pci_core_device *vdev,
 	return i;
 }
 
-static ssize_t vfio_config_do_rw(struct vfio_pci_core_device *vdev, char __user *buf,
-				 size_t count, loff_t *ppos, bool iswrite)
+ssize_t vfio_pci_config_rw_single(struct vfio_pci_core_device *vdev,
+			      char __user *buf, size_t count, loff_t *ppos,
+			      bool iswrite)
 {
 	struct pci_dev *pdev = vdev->pdev;
 	struct perm_bits *perm;

drivers/vfio/pci/vfio_pci_config.c static ssize_t vfio_config_do_rw(struct vfio_pci_core_device *vdev, char __user · line 1977

Quoted 7 lines
drivers/vfio/pci/vfio_pci_config.c +1 -0
@@ -1970,6 +1971,7 @@ static ssize_t vfio_config_do_rw(struct vfio_pci_core_device *vdev, char __user
 
 	return ret;
 }
+EXPORT_SYMBOL_GPL(vfio_pci_config_rw_single);
 
 ssize_t vfio_pci_config_rw(struct vfio_pci_core_device *vdev, char __user *buf,
 			   size_t count, loff_t *ppos, bool iswrite)

Nit: The renaming results in formatting CHECKs when running checkpatch.pl --strict. Sorry I missed this in my last review.

Quoted 7 lines
drivers/vfio/pci/vfio_pci_config.c +1 -1
@@ -1981,7 +1983,7 @@ ssize_t vfio_pci_config_rw(struct vfio_pci_core_device *vdev, char __user *buf,
 	pos &= VFIO_PCI_OFFSET_MASK;
 
 	while (count) {
-		ret = vfio_config_do_rw(vdev, buf, count, &pos, iswrite);
+		ret = vfio_pci_config_rw_single(vdev, buf, count, &pos, iswrite);
 
author maintainer
#3 lore raw

PATCH v7 2/3 vfio/ism: Implement vfio_pci driver for ISM devices

In reply to Julian Ruess

Add a vfio_pci variant driver for the s390-specific Internal Shared Memory (ISM) devices used for inter-VM communication.

This enables the development of vfio-pci-based user space drivers for ISM devices.

On s390, kernel primitives such as ioread() and iowrite() are switched over from function handle based PCI load/stores instructions to PCI memory-I/O (MIO) loads/stores when these are available and not explicitly disabled. Since these instructions cannot be used with ISM devices, ensure that classic function handle-based PCI instructions are used instead.

The driver is still required even when MIO instructions are disabled, as the ISM device relies on the PCI store block (PCISTB) instruction to perform write operations.

Stores are not fragmented, therefore one ioctl corresponds to exactly one PCISTB instruction. User space must ensure to not write more than 4096 bytes at once to an ISM BAR which is the maximum payload of the PCISTB instruction.

---
 drivers/vfio/pci/Kconfig      |   2 +
 drivers/vfio/pci/Makefile     |   2 +
 drivers/vfio/pci/ism/Kconfig  |  10 ++
 drivers/vfio/pci/ism/Makefile |   3 +
 drivers/vfio/pci/ism/main.c   | 401 ++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 418 insertions(+)
drivers/vfio/pci/Kconfig +2 -0
index 1e82b44bda1a0a544e1add7f4b06edecf35aaf81..296bf01e185ecacc388ebc69e92706c99e47c814 100644
@@ -60,6 +60,8 @@ config VFIO_PCI_DMABUF
 
 source "drivers/vfio/pci/mlx5/Kconfig"
 
+source "drivers/vfio/pci/ism/Kconfig"
+
 source "drivers/vfio/pci/hisilicon/Kconfig"
 
 source "drivers/vfio/pci/pds/Kconfig"
drivers/vfio/pci/Makefile +2 -0
index e0a0757dd1d2b0bc69b7e4d79441d5cacf4e1cd8..6138f1bf241df04e7419f196b404abdf9b194050 100644
@@ -11,6 +11,8 @@ obj-$(CONFIG_VFIO_PCI) += vfio-pci.o
 
 obj-$(CONFIG_MLX5_VFIO_PCI)           += mlx5/
 
+obj-$(CONFIG_ISM_VFIO_PCI)           += ism/
+
 obj-$(CONFIG_HISI_ACC_VFIO_PCI) += hisilicon/
 
 obj-$(CONFIG_PDS_VFIO_PCI) += pds/
drivers/vfio/pci/ism/Kconfig new file +10 -0
new file mode 100644
index 0000000000000000000000000000000000000000..02f47d25fed2d34c732b67b3a3655b64a7625467
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: GPL-2.0
+config ISM_VFIO_PCI
+	tristate "VFIO support for ISM devices"
+	depends on S390
+	select VFIO_PCI_CORE
+	help
+	  This provides user space support for IBM Internal Shared Memory (ISM)
+	  Adapter devices using the VFIO framework.
+
+	  If you don't know what to do here, say N.
drivers/vfio/pci/ism/Makefile new file +3 -0
new file mode 100644
index 0000000000000000000000000000000000000000..32cc3c66dd11395da85a2b6f05b3d97036ed8a35
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0
+obj-$(CONFIG_ISM_VFIO_PCI) += ism-vfio-pci.o
+ism-vfio-pci-y := main.o
drivers/vfio/pci/ism/main.c new file +401 -0

401 lines not on this page · raw message

Signature · 1 line
2.51.0
Reviewed-by on v7 2/3
#4 lore raw

Re: [PATCH v7 2/3] vfio/ism: Implement vfio_pci driver for ISM devices

Quoted 6 lines · Julian Ruess

Add a vfio_pci variant driver for the s390-specific Internal Shared Memory (ISM) devices used for inter-VM communication.

This enables the development of vfio-pci-based user space drivers for ISM devices.

Quoted 7 lines

On s390, kernel primitives such as ioread() and iowrite() are switched over from function handle based PCI load/stores instructions to PCI memory-I/O (MIO) loads/stores when these are available and not explicitly disabled. Since these instructions cannot be used with ISM devices, ensure that classic function handle-based PCI instructions are used instead.

Nit: How do you want to spell "function-handle-based"? (You have one with 1 hyphen, one without)

Quoted 11 lines

The driver is still required even when MIO instructions are disabled, as the ISM device relies on the PCI store block (PCISTB) instruction to perform write operations.

Stores are not fragmented, therefore one ioctl corresponds to exactly one PCISTB instruction. User space must ensure to not write more than 4096 bytes at once to an ISM BAR which is the maximum payload of the PCISTB instruction.

---

Looks good to me.

Reviewed-by on v7 2/3
#5 lore raw

Re: [PATCH v7 2/3] vfio/ism: Implement vfio_pci driver for ISM devices

In reply to Julian Ruess
Quoted 22 lines · Julian Ruess

Add a vfio_pci variant driver for the s390-specific Internal Shared Memory (ISM) devices used for inter-VM communication.

This enables the development of vfio-pci-based user space drivers for ISM devices.

On s390, kernel primitives such as ioread() and iowrite() are switched over from function handle based PCI load/stores instructions to PCI memory-I/O (MIO) loads/stores when these are available and not explicitly disabled. Since these instructions cannot be used with ISM devices, ensure that classic function handle-based PCI instructions are used instead.

The driver is still required even when MIO instructions are disabled, as the ISM device relies on the PCI store block (PCISTB) instruction to perform write operations.

Stores are not fragmented, therefore one ioctl corresponds to exactly one PCISTB instruction. User space must ensure to not write more than 4096 bytes at once to an ISM BAR which is the maximum payload of the PCISTB instruction.

Quoted 9 lines
---
 drivers/vfio/pci/Kconfig      |   2 +
 drivers/vfio/pci/Makefile     |   2 +
 drivers/vfio/pci/ism/Kconfig  |  10 ++
 drivers/vfio/pci/ism/Makefile |   3 +
 drivers/vfio/pci/ism/main.c   | 401 ++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 418 insertions(+)

--- snip ---

Quoted 5 lines
drivers/vfio/pci/ism/main.c new file +2 -0
@@ -0,0 +1,401 @@
+// SPDX-License-Identifier: GPL-2.0
+/*

drivers/vfio/pci/ism/main.c line 12

Quoted 10 lines
drivers/vfio/pci/ism/main.c new file +10 -0
+ * vfio-ISM driver for s390
+ *
+ * Copyright IBM Corp.
+ */
+
+#include "../vfio_pci_priv.h"
+#include "linux/slab.h"
+
+#define ISM_VFIO_PCI_OFFSET_SHIFT   48
+#define ISM_VFIO_PCI_OFFSET_TO_INDEX(off) (off >> ISM_VFIO_PCI_OFFSET_SHIFT)

Nit: checkpatch.pl --strict recommends to add () around the off macro argument to prevent precedence issues.

Quoted 7 lines
drivers/vfio/pci/ism/main.c new file +7 -0
+#define ISM_VFIO_PCI_INDEX_TO_OFFSET(index) ((u64)(index) << ISM_VFIO_PCI_OFFSET_SHIFT)
+#define ISM_VFIO_PCI_OFFSET_MASK (((u64)(1) << ISM_VFIO_PCI_OFFSET_SHIFT) - 1)
+
+/*
+ * Use __zpci_load() to bypass automatic use of
+ * PCI MIO instructions which are not supported on ISM devices
+ */

drivers/vfio/pci/ism/main.c line 29

Quoted 10 lines
drivers/vfio/pci/ism/main.c new file +10 -0
+#define ISM_READ(size)                                                        \
+	static int ism_read##size(struct zpci_dev *zdev, int bar,             \
+				  ssize_t *filled, char __user *buf,          \
+				  loff_t off)                                 \
+	{                                                                     \
+		u64 req, tmp;                                                 \
+		u##size val;                                                  \
+		int ret;                                                      \
+									      \
+		req = ZPCI_CREATE_REQ(READ_ONCE(zdev->fh), bar, sizeof(val)); \

Not your fault since it is in the original ZPCI_CREATE_REQ() also but I think the READ_ONCE() is actually useless. The zdev->fh can indeed change under us in some error/recovery scenarios but then the PCI instructions would also just return a stale handle error and really the access isn't really at risk of getting torn either. Still, I'd keep this for consistency and then we can think of cleaning this up everywhere.

Quoted 5 lines
drivers/vfio/pci/ism/main.c new file +5 -0
+		ret = __zpci_load(&tmp, req, off);                            \
+		if (ret)                                                      \
+			return ret;                                           \
+		val = (u##size)tmp;                                           \
+		if (copy_to_user(buf, &val, sizeof(val)))                     \

drivers/vfio/pci/ism/main.c line 44

Quoted 10 lines
drivers/vfio/pci/ism/main.c new file +10 -0
+			return -EFAULT;                                       \
+		*filled = sizeof(val);                                        \
+		return 0;						      \
+	}
+
+ISM_READ(64);
+ISM_READ(32);
+ISM_READ(16);
+ISM_READ(8);
+

--- snip ---

Quoted 19 lines
drivers/vfio/pci/ism/main.c new file +19 -0
+
+static int ism_vfio_pci_init_dev(struct vfio_device *core_vdev)
+{
+	struct zpci_dev *zdev = to_zpci(to_pci_dev(core_vdev->dev));
+	struct ism_vfio_pci_core_device *ivpcd;
+	char cache_name[20];
+	int ret;
+
+	ivpcd = container_of(core_vdev, struct ism_vfio_pci_core_device,
+			     core_device.vdev);
+
+	snprintf(cache_name, sizeof(cache_name), "ism_sb_fid_%08x", zdev->fid);
+	ivpcd->store_block_cache =
+		kmem_cache_create(cache_name, zdev->maxstbl, PAGE_SIZE,
+				  (SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT), NULL);
+	if (!ivpcd->store_block_cache)
+		return -ENOMEM;
+
+	ret = vfio_pci_core_init_dev(core_vdev);

drivers/vfio/pci/ism/main.c line 322

Quoted 10 lines
drivers/vfio/pci/ism/main.c new file +10 -0
+	if (ret)
+		kmem_cache_destroy(ivpcd->store_block_cache);
+
+	return ret;
+}
+
+static void ism_vfio_pci_release_dev(struct vfio_device *core_vdev)
+{
+	struct ism_vfio_pci_core_device *ivpcd = container_of(
+		core_vdev, struct ism_vfio_pci_core_device, core_device.vdev);

Here checkpatch.pl --strict complains about '(' at the end of a line but clang-format also does it this way and even shortening the name doesn't make it fit so I'd tend to keep it this way.

drivers/vfio/pci/ism/main.c line 325

Quoted 3 lines
drivers/vfio/pci/ism/main.c new file +3 -0
+
+	kmem_cache_destroy(ivpcd->store_block_cache);
+	vfio_pci_core_release_dev(core_vdev);

As discussed offline. I think it may be a bug in the Xe variant driver that their xe_vfio_pci_release_dev() doesn't call vfio_pci_core_release_dev() even though xe_vfio_pci_init_dev() calls vfio_pci_core_init_dev(). @Alex, @Michal?

drivers/vfio/pci/ism/main.c line 78

Quoted 2 lines
drivers/vfio/pci/ism/main.c new file +2 -0
+}
+

--- snip ---

drivers/vfio/pci/ism/main.c line 401

Quoted 3 lines
drivers/vfio/pci/ism/main.c new file +3 -0
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("vfio-pci variant driver for the IBM Internal Shared Memory (ISM) device");
+MODULE_AUTHOR("IBM Corporation");

Apart from the code style nits this looks good to me now. Sorry for sending us on a few detours.

Feel free to add my:

Thanks,
Niklas

author maintainer
#6 lore raw

PATCH v7 3/3 MAINTAINERS: add VFIO ISM PCI DRIVER section

In reply to Julian Ruess

ism_vfio_pci is a new kernel component that allows to use the ISM device from userspace. Add myself as a maintainer.

---
 MAINTAINERS | 6 ++++++
 1 file changed, 6 insertions(+)
MAINTAINERS +6 -0
index 7d10988cbc62b77515aa1a1fee1c7d3e594869fa..4784a2514c0e057fded12364c680c928bde5876c 100644
@@ -27704,6 +27704,12 @@ L:	[email protected]
 S:	Maintained
 F:	drivers/vfio/pci/hisilicon/
 
+VFIO ISM PCI DRIVER
+M:	Julian Ruess <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/vfio/pci/ism/
+
 VFIO MEDIATED DEVICE DRIVERS
 M:	Kirti Wankhede <[email protected]>
 L:	[email protected]
Signature · 1 line
2.51.0