public inbox for linux-s390@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v7 0/3] vfio/pci: s390: Fix issues preventing VFIO_PCI_MMAP=y for s390 and enable it
@ 2025-02-26 12:07 Niklas Schnelle
  2025-02-26 12:07 ` [PATCH v7 1/3] s390/pci: Fix s390_mmio_read/write syscall page fault handling Niklas Schnelle
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Niklas Schnelle @ 2025-02-26 12:07 UTC (permalink / raw)
  To: Bjorn Helgaas, Christoph Hellwig, Alexandra Winter,
	Alex Williamson, Gerd Bayer, Matthew Rosato, Jason Gunthorpe,
	Thorsten Winkler, Bjorn Helgaas
  Cc: Julian Ruess, Halil Pasic, Christian Borntraeger, Sven Schnelle,
	Gerald Schaefer, Heiko Carstens, Vasily Gorbik, Alexander Gordeev,
	linux-s390, linux-kernel, kvm, linux-pci, Niklas Schnelle

With the introduction of memory I/O (MIO) instructions enbaled in commit
71ba41c9b1d9 ("s390/pci: provide support for MIO instructions") s390
gained support for direct user-space access to mapped PCI resources.
Even without those however user-space can access mapped PCI resources
via the s390 specific MMIO syscalls. There is thus nothing fundamentally
preventing s390 from supporting VFIO_PCI_MMAP, allowing user-space
drivers to access PCI resources without going through the pread()
interface. To actually enable VFIO_PCI_MMAP a few issues need fixing
however.

Firstly the s390 MMIO syscalls do not cause a page fault when
follow_pte() fails due to the page not being present. This breaks
vfio-pci's mmap() handling which lazily maps on first access.

Secondly on s390 there is a virtual PCI device called ISM which has
a few oddities. For one it claims to have a 256 TiB PCI BAR (not a typo)
which leads to any attempt to mmap() it fail with the following message:

    vmap allocation for size 281474976714752 failed: use vmalloc=<size> to increase size

Even if one tried to map this BAR only partially the mapping would not
be usable on systems with MIO support enabled. So just block mapping
BARs which don't fit between IOREMAP_START and IOREMAP_END. Solve this
by keeping the vfio-pci mmap() blocking behavior around for this
specific device via a PCI quirk and new pdev->non_mappable_bars
flag.

As noted by Alex Williamson With mmap() enabled in vfio-pci it makes
sense to also enable HAVE_PCI_MMAP with the same restriction for pdev->
non_mappable_bars. So this is added in patch 3 and I tested this with
another small test program.

Note:
For your convenience the code is also available in the tagged
b4/vfio_pci_mmap branch on my git.kernel.org site below:
https://git.kernel.org/pub/scm/linux/kernel/git/niks/linux.git/

Thanks,
Niklas

Link: https://lore.kernel.org/all/c5ba134a1d4f4465b5956027e6a4ea6f6beff969.camel@linux.ibm.com/
Signed-off-by: Niklas Schnelle <schnelle@linux.ibm.com>
---
Changes in v7:
- Move all s390 changes, except for a one-lineer to set pdev->
  non_mappable_bars for all devices, to the third patch (Bjorn)
- Move checks in pci-sysfs.c and proc.c to the second patch (Bjorn)
- Only set ARCH_GENERIC_PCI_MMAP_RESOURCES not HAVE_PCI_MMAP following
  the recommendation for new architectures in
  Documentation/PCI/sysfs-pci.rst. This only enables the sysfs but not
  the proc interface.
- Link to v6: https://lore.kernel.org/r/20250214-vfio_pci_mmap-v6-0-6f300cb63a7e@linux.ibm.com

Changes in v6:
- Add a patch to also enable PCI resource mmap() via sysfs and proc
  exlcluding pdev->non_mappable_bars devices (Alex Williamson)
- Added Acks
- Link to v5: https://lore.kernel.org/r/20250212-vfio_pci_mmap-v5-0-633ca5e056da@linux.ibm.com

Changes in v5:
- Instead of relying on the existing pdev->non_compliant_bars introduce
  a new pdev->non_mappable_bars flag. This replaces the VFIO_PCI_MMAP
  Kconfig option and makes it per-device. This is necessary to not break
  upcoming vfio-pci use of ISM devices (Julian Ruess)
- Squash the removal of VFIO_PCI_MMAP into the second commit as this
  is now where its only use goes away.
- Switch to using follow_pfnmap_start() in MMIO syscall page fault
  handling to match upstream changes
- Dropped R-b's because the changes are significant
- Link to v4: https://lore.kernel.org/r/20240626-vfio_pci_mmap-v4-0-7f038870f022@linux.ibm.com

Changes in v4:
- Overhauled and split up patch 2 which caused errors on ppc due to
  unexported __kernel_io_end. Replaced it with a minimal s390 PCI fixup
  harness to set pdev->non_compliant_bars for ISM plus ignoring devices
  with this flag in vfio-pci. Idea for using PCI quirks came from
  Christoph Hellwig, thanks. Dropped R-bs for patch 2 accordingly.
- Rebased on v6.10-rc5 which includes the vfio-pci mmap fault handler
  fix to the issue I stumbled over independently in v3
- Link to v3: https://lore.kernel.org/r/20240529-vfio_pci_mmap-v3-0-cd217d019218@linux.ibm.com

Changes in v3:
- Rebased on v6.10-rc1 requiring change to follow_pte() call
- Use current->mm for fixup_user_fault() as seems more common
- Collected new trailers
- Link to v2: https://lore.kernel.org/r/20240523-vfio_pci_mmap-v2-0-0dc6c139a4f1@linux.ibm.com

Changes in v2:
- Changed last patch to remove VFIO_PCI_MMAP instead of just enabling it
  for s390 as it is unconditionally true with s390 supporting PCI resource mmap() (Jason)
- Collected R-bs from Jason
- Link to v1: https://lore.kernel.org/r/20240521-vfio_pci_mmap-v1-0-2f6315e0054e@linux.ibm.com

---
Niklas Schnelle (3):
      s390/pci: Fix s390_mmio_read/write syscall page fault handling
      PCI: s390: Introduce pdev->non_mappable_bars and replace VFIO_PCI_MMAP
      PCI: s390: Support mmap() of PCI resources except for ISM devices

 arch/s390/Kconfig                |  4 +---
 arch/s390/include/asm/pci.h      |  3 +++
 arch/s390/pci/Makefile           |  2 +-
 arch/s390/pci/pci_fixup.c        | 23 +++++++++++++++++++++++
 arch/s390/pci/pci_mmio.c         | 18 +++++++++++++-----
 drivers/pci/pci-sysfs.c          |  4 ++++
 drivers/pci/proc.c               |  4 ++++
 drivers/s390/net/ism_drv.c       |  1 -
 drivers/vfio/pci/Kconfig         |  4 ----
 drivers/vfio/pci/vfio_pci_core.c |  2 +-
 include/linux/pci.h              |  1 +
 include/linux/pci_ids.h          |  1 +
 12 files changed, 52 insertions(+), 15 deletions(-)
---
base-commit: d082ecbc71e9e0bf49883ee4afd435a77a5101b6
change-id: 20240503-vfio_pci_mmap-1549e3d02ca7

Best regards,
-- 
Niklas Schnelle


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v7 1/3] s390/pci: Fix s390_mmio_read/write syscall page fault handling
  2025-02-26 12:07 [PATCH v7 0/3] vfio/pci: s390: Fix issues preventing VFIO_PCI_MMAP=y for s390 and enable it Niklas Schnelle
@ 2025-02-26 12:07 ` Niklas Schnelle
  2025-02-28 21:56   ` Matthew Rosato
  2025-02-26 12:07 ` [PATCH v7 2/3] PCI: s390: Introduce pdev->non_mappable_bars and replace VFIO_PCI_MMAP Niklas Schnelle
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 6+ messages in thread
From: Niklas Schnelle @ 2025-02-26 12:07 UTC (permalink / raw)
  To: Bjorn Helgaas, Christoph Hellwig, Alexandra Winter,
	Alex Williamson, Gerd Bayer, Matthew Rosato, Jason Gunthorpe,
	Thorsten Winkler, Bjorn Helgaas
  Cc: Julian Ruess, Halil Pasic, Christian Borntraeger, Sven Schnelle,
	Gerald Schaefer, Heiko Carstens, Vasily Gorbik, Alexander Gordeev,
	linux-s390, linux-kernel, kvm, linux-pci, Niklas Schnelle

The s390 MMIO syscalls when using the classic PCI instructions do not
cause a page fault when follow_pfnmap_start() fails due to the page not
being present. Besides being a general deficiency this breaks vfio-pci's
mmap() handling once VFIO_PCI_MMAP gets enabled as this lazily maps on
first access. Fix this by following a failed follow_pfnmap_start() with
fixup_user_page() and retrying the follow_pfnmap_start(). Also fix
a VM_READ vs VM_WRITE mixup in the read syscall.

Signed-off-by: Niklas Schnelle <schnelle@linux.ibm.com>
---
 arch/s390/pci/pci_mmio.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/arch/s390/pci/pci_mmio.c b/arch/s390/pci/pci_mmio.c
index 46f99dc164ade4ca10f170cd66bdb648f92aa904..1997d9b7965df3b9b6019c7537028cd29d52fc13 100644
--- a/arch/s390/pci/pci_mmio.c
+++ b/arch/s390/pci/pci_mmio.c
@@ -175,8 +175,12 @@ SYSCALL_DEFINE3(s390_pci_mmio_write, unsigned long, mmio_addr,
 	args.address = mmio_addr;
 	args.vma = vma;
 	ret = follow_pfnmap_start(&args);
-	if (ret)
-		goto out_unlock_mmap;
+	if (ret) {
+		fixup_user_fault(current->mm, mmio_addr, FAULT_FLAG_WRITE, NULL);
+		ret = follow_pfnmap_start(&args);
+		if (ret)
+			goto out_unlock_mmap;
+	}
 
 	io_addr = (void __iomem *)((args.pfn << PAGE_SHIFT) |
 			(mmio_addr & ~PAGE_MASK));
@@ -315,14 +319,18 @@ SYSCALL_DEFINE3(s390_pci_mmio_read, unsigned long, mmio_addr,
 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
 		goto out_unlock_mmap;
 	ret = -EACCES;
-	if (!(vma->vm_flags & VM_WRITE))
+	if (!(vma->vm_flags & VM_READ))
 		goto out_unlock_mmap;
 
 	args.vma = vma;
 	args.address = mmio_addr;
 	ret = follow_pfnmap_start(&args);
-	if (ret)
-		goto out_unlock_mmap;
+	if (ret) {
+		fixup_user_fault(current->mm, mmio_addr, 0, NULL);
+		ret = follow_pfnmap_start(&args);
+		if (ret)
+			goto out_unlock_mmap;
+	}
 
 	io_addr = (void __iomem *)((args.pfn << PAGE_SHIFT) |
 			(mmio_addr & ~PAGE_MASK));

-- 
2.45.2


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v7 2/3] PCI: s390: Introduce pdev->non_mappable_bars and replace VFIO_PCI_MMAP
  2025-02-26 12:07 [PATCH v7 0/3] vfio/pci: s390: Fix issues preventing VFIO_PCI_MMAP=y for s390 and enable it Niklas Schnelle
  2025-02-26 12:07 ` [PATCH v7 1/3] s390/pci: Fix s390_mmio_read/write syscall page fault handling Niklas Schnelle
@ 2025-02-26 12:07 ` Niklas Schnelle
  2025-02-26 12:07 ` [PATCH v7 3/3] PCI: s390: Support mmap() of PCI resources except for ISM devices Niklas Schnelle
  2025-03-04 22:59 ` [PATCH v7 0/3] vfio/pci: s390: Fix issues preventing VFIO_PCI_MMAP=y for s390 and enable it Bjorn Helgaas
  3 siblings, 0 replies; 6+ messages in thread
From: Niklas Schnelle @ 2025-02-26 12:07 UTC (permalink / raw)
  To: Bjorn Helgaas, Christoph Hellwig, Alexandra Winter,
	Alex Williamson, Gerd Bayer, Matthew Rosato, Jason Gunthorpe,
	Thorsten Winkler, Bjorn Helgaas
  Cc: Julian Ruess, Halil Pasic, Christian Borntraeger, Sven Schnelle,
	Gerald Schaefer, Heiko Carstens, Vasily Gorbik, Alexander Gordeev,
	linux-s390, linux-kernel, kvm, linux-pci, Niklas Schnelle

The ability to map PCI resources to user-space is controlled by global
defines. For vfio there is VFIO_PCI_MMAP which is only disabled on s390
and controls mapping of PCI resources using vfio-pci with a fallback
option via the pread()/pwrite() interface.

For the PCI core there is ARCH_GENERIC_PCI_MMAP_RESOURCE which enables
a generic implementation for mapping PCI resources plus the newer sysfs
interface. Then there is HAVE_PCI_MMAP which can be used with custom
definitions of pci_mmap_resource_range() and the historical
/proc/bus/pci interface. Both mechanisms are all or nothing.

For s390 mapping PCI resources is possible and useful for testing and
certain applications such as QEMU's vfio-pci based user-space NVMe
driver. For certain devices, however access to PCI resources via
mappings to user-space is not possible and these must be excluded from
the general PCI resource mapping mechanisms.

Introduce pdev->non_mappable_bars to indicate that a PCI device's BARs
can not be accessed via mappings to user-space. In the future this
enables per-device restrictions of PCI resource mapping.

For now, set this flag for all PCI devices on s390 in line with the
existing, general disable of PCI resource mapping. As s390 is the only
user of the VFI_PCI_MMAP Kconfig options this can already be replaced
with a check of this new flag. Also add similar checks in the other code
protected by HAVE_PCI_MMAP respectively ARCH_GENERIC_PCI_MMAP in
preparation for enabling these for supported devices.

Link: https://lore.kernel.org/lkml/20250212132808.08dcf03c.alex.williamson@redhat.com/
Signed-off-by: Niklas Schnelle <schnelle@linux.ibm.com>
---
 arch/s390/pci/pci.c              | 1 +
 drivers/pci/pci-sysfs.c          | 4 ++++
 drivers/pci/proc.c               | 4 ++++
 drivers/vfio/pci/Kconfig         | 4 ----
 drivers/vfio/pci/vfio_pci_core.c | 2 +-
 include/linux/pci.h              | 1 +
 6 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/arch/s390/pci/pci.c b/arch/s390/pci/pci.c
index 88f72745fa59e16df26b1563de27594aac954a78..d14b8605a32ce1bc132dff225ac433cf3aae9265 100644
--- a/arch/s390/pci/pci.c
+++ b/arch/s390/pci/pci.c
@@ -590,6 +590,7 @@ int pcibios_device_add(struct pci_dev *pdev)
 	zpci_zdev_get(zdev);
 	if (pdev->is_physfn)
 		pdev->no_vf_scan = 1;
+	pdev->non_mappable_bars = 1;
 
 	zpci_map_resources(pdev);
 
diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
index b46ce1a2c5542cdea0a3f9df324434fdb7e8a4d2..7373eca0a4943bf896b4a177124e0d4572baec2b 100644
--- a/drivers/pci/pci-sysfs.c
+++ b/drivers/pci/pci-sysfs.c
@@ -1257,6 +1257,10 @@ static int pci_create_resource_files(struct pci_dev *pdev)
 	int i;
 	int retval;
 
+	/* Skip devices with non-mappable BARs */
+	if (pdev->non_mappable_bars)
+		return 0;
+
 	/* Expose the PCI resources from this device as files */
 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
 
diff --git a/drivers/pci/proc.c b/drivers/pci/proc.c
index f967709082d654a101039091b5493b2dec5f57b4..9348a0fb808477ca9be80a8b88bbc036565bc411 100644
--- a/drivers/pci/proc.c
+++ b/drivers/pci/proc.c
@@ -251,6 +251,10 @@ static int proc_bus_pci_mmap(struct file *file, struct vm_area_struct *vma)
 	    security_locked_down(LOCKDOWN_PCI_ACCESS))
 		return -EPERM;
 
+	/* Skip devices with non-mappable BARs */
+	if (dev->non_mappable_bars)
+		return -EINVAL;
+
 	if (fpriv->mmap_state == pci_mmap_io) {
 		if (!arch_can_pci_mmap_io())
 			return -EINVAL;
diff --git a/drivers/vfio/pci/Kconfig b/drivers/vfio/pci/Kconfig
index bf50ffa10bdea9e52a9d01cc3d6ee4cade39a08c..c3bcb6911c538286f7985f9c5e938d587fc04b56 100644
--- a/drivers/vfio/pci/Kconfig
+++ b/drivers/vfio/pci/Kconfig
@@ -7,10 +7,6 @@ config VFIO_PCI_CORE
 	select VFIO_VIRQFD
 	select IRQ_BYPASS_MANAGER
 
-config VFIO_PCI_MMAP
-	def_bool y if !S390
-	depends on VFIO_PCI_CORE
-
 config VFIO_PCI_INTX
 	def_bool y if !S390
 	depends on VFIO_PCI_CORE
diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index 586e49efb81be32ccb50ca554a60cec684c37402..c8586d47704c74cf9a5256d65bbf888db72b2f91 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -116,7 +116,7 @@ static void vfio_pci_probe_mmaps(struct vfio_pci_core_device *vdev)
 
 		res = &vdev->pdev->resource[bar];
 
-		if (!IS_ENABLED(CONFIG_VFIO_PCI_MMAP))
+		if (vdev->pdev->non_mappable_bars)
 			goto no_mmap;
 
 		if (!(res->flags & IORESOURCE_MEM))
diff --git a/include/linux/pci.h b/include/linux/pci.h
index 47b31ad724fa5bf7abd7c3dc572947551b0f2148..7192b9d78d7e337ce6144190325458fe3c0f1696 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -476,6 +476,7 @@ struct pci_dev {
 	unsigned int	no_command_memory:1;	/* No PCI_COMMAND_MEMORY */
 	unsigned int	rom_bar_overlap:1;	/* ROM BAR disable broken */
 	unsigned int	rom_attr_enabled:1;	/* Display of ROM attribute enabled? */
+	unsigned int	non_mappable_bars:1;	/* BARs can't be mapped to user-space  */
 	pci_dev_flags_t dev_flags;
 	atomic_t	enable_cnt;	/* pci_enable_device has been called */
 

-- 
2.45.2


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v7 3/3] PCI: s390: Support mmap() of PCI resources except for ISM devices
  2025-02-26 12:07 [PATCH v7 0/3] vfio/pci: s390: Fix issues preventing VFIO_PCI_MMAP=y for s390 and enable it Niklas Schnelle
  2025-02-26 12:07 ` [PATCH v7 1/3] s390/pci: Fix s390_mmio_read/write syscall page fault handling Niklas Schnelle
  2025-02-26 12:07 ` [PATCH v7 2/3] PCI: s390: Introduce pdev->non_mappable_bars and replace VFIO_PCI_MMAP Niklas Schnelle
@ 2025-02-26 12:07 ` Niklas Schnelle
  2025-03-04 22:59 ` [PATCH v7 0/3] vfio/pci: s390: Fix issues preventing VFIO_PCI_MMAP=y for s390 and enable it Bjorn Helgaas
  3 siblings, 0 replies; 6+ messages in thread
From: Niklas Schnelle @ 2025-02-26 12:07 UTC (permalink / raw)
  To: Bjorn Helgaas, Christoph Hellwig, Alexandra Winter,
	Alex Williamson, Gerd Bayer, Matthew Rosato, Jason Gunthorpe,
	Thorsten Winkler, Bjorn Helgaas
  Cc: Julian Ruess, Halil Pasic, Christian Borntraeger, Sven Schnelle,
	Gerald Schaefer, Heiko Carstens, Vasily Gorbik, Alexander Gordeev,
	linux-s390, linux-kernel, kvm, linux-pci, Niklas Schnelle

So far s390 does not allow mmap() of PCI resources to user-space via the
usual mechanisms, though it does use it for RDMA. For the PCI sysfs
resource files and /proc/bus/pci it defines neither HAVE_PCI_MMAP nor
ARCH_GENERIC_PCI_MMAP_RESOURCE. For vfio-pci s390 previously relied on
disabled VFIO_PCI_MMAP and now relies on setting pdev->non_mappable_bars
for all devices.

This is partly because access to mapped PCI resources from user-space
requires special PCI load/store memory-I/O (MIO) instructions, or the
special MMIO syscalls when these are not available. Still, such access
is possible and useful not just for RDMA, in fact not being able to
mmap() PCI resources has previously caused extra work when testing
devices.

One thing that doesn't work with PCI resources mapped to user-space
though is the s390 specific virtual ISM device. Not only because the BAR
size of 256 TiB prevents mapping the whole BAR but also because access
requires use of the legacy PCI instructions which are not accessible to
user-space on systems with the newer MIO PCI instructions.

Now with the pdev->non_mappable_bars flag ISM can be excluded from
mapping its resources while making this functionality available for all
other PCI devices. To this end introduce a minimal implementation of
PCI_QUIRKS and use that to set pdev->non_mappable_bars for ISM devices
only. Then also set ARCH_GENERIC_PCI_MMAP_RESOURCE to take advantage of
the generic implementation of pci_mmap_resource_range() enabling only
the newer sysfs mmap() interface. This follows the recommendation in
Documentation/PCI/sysfs-pci.rst.

Signed-off-by: Niklas Schnelle <schnelle@linux.ibm.com>
---
 arch/s390/Kconfig           |  4 +---
 arch/s390/include/asm/pci.h |  3 +++
 arch/s390/pci/Makefile      |  2 +-
 arch/s390/pci/pci.c         |  1 -
 arch/s390/pci/pci_fixup.c   | 23 +++++++++++++++++++++++
 drivers/s390/net/ism_drv.c  |  1 -
 include/linux/pci_ids.h     |  1 +
 7 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/arch/s390/Kconfig b/arch/s390/Kconfig
index 9c9ec08d78c71b4d227beeafab1b82d6434cb5c7..e48741e001476f765e8aba0037a1b386df393683 100644
--- a/arch/s390/Kconfig
+++ b/arch/s390/Kconfig
@@ -41,9 +41,6 @@ config AUDIT_ARCH
 config NO_IOPORT_MAP
 	def_bool y
 
-config PCI_QUIRKS
-	def_bool n
-
 config ARCH_SUPPORTS_UPROBES
 	def_bool y
 
@@ -258,6 +255,7 @@ config S390
 	select PCI_DOMAINS		if PCI
 	select PCI_MSI			if PCI
 	select PCI_MSI_ARCH_FALLBACKS	if PCI_MSI
+	select PCI_QUIRKS		if PCI
 	select SPARSE_IRQ
 	select SWIOTLB
 	select SYSCTL_EXCEPTION_TRACE
diff --git a/arch/s390/include/asm/pci.h b/arch/s390/include/asm/pci.h
index 474e1f8d1d3c2fc5685b459cc68b67ac651ea3e9..d2086af3434c0206fe306089fb17b778f8d17bbe 100644
--- a/arch/s390/include/asm/pci.h
+++ b/arch/s390/include/asm/pci.h
@@ -11,6 +11,9 @@
 #include <asm/pci_insn.h>
 #include <asm/sclp.h>
 
+#define ARCH_GENERIC_PCI_MMAP_RESOURCE	1
+#define arch_can_pci_mmap_wc()		1
+
 #define PCIBIOS_MIN_IO		0x1000
 #define PCIBIOS_MIN_MEM		0x10000000
 
diff --git a/arch/s390/pci/Makefile b/arch/s390/pci/Makefile
index df73c5182990ad3ae4ed5a785953011feb9a093c..1810e0944a4ed9d31261788f0f6eb341e5316546 100644
--- a/arch/s390/pci/Makefile
+++ b/arch/s390/pci/Makefile
@@ -5,6 +5,6 @@
 
 obj-$(CONFIG_PCI)	+= pci.o pci_irq.o pci_clp.o \
 			   pci_event.o pci_debug.o pci_insn.o pci_mmio.o \
-			   pci_bus.o pci_kvm_hook.o pci_report.o
+			   pci_bus.o pci_kvm_hook.o pci_report.o pci_fixup.o
 obj-$(CONFIG_PCI_IOV)	+= pci_iov.o
 obj-$(CONFIG_SYSFS)	+= pci_sysfs.o
diff --git a/arch/s390/pci/pci.c b/arch/s390/pci/pci.c
index d14b8605a32ce1bc132dff225ac433cf3aae9265..88f72745fa59e16df26b1563de27594aac954a78 100644
--- a/arch/s390/pci/pci.c
+++ b/arch/s390/pci/pci.c
@@ -590,7 +590,6 @@ int pcibios_device_add(struct pci_dev *pdev)
 	zpci_zdev_get(zdev);
 	if (pdev->is_physfn)
 		pdev->no_vf_scan = 1;
-	pdev->non_mappable_bars = 1;
 
 	zpci_map_resources(pdev);
 
diff --git a/arch/s390/pci/pci_fixup.c b/arch/s390/pci/pci_fixup.c
new file mode 100644
index 0000000000000000000000000000000000000000..35688b645098329f082d0c40cc8c59231c390eaa
--- /dev/null
+++ b/arch/s390/pci/pci_fixup.c
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Exceptions for specific devices,
+ *
+ * Copyright IBM Corp. 2025
+ *
+ * Author(s):
+ *   Niklas Schnelle <schnelle@linux.ibm.com>
+ */
+#include <linux/pci.h>
+
+static void zpci_ism_bar_no_mmap(struct pci_dev *pdev)
+{
+	/*
+	 * ISM's BAR is special. Drivers written for ISM know
+	 * how to handle this but others need to be aware of their
+	 * special nature e.g. to prevent attempts to mmap() it.
+	 */
+	pdev->non_mappable_bars = 1;
+}
+DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_IBM,
+			PCI_DEVICE_ID_IBM_ISM,
+			zpci_ism_bar_no_mmap);
diff --git a/drivers/s390/net/ism_drv.c b/drivers/s390/net/ism_drv.c
index 2f34761e64135c154b5193f870c27753481feed0..60ed70a39d2ccade0dcd73207a30551f4bd1203a 100644
--- a/drivers/s390/net/ism_drv.c
+++ b/drivers/s390/net/ism_drv.c
@@ -20,7 +20,6 @@
 MODULE_DESCRIPTION("ISM driver for s390");
 MODULE_LICENSE("GPL");
 
-#define PCI_DEVICE_ID_IBM_ISM 0x04ED
 #define DRV_NAME "ism"
 
 static const struct pci_device_id ism_device_table[] = {
diff --git a/include/linux/pci_ids.h b/include/linux/pci_ids.h
index 1a2594a38199fe77cbcabcfa57dc407fc66ddb44..3efb45beabf8ad46d1c28053fedb776b5027ddef 100644
--- a/include/linux/pci_ids.h
+++ b/include/linux/pci_ids.h
@@ -518,6 +518,7 @@
 #define PCI_DEVICE_ID_IBM_ICOM_V2_ONE_PORT_RVX_ONE_PORT_MDM	0x0251
 #define PCI_DEVICE_ID_IBM_ICOM_V2_ONE_PORT_RVX_ONE_PORT_MDM_PCIE 0x0361
 #define PCI_DEVICE_ID_IBM_ICOM_FOUR_PORT_MODEL	0x252
+#define PCI_DEVICE_ID_IBM_ISM		0x04ed
 
 #define PCI_SUBVENDOR_ID_IBM		0x1014
 #define PCI_SUBDEVICE_ID_IBM_SATURN_SERIAL_ONE_PORT	0x03d4

-- 
2.45.2


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v7 1/3] s390/pci: Fix s390_mmio_read/write syscall page fault handling
  2025-02-26 12:07 ` [PATCH v7 1/3] s390/pci: Fix s390_mmio_read/write syscall page fault handling Niklas Schnelle
@ 2025-02-28 21:56   ` Matthew Rosato
  0 siblings, 0 replies; 6+ messages in thread
From: Matthew Rosato @ 2025-02-28 21:56 UTC (permalink / raw)
  To: Niklas Schnelle, Bjorn Helgaas, Christoph Hellwig,
	Alexandra Winter, Alex Williamson, Gerd Bayer, Jason Gunthorpe,
	Thorsten Winkler, Bjorn Helgaas
  Cc: Julian Ruess, Halil Pasic, Christian Borntraeger, Sven Schnelle,
	Gerald Schaefer, Heiko Carstens, Vasily Gorbik, Alexander Gordeev,
	linux-s390, linux-kernel, kvm, linux-pci

On 2/26/25 7:07 AM, Niklas Schnelle wrote:
> The s390 MMIO syscalls when using the classic PCI instructions do not
> cause a page fault when follow_pfnmap_start() fails due to the page not
> being present. Besides being a general deficiency this breaks vfio-pci's
> mmap() handling once VFIO_PCI_MMAP gets enabled as this lazily maps on
> first access. Fix this by following a failed follow_pfnmap_start() with
> fixup_user_page() and retrying the follow_pfnmap_start(). Also fix
> a VM_READ vs VM_WRITE mixup in the read syscall.
> 
> Signed-off-by: Niklas Schnelle <schnelle@linux.ibm.com>

Reviewed-by: Matthew Rosato <mjrosato@linux.ibm.com>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v7 0/3] vfio/pci: s390: Fix issues preventing VFIO_PCI_MMAP=y for s390 and enable it
  2025-02-26 12:07 [PATCH v7 0/3] vfio/pci: s390: Fix issues preventing VFIO_PCI_MMAP=y for s390 and enable it Niklas Schnelle
                   ` (2 preceding siblings ...)
  2025-02-26 12:07 ` [PATCH v7 3/3] PCI: s390: Support mmap() of PCI resources except for ISM devices Niklas Schnelle
@ 2025-03-04 22:59 ` Bjorn Helgaas
  3 siblings, 0 replies; 6+ messages in thread
From: Bjorn Helgaas @ 2025-03-04 22:59 UTC (permalink / raw)
  To: Niklas Schnelle
  Cc: Christoph Hellwig, Alexandra Winter, Alex Williamson, Gerd Bayer,
	Matthew Rosato, Jason Gunthorpe, Thorsten Winkler, Bjorn Helgaas,
	Julian Ruess, Halil Pasic, Christian Borntraeger, Sven Schnelle,
	Gerald Schaefer, Heiko Carstens, Vasily Gorbik, Alexander Gordeev,
	linux-s390, linux-kernel, kvm, linux-pci

On Wed, Feb 26, 2025 at 01:07:44PM +0100, Niklas Schnelle wrote:
> With the introduction of memory I/O (MIO) instructions enbaled in commit
> 71ba41c9b1d9 ("s390/pci: provide support for MIO instructions") s390
> gained support for direct user-space access to mapped PCI resources.
> Even without those however user-space can access mapped PCI resources
> via the s390 specific MMIO syscalls. There is thus nothing fundamentally
> preventing s390 from supporting VFIO_PCI_MMAP, allowing user-space
> drivers to access PCI resources without going through the pread()
> interface. To actually enable VFIO_PCI_MMAP a few issues need fixing
> however.
> 
> Firstly the s390 MMIO syscalls do not cause a page fault when
> follow_pte() fails due to the page not being present. This breaks
> vfio-pci's mmap() handling which lazily maps on first access.
> 
> Secondly on s390 there is a virtual PCI device called ISM which has
> a few oddities. For one it claims to have a 256 TiB PCI BAR (not a typo)
> which leads to any attempt to mmap() it fail with the following message:
> 
>     vmap allocation for size 281474976714752 failed: use vmalloc=<size> to increase size
> 
> Even if one tried to map this BAR only partially the mapping would not
> be usable on systems with MIO support enabled. So just block mapping
> BARs which don't fit between IOREMAP_START and IOREMAP_END. Solve this
> by keeping the vfio-pci mmap() blocking behavior around for this
> specific device via a PCI quirk and new pdev->non_mappable_bars
> flag.
> 
> As noted by Alex Williamson With mmap() enabled in vfio-pci it makes
> sense to also enable HAVE_PCI_MMAP with the same restriction for pdev->
> non_mappable_bars. So this is added in patch 3 and I tested this with
> another small test program.
> 
> Note:
> For your convenience the code is also available in the tagged
> b4/vfio_pci_mmap branch on my git.kernel.org site below:
> https://git.kernel.org/pub/scm/linux/kernel/git/niks/linux.git/
> 
> Thanks,
> Niklas
> 
> Link: https://lore.kernel.org/all/c5ba134a1d4f4465b5956027e6a4ea6f6beff969.camel@linux.ibm.com/
> Signed-off-by: Niklas Schnelle <schnelle@linux.ibm.com>
> ---
> Changes in v7:
> - Move all s390 changes, except for a one-lineer to set pdev->
>   non_mappable_bars for all devices, to the third patch (Bjorn)
> - Move checks in pci-sysfs.c and proc.c to the second patch (Bjorn)
> - Only set ARCH_GENERIC_PCI_MMAP_RESOURCES not HAVE_PCI_MMAP following
>   the recommendation for new architectures in
>   Documentation/PCI/sysfs-pci.rst. This only enables the sysfs but not
>   the proc interface.
> - Link to v6: https://lore.kernel.org/r/20250214-vfio_pci_mmap-v6-0-6f300cb63a7e@linux.ibm.com
> 
> Changes in v6:
> - Add a patch to also enable PCI resource mmap() via sysfs and proc
>   exlcluding pdev->non_mappable_bars devices (Alex Williamson)
> - Added Acks
> - Link to v5: https://lore.kernel.org/r/20250212-vfio_pci_mmap-v5-0-633ca5e056da@linux.ibm.com
> 
> Changes in v5:
> - Instead of relying on the existing pdev->non_compliant_bars introduce
>   a new pdev->non_mappable_bars flag. This replaces the VFIO_PCI_MMAP
>   Kconfig option and makes it per-device. This is necessary to not break
>   upcoming vfio-pci use of ISM devices (Julian Ruess)
> - Squash the removal of VFIO_PCI_MMAP into the second commit as this
>   is now where its only use goes away.
> - Switch to using follow_pfnmap_start() in MMIO syscall page fault
>   handling to match upstream changes
> - Dropped R-b's because the changes are significant
> - Link to v4: https://lore.kernel.org/r/20240626-vfio_pci_mmap-v4-0-7f038870f022@linux.ibm.com
> 
> Changes in v4:
> - Overhauled and split up patch 2 which caused errors on ppc due to
>   unexported __kernel_io_end. Replaced it with a minimal s390 PCI fixup
>   harness to set pdev->non_compliant_bars for ISM plus ignoring devices
>   with this flag in vfio-pci. Idea for using PCI quirks came from
>   Christoph Hellwig, thanks. Dropped R-bs for patch 2 accordingly.
> - Rebased on v6.10-rc5 which includes the vfio-pci mmap fault handler
>   fix to the issue I stumbled over independently in v3
> - Link to v3: https://lore.kernel.org/r/20240529-vfio_pci_mmap-v3-0-cd217d019218@linux.ibm.com
> 
> Changes in v3:
> - Rebased on v6.10-rc1 requiring change to follow_pte() call
> - Use current->mm for fixup_user_fault() as seems more common
> - Collected new trailers
> - Link to v2: https://lore.kernel.org/r/20240523-vfio_pci_mmap-v2-0-0dc6c139a4f1@linux.ibm.com
> 
> Changes in v2:
> - Changed last patch to remove VFIO_PCI_MMAP instead of just enabling it
>   for s390 as it is unconditionally true with s390 supporting PCI resource mmap() (Jason)
> - Collected R-bs from Jason
> - Link to v1: https://lore.kernel.org/r/20240521-vfio_pci_mmap-v1-0-2f6315e0054e@linux.ibm.com
> 
> ---
> Niklas Schnelle (3):
>       s390/pci: Fix s390_mmio_read/write syscall page fault handling
>       PCI: s390: Introduce pdev->non_mappable_bars and replace VFIO_PCI_MMAP
>       PCI: s390: Support mmap() of PCI resources except for ISM devices
> 
>  arch/s390/Kconfig                |  4 +---
>  arch/s390/include/asm/pci.h      |  3 +++
>  arch/s390/pci/Makefile           |  2 +-
>  arch/s390/pci/pci_fixup.c        | 23 +++++++++++++++++++++++
>  arch/s390/pci/pci_mmio.c         | 18 +++++++++++++-----
>  drivers/pci/pci-sysfs.c          |  4 ++++
>  drivers/pci/proc.c               |  4 ++++
>  drivers/s390/net/ism_drv.c       |  1 -
>  drivers/vfio/pci/Kconfig         |  4 ----
>  drivers/vfio/pci/vfio_pci_core.c |  2 +-
>  include/linux/pci.h              |  1 +
>  include/linux/pci_ids.h          |  1 +
>  12 files changed, 52 insertions(+), 15 deletions(-)

Applied to pci/resource for v6.15, thanks!

I updated the subject lines to all start with "s390/pci" since that's
where all the interesting bits are and there's only a single instance
of "PCI: s390" in the history.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-03-04 22:59 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-26 12:07 [PATCH v7 0/3] vfio/pci: s390: Fix issues preventing VFIO_PCI_MMAP=y for s390 and enable it Niklas Schnelle
2025-02-26 12:07 ` [PATCH v7 1/3] s390/pci: Fix s390_mmio_read/write syscall page fault handling Niklas Schnelle
2025-02-28 21:56   ` Matthew Rosato
2025-02-26 12:07 ` [PATCH v7 2/3] PCI: s390: Introduce pdev->non_mappable_bars and replace VFIO_PCI_MMAP Niklas Schnelle
2025-02-26 12:07 ` [PATCH v7 3/3] PCI: s390: Support mmap() of PCI resources except for ISM devices Niklas Schnelle
2025-03-04 22:59 ` [PATCH v7 0/3] vfio/pci: s390: Fix issues preventing VFIO_PCI_MMAP=y for s390 and enable it Bjorn Helgaas

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox