vfio/pci: s390: Fix issues preventing VFIO_PCI_MMAP=y for s390 and enable it
Subsystems (7)
Niklas Schnelle
maintainerv7 cover vfio/pci: s390: Fix issues preventing VFIO_PCI_MMAP=y for s390 and enable it
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
---
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/[email protected]
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/[email protected]
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/[email protected]
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/[email protected]
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/[email protected]
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/[email protected]
---
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,
Signature · 1 line
Niklas Schnelle
Niklas Schnelle
maintainerPATCH v7 1/3 s390/pci: Fix s390_mmio_read/write syscall page fault handling
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.
--- arch/s390/pci/pci_mmio.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-)
arch/s390/pci/pci_mmio.c +13 -5
@@ -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 ));
Signature · 1 line
2.45.2
Re: [PATCH v7 1/3] s390/pci: Fix s390_mmio_read/write syscall page fault handling
Quoted 9 lines · 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'smmap()handling onceVFIO_PCI_MMAPgets enabled as this lazily maps on first access. Fix this by following a failedfollow_pfnmap_start()withfixup_user_page()and retrying thefollow_pfnmap_start(). Also fix aVM_READvsVM_WRITEmixup in the read syscall.Signed-off-by Niklas Schnelle <[email protected]>
Niklas Schnelle
maintainerPATCH v7 2/3 PCI: s390: Introduce pdev->non_mappable_bars and replace VFIO_PCI_MMAP
In reply to Niklas SchnelleThe 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.
--- 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(-)
arch/s390/pci/pci.c +1 -0
@@ -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);
drivers/pci/pci-sysfs.c +4 -0
@@ -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++) {
drivers/pci/proc.c +4 -0
@@ -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 ;
drivers/vfio/pci/Kconfig +0 -4
@@ -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
drivers/vfio/pci/vfio_pci_core.c +1 -1
@@ -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 ))
include/linux/pci.h +1 -0
@@ -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 */
Signature · 1 line
2.45.2
Niklas Schnelle
maintainerPATCH v7 3/3 PCI: s390: Support mmap() of PCI resources except for ISM devices
In reply to Niklas SchnelleSo 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.
--- 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(-)
arch/s390/Kconfig +1 -3
@@ -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
arch/s390/include/asm/pci.h +3 -0
@@ -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
arch/s390/pci/Makefile +1 -1
@@ -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
arch/s390/pci/pci.c +0 -1
@@ -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);
arch/s390/pci/pci_fixup.c new file +23 -0
@@ -0,0 +1,23 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Exceptions for specific devices, +* +* Copyright IBM Corp. 2025 +* +* Author(s): +* Niklas Schnelle <[email protected]> +*/ +#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);
drivers/s390/net/ism_drv.c +0 -1
@@ -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[] = {
include/linux/pci_ids.h +1 -0
@@ -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
Signature · 1 line
2.45.2
Bjorn Helgaas
AppliedQuoted 99 lines · 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 supportingVFIO_PCI_MMAP, allowing user-space drivers to access PCI resources without going through thepread()interface. To actually enableVFIO_PCI_MMAPa 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'smmap()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_STARTandIOREMAP_END. Solve this by keeping the vfio-pcimmap()blocking behavior around for this specific device via a PCI quirk and newpdev->non_mappable_barsflag.As noted by Alex Williamson With
mmap()enabled in vfio-pci it makes sense to also enableHAVE_PCI_MMAPwith 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,
NiklasSigned-off-by Niklas Schnelle <[email protected]>---
Changes in v7:
- Move all s390 changes, except for a one-lineer to set pdev->non_mappable_barsfor all devices, to the third patch (Bjorn)
- Move checks inpci-sysfs.candproc.cto the second patch (Bjorn)
- Only setARCH_GENERIC_PCI_MMAP_RESOURCESnotHAVE_PCI_MMAPfollowing
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/[email protected]Changes in v6:
- Add a patch to also enable PCI resourcemmap()via sysfs and proc
exlcludingpdev->non_mappable_barsdevices (Alex Williamson)
- Added Acks
- Link to v5: https://lore.kernel.org/r/[email protected]Changes in v5:
- Instead of relying on the existingpdev->non_compliant_barsintroduce
a newpdev->non_mappable_barsflag. This replaces theVFIO_PCI_MMAP
Kconfigoption and makes it per-device. This is necessary to not break upcoming vfio-pci use of ISM devices (Julian Ruess)
- Squash the removal ofVFIO_PCI_MMAPinto the second commit as this
is now where its only use goes away.
- Switch to usingfollow_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/[email protected]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 setpdev->non_compliant_barsfor 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/[email protected]Changes in v3:
- Rebased on v6.10-rc1 requiring change tofollow_pte()call
- Usecurrent->mmforfixup_user_fault()as seems more common
- Collected new trailers
- Link to v2: https://lore.kernel.org/r/[email protected]Changes in v2:
- Changed last patch to removeVFIO_PCI_MMAPinstead of just enabling it
for s390 as it is unconditionally true with s390 supporting PCI resourcemmap()(Jason)
- Collected R-bs from Jason
- Link to v1: https://lore.kernel.org/r/[email protected]---
Niklas Schnelle (3):
s390/pci: Fix s390_mmio_read/write syscall page fault handling
PCI: s390: Introducepdev->non_mappable_barsand replaceVFIO_PCI_MMAP
PCI: s390: Supportmmap()of PCI resources except for ISM devices
Quoted 13 lines
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.