Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

s390/pci: Introduce pdev->non_mappable_bars and replace VFIO_PCI_MMAP

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/
Link: https://lore.kernel.org/r/20250226-vfio_pci_mmap-v7-2-c5c0f1d26efd@linux.ibm.com
Signed-off-by: Niklas Schnelle <schnelle@linux.ibm.com>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>

authored by

Niklas Schnelle and committed by
Bjorn Helgaas
888bd832 41a0926e

+11 -5
+1
arch/s390/pci/pci.c
··· 590 590 zpci_zdev_get(zdev); 591 591 if (pdev->is_physfn) 592 592 pdev->no_vf_scan = 1; 593 + pdev->non_mappable_bars = 1; 593 594 594 595 zpci_map_resources(pdev); 595 596
+4
drivers/pci/pci-sysfs.c
··· 1257 1257 int i; 1258 1258 int retval; 1259 1259 1260 + /* Skip devices with non-mappable BARs */ 1261 + if (pdev->non_mappable_bars) 1262 + return 0; 1263 + 1260 1264 /* Expose the PCI resources from this device as files */ 1261 1265 for (i = 0; i < PCI_STD_NUM_BARS; i++) { 1262 1266
+4
drivers/pci/proc.c
··· 251 251 security_locked_down(LOCKDOWN_PCI_ACCESS)) 252 252 return -EPERM; 253 253 254 + /* Skip devices with non-mappable BARs */ 255 + if (dev->non_mappable_bars) 256 + return -EINVAL; 257 + 254 258 if (fpriv->mmap_state == pci_mmap_io) { 255 259 if (!arch_can_pci_mmap_io()) 256 260 return -EINVAL;
-4
drivers/vfio/pci/Kconfig
··· 7 7 select VFIO_VIRQFD 8 8 select IRQ_BYPASS_MANAGER 9 9 10 - config VFIO_PCI_MMAP 11 - def_bool y if !S390 12 - depends on VFIO_PCI_CORE 13 - 14 10 config VFIO_PCI_INTX 15 11 def_bool y if !S390 16 12 depends on VFIO_PCI_CORE
+1 -1
drivers/vfio/pci/vfio_pci_core.c
··· 116 116 117 117 res = &vdev->pdev->resource[bar]; 118 118 119 - if (!IS_ENABLED(CONFIG_VFIO_PCI_MMAP)) 119 + if (vdev->pdev->non_mappable_bars) 120 120 goto no_mmap; 121 121 122 122 if (!(res->flags & IORESOURCE_MEM))
+1
include/linux/pci.h
··· 476 476 unsigned int no_command_memory:1; /* No PCI_COMMAND_MEMORY */ 477 477 unsigned int rom_bar_overlap:1; /* ROM BAR disable broken */ 478 478 unsigned int rom_attr_enabled:1; /* Display of ROM attribute enabled? */ 479 + unsigned int non_mappable_bars:1; /* BARs can't be mapped to user-space */ 479 480 pci_dev_flags_t dev_flags; 480 481 atomic_t enable_cnt; /* pci_enable_device has been called */ 481 482