2024-05-21 12:16:51

by Niklas Schnelle

[permalink] [raw]
Subject: [PATCH 0/3] 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.

Note:
For your convenience the code is also available in the tagged and signed
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/[email protected]/
Signed-off-by: Niklas Schnelle <[email protected]>
---
Niklas Schnelle (3):
s390/pci: Fix s390_mmio_read/write syscall page fault handling
vfio/pci: Tolerate oversized BARs by disallowing mmap
vfio/pci: Enable VFIO_PCI_MMAP for s390

arch/s390/pci/pci_mmio.c | 18 +++++++++++++-----
drivers/vfio/pci/Kconfig | 2 +-
drivers/vfio/pci/vfio_pci_core.c | 8 ++++++--
3 files changed, 20 insertions(+), 8 deletions(-)
---
base-commit: a38297e3fb012ddfa7ce0321a7e5a8daeb1872b6
change-id: 20240503-vfio_pci_mmap-1549e3d02ca7

Best regards,
--
Niklas Schnelle



2024-05-21 12:17:07

by Niklas Schnelle

[permalink] [raw]
Subject: [PATCH 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_pte() 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_pte() with
fixup_user_page() and retrying the follow_pte().

Signed-off-by: Niklas Schnelle <[email protected]>
---
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 a90499c087f0..217defbcb4f1 100644
--- a/arch/s390/pci/pci_mmio.c
+++ b/arch/s390/pci/pci_mmio.c
@@ -170,8 +170,12 @@ SYSCALL_DEFINE3(s390_pci_mmio_write, unsigned long, mmio_addr,
goto out_unlock_mmap;

ret = follow_pte(vma->vm_mm, mmio_addr, &ptep, &ptl);
- if (ret)
- goto out_unlock_mmap;
+ if (ret) {
+ fixup_user_fault(vma->vm_mm, mmio_addr, FAULT_FLAG_WRITE, NULL);
+ ret = follow_pte(vma->vm_mm, mmio_addr, &ptep, &ptl);
+ if (ret)
+ goto out_unlock_mmap;
+ }

io_addr = (void __iomem *)((pte_pfn(*ptep) << PAGE_SHIFT) |
(mmio_addr & ~PAGE_MASK));
@@ -305,12 +309,16 @@ 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;

ret = follow_pte(vma->vm_mm, mmio_addr, &ptep, &ptl);
- if (ret)
- goto out_unlock_mmap;
+ if (ret) {
+ fixup_user_fault(vma->vm_mm, mmio_addr, 0, NULL);
+ ret = follow_pte(vma->vm_mm, mmio_addr, &ptep, &ptl);
+ if (ret)
+ goto out_unlock_mmap;
+ }

io_addr = (void __iomem *)((pte_pfn(*ptep) << PAGE_SHIFT) |
(mmio_addr & ~PAGE_MASK));

--
2.40.1


2024-05-21 12:17:21

by Niklas Schnelle

[permalink] [raw]
Subject: [PATCH 2/3] vfio/pci: Tolerate oversized BARs by disallowing mmap

On s390 there is a virtual PCI device called ISM which has a few rather
annoying oddities. For one it claims to have a 256 TiB PCI BAR (not
a typo) which leads to any attempt to mmap() it failing during vmap.

Even if one tried to map this "BAR" only partially the mapping would not
be usable on systems with MIO support enabled however. This is because
of another oddity in that this virtual PCI device does not support the
newer memory I/O (MIO) PCI instructions and legacy PCI instructions are
not accessible by user-space when MIO is in use. If this device needs to
be accessed by user-space it will thus need a vfio-pci variant driver.
Until then work around both issues by excluding resources which don't
fit between IOREMAP_START and IOREMAP_END in vfio_pci_probe_mmaps().

Signed-off-by: Niklas Schnelle <[email protected]>
---
drivers/vfio/pci/vfio_pci_core.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index d94d61b92c1a..23961299b695 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -28,6 +28,7 @@
#include <linux/nospec.h>
#include <linux/sched/mm.h>
#include <linux/iommufd.h>
+#include <linux/ioremap.h>
#if IS_ENABLED(CONFIG_EEH)
#include <asm/eeh.h>
#endif
@@ -129,9 +130,12 @@ static void vfio_pci_probe_mmaps(struct vfio_pci_core_device *vdev)
/*
* The PCI core shouldn't set up a resource with a
* type but zero size. But there may be bugs that
- * cause us to do that.
+ * cause us to do that. There is also at least one
+ * device which advertises a resource too large to
+ * ioremap().
*/
- if (!resource_size(res))
+ if (!resource_size(res) ||
+ resource_size(res) > (IOREMAP_END + 1 - IOREMAP_START))
goto no_mmap;

if (resource_size(res) >= PAGE_SIZE) {

--
2.40.1


2024-05-21 12:17:35

by Niklas Schnelle

[permalink] [raw]
Subject: [PATCH 3/3] vfio/pci: Enable VFIO_PCI_MMAP for s390

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. Thus VFIO_PCI_MMAP can be enabled
on all s390 systems with native PCI allowing vfio-pci user-space
applications direct access to mapped resources.

Link: https://lore.kernel.org/all/[email protected]/
Signed-off-by: Niklas Schnelle <[email protected]>
---
drivers/vfio/pci/Kconfig | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/vfio/pci/Kconfig b/drivers/vfio/pci/Kconfig
index 15821a2d77d2..814aa0941d61 100644
--- a/drivers/vfio/pci/Kconfig
+++ b/drivers/vfio/pci/Kconfig
@@ -8,7 +8,7 @@ config VFIO_PCI_CORE
select IRQ_BYPASS_MANAGER

config VFIO_PCI_MMAP
- def_bool y if !S390
+ def_bool y
depends on VFIO_PCI_CORE

config VFIO_PCI_INTX

--
2.40.1


2024-05-22 23:19:07

by Jason Gunthorpe

[permalink] [raw]
Subject: Re: [PATCH 1/3] s390/pci: Fix s390_mmio_read/write syscall page fault handling

On Tue, May 21, 2024 at 02:14:57PM +0200, Niklas Schnelle wrote:
> The s390 MMIO syscalls when using the classic PCI instructions do not
> cause a page fault when follow_pte() 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_pte() with
> fixup_user_page() and retrying the follow_pte().
>
> Signed-off-by: Niklas Schnelle <[email protected]>
> ---
> arch/s390/pci/pci_mmio.c | 18 +++++++++++++-----
> 1 file changed, 13 insertions(+), 5 deletions(-)

Reviewed-by: Jason Gunthorpe <[email protected]>

Jason

2024-05-22 23:19:50

by Jason Gunthorpe

[permalink] [raw]
Subject: Re: [PATCH 2/3] vfio/pci: Tolerate oversized BARs by disallowing mmap

On Tue, May 21, 2024 at 02:14:58PM +0200, Niklas Schnelle wrote:
> On s390 there is a virtual PCI device called ISM which has a few rather
> annoying oddities. For one it claims to have a 256 TiB PCI BAR (not
> a typo) which leads to any attempt to mmap() it failing during vmap.
>
> Even if one tried to map this "BAR" only partially the mapping would not
> be usable on systems with MIO support enabled however. This is because
> of another oddity in that this virtual PCI device does not support the
> newer memory I/O (MIO) PCI instructions and legacy PCI instructions are
> not accessible by user-space when MIO is in use. If this device needs to
> be accessed by user-space it will thus need a vfio-pci variant driver.
> Until then work around both issues by excluding resources which don't
> fit between IOREMAP_START and IOREMAP_END in vfio_pci_probe_mmaps().
>
> Signed-off-by: Niklas Schnelle <[email protected]>
> ---
> drivers/vfio/pci/vfio_pci_core.c | 8 ++++++--
> 1 file changed, 6 insertions(+), 2 deletions(-)

Reviewed-by: Jason Gunthorpe <[email protected]>

Jason

2024-05-22 23:20:38

by Jason Gunthorpe

[permalink] [raw]
Subject: Re: [PATCH 3/3] vfio/pci: Enable VFIO_PCI_MMAP for s390

On Tue, May 21, 2024 at 02:14:59PM +0200, 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. Thus VFIO_PCI_MMAP can be enabled
> on all s390 systems with native PCI allowing vfio-pci user-space
> applications direct access to mapped resources.
>
> Link: https://lore.kernel.org/all/[email protected]/
> Signed-off-by: Niklas Schnelle <[email protected]>
> ---
> drivers/vfio/pci/Kconfig | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/vfio/pci/Kconfig b/drivers/vfio/pci/Kconfig
> index 15821a2d77d2..814aa0941d61 100644
> --- a/drivers/vfio/pci/Kconfig
> +++ b/drivers/vfio/pci/Kconfig
> @@ -8,7 +8,7 @@ config VFIO_PCI_CORE
> select IRQ_BYPASS_MANAGER
>
> config VFIO_PCI_MMAP
> - def_bool y if !S390
> + def_bool y
> depends on VFIO_PCI_CORE

Should we just purge this kconfig entirely? It is never meaningfully n now?

Jason

2024-05-23 08:24:54

by Niklas Schnelle

[permalink] [raw]
Subject: Re: [PATCH 3/3] vfio/pci: Enable VFIO_PCI_MMAP for s390

On Wed, 2024-05-22 at 20:20 -0300, Jason Gunthorpe wrote:
> On Tue, May 21, 2024 at 02:14:59PM +0200, 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. Thus VFIO_PCI_MMAP can be enabled
> > on all s390 systems with native PCI allowing vfio-pci user-space
> > applications direct access to mapped resources.
> >
> > Link: https://lore.kernel.org/all/[email protected]/
> > Signed-off-by: Niklas Schnelle <[email protected]>
> > ---
> > drivers/vfio/pci/Kconfig | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/drivers/vfio/pci/Kconfig b/drivers/vfio/pci/Kconfig
> > index 15821a2d77d2..814aa0941d61 100644
> > --- a/drivers/vfio/pci/Kconfig
> > +++ b/drivers/vfio/pci/Kconfig
> > @@ -8,7 +8,7 @@ config VFIO_PCI_CORE
> > select IRQ_BYPASS_MANAGER
> >
> > config VFIO_PCI_MMAP
> > - def_bool y if !S390
> > + def_bool y
> > depends on VFIO_PCI_CORE
>
> Should we just purge this kconfig entirely? It is never meaningfully n now?
>
> Jason

Makes sense to me. Will change this for v2. I'll also add a Suggested-
by for you if that's okay, should probably have been in this version
already.

Thanks,
Niklas