resource: export iomem_get_mapping() for loadable modules
Subsystems (2)
PATCH 1/1 resource: export iomem_get_mapping() for loadable modules
Loadable PCIe driver modules that handle surprise removal or link-down events need to zap userspace mappings of PCI BAR resources to deliver SIGBUS on next access, rather than leaving stale mappings to a dead device.
The correct way to do this is via unmap_mapping_range() on the iomem address space, which is already exported via EXPORT_SYMBOL. However, iomem_get_mapping() which returns the iomem address space is not exported, making it impossible to use unmap_mapping_range() correctly from a loadable module without resorting to workarounds such as walking all process VMAs or opening /dev/mem.
Export iomem_get_mapping() via EXPORT_SYMBOL_GPL to complete the existing exported API and allow loadable modules to properly zap PCI BAR mappings on device removal.
--- kernel/resource.c | 1 + 1 file changed, 1 insertion(+)
kernel/resource.c +1 -0
@@ -1311,6 +1311,7 @@ struct address_space *iomem_get_mapping(void)*/ return smp_load_acquire (&iomem_inode)->i_mapping; } +EXPORT_SYMBOL_GPL (iomem_get_mapping); static int __request_region_locked(struct resource *res, struct resource *parent, resource_size_t start, resource_size_t n,
Signature · 1 line
2.47.3
Quoted 26 lines · Ravi Kumar Bandi
Loadable PCIe driver modules that handle surprise removal or link-down events need to zap userspace mappings of PCI BAR resources to deliver SIGBUS on next access, rather than leaving stale mappings to a dead device.
The correct way to do this is via
unmap_mapping_range()on the iomem address space, which is already exported viaEXPORT_SYMBOL. However,iomem_get_mapping()which returns the iomem address space is not exported, making it impossible to useunmap_mapping_range()correctly from a loadable module without resorting to workarounds such as walking all process VMAs or opening/dev/mem.Export
iomem_get_mapping()viaEXPORT_SYMBOL_GPLto complete the existing exported API and allow loadable modules to properly zap PCI BAR mappings on device removal.Signed-off-by Ravi Kumar Bandi <[email protected]>--- kernel/resource.c | 1 + 1 file changed, 1 insertion(+)kernel/resource.c +0
-0
kernel/resource.c struct address_space *iomem_get_mapping(void) · line 1317
Quoted 7 lines
kernel/resource.c +1
-0@@ -1311,6 +1311,7 @@ struct address_space *iomem_get_mapping(void)*/ return smp_load_acquire (&iomem_inode)->i_mapping; } +EXPORT_SYMBOL_GPL (iomem_get_mapping); static int __request_region_locked(struct resource *res, struct resource *parent, resource_size_t start, resource_size_t n,
Which in-tree driver wants to make use of this?
Signature · 3 lines
Cheers, David
Quoted 5 lines · David Hildenbrand (Arm)
Quoted 3 lines
static int __request_region_locked(struct resource *res, struct resource *parent,
resource_size_tstart,resource_size_tn,Which in-tree driver wants to make use of this?
And why?
Quoted 1 line · Christoph Hellwig
And why?
Quoted 1 line · David Hildenbrand
Which in-tree driver wants to make use of this?
Thank you both for reviewing the patch.
Currently this is needed by a PCIe endpoint driver that handles surprise removal on a device without a hot-plug pin routed to the CPU. The link-down event is detected by the in-tree Xilinx DMA PL PCIe controller driver (drivers/pci/controller/pcie-xilinx-dma-pl.c), which notifies the endpoint driver via the link-down callback.
When link-down is detected, the endpoint driver needs to zap existing userspace BAR mappings (mmap'd via sysfs resource files) to deliver SIGBUS rather than leaving stale mappings to a dead device, preventing kernel crashes on subsequent accesses.
unmap_mapping_range() is already EXPORT_SYMBOL'd for this purpose (mm/memory.c), but without iomem_get_mapping() being exported, loadable modules cannot use it correctly for PCI BAR mappings, forcing ugly workarounds such as walking all process VMAs.
Regards,
Ravi Kumar Bandi
Quoted 19 lines · Ravi Kumar Bandi
Quoted 1 line · Christoph Hellwig
And why?
Quoted 1 line · David Hildenbrand
Which in-tree driver wants to make use of this?
Thank you both for reviewing the patch.
Currently this is needed by a PCIe endpoint driver that handles surprise removal on a device without a hot-plug pin routed to the CPU. The link-down event is detected by the in-tree Xilinx DMA PL PCIe controller driver (
drivers/pci/controller/pcie-xilinx-dma-pl.c), which notifies the endpoint driver via the link-down callback.When link-down is detected, the endpoint driver needs to zap existing userspace BAR mappings (mmap'd via sysfs resource files) to deliver SIGBUS rather than leaving stale mappings to a dead device, preventing kernel crashes on subsequent accesses.
Quoted 4 lines
unmap_mapping_range()is already EXPORT_SYMBOL'd for this purpose (mm/memory.c), but withoutiomem_get_mapping()being exported, loadable modules cannot use it correctly for PCI BAR mappings, forcing ugly workarounds such as walking all process VMAs.
Okay, so you essentially want to implement something similar to revoke_iomem()?
Signature · 3 lines
Cheers, David
Quoted 10 lines · Ravi Kumar Bandi
Quoted 1 line · Christoph Hellwig
And why?
Quoted 1 line · David Hildenbrand
Which in-tree driver wants to make use of this?
Thank you both for reviewing the patch.
Currently this is needed by a PCIe endpoint driver that handles surprise removal on a device without a hot-plug pin routed to the CPU.
Which endpoint driver?
Quoted 4 lines
When link-down is detected, the endpoint driver needs to zap existing userspace BAR mappings (mmap'd via sysfs resource files) to deliver SIGBUS rather than leaving stale mappings to a dead device, preventing kernel crashes on subsequent accesses.
Why would the endpoint driver map iomem to userspace?
Quoted 4 lines
unmap_mapping_range()is already EXPORT_SYMBOL'd for this purpose (mm/memory.c), but withoutiomem_get_mapping()being exported, loadable modules cannot use it correctly for PCI BAR mappings, forcing ugly workarounds such as walking all process VMAs.
Well, we need to see the user(s) first, but once they materialize this begs for a higher level API. Until then it is moot anyway.
Quoted 1 line · Christoph Hellwig
Okay, so you essentially want to implement something similar to
revoke_iomem()?
Thank you for checking.
Yes, that's correct.
Regards,
Ravi Kumar Bandi
Quoted 1 line · Bjorn Helgaas
Which endpoint driver?
Thank you for checking.
It is Marvell PCIe driver for a device without a hot-plug pin routed to the CPU. The link-down event is detected by the in-tree Xilinx DMA PL PCIe controller driver (drivers/pci/controller/pcie-xilinx-dma-pl.c).
Quoted 1 line
Why would the endpoint driver map iomem to userspace?
sysfs exposed BAR resource files are mmap'd directly by a third-party userspace driver.
Quoted 2 lines
Well, we need to see the user(s) first, but once they materialize this begs for a higher level API. Until then it is moot anyway.
Understood. Thank you for the feedback. We will work on a higher level API proposal and get back.
Regards,
Ravi Kumar Bandi
Quoted 11 lines · Ravi Kumar Bandi
Quoted 1 line · Bjorn Helgaas
Which endpoint driver?
Thank you for checking.
It is Marvell PCIe driver for a device without a hot-plug pin routed to the CPU. The link-down event is detected by the in-tree Xilinx DMA PL PCIe controller driver (
drivers/pci/controller/pcie-xilinx-dma-pl.c).Quoted 1 line
Why would the endpoint driver map iomem to userspace?
Quoted 8 lines
sysfs exposed BAR resource files are mmap'd directly by a third-party userspace driver.
Quoted 2 lines
Well, we need to see the user(s) first, but once they materialize this begs for a higher level API. Until then it is moot anyway.
Understood. Thank you for the feedback. We will work on a higher level API proposal and get back.
I took a look and there are some problems here.
My first thought was, "why does the endpoint driver need to do this? The PCI core device removal should be responsible for zapping mappings."
2 things defeat this:
1/ for sysfs bar mappings, the unmap_mapping_range() in kernfs_drain_open_files() misses mappings established against the shared iomem_get_mapping().
2/ procfs access to BAR space has never unmapped on device removal
The practical implication of this is that userspace mappings of BARs can survive past device removal. As for mitigations, with IO_STRICT_DEVMEM the kernel will zap them before use, with LOCKDOWN the mappings can not be established, and CAP_SYS_RAWIO is required (for procfs) to create these mappings.
I recall that Sima added support for ioport mmap revoke support in:
636b21b50152 PCI: Revoke mappings like devmem
...but given revoke_iomem() only evacuates at request_mem_region() time, I do not see how that ever worked.
We could do something like the following for kernfs regression in the near term, or just proceed with making revoke_iomem() something that the PCI core does unconditionally by physical address on device removal. That would also fix the procfs gap.
Ravi, I think your time is best spent getting the PCI core to handle the unmap on device removal.
-- >8 --
Subject: resource: Fix PCI/sysfs mmap revocation vs CONFIG_IO_STRICT_DEVMEM=n
From: Dan Williams <[email protected]>
CONFIG_IO_STRICT_DEVMEM wants to arrange for exclusive kernel mappings of device MMIO resources. It prevents scenarios where the kernel is confused by a user triggered device effect while the driver is running. It uses a shared inode address_space such that request_mem_region() can unmap user mappings by physical resource address.
This shared inode however is not considered by kernfs_drain_open_files().
It is also the case that the procfs method for mapping PCI resources has historically never cleaned up user mappings on device removal.
Fix at least the regression of unmap behavior for the CONFIG_IO_STRICT_DEVMEM=n case by disabling the shared inode when revoke_iomem() is disabled.
--- drivers/char/mem.c | 3 ++- fs/sysfs/file.c | 2 +- kernel/resource.c | 11 ++++++++--- 3 files changed, 11 insertions(+), 5 deletions(-)
drivers/char/mem.c +2 -1
@@ -632,7 +632,8 @@ static int open_port(struct inode *inode, struct file *filp)* revocations when drivers want to take over a /dev/mem mapped * range. */ - filp->f_mapping =iomem_get_mapping (); +if (iomem_get_mapping ()) + filp->f_mapping =iomem_get_mapping ();return 0 ; }
fs/sysfs/file.c +1 -1
@@ -189,7 +189,7 @@ static int sysfs_kf_bin_open(struct kernfs_open_file *of) {const struct bin_attribute *battr = of->kn->priv; -if (battr->f_mapping) +if (battr->f_mapping && battr->f_mapping ()) of->file->f_mapping = battr->f_mapping ();return 0 ;
kernel/resource.c +8 -3
@@ -1297,9 +1297,6 @@ static void revoke_iomem(struct resource *res)unmap_mapping_range (inode->i_mapping, res->start,resource_size (res),1 ); } -#else -static void revoke_iomem (struct resource *res) {} -#endif struct address_space *iomem_get_mapping (void ) {
@@ -1311,6 +1308,14 @@ struct address_space *iomem_get_mapping(void)*/ return smp_load_acquire (&iomem_inode)->i_mapping; } +#else +static void revoke_iomem (struct resource *res) {} + +struct address_space *iomem_get_mapping (void ) +{ +return NULL ; +} +#endif static int __request_region_locked(struct resource *res, struct resource *parent, resource_size_t start, resource_size_t n,
Quoted 11 lines · Dan Williams (nvidia)
I took a look and there are some problems here.
My first thought was, "why does the endpoint driver need to do this? The PCI core device removal should be responsible for zapping mappings."
2 things defeat this:
1/ for sysfs bar mappings, the
unmap_mapping_range()inkernfs_drain_open_files()misses mappings established against the sharediomem_get_mapping().
Quoted 7 lines
2/ procfs access to BAR space has never unmapped on device removal
The practical implication of this is that userspace mappings of BARs can survive past device removal. As for mitigations, with
IO_STRICT_DEVMEMthe kernel will zap them before use, with LOCKDOWN the mappings can not be established, andCAP_SYS_RAWIOis required (for procfs) to create these mappings.
Hello Dan, thank you for the review and guidance.
Quoted 8 lines
I recall that Sima added support for ioport mmap revoke support in:
636b21b50152PCI: Revoke mappings like devmem...but given
revoke_iomem()only evacuates atrequest_mem_region()time, I do not see how that ever worked.
Quoted 7 lines
We could do something like the following for kernfs regression in the near term, or just proceed with making
revoke_iomem()something that the PCI core does unconditionally by physical address on device removal. That would also fix the procfs gap.Ravi, I think your time is best spent getting the PCI core to handle the unmap on device removal.
Agree. Thank you for the pointers, I will work on it and get back here.
Quoted 5 lines
-- >8 --
Subject: resource: Fix PCI/sysfs mmap revocation vsCONFIG_IO_STRICT_DEVMEM=nFrom: Dan Williams <[email protected]>