Skip to contentSkip to conversation

PCI/P2PDMA: Avoid returning a provider for non_mappable_bars

PATCH Applied Matt Evans 4 participants 4 messages Apr 23 – Apr 24

Subsystems (2)
lore
Share & tools
lore

Matt Evans

author
#0 lore raw

PATCH v2 1/1 PCI/P2PDMA: Avoid returning a provider for non_mappable_bars

Extend the checks in pcim_p2pdma_init() and pcim_p2pdma_provider() to exclude functions that have pdev->non_mappable_bars set.

Consumers such as VFIO were previously able to map these for access by the CPU or P2P. Update the comment on non_mappable_bars to show it refers to any access, not just userspace CPU access.

Fixes 372d6d1b8ae3c ("PCI/P2PDMA: Refactor to separate core P2P functionality from memory allocation")

---

This arises from Alex Williamson's suggestion to test non_mappable_bars when getting the provider, with discussion here:

https://lore.kernel.org/kvm/[email protected]/

The goal was to prevent a hole where VFIO could export DMABUFs for BARs marked non-mappable, and to fix for all users of the provider rather than just VFIO. Alex observed that non_mappable_bars should be taken to mean BARs weren't usable by the CPU _or_ peers and, considering that, its comment about userspace access wasn't quite right.

== Changes ==

v2:
- Also test non_mappable_bars in pcim_p2pdma_init(), as otherwise pci_p2pdma_add_resource() will WARN_ON pcim_p2pdma_provider() failing.

Niklas and Logan, I didn't re-add your R-B from v1 as (splitting hairs...) the code's changed.

v1:
https://lore.kernel.org/linux-pci/[email protected]/

 drivers/pci/p2pdma.c | 6 +++++-
 include/linux/pci.h  | 2 +-
 2 files changed, 6 insertions(+), 2 deletions(-)
drivers/pci/p2pdma.c +5 -1
index 7c898542af8d..adb17a4f6939 100644
@@ -262,6 +262,9 @@ int pcim_p2pdma_init(struct pci_dev *pdev)
 	struct pci_p2pdma *p2p;
 	int i, ret;
 
+	if (pdev->non_mappable_bars)
+		return -EOPNOTSUPP;
+
 	p2p = rcu_dereference_protected(pdev->p2pdma, 1);
 	if (p2p)
 		return 0;
@@ -318,7 +321,8 @@ struct p2pdma_provider *pcim_p2pdma_provider(struct pci_dev *pdev, int bar)
 {
 	struct pci_p2pdma *p2p;
 
-	if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM))
+	if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM) ||
+	    pdev->non_mappable_bars)
 		return NULL;
 
 	p2p = rcu_dereference_protected(pdev->p2pdma, 1);
include/linux/pci.h +1 -1
index 2c4454583c11..1e6802017d6b 100644
@@ -508,7 +508,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  */
+	unsigned int	non_mappable_bars:1;	/* BARs can't be mapped by CPU or peers */
 	pci_dev_flags_t dev_flags;
 	atomic_t	enable_cnt;	/* pci_enable_device has been called */
 
Signature · 1 line
2.47.3
Reviewed-by on v2 1/1
#1 lore raw
Quoted 25 lines · Matt Evans

Extend the checks in pcim_p2pdma_init() and pcim_p2pdma_provider() to exclude functions that have pdev->non_mappable_bars set.

Consumers such as VFIO were previously able to map these for access by the CPU or P2P. Update the comment on non_mappable_bars to show it refers to any access, not just userspace CPU access.

Fixes 372d6d1b8ae3c ("PCI/P2PDMA: Refactor to separate core P2P functionality from memory allocation")

---

This arises from Alex Williamson's suggestion to test non_mappable_bars when getting the provider, with discussion here:

https://lore.kernel.org/kvm/[email protected]/

The goal was to prevent a hole where VFIO could export DMABUFs for BARs marked non-mappable, and to fix for all users of the provider rather than just VFIO. Alex observed that non_mappable_bars should be taken to mean BARs weren't usable by the CPU _or_ peers and, considering that, its comment about userspace access wasn't quite right.

== Changes ==

Quoted 8 lines

v2:
- Also test non_mappable_bars in pcim_p2pdma_init(), as otherwise pci_p2pdma_add_resource() will WARN_ON pcim_p2pdma_provider() failing.

Niklas and Logan, I didn't re-add your R-B from v1 as (splitting hairs...) the code's changed.

Makes sense, now feel free to re-add ;)

Thanks,
Niklas

Reviewed-by on v2 1/1
#2 lore raw
In reply to Matt Evans
Quoted 70 lines · On Thu, 23 Apr 2026 10:30:51 -0700 Matt Evans <[email protected]> wrote:

Extend the checks in pcim_p2pdma_init() and pcim_p2pdma_provider() to exclude functions that have pdev->non_mappable_bars set.

Consumers such as VFIO were previously able to map these for access by the CPU or P2P. Update the comment on non_mappable_bars to show it refers to any access, not just userspace CPU access.

Fixes 372d6d1b8ae3c ("PCI/P2PDMA: Refactor to separate core P2P functionality from memory allocation")

---

This arises from Alex Williamson's suggestion to test non_mappable_bars when getting the provider, with discussion here:

https://lore.kernel.org/kvm/[email protected]/

The goal was to prevent a hole where VFIO could export DMABUFs for BARs marked non-mappable, and to fix for all users of the provider rather than just VFIO. Alex observed that non_mappable_bars should be taken to mean BARs weren't usable by the CPU _or_ peers and, considering that, its comment about userspace access wasn't quite right.

== Changes ==

v2:
- Also test non_mappable_bars in pcim_p2pdma_init(), as otherwise pci_p2pdma_add_resource() will WARN_ON pcim_p2pdma_provider() failing.

Niklas and Logan, I didn't re-add your R-B from v1 as (splitting hairs...) the code's changed.

v1:
https://lore.kernel.org/linux-pci/[email protected]/

 drivers/pci/p2pdma.c | 6 +++++-
 include/linux/pci.h  | 2 +-
 2 files changed, 6 insertions(+), 2 deletions(-)
drivers/pci/p2pdma.c +5 -1
index 7c898542af8d..adb17a4f6939 100644
@@ -262,6 +262,9 @@ int pcim_p2pdma_init(struct pci_dev *pdev)
 	struct pci_p2pdma *p2p;
 	int i, ret;
 
+	if (pdev->non_mappable_bars)
+		return -EOPNOTSUPP;
+
 	p2p = rcu_dereference_protected(pdev->p2pdma, 1);
 	if (p2p)
 		return 0;
@@ -318,7 +321,8 @@ struct p2pdma_provider *pcim_p2pdma_provider(struct pci_dev *pdev, int bar)
 {
 	struct pci_p2pdma *p2p;
 
-	if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM))
+	if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM) ||
+	    pdev->non_mappable_bars)
 		return NULL;
 
 	p2p = rcu_dereference_protected(pdev->p2pdma, 1);
include/linux/pci.h +0 -0
index 2c4454583c11..1e6802017d6b 100644

include/linux/pci.h struct pci_dev { · line 514

Quoted 8 lines
include/linux/pci.h +1 -1
@@ -508,7 +508,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  */
+	unsigned int	non_mappable_bars:1;	/* BARs can't be mapped by CPU or peers */
 	pci_dev_flags_t dev_flags;
 	atomic_t	enable_cnt;	/* pci_enable_device has been called */
 

Thanks,
Alex

#3 lore raw
In reply to Matt Evans
Quoted 9 lines · Matt Evans

Extend the checks in pcim_p2pdma_init() and pcim_p2pdma_provider() to exclude functions that have pdev->non_mappable_bars set.

Consumers such as VFIO were previously able to map these for access by the CPU or P2P. Update the comment on non_mappable_bars to show it refers to any access, not just userspace CPU access.

Fixes 372d6d1b8ae3c ("PCI/P2PDMA: Refactor to separate core P2P functionality from memory allocation")

Applied to pci/p2pdma for v7.2, thanks! Will be rebased after v7.1-rc1.

Quoted 72 lines

---

This arises from Alex Williamson's suggestion to test non_mappable_bars when getting the provider, with discussion here:

https://lore.kernel.org/kvm/[email protected]/

The goal was to prevent a hole where VFIO could export DMABUFs for BARs marked non-mappable, and to fix for all users of the provider rather than just VFIO. Alex observed that non_mappable_bars should be taken to mean BARs weren't usable by the CPU _or_ peers and, considering that, its comment about userspace access wasn't quite right.

== Changes ==

v2:
- Also test non_mappable_bars in pcim_p2pdma_init(), as otherwise pci_p2pdma_add_resource() will WARN_ON pcim_p2pdma_provider() failing.

Niklas and Logan, I didn't re-add your R-B from v1 as (splitting hairs...) the code's changed.

v1:
https://lore.kernel.org/linux-pci/[email protected]/

 drivers/pci/p2pdma.c | 6 +++++-
 include/linux/pci.h  | 2 +-
 2 files changed, 6 insertions(+), 2 deletions(-)
drivers/pci/p2pdma.c +5 -1
index 7c898542af8d..adb17a4f6939 100644
@@ -262,6 +262,9 @@ int pcim_p2pdma_init(struct pci_dev *pdev)
 	struct pci_p2pdma *p2p;
 	int i, ret;
 
+	if (pdev->non_mappable_bars)
+		return -EOPNOTSUPP;
+
 	p2p = rcu_dereference_protected(pdev->p2pdma, 1);
 	if (p2p)
 		return 0;
@@ -318,7 +321,8 @@ struct p2pdma_provider *pcim_p2pdma_provider(struct pci_dev *pdev, int bar)
 {
 	struct pci_p2pdma *p2p;
 
-	if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM))
+	if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM) ||
+	    pdev->non_mappable_bars)
 		return NULL;
 
 	p2p = rcu_dereference_protected(pdev->p2pdma, 1);
include/linux/pci.h +1 -1
index 2c4454583c11..1e6802017d6b 100644
@@ -508,7 +508,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  */
+	unsigned int	non_mappable_bars:1;	/* BARs can't be mapped by CPU or peers */
 	pci_dev_flags_t dev_flags;
 	atomic_t	enable_cnt;	/* pci_enable_device has been called */
 
Signature · 1 line
2.47.3