Skip to contentSkip to conversation

s390/pci: Fix dev.dma_range_map missing sentinel element

PATCH Superseded Niklas Schnelle 2 participants 3 messages Mar 6, 2025 – Mar 10, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Niklas Schnelle

author maintainer
#0 lore raw

PATCH 1/1 s390/pci: Fix dev.dma_range_map missing sentinel element

The fixed commit sets up dev.dma_range_map but missed that this is supposed to be an array of struct bus_dma_region with a sentinel element with the size field set to 0 at the end. This would lead to overruns in e.g. dma_range_map_min(). It could also result in wrong translations instead of DMA_MAPPIN_ERROR in translate_phys_to_dma() if the paddr were to not fit in the aperture. Fix this by using the dma_direct_set_offset() helper which creates a sentinel for us.

Fixes d236843a6964 ("s390/pci: store DMA offset in bus_dma_region")

---
This is based on iommu/next which contains the fixed commit

---
 arch/s390/pci/pci_bus.c | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)
arch/s390/pci/pci_bus.c +11 -13
index 0e725039861f92925a38f7ff7cb6de6b0d965ac3..14310c3b48860a16de13536adf95ef99e6af21cc 100644
@@ -287,23 +287,21 @@ static struct zpci_bus *zpci_bus_alloc(int topo, bool topo_is_tid)
 static void pci_dma_range_setup(struct pci_dev *pdev)
 {
 	struct zpci_dev *zdev = to_zpci(pdev);
-	struct bus_dma_region *map;
-	u64 aligned_end;
+	u64 aligned_end, size;
+	dma_addr_t dma_start;
+	int ret;
 
-	map = kzalloc(sizeof(*map), GFP_KERNEL);
-	if (!map)
-		return;
-
-	map->cpu_start = 0;
-	map->dma_start = PAGE_ALIGN(zdev->start_dma);
+	dma_start = PAGE_ALIGN(zdev->start_dma);
 	aligned_end = PAGE_ALIGN_DOWN(zdev->end_dma + 1);
-	if (aligned_end >= map->dma_start)
-		map->size = aligned_end - map->dma_start;
+	if (aligned_end >= dma_start)
+		size = aligned_end - dma_start;
 	else
-		map->size = 0;
-	WARN_ON_ONCE(map->size == 0);
+		size = 0;
+	WARN_ON_ONCE(size == 0);
 
-	pdev->dev.dma_range_map = map;
+	ret = dma_direct_set_offset(&pdev->dev, 0, dma_start, size);
+	if (ret)
+		pr_err("Failed to allocate DMA range map for %s\n", pci_name(pdev));
 }
 
 void pcibios_bus_add_device(struct pci_dev *pdev)

---
base-commit: e840414e5a73ac02ffba6299b46f535a0b7cba98
change-id: 20250306-fix_dma_map_alloc-b3b05903dcfb

Best regards,

Signature · 1 line
Niklas Schnelle
author maintainer
#1 lore raw
Quoted 42 lines · Niklas Schnelle

The fixed commit sets up dev.dma_range_map but missed that this is supposed to be an array of struct bus_dma_region with a sentinel element with the size field set to 0 at the end. This would lead to overruns in e.g. dma_range_map_min(). It could also result in wrong translations instead of DMA_MAPPIN_ERROR in translate_phys_to_dma() if the paddr were to not fit in the aperture. Fix this by using the dma_direct_set_offset() helper which creates a sentinel for us.

Fixes d236843a6964 ("s390/pci: store DMA offset in bus_dma_region")

---
This is based on iommu/next which contains the fixed commit

---
 arch/s390/pci/pci_bus.c | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)
arch/s390/pci/pci_bus.c +6 -10
index 0e725039861f92925a38f7ff7cb6de6b0d965ac3..14310c3b48860a16de13536adf95ef99e6af21cc 100644
@@ -287,23 +287,21 @@ static struct zpci_bus *zpci_bus_alloc(int topo, bool topo_is_tid)
 static void pci_dma_range_setup(struct pci_dev *pdev)
 {
 	struct zpci_dev *zdev = to_zpci(pdev);
-	struct bus_dma_region *map;
-	u64 aligned_end;
+	u64 aligned_end, size;
+	dma_addr_t dma_start;
+	int ret;
 
-	map = kzalloc(sizeof(*map), GFP_KERNEL);
-	if (!map)
-		return;
-
-	map->cpu_start = 0;
-	map->dma_start = PAGE_ALIGN(zdev->start_dma);
+	dma_start = PAGE_ALIGN(zdev->start_dma);
 	aligned_end = PAGE_ALIGN_DOWN(zdev->end_dma + 1);
-	if (aligned_end >= map->dma_start)
-		map->size = aligned_end - map->dma_start;
+	if (aligned_end >= dma_start)
+		size = aligned_end - dma_start;

arch/s390/pci/pci_bus.c static struct zpci_bus *zpci_bus_alloc(int topo, bool topo_is_tid) · line 304

Quoted 10 lines
arch/s390/pci/pci_bus.c +5 -3
 	else
-		map->size = 0;
-	WARN_ON_ONCE(map->size == 0);
+		size = 0;
+	WARN_ON_ONCE(size == 0);
 
-	pdev->dev.dma_range_map = map;
+	ret = dma_direct_set_offset(&pdev->dev, 0, dma_start, size);
+	if (ret)
+		pr_err("Failed to allocate DMA range map for %s\n", pci_name(pdev));

Arguably one might want to use dev_err() here but the rest of the file uses pr_err(). On the other hand dma_direct_set_offset() uses dev_err() in case there is already a dev.dma_range_map.

Also maybe s/allocate/set up/ since we're not checking just for -ENOMEM

Quoted 9 lines
arch/s390/pci/pci_bus.c +0 -1
 }
 
 void pcibios_bus_add_device(struct pci_dev *pdev)
 
---

base-commit: e840414e5a73ac02ffba6299b46f535a0b7cba98
change-id: 20250306-fix_dma_map_alloc-b3b05903dcfb

Best regards,

Reviewed-by on v1 1/1
#2 lore raw
In reply to Niklas Schnelle
Quoted 5 lines · Niklas Schnelle

The fixed commit sets up dev.dma_range_map but missed that this is supposed to be an array of struct bus_dma_region with a sentinel element with the size field set to 0 at the end. This would lead to overruns in e.g. dma_range_map_min(). It could also result in wrong translations instead of DMA_MAPPIN_ERROR in translate_phys_to_dma() if the paddr were

Typo, DMA_MAPPING_ERROR

Quoted 5 lines

to not fit in the aperture. Fix this by using the dma_direct_set_offset() helper which creates a sentinel for us.

Fixes d236843a6964 ("s390/pci: store DMA offset in bus_dma_region")

Thanks for looking into this while I was out. I confirmed that I can reproduce the potential issue by forcing codepaths - and that your proposed fix also resolves it (by forcing a DMA mapping error instead of using random values for a bus_dma_region).

Quoted 51 lines

---
This is based on iommu/next which contains the fixed commit

---
 arch/s390/pci/pci_bus.c | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)
arch/s390/pci/pci_bus.c +11 -14
index 0e725039861f92925a38f7ff7cb6de6b0d965ac3..14310c3b48860a16de13536adf95ef99e6af21cc 100644
@@ -287,23 +287,21 @@ static struct zpci_bus *zpci_bus_alloc(int topo, bool topo_is_tid)
 static void pci_dma_range_setup(struct pci_dev *pdev)
 {
 	struct zpci_dev *zdev = to_zpci(pdev);
-	struct bus_dma_region *map;
-	u64 aligned_end;
+	u64 aligned_end, size;
+	dma_addr_t dma_start;
+	int ret;
 
-	map = kzalloc(sizeof(*map), GFP_KERNEL);
-	if (!map)
-		return;
-
-	map->cpu_start = 0;
-	map->dma_start = PAGE_ALIGN(zdev->start_dma);
+	dma_start = PAGE_ALIGN(zdev->start_dma);
 	aligned_end = PAGE_ALIGN_DOWN(zdev->end_dma + 1);
-	if (aligned_end >= map->dma_start)
-		map->size = aligned_end - map->dma_start;
+	if (aligned_end >= dma_start)
+		size = aligned_end - dma_start;
 	else
-		map->size = 0;
-	WARN_ON_ONCE(map->size == 0);
+		size = 0;
+	WARN_ON_ONCE(size == 0);
 
-	pdev->dev.dma_range_map = map;
+	ret = dma_direct_set_offset(&pdev->dev, 0, dma_start, size);
+	if (ret)
+		pr_err("Failed to allocate DMA range map for %s\n", pci_name(pdev));
 }
 
 void pcibios_bus_add_device(struct pci_dev *pdev)
 
---

base-commit: e840414e5a73ac02ffba6299b46f535a0b7cba98
change-id: 20250306-fix_dma_map_alloc-b3b05903dcfb

Best regards,