Skip to contentSkip to conversation

sparc: Fix page alignment in dma mapping

PATCH Applied [email protected] 4 participants 4 messages Feb 18 – Feb 23 SPARC + UltraSPARC (sparc/sparc64)

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 sparc: Fix page alignment in dma mapping

From: Stian Halseth <[email protected]>

'phys' may include an offset within the page, while 'base_paddr' is already page-aligned. This caused incorrect DMA mapping in dma_4u_map_phys and dma_4v_map_phys.

Fix both functions by masking phys with IO_PAGE_MASK or subtracting the page offset, covering both generic SPARC code and sun4v.

Fixes 38c0d0ebf520 ("sparc: Use physical address DMA mapping")

---
Changelog:
v3:
- Use IO_PAGE_MASK for alignment in both dma_4u_map_phys and dma_4v_map_phys
- Align phys before the loop in pci_sun4v.c
- Drop masking inside iommu_batch_add()

v2: https://lore.kernel.org/all/[email protected]/
- Add Fixes tag
- Add Closes tag
- Trim commit message to 80-column lines
- Remove newlines
- Subject: Remove the "iommu/pci_sun4v:" (generic sparc patch, not specific to sun4v)

v1: https://lore.kernel.org/all/[email protected]/

---
 arch/sparc/kernel/iommu.c     | 2 ++
 arch/sparc/kernel/pci_sun4v.c | 2 ++
 2 files changed, 4 insertions(+)
arch/sparc/kernel/iommu.c +2 -0
index 46ef88bc9c26..7613ab0ffb89 100644
@@ -312,6 +312,8 @@ static dma_addr_t dma_4u_map_phys(struct device *dev, phys_addr_t phys,
 	if (direction != DMA_TO_DEVICE)
 		iopte_protection |= IOPTE_WRITE;
 
+	phys &= IO_PAGE_MASK;
+
 	for (i = 0; i < npages; i++, base++, phys += IO_PAGE_SIZE)
 		iopte_val(*base) = iopte_protection | phys;
 
arch/sparc/kernel/pci_sun4v.c +2 -0
index 791f0a76665f..58ca4148f86b 100644
@@ -410,6 +410,8 @@ static dma_addr_t dma_4v_map_phys(struct device *dev, phys_addr_t phys,
 
 	iommu_batch_start(dev, prot, entry);
 
+	phys &= IO_PAGE_MASK;
+
 	for (i = 0; i < npages; i++, phys += IO_PAGE_SIZE) {
 		long err = iommu_batch_add(phys, mask);
 		if (unlikely(err < 0L))
Signature · 1 line
2.53.0
Tested-by on v3 1/1
#1 lore raw
Quoted 52 lines · On 18/2/26 20:00, [email protected] wrote:

From: Stian Halseth <[email protected]>

'phys' may include an offset within the page, while 'base_paddr' is already page-aligned. This caused incorrect DMA mapping in dma_4u_map_phys and dma_4v_map_phys.

Fix both functions by masking phys with IO_PAGE_MASK or subtracting the page offset, covering both generic SPARC code and sun4v.

Fixes 38c0d0ebf520 ("sparc: Use physical address DMA mapping")

---
Changelog:
v3:
- Use IO_PAGE_MASK for alignment in both dma_4u_map_phys and dma_4v_map_phys
- Align phys before the loop in pci_sun4v.c
- Drop masking inside iommu_batch_add()

v2: https://lore.kernel.org/all/[email protected]/
- Add Fixes tag
- Add Closes tag
- Trim commit message to 80-column lines
- Remove newlines
- Subject: Remove the "iommu/pci_sun4v:" (generic sparc patch, not specific to sun4v)

v1: https://lore.kernel.org/all/[email protected]/

---
  arch/sparc/kernel/iommu.c     | 2 ++
  arch/sparc/kernel/pci_sun4v.c | 2 ++
  2 files changed, 4 insertions(+)
arch/sparc/kernel/iommu.c +2 -0
index 46ef88bc9c26..7613ab0ffb89 100644
@@ -312,6 +312,8 @@ static dma_addr_t dma_4u_map_phys(struct device *dev, phys_addr_t phys,
  	if (direction != DMA_TO_DEVICE)
  		iopte_protection |= IOPTE_WRITE;
 
+	phys &= IO_PAGE_MASK;
+
  	for (i = 0; i < npages; i++, base++, phys += IO_PAGE_SIZE)
  		iopte_val(*base) = iopte_protection | phys;
 
arch/sparc/kernel/pci_sun4v.c +0 -0
index 791f0a76665f..58ca4148f86b 100644

arch/sparc/kernel/pci_sun4v.c static dma_addr_t dma_4v_map_phys(struct device *dev, phys_addr_t phys, · line 417

Quoted 8 lines
arch/sparc/kernel/pci_sun4v.c +2 -0
@@ -410,6 +410,8 @@ static dma_addr_t dma_4v_map_phys(struct device *dev, phys_addr_t phys,
 
  	iommu_batch_start(dev, prot, entry);
 
+	phys &= IO_PAGE_MASK;
+
  	for (i = 0; i < npages; i++, phys += IO_PAGE_SIZE) {
  		long err = iommu_batch_add(phys, mask);
  		if (unlikely(err < 0L))
Tested-by on v3 1/1
#2 lore raw
In reply to [email protected]
Quoted 68 lines · On Feb 18, 2026, at 20:00, [email protected] wrote:

From: Stian Halseth <[email protected]>

'phys' may include an offset within the page, while 'base_paddr' is already page-aligned. This caused incorrect DMA mapping in dma_4u_map_phys and dma_4v_map_phys.

Fix both functions by masking phys with IO_PAGE_MASK or subtracting the page offset, covering both generic SPARC code and sun4v.

Fixes 38c0d0ebf520 ("sparc: Use physical address DMA mapping")

---
Changelog:
v3:
- Use IO_PAGE_MASK for alignment in both dma_4u_map_phys and dma_4v_map_phys
- Align phys before the loop in pci_sun4v.c
- Drop masking inside iommu_batch_add()

v2: https://lore.kernel.org/all/[email protected]/
- Add Fixes tag
- Add Closes tag
- Trim commit message to 80-column lines
- Remove newlines
- Subject: Remove the "iommu/pci_sun4v:" (generic sparc patch, not specific to sun4v)

v1: https://lore.kernel.org/all/[email protected]/
---
arch/sparc/kernel/iommu.c | 2 ++
arch/sparc/kernel/pci_sun4v.c | 2 ++

2 files changed, 4 insertions(+)
arch/sparc/kernel/iommu.c +2 -0
index 46ef88bc9c26..7613ab0ffb89 100644
@@ -312,6 +312,8 @@ static dma_addr_t dma_4u_map_phys(struct device *dev, phys_addr_t phys,
 if (direction != DMA_TO_DEVICE)
 iopte_protection |= IOPTE_WRITE;
 
+ phys &= IO_PAGE_MASK;
+
 for (i = 0; i < npages; i++, base++, phys += IO_PAGE_SIZE)
 iopte_val(*base) = iopte_protection | phys;
 
arch/sparc/kernel/pci_sun4v.c +2 -0
index 791f0a76665f..58ca4148f86b 100644
@@ -410,6 +410,8 @@ static dma_addr_t dma_4v_map_phys(struct device *dev, phys_addr_t phys,
 
 iommu_batch_start(dev, prot, entry);
 
+ phys &= IO_PAGE_MASK;
+
 for (i = 0; i < npages; i++, phys += IO_PAGE_SIZE) {
 long err = iommu_batch_add(phys, mask);
 if (unlikely(err < 0L))
Signature · 1 line
2.53.0
#3 lore raw
In reply to [email protected]
Quoted 6 lines · On 18.02.2026 13:00, [email protected] wrote:

From: Stian Halseth <[email protected]>

'phys' may include an offset within the page, while 'base_paddr' is already page-aligned. This caused incorrect DMA mapping in dma_4u_map_phys and dma_4v_map_phys.

Quoted 8 lines

Fix both functions by masking phys with IO_PAGE_MASK or subtracting the page offset, covering both generic SPARC code and sun4v.

Applied to dma-mapping-fixes branch with slightly adjusted commit description (to match v3 changes). Thanks!

Quoted 38 lines

---
Changelog:
v3:
- Use IO_PAGE_MASK for alignment in both dma_4u_map_phys and dma_4v_map_phys
- Align phys before the loop in pci_sun4v.c
- Drop masking inside iommu_batch_add()

v2: https://lore.kernel.org/all/[email protected]/
- Add Fixes tag
- Add Closes tag
- Trim commit message to 80-column lines
- Remove newlines
- Subject: Remove the "iommu/pci_sun4v:" (generic sparc patch, not specific to sun4v)

v1: https://lore.kernel.org/all/[email protected]/

---
  arch/sparc/kernel/iommu.c     | 2 ++
  arch/sparc/kernel/pci_sun4v.c | 2 ++
  2 files changed, 4 insertions(+)
arch/sparc/kernel/iommu.c +2 -0
index 46ef88bc9c26..7613ab0ffb89 100644
@@ -312,6 +312,8 @@ static dma_addr_t dma_4u_map_phys(struct device *dev, phys_addr_t phys,
  	if (direction != DMA_TO_DEVICE)
  		iopte_protection |= IOPTE_WRITE;
 
+	phys &= IO_PAGE_MASK;
+
  	for (i = 0; i < npages; i++, base++, phys += IO_PAGE_SIZE)
  		iopte_val(*base) = iopte_protection | phys;
 
arch/sparc/kernel/pci_sun4v.c +0 -0
index 791f0a76665f..58ca4148f86b 100644

arch/sparc/kernel/pci_sun4v.c static dma_addr_t dma_4v_map_phys(struct device *dev, phys_addr_t phys, · line 417

Quoted 8 lines
arch/sparc/kernel/pci_sun4v.c +2 -0
@@ -410,6 +410,8 @@ static dma_addr_t dma_4v_map_phys(struct device *dev, phys_addr_t phys,
 
  	iommu_batch_start(dev, prot, entry);
 
+	phys &= IO_PAGE_MASK;
+
  	for (i = 0; i < npages; i++, phys += IO_PAGE_SIZE) {
  		long err = iommu_batch_add(phys, mask);
  		if (unlikely(err < 0L))

Best regards

Signature · 2 lines
Marek Szyprowski, PhD
Samsung R&D Institute Poland