Skip to contentSkip to conversation

arch/arm/mm/dma-mapping.c: fix __alloc_from_pool returning a dirty buffer

PATCH In review Igor Klochko 4 participants 5 messages Dec 23, 2022 – Jan 3, 2023 Arm (32-bit)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arch/arm/mm/dma-mapping.c: fix __alloc_from_pool returning a dirty buffer

Hi,

A small patch for __alloc_from_pool to clean the buffer before returning.

---
 arch/arm/mm/dma-mapping.c | 1 +
 1 file changed, 1 insertion(+)
arch/arm/mm/dma-mapping.c +1 -0
index c135f6e37a00..bb2bb3ab497a 100644
@@ -366,6 +366,7 @@ static void *__alloc_from_pool(size_t size, struct page **ret_page)
 
 		*ret_page = phys_to_page(phys);
 		ptr = (void *)val;
+		memset(ptr, 0, size);
 	}
 
 	return ptr;
Signature · 5 lines
2.39.0


Kind regards,
Igor Klochko
#1 lore raw
Quoted 4 lines · Igor Klochko

Hi,

A small patch for __alloc_from_pool to clean the buffer before returning.

This does look correct. The "normal" allocators seems to do the memset through __dma_clear_buffer, but the __alloc_from_pool seems to be missing it. Please write a proper changelog with a signoff, and preferably a Fixes tag if you can find what introduced this. Also the ARM code needs to go to the ARM maintainer and arm mailing list.

#2 lore raw

Thanks Christoph,

Added fixes and a changelog.
This issue is present in all current LTS versions.

----
Buffers allocated by __alloc_from_pool() should be zeroed out as done by other allocators. Certain drivers expect a clean buffer and clearing the buffer is beneficial from the security point of view.
---

Fixes 36d0fd2198da3 (*arm: use genalloc for the atomic pool*)
---
 arch/arm/mm/dma-mapping.c | 1 +
 1 file changed, 1 insertion(+)
arch/arm/mm/dma-mapping.c +1 -0
index c135f6e37a00..bb2bb3ab497a 100644
@@ -366,6 +366,7 @@ static void *__alloc_from_pool(size_t size, struct page **ret_page)
 
 		*ret_page = phys_to_page(phys);
 		ptr = (void *)val;
+		memset(ptr, 0, size);
 	}
 
 	return ptr;
Signature · 7 lines
2.39.0




Kind regards,
Igor Klochko
Quoted 11 lines · Christoph Hellwig
Quoted 4 lines · Igor Klochko

Hi,

A small patch for __alloc_from_pool to clean the buffer before returning.

This does look correct. The "normal" allocators seems to do the memset through __dma_clear_buffer, but the __alloc_from_pool seems to be missing it. Please write a proper changelog with a signoff, and preferably a Fixes tag if you can find what introduced this. Also the ARM code needs to go to the ARM maintainer and arm mailing list.

#3 lore raw
Quoted 20 lines · Igor Klochko

Thanks Christoph,

Added fixes and a changelog.
This issue is present in all current LTS versions.

----
Buffers allocated by __alloc_from_pool() should be zeroed out as done by other allocators. Certain drivers expect a clean buffer and clearing the buffer is beneficial from the security point of view.
---

Fixes 36d0fd2198da3 (*arm: use genalloc for the atomic pool*)
---
 arch/arm/mm/dma-mapping.c | 1 +
 1 file changed, 1 insertion(+)
arch/arm/mm/dma-mapping.c +0 -0
index c135f6e37a00..bb2bb3ab497a 100644

arch/arm/mm/dma-mapping.c static void *__alloc_from_pool(size_t size, struct page **ret_page) · line 369

Quoted 4 lines
arch/arm/mm/dma-mapping.c +1 -0
@@ -366,6 +366,7 @@ static void *__alloc_from_pool(size_t size, struct page **ret_page)
 
 		*ret_page = phys_to_page(phys);
 		ptr = (void *)val;
+		memset(ptr, 0, size);

I'm not up to speed with the current structure of the DMA implementation on ARM, but isn't this going to make cache lines dirty for the returned buffer, which will corrupt DMA on non-coherent devices?

Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!
#4 lore raw
Quoted 22 lines · Russell King (Oracle)
Quoted 21 lines · Igor Klochko

Thanks Christoph,

Added fixes and a changelog.
This issue is present in all current LTS versions.

----
Buffers allocated by __alloc_from_pool() should be zeroed out as done by other allocators. Certain drivers expect a clean buffer and clearing the buffer is beneficial from the security point of view.
---

Fixes 36d0fd2198da3 (*arm: use genalloc for the atomic pool*)
---
  arch/arm/mm/dma-mapping.c | 1 +
  1 file changed, 1 insertion(+)
arch/arm/mm/dma-mapping.c +0 -0
index c135f6e37a00..bb2bb3ab497a 100644
@@ -366,6 +366,7 @@ static void *__alloc_from_pool(size_t size, struct page **ret_page)
 
Quoted 7 lines

arch/arm/mm/dma-mapping.c static void *__alloc_from_pool(size_t size, struct page **ret_page) · line 369

Quoted 3 lines
arch/arm/mm/dma-mapping.c +1 -0
  		*ret_page = phys_to_page(phys);
  		ptr = (void *)val;
+		memset(ptr, 0, size);

I'm not up to speed with the current structure of the DMA implementation on ARM, but isn't this going to make cache lines dirty for the returned buffer, which will corrupt DMA on non-coherent devices?

The virtual address of the pool, and thus VAs within the pool as returned by gen_pool_alloc(), should be a non-cacheable remap set up by atomic_pool_init(), so it looks appropriate to me. In fact "ptr" here appears to be the final return value propagated all the way back out to the caller of dma_alloc_coherent(), so if that were dodgy then there would already be problems afoot.

Robin.