Skip to contentSkip to conversation

arm64: dma-mapping: always clear allocated buffers

PATCH New Marek Szyprowski 2 participants 2 messages Apr 23, 2015 arm64

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm64: dma-mapping: always clear allocated buffers

Buffers allocated by dma_alloc_coherent() are always zeroed on Alpha, ARM (32bit), MIPS, PowerPC, x86/x86_64 and probably other architectures. It turned out that some drivers rely on this 'feature'. Allocated buffer might be also exposed to userspace with dma_mmap() call, so clearing it is desired from security point of view to avoid exposing random memory to userspace. This patch unifies dma_alloc_coherent() behavior on ARM64 architecture with other implementations by unconditionally zeroing allocated buffer.

---
Hello,

This issue was really nasty to debug. The problem has been observed with dw_mmc driver on Exynos 5433. I've also check if there are any calls to dma_alloc_coherent() with __GFP_ZERO flag and found only two drivers in the whole kernel tree: drivers/staging/android/ion/ion_cma_heap.c and drivers/thunderbolt/nhi.c. The best will be to review all drivers and add missing __GFP_ZERO flag or replace it with dma_zalloc_coherent() and only then convert all implementation for all architectures to honor this flag. However I expect that this is a huge task and for now ARM64 should just match the behavior of other architectures.

Best regards
Marek Szyprowski
Samsung Poland R&D Center

---
 arch/arm64/mm/dma-mapping.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
arch/arm64/mm/dma-mapping.c +2 -4
index ef7d112..e0f14ee 100644
@@ -67,8 +67,7 @@ static void *__alloc_from_pool(size_t size, struct page **ret_page, gfp_t flags)
 
 		*ret_page = phys_to_page(phys);
 		ptr = (void *)val;
-		if (flags & __GFP_ZERO)
-			memset(ptr, 0, size);
+		memset(ptr, 0, size);
 	}
 
 	return ptr;
@@ -113,8 +112,7 @@ static void *__dma_alloc_coherent(struct device *dev, size_t size,
 
 		*dma_handle = phys_to_dma(dev, page_to_phys(page));
 		addr = page_address(page);
-		if (flags & __GFP_ZERO)
-			memset(addr, 0, size);
+		memset(addr, 0, size);
 		return addr;
 	} else {
 		return swiotlb_alloc_coherent(dev, size, dma_handle, flags);
Signature · 1 line
1.9.2
#1 lore raw

PATCH 1/1 arm64: dma-mapping: always clear allocated buffers

Quoted 2 lines · Marek Szyprowski

Buffers allocated by dma_alloc_coherent() are always zeroed on Alpha, ARM (32bit), MIPS, PowerPC, x86/x86_64 and probably other architectures.

That's pretty compelling, even if it sucks. Maybe we could add a debug option to poison DMA buffers if __GFP_ZERO isn't passed? It might help others avoid the painful debugging experience you had...

Will