ensure dma_alloc_coherent always returns zeroed memory
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 18 lines · On 12/28/18 9:58 AM, [email protected] wrote:
Quoted 17 lines · Eugeniy Paltsev
What happens:
First coherent allocation (PAL#1):
1) allocate page viaalloc_pages(__GFP_ZERO), got page vith paddr0xbf2d6000
2) zero it via memset [deep insidealloc_pages()]
3) remap it as uncached viaioremap_nocache(), got vaddr0x70004000
4) flush and invalidate L1 and L2 lines which belongs to allocated page. Everything is OK for now.Second coherent allocation (PAL#2):
1) allocate page viaalloc_pages(__GFP_ZERO), got page vith paddr0xbf2d4000
2) zero it via memset [deep insidealloc_pages()].
-> Ooops: ARC memset implementation (due to SW bug) calls 'prefetchw' instruction for address which lies outside of memset area (it will be0xbf2d6000in this case) So we got one modified (dirty) cache line in the beginning of previous coherent buffer which has been already flushed and invalidated (in PAL#1).
Quoted 4 lines
Quoted 2 lines
So if we put some real data in this buffer [via virtual (uncached) address)] before this line is flushed - we will got corrupted data.
We could instead do the memset on the remapped address, which would work around this prefetching issue:
Right, it probably would, but that would still be too fragile, there are other bad things in non-dma scenarios which happen due to this.
This needed fixing and Eugeniy's fix is in upstream/master now
2019-01-14 e6a72b7daeeb ARCv2: lib: memeset: fix doing prefetchw outside of
buffer
Quoted 21 lines
arch/arc/mm/dma.c +2
-1@@ -33,7 +33,7 @@ void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,*/ BUG_ON (gfp & __GFP_HIGHMEM); - page =alloc_pages (gfp | __GFP_ZERO, order); + page =alloc_pages (gfp, order);if (!page)return NULL ;@@ -69,6 +69,7 @@ void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,if (need_coh)dma_cache_wback_inv (paddr, size); +memset (kvaddr,0 , size);return kvaddr; }