Skip to content

Commit ca085fa

Browse files
committed
dma-mapping: add __dma_from_device_group_begin()/end()
When a structure contains a buffer that DMA writes to alongside fields that the CPU writes to, cache line sharing between the DMA buffer and CPU-written fields can cause data corruption on non-cache-coherent platforms. Add __dma_from_device_group_begin()/end() annotations to ensure proper alignment to prevent this: struct my_device { spinlock_t lock1; __dma_from_device_group_begin(); char dma_buffer1[16]; char dma_buffer2[16]; __dma_from_device_group_end(); spinlock_t lock2; }; Message-ID: <19163086d5e4704c316f18f6da06bc1c72968904.1767601130.git.mst@redhat.com> Acked-by: Marek Szyprowski <m.szyprowski@samsung.com> Reviewed-by: Petr Tesarik <ptesarik@suse.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
1 parent f6a15d8 commit ca085fa

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

include/linux/dma-mapping.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <linux/dma-direction.h>
88
#include <linux/scatterlist.h>
99
#include <linux/bug.h>
10+
#include <linux/cache.h>
1011

1112
/**
1213
* List of possible attributes associated with a DMA mapping. The semantics
@@ -703,6 +704,18 @@ static inline int dma_get_cache_alignment(void)
703704
}
704705
#endif
705706

707+
#ifdef ARCH_HAS_DMA_MINALIGN
708+
#define ____dma_from_device_aligned __aligned(ARCH_DMA_MINALIGN)
709+
#else
710+
#define ____dma_from_device_aligned
711+
#endif
712+
/* Mark start of DMA buffer */
713+
#define __dma_from_device_group_begin(GROUP) \
714+
__cacheline_group_begin(GROUP) ____dma_from_device_aligned
715+
/* Mark end of DMA buffer */
716+
#define __dma_from_device_group_end(GROUP) \
717+
__cacheline_group_end(GROUP) ____dma_from_device_aligned
718+
706719
static inline void *dmam_alloc_coherent(struct device *dev, size_t size,
707720
dma_addr_t *dma_handle, gfp_t gfp)
708721
{

0 commit comments

Comments
 (0)