mm: dmapool: Align to ARCH_DMA_MINALIGN in non-coherent DMA mode
Subsystems (3)
PATCH v5 2/3 mm: dmapool: Align to ARCH_DMA_MINALIGN in non-coherent DMA mode
In non-coherent DMA mode, kernel uses cache flushing operations to maintain I/O coherency, so the dmapool objects should be aligned to ARCH_DMA_MINALIGN. Otherwise, it will cause data corruption, at least on MIPS:
Step 1, dma_map_single
Step 2, cache_invalidate (no writeback)
Step 3, dma_from_device
Step 4, dma_unmap_single
If a DMA buffer and a kernel structure share a same cache line, and if the kernel structure has dirty data, cache_invalidate (no writeback) will cause data lost.
--- mm/dmapool.c | 3 +++ 1 file changed, 3 insertions(+)
mm/dmapool.c +3 -0
@@ -140,6 +140,9 @@ struct dma_pool *dma_pool_create(const char *name, struct device *dev,else if (align & (align -1 ))return NULL ; +if (!device_is_coherent (dev)) + align =max_t (size_t, align,dma_get_cache_alignment ()); +if (size ==0 )return NULL ;else if (size <4 )
Signature · 9 lines
2.7.0 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
The dmapool code uses dma_alloc_coherent to allocate each element, and dma_alloc_coherent must align to ARCH_DMA_MINALIGN already. If you implementation doesn't do that it needs to be fixed.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH V5 2/3] mm: dmapool: Align to ARCH_DMA_MINALIGN innon-coherent DMA mode
SGksIENocmlzdG9waCwNCg0KTWF5YmUgeW91IG1pc3NlZCBzb21ldGhpbmcuDQoxLCBwb29s X2FsbG9jX3BhZ2UoKSB1c2UgZG1hX2FsbG9jX2NvaGVyZW50KCkgdG8gYWxsb2NhdGUgcG9v bCBwYWdlcywgYW5kIG9mIGNvdXJzZSB0aGVzZSBwYWdlcyBhcmUgYWxpZ25lZCB0byAgQVJD SF9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IGFsbG9jYXRlIGVhY2gg ZWxlbWVudCwNCmFuZCBkbWFfYWxsb2NfY29oZXJlbnQgbXVzdCBhbGlnbiB0byBBUkNIX0RN QV9NSU5BTElHTiBhbHJlYWR5Lg0KSWYgeW91IGltcGxlbWVudGF0aW9uIGRvZXNuJ3QgZG8g dGhhdCBpdCBuZWVkcyB0byBiZSBmaXhlZC4=
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 9 lines · Huacai Chen
In non-coherent DMA mode, kernel uses cache flushing operations to maintain I/O coherency, so the dmapool objects should be aligned to
ARCH_DMA_MINALIGN. Otherwise, it will cause data corruption, at least on MIPS:Step 1,
dma_map_single
Step 2,cache_invalidate(no writeback)
Step 3,dma_from_device
Step 4,dma_unmap_single
This is a massive red warning flag for the whole series, because DMA pools don't work like that. At best, this will do nothing, and at worst it is papering over egregious bugs elsewhere. Streaming mappings of coherent allocations means completely broken code.
Quoted 3 lines
If a DMA buffer and a kernel structure share a same cache line, and if the kernel structure has dirty data,
cache_invalidate(no writeback) will cause data lost.
DMA pools are backed by coherent allocations, and those should already be at *page* granularity, so this doubly cannot happen for correct code.
More generally, the whole point of having the DMA APIs is that drivers and subsystems should not have to be aware of details like hardware coherency. Besides, cache line sharing that could pose a correctness issue for non-hardware-coherent systems could still be a performance issue in the presence of hardware coherency (due to unnecessary line migration), so there's still an argument for not treating them differently.
Robin.
Quoted 12 lines
Signed-off-by Huacai Chen <[email protected]>--- mm/dmapool.c | 3 +++ 1 file changed, 3 insertions(+)mm/dmapool.c +0
-0
mm/dmapool.c struct dma_pool *dma_pool_create(const char *name, struct device *dev, · line 148
Quoted 9 lines
mm/dmapool.c +3
-0@@ -140,6 +140,9 @@ struct dma_pool *dma_pool_create(const char *name, struct device *dev,else if (align & (align -1 ))return NULL ; +if (!device_is_coherent (dev)) + align =max_t (size_t, align,dma_get_cache_alignment ()); +if (size ==0 )return NULL ;else if (size <4 )
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 11 lines · Robin Murphy
Quoted 9 lines · Huacai Chen
In non-coherent DMA mode, kernel uses cache flushing operations to maintain I/O coherency, so the dmapool objects should be aligned to
ARCH_DMA_MINALIGN. Otherwise, it will cause data corruption, at least on MIPS:Step 1,
dma_map_single
Step 2,cache_invalidate(no writeback)
Step 3,dma_from_device
Step 4,dma_unmap_single
Quoted 4 lines
This is a massive red warning flag for the whole series, because DMA pools don't work like that. At best, this will do nothing, and at worst it is papering over egregious bugs elsewhere. Streaming mappings of coherent allocations means completely broken code.
Oh, I hadn't even seen that part. Yes, dma coherent (and pool) allocations must never be used for streaming mappings. I wish we'd have some debug infrastructure to warn on such uses.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [V5, 2/3] mm: dmapool: Align to ARCH_DMA_MINALIGN innon-coherent DMA mode
T2gsIEkga25vdywgSSd2ZSBtYWtlIGEgbWlzdGFrZSwgZG1hcG9vbCBkb2Vzbid0IG5lZWQg dG8gY2hhbmdlLg0KDQpIdWFjYWkNCiANCiANCi0tLS0tLS0tLS0tLS0tLS0tLSBPcmlnaW5h bCAtLS0tLS0tLS0tLS0tLS0tLS0NCkZyb206ICAiQ2hyaXN0b3BoIEhlbGx3aWciPGhjaEBp bmZyYWRlYWQub3JnPjsNCkRhdGU6ICBNb24sIFNlcCAxOCwgMjAxNyAxMTo1MSBQTQ0KVG86 ICAiUm9iaW4gTXVycGh5Ijxyb2Jpbi5tdXJwaHlAYXJtLmNvbT47IA0KQ2M6ICAiSHVhY2Fp IENoZW4iPGNoZW5oY0BsZW1vdGUuY29tPjsgIkFuZHJldyBNb3J0b24iPGF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GZvciB0aGUgd2hvbGUgc2VyaWVzLCBiZWNhdXNl IERNQQ0KPiBwb29scyBkb24ndCB3b3JrIGxpa2UgdGhhdC4gQXQgYmVzdCwgdGhpcyB3aWxs IGRvIG5vdGhpbmcsIGFuZCBhdCB3b3JzdA0KPiBpdCBpcyBwYXBlcmluZyBvdmVyIGVncmVn aW91cyBidWdzIGVsc2V3aGVyZS4gU3RyZWFtaW5nIG1hcHBpbmdzIG9mDQo+IGNvaGVyZW50 IGFsbG9jYXRpb25zIG1lYW5zIGNvbXBsZXRlbHkgYnJva2VuIGNvZGUuDQoNCk9oLCBJIGhh ZG4ndCBldmVuIHNlZW4gdGhhdCBwYXJ0LiAgWWVzLCBkbWEgY29oZXJlbnQgKGFuZCBwb29s KQ0KYWxsb2NhdGlvbnMgbXVzdCBuZXZlciBiZSB1c2VkIGZvciBzdHJlYW1pbmcgbWFwcGlu Z3MuICBJIHdpc2ggd2UnZA0KaGF2ZSBzb21lIGRlYnVnIGluZnJhc3RydWN0dXJlIHRvIHdh cm4gb24gc3VjaCB1c2VzLg==
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Ok, it looks like adding the dma_get_cache_alignment constraint here looks good - but again it should be unconditional.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>