Hi All, arch_dma_prep_coherent() is called by the generic DMA code on pages which may be located in highmem: dma_direct_alloc() forces the remap path for a highmem page returned from CMA and calls this hook on it before creating the uncached mapping. However, the hook has never been documented and some implementations assume that the page has a kernel linear mapping. This issue affects MIPS, xtensa and PowerPC. It gets fixed by patches #2-#5.
xtensa also only invalidates the cache, which discards the zeroes written by the dma_direct_alloc(). Patch #1 switches it to a writeback+invalidate. Patch #6 documents that arch_dma_prep_coherent() must write back dirty lines and must accept highmem pages. With that in place, patch #7 drops the PageHighMem() check in dma_direct_alloc_no_mapping(), which so far skipped cache maintenance for highmem CMA pages allocated with DMA_ATTR_NO_KERNEL_MAPPING. Patch #8 makes dma_direct_alloc_no_mapping() always return zeroed memory, like all other dma_alloc_* paths. Passing __GFP_ZERO is not enough there, because CMA ignores it. The analysis and the patchset was prepared with the help of an LLM. The code compiles fine, but it has not been runtime tested on any of the affected architectures yet. Testing on real hardware would be appreciated. I've reviewed the resulting changes. I plan to merge this via my dma-mapping tree, I can provide a tag if needed for merging it also to arch code. Best regards Marek Szyprowski, PhD Samsung R&D Institute Poland Changelog: v3: - added patch improving lowmem handling in __dma_sync_page() (PowerPC arch) to avoid performance regression in latter patches - added fixes tag to "powerpc/mm: handle highmem pages in arch_dma_prep_coherent()" patch - collected tags v2: https://lore.kernel.org/all/[email protected]/ - fixed minor issues in comments and patch descriptions pointed by sashiko - dropped RFC annotation v1: https://lore.kernel.org/all/[email protected]/ Patch summary: Marek Szyprowski (8): xtensa: write back dirty lines in arch_dma_prep_coherent() xtensa: handle highmem pages in arch_dma_prep_coherent() MIPS: mm: handle highmem pages in arch_dma_prep_coherent() powerpc/mm: skip kmap for lowmem pages in __dma_sync_page() powerpc/mm: handle highmem pages in arch_dma_prep_coherent() dma-mapping: document that arch_dma_prep_coherent() must handle highmem dma-direct: do not skip cache maintenance for highmem pages in no-mapping allocations dma-direct: zero memory allocated with DMA_ATTR_NO_KERNEL_MAPPING arch/mips/mm/dma-noncoherent.c | 10 +++++----- arch/powerpc/mm/dma-noncoherent.c | 16 +++++++++------- arch/xtensa/kernel/pci-dma.c | 12 +++++++++++- include/linux/dma-map-ops.h | 11 +++++++++++ kernel/dma/direct.c | 9 +++++++-- 5 files changed, 43 insertions(+), 15 deletions(-) -- 2.43.0
