Re: [PATCH] dma-mapping: fix page attributes for dma_mmap_*

From: Will Deacon
Date: Thu Aug 01 2019 - 12:23:14 EST


On Thu, Aug 01, 2019 at 05:21:18PM +0300, Christoph Hellwig wrote:
> All the way back to introducing dma_common_mmap we've defaulyed to mark
> the pages as uncached. But this is wrong for DMA coherent devices or
> if using DMA_ATTR_NON_CONSISTENT. Later on DMA_ATTR_WRITE_COMBINE
> also got incorrect treatment as that flag is only treated special on
> the alloc side for non-coherent devices.
>
> Introduce a new dma_mmap_pgprot helper that deals with the check
> for coherent devices and DMA_ATTR_NON_CONSISTENT so that only the
> remapping cases even reach arch_dma_mmap_pgprot and we thus ensure
> no aliasing of page attributes happens.
>
> Signed-off-by: Christoph Hellwig <hch@xxxxxx>
> ---
> arch/arm/mm/dma-mapping.c | 4 +---
> arch/arm64/mm/dma-mapping.c | 4 +---
> arch/powerpc/kernel/Makefile | 3 +--
> arch/powerpc/kernel/dma-common.c | 17 -----------------
> drivers/iommu/dma-iommu.c | 6 +++---
> include/linux/dma-mapping.h | 1 +
> include/linux/dma-noncoherent.h | 5 -----
> kernel/dma/mapping.c | 11 ++++++++++-
> kernel/dma/remap.c | 2 +-
> 9 files changed, 18 insertions(+), 35 deletions(-)
> delete mode 100644 arch/powerpc/kernel/dma-common.c
>
> diff --git a/arch/arm/mm/dma-mapping.c b/arch/arm/mm/dma-mapping.c
> index 9c9a23e5600d..cfe44df169c5 100644
> --- a/arch/arm/mm/dma-mapping.c
> +++ b/arch/arm/mm/dma-mapping.c
> @@ -2397,9 +2397,7 @@ long arch_dma_coherent_to_pfn(struct device *dev, void *cpu_addr,
> pgprot_t arch_dma_mmap_pgprot(struct device *dev, pgprot_t prot,
> unsigned long attrs)
> {
> - if (!dev_is_dma_coherent(dev))
> - return __get_dma_pgprot(attrs, prot);
> - return prot;
> + return __get_dma_pgprot(attrs, prot);
> }
>
> void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
> diff --git a/arch/arm64/mm/dma-mapping.c b/arch/arm64/mm/dma-mapping.c
> index 1d3f0b5a9940..bd2b039f43a6 100644
> --- a/arch/arm64/mm/dma-mapping.c
> +++ b/arch/arm64/mm/dma-mapping.c
> @@ -14,9 +14,7 @@
> pgprot_t arch_dma_mmap_pgprot(struct device *dev, pgprot_t prot,
> unsigned long attrs)
> {
> - if (!dev_is_dma_coherent(dev) || (attrs & DMA_ATTR_WRITE_COMBINE))
> - return pgprot_writecombine(prot);
> - return prot;
> + return pgprot_writecombine(prot);
> }

Seems like a sensible cleanup to me:

Acked-by: Will Deacon <will@xxxxxxxxxx>

Although arch_dma_mmap_pgprot() is a bit of a misnomer now that it only
gets involved in the non-coherent case.

Will