Skip to content

Commit

Permalink
dma-direct: fix DMA_ATTR_NO_KERNEL_MAPPING
Browse files Browse the repository at this point in the history
DMA_ATTR_NO_KERNEL_MAPPING is to avoid creating a kernel mapping
for the allocated buffer, but current implementation is that
PTE of allocated buffer in kernel page table is valid. So we
should set invalid for PTE of allocate buffer so that there are
no kernel mapping for the allocated buffer.

In some cases, we don't hope the allocated buffer to be read
by cpu or speculative execution, so we use DMA_ATTR_NO_KERNEL_MAPPING
to get no kernel mapping in order to achieve this goal.

Signed-off-by: Walter Wu <walter-zh.wu@mediatek.com>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Marek Szyprowski <m.szyprowski@samsung.com>
Cc: Robin Murphy <robin.murphy@arm.com>
Cc: Matthias Brugger <matthias.bgg@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
  • Loading branch information
truhuan authored and intel-lab-lkp committed Nov 1, 2021
1 parent 2f111a6 commit 4694d2a
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions kernel/dma/direct.c
Expand Up @@ -13,6 +13,7 @@
#include <linux/vmalloc.h>
#include <linux/set_memory.h>
#include <linux/slab.h>
#include <asm/cacheflush.h>
#include "direct.h"

/*
Expand Down Expand Up @@ -169,6 +170,9 @@ void *dma_direct_alloc(struct device *dev, size_t size,
if (!PageHighMem(page))
arch_dma_prep_coherent(page, size);
*dma_handle = phys_to_dma_direct(dev, page_to_phys(page));
/* remove kernel mapping for pages */
set_memory_valid((unsigned long)phys_to_virt(dma_to_phys(dev, *dma_handle)),
size >> PAGE_SHIFT, 0);
/* return the page pointer as the opaque cookie */
return page;
}
Expand Down Expand Up @@ -278,6 +282,10 @@ void dma_direct_free(struct device *dev, size_t size,

if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) &&
!force_dma_unencrypted(dev) && !is_swiotlb_for_alloc(dev)) {
size = PAGE_ALIGN(size);
/* create kernel mapping for pages */
set_memory_valid((unsigned long)phys_to_virt(dma_to_phys(dev, dma_addr)),
size >> PAGE_SHIFT, 1);
/* cpu_addr is a struct page cookie, not a kernel address */
dma_free_contiguous(dev, cpu_addr, size);
return;
Expand Down

0 comments on commit 4694d2a

Please sign in to comment.