Commit 566fb90e authored by Christoph Hellwig's avatar Christoph Hellwig
Browse files

swiotlb-xen: fix DMA_ATTR_NO_KERNEL_MAPPING on arm



swiotlb-xen uses very different ways to allocate coherent memory on x86
vs arm.  On the former it allocates memory from the page allocator, while
on the later it reuses the dma-direct allocator the handles the
complexities of non-coherent DMA on arm platforms.

Unfortunately the complexities of trying to deal with the two cases in
the swiotlb-xen.c code lead to a bug in the handling of
DMA_ATTR_NO_KERNEL_MAPPING on arm.  With the DMA_ATTR_NO_KERNEL_MAPPING
flag the coherent memory allocator does not actually allocate coherent
memory, but just a DMA handle for some memory that is DMA addressable
by the device, but which does not have to have a kernel mapping.  Thus
dereferencing the return value will lead to kernel crashed and memory
corruption.

Fix this by using the dma-direct allocator directly for arm, which works
perfectly fine because on arm swiotlb-xen is only used when the domain is
1:1 mapped, and then simplifying the remaining code to only cater for the
x86 case with DMA coherent device.

Reported-by: default avatarRahul Singh <Rahul.Singh@arm.com>
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
Reviewed-by: default avatarRahul Singh <rahul.singh@arm.com>
Reviewed-by: default avatarStefano Stabellini <sstabellini@kernel.org>
Tested-by: default avatarRahul Singh <rahul.singh@arm.com>
parent 3cb4503a
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
/* SPDX-License-Identifier: GPL-2.0 */
#include <xen/arm/page-coherent.h>
+0 −14
Original line number Diff line number Diff line
@@ -116,20 +116,6 @@ bool xen_arch_need_swiotlb(struct device *dev,
		!dev_is_dma_coherent(dev));
}

int xen_create_contiguous_region(phys_addr_t pstart, unsigned int order,
				 unsigned int address_bits,
				 dma_addr_t *dma_handle)
{
	/* the domain is 1:1 mapped to use swiotlb-xen */
	*dma_handle = pstart;
	return 0;
}

void xen_destroy_contiguous_region(phys_addr_t pstart, unsigned int order)
{
	return;
}

static int __init xen_mm_init(void)
{
	struct gnttab_cache_flush cflush;
+0 −2
Original line number Diff line number Diff line
/* SPDX-License-Identifier: GPL-2.0 */
#include <xen/arm/page-coherent.h>
+0 −24
Original line number Diff line number Diff line
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _ASM_X86_XEN_PAGE_COHERENT_H
#define _ASM_X86_XEN_PAGE_COHERENT_H

#include <asm/page.h>
#include <linux/dma-mapping.h>

static inline void *xen_alloc_coherent_pages(struct device *hwdev, size_t size,
		dma_addr_t *dma_handle, gfp_t flags,
		unsigned long attrs)
{
	void *vstart = (void*)__get_free_pages(flags, get_order(size));
	*dma_handle = virt_to_phys(vstart);
	return vstart;
}

static inline void xen_free_coherent_pages(struct device *hwdev, size_t size,
		void *cpu_addr, dma_addr_t dma_handle,
		unsigned long attrs)
{
	free_pages((unsigned long) cpu_addr, get_order(size));
}

#endif /* _ASM_X86_XEN_PAGE_COHERENT_H */
+6 −0
Original line number Diff line number Diff line
@@ -8,4 +8,10 @@ extern int pci_xen_swiotlb_init_late(void);
static inline int pci_xen_swiotlb_init_late(void) { return -ENXIO; }
#endif

int xen_swiotlb_fixup(void *buf, unsigned long nslabs);
int xen_create_contiguous_region(phys_addr_t pstart, unsigned int order,
				unsigned int address_bits,
				dma_addr_t *dma_handle);
void xen_destroy_contiguous_region(phys_addr_t pstart, unsigned int order);

#endif /* _ASM_X86_SWIOTLB_XEN_H */
Loading