[PATCH 1/2] dma-mapping: zero memory returned from dma_alloc_*
If we want to map memory from the DMA allocator to userspace it must be zeroed at allocation time to prevent stale data leaks. We already do this on most common architectures, but some architectures don't do this yet, fix them up, either by passing GFP_ZERO when we use the normal page allocator or doing a manual memset otherwise. Signed-off-by: Christoph Hellwig --- arch/alpha/kernel/pci_iommu.c| 2 +- arch/arc/mm/dma.c| 2 +- arch/c6x/mm/dma-coherent.c | 5 - arch/m68k/kernel/dma.c | 2 +- arch/microblaze/mm/consistent.c | 2 +- arch/openrisc/kernel/dma.c | 2 +- arch/parisc/kernel/pci-dma.c | 4 ++-- arch/s390/pci/pci_dma.c | 2 +- arch/sparc/kernel/ioport.c | 2 +- arch/sparc/mm/io-unit.c | 2 +- arch/sparc/mm/iommu.c| 2 +- arch/xtensa/kernel/pci-dma.c | 2 +- drivers/misc/mic/host/mic_boot.c | 2 +- kernel/dma/virt.c| 2 +- 14 files changed, 18 insertions(+), 15 deletions(-) diff --git a/arch/alpha/kernel/pci_iommu.c b/arch/alpha/kernel/pci_iommu.c index e1716e0d92fd..28a025eda80d 100644 --- a/arch/alpha/kernel/pci_iommu.c +++ b/arch/alpha/kernel/pci_iommu.c @@ -443,7 +443,7 @@ static void *alpha_pci_alloc_coherent(struct device *dev, size_t size, gfp &= ~GFP_DMA; try_again: - cpu_addr = (void *)__get_free_pages(gfp, order); + cpu_addr = (void *)__get_free_pages(gfp | GFP_ZERO, order); if (! cpu_addr) { printk(KERN_INFO "pci_alloc_consistent: " "get_free_pages failed from %pf\n", diff --git a/arch/arc/mm/dma.c b/arch/arc/mm/dma.c index db203ff69ccf..b0754581efc6 100644 --- a/arch/arc/mm/dma.c +++ b/arch/arc/mm/dma.c @@ -33,7 +33,7 @@ void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle, */ BUG_ON(gfp & __GFP_HIGHMEM); - page = alloc_pages(gfp, order); + page = alloc_pages(gfp | GFP_ZERO, order); if (!page) return NULL; diff --git a/arch/c6x/mm/dma-coherent.c b/arch/c6x/mm/dma-coherent.c index 01305c787201..75b79571732c 100644 --- a/arch/c6x/mm/dma-coherent.c +++ b/arch/c6x/mm/dma-coherent.c @@ -78,6 +78,7 @@ static void __free_dma_pages(u32 addr, int order) void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp, unsigned long attrs) { + void *ret; u32 paddr; int order; @@ -94,7 +95,9 @@ void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, if (!paddr) return NULL; - return phys_to_virt(paddr); + ret = phys_to_virt(paddr); + memset(ret, 0, 1 << order); + return ret; } /* diff --git a/arch/m68k/kernel/dma.c b/arch/m68k/kernel/dma.c index e3c57d6b..dafe99d08a6a 100644 --- a/arch/m68k/kernel/dma.c +++ b/arch/m68k/kernel/dma.c @@ -32,7 +32,7 @@ void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, size = PAGE_ALIGN(size); order = get_order(size); - page = alloc_pages(flag, order); + page = alloc_pages(flag | GFP_ZERO, order); if (!page) return NULL; diff --git a/arch/microblaze/mm/consistent.c b/arch/microblaze/mm/consistent.c index 45e0a1aa9357..79b9f4695a1b 100644 --- a/arch/microblaze/mm/consistent.c +++ b/arch/microblaze/mm/consistent.c @@ -81,7 +81,7 @@ void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle, size = PAGE_ALIGN(size); order = get_order(size); - vaddr = __get_free_pages(gfp, order); + vaddr = __get_free_pages(gfp | GFP_ZERO, order); if (!vaddr) return NULL; diff --git a/arch/openrisc/kernel/dma.c b/arch/openrisc/kernel/dma.c index 159336adfa2f..cdd03f63207c 100644 --- a/arch/openrisc/kernel/dma.c +++ b/arch/openrisc/kernel/dma.c @@ -89,7 +89,7 @@ arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle, .mm = &init_mm }; - page = alloc_pages_exact(size, gfp); + page = alloc_pages_exact(size, gfp | GFP_ZERO); if (!page) return NULL; diff --git a/arch/parisc/kernel/pci-dma.c b/arch/parisc/kernel/pci-dma.c index 04c48f1ef3fb..7fa396714b5a 100644 --- a/arch/parisc/kernel/pci-dma.c +++ b/arch/parisc/kernel/pci-dma.c @@ -404,7 +404,7 @@ static void *pcxl_dma_alloc(struct device *dev, size_t size, order = get_order(size); size = 1 << (order + PAGE_SHIFT); vaddr = pcxl_alloc_range(size); - paddr = __get_free_pages(flag, order); + paddr = __get_free_pages(flag | GFP_ZERO, order); flush_kernel_dcache_range(paddr, size); paddr = __pa(paddr); map_uncached_pages(vaddr, size, paddr); @@ -429,7 +429,7 @@ static void *pcx_dma_alloc(struct device *dev, size_t size, if ((attrs & DMA_ATTR_NON_CONSISTENT) == 0) return NULL; - addr = (void *)__get_free_pages(flag, get_order(size)
Re: [PATCH 1/2] dma-mapping: zero memory returned from dma_alloc_*
Hi Christoph, On Fri, Dec 14, 2018 at 9:26 AM Christoph Hellwig wrote: > If we want to map memory from the DMA allocator to userspace it must be > zeroed at allocation time to prevent stale data leaks. We already do > this on most common architectures, but some architectures don't do this > yet, fix them up, either by passing GFP_ZERO when we use the normal page > allocator or doing a manual memset otherwise. > > Signed-off-by: Christoph Hellwig Thanks for your patch! > --- a/arch/m68k/kernel/dma.c > +++ b/arch/m68k/kernel/dma.c > @@ -32,7 +32,7 @@ void *arch_dma_alloc(struct device *dev, size_t size, > dma_addr_t *handle, > size = PAGE_ALIGN(size); > order = get_order(size); > > - page = alloc_pages(flag, order); > + page = alloc_pages(flag | GFP_ZERO, order); > if (!page) > return NULL; There's second implementation below, which calls __get_free_pages() and does an explicit memset(). As __get_free_pages() calls alloc_pages(), perhaps it makes sense to replace the memset() by GFP_ZERO, to increase consistency? Gr{oetje,eeting}s, Geert -- Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- ge...@linux-m68k.org In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds ___ iommu mailing list iommu@lists.linux-foundation.org https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH 1/2] dma-mapping: zero memory returned from dma_alloc_*
On Fri, Dec 14, 2018 at 10:54 AM Geert Uytterhoeven wrote: > On Fri, Dec 14, 2018 at 9:26 AM Christoph Hellwig wrote: > > If we want to map memory from the DMA allocator to userspace it must be > > zeroed at allocation time to prevent stale data leaks. We already do > > this on most common architectures, but some architectures don't do this > > yet, fix them up, either by passing GFP_ZERO when we use the normal page > > allocator or doing a manual memset otherwise. > > > > Signed-off-by: Christoph Hellwig > > Thanks for your patch! > > > --- a/arch/m68k/kernel/dma.c > > +++ b/arch/m68k/kernel/dma.c > > @@ -32,7 +32,7 @@ void *arch_dma_alloc(struct device *dev, size_t size, > > dma_addr_t *handle, > > size = PAGE_ALIGN(size); > > order = get_order(size); > > > > - page = alloc_pages(flag, order); > > + page = alloc_pages(flag | GFP_ZERO, order); > > if (!page) > > return NULL; > > There's second implementation below, which calls __get_free_pages() and > does an explicit memset(). As __get_free_pages() calls alloc_pages(), perhaps > it makes sense to replace the memset() by GFP_ZERO, to increase consistency? Regardless, for m68k: Acked-by: Geert Uytterhoeven Gr{oetje,eeting}s, Geert -- Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- ge...@linux-m68k.org In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds ___ iommu mailing list iommu@lists.linux-foundation.org https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH 1/2] dma-mapping: zero memory returned from dma_alloc_*
On Fri, Dec 14, 2018 at 10:54:32AM +0100, Geert Uytterhoeven wrote: > > - page = alloc_pages(flag, order); > > + page = alloc_pages(flag | GFP_ZERO, order); > > if (!page) > > return NULL; > > There's second implementation below, which calls __get_free_pages() and > does an explicit memset(). As __get_free_pages() calls alloc_pages(), perhaps > it makes sense to replace the memset() by GFP_ZERO, to increase consistency? It would, but this patch really tries to be minimally invasive to just provide the zeroing everywhere. There is plenty of opportunity to improve the m68k dma allocator if I can get enough reviewers/testers: - for one the coldfire/nommu case absolutely does not make sense to me as there is not work done at all to make sure the memory is mapped uncached despite the architecture implementing cache flushing for the map interface. So this whole implementation looks broken to me and will need some major work (I had a previous discussion with Greg on that which needs to be dug out) - the "regular" implementation in this patch should probably be replaced with the generic remapping helpers that have been added for the 4.21 merge window: http://git.infradead.org/users/hch/dma-mapping.git/commitdiff/0c3b3171ceccb8830c2bb5adff1b4e9b204c1450 Compile tested only patch below: -- >From ade86dc75b9850daf9111ebf9ce15825a6144f2d Mon Sep 17 00:00:00 2001 From: Christoph Hellwig Date: Fri, 14 Dec 2018 12:41:45 +0100 Subject: m68k: use the generic dma coherent remap allocator This switche to using common code for the DMA allocations, including potential use of the CMA allocator if configure. Also add a few comments where the existing behavior seems to be lacking. Signed-off-by: Christoph Hellwig --- arch/m68k/Kconfig | 2 ++ arch/m68k/kernel/dma.c | 64 -- 2 files changed, 20 insertions(+), 46 deletions(-) diff --git a/arch/m68k/Kconfig b/arch/m68k/Kconfig index 8a5868e9a3a0..60788cf02fbc 100644 --- a/arch/m68k/Kconfig +++ b/arch/m68k/Kconfig @@ -2,10 +2,12 @@ config M68K bool default y + select ARCH_HAS_DMA_MMAP_PGPROT if MMU && !COLDFIRE select ARCH_HAS_SYNC_DMA_FOR_DEVICE if HAS_DMA select ARCH_MIGHT_HAVE_PC_PARPORT if ISA select ARCH_NO_COHERENT_DMA_MMAP if !MMU select ARCH_NO_PREEMPT if !COLDFIRE + select DMA_DIRECT_REMAP if MMU && !COLDFIRE select HAVE_IDE select HAVE_AOUT if MMU select HAVE_DEBUG_BUGVERBOSE diff --git a/arch/m68k/kernel/dma.c b/arch/m68k/kernel/dma.c index dafe99d08a6a..16da5d96e228 100644 --- a/arch/m68k/kernel/dma.c +++ b/arch/m68k/kernel/dma.c @@ -18,57 +18,29 @@ #include #if defined(CONFIG_MMU) && !defined(CONFIG_COLDFIRE) - -void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, - gfp_t flag, unsigned long attrs) +void arch_dma_prep_coherent(struct page *page, size_t size) { - struct page *page, **map; - pgprot_t pgprot; - void *addr; - int i, order; - - pr_debug("dma_alloc_coherent: %d,%x\n", size, flag); - - size = PAGE_ALIGN(size); - order = get_order(size); - - page = alloc_pages(flag | GFP_ZERO, order); - if (!page) - return NULL; - - *handle = page_to_phys(page); - map = kmalloc(sizeof(struct page *) << order, flag & ~__GFP_DMA); - if (!map) { - __free_pages(page, order); - return NULL; - } - split_page(page, order); - - order = 1 << order; - size >>= PAGE_SHIFT; - map[0] = page; - for (i = 1; i < size; i++) - map[i] = page + i; - for (; i < order; i++) - __free_page(page + i); - pgprot = __pgprot(_PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_DIRTY); - if (CPU_IS_040_OR_060) - pgprot_val(pgprot) |= _PAGE_GLOBAL040 | _PAGE_NOCACHE_S; - else - pgprot_val(pgprot) |= _PAGE_NOCACHE030; - addr = vmap(map, size, VM_MAP, pgprot); - kfree(map); - - return addr; + /* +* XXX: don't we need to flush and invalidate the caches before +* creating a coherent mapping? +* coherent? +*/ } -void arch_dma_free(struct device *dev, size_t size, void *addr, - dma_addr_t handle, unsigned long attrs) +pgprot_t arch_dma_mmap_pgprot(struct device *dev, pgprot_t prot, + unsigned long attrs) { - pr_debug("dma_free_coherent: %p, %x\n", addr, handle); - vfree(addr); + /* +* XXX: this doesn't seem to handle the sun3 MMU at all. +*/ + if (CPU_IS_040_OR_060) { + pgprot_val(prot) &= ~_PAGE_CACHE040; + pgprot_val(prot) |= _PAGE_GLOBAL040 | _PAGE_NOCACHE_S; + } else { + pgprot_val(prot) |= _PAGE_NOCACHE030; + } + return prot; } - #else #include -- 2.19.2
Re: [PATCH 1/2] dma-mapping: zero memory returned from dma_alloc_*
On Fri, Dec 14, 2018 at 12:12:00PM +, Eugeniy Paltsev wrote: > Hi Christoph, > > do you have any public git repository with all your dma changes? Most of the tree show up in my misc.git repo for testing. This series is here: http://git.infradead.org/users/hch/misc.git/shortlog/refs/heads/dma-alloc-always-zero There current version of the DMA_ATTR_NON_CONSISTENT series is here: http://git.infradead.org/users/hch/misc.git/shortlog/refs/heads/dma-noncoherent-allocator.2 > I want to test them for ARC. Thanks a lot! ___ iommu mailing list iommu@lists.linux-foundation.org https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH 1/2] dma-mapping: zero memory returned from dma_alloc_*
Hi Christoph, On Fri, Dec 14, 2018 at 12:47 PM Christoph Hellwig wrote: > > On Fri, Dec 14, 2018 at 10:54:32AM +0100, Geert Uytterhoeven wrote: > > > - page = alloc_pages(flag, order); > > > + page = alloc_pages(flag | GFP_ZERO, order); > > > if (!page) > > > return NULL; > > > > There's second implementation below, which calls __get_free_pages() and > > does an explicit memset(). As __get_free_pages() calls alloc_pages(), > > perhaps > > it makes sense to replace the memset() by GFP_ZERO, to increase consistency? > > It would, but this patch really tries to be minimally invasive to just > provide the zeroing everywhere. Fair enough. > There is plenty of opportunity > to improve the m68k dma allocator if I can get enough reviewers/testers: > > - for one the coldfire/nommu case absolutely does not make sense to >me as there is not work done at all to make sure the memory is >mapped uncached despite the architecture implementing cache >flushing for the map interface. So this whole implementation >looks broken to me and will need some major work (I had a previous >discussion with Greg on that which needs to be dug out) > - the "regular" implementation in this patch should probably be replaced >with the generic remapping helpers that have been added for the 4.21 >merge window: > > > http://git.infradead.org/users/hch/dma-mapping.git/commitdiff/0c3b3171ceccb8830c2bb5adff1b4e9b204c1450 > > Compile tested only patch below: > > -- > From ade86dc75b9850daf9111ebf9ce15825a6144f2d Mon Sep 17 00:00:00 2001 > From: Christoph Hellwig > Date: Fri, 14 Dec 2018 12:41:45 +0100 > Subject: m68k: use the generic dma coherent remap allocator > > This switche to using common code for the DMA allocations, including > potential use of the CMA allocator if configure. Also add a few > comments where the existing behavior seems to be lacking. > > Signed-off-by: Christoph Hellwig Thanks, looks OK to me. M68k doesn't have many drivers using the DMA framework, as most of them predated that framework. > --- a/arch/m68k/kernel/dma.c > +++ b/arch/m68k/kernel/dma.c > > -void arch_dma_free(struct device *dev, size_t size, void *addr, > - dma_addr_t handle, unsigned long attrs) > +pgprot_t arch_dma_mmap_pgprot(struct device *dev, pgprot_t prot, > + unsigned long attrs) > { > - pr_debug("dma_free_coherent: %p, %x\n", addr, handle); > - vfree(addr); > + /* > +* XXX: this doesn't seem to handle the sun3 MMU at all. Sun-3 selects NO_DMA, and this file is compiled for the HAS_DMA case only. > +*/ > + if (CPU_IS_040_OR_060) { > + pgprot_val(prot) &= ~_PAGE_CACHE040; > + pgprot_val(prot) |= _PAGE_GLOBAL040 | _PAGE_NOCACHE_S; > + } else { > + pgprot_val(prot) |= _PAGE_NOCACHE030; > + } > + return prot; > } Gr{oetje,eeting}s, Geert -- Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- ge...@linux-m68k.org In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds ___ iommu mailing list iommu@lists.linux-foundation.org https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH 1/2] dma-mapping: zero memory returned from dma_alloc_*
On 14/12/18 9:47 pm, Christoph Hellwig wrote: On Fri, Dec 14, 2018 at 10:54:32AM +0100, Geert Uytterhoeven wrote: - page = alloc_pages(flag, order); + page = alloc_pages(flag | GFP_ZERO, order); if (!page) return NULL; There's second implementation below, which calls __get_free_pages() and does an explicit memset(). As __get_free_pages() calls alloc_pages(), perhaps it makes sense to replace the memset() by GFP_ZERO, to increase consistency? It would, but this patch really tries to be minimally invasive to just provide the zeroing everywhere. There is plenty of opportunity to improve the m68k dma allocator if I can get enough reviewers/testers: - for one the coldfire/nommu case absolutely does not make sense to me as there is not work done at all to make sure the memory is mapped uncached despite the architecture implementing cache flushing for the map interface. So this whole implementation looks broken to me and will need some major work (I had a previous discussion with Greg on that which needs to be dug out) Yep, that is right. Certainly the MMU case is broken. Some noMMU cases work by virtue of the SoC only having an instruction cache (the older V2 cores). The MMU case is fixable, but I think it will mean changing away from the fall-back virtual:physical 1:1 mapping it uses for the kernel address space. So not completely trivial. Either that or a dedicated area of RAM for coherent allocations that we can mark as non-cachable via the really course grained and limited ACR registers - not really very appealing. The noMMU case in general is probably limited to something like that same type of dedicated RAM/ACR register mechamism. The most commonly used periperal with DMA is the FEC ethernet module, and it has some "special" (used very loosely) cache flushing for parts like the 532x family which probably makes it mostly work right. There is a PCI bus on the 54xx family of parts, and I know general ethernet cards on it (like e1000's) have problems I am sure are related to the fact that coherent memory allocations aren't. I do plan to have a look at this for the MMU case some time soon. Regards Greg - the "regular" implementation in this patch should probably be replaced with the generic remapping helpers that have been added for the 4.21 merge window: http://git.infradead.org/users/hch/dma-mapping.git/commitdiff/0c3b3171ceccb8830c2bb5adff1b4e9b204c1450 Compile tested only patch below: -- From ade86dc75b9850daf9111ebf9ce15825a6144f2d Mon Sep 17 00:00:00 2001 From: Christoph Hellwig Date: Fri, 14 Dec 2018 12:41:45 +0100 Subject: m68k: use the generic dma coherent remap allocator This switche to using common code for the DMA allocations, including potential use of the CMA allocator if configure. Also add a few comments where the existing behavior seems to be lacking. Signed-off-by: Christoph Hellwig --- arch/m68k/Kconfig | 2 ++ arch/m68k/kernel/dma.c | 64 -- 2 files changed, 20 insertions(+), 46 deletions(-) diff --git a/arch/m68k/Kconfig b/arch/m68k/Kconfig index 8a5868e9a3a0..60788cf02fbc 100644 --- a/arch/m68k/Kconfig +++ b/arch/m68k/Kconfig @@ -2,10 +2,12 @@ config M68K bool default y + select ARCH_HAS_DMA_MMAP_PGPROT if MMU && !COLDFIRE select ARCH_HAS_SYNC_DMA_FOR_DEVICE if HAS_DMA select ARCH_MIGHT_HAVE_PC_PARPORT if ISA select ARCH_NO_COHERENT_DMA_MMAP if !MMU select ARCH_NO_PREEMPT if !COLDFIRE + select DMA_DIRECT_REMAP if MMU && !COLDFIRE select HAVE_IDE select HAVE_AOUT if MMU select HAVE_DEBUG_BUGVERBOSE diff --git a/arch/m68k/kernel/dma.c b/arch/m68k/kernel/dma.c index dafe99d08a6a..16da5d96e228 100644 --- a/arch/m68k/kernel/dma.c +++ b/arch/m68k/kernel/dma.c @@ -18,57 +18,29 @@ #include #if defined(CONFIG_MMU) && !defined(CONFIG_COLDFIRE) - -void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, - gfp_t flag, unsigned long attrs) +void arch_dma_prep_coherent(struct page *page, size_t size) { - struct page *page, **map; - pgprot_t pgprot; - void *addr; - int i, order; - - pr_debug("dma_alloc_coherent: %d,%x\n", size, flag); - - size = PAGE_ALIGN(size); - order = get_order(size); - - page = alloc_pages(flag | GFP_ZERO, order); - if (!page) - return NULL; - - *handle = page_to_phys(page); - map = kmalloc(sizeof(struct page *) << order, flag & ~__GFP_DMA); - if (!map) { - __free_pages(page, order); - return NULL; - } - split_page(page, order); - - order = 1 << order; - size >>= PAGE_SHIFT; - map[0] = page; - for (i = 1; i < size; i++) - map[i] = page + i; - for (; i < order; i++) - __free_page(page + i); - pgprot = __pgpro
Re: [PATCH 1/2] dma-mapping: zero memory returned from dma_alloc_*
Hi Christoph, do you have any public git repository with all your dma changes? I want to test them for ARC. Thanks. On Fri, 2018-12-14 at 09:25 +0100, Christoph Hellwig wrote: > If we want to map memory from the DMA allocator to userspace it must be > zeroed at allocation time to prevent stale data leaks. We already do > this on most common architectures, but some architectures don't do this > yet, fix them up, either by passing GFP_ZERO when we use the normal page > allocator or doing a manual memset otherwise. > > Signed-off-by: Christoph Hellwig > --- -- Eugeniy Paltsev ___ iommu mailing list iommu@lists.linux-foundation.org https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH 1/2] dma-mapping: zero memory returned from dma_alloc_*
Hi Christoph, I stumbled upon this one: #define __get_dma_pages(gfp_mask, order) \ __get_free_pages((gfp_mask) | GFP_DMA, (order)) (include/linux/gfp.h) Should it also have the __GFP_ZERO treatment? Or maybe this is already done in your tree.. As for the sparc bits: Acked-by: Sam Ravnborg [sparc] Sam ___ iommu mailing list iommu@lists.linux-foundation.org https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH 1/2] dma-mapping: zero memory returned from dma_alloc_*
On Fri, Dec 14, 2018 at 07:10:56PM +0100, Sam Ravnborg wrote: > Hi Christoph, > > I stumbled upon this one: > > #define __get_dma_pages(gfp_mask, order) \ > __get_free_pages((gfp_mask) | GFP_DMA, (order)) This isn't directly related to the dma mapping, but another place that hides GFP_DMA allocations. So no need for the treatment, but we really should kill this obsfucating wrapper.. ___ iommu mailing list iommu@lists.linux-foundation.org https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH 1/2] dma-mapping: zero memory returned from dma_alloc_*
On Sat, Dec 15, 2018 at 12:14:29AM +1000, Greg Ungerer wrote: > Yep, that is right. Certainly the MMU case is broken. Some noMMU cases work > by virtue of the SoC only having an instruction cache (the older V2 cores). Is there a good an easy case to detect if a core has a cache? Either runtime or in Kconfig? > The MMU case is fixable, but I think it will mean changing away from > the fall-back virtual:physical 1:1 mapping it uses for the kernel address > space. So not completely trivial. Either that or a dedicated area of RAM > for coherent allocations that we can mark as non-cachable via the really > course grained and limited ACR registers - not really very appealing. What about CF_PAGE_NOCACHE? Reading arch/m68k/include/asm/mcf_pgtable.h suggest this would cause an uncached mapping, in which case something like this should work: http://git.infradead.org/users/hch/misc.git/commitdiff/4b8711d436e8d56edbc5ca19aa2be639705bbfef > The noMMU case in general is probably limited to something like that same > type of dedicated RAM/ACR register mechamism. > > The most commonly used periperal with DMA is the FEC ethernet module, > and it has some "special" (used very loosely) cache flushing for > parts like the 532x family which probably makes it mostly work right. > There is a PCI bus on the 54xx family of parts, and I know general > ethernet cards on it (like e1000's) have problems I am sure are > related to the fact that coherent memory allocations aren't. If we really just care about FEC we can just switch it do use DMA_ATTR_NON_CONSISTENT and do explicit cache flushing. But as far as I can tell FEC only uses DMA coherent allocations for the TSO headers anyway, is TSO even used on this SOC? ___ iommu mailing list iommu@lists.linux-foundation.org https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH 1/2] dma-mapping: zero memory returned from dma_alloc_*
Hi Christoph, On 17/12/18 9:59 pm, Christoph Hellwig wrote: On Sat, Dec 15, 2018 at 12:14:29AM +1000, Greg Ungerer wrote: Yep, that is right. Certainly the MMU case is broken. Some noMMU cases work by virtue of the SoC only having an instruction cache (the older V2 cores). Is there a good an easy case to detect if a core has a cache? Either runtime or in Kconfig? The MMU case is fixable, but I think it will mean changing away from the fall-back virtual:physical 1:1 mapping it uses for the kernel address space. So not completely trivial. Either that or a dedicated area of RAM for coherent allocations that we can mark as non-cachable via the really course grained and limited ACR registers - not really very appealing. What about CF_PAGE_NOCACHE? Reading arch/m68k/include/asm/mcf_pgtable.h suggest this would cause an uncached mapping, in which case something like this should work: http://git.infradead.org/users/hch/misc.git/commitdiff/4b8711d436e8d56edbc5ca19aa2be639705bbfef No, that won't work. The current MMU setup for ColdFire relies on a quirk of the cache control subsystem to map kernel mapping (actually all of RAM when accessed in supervisor mode). The effective address calculation by the CPU/MMU firstly checks the RAMBAR access, then From the ColdFire 5475 Reference Manual (section 5.5.1): If virtual mode is enabled, any normal mode access that does not hit in the MMUBAR, RAMBARs, ROMBARs, or ACRs is considered a normal mode virtual address request and generates its access attributes from the MMU. For this case, the default CACR address attributes are not used. The MMUBAR is the MMU control registers, the RAMBAR/ROMBAR are the internal static RAM/ROM regions and the ACR are the cache control registers. The code in arch/m68k/coldfire/head.S sets up the ACR registers so that all of RAM is accessible and cached when in supervisor mode. So kernel code and data accesses will hit this and use the address for access. User pages won't hit this and will go through to hit the MMU mappings. The net out is we don't need page mappings or use TLB entries for kernel code/data. The problem is we also can't map individual regions as not cached for coherent allocations... The ACR mapping means all-or-nothing. This leads back to what I mentioned earlier about changing the VM mapping to not use the ACR mapping method and actually page mapping the kernel space. Not completely trivial and I expect there will be a performance hit with the extra TLB pressure and their setup/remapping overhead. The noMMU case in general is probably limited to something like that same type of dedicated RAM/ACR register mechamism. The most commonly used periperal with DMA is the FEC ethernet module, and it has some "special" (used very loosely) cache flushing for parts like the 532x family which probably makes it mostly work right. There is a PCI bus on the 54xx family of parts, and I know general ethernet cards on it (like e1000's) have problems I am sure are related to the fact that coherent memory allocations aren't. If we really just care about FEC we can just switch it do use DMA_ATTR_NON_CONSISTENT and do explicit cache flushing. But as far as I can tell FEC only uses DMA coherent allocations for the TSO headers anyway, is TSO even used on this SOC? The FEC is the most commonly used, but not the only. I test generic PCI NICs on the PCI bus on the ColdFire 5475 - and a lot of those drivers rely on coherent allocations. Regards Greg ___ iommu mailing list iommu@lists.linux-foundation.org https://lists.linuxfoundation.org/mailman/listinfo/iommu