cleanup vmap usage in the dma-mapping layer
Subsystems (7)
Hi all,
the common DMA remapping code uses the vmalloc/vmap code to create page table entries for DMA mappings. This series lifts the currently arm specific VM_* flag for that into common code, and also exposes it to userspace in procfs to better understand the mappings.
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH 1/4 vmalloc: lift the arm flag for coherent mappings to common code
The arm architecture had a VM_ARM_DMA_CONSISTENT flag to mark DMA coherent remapping for a while. Lift this flag to common code so that we can use it generically. We also check it in the only place VM_USERMAP is directly check so that we can entirely replace that flag as well (although I'm not even sure why we'd want to allow remapping DMA appings, but I'd rather not change behavior).
--- arch/arm/mm/dma-mapping.c | 22 +++++++--------------- arch/arm/mm/mm.h | 3 --- include/linux/vmalloc.h | 2 ++ mm/vmalloc.c | 5 ++++- 4 files changed, 13 insertions(+), 19 deletions(-)
arch/arm/mm/dma-mapping.c +7 -15
@@ -340,19 +340,13 @@ static void * __dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot, const void *caller) { - /* - * DMA allocation can be mapped to user space, so lets - * set VM_USERMAP flags too. - */ - return dma_common_contiguous_remap(page, size, - VM_ARM_DMA_CONSISTENT | VM_USERMAP, + return dma_common_contiguous_remap(page, size, VM_DMA_COHERENT, prot, caller); }static void __dma_free_remap (void *cpu_addr,size_t size) { -dma_common_free_remap (cpu_addr, size, -VM_ARM_DMA_CONSISTENT |VM_USERMAP ); +dma_common_free_remap (cpu_addr, size,VM_DMA_COHERENT ); }#define DEFAULT_DMA_COHERENT_POOL_SIZE SZ_256K
@@ -1379,8 +1373,8 @@ static void * __iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot, const void *caller) { - return dma_common_pages_remap(pages, size, - VM_ARM_DMA_CONSISTENT | VM_USERMAP, prot, caller); + return dma_common_pages_remap(pages, size, VM_DMA_COHERENT, prot, + caller); }/*
@@ -1464,7 +1458,7 @@ static struct page **__iommu_get_pages(void *cpu_addr, unsigned long attrs)return cpu_addr; area =find_vm_area (cpu_addr); -if (area && (area->flags &VM_ARM_DMA_CONSISTENT )) +if (area && (area->flags &VM_DMA_COHERENT ))return area->pages;return NULL ; }
@@ -1622,10 +1616,8 @@ void __arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,return ; } -if ((attrs &DMA_ATTR_NO_KERNEL_MAPPING ) ==0 ) { -dma_common_free_remap (cpu_addr, size, -VM_ARM_DMA_CONSISTENT |VM_USERMAP ); - } +if ((attrs &DMA_ATTR_NO_KERNEL_MAPPING ) ==0 ) +dma_common_free_remap (cpu_addr, size,VM_DMA_COHERENT );__iommu_remove_mapping (dev, handle, size);__iommu_free_buffer (dev, pages, size, attrs);
arch/arm/mm/mm.h +0 -3
@@ -70,9 +70,6 @@ extern void __flush_dcache_page(struct address_space *mapping, struct page *page#define VM_ARM_MTYPE (mt) ((mt) << 20)#define VM_ARM_MTYPE_MASK (0x1f << 20) -/* consistent regions used by dma_alloc_attrs() */ -#define VM_ARM_DMA_CONSISTENT 0x20000000 -struct static_vm {struct vm_struct vm;
include/linux/vmalloc.h +2 -0
@@ -18,6 +18,7 @@ struct notifier_block; /* in notifier.h */#define VM_ALLOC 0x00000002/* vmalloc() */ #define VM_MAP 0x00000004/* vmap()ed pages */ #define VM_USERMAP 0x00000008/* suitable for remap_vmalloc_range */ +#define VM_DMA_COHERENT 0x00000010/* dma_alloc_coherent */ #define VM_UNINITIALIZED 0x00000020/* vm_struct is not fully initialized */ #define VM_NO_GUARD 0x00000040/* don't add guard page */ #define VM_KASAN 0x00000080/* has allocated kasan shadow memory */
@@ -26,6 +27,7 @@ struct notifier_block; /* in notifier.h */* vfree_atomic(). */ #define VM_FLUSH_RESET_PERMS 0x00000100/* Reset direct map and flush TLB on unmap */ +/* bits [20..32] reserved for arch specific ioremap internals */ /*
mm/vmalloc.c +4 -1
@@ -2993,7 +2993,7 @@ int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr,if (!area)return -EINVAL ; -if (!(area->flags &VM_USERMAP )) +if (!(area->flags & (VM_USERMAP |VM_DMA_COHERENT )))return -EINVAL ;if (kaddr + size > area->addr +get_vm_area_size (area))
@@ -3496,6 +3496,9 @@ static int s_show(struct seq_file *m, void *p)if (v->flags &VM_USERMAP )seq_puts (m," user" ); +if (v->flags &VM_DMA_COHERENT ) +seq_puts (m," dma-coherent" ); +if (is_vmalloc_addr (v->pages))seq_puts (m," vpages" );
Signature · 1 line
2.20.1
Russell King - ARM Linux admin
maintainerRe: [PATCH 1/4] vmalloc: lift the arm flag for coherent mappings to common code
Quoted 6 lines · Christoph Hellwig
The arm architecture had a
VM_ARM_DMA_CONSISTENTflag to mark DMA coherent remapping for a while. Lift this flag to common code so that we can use it generically. We also check it in the only placeVM_USERMAPis directly check so that we can entirely replace that flag as well (although I'm not even sure why we'd want to allow remapping DMA appings, but I'd rather not change behavior).
Good, because if you did change that behaviour, you'd break almost every ARM framebuffer and cripple ARM audio drivers.
Signature · 3 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTC broadband for 0.8mile line in suburbia: sync at 12.1Mbps down 622kbps up According to speedtest.net: 11.9Mbps down 500kbps up
Re: [PATCH 1/4] vmalloc: lift the arm flag for coherent mappings to common code
Quoted 10 lines · Russell King - ARM Linux admin
Quoted 6 lines · Christoph Hellwig
The arm architecture had a
VM_ARM_DMA_CONSISTENTflag to mark DMA coherent remapping for a while. Lift this flag to common code so that we can use it generically. We also check it in the only placeVM_USERMAPis directly check so that we can entirely replace that flag as well (although I'm not even sure why we'd want to allow remapping DMA appings, but I'd rather not change behavior).Good, because if you did change that behaviour, you'd break almost every ARM framebuffer and cripple ARM audio drivers.
How would that break them? All the usual video and audio drivers that use dma_alloc_* then use dma_mmap_* which never end up in the only place that actually checks VM_USERMAP (remap_vmalloc_range_partial) as they end up in the dma_map_ops mmap methods which contain what is effecitvely open coded versions of that routine. There are very few callers of remap_vmalloc_range_partial / remap_vmalloc_range, and while a few of those actually are in media drivers and the virtual frame buffer video driver, none of these seems to be called on dma memory (which would be a layering violation anyway).
PATCH 2/4 dma-mapping: always use VM_DMA_COHERENT for generic DMA remap
In reply to Christoph HellwigCurrently the generic dma remap allocator gets a vm_flags passed by the caller that is a little confusing. We just introduced a generic vmalloc-level flag to identify the dma coherent allocations, so use that everywhere and remove the now pointless argument.
--- arch/arm/mm/dma-mapping.c | 10 ++++------ arch/xtensa/kernel/pci-dma.c | 4 ++-- drivers/iommu/dma-iommu.c | 6 +++--- include/linux/dma-mapping.h | 6 ++---- kernel/dma/remap.c | 25 +++++++++++-------------- 5 files changed, 22 insertions(+), 29 deletions(-)
arch/arm/mm/dma-mapping.c +4 -6
@@ -340,13 +340,12 @@ static void * __dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot, const void *caller) { - return dma_common_contiguous_remap(page, size, VM_DMA_COHERENT, - prot, caller); + return dma_common_contiguous_remap(page, size, prot, caller); }static void __dma_free_remap (void *cpu_addr,size_t size) { -dma_common_free_remap (cpu_addr, size,VM_DMA_COHERENT ); +dma_common_free_remap (cpu_addr, size); }#define DEFAULT_DMA_COHERENT_POOL_SIZE SZ_256K
@@ -1373,8 +1372,7 @@ static void * __iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot, const void *caller) { - return dma_common_pages_remap(pages, size, VM_DMA_COHERENT, prot, - caller); + return dma_common_pages_remap(pages, size, prot, caller); }/*
@@ -1617,7 +1615,7 @@ void __arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr, }if ((attrs &DMA_ATTR_NO_KERNEL_MAPPING ) ==0 ) -dma_common_free_remap (cpu_addr, size,VM_DMA_COHERENT ); +dma_common_free_remap (cpu_addr, size);__iommu_remove_mapping (dev, handle, size);__iommu_free_buffer (dev, pages, size, attrs);
arch/xtensa/kernel/pci-dma.c +2 -2
@@ -167,7 +167,7 @@ void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,if (PageHighMem (page)) {void *p; - p =dma_common_contiguous_remap (page, size,VM_MAP , + p =dma_common_contiguous_remap (page, size,pgprot_noncached (PAGE_KERNEL ),__builtin_return_address (0 ));if (!p) {
@@ -192,7 +192,7 @@ void arch_dma_free(struct device *dev, size_t size, void *vaddr, page =virt_to_page (platform_vaddr_to_cached (vaddr)); }else {#ifdef CONFIG_MMU -dma_common_free_remap (vaddr, size,VM_MAP ); +dma_common_free_remap (vaddr, size);#endif page =pfn_to_page (PHYS_PFN (dma_to_phys (dev, dma_handle))); }
drivers/iommu/dma-iommu.c +3 -3
@@ -617,7 +617,7 @@ static void *iommu_dma_alloc_remap(struct device *dev, size_t size, < size) goto out_free_sg; - vaddr =dma_common_pages_remap (pages, size,VM_USERMAP , prot, + vaddr =dma_common_pages_remap (pages, size, prot,__builtin_return_address (0 ));if (!vaddr) goto out_unmap;
@@ -941,7 +941,7 @@ static void __iommu_dma_free(struct device *dev, size_t size, void *cpu_addr) pages =__iommu_dma_get_pages (cpu_addr);if (!pages) page =vmalloc_to_page (cpu_addr); -dma_common_free_remap (cpu_addr, alloc_size,VM_USERMAP ); +dma_common_free_remap (cpu_addr, alloc_size); }else {/* Lowmem means a coherent atomic or CMA allocation */ page =virt_to_page (cpu_addr);
@@ -979,7 +979,7 @@ static void *iommu_dma_alloc_pages(struct device *dev, size_t size,pgprot_t prot =dma_pgprot (dev,PAGE_KERNEL , attrs); cpu_addr =dma_common_contiguous_remap (page, alloc_size, -VM_USERMAP , prot,__builtin_return_address (0 )); + prot,__builtin_return_address (0 ));if (!cpu_addr) goto out_free_pages;
include/linux/dma-mapping.h +2 -4
@@ -616,13 +616,11 @@ extern int dma_common_mmap(struct device *dev, struct vm_area_struct *vma, unsigned long attrs);void *dma_common_contiguous_remap (struct page *page,size_t size, -unsigned long vm_flags,pgprot_t prot,const void *caller);void *dma_common_pages_remap (struct page **pages,size_t size, -unsigned long vm_flags,pgprot_t prot, -const void *caller); -void dma_common_free_remap (void *cpu_addr,size_t size,unsigned long vm_flags); +pgprot_t prot,const void *caller); +void dma_common_free_remap (void *cpu_addr,size_t size);int __init dma_atomic_pool_init (gfp_t gfp,pgprot_t prot);bool dma_in_atomic_pool (void *start,size_t size);
kernel/dma/remap.c +11 -14
@@ -12,12 +12,11 @@#include <linux/vmalloc.h> static struct vm_struct *__dma_common_pages_remap (struct page **pages, -size_t size,unsigned long vm_flags,pgprot_t prot, -const void *caller) +size_t size,pgprot_t prot,const void *caller) {struct vm_struct *area; - area =get_vm_area_caller (size, vm_flags, caller); + area =get_vm_area_caller (size,VM_DMA_COHERENT , caller);if (!area)return NULL ;
@@ -34,12 +33,11 @@ static struct vm_struct *__dma_common_pages_remap(struct page **pages,* Cannot be used in non-sleeping contexts */ void *dma_common_pages_remap (struct page **pages,size_t size, -unsigned long vm_flags,pgprot_t prot, -const void *caller) +pgprot_t prot,const void *caller) {struct vm_struct *area; - area =__dma_common_pages_remap (pages, size, vm_flags, prot, caller); + area =__dma_common_pages_remap (pages, size, prot, caller);if (!area)return NULL ;
@@ -53,7 +51,6 @@ void *dma_common_pages_remap(struct page **pages, size_t size,* Cannot be used in non-sleeping contexts */ void *dma_common_contiguous_remap (struct page *page,size_t size, -unsigned long vm_flags,pgprot_t prot,const void *caller) {int i;
@@ -67,7 +64,7 @@ void *dma_common_contiguous_remap(struct page *page, size_t size,for (i =0 ; i < (size >>PAGE_SHIFT ); i++) pages[i] =nth_page (page, i); - area =__dma_common_pages_remap (pages, size, vm_flags, prot, caller); + area =__dma_common_pages_remap (pages, size, prot, caller);kfree (pages);
@@ -79,11 +76,11 @@ void *dma_common_contiguous_remap(struct page *page, size_t size,/* * Unmaps a range previously mapped by dma_common_*_remap */ -void dma_common_free_remap (void *cpu_addr,size_t size,unsigned long vm_flags) +void dma_common_free_remap (void *cpu_addr,size_t size) {struct vm_struct *area =find_vm_area (cpu_addr); -if (!area || (area->flags & vm_flags) != vm_flags) { +if (!area || area->flags !=VM_DMA_COHERENT ) {WARN (1 ,"trying to free invalid coherent area: %p\n" , cpu_addr);return ; }
@@ -127,8 +124,8 @@ int __init dma_atomic_pool_init(gfp_t gfp, pgprot_t prot)if (!atomic_pool) goto free_page; - addr =dma_common_contiguous_remap (page, atomic_pool_size,VM_USERMAP , - prot,__builtin_return_address (0 )); + addr =dma_common_contiguous_remap (page, atomic_pool_size, prot, +__builtin_return_address (0 ));if (!addr) goto destroy_genpool;
@@ -143,7 +140,7 @@ int __init dma_atomic_pool_init(gfp_t gfp, pgprot_t prot)return 0 ; remove_mapping: -dma_common_free_remap (addr, atomic_pool_size,VM_USERMAP ); +dma_common_free_remap (addr, atomic_pool_size); destroy_genpool:gen_pool_destroy (atomic_pool); atomic_pool =NULL ;
@@ -217,7 +214,7 @@ void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,arch_dma_prep_coherent (page, size);/* create a coherent mapping */ - ret =dma_common_contiguous_remap (page, size,VM_USERMAP , + ret =dma_common_contiguous_remap (page, size,dma_pgprot (dev,PAGE_KERNEL , attrs),__builtin_return_address (0 ));if (!ret) {
Signature · 1 line
2.20.1
PATCH 3/4 dma-mapping: introduce a dma_common_find_pages helper
In reply to Christoph HellwigA helper to find the backing page array based on a virtual address. This also ensures we do the same vm_flags check everywhere instead of slightly different or missing ones in a few places.
--- arch/arm/mm/dma-mapping.c | 7 +------ drivers/iommu/dma-iommu.c | 15 +++------------ include/linux/dma-mapping.h | 1 + kernel/dma/remap.c | 13 +++++++++++-- 4 files changed, 16 insertions(+), 20 deletions(-)
arch/arm/mm/dma-mapping.c +1 -6
@@ -1447,18 +1447,13 @@ static struct page **__atomic_get_pages(void *addr)static struct page **__iommu_get_pages (void *cpu_addr,unsigned long attrs) { -struct vm_struct *area; -if (__in_atomic_pool (cpu_addr,PAGE_SIZE ))return __atomic_get_pages (cpu_addr);if (attrs &DMA_ATTR_NO_KERNEL_MAPPING )return cpu_addr; - area =find_vm_area (cpu_addr); -if (area && (area->flags &VM_DMA_COHERENT )) -return area->pages; -return NULL ; +return dma_common_find_pages (cpu_addr); } static void *__iommu_alloc_simple(struct device *dev, size_t size, gfp_t gfp,
drivers/iommu/dma-iommu.c +3 -12
@@ -541,15 +541,6 @@ static struct page **__iommu_dma_alloc_pages(struct device *dev,return pages; } -static struct page **__iommu_dma_get_pages (void *cpu_addr) -{ -struct vm_struct *area =find_vm_area (cpu_addr); - -if (!area || !area->pages) -return NULL ; -return area->pages; -} -/** * iommu_dma_alloc_remap - Allocate and map a buffer contiguous in IOVA space * @dev: Device to allocate memory for. Must be a real device
@@ -938,7 +929,7 @@ static void __iommu_dma_free(struct device *dev, size_t size, void *cpu_addr)* If it the address is remapped, then it's either non-coherent * or highmem CMA, or an iommu_dma_alloc_remap() construction. */ - pages =__iommu_dma_get_pages (cpu_addr); + pages =dma_common_find_pages (cpu_addr);if (!pages) page =vmalloc_to_page (cpu_addr);dma_common_free_remap (cpu_addr, alloc_size);
@@ -1045,7 +1036,7 @@ static int iommu_dma_mmap(struct device *dev, struct vm_area_struct *vma,return -ENXIO ;if (IS_ENABLED (CONFIG_DMA_REMAP ) &&is_vmalloc_addr (cpu_addr)) { -struct page **pages =__iommu_dma_get_pages (cpu_addr); +struct page **pages =dma_common_find_pages (cpu_addr);if (pages)return __iommu_dma_mmap (pages, size, vma);
@@ -1067,7 +1058,7 @@ static int iommu_dma_get_sgtable(struct device *dev, struct sg_table *sgt,int ret;if (IS_ENABLED (CONFIG_DMA_REMAP ) &&is_vmalloc_addr (cpu_addr)) { -struct page **pages =__iommu_dma_get_pages (cpu_addr); +struct page **pages =dma_common_find_pages (cpu_addr);if (pages) { return sg_alloc_table_from_pages(sgt, pages,
include/linux/dma-mapping.h +1 -0
@@ -615,6 +615,7 @@ extern int dma_common_mmap(struct device *dev, struct vm_area_struct *vma, void *cpu_addr, dma_addr_t dma_addr, size_t size, unsigned long attrs); +struct page **dma_common_find_pages (void *cpu_addr);void *dma_common_contiguous_remap (struct page *page,size_t size,pgprot_t prot,const void *caller);
kernel/dma/remap.c +11 -2
@@ -11,6 +11,15 @@#include <linux/slab.h> #include <linux/vmalloc.h> +struct page **dma_common_find_pages (void *cpu_addr) +{ +struct vm_struct *area =find_vm_area (cpu_addr); + +if (!area || area->flags !=VM_DMA_COHERENT ) +return NULL ; +return area->pages; +} +static struct vm_struct *__dma_common_pages_remap (struct page **pages,size_t size,pgprot_t prot,const void *caller) {
@@ -78,9 +87,9 @@ void *dma_common_contiguous_remap(struct page *page, size_t size,*/ void dma_common_free_remap (void *cpu_addr,size_t size) { -struct vm_struct *area =find_vm_area (cpu_addr); +struct page **pages =dma_common_find_pages (cpu_addr); -if (!area || area->flags !=VM_DMA_COHERENT ) { +if (!pages) {WARN (1 ,"trying to free invalid coherent area: %p\n" , cpu_addr);return ; }
Signature · 1 line
2.20.1
Re: [PATCH 3/4] dma-mapping: introduce a dma_common_find_pages helper
Hi Christoph,
Quoted 5 lines · Christoph Hellwig
A helper to find the backing page array based on a virtual address. This also ensures we do the same
vm_flagscheck everywhere instead of slightly different or missing ones in a few places.Signed-off-by Christoph Hellwig <[email protected]>
This is now commit 5cf4537975bbd569 ("dma-mapping: introduce a dma_common_find_pages helper") in v5.4-rc1, and causes the following warning during s2ram on r8a7740/armadillo, r7s72100/rskrza1, and r7s9210/rza2mevb:
sh-eth e9a00000.ethernet eth0: Link is Down
+------------[ cut here ]------------
+WARNING: CPU: 0 PID: 556 at kernel/dma/remap.c:93
remap_allocator_free+0x20/0x30
+trying to free invalid coherent area: 6909579a
+Modules linked in:
+CPU: 0 PID: 556 Comm: s2ram Not tainted
5.3.0-rc6-armadillo-00027-g5cf4537975bbd569-dirty #113
+Hardware name: Generic R8A7740 (Flattened Device Tree)
+[<c010d4b8>] (unwind_backtrace) from [<c010b4d0>] (show_stack+0x10/0x14)
+[<c010b4d0>] (show_stack) from [<c011d978>] (__warn+0xec/0x104)
+[<c011d978>] (__warn) from [<c011d9d4>] (warn_slowpath_fmt+0x44/0x6c)
+[<c011d9d4>] (warn_slowpath_fmt) from [<c011123c>]
(remap_allocator_free+0x20/0x30)
+[<c011123c>] (remap_allocator_free) from [<c0111cc4>]
(__arm_dma_free.constprop.2+0x114/0x144)
+[<c0111cc4>] (__arm_dma_free.constprop.2) from [<c03f193c>]
(sh_eth_ring_free+0xb8/0x114)
+[<c03f193c>] (sh_eth_ring_free) from [<c03f1a00>] (sh_eth_close+0x68/0x8c)
+[<c03f1a00>] (sh_eth_close) from [<c03f1f6c>] (sh_eth_resume+0x44/0x90)
+[<c03f1f6c>] (sh_eth_resume) from [<c03b7d3c>] (dpm_run_callback+0x64/0xdc)
+[<c03b7d3c>] (dpm_run_callback) from [<c03b80c0>]
(device_resume+0xbc/0x180)
+[<c03b80c0>] (device_resume) from [<c03b89b0>] (dpm_resume+0x124/0x1b0)
+[<c03b89b0>] (dpm_resume) from [<c03b8c5c>] (dpm_resume_end+0xc/0x18)
+[<c03b8c5c>] (dpm_resume_end) from [<c0158d2c>]
(suspend_devices_and_enter+0x15c/0x5ac)
+[<c0158d2c>] (suspend_devices_and_enter) from [<c01593bc>]
(pm_suspend+0x240/0x2f4)
+[<c01593bc>] (pm_suspend) from [<c0157b48>] (state_store+0x54/0x8c)
+[<c0157b48>] (state_store) from [<c0276ecc>] (kernfs_fop_write+0x154/0x1c8)
+[<c0276ecc>] (kernfs_fop_write) from [<c0209f74>] (__vfs_write+0x28/0xe0)
+[<c0209f74>] (__vfs_write) from [<c020acc0>] (vfs_write+0x98/0xbc)
+[<c020acc0>] (vfs_write) from [<c020ae48>] (ksys_write+0x68/0xb4)
+[<c020ae48>] (ksys_write) from [<c0101000>] (ret_fast_syscall+0x0/0x28)
+Exception stack(0xdd74dfa8 to 0xdd74dff0)
+dfa0: 00000004 000e2408 00000001 000e2408
00000004 00000000
+dfc0: 00000004 000e2408 b6f36d60 00000004 000e2408 00000004
00000000 00000000
+dfe0: 00000000 be9fc74c b6e991bb b6ed5af6
+irq event stamp: 0
+hardirqs last enabled at (0): [<00000000>] 0x0
+hardirqs last disabled at (0): [<c011b73c>] copy_process+0x520/0x14b8
+softirqs last enabled at (0): [<c011b73c>] copy_process+0x520/0x14b8
+softirqs last disabled at (0): [<00000000>] 0x0
+---[ end trace 22461a068edbf2c1 ]---
+------------[ cut here ]------------
+WARNING: CPU: 0 PID: 556 at kernel/dma/remap.c:93
remap_allocator_free+0x20/0x30
+trying to free invalid coherent area: f39c52ba [...]
+---[ end trace 22461a068edbf2c2 ]---
SMSC LAN8710/LAN8720 e9a00000.ethernet-ffffffff:00: attached PHYdriver [SMSC LAN8710/LAN8720] (mii_bus:phy_addr=e9a00000.ethernet-ffffffff:00, irq=POLL)
(the dirty is due to the need for "ARM: fix __get_user_check() in case
uaccess_* calls are not inlined").
BTW, I cannot trigger the issue on r8a7791/koelsch, which also uses sh-eth, not even when disabling CONFIG_IOMMU_SUPPORT and CONFIG_ARM_LPAE (both are not set for the affected platforms).
drivers/iommu/dma-iommu.c static struct page **__iommu_dma_alloc_pages(struct device *dev, · line 548
Quoted 11 lines
drivers/iommu/dma-iommu.c +0
-5@@ -541,15 +541,6 @@ static struct page **__iommu_dma_alloc_pages(struct device *dev,return pages; } -static struct page **__iommu_dma_get_pages (void *cpu_addr) -{ -struct vm_struct *area =find_vm_area (cpu_addr); - -if (!area || !area->pages)
This checks area->pages...
Quoted 38 lines
drivers/iommu/dma-iommu.c +3
-7-return NULL ; -return area->pages; -} -/** * iommu_dma_alloc_remap - Allocate and map a buffer contiguous in IOVA space * @dev: Device to allocate memory for. Must be a real device @@ -938,7 +929,7 @@ static void __iommu_dma_free(struct device *dev, size_t size, void *cpu_addr)* If it the address is remapped, then it's either non-coherent * or highmem CMA, or an iommu_dma_alloc_remap() construction. */ - pages =__iommu_dma_get_pages (cpu_addr); + pages =dma_common_find_pages (cpu_addr);if (!pages) page =vmalloc_to_page (cpu_addr);dma_common_free_remap (cpu_addr, alloc_size);@@ -1045,7 +1036,7 @@ static int iommu_dma_mmap(struct device *dev, struct vm_area_struct *vma,return -ENXIO ;if (IS_ENABLED (CONFIG_DMA_REMAP ) &&is_vmalloc_addr (cpu_addr)) { -struct page **pages =__iommu_dma_get_pages (cpu_addr); +struct page **pages =dma_common_find_pages (cpu_addr);if (pages)return __iommu_dma_mmap (pages, size, vma);@@ -1067,7 +1058,7 @@ static int iommu_dma_get_sgtable(struct device *dev, struct sg_table *sgt,int ret;if (IS_ENABLED (CONFIG_DMA_REMAP ) &&is_vmalloc_addr (cpu_addr)) { -struct page **pages =__iommu_dma_get_pages (cpu_addr); +struct page **pages =dma_common_find_pages (cpu_addr);if (pages) { return sg_alloc_table_from_pages(sgt, pages,
kernel/dma/remap.c line 18
Quoted 8 lines
kernel/dma/remap.c +5
-0@@ -11,6 +11,15 @@#include <linux/slab.h> #include <linux/vmalloc.h> +struct page **dma_common_find_pages (void *cpu_addr) +{ +struct vm_struct *area =find_vm_area (cpu_addr); + +if (!area || area->flags !=VM_DMA_COHERENT )
... while this one checks area->flags?
kernel/dma/remap.c line 25
Quoted 7 lines
kernel/dma/remap.c +4
-0+return NULL ; +return area->pages; +} +static struct vm_struct *__dma_common_pages_remap (struct page **pages,size_t size,pgprot_t prot,const void *caller) {
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] 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
Robin Murphy
maintainerRe: [PATCH 3/4] dma-mapping: introduce a dma_common_find_pages helper
Quoted 86 lines · Geert Uytterhoeven
Hi Christoph,
Quoted 5 lines · Christoph Hellwig
A helper to find the backing page array based on a virtual address. This also ensures we do the same
vm_flagscheck everywhere instead of slightly different or missing ones in a few places.Signed-off-by Christoph Hellwig <[email protected]>This is now commit
5cf4537975bbd569("dma-mapping: introduce adma_common_find_pageshelper") in v5.4-rc1, and causes the following warning during s2ram on r8a7740/armadillo, r7s72100/rskrza1, and r7s9210/rza2mevb:sh-eth e9a00000.ethernet eth0: Link is Down +------------[ cut here ]------------ +WARNING: CPU: 0 PID: 556 at kernel/dma/remap.c:93 remap_allocator_free+0x20/0x30 +trying to free invalid coherent area: 6909579a +Modules linked in: +CPU: 0 PID: 556 Comm: s2ram Not tainted 5.3.0-rc6-armadillo-00027-g5cf4537975bbd569-dirty #113 +Hardware name: Generic R8A7740 (Flattened Device Tree) +[<c010d4b8>] (unwind_backtrace) from [<c010b4d0>] (show_stack+0x10/0x14) +[<c010b4d0>] (show_stack) from [<c011d978>] (__warn+0xec/0x104) +[<c011d978>] (__warn) from [<c011d9d4>] (warn_slowpath_fmt+0x44/0x6c) +[<c011d9d4>] (warn_slowpath_fmt) from [<c011123c>] (remap_allocator_free+0x20/0x30) +[<c011123c>] (remap_allocator_free) from [<c0111cc4>] (__arm_dma_free.constprop.2+0x114/0x144) +[<c0111cc4>] (__arm_dma_free.constprop.2) from [<c03f193c>] (sh_eth_ring_free+0xb8/0x114) +[<c03f193c>] (sh_eth_ring_free) from [<c03f1a00>] (sh_eth_close+0x68/0x8c) +[<c03f1a00>] (sh_eth_close) from [<c03f1f6c>] (sh_eth_resume+0x44/0x90) +[<c03f1f6c>] (sh_eth_resume) from [<c03b7d3c>] (dpm_run_callback+0x64/0xdc) +[<c03b7d3c>] (dpm_run_callback) from [<c03b80c0>] (device_resume+0xbc/0x180) +[<c03b80c0>] (device_resume) from [<c03b89b0>] (dpm_resume+0x124/0x1b0) +[<c03b89b0>] (dpm_resume) from [<c03b8c5c>] (dpm_resume_end+0xc/0x18) +[<c03b8c5c>] (dpm_resume_end) from [<c0158d2c>] (suspend_devices_and_enter+0x15c/0x5ac) +[<c0158d2c>] (suspend_devices_and_enter) from [<c01593bc>] (pm_suspend+0x240/0x2f4) +[<c01593bc>] (pm_suspend) from [<c0157b48>] (state_store+0x54/0x8c) +[<c0157b48>] (state_store) from [<c0276ecc>] (kernfs_fop_write+0x154/0x1c8) +[<c0276ecc>] (kernfs_fop_write) from [<c0209f74>] (__vfs_write+0x28/0xe0) +[<c0209f74>] (__vfs_write) from [<c020acc0>] (vfs_write+0x98/0xbc) +[<c020acc0>] (vfs_write) from [<c020ae48>] (ksys_write+0x68/0xb4) +[<c020ae48>] (ksys_write) from [<c0101000>] (ret_fast_syscall+0x0/0x28) +Exception stack(0xdd74dfa8 to 0xdd74dff0) +dfa0: 00000004 000e2408 00000001 000e2408 00000004 00000000 +dfc0: 00000004 000e2408 b6f36d60 00000004 000e2408 00000004 00000000 00000000 +dfe0: 00000000 be9fc74c b6e991bb b6ed5af6 +irq event stamp: 0 +hardirqs last enabled at (0): [<00000000>] 0x0 +hardirqs last disabled at (0): [<c011b73c>] copy_process+0x520/0x14b8 +softirqs last enabled at (0): [<c011b73c>] copy_process+0x520/0x14b8 +softirqs last disabled at (0): [<00000000>] 0x0 +---[ end trace 22461a068edbf2c1 ]--- +------------[ cut here ]------------ +WARNING: CPU: 0 PID: 556 at kernel/dma/remap.c:93 remap_allocator_free+0x20/0x30 +trying to free invalid coherent area: f39c52ba[...] +---[ end trace 22461a068edbf2c2 ]--- SMSC LAN8710/LAN8720 e9a00000.ethernet-ffffffff:00: attached PHYdriver [SMSC LAN8710/LAN8720] (mii_bus:phy_addr=e9a00000.ethernet-ffffffff:00, irq=POLL)
(the dirty is due to the need for "ARM: fix
__get_user_check()in case
uaccess_* calls are not inlined").BTW, I cannot trigger the issue on r8a7791/koelsch, which also uses sh-eth, not even when disabling
CONFIG_IOMMU_SUPPORTandCONFIG_ARM_LPAE(both are not set for the affected platforms).Quoted 6 lines
drivers/iommu/dma-iommu.c +0
-0@@ -541,15 +541,6 @@ static struct page **__iommu_dma_alloc_pages(struct device *dev,return pages; }
Quoted 7 lines
drivers/iommu/dma-iommu.c static struct page **__iommu_dma_alloc_pages(struct device *dev, · line 548
Quoted 5 lines
drivers/iommu/dma-iommu.c +0
-5-static struct page **__iommu_dma_get_pages (void *cpu_addr) -{ -struct vm_struct *area =find_vm_area (cpu_addr); - -if (!area || !area->pages)This checks
area->pages...
32-bit Arm doesn't use iommu-dma, so I don't see how this could be relevant to the reported problem, but either way this "check" of area->pages...
Quoted 41 lines
Quoted 41 lines
drivers/iommu/dma-iommu.c +3
-7-return NULL ; -return area->pages; -} -/** * iommu_dma_alloc_remap - Allocate and map a buffer contiguous in IOVA space * @dev: Device to allocate memory for. Must be a real device @@ -938,7 +929,7 @@ static void __iommu_dma_free(struct device *dev, size_t size, void *cpu_addr)* If it the address is remapped, then it's either non-coherent * or highmem CMA, or an iommu_dma_alloc_remap() construction. */ - pages =__iommu_dma_get_pages (cpu_addr); + pages =dma_common_find_pages (cpu_addr);if (!pages) page =vmalloc_to_page (cpu_addr);dma_common_free_remap (cpu_addr, alloc_size);@@ -1045,7 +1036,7 @@ static int iommu_dma_mmap(struct device *dev, struct vm_area_struct *vma,return -ENXIO ;if (IS_ENABLED (CONFIG_DMA_REMAP ) &&is_vmalloc_addr (cpu_addr)) { -struct page **pages =__iommu_dma_get_pages (cpu_addr); +struct page **pages =dma_common_find_pages (cpu_addr);if (pages)return __iommu_dma_mmap (pages, size, vma);@@ -1067,7 +1058,7 @@ static int iommu_dma_get_sgtable(struct device *dev, struct sg_table *sgt,int ret;if (IS_ENABLED (CONFIG_DMA_REMAP ) &&is_vmalloc_addr (cpu_addr)) { -struct page **pages =__iommu_dma_get_pages (cpu_addr); +struct page **pages =dma_common_find_pages (cpu_addr);if (pages) { return sg_alloc_table_from_pages(sgt, pages,kernel/dma/remap.c +0
-0@@ -11,6 +11,15 @@#include <linux/slab.h> #include <linux/vmalloc.h>
Quoted 10 lines
kernel/dma/remap.c line 18
Quoted 5 lines
kernel/dma/remap.c +5
-0+struct page **dma_common_find_pages (void *cpu_addr) +{ +struct vm_struct *area =find_vm_area (cpu_addr); + +if (!area || area->flags !=VM_DMA_COHERENT )... while this one checks
area->flags?Quoted 2 lines
kernel/dma/remap.c +2
-0+return NULL ; +return area->pages;
...remains inherent in this statement ;)
Introducing the additional area->flags check is rather the point of the patch. As to how it could affect the behaviour of remap_allocator_{alloc,free}(), that probably warrants some more digging in the arch/arm code. Having looked briefly to see if anything jumped out, I do notice that for the dma_common_contiguous_remap() case we're now relying on a dangling pointer to a long-gone kernel stack in area->pages to pass the check in dma_common_free_remap() - I can't see that that has any bearing on the reported issue, but it is a bit icky.
Robin.
Quoted 10 lines
kernel/dma/remap.c line 25
Quoted 5 lines
kernel/dma/remap.c +2
-0+} +static struct vm_struct *__dma_common_pages_remap (struct page **pages,size_t size,pgprot_t prot,const void *caller) {Gr{oetje,eeting}s,
Geert
Re: [PATCH 3/4] dma-mapping: introduce a dma_common_find_pages helper
In reply to Geert UytterhoevenHi Geert,
please try Linus' current tree. It has a fix from Andrey Smirnov for what looks the same problem that you reported.
Re: [PATCH 3/4] dma-mapping: introduce a dma_common_find_pages helper
Hi Christoph,
Quoted 2 lines · Christoph Hellwig
please try Linus' current tree. It has a fix from Andrey Smirnov for what looks the same problem that you reported.
Thanks, confirmed fixed by commit 2cf2aa6a69db0b17 ("dma-mapping: fix false positivse warnings in dma_common_free_remap()").
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] 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
PATCH 4/4 arm: remove wrappers for the generic dma remap helpers
In reply to Christoph HellwigRemove a few tiny wrappers around the generic dma remap code.
--- arch/arm/mm/dma-mapping.c | 32 +++++--------------------------- 1 file changed, 5 insertions(+), 27 deletions(-)
arch/arm/mm/dma-mapping.c +5 -27
@@ -336,18 +336,6 @@ static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp, pgprot_t prot, struct page **ret_page, const void *caller, bool want_vaddr); -static void * -__dma_alloc_remap (struct page *page,size_t size,gfp_t gfp,pgprot_t prot, -const void *caller) -{ -return dma_common_contiguous_remap (page, size, prot, caller); -} - -static void __dma_free_remap (void *cpu_addr,size_t size) -{ -dma_common_free_remap (cpu_addr, size); -} -#define DEFAULT_DMA_COHERENT_POOL_SIZE SZ_256Kstatic struct gen_pool *atomic_pool__ro_after_init ;
@@ -503,7 +491,7 @@ static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,if (!want_vaddr) goto out; - ptr =__dma_alloc_remap (page, size, gfp, prot, caller); + ptr =dma_common_contiguous_remap (page, size, prot, caller);if (!ptr) {__dma_free_buffer (page, size);return NULL ;
@@ -570,7 +558,7 @@ static void *__alloc_from_contiguous(struct device *dev, size_t size, goto out;if (PageHighMem (page)) { - ptr =__dma_alloc_remap (page, size,GFP_KERNEL , prot, caller); + ptr =dma_common_contiguous_remap (page, size, prot, caller);if (!ptr) {dma_release_from_contiguous (dev, page, count);return NULL ;
@@ -590,7 +578,7 @@ static void __free_from_contiguous(struct device *dev, struct page *page, {if (want_vaddr) {if (PageHighMem (page)) -__dma_free_remap (cpu_addr, size); +dma_common_free_remap (cpu_addr, size);else __dma_remap (page, size,PAGE_KERNEL ); }
@@ -682,7 +670,7 @@ static void *remap_allocator_alloc(struct arm_dma_alloc_args *args,static void remap_allocator_free (struct arm_dma_free_args *args) {if (args->want_vaddr) -__dma_free_remap (args->cpu_addr, args->size); +dma_common_free_remap (args->cpu_addr, args->size);__dma_free_buffer (args->page, args->size); }
@@ -1365,16 +1353,6 @@ static int __iommu_free_buffer(struct device *dev, struct page **pages,return 0 ; } -/* -* Create a CPU mapping for a specified pages -*/ -static void * -__iommu_alloc_remap (struct page **pages,size_t size,gfp_t gfp,pgprot_t prot, -const void *caller) -{ -return dma_common_pages_remap (pages, size, prot, caller); -} -/* * Create a mapping in device IO address space for specified pages */
@@ -1526,7 +1504,7 @@ static void *__arm_iommu_alloc_attrs(struct device *dev, size_t size,if (attrs &DMA_ATTR_NO_KERNEL_MAPPING )return pages; - addr =__iommu_alloc_remap (pages, size, gfp, prot, + addr =dma_common_pages_remap (pages, size, prot,__builtin_return_address (0 ));if (!addr) goto err_mapping;
Signature · 1 line
2.20.1