mm, dma, arm64: Reduce ARCH_KMALLOC_MINALIGN to 8
Subsystems (14)
Catalin Marinas
maintainerv6 cover mm, dma, arm64: Reduce ARCH_KMALLOC_MINALIGN to 8
Hi,
Here's version 6 of the series reducing the kmalloc() minimum alignment on arm64 to 8 (from 128). There are patches already to do the same for riscv (pretty straight-forward after this series).
The first 11 patches decouple ARCH_KMALLOC_MINALIGN from ARCH_DMA_MINALIGN and, for arm64, limit the kmalloc() caches to those aligned to the run-time probed cache_line_size(). On arm64 we gain the kmalloc-{64,192} caches.
The subsequent patches (11 to 17) further reduce the kmalloc() caches to kmalloc-{8,16,32,96} if the default swiotlb is present by bouncing small buffers in the DMA API.
Changes since v5:
- Renaming of the sg_* accessors for consistency.
- IIO_DMA_MINALIGN defined to ARCH_DMA_MINALIGN (missed it in previous versions).
- Modified Robin's patch 11 to use #ifdef CONFIG_NEED_SG_DMA_FLAGS instead of CONFIG_PCI_P2PDMA in scatterlist.h.
- Added the new sg_dma_*_swiotlb() under the same #ifdef as above.
The updated patches are also available on this branch:
git://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux devel/kmalloc-minalign
Thanks.
Catalin Marinas (15):
mm/slab: Decouple ARCH_KMALLOC_MINALIGN from ARCH_DMA_MINALIGN
dma: Allow dma_get_cache_alignment() to be overridden by the arch code mm/slab: Simplify create_kmalloc_cache() args and make it static mm/slab: Limit kmalloc() minimum alignment to
dma_get_cache_alignment()
drivers/base: Use ARCH_DMA_MINALIGN instead of ARCH_KMALLOC_MINALIGN drivers/gpu: Use ARCH_DMA_MINALIGN instead of ARCH_KMALLOC_MINALIGN drivers/usb: Use ARCH_DMA_MINALIGN instead of ARCH_KMALLOC_MINALIGN drivers/spi: Use ARCH_DMA_MINALIGN instead of ARCH_KMALLOC_MINALIGN
dm-crypt: Use ARCH_DMA_MINALIGN instead of ARCH_KMALLOC_MINALIGN
iio: core: Use ARCH_DMA_MINALIGN instead of ARCH_KMALLOC_MINALIGN
arm64: Allow kmalloc() caches aligned to the smaller cache_line_size()
dma-mapping: Force bouncing if the kmalloc() size is not
cache-line-aligned
iommu/dma: Force bouncing if the size is not cacheline-aligned
mm: slab: Reduce the kmalloc() minimum alignment if DMA bouncing
possible
arm64: Enable ARCH_WANT_KMALLOC_DMA_BOUNCE for arm64
Robin Murphy (2):
scatterlist: Add dedicated config for DMA flags
dma-mapping: Name SG DMA flag helpers consistently
arch/arm64/Kconfig | 1 + arch/arm64/include/asm/cache.h | 3 ++ arch/arm64/mm/init.c | 7 +++- drivers/base/devres.c | 6 ++-- drivers/gpu/drm/drm_managed.c | 6 ++-- drivers/iommu/Kconfig | 1 + drivers/iommu/dma-iommu.c | 58 ++++++++++++++++++++++++-------- drivers/iommu/iommu.c | 2 +- drivers/md/dm-crypt.c | 2 +- drivers/pci/Kconfig | 1 + drivers/spi/spidev.c | 2 +- drivers/usb/core/buffer.c | 8 ++--- include/linux/dma-map-ops.h | 61 ++++++++++++++++++++++++++++++++++ include/linux/dma-mapping.h | 4 ++- include/linux/iio/iio.h | 2 +- include/linux/scatterlist.h | 60 ++++++++++++++++++++++++++------- include/linux/slab.h | 14 ++++++-- kernel/dma/Kconfig | 7 ++++ kernel/dma/direct.c | 2 +- kernel/dma/direct.h | 3 +- mm/slab.c | 6 +--- mm/slab.h | 5 ++- mm/slab_common.c | 46 +++++++++++++++++++------ 23 files changed, 243 insertions(+), 64 deletions(-)
Catalin Marinas
maintainerPATCH v6 1/17 mm/slab: Decouple ARCH_KMALLOC_MINALIGN from ARCH_DMA_MINALIGN
In preparation for supporting a kmalloc() minimum alignment smaller than the arch DMA alignment, decouple the two definitions. This requires that either the kmalloc() caches are aligned to a (run-time) cache-line size or the DMA API bounces unaligned kmalloc() allocations. Subsequent patches will implement both options.
After this patch, ARCH_DMA_MINALIGN is expected to be used in static alignment annotations and defined by an architecture to be the maximum alignment for all supported configurations/SoCs in a single Image. Architectures opting in to a smaller ARCH_KMALLOC_MINALIGN will need to define its value in the arch headers.
Since ARCH_DMA_MINALIGN is now always defined, adjust the #ifdef in dma_get_cache_alignment() so that there is no change for architectures not requiring a minimum DMA alignment.
--- include/linux/dma-mapping.h | 2 +- include/linux/slab.h | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-)
include/linux/dma-mapping.h +1 -1
@@ -545,7 +545,7 @@ static inline int dma_set_min_align_mask(struct device *dev,static inline int dma_get_cache_alignment (void ) { -#ifdef ARCH_DMA_MINALIGN +#ifdef ARCH_HAS_DMA_MINALIGN return ARCH_DMA_MINALIGN ;#endif return 1 ;
include/linux/slab.h +11 -3
@@ -235,12 +235,20 @@ void kmem_dump_obj(void *object);* alignment larger than the alignment of a 64-bit integer. * Setting ARCH_DMA_MINALIGN in arch headers allows that. */ -#if defined(ARCH_DMA_MINALIGN ) &&ARCH_DMA_MINALIGN >8 +#ifdef ARCH_DMA_MINALIGN +#define ARCH_HAS_DMA_MINALIGN +#if ARCH_DMA_MINALIGN >8 && !defined(ARCH_KMALLOC_MINALIGN )#define ARCH_KMALLOC_MINALIGN ARCH_DMA_MINALIGN -#define KMALLOC_MIN_SIZE ARCH_DMA_MINALIGN -#define KMALLOC_SHIFT_LOW ilog2(ARCH_DMA_MINALIGN) +#endif #else +#define ARCH_DMA_MINALIGN __alignof__(unsigned long long) +#endif + +#ifndef ARCH_KMALLOC_MINALIGN #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long) +#elif ARCH_KMALLOC_MINALIGN >8 +#define KMALLOC_MIN_SIZE ARCH_KMALLOC_MINALIGN +#define KMALLOC_SHIFT_LOW ilog2(KMALLOC_MIN_SIZE)#endif /*
Re: [PATCH v6 01/17] mm/slab: Decouple ARCH_KMALLOC_MINALIGN from ARCH_DMA_MINALIGN
Quoted 47 lines · Catalin Marinas
In preparation for supporting a
kmalloc()minimum alignment smaller than the arch DMA alignment, decouple the two definitions. This requires that either thekmalloc()caches are aligned to a (run-time) cache-line size or the DMA API bounces unalignedkmalloc()allocations. Subsequent patches will implement both options.After this patch,
ARCH_DMA_MINALIGNis expected to be used in static alignment annotations and defined by an architecture to be the maximum alignment for all supported configurations/SoCs in a single Image. Architectures opting in to a smallerARCH_KMALLOC_MINALIGNwill need to define its value in the arch headers.Since
ARCH_DMA_MINALIGNis now always defined, adjust the #ifdef indma_get_cache_alignment()so that there is no change for architectures not requiring a minimum DMA alignment.Signed-off-by Catalin Marinas <[email protected]>Cc Andrew Morton <[email protected]>Cc Christoph Hellwig <[email protected]>Cc Robin Murphy <[email protected]>--- include/linux/dma-mapping.h | 2 +- include/linux/slab.h | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-)include/linux/dma-mapping.h +1
-1@@ -545,7 +545,7 @@ static inline int dma_set_min_align_mask(struct device *dev,static inline int dma_get_cache_alignment (void ) { -#ifdef ARCH_DMA_MINALIGN +#ifdef ARCH_HAS_DMA_MINALIGN return ARCH_DMA_MINALIGN ;#endif return 1 ;include/linux/slab.h +0
-1@@ -235,12 +235,20 @@ void kmem_dump_obj(void *object);* alignment larger than the alignment of a 64-bit integer. * Setting ARCH_DMA_MINALIGN in arch headers allows that. */ -#if defined(ARCH_DMA_MINALIGN ) &&ARCH_DMA_MINALIGN >8
include/linux/slab.h void kmem_dump_obj(void *object); · line 245
Quoted 10 lines
include/linux/slab.h +6
-2+#ifdef ARCH_DMA_MINALIGN +#define ARCH_HAS_DMA_MINALIGN +#if ARCH_DMA_MINALIGN >8 && !defined(ARCH_KMALLOC_MINALIGN )#define ARCH_KMALLOC_MINALIGN ARCH_DMA_MINALIGN -#define KMALLOC_MIN_SIZE ARCH_DMA_MINALIGN -#define KMALLOC_SHIFT_LOW ilog2(ARCH_DMA_MINALIGN) +#endif #else +#define ARCH_DMA_MINALIGN __alignof__(unsigned long long) +#endif
It seems weird to make slab.h responsible for this part, especially for #define ARCH_HAS_DMA_MINALIGN, which dma-mapping.h consumes. Maybe it would be difficult to do differently due to some dependency hell, but minimally I don't see dma-mapping.h including slab.h so the result is include-order-dependent? Maybe it's included transitively, but then it's fragile and would be better to do explicitly?
Quoted 10 lines
include/linux/slab.h +5
-0+ +#ifndef ARCH_KMALLOC_MINALIGN #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long) +#elif ARCH_KMALLOC_MINALIGN >8 +#define KMALLOC_MIN_SIZE ARCH_KMALLOC_MINALIGN +#define KMALLOC_SHIFT_LOW ilog2(KMALLOC_MIN_SIZE)#endif /*
Catalin Marinas
maintainerRe: [PATCH v6 01/17] mm/slab: Decouple ARCH_KMALLOC_MINALIGN from ARCH_DMA_MINALIGN
Quoted 32 lines · Vlastimil Babka
Quoted 31 lines · Catalin Marinas
include/linux/dma-mapping.h +1
-1@@ -545,7 +545,7 @@ static inline int dma_set_min_align_mask(struct device *dev,static inline int dma_get_cache_alignment (void ) { -#ifdef ARCH_DMA_MINALIGN +#ifdef ARCH_HAS_DMA_MINALIGN return ARCH_DMA_MINALIGN ;#endif return 1 ;include/linux/slab.h +5
-3@@ -235,12 +235,20 @@ void kmem_dump_obj(void *object);* alignment larger than the alignment of a 64-bit integer. * Setting ARCH_DMA_MINALIGN in arch headers allows that. */ -#if defined(ARCH_DMA_MINALIGN ) &&ARCH_DMA_MINALIGN >8 +#ifdef ARCH_DMA_MINALIGN +#define ARCH_HAS_DMA_MINALIGN +#if ARCH_DMA_MINALIGN >8 && !defined(ARCH_KMALLOC_MINALIGN )#define ARCH_KMALLOC_MINALIGN ARCH_DMA_MINALIGN -#define KMALLOC_MIN_SIZE ARCH_DMA_MINALIGN -#define KMALLOC_SHIFT_LOW ilog2(ARCH_DMA_MINALIGN) +#endif #else +#define ARCH_DMA_MINALIGN __alignof__(unsigned long long)
Quoted 8 lines
include/linux/slab.h void kmem_dump_obj(void *object); · line 245
Quoted 1 line
include/linux/slab.h +1
-0+#endif It seems weird to make
slab.hresponsible for this part, especially for #defineARCH_HAS_DMA_MINALIGN, whichdma-mapping.hconsumes. Maybe it would be difficult to do differently due to some dependency hell, but minimally I don't seedma-mapping.hincludingslab.hso the result is include-order-dependent? Maybe it's included transitively, but then it's fragile and would be better to do explicitly?
True, there's a risk that it doesn't get included with some future header refactoring.
What about moving ARCH_DMA_MINALIGN to linux/cache.h? Alternatively, I could create a new linux/dma-minalign.h file but I feel since this is about caches, having it in cache.h makes more sense. asm/cache.h is also where most archs define the constant (apart from mips, sh, microblaze).
Signature · 1 line
Catalin
Catalin Marinas
maintainerRe: [PATCH v6 01/17] mm/slab: Decouple ARCH_KMALLOC_MINALIGN from ARCH_DMA_MINALIGN
Quoted 42 lines · Catalin Marinas
Quoted 32 lines · Vlastimil Babka
Quoted 31 lines · Catalin Marinas
include/linux/dma-mapping.h +1
-1@@ -545,7 +545,7 @@ static inline int dma_set_min_align_mask(struct device *dev,static inline int dma_get_cache_alignment (void ) { -#ifdef ARCH_DMA_MINALIGN +#ifdef ARCH_HAS_DMA_MINALIGN return ARCH_DMA_MINALIGN ;#endif return 1 ;include/linux/slab.h +5
-3@@ -235,12 +235,20 @@ void kmem_dump_obj(void *object);* alignment larger than the alignment of a 64-bit integer. * Setting ARCH_DMA_MINALIGN in arch headers allows that. */ -#if defined(ARCH_DMA_MINALIGN ) &&ARCH_DMA_MINALIGN >8 +#ifdef ARCH_DMA_MINALIGN +#define ARCH_HAS_DMA_MINALIGN +#if ARCH_DMA_MINALIGN >8 && !defined(ARCH_KMALLOC_MINALIGN )#define ARCH_KMALLOC_MINALIGN ARCH_DMA_MINALIGN -#define KMALLOC_MIN_SIZE ARCH_DMA_MINALIGN -#define KMALLOC_SHIFT_LOW ilog2(ARCH_DMA_MINALIGN) +#endif #else +#define ARCH_DMA_MINALIGN __alignof__(unsigned long long)Quoted 8 lines
include/linux/slab.h void kmem_dump_obj(void *object); · line 245
Quoted 1 line
include/linux/slab.h +1
-0+#endif It seems weird to make
slab.hresponsible for this part, especially for #defineARCH_HAS_DMA_MINALIGN, whichdma-mapping.hconsumes. Maybe it would be difficult to do differently due to some dependency hell, but minimally I don't seedma-mapping.hincludingslab.hso the result is include-order-dependent? Maybe it's included transitively, but then it's fragile and would be better to do explicitly?
Quoted 7 lines
True, there's a risk that it doesn't get included with some future header refactoring.
What about moving
ARCH_DMA_MINALIGNtolinux/cache.h? Alternatively, I could create a newlinux/dma-minalign.hfile but I feel since this is about caches, having it incache.hmakes more sense.asm/cache.his also where most archs define the constant (apart from mips, sh, microblaze).
Something like this (still compiling):
include/linux/cache.h +6 -0
@@ -98,4 +98,10 @@ struct cacheline_padding {#define CACHELINE_PADDING (name)#endif +#ifdef ARCH_DMA_MINALIGN +#define ARCH_HAS_DMA_MINALIGN +#else +#define ARCH_DMA_MINALIGN __alignof__(unsigned long long) +#endif +#endif /* __LINUX_CACHE_H */
include/linux/dma-mapping.h +1 -0
@@ -2,6 +2,7 @@#ifndef _LINUX_DMA_MAPPING_H#define _LINUX_DMA_MAPPING_H +#include <linux/cache.h> #include <linux/sizes.h> #include <linux/string.h> #include <linux/device.h>
include/linux/slab.h +3 -6
@@ -12,6 +12,7 @@#ifndef _LINUX_SLAB_H#define _LINUX_SLAB_H +#include <linux/cache.h> #include <linux/gfp.h> #include <linux/overflow.h> #include <linux/types.h>
@@ -235,14 +236,10 @@ void kmem_dump_obj(void *object);* alignment larger than the alignment of a 64-bit integer. * Setting ARCH_DMA_MINALIGN in arch headers allows that. */ -#ifdef ARCH_DMA_MINALIGN -#define ARCH_HAS_DMA_MINALIGN -#if ARCH_DMA_MINALIGN >8 && !defined(ARCH_KMALLOC_MINALIGN ) +#if defined(ARCH_HAS_DMA_MINALIGN ) &&ARCH_DMA_MINALIGN >8 && \ + !defined(ARCH_KMALLOC_MINALIGN )#define ARCH_KMALLOC_MINALIGN ARCH_DMA_MINALIGN#endif -#else -#define ARCH_DMA_MINALIGN __alignof__(unsigned long long) -#endif #ifndef ARCH_KMALLOC_MINALIGN #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
Signature · 1 line
Catalin
Re: [PATCH v6 01/17] mm/slab: Decouple ARCH_KMALLOC_MINALIGN from ARCH_DMA_MINALIGN
Quoted 46 lines · Catalin Marinas
Quoted 45 lines · Catalin Marinas
Quoted 32 lines · Vlastimil Babka
Quoted 31 lines · Catalin Marinas
include/linux/dma-mapping.h +1
-1@@ -545,7 +545,7 @@ static inline int dma_set_min_align_mask(struct device *dev,static inline int dma_get_cache_alignment (void ) { -#ifdef ARCH_DMA_MINALIGN +#ifdef ARCH_HAS_DMA_MINALIGN return ARCH_DMA_MINALIGN ;#endif return 1 ;include/linux/slab.h +5
-3@@ -235,12 +235,20 @@ void kmem_dump_obj(void *object);* alignment larger than the alignment of a 64-bit integer. * Setting ARCH_DMA_MINALIGN in arch headers allows that. */ -#if defined(ARCH_DMA_MINALIGN ) &&ARCH_DMA_MINALIGN >8 +#ifdef ARCH_DMA_MINALIGN +#define ARCH_HAS_DMA_MINALIGN +#if ARCH_DMA_MINALIGN >8 && !defined(ARCH_KMALLOC_MINALIGN )#define ARCH_KMALLOC_MINALIGN ARCH_DMA_MINALIGN -#define KMALLOC_MIN_SIZE ARCH_DMA_MINALIGN -#define KMALLOC_SHIFT_LOW ilog2(ARCH_DMA_MINALIGN) +#endif #else +#define ARCH_DMA_MINALIGN __alignof__(unsigned long long)Quoted 8 lines
include/linux/slab.h void kmem_dump_obj(void *object); · line 245
Quoted 1 line
include/linux/slab.h +1
-0+#endif It seems weird to make
slab.hresponsible for this part, especially for #defineARCH_HAS_DMA_MINALIGN, whichdma-mapping.hconsumes. Maybe it would be difficult to do differently due to some dependency hell, but minimally I don't seedma-mapping.hincludingslab.hso the result is include-order-dependent? Maybe it's included transitively, but then it's fragile and would be better to do explicitly?True, there's a risk that it doesn't get included with some future header refactoring.
Quoted 6 lines
Quoted 4 lines
What about moving
ARCH_DMA_MINALIGNtolinux/cache.h? Alternatively, I could create a newlinux/dma-minalign.hfile but I feel since this is about caches, having it incache.hmakes more sense.asm/cache.his also where most archs define the constant (apart from mips, sh, microblaze).Something like this (still compiling):
Yeah that would be great!
Catalin Marinas
maintainerPATCH v6 2/17 dma: Allow dma_get_cache_alignment() to be overridden by the arch code
In reply to Catalin MarinasOn arm64, ARCH_DMA_MINALIGN is larger than most cache line size configurations deployed. Allow an architecture to override dma_get_cache_alignment() in order to return a run-time probed value (e.g. cache_line_size()).
--- include/linux/dma-mapping.h | 2 ++ 1 file changed, 2 insertions(+)
include/linux/dma-mapping.h +2 -0
@@ -543,6 +543,7 @@ static inline int dma_set_min_align_mask(struct device *dev,return 0 ; } +#ifndef dma_get_cache_alignmentstatic inline int dma_get_cache_alignment (void ) {#ifdef ARCH_HAS_DMA_MINALIGN
@@ -550,6 +551,7 @@ static inline int dma_get_cache_alignment(void)#endif return 1; } +#endif static inline void *dmam_alloc_coherent (struct device *dev,size_t size,dma_addr_t *dma_handle,gfp_t gfp)
Catalin Marinas
maintainerPATCH v6 3/17 mm/slab: Simplify create_kmalloc_cache() args and make it static
In reply to Catalin MarinasIn the slab variant of kmem_cache_init(), call new_kmalloc_cache() instead of initialising the kmalloc_caches array directly. With this, create_kmalloc_cache() is now only called from new_kmalloc_cache() in the same file, so make it static. In addition, the useroffset argument is always 0 while usersize is the same as size. Remove them.
--- mm/slab.c | 6 +----- mm/slab.h | 5 ++--- mm/slab_common.c | 14 ++++++-------- 3 files changed, 9 insertions(+), 16 deletions(-)
mm/slab.c +1 -5
@@ -1240,11 +1240,7 @@ void __init kmem_cache_init(void)* Initialize the caches that provide memory for the kmem_cache_node * structures first. Without this, further allocations will bug. */ - kmalloc_caches[KMALLOC_NORMAL ][INDEX_NODE ] =create_kmalloc_cache ( - kmalloc_info[INDEX_NODE ].name[KMALLOC_NORMAL ], - kmalloc_info[INDEX_NODE ].size, -ARCH_KMALLOC_FLAGS ,0 , - kmalloc_info[INDEX_NODE ].size); +new_kmalloc_cache (INDEX_NODE ,KMALLOC_NORMAL ,ARCH_KMALLOC_FLAGS ); slab_state =PARTIAL_NODE ;setup_kmalloc_cache_index_table ();
mm/slab.h +2 -3
@@ -255,9 +255,8 @@ gfp_t kmalloc_fix_flags(gfp_t flags);/* Functions provided by the slab allocators */ int __kmem_cache_create (struct kmem_cache *,slab_flags_t flags); -struct kmem_cache *create_kmalloc_cache (const char *name,unsigned int size, -slab_flags_t flags,unsigned int useroffset, -unsigned int usersize); +void __init new_kmalloc_cache (int idx,enum kmalloc_cache_type type, +slab_flags_t flags);extern void create_boot_cache (struct kmem_cache *,const char *name,unsigned int size,slab_flags_t flags,unsigned int useroffset,unsigned int usersize);
mm/slab_common.c +6 -8
@@ -658,17 +658,16 @@ void __init create_boot_cache(struct kmem_cache *s, const char *name, s->refcount =-1 ;/* Exempt from merging for now */ } -struct kmem_cache *__init create_kmalloc_cache (const char *name, -unsigned int size,slab_flags_t flags, -unsigned int useroffset,unsigned int usersize) +static struct kmem_cache *__init create_kmalloc_cache (const char *name, +unsigned int size, +slab_flags_t flags) {struct kmem_cache *s =kmem_cache_zalloc (kmem_cache,GFP_NOWAIT );if (!s)panic ("Out of memory when creating slab %s\n" , name); -create_boot_cache (s, name, size, flags |SLAB_KMALLOC , useroffset, - usersize); +create_boot_cache (s, name, size, flags |SLAB_KMALLOC ,0 , size);list_add (&s->list, &slab_caches); s->refcount =1 ;return s;
@@ -863,7 +862,7 @@ void __init setup_kmalloc_cache_index_table(void) } } -static void __init +void __init new_kmalloc_cache (int idx,enum kmalloc_cache_type type,slab_flags_t flags) {if ((KMALLOC_RECLAIM !=KMALLOC_NORMAL ) && (type ==KMALLOC_RECLAIM )) {
@@ -880,8 +879,7 @@ new_kmalloc_cache(int idx, enum kmalloc_cache_type type, slab_flags_t flags) kmalloc_caches[type][idx] =create_kmalloc_cache ( kmalloc_info[idx].name[type], - kmalloc_info[idx].size, flags,0 , - kmalloc_info[idx].size); + kmalloc_info[idx].size, flags);/* * If CONFIG_MEMCG_KMEM is enabled, disable cache merging for
Re: [PATCH v6 03/17] mm/slab: Simplify create_kmalloc_cache() args and make it static
Quoted 8 lines · Catalin Marinas
In the slab variant of
kmem_cache_init(), callnew_kmalloc_cache()instead of initialising thekmalloc_cachesarray directly. With this,create_kmalloc_cache()is now only called fromnew_kmalloc_cache()in the same file, so make it static. In addition, the useroffset argument is always 0 while usersize is the same as size. Remove them.Signed-off-by Catalin Marinas <[email protected]>Cc Andrew Morton <[email protected]>
Nice cleanup, thanks!
Quoted 85 lines
--- mm/slab.c | 6 +----- mm/slab.h | 5 ++--- mm/slab_common.c | 14 ++++++-------- 3 files changed, 9 insertions(+), 16 deletions(-)mm/slab.c +1
-5@@ -1240,11 +1240,7 @@ void __init kmem_cache_init(void)* Initialize the caches that provide memory for the kmem_cache_node * structures first. Without this, further allocations will bug. */ - kmalloc_caches[KMALLOC_NORMAL ][INDEX_NODE ] =create_kmalloc_cache ( - kmalloc_info[INDEX_NODE ].name[KMALLOC_NORMAL ], - kmalloc_info[INDEX_NODE ].size, -ARCH_KMALLOC_FLAGS ,0 , - kmalloc_info[INDEX_NODE ].size); +new_kmalloc_cache (INDEX_NODE ,KMALLOC_NORMAL ,ARCH_KMALLOC_FLAGS ); slab_state =PARTIAL_NODE ;setup_kmalloc_cache_index_table ();mm/slab.h +2
-3@@ -255,9 +255,8 @@ gfp_t kmalloc_fix_flags(gfp_t flags);/* Functions provided by the slab allocators */ int __kmem_cache_create (struct kmem_cache *,slab_flags_t flags); -struct kmem_cache *create_kmalloc_cache (const char *name,unsigned int size, -slab_flags_t flags,unsigned int useroffset, -unsigned int usersize); +void __init new_kmalloc_cache (int idx,enum kmalloc_cache_type type, +slab_flags_t flags);extern void create_boot_cache (struct kmem_cache *,const char *name,unsigned int size,slab_flags_t flags,unsigned int useroffset,unsigned int usersize);mm/slab_common.c +6
-8@@ -658,17 +658,16 @@ void __init create_boot_cache(struct kmem_cache *s, const char *name, s->refcount =-1 ;/* Exempt from merging for now */ } -struct kmem_cache *__init create_kmalloc_cache (const char *name, -unsigned int size,slab_flags_t flags, -unsigned int useroffset,unsigned int usersize) +static struct kmem_cache *__init create_kmalloc_cache (const char *name, +unsigned int size, +slab_flags_t flags) {struct kmem_cache *s =kmem_cache_zalloc (kmem_cache,GFP_NOWAIT );if (!s)panic ("Out of memory when creating slab %s\n" , name); -create_boot_cache (s, name, size, flags |SLAB_KMALLOC , useroffset, - usersize); +create_boot_cache (s, name, size, flags |SLAB_KMALLOC ,0 , size);list_add (&s->list, &slab_caches); s->refcount =1 ;return s;@@ -863,7 +862,7 @@ void __init setup_kmalloc_cache_index_table(void) } } -static void __init +void __init new_kmalloc_cache (int idx,enum kmalloc_cache_type type,slab_flags_t flags) {if ((KMALLOC_RECLAIM !=KMALLOC_NORMAL ) && (type ==KMALLOC_RECLAIM )) {@@ -880,8 +879,7 @@ new_kmalloc_cache(int idx, enum kmalloc_cache_type type, slab_flags_t flags) kmalloc_caches[type][idx] =create_kmalloc_cache ( kmalloc_info[idx].name[type], - kmalloc_info[idx].size, flags,0 , - kmalloc_info[idx].size); + kmalloc_info[idx].size, flags);/* * If CONFIG_MEMCG_KMEM is enabled, disable cache merging for
Catalin Marinas
maintainerPATCH v6 4/17 mm/slab: Limit kmalloc() minimum alignment to dma_get_cache_alignment()
In reply to Catalin MarinasDo not create kmalloc() caches which are not aligned to dma_get_cache_alignment(). There is no functional change since for current architectures defining ARCH_DMA_MINALIGN, ARCH_KMALLOC_MINALIGN equals ARCH_DMA_MINALIGN (and dma_get_cache_alignment()). On architectures without a specific ARCH_DMA_MINALIGN, dma_get_cache_alignment() is 1, so no change to the kmalloc() caches.
If an architecture selects ARCH_HAS_DMA_CACHE_LINE_SIZE (introduced previously), the kmalloc() caches will be aligned to a cache line size.
--- mm/slab_common.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-)
mm/slab_common.c +21 -3
@@ -17,6 +17,7 @@#include <linux/cpu.h> #include <linux/uaccess.h> #include <linux/seq_file.h> +#include <linux/dma-mapping.h> #include <linux/proc_fs.h> #include <linux/debugfs.h> #include <linux/kasan.h>
@@ -862,9 +863,18 @@ void __init setup_kmalloc_cache_index_table(void) } } +static unsigned int __kmalloc_minalign (void ) +{ +return dma_get_cache_alignment (); +} +void __init new_kmalloc_cache (int idx,enum kmalloc_cache_type type,slab_flags_t flags) { +unsigned int minalign =__kmalloc_minalign (); +unsigned int aligned_size = kmalloc_info[idx].size; +int aligned_idx = idx; +if ((KMALLOC_RECLAIM !=KMALLOC_NORMAL ) && (type ==KMALLOC_RECLAIM )) { flags |=SLAB_RECLAIM_ACCOUNT ; }else if (IS_ENABLED (CONFIG_MEMCG_KMEM ) && (type ==KMALLOC_CGROUP )) {
@@ -877,9 +887,17 @@ new_kmalloc_cache(int idx, enum kmalloc_cache_type type, slab_flags_t flags) flags |=SLAB_CACHE_DMA ; } - kmalloc_caches[type][idx] =create_kmalloc_cache ( - kmalloc_info[idx].name[type], - kmalloc_info[idx].size, flags); +if (minalign >ARCH_KMALLOC_MINALIGN ) { + aligned_size =ALIGN (aligned_size, minalign); + aligned_idx =__kmalloc_index (aligned_size, false); + } + +if (!kmalloc_caches[type][aligned_idx]) + kmalloc_caches[type][aligned_idx] =create_kmalloc_cache ( + kmalloc_info[aligned_idx].name[type], + aligned_size, flags); +if (idx != aligned_idx) + kmalloc_caches[type][idx] = kmalloc_caches[type][aligned_idx];/* * If CONFIG_MEMCG_KMEM is enabled, disable cache merging for
Re: [PATCH v6 04/17] mm/slab: Limit kmalloc() minimum alignment to dma_get_cache_alignment()
Quoted 9 lines · Catalin Marinas
Do not create
kmalloc()caches which are not aligned todma_get_cache_alignment(). There is no functional change since for current architectures definingARCH_DMA_MINALIGN,ARCH_KMALLOC_MINALIGNequalsARCH_DMA_MINALIGN(anddma_get_cache_alignment()). On architectures without a specificARCH_DMA_MINALIGN,dma_get_cache_alignment()is 1, so no change to thekmalloc()caches.If an architecture selects
ARCH_HAS_DMA_CACHE_LINE_SIZE(introduced previously), thekmalloc()caches will be aligned to a cache line size.
It this part leftover from a previous version?
Quoted 4 lines
Signed-off-by Catalin Marinas <[email protected]>Cc Andrew Morton <[email protected]>Cc Christoph Hellwig <[email protected]>Cc Robin Murphy <[email protected]>
Quoted 57 lines
--- mm/slab_common.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-)mm/slab_common.c +21
-3@@ -17,6 +17,7 @@#include <linux/cpu.h> #include <linux/uaccess.h> #include <linux/seq_file.h> +#include <linux/dma-mapping.h> #include <linux/proc_fs.h> #include <linux/debugfs.h> #include <linux/kasan.h> @@ -862,9 +863,18 @@ void __init setup_kmalloc_cache_index_table(void) } } +static unsigned int __kmalloc_minalign (void ) +{ +return dma_get_cache_alignment (); +} +void __init new_kmalloc_cache (int idx,enum kmalloc_cache_type type,slab_flags_t flags) { +unsigned int minalign =__kmalloc_minalign (); +unsigned int aligned_size = kmalloc_info[idx].size; +int aligned_idx = idx; +if ((KMALLOC_RECLAIM !=KMALLOC_NORMAL ) && (type ==KMALLOC_RECLAIM )) { flags |=SLAB_RECLAIM_ACCOUNT ; }else if (IS_ENABLED (CONFIG_MEMCG_KMEM ) && (type ==KMALLOC_CGROUP )) {@@ -877,9 +887,17 @@ new_kmalloc_cache(int idx, enum kmalloc_cache_type type, slab_flags_t flags) flags |=SLAB_CACHE_DMA ; } - kmalloc_caches[type][idx] =create_kmalloc_cache ( - kmalloc_info[idx].name[type], - kmalloc_info[idx].size, flags); +if (minalign >ARCH_KMALLOC_MINALIGN ) { + aligned_size =ALIGN (aligned_size, minalign); + aligned_idx =__kmalloc_index (aligned_size, false); + } + +if (!kmalloc_caches[type][aligned_idx]) + kmalloc_caches[type][aligned_idx] =create_kmalloc_cache ( + kmalloc_info[aligned_idx].name[type], + aligned_size, flags); +if (idx != aligned_idx) + kmalloc_caches[type][idx] = kmalloc_caches[type][aligned_idx];/* * If CONFIG_MEMCG_KMEM is enabled, disable cache merging for
Catalin Marinas
maintainerPATCH v6 5/17 drivers/base: Use ARCH_DMA_MINALIGN instead of ARCH_KMALLOC_MINALIGN
In reply to Catalin MarinasARCH_DMA_MINALIGN represents the minimum (static) alignment for safe DMA operations while ARCH_KMALLOC_MINALIGN is the minimum kmalloc() objects alignment.
--- drivers/base/devres.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/base/devres.c +3 -3
@@ -29,10 +29,10 @@ struct devres {* Some archs want to perform DMA into kmalloc caches * and need a guaranteed alignment larger than * the alignment of a 64-bit integer. -* Thus we use ARCH_KMALLOC_MINALIGN here and get exactly the same -* buffer alignment as if it was allocated by plain kmalloc(). +* Thus we use ARCH_DMA_MINALIGN for data[] which will force the same +* alignment for struct devres when allocated by kmalloc(). */ -u8 __aligned(ARCH_KMALLOC_MINALIGN) data[]; +u8 __aligned(ARCH_DMA_MINALIGN) data[]; };struct devres_group {
Catalin Marinas
maintainerPATCH v6 6/17 drivers/gpu: Use ARCH_DMA_MINALIGN instead of ARCH_KMALLOC_MINALIGN
In reply to Catalin MarinasARCH_DMA_MINALIGN represents the minimum (static) alignment for safe DMA operations while ARCH_KMALLOC_MINALIGN is the minimum kmalloc() objects alignment.
--- drivers/gpu/drm/drm_managed.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_managed.c +3 -3
@@ -49,10 +49,10 @@ struct drmres {* Some archs want to perform DMA into kmalloc caches * and need a guaranteed alignment larger than * the alignment of a 64-bit integer. -* Thus we use ARCH_KMALLOC_MINALIGN here and get exactly the same -* buffer alignment as if it was allocated by plain kmalloc(). +* Thus we use ARCH_DMA_MINALIGN for data[] which will force the same +* alignment for struct drmres when allocated by kmalloc(). */ -u8 __aligned(ARCH_KMALLOC_MINALIGN) data[]; +u8 __aligned(ARCH_DMA_MINALIGN) data[]; };static void free_dr (struct drmres *dr)
Catalin Marinas
maintainerPATCH v6 7/17 drivers/usb: Use ARCH_DMA_MINALIGN instead of ARCH_KMALLOC_MINALIGN
In reply to Catalin MarinasARCH_DMA_MINALIGN represents the minimum (static) alignment for safe DMA operations while ARCH_KMALLOC_MINALIGN is the minimum kmalloc() objects alignment.
--- drivers/usb/core/buffer.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/usb/core/buffer.c +4 -4
@@ -34,13 +34,13 @@ void __init usb_init_pool_max(void) {/* * The pool_max values must never be smaller than -* ARCH_KMALLOC_MINALIGN. +* ARCH_DMA_MINALIGN. */ -if (ARCH_KMALLOC_MINALIGN <=32 ) +if (ARCH_DMA_MINALIGN <=32 ) ;/* Original value is okay */ -else if (ARCH_KMALLOC_MINALIGN <=64 ) +else if (ARCH_DMA_MINALIGN <=64 ) pool_max[0 ] =64 ; -else if (ARCH_KMALLOC_MINALIGN <=128 ) +else if (ARCH_DMA_MINALIGN <=128 ) pool_max[0 ] =0 ;/* Don't use this pool */ else BUILD_BUG ();/* We don't allow this */
Catalin Marinas
maintainerPATCH v6 8/17 drivers/spi: Use ARCH_DMA_MINALIGN instead of ARCH_KMALLOC_MINALIGN
In reply to Catalin MarinasARCH_DMA_MINALIGN represents the minimum (static) alignment for safe DMA operations while ARCH_KMALLOC_MINALIGN is the minimum kmalloc() objects alignment.
--- drivers/spi/spidev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/spi/spidev.c +1 -1
@@ -237,7 +237,7 @@ static int spidev_message(struct spidev_data *spidev,/* Ensure that also following allocations from rx_buf/tx_buf will meet * DMA alignment requirements. */ -unsigned int len_aligned =ALIGN (u_tmp->len,ARCH_KMALLOC_MINALIGN ); +unsigned int len_aligned =ALIGN (u_tmp->len,ARCH_DMA_MINALIGN ); k_tmp->len = u_tmp->len;
Catalin Marinas
maintainerPATCH v6 9/17 dm-crypt: Use ARCH_DMA_MINALIGN instead of ARCH_KMALLOC_MINALIGN
In reply to Catalin MarinasARCH_DMA_MINALIGN represents the minimum (static) alignment for safe DMA operations while ARCH_KMALLOC_MINALIGN is the minimum kmalloc() objects alignment.
--- drivers/md/dm-crypt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/dm-crypt.c +1 -1
@@ -3256,7 +3256,7 @@ static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv) cc->per_bio_data_size = ti->per_io_data_size =ALIGN (sizeof (struct dm_crypt_io ) + cc->dmreq_start + additional_req_size, -ARCH_KMALLOC_MINALIGN ); +ARCH_DMA_MINALIGN ); ret =mempool_init (&cc->page_pool,BIO_MAX_VECS , crypt_page_alloc, crypt_page_free, cc);if (ret) {
Catalin Marinas
maintainerPATCH v6 10/17 iio: core: Use ARCH_DMA_MINALIGN instead of ARCH_KMALLOC_MINALIGN
In reply to Catalin MarinasARCH_DMA_MINALIGN represents the minimum (static) alignment for safe DMA operations while ARCH_KMALLOC_MINALIGN is the minimum kmalloc() objects alignment.
--- include/linux/iio/iio.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/iio/iio.h +1 -1
@@ -722,7 +722,7 @@ static inline void *iio_device_get_drvdata(const struct iio_dev *indio_dev)* must not share cachelines with the rest of the structure, thus making * them safe for use with non-coherent DMA. */ -#define IIO_DMA_MINALIGN ARCH_KMALLOC_MINALIGN +#define IIO_DMA_MINALIGN ARCH_DMA_MINALIGNstruct iio_dev *iio_device_alloc (struct device *parent,int sizeof_priv);/* The information at the returned address is guaranteed to be cacheline aligned */
Re: [PATCH v6 10/17] iio: core: Use ARCH_DMA_MINALIGN instead of ARCH_KMALLOC_MINALIGN
Quoted 7 lines · On Wed, 31 May 2023 16:48:29 +0100 Catalin Marinas <[email protected]>…
ARCH_DMA_MINALIGNrepresents the minimum (static) alignment for safe DMA operations whileARCH_KMALLOC_MINALIGNis the minimumkmalloc()objects alignment.Signed-off-by Catalin Marinas <[email protected]>Cc Jonathan Cameron <[email protected]>Cc Lars-Peter Clausen <[email protected]>
Thanks.
Jonathan
Quoted 19 lines
--- include/linux/iio/iio.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/iio/iio.h +1
-1@@ -722,7 +722,7 @@ static inline void *iio_device_get_drvdata(const struct iio_dev *indio_dev)* must not share cachelines with the rest of the structure, thus making * them safe for use with non-coherent DMA. */ -#define IIO_DMA_MINALIGN ARCH_KMALLOC_MINALIGN +#define IIO_DMA_MINALIGN ARCH_DMA_MINALIGNstruct iio_dev *iio_device_alloc (struct device *parent,int sizeof_priv);/* The information at the returned address is guaranteed to be cacheline aligned */
Catalin Marinas
maintainerPATCH v6 11/17 arm64: Allow kmalloc() caches aligned to the smaller cache_line_size()
In reply to Catalin MarinasOn arm64, ARCH_DMA_MINALIGN is 128, larger than the cache line size on most of the current platforms (typically 64). Define ARCH_KMALLOC_MINALIGN to 8 (the default for architectures without their own ARCH_DMA_MINALIGN) and override dma_get_cache_alignment() to return cache_line_size(), probed at run-time. The kmalloc() caches will be limited to the cache line size. This will allow the additional kmalloc-{64,192} caches on most arm64 platforms.
--- arch/arm64/include/asm/cache.h | 3 +++ 1 file changed, 3 insertions(+)
arch/arm64/include/asm/cache.h +3 -0
@@ -33,6 +33,7 @@* the CPU. */ #define ARCH_DMA_MINALIGN (128) +#define ARCH_KMALLOC_MINALIGN (8)#ifndef __ASSEMBLY__
@@ -90,6 +91,8 @@ static inline int cache_line_size_of_cpu(void)int cache_line_size (void ); +#define dma_get_cache_alignment cache_line_size +/* * Read the effective value of CTR_EL0. *
Catalin Marinas
maintainerPATCH v6 12/17 scatterlist: Add dedicated config for DMA flags
In reply to Catalin MarinasFrom: Robin Murphy <[email protected]>
The DMA flags field will be useful for users beyond PCI P2P, so upgrade to its own dedicated config option.
--- drivers/pci/Kconfig | 1 + include/linux/scatterlist.h | 13 ++++++------- kernel/dma/Kconfig | 3 +++ 3 files changed, 10 insertions(+), 7 deletions(-)
drivers/pci/Kconfig +1 -0
@@ -168,6 +168,7 @@ config PCI_P2PDMA # depends on 64BIT select GENERIC_ALLOCATOR + select NEED_SG_DMA_FLAGS help Enableѕ drivers to do PCI peer-to-peer transactions to and from BARs that are exposed in other devices that are the part of
include/linux/scatterlist.h +6 -7
@@ -16,7 +16,7 @@ struct scatterlist {#ifdef CONFIG_NEED_SG_DMA_LENGTH unsigned int dma_length;#endif -#ifdef CONFIG_PCI_P2PDMA +#ifdef CONFIG_NEED_SG_DMA_FLAGS unsigned int dma_flags;#endif };
@@ -249,12 +249,11 @@ static inline void sg_unmark_end(struct scatterlist *sg) }/* -* CONFGI_PCI_P2PDMA depends on CONFIG_64BIT which means there is 4 bytes -* in struct scatterlist (assuming also CONFIG_NEED_SG_DMA_LENGTH is set). -* Use this padding for DMA flags bits to indicate when a specific -* dma address is a bus address. +* One 64-bit architectures there is a 4-byte padding in struct scatterlist +* (assuming also CONFIG_NEED_SG_DMA_LENGTH is set). Use this padding for DMA +* flags bits to indicate when a specific dma address is a bus address. */ -#ifdef CONFIG_PCI_P2PDMA +#ifdef CONFIG_NEED_SG_DMA_FLAGS #define SG_DMA_BUS_ADDRESS (1 << 0)
@@ -312,7 +311,7 @@ static inline void sg_dma_unmark_bus_address(struct scatterlist *sg) { } -#endif +#endif /* CONFIG_NEED_SG_DMA_FLAGS */ /** * sg_phys - Return physical address of an sg entry
kernel/dma/Kconfig +3 -0
@@ -24,6 +24,9 @@ config DMA_OPS_BYPASS config ARCH_HAS_DMA_MAP_DIRECT bool +config NEED_SG_DMA_FLAGS + bool + config NEED_SG_DMA_LENGTH bool
Catalin Marinas
maintainerPATCH v6 13/17 dma-mapping: Name SG DMA flag helpers consistently
In reply to Catalin MarinasFrom: Robin Murphy <[email protected]>
sg_is_dma_bus_address() is inconsistent with the naming pattern of its corresponding setters and its own kerneldoc, so take the majority vote and rename it sg_dma_is_bus_address() (and fix up the missing underscores in the kerneldoc too). This gives us a nice clear pattern where SG DMA flags are SG_DMA_<NAME>, and the helpers for acting on them are sg_dma_<action>_<name>().
--- drivers/iommu/dma-iommu.c | 8 ++++---- drivers/iommu/iommu.c | 2 +- include/linux/scatterlist.h | 8 ++++---- kernel/dma/direct.c | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-)
drivers/iommu/dma-iommu.c +4 -4
@@ -1080,7 +1080,7 @@ static int __finalise_sg(struct device *dev, struct scatterlist *sg, int nents,sg_dma_address (s) =DMA_MAPPING_ERROR ;sg_dma_len (s) =0 ; -if (sg_is_dma_bus_address (s)) { +if (sg_dma_is_bus_address (s)) {if (i >0 ) cur =sg_next (cur);
@@ -1136,7 +1136,7 @@ static void __invalidate_sg(struct scatterlist *sg, int nents)int i;for_each_sg (sg, s, nents, i) { -if (sg_is_dma_bus_address (s)) { +if (sg_dma_is_bus_address (s)) {sg_dma_unmark_bus_address (s); }else {if (sg_dma_address (s) !=DMA_MAPPING_ERROR )
@@ -1329,7 +1329,7 @@ static void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg,* just have to be determined. */ for_each_sg (sg, tmp, nents, i) { -if (sg_is_dma_bus_address (tmp)) { +if (sg_dma_is_bus_address (tmp)) {sg_dma_unmark_bus_address (tmp);continue ; }
@@ -1343,7 +1343,7 @@ static void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg, nents -= i;for_each_sg (tmp, tmp, nents, i) { -if (sg_is_dma_bus_address (tmp)) { +if (sg_dma_is_bus_address (tmp)) {sg_dma_unmark_bus_address (tmp);continue ; }
drivers/iommu/iommu.c +1 -1
@@ -2567,7 +2567,7 @@ ssize_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova, len =0 ; } -if (sg_is_dma_bus_address (sg)) +if (sg_dma_is_bus_address (sg)) goto next;if (len) {
include/linux/scatterlist.h +4 -4
@@ -258,7 +258,7 @@ static inline void sg_unmark_end(struct scatterlist *sg)#define SG_DMA_BUS_ADDRESS (1 << 0)/** -* sg_dma_is_bus address - Return whether a given segment was marked +* sg_dma_is_bus_address - Return whether a given segment was marked * as a bus address * @sg: SG entry *
@@ -266,13 +266,13 @@ static inline void sg_unmark_end(struct scatterlist *sg)* Returns true if sg_dma_mark_bus_address() has been called on * this segment. **/ -static inline bool sg_is_dma_bus_address (struct scatterlist *sg) +static inline bool sg_dma_is_bus_address (struct scatterlist *sg) {return sg->dma_flags &SG_DMA_BUS_ADDRESS ; }/** -* sg_dma_mark_bus address - Mark the scatterlist entry as a bus address +* sg_dma_mark_bus_address - Mark the scatterlist entry as a bus address * @sg: SG entry * * Description:
@@ -300,7 +300,7 @@ static inline void sg_dma_unmark_bus_address(struct scatterlist *sg)#else -static inline bool sg_is_dma_bus_address (struct scatterlist *sg) +static inline bool sg_dma_is_bus_address (struct scatterlist *sg) {return false; }
kernel/dma/direct.c +1 -1
@@ -463,7 +463,7 @@ void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sgl,int i;for_each_sg (sgl, sg, nents, i) { -if (sg_is_dma_bus_address (sg)) +if (sg_dma_is_bus_address (sg))sg_dma_unmark_bus_address (sg);else dma_direct_unmap_page(dev, sg->dma_address,
Catalin Marinas
maintainerPATCH v6 14/17 dma-mapping: Force bouncing if the kmalloc() size is not cache-line-aligned
In reply to Catalin MarinasFor direct DMA, if the size is small enough to have originated from a kmalloc() cache below ARCH_DMA_MINALIGN, check its alignment against dma_get_cache_alignment() and bounce if necessary. For larger sizes, it is the responsibility of the DMA API caller to ensure proper alignment.
At this point, the kmalloc() caches are properly aligned but this will change in a subsequent patch.
Architectures can opt in by selecting ARCH_WANT_KMALLOC_DMA_BOUNCE.
--- include/linux/dma-map-ops.h | 61 +++++++++++++++++++++++++++++++++++++ kernel/dma/Kconfig | 4 +++ kernel/dma/direct.h | 3 +- 3 files changed, 67 insertions(+), 1 deletion(-)
include/linux/dma-map-ops.h +61 -0
@@ -8,6 +8,7 @@#include <linux/dma-mapping.h> #include <linux/pgtable.h> +#include <linux/slab.h> struct cma ;
@@ -277,6 +278,66 @@ static inline bool dev_is_dma_coherent(struct device *dev) }#endif /* CONFIG_ARCH_HAS_DMA_COHERENCE_H */ +/* +* Check whether potential kmalloc() buffers are safe for non-coherent DMA. +*/ +static inline bool dma_kmalloc_safe (struct device *dev, +enum dma_data_direction dir) +{ +/* +* If DMA bouncing of kmalloc() buffers is disabled, the kmalloc() +* caches have already been aligned to a DMA-safe size. +*/ +if (!IS_ENABLED (CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC )) +return true; + +/* +* kmalloc() buffers are DMA-safe irrespective of size if the device +* is coherent or the direction is DMA_TO_DEVICE (non-desctructive +* cache maintenance and benign cache line evictions). +*/ +if (dev_is_dma_coherent (dev) || dir ==DMA_TO_DEVICE ) +return true; + +return false; +} + +/* +* Check whether the given size, assuming it is for a kmalloc()'ed buffer, is +* sufficiently aligned for non-coherent DMA. +*/ +static inline bool dma_kmalloc_size_aligned (size_t size) +{ +/* +* Larger kmalloc() sizes are guaranteed to be aligned to +* ARCH_DMA_MINALIGN. +*/ +if (size >=2 *ARCH_DMA_MINALIGN || +IS_ALIGNED (kmalloc_size_roundup (size),dma_get_cache_alignment ())) +return true; + +return false; +} + +/* +* Check whether the given object size may have originated from a kmalloc() +* buffer with a slab alignment below the DMA-safe alignment and needs +* bouncing for non-coherent DMA. The pointer alignment is not considered and +* in-structure DMA-safe offsets are the responsibility of the caller. Such +* code should use the static ARCH_DMA_MINALIGN for compiler annotations. +* +* The heuristics can have false positives, bouncing unnecessarily, though the +* buffers would be small. False negatives are theoretically possible if, for +* example, multiple small kmalloc() buffers are coalesced into a larger +* buffer that passes the alignment check. There are no such known constructs +* in the kernel. +*/ +static inline bool dma_kmalloc_needs_bounce (struct device *dev,size_t size, +enum dma_data_direction dir) +{ +return !dma_kmalloc_safe (dev, dir) && !dma_kmalloc_size_aligned (size); +} +void *arch_dma_alloc (struct device *dev,size_t size,dma_addr_t *dma_handle,gfp_t gfp,unsigned long attrs); void arch_dma_free(struct device *dev, size_t size, void *cpu_addr,
kernel/dma/Kconfig +4 -0
@@ -90,6 +90,10 @@ config SWIOTLB bool select NEED_DMA_MAP_STATE +config DMA_BOUNCE_UNALIGNED_KMALLOC + bool + depends on SWIOTLB + config DMA_RESTRICTED_POOL bool "DMA Restricted Pool" depends on OF && OF_RESERVED_MEM && SWIOTLB
kernel/dma/direct.h +2 -1
@@ -94,7 +94,8 @@ static inline dma_addr_t dma_direct_map_page(struct device *dev,return swiotlb_map (dev, phys, size, dir, attrs); } -if (unlikely (!dma_capable (dev, dma_addr, size, true))) { +if (unlikely (!dma_capable (dev, dma_addr, size, true)) || +dma_kmalloc_needs_bounce (dev, size, dir)) {if (is_pci_p2pdma_page (page))return DMA_MAPPING_ERROR ;if (is_swiotlb_active (dev))
Catalin Marinas
maintainerPATCH v6 15/17 iommu/dma: Force bouncing if the size is not cacheline-aligned
In reply to Catalin MarinasSimilarly to the direct DMA, bounce small allocations as they may have originated from a kmalloc() cache not safe for DMA. Unlike the direct DMA, iommu_dma_map_sg() cannot call iommu_dma_map_sg_swiotlb() for all non-coherent devices as this would break some cases where the iova is expected to be contiguous (dmabuf). Instead, scan the scatterlist for any small sizes and only go the swiotlb path if any element of the list needs bouncing (note that iommu_dma_map_page() would still only bounce those buffers which are not DMA-aligned).
To avoid scanning the scatterlist on the 'sync' operations, introduce an SG_DMA_SWIOTLB flag set by iommu_dma_map_sg_swiotlb(). The dev_use_swiotlb() function together with the newly added dev_use_sg_swiotlb() now check for both untrusted devices and unaligned kmalloc() buffers (suggested by Robin Murphy).
--- drivers/iommu/Kconfig | 1 + drivers/iommu/dma-iommu.c | 50 ++++++++++++++++++++++++++++++------- include/linux/scatterlist.h | 41 ++++++++++++++++++++++++++++-- 3 files changed, 81 insertions(+), 11 deletions(-)
drivers/iommu/Kconfig +1 -0
@@ -152,6 +152,7 @@ config IOMMU_DMA select IOMMU_IOVA select IRQ_MSI_IOMMU select NEED_SG_DMA_LENGTH + select NEED_SG_DMA_FLAGS if SWIOTLB # Shared Virtual Addressing config IOMMU_SVA
drivers/iommu/dma-iommu.c +41 -9
105 lines not on this page · raw message
include/linux/scatterlist.h +39 -2
62 lines not on this page · raw message
Robin Murphy
maintainerRe: [PATCH v6 15/17] iommu/dma: Force bouncing if the size is not cacheline-aligned
On 2023-05-31 16:48, Catalin Marinas wrote:
[...]
Quoted 25 lines
include/linux/scatterlist.h +5
-2@@ -251,11 +251,13 @@ static inline void sg_unmark_end(struct scatterlist *sg)/* * One 64-bit architectures there is a 4-byte padding in struct scatterlist * (assuming also CONFIG_NEED_SG_DMA_LENGTH is set). Use this padding for DMA -* flags bits to indicate when a specific dma address is a bus address. +* flags bits to indicate when a specific dma address is a bus address or the +* buffer may have been bounced via SWIOTLB. */ #ifdef CONFIG_NEED_SG_DMA_FLAGS -#define SG_DMA_BUS_ADDRESS (1 << 0) +#define SG_DMA_BUS_ADDRESS (1 << 0) +#define SG_DMA_SWIOTLB (1 << 1)/** * sg_dma_is_bus_address - Return whether a given segment was marked @@ -298,6 +300,34 @@ static inline void sg_dma_unmark_bus_address(struct scatterlist *sg) sg->dma_flags &= ~SG_DMA_BUS_ADDRESS ; } +/**
include/linux/scatterlist.h static inline void sg_dma_unmark_bus_address(struct scatterlist *sg) · line 313
Quoted 10 lines
include/linux/scatterlist.h +10
-0+* sg_dma_use_swiotlb - Return whether the scatterlist was marked for SWIOTLB +* bouncing +* @sg: SG entry +* +* Description: +* Returns true if the scatterlist was marked for SWIOTLB bouncing. Not all +* elements may have been bounced, so the caller would have to check +* individual SG entries with is_swiotlb_buffer(). +*/ +static inline bool sg_dma_use_swiotlb (struct scatterlist *sg)
Nit: since you tweaked the flag name again, we could happily go back to the pattern with s/use/is/ for this one now too.
include/linux/scatterlist.h static inline void sg_dma_unmark_bus_address(struct scatterlist *sg) · line 319
Quoted 6 lines
include/linux/scatterlist.h +6
-0+{ +return sg->dma_flags &SG_DMA_SWIOTLB ; +} + +/** +* sg_dma_use_swiotlb - Mark the scatterlist for SWIOTLB bouncing
Oops - s/use/mark/
Feel free to fix those up when applying if there's no other reason for a v7.
Thanks,
Robin.
Quoted 28 lines
include/linux/scatterlist.h +18
-0+* @sg: SG entry +* +* Description: +* Marks a a scatterlist for SWIOTLB bounce. Not all SG entries may be +* bounced. +*/ +static inline void sg_dma_mark_swiotlb (struct scatterlist *sg) +{ + sg->dma_flags |=SG_DMA_SWIOTLB ; +} +#else static inline bool sg_dma_is_bus_address (struct scatterlist *sg)@@ -310,6 +340,13 @@ static inline void sg_dma_mark_bus_address(struct scatterlist *sg)static inline void sg_dma_unmark_bus_address (struct scatterlist *sg) { } +static inline bool sg_dma_use_swiotlb (struct scatterlist *sg) +{ +return false; +} +static inline void sg_dma_mark_swiotlb (struct scatterlist *sg) +{ +}#endif /* CONFIG_NEED_SG_DMA_FLAGS */
Catalin Marinas
maintainerPATCH v6 16/17 mm: slab: Reduce the kmalloc() minimum alignment if DMA bouncing possible
In reply to Catalin MarinasIf an architecture opted in to DMA bouncing of unaligned kmalloc() buffers (ARCH_WANT_KMALLOC_DMA_BOUNCE), reduce the minimum kmalloc() cache alignment below cache-line size to ARCH_KMALLOC_MINALIGN.
--- mm/slab_common.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
mm/slab_common.c +10 -0
@@ -18,6 +18,7 @@#include <linux/uaccess.h> #include <linux/seq_file.h> #include <linux/dma-mapping.h> +#include <linux/swiotlb.h> #include <linux/proc_fs.h> #include <linux/debugfs.h> #include <linux/kasan.h>
@@ -863,10 +864,19 @@ void __init setup_kmalloc_cache_index_table(void) } } +#ifdef CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC +static unsigned int __kmalloc_minalign (void ) +{ +if (io_tlb_default_mem.nslabs) +return ARCH_KMALLOC_MINALIGN ; +return dma_get_cache_alignment (); +} +#else static unsigned int __kmalloc_minalign (void ) {return dma_get_cache_alignment (); } +#endif void __init new_kmalloc_cache (int idx,enum kmalloc_cache_type type,slab_flags_t flags)
Re: [PATCH v6 16/17] mm: slab: Reduce the kmalloc() minimum alignment if DMA bouncing possible
Quoted 5 lines · Catalin Marinas
If an architecture opted in to DMA bouncing of unaligned
kmalloc()buffers (ARCH_WANT_KMALLOC_DMA_BOUNCE), reduce the minimumkmalloc()cache alignment below cache-line size toARCH_KMALLOC_MINALIGN.Signed-off-by Catalin Marinas <[email protected]>
Nit below:
Quoted 26 lines
Cc Andrew Morton <[email protected]>Cc Christoph Hellwig <[email protected]>Cc Robin Murphy <[email protected]>--- mm/slab_common.c | 10 ++++++++++ 1 file changed, 10 insertions(+)mm/slab_common.c +4
-0@@ -18,6 +18,7 @@#include <linux/uaccess.h> #include <linux/seq_file.h> #include <linux/dma-mapping.h> +#include <linux/swiotlb.h> #include <linux/proc_fs.h> #include <linux/debugfs.h> #include <linux/kasan.h> @@ -863,10 +864,19 @@ void __init setup_kmalloc_cache_index_table(void) } } +#ifdef CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC +static unsigned int __kmalloc_minalign (void ) +{
mm/slab_common.c void __init setup_kmalloc_cache_index_table(void) · line 879
Quoted 10 lines
mm/slab_common.c +6
-0+if (io_tlb_default_mem.nslabs) +return ARCH_KMALLOC_MINALIGN ; + return dma_get_cache_alignment(); +} +#else static unsigned int __kmalloc_minalign (void ) {return dma_get_cache_alignment (); } +#endif
Should be enough to put the #ifdef around the two lines into a single implementation of the function?
Quoted 3 lines
mm/slab_common.c +0
-0void __init new_kmalloc_cache (int idx,enum kmalloc_cache_type type,slab_flags_t flags)
Catalin Marinas
maintainerPATCH v6 17/17 arm64: Enable ARCH_WANT_KMALLOC_DMA_BOUNCE for arm64
In reply to Catalin MarinasWith the DMA bouncing of unaligned kmalloc() buffers now in place, enable it for arm64 to allow the kmalloc-{8,16,32,48,96} caches. In addition, always create the swiotlb buffer even when the end of RAM is within the 32-bit physical address range (the swiotlb buffer can still be disabled on the kernel command line).
--- arch/arm64/Kconfig | 1 + arch/arm64/mm/init.c | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-)
arch/arm64/Kconfig +1 -0
@@ -120,6 +120,7 @@ config ARM64 select CRC32 select DCACHE_WORD_ACCESS select DYNAMIC_FTRACE if FUNCTION_TRACER + select DMA_BOUNCE_UNALIGNED_KMALLOC select DMA_DIRECT_REMAP select EDAC_SUPPORT select FRAME_POINTER
arch/arm64/mm/init.c +6 -1
@@ -442,7 +442,12 @@ void __init bootmem_init(void)*/ void __init mem_init (void ) { -swiotlb_init (max_pfn >PFN_DOWN (arm64_dma_phys_limit),SWIOTLB_VERBOSE ); +bool swiotlb = max_pfn >PFN_DOWN (arm64_dma_phys_limit); + +if (IS_ENABLED (CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC )) + swiotlb = true; + +swiotlb_init (swiotlb,SWIOTLB_VERBOSE );/* this will put all unused low memory onto the freelists */ memblock_free_all ();
Quoted 3 lines · Catalin Marinas
Here's version 6 of the series reducing the
kmalloc()minimum alignment on arm64 to 8 (from 128). There are patches already to do the same for riscv (pretty straight-forward after this series).
Thanks, Catalin for getting these patches out. Please add my "Tested-by:" tag for the series:
With the first 11 patches, I observed a reduction of 18.4 MB in the slab memory footprint on my Pixel 6 device. After applying the rest of the patches in the series, I observed a total reduction of 26.5 MB in the
slab memory footprint on my device. These are great results!
--Isaac
Quoted 8 lines · Isaac Manjarres
Quoted 3 lines · Catalin Marinas
Here's version 6 of the series reducing the
kmalloc()minimum alignment on arm64 to 8 (from 128). There are patches already to do the same for riscv (pretty straight-forward after this series).Thanks, Catalin for getting these patches out. Please add my "Tested-by:" tag for the series:
Quoted 8 lines
Tested-by Isaac J. Manjarres <[email protected]>With the first 11 patches, I observed a reduction of 18.4 MB in the slab memory footprint on my Pixel 6 device. After applying the rest of the patches in the series, I observed a total reduction of 26.5 MB in the
slab memory footprint on my device. These are great results!
It would also be good to get an insight into how much bouncing is going on in this case, given that (AFAIK) Pixel 6 uses non-cache coherent DMA.
Quoted 18 lines · Ard Biesheuvel
Quoted 8 lines · Isaac Manjarres
Quoted 3 lines · Catalin Marinas
Here's version 6 of the series reducing the
kmalloc()minimum alignment on arm64 to 8 (from 128). There are patches already to do the same for riscv (pretty straight-forward after this series).Thanks, Catalin for getting these patches out. Please add my "Tested-by:" tag for the series:
Quoted 8 lines
Tested-by Isaac J. Manjarres <[email protected]>With the first 11 patches, I observed a reduction of 18.4 MB in the slab memory footprint on my Pixel 6 device. After applying the rest of the patches in the series, I observed a total reduction of 26.5 MB in the
slab memory footprint on my device. These are great results!
Quoted 3 lines
It would also be good to get an insight into how much bouncing is going on in this case, given that (AFAIK) Pixel 6 uses non-cache coherent DMA.
I enabled the "swiotlb_bounced" trace event from the kernel command line to see if anything was being bounced. It turns out that for Pixel 6 there are non-coherent DMA transfers occurring, but none of the transfers that are in either the DMA_FROM_DEVICE or DMA_BIDIRECTIONAL directions are small enough to require bouncing.
--Isaac
P.S. I noticed that the trace_swiotlb_bounced() tracepoint may not be invoked even though bouncing occurs. For example, in the dma-iommu path, swiotlb_tbl_map_single() is called when bouncing, instead of swiotlb_map(), which is what ends up calling trace_swiotlb_bounced().
Would it make sense to move the call to trace_swiotlb_bounced() to swiotlb_tbl_map_single() since that function is always invoked?
Quoted 8 lines · On Thu, 8 Jun 2023 14:29:45 -0700 Isaac Manjarres <[email protected]>…
[...]
P.S. I noticed that thetrace_swiotlb_bounced()tracepoint may not be invoked even though bouncing occurs. For example, in the dma-iommu path,swiotlb_tbl_map_single()is called when bouncing, instead ofswiotlb_map(), which is what ends up callingtrace_swiotlb_bounced().Would it make sense to move the call to
trace_swiotlb_bounced()toswiotlb_tbl_map_single()since that function is always invoked?
Definitely, if you ask me. I believe the change was merely forgotten in commit eb605a5754d0 ("swiotlb: add swiotlb_tbl_map_single library function").
Let me take the author into Cc. Plus Konrad, who built further on that commit, may also have an opinion.
Petr T
Hi,
Quoted 8 lines · On Thu, 8 Jun 2023 14:29:45 -0700 Isaac Manjarres <[email protected]>…
P.S. I noticed that thetrace_swiotlb_bounced()tracepoint may not be invoked even though bouncing occurs. For example, in the dma-iommu path,swiotlb_tbl_map_single()is called when bouncing, instead ofswiotlb_map(), which is what ends up callingtrace_swiotlb_bounced().Would it make sense to move the call to
trace_swiotlb_bounced()toswiotlb_tbl_map_single()since that function is always invoked?
Definitely, if you ask me. I believe the change was merely forgotten in commit eb605a5754d0 ("swiotlb: add swiotlb_tbl_map_single library function").
Let me take the author into Cc. Plus Konrad, who built further on that commit, may also have an opinion.
When I wrote the patch, trace_swiotlb_bounced() existed?
I cannot recall the patch but from quick look, moving trace_swiotlb_bounced() to swiotlb_tbl_map_single() makes sense.
thanks
Quoted 2 lines · Tomonori Fujita
I cannot recall the patch but from quick look, moving
trace_swiotlb_bounced()toswiotlb_tbl_map_single()makes sense.
Agreed.
Quoted 5 lines · Christoph Hellwig
Quoted 2 lines · Tomonori Fujita
I cannot recall the patch but from quick look, moving
trace_swiotlb_bounced()toswiotlb_tbl_map_single()makes sense.Agreed.
There's actually two call-sites for trace_swiotlb_bounced():swiotlb_map() and xen_swiotlb_map_page(). Both those functions also invoke swiotlb_tbl_map_single(), so moving the call to trace_swiotlb_bounced() to swiotlb_tbl_map_single() means that there will be 2 traces per bounce buffering event.
The difference between the two call-sites of trace_swiotlb_bounced() is that the call in swiotlb_map() uses phys_to_dma() for the device address, while xen_swiotlb_map_page() uses xen_phys_to_dma().
Would it make sense to move the trace_swiotlb_bounced() call to swiotlb_tbl_map_single() and then introduce a swiotlb_tbl_map_single_notrace() function which doesn't do the tracing, and xen_swiotlb_map_page() can call this?
--Isaac