@@ -426,8 +426,9 @@ enum kmalloc_cache_type {
426426 NR_KMALLOC_TYPES
427427};
428428
429- extern struct kmem_cache *
430- kmalloc_caches [NR_KMALLOC_TYPES ][KMALLOC_SHIFT_HIGH + 1 ];
429+ typedef struct kmem_cache * kmem_buckets [KMALLOC_SHIFT_HIGH + 1 ];
430+
431+ extern kmem_buckets kmalloc_caches [NR_KMALLOC_TYPES ];
431432
432433/*
433434 * Define gfp bits that should not be set for KMALLOC_NORMAL.
@@ -528,9 +529,6 @@ static_assert(PAGE_SHIFT <= 20);
528529
529530#include <linux/alloc_tag.h>
530531
531- void * __kmalloc_noprof (size_t size , gfp_t flags ) __assume_kmalloc_alignment __alloc_size (1 );
532- #define __kmalloc (...) alloc_hooks(__kmalloc_noprof(__VA_ARGS__))
533-
534532/**
535533 * kmem_cache_alloc - Allocate an object
536534 * @cachep: The cache to allocate from.
@@ -551,6 +549,10 @@ void *kmem_cache_alloc_lru_noprof(struct kmem_cache *s, struct list_lru *lru,
551549
552550void kmem_cache_free (struct kmem_cache * s , void * objp );
553551
552+ kmem_buckets * kmem_buckets_create (const char * name , slab_flags_t flags ,
553+ unsigned int useroffset , unsigned int usersize ,
554+ void (* ctor )(void * ));
555+
554556/*
555557 * Bulk allocation and freeing operations. These are accelerated in an
556558 * allocator specific way to avoid taking locks repeatedly or building
@@ -568,31 +570,49 @@ static __always_inline void kfree_bulk(size_t size, void **p)
568570 kmem_cache_free_bulk (NULL , size , p );
569571}
570572
571- void * __kmalloc_node_noprof (size_t size , gfp_t flags , int node ) __assume_kmalloc_alignment
572- __alloc_size (1 );
573- #define __kmalloc_node (...) alloc_hooks(__kmalloc_node_noprof(__VA_ARGS__))
574-
575573void * kmem_cache_alloc_node_noprof (struct kmem_cache * s , gfp_t flags ,
576574 int node ) __assume_slab_alignment __malloc ;
577575#define kmem_cache_alloc_node (...) alloc_hooks(kmem_cache_alloc_node_noprof(__VA_ARGS__))
578576
579- void * kmalloc_trace_noprof (struct kmem_cache * s , gfp_t flags , size_t size )
580- __assume_kmalloc_alignment __alloc_size (3 );
577+ /*
578+ * These macros allow declaring a kmem_buckets * parameter alongside size, which
579+ * can be compiled out with CONFIG_SLAB_BUCKETS=n so that a large number of call
580+ * sites don't have to pass NULL.
581+ */
582+ #ifdef CONFIG_SLAB_BUCKETS
583+ #define DECL_BUCKET_PARAMS (_size , _b ) size_t (_size), kmem_buckets *(_b)
584+ #define PASS_BUCKET_PARAMS (_size , _b ) (_size), (_b)
585+ #define PASS_BUCKET_PARAM (_b ) (_b)
586+ #else
587+ #define DECL_BUCKET_PARAMS (_size , _b ) size_t (_size)
588+ #define PASS_BUCKET_PARAMS (_size , _b ) (_size)
589+ #define PASS_BUCKET_PARAM (_b ) NULL
590+ #endif
591+
592+ /*
593+ * The following functions are not to be used directly and are intended only
594+ * for internal use from kmalloc() and kmalloc_node()
595+ * with the exception of kunit tests
596+ */
597+
598+ void * __kmalloc_noprof (size_t size , gfp_t flags )
599+ __assume_kmalloc_alignment __alloc_size (1 );
581600
582- void * kmalloc_node_trace_noprof (struct kmem_cache * s , gfp_t gfpflags ,
583- int node , size_t size ) __assume_kmalloc_alignment
584- __alloc_size (4 );
585- #define kmalloc_trace (...) alloc_hooks(kmalloc_trace_noprof(__VA_ARGS__))
601+ void * __kmalloc_node_noprof (DECL_BUCKET_PARAMS (size , b ), gfp_t flags , int node )
602+ __assume_kmalloc_alignment __alloc_size (1 );
586603
587- #define kmalloc_node_trace (...) alloc_hooks(kmalloc_node_trace_noprof(__VA_ARGS__))
604+ void * __kmalloc_cache_noprof (struct kmem_cache * s , gfp_t flags , size_t size )
605+ __assume_kmalloc_alignment __alloc_size (3 );
588606
589- void * kmalloc_large_noprof ( size_t size , gfp_t flags ) __assume_page_alignment
590- __alloc_size ( 1 );
591- #define kmalloc_large (...) alloc_hooks(kmalloc_large_noprof(__VA_ARGS__))
607+ void * __kmalloc_cache_node_noprof ( struct kmem_cache * s , gfp_t gfpflags ,
608+ int node , size_t size )
609+ __assume_kmalloc_alignment __alloc_size ( 4 );
592610
593- void * kmalloc_large_node_noprof (size_t size , gfp_t flags , int node ) __assume_page_alignment
594- __alloc_size (1 );
595- #define kmalloc_large_node (...) alloc_hooks(kmalloc_large_node_noprof(__VA_ARGS__))
611+ void * __kmalloc_large_noprof (size_t size , gfp_t flags )
612+ __assume_page_alignment __alloc_size (1 );
613+
614+ void * __kmalloc_large_node_noprof (size_t size , gfp_t flags , int node )
615+ __assume_page_alignment __alloc_size (1 );
596616
597617/**
598618 * kmalloc - allocate kernel memory
@@ -604,7 +624,8 @@ void *kmalloc_large_node_noprof(size_t size, gfp_t flags, int node) __assume_pag
604624 *
605625 * The allocated object address is aligned to at least ARCH_KMALLOC_MINALIGN
606626 * bytes. For @size of power of two bytes, the alignment is also guaranteed
607- * to be at least to the size.
627+ * to be at least to the size. For other sizes, the alignment is guaranteed to
628+ * be at least the largest power-of-two divisor of @size.
608629 *
609630 * The @flags argument may be one of the GFP flags defined at
610631 * include/linux/gfp_types.h and described at
@@ -654,31 +675,37 @@ static __always_inline __alloc_size(1) void *kmalloc_noprof(size_t size, gfp_t f
654675 unsigned int index ;
655676
656677 if (size > KMALLOC_MAX_CACHE_SIZE )
657- return kmalloc_large_noprof (size , flags );
678+ return __kmalloc_large_noprof (size , flags );
658679
659680 index = kmalloc_index (size );
660- return kmalloc_trace_noprof (
681+ return __kmalloc_cache_noprof (
661682 kmalloc_caches [kmalloc_type (flags , _RET_IP_ )][index ],
662683 flags , size );
663684 }
664685 return __kmalloc_noprof (size , flags );
665686}
666687#define kmalloc (...) alloc_hooks(kmalloc_noprof(__VA_ARGS__))
667688
689+ #define kmem_buckets_alloc (_b , _size , _flags ) \
690+ alloc_hooks(__kmalloc_node_noprof(PASS_BUCKET_PARAMS(_size, _b), _flags, NUMA_NO_NODE))
691+
692+ #define kmem_buckets_alloc_track_caller (_b , _size , _flags ) \
693+ alloc_hooks(__kmalloc_node_track_caller_noprof(PASS_BUCKET_PARAMS(_size, _b), _flags, NUMA_NO_NODE, _RET_IP_))
694+
668695static __always_inline __alloc_size (1 ) void * kmalloc_node_noprof (size_t size , gfp_t flags , int node )
669696{
670697 if (__builtin_constant_p (size ) && size ) {
671698 unsigned int index ;
672699
673700 if (size > KMALLOC_MAX_CACHE_SIZE )
674- return kmalloc_large_node_noprof (size , flags , node );
701+ return __kmalloc_large_node_noprof (size , flags , node );
675702
676703 index = kmalloc_index (size );
677- return kmalloc_node_trace_noprof (
704+ return __kmalloc_cache_node_noprof (
678705 kmalloc_caches [kmalloc_type (flags , _RET_IP_ )][index ],
679706 flags , node , size );
680707 }
681- return __kmalloc_node_noprof (size , flags , node );
708+ return __kmalloc_node_noprof (PASS_BUCKET_PARAMS ( size , NULL ) , flags , node );
682709}
683710#define kmalloc_node (...) alloc_hooks(kmalloc_node_noprof(__VA_ARGS__))
684711
@@ -729,8 +756,10 @@ static inline __realloc_size(2, 3) void * __must_check krealloc_array_noprof(voi
729756 */
730757#define kcalloc (n , size , flags ) kmalloc_array(n, size, (flags) | __GFP_ZERO)
731758
732- void * kmalloc_node_track_caller_noprof (size_t size , gfp_t flags , int node ,
733- unsigned long caller ) __alloc_size (1 );
759+ void * __kmalloc_node_track_caller_noprof (DECL_BUCKET_PARAMS (size , b ), gfp_t flags , int node ,
760+ unsigned long caller ) __alloc_size (1 );
761+ #define kmalloc_node_track_caller_noprof (size , flags , node , caller ) \
762+ __kmalloc_node_track_caller_noprof(PASS_BUCKET_PARAMS(size, NULL), flags, node, caller)
734763#define kmalloc_node_track_caller (...) \
735764 alloc_hooks(kmalloc_node_track_caller_noprof(__VA_ARGS__, _RET_IP_))
736765
@@ -756,7 +785,7 @@ static inline __alloc_size(1, 2) void *kmalloc_array_node_noprof(size_t n, size_
756785 return NULL ;
757786 if (__builtin_constant_p (n ) && __builtin_constant_p (size ))
758787 return kmalloc_node_noprof (bytes , flags , node );
759- return __kmalloc_node_noprof (bytes , flags , node );
788+ return __kmalloc_node_noprof (PASS_BUCKET_PARAMS ( bytes , NULL ) , flags , node );
760789}
761790#define kmalloc_array_node (...) alloc_hooks(kmalloc_array_node_noprof(__VA_ARGS__))
762791
@@ -780,14 +809,18 @@ static inline __alloc_size(1) void *kzalloc_noprof(size_t size, gfp_t flags)
780809#define kzalloc (...) alloc_hooks (kzalloc_noprof (__VA_ARGS__ ))
781810#define kzalloc_node (_size , _flags , _node ) kmalloc_node (_size , (_flags )|__GFP_ZERO , _node )
782811
783- extern void * kvmalloc_node_noprof (size_t size , gfp_t flags , int node ) __alloc_size (1 );
812+ void * __kvmalloc_node_noprof (DECL_BUCKET_PARAMS (size , b ), gfp_t flags , int node ) __alloc_size (1 );
813+ #define kvmalloc_node_noprof (size , flags , node ) \
814+ __kvmalloc_node_noprof(PASS_BUCKET_PARAMS(size, NULL), flags, node)
784815#define kvmalloc_node (...) alloc_hooks(kvmalloc_node_noprof(__VA_ARGS__))
785816
786817#define kvmalloc (_size , _flags ) kvmalloc_node(_size, _flags, NUMA_NO_NODE)
787818#define kvmalloc_noprof (_size , _flags ) kvmalloc_node_noprof(_size, _flags, NUMA_NO_NODE)
788819#define kvzalloc (_size , _flags ) kvmalloc(_size, (_flags)|__GFP_ZERO)
789820
790821#define kvzalloc_node (_size , _flags , _node ) kvmalloc_node(_size, (_flags)|__GFP_ZERO, _node)
822+ #define kmem_buckets_valloc (_b , _size , _flags ) \
823+ alloc_hooks(__kvmalloc_node_noprof(PASS_BUCKET_PARAMS(_size, _b), _flags, NUMA_NO_NODE))
791824
792825static inline __alloc_size (1 , 2 ) void *
793826kvmalloc_array_node_noprof (size_t n , size_t size , gfp_t flags , int node )
0 commit comments