On 3/31/2026 7:12 PM, Marco Elver wrote:
> ...
> @@ -662,9 +662,20 @@ extern kmem_buckets kmalloc_caches[NR_KMALLOC_TYPES];
>       (IS_ENABLED(CONFIG_ZONE_DMA)   ? __GFP_DMA : 0) |       \
>       (IS_ENABLED(CONFIG_MEMCG) ? __GFP_ACCOUNT : 0))
>  
> +#ifdef CONFIG_RANDOM_KMALLOC_CACHES
>  extern unsigned long random_kmalloc_seed;
> +typedef struct { unsigned long ip; } kmalloc_token_t;
> +#define __kmalloc_token(...) ((kmalloc_token_t) { .ip = _RET_IP_ })
> +#elif defined(CONFIG_TYPED_KMALLOC_CACHES)
> +typedef struct { unsigned long v; } kmalloc_token_t;
> +#define __kmalloc_token(...) ((kmalloc_token_t){ .v = 
> __builtin_infer_alloc_token(__VA_ARGS__) })

One tiny suggestion: we could use the same name for kmalloc_token_t's
member in both cases, which would make the code a bit more concise.

Acked-by: GONG Ruiqi <[email protected]>

> +#else
> +/* no-op */
> +typedef struct {} kmalloc_token_t;
> +#define __kmalloc_token(...) ((kmalloc_token_t){})
> +#endif
>  
> -static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags, 
> unsigned long caller)
> +static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags, 
> kmalloc_token_t token)
>  {
>       /*
>        * The most common case is KMALLOC_NORMAL, so test for it
> @@ -672,9 +683,11 @@ static __always_inline enum kmalloc_cache_type 
> kmalloc_type(gfp_t flags, unsigne
>        */
>       if (likely((flags & KMALLOC_NOT_NORMAL_BITS) == 0))
>  #ifdef CONFIG_RANDOM_KMALLOC_CACHES
> -             /* RANDOM_KMALLOC_CACHES_NR (=15) copies + the KMALLOC_NORMAL */
> -             return KMALLOC_RANDOM_START + hash_64(caller ^ 
> random_kmalloc_seed,
> -                                                   
> ilog2(RANDOM_KMALLOC_CACHES_NR + 1));
> +             /* PARTITION_KMALLOC_CACHES_NR (=15) copies + the 
> KMALLOC_NORMAL */
> +             return KMALLOC_PARTITION_START + hash_64(token.ip ^ 
> random_kmalloc_seed,
> +                                                      
> ilog2(PARTITION_KMALLOC_CACHES_NR + 1));
> +#elif defined(CONFIG_TYPED_KMALLOC_CACHES)
> +             return KMALLOC_PARTITION_START + token.v;
>  #else
>               return KMALLOC_NORMAL;
>  #endif
> ...

Reply via email to