From: Suren Baghdasaryan <surenb@google.com>
To: Vlastimil Babka <vbabka@suse.cz>
Cc: "Liam R. Howlett" <Liam.Howlett@oracle.com>,
Christoph Lameter <cl@gentwo.org>,
David Rientjes <rientjes@google.com>,
Roman Gushchin <roman.gushchin@linux.dev>,
Harry Yoo <harry.yoo@oracle.com>,
Uladzislau Rezki <urezki@gmail.com>,
Sidhartha Kumar <sidhartha.kumar@oracle.com>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
rcu@vger.kernel.org, maple-tree@lists.infradead.org
Subject: Re: [PATCH v8 13/23] tools/testing: Add support for changes to slab for sheaves
Date: Fri, 26 Sep 2025 16:28:43 -0700 [thread overview]
Message-ID: <CAJuCfpGYgOVG7hpZJ7MED_ALXZfKgUuRcPN3YYjx6k_t-dGXhg@mail.gmail.com> (raw)
In-Reply-To: <20250910-slub-percpu-caches-v8-13-ca3099d8352c@suse.cz>
On Wed, Sep 10, 2025 at 1:01 AM Vlastimil Babka <vbabka@suse.cz> wrote:
>
> From: "Liam R. Howlett" <Liam.Howlett@Oracle.com>
>
> The slab changes for sheaves requires more effort in the testing code.
> Unite all the kmem_cache work into the tools/include slab header for
> both the vma and maple tree testing.
>
> The vma test code also requires importing more #defines to allow for
> seamless use of the shared kmem_cache code.
>
> This adds the pthread header to the slab header in the tools directory
> to allow for the pthread_mutex in linux.c.
>
> Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
> Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
Reviewed-by: Suren Baghdasaryan <surenb@google.com>
The patch does several things and could be split in 3 (code
refactoring, kmem_cache_create change, new definitions like
_slab_flag_bits) but I don't think it's worth a respin.
> ---
> tools/include/linux/slab.h | 137 ++++++++++++++++++++++++++++++++++++--
> tools/testing/shared/linux.c | 26 ++------
> tools/testing/shared/maple-shim.c | 1 +
> tools/testing/vma/vma_internal.h | 92 +------------------------
> 4 files changed, 142 insertions(+), 114 deletions(-)
>
> diff --git a/tools/include/linux/slab.h b/tools/include/linux/slab.h
> index c87051e2b26f5a7fee0362697fae067076b8e84d..c5c5cc6db5668be2cc94c29065ccfa7ca7b4bb08 100644
> --- a/tools/include/linux/slab.h
> +++ b/tools/include/linux/slab.h
> @@ -4,11 +4,31 @@
>
> #include <linux/types.h>
> #include <linux/gfp.h>
> +#include <pthread.h>
>
> -#define SLAB_PANIC 2
> #define SLAB_RECLAIM_ACCOUNT 0x00020000UL /* Objects are reclaimable */
>
> #define kzalloc_node(size, flags, node) kmalloc(size, flags)
> +enum _slab_flag_bits {
> + _SLAB_KMALLOC,
> + _SLAB_HWCACHE_ALIGN,
> + _SLAB_PANIC,
> + _SLAB_TYPESAFE_BY_RCU,
> + _SLAB_ACCOUNT,
> + _SLAB_FLAGS_LAST_BIT
> +};
> +
> +#define __SLAB_FLAG_BIT(nr) ((unsigned int __force)(1U << (nr)))
> +#define __SLAB_FLAG_UNUSED ((unsigned int __force)(0U))
> +
> +#define SLAB_HWCACHE_ALIGN __SLAB_FLAG_BIT(_SLAB_HWCACHE_ALIGN)
> +#define SLAB_PANIC __SLAB_FLAG_BIT(_SLAB_PANIC)
> +#define SLAB_TYPESAFE_BY_RCU __SLAB_FLAG_BIT(_SLAB_TYPESAFE_BY_RCU)
> +#ifdef CONFIG_MEMCG
> +# define SLAB_ACCOUNT __SLAB_FLAG_BIT(_SLAB_ACCOUNT)
> +#else
> +# define SLAB_ACCOUNT __SLAB_FLAG_UNUSED
> +#endif
>
> void *kmalloc(size_t size, gfp_t gfp);
> void kfree(void *p);
> @@ -23,6 +43,86 @@ enum slab_state {
> FULL
> };
>
> +struct kmem_cache {
> + pthread_mutex_t lock;
> + unsigned int size;
> + unsigned int align;
> + unsigned int sheaf_capacity;
> + int nr_objs;
> + void *objs;
> + void (*ctor)(void *);
> + bool non_kernel_enabled;
> + unsigned int non_kernel;
> + unsigned long nr_allocated;
> + unsigned long nr_tallocated;
> + bool exec_callback;
> + void (*callback)(void *);
> + void *private;
> +};
> +
> +struct kmem_cache_args {
> + /**
> + * @align: The required alignment for the objects.
> + *
> + * %0 means no specific alignment is requested.
> + */
> + unsigned int align;
> + /**
> + * @sheaf_capacity: The maximum size of the sheaf.
> + */
> + unsigned int sheaf_capacity;
> + /**
> + * @useroffset: Usercopy region offset.
> + *
> + * %0 is a valid offset, when @usersize is non-%0
> + */
> + unsigned int useroffset;
> + /**
> + * @usersize: Usercopy region size.
> + *
> + * %0 means no usercopy region is specified.
> + */
> + unsigned int usersize;
> + /**
> + * @freeptr_offset: Custom offset for the free pointer
> + * in &SLAB_TYPESAFE_BY_RCU caches
> + *
> + * By default &SLAB_TYPESAFE_BY_RCU caches place the free pointer
> + * outside of the object. This might cause the object to grow in size.
> + * Cache creators that have a reason to avoid this can specify a custom
> + * free pointer offset in their struct where the free pointer will be
> + * placed.
> + *
> + * Note that placing the free pointer inside the object requires the
> + * caller to ensure that no fields are invalidated that are required to
> + * guard against object recycling (See &SLAB_TYPESAFE_BY_RCU for
> + * details).
> + *
> + * Using %0 as a value for @freeptr_offset is valid. If @freeptr_offset
> + * is specified, %use_freeptr_offset must be set %true.
> + *
> + * Note that @ctor currently isn't supported with custom free pointers
> + * as a @ctor requires an external free pointer.
> + */
> + unsigned int freeptr_offset;
> + /**
> + * @use_freeptr_offset: Whether a @freeptr_offset is used.
> + */
> + bool use_freeptr_offset;
> + /**
> + * @ctor: A constructor for the objects.
> + *
> + * The constructor is invoked for each object in a newly allocated slab
> + * page. It is the cache user's responsibility to free object in the
> + * same state as after calling the constructor, or deal appropriately
> + * with any differences between a freshly constructed and a reallocated
> + * object.
> + *
> + * %NULL means no constructor.
> + */
> + void (*ctor)(void *);
> +};
> +
> static inline void *kzalloc(size_t size, gfp_t gfp)
> {
> return kmalloc(size, gfp | __GFP_ZERO);
> @@ -37,9 +137,38 @@ static inline void *kmem_cache_alloc(struct kmem_cache *cachep, int flags)
> }
> void kmem_cache_free(struct kmem_cache *cachep, void *objp);
>
> -struct kmem_cache *kmem_cache_create(const char *name, unsigned int size,
> - unsigned int align, unsigned int flags,
> - void (*ctor)(void *));
> +
> +struct kmem_cache *
> +__kmem_cache_create_args(const char *name, unsigned int size,
> + struct kmem_cache_args *args, unsigned int flags);
> +
> +/* If NULL is passed for @args, use this variant with default arguments. */
> +static inline struct kmem_cache *
> +__kmem_cache_default_args(const char *name, unsigned int size,
> + struct kmem_cache_args *args, unsigned int flags)
> +{
> + struct kmem_cache_args kmem_default_args = {};
> +
> + return __kmem_cache_create_args(name, size, &kmem_default_args, flags);
> +}
> +
> +static inline struct kmem_cache *
> +__kmem_cache_create(const char *name, unsigned int size, unsigned int align,
> + unsigned int flags, void (*ctor)(void *))
> +{
> + struct kmem_cache_args kmem_args = {
> + .align = align,
> + .ctor = ctor,
> + };
> +
> + return __kmem_cache_create_args(name, size, &kmem_args, flags);
> +}
> +
> +#define kmem_cache_create(__name, __object_size, __args, ...) \
> + _Generic((__args), \
> + struct kmem_cache_args *: __kmem_cache_create_args, \
> + void *: __kmem_cache_default_args, \
> + default: __kmem_cache_create)(__name, __object_size, __args, __VA_ARGS__)
>
> void kmem_cache_free_bulk(struct kmem_cache *cachep, size_t size, void **list);
> int kmem_cache_alloc_bulk(struct kmem_cache *cachep, gfp_t gfp, size_t size,
> diff --git a/tools/testing/shared/linux.c b/tools/testing/shared/linux.c
> index 0f97fb0d19e19c327aa4843a35b45cc086f4f366..97b8412ccbb6d222604c7b397c53c65618d8d51b 100644
> --- a/tools/testing/shared/linux.c
> +++ b/tools/testing/shared/linux.c
> @@ -16,21 +16,6 @@ int nr_allocated;
> int preempt_count;
> int test_verbose;
>
> -struct kmem_cache {
> - pthread_mutex_t lock;
> - unsigned int size;
> - unsigned int align;
> - int nr_objs;
> - void *objs;
> - void (*ctor)(void *);
> - unsigned int non_kernel;
> - unsigned long nr_allocated;
> - unsigned long nr_tallocated;
> - bool exec_callback;
> - void (*callback)(void *);
> - void *private;
> -};
> -
> void kmem_cache_set_callback(struct kmem_cache *cachep, void (*callback)(void *))
> {
> cachep->callback = callback;
> @@ -234,23 +219,26 @@ int kmem_cache_alloc_bulk(struct kmem_cache *cachep, gfp_t gfp, size_t size,
> }
>
> struct kmem_cache *
> -kmem_cache_create(const char *name, unsigned int size, unsigned int align,
> - unsigned int flags, void (*ctor)(void *))
> +__kmem_cache_create_args(const char *name, unsigned int size,
> + struct kmem_cache_args *args,
> + unsigned int flags)
> {
> struct kmem_cache *ret = malloc(sizeof(*ret));
>
> pthread_mutex_init(&ret->lock, NULL);
> ret->size = size;
> - ret->align = align;
> + ret->align = args->align;
> + ret->sheaf_capacity = args->sheaf_capacity;
> ret->nr_objs = 0;
> ret->nr_allocated = 0;
> ret->nr_tallocated = 0;
> ret->objs = NULL;
> - ret->ctor = ctor;
> + ret->ctor = args->ctor;
> ret->non_kernel = 0;
> ret->exec_callback = false;
> ret->callback = NULL;
> ret->private = NULL;
> +
> return ret;
> }
>
> diff --git a/tools/testing/shared/maple-shim.c b/tools/testing/shared/maple-shim.c
> index 640df76f483e09f3b6f85612786060dd273e2362..9d7b743415660305416e972fa75b56824211b0eb 100644
> --- a/tools/testing/shared/maple-shim.c
> +++ b/tools/testing/shared/maple-shim.c
> @@ -3,5 +3,6 @@
> /* Very simple shim around the maple tree. */
>
> #include "maple-shared.h"
> +#include <linux/slab.h>
>
> #include "../../../lib/maple_tree.c"
> diff --git a/tools/testing/vma/vma_internal.h b/tools/testing/vma/vma_internal.h
> index 6b6e2b05918c9f95b537f26e20a943b34082825a..d5b87fa6a133f6d676488de2538c509e0f0e1d54 100644
> --- a/tools/testing/vma/vma_internal.h
> +++ b/tools/testing/vma/vma_internal.h
> @@ -26,6 +26,7 @@
> #include <linux/mm.h>
> #include <linux/rbtree.h>
> #include <linux/refcount.h>
> +#include <linux/slab.h>
>
> extern unsigned long stack_guard_gap;
> #ifdef CONFIG_MMU
> @@ -509,65 +510,6 @@ struct pagetable_move_control {
> .len_in = len_, \
> }
>
> -struct kmem_cache_args {
> - /**
> - * @align: The required alignment for the objects.
> - *
> - * %0 means no specific alignment is requested.
> - */
> - unsigned int align;
> - /**
> - * @useroffset: Usercopy region offset.
> - *
> - * %0 is a valid offset, when @usersize is non-%0
> - */
> - unsigned int useroffset;
> - /**
> - * @usersize: Usercopy region size.
> - *
> - * %0 means no usercopy region is specified.
> - */
> - unsigned int usersize;
> - /**
> - * @freeptr_offset: Custom offset for the free pointer
> - * in &SLAB_TYPESAFE_BY_RCU caches
> - *
> - * By default &SLAB_TYPESAFE_BY_RCU caches place the free pointer
> - * outside of the object. This might cause the object to grow in size.
> - * Cache creators that have a reason to avoid this can specify a custom
> - * free pointer offset in their struct where the free pointer will be
> - * placed.
> - *
> - * Note that placing the free pointer inside the object requires the
> - * caller to ensure that no fields are invalidated that are required to
> - * guard against object recycling (See &SLAB_TYPESAFE_BY_RCU for
> - * details).
> - *
> - * Using %0 as a value for @freeptr_offset is valid. If @freeptr_offset
> - * is specified, %use_freeptr_offset must be set %true.
> - *
> - * Note that @ctor currently isn't supported with custom free pointers
> - * as a @ctor requires an external free pointer.
> - */
> - unsigned int freeptr_offset;
> - /**
> - * @use_freeptr_offset: Whether a @freeptr_offset is used.
> - */
> - bool use_freeptr_offset;
> - /**
> - * @ctor: A constructor for the objects.
> - *
> - * The constructor is invoked for each object in a newly allocated slab
> - * page. It is the cache user's responsibility to free object in the
> - * same state as after calling the constructor, or deal appropriately
> - * with any differences between a freshly constructed and a reallocated
> - * object.
> - *
> - * %NULL means no constructor.
> - */
> - void (*ctor)(void *);
> -};
> -
> static inline void vma_iter_invalidate(struct vma_iterator *vmi)
> {
> mas_pause(&vmi->mas);
> @@ -652,38 +594,6 @@ static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm)
> vma->vm_lock_seq = UINT_MAX;
> }
>
> -struct kmem_cache {
> - const char *name;
> - size_t object_size;
> - struct kmem_cache_args *args;
> -};
> -
> -static inline struct kmem_cache *__kmem_cache_create(const char *name,
> - size_t object_size,
> - struct kmem_cache_args *args)
> -{
> - struct kmem_cache *ret = malloc(sizeof(struct kmem_cache));
> -
> - ret->name = name;
> - ret->object_size = object_size;
> - ret->args = args;
> -
> - return ret;
> -}
> -
> -#define kmem_cache_create(__name, __object_size, __args, ...) \
> - __kmem_cache_create((__name), (__object_size), (__args))
> -
> -static inline void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
> -{
> - return calloc(1, s->object_size);
> -}
> -
> -static inline void kmem_cache_free(struct kmem_cache *s, void *x)
> -{
> - free(x);
> -}
> -
> /*
> * These are defined in vma.h, but sadly vm_stat_account() is referenced by
> * kernel/fork.c, so we have to these broadly available there, and temporarily
>
> --
> 2.51.0
>
next prev parent reply other threads:[~2025-09-26 23:28 UTC|newest]
Thread overview: 95+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-10 8:01 [PATCH v8 00/23] SLUB percpu sheaves Vlastimil Babka
2025-09-10 8:01 ` [PATCH v8 01/23] locking/local_lock: Expose dep_map in local_trylock_t Vlastimil Babka
2025-09-24 16:49 ` Suren Baghdasaryan
2025-09-10 8:01 ` [PATCH v8 02/23] slab: simplify init_kmem_cache_nodes() error handling Vlastimil Babka
2025-09-24 16:52 ` Suren Baghdasaryan
2025-09-10 8:01 ` [PATCH v8 03/23] slab: add opt-in caching layer of percpu sheaves Vlastimil Babka
2025-12-02 8:48 ` [PATCH] slub: add barn_get_full_sheaf() and refine empty-main sheaf Hao Li
2025-12-02 8:55 ` Hao Li
2025-12-02 9:00 ` slub: add barn_get_full_sheaf() and refine empty-main sheaf replacement Hao Li
2025-12-03 5:46 ` Harry Yoo
2025-12-03 11:15 ` Hao Li
2025-09-10 8:01 ` [PATCH v8 04/23] slab: add sheaf support for batching kfree_rcu() operations Vlastimil Babka
2025-09-12 0:38 ` Sergey Senozhatsky
2025-09-12 7:03 ` Vlastimil Babka
2025-09-17 8:30 ` Harry Yoo
2025-09-17 9:55 ` Vlastimil Babka
2025-09-17 11:32 ` Harry Yoo
2025-09-17 12:05 ` Vlastimil Babka
2025-09-17 13:07 ` Harry Yoo
2025-09-17 13:21 ` Vlastimil Babka
2025-09-17 13:34 ` Harry Yoo
2025-09-17 14:14 ` Vlastimil Babka
2025-09-18 8:09 ` Vlastimil Babka
2025-09-19 6:47 ` Harry Yoo
2025-09-19 7:02 ` Vlastimil Babka
2025-09-19 8:59 ` Harry Yoo
2025-09-25 4:35 ` Suren Baghdasaryan
2025-09-25 8:52 ` Harry Yoo
2025-09-25 13:38 ` Suren Baghdasaryan
2025-09-26 10:08 ` Vlastimil Babka
2025-09-26 15:41 ` Suren Baghdasaryan
2025-09-17 11:36 ` Paul E. McKenney
2025-09-17 12:13 ` Vlastimil Babka
2025-10-31 21:32 ` Daniel Gomez
2025-11-03 3:17 ` Harry Yoo
2025-11-05 11:25 ` Vlastimil Babka
2025-11-27 14:00 ` Daniel Gomez
2025-11-27 19:29 ` Suren Baghdasaryan
2025-11-28 11:37 ` [PATCH V1] mm/slab: introduce kvfree_rcu_barrier_on_cache() for cache destruction Harry Yoo
2025-11-28 12:22 ` Harry Yoo
2025-11-28 12:38 ` Daniel Gomez
2025-12-02 9:29 ` Jon Hunter
2025-12-02 10:18 ` Harry Yoo
2025-11-27 11:38 ` [PATCH v8 04/23] slab: add sheaf support for batching kfree_rcu() operations Jon Hunter
2025-11-27 11:50 ` Jon Hunter
2025-11-27 12:33 ` Harry Yoo
2025-11-27 12:48 ` Harry Yoo
2025-11-28 8:57 ` Jon Hunter
2025-12-01 6:55 ` Harry Yoo
2025-11-27 13:18 ` Vlastimil Babka
2025-11-28 8:59 ` Jon Hunter
2025-09-10 8:01 ` [PATCH v8 05/23] slab: sheaf prefilling for guaranteed allocations Vlastimil Babka
2025-09-10 8:01 ` [PATCH v8 06/23] slab: determine barn status racily outside of lock Vlastimil Babka
2025-09-10 8:01 ` [PATCH v8 07/23] slab: skip percpu sheaves for remote object freeing Vlastimil Babka
2025-09-25 16:14 ` Suren Baghdasaryan
2025-09-10 8:01 ` [PATCH v8 08/23] slab: allow NUMA restricted allocations to use percpu sheaves Vlastimil Babka
2025-09-25 16:27 ` Suren Baghdasaryan
2025-09-10 8:01 ` [PATCH v8 09/23] maple_tree: remove redundant __GFP_NOWARN Vlastimil Babka
2025-09-10 8:01 ` [PATCH v8 10/23] tools/testing/vma: clean up stubs in vma_internal.h Vlastimil Babka
2025-09-10 8:01 ` [PATCH v8 11/23] maple_tree: Drop bulk insert support Vlastimil Babka
2025-09-25 16:38 ` Suren Baghdasaryan
2025-09-10 8:01 ` [PATCH v8 12/23] tools/testing/vma: Implement vm_refcnt reset Vlastimil Babka
2025-09-25 16:38 ` Suren Baghdasaryan
2025-09-10 8:01 ` [PATCH v8 13/23] tools/testing: Add support for changes to slab for sheaves Vlastimil Babka
2025-09-26 23:28 ` Suren Baghdasaryan [this message]
2025-09-10 8:01 ` [PATCH v8 14/23] mm, vma: use percpu sheaves for vm_area_struct cache Vlastimil Babka
2025-09-10 8:01 ` [PATCH v8 15/23] maple_tree: use percpu sheaves for maple_node_cache Vlastimil Babka
2025-09-12 2:20 ` Liam R. Howlett
2025-10-16 15:16 ` D, Suneeth
2025-10-16 16:15 ` Vlastimil Babka
2025-10-17 18:26 ` D, Suneeth
2025-09-10 8:01 ` [PATCH v8 16/23] tools/testing: include maple-shim.c in maple.c Vlastimil Babka
2025-09-26 23:45 ` Suren Baghdasaryan
2025-09-10 8:01 ` [PATCH v8 17/23] testing/radix-tree/maple: Hack around kfree_rcu not existing Vlastimil Babka
2025-09-26 23:53 ` Suren Baghdasaryan
2025-09-10 8:01 ` [PATCH v8 18/23] maple_tree: Use kfree_rcu in ma_free_rcu Vlastimil Babka
2025-09-17 11:46 ` Harry Yoo
2025-09-27 0:05 ` Suren Baghdasaryan
2025-09-10 8:01 ` [PATCH v8 19/23] maple_tree: Replace mt_free_one() with kfree() Vlastimil Babka
2025-09-27 0:06 ` Suren Baghdasaryan
2025-09-10 8:01 ` [PATCH v8 20/23] tools/testing: Add support for prefilled slab sheafs Vlastimil Babka
2025-09-27 0:28 ` Suren Baghdasaryan
2025-09-10 8:01 ` [PATCH v8 21/23] maple_tree: Prefilled sheaf conversion and testing Vlastimil Babka
2025-09-27 1:08 ` Suren Baghdasaryan
2025-09-29 7:30 ` Vlastimil Babka
2025-09-29 16:51 ` Liam R. Howlett
2025-09-10 8:01 ` [PATCH v8 22/23] maple_tree: Add single node allocation support to maple state Vlastimil Babka
2025-09-27 1:17 ` Suren Baghdasaryan
2025-09-29 7:39 ` Vlastimil Babka
2025-09-10 8:01 ` [PATCH v8 23/23] maple_tree: Convert forking to use the sheaf interface Vlastimil Babka
2025-10-07 6:34 ` [PATCH v8 00/23] SLUB percpu sheaves Christoph Hellwig
2025-10-07 8:03 ` Vlastimil Babka
2025-10-08 6:04 ` Christoph Hellwig
2025-10-15 8:32 ` Vlastimil Babka
2025-10-22 6:47 ` Christoph Hellwig
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAJuCfpGYgOVG7hpZJ7MED_ALXZfKgUuRcPN3YYjx6k_t-dGXhg@mail.gmail.com \
--to=surenb@google.com \
--cc=Liam.Howlett@oracle.com \
--cc=cl@gentwo.org \
--cc=harry.yoo@oracle.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=maple-tree@lists.infradead.org \
--cc=rcu@vger.kernel.org \
--cc=rientjes@google.com \
--cc=roman.gushchin@linux.dev \
--cc=sidhartha.kumar@oracle.com \
--cc=urezki@gmail.com \
--cc=vbabka@suse.cz \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox