From nobody Tue Feb 10 02:47:55 2026 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2200B241C90 for ; Mon, 17 Mar 2025 14:33:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742222036; cv=none; b=pXr+EZdeQS5o/ijo9r/bLmYb6Q2bEUyTIbcBTUWPgWE8y9LsAoKYZPaHFa6Dw+GyLQpAyxgw8iq+P9s18CF8VMeyIzAgnBqeHV95T7eGJLXI2vbm/iJNmI4JRkmP6jloy20+/gs4xxcbcpJr/O29lmXKGZ46upP12iVOsafcLLM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742222036; c=relaxed/simple; bh=qUa6SQ3/K8ohFfOKNMBJrs7GgsuyLmWV+TxdT8k3KnU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Xr+T72x42Z+LX+1W7uRiFm5xCUoPqVE9kzdKinEe9E9+HxNu6qkXRla1xAsQ4YSaWcLyZSiwycmARuOU9ZgseuaDdo4WgH3lKY+Vq4SVptHSYIYSFnF0paaSfftl0rcsl49eh2nULsmeGrt9CoynaDCJe9fav80EtycuBmH9v2Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz; spf=pass smtp.mailfrom=suse.cz; arc=none smtp.client-ip=195.135.223.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.cz Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 233BA1F823; Mon, 17 Mar 2025 14:33:34 +0000 (UTC) Authentication-Results: smtp-out2.suse.de; none Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 0ED5313A2C; Mon, 17 Mar 2025 14:33:34 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id qPFTA74y2GcycQAAD6G6ig (envelope-from ); Mon, 17 Mar 2025 14:33:34 +0000 From: Vlastimil Babka Date: Mon, 17 Mar 2025 15:33:07 +0100 Subject: [PATCH RFC v3 6/8] tools: Add testing support for changes to rcu and slab for sheaves Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250317-slub-percpu-caches-v3-6-9d9884d8b643@suse.cz> References: <20250317-slub-percpu-caches-v3-0-9d9884d8b643@suse.cz> In-Reply-To: <20250317-slub-percpu-caches-v3-0-9d9884d8b643@suse.cz> To: Suren Baghdasaryan , "Liam R. Howlett" , Christoph Lameter , David Rientjes Cc: Roman Gushchin , Harry Yoo , Uladzislau Rezki , linux-mm@kvack.org, linux-kernel@vger.kernel.org, rcu@vger.kernel.org, maple-tree@lists.infradead.org, vbabka@suse.cz, "Liam R. Howlett" X-Mailer: b4 0.14.2 X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Spam-Level: X-Spamd-Result: default: False [-4.00 / 50.00]; REPLY(-4.00)[] X-Spam-Score: -4.00 X-Spam-Flag: NO X-Rspamd-Queue-Id: 233BA1F823 X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Rspamd-Action: no action X-Rspamd-Server: rspamd2.dmz-prg2.suse.org From: "Liam R. Howlett" Make testing work for the slab and rcu changes that have come in with the sheaves work. This only works with one kmem_cache, and only the first one used. Subsequent setting of kmem_cache will not update the active kmem_cache and will be silently dropped because there are other tests which happen after the kmem_cache of interest is set. The saved active kmem_cache is used in the rcu callback, which passes the object to be freed. The rcu call takes the rcu_head, which is passed in as the field in the struct (in this case rcu in the maple tree node), which is calculated by pointer math. The offset of which is saved (in a global variable) for restoring the node pointer on the callback after the rcu grace period expires. Don't use any of this outside of testing, please. Signed-off-by: Liam R. Howlett --- tools/include/linux/slab.h | 41 +++++++++++++++++++++++++++++++= +--- tools/testing/shared/linux.c | 24 ++++++++++++++++---- tools/testing/shared/linux/rcupdate.h | 22 +++++++++++++++++++ 3 files changed, 80 insertions(+), 7 deletions(-) diff --git a/tools/include/linux/slab.h b/tools/include/linux/slab.h index 51b25e9c4ec7b66bdf4c68cc1353c6faf1ca7bb8..a475364cfd9fcdb10db252aab18= ea3a620326b6b 100644 --- a/tools/include/linux/slab.h +++ b/tools/include/linux/slab.h @@ -22,6 +22,12 @@ enum slab_state { FULL }; =20 +struct kmem_cache_args { + unsigned int align; + unsigned int sheaf_capacity; + void (*ctor)(void *); +}; + static inline void *kzalloc(size_t size, gfp_t gfp) { return kmalloc(size, gfp | __GFP_ZERO); @@ -36,9 +42,38 @@ static inline void *kmem_cache_alloc(struct kmem_cache *= cachep, int flags) } void kmem_cache_free(struct kmem_cache *cachep, void *objp); =20 -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 =3D {}; + + 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 alig= n, + unsigned int flags, void (*ctor)(void *)) +{ + struct kmem_cache_args kmem_args =3D { + .align =3D align, + .ctor =3D 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__) =20 void kmem_cache_free_bulk(struct kmem_cache *cachep, size_t size, void **l= ist); int kmem_cache_alloc_bulk(struct kmem_cache *cachep, gfp_t gfp, size_t siz= e, diff --git a/tools/testing/shared/linux.c b/tools/testing/shared/linux.c index 66dbb362385f3c3d923233448cc591adfe6dc9e7..9f5fd722f27f1d3877be8927be3= 0409cd74ab3c3 100644 --- a/tools/testing/shared/linux.c +++ b/tools/testing/shared/linux.c @@ -20,6 +20,7 @@ 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 *); @@ -31,6 +32,8 @@ struct kmem_cache { void *private; }; =20 +static struct kmem_cache *kmem_active =3D NULL; + void kmem_cache_set_callback(struct kmem_cache *cachep, void (*callback)(v= oid *)) { cachep->callback =3D callback; @@ -147,6 +150,14 @@ void kmem_cache_free(struct kmem_cache *cachep, void *= objp) pthread_mutex_unlock(&cachep->lock); } =20 +void kmem_cache_free_active(void *objp) +{ + if (!kmem_active) + printf("WARNING: No active kmem_cache\n"); + + kmem_cache_free(kmem_active, objp); +} + void kmem_cache_free_bulk(struct kmem_cache *cachep, size_t size, void **l= ist) { if (kmalloc_verbose) @@ -234,23 +245,28 @@ int kmem_cache_alloc_bulk(struct kmem_cache *cachep, = gfp_t gfp, size_t size, } =20 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 =3D malloc(sizeof(*ret)); =20 pthread_mutex_init(&ret->lock, NULL); ret->size =3D size; - ret->align =3D align; + ret->align =3D args->align; + ret->sheaf_capacity =3D args->sheaf_capacity; ret->nr_objs =3D 0; ret->nr_allocated =3D 0; ret->nr_tallocated =3D 0; ret->objs =3D NULL; - ret->ctor =3D ctor; + ret->ctor =3D args->ctor; ret->non_kernel =3D 0; ret->exec_callback =3D false; ret->callback =3D NULL; ret->private =3D NULL; + if (!kmem_active) + kmem_active =3D ret; + return ret; } =20 diff --git a/tools/testing/shared/linux/rcupdate.h b/tools/testing/shared/l= inux/rcupdate.h index fed468fb0c78db6f33fb1900c7110ab5f3c19c65..c95e2f0bbd93798e544d7d34e08= 23ed68414f924 100644 --- a/tools/testing/shared/linux/rcupdate.h +++ b/tools/testing/shared/linux/rcupdate.h @@ -9,4 +9,26 @@ #define rcu_dereference_check(p, cond) rcu_dereference(p) #define RCU_INIT_POINTER(p, v) do { (p) =3D (v); } while (0) =20 +void kmem_cache_free_active(void *objp); +static unsigned long kfree_cb_offset =3D 0; + +static inline void kfree_rcu_cb(struct rcu_head *head) +{ + void *objp =3D (void *) ((unsigned long)head - kfree_cb_offset); + + kmem_cache_free_active(objp); +} + +#ifndef offsetof +#define offsetof(TYPE, MEMBER) __builtin_offsetof(TYPE, MEMBER) +#endif + +#define kfree_rcu(ptr, rhv) \ +do { \ + if (!kfree_cb_offset) \ + kfree_cb_offset =3D offsetof(typeof(*(ptr)), rhv); \ + \ + call_rcu(&ptr->rhv, kfree_rcu_cb); \ +} while (0) + #endif --=20 2.48.1