From patchwork Tue Sep 3 14:20:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 13788784 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7DC85CD343B for ; Tue, 3 Sep 2024 14:22:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0BD398D0178; Tue, 3 Sep 2024 10:22:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 01CA38D0151; Tue, 3 Sep 2024 10:22:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E00778D0178; Tue, 3 Sep 2024 10:22:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id B4CCA8D0151 for ; Tue, 3 Sep 2024 10:22:30 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 72C96A8254 for ; Tue, 3 Sep 2024 14:22:30 +0000 (UTC) X-FDA: 82523642460.15.E416B32 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf06.hostedemail.com (Postfix) with ESMTP id 9AEB5180006 for ; Tue, 3 Sep 2024 14:22:28 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=iQJr2KSY; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf06.hostedemail.com: domain of brauner@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=brauner@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725373278; a=rsa-sha256; cv=none; b=DR2pw+8m4dz51TcRN5AU2C9xsb6/yQo+kUPsNe12266keym/RcE42zzYNrITPYqm2o7RHt aM7ta783PTumdu6zYLRP8HNtbLUwk0OTw76v+8EkMscHoGcVcUp80mStZysxFTPRHsldMt TWQuYHPuE/SGOVEw9xRD2vvU1Ej6SrE= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=iQJr2KSY; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf06.hostedemail.com: domain of brauner@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=brauner@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725373278; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=p8tzsaUFpSg3rt58dz7tkuciGP9etf4F0l6ymHLZ59o=; b=rDcc5LXFSj04hZbwKBXEl9nZCmgXuwjpEfJNDOvJ1gTBIzHPua3MSYxdDIFqE8adPcgHOS ZzIxw2DEsY01YVtrQ8dhs29qxxzALzqIbFTpTrdGwmpwF8mJEqrgYGEkANWme+mRHWUcll lynG0TNIa3RL2g5SpEvnOzxb7uV872w= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id BE5BBA43662; Tue, 3 Sep 2024 14:22:20 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D032DC4CEC7; Tue, 3 Sep 2024 14:22:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725373347; bh=2h5DOrmEliYOEskMyLdOlS+a422lNEm7iLYnRuCn078=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=iQJr2KSYx/rknS/FfpX+JxiEROKLTM5PnAYZ+o7XEVv6NgP3Ss7tjJhNVLJDjAYmg V1w3/5f+g0CdyNTOTYOk+MyMT016PpeTQSQ9CVWlQjEmYiiidRhd5oBc/sSsG1zBIa e2QfgbzfX02r9yFo7QRX2G58gqj7RaPb+Tur00aybamWoo1hRqz8NyWQLbQXnL5bTq 9TJ8oEHrOK+F3C/N/OCCnqLEonwIvdHgPRKO+9swiWGr5ikMlWxa7lOaY4WvE2YHLg NAXHIZBEH3+euJk3IsDszxZJDMupI0XClXyaDEV4YW6f5o0YzZ0GNF3IuO1vHXJZhy CdnYHOYX2nb8w== From: Christian Brauner Date: Tue, 03 Sep 2024 16:20:43 +0200 Subject: [PATCH v2 02/15] slab: add struct kmem_cache_args MIME-Version: 1.0 Message-Id: <20240903-work-kmem_cache_args-v2-2-76f97e9a4560@kernel.org> References: <20240903-work-kmem_cache_args-v2-0-76f97e9a4560@kernel.org> In-Reply-To: <20240903-work-kmem_cache_args-v2-0-76f97e9a4560@kernel.org> To: Vlastimil Babka , Jens Axboe , Jann Horn , Linus Torvalds , Mike Rapoport Cc: linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Christian Brauner X-Mailer: b4 0.15-dev-37811 X-Developer-Signature: v=1; a=openpgp-sha256; l=5053; i=brauner@kernel.org; h=from:subject:message-id; bh=2h5DOrmEliYOEskMyLdOlS+a422lNEm7iLYnRuCn078=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMaRdl559e5Opz1zVj/eFzHY8dLq47YuR2uY7dlX5wfVT8 wO/B7+80lHKwiDGxSArpsji0G4SLrecp2KzUaYGzBxWJpAhDFycAjCRgrmMDJvP/rf4f7iw78yF f8xdB6L/NnxZxs9957/CyR9zXj9dJerAyDC/eIqDhMKq5ZGlvj+2LF9+QOFBocTslZo74hdGpq9 /+YcBAA== X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 X-Rspamd-Queue-Id: 9AEB5180006 X-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yo3kE KAoX5Kxv 0NHcOfu9ggEwptVFmmdW9acHuNBHWjBqv6+UUHkJP+ep/zvn4nMTvYTCGZKvbwnDZEB9dcw7n/1wAbIceqcc0pPg9qyhT4gw9XhiOZMWFW8WLq2ZMlQEDnHHw0jVjn1oMKKcQZuUJJkKOIbMQnq+lsOaBL4gurEMyPWk5ijwOXjMyqWGeoUC8T+S+Q3s9bwXO8XPD6VaNQPp37OI/sRJd4jtAWg95ZfADqu3jYgUzNUaltbypYnjRtX0gt8VTyFaV43CKkbMskRasOLgqkG86/qm3mrjmrFmaPhA9 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Signed-off-by: Christian Brauner Reviewed-by: Mike Rapoport (Microsoft) --- include/linux/slab.h | 21 ++++++++++++++++ mm/slab_common.c | 67 +++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 72 insertions(+), 16 deletions(-) diff --git a/include/linux/slab.h b/include/linux/slab.h index 5b2da2cf31a8..79d8c8bca4a4 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -240,6 +240,27 @@ struct mem_cgroup; */ bool slab_is_available(void); +/** + * @align: The required alignment for the objects. + * @useroffset: Usercopy region offset + * @usersize: Usercopy region size + * @freeptr_offset: Custom offset for the free pointer in RCU caches + * @use_freeptr_offset: Whether a @freeptr_offset is used + * @ctor: A constructor for the objects. + */ +struct kmem_cache_args { + unsigned int align; + unsigned int useroffset; + unsigned int usersize; + unsigned int freeptr_offset; + bool use_freeptr_offset; + void (*ctor)(void *); +}; + +struct kmem_cache *__kmem_cache_create_args(const char *name, + unsigned int object_size, + struct kmem_cache_args *args, + slab_flags_t flags); struct kmem_cache *kmem_cache_create(const char *name, unsigned int size, unsigned int align, slab_flags_t flags, void (*ctor)(void *)); diff --git a/mm/slab_common.c b/mm/slab_common.c index 91e0e36e4379..0f13c045b8d1 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -248,14 +248,24 @@ static struct kmem_cache *create_cache(const char *name, return ERR_PTR(err); } -static struct kmem_cache * -do_kmem_cache_create_usercopy(const char *name, - unsigned int size, unsigned int freeptr_offset, - unsigned int align, slab_flags_t flags, - unsigned int useroffset, unsigned int usersize, - void (*ctor)(void *)) +/** + * __kmem_cache_create_args - Create a kmem cache + * @name: A string which is used in /proc/slabinfo to identify this cache. + * @object_size: The size of objects to be created in this cache. + * @args: Arguments for the cache creation (see struct kmem_cache_args). + * @flags: See %SLAB_* flags for an explanation of individual @flags. + * + * Cannot be called within a interrupt, but can be interrupted. + * + * Return: a pointer to the cache on success, NULL on failure. + */ +struct kmem_cache *__kmem_cache_create_args(const char *name, + unsigned int object_size, + struct kmem_cache_args *args, + slab_flags_t flags) { struct kmem_cache *s = NULL; + unsigned int freeptr_offset = UINT_MAX; const char *cache_name; int err; @@ -275,7 +285,7 @@ do_kmem_cache_create_usercopy(const char *name, mutex_lock(&slab_mutex); - err = kmem_cache_sanity_check(name, size); + err = kmem_cache_sanity_check(name, object_size); if (err) { goto out_unlock; } @@ -296,12 +306,14 @@ do_kmem_cache_create_usercopy(const char *name, /* Fail closed on bad usersize of useroffset values. */ if (!IS_ENABLED(CONFIG_HARDENED_USERCOPY) || - WARN_ON(!usersize && useroffset) || - WARN_ON(size < usersize || size - usersize < useroffset)) - usersize = useroffset = 0; - - if (!usersize) - s = __kmem_cache_alias(name, size, align, flags, ctor); + WARN_ON(!args->usersize && args->useroffset) || + WARN_ON(object_size < args->usersize || + object_size - args->usersize < args->useroffset)) + args->usersize = args->useroffset = 0; + + if (!args->usersize) + s = __kmem_cache_alias(name, object_size, args->align, flags, + args->ctor); if (s) goto out_unlock; @@ -311,9 +323,11 @@ do_kmem_cache_create_usercopy(const char *name, goto out_unlock; } - s = create_cache(cache_name, size, freeptr_offset, - calculate_alignment(flags, align, size), - flags, useroffset, usersize, ctor); + if (args->use_freeptr_offset) + freeptr_offset = args->freeptr_offset; + s = create_cache(cache_name, object_size, freeptr_offset, + calculate_alignment(flags, args->align, object_size), + flags, args->useroffset, args->usersize, args->ctor); if (IS_ERR(s)) { err = PTR_ERR(s); kfree_const(cache_name); @@ -335,6 +349,27 @@ do_kmem_cache_create_usercopy(const char *name, } return s; } +EXPORT_SYMBOL(__kmem_cache_create_args); + +static struct kmem_cache * +do_kmem_cache_create_usercopy(const char *name, + unsigned int size, unsigned int freeptr_offset, + unsigned int align, slab_flags_t flags, + unsigned int useroffset, unsigned int usersize, + void (*ctor)(void *)) +{ + struct kmem_cache_args kmem_args = { + .align = align, + .use_freeptr_offset = freeptr_offset != UINT_MAX, + .freeptr_offset = freeptr_offset, + .useroffset = useroffset, + .usersize = usersize, + .ctor = ctor, + }; + + return __kmem_cache_create_args(name, size, &kmem_args, flags); +} + /** * kmem_cache_create_usercopy - Create a cache with a region suitable