From patchwork Thu Sep 5 07:56:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 13791892 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 CB1B4CD4F5E for ; Thu, 5 Sep 2024 07:57:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 542756B00FE; Thu, 5 Sep 2024 03:57:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4F0CD6B0105; Thu, 5 Sep 2024 03:57:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 343DC6B00FF; Thu, 5 Sep 2024 03:57:54 -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 1466E6B00F3 for ; Thu, 5 Sep 2024 03:57:54 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id C797581A2E for ; Thu, 5 Sep 2024 07:57:53 +0000 (UTC) X-FDA: 82529930826.14.8FD85F5 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf19.hostedemail.com (Postfix) with ESMTP id 135E81A000B for ; Thu, 5 Sep 2024 07:57:51 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=I7HvquUc; spf=pass (imf19.hostedemail.com: domain of brauner@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=brauner@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725522974; 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=eMA77wqaMeZKMKqnOy472wO3XiINXeFs1N0hPm98s/g=; b=nR9RBvi1HvuJHVcGjB/r7qsTOEATB+7CfrLJMvvzJr7yseSdk7/9g8x2G6L+lZsDh0Fi7L uHwKrLhZMBfL7SBqAwA/1vn5TV81hNOuqbfwUxpCAFmtCXVsSNWBxYVUCrydXUaBQModTs 2TiMoSXKN4elYGXjUEvCnVZOiSngyhs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725522974; a=rsa-sha256; cv=none; b=zvP1GDDnMgcP0zOBmgVxOXanC2S50LSTyTlLi2mgJauL9QlNSslm4gcS8/CBTr4p8dxsIv lXUOyc94pKZyafG+NLlru3DXIFGBXW6XE6LNY4T4B21Vlq8lCSrOpPAaTypStAvcUFNRkk 8KpV2SPLVgahb4A8kGfhca4qPVPLsX4= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=I7HvquUc; spf=pass (imf19.hostedemail.com: domain of brauner@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=brauner@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 37A07A44379; Thu, 5 Sep 2024 07:57:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A0D83C4CEC9; Thu, 5 Sep 2024 07:57:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725523071; bh=xO3vcgDDnoCbF7ELZ5lnFORM5yLOC/pUkBkeRwkJlfs=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=I7HvquUcEms6uaml8hK37xscwhQS72RhSKTnUPyh0vN66t2ZzapSWd1Gvi8uO6rhw umvVp1zmu8b5gVdoEmQ/RkZVPFUZFdUddhPh6Z/DD38rDAsP8xSVXqXx6ZTX1QkApz 89BQ+AZbPcJBU/XOw9PXkxOKGsgt/flpFUZvv+hJzOMDTzNxLwcjMGeBnU+fRS/+qG YGuLmL7irHDeC4iYwA9A8x36GNAJ7vj6gYJ3uhr6CIif750S1YMIKkrAzdal+WxfzE gA542w9e/qVTZIpPda7NJzSMIT52pzB4GuW4/Ijqve1H4S0Xg5R030ZVEkEuL8kt0h QZX8zNQI101Mw== From: Christian Brauner Date: Thu, 05 Sep 2024 09:56:55 +0200 Subject: [PATCH v4 12/17] slab: create kmem_cache_create() compatibility layer MIME-Version: 1.0 Message-Id: <20240905-work-kmem_cache_args-v4-12-ed45d5380679@kernel.org> References: <20240905-work-kmem_cache_args-v4-0-ed45d5380679@kernel.org> In-Reply-To: <20240905-work-kmem_cache_args-v4-0-ed45d5380679@kernel.org> To: Vlastimil Babka , Jens Axboe , Jann Horn , Linus Torvalds , Mike Rapoport Cc: Kees Cook , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, 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=3953; i=brauner@kernel.org; h=from:subject:message-id; bh=xO3vcgDDnoCbF7ELZ5lnFORM5yLOC/pUkBkeRwkJlfs=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMaTdTPGWnv7hec00zY7uWcHZa36fWJ7VU33/TDKP6lqLz PW/14vGd5SyMIhxMciKKbI4tJuEyy3nqdhslKkBM4eVCWQIAxenAExk1kJGhmWHXumx3lsayv60 +XC6uci3R/ZlmQHCfw+In94cNDlrqxQjw/v1V2ZwbjStnHKhNWCVFYNj/4XntZ8kj/4XWR3ftMu pggcA X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 X-Stat-Signature: w7kdrq3bykakq1whmdjc8ao8tahxiobg X-Rspamd-Queue-Id: 135E81A000B X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1725523071-176697 X-HE-Meta: U2FsdGVkX18uiSxlzNb57TU0Jn5mln+bxAO0AOVw/zAjQ7T2MY24cIL5qLTWgx3LmG5pbpOkogvcSs9S8TChrWQFihNtlwO9EwTcg8rCZxvVP4rnNgGNDqibXhyuQK6s9Gii39hLtz83eq6JYWfcFPThwFp62z1lm4NGCYulkla2jdkITEnF00EbC/OLsg528R9diEKzzixA7CzfnFHDuysGLo0+oMGf0B0o0clJPu9LReppSCbOTln3R/Swa6lW4vyiAHQ0AXeGpJYq/ga/oWoQkWej3NSHYhaiE1/PotHIxBM7PVbZMMTXq99WNCAK3TCfVH60dOHvfCWwHe/0G7rI5USTzOD7xWn0dNxeVbtCdIFklYbq6IpggM+U+BWIs1TF09RODsnhr6UFxbe8BGV9nlIIO9V4zJot9e5DqqBsCBm47Isrd7EmUiDR5UBdkl8YrDFUSFgUXe8CizeHSFTuRRXPrDK67sUcuHPMuLnL71bLBPbvtqrfM/34jwsjDY+qf6tGiY+iOeixWjqCmtGW2U0yNuYhB3H0my7ZW6uuvOaJ0Av9/yiNVQHxxne4bblNw4UaL2x2H8aAfSPE9q1CAGGD6rCLg3Z8xWmoYWxDABle6ABSGFwhAOn4GU8SH9qfiOz/BBA2P8TKm4e8vkYiZCwVHQ2xnprlgtWZPE6RIAMfsh5QDgZ1YUDQTQQIWVFxC0uF3rhaV4CDSeTJNQqz/rX6FhAlCtARUIwuLqA73LYLAwfk0thRgGshhAV52XETVqT/MXbDCl1Xi6EIdbFRgl6WwbwLJFNl7T6RrR6kkGQyOqEbUv18II+BXRDVXRSLE9Tg+KMatZ8N7ytUdSI7aQN0Se6x5Xm96gsAaNbTXD6cE1QANWj30ckaXL3N7nRooHwXb4v3oJ0w+OD93K5uiczneVV+qGhPJxw2sMWCDP1pjm4ZKfkEbp3cey7syS2Fg2Q7qrtE99LLfhr wHayORbr ifZ8WrPjzu/bmEQu3vt4nP/AJOFVU0lD3ml0+k6vg6UiIJG22mn/oUfjYbA3nM2338oSvhw2uydCxIiFnyoMAI16HSglraUVNF67x3phrThN/YQclHSArE8yJfSZbeUNGVqlUMjqJyCRQ9YbDc3N0zrLjFkQOVdRZw/iJjTVdxYAxOBeP2DL8PU3xGwxEjQe6blOFmLYx/3Ngi4FUgLLMPjiWVYzQG70VS25YdKfoHuVrrbAJ3havR9pRLftzIDqMRU5gXFnBCLMu3wBo+afloaBrScTpNGy4R1yeqqLDOklDCcjir9pSyRfvwcm5iAd99k2qBQlkfLP87Rir0e154jawx6RChCIx9fzLkbr9RP+pH3tA37in6LAdrw== 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: Use _Generic() to create a compatibility layer that type switches on the third argument to either call __kmem_cache_create() or __kmem_cache_create_args(). If NULL is passed for the struct kmem_cache_args argument use default args making porting for callers that don't care about additional arguments easy. Reviewed-by: Kees Cook Reviewed-by: Jens Axboe Signed-off-by: Christian Brauner Reviewed-by: Mike Rapoport (Microsoft) Reviewed-by: Roman Gushchin --- include/linux/slab.h | 29 ++++++++++++++++++++++++++--- mm/slab_common.c | 10 +++++----- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/include/linux/slab.h b/include/linux/slab.h index cb264dded324..f74ceb788ac1 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -261,9 +261,10 @@ 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 *)); + +struct kmem_cache *__kmem_cache_create(const char *name, unsigned int size, + unsigned int align, slab_flags_t flags, + void (*ctor)(void *)); struct kmem_cache *kmem_cache_create_usercopy(const char *name, unsigned int size, unsigned int align, slab_flags_t flags, @@ -272,6 +273,28 @@ struct kmem_cache *kmem_cache_create_usercopy(const char *name, struct kmem_cache *kmem_cache_create_rcu(const char *name, unsigned int size, unsigned int freeptr_offset, slab_flags_t 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, + slab_flags_t flags) +{ + struct kmem_cache_args kmem_default_args = {}; + + /* Make sure we don't get passed garbage. */ + if (WARN_ON_ONCE(args)) + return NULL; + + return __kmem_cache_create_args(name, size, &kmem_default_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_destroy(struct kmem_cache *s); int kmem_cache_shrink(struct kmem_cache *s); diff --git a/mm/slab_common.c b/mm/slab_common.c index 19ae3dd6e36f..418459927670 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -383,7 +383,7 @@ kmem_cache_create_usercopy(const char *name, unsigned int size, EXPORT_SYMBOL(kmem_cache_create_usercopy); /** - * kmem_cache_create - Create a cache. + * __kmem_cache_create - Create a cache. * @name: A string which is used in /proc/slabinfo to identify this cache. * @size: The size of objects to be created in this cache. * @align: The required alignment for the objects. @@ -407,9 +407,9 @@ EXPORT_SYMBOL(kmem_cache_create_usercopy); * * Return: a pointer to the cache on success, NULL on failure. */ -struct kmem_cache * -kmem_cache_create(const char *name, unsigned int size, unsigned int align, - slab_flags_t flags, void (*ctor)(void *)) +struct kmem_cache *__kmem_cache_create(const char *name, unsigned int size, + unsigned int align, slab_flags_t flags, + void (*ctor)(void *)) { struct kmem_cache_args kmem_args = { .align = align, @@ -418,7 +418,7 @@ kmem_cache_create(const char *name, unsigned int size, unsigned int align, return __kmem_cache_create_args(name, size, &kmem_args, flags); } -EXPORT_SYMBOL(kmem_cache_create); +EXPORT_SYMBOL(__kmem_cache_create); /** * kmem_cache_create_rcu - Create a SLAB_TYPESAFE_BY_RCU cache.