From patchwork Mon Dec 4 19:34:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vlastimil Babka X-Patchwork-Id: 13479025 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 EE6A9C4167B for ; Mon, 4 Dec 2023 19:35:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 252496B02EF; Mon, 4 Dec 2023 14:34:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EB3316B02F0; Mon, 4 Dec 2023 14:34:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C595E6B02F6; Mon, 4 Dec 2023 14:34:57 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 58FB16B02F0 for ; Mon, 4 Dec 2023 14:34:57 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 332161C0502 for ; Mon, 4 Dec 2023 19:34:57 +0000 (UTC) X-FDA: 81530138634.14.4A6E400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by imf17.hostedemail.com (Postfix) with ESMTP id AD7CB40004 for ; Mon, 4 Dec 2023 19:34:54 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=cYuegURd; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=z8JNsCGQ; dmarc=none; spf=pass (imf17.hostedemail.com: domain of vbabka@suse.cz designates 195.135.223.131 as permitted sender) smtp.mailfrom=vbabka@suse.cz ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1701718495; 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=4fSahCwhPLa3fIglvL1TRmtTOqYm3xhkbDoawZHdw9A=; b=TtOEv6/0if8Xin5VKmXWsGPxcV0MAunevfCYxbwPsHrNZrTPMkngt6f1PSkSr9YjEJFSBo EeBJg6FGMqaRi+k7V5E1hY2dgj7VgzLsdYa8iq/N59/uw0Az64xxlfONZr+f3UIO4GBuY4 zdKlMDGRHmyfNZ53aNBQsO7hPIzwoBs= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=cYuegURd; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=z8JNsCGQ; dmarc=none; spf=pass (imf17.hostedemail.com: domain of vbabka@suse.cz designates 195.135.223.131 as permitted sender) smtp.mailfrom=vbabka@suse.cz ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701718495; a=rsa-sha256; cv=none; b=2TEpJvdxw8k5lTDGQMRfRtwWgRawkEQA/o33KC19j8ysdYXmJTDUiY6dD3rIxLJFlc6IsU DXgyH1Sc6U1VRRJs18WdMAKp9VTR/GbsUjyXPO30Bv+5ujKql77Ee9yKAQzlTDckQtYq4M Tif3RsSJ7G1bIjmNkB/ZycM5hw0lUto= Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [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 A51611FE6E; Mon, 4 Dec 2023 19:34:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1701718492; h=from:from:reply-to: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; bh=4fSahCwhPLa3fIglvL1TRmtTOqYm3xhkbDoawZHdw9A=; b=cYuegURdK6TarNJSSo68uWkNBOrhbmxZDmoXF/uS7znw/iEtrWLpnV32Dfv5L/PQtD6jMV QULfAq7/mAGtPm9Cbv6R2L7cGyXMaX4ps56QutuI/yGpFkZL7SUzA/8QS4L54o79rRjq4f NWPBKnLkVzZDYQTjYHbbwLf2ZFVPFKY= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1701718492; h=from:from:reply-to: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; bh=4fSahCwhPLa3fIglvL1TRmtTOqYm3xhkbDoawZHdw9A=; b=z8JNsCGQ0Xr3e3YcoHYBA6JiZnYEPfBf9mIKjtgLx1BoQFMrFN0F1iTKMetguIwoGikLxp evtQCq2pw+erbpCw== 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 8875313AC2; Mon, 4 Dec 2023 19:34:52 +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 OCn7INwpbmUPMwAAD6G6ig (envelope-from ); Mon, 04 Dec 2023 19:34:52 +0000 From: Vlastimil Babka Date: Mon, 04 Dec 2023 20:34:41 +0100 Subject: [PATCH 2/4] mm/slub: introduce __kmem_cache_free_bulk() without free hooks MIME-Version: 1.0 Message-Id: <20231204-slub-cleanup-hooks-v1-2-88b65f7cd9d5@suse.cz> References: <20231204-slub-cleanup-hooks-v1-0-88b65f7cd9d5@suse.cz> In-Reply-To: <20231204-slub-cleanup-hooks-v1-0-88b65f7cd9d5@suse.cz> To: Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim Cc: Andrew Morton , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Alexander Potapenko , Marco Elver , Dmitry Vyukov , linux-mm@kvack.org, linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, Vlastimil Babka X-Mailer: b4 0.12.4 X-Rspam-User: X-Stat-Signature: j7p3gfs8qtkrp4g7iaqmcaruqw9rybyn X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: AD7CB40004 X-HE-Tag: 1701718494-946256 X-HE-Meta: U2FsdGVkX19aOry7Oiy1LDuEjxGTNRL/Pdz72rVMKaeLbFtn248COiDjEspeS5SHOwqJKGLrIHTXUG3VUYkgJtgqOe8o+/M8tudH1yfS8Itzka5+B4LbQ7T9u4zpOJhsVbWPrwgZ3+iT1NXnu8yJmzewrOYHSNpNQt+rrWLCy9FgDmtk0tzXfKx+ZJnqyrDsA7V1Aqca8Z/kohrLhhPhYXbGe8JkKQf5FWRvx46+1qncsPcfCdX4kGp8urjDru5IJ/b1+hgh5WBtb7kmGfxudSwzej5hBqwSKS8hePbhvnBCEvTMVfjDFtg37zKZCCr5YZ1fOTmtDCBdIVY4dxXMYCSYGLG7at4u5Jtv8eL66BUuKcyFeJt89imn8Bza8OmbnSwghRaprzNKYbqZaFnwA9Qp2tfjBzZUAFN6xCeLQ0lU6cReXrwbXublYyt6JV8parn+XkxPImUzA7IoIDvfUAz0i6HtqiiZYBPklQM5Je3RJYfN73Y++8dbb8RWDFesqswx5PmjMphF8ikudJ20xSxFh/wi1AXXolI3odKO3w8byF5JEAUAofPE0cpt+nIU0c7F3IT/NuatSCqCmtiZ/+fcKZLO7jIBduZ6BT+bqLkeI9gnUXu13bU5YKYH3fU8hmQGOnjl6pIdkQ53Uyk9n4IZhnLMs0/3KZ9zSwxDsTutfhCga91Vg7qLHRp6Z5fbL5VprXocX4A9f4vthRHfaj5Nns5hlWNUsZAa0VPvvILCBX5ZwOt0yKAG6LXi7ipj+gi4lyvfAB8XrOsM1zzRy0hPcEslSFnNFzzk9CUPLknQggOsUvn0MYlEIv+Is4FTBFDruJqaTFvu3ltiGy0aYllscGtHWqidJP86mUbq4t1il12NqIYLIWmXDvIPrFXM29EvTlhs0iIuVNPSnNuz+NCZPUJPRo3+smsIIgHlIJcvXmLuAuVn0kfgmKVQyktmo2SuoRd+EU9O/uIzCIf jGKxBN66 na8WiAgMUCqH3tTp26LZH6vG6bPUqtw3Y5Dbyz4Hozi8E4s3uESwBcb4Rz37wthlty7cmdqtpHA7eo9lq970DJaj6ODtbENR5fEw3CEH5tsVdZF03xDY22u6fqZtEYBJ0ZzaZ4UGqvTvm+ZeyrFRSWYWySwdofY8Hl6Y8zU+Vga6YBaDqj2PhSU1iwQ5W/nSV4ii5TYa3YUXckRck3liJQHzljYJzGgmpbxvqlFrVcQ9nM0tO2nFDzzpe1i8fpVkaamEGHfYdNXsotrnBIPWWNmRqjagsAJ3R9W4+/iR3KJT/Ep2kKBdMZ2aEe/X0+MWs/qblNdw+1maD0OPCFVoh6Ie/R6ZcuwRz7GOd 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: Currently, when __kmem_cache_alloc_bulk() fails, it frees back the objects that were allocated before the failure, using kmem_cache_free_bulk(). Because kmem_cache_free_bulk() calls the free hooks (KASAN etc.) and those expect objects that were processed by the post alloc hooks, slab_post_alloc_hook() is called before kmem_cache_free_bulk(). This is wasteful, although not a big concern in practice for the rare error path. But in order to efficiently handle percpu array batch refill and free in the near future, we will also need a variant of kmem_cache_free_bulk() that avoids the free hooks. So introduce it now and use it for the failure path. As a consequence, __kmem_cache_alloc_bulk() no longer needs the objcg parameter, remove it. Signed-off-by: Vlastimil Babka Signed-off-by: Vlastimil Babka Reviewed-by: Chengming Zhou --- mm/slub.c | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index d7b0ca6012e0..0742564c4538 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -4478,6 +4478,27 @@ int build_detached_freelist(struct kmem_cache *s, size_t size, return same; } +/* + * Internal bulk free of objects that were not initialised by the post alloc + * hooks and thus should not be processed by the free hooks + */ +static void __kmem_cache_free_bulk(struct kmem_cache *s, size_t size, void **p) +{ + if (!size) + return; + + do { + struct detached_freelist df; + + size = build_detached_freelist(s, size, p, &df); + if (!df.slab) + continue; + + do_slab_free(df.s, df.slab, df.freelist, df.tail, df.cnt, + _RET_IP_); + } while (likely(size)); +} + /* Note that interrupts must be enabled when calling this function. */ void kmem_cache_free_bulk(struct kmem_cache *s, size_t size, void **p) { @@ -4499,7 +4520,7 @@ EXPORT_SYMBOL(kmem_cache_free_bulk); #ifndef CONFIG_SLUB_TINY static inline int __kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, - size_t size, void **p, struct obj_cgroup *objcg) + size_t size, void **p) { struct kmem_cache_cpu *c; unsigned long irqflags; @@ -4563,14 +4584,13 @@ static inline int __kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, error: slub_put_cpu_ptr(s->cpu_slab); - slab_post_alloc_hook(s, objcg, flags, i, p, false, s->object_size); - kmem_cache_free_bulk(s, i, p); + __kmem_cache_free_bulk(s, i, p); return 0; } #else /* CONFIG_SLUB_TINY */ static int __kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, - size_t size, void **p, struct obj_cgroup *objcg) + size_t size, void **p) { int i; @@ -4593,8 +4613,7 @@ static int __kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, return i; error: - slab_post_alloc_hook(s, objcg, flags, i, p, false, s->object_size); - kmem_cache_free_bulk(s, i, p); + __kmem_cache_free_bulk(s, i, p); return 0; } #endif /* CONFIG_SLUB_TINY */ @@ -4614,7 +4633,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size, if (unlikely(!s)) return 0; - i = __kmem_cache_alloc_bulk(s, flags, size, p, objcg); + i = __kmem_cache_alloc_bulk(s, flags, size, p); /* * memcg and kmem_cache debug support and memory initialization.