From mboxrd@z Thu Jan 1 00:00:00 1970 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 5C1D4C4167B for ; Wed, 6 Dec 2023 00:33:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B81CB6B0082; Tue, 5 Dec 2023 19:33:01 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B2FD46B0083; Tue, 5 Dec 2023 19:33:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9F7B76B0087; Tue, 5 Dec 2023 19:33:01 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 8F4EC6B0082 for ; Tue, 5 Dec 2023 19:33:01 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 6D658A0DBB for ; Wed, 6 Dec 2023 00:33:01 +0000 (UTC) X-FDA: 81534518562.08.1D51B06 Received: from out-185.mta0.migadu.com (out-185.mta0.migadu.com [91.218.175.185]) by imf23.hostedemail.com (Postfix) with ESMTP id 4F2FA140004 for ; Wed, 6 Dec 2023 00:32:59 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=fw9nU65c; spf=pass (imf23.hostedemail.com: domain of chengming.zhou@linux.dev designates 91.218.175.185 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1701822779; 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=3SnfDF8plKJgpWd+lzxgl5v07A2wnaRHIaQlR7xVpG0=; b=kRkzu8qz6Agr+iBTrzxJAP6aZr8r1COCgqMrsquRWLxZbedLreTUw/3TRRMLSB7zYULdBN gMp4/kpnc3Y8RcDb7t0cXNBOpDFcF7yx92+RhQdTmiSc8GKrA9VuBtJtnbPs0pScdsFpie DY3DtcdLTlANe8tgWo3MIciZMTZ1Yls= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701822779; a=rsa-sha256; cv=none; b=jHCntCWd0pR+eFK5QZc0A+RaIgNjKm4x4c7KimOkRyISn/mxdM81Fc/EMVL3MfaQbJo+/9 hKi2ZZZhPxLFAmCS5jKF7uUZtgt7kuYYIki+ohE7MDVvcCVdjHOZ/j7knAp/P4VrYeyBXu zwk4juX07h+PTeBU93L4Uk0TQRd7r2c= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=fw9nU65c; spf=pass (imf23.hostedemail.com: domain of chengming.zhou@linux.dev designates 91.218.175.185 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev; dmarc=pass (policy=none) header.from=linux.dev Message-ID: <836818de-73ca-4233-830a-71a80dcc1c6c@linux.dev> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1701822777; h=from:from: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; bh=3SnfDF8plKJgpWd+lzxgl5v07A2wnaRHIaQlR7xVpG0=; b=fw9nU65cSvACJbDJPcf++nb7x1mNDM+TSff2WJ3ew4mQ0gk8mKsC5v2oqPNMSV1PZ0ELXZ /APKjQ9tcywQDH0ZY8bQgQQbX6EzI/NewhfDQIHYCxD1VfZ75DuPljIMfkcQwGUp+kCuEQ d1IjaPaSJoV7L4sCwCbTtJ1IXYu8504= Date: Wed, 6 Dec 2023 08:31:45 +0800 MIME-Version: 1.0 Subject: Re: [PATCH 2/4] mm/slub: introduce __kmem_cache_free_bulk() without free hooks Content-Language: en-US To: Vlastimil Babka , 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 References: <20231204-slub-cleanup-hooks-v1-0-88b65f7cd9d5@suse.cz> <20231204-slub-cleanup-hooks-v1-2-88b65f7cd9d5@suse.cz> <30f88452-740b-441f-bb4f-a2d946e35cf5@linux.dev> <25eb93ee-e71a-c257-ef4b-9fbb3b694faf@suse.cz> X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Chengming Zhou In-Reply-To: <25eb93ee-e71a-c257-ef4b-9fbb3b694faf@suse.cz> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_OUT X-Stat-Signature: oj3onz7qrxbe7m64srtfniq3eqiwfjng X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 4F2FA140004 X-Rspam-User: X-HE-Tag: 1701822779-364507 X-HE-Meta: U2FsdGVkX18iPabrKZWmRHh6Z944xHbNpMNbAbx10shulymoLC4JEoaPLTvdRUGroH28fDoxg5BkwLoNSBt+IG3tL8rAa3e8P/HOO2zIuZiPB8q7cArp4Cbpl9Rr5nd6m+Vg7Ox7OGHUOPa5A8Ae+qJ11f2tAZMPa2AJ724Ma1z5yQ4Y8Jk4R3q0BCYITcnCLT1Wz8j6+4k76BccTP0QjoGgiRQwD1hSGIHc6Uodf7wDfozMY6l0O2uxmupwqytzImLGoTtzMsrshUysNhvtPy7+CmlJdLBkjAukje4bOzGLMEkHlGzOb3f7F/LK8BsQ5y5S9NGGhp8K2anU0efpO+gfbXNDWZMGgnOWYnvaloLTGoH1x/2FurTDi+iSRi9eg+I8D59tYZN4ZQtqIpz9D5icC+4OOmAj34EEcVibW56IFPLal4Hv8u1+SNYPL76UyeTIs8nOE3vs/gZ9AybMIRK5OuPdkyi0IrBdztWBXR5zIuwL8HkHt46/4CrGXw4TfoIaYMUgNQxhroiPWX3wm1CeWcu9m3SOCbZ3UbnGE5JkGkx/mOIFPrIK7Xeogf/ogH+UrRS0lis6cVr0ReF1QryKruFCN1SiFD8AbTbxeB9QeHzShMdq5XuFi/JVyp8mwR95dUxoUIxFs1nblM1wPgtL3e1XW8WiJgnwBIq1yyK0dc+QNrK0PZ6Y5C1fKPqNtjMlkA9HnWYcUGxeSk9Cqo6AKHcAs799jMRKrpwj1YXJNKJPybue9EcZKLS1iEYC910mnvTaz2hbjK66ZBGvvfWVrEjhWo6nK00R5iBk4yaKbwXqVG37yQdHoY9vTkHTgvjvkCLUC0Rm8abc+todJ4LJvXZg2czDraxL/J4LCz7bXjwXG+3GGuK8WtuVrSho36Wp/lyo3sRtQl2V1nJFr14uIgYQH2RdjXGy9jDxcSenT2hmEel7BGlDDnU5LDUszueo3R2DiPgboudDrWe 9jVOctFI jvl0FdTVYGCK5Dm6ydr5kV7Cy7BRLahs7WaXnj1nHiEU9pWqlb9vYrvwBvAuCiUMJedYZFegW/YGxSYn+9q8P4QJZFPTWd+wbQpsC8pjHO+vnuZqxb8AI/U5a+oyJqfGZhAUdSE/hPSmn2C2FUK/aO0JVG1xd3Dgx/LbEHwgFP0zPoBYbhT4zya3EayOCXIXeA0KvLbJ3WWkvNcsrrRMsQzMrAEOWe+PRW3NpRztj2IxhoNJe8qIpl8xif00yC3nn4YuuJNyTDZAY2XI= 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: On 2023/12/6 03:57, Vlastimil Babka wrote: > On 12/5/23 09:19, Chengming Zhou wrote: >> On 2023/12/5 03:34, Vlastimil Babka wrote: >>> 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. >> >> The objects may have been charged before, but it seems __kmem_cache_alloc_bulk() >> forget to uncharge them? I can't find "uncharge" in do_slab_free(), or maybe >> the bulk interface won't be used on chargeable slab? > > You're right! I missed that the memcg_pre_alloc_hook() already does the > charging, so we need to uncharge. How does this look? Thanks for noticing! > > ----8<---- > From 52f8e77fdfeabffffdce6b761ba5508e940df3be Mon Sep 17 00:00:00 2001 > From: Vlastimil Babka > Date: Thu, 2 Nov 2023 16:34:39 +0100 > Subject: [PATCH 2/4] mm/slub: introduce __kmem_cache_free_bulk() without free > hooks > > 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. > > In case of failure we however still need to perform memcg uncharge so > handle that in a new memcg_slab_alloc_error_hook(). Thanks to Chengming > Zhou for noticing the missing uncharge. > > As a consequence, __kmem_cache_alloc_bulk() no longer needs the objcg > parameter, remove it. > > Signed-off-by: Vlastimil Babka Looks good to me! Reviewed-by: Chengming Zhou Thanks! > --- > mm/slub.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++--------- > 1 file changed, 47 insertions(+), 9 deletions(-) > > diff --git a/mm/slub.c b/mm/slub.c > index d7b0ca6012e0..0a9e4bd0dd68 100644 > --- a/mm/slub.c > +++ b/mm/slub.c > @@ -2003,6 +2003,14 @@ void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab, void **p, > > __memcg_slab_free_hook(s, slab, p, objects, objcgs); > } > + > +static inline > +void memcg_slab_alloc_error_hook(struct kmem_cache *s, int objects, > + struct obj_cgroup *objcg) > +{ > + if (objcg) > + obj_cgroup_uncharge(objcg, objects * obj_full_size(s)); > +} > #else /* CONFIG_MEMCG_KMEM */ > static inline struct mem_cgroup *memcg_from_slab_obj(void *ptr) > { > @@ -2032,6 +2040,12 @@ static inline void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab, > void **p, int objects) > { > } > + > +static inline > +void memcg_slab_alloc_error_hook(struct kmem_cache *s, int objects, > + struct obj_cgroup *objcg) > +{ > +} > #endif /* CONFIG_MEMCG_KMEM */ > > /* > @@ -4478,6 +4492,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) > { > @@ -4498,8 +4533,9 @@ void kmem_cache_free_bulk(struct kmem_cache *s, size_t size, void **p) > 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) > +static inline > +int __kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size, > + void **p) > { > struct kmem_cache_cpu *c; > unsigned long irqflags; > @@ -4563,14 +4599,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 +4628,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,15 +4648,19 @@ 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. > * Done outside of the IRQ disabled fastpath loop. > */ > - if (i != 0) > + if (likely(i != 0)) { > slab_post_alloc_hook(s, objcg, flags, size, p, > slab_want_init_on_alloc(flags, s), s->object_size); > + } else { > + memcg_slab_alloc_error_hook(s, size, objcg); > + } > + > return i; > } > EXPORT_SYMBOL(kmem_cache_alloc_bulk);