Received: by 2002:a05:6358:e9c4:b0:b2:91dc:71ab with SMTP id hc4csp6438481rwb; Tue, 9 Aug 2022 15:37:45 -0700 (PDT) X-Google-Smtp-Source: AA6agR5wR6jMcdKMMC+lTR2ZnyVJuIsA27CS0C/HIqnfbncvUG7oJBDMfzEziFri/AUfLzK6nILQ X-Received: by 2002:a17:907:2da6:b0:730:8b30:e517 with SMTP id gt38-20020a1709072da600b007308b30e517mr18315905ejc.291.1660084665160; Tue, 09 Aug 2022 15:37:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660084665; cv=none; d=google.com; s=arc-20160816; b=B7HkxAwkXglKkPuJvP8xFxk4DwvskQhd0SFu1xBgtMzuGPhxRX9X5qoY53J+EL08yd blO+H3ne0f/8nfWMPhJOmXK9hn6Su/xH8zDN+6LFYY67FJrm6fS6gPIpFQp8qg6BnVwI 0GwMDDiDoMH4Xqz7QUwMuJUSKWd9y3LbdH0agPPSt6w4D6VgCMq4MA6yDli40ID5ruI/ mamTTLQqeel71AIlr2xYdn3tduL34fWCsGKmX8d8jMAL3JhQe3XxMuX8Irw642QIlWA5 P7n0vy1txiasg93qYH9+DExAhU+ztnaywPTV61wHndxU5JYLc5VRxNgf6yHj+pjj6tcG OnvA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:dkim-signature:date; bh=hOnt2Z7v85TLJ7c4EagfbuOt1hNBAIsJlwJaIbi0B/k=; b=Rc7oSaopx07KTNfMKDMom/Ljhm404VC8WdmSXHhjU5hpF4X8B4XILwdaNlYoKYscLN SWI79BdM+7ETdHAc6rjMvtM/AX8tJAflilIRMRqvI5QX5cP4RUQyR1Ug22DHihuyCm4y MDlnegxLslD4gUByNIwzy918kz7fOfcjZc3jE2LFX1O4RCIvO9i/LvqtfPvkTUMD8U2K GF4F/1ve5eyclYEn+0B24v7FNXEaDHqWaYQnjiJTjVqWQScn8za0kbHCEiDKuljWjrAN cYJMDVY5GuXpn/tsvhQDaQFiSv5ppzbVhrsc4/qRWpGs6Qe3SvTRWQCF02u25KtyMl/C dofQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=Rxnb7nBz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i3-20020a50fc03000000b0043be28d93adsi7902823edr.261.2022.08.09.15.37.19; Tue, 09 Aug 2022 15:37:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=Rxnb7nBz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229928AbiHIW0W (ORCPT + 99 others); Tue, 9 Aug 2022 18:26:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48730 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230117AbiHIW0R (ORCPT ); Tue, 9 Aug 2022 18:26:17 -0400 Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C6C35D0FF for ; Tue, 9 Aug 2022 15:26:14 -0700 (PDT) Date: Tue, 9 Aug 2022 15:25:54 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1660083972; 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: in-reply-to:in-reply-to:references:references; bh=hOnt2Z7v85TLJ7c4EagfbuOt1hNBAIsJlwJaIbi0B/k=; b=Rxnb7nBzIHrkN8GbjZULOtGCCexNPdqnvEmPDG5K/SP7Ugt2QYQYtVR1qWLBe0mggDxdJo EdcQXS/P8LMsRN+FfCYXKckwQLMJyom1+ULCj6DcSB/xOQgTX/E0GtgJWJrDkZ0XzC0fGD xMRaZwT1xiA+Rt7WT19Ek5pZbPbC1wc= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Roman Gushchin To: Waiman Long Cc: Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Vlastimil Babka , Hyeonggon Yoo <42.hyeyoo@gmail.com>, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] mm/slab_common: Deleting kobject in kmem_cache_destroy() without holding slab_mutex/cpu_hotplug_lock Message-ID: References: <20220809205901.76595-1-longman@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220809205901.76595-1-longman@redhat.com> X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Aug 09, 2022 at 04:59:01PM -0400, Waiman Long wrote: > A circular locking problem is reported by lockdep due to the following > circular locking dependency. > > +--> cpu_hotplug_lock --> slab_mutex --> kn->active#126 --+ > | | > +---------------------------------------------------------+ > > One way to break this circular locking chain is to avoid holding > cpu_hotplug_lock and slab_mutex while deleting the kobject in > sysfs_slab_unlink() which should be equivalent to doing a write_lock > and write_unlock pair of the kn->active virtual lock. > > Since the kobject structures are not protected by slab_mutex or the > cpu_hotplug_lock, we can certainly release those locks before doing > the delete operation. > > Move sysfs_slab_unlink() and sysfs_slab_release() to the newly > created kmem_cache_release() and call it outside the slab_mutex & > cpu_hotplug_lock critical sections. > > Signed-off-by: Waiman Long > --- > mm/slab_common.c | 48 +++++++++++++++++++++++++++++++----------------- > 1 file changed, 31 insertions(+), 17 deletions(-) > > diff --git a/mm/slab_common.c b/mm/slab_common.c > index 17996649cfe3..9274fb03563e 100644 > --- a/mm/slab_common.c > +++ b/mm/slab_common.c > @@ -392,6 +392,30 @@ kmem_cache_create(const char *name, unsigned int size, unsigned int align, > } > EXPORT_SYMBOL(kmem_cache_create); > > +#ifdef SLAB_SUPPORTS_SYSFS > +/* > + * For a given kmem_cache, kmem_cache_destroy() should only be called > + * once or there will be a use-after-free problem. The actual deletion > + * and release of the kobject does not need slab_mutex or cpu_hotplug_lock > + * protection. So they are now done without holding those locks. > + */ > +static void kmem_cache_release(struct kmem_cache *s, bool workfn) > +{ > + if (!workfn) > + sysfs_slab_unlink(s); > + > + if (workfn || !(s->flags & SLAB_TYPESAFE_BY_RCU)) > + sysfs_slab_release(s); > + else > + schedule_work(&slab_caches_to_rcu_destroy_work); > +} > +#else > +static inline void kmem_cache_release(struct kmem_cache *s, bool workfn) > +{ > + slab_kmem_cache_release(s); > +} > +#endif > + > static void slab_caches_to_rcu_destroy_workfn(struct work_struct *work) > { > LIST_HEAD(to_destroy); > @@ -418,11 +442,7 @@ static void slab_caches_to_rcu_destroy_workfn(struct work_struct *work) > list_for_each_entry_safe(s, s2, &to_destroy, list) { > debugfs_slab_release(s); > kfence_shutdown_cache(s); > -#ifdef SLAB_SUPPORTS_SYSFS > - sysfs_slab_release(s); > -#else > - slab_kmem_cache_release(s); > -#endif > + kmem_cache_release(s, true); Hi Waiman! As I understand, with SLAB_SUPPORTS_SYSFS kmem_cache_release() can effectively call into itself: first it's called with workfn == false from shutdown_cache() and then optionally it's scheduled to call itself from a work context with workfn == true just to call sysfs_slab_release(). Is it right? If !SLAB_SUPPORTS_SYSFS, shutdown_cache() optionally adds kmem_cache to the slab_caches_to_rcu_destroy list and calls kmem_cache_release(s, false) == slab_kmem_cache_release(). How it's then removed from the list? Overall the patch is a bit hard to follow (not like this code was easy to read before, so can't blame the patch). But I wonder if it will make things simpler to decouple kmem_cache_release(workfn == true) and kmem_cache_release(workfn == false) into 2 different helpers? Or at least add a bold comment on how things are supposed to work. Thanks!