Received: by 2002:a05:6358:e9c4:b0:b2:91dc:71ab with SMTP id hc4csp6899534rwb; Wed, 10 Aug 2022 03:13:04 -0700 (PDT) X-Google-Smtp-Source: AA6agR5fcmPVe+nTD0xyPNSB91889r3zSDInoqJ7lIRkV6+1orU+Gc2xZyfKicaPeYE3fuBgPK7s X-Received: by 2002:a05:6402:5107:b0:43d:6b26:bdc5 with SMTP id m7-20020a056402510700b0043d6b26bdc5mr25695735edd.156.1660126384692; Wed, 10 Aug 2022 03:13:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660126384; cv=none; d=google.com; s=arc-20160816; b=uJMLjlCP4kGiFfpwwWc+NX7tKH9i17Ug1mzf/9d5jXI/3oAnKWqhWk6UCtI21pzZKk XGLZhabN4l8JAq1qOKtnKpAJhZx3JyQQWQXdg12JJw/yMxPg8hkB0f1SxrFvXK0KPClH 5Kj+x+VamRlJFqgL0dRtW+hyX8Z7lFxYw1buKakjqQXbrEaSlb3e5FafHR3eTohvhu3h p+tyDtj8CDH6Zbh+Ze39EsUPQRzfs55U++VWEN8cS8QfGYBEmbzmHTkDmR2AFj8EGf1a QCz05Tia3Iv99/zkmL7Hb5kKffkcKSaeysOIFck+zg8LjmP2WtddGszQI1bdvqPOAoBD H0/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature:dkim-signature; bh=kQSwjPiKaiGHj/f8V1/GnqkouaKOzplWk5CMp2oq4Fk=; b=fv+eGrlQfwMXQFaU1Zkk0ISY6BIgv+/Y/jClqDlJfwVs+sy5ifrJ2X4kLRbITWoR9h y0j22RjjwiMS4Fl14Ak7xFA8KQx0rA22h3spPX0iUUR6noKj2PxrSSa9PetrH9YTxQGv Ag0GqUJama9p4gOdWE7IgyaexZIF7Z+bK5ZR1eP3e4nOggdOzCDc6eduKny0IkIJwEec VmKwKpz6j6RpJr4O6ICdX/wfuPSAc0Ffa10r4kU1g0eUlurayRXlYCwRYOZxmuzqSnUS hPyzacugryLrhkKqMO9wmXvXT2AY0o+v804mlIHAKzIPlMDOR75C7KfJASkCKQe3nyj0 IFyQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=emeQYDqj; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519 header.b=xNzhxFxR; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qa16-20020a170907869000b0073045f4792dsi4313068ejc.491.2022.08.10.03.12.38; Wed, 10 Aug 2022 03:13:04 -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=@suse.cz header.s=susede2_rsa header.b=emeQYDqj; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519 header.b=xNzhxFxR; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232151AbiHJJfE (ORCPT + 99 others); Wed, 10 Aug 2022 05:35:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33752 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229771AbiHJJfC (ORCPT ); Wed, 10 Aug 2022 05:35:02 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 92A096CF46 for ; Wed, 10 Aug 2022 02:35:00 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 43ECD3458C; Wed, 10 Aug 2022 09:34:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1660124099; 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=kQSwjPiKaiGHj/f8V1/GnqkouaKOzplWk5CMp2oq4Fk=; b=emeQYDqj0tovgbYqATK72rnUukDw5gKb++2YT8oC4dMxVGmfNN+fzty2iU0NzgUdwL4BIU ZoLMeDxbZ+De2FePQ8bdzBkFL58oyYlRLoekB3c/ce9+aGencVeznxWfKQovcCZRQzdvyl VKnnrRZS/DLV1PIDoDd29Pp8DVl9afY= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1660124099; 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=kQSwjPiKaiGHj/f8V1/GnqkouaKOzplWk5CMp2oq4Fk=; b=xNzhxFxRGPBn3fYPhoVReqHx1UBBH6BtRfLUMZfU/KFeq1VwT9PyFzjaFo2irA5YPiXTJy tMF3+ylRot+pNsDA== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id D719813AB3; Wed, 10 Aug 2022 09:34:58 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id mPEvM8J782JRIQAAMHmgww (envelope-from ); Wed, 10 Aug 2022 09:34:58 +0000 Message-ID: <48b66d90-34ae-8666-d9ee-2d36c82a6653@suse.cz> Date: Wed, 10 Aug 2022 11:34:58 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.1.0 Subject: Re: [PATCH] mm/slab_common: Deleting kobject in kmem_cache_destroy() without holding slab_mutex/cpu_hotplug_lock Content-Language: en-US To: Waiman Long , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Sebastian Andrzej Siewior , Xin Long Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20220809205901.76595-1-longman@redhat.com> From: Vlastimil Babka In-Reply-To: <20220809205901.76595-1-longman@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,SPF_HELO_NONE, SPF_SOFTFAIL,T_SCC_BODY_TEXT_LINE autolearn=no 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 8/9/22 22:59, 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 --+ > | | > +---------------------------------------------------------+ This sounded familiar and I've found a thread from January: https://lore.kernel.org/all/388098b2c03fbf0a732834fc01b2d875c335bc49.1642170196.git.lucien.xin@gmail.com/ But that seemed to be specific to RHEL-8 RT kernel and not reproduced with mainline. Is it different this time? Can you share the splats? > 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); > } > } > > @@ -437,20 +457,10 @@ static int shutdown_cache(struct kmem_cache *s) > list_del(&s->list); > > if (s->flags & SLAB_TYPESAFE_BY_RCU) { > -#ifdef SLAB_SUPPORTS_SYSFS > - sysfs_slab_unlink(s); > -#endif > list_add_tail(&s->list, &slab_caches_to_rcu_destroy); > - schedule_work(&slab_caches_to_rcu_destroy_work); > } else { > kfence_shutdown_cache(s); > debugfs_slab_release(s); > -#ifdef SLAB_SUPPORTS_SYSFS > - sysfs_slab_unlink(s); > - sysfs_slab_release(s); > -#else > - slab_kmem_cache_release(s); > -#endif > } > > return 0; > @@ -465,14 +475,16 @@ void slab_kmem_cache_release(struct kmem_cache *s) > > void kmem_cache_destroy(struct kmem_cache *s) > { > + int refcnt; > + > if (unlikely(!s) || !kasan_check_byte(s)) > return; > > cpus_read_lock(); > mutex_lock(&slab_mutex); > > - s->refcount--; > - if (s->refcount) > + refcnt = --s->refcount; > + if (refcnt) > goto out_unlock; > > WARN(shutdown_cache(s), > @@ -481,6 +493,8 @@ void kmem_cache_destroy(struct kmem_cache *s) > out_unlock: > mutex_unlock(&slab_mutex); > cpus_read_unlock(); > + if (!refcnt) > + kmem_cache_release(s, false); > } > EXPORT_SYMBOL(kmem_cache_destroy); >