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 34B23C00140 for ; Wed, 10 Aug 2022 09:35:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C1F738E0002; Wed, 10 Aug 2022 05:35:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BCEB28E0001; Wed, 10 Aug 2022 05:35:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A95FA8E0002; Wed, 10 Aug 2022 05:35:01 -0400 (EDT) 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 9B66B8E0001 for ; Wed, 10 Aug 2022 05:35:01 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 6A5431C673A for ; Wed, 10 Aug 2022 09:35:01 +0000 (UTC) X-FDA: 79783174002.10.483C9E6 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by imf08.hostedemail.com (Postfix) with ESMTP id 749E816018E for ; Wed, 10 Aug 2022 09:35:00 +0000 (UTC) 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 ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=emeQYDqj; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=xNzhxFxR; spf=pass (imf08.hostedemail.com: domain of vbabka@suse.cz designates 195.135.220.28 as permitted sender) smtp.mailfrom=vbabka@suse.cz; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660124100; a=rsa-sha256; cv=none; b=xMzCzRZ8tAKax53Vax+/F8Iv9/DeExb4ysYfCzPsiXnmBLjyJSLiIxJXZZJF/8iuQQqv5Z WC4nEJrzvTkTc3joqcplDI7F7b8rnEbXl3xMe3Q5Z4maD4kMNdZPKn7lrFiARIfzxq53sk 6v2QHuYuLo2TH8aDSd+pAgWgdsOWJM4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660124100; 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=kQSwjPiKaiGHj/f8V1/GnqkouaKOzplWk5CMp2oq4Fk=; b=7WSHYJSvk9GsZNnnYmP1EzDr04ACma5eFIX65EePzlLPuHqUDnMfKnHlWzjaeXSUlcpVRp PdTlgXyShGbMMTegjV3XBZw7MfynpI+XRhXnQ1paknk2jSEUuS6kvlrkvnhkO+ZPYo4Lko Y5JXzzgj8DiMrUiP1B6sPjJM1W5nSmU= X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 749E816018E X-Rspam-User: Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=emeQYDqj; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=xNzhxFxR; spf=pass (imf08.hostedemail.com: domain of vbabka@suse.cz designates 195.135.220.28 as permitted sender) smtp.mailfrom=vbabka@suse.cz; dmarc=none X-Stat-Signature: bzf4qbjqeord6oarkyxy36begwx7cbar X-HE-Tag: 1660124100-818302 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: 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); >