From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4323126E142 for ; Mon, 27 Apr 2026 13:01:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.49 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777294904; cv=none; b=oZBO8pUAmly7BS2tOpcRPSrV8KOCQrYeXIVIyqeuC6JK05tOiT7b9i6qGTdcduTe0H5w/HjhcSrGeDmhLkjFIqtj+1VsB/AGPFWkY8UvXYLUeCdHYissMis4PKgAcz3WiWIfirtVf3SqNki81FCPpxKyJmmwfvvIpewJI7o2wh0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777294904; c=relaxed/simple; bh=S1c9d/jr7w43ANbE7m7+/oGdlxvY8ri7HX7HICnmuTA=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=kQaXMRw8IWe+iZHEqtJiqtiBxnde8RTPUoevz5YJnmjT7tHs426OEhMoKE+K+cm88nwqkbpVI3f61gwsn0LdH1WFtq3FgR4SA3IJy0NfJtRi8MEgDswU0HuReDMsl5iEvmIrB/88jRXv1LV2iopwidLLdJc+y4QL5wMx8zM+CsE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b=Q2KYPalb; arc=none smtp.client-ip=209.85.128.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b="Q2KYPalb" Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-488af96f6b2so134464445e9.0 for ; Mon, 27 Apr 2026 06:01:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1777294902; x=1777899702; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=qKk3ZzGqPoxad8o9BetIXz6fD01VB9XTvLwJqis+UP8=; b=Q2KYPalbQaFr6kkwH2Lnf1ZpKfQ5wZdAgFZ2g2u3jR2bo4vNpxbOimV0gqnWgafB7/ knpMqYtKTYIR7X3ejL6Ejhz7zHCEACMju4LyYzrYFUS5mcSitQIGdwO8QhgD8kB8c9Zh 1XCTzQMqpdkoqt0U41P/JQhXGts8+ZET61KpiS83XyeSLO/6nbrJIMryuCm5bYQn16S6 pPyS+d2mdlFF1D8DphND0ioRa/c1CVuxuq0EgvfFKMeQX79GBoihPYHzw20M25qBx4bE 436jQRM6V5PTMk5N2tYOslRz/oNYTKscP6QEhjGc21v2gwgsN+9ik/O+oQYueYie5vxZ 7f+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777294902; x=1777899702; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qKk3ZzGqPoxad8o9BetIXz6fD01VB9XTvLwJqis+UP8=; b=eK9ZZeLvrz/b3bj9vtBnkpF1UtdbW2zVi9DyaUCbhp3/QLyJ5aWdePfLnv6T2G4vkN 3FkjoZ5vtxv2CiCxmMGEZYKQqFRLKJBeHJYSswMiY0WysM1R082ObydAXSueUHLR9Z3f qteEmUwCGa9QdsxenUwQb0+mmBZXkTnxg+GtUkZHZwQRqXy0kpWiWIkFdgwbvZV4uV3j UuRX0yL+OSmiqpoHFdCVCeSfdaxlZnuimanVXCRtvim2xyE+AHCH25BRtlyjdSpCJucu ea7DZW2SyPAqGW5zeiUqCkVDfFF+oqn9My/oWzsmMfcLZRq7egIhaSEU82QNxo9p84BW U7tA== X-Forwarded-Encrypted: i=1; AFNElJ9J14YWDs52tj3Hnv8LgA6E0AJuXxt9y5R16UJdSpX2dHXQR0BHPmVhAat2vJs7n3se4HPNU1lWE4iM3no=@vger.kernel.org X-Gm-Message-State: AOJu0YzkYJta1bViEAK6i1E3pU+D3W0DKIc8CpLKO2VDy2VJd5wp1bIk bEv/M7YqTq45NVry4Ff1WK/6Mw6tTcWKcxyAZecKEnHV6cwtNbSYTJx+rHxBYEAFGwB0wM509H8 aU16MHHk= X-Gm-Gg: AeBDieuXQ0FX4EQtpRhotPev8HMTCUoMMjaHvn4xbmXztwwCd5NXSv0d1HiFuxHnoWK 8p66MuCzdhg+lxXmbtMsaje8BeHiQgWrPAFz+oq0KOCVHl5EYb42ZsbKPbiSHgyf5txWjaNqZrL xfZjK0HtOIAgvnDEesB5tGJrjPIRdJgNSvNViz7Lbc844EMDLS3JPxgGrcB1OmV1iyOcVkGIX/h 71NBWpJdao9uz6xKJv4Sjp5TmHdGoJW2Qp0DLBy346dBW3hZGL3u3zbw7pcqkGSd9pY9jVcc97X DhKBLZdeVQLjMolF68aLJu2HjQCW4kCtqU329rq70FJgDVNP5u988dRRtcJMnz3Xre/riDYUusR vbcFYkDrEUNRMcmiDI1l37Wp08Gu4cB1Lkw2oXLJYtMNsbvtdJ6GDCeSBBGEOaonvJEVpPl6vpq GvcdLJ6plV3BkzIKAVTfKojyOGyDeLt1iLwLcqy3FDj+ikAKg= X-Received: by 2002:a05:600d:8408:b0:486:ff92:63e5 with SMTP id 5b1f17b1804b1-488fb73dc6fmr477069085e9.6.1777294901488; Mon, 27 Apr 2026 06:01:41 -0700 (PDT) Received: from localhost (109-81-17-171.rct.o2.cz. [109.81.17.171]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43fe4cb135asm86264355f8f.6.2026.04.27.06.01.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Apr 2026 06:01:40 -0700 (PDT) Date: Mon, 27 Apr 2026 15:01:33 +0200 From: Michal Hocko To: Christian Brauner Cc: Thomas Graf , Herbert Xu , Andrew Morton , Vlastimil Babka , Lorenzo Stoakes , David Hildenbrand , Suren Baghdasaryan , linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, syzbot+5af806780f38a5fe691f@syzkaller.appspotmail.com Subject: Re: [PATCH] rhashtable: give each instance its own lockdep class Message-ID: References: <20260427-work-rhashtable-lockdep-v1-1-f69e8bd91cb2@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260427-work-rhashtable-lockdep-v1-1-f69e8bd91cb2@kernel.org> On Mon 27-04-26 13:09:57, Christian Brauner wrote: > syzbot reported a possible circular locking dependency between > &ht->mutex and fs_reclaim: > > CPU0 (kswapd0) CPU1 (kworker) > -------------- -------------- > fs_reclaim ht->mutex > shmem_evict_inode rhashtable_rehash_alloc > simple_xattrs_free bucket_table_alloc(GFP_KERNEL) > rhashtable_free_and_destroy __kvmalloc_node > mutex_lock(&ht->mutex) might_alloc -> fs_reclaim > > The two halves of the splat refer to two different events on > &ht->mutex. > > The kswapd0 path is unambiguous: shmem_evict_inode at mm/shmem.c:1429 > calls simple_xattrs_free(), which calls rhashtable_free_and_destroy() > on the per-inode simple_xattrs rhashtable being torn down with the > inode. > > The previously-recorded ht->mutex -> fs_reclaim edge comes from > rht_deferred_worker -> rhashtable_rehash_alloc -> > bucket_table_alloc(GFP_KERNEL) -> __kvmalloc_node -> > might_alloc -> fs_reclaim. That stack stops at generic library code: > there is no subsystem-specific frame above rht_deferred_worker, so > the splat does not identify which rhashtable's worker recorded the > edge -- only that some rhashtable in the system did. > > Whether or not that recording happened on the same simple_xattrs ht > that is now being destroyed, the predicted deadlock cannot occur: > rhashtable_free_and_destroy() does cancel_work_sync(&ht->run_work) > before taking ht->mutex, so the deferred worker cannot be running on > the instance being torn down. If the recording was on a different > rhashtable instance, the two ht->mutex acquisitions are on distinct > mutex objects and cannot deadlock either. > > Lockdep flags a cycle regardless because mutex_init(&ht->mutex) lives > on a single source line in rhashtable_init_noprof(), so every > ht->mutex in the kernel shares one static lockdep class. Lockdep > matches by class, not by instance, and collapses all of these into > one node. > > Lift the lockdep key out of rhashtable_init_noprof() and into the > caller. The user-visible rhashtable_init_noprof() / > rhltable_init_noprof() identifiers become macros that declare a > per-call-site static lock_class_key. > > Reported-by: syzbot+5af806780f38a5fe691f@syzkaller.appspotmail.com > Closes: https://lore.kernel.org/69e798fe.050a0220.24bfd3.0032.GAE@google.com > Signed-off-by: Christian Brauner Acked-by: Michal Hocko Thanks! > --- > include/linux/rhashtable-types.h | 22 ++++++++++++++++++---- > lib/rhashtable.c | 17 ++++++++++------- > 2 files changed, 28 insertions(+), 11 deletions(-) > > diff --git a/include/linux/rhashtable-types.h b/include/linux/rhashtable-types.h > index 015c8298bebc..841021c67d3d 100644 > --- a/include/linux/rhashtable-types.h > +++ b/include/linux/rhashtable-types.h > @@ -131,12 +131,26 @@ struct rhashtable_iter { > bool end_of_table; > }; > > -int rhashtable_init_noprof(struct rhashtable *ht, > - const struct rhashtable_params *params); > +int __rhashtable_init_noprof(struct rhashtable *ht, > + const struct rhashtable_params *params, > + struct lock_class_key *key); > +#define rhashtable_init_noprof(ht, params) \ > +({ \ > + static struct lock_class_key __key; \ > + \ > + __rhashtable_init_noprof(ht, params, &__key); \ > +}) > #define rhashtable_init(...) alloc_hooks(rhashtable_init_noprof(__VA_ARGS__)) > > -int rhltable_init_noprof(struct rhltable *hlt, > - const struct rhashtable_params *params); > +int __rhltable_init_noprof(struct rhltable *hlt, > + const struct rhashtable_params *params, > + struct lock_class_key *key); > +#define rhltable_init_noprof(hlt, params) \ > +({ \ > + static struct lock_class_key __key; \ > + \ > + __rhltable_init_noprof(hlt, params, &__key); \ > +}) > #define rhltable_init(...) alloc_hooks(rhltable_init_noprof(__VA_ARGS__)) > > #endif /* _LINUX_RHASHTABLE_TYPES_H */ > diff --git a/lib/rhashtable.c b/lib/rhashtable.c > index 6074ed5f66f3..fb13749d824a 100644 > --- a/lib/rhashtable.c > +++ b/lib/rhashtable.c > @@ -1025,8 +1025,9 @@ static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed) > * .obj_hashfn = my_hash_fn, > * }; > */ > -int rhashtable_init_noprof(struct rhashtable *ht, > - const struct rhashtable_params *params) > +int __rhashtable_init_noprof(struct rhashtable *ht, > + const struct rhashtable_params *params, > + struct lock_class_key *key) > { > struct bucket_table *tbl; > size_t size; > @@ -1036,7 +1037,7 @@ int rhashtable_init_noprof(struct rhashtable *ht, > return -EINVAL; > > memset(ht, 0, sizeof(*ht)); > - mutex_init(&ht->mutex); > + mutex_init_with_key(&ht->mutex, key); > spin_lock_init(&ht->lock); > memcpy(&ht->p, params, sizeof(*params)); > > @@ -1087,7 +1088,7 @@ int rhashtable_init_noprof(struct rhashtable *ht, > > return 0; > } > -EXPORT_SYMBOL_GPL(rhashtable_init_noprof); > +EXPORT_SYMBOL_GPL(__rhashtable_init_noprof); > > /** > * rhltable_init - initialize a new hash list table > @@ -1098,15 +1099,17 @@ EXPORT_SYMBOL_GPL(rhashtable_init_noprof); > * > * See documentation for rhashtable_init. > */ > -int rhltable_init_noprof(struct rhltable *hlt, const struct rhashtable_params *params) > +int __rhltable_init_noprof(struct rhltable *hlt, > + const struct rhashtable_params *params, > + struct lock_class_key *key) > { > int err; > > - err = rhashtable_init_noprof(&hlt->ht, params); > + err = __rhashtable_init_noprof(&hlt->ht, params, key); > hlt->ht.rhlist = true; > return err; > } > -EXPORT_SYMBOL_GPL(rhltable_init_noprof); > +EXPORT_SYMBOL_GPL(__rhltable_init_noprof); > > static void rhashtable_free_one(struct rhashtable *ht, struct rhash_head *obj, > void (*free_fn)(void *ptr, void *arg), > > --- > base-commit: 6596a02b207886e9e00bb0161c7fd59fea53c081 > change-id: 20260427-work-rhashtable-lockdep-cb0356367073 -- Michal Hocko SUSE Labs