rhashtable: give each instance its own lockdep class
Subsystems (5)
Christian Brauner
maintainerPATCH 1/1 rhashtable: give each instance its own lockdep class
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_reclaimThe 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.
--- include/linux/rhashtable-types.h | 22 ++++++++++++++++++---- lib/rhashtable.c | 17 ++++++++++------- 2 files changed, 28 insertions(+), 11 deletions(-)
include/linux/rhashtable-types.h +18 -4
@@ -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 */
lib/rhashtable.c +10 -7
@@ -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
Herbert Xu
maintainerQuoted 46 lines · Christian Brauner
syzbot reported a possible circular locking dependency between
&ht->mutexandfs_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_reclaimThe two halves of the splat refer to two different events on
&ht->mutex.The kswapd0 path is unambiguous:
shmem_evict_inodeatmm/shmem.c:1429callssimple_xattrs_free(), which callsrhashtable_free_and_destroy()on the per-inodesimple_xattrsrhashtable being torn down with the inode.The previously-recorded
ht->mutex->fs_reclaimedge comes fromrht_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 aboverht_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_xattrsht that is now being destroyed, the predicted deadlock cannot occur:rhashtable_free_and_destroy()doescancel_work_sync(&ht->run_work)before takinght->mutex, so the deferred worker cannot be running on the instance being torn down. If the recording was on a different rhashtable instance, the twoht->mutexacquisitions 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 inrhashtable_init_noprof(), so everyht->mutexin 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-visiblerhashtable_init_noprof()/rhltable_init_noprof()identifiers become macros that declare a per-call-site staticlock_class_key.
Quoted 7 lines
Reported-by [email protected]Signed-off-by Christian Brauner <[email protected]>--- include/linux/rhashtable-types.h | 22 ++++++++++++++++++---- lib/rhashtable.c | 17 ++++++++++------- 2 files changed, 28 insertions(+), 11 deletions(-)
Thanks for the patch.
But could you please try this patch and see if it also fixes your problem?
https://patchwork.kernel.org/project/linux-crypto/patch/[email protected]/
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Christian Brauner
maintainerQuoted 55 lines · Herbert Xu
Quoted 46 lines · Christian Brauner
syzbot reported a possible circular locking dependency between
&ht->mutexandfs_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_reclaimThe two halves of the splat refer to two different events on
&ht->mutex.The kswapd0 path is unambiguous:
shmem_evict_inodeatmm/shmem.c:1429callssimple_xattrs_free(), which callsrhashtable_free_and_destroy()on the per-inodesimple_xattrsrhashtable being torn down with the inode.The previously-recorded
ht->mutex->fs_reclaimedge comes fromrht_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 aboverht_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_xattrsht that is now being destroyed, the predicted deadlock cannot occur:rhashtable_free_and_destroy()doescancel_work_sync(&ht->run_work)before takinght->mutex, so the deferred worker cannot be running on the instance being torn down. If the recording was on a different rhashtable instance, the twoht->mutexacquisitions 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 inrhashtable_init_noprof(), so everyht->mutexin 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-visiblerhashtable_init_noprof()/rhltable_init_noprof()identifiers become macros that declare a per-call-site staticlock_class_key.Quoted 7 lines
Reported-by [email protected]Signed-off-by Christian Brauner <[email protected]>--- include/linux/rhashtable-types.h | 22 ++++++++++++++++++---- lib/rhashtable.c | 17 ++++++++++------- 2 files changed, 28 insertions(+), 11 deletions(-)
Quoted 6 lines
Thanks for the patch.
But could you please try this patch and see if it also fixes your problem?
https://patchwork.kernel.org/project/linux-crypto/patch/[email protected]/
Possibly, I don't have a way to easily reproduce this though. Imho, the right thing would be to have both: actual useful keyed lockdep annotation and - if safe - dropping the mutex.
Quoted 9 lines · Christian Brauner
Quoted 5 lines
But could you please try this patch and see if it also fixes your problem?
https://patchwork.kernel.org/project/linux-crypto/patch/[email protected]/
Possibly, I don't have a way to easily reproduce this though. Imho, the right thing would be to have both: actual useful keyed lockdep annotation and - if safe - dropping the mutex.
Agreed -- the two changes are orthogonal: yours fixes the lockdep class collapse, mine removes a mutex acquisition that has been unnecessary since cancel_work_sync() was added in front of it.
On the safety of dropping the mutex: rhashtable_free_and_destroy()'s documented contract already requires the caller to ensure no concurrent operations, and cancel_work_sync(&ht->run_work) at the top of the function quiesces the only in-library writer (the rehash worker). After that, the tables are owned exclusively by this function. The walks I switch from rht_dereference() to rcu_dereference_raw() were already correct; the lockdep annotation was just asking for a lock that no longer needs to be held.
I checked all in-tree callers of rhashtable_free_and_destroy() (and of rhashtable_destroy(), which inlines the same teardown) for correctness against the contract; none rely on the mutex for serialization with anything other than the rehash worker.
I have no objection to either patch order. Happy to rebase mine on top of yours, or for it to go in independently via Herbert's tree once yours is merged.
Signature · 2 lines
Thanks, Mikhail
Andrew Morton
maintainerQuoted 41 lines · On Mon, 27 Apr 2026 13:09:57 +0200 Christian Brauner <[email protected]> wrote:
syzbot reported a possible circular locking dependency between
&ht->mutexandfs_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_reclaimThe two halves of the splat refer to two different events on
&ht->mutex.The kswapd0 path is unambiguous:
shmem_evict_inodeatmm/shmem.c:1429callssimple_xattrs_free(), which callsrhashtable_free_and_destroy()on the per-inodesimple_xattrsrhashtable being torn down with the inode.The previously-recorded
ht->mutex->fs_reclaimedge comes fromrht_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 aboverht_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_xattrsht that is now being destroyed, the predicted deadlock cannot occur:rhashtable_free_and_destroy()doescancel_work_sync(&ht->run_work)before takinght->mutex, so the deferred worker cannot be running on the instance being torn down. If the recording was on a different rhashtable instance, the twoht->mutexacquisitions 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 inrhashtable_init_noprof(), so everyht->mutexin the kernel shares one static lockdep class. Lockdep matches by class, not by instance, and collapses all of these into one node.
Quoted 7 lines
Lift the lockdep key out of
rhashtable_init_noprof()and into the caller. The user-visiblerhashtable_init_noprof()/rhltable_init_noprof()identifiers become macros that declare a per-call-site staticlock_class_key.Reported-by [email protected]
Thanks.
Is this related to https://lore.kernel.org/[email protected]
Are we able to identify a Fixes: target?
In the above-linked thread Herbert had
Herbert Xu
maintainerQuoted 7 lines · Andrew Morton
Is this related to https://lore.kernel.org/[email protected]
Are we able to identify a Fixes: target?
In the above-linked thread Herbert had
Fixes c6307674ed82 ("mm: kvmalloc: add non-blocking support for vmalloc")
I think they're two separate issues. The addition of non-blocking support triggered the vfree while atomic issue, while the lockdep splat is simply due to the use of rhashtable in fs/core.
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Quoted 41 lines · Christian Brauner
syzbot reported a possible circular locking dependency between
&ht->mutexandfs_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_reclaimThe two halves of the splat refer to two different events on
&ht->mutex.The kswapd0 path is unambiguous:
shmem_evict_inodeatmm/shmem.c:1429callssimple_xattrs_free(), which callsrhashtable_free_and_destroy()on the per-inodesimple_xattrsrhashtable being torn down with the inode.The previously-recorded
ht->mutex->fs_reclaimedge comes fromrht_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 aboverht_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_xattrsht that is now being destroyed, the predicted deadlock cannot occur:rhashtable_free_and_destroy()doescancel_work_sync(&ht->run_work)before takinght->mutex, so the deferred worker cannot be running on the instance being torn down. If the recording was on a different rhashtable instance, the twoht->mutexacquisitions 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 inrhashtable_init_noprof(), so everyht->mutexin the kernel shares one static lockdep class. Lockdep matches by class, not by instance, and collapses all of these into one node.
Quoted 8 lines
Lift the lockdep key out of
rhashtable_init_noprof()and into the caller. The user-visiblerhashtable_init_noprof()/rhltable_init_noprof()identifiers become macros that declare a per-call-site staticlock_class_key.Reported-by [email protected]Signed-off-by Christian Brauner <[email protected]>
Thanks!
Quoted 99 lines
--- include/linux/rhashtable-types.h | 22 ++++++++++++++++++---- lib/rhashtable.c | 17 ++++++++++------- 2 files changed, 28 insertions(+), 11 deletions(-)include/linux/rhashtable-types.h +18
-4@@ -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 */ lib/rhashtable.c +10
-8@@ -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
Signature · 2 lines
Michal Hocko SUSE Labs
kernel test robot
build botkernel test robot noticed the following build warnings:
Hi Christian,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 6596a02b207886e9e00bb0161c7fd59fea53c081]
url: https://github.com/intel-lab-lkp/linux/commits/Christian-Brauner/rhashtable-give-each-instance-its-own-lockdep-class/20260428-182539 base: 6596a02b207886e9e00bb0161c7fd59fea53c081 patch link: https://lore.kernel.org/r/20260427-work-rhashtable-lockdep-v1-1-f69e8bd91cb2%40kernel.org patch subject: [PATCH] rhashtable: give each instance its own lockdep class config: x86_64-rhel-9.4 (https://download.01.org/0day-ci/archive/20260429/[email protected]/config) compiler: gcc-14 (Debian 14.2.0-19) 14.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260429/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> Warning: lib/rhashtable.c:1030 function parameter 'key' not described in '__rhashtable_init' >> Warning: lib/rhashtable.c:1030 expecting prototype for rhashtable_init(). Prototype was for __rhashtable_init() instead >> Warning: lib/rhashtable.c:1104 function parameter 'key' not described in '__rhltable_init' >> Warning: lib/rhashtable.c:1104 expecting prototype for rhltable_init(). Prototype was for __rhltable_init() instead
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki