public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] rhashtable: Fix potential deadlock by moving schedule_work outside lock
@ 2024-11-28 12:16 Breno Leitao
  2024-11-29  5:47 ` Herbert Xu
                   ` (3 more replies)
  0 siblings, 4 replies; 25+ messages in thread
From: Breno Leitao @ 2024-11-28 12:16 UTC (permalink / raw)
  To: Andrew Morton, Thomas Graf, Herbert Xu, Tejun Heo, Hao Luo,
	Josh Don, Barret Rhoden
  Cc: netdev, linux-kernel, Breno Leitao

Move the hash table growth check and work scheduling outside the
rht lock to prevent a possible circular locking dependency.

The original implementation could trigger a lockdep warning due to
a potential deadlock scenario involving nested locks between
rhashtable bucket, rq lock, and dsq lock. By relocating the
growth check and work scheduling after releasing the rth lock, we break
this potential deadlock chain.

This change expands the flexibility of rhashtable by removing
restrictive locking that previously limited its use in scheduler
and workqueue contexts.

Import to say that this calls rht_grow_above_75(), which reads from
struct rhashtable without holding the lock, if this is a problem, we can
move the check to the lock, and schedule the workqueue after the lock.

Fixes: f0e1a0643a59 ("sched_ext: Implement BPF extensible scheduler class")
Suggested-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Breno Leitao <leitao@debian.org>
---
 lib/rhashtable.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/lib/rhashtable.c b/lib/rhashtable.c
index 6c902639728b767cc3ee42c61256d2e9618e6ce7..5a27ccd72db9a25d92d1ed2f8d519afcfc672afe 100644
--- a/lib/rhashtable.c
+++ b/lib/rhashtable.c
@@ -585,9 +585,6 @@ static struct bucket_table *rhashtable_insert_one(
 	rht_assign_locked(bkt, obj);
 
 	atomic_inc(&ht->nelems);
-	if (rht_grow_above_75(ht, tbl))
-		schedule_work(&ht->run_work);
-
 	return NULL;
 }
 
@@ -624,6 +621,9 @@ static void *rhashtable_try_insert(struct rhashtable *ht, const void *key,
 				data = ERR_CAST(new_tbl);
 
 			rht_unlock(tbl, bkt, flags);
+			if (rht_grow_above_75(ht, tbl))
+				schedule_work(&ht->run_work);
+
 		}
 	} while (!IS_ERR_OR_NULL(new_tbl));
 

---
base-commit: 0a31ca318eea4da46e5f495c79ccc4442c92f4dc
change-id: 20241128-scx_lockdep-3fa87553609d

Best regards,
-- 
Breno Leitao <leitao@debian.org>


^ permalink raw reply related	[flat|nested] 25+ messages in thread

end of thread, other threads:[~2025-01-17 13:21 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-28 12:16 [PATCH] rhashtable: Fix potential deadlock by moving schedule_work outside lock Breno Leitao
2024-11-29  5:47 ` Herbert Xu
2024-12-02 20:16   ` Breno Leitao
2024-12-03 20:16 ` Tejun Heo
2024-12-04  1:34   ` Herbert Xu
2024-12-12 12:33 ` Herbert Xu
2024-12-21  9:06   ` Herbert Xu
2025-01-02 10:15     ` Breno Leitao
2025-01-09  3:16       ` Michael Kelley
2025-01-09 10:15         ` Breno Leitao
2025-01-10  9:27           ` Herbert Xu
2025-01-10  9:49             ` Breno Leitao
2025-01-10 10:07               ` Herbert Xu
2025-01-10 14:46             ` Zaslonko Mikhail
2025-01-10 16:59             ` Michael Kelley
2025-01-10 17:24               ` [v2 PATCH] rhashtable: Fix rhashtable_try_insert test Herbert Xu
2025-01-10 18:22                 ` Michael Kelley
2025-01-14  3:15                   ` [v3 " Herbert Xu
2025-01-14 11:58                     ` Michael Kelley
2025-01-15 15:15                     ` Breno Leitao
2025-01-16  9:10                       ` Herbert Xu
2025-01-16 11:48                     ` Alexander Gordeev
2025-01-17 13:20                     ` Zaslonko Mikhail
2025-01-13 19:50 ` [PATCH] rhashtable: Fix potential deadlock by moving schedule_work outside lock Guenter Roeck
2025-01-14  3:23   ` Herbert Xu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox