Skip to content

Commit dc8c5fa

Browse files
Don't walk route_ht while holding peertab->lock
The rhashtable walk functions acquire the table's internal lock with spin_lock, and the rhashtable rehash worker holds that lock with softirqs enabled. peertab->lock is acquired in softirq context (homa_route_get, homa_route_free), so holding it while walking the table can deadlock: lockdep reports "SOFTIRQ-safe -> SOFTIRQ-unsafe lock order detected" from homa_peer_free_net at netns exit, and homa_route_gc has the same problem. Add peertab->walk_mutex to serialize the two walks (and protect route_ht_iter), and acquire peertab->lock only around the removals. homa_route_gc now holds the RCU read lock from choosing victims until it has removed them, since another thread may remove a victim in between. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 5cb0ec2 commit dc8c5fa

2 files changed

Lines changed: 39 additions & 9 deletions

File tree

‎homa_peer.c‎

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ struct homa_peertab *homa_peer_alloc_peertab(void)
104104
return ERR_PTR(-ENOMEM);
105105

106106
spin_lock_init(&peertab->lock);
107+
mutex_init(&peertab->walk_mutex);
107108
err = rhashtable_init(&peertab->peer_ht, &peer_ht_params);
108109
if (err)
109110
goto error;
@@ -148,7 +149,7 @@ void homa_peer_free_net(struct homa_net *hnet)
148149
struct rhashtable_iter iter;
149150
struct homa_route *route;
150151

151-
spin_lock_bh(&peertab->lock);
152+
mutex_lock(&peertab->walk_mutex);
152153
rhashtable_walk_enter(&peertab->route_ht, &iter);
153154
rhashtable_walk_start(&iter);
154155
while (1) {
@@ -159,19 +160,23 @@ void homa_peer_free_net(struct homa_net *hnet)
159160
continue;
160161
if (route->key.hnet != hnet)
161162
continue;
163+
spin_lock_bh(&peertab->lock);
162164
if (rhashtable_remove_fast(&peertab->route_ht,
163165
&route->ht_linkage,
164166
route_ht_params) == 0) {
165167
homa_route_release(route);
166168
hnet->num_routes--;
167169
peertab->num_routes--;
168170
}
171+
spin_unlock_bh(&peertab->lock);
169172
}
170173
rhashtable_walk_stop(&iter);
171174
rhashtable_walk_exit(&iter);
175+
spin_lock_bh(&peertab->lock);
172176
WARN(hnet->num_routes != 0, "%s ended up with hnet->num_routes %d",
173177
__func__, hnet->num_routes);
174178
spin_unlock_bh(&peertab->lock);
179+
mutex_unlock(&peertab->walk_mutex);
175180

176181
/* homa_route_free uses hnet, which will be freed as soon as we
177182
* return, so wait for the callbacks queued above.
@@ -593,14 +598,23 @@ void homa_route_gc(struct homa_peertab *peertab)
593598
int num_victims;
594599
int i;
595600

596-
spin_lock_bh(&peertab->lock);
597-
if (peertab->num_routes < peertab->gc_threshold)
598-
goto done;
601+
if (READ_ONCE(peertab->num_routes) < peertab->gc_threshold)
602+
return;
603+
604+
/* The victims are chosen without holding peertab->lock (see the
605+
* documentation for walk_mutex), so another thread may remove one
606+
* of them before we lock; rhashtable_remove_fast will then fail.
607+
* The RCU read lock keeps such a route from being freed until we're
608+
* done with it.
609+
*/
610+
mutex_lock(&peertab->walk_mutex);
611+
rcu_read_lock();
599612
num_victims = homa_route_pick_victims(peertab, victims,
600613
EVICT_BATCH_SIZE);
601614
if (num_victims == 0)
602615
goto done;
603616

617+
spin_lock_bh(&peertab->lock);
604618
for (i = 0; i < num_victims; i++) {
605619
struct homa_route *route = victims[i];
606620

@@ -614,8 +628,10 @@ void homa_route_gc(struct homa_peertab *peertab)
614628
homa_route_release(route);
615629
}
616630
}
617-
done:
618631
spin_unlock_bh(&peertab->lock);
632+
done:
633+
rcu_read_unlock();
634+
mutex_unlock(&peertab->walk_mutex);
619635
}
620636

621637
/**
@@ -625,9 +641,14 @@ void homa_route_gc(struct homa_peertab *peertab)
625641
* @max_victims: Limit on how many victims to choose (and size of @victims
626642
* array).
627643
* Return: The number of routes stored in @victims; may be zero.
644+
*
645+
* The caller must hold @peertab->walk_mutex and the RCU read lock; the
646+
* victims may be removed by other threads, and are only safe to use until
647+
* the RCU read lock is released.
628648
*/
629649
int homa_route_pick_victims(struct homa_peertab *peertab,
630650
struct homa_route *victims[], int max_victims)
651+
__must_hold(peertab->walk_mutex)
631652
{
632653
struct homa_route *route;
633654
int num_victims = 0;
@@ -669,7 +690,7 @@ int homa_route_pick_victims(struct homa_peertab *peertab,
669690
if (idle < peertab->idle_jiffies_min)
670691
continue;
671692
if (idle < peertab->idle_jiffies_max &&
672-
route->key.hnet->num_routes <= peertab->net_max)
693+
READ_ONCE(route->key.hnet->num_routes) <= peertab->net_max)
673694
continue;
674695

675696
/* Sort the candidate into the existing list of victims. */
@@ -710,14 +731,14 @@ int homa_route_prefer_evict(struct homa_peertab *peertab,
710731
/* Prefer a route whose homa_net is over its limit; if both are either
711732
* over or under, then prefer the route with the longest idle time.
712733
*/
713-
if (route1->key.hnet->num_routes > peertab->net_max) {
714-
if (route2->key.hnet->num_routes <= peertab->net_max)
734+
if (READ_ONCE(route1->key.hnet->num_routes) > peertab->net_max) {
735+
if (READ_ONCE(route2->key.hnet->num_routes) <= peertab->net_max)
715736
return true;
716737
else
717738
return time_before(route1->access_jiffies,
718739
route2->access_jiffies);
719740
}
720-
if (route2->key.hnet->num_routes > peertab->net_max)
741+
if (READ_ONCE(route2->key.hnet->num_routes) > peertab->net_max)
721742
return false;
722743
else
723744
return time_before(route1->access_jiffies,

‎homa_peer.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ struct homa_peertab {
2929
*/
3030
spinlock_t lock;
3131

32+
/**
33+
* @walk_mutex: Held while scanning @route_ht (also protects
34+
* @route_ht_iter). @lock can't be held for this, because the
35+
* rhashtable walk functions acquire the table's internal lock
36+
* without disabling softirqs, while @lock is acquired at softirq
37+
* level. Must be acquired before @lock.
38+
*/
39+
struct mutex walk_mutex;
40+
3241
/** @peer_ht: Hash table that stores struct peers. */
3342
struct rhashtable peer_ht;
3443

0 commit comments

Comments
 (0)