Skip to content

Commit 9c29a2f

Browse files
dsaherndavem330
authored andcommitted
neighbor: Fix locking order for gc_list changes
Lock checker noted an inverted lock order between neigh_change_state (neighbor lock then table lock) and neigh_periodic_work (table lock and then neighbor lock) resulting in: [ 121.057652] ====================================================== [ 121.058740] WARNING: possible circular locking dependency detected [ 121.059861] 4.20.0-rc6+ #43 Not tainted [ 121.060546] ------------------------------------------------------ [ 121.061630] kworker/0:2/65 is trying to acquire lock: [ 121.062519] (____ptrval____) (&n->lock){++--}, at: neigh_periodic_work+0x237/0x324 [ 121.063894] [ 121.063894] but task is already holding lock: [ 121.064920] (____ptrval____) (&tbl->lock){+.-.}, at: neigh_periodic_work+0x194/0x324 [ 121.066274] [ 121.066274] which lock already depends on the new lock. [ 121.066274] [ 121.067693] [ 121.067693] the existing dependency chain (in reverse order) is: ... Fix by renaming neigh_change_state to neigh_update_gc_list, changing it to only manage whether an entry should be on the gc_list and taking locks in the same order as neigh_periodic_work. Invoke at the end of neigh_update only if diff between old or new states has the PERMANENT flag set. Fixes: 8cc196d ("neighbor: gc_list changes should be protected by table lock") Signed-off-by: David Ahern <dsahern@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent aeb3fec commit 9c29a2f

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

net/core/neighbour.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,30 +127,30 @@ static void neigh_mark_dead(struct neighbour *n)
127127
}
128128
}
129129

130-
static void neigh_change_state(struct neighbour *n, u8 new)
130+
static void neigh_update_gc_list(struct neighbour *n)
131131
{
132-
bool on_gc_list = !list_empty(&n->gc_list);
133-
bool new_is_perm = new & NUD_PERMANENT;
132+
bool on_gc_list, new_is_perm;
134133

135-
n->nud_state = new;
134+
write_lock_bh(&n->tbl->lock);
135+
write_lock(&n->lock);
136136

137137
/* remove from the gc list if new state is permanent;
138138
* add to the gc list if new state is not permanent
139139
*/
140+
new_is_perm = n->nud_state & NUD_PERMANENT;
141+
on_gc_list = !list_empty(&n->gc_list);
142+
140143
if (new_is_perm && on_gc_list) {
141-
write_lock_bh(&n->tbl->lock);
142144
list_del_init(&n->gc_list);
143-
write_unlock_bh(&n->tbl->lock);
144-
145145
atomic_dec(&n->tbl->gc_entries);
146146
} else if (!new_is_perm && !on_gc_list) {
147147
/* add entries to the tail; cleaning removes from the front */
148-
write_lock_bh(&n->tbl->lock);
149148
list_add_tail(&n->gc_list, &n->tbl->gc_list);
150-
write_unlock_bh(&n->tbl->lock);
151-
152149
atomic_inc(&n->tbl->gc_entries);
153150
}
151+
152+
write_unlock(&n->lock);
153+
write_unlock_bh(&n->tbl->lock);
154154
}
155155

156156
static bool neigh_del(struct neighbour *n, __u8 state, __u8 flags,
@@ -1220,7 +1220,7 @@ static int __neigh_update(struct neighbour *neigh, const u8 *lladdr,
12201220
neigh_del_timer(neigh);
12211221
if (old & NUD_CONNECTED)
12221222
neigh_suspect(neigh);
1223-
neigh_change_state(neigh, new);
1223+
neigh->nud_state = new;
12241224
err = 0;
12251225
notify = old & NUD_VALID;
12261226
if ((old & (NUD_INCOMPLETE | NUD_PROBE)) &&
@@ -1299,7 +1299,7 @@ static int __neigh_update(struct neighbour *neigh, const u8 *lladdr,
12991299
((new & NUD_REACHABLE) ?
13001300
neigh->parms->reachable_time :
13011301
0)));
1302-
neigh_change_state(neigh, new);
1302+
neigh->nud_state = new;
13031303
notify = 1;
13041304
}
13051305

@@ -1360,6 +1360,9 @@ static int __neigh_update(struct neighbour *neigh, const u8 *lladdr,
13601360
neigh_update_is_router(neigh, flags, &notify);
13611361
write_unlock_bh(&neigh->lock);
13621362

1363+
if ((new ^ old) & NUD_PERMANENT)
1364+
neigh_update_gc_list(neigh);
1365+
13631366
if (notify)
13641367
neigh_update_notify(neigh, nlmsg_pid);
13651368

0 commit comments

Comments
 (0)