Skip to content

Commit 6bd00b8

Browse files
congwangdavem330
authored andcommitted
act_mirred: fix a race condition on mirred_list
After commit 1ce8772 ("net: sched: make cls_u32 lockless") we began to release tc actions in a RCU callback. However, mirred action relies on RTNL lock to protect the global mirred_list, therefore we could have a race condition between RCU callback and netdevice event, which caused a list corruption as reported by Vinson. Instead of relying on RTNL lock, introduce a spinlock to protect this list. Note, in non-bind case, it is still called with RTNL lock, therefore should disable BH too. Reported-by: Vinson Lee <vlee@twopensource.com> Cc: John Fastabend <john.fastabend@gmail.com> Cc: Jamal Hadi Salim <jhs@mojatatu.com> Signed-off-by: Cong Wang <cwang@twopensource.com> Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com> Acked-by: Jamal Hadi Salim <jhs@mojatatu.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent fec31ff commit 6bd00b8

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

‎net/sched/act_mirred.c‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,17 @@
3131

3232
#define MIRRED_TAB_MASK 7
3333
static LIST_HEAD(mirred_list);
34+
static DEFINE_SPINLOCK(mirred_list_lock);
3435

3536
static void tcf_mirred_release(struct tc_action *a, int bind)
3637
{
3738
struct tcf_mirred *m = to_mirred(a);
3839
struct net_device *dev = rcu_dereference_protected(m->tcfm_dev, 1);
3940

41+
/* We could be called either in a RCU callback or with RTNL lock held. */
42+
spin_lock_bh(&mirred_list_lock);
4043
list_del(&m->tcfm_list);
44+
spin_unlock_bh(&mirred_list_lock);
4145
if (dev)
4246
dev_put(dev);
4347
}
@@ -123,7 +127,9 @@ static int tcf_mirred_init(struct net *net, struct nlattr *nla,
123127
}
124128

125129
if (ret == ACT_P_CREATED) {
130+
spin_lock_bh(&mirred_list_lock);
126131
list_add(&m->tcfm_list, &mirred_list);
132+
spin_unlock_bh(&mirred_list_lock);
127133
tcf_hash_insert(a);
128134
}
129135

@@ -221,7 +227,8 @@ static int mirred_device_event(struct notifier_block *unused,
221227
struct tcf_mirred *m;
222228

223229
ASSERT_RTNL();
224-
if (event == NETDEV_UNREGISTER)
230+
if (event == NETDEV_UNREGISTER) {
231+
spin_lock_bh(&mirred_list_lock);
225232
list_for_each_entry(m, &mirred_list, tcfm_list) {
226233
if (rcu_access_pointer(m->tcfm_dev) == dev) {
227234
dev_put(dev);
@@ -231,6 +238,8 @@ static int mirred_device_event(struct notifier_block *unused,
231238
RCU_INIT_POINTER(m->tcfm_dev, NULL);
232239
}
233240
}
241+
spin_unlock_bh(&mirred_list_lock);
242+
}
234243

235244
return NOTIFY_DONE;
236245
}

0 commit comments

Comments
 (0)