Skip to content

Commit 623859a

Browse files
committed
Merge branch 'net-sched-race-fix'
Cong Wang says: ==================== net_sched: close the race between call_rcu() and cleanup_net() This patchset tries to fix the race between call_rcu() and cleanup_net() again. Without holding the netns refcnt the tc_action_net_exit() in netns workqueue could be called before filter destroy works in tc filter workqueue. This patchset moves the netns refcnt from tc actions to tcf_exts, without breaking per-netns tc actions. Patch 1 reverts the previous fix, patch 2 introduces two new API's to help to address the bug and the rest patches switch to the new API's. Please see each patch for details. I was not able to reproduce this bug, but now after adding some delay in filter destroy work I manage to trigger the crash. After this patchset, the crash is not reproducible any more and the debugging printk's show the order is expected too. ==================== Fixes: ddf97cc ("net_sched: add network namespace support for tc actions") Reported-by: Lucas Bates <[email protected]> Cc: Lucas Bates <[email protected]> Cc: Jamal Hadi Salim <[email protected]> Cc: Jiri Pirko <[email protected]> Signed-off-by: Cong Wang <[email protected]> Signed-off-by: David S. Miller <[email protected]>
2 parents 8f56246 + 35c55fc commit 623859a

31 files changed

+198
-63
lines changed

include/net/act_api.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
struct tcf_idrinfo {
1515
spinlock_t lock;
1616
struct idr action_idr;
17-
struct net *net;
1817
};
1918

2019
struct tc_action_ops;
@@ -106,15 +105,14 @@ struct tc_action_net {
106105

107106
static inline
108107
int tc_action_net_init(struct tc_action_net *tn,
109-
const struct tc_action_ops *ops, struct net *net)
108+
const struct tc_action_ops *ops)
110109
{
111110
int err = 0;
112111

113112
tn->idrinfo = kmalloc(sizeof(*tn->idrinfo), GFP_KERNEL);
114113
if (!tn->idrinfo)
115114
return -ENOMEM;
116115
tn->ops = ops;
117-
tn->idrinfo->net = net;
118116
spin_lock_init(&tn->idrinfo->lock);
119117
idr_init(&tn->idrinfo->action_idr);
120118
return err;

include/net/pkt_cls.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ struct tcf_exts {
9494
__u32 type; /* for backward compat(TCA_OLD_COMPAT) */
9595
int nr_actions;
9696
struct tc_action **actions;
97+
struct net *net;
9798
#endif
9899
/* Map to export classifier specific extension TLV types to the
99100
* generic extensions API. Unsupported extensions must be set to 0.
@@ -107,6 +108,7 @@ static inline int tcf_exts_init(struct tcf_exts *exts, int action, int police)
107108
#ifdef CONFIG_NET_CLS_ACT
108109
exts->type = 0;
109110
exts->nr_actions = 0;
111+
exts->net = NULL;
110112
exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *),
111113
GFP_KERNEL);
112114
if (!exts->actions)
@@ -117,6 +119,28 @@ static inline int tcf_exts_init(struct tcf_exts *exts, int action, int police)
117119
return 0;
118120
}
119121

122+
/* Return false if the netns is being destroyed in cleanup_net(). Callers
123+
* need to do cleanup synchronously in this case, otherwise may race with
124+
* tc_action_net_exit(). Return true for other cases.
125+
*/
126+
static inline bool tcf_exts_get_net(struct tcf_exts *exts)
127+
{
128+
#ifdef CONFIG_NET_CLS_ACT
129+
exts->net = maybe_get_net(exts->net);
130+
return exts->net != NULL;
131+
#else
132+
return true;
133+
#endif
134+
}
135+
136+
static inline void tcf_exts_put_net(struct tcf_exts *exts)
137+
{
138+
#ifdef CONFIG_NET_CLS_ACT
139+
if (exts->net)
140+
put_net(exts->net);
141+
#endif
142+
}
143+
120144
static inline void tcf_exts_to_list(const struct tcf_exts *exts,
121145
struct list_head *actions)
122146
{

net/sched/act_api.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ static void tcf_idr_remove(struct tcf_idrinfo *idrinfo, struct tc_action *p)
7878
spin_lock_bh(&idrinfo->lock);
7979
idr_remove_ext(&idrinfo->action_idr, p->tcfa_index);
8080
spin_unlock_bh(&idrinfo->lock);
81-
put_net(idrinfo->net);
8281
gen_kill_estimator(&p->tcfa_rate_est);
8382
free_tcf(p);
8483
}
@@ -337,7 +336,6 @@ int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
337336
p->idrinfo = idrinfo;
338337
p->ops = ops;
339338
INIT_LIST_HEAD(&p->list);
340-
get_net(idrinfo->net);
341339
*a = p;
342340
return 0;
343341
}

net/sched/act_bpf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ static __net_init int bpf_init_net(struct net *net)
398398
{
399399
struct tc_action_net *tn = net_generic(net, bpf_net_id);
400400

401-
return tc_action_net_init(tn, &act_bpf_ops, net);
401+
return tc_action_net_init(tn, &act_bpf_ops);
402402
}
403403

404404
static void __net_exit bpf_exit_net(struct net *net)

net/sched/act_connmark.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ static __net_init int connmark_init_net(struct net *net)
206206
{
207207
struct tc_action_net *tn = net_generic(net, connmark_net_id);
208208

209-
return tc_action_net_init(tn, &act_connmark_ops, net);
209+
return tc_action_net_init(tn, &act_connmark_ops);
210210
}
211211

212212
static void __net_exit connmark_exit_net(struct net *net)

net/sched/act_csum.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ static __net_init int csum_init_net(struct net *net)
626626
{
627627
struct tc_action_net *tn = net_generic(net, csum_net_id);
628628

629-
return tc_action_net_init(tn, &act_csum_ops, net);
629+
return tc_action_net_init(tn, &act_csum_ops);
630630
}
631631

632632
static void __net_exit csum_exit_net(struct net *net)

net/sched/act_gact.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ static __net_init int gact_init_net(struct net *net)
232232
{
233233
struct tc_action_net *tn = net_generic(net, gact_net_id);
234234

235-
return tc_action_net_init(tn, &act_gact_ops, net);
235+
return tc_action_net_init(tn, &act_gact_ops);
236236
}
237237

238238
static void __net_exit gact_exit_net(struct net *net)

net/sched/act_ife.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ static __net_init int ife_init_net(struct net *net)
818818
{
819819
struct tc_action_net *tn = net_generic(net, ife_net_id);
820820

821-
return tc_action_net_init(tn, &act_ife_ops, net);
821+
return tc_action_net_init(tn, &act_ife_ops);
822822
}
823823

824824
static void __net_exit ife_exit_net(struct net *net)

net/sched/act_ipt.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ static __net_init int ipt_init_net(struct net *net)
334334
{
335335
struct tc_action_net *tn = net_generic(net, ipt_net_id);
336336

337-
return tc_action_net_init(tn, &act_ipt_ops, net);
337+
return tc_action_net_init(tn, &act_ipt_ops);
338338
}
339339

340340
static void __net_exit ipt_exit_net(struct net *net)
@@ -384,7 +384,7 @@ static __net_init int xt_init_net(struct net *net)
384384
{
385385
struct tc_action_net *tn = net_generic(net, xt_net_id);
386386

387-
return tc_action_net_init(tn, &act_xt_ops, net);
387+
return tc_action_net_init(tn, &act_xt_ops);
388388
}
389389

390390
static void __net_exit xt_exit_net(struct net *net)

net/sched/act_mirred.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ static __net_init int mirred_init_net(struct net *net)
343343
{
344344
struct tc_action_net *tn = net_generic(net, mirred_net_id);
345345

346-
return tc_action_net_init(tn, &act_mirred_ops, net);
346+
return tc_action_net_init(tn, &act_mirred_ops);
347347
}
348348

349349
static void __net_exit mirred_exit_net(struct net *net)

0 commit comments

Comments
 (0)