net_sched: close the race between call_rcu() and cleanup_net()
Subsystems (5)
cover 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.
Cong Wang (13):
Revert "net_sched: hold netns refcnt for each action"
net_sched: introduce tcf_exts_get_net() and tcf_exts_put_net()
cls_basic: use tcf_exts_get_net() before call_rcu()
cls_bpf: use tcf_exts_get_net() before call_rcu()
cls_cgroup: use tcf_exts_get_net() before call_rcu()
cls_flow: use tcf_exts_get_net() before call_rcu()
cls_flower: use tcf_exts_get_net() before call_rcu()
cls_fw: use tcf_exts_get_net() before call_rcu()
cls_matchall: use tcf_exts_get_net() before call_rcu()
cls_route: use tcf_exts_get_net() before call_rcu()
cls_rsvp: use tcf_exts_get_net() before call_rcu()
cls_tcindex: use tcf_exts_get_net() before call_rcu()
cls_u32: use tcf_exts_get_net() before call_rcu()
include/net/act_api.h | 4 +--- include/net/pkt_cls.h | 24 ++++++++++++++++++++++++ net/sched/act_api.c | 2 -- net/sched/act_bpf.c | 2 +- net/sched/act_connmark.c | 2 +- net/sched/act_csum.c | 2 +- net/sched/act_gact.c | 2 +- net/sched/act_ife.c | 2 +- net/sched/act_ipt.c | 4 ++-- net/sched/act_mirred.c | 2 +- net/sched/act_nat.c | 2 +- net/sched/act_pedit.c | 2 +- net/sched/act_police.c | 2 +- net/sched/act_sample.c | 2 +- net/sched/act_simple.c | 2 +- net/sched/act_skbedit.c | 2 +- net/sched/act_skbmod.c | 2 +- net/sched/act_tunnel_key.c | 2 +- net/sched/act_vlan.c | 2 +- net/sched/cls_api.c | 1 + net/sched/cls_basic.c | 20 +++++++++++++++----- net/sched/cls_bpf.c | 7 ++++++- net/sched/cls_cgroup.c | 24 ++++++++++++++++++------ net/sched/cls_flow.c | 24 ++++++++++++++++++------ net/sched/cls_flower.c | 16 +++++++++++++--- net/sched/cls_fw.c | 17 ++++++++++++++--- net/sched/cls_matchall.c | 15 ++++++++++++--- net/sched/cls_route.c | 17 ++++++++++++++--- net/sched/cls_rsvp.h | 15 ++++++++++++--- net/sched/cls_tcindex.c | 33 ++++++++++++++++++++++++++------- net/sched/cls_u32.c | 8 +++++++- 31 files changed, 198 insertions(+), 63 deletions(-)
Signature · 1 line
2.13.0
PATCH 1/13 Revert "net_sched: hold netns refcnt for each action"
This reverts commit ceffcc5e254b450e6159f173e4538215cebf1b59. If we hold that refcnt, the netns can never be destroyed until all actions are destroyed by user, this breaks our netns design which we expect all actions are destroyed when we destroy the whole netns.
--- include/net/act_api.h | 4 +--- net/sched/act_api.c | 2 -- net/sched/act_bpf.c | 2 +- net/sched/act_connmark.c | 2 +- net/sched/act_csum.c | 2 +- net/sched/act_gact.c | 2 +- net/sched/act_ife.c | 2 +- net/sched/act_ipt.c | 4 ++-- net/sched/act_mirred.c | 2 +- net/sched/act_nat.c | 2 +- net/sched/act_pedit.c | 2 +- net/sched/act_police.c | 2 +- net/sched/act_sample.c | 2 +- net/sched/act_simple.c | 2 +- net/sched/act_skbedit.c | 2 +- net/sched/act_skbmod.c | 2 +- net/sched/act_tunnel_key.c | 2 +- net/sched/act_vlan.c | 2 +- 18 files changed, 18 insertions(+), 22 deletions(-)
include/net/act_api.h +1 -3
@@ -14,7 +14,6 @@struct tcf_idrinfo {spinlock_t lock;struct idr action_idr; -struct net *net; };struct tc_action_ops ;
@@ -106,7 +105,7 @@ struct tc_action_net {static inline int tc_action_net_init (struct tc_action_net *tn, -const struct tc_action_ops *ops,struct net *net) +const struct tc_action_ops *ops) {int err =0 ;
@@ -114,7 +113,6 @@ int tc_action_net_init(struct tc_action_net *tn,if (!tn->idrinfo)return -ENOMEM ; tn->ops = ops; - tn->idrinfo->net = net;spin_lock_init (&tn->idrinfo->lock);idr_init (&tn->idrinfo->action_idr);return err;
net/sched/act_api.c +0 -2
@@ -78,7 +78,6 @@ static void tcf_idr_remove(struct tcf_idrinfo *idrinfo, struct tc_action *p)spin_lock_bh (&idrinfo->lock);idr_remove_ext (&idrinfo->action_idr, p->tcfa_index);spin_unlock_bh (&idrinfo->lock); -put_net (idrinfo->net);gen_kill_estimator (&p->tcfa_rate_est);free_tcf (p); }
@@ -337,7 +336,6 @@ int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est, p->idrinfo = idrinfo; p->ops = ops;INIT_LIST_HEAD (&p->list); -get_net (idrinfo->net); *a = p;return 0 ; }
net/sched/act_bpf.c +1 -1
@@ -398,7 +398,7 @@ static __net_init int bpf_init_net(struct net *net) {struct tc_action_net *tn =net_generic (net, bpf_net_id); -return tc_action_net_init (tn, &act_bpf_ops, net); +return tc_action_net_init (tn, &act_bpf_ops); } static void __net_exit bpf_exit_net(struct net *net)
net/sched/act_connmark.c +1 -1
@@ -206,7 +206,7 @@ static __net_init int connmark_init_net(struct net *net) {struct tc_action_net *tn =net_generic (net, connmark_net_id); -return tc_action_net_init (tn, &act_connmark_ops, net); +return tc_action_net_init (tn, &act_connmark_ops); } static void __net_exit connmark_exit_net(struct net *net)
net/sched/act_csum.c +1 -1
@@ -626,7 +626,7 @@ static __net_init int csum_init_net(struct net *net) {struct tc_action_net *tn =net_generic (net, csum_net_id); -return tc_action_net_init (tn, &act_csum_ops, net); +return tc_action_net_init (tn, &act_csum_ops); } static void __net_exit csum_exit_net(struct net *net)
net/sched/act_gact.c +1 -1
@@ -232,7 +232,7 @@ static __net_init int gact_init_net(struct net *net) {struct tc_action_net *tn =net_generic (net, gact_net_id); -return tc_action_net_init (tn, &act_gact_ops, net); +return tc_action_net_init (tn, &act_gact_ops); } static void __net_exit gact_exit_net(struct net *net)
net/sched/act_ife.c +1 -1
@@ -818,7 +818,7 @@ static __net_init int ife_init_net(struct net *net) {struct tc_action_net *tn =net_generic (net, ife_net_id); -return tc_action_net_init (tn, &act_ife_ops, net); +return tc_action_net_init (tn, &act_ife_ops); } static void __net_exit ife_exit_net(struct net *net)
net/sched/act_ipt.c +2 -2
@@ -334,7 +334,7 @@ static __net_init int ipt_init_net(struct net *net) {struct tc_action_net *tn =net_generic (net, ipt_net_id); -return tc_action_net_init (tn, &act_ipt_ops, net); +return tc_action_net_init (tn, &act_ipt_ops); } static void __net_exit ipt_exit_net(struct net *net)
@@ -384,7 +384,7 @@ static __net_init int xt_init_net(struct net *net) {struct tc_action_net *tn =net_generic (net, xt_net_id); -return tc_action_net_init (tn, &act_xt_ops, net); +return tc_action_net_init (tn, &act_xt_ops); } static void __net_exit xt_exit_net(struct net *net)
net/sched/act_mirred.c +1 -1
@@ -343,7 +343,7 @@ static __net_init int mirred_init_net(struct net *net) {struct tc_action_net *tn =net_generic (net, mirred_net_id); -return tc_action_net_init (tn, &act_mirred_ops, net); +return tc_action_net_init (tn, &act_mirred_ops); } static void __net_exit mirred_exit_net(struct net *net)
net/sched/act_nat.c +1 -1
@@ -307,7 +307,7 @@ static __net_init int nat_init_net(struct net *net) {struct tc_action_net *tn =net_generic (net, nat_net_id); -return tc_action_net_init (tn, &act_nat_ops, net); +return tc_action_net_init (tn, &act_nat_ops); } static void __net_exit nat_exit_net(struct net *net)
net/sched/act_pedit.c +1 -1
@@ -450,7 +450,7 @@ static __net_init int pedit_init_net(struct net *net) {struct tc_action_net *tn =net_generic (net, pedit_net_id); -return tc_action_net_init (tn, &act_pedit_ops, net); +return tc_action_net_init (tn, &act_pedit_ops); } static void __net_exit pedit_exit_net(struct net *net)
net/sched/act_police.c +1 -1
@@ -331,7 +331,7 @@ static __net_init int police_init_net(struct net *net) {struct tc_action_net *tn =net_generic (net, police_net_id); -return tc_action_net_init (tn, &act_police_ops, net); +return tc_action_net_init (tn, &act_police_ops); } static void __net_exit police_exit_net(struct net *net)
net/sched/act_sample.c +1 -1
@@ -240,7 +240,7 @@ static __net_init int sample_init_net(struct net *net) {struct tc_action_net *tn =net_generic (net, sample_net_id); -return tc_action_net_init (tn, &act_sample_ops, net); +return tc_action_net_init (tn, &act_sample_ops); } static void __net_exit sample_exit_net(struct net *net)
net/sched/act_simple.c +1 -1
@@ -201,7 +201,7 @@ static __net_init int simp_init_net(struct net *net) {struct tc_action_net *tn =net_generic (net, simp_net_id); -return tc_action_net_init (tn, &act_simp_ops, net); +return tc_action_net_init (tn, &act_simp_ops); } static void __net_exit simp_exit_net(struct net *net)
net/sched/act_skbedit.c +1 -1
@@ -238,7 +238,7 @@ static __net_init int skbedit_init_net(struct net *net) {struct tc_action_net *tn =net_generic (net, skbedit_net_id); -return tc_action_net_init (tn, &act_skbedit_ops, net); +return tc_action_net_init (tn, &act_skbedit_ops); } static void __net_exit skbedit_exit_net(struct net *net)
net/sched/act_skbmod.c +1 -1
@@ -263,7 +263,7 @@ static __net_init int skbmod_init_net(struct net *net) {struct tc_action_net *tn =net_generic (net, skbmod_net_id); -return tc_action_net_init (tn, &act_skbmod_ops, net); +return tc_action_net_init (tn, &act_skbmod_ops); } static void __net_exit skbmod_exit_net(struct net *net)
net/sched/act_tunnel_key.c +1 -1
@@ -322,7 +322,7 @@ static __net_init int tunnel_key_init_net(struct net *net) {struct tc_action_net *tn =net_generic (net, tunnel_key_net_id); -return tc_action_net_init (tn, &act_tunnel_key_ops, net); +return tc_action_net_init (tn, &act_tunnel_key_ops); } static void __net_exit tunnel_key_exit_net(struct net *net)
net/sched/act_vlan.c +1 -1
@@ -269,7 +269,7 @@ static __net_init int vlan_init_net(struct net *net) {struct tc_action_net *tn =net_generic (net, vlan_net_id); -return tc_action_net_init (tn, &act_vlan_ops, net); +return tc_action_net_init (tn, &act_vlan_ops); } static void __net_exit vlan_exit_net(struct net *net)
Signature · 1 line
2.13.0
PATCH 2/13 net_sched: introduce tcf_exts_get_net() and tcf_exts_put_net()
In reply to Cong WangInstead of holding netns refcnt in tc actions, we can minimize the holding time by saving it in struct tcf_exts instead. This means we can just hold netns refcnt right before call_rcu() and release it after tcf_exts_destroy() is done.
However, because on netns cleanup path we call tcf_proto_destroy() too, obviously we can not hold netns for a zero refcnt, in this case we have to do cleanup synchronously. It is fine for RCU too, the caller cleanup_net() already waits for a grace period.
For other cases, refcnt is non-zero and we can safely grab it as normal and release it after we are done.
This patch provides two new API for each filter to use:tcf_exts_get_net() and tcf_exts_put_net(). And all filters now can use the following pattern:
void __destroy_filter() {
tcf_exts_destroy();
tcf_exts_put_net(); // <== release netns refcnt kfree();
}
void some_work() {
rtnl_lock();
__destroy_filter();
rtnl_unlock();
}
void some_rcu_callback() {
tcf_queue_work(some_work);
}
if (tcf_exts_get_net()) // <== hold netns refcnt
call_rcu(some_rcu_callback);
else
__destroy_filter();
--- include/net/pkt_cls.h | 24 ++++++++++++++++++++++++ net/sched/cls_api.c | 1 + 2 files changed, 25 insertions(+)
include/net/pkt_cls.h +24 -0
@@ -94,6 +94,7 @@ struct tcf_exts {__u32 type;/* for backward compat(TCA_OLD_COMPAT) */ int nr_actions;struct tc_action **actions; +struct net *net;#endif /* Map to export classifier specific extension TLV types to the * 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)#ifdef CONFIG_NET_CLS_ACT exts->type =0 ; exts->nr_actions =0 ; + exts->net =NULL ; exts->actions =kcalloc (TCA_ACT_MAX_PRIO ,sizeof (struct tc_action *),GFP_KERNEL );if (!exts->actions)
@@ -117,6 +119,28 @@ static inline int tcf_exts_init(struct tcf_exts *exts, int action, int police)return 0 ; } +/* Return false if the netns is being destroyed in cleanup_net(). Callers +* need to do cleanup synchronously in this case, otherwise may race with +* tc_action_net_exit(). Return true for other cases. +*/ +static inline bool tcf_exts_get_net (struct tcf_exts *exts) +{ +#ifdef CONFIG_NET_CLS_ACT + exts->net =maybe_get_net (exts->net); +return exts->net !=NULL ; +#else +return true; +#endif +} + +static inline void tcf_exts_put_net (struct tcf_exts *exts) +{ +#ifdef CONFIG_NET_CLS_ACT +if (exts->net) +put_net (exts->net); +#endif +} +static inline void tcf_exts_to_list (const struct tcf_exts *exts,struct list_head *actions) {
net/sched/cls_api.c +1 -0
@@ -927,6 +927,7 @@ int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb, exts->actions[i++] = act; exts->nr_actions = i; } + exts->net = net; }#else if ((exts->action && tb[exts->action]) ||
Signature · 1 line
2.13.0
PATCH 3/13 cls_basic: use tcf_exts_get_net() before call_rcu()
In reply to Cong WangHold netns refcnt before call_rcu() and release it after the tcf_exts_destroy() is done.
Note, on ->destroy() path we have to respect the return value of tcf_exts_get_net(), on other paths it should always return true, so we don't need to care.
--- net/sched/cls_basic.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)
net/sched/cls_basic.c +15 -5
@@ -85,16 +85,21 @@ static int basic_init(struct tcf_proto *tp)return 0 ; } +static void __basic_delete_filter (struct basic_filter *f) +{ +tcf_exts_destroy (&f->exts); +tcf_em_tree_destroy (&f->ematches); +tcf_exts_put_net (&f->exts); +kfree (f); +} +static void basic_delete_filter_work (struct work_struct *work) { struct basic_filter *f = container_of(work, struct basic_filter, work);rtnl_lock (); -tcf_exts_destroy (&f->exts); -tcf_em_tree_destroy (&f->ematches); +__basic_delete_filter (f);rtnl_unlock (); - -kfree (f); }static void basic_delete_filter (struct rcu_head *head)
@@ -113,7 +118,10 @@ static void basic_destroy(struct tcf_proto *tp)list_for_each_entry_safe (f, n, &head->flist, link) {list_del_rcu (&f->link);tcf_unbind_filter (tp, &f->res); -call_rcu (&f->rcu, basic_delete_filter); +if (tcf_exts_get_net (&f->exts)) +call_rcu (&f->rcu, basic_delete_filter); +else +__basic_delete_filter (f); }kfree_rcu (head, rcu); }
@@ -125,6 +133,7 @@ static int basic_delete(struct tcf_proto *tp, void *arg, bool *last)list_del_rcu (&f->link);tcf_unbind_filter (tp, &f->res); +tcf_exts_get_net (&f->exts);call_rcu (&f->rcu, basic_delete_filter); *last =list_empty (&head->flist);return 0 ;
@@ -219,6 +228,7 @@ static int basic_change(struct net *net, struct sk_buff *in_skb,if (fold) {list_replace_rcu (&fold->link, &fnew->link);tcf_unbind_filter (tp, &fold->res); +tcf_exts_get_net (&fold->exts);call_rcu (&fold->rcu, basic_delete_filter); }else {list_add_rcu (&fnew->link, &head->flist);
Signature · 1 line
2.13.0
PATCH 4/13 cls_bpf: use tcf_exts_get_net() before call_rcu()
In reply to Cong WangHold netns refcnt before call_rcu() and release it after the tcf_exts_destroy() is done.
Note, on ->destroy() path we have to respect the return value of tcf_exts_get_net(), on other paths it should always return true, so we don't need to care.
--- net/sched/cls_bpf.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
net/sched/cls_bpf.c +6 -1
@@ -249,6 +249,7 @@ static int cls_bpf_init(struct tcf_proto *tp)static void __cls_bpf_delete_prog (struct cls_bpf_prog *prog) {tcf_exts_destroy (&prog->exts); +tcf_exts_put_net (&prog->exts);if (cls_bpf_is_ebpf (prog))bpf_prog_put (prog->filter);
@@ -282,7 +283,10 @@ static void __cls_bpf_delete(struct tcf_proto *tp, struct cls_bpf_prog *prog)cls_bpf_stop_offload (tp, prog);list_del_rcu (&prog->link);tcf_unbind_filter (tp, &prog->res); -call_rcu (&prog->rcu, cls_bpf_delete_prog_rcu); +if (tcf_exts_get_net (&prog->exts)) +call_rcu (&prog->rcu, cls_bpf_delete_prog_rcu); +else +__cls_bpf_delete_prog (prog); }static int cls_bpf_delete (struct tcf_proto *tp,void *arg,bool *last)
@@ -516,6 +520,7 @@ static int cls_bpf_change(struct net *net, struct sk_buff *in_skb,if (oldprog) {list_replace_rcu (&oldprog->link, &prog->link);tcf_unbind_filter (tp, &oldprog->res); +tcf_exts_get_net (&oldprog->exts);call_rcu (&oldprog->rcu, cls_bpf_delete_prog_rcu); }else {list_add_rcu (&prog->link, &head->plist);
Signature · 1 line
2.13.0
PATCH 5/13 cls_cgroup: use tcf_exts_get_net() before call_rcu()
In reply to Cong WangHold netns refcnt before call_rcu() and release it after the tcf_exts_destroy() is done.
Note, on ->destroy() path we have to respect the return value of tcf_exts_get_net(), on other paths it should always return true, so we don't need to care.
--- net/sched/cls_cgroup.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-)
net/sched/cls_cgroup.c +18 -6
@@ -60,15 +60,21 @@ static const struct nla_policy cgroup_policy[TCA_CGROUP_MAX + 1] = { [TCA_CGROUP_EMATCHES] = { .type = NLA_NESTED }, }; +static void __cls_cgroup_destroy (struct cls_cgroup_head *head) +{ +tcf_exts_destroy (&head->exts); +tcf_em_tree_destroy (&head->ematches); +tcf_exts_put_net (&head->exts); +kfree (head); +} +static void cls_cgroup_destroy_work (struct work_struct *work) {struct cls_cgroup_head *head =container_of (work, struct cls_cgroup_head, work);rtnl_lock (); -tcf_exts_destroy (&head->exts); -tcf_em_tree_destroy (&head->ematches); -kfree (head); +__cls_cgroup_destroy (head);rtnl_unlock (); }
@@ -124,8 +130,10 @@ static int cls_cgroup_change(struct net *net, struct sk_buff *in_skb, goto errout;rcu_assign_pointer (tp->root, new); -if (head) +if (head) { +tcf_exts_get_net (&head->exts);call_rcu (&head->rcu, cls_cgroup_destroy_rcu); + }return 0 ; errout:tcf_exts_destroy (&new->exts);
@@ -138,8 +146,12 @@ static void cls_cgroup_destroy(struct tcf_proto *tp)struct cls_cgroup_head *head =rtnl_dereference (tp->root);/* Head can still be NULL due to cls_cgroup_init(). */ -if (head) -call_rcu (&head->rcu, cls_cgroup_destroy_rcu); +if (head) { +if (tcf_exts_get_net (&head->exts)) +call_rcu (&head->rcu, cls_cgroup_destroy_rcu); +else +__cls_cgroup_destroy (head); + } }static int cls_cgroup_delete (struct tcf_proto *tp,void *arg,bool *last)
Signature · 1 line
2.13.0
PATCH 6/13 cls_flow: use tcf_exts_get_net() before call_rcu()
In reply to Cong WangHold netns refcnt before call_rcu() and release it after the tcf_exts_destroy() is done.
Note, on ->destroy() path we have to respect the return value of tcf_exts_get_net(), on other paths it should always return true, so we don't need to care.
--- net/sched/cls_flow.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-)
net/sched/cls_flow.c +18 -6
@@ -372,15 +372,21 @@ static const struct nla_policy flow_policy[TCA_FLOW_MAX + 1] = { [TCA_FLOW_PERTURB] = { .type = NLA_U32 }, }; -static void flow_destroy_filter_work (struct work_struct *work) +static void __flow_destroy_filter (struct flow_filter *f) { - struct flow_filter *f = container_of(work, struct flow_filter, work); - -rtnl_lock ();del_timer_sync (&f->perturb_timer);tcf_exts_destroy (&f->exts);tcf_em_tree_destroy (&f->ematches); +tcf_exts_put_net (&f->exts);kfree (f); +} + +static void flow_destroy_filter_work (struct work_struct *work) +{ + struct flow_filter *f = container_of(work, struct flow_filter, work); + +rtnl_lock (); +__flow_destroy_filter (f);rtnl_unlock (); }
@@ -552,8 +558,10 @@ static int flow_change(struct net *net, struct sk_buff *in_skb, *arg = fnew; -if (fold) +if (fold) { +tcf_exts_get_net (&fold->exts);call_rcu (&fold->rcu, flow_destroy_filter); + }return 0 ; err2:
@@ -570,6 +578,7 @@ static int flow_delete(struct tcf_proto *tp, void *arg, bool *last)struct flow_filter *f = arg;list_del_rcu (&f->list); +tcf_exts_get_net (&f->exts);call_rcu (&f->rcu, flow_destroy_filter); *last =list_empty (&head->filters);return 0 ;
@@ -594,7 +603,10 @@ static void flow_destroy(struct tcf_proto *tp)list_for_each_entry_safe (f, next, &head->filters, list) {list_del_rcu (&f->list); -call_rcu (&f->rcu, flow_destroy_filter); +if (tcf_exts_get_net (&f->exts)) +call_rcu (&f->rcu, flow_destroy_filter); +else +__flow_destroy_filter (f); }kfree_rcu (head, rcu); }
Signature · 1 line
2.13.0
PATCH 7/13 cls_flower: use tcf_exts_get_net() before call_rcu()
In reply to Cong WangHold netns refcnt before call_rcu() and release it after the tcf_exts_destroy() is done.
Note, on ->destroy() path we have to respect the return value of tcf_exts_get_net(), on other paths it should always return true, so we don't need to care.
--- net/sched/cls_flower.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
net/sched/cls_flower.c +13 -3
@@ -218,13 +218,19 @@ static int fl_init(struct tcf_proto *tp)return 0 ; } +static void __fl_destroy_filter (struct cls_fl_filter *f) +{ +tcf_exts_destroy (&f->exts); +tcf_exts_put_net (&f->exts); +kfree (f); +} +static void fl_destroy_filter_work (struct work_struct *work) { struct cls_fl_filter *f = container_of(work, struct cls_fl_filter, work);rtnl_lock (); -tcf_exts_destroy (&f->exts); -kfree (f); +__fl_destroy_filter (f);rtnl_unlock (); }
@@ -318,7 +324,10 @@ static void __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f)if (!tc_skip_hw (f->flags))fl_hw_destroy_filter (tp, f);tcf_unbind_filter (tp, &f->res); -call_rcu (&f->rcu, fl_destroy_filter); +if (tcf_exts_get_net (&f->exts)) +call_rcu (&f->rcu, fl_destroy_filter); +else +__fl_destroy_filter (f); }static void fl_destroy_sleepable (struct work_struct *work)
@@ -988,6 +997,7 @@ static int fl_change(struct net *net, struct sk_buff *in_skb,idr_replace_ext (&head->handle_idr, fnew, fnew->handle);list_replace_rcu (&fold->list, &fnew->list);tcf_unbind_filter (tp, &fold->res); +tcf_exts_get_net (&fold->exts);call_rcu (&fold->rcu, fl_destroy_filter); }else {list_add_tail_rcu (&fnew->list, &head->filters);
Signature · 1 line
2.13.0
PATCH 8/13 cls_fw: use tcf_exts_get_net() before call_rcu()
In reply to Cong WangHold netns refcnt before call_rcu() and release it after the tcf_exts_destroy() is done.
Note, on ->destroy() path we have to respect the return value of tcf_exts_get_net(), on other paths it should always return true, so we don't need to care.
--- net/sched/cls_fw.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
net/sched/cls_fw.c +14 -3
@@ -122,13 +122,19 @@ static int fw_init(struct tcf_proto *tp)return 0 ; } +static void __fw_delete_filter (struct fw_filter *f) +{ +tcf_exts_destroy (&f->exts); +tcf_exts_put_net (&f->exts); +kfree (f); +} +static void fw_delete_filter_work (struct work_struct *work) { struct fw_filter *f = container_of(work, struct fw_filter, work);rtnl_lock (); -tcf_exts_destroy (&f->exts); -kfree (f); +__fw_delete_filter (f);rtnl_unlock (); }
@@ -154,7 +160,10 @@ static void fw_destroy(struct tcf_proto *tp)RCU_INIT_POINTER (head->ht[h],rtnl_dereference (f->next));tcf_unbind_filter (tp, &f->res); -call_rcu (&f->rcu, fw_delete_filter); +if (tcf_exts_get_net (&f->exts)) +call_rcu (&f->rcu, fw_delete_filter); +else +__fw_delete_filter (f); } }kfree_rcu (head, rcu);
@@ -179,6 +188,7 @@ static int fw_delete(struct tcf_proto *tp, void *arg, bool *last)if (pfp == f) {RCU_INIT_POINTER (*fp,rtnl_dereference (f->next));tcf_unbind_filter (tp, &f->res); +tcf_exts_get_net (&f->exts);call_rcu (&f->rcu, fw_delete_filter); ret =0 ;break ;
@@ -299,6 +309,7 @@ static int fw_change(struct net *net, struct sk_buff *in_skb,RCU_INIT_POINTER (fnew->next,rtnl_dereference (pfp->next));rcu_assign_pointer (*fp, fnew);tcf_unbind_filter (tp, &f->res); +tcf_exts_get_net (&f->exts);call_rcu (&f->rcu, fw_delete_filter); *arg = fnew;
Signature · 1 line
2.13.0
PATCH 9/13 cls_matchall: use tcf_exts_get_net() before call_rcu()
In reply to Cong WangHold netns refcnt before call_rcu() and release it after the tcf_exts_destroy() is done.
--- net/sched/cls_matchall.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
net/sched/cls_matchall.c +12 -3
@@ -44,13 +44,19 @@ static int mall_init(struct tcf_proto *tp)return 0 ; } +static void __mall_destroy (struct cls_mall_head *head) +{ +tcf_exts_destroy (&head->exts); +tcf_exts_put_net (&head->exts); +kfree (head); +} +static void mall_destroy_work (struct work_struct *work) { struct cls_mall_head *head = container_of(work, struct cls_mall_head, work);rtnl_lock (); -tcf_exts_destroy (&head->exts); -kfree (head); +__mall_destroy (head);rtnl_unlock (); }
@@ -109,7 +115,10 @@ static void mall_destroy(struct tcf_proto *tp)if (tc_should_offload (dev, head->flags))mall_destroy_hw_filter (tp, head, (unsigned long ) head); -call_rcu (&head->rcu, mall_destroy_rcu); +if (tcf_exts_get_net (&head->exts)) +call_rcu (&head->rcu, mall_destroy_rcu); +else +__mall_destroy (head); }static void *mall_get (struct tcf_proto *tp,u32 handle)
Signature · 1 line
2.13.0
PATCH 10/13 cls_route: use tcf_exts_get_net() before call_rcu()
In reply to Cong WangHold netns refcnt before call_rcu() and release it after the tcf_exts_destroy() is done.
Note, on ->destroy() path we have to respect the return value of tcf_exts_get_net(), on other paths it should always return true, so we don't need to care.
--- net/sched/cls_route.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
net/sched/cls_route.c +14 -3
@@ -257,13 +257,19 @@ static int route4_init(struct tcf_proto *tp)return 0 ; } +static void __route4_delete_filter (struct route4_filter *f) +{ +tcf_exts_destroy (&f->exts); +tcf_exts_put_net (&f->exts); +kfree (f); +} +static void route4_delete_filter_work (struct work_struct *work) { struct route4_filter *f = container_of(work, struct route4_filter, work);rtnl_lock (); -tcf_exts_destroy (&f->exts); -kfree (f); +__route4_delete_filter (f);rtnl_unlock (); }
@@ -297,7 +303,10 @@ static void route4_destroy(struct tcf_proto *tp) next =rtnl_dereference (f->next);RCU_INIT_POINTER (b->ht[h2], next);tcf_unbind_filter (tp, &f->res); -call_rcu (&f->rcu, route4_delete_filter); +if (tcf_exts_get_net (&f->exts)) +call_rcu (&f->rcu, route4_delete_filter); +else +__route4_delete_filter (f); } }RCU_INIT_POINTER (head->table[h1],NULL );
@@ -338,6 +347,7 @@ static int route4_delete(struct tcf_proto *tp, void *arg, bool *last)/* Delete it */ tcf_unbind_filter (tp, &f->res); +tcf_exts_get_net (&f->exts);call_rcu (&f->rcu, route4_delete_filter);/* Strip RTNL protected tree */
@@ -541,6 +551,7 @@ static int route4_change(struct net *net, struct sk_buff *in_skb, *arg = f;if (fold) {tcf_unbind_filter (tp, &fold->res); +tcf_exts_get_net (&fold->exts);call_rcu (&fold->rcu, route4_delete_filter); }return 0 ;
Signature · 1 line
2.13.0
PATCH 11/13 cls_rsvp: use tcf_exts_get_net() before call_rcu()
In reply to Cong WangHold netns refcnt before call_rcu() and release it after the tcf_exts_destroy() is done.
Note, on ->destroy() path we have to respect the return value of tcf_exts_get_net(), on other paths it should always return true, so we don't need to care.
--- net/sched/cls_rsvp.h | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
net/sched/cls_rsvp.h +12 -3
@@ -285,13 +285,19 @@ static int rsvp_init(struct tcf_proto *tp)return -ENOBUFS ; } +static void __rsvp_delete_filter (struct rsvp_filter *f) +{ +tcf_exts_destroy (&f->exts); +tcf_exts_put_net (&f->exts); +kfree (f); +} +static void rsvp_delete_filter_work (struct work_struct *work) { struct rsvp_filter *f = container_of(work, struct rsvp_filter, work);rtnl_lock (); -tcf_exts_destroy (&f->exts); -kfree (f); +__rsvp_delete_filter (f);rtnl_unlock (); }
@@ -310,7 +316,10 @@ static void rsvp_delete_filter(struct tcf_proto *tp, struct rsvp_filter *f)* grace period, since converted-to-rcu actions are relying on that * in cleanup() callback */ -call_rcu (&f->rcu, rsvp_delete_filter_rcu); +if (tcf_exts_get_net (&f->exts)) +call_rcu (&f->rcu, rsvp_delete_filter_rcu); +else +__rsvp_delete_filter (f); }static void rsvp_destroy (struct tcf_proto *tp)
Signature · 1 line
2.13.0
PATCH 12/13 cls_tcindex: use tcf_exts_get_net() before call_rcu()
In reply to Cong WangHold netns refcnt before call_rcu() and release it after the tcf_exts_destroy() is done.
Note, on ->destroy() path we have to respect the return value of tcf_exts_get_net(), on other paths it should always return true, so we don't need to care.
--- net/sched/cls_tcindex.c | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-)
net/sched/cls_tcindex.c +26 -7
@@ -139,13 +139,19 @@ static int tcindex_init(struct tcf_proto *tp)return 0 ; } +static void __tcindex_destroy_rexts (struct tcindex_filter_result *r) +{ +tcf_exts_destroy (&r->exts); +tcf_exts_put_net (&r->exts); +} +static void tcindex_destroy_rexts_work (struct work_struct *work) {struct tcindex_filter_result *r; r = container_of(work, struct tcindex_filter_result, work);rtnl_lock (); -tcf_exts_destroy (&r->exts); +__tcindex_destroy_rexts (r);rtnl_unlock (); }
@@ -158,14 +164,20 @@ static void tcindex_destroy_rexts(struct rcu_head *head)tcf_queue_work (&r->work); } +static void __tcindex_destroy_fexts (struct tcindex_filter *f) +{ +tcf_exts_destroy (&f->result.exts); +tcf_exts_put_net (&f->result.exts); +kfree (f); +} +static void tcindex_destroy_fexts_work (struct work_struct *work) { struct tcindex_filter *f = container_of(work, struct tcindex_filter, work);rtnl_lock (); -tcf_exts_destroy (&f->result.exts); -kfree (f); +__tcindex_destroy_fexts (f);rtnl_unlock (); }
@@ -210,10 +222,17 @@ static int tcindex_delete(struct tcf_proto *tp, void *arg, bool *last)* grace period, since converted-to-rcu actions are relying on that * in cleanup() callback */ -if (f) -call_rcu (&f->rcu, tcindex_destroy_fexts); -else -call_rcu (&r->rcu, tcindex_destroy_rexts); +if (f) { +if (tcf_exts_get_net (&f->result.exts)) +call_rcu (&f->rcu, tcindex_destroy_fexts); +else +__tcindex_destroy_fexts (f); + }else { +if (tcf_exts_get_net (&r->exts)) +call_rcu (&r->rcu, tcindex_destroy_rexts); +else +__tcindex_destroy_rexts (r); + } *last = false;return 0 ;
Signature · 1 line
2.13.0
PATCH 13/13 cls_u32: use tcf_exts_get_net() before call_rcu()
In reply to Cong WangHold netns refcnt before call_rcu() and release it after the tcf_exts_destroy() is done.
Note, on ->destroy() path we have to respect the return value of tcf_exts_get_net(), on other paths it should always return true, so we don't need to care.
--- net/sched/cls_u32.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
net/sched/cls_u32.c +7 -1
@@ -399,6 +399,7 @@ static int u32_destroy_key(struct tcf_proto *tp, struct tc_u_knode *n, bool free_pf) {tcf_exts_destroy (&n->exts); +tcf_exts_put_net (&n->exts);if (n->ht_down) n->ht_down->refcnt--;#ifdef CONFIG_CLS_U32_PERF
@@ -476,6 +477,7 @@ static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode *key)RCU_INIT_POINTER (*kp, key->next);tcf_unbind_filter (tp, &key->res); +tcf_exts_get_net (&key->exts);call_rcu (&key->rcu, u32_delete_key_freepf_rcu);return 0 ; }
@@ -588,7 +590,10 @@ static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht) rtnl_dereference(n->next));tcf_unbind_filter (tp, &n->res);u32_remove_hw_knode (tp, n->handle); -call_rcu (&n->rcu, u32_delete_key_freepf_rcu); +if (tcf_exts_get_net (&n->exts)) +call_rcu (&n->rcu, u32_delete_key_freepf_rcu); +else +u32_destroy_key (n->tp, n, true); } } }
@@ -949,6 +954,7 @@ static int u32_change(struct net *net, struct sk_buff *in_skb,u32_replace_knode (tp, tp_c, new);tcf_unbind_filter (tp, &n->res); +tcf_exts_get_net (&n->exts);call_rcu (&n->rcu, u32_delete_key_rcu);return 0 ; }
Signature · 1 line
2.13.0
David Miller
maintainerFrom: Cong Wang <[email protected]>
Date: Mon, 6 Nov 2017 13:47:17 -0800
Quoted 17 lines
This patchset tries to fix the race between
call_rcu()andcleanup_net()again. Without holding the netns refcnt thetc_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 totcf_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.
Quoted 6 lines
Fixes ddf97ccdd7cb ("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]>
I have to say this was a lot of churn so late in the release cycle.... but I ended up pulling anyways.
I cannot guarantee that I will be able to push this to Linus in time for 4.14-final.
Thanks.
Quoted 27 lines · David Miller
From: Cong Wang <[email protected]>
Date: Mon, 6 Nov 2017 13:47:17 -0800Quoted 17 lines
This patchset tries to fix the race between
call_rcu()andcleanup_net()again. Without holding the netns refcnt thetc_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 totcf_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.
Quoted 6 lines
Fixes ddf97ccdd7cb ("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]>
Quoted 6 lines
I have to say this was a lot of churn so late in the release cycle.... but I ended up pulling anyways.
I cannot guarantee that I will be able to push this to Linus in time for 4.14-final.
Yeah, it is kinda too late. I'd suggest you to just push the revert to Linus for 4.14 and defer the rest to 4.15, if it's doable...
Thanks!
Re: [Patch net 05/13] cls_cgroup: use tcf_exts_get_net() before call_rcu()
In reply to Cong WangCong Wang <[email protected]> writes:
[...]
Quoted 14 lines
static void
cls_cgroup_destroy_work(struct work_struct *work){net/sched/cls_cgroup.c +1
-3struct cls_cgroup_head *head =container_of (work, struct cls_cgroup_head, work);rtnl_lock (); -tcf_exts_destroy (&head->exts); -tcf_em_tree_destroy (&head->ematches); -kfree (head); +__cls_cgroup_destroy (head);rtnl_unlock (); }
net/sched/cls_cgroup.c static int cls_cgroup_change(struct net *net, struct sk_buff *in_skb, · line 135
Quoted 7 lines
net/sched/cls_cgroup.c +2
-1@@ -124,8 +130,10 @@ static int cls_cgroup_change(struct net *net, struct sk_buff *in_skb, goto errout;rcu_assign_pointer (tp->root, new); -if (head) +if (head) { +tcf_exts_get_net (&head->exts);call_rcu (&head->rcu, cls_cgroup_destroy_rcu);
In this case why do you not need to care about success/failure of tcf_exts_get_net() ?
Quoted 19 lines
net/sched/cls_cgroup.c +7
-2+ }return 0 ; errout:tcf_exts_destroy (&new->exts);@@ -138,8 +146,12 @@ static void cls_cgroup_destroy(struct tcf_proto *tp)struct cls_cgroup_head *head =rtnl_dereference (tp->root);/* Head can still be NULL due to cls_cgroup_init(). */ -if (head) -call_rcu (&head->rcu, cls_cgroup_destroy_rcu); +if (head) { +if (tcf_exts_get_net (&head->exts)) +call_rcu (&head->rcu, cls_cgroup_destroy_rcu); +else +__cls_cgroup_destroy (head); + } }static int cls_cgroup_delete (struct tcf_proto *tp,void *arg,bool *last)
Re: [Patch net 05/13] cls_cgroup: use tcf_exts_get_net() before call_rcu()
Quoted 4 lines · Roman Mashak
Quoted 3 lines · Cong Wang
net/sched/cls_cgroup.c +0
-0@@ -124,8 +130,10 @@ static int cls_cgroup_change(struct net *net, struct sk_buff *in_skb, goto errout;
Quoted 8 lines
net/sched/cls_cgroup.c static int cls_cgroup_change(struct net *net, struct sk_buff *in_skb, · line 135
Quoted 5 lines
net/sched/cls_cgroup.c +2
-1rcu_assign_pointer (tp->root, new); -if (head) +if (head) { +tcf_exts_get_net (&head->exts);call_rcu (&head->rcu, cls_cgroup_destroy_rcu);In this case why do you not need to care about success/failure of
tcf_exts_get_net()?
The answer is right in the changelog you omitted... Quoted below:
"Note, on ->destroy() path we have to respect the return value of tcf_exts_get_net(), on other paths it should always return true, so we don't need to care."