Commit e49d8c22 authored by Cong Wang's avatar Cong Wang Committed by David S. Miller

net_sched: defer tcf_idr_insert() in tcf_action_init_1()

All TC actions call tcf_idr_insert() for new action at the end
of their ->init(), so we can actually move it to a central place
in tcf_action_init_1().

And once the action is inserted into the global IDR, other parallel
process could free it immediately as its refcnt is still 1, so we can
not fail after this, we need to move it after the goto action
validation to avoid handling the failure case after insertion.

This is found during code review, is not directly triggered by syzbot.
And this prepares for the next patch.

Cc: Vlad Buslov <vladbu@mellanox.com>
Cc: Jamal Hadi Salim <jhs@mojatatu.com>
Cc: Jiri Pirko <jiri@resnulli.us>
Signed-off-by: default avatarCong Wang <xiyou.wangcong@gmail.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 7241c5a6
...@@ -166,8 +166,6 @@ int tcf_idr_create_from_flags(struct tc_action_net *tn, u32 index, ...@@ -166,8 +166,6 @@ int tcf_idr_create_from_flags(struct tc_action_net *tn, u32 index,
struct nlattr *est, struct tc_action **a, struct nlattr *est, struct tc_action **a,
const struct tc_action_ops *ops, int bind, const struct tc_action_ops *ops, int bind,
u32 flags); u32 flags);
void tcf_idr_insert(struct tc_action_net *tn, struct tc_action *a);
void tcf_idr_cleanup(struct tc_action_net *tn, u32 index); void tcf_idr_cleanup(struct tc_action_net *tn, u32 index);
int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index, int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index,
struct tc_action **a, int bind); struct tc_action **a, int bind);
......
...@@ -467,17 +467,6 @@ int tcf_idr_create_from_flags(struct tc_action_net *tn, u32 index, ...@@ -467,17 +467,6 @@ int tcf_idr_create_from_flags(struct tc_action_net *tn, u32 index,
} }
EXPORT_SYMBOL(tcf_idr_create_from_flags); EXPORT_SYMBOL(tcf_idr_create_from_flags);
void tcf_idr_insert(struct tc_action_net *tn, struct tc_action *a)
{
struct tcf_idrinfo *idrinfo = tn->idrinfo;
mutex_lock(&idrinfo->lock);
/* Replace ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
WARN_ON(!IS_ERR(idr_replace(&idrinfo->action_idr, a, a->tcfa_index)));
mutex_unlock(&idrinfo->lock);
}
EXPORT_SYMBOL(tcf_idr_insert);
/* Cleanup idr index that was allocated but not initialized. */ /* Cleanup idr index that was allocated but not initialized. */
void tcf_idr_cleanup(struct tc_action_net *tn, u32 index) void tcf_idr_cleanup(struct tc_action_net *tn, u32 index)
...@@ -902,6 +891,16 @@ static const struct nla_policy tcf_action_policy[TCA_ACT_MAX + 1] = { ...@@ -902,6 +891,16 @@ static const struct nla_policy tcf_action_policy[TCA_ACT_MAX + 1] = {
[TCA_ACT_HW_STATS] = NLA_POLICY_BITFIELD32(TCA_ACT_HW_STATS_ANY), [TCA_ACT_HW_STATS] = NLA_POLICY_BITFIELD32(TCA_ACT_HW_STATS_ANY),
}; };
static void tcf_idr_insert(struct tc_action *a)
{
struct tcf_idrinfo *idrinfo = a->idrinfo;
mutex_lock(&idrinfo->lock);
/* Replace ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
WARN_ON(!IS_ERR(idr_replace(&idrinfo->action_idr, a, a->tcfa_index)));
mutex_unlock(&idrinfo->lock);
}
struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp, struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
struct nlattr *nla, struct nlattr *est, struct nlattr *nla, struct nlattr *est,
char *name, int ovr, int bind, char *name, int ovr, int bind,
...@@ -989,6 +988,16 @@ struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp, ...@@ -989,6 +988,16 @@ struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
if (err < 0) if (err < 0)
goto err_mod; goto err_mod;
if (TC_ACT_EXT_CMP(a->tcfa_action, TC_ACT_GOTO_CHAIN) &&
!rcu_access_pointer(a->goto_chain)) {
tcf_action_destroy_1(a, bind);
NL_SET_ERR_MSG(extack, "can't use goto chain with NULL chain");
return ERR_PTR(-EINVAL);
}
if (err == ACT_P_CREATED)
tcf_idr_insert(a);
if (!name && tb[TCA_ACT_COOKIE]) if (!name && tb[TCA_ACT_COOKIE])
tcf_set_action_cookie(&a->act_cookie, cookie); tcf_set_action_cookie(&a->act_cookie, cookie);
...@@ -1002,13 +1011,6 @@ struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp, ...@@ -1002,13 +1011,6 @@ struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
if (err != ACT_P_CREATED) if (err != ACT_P_CREATED)
module_put(a_o->owner); module_put(a_o->owner);
if (TC_ACT_EXT_CMP(a->tcfa_action, TC_ACT_GOTO_CHAIN) &&
!rcu_access_pointer(a->goto_chain)) {
tcf_action_destroy_1(a, bind);
NL_SET_ERR_MSG(extack, "can't use goto chain with NULL chain");
return ERR_PTR(-EINVAL);
}
return a; return a;
err_mod: err_mod:
......
...@@ -365,9 +365,7 @@ static int tcf_bpf_init(struct net *net, struct nlattr *nla, ...@@ -365,9 +365,7 @@ static int tcf_bpf_init(struct net *net, struct nlattr *nla,
if (goto_ch) if (goto_ch)
tcf_chain_put_by_act(goto_ch); tcf_chain_put_by_act(goto_ch);
if (res == ACT_P_CREATED) { if (res != ACT_P_CREATED) {
tcf_idr_insert(tn, *act);
} else {
/* make sure the program being replaced is no longer executing */ /* make sure the program being replaced is no longer executing */
synchronize_rcu(); synchronize_rcu();
tcf_bpf_cfg_cleanup(&old); tcf_bpf_cfg_cleanup(&old);
......
...@@ -139,7 +139,6 @@ static int tcf_connmark_init(struct net *net, struct nlattr *nla, ...@@ -139,7 +139,6 @@ static int tcf_connmark_init(struct net *net, struct nlattr *nla,
ci->net = net; ci->net = net;
ci->zone = parm->zone; ci->zone = parm->zone;
tcf_idr_insert(tn, *a);
ret = ACT_P_CREATED; ret = ACT_P_CREATED;
} else if (ret > 0) { } else if (ret > 0) {
ci = to_connmark(*a); ci = to_connmark(*a);
......
...@@ -110,9 +110,6 @@ static int tcf_csum_init(struct net *net, struct nlattr *nla, ...@@ -110,9 +110,6 @@ static int tcf_csum_init(struct net *net, struct nlattr *nla,
if (params_new) if (params_new)
kfree_rcu(params_new, rcu); kfree_rcu(params_new, rcu);
if (ret == ACT_P_CREATED)
tcf_idr_insert(tn, *a);
return ret; return ret;
put_chain: put_chain:
if (goto_ch) if (goto_ch)
......
...@@ -1297,8 +1297,6 @@ static int tcf_ct_init(struct net *net, struct nlattr *nla, ...@@ -1297,8 +1297,6 @@ static int tcf_ct_init(struct net *net, struct nlattr *nla,
tcf_chain_put_by_act(goto_ch); tcf_chain_put_by_act(goto_ch);
if (params) if (params)
call_rcu(&params->rcu, tcf_ct_params_free); call_rcu(&params->rcu, tcf_ct_params_free);
if (res == ACT_P_CREATED)
tcf_idr_insert(tn, *a);
return res; return res;
......
...@@ -269,9 +269,6 @@ static int tcf_ctinfo_init(struct net *net, struct nlattr *nla, ...@@ -269,9 +269,6 @@ static int tcf_ctinfo_init(struct net *net, struct nlattr *nla,
if (cp_new) if (cp_new)
kfree_rcu(cp_new, rcu); kfree_rcu(cp_new, rcu);
if (ret == ACT_P_CREATED)
tcf_idr_insert(tn, *a);
return ret; return ret;
put_chain: put_chain:
......
...@@ -140,8 +140,6 @@ static int tcf_gact_init(struct net *net, struct nlattr *nla, ...@@ -140,8 +140,6 @@ static int tcf_gact_init(struct net *net, struct nlattr *nla,
if (goto_ch) if (goto_ch)
tcf_chain_put_by_act(goto_ch); tcf_chain_put_by_act(goto_ch);
if (ret == ACT_P_CREATED)
tcf_idr_insert(tn, *a);
return ret; return ret;
release_idr: release_idr:
tcf_idr_release(*a, bind); tcf_idr_release(*a, bind);
......
...@@ -437,9 +437,6 @@ static int tcf_gate_init(struct net *net, struct nlattr *nla, ...@@ -437,9 +437,6 @@ static int tcf_gate_init(struct net *net, struct nlattr *nla,
if (goto_ch) if (goto_ch)
tcf_chain_put_by_act(goto_ch); tcf_chain_put_by_act(goto_ch);
if (ret == ACT_P_CREATED)
tcf_idr_insert(tn, *a);
return ret; return ret;
chain_put: chain_put:
......
...@@ -627,9 +627,6 @@ static int tcf_ife_init(struct net *net, struct nlattr *nla, ...@@ -627,9 +627,6 @@ static int tcf_ife_init(struct net *net, struct nlattr *nla,
if (p) if (p)
kfree_rcu(p, rcu); kfree_rcu(p, rcu);
if (ret == ACT_P_CREATED)
tcf_idr_insert(tn, *a);
return ret; return ret;
metadata_parse_err: metadata_parse_err:
if (goto_ch) if (goto_ch)
......
...@@ -189,8 +189,6 @@ static int __tcf_ipt_init(struct net *net, unsigned int id, struct nlattr *nla, ...@@ -189,8 +189,6 @@ static int __tcf_ipt_init(struct net *net, unsigned int id, struct nlattr *nla,
ipt->tcfi_t = t; ipt->tcfi_t = t;
ipt->tcfi_hook = hook; ipt->tcfi_hook = hook;
spin_unlock_bh(&ipt->tcf_lock); spin_unlock_bh(&ipt->tcf_lock);
if (ret == ACT_P_CREATED)
tcf_idr_insert(tn, *a);
return ret; return ret;
err3: err3:
......
...@@ -194,8 +194,6 @@ static int tcf_mirred_init(struct net *net, struct nlattr *nla, ...@@ -194,8 +194,6 @@ static int tcf_mirred_init(struct net *net, struct nlattr *nla,
spin_lock(&mirred_list_lock); spin_lock(&mirred_list_lock);
list_add(&m->tcfm_list, &mirred_list); list_add(&m->tcfm_list, &mirred_list);
spin_unlock(&mirred_list_lock); spin_unlock(&mirred_list_lock);
tcf_idr_insert(tn, *a);
} }
return ret; return ret;
......
...@@ -273,8 +273,6 @@ static int tcf_mpls_init(struct net *net, struct nlattr *nla, ...@@ -273,8 +273,6 @@ static int tcf_mpls_init(struct net *net, struct nlattr *nla,
if (p) if (p)
kfree_rcu(p, rcu); kfree_rcu(p, rcu);
if (ret == ACT_P_CREATED)
tcf_idr_insert(tn, *a);
return ret; return ret;
put_chain: put_chain:
if (goto_ch) if (goto_ch)
......
...@@ -93,9 +93,6 @@ static int tcf_nat_init(struct net *net, struct nlattr *nla, struct nlattr *est, ...@@ -93,9 +93,6 @@ static int tcf_nat_init(struct net *net, struct nlattr *nla, struct nlattr *est,
if (goto_ch) if (goto_ch)
tcf_chain_put_by_act(goto_ch); tcf_chain_put_by_act(goto_ch);
if (ret == ACT_P_CREATED)
tcf_idr_insert(tn, *a);
return ret; return ret;
release_idr: release_idr:
tcf_idr_release(*a, bind); tcf_idr_release(*a, bind);
......
...@@ -238,8 +238,6 @@ static int tcf_pedit_init(struct net *net, struct nlattr *nla, ...@@ -238,8 +238,6 @@ static int tcf_pedit_init(struct net *net, struct nlattr *nla,
spin_unlock_bh(&p->tcf_lock); spin_unlock_bh(&p->tcf_lock);
if (goto_ch) if (goto_ch)
tcf_chain_put_by_act(goto_ch); tcf_chain_put_by_act(goto_ch);
if (ret == ACT_P_CREATED)
tcf_idr_insert(tn, *a);
return ret; return ret;
put_chain: put_chain:
......
...@@ -201,8 +201,6 @@ static int tcf_police_init(struct net *net, struct nlattr *nla, ...@@ -201,8 +201,6 @@ static int tcf_police_init(struct net *net, struct nlattr *nla,
if (new) if (new)
kfree_rcu(new, rcu); kfree_rcu(new, rcu);
if (ret == ACT_P_CREATED)
tcf_idr_insert(tn, *a);
return ret; return ret;
failure: failure:
......
...@@ -116,8 +116,6 @@ static int tcf_sample_init(struct net *net, struct nlattr *nla, ...@@ -116,8 +116,6 @@ static int tcf_sample_init(struct net *net, struct nlattr *nla,
if (goto_ch) if (goto_ch)
tcf_chain_put_by_act(goto_ch); tcf_chain_put_by_act(goto_ch);
if (ret == ACT_P_CREATED)
tcf_idr_insert(tn, *a);
return ret; return ret;
put_chain: put_chain:
if (goto_ch) if (goto_ch)
......
...@@ -157,8 +157,6 @@ static int tcf_simp_init(struct net *net, struct nlattr *nla, ...@@ -157,8 +157,6 @@ static int tcf_simp_init(struct net *net, struct nlattr *nla,
goto release_idr; goto release_idr;
} }
if (ret == ACT_P_CREATED)
tcf_idr_insert(tn, *a);
return ret; return ret;
put_chain: put_chain:
if (goto_ch) if (goto_ch)
......
...@@ -225,8 +225,6 @@ static int tcf_skbedit_init(struct net *net, struct nlattr *nla, ...@@ -225,8 +225,6 @@ static int tcf_skbedit_init(struct net *net, struct nlattr *nla,
if (goto_ch) if (goto_ch)
tcf_chain_put_by_act(goto_ch); tcf_chain_put_by_act(goto_ch);
if (ret == ACT_P_CREATED)
tcf_idr_insert(tn, *a);
return ret; return ret;
put_chain: put_chain:
if (goto_ch) if (goto_ch)
......
...@@ -190,8 +190,6 @@ static int tcf_skbmod_init(struct net *net, struct nlattr *nla, ...@@ -190,8 +190,6 @@ static int tcf_skbmod_init(struct net *net, struct nlattr *nla,
if (goto_ch) if (goto_ch)
tcf_chain_put_by_act(goto_ch); tcf_chain_put_by_act(goto_ch);
if (ret == ACT_P_CREATED)
tcf_idr_insert(tn, *a);
return ret; return ret;
put_chain: put_chain:
if (goto_ch) if (goto_ch)
......
...@@ -537,9 +537,6 @@ static int tunnel_key_init(struct net *net, struct nlattr *nla, ...@@ -537,9 +537,6 @@ static int tunnel_key_init(struct net *net, struct nlattr *nla,
if (goto_ch) if (goto_ch)
tcf_chain_put_by_act(goto_ch); tcf_chain_put_by_act(goto_ch);
if (ret == ACT_P_CREATED)
tcf_idr_insert(tn, *a);
return ret; return ret;
put_chain: put_chain:
......
...@@ -229,8 +229,6 @@ static int tcf_vlan_init(struct net *net, struct nlattr *nla, ...@@ -229,8 +229,6 @@ static int tcf_vlan_init(struct net *net, struct nlattr *nla,
if (p) if (p)
kfree_rcu(p, rcu); kfree_rcu(p, rcu);
if (ret == ACT_P_CREATED)
tcf_idr_insert(tn, *a);
return ret; return ret;
put_chain: put_chain:
if (goto_ch) if (goto_ch)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment