net: sched: refactor tc_ctl_chain() to use block->lock

In order to remove dependency on rtnl lock, modify chain API to use
block->lock to protect chain from concurrent modification. Rearrange
tc_ctl_chain() code to call tcf_chain_hold() while holding block->lock to
prevent concurrent chain removal.

Signed-off-by: Vlad Buslov <vladbu@mellanox.com>
Acked-by: Jiri Pirko <jiri@mellanox.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
Vlad Buslov 2019-02-11 10:55:34 +02:00 committed by David S. Miller
parent 91052fa1c6
commit 2cbfab07c6
1 changed files with 26 additions and 12 deletions

View File

@ -2255,6 +2255,8 @@ replay:
err = -EINVAL; err = -EINVAL;
goto errout_block; goto errout_block;
} }
mutex_lock(&block->lock);
chain = tcf_chain_lookup(block, chain_index); chain = tcf_chain_lookup(block, chain_index);
if (n->nlmsg_type == RTM_NEWCHAIN) { if (n->nlmsg_type == RTM_NEWCHAIN) {
if (chain) { if (chain) {
@ -2266,41 +2268,49 @@ replay:
} else { } else {
NL_SET_ERR_MSG(extack, "Filter chain already exists"); NL_SET_ERR_MSG(extack, "Filter chain already exists");
err = -EEXIST; err = -EEXIST;
goto errout_block; goto errout_block_locked;
} }
} else { } else {
if (!(n->nlmsg_flags & NLM_F_CREATE)) { if (!(n->nlmsg_flags & NLM_F_CREATE)) {
NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain"); NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
err = -ENOENT; err = -ENOENT;
goto errout_block; goto errout_block_locked;
} }
chain = tcf_chain_create(block, chain_index); chain = tcf_chain_create(block, chain_index);
if (!chain) { if (!chain) {
NL_SET_ERR_MSG(extack, "Failed to create filter chain"); NL_SET_ERR_MSG(extack, "Failed to create filter chain");
err = -ENOMEM; err = -ENOMEM;
goto errout_block; goto errout_block_locked;
} }
} }
} else { } else {
if (!chain || tcf_chain_held_by_acts_only(chain)) { if (!chain || tcf_chain_held_by_acts_only(chain)) {
NL_SET_ERR_MSG(extack, "Cannot find specified filter chain"); NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
err = -EINVAL; err = -EINVAL;
goto errout_block; goto errout_block_locked;
} }
tcf_chain_hold(chain); tcf_chain_hold(chain);
} }
switch (n->nlmsg_type) { if (n->nlmsg_type == RTM_NEWCHAIN) {
case RTM_NEWCHAIN: /* Modifying chain requires holding parent block lock. In case
err = tc_chain_tmplt_add(chain, net, tca, extack); * the chain was successfully added, take a reference to the
if (err) * chain. This ensures that an empty chain does not disappear at
goto errout; * the end of this function.
/* In case the chain was successfully added, take a reference
* to the chain. This ensures that an empty chain
* does not disappear at the end of this function.
*/ */
tcf_chain_hold(chain); tcf_chain_hold(chain);
chain->explicitly_created = true; chain->explicitly_created = true;
}
mutex_unlock(&block->lock);
switch (n->nlmsg_type) {
case RTM_NEWCHAIN:
err = tc_chain_tmplt_add(chain, net, tca, extack);
if (err) {
tcf_chain_put_explicitly_created(chain);
goto errout;
}
tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL, tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
RTM_NEWCHAIN, false); RTM_NEWCHAIN, false);
break; break;
@ -2334,6 +2344,10 @@ errout_block:
/* Replay the request. */ /* Replay the request. */
goto replay; goto replay;
return err; return err;
errout_block_locked:
mutex_unlock(&block->lock);
goto errout_block;
} }
/* called with RTNL */ /* called with RTNL */