Extend tcf_chain with 'flushing' flag. Use the flag to prevent insertion of
new classifier instances when chain flushing is in progress in order to
prevent resource leak when tcf_proto is created by unlocked users
concurrently.
Return EAGAIN error from tcf_chain_tp_insert_unique() to restart
tc_new_tfilter() and lookup the chain/proto again.
Signed-off-by: Vlad Buslov <vladbu@mellanox.com>
Acked-by: Jiri Pirko <jiri@mellanox.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
unsigned int refcnt;
unsigned int action_refcnt;
bool explicitly_created;
unsigned int refcnt;
unsigned int action_refcnt;
bool explicitly_created;
const struct tcf_proto_ops *tmplt_ops;
void *tmplt_priv;
};
const struct tcf_proto_ops *tmplt_ops;
void *tmplt_priv;
};
mutex_unlock(&block->lock);
/* The last dropped non-action reference will trigger notification. */
mutex_unlock(&block->lock);
/* The last dropped non-action reference will trigger notification. */
- if (is_last && !by_act)
+ if (is_last && !by_act) {
tc_chain_notify_delete(tmplt_ops, tmplt_priv, chain_index,
block, NULL, 0, 0, false);
tc_chain_notify_delete(tmplt_ops, tmplt_priv, chain_index,
block, NULL, 0, 0, false);
+ /* Last reference to chain, no need to lock. */
+ chain->flushing = false;
+ }
if (refcnt == 0) {
tc_chain_tmplt_del(tmplt_ops, tmplt_priv);
if (refcnt == 0) {
tc_chain_tmplt_del(tmplt_ops, tmplt_priv);
tp = tcf_chain_dereference(chain->filter_chain, chain);
RCU_INIT_POINTER(chain->filter_chain, NULL);
tcf_chain0_head_change(chain, NULL);
tp = tcf_chain_dereference(chain->filter_chain, chain);
RCU_INIT_POINTER(chain->filter_chain, NULL);
tcf_chain0_head_change(chain, NULL);
+ chain->flushing = true;
mutex_unlock(&chain->filter_chain_lock);
while (tp) {
mutex_unlock(&chain->filter_chain_lock);
while (tp) {
return tcf_chain_dereference(*chain_info->pprev, chain);
}
return tcf_chain_dereference(*chain_info->pprev, chain);
}
-static void tcf_chain_tp_insert(struct tcf_chain *chain,
- struct tcf_chain_info *chain_info,
- struct tcf_proto *tp)
+static int tcf_chain_tp_insert(struct tcf_chain *chain,
+ struct tcf_chain_info *chain_info,
+ struct tcf_proto *tp)
+ if (chain->flushing)
+ return -EAGAIN;
+
if (*chain_info->pprev == chain->filter_chain)
tcf_chain0_head_change(chain, tp);
tcf_proto_get(tp);
RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain, chain_info));
rcu_assign_pointer(*chain_info->pprev, tp);
if (*chain_info->pprev == chain->filter_chain)
tcf_chain0_head_change(chain, tp);
tcf_proto_get(tp);
RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain, chain_info));
rcu_assign_pointer(*chain_info->pprev, tp);
}
static void tcf_chain_tp_remove(struct tcf_chain *chain,
}
static void tcf_chain_tp_remove(struct tcf_chain *chain,
{
struct tcf_chain_info chain_info;
struct tcf_proto *tp;
{
struct tcf_chain_info chain_info;
struct tcf_proto *tp;
mutex_lock(&chain->filter_chain_lock);
tp = tcf_chain_tp_find(chain, &chain_info,
protocol, prio, false);
if (!tp)
mutex_lock(&chain->filter_chain_lock);
tp = tcf_chain_tp_find(chain, &chain_info,
protocol, prio, false);
if (!tp)
- tcf_chain_tp_insert(chain, &chain_info, tp_new);
+ err = tcf_chain_tp_insert(chain, &chain_info, tp_new);
mutex_unlock(&chain->filter_chain_lock);
if (tp) {
tcf_proto_destroy(tp_new, NULL);
tp_new = tp;
mutex_unlock(&chain->filter_chain_lock);
if (tp) {
tcf_proto_destroy(tp_new, NULL);
tp_new = tp;
+ } else if (err) {
+ tcf_proto_destroy(tp_new, NULL);
+ tp_new = ERR_PTR(err);
if (tp == NULL) {
struct tcf_proto *tp_new = NULL;
if (tp == NULL) {
struct tcf_proto *tp_new = NULL;
+ if (chain->flushing) {
+ err = -EAGAIN;
+ goto errout_locked;
+ }
+
/* Proto-tcf does not exist, create new one */
if (tca[TCA_KIND] == NULL || !protocol) {
/* Proto-tcf does not exist, create new one */
if (tca[TCA_KIND] == NULL || !protocol) {
protocol, prio, chain, extack);
if (IS_ERR(tp_new)) {
err = PTR_ERR(tp_new);
protocol, prio, chain, extack);
if (IS_ERR(tp_new)) {
err = PTR_ERR(tp_new);
}
tp_created = 1;
tp = tcf_chain_tp_insert_unique(chain, tp_new, protocol, prio);
}
tp_created = 1;
tp = tcf_chain_tp_insert_unique(chain, tp_new, protocol, prio);
+ if (IS_ERR(tp)) {
+ err = PTR_ERR(tp);
+ goto errout_tp;
+ }
} else {
mutex_unlock(&chain->filter_chain_lock);
}
} else {
mutex_unlock(&chain->filter_chain_lock);
}
errout:
if (err && tp_created)
tcf_chain_tp_delete_empty(chain, tp, NULL);
errout:
if (err && tp_created)
tcf_chain_tp_delete_empty(chain, tp, NULL);
if (chain) {
if (tp && !IS_ERR(tp))
tcf_proto_put(tp, NULL);
if (chain) {
if (tp && !IS_ERR(tp))
tcf_proto_put(tp, NULL);