Commit b5d30812 authored by David S. Miller's avatar David S. Miller

Merge branch 'Remove-rtnl-lock-dependency-from-flow_action-infra'

Vlad Buslov says:

====================
Remove rtnl lock dependency from flow_action infra

Currently, TC flow_action infrastructure code obtain rtnl lock before
accessing action state in tc_setup_flow_action() function and releases
it afterwards. This behavior is not supposed to impact TC filter
insertion rate because filling flow_action representation is only a
small part of creating new filter and expensive operations (hardware
offload callbacks, classifiers, cls API code that creates chains and
classifiers instances) already support unlocked execution. However,
typical vswitch implementation might need to also dump TC filters
concurrently, for example to age out unused flows or update flow
counters. TC dump is fully serialized and holds rtnl lock during its
whole execution in kernel space. As such, it can significantly impact
concurrent tasks that try to intermittently obtain rtnl lock when
filling intermediate representation for new filter offload (performance
evaluation at the end of this mail).

Refactor flow_action cls API infrastructure and its dependencies to not
rely on rtnl lock for synchronization. Patch set overview:

- Refactor tc_setup_flow_action() to obtain action tcf_lock when
  accessing action state. Fix its dependencies to not obtain tcf_lock
  themselves and assume that caller already holds it (needs to be done
  in same patch to prevent deadlock) and not to call sleeping functions
  (needs to be done in same patch to prevent "sleeping while atomic"
  dmesg warnings).

- Refactor action helper functions to require tcf_lock instead of rtnl.
  Internally, all of the actions already use tcf_lock for
  synchronization to accommodate unlocked classifier API, so this change
  relies on already existing functionality.

- Remove rtnl lock and "rtnl_held" argument from tc_setup_flow_action()
  function.

To test the change, multiple concurrent TC instances are invoked with
following command:

time ls add* | xargs -n 1 -P 100 sudo tc -b

Ten batch files with following typical rules (100k each) are used:

filter add dev ens1f0_0 protocol ip ingress prio 1 handle 1 flower
	src_mac e4:11:0:0:0:0 dst_mac e4:12:0:0:0:0 src_ip 192.168.111.1
	dst_ip 192.168.111.2 ip_proto udp dst_port 1 src_port 1 action
	tunnel_key set id 1 src_ip 2.2.2.2 dst_ip 2.2.2.3 dst_port 4789
	no_percpu action mirred egress redirect dev vxlan1 no_percpu

TC dump of same device is called in infinite loop from five concurrent
instances:

while true do tc -s filter show dev $NIC ingress >/dev/null done

Results obtained on current net-next commit 9f68e365 ("Merge tag
'drm-next-2020-01-30' of git://anongit.freedesktop.org/drm/drm"):

               | net-next | this change
---------------+----------+-------------
 TC add        | 6.3s     | 6.3s
 TC add + dump | 29.3s    | 6.8s

Test results confirm significant impact of concurrent TC dump. The
impact is almost fully mitigated by proposed change (differences can be
attributed to contention for chain and tp locks between add and dump TC
instances).
====================
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents 92df9f8a b15e7a6e
...@@ -509,7 +509,7 @@ tcf_match_indev(struct sk_buff *skb, int ifindex) ...@@ -509,7 +509,7 @@ tcf_match_indev(struct sk_buff *skb, int ifindex)
} }
int tc_setup_flow_action(struct flow_action *flow_action, int tc_setup_flow_action(struct flow_action *flow_action,
const struct tcf_exts *exts, bool rtnl_held); const struct tcf_exts *exts);
void tc_cleanup_flow_action(struct flow_action *flow_action); void tc_cleanup_flow_action(struct flow_action *flow_action);
int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type, int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
......
...@@ -33,8 +33,10 @@ struct tcf_ct { ...@@ -33,8 +33,10 @@ struct tcf_ct {
}; };
#define to_ct(a) ((struct tcf_ct *)a) #define to_ct(a) ((struct tcf_ct *)a)
#define to_ct_params(a) ((struct tcf_ct_params *) \ #define to_ct_params(a) \
rtnl_dereference((to_ct(a)->params))) ((struct tcf_ct_params *) \
rcu_dereference_protected(to_ct(a)->params, \
lockdep_is_held(&a->tcfa_lock)))
static inline uint16_t tcf_ct_zone(const struct tc_action *a) static inline uint16_t tcf_ct_zone(const struct tc_action *a)
{ {
......
...@@ -54,7 +54,8 @@ static inline u64 tcf_police_rate_bytes_ps(const struct tc_action *act) ...@@ -54,7 +54,8 @@ static inline u64 tcf_police_rate_bytes_ps(const struct tc_action *act)
struct tcf_police *police = to_police(act); struct tcf_police *police = to_police(act);
struct tcf_police_params *params; struct tcf_police_params *params;
params = rcu_dereference_bh_rtnl(police->params); params = rcu_dereference_protected(police->params,
lockdep_is_held(&police->tcf_lock));
return params->rate.rate_bytes_ps; return params->rate.rate_bytes_ps;
} }
...@@ -63,7 +64,8 @@ static inline s64 tcf_police_tcfp_burst(const struct tc_action *act) ...@@ -63,7 +64,8 @@ static inline s64 tcf_police_tcfp_burst(const struct tc_action *act)
struct tcf_police *police = to_police(act); struct tcf_police *police = to_police(act);
struct tcf_police_params *params; struct tcf_police_params *params;
params = rcu_dereference_bh_rtnl(police->params); params = rcu_dereference_protected(police->params,
lockdep_is_held(&police->tcf_lock));
return params->tcfp_burst; return params->tcfp_burst;
} }
......
...@@ -69,7 +69,7 @@ tcf_tunnel_info_copy(const struct tc_action *a) ...@@ -69,7 +69,7 @@ tcf_tunnel_info_copy(const struct tc_action *a)
if (tun) { if (tun) {
size_t tun_size = sizeof(*tun) + tun->options_len; size_t tun_size = sizeof(*tun) + tun->options_len;
struct ip_tunnel_info *tun_copy = kmemdup(tun, tun_size, struct ip_tunnel_info *tun_copy = kmemdup(tun, tun_size,
GFP_KERNEL); GFP_ATOMIC);
return tun_copy; return tun_copy;
} }
......
...@@ -267,14 +267,12 @@ tcf_sample_get_group(const struct tc_action *a, ...@@ -267,14 +267,12 @@ tcf_sample_get_group(const struct tc_action *a,
struct tcf_sample *s = to_sample(a); struct tcf_sample *s = to_sample(a);
struct psample_group *group; struct psample_group *group;
spin_lock_bh(&s->tcf_lock);
group = rcu_dereference_protected(s->psample_group, group = rcu_dereference_protected(s->psample_group,
lockdep_is_held(&s->tcf_lock)); lockdep_is_held(&s->tcf_lock));
if (group) { if (group) {
psample_group_take(group); psample_group_take(group);
*destructor = tcf_psample_group_put; *destructor = tcf_psample_group_put;
} }
spin_unlock_bh(&s->tcf_lock);
return group; return group;
} }
......
...@@ -3433,22 +3433,20 @@ static void tcf_sample_get_group(struct flow_action_entry *entry, ...@@ -3433,22 +3433,20 @@ static void tcf_sample_get_group(struct flow_action_entry *entry,
} }
int tc_setup_flow_action(struct flow_action *flow_action, int tc_setup_flow_action(struct flow_action *flow_action,
const struct tcf_exts *exts, bool rtnl_held) const struct tcf_exts *exts)
{ {
const struct tc_action *act; struct tc_action *act;
int i, j, k, err = 0; int i, j, k, err = 0;
if (!exts) if (!exts)
return 0; return 0;
if (!rtnl_held)
rtnl_lock();
j = 0; j = 0;
tcf_exts_for_each_action(i, act, exts) { tcf_exts_for_each_action(i, act, exts) {
struct flow_action_entry *entry; struct flow_action_entry *entry;
entry = &flow_action->entries[j]; entry = &flow_action->entries[j];
spin_lock_bh(&act->tcfa_lock);
if (is_tcf_gact_ok(act)) { if (is_tcf_gact_ok(act)) {
entry->id = FLOW_ACTION_ACCEPT; entry->id = FLOW_ACTION_ACCEPT;
} else if (is_tcf_gact_shot(act)) { } else if (is_tcf_gact_shot(act)) {
...@@ -3489,13 +3487,13 @@ int tc_setup_flow_action(struct flow_action *flow_action, ...@@ -3489,13 +3487,13 @@ int tc_setup_flow_action(struct flow_action *flow_action,
break; break;
default: default:
err = -EOPNOTSUPP; err = -EOPNOTSUPP;
goto err_out; goto err_out_locked;
} }
} else if (is_tcf_tunnel_set(act)) { } else if (is_tcf_tunnel_set(act)) {
entry->id = FLOW_ACTION_TUNNEL_ENCAP; entry->id = FLOW_ACTION_TUNNEL_ENCAP;
err = tcf_tunnel_encap_get_tunnel(entry, act); err = tcf_tunnel_encap_get_tunnel(entry, act);
if (err) if (err)
goto err_out; goto err_out_locked;
} else if (is_tcf_tunnel_release(act)) { } else if (is_tcf_tunnel_release(act)) {
entry->id = FLOW_ACTION_TUNNEL_DECAP; entry->id = FLOW_ACTION_TUNNEL_DECAP;
} else if (is_tcf_pedit(act)) { } else if (is_tcf_pedit(act)) {
...@@ -3509,7 +3507,7 @@ int tc_setup_flow_action(struct flow_action *flow_action, ...@@ -3509,7 +3507,7 @@ int tc_setup_flow_action(struct flow_action *flow_action,
break; break;
default: default:
err = -EOPNOTSUPP; err = -EOPNOTSUPP;
goto err_out; goto err_out_locked;
} }
entry->mangle.htype = tcf_pedit_htype(act, k); entry->mangle.htype = tcf_pedit_htype(act, k);
entry->mangle.mask = tcf_pedit_mask(act, k); entry->mangle.mask = tcf_pedit_mask(act, k);
...@@ -3560,28 +3558,29 @@ int tc_setup_flow_action(struct flow_action *flow_action, ...@@ -3560,28 +3558,29 @@ int tc_setup_flow_action(struct flow_action *flow_action,
entry->mpls_mangle.ttl = tcf_mpls_ttl(act); entry->mpls_mangle.ttl = tcf_mpls_ttl(act);
break; break;
default: default:
goto err_out; goto err_out_locked;
} }
} else if (is_tcf_skbedit_ptype(act)) { } else if (is_tcf_skbedit_ptype(act)) {
entry->id = FLOW_ACTION_PTYPE; entry->id = FLOW_ACTION_PTYPE;
entry->ptype = tcf_skbedit_ptype(act); entry->ptype = tcf_skbedit_ptype(act);
} else { } else {
err = -EOPNOTSUPP; err = -EOPNOTSUPP;
goto err_out; goto err_out_locked;
} }
spin_unlock_bh(&act->tcfa_lock);
if (!is_tcf_pedit(act)) if (!is_tcf_pedit(act))
j++; j++;
} }
err_out: err_out:
if (!rtnl_held)
rtnl_unlock();
if (err) if (err)
tc_cleanup_flow_action(flow_action); tc_cleanup_flow_action(flow_action);
return err; return err;
err_out_locked:
spin_unlock_bh(&act->tcfa_lock);
goto err_out;
} }
EXPORT_SYMBOL(tc_setup_flow_action); EXPORT_SYMBOL(tc_setup_flow_action);
......
...@@ -449,8 +449,7 @@ static int fl_hw_replace_filter(struct tcf_proto *tp, ...@@ -449,8 +449,7 @@ static int fl_hw_replace_filter(struct tcf_proto *tp,
cls_flower.rule->match.key = &f->mkey; cls_flower.rule->match.key = &f->mkey;
cls_flower.classid = f->res.classid; cls_flower.classid = f->res.classid;
err = tc_setup_flow_action(&cls_flower.rule->action, &f->exts, err = tc_setup_flow_action(&cls_flower.rule->action, &f->exts);
rtnl_held);
if (err) { if (err) {
kfree(cls_flower.rule); kfree(cls_flower.rule);
if (skip_sw) { if (skip_sw) {
...@@ -2000,8 +1999,7 @@ static int fl_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb, ...@@ -2000,8 +1999,7 @@ static int fl_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb,
cls_flower.rule->match.mask = &f->mask->key; cls_flower.rule->match.mask = &f->mask->key;
cls_flower.rule->match.key = &f->mkey; cls_flower.rule->match.key = &f->mkey;
err = tc_setup_flow_action(&cls_flower.rule->action, &f->exts, err = tc_setup_flow_action(&cls_flower.rule->action, &f->exts);
true);
if (err) { if (err) {
kfree(cls_flower.rule); kfree(cls_flower.rule);
if (tc_skip_sw(f->flags)) { if (tc_skip_sw(f->flags)) {
......
...@@ -97,7 +97,7 @@ static int mall_replace_hw_filter(struct tcf_proto *tp, ...@@ -97,7 +97,7 @@ static int mall_replace_hw_filter(struct tcf_proto *tp,
cls_mall.command = TC_CLSMATCHALL_REPLACE; cls_mall.command = TC_CLSMATCHALL_REPLACE;
cls_mall.cookie = cookie; cls_mall.cookie = cookie;
err = tc_setup_flow_action(&cls_mall.rule->action, &head->exts, true); err = tc_setup_flow_action(&cls_mall.rule->action, &head->exts);
if (err) { if (err) {
kfree(cls_mall.rule); kfree(cls_mall.rule);
mall_destroy_hw_filter(tp, head, cookie, NULL); mall_destroy_hw_filter(tp, head, cookie, NULL);
...@@ -302,7 +302,7 @@ static int mall_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb, ...@@ -302,7 +302,7 @@ static int mall_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb,
TC_CLSMATCHALL_REPLACE : TC_CLSMATCHALL_DESTROY; TC_CLSMATCHALL_REPLACE : TC_CLSMATCHALL_DESTROY;
cls_mall.cookie = (unsigned long)head; cls_mall.cookie = (unsigned long)head;
err = tc_setup_flow_action(&cls_mall.rule->action, &head->exts, true); err = tc_setup_flow_action(&cls_mall.rule->action, &head->exts);
if (err) { if (err) {
kfree(cls_mall.rule); kfree(cls_mall.rule);
if (add && tc_skip_sw(head->flags)) { if (add && tc_skip_sw(head->flags)) {
......
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