Skip to content

Commit 6b44d0f

Browse files
committed
Merge branch 'act_csum-spinlock-remove'
Davide Caratti says: ==================== net/sched: remove spinlock from 'csum' action Similarly to what has been done earlier with other actions [1][2], this series tries to improve the performance of 'csum' tc action, removing a spinlock in the data path. Patch 1 lets act_csum use per-CPU counters; patch 2 removes spin_{,un}lock_bh() calls from the act() method. test procedure (using pktgen from https://github.com/netoptimizer): # ip link add name eth1 type dummy # ip link set dev eth1 up # tc qdisc add dev eth1 root handle 1: prio # for a in pass drop; do > tc filter del dev eth1 parent 1: pref 10 matchall action csum udp > tc filter add dev eth1 parent 1: pref 10 matchall action csum udp $a > for n in 2 4; do > ./pktgen_bench_xmit_mode_queue_xmit.sh -v -s 64 -t $n -n 1000000 -i eth1 > done > done test results: | | before patch | after patch $a | $n | avg. pps/thread | avg. pps/thread -----+----+-----------------+---------------- pass | 2 | 1671463 ± 4% | 1920789 ± 3% pass | 4 | 648797 ± 1% | 738190 ± 1% drop | 2 | 3212692 ± 2% | 3719811 ± 2% drop | 4 | 1078824 ± 1% | 1328099 ± 1% references: [1] https://www.spinics.net/lists/netdev/msg334760.html [2] https://www.spinics.net/lists/netdev/msg465862.html v3 changes: - use rtnl_dereference() in place of rcu_dereference() in tcf_csum_dump() v2 changes: - add 'drop' test, it produces more contentions - use RCU-protected struct to store 'action' and 'update_flags', to avoid reading the values from subsequent configurations ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents b76f418 + 9c5f69b commit 6b44d0f

File tree

2 files changed

+62
-20
lines changed

2 files changed

+62
-20
lines changed

include/net/tc_act/tc_csum.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@
66
#include <net/act_api.h>
77
#include <linux/tc_act/tc_csum.h>
88

9+
struct tcf_csum_params {
10+
int action;
11+
u32 update_flags;
12+
struct rcu_head rcu;
13+
};
14+
915
struct tcf_csum {
1016
struct tc_action common;
1117

12-
u32 update_flags;
18+
struct tcf_csum_params __rcu *params;
1319
};
1420
#define to_tcf_csum(a) ((struct tcf_csum *)a)
1521

@@ -24,7 +30,13 @@ static inline bool is_tcf_csum(const struct tc_action *a)
2430

2531
static inline u32 tcf_csum_update_flags(const struct tc_action *a)
2632
{
27-
return to_tcf_csum(a)->update_flags;
33+
u32 update_flags;
34+
35+
rcu_read_lock();
36+
update_flags = rcu_dereference(to_tcf_csum(a)->params)->update_flags;
37+
rcu_read_unlock();
38+
39+
return update_flags;
2840
}
2941

3042
#endif /* __NET_TC_CSUM_H */

net/sched/act_csum.c

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ static int tcf_csum_init(struct net *net, struct nlattr *nla,
4949
int bind)
5050
{
5151
struct tc_action_net *tn = net_generic(net, csum_net_id);
52+
struct tcf_csum_params *params_old, *params_new;
5253
struct nlattr *tb[TCA_CSUM_MAX + 1];
5354
struct tc_csum *parm;
5455
struct tcf_csum *p;
@@ -67,7 +68,7 @@ static int tcf_csum_init(struct net *net, struct nlattr *nla,
6768

6869
if (!tcf_idr_check(tn, parm->index, a, bind)) {
6970
ret = tcf_idr_create(tn, parm->index, est, a,
70-
&act_csum_ops, bind, false);
71+
&act_csum_ops, bind, true);
7172
if (ret)
7273
return ret;
7374
ret = ACT_P_CREATED;
@@ -80,10 +81,21 @@ static int tcf_csum_init(struct net *net, struct nlattr *nla,
8081
}
8182

8283
p = to_tcf_csum(*a);
83-
spin_lock_bh(&p->tcf_lock);
84-
p->tcf_action = parm->action;
85-
p->update_flags = parm->update_flags;
86-
spin_unlock_bh(&p->tcf_lock);
84+
ASSERT_RTNL();
85+
86+
params_new = kzalloc(sizeof(*params_new), GFP_KERNEL);
87+
if (unlikely(!params_new)) {
88+
if (ret == ACT_P_CREATED)
89+
tcf_idr_release(*a, bind);
90+
return -ENOMEM;
91+
}
92+
params_old = rtnl_dereference(p->params);
93+
94+
params_new->action = parm->action;
95+
params_new->update_flags = parm->update_flags;
96+
rcu_assign_pointer(p->params, params_new);
97+
if (params_old)
98+
kfree_rcu(params_old, rcu);
8799

88100
if (ret == ACT_P_CREATED)
89101
tcf_idr_insert(tn, *a);
@@ -539,19 +551,21 @@ static int tcf_csum(struct sk_buff *skb, const struct tc_action *a,
539551
struct tcf_result *res)
540552
{
541553
struct tcf_csum *p = to_tcf_csum(a);
542-
int action;
554+
struct tcf_csum_params *params;
543555
u32 update_flags;
556+
int action;
557+
558+
rcu_read_lock();
559+
params = rcu_dereference(p->params);
544560

545-
spin_lock(&p->tcf_lock);
546561
tcf_lastuse_update(&p->tcf_tm);
547-
bstats_update(&p->tcf_bstats, skb);
548-
action = p->tcf_action;
549-
update_flags = p->update_flags;
550-
spin_unlock(&p->tcf_lock);
562+
bstats_cpu_update(this_cpu_ptr(p->common.cpu_bstats), skb);
551563

564+
action = params->action;
552565
if (unlikely(action == TC_ACT_SHOT))
553-
goto drop;
566+
goto drop_stats;
554567

568+
update_flags = params->update_flags;
555569
switch (tc_skb_protocol(skb)) {
556570
case cpu_to_be16(ETH_P_IP):
557571
if (!tcf_csum_ipv4(skb, update_flags))
@@ -563,29 +577,35 @@ static int tcf_csum(struct sk_buff *skb, const struct tc_action *a,
563577
break;
564578
}
565579

580+
unlock:
581+
rcu_read_unlock();
566582
return action;
567583

568584
drop:
569-
spin_lock(&p->tcf_lock);
570-
p->tcf_qstats.drops++;
571-
spin_unlock(&p->tcf_lock);
572-
return TC_ACT_SHOT;
585+
action = TC_ACT_SHOT;
586+
587+
drop_stats:
588+
qstats_drop_inc(this_cpu_ptr(p->common.cpu_qstats));
589+
goto unlock;
573590
}
574591

575592
static int tcf_csum_dump(struct sk_buff *skb, struct tc_action *a, int bind,
576593
int ref)
577594
{
578595
unsigned char *b = skb_tail_pointer(skb);
579596
struct tcf_csum *p = to_tcf_csum(a);
597+
struct tcf_csum_params *params;
580598
struct tc_csum opt = {
581-
.update_flags = p->update_flags,
582599
.index = p->tcf_index,
583-
.action = p->tcf_action,
584600
.refcnt = p->tcf_refcnt - ref,
585601
.bindcnt = p->tcf_bindcnt - bind,
586602
};
587603
struct tcf_t t;
588604

605+
params = rtnl_dereference(p->params);
606+
opt.action = params->action;
607+
opt.update_flags = params->update_flags;
608+
589609
if (nla_put(skb, TCA_CSUM_PARMS, sizeof(opt), &opt))
590610
goto nla_put_failure;
591611

@@ -600,6 +620,15 @@ static int tcf_csum_dump(struct sk_buff *skb, struct tc_action *a, int bind,
600620
return -1;
601621
}
602622

623+
static void tcf_csum_cleanup(struct tc_action *a)
624+
{
625+
struct tcf_csum *p = to_tcf_csum(a);
626+
struct tcf_csum_params *params;
627+
628+
params = rcu_dereference_protected(p->params, 1);
629+
kfree_rcu(params, rcu);
630+
}
631+
603632
static int tcf_csum_walker(struct net *net, struct sk_buff *skb,
604633
struct netlink_callback *cb, int type,
605634
const struct tc_action_ops *ops)
@@ -623,6 +652,7 @@ static struct tc_action_ops act_csum_ops = {
623652
.act = tcf_csum,
624653
.dump = tcf_csum_dump,
625654
.init = tcf_csum_init,
655+
.cleanup = tcf_csum_cleanup,
626656
.walk = tcf_csum_walker,
627657
.lookup = tcf_csum_search,
628658
.size = sizeof(struct tcf_csum),

0 commit comments

Comments
 (0)