Skip to content

Commit 9c5f69b

Browse files
dcarattidavem330
authored andcommitted
net/sched: act_csum: don't use spinlock in the fast path
use RCU instead of spin_{,unlock}_bh() to protect concurrent read/write on act_csum configuration, to reduce the effects of contention in the data path when multiple readers are present. Signed-off-by: Davide Caratti <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent f6052cf commit 9c5f69b

File tree

2 files changed

+59
-15
lines changed

2 files changed

+59
-15
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: 45 additions & 13 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;
@@ -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

545561
tcf_lastuse_update(&p->tcf_tm);
546562
bstats_cpu_update(this_cpu_ptr(p->common.cpu_bstats), skb);
547-
spin_lock(&p->tcf_lock);
548-
action = p->tcf_action;
549-
update_flags = p->update_flags;
550-
spin_unlock(&p->tcf_lock);
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,27 +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:
585+
action = TC_ACT_SHOT;
586+
587+
drop_stats:
569588
qstats_drop_inc(this_cpu_ptr(p->common.cpu_qstats));
570-
return TC_ACT_SHOT;
589+
goto unlock;
571590
}
572591

573592
static int tcf_csum_dump(struct sk_buff *skb, struct tc_action *a, int bind,
574593
int ref)
575594
{
576595
unsigned char *b = skb_tail_pointer(skb);
577596
struct tcf_csum *p = to_tcf_csum(a);
597+
struct tcf_csum_params *params;
578598
struct tc_csum opt = {
579-
.update_flags = p->update_flags,
580599
.index = p->tcf_index,
581-
.action = p->tcf_action,
582600
.refcnt = p->tcf_refcnt - ref,
583601
.bindcnt = p->tcf_bindcnt - bind,
584602
};
585603
struct tcf_t t;
586604

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

@@ -598,6 +620,15 @@ static int tcf_csum_dump(struct sk_buff *skb, struct tc_action *a, int bind,
598620
return -1;
599621
}
600622

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+
601632
static int tcf_csum_walker(struct net *net, struct sk_buff *skb,
602633
struct netlink_callback *cb, int type,
603634
const struct tc_action_ops *ops)
@@ -621,6 +652,7 @@ static struct tc_action_ops act_csum_ops = {
621652
.act = tcf_csum,
622653
.dump = tcf_csum_dump,
623654
.init = tcf_csum_init,
655+
.cleanup = tcf_csum_cleanup,
624656
.walk = tcf_csum_walker,
625657
.lookup = tcf_csum_search,
626658
.size = sizeof(struct tcf_csum),

0 commit comments

Comments
 (0)