Skip to content

Commit 2ee22a9

Browse files
edumazetdavem330
authored andcommitted
net_sched: act_mirred: remove spinlock in fast path
Like act_gact, act_mirred can be lockless in packet processing 1) Use percpu stats 2) update lastuse only every clock tick to avoid false sharing 3) use rcu to protect tcfm_dev 4) Remove spinlock usage, as it is no longer needed. Next step : add multi queue capability to ifb device Signed-off-by: Eric Dumazet <edumazet@google.com> Cc: Alexei Starovoitov <ast@plumgrid.com> Cc: Jamal Hadi Salim <jhs@mojatatu.com> Cc: John Fastabend <john.fastabend@gmail.com> Acked-by: Jamal Hadi Salim <jhs@mojatatu.com> Acked-by: Alexei Starovoitov <ast@plumgrid.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 56e5d1c commit 2ee22a9

2 files changed

Lines changed: 31 additions & 28 deletions

File tree

‎include/net/tc_act/tc_mirred.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ struct tcf_mirred {
88
int tcfm_eaction;
99
int tcfm_ifindex;
1010
int tcfm_ok_push;
11-
struct net_device *tcfm_dev;
11+
struct net_device __rcu *tcfm_dev;
1212
struct list_head tcfm_list;
1313
};
1414
#define to_mirred(a) \

‎net/sched/act_mirred.c‎

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ static LIST_HEAD(mirred_list);
3535
static void tcf_mirred_release(struct tc_action *a, int bind)
3636
{
3737
struct tcf_mirred *m = to_mirred(a);
38+
struct net_device *dev = rcu_dereference_protected(m->tcfm_dev, 1);
39+
3840
list_del(&m->tcfm_list);
39-
if (m->tcfm_dev)
40-
dev_put(m->tcfm_dev);
41+
if (dev)
42+
dev_put(dev);
4143
}
4244

4345
static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
@@ -94,7 +96,7 @@ static int tcf_mirred_init(struct net *net, struct nlattr *nla,
9496
if (dev == NULL)
9597
return -EINVAL;
9698
ret = tcf_hash_create(parm->index, est, a, sizeof(*m),
97-
bind, false);
99+
bind, true);
98100
if (ret)
99101
return ret;
100102
ret = ACT_P_CREATED;
@@ -106,18 +108,18 @@ static int tcf_mirred_init(struct net *net, struct nlattr *nla,
106108
}
107109
m = to_mirred(a);
108110

109-
spin_lock_bh(&m->tcf_lock);
111+
ASSERT_RTNL();
110112
m->tcf_action = parm->action;
111113
m->tcfm_eaction = parm->eaction;
112114
if (dev != NULL) {
113115
m->tcfm_ifindex = parm->ifindex;
114116
if (ret != ACT_P_CREATED)
115-
dev_put(m->tcfm_dev);
117+
dev_put(rcu_dereference_protected(m->tcfm_dev, 1));
116118
dev_hold(dev);
117-
m->tcfm_dev = dev;
119+
rcu_assign_pointer(m->tcfm_dev, dev);
118120
m->tcfm_ok_push = ok_push;
119121
}
120-
spin_unlock_bh(&m->tcf_lock);
122+
121123
if (ret == ACT_P_CREATED) {
122124
list_add(&m->tcfm_list, &mirred_list);
123125
tcf_hash_insert(a);
@@ -132,28 +134,30 @@ static int tcf_mirred(struct sk_buff *skb, const struct tc_action *a,
132134
struct tcf_mirred *m = a->priv;
133135
struct net_device *dev;
134136
struct sk_buff *skb2;
137+
int retval, err;
135138
u32 at;
136-
int retval, err = 1;
137139

138-
spin_lock(&m->tcf_lock);
139-
m->tcf_tm.lastuse = jiffies;
140-
bstats_update(&m->tcf_bstats, skb);
140+
tcf_lastuse_update(&m->tcf_tm);
141+
142+
bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb);
141143

142-
dev = m->tcfm_dev;
143-
if (!dev) {
144-
printk_once(KERN_NOTICE "tc mirred: target device is gone\n");
144+
rcu_read_lock();
145+
retval = READ_ONCE(m->tcf_action);
146+
dev = rcu_dereference(m->tcfm_dev);
147+
if (unlikely(!dev)) {
148+
pr_notice_once("tc mirred: target device is gone\n");
145149
goto out;
146150
}
147151

148-
if (!(dev->flags & IFF_UP)) {
152+
if (unlikely(!(dev->flags & IFF_UP))) {
149153
net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
150154
dev->name);
151155
goto out;
152156
}
153157

154158
at = G_TC_AT(skb->tc_verd);
155159
skb2 = skb_clone(skb, GFP_ATOMIC);
156-
if (skb2 == NULL)
160+
if (!skb2)
157161
goto out;
158162

159163
if (!(at & AT_EGRESS)) {
@@ -169,16 +173,13 @@ static int tcf_mirred(struct sk_buff *skb, const struct tc_action *a,
169173
skb2->dev = dev;
170174
err = dev_queue_xmit(skb2);
171175

172-
out:
173176
if (err) {
174-
m->tcf_qstats.overlimits++;
177+
out:
178+
qstats_overlimit_inc(this_cpu_ptr(m->common.cpu_qstats));
175179
if (m->tcfm_eaction != TCA_EGRESS_MIRROR)
176180
retval = TC_ACT_SHOT;
177-
else
178-
retval = m->tcf_action;
179-
} else
180-
retval = m->tcf_action;
181-
spin_unlock(&m->tcf_lock);
181+
}
182+
rcu_read_unlock();
182183

183184
return retval;
184185
}
@@ -217,14 +218,16 @@ static int mirred_device_event(struct notifier_block *unused,
217218
struct net_device *dev = netdev_notifier_info_to_dev(ptr);
218219
struct tcf_mirred *m;
219220

221+
ASSERT_RTNL();
220222
if (event == NETDEV_UNREGISTER)
221223
list_for_each_entry(m, &mirred_list, tcfm_list) {
222-
spin_lock_bh(&m->tcf_lock);
223-
if (m->tcfm_dev == dev) {
224+
if (rcu_access_pointer(m->tcfm_dev) == dev) {
224225
dev_put(dev);
225-
m->tcfm_dev = NULL;
226+
/* Note : no rcu grace period necessary, as
227+
* net_device are already rcu protected.
228+
*/
229+
RCU_INIT_POINTER(m->tcfm_dev, NULL);
226230
}
227-
spin_unlock_bh(&m->tcf_lock);
228231
}
229232

230233
return NOTIFY_DONE;

0 commit comments

Comments
 (0)