Skip to content

Commit 2922bc8

Browse files
Eric Dumazetdavem330
authored andcommitted
ip6tnl: convert hash tables locking to RCU
ip6_tunnels use one rwlock to protect their hash tables. This locking scheme can be converted to RCU for free, since netdevice already must wait for a RCU grace period at dismantle time. Signed-off-by: Eric Dumazet <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 8f95dd6 commit 2922bc8

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

net/ipv6/ip6_tunnel.c

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,10 @@ struct ip6_tnl_net {
8888
struct ip6_tnl **tnls[2];
8989
};
9090

91-
/* lock for the tunnel lists */
92-
static DEFINE_RWLOCK(ip6_tnl_lock);
91+
/*
92+
* Locking : hash tables are protected by RCU and a spinlock
93+
*/
94+
static DEFINE_SPINLOCK(ip6_tnl_lock);
9395

9496
static inline struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
9597
{
@@ -130,6 +132,9 @@ static inline void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst)
130132
* else %NULL
131133
**/
132134

135+
#define for_each_ip6_tunnel_rcu(start) \
136+
for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
137+
133138
static struct ip6_tnl *
134139
ip6_tnl_lookup(struct net *net, struct in6_addr *remote, struct in6_addr *local)
135140
{
@@ -138,13 +143,14 @@ ip6_tnl_lookup(struct net *net, struct in6_addr *remote, struct in6_addr *local)
138143
struct ip6_tnl *t;
139144
struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
140145

141-
for (t = ip6n->tnls_r_l[h0 ^ h1]; t; t = t->next) {
146+
for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[h0 ^ h1]) {
142147
if (ipv6_addr_equal(local, &t->parms.laddr) &&
143148
ipv6_addr_equal(remote, &t->parms.raddr) &&
144149
(t->dev->flags & IFF_UP))
145150
return t;
146151
}
147-
if ((t = ip6n->tnls_wc[0]) != NULL && (t->dev->flags & IFF_UP))
152+
t = rcu_dereference(ip6n->tnls_wc[0]);
153+
if (t && (t->dev->flags & IFF_UP))
148154
return t;
149155

150156
return NULL;
@@ -186,10 +192,10 @@ ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
186192
{
187193
struct ip6_tnl **tp = ip6_tnl_bucket(ip6n, &t->parms);
188194

195+
spin_lock_bh(&ip6_tnl_lock);
189196
t->next = *tp;
190-
write_lock_bh(&ip6_tnl_lock);
191-
*tp = t;
192-
write_unlock_bh(&ip6_tnl_lock);
197+
rcu_assign_pointer(*tp, t);
198+
spin_unlock_bh(&ip6_tnl_lock);
193199
}
194200

195201
/**
@@ -204,9 +210,9 @@ ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
204210

205211
for (tp = ip6_tnl_bucket(ip6n, &t->parms); *tp; tp = &(*tp)->next) {
206212
if (t == *tp) {
207-
write_lock_bh(&ip6_tnl_lock);
213+
spin_lock_bh(&ip6_tnl_lock);
208214
*tp = t->next;
209-
write_unlock_bh(&ip6_tnl_lock);
215+
spin_unlock_bh(&ip6_tnl_lock);
210216
break;
211217
}
212218
}
@@ -313,9 +319,9 @@ ip6_tnl_dev_uninit(struct net_device *dev)
313319
struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
314320

315321
if (dev == ip6n->fb_tnl_dev) {
316-
write_lock_bh(&ip6_tnl_lock);
322+
spin_lock_bh(&ip6_tnl_lock);
317323
ip6n->tnls_wc[0] = NULL;
318-
write_unlock_bh(&ip6_tnl_lock);
324+
spin_unlock_bh(&ip6_tnl_lock);
319325
} else {
320326
ip6_tnl_unlink(ip6n, t);
321327
}
@@ -409,7 +415,7 @@ ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
409415
in trouble since we might need the source address for further
410416
processing of the error. */
411417

412-
read_lock(&ip6_tnl_lock);
418+
rcu_read_lock();
413419
if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr,
414420
&ipv6h->saddr)) == NULL)
415421
goto out;
@@ -482,7 +488,7 @@ ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
482488
*msg = rel_msg;
483489

484490
out:
485-
read_unlock(&ip6_tnl_lock);
491+
rcu_read_unlock();
486492
return err;
487493
}
488494

@@ -693,23 +699,23 @@ static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
693699
struct ip6_tnl *t;
694700
struct ipv6hdr *ipv6h = ipv6_hdr(skb);
695701

696-
read_lock(&ip6_tnl_lock);
702+
rcu_read_lock();
697703

698704
if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr,
699705
&ipv6h->daddr)) != NULL) {
700706
if (t->parms.proto != ipproto && t->parms.proto != 0) {
701-
read_unlock(&ip6_tnl_lock);
707+
rcu_read_unlock();
702708
goto discard;
703709
}
704710

705711
if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
706-
read_unlock(&ip6_tnl_lock);
712+
rcu_read_unlock();
707713
goto discard;
708714
}
709715

710716
if (!ip6_tnl_rcv_ctl(t)) {
711717
t->dev->stats.rx_dropped++;
712-
read_unlock(&ip6_tnl_lock);
718+
rcu_read_unlock();
713719
goto discard;
714720
}
715721
secpath_reset(skb);
@@ -727,10 +733,10 @@ static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
727733
t->dev->stats.rx_packets++;
728734
t->dev->stats.rx_bytes += skb->len;
729735
netif_rx(skb);
730-
read_unlock(&ip6_tnl_lock);
736+
rcu_read_unlock();
731737
return 0;
732738
}
733-
read_unlock(&ip6_tnl_lock);
739+
rcu_read_unlock();
734740
return 1;
735741

736742
discard:

0 commit comments

Comments
 (0)