diff options
author | Eric Dumazet <dada1@cosmosbay.com> | 2008-10-29 05:11:14 -0400 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2008-10-29 05:11:14 -0400 |
commit | 271b72c7fa82c2c7a795bc16896149933110672d (patch) | |
tree | 5634b95c04b4a7ac9babf2d8ac34cfb6c38a8f83 /net/ipv4/udp.c | |
parent | 645ca708f936b2fbeb79e52d7823e3eb2c0905f8 (diff) |
udp: RCU handling for Unicast packets.
Goals are :
1) Optimizing handling of incoming Unicast UDP frames, so that no memory
writes should happen in the fast path.
Note: Multicasts and broadcasts still will need to take a lock,
because doing a full lockless lookup in this case is difficult.
2) No expensive operations in the socket bind/unhash phases :
- No expensive synchronize_rcu() calls.
- No added rcu_head in socket structure, increasing memory needs,
but more important, forcing us to use call_rcu() calls,
that have the bad property of making sockets structure cold.
(rcu grace period between socket freeing and its potential reuse
make this socket being cold in CPU cache).
David did a previous patch using call_rcu() and noticed a 20%
impact on TCP connection rates.
Quoting Cristopher Lameter :
"Right. That results in cacheline cooldown. You'd want to recycle
the object as they are cache hot on a per cpu basis. That is screwed
up by the delayed regular rcu processing. We have seen multiple
regressions due to cacheline cooldown.
The only choice in cacheline hot sensitive areas is to deal with the
complexity that comes with SLAB_DESTROY_BY_RCU or give up on RCU."
- Because udp sockets are allocated from dedicated kmem_cache,
use of SLAB_DESTROY_BY_RCU can help here.
Theory of operation :
---------------------
As the lookup is lockfree (using rcu_read_lock()/rcu_read_unlock()),
special attention must be taken by readers and writers.
Use of SLAB_DESTROY_BY_RCU is tricky too, because a socket can be freed,
reused, inserted in a different chain or in worst case in the same chain
while readers could do lookups in the same time.
In order to avoid loops, a reader must check each socket found in a chain
really belongs to the chain the reader was traversing. If it finds a
mismatch, lookup must start again at the begining. This *restart* loop
is the reason we had to use rdlock for the multicast case, because
we dont want to send same message several times to the same socket.
We use RCU only for fast path.
Thus, /proc/net/udp still takes spinlocks.
Signed-off-by: Eric Dumazet <dada1@cosmosbay.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net/ipv4/udp.c')
-rw-r--r-- | net/ipv4/udp.c | 35 |
1 files changed, 26 insertions, 9 deletions
diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c index 2a6c491f97d7..0ea974bf7962 100644 --- a/net/ipv4/udp.c +++ b/net/ipv4/udp.c | |||
@@ -187,7 +187,7 @@ int udp_lib_get_port(struct sock *sk, unsigned short snum, | |||
187 | inet_sk(sk)->num = snum; | 187 | inet_sk(sk)->num = snum; |
188 | sk->sk_hash = snum; | 188 | sk->sk_hash = snum; |
189 | if (sk_unhashed(sk)) { | 189 | if (sk_unhashed(sk)) { |
190 | sk_add_node(sk, &hslot->head); | 190 | sk_add_node_rcu(sk, &hslot->head); |
191 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); | 191 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); |
192 | } | 192 | } |
193 | error = 0; | 193 | error = 0; |
@@ -253,15 +253,24 @@ static struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, | |||
253 | __be16 sport, __be32 daddr, __be16 dport, | 253 | __be16 sport, __be32 daddr, __be16 dport, |
254 | int dif, struct udp_table *udptable) | 254 | int dif, struct udp_table *udptable) |
255 | { | 255 | { |
256 | struct sock *sk, *result = NULL; | 256 | struct sock *sk, *result; |
257 | struct hlist_node *node; | 257 | struct hlist_node *node; |
258 | unsigned short hnum = ntohs(dport); | 258 | unsigned short hnum = ntohs(dport); |
259 | unsigned int hash = udp_hashfn(net, hnum); | 259 | unsigned int hash = udp_hashfn(net, hnum); |
260 | struct udp_hslot *hslot = &udptable->hash[hash]; | 260 | struct udp_hslot *hslot = &udptable->hash[hash]; |
261 | int score, badness = -1; | 261 | int score, badness; |
262 | 262 | ||
263 | spin_lock(&hslot->lock); | 263 | rcu_read_lock(); |
264 | sk_for_each(sk, node, &hslot->head) { | 264 | begin: |
265 | result = NULL; | ||
266 | badness = -1; | ||
267 | sk_for_each_rcu(sk, node, &hslot->head) { | ||
268 | /* | ||
269 | * lockless reader, and SLAB_DESTROY_BY_RCU items: | ||
270 | * We must check this item was not moved to another chain | ||
271 | */ | ||
272 | if (udp_hashfn(net, sk->sk_hash) != hash) | ||
273 | goto begin; | ||
265 | score = compute_score(sk, net, saddr, hnum, sport, | 274 | score = compute_score(sk, net, saddr, hnum, sport, |
266 | daddr, dport, dif); | 275 | daddr, dport, dif); |
267 | if (score > badness) { | 276 | if (score > badness) { |
@@ -269,9 +278,16 @@ static struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, | |||
269 | badness = score; | 278 | badness = score; |
270 | } | 279 | } |
271 | } | 280 | } |
272 | if (result) | 281 | if (result) { |
273 | sock_hold(result); | 282 | if (unlikely(!atomic_inc_not_zero(&result->sk_refcnt))) |
274 | spin_unlock(&hslot->lock); | 283 | result = NULL; |
284 | else if (unlikely(compute_score(result, net, saddr, hnum, sport, | ||
285 | daddr, dport, dif) < badness)) { | ||
286 | sock_put(result); | ||
287 | goto begin; | ||
288 | } | ||
289 | } | ||
290 | rcu_read_unlock(); | ||
275 | return result; | 291 | return result; |
276 | } | 292 | } |
277 | 293 | ||
@@ -953,7 +969,7 @@ void udp_lib_unhash(struct sock *sk) | |||
953 | struct udp_hslot *hslot = &udptable->hash[hash]; | 969 | struct udp_hslot *hslot = &udptable->hash[hash]; |
954 | 970 | ||
955 | spin_lock(&hslot->lock); | 971 | spin_lock(&hslot->lock); |
956 | if (sk_del_node_init(sk)) { | 972 | if (sk_del_node_init_rcu(sk)) { |
957 | inet_sk(sk)->num = 0; | 973 | inet_sk(sk)->num = 0; |
958 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); | 974 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); |
959 | } | 975 | } |
@@ -1517,6 +1533,7 @@ struct proto udp_prot = { | |||
1517 | .sysctl_wmem = &sysctl_udp_wmem_min, | 1533 | .sysctl_wmem = &sysctl_udp_wmem_min, |
1518 | .sysctl_rmem = &sysctl_udp_rmem_min, | 1534 | .sysctl_rmem = &sysctl_udp_rmem_min, |
1519 | .obj_size = sizeof(struct udp_sock), | 1535 | .obj_size = sizeof(struct udp_sock), |
1536 | .slab_flags = SLAB_DESTROY_BY_RCU, | ||
1520 | .h.udp_table = &udp_table, | 1537 | .h.udp_table = &udp_table, |
1521 | #ifdef CONFIG_COMPAT | 1538 | #ifdef CONFIG_COMPAT |
1522 | .compat_setsockopt = compat_udp_setsockopt, | 1539 | .compat_setsockopt = compat_udp_setsockopt, |