net: clean up locking in inet_frag_find()
Subsystems (4)
PATCH 1/1 net: clean up locking in inet_frag_find()
It is weird to take the read lock outside of inet_frag_find() but release it inside... Also, the hash function doesn't need this lock.
---
net/ipv4/inet_fragment.c +1 -1
@@ -272,11 +272,11 @@ static struct inet_frag_queue *inet_frag_create(struct netns_frags *nf,struct inet_frag_queue *inet_frag_find (struct netns_frags *nf,struct inet_frags *f,void *key,unsigned int hash) - __releases(&f->lock) {struct inet_frag_queue *q;struct hlist_node *n; +read_lock (&f->lock);hlist_for_each_entry (q, n, &f->hash[hash], list) {if (q->net == nf && f->match (q, key)) {atomic_inc (&q->refcnt);
net/ipv4/ip_fragment.c +0 -2
@@ -295,9 +295,7 @@ static inline struct ipq *ip_find(struct net *net, struct iphdr *iph, u32 user) arg.iph = iph; arg.user = user; -read_lock (&ip4_frags.lock); hash =ipqhashfn (iph->id, iph->saddr, iph->daddr, iph->protocol); - q =inet_frag_find (&net->ipv4.frags, &ip4_frags, &arg, hash);if (q ==NULL ) goto out_nomem;
net/ipv6/netfilter/nf_conntrack_reasm.c +1 -2
@@ -175,9 +175,8 @@ static inline struct frag_queue *fq_find(struct net *net, __be32 id, arg.src = src; arg.dst = dst; -read_lock_bh (&nf_frags.lock); hash =inet6_hash_frag (id, src, dst, nf_frags.rnd); - +local_bh_disable (); q =inet_frag_find (&net->nf_frag.frags, &nf_frags, &arg, hash);local_bh_enable ();if (q ==NULL )
net/ipv6/reassembly.c +0 -2
@@ -193,9 +193,7 @@ fq_find(struct net *net, __be32 id, const struct in6_addr *src, const struct in6 arg.src = src; arg.dst = dst; -read_lock (&ip6_frags.lock); hash =inet6_hash_frag (id, src, dst, ip6_frags.rnd); - q =inet_frag_find (&net->ipv6.frags, &ip6_frags, &arg, hash);if (q ==NULL )return NULL ;
Quoted 3 lines · Cong Wang
It is weird to take the read lock outside of
inet_frag_find()but release it inside... Also, the hash function doesn't need this lock.
This patch is wrong. hash needs the lock.
Quoted 8 lines · Eric Dumazet
Quoted 3 lines · Cong Wang
It is weird to take the read lock outside of
inet_frag_find()but release it inside... Also, the hash function doesn't need this lock.This patch is wrong. hash needs the lock.
Indeed, I missed .rnd...
I am cooking a better patch now.
Thanks!
PATCH v2 1/1 net: clean up locking in inet_frag_find()
It is weird to take the read lock outside of inet_frag_find() but release it inside... This can be improved by refactoring the code, that is, introducing inet{4,6}_frag_find() which call the their own hash function, inet{4,6}_hash_frag(), hiding the details from their callers.
--- include/net/inet_frag.h | 17 +++++- include/net/ipv6.h | 3 - net/ipv4/inet_fragment.c | 82 +++++++++++++++++++++++++++++-- net/ipv4/ip_fragment.c | 16 +----- net/ipv6/netfilter/nf_conntrack_reasm.c | 7 +-- net/ipv6/reassembly.c | 34 +------------ 6 files changed, 97 insertions(+), 62 deletions(-)
include/net/inet_frag.h +14 -3
@@ -1,6 +1,8 @@ #ifndef __NET_FRAG_H__ #define __NET_FRAG_H__ +#include <linux/in6.h> + struct netns_frags { int nqueues; atomic_t mem;
@@ -62,9 +64,18 @@ void inet_frag_kill(struct inet_frag_queue *q, struct inet_frags *f);void inet_frag_destroy (struct inet_frag_queue *q,struct inet_frags *f,int *work);int inet_frag_evictor (struct netns_frags *nf,struct inet_frags *f,bool force); -struct inet_frag_queue *inet_frag_find (struct netns_frags *nf, -struct inet_frags *f,void *key,unsigned int hash) - __releases(&f->lock); + +extern unsigned int inet4_hash_frag (__be16 id,__be32 saddr,__be32 daddr, +u8 prot,u32 rnd); +struct inet_frag_queue *inet4_frag_find (struct netns_frags *nf, +struct inet_frags *f,void *key); + +#if IS_ENABLED (CONFIG_IPV6 ) +extern unsigned int inet6_hash_frag (__be32 id,const struct in6_addr *saddr, +const struct in6_addr *daddr,u32 rnd); +struct inet_frag_queue *inet6_frag_find (struct netns_frags *nf, +struct inet_frags *f,void *key); +#endif static inline void inet_frag_put (struct inet_frag_queue *q,struct inet_frags *f) {
include/net/ipv6.h +0 -3
@@ -695,9 +695,6 @@ extern int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf);extern int ip6_mc_msfget (struct sock *sk,struct group_filter *gsf,struct group_filter __user *optval,int __user *optlen); -extern unsigned int inet6_hash_frag (__be32 id,const struct in6_addr *saddr, -const struct in6_addr *daddr,u32 rnd); -#ifdef CONFIG_PROC_FS extern int ac6_proc_init (struct net *net);extern void ac6_proc_exit (struct net *net);
net/ipv4/inet_fragment.c +78 -4
106 lines not on this page · raw message
net/ipv4/ip_fragment.c +2 -14
@@ -32,7 +32,6 @@#include <linux/ip.h> #include <linux/icmp.h> #include <linux/netdevice.h> -#include <linux/jhash.h> #include <linux/random.h> #include <linux/slab.h> #include <net/route.h>
@@ -133,19 +132,12 @@ struct ip4_create_arg {u32 user; }; -static unsigned int ipqhashfn (__be16 id,__be32 saddr,__be32 daddr,u8 prot) -{ -return jhash_3words ((__force u32 )id <<16 | prot, - (__force u32 )saddr, (__force u32 )daddr, - ip4_frags.rnd) & (INETFRAGS_HASHSZ -1 ); -} -static unsigned int ip4_hashfn (struct inet_frag_queue *q) {struct ipq *ipq; ipq = container_of(q, struct ipq, q); -return ipqhashfn (ipq->id, ipq->saddr, ipq->daddr, ipq->protocol); +return inet4_hash_frag (ipq->id, ipq->saddr, ipq->daddr, ipq->protocol, ip4_frags.rnd); }static bool ip4_frag_match (struct inet_frag_queue *q,void *a)
@@ -290,15 +282,11 @@ static inline struct ipq *ip_find(struct net *net, struct iphdr *iph, u32 user) {struct inet_frag_queue *q;struct ip4_create_arg arg; -unsigned int hash; arg.iph = iph; arg.user = user; -read_lock (&ip4_frags.lock); - hash =ipqhashfn (iph->id, iph->saddr, iph->daddr, iph->protocol); - - q =inet_frag_find (&net->ipv4.frags, &ip4_frags, &arg, hash); + q =inet4_frag_find (&net->ipv4.frags, &ip4_frags, &arg);if (q ==NULL ) goto out_nomem;
net/ipv6/netfilter/nf_conntrack_reasm.c +2 -5
@@ -168,17 +168,14 @@ static inline struct frag_queue *fq_find(struct net *net, __be32 id, {struct inet_frag_queue *q;struct ip6_create_arg arg; -unsigned int hash; arg.id = id; arg.user = user; arg.src = src; arg.dst = dst; -read_lock_bh (&nf_frags.lock); - hash =inet6_hash_frag (id, src, dst, nf_frags.rnd); - - q =inet_frag_find (&net->nf_frag.frags, &nf_frags, &arg, hash); +local_bh_disable (); + q =inet6_frag_find (&net->nf_frag.frags, &nf_frags, &arg);local_bh_enable ();if (q ==NULL ) goto oom;
net/ipv6/reassembly.c +1 -33
@@ -70,34 +70,6 @@ static struct inet_frags ip6_frags;static int ip6_frag_reasm (struct frag_queue *fq,struct sk_buff *prev,struct net_device *dev); -/* -* callers should be careful not to use the hash value outside the ipfrag_lock -* as doing so could race with ipfrag_hash_rnd being recalculated. -*/ -unsigned int inet6_hash_frag (__be32 id,const struct in6_addr *saddr, -const struct in6_addr *daddr,u32 rnd) -{ -u32 c; - - c =jhash_3words ((__force u32 )saddr->s6_addr32[0 ], - (__force u32 )saddr->s6_addr32[1 ], - (__force u32 )saddr->s6_addr32[2 ], - rnd); - - c =jhash_3words ((__force u32 )saddr->s6_addr32[3 ], - (__force u32 )daddr->s6_addr32[0 ], - (__force u32 )daddr->s6_addr32[1 ], - c); - - c =jhash_3words ((__force u32 )daddr->s6_addr32[2 ], - (__force u32 )daddr->s6_addr32[3 ], - (__force u32 )id, - c); - -return c & (INETFRAGS_HASHSZ -1 ); -} -EXPORT_SYMBOL_GPL (inet6_hash_frag); -static unsigned int ip6_hashfn (struct inet_frag_queue *q) {struct frag_queue *fq;
@@ -186,17 +158,13 @@ fq_find(struct net *net, __be32 id, const struct in6_addr *src, const struct in6 {struct inet_frag_queue *q;struct ip6_create_arg arg; -unsigned int hash; arg.id = id; arg.user =IP6_DEFRAG_LOCAL_DELIVER ; arg.src = src; arg.dst = dst; -read_lock (&ip6_frags.lock); - hash =inet6_hash_frag (id, src, dst, ip6_frags.rnd); - - q =inet_frag_find (&net->ipv6.frags, &ip6_frags, &arg, hash); + q =inet6_frag_find (&net->ipv6.frags, &ip6_frags, &arg);if (q ==NULL )return NULL ;
Could we please hold back on this cleanup patch, as I have a stack of 9 patches modifying this area.
If people find this cleanup useful/correct?, I can integrate it into my patch stack...
--Jesper
Quoted 5 lines · Cong Wang
It is weird to take the read lock outside of
inet_frag_find()but release it inside... This can be improved by refactoring the code, that is, introducing inet{4,6}_frag_find() which call the their own hash function, inet{4,6}_hash_frag(), hiding the details from their callers.
Quoted 12 lines · Cong Wang
It is weird to take the read lock outside of
inet_frag_find()but release it inside... This can be improved by refactoring the code, that is, introducing inet{4,6}_frag_find() which call the their own hash function, inet{4,6}_hash_frag(), hiding the details from their callers.Cc Eric Dumazet <[email protected]>Cc Patrick McHardy <[email protected]>Cc Pablo Neira Ayuso <[email protected]>Cc David S. Miller <[email protected]>Signed-off-by Cong Wang <[email protected]>
Quoted 8 lines
--- include/net/inet_frag.h | 17 +++++- include/net/ipv6.h | 3 - net/ipv4/inet_fragment.c | 82 +++++++++++++++++++++++++++++-- net/ipv4/ip_fragment.c | 16 +----- net/ipv6/netfilter/nf_conntrack_reasm.c | 7 +-- net/ipv6/reassembly.c | 34 +------------ 6 files changed, 97 insertions(+), 62 deletions(-)
Not clear to me its a win, as it adds 35 LOC. Nobody really complained of this locking schem in the past.
Also Jesper is working on this stuff, so you dont really ease its work.
Quoted 23 lines · Eric Dumazet
Quoted 12 lines · Cong Wang
It is weird to take the read lock outside of
inet_frag_find()but release it inside... This can be improved by refactoring the code, that is, introducing inet{4,6}_frag_find() which call the their own hash function, inet{4,6}_hash_frag(), hiding the details from their callers.Cc Eric Dumazet <[email protected]>Cc Patrick McHardy <[email protected]>Cc Pablo Neira Ayuso <[email protected]>Cc David S. Miller <[email protected]>Signed-off-by Cong Wang <[email protected]>Quoted 8 lines
--- include/net/inet_frag.h | 17 +++++- include/net/ipv6.h | 3 - net/ipv4/inet_fragment.c | 82 +++++++++++++++++++++++++++++-- net/ipv4/ip_fragment.c | 16 +----- net/ipv6/netfilter/nf_conntrack_reasm.c | 7 +-- net/ipv6/reassembly.c | 34 +------------ 6 files changed, 97 insertions(+), 62 deletions(-)
Quoted 2 lines
Not clear to me its a win, as it adds 35 LOC. Nobody really complained of this locking schem in the past.
Yeah, seems every people here is able to read any ugly code, except me. :-)
Quoted 4 lines
Also Jesper is working on this stuff, so you dont really ease its work.
I will rebase his tree for him, no worry, handling conflicts is part of my life everyday (I am a heavy git rebase -i user).