net: Fix __inet_inherit_port() to correctly increment bsockets and num_owners
Subsystems (3)
PATCH 1/1 net: Fix __inet_inherit_port() to correctly increment bsockets and num_owners
inet sockets corresponding to passive connections are added to the bind hash using ___inet_inherit_port(). These sockets are later removed from the bind hash using __inet_put_port(). These two functions are not exactly symmetrical. __inet_put_port() decrements hashinfo->bsockets and tb->num_owners, whereas ___inet_inherit_port() does not increment them. This results in both of these going to -ve values.
This patch fixes this by calling inet_bind_hash() from ___inet_inherit_port(), which does the right thing.
---
net/ipv4/inet_hashtables.c renamed from net/ipv4/inet_hashtables.c.orig +2 -4
@@ -107,12 +107,10 @@ void __inet_inherit_port(struct sock *skconst int bhash =inet_bhashfn (sock_net (sk),inet_sk (child)->inet_num, table->bhash_size);struct inet_bind_hashbucket *head = &table->bhash[bhash]; -struct inet_bind_bucket *tb;spin_lock (&head->lock); - tb =inet_csk (sk)->icsk_bind_hash; -sk_add_bind_node (child, &tb->owners); -inet_csk (child)->icsk_bind_hash = tb; +inet_bind_hash (child,inet_csk (sk)->icsk_bind_hash, +inet_sk (child)->inet_num);spin_unlock (&head->lock); }EXPORT_SYMBOL_GPL (__inet_inherit_port);
---
Quoted 22 lines · Nagendra Tomar
inet sockets corresponding to passive connections are added to the bind hash using
___inet_inherit_port(). These sockets are later removed from the bind hash using__inet_put_port(). These two functions are not exactly symmetrical.__inet_put_port()decrementshashinfo->bsocketsandtb->num_owners, whereas___inet_inherit_port()does not increment them. This results in both of these going to -ve values.This patch fixes this by calling
inet_bind_hash()from___inet_inherit_port(), which does the right thing.Signed-off-by Nagendra Singh Tomar <[email protected]>---
net/ipv4/inet_hashtables.c renamed from net/ipv4/inet_hashtables.c.orig +0
-1@@ -107,12 +107,10 @@ void __inet_inherit_port(struct sock *skconst int bhash =inet_bhashfn (sock_net (sk),inet_sk (child)->inet_num, table->bhash_size);struct inet_bind_hashbucket *head = &table->bhash[bhash]; -struct inet_bind_bucket *tb;
net/ipv4/inet_hashtables.c void __inet_inherit_port(struct sock *sk
Quoted 10 lines
net/ipv4/inet_hashtables.c renamed from net/ipv4/inet_hashtables.c.orig +2
-4spin_lock (&head->lock); - tb =inet_csk (sk)->icsk_bind_hash; -sk_add_bind_node (child, &tb->owners); -inet_csk (child)->icsk_bind_hash = tb; +inet_bind_hash (child,inet_csk (sk)->icsk_bind_hash, +inet_sk (child)->inet_num);spin_unlock (&head->lock); }EXPORT_SYMBOL_GPL (__inet_inherit_port); ---
Interesting, how did you notice it is wrong ?
Quoted 3 lines · Eric Dumazet
Interesting, how did you notice it is wrong ?
Bumped on it while reading related code.
Quoted 11 lines · Nagendra Tomar
Quoted 3 lines · Eric Dumazet
Interesting, how did you notice it is wrong ?
Bumped on it while reading related code.
OK so you'll have to make a proof, because current code seems to work ;)
Quoted 5 lines · Eric Dumazet
OK so you'll have to make a proof, because current code seems to work ;)
ok, so I printed hashinfo->bsockets and tb->num_owners inside __inet_put_port() and I could see both of them to be -ve. All we need to do is establish and terminate a connection. I used netcat for that.
The only place 'bsockets' and 'num_owners' are used is inet_csk_get_port() and the only effect that they might have is on the choice of the port to be used for binding. 'bsockets' is used as a hint to stop searching for a free port (and instead share an already used port) when we know that all the ports could be used up. 'num_owners' is used to find the port which is least shared (to balance the 'owners' list) in case we need to share a port.
Since both of these are used as optimizations (in the bind path), they do not affect correctness and hence the code works even with these values not being updated correctly.
Thanks,
Tomar
From: Nagendra Tomar <[email protected]>
Quoted 22 lines · Nagendra Tomar
Quoted 5 lines · Eric Dumazet
OK so you'll have to make a proof, because current code seems to work ;)
ok, so I printed
hashinfo->bsocketsandtb->num_ownersinside__inet_put_port()and I could see both of them to be -ve. All we need to do is establish and terminate a connection. I used netcat for that.The only place 'bsockets' and '
num_owners' are used isinet_csk_get_port()and the only effect that they might have is on the choice of the port to be used for binding.
'bsockets' is used as a hint to stop searching for a free port (and instead share an already used port) when we know that all the ports could be used up.
'num_owners' is used to find the port which is least shared (to balance the 'owners' list) in case we need to share a port.
Quoted 3 lines
Since both of these are used as optimizations (in the bind path), they do not affect correctness and hence the code works even with these values not being updated correctly.
Hmm, thanks for clarification.
bsockets / mnum_owners iscount is indeed an 'optimization' problem.
Problem is your patch is not applicable to current tree.
In order to submit it to stable team, you should first post a patch for next/current kernel (net-next-2.6 tree).
David will decide if its net-2.6 material or not.
You could add in your changelog the problem comes from commit a9d8f9110d7e953c (inet: Allowing more than 64k connections and heavily optimize bind(0)), included in 2.6.30, to ease stable team work.
On current tree your patch would be :
net/ipv4/inet_hashtables.c +1 -2
@@ -133,8 +133,7 @@ int __inet_inherit_port(struct sock *sk, struct sock *child) } } } -sk_add_bind_node (child, &tb->owners); -inet_csk (child)->icsk_bind_hash = tb; +inet_bind_hash (child, tb, port);spin_unlock (&head->lock);return 0 ;
Quoted 7 lines · Eric Dumazet
Problem is your patch is not applicable to current tree.
In order to submit it to stable team, you should first post a patch for
next/current kernel (net-next-2.6 tree).
Thanks, Erik.
I'd made the patch against 2.6.36.1 which is the latest stable kernel per kernel.org. I thought that was the right kernel version to make a patch against. I do not use git. Shall I make a patch against linux-next as it appears in kernel.org.
Quoted 29 lines
David will decide if its net-2.6 material or not.
You could add in your changelog the problem comes from commit
a9d8f9110d7e953c(inet: Allowing more than 64k connections and heavily
optimize bind(0)), included in 2.6.30, to ease stable team work.On current tree your patch would be :
net/ipv4/inet_hashtables.c +0
-2@@ -133,8 +133,7 @@ int __inet_inherit_port(struct sock *sk, struct sock *child) } } } - sk_add_bind_node(child, &tb->owners); - inet_csk(child)->icsk_bind_hash =tb;
+inet_bind_hash(child, tb, port);
spin_unlock(&head->lock);
Quoted 4 lines
return 0;
Thanks,
Tomar
Hi.
Quoted 23 lines · Eric Dumazet
Quoted 13 lines
ok, so I printed
hashinfo->bsocketsandtb->num_ownersinside__inet_put_port()and I could see both of them to be -ve. All we need to do is establish and terminate a connection. I used netcat for that.The only place 'bsockets' and '
num_owners' are used isinet_csk_get_port()and the only effect that they might have is on the choice of the port to be used for binding.
'bsockets' is used as a hint to stop searching for a free port (and instead share an already used port) when we know that all the ports could be used up.
'num_owners' is used to find the port which is least shared (to balance the 'owners' list) in case we need to share a port.Quoted 3 lines
Since both of these are used as optimizations (in the bind path), they do not affect correctness and hence the code works even with these values not being updated correctly.
Hmm, thanks for clarification.
bsockets /
mnum_ownersiscount is indeed an 'optimization' problem.Problem is your patch is not applicable to current tree.
Quoted 8 lines
In order to submit it to stable team, you should first post a patch for next/current kernel (net-next-2.6 tree).
David will decide if its net-2.6 material or not.
You could add in your changelog the problem comes from commit
a9d8f9110d7e953c(inet: Allowing more than 64k connections and heavily optimize bind(0)), included in 2.6.30, to ease stable team work.
Frankly I did not find how those optimizations made a bug as well as what is this bug about from given description, but I'm glad it is resolved now :)
Signature · 1 line
Evgeniy Polyakov
Quoted 5 lines · Evgeniy Polyakov
Frankly I did not find how those optimizations made a bug as well as
what is this bug about from given description, but I'm glad it is resolved now :)
I'm not sure of what all went into the "optimization" patch, but the bug is not due to the optimization per-se. As my original post says, it is due to the 'bsockets' and 'num_owners' not being incremented inside __inet_inherit_port(), where it should have been, as __inet_put_port() decrements them on port release, which causes the imbalance.
Thanks,
Tomar
Quoted 7 lines · Nagendra Tomar
Quoted 5 lines
Frankly I did not find how those optimizations made a bug as well as
what is this bug about from given description, but I'm glad it is resolved now :)I'm not sure of what all went into the "optimization" patch, but the bug is not due to the optimization per-se. As my original post says, it is due to the 'bsockets' and '
num_owners' not being incremented inside__inet_inherit_port(), where it should have been, as__inet_put_port()decrements them on port release, which causes the imbalance.
Argh, I see, thanks a lot for explanation!
Signature · 1 line
Evgeniy Polyakov
Re: [PATCH] net-next: Fix __inet_inherit_port() to correctly increment bsockets and num_owners
In reply to Eric DumazetOk, here is the patch against net-next.
Thanks,
Tomar
---
inet sockets corresponding to passive connections are added to the bind hash using ___inet_inherit_port(). These sockets are later removed from the bind hash using __inet_put_port(). These two functions are not exactly symmetrical. __inet_put_port() decrements hashinfo->bsockets and tb->num_owners, whereas ___inet_inherit_port() does not increment them. This results in both of these going to -ve values.
This patch fixes this by calling inet_bind_hash() from ___inet_inherit_port(), which does the right thing.
'bsockets' and 'num_owners' were introduced by commit a9d8f9110d7e953c (inet: Allowing more than 64k connections and heavily optimize bind(0))
---
net/ipv4/inet_hashtables.c renamed from net/ipv4/inet_hashtables.c.orig +1 -2
@@ -133,8 +133,7 @@ int __inet_inherit_port(struct sock *sk, } } } -sk_add_bind_node (child, &tb->owners); -inet_csk (child)->icsk_bind_hash = tb; +inet_bind_hash (child, tb, port);spin_unlock (&head->lock);return 0 ;
---