2019-05-19 15:08:55 +03:00
// SPDX-License-Identifier: GPL-2.0-only
2005-08-10 07:09:46 +04:00
/*
* INET An implementation of the TCP / IP protocol suite for the LINUX
* operating system . INET is implemented using the BSD Socket
* interface as the means of communication with the user level .
*
* Generic TIME_WAIT sockets functions
*
* From code orinally in TCP
*/
2007-08-29 02:50:33 +04:00
# include <linux/kernel.h>
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 11:04:11 +03:00
# include <linux/slab.h>
2011-05-27 17:12:25 +04:00
# include <linux/module.h>
2005-08-10 07:09:46 +04:00
# include <net/inet_hashtables.h>
# include <net/inet_timewait_sock.h>
2005-08-10 07:45:03 +04:00
# include <net/ip.h>
2005-08-10 07:09:46 +04:00
2009-12-03 01:31:19 +03:00
2009-12-09 07:19:53 +03:00
/**
* inet_twsk_bind_unhash - unhash a timewait socket from bind hash
* @ tw : timewait socket
* @ hashinfo : hashinfo pointer
*
* unhash a timewait socket from bind hash , if hashed .
* bind hash lock must be held by caller .
* Returns 1 if caller should call inet_twsk_put ( ) after lock release .
2009-12-04 06:47:42 +03:00
*/
2015-07-09 00:28:29 +03:00
void inet_twsk_bind_unhash ( struct inet_timewait_sock * tw ,
2009-12-04 06:47:42 +03:00
struct inet_hashinfo * hashinfo )
{
2022-12-26 16:27:52 +03:00
struct inet_bind2_bucket * tb2 = tw - > tw_tb2 ;
2009-12-04 06:47:42 +03:00
struct inet_bind_bucket * tb = tw - > tw_tb ;
if ( ! tb )
2015-07-09 00:28:29 +03:00
return ;
2009-12-04 06:47:42 +03:00
2023-12-19 03:18:32 +03:00
__sk_del_bind_node ( ( struct sock * ) tw ) ;
2009-12-04 06:47:42 +03:00
tw - > tw_tb = NULL ;
2022-12-26 16:27:52 +03:00
tw - > tw_tb2 = NULL ;
inet_bind2_bucket_destroy ( hashinfo - > bind2_bucket_cachep , tb2 ) ;
2023-12-19 03:18:30 +03:00
inet_bind_bucket_destroy ( hashinfo - > bind_bucket_cachep , tb ) ;
2022-12-26 16:27:52 +03:00
2015-07-09 00:28:29 +03:00
__sock_put ( ( struct sock * ) tw ) ;
2009-12-04 06:47:42 +03:00
}
2005-08-10 07:09:46 +04:00
/* Must be called with locally disabled BHs. */
tcp/dccp: get rid of central timewait timer
Using a timer wheel for timewait sockets was nice ~15 years ago when
memory was expensive and machines had a single processor.
This does not scale, code is ugly and source of huge latencies
(Typically 30 ms have been seen, cpus spinning on death_lock spinlock.)
We can afford to use an extra 64 bytes per timewait sock and spread
timewait load to all cpus to have better behavior.
Tested:
On following test, /proc/sys/net/ipv4/tcp_tw_recycle is set to 1
on the target (lpaa24)
Before patch :
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
419594
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
437171
While test is running, we can observe 25 or even 33 ms latencies.
lpaa24:~# ping -c 1000 -i 0.02 -qn lpaa23
...
1000 packets transmitted, 1000 received, 0% packet loss, time 20601ms
rtt min/avg/max/mdev = 0.020/0.217/25.771/1.535 ms, pipe 2
lpaa24:~# ping -c 1000 -i 0.02 -qn lpaa23
...
1000 packets transmitted, 1000 received, 0% packet loss, time 20702ms
rtt min/avg/max/mdev = 0.019/0.183/33.761/1.441 ms, pipe 2
After patch :
About 90% increase of throughput :
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
810442
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
800992
And latencies are kept to minimal values during this load, even
if network utilization is 90% higher :
lpaa24:~# ping -c 1000 -i 0.02 -qn lpaa23
...
1000 packets transmitted, 1000 received, 0% packet loss, time 19991ms
rtt min/avg/max/mdev = 0.023/0.064/0.360/0.042 ms
Signed-off-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2015-04-13 04:51:09 +03:00
static void inet_twsk_kill ( struct inet_timewait_sock * tw )
2005-08-10 07:09:46 +04:00
{
tcp/dccp: get rid of central timewait timer
Using a timer wheel for timewait sockets was nice ~15 years ago when
memory was expensive and machines had a single processor.
This does not scale, code is ugly and source of huge latencies
(Typically 30 ms have been seen, cpus spinning on death_lock spinlock.)
We can afford to use an extra 64 bytes per timewait sock and spread
timewait load to all cpus to have better behavior.
Tested:
On following test, /proc/sys/net/ipv4/tcp_tw_recycle is set to 1
on the target (lpaa24)
Before patch :
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
419594
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
437171
While test is running, we can observe 25 or even 33 ms latencies.
lpaa24:~# ping -c 1000 -i 0.02 -qn lpaa23
...
1000 packets transmitted, 1000 received, 0% packet loss, time 20601ms
rtt min/avg/max/mdev = 0.020/0.217/25.771/1.535 ms, pipe 2
lpaa24:~# ping -c 1000 -i 0.02 -qn lpaa23
...
1000 packets transmitted, 1000 received, 0% packet loss, time 20702ms
rtt min/avg/max/mdev = 0.019/0.183/33.761/1.441 ms, pipe 2
After patch :
About 90% increase of throughput :
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
810442
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
800992
And latencies are kept to minimal values during this load, even
if network utilization is 90% higher :
lpaa24:~# ping -c 1000 -i 0.02 -qn lpaa23
...
1000 packets transmitted, 1000 received, 0% packet loss, time 19991ms
rtt min/avg/max/mdev = 0.023/0.064/0.360/0.042 ms
Signed-off-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2015-04-13 04:51:09 +03:00
struct inet_hashinfo * hashinfo = tw - > tw_dr - > hashinfo ;
2008-11-21 07:39:09 +03:00
spinlock_t * lock = inet_ehash_lockp ( hashinfo , tw - > tw_hash ) ;
2022-12-26 16:27:52 +03:00
struct inet_bind_hashbucket * bhead , * bhead2 ;
2005-08-10 07:09:46 +04:00
2008-11-21 07:39:09 +03:00
spin_lock ( lock ) ;
2015-07-09 00:28:29 +03:00
sk_nulls_del_node_init_rcu ( ( struct sock * ) tw ) ;
2008-11-21 07:39:09 +03:00
spin_unlock ( lock ) ;
2005-08-10 07:09:46 +04:00
/* Disassociate with bind bucket. */
2022-05-13 00:14:56 +03:00
bhead = & hashinfo - > bhash [ inet_bhashfn ( twsk_net ( tw ) , tw - > tw_num ,
hashinfo - > bhash_size ) ] ;
2022-12-26 16:27:52 +03:00
bhead2 = inet_bhashfn_portaddr ( hashinfo , ( struct sock * ) tw ,
twsk_net ( tw ) , tw - > tw_num ) ;
2009-12-04 06:47:42 +03:00
2005-08-10 07:09:46 +04:00
spin_lock ( & bhead - > lock ) ;
2022-12-26 16:27:52 +03:00
spin_lock ( & bhead2 - > lock ) ;
2015-07-09 00:28:29 +03:00
inet_twsk_bind_unhash ( tw , hashinfo ) ;
2022-12-26 16:27:52 +03:00
spin_unlock ( & bhead2 - > lock ) ;
2005-08-10 07:09:46 +04:00
spin_unlock ( & bhead - > lock ) ;
2009-12-04 06:47:42 +03:00
2022-09-08 04:10:18 +03:00
refcount_dec ( & tw - > tw_dr - > tw_refcount ) ;
tcp/dccp: get rid of central timewait timer
Using a timer wheel for timewait sockets was nice ~15 years ago when
memory was expensive and machines had a single processor.
This does not scale, code is ugly and source of huge latencies
(Typically 30 ms have been seen, cpus spinning on death_lock spinlock.)
We can afford to use an extra 64 bytes per timewait sock and spread
timewait load to all cpus to have better behavior.
Tested:
On following test, /proc/sys/net/ipv4/tcp_tw_recycle is set to 1
on the target (lpaa24)
Before patch :
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
419594
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
437171
While test is running, we can observe 25 or even 33 ms latencies.
lpaa24:~# ping -c 1000 -i 0.02 -qn lpaa23
...
1000 packets transmitted, 1000 received, 0% packet loss, time 20601ms
rtt min/avg/max/mdev = 0.020/0.217/25.771/1.535 ms, pipe 2
lpaa24:~# ping -c 1000 -i 0.02 -qn lpaa23
...
1000 packets transmitted, 1000 received, 0% packet loss, time 20702ms
rtt min/avg/max/mdev = 0.019/0.183/33.761/1.441 ms, pipe 2
After patch :
About 90% increase of throughput :
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
810442
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
800992
And latencies are kept to minimal values during this load, even
if network utilization is 90% higher :
lpaa24:~# ping -c 1000 -i 0.02 -qn lpaa23
...
1000 packets transmitted, 1000 received, 0% packet loss, time 19991ms
rtt min/avg/max/mdev = 0.023/0.064/0.360/0.042 ms
Signed-off-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2015-04-13 04:51:09 +03:00
inet_twsk_put ( tw ) ;
2005-08-10 07:09:46 +04:00
}
tcp/dccp: remove twchain
TCP listener refactoring, part 3 :
Our goal is to hash SYN_RECV sockets into main ehash for fast lookup,
and parallel SYN processing.
Current inet_ehash_bucket contains two chains, one for ESTABLISH (and
friend states) sockets, another for TIME_WAIT sockets only.
As the hash table is sized to get at most one socket per bucket, it
makes little sense to have separate twchain, as it makes the lookup
slightly more complicated, and doubles hash table memory usage.
If we make sure all socket types have the lookup keys at the same
offsets, we can use a generic and faster lookup. It turns out TIME_WAIT
and ESTABLISHED sockets already have common lookup fields for IPv4.
[ INET_TW_MATCH() is no longer needed ]
I'll provide a follow-up to factorize IPv6 lookup as well, to remove
INET6_TW_MATCH()
This way, SYN_RECV pseudo sockets will be supported the same.
A new sock_gen_put() helper is added, doing either a sock_put() or
inet_twsk_put() [ and will support SYN_RECV later ].
Note this helper should only be called in real slow path, when rcu
lookup found a socket that was moved to another identity (freed/reused
immediately), but could eventually be used in other contexts, like
sock_edemux()
Before patch :
dmesg | grep "TCP established"
TCP established hash table entries: 524288 (order: 11, 8388608 bytes)
After patch :
TCP established hash table entries: 524288 (order: 10, 4194304 bytes)
Signed-off-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2013-10-03 11:22:02 +04:00
void inet_twsk_free ( struct inet_timewait_sock * tw )
2007-12-21 02:32:54 +03:00
{
2009-05-07 03:50:52 +04:00
struct module * owner = tw - > tw_prot - > owner ;
twsk_destructor ( ( struct sock * ) tw ) ;
kmem_cache_free ( tw - > tw_prot - > twsk_prot - > twsk_slab , tw ) ;
module_put ( owner ) ;
}
void inet_twsk_put ( struct inet_timewait_sock * tw )
{
2017-06-30 13:08:01 +03:00
if ( refcount_dec_and_test ( & tw - > tw_refcnt ) )
2009-05-07 03:50:52 +04:00
inet_twsk_free ( tw ) ;
2007-12-21 02:32:54 +03:00
}
EXPORT_SYMBOL_GPL ( inet_twsk_put ) ;
Revert "tcp: avoid the lookup process failing to get sk in ehash table"
This reverts commit 3f4ca5fafc08881d7a57daa20449d171f2887043.
Commit 3f4ca5fafc08 ("tcp: avoid the lookup process failing to get sk in
ehash table") reversed the order in how a socket is inserted into ehash
to fix an issue that ehash-lookup could fail when reqsk/full sk/twsk are
swapped. However, it introduced another lookup failure.
The full socket in ehash is allocated from a slab with SLAB_TYPESAFE_BY_RCU
and does not have SOCK_RCU_FREE, so the socket could be reused even while
it is being referenced on another CPU doing RCU lookup.
Let's say a socket is reused and inserted into the same hash bucket during
lookup. After the blamed commit, a new socket is inserted at the end of
the list. If that happens, we will skip sockets placed after the previous
position of the reused socket, resulting in ehash lookup failure.
As described in Documentation/RCU/rculist_nulls.rst, we should insert a
new socket at the head of the list to avoid such an issue.
This issue, the swap-lookup-failure, and another variant reported in [0]
can all be handled properly by adding a locked ehash lookup suggested by
Eric Dumazet [1].
However, this issue could occur for every packet, thus more likely than
the other two races, so let's revert the change for now.
Link: https://lore.kernel.org/netdev/20230606064306.9192-1-duanmuquan@baidu.com/ [0]
Link: https://lore.kernel.org/netdev/CANn89iK8snOz8TYOhhwfimC7ykYA78GA3Nyv8x06SZYa1nKdyA@mail.gmail.com/ [1]
Fixes: 3f4ca5fafc08 ("tcp: avoid the lookup process failing to get sk in ehash table")
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Link: https://lore.kernel.org/r/20230717215918.15723-1-kuniyu@amazon.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2023-07-18 00:59:18 +03:00
static void inet_twsk_add_node_rcu ( struct inet_timewait_sock * tw ,
struct hlist_nulls_head * list )
tcp/dccp: remove twchain
TCP listener refactoring, part 3 :
Our goal is to hash SYN_RECV sockets into main ehash for fast lookup,
and parallel SYN processing.
Current inet_ehash_bucket contains two chains, one for ESTABLISH (and
friend states) sockets, another for TIME_WAIT sockets only.
As the hash table is sized to get at most one socket per bucket, it
makes little sense to have separate twchain, as it makes the lookup
slightly more complicated, and doubles hash table memory usage.
If we make sure all socket types have the lookup keys at the same
offsets, we can use a generic and faster lookup. It turns out TIME_WAIT
and ESTABLISHED sockets already have common lookup fields for IPv4.
[ INET_TW_MATCH() is no longer needed ]
I'll provide a follow-up to factorize IPv6 lookup as well, to remove
INET6_TW_MATCH()
This way, SYN_RECV pseudo sockets will be supported the same.
A new sock_gen_put() helper is added, doing either a sock_put() or
inet_twsk_put() [ and will support SYN_RECV later ].
Note this helper should only be called in real slow path, when rcu
lookup found a socket that was moved to another identity (freed/reused
immediately), but could eventually be used in other contexts, like
sock_edemux()
Before patch :
dmesg | grep "TCP established"
TCP established hash table entries: 524288 (order: 11, 8388608 bytes)
After patch :
TCP established hash table entries: 524288 (order: 10, 4194304 bytes)
Signed-off-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2013-10-03 11:22:02 +04:00
{
Revert "tcp: avoid the lookup process failing to get sk in ehash table"
This reverts commit 3f4ca5fafc08881d7a57daa20449d171f2887043.
Commit 3f4ca5fafc08 ("tcp: avoid the lookup process failing to get sk in
ehash table") reversed the order in how a socket is inserted into ehash
to fix an issue that ehash-lookup could fail when reqsk/full sk/twsk are
swapped. However, it introduced another lookup failure.
The full socket in ehash is allocated from a slab with SLAB_TYPESAFE_BY_RCU
and does not have SOCK_RCU_FREE, so the socket could be reused even while
it is being referenced on another CPU doing RCU lookup.
Let's say a socket is reused and inserted into the same hash bucket during
lookup. After the blamed commit, a new socket is inserted at the end of
the list. If that happens, we will skip sockets placed after the previous
position of the reused socket, resulting in ehash lookup failure.
As described in Documentation/RCU/rculist_nulls.rst, we should insert a
new socket at the head of the list to avoid such an issue.
This issue, the swap-lookup-failure, and another variant reported in [0]
can all be handled properly by adding a locked ehash lookup suggested by
Eric Dumazet [1].
However, this issue could occur for every packet, thus more likely than
the other two races, so let's revert the change for now.
Link: https://lore.kernel.org/netdev/20230606064306.9192-1-duanmuquan@baidu.com/ [0]
Link: https://lore.kernel.org/netdev/CANn89iK8snOz8TYOhhwfimC7ykYA78GA3Nyv8x06SZYa1nKdyA@mail.gmail.com/ [1]
Fixes: 3f4ca5fafc08 ("tcp: avoid the lookup process failing to get sk in ehash table")
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Link: https://lore.kernel.org/r/20230717215918.15723-1-kuniyu@amazon.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2023-07-18 00:59:18 +03:00
hlist_nulls_add_head_rcu ( & tw - > tw_node , list ) ;
tcp/dccp: remove twchain
TCP listener refactoring, part 3 :
Our goal is to hash SYN_RECV sockets into main ehash for fast lookup,
and parallel SYN processing.
Current inet_ehash_bucket contains two chains, one for ESTABLISH (and
friend states) sockets, another for TIME_WAIT sockets only.
As the hash table is sized to get at most one socket per bucket, it
makes little sense to have separate twchain, as it makes the lookup
slightly more complicated, and doubles hash table memory usage.
If we make sure all socket types have the lookup keys at the same
offsets, we can use a generic and faster lookup. It turns out TIME_WAIT
and ESTABLISHED sockets already have common lookup fields for IPv4.
[ INET_TW_MATCH() is no longer needed ]
I'll provide a follow-up to factorize IPv6 lookup as well, to remove
INET6_TW_MATCH()
This way, SYN_RECV pseudo sockets will be supported the same.
A new sock_gen_put() helper is added, doing either a sock_put() or
inet_twsk_put() [ and will support SYN_RECV later ].
Note this helper should only be called in real slow path, when rcu
lookup found a socket that was moved to another identity (freed/reused
immediately), but could eventually be used in other contexts, like
sock_edemux()
Before patch :
dmesg | grep "TCP established"
TCP established hash table entries: 524288 (order: 11, 8388608 bytes)
After patch :
TCP established hash table entries: 524288 (order: 10, 4194304 bytes)
Signed-off-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2013-10-03 11:22:02 +04:00
}
2005-08-10 07:09:46 +04:00
/*
2017-12-02 02:28:44 +03:00
* Enter the time wait state . This is called with locally disabled BH .
2005-08-10 07:09:46 +04:00
* Essentially we whip up a timewait bucket , copy the relevant info into it
* from the SK , and mess with hash chains and list linkage .
*/
2017-12-12 08:25:12 +03:00
void inet_twsk_hashdance ( struct inet_timewait_sock * tw , struct sock * sk ,
2005-08-10 07:09:46 +04:00
struct inet_hashinfo * hashinfo )
{
const struct inet_sock * inet = inet_sk ( sk ) ;
2005-08-10 07:10:42 +04:00
const struct inet_connection_sock * icsk = inet_csk ( sk ) ;
[INET]: speedup inet (tcp/dccp) lookups
Arnaldo and I agreed it could be applied now, because I have other
pending patches depending on this one (Thank you Arnaldo)
(The other important patch moves skc_refcnt in a separate cache line,
so that the SMP/NUMA performance doesnt suffer from cache line ping pongs)
1) First some performance data :
--------------------------------
tcp_v4_rcv() wastes a *lot* of time in __inet_lookup_established()
The most time critical code is :
sk_for_each(sk, node, &head->chain) {
if (INET_MATCH(sk, acookie, saddr, daddr, ports, dif))
goto hit; /* You sunk my battleship! */
}
The sk_for_each() does use prefetch() hints but only the begining of
"struct sock" is prefetched.
As INET_MATCH first comparison uses inet_sk(__sk)->daddr, wich is far
away from the begining of "struct sock", it has to bring into CPU
cache cold cache line. Each iteration has to use at least 2 cache
lines.
This can be problematic if some chains are very long.
2) The goal
-----------
The idea I had is to change things so that INET_MATCH() may return
FALSE in 99% of cases only using the data already in the CPU cache,
using one cache line per iteration.
3) Description of the patch
---------------------------
Adds a new 'unsigned int skc_hash' field in 'struct sock_common',
filling a 32 bits hole on 64 bits platform.
struct sock_common {
unsigned short skc_family;
volatile unsigned char skc_state;
unsigned char skc_reuse;
int skc_bound_dev_if;
struct hlist_node skc_node;
struct hlist_node skc_bind_node;
atomic_t skc_refcnt;
+ unsigned int skc_hash;
struct proto *skc_prot;
};
Store in this 32 bits field the full hash, not masked by (ehash_size -
1) Using this full hash as the first comparison done in INET_MATCH
permits us immediatly skip the element without touching a second cache
line in case of a miss.
Suppress the sk_hashent/tw_hashent fields since skc_hash (aliased to
sk_hash and tw_hash) already contains the slot number if we mask with
(ehash_size - 1)
File include/net/inet_hashtables.h
64 bits platforms :
#define INET_MATCH(__sk, __hash, __cookie, __saddr, __daddr, __ports, __dif)\
(((__sk)->sk_hash == (__hash))
((*((__u64 *)&(inet_sk(__sk)->daddr)))== (__cookie)) && \
((*((__u32 *)&(inet_sk(__sk)->dport))) == (__ports)) && \
(!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
32bits platforms:
#define TCP_IPV4_MATCH(__sk, __hash, __cookie, __saddr, __daddr, __ports, __dif)\
(((__sk)->sk_hash == (__hash)) && \
(inet_sk(__sk)->daddr == (__saddr)) && \
(inet_sk(__sk)->rcv_saddr == (__daddr)) && \
(!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
- Adds a prefetch(head->chain.first) in
__inet_lookup_established()/__tcp_v4_check_established() and
__inet6_lookup_established()/__tcp_v6_check_established() and
__dccp_v4_check_established() to bring into cache the first element of the
list, before the {read|write}_lock(&head->lock);
Signed-off-by: Eric Dumazet <dada1@cosmosbay.com>
Acked-by: Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
2005-10-04 01:13:38 +04:00
struct inet_ehash_bucket * ehead = inet_ehash_bucket ( hashinfo , sk - > sk_hash ) ;
2008-11-21 07:39:09 +03:00
spinlock_t * lock = inet_ehash_lockp ( hashinfo , sk - > sk_hash ) ;
2022-12-26 16:27:52 +03:00
struct inet_bind_hashbucket * bhead , * bhead2 ;
2005-08-10 07:09:46 +04:00
/* Step 1: Put TW into bind hash. Original socket stays there too.
Note , that any socket with inet - > num ! = 0 MUST be bound in
binding cache , even if it is closed .
*/
2022-05-13 00:14:56 +03:00
bhead = & hashinfo - > bhash [ inet_bhashfn ( twsk_net ( tw ) , inet - > inet_num ,
hashinfo - > bhash_size ) ] ;
2022-12-26 16:27:52 +03:00
bhead2 = inet_bhashfn_portaddr ( hashinfo , sk , twsk_net ( tw ) , inet - > inet_num ) ;
2017-12-02 02:28:44 +03:00
spin_lock ( & bhead - > lock ) ;
2022-12-26 16:27:52 +03:00
spin_lock ( & bhead2 - > lock ) ;
2005-08-10 07:10:42 +04:00
tw - > tw_tb = icsk - > icsk_bind_hash ;
2008-07-26 08:43:18 +04:00
WARN_ON ( ! icsk - > icsk_bind_hash ) ;
2022-12-26 16:27:52 +03:00
tw - > tw_tb2 = icsk - > icsk_bind2_hash ;
WARN_ON ( ! icsk - > icsk_bind2_hash ) ;
2023-12-19 03:18:32 +03:00
sk_add_bind_node ( ( struct sock * ) tw , & tw - > tw_tb2 - > owners ) ;
2022-12-26 16:27:52 +03:00
spin_unlock ( & bhead2 - > lock ) ;
2005-08-10 07:09:46 +04:00
spin_unlock ( & bhead - > lock ) ;
2008-11-21 07:39:09 +03:00
spin_lock ( lock ) ;
2005-08-10 07:09:46 +04:00
Revert "tcp: avoid the lookup process failing to get sk in ehash table"
This reverts commit 3f4ca5fafc08881d7a57daa20449d171f2887043.
Commit 3f4ca5fafc08 ("tcp: avoid the lookup process failing to get sk in
ehash table") reversed the order in how a socket is inserted into ehash
to fix an issue that ehash-lookup could fail when reqsk/full sk/twsk are
swapped. However, it introduced another lookup failure.
The full socket in ehash is allocated from a slab with SLAB_TYPESAFE_BY_RCU
and does not have SOCK_RCU_FREE, so the socket could be reused even while
it is being referenced on another CPU doing RCU lookup.
Let's say a socket is reused and inserted into the same hash bucket during
lookup. After the blamed commit, a new socket is inserted at the end of
the list. If that happens, we will skip sockets placed after the previous
position of the reused socket, resulting in ehash lookup failure.
As described in Documentation/RCU/rculist_nulls.rst, we should insert a
new socket at the head of the list to avoid such an issue.
This issue, the swap-lookup-failure, and another variant reported in [0]
can all be handled properly by adding a locked ehash lookup suggested by
Eric Dumazet [1].
However, this issue could occur for every packet, thus more likely than
the other two races, so let's revert the change for now.
Link: https://lore.kernel.org/netdev/20230606064306.9192-1-duanmuquan@baidu.com/ [0]
Link: https://lore.kernel.org/netdev/CANn89iK8snOz8TYOhhwfimC7ykYA78GA3Nyv8x06SZYa1nKdyA@mail.gmail.com/ [1]
Fixes: 3f4ca5fafc08 ("tcp: avoid the lookup process failing to get sk in ehash table")
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Link: https://lore.kernel.org/r/20230717215918.15723-1-kuniyu@amazon.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2023-07-18 00:59:18 +03:00
inet_twsk_add_node_rcu ( tw , & ehead - > chain ) ;
2008-11-17 06:40:17 +03:00
tcp/dccp: remove twchain
TCP listener refactoring, part 3 :
Our goal is to hash SYN_RECV sockets into main ehash for fast lookup,
and parallel SYN processing.
Current inet_ehash_bucket contains two chains, one for ESTABLISH (and
friend states) sockets, another for TIME_WAIT sockets only.
As the hash table is sized to get at most one socket per bucket, it
makes little sense to have separate twchain, as it makes the lookup
slightly more complicated, and doubles hash table memory usage.
If we make sure all socket types have the lookup keys at the same
offsets, we can use a generic and faster lookup. It turns out TIME_WAIT
and ESTABLISHED sockets already have common lookup fields for IPv4.
[ INET_TW_MATCH() is no longer needed ]
I'll provide a follow-up to factorize IPv6 lookup as well, to remove
INET6_TW_MATCH()
This way, SYN_RECV pseudo sockets will be supported the same.
A new sock_gen_put() helper is added, doing either a sock_put() or
inet_twsk_put() [ and will support SYN_RECV later ].
Note this helper should only be called in real slow path, when rcu
lookup found a socket that was moved to another identity (freed/reused
immediately), but could eventually be used in other contexts, like
sock_edemux()
Before patch :
dmesg | grep "TCP established"
TCP established hash table entries: 524288 (order: 11, 8388608 bytes)
After patch :
TCP established hash table entries: 524288 (order: 10, 4194304 bytes)
Signed-off-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2013-10-03 11:22:02 +04:00
/* Step 3: Remove SK from hash chain */
2008-11-17 06:40:17 +03:00
if ( __sk_nulls_del_node_init_rcu ( sk ) )
sock_prot_inuse_add ( sock_net ( sk ) , sk - > sk_prot , - 1 ) ;
2005-08-10 07:09:46 +04:00
2017-12-02 02:28:44 +03:00
spin_unlock ( lock ) ;
2017-12-12 08:25:12 +03:00
/* tw_refcnt is set to 3 because we have :
* - one reference for bhash chain .
* - one reference for ehash chain .
* - one reference for timer .
* We can use atomic_set ( ) because prior spin_lock ( ) / spin_unlock ( )
* committed into memory all tw fields .
* Also note that after this point , we lost our implicit reference
* so we are not allowed to use tw anymore .
*/
refcount_set ( & tw - > tw_refcnt , 3 ) ;
2005-08-10 07:09:46 +04:00
}
2017-12-12 08:25:12 +03:00
EXPORT_SYMBOL_GPL ( inet_twsk_hashdance ) ;
2005-08-10 07:45:03 +04:00
2017-10-17 03:29:41 +03:00
static void tw_timer_handler ( struct timer_list * t )
2005-08-10 07:09:59 +04:00
{
2017-10-17 03:29:41 +03:00
struct inet_timewait_sock * tw = from_timer ( tw , t , tw_timer ) ;
tcp/dccp: get rid of central timewait timer
Using a timer wheel for timewait sockets was nice ~15 years ago when
memory was expensive and machines had a single processor.
This does not scale, code is ugly and source of huge latencies
(Typically 30 ms have been seen, cpus spinning on death_lock spinlock.)
We can afford to use an extra 64 bytes per timewait sock and spread
timewait load to all cpus to have better behavior.
Tested:
On following test, /proc/sys/net/ipv4/tcp_tw_recycle is set to 1
on the target (lpaa24)
Before patch :
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
419594
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
437171
While test is running, we can observe 25 or even 33 ms latencies.
lpaa24:~# ping -c 1000 -i 0.02 -qn lpaa23
...
1000 packets transmitted, 1000 received, 0% packet loss, time 20601ms
rtt min/avg/max/mdev = 0.020/0.217/25.771/1.535 ms, pipe 2
lpaa24:~# ping -c 1000 -i 0.02 -qn lpaa23
...
1000 packets transmitted, 1000 received, 0% packet loss, time 20702ms
rtt min/avg/max/mdev = 0.019/0.183/33.761/1.441 ms, pipe 2
After patch :
About 90% increase of throughput :
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
810442
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
800992
And latencies are kept to minimal values during this load, even
if network utilization is 90% higher :
lpaa24:~# ping -c 1000 -i 0.02 -qn lpaa23
...
1000 packets transmitted, 1000 received, 0% packet loss, time 19991ms
rtt min/avg/max/mdev = 0.023/0.064/0.360/0.042 ms
Signed-off-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2015-04-13 04:51:09 +03:00
inet_twsk_kill ( tw ) ;
}
struct inet_timewait_sock * inet_twsk_alloc ( const struct sock * sk ,
struct inet_timewait_death_row * dr ,
const int state )
{
struct inet_timewait_sock * tw ;
2022-07-12 03:15:21 +03:00
if ( refcount_read ( & dr - > tw_refcount ) - 1 > =
READ_ONCE ( dr - > sysctl_max_tw_buckets ) )
tcp/dccp: get rid of central timewait timer
Using a timer wheel for timewait sockets was nice ~15 years ago when
memory was expensive and machines had a single processor.
This does not scale, code is ugly and source of huge latencies
(Typically 30 ms have been seen, cpus spinning on death_lock spinlock.)
We can afford to use an extra 64 bytes per timewait sock and spread
timewait load to all cpus to have better behavior.
Tested:
On following test, /proc/sys/net/ipv4/tcp_tw_recycle is set to 1
on the target (lpaa24)
Before patch :
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
419594
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
437171
While test is running, we can observe 25 or even 33 ms latencies.
lpaa24:~# ping -c 1000 -i 0.02 -qn lpaa23
...
1000 packets transmitted, 1000 received, 0% packet loss, time 20601ms
rtt min/avg/max/mdev = 0.020/0.217/25.771/1.535 ms, pipe 2
lpaa24:~# ping -c 1000 -i 0.02 -qn lpaa23
...
1000 packets transmitted, 1000 received, 0% packet loss, time 20702ms
rtt min/avg/max/mdev = 0.019/0.183/33.761/1.441 ms, pipe 2
After patch :
About 90% increase of throughput :
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
810442
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
800992
And latencies are kept to minimal values during this load, even
if network utilization is 90% higher :
lpaa24:~# ping -c 1000 -i 0.02 -qn lpaa23
...
1000 packets transmitted, 1000 received, 0% packet loss, time 19991ms
rtt min/avg/max/mdev = 0.023/0.064/0.360/0.042 ms
Signed-off-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2015-04-13 04:51:09 +03:00
return NULL ;
tw = kmem_cache_alloc ( sk - > sk_prot_creator - > twsk_prot - > twsk_slab ,
GFP_ATOMIC ) ;
2015-04-03 11:17:27 +03:00
if ( tw ) {
2005-08-10 07:09:59 +04:00
const struct inet_sock * inet = inet_sk ( sk ) ;
tcp/dccp: get rid of central timewait timer
Using a timer wheel for timewait sockets was nice ~15 years ago when
memory was expensive and machines had a single processor.
This does not scale, code is ugly and source of huge latencies
(Typically 30 ms have been seen, cpus spinning on death_lock spinlock.)
We can afford to use an extra 64 bytes per timewait sock and spread
timewait load to all cpus to have better behavior.
Tested:
On following test, /proc/sys/net/ipv4/tcp_tw_recycle is set to 1
on the target (lpaa24)
Before patch :
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
419594
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
437171
While test is running, we can observe 25 or even 33 ms latencies.
lpaa24:~# ping -c 1000 -i 0.02 -qn lpaa23
...
1000 packets transmitted, 1000 received, 0% packet loss, time 20601ms
rtt min/avg/max/mdev = 0.020/0.217/25.771/1.535 ms, pipe 2
lpaa24:~# ping -c 1000 -i 0.02 -qn lpaa23
...
1000 packets transmitted, 1000 received, 0% packet loss, time 20702ms
rtt min/avg/max/mdev = 0.019/0.183/33.761/1.441 ms, pipe 2
After patch :
About 90% increase of throughput :
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
810442
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
800992
And latencies are kept to minimal values during this load, even
if network utilization is 90% higher :
lpaa24:~# ping -c 1000 -i 0.02 -qn lpaa23
...
1000 packets transmitted, 1000 received, 0% packet loss, time 19991ms
rtt min/avg/max/mdev = 0.023/0.064/0.360/0.042 ms
Signed-off-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2015-04-13 04:51:09 +03:00
tw - > tw_dr = dr ;
2005-08-10 07:09:59 +04:00
/* Give us an identity. */
2009-10-15 10:30:45 +04:00
tw - > tw_daddr = inet - > inet_daddr ;
tw - > tw_rcv_saddr = inet - > inet_rcv_saddr ;
2005-08-10 07:09:59 +04:00
tw - > tw_bound_dev_if = sk - > sk_bound_dev_if ;
2011-10-24 11:06:21 +04:00
tw - > tw_tos = inet - > tos ;
2009-10-15 10:30:45 +04:00
tw - > tw_num = inet - > inet_num ;
2005-08-10 07:09:59 +04:00
tw - > tw_state = TCP_TIME_WAIT ;
tw - > tw_substate = state ;
2009-10-15 10:30:45 +04:00
tw - > tw_sport = inet - > inet_sport ;
tw - > tw_dport = inet - > inet_dport ;
2005-08-10 07:09:59 +04:00
tw - > tw_family = sk - > sk_family ;
tw - > tw_reuse = sk - > sk_reuse ;
2018-04-07 23:42:43 +03:00
tw - > tw_reuseport = sk - > sk_reuseport ;
[INET]: speedup inet (tcp/dccp) lookups
Arnaldo and I agreed it could be applied now, because I have other
pending patches depending on this one (Thank you Arnaldo)
(The other important patch moves skc_refcnt in a separate cache line,
so that the SMP/NUMA performance doesnt suffer from cache line ping pongs)
1) First some performance data :
--------------------------------
tcp_v4_rcv() wastes a *lot* of time in __inet_lookup_established()
The most time critical code is :
sk_for_each(sk, node, &head->chain) {
if (INET_MATCH(sk, acookie, saddr, daddr, ports, dif))
goto hit; /* You sunk my battleship! */
}
The sk_for_each() does use prefetch() hints but only the begining of
"struct sock" is prefetched.
As INET_MATCH first comparison uses inet_sk(__sk)->daddr, wich is far
away from the begining of "struct sock", it has to bring into CPU
cache cold cache line. Each iteration has to use at least 2 cache
lines.
This can be problematic if some chains are very long.
2) The goal
-----------
The idea I had is to change things so that INET_MATCH() may return
FALSE in 99% of cases only using the data already in the CPU cache,
using one cache line per iteration.
3) Description of the patch
---------------------------
Adds a new 'unsigned int skc_hash' field in 'struct sock_common',
filling a 32 bits hole on 64 bits platform.
struct sock_common {
unsigned short skc_family;
volatile unsigned char skc_state;
unsigned char skc_reuse;
int skc_bound_dev_if;
struct hlist_node skc_node;
struct hlist_node skc_bind_node;
atomic_t skc_refcnt;
+ unsigned int skc_hash;
struct proto *skc_prot;
};
Store in this 32 bits field the full hash, not masked by (ehash_size -
1) Using this full hash as the first comparison done in INET_MATCH
permits us immediatly skip the element without touching a second cache
line in case of a miss.
Suppress the sk_hashent/tw_hashent fields since skc_hash (aliased to
sk_hash and tw_hash) already contains the slot number if we mask with
(ehash_size - 1)
File include/net/inet_hashtables.h
64 bits platforms :
#define INET_MATCH(__sk, __hash, __cookie, __saddr, __daddr, __ports, __dif)\
(((__sk)->sk_hash == (__hash))
((*((__u64 *)&(inet_sk(__sk)->daddr)))== (__cookie)) && \
((*((__u32 *)&(inet_sk(__sk)->dport))) == (__ports)) && \
(!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
32bits platforms:
#define TCP_IPV4_MATCH(__sk, __hash, __cookie, __saddr, __daddr, __ports, __dif)\
(((__sk)->sk_hash == (__hash)) && \
(inet_sk(__sk)->daddr == (__saddr)) && \
(inet_sk(__sk)->rcv_saddr == (__daddr)) && \
(!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
- Adds a prefetch(head->chain.first) in
__inet_lookup_established()/__tcp_v4_check_established() and
__inet6_lookup_established()/__tcp_v6_check_established() and
__dccp_v4_check_established() to bring into cache the first element of the
list, before the {read|write}_lock(&head->lock);
Signed-off-by: Eric Dumazet <dada1@cosmosbay.com>
Acked-by: Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
2005-10-04 01:13:38 +04:00
tw - > tw_hash = sk - > sk_hash ;
2005-08-10 07:09:59 +04:00
tw - > tw_ipv6only = 0 ;
2023-08-16 11:15:41 +03:00
tw - > tw_transparent = inet_test_bit ( TRANSPARENT , sk ) ;
2005-08-10 07:09:59 +04:00
tw - > tw_prot = sk - > sk_prot_creator ;
2015-03-12 04:53:14 +03:00
atomic64_set ( & tw - > tw_cookie , atomic64_read ( & sk - > sk_cookie ) ) ;
2015-03-12 07:04:08 +03:00
twsk_net_set ( tw , sock_net ( sk ) ) ;
2017-10-17 03:29:41 +03:00
timer_setup ( & tw - > tw_timer , tw_timer_handler , TIMER_PINNED ) ;
2009-12-03 03:49:01 +03:00
/*
* Because we use RCU lookups , we should not set tw_refcnt
* to a non null value before everything is setup for this
* timewait socket .
*/
2017-06-30 13:08:01 +03:00
refcount_set ( & tw - > tw_refcnt , 0 ) ;
tcp/dccp: get rid of central timewait timer
Using a timer wheel for timewait sockets was nice ~15 years ago when
memory was expensive and machines had a single processor.
This does not scale, code is ugly and source of huge latencies
(Typically 30 ms have been seen, cpus spinning on death_lock spinlock.)
We can afford to use an extra 64 bytes per timewait sock and spread
timewait load to all cpus to have better behavior.
Tested:
On following test, /proc/sys/net/ipv4/tcp_tw_recycle is set to 1
on the target (lpaa24)
Before patch :
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
419594
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
437171
While test is running, we can observe 25 or even 33 ms latencies.
lpaa24:~# ping -c 1000 -i 0.02 -qn lpaa23
...
1000 packets transmitted, 1000 received, 0% packet loss, time 20601ms
rtt min/avg/max/mdev = 0.020/0.217/25.771/1.535 ms, pipe 2
lpaa24:~# ping -c 1000 -i 0.02 -qn lpaa23
...
1000 packets transmitted, 1000 received, 0% packet loss, time 20702ms
rtt min/avg/max/mdev = 0.019/0.183/33.761/1.441 ms, pipe 2
After patch :
About 90% increase of throughput :
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
810442
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
800992
And latencies are kept to minimal values during this load, even
if network utilization is 90% higher :
lpaa24:~# ping -c 1000 -i 0.02 -qn lpaa23
...
1000 packets transmitted, 1000 received, 0% packet loss, time 19991ms
rtt min/avg/max/mdev = 0.023/0.064/0.360/0.042 ms
Signed-off-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2015-04-13 04:51:09 +03:00
2005-10-11 08:25:23 +04:00
__module_get ( tw - > tw_prot - > owner ) ;
2005-08-10 07:09:59 +04:00
}
return tw ;
}
2005-08-10 07:45:03 +04:00
EXPORT_SYMBOL_GPL ( inet_twsk_alloc ) ;
/* These are always called from BH context. See callers in
* tcp_input . c to verify this .
*/
2015-07-09 00:28:30 +03:00
/* This is for handling early-kills of TIME_WAIT sockets.
* Warning : consume reference .
* Caller should not access tw anymore .
*/
void inet_twsk_deschedule_put ( struct inet_timewait_sock * tw )
2005-08-10 07:45:03 +04:00
{
tcp/dccp: get rid of central timewait timer
Using a timer wheel for timewait sockets was nice ~15 years ago when
memory was expensive and machines had a single processor.
This does not scale, code is ugly and source of huge latencies
(Typically 30 ms have been seen, cpus spinning on death_lock spinlock.)
We can afford to use an extra 64 bytes per timewait sock and spread
timewait load to all cpus to have better behavior.
Tested:
On following test, /proc/sys/net/ipv4/tcp_tw_recycle is set to 1
on the target (lpaa24)
Before patch :
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
419594
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
437171
While test is running, we can observe 25 or even 33 ms latencies.
lpaa24:~# ping -c 1000 -i 0.02 -qn lpaa23
...
1000 packets transmitted, 1000 received, 0% packet loss, time 20601ms
rtt min/avg/max/mdev = 0.020/0.217/25.771/1.535 ms, pipe 2
lpaa24:~# ping -c 1000 -i 0.02 -qn lpaa23
...
1000 packets transmitted, 1000 received, 0% packet loss, time 20702ms
rtt min/avg/max/mdev = 0.019/0.183/33.761/1.441 ms, pipe 2
After patch :
About 90% increase of throughput :
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
810442
lpaa23:~# ./super_netperf 200 -H lpaa24 -t TCP_CC -l 60 -- -p0,0
800992
And latencies are kept to minimal values during this load, even
if network utilization is 90% higher :
lpaa24:~# ping -c 1000 -i 0.02 -qn lpaa23
...
1000 packets transmitted, 1000 received, 0% packet loss, time 19991ms
rtt min/avg/max/mdev = 0.023/0.064/0.360/0.042 ms
Signed-off-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2015-04-13 04:51:09 +03:00
if ( del_timer_sync ( & tw - > tw_timer ) )
inet_twsk_kill ( tw ) ;
2015-07-09 00:28:30 +03:00
inet_twsk_put ( tw ) ;
2005-08-10 07:45:03 +04:00
}
2015-07-09 00:28:30 +03:00
EXPORT_SYMBOL ( inet_twsk_deschedule_put ) ;
2005-08-10 07:45:03 +04:00
2015-09-19 19:08:34 +03:00
void __inet_twsk_schedule ( struct inet_timewait_sock * tw , int timeo , bool rearm )
2005-08-10 07:45:03 +04:00
{
/* timeout := RTO * 3.5
*
* 3.5 = 1 + 2 + 0.5 to wait for two retransmits .
*
* RATIONALE : if FIN arrived and we entered TIME - WAIT state ,
* our ACK acking that FIN can be lost . If N subsequent retransmitted
* FINs ( or previous seqments ) are lost ( probability of such event
* is p ^ ( N + 1 ) , where p is probability to lose single packet and
* time to detect the loss is about RTO * ( 2 ^ N - 1 ) with exponential
* backoff ) . Normal timewait length is calculated so , that we
* waited at least for one retransmitted FIN ( maximal RTO is 120 sec ) .
* [ BTW Linux . following BSD , violates this requirement waiting
* only for 60 sec , we should wait at least for 240 secs .
* Well , 240 consumes too much of resources 8 )
* ]
* This interval is not reduced to catch old duplicate and
* responces to our wandering segments living for two MSLs .
* However , if we use PAWS to detect
* old duplicates , we can reduce the interval to bounds required
* by RTO , rather than MSL . So , if peer understands PAWS , we
* kill tw bucket after 3.5 * RTO ( it is important that this number
* is greater than TS tick ! ) and detect old duplicates with help
* of PAWS .
*/
2015-09-19 19:08:34 +03:00
if ( ! rearm ) {
2022-01-24 23:24:53 +03:00
bool kill = timeo < = 4 * HZ ;
__NET_INC_STATS ( twsk_net ( tw ) , kill ? LINUX_MIB_TIMEWAITKILLED :
LINUX_MIB_TIMEWAITED ) ;
2016-07-04 12:50:23 +03:00
BUG_ON ( mod_timer ( & tw - > tw_timer , jiffies + timeo ) ) ;
2022-01-26 21:07:14 +03:00
refcount_inc ( & tw - > tw_dr - > tw_refcount ) ;
2015-09-19 19:08:34 +03:00
} else {
mod_timer_pending ( & tw - > tw_timer , jiffies + timeo ) ;
2005-08-10 07:45:03 +04:00
}
}
2015-09-19 19:08:34 +03:00
EXPORT_SYMBOL_GPL ( __inet_twsk_schedule ) ;
2022-05-13 00:14:56 +03:00
2024-03-08 23:01:21 +03:00
/* Remove all non full sockets (TIME_WAIT and NEW_SYN_RECV) for dead netns */
2024-03-29 18:32:03 +03:00
void inet_twsk_purge ( struct inet_hashinfo * hashinfo )
2022-05-13 00:14:56 +03:00
{
2024-03-27 22:12:06 +03:00
struct inet_ehash_bucket * head = & hashinfo - > ehash [ 0 ] ;
unsigned int ehash_mask = hashinfo - > ehash_mask ;
2022-05-13 00:14:56 +03:00
struct hlist_nulls_node * node ;
unsigned int slot ;
2024-03-08 23:01:21 +03:00
struct sock * sk ;
2022-05-13 00:14:56 +03:00
2024-03-27 22:12:06 +03:00
for ( slot = 0 ; slot < = ehash_mask ; slot + + , head + + ) {
if ( hlist_nulls_empty ( & head - > chain ) )
continue ;
2022-05-13 00:14:56 +03:00
restart_rcu :
cond_resched ( ) ;
rcu_read_lock ( ) ;
restart :
sk_nulls_for_each_rcu ( sk , node , & head - > chain ) {
2024-03-08 23:01:21 +03:00
int state = inet_sk_state_load ( sk ) ;
2022-10-12 17:50:36 +03:00
2024-03-08 23:01:21 +03:00
if ( ( 1 < < state ) & ~ ( TCPF_TIME_WAIT |
TCPF_NEW_SYN_RECV ) )
2022-05-13 00:14:56 +03:00
continue ;
2022-10-12 17:50:36 +03:00
2024-03-29 18:32:03 +03:00
if ( refcount_read ( & sock_net ( sk ) - > ns . count ) )
2022-05-13 00:14:56 +03:00
continue ;
2024-03-08 23:01:21 +03:00
if ( unlikely ( ! refcount_inc_not_zero ( & sk - > sk_refcnt ) ) )
2022-05-13 00:14:56 +03:00
continue ;
2024-03-29 18:32:03 +03:00
if ( refcount_read ( & sock_net ( sk ) - > ns . count ) ) {
2024-03-08 23:01:21 +03:00
sock_gen_put ( sk ) ;
2022-05-13 00:14:56 +03:00
goto restart ;
}
rcu_read_unlock ( ) ;
local_bh_disable ( ) ;
2024-03-08 23:01:21 +03:00
if ( state = = TCP_TIME_WAIT ) {
inet_twsk_deschedule_put ( inet_twsk ( sk ) ) ;
} else {
struct request_sock * req = inet_reqsk ( sk ) ;
inet_csk_reqsk_queue_drop_and_put ( req - > rsk_listener ,
req ) ;
}
2022-05-13 00:14:56 +03:00
local_bh_enable ( ) ;
goto restart_rcu ;
}
/* If the nulls value we got at the end of this lookup is
* not the expected one , we must restart lookup .
* We probably met an item that was moved to another chain .
*/
if ( get_nulls_value ( node ) ! = slot )
goto restart ;
rcu_read_unlock ( ) ;
}
}
EXPORT_SYMBOL_GPL ( inet_twsk_purge ) ;