tcp: tcp_get_info() locking changes
Subsystems (3)
cover tcp: tcp_get_info() locking changes
This short series prepares tcp_get_info() for more detailed infos.
In order to not slow down fast path, our goal is to use the normal socket spinlock instead of custom synchronization.
All we need to ensure is that tcp_get_info() is not called with ehash lock, which might dead lock, since packet processing would acquire the spinlocks in reverse way.
Eric Dumazet (2):
tcp: shortcut listeners in get_tcp_info()
tcp: no longer hold ehash lock while calling tcp_get_info()
include/linux/tcp.h | 2 -- net/ipv4/inet_diag.c | 48 +++++++++++++++++++++++++++++-------------- net/ipv4/tcp.c | 57 ++++++++++++++++++++++++++++------------------------ net/ipv4/tcp_input.c | 4 ---- 4 files changed, 64 insertions(+), 47 deletions(-)
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH 1/2 tcp: shortcut listeners in tcp_get_info()
Being lockless in tcp_get_info() is hard, because we need to add specific synchronization in TCP fast path, like seqcount.
Following patch will change inet_diag_dump_icsk() to no longer hold any lock for non listeners, so that we can properly acquire socket lock in get_tcp_info() and let it return more consistent counters.
--- net/ipv4/tcp.c | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-)
net/ipv4/tcp.c +24 -17
@@ -2721,6 +2721,27 @@ void tcp_get_info(struct sock *sk, struct tcp_info *info) info->tcpi_state =sk_state_load (sk); +/* Report meaningful fields for all TCP states, including listeners */ + rate =READ_ONCE (sk->sk_pacing_rate); + rate64 = rate != ~0U ? rate : ~0ULL ; +put_unaligned (rate64, &info->tcpi_pacing_rate); + + rate =READ_ONCE (sk->sk_max_pacing_rate); + rate64 = rate != ~0U ? rate : ~0ULL ; +put_unaligned (rate64, &info->tcpi_max_pacing_rate); + + info->tcpi_reordering = tp->reordering; + info->tcpi_snd_cwnd = tp->snd_cwnd; + +if (info->tcpi_state ==TCP_LISTEN ) { +/* listeners aliased fields : +* tcpi_unacked -> Number of children ready for accept() +* tcpi_sacked -> max backlog +*/ + info->tcpi_unacked = sk->sk_ack_backlog; + info->tcpi_sacked = sk->sk_max_ack_backlog; +return ; + } info->tcpi_ca_state = icsk->icsk_ca_state; info->tcpi_retransmits = icsk->icsk_retransmits; info->tcpi_probes = icsk->icsk_probes_out;
@@ -2748,13 +2769,9 @@ void tcp_get_info(struct sock *sk, struct tcp_info *info) info->tcpi_snd_mss = tp->mss_cache; info->tcpi_rcv_mss = icsk->icsk_ack.rcv_mss; -if (info->tcpi_state ==TCP_LISTEN ) { - info->tcpi_unacked = sk->sk_ack_backlog; - info->tcpi_sacked = sk->sk_max_ack_backlog; - }else { - info->tcpi_unacked = tp->packets_out; - info->tcpi_sacked = tp->sacked_out; - } + info->tcpi_unacked = tp->packets_out; + info->tcpi_sacked = tp->sacked_out; + info->tcpi_lost = tp->lost_out; info->tcpi_retrans = tp->retrans_out; info->tcpi_fackets = tp->fackets_out;
@@ -2768,23 +2785,13 @@ void tcp_get_info(struct sock *sk, struct tcp_info *info) info->tcpi_rtt = tp->srtt_us >>3 ; info->tcpi_rttvar = tp->mdev_us >>2 ; info->tcpi_snd_ssthresh = tp->snd_ssthresh; - info->tcpi_snd_cwnd = tp->snd_cwnd; info->tcpi_advmss = tp->advmss; - info->tcpi_reordering = tp->reordering; info->tcpi_rcv_rtt =jiffies_to_usecs (tp->rcv_rtt_est.rtt)>>3 ; info->tcpi_rcv_space = tp->rcvq_space.space; info->tcpi_total_retrans = tp->total_retrans; - rate =READ_ONCE (sk->sk_pacing_rate); - rate64 = rate != ~0U ? rate : ~0ULL ; -put_unaligned (rate64, &info->tcpi_pacing_rate); - - rate =READ_ONCE (sk->sk_max_pacing_rate); - rate64 = rate != ~0U ? rate : ~0ULL ; -put_unaligned (rate64, &info->tcpi_max_pacing_rate); - do { start = u64_stats_fetch_begin_irq(&tp->syncp); put_unaligned(tp->bytes_acked, &info->tcpi_bytes_acked);
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH 2/2 tcp: no longer hold ehash lock while calling tcp_get_info()
In reply to Eric DumazetWe had various problems in the past in tcp_get_info() and used specific synchronization to avoid deadlocks.
We would like to add more instrumentation points for TCP, and avoiding grabing socket lock in tcp_getinfo() was too costly.
Being able to lock the socket allows to provide consistent set of fields.
inet_diag_dump_icsk() can make sure ehash locks are not held any more when tcp_get_info() is called.
We can remove syncp added in commit d654976cbf85
("tcp: fix a potential deadlock in tcp_get_info()"), but we need to use lock_sock_fast() instead of spin_lock_bh() since TCP input path can now be run from process context.
--- include/linux/tcp.h | 2 -- net/ipv4/inet_diag.c | 48 +++++++++++++++++++++++++++++++++--------------- net/ipv4/tcp.c | 20 +++++++++----------- net/ipv4/tcp_input.c | 4 ---- 4 files changed, 42 insertions(+), 32 deletions(-)
include/linux/tcp.h +0 -2
@@ -176,8 +176,6 @@ struct tcp_sock {* sum(delta(snd_una)), or how many bytes * were acked. */ -struct u64_stats_sync syncp;/* protects 64bit vars (cf tcp_get_info()) */ -u32 snd_una;/* First byte we want an ack for */ u32 snd_sml;/* Last byte of the most recently transmitted small packet */ u32 rcv_tstamp;/* timestamp of last received ACK (for keepalives) */
net/ipv4/inet_diag.c +33 -15
93 lines not on this page · raw message
net/ipv4/tcp.c +9 -11
@@ -405,7 +405,6 @@ void tcp_init_sock(struct sock *sk) tp->snd_ssthresh =TCP_INFINITE_SSTHRESH ; tp->snd_cwnd_clamp = ~0 ; tp->mss_cache =TCP_MSS_DEFAULT ; -u64_stats_init (&tp->syncp); tp->reordering =sock_net (sk)->ipv4.sysctl_tcp_reordering;tcp_enable_early_retrans (tp);
@@ -2710,9 +2709,8 @@ void tcp_get_info(struct sock *sk, struct tcp_info *info)const struct tcp_sock *tp =tcp_sk (sk);/* iff sk_type == SOCK_STREAM */ const struct inet_connection_sock *icsk =inet_csk (sk);u32 now = tcp_time_stamp, intv; -unsigned int start; -int notsent_bytes;u64 rate64; +bool slow;u32 rate;memset (info,0 ,sizeof (*info));
@@ -2792,17 +2790,17 @@ void tcp_get_info(struct sock *sk, struct tcp_info *info) info->tcpi_total_retrans = tp->total_retrans; -do { - start =u64_stats_fetch_begin_irq (&tp->syncp); -put_unaligned (tp->bytes_acked, &info->tcpi_bytes_acked); -put_unaligned (tp->bytes_received, &info->tcpi_bytes_received); - }while (u64_stats_fetch_retry_irq (&tp->syncp, start)); + slow =lock_sock_fast (sk); + +put_unaligned (tp->bytes_acked, &info->tcpi_bytes_acked); +put_unaligned (tp->bytes_received, &info->tcpi_bytes_received); + info->tcpi_notsent_bytes =max_t (int,0 , tp->write_seq - tp->snd_nxt); + +unlock_sock_fast (sk, slow); + info->tcpi_segs_out = tp->segs_out; info->tcpi_segs_in = tp->segs_in; - notsent_bytes =READ_ONCE (tp->write_seq) -READ_ONCE (tp->snd_nxt); - info->tcpi_notsent_bytes =max (0 , notsent_bytes); - info->tcpi_min_rtt =tcp_min_rtt (tp); info->tcpi_data_segs_in = tp->data_segs_in; info->tcpi_data_segs_out = tp->data_segs_out;
net/ipv4/tcp_input.c +0 -4
@@ -3351,9 +3351,7 @@ static void tcp_snd_una_update(struct tcp_sock *tp, u32 ack)u32 delta = ack - tp->snd_una;sock_owned_by_me ((struct sock *)tp); -u64_stats_update_begin_raw (&tp->syncp); tp->bytes_acked += delta; -u64_stats_update_end_raw (&tp->syncp); tp->snd_una = ack; }
@@ -3363,9 +3361,7 @@ static void tcp_rcv_nxt_update(struct tcp_sock *tp, u32 seq)u32 delta = seq - tp->rcv_nxt;sock_owned_by_me ((struct sock *)tp); -u64_stats_update_begin_raw (&tp->syncp); tp->bytes_received += delta; -u64_stats_update_end_raw (&tp->syncp); tp->rcv_nxt = seq; }
Signature · 1 line
2.8.0.rc3.226.g39d4020
David Miller
maintainerFrom: Eric Dumazet <[email protected]>
Date: Fri, 4 Nov 2016 11:54:30 -0700
Quoted 8 lines
This short series prepares
tcp_get_info()for more detailed infos.In order to not slow down fast path, our goal is to use the normal socket spinlock instead of custom synchronization.
All we need to ensure is that
tcp_get_info()is not called with ehash lock, which might dead lock, since packet processing would acquire the spinlocks in reverse way.
Looks really nice, series applied, thanks Eric.
BTW, can we possibly use the netlink NOP trick to align the netlink attribute data here and avoid these unaligned accesses?
Thanks again.
Quoted 5 lines · David Miller
Looks really nice, series applied, thanks Eric.
BTW, can we possibly use the netlink NOP trick to align the netlink attribute data here and avoid these unaligned accesses?
Good idea, I will do this, thanks for the suggestion.
PATCH 1/1 tcp: remove unaligned accesses from tcp_get_info()
From: Eric Dumazet <[email protected]>
After commit 6ed46d1247a5 ("sock_diag: align nlattr properly when needed"), tcp_get_info() gets 64bit aligned memory, so we can avoid the unaligned helpers.
--- net/ipv4/tcp.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
net/ipv4/tcp.c +5 -6
@@ -279,7 +279,6 @@#include <asm/uaccess.h> #include <asm/ioctls.h> -#include <asm/unaligned.h> #include <net/busy_poll.h> int sysctl_tcp_min_tso_segs__read_mostly =2 ;
@@ -2722,11 +2721,11 @@ void tcp_get_info(struct sock *sk, struct tcp_info *info)/* Report meaningful fields for all TCP states, including listeners */ rate =READ_ONCE (sk->sk_pacing_rate); rate64 = rate != ~0U ? rate : ~0ULL ; -put_unaligned (rate64, &info->tcpi_pacing_rate); + info->tcpi_pacing_rate = rate64; rate =READ_ONCE (sk->sk_max_pacing_rate); rate64 = rate != ~0U ? rate : ~0ULL ; -put_unaligned (rate64, &info->tcpi_max_pacing_rate); + info->tcpi_max_pacing_rate = rate64; info->tcpi_reordering = tp->reordering; info->tcpi_snd_cwnd = tp->snd_cwnd;
@@ -2792,8 +2791,8 @@ void tcp_get_info(struct sock *sk, struct tcp_info *info) slow =lock_sock_fast (sk); -put_unaligned (tp->bytes_acked, &info->tcpi_bytes_acked); -put_unaligned (tp->bytes_received, &info->tcpi_bytes_received); + info->tcpi_bytes_acked = tp->bytes_acked; + info->tcpi_bytes_received = tp->bytes_received; info->tcpi_notsent_bytes =max_t (int,0 , tp->write_seq - tp->snd_nxt);unlock_sock_fast (sk, slow);
@@ -2811,7 +2810,7 @@ void tcp_get_info(struct sock *sk, struct tcp_info *info)if (rate && intv) { rate64 = (u64 )rate * tp->mss_cache *USEC_PER_SEC ;do_div (rate64, intv); -put_unaligned (rate64, &info->tcpi_delivery_rate); + info->tcpi_delivery_rate = rate64; } }EXPORT_SYMBOL_GPL (tcp_get_info);
Re: [PATCH net-next] tcp: remove unaligned accesses from tcp_get_info()
Quoted 9 lines · Eric Dumazet
From: Eric Dumazet <[email protected]>
After commit
6ed46d1247a5("sock_diag: align nlattr properly when needed"),tcp_get_info()gets 64bit aligned memory, so we can avoid the unaligned helpers.Suggested-by David Miller <[email protected]>Signed-off-by Eric Dumazet <[email protected]>
Quoted 44 lines
--- net/ipv4/tcp.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)net/ipv4/tcp.c +4
-5@@ -279,7 +279,6 @@#include <asm/uaccess.h> #include <asm/ioctls.h> -#include <asm/unaligned.h> #include <net/busy_poll.h> int sysctl_tcp_min_tso_segs__read_mostly =2 ;@@ -2722,11 +2721,11 @@ void tcp_get_info(struct sock *sk, struct tcp_info *info)/* Report meaningful fields for all TCP states, including listeners */ rate =READ_ONCE (sk->sk_pacing_rate); rate64 = rate != ~0U ? rate : ~0ULL ; -put_unaligned (rate64, &info->tcpi_pacing_rate); + info->tcpi_pacing_rate = rate64; rate =READ_ONCE (sk->sk_max_pacing_rate); rate64 = rate != ~0U ? rate : ~0ULL ; -put_unaligned (rate64, &info->tcpi_max_pacing_rate); + info->tcpi_max_pacing_rate = rate64; info->tcpi_reordering = tp->reordering; info->tcpi_snd_cwnd = tp->snd_cwnd;@@ -2792,8 +2791,8 @@ void tcp_get_info(struct sock *sk, struct tcp_info *info) slow =lock_sock_fast (sk); -put_unaligned (tp->bytes_acked, &info->tcpi_bytes_acked); -put_unaligned (tp->bytes_received, &info->tcpi_bytes_received); + info->tcpi_bytes_acked = tp->bytes_acked; + info->tcpi_bytes_received = tp->bytes_received; info->tcpi_notsent_bytes =max_t (int,0 , tp->write_seq - tp->snd_nxt);unlock_sock_fast (sk, slow);
net/ipv4/tcp.c void tcp_get_info(struct sock *sk, struct tcp_info *info) · line 2816
Quoted 8 lines
net/ipv4/tcp.c +1
-1@@ -2811,7 +2810,7 @@ void tcp_get_info(struct sock *sk, struct tcp_info *info)if (rate && intv) { rate64 = (u64 )rate * tp->mss_cache *USEC_PER_SEC ;do_div (rate64, intv); -put_unaligned (rate64, &info->tcpi_delivery_rate); + info->tcpi_delivery_rate = rate64; } }EXPORT_SYMBOL_GPL (tcp_get_info);
Very nice! Thanks.
Re: [PATCH net-next] tcp: remove unaligned accesses from tcp_get_info()
In reply to Eric DumazetQuoted 9 lines · Eric Dumazet
From: Eric Dumazet <[email protected]>
After commit
6ed46d1247a5("sock_diag: align nlattr properly when needed"),tcp_get_info()gets 64bit aligned memory, so we can avoid the unaligned helpers.Suggested-by David Miller <[email protected]>Signed-off-by Eric Dumazet <[email protected]>
Quoted 52 lines
--- net/ipv4/tcp.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)net/ipv4/tcp.c +5
-6@@ -279,7 +279,6 @@#include <asm/uaccess.h> #include <asm/ioctls.h> -#include <asm/unaligned.h> #include <net/busy_poll.h> int sysctl_tcp_min_tso_segs__read_mostly =2 ;@@ -2722,11 +2721,11 @@ void tcp_get_info(struct sock *sk, struct tcp_info *info)/* Report meaningful fields for all TCP states, including listeners */ rate =READ_ONCE (sk->sk_pacing_rate); rate64 = rate != ~0U ? rate : ~0ULL ; -put_unaligned (rate64, &info->tcpi_pacing_rate); + info->tcpi_pacing_rate = rate64; rate =READ_ONCE (sk->sk_max_pacing_rate); rate64 = rate != ~0U ? rate : ~0ULL ; -put_unaligned (rate64, &info->tcpi_max_pacing_rate); + info->tcpi_max_pacing_rate = rate64; info->tcpi_reordering = tp->reordering; info->tcpi_snd_cwnd = tp->snd_cwnd;@@ -2792,8 +2791,8 @@ void tcp_get_info(struct sock *sk, struct tcp_info *info) slow =lock_sock_fast (sk); -put_unaligned (tp->bytes_acked, &info->tcpi_bytes_acked); -put_unaligned (tp->bytes_received, &info->tcpi_bytes_received); + info->tcpi_bytes_acked = tp->bytes_acked; + info->tcpi_bytes_received = tp->bytes_received; info->tcpi_notsent_bytes =max_t (int,0 , tp->write_seq - tp->snd_nxt);unlock_sock_fast (sk, slow);@@ -2811,7 +2810,7 @@ void tcp_get_info(struct sock *sk, struct tcp_info *info)if (rate && intv) { rate64 = (u64 )rate * tp->mss_cache *USEC_PER_SEC ;do_div (rate64, intv); -put_unaligned (rate64, &info->tcpi_delivery_rate); + info->tcpi_delivery_rate = rate64; } }EXPORT_SYMBOL_GPL (tcp_get_info);
David Miller
maintainerRe: [PATCH net-next] tcp: remove unaligned accesses from tcp_get_info()
In reply to Eric DumazetFrom: Eric Dumazet <[email protected]>
Date: Wed, 09 Nov 2016 11:24:22 -0800
Quoted 8 lines
From: Eric Dumazet <[email protected]>
After commit
6ed46d1247a5("sock_diag: align nlattr properly when needed"),tcp_get_info()gets 64bit aligned memory, so we can avoid the unaligned helpers.Suggested-by David Miller <[email protected]>Signed-off-by Eric Dumazet <[email protected]>
Nice, applied.
Thanks!