Skip to content

Commit 67db3e4

Browse files
edumazetdavem330
authored andcommitted
tcp: no longer hold ehash lock while calling tcp_get_info()
We 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 d654976 ("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. Signed-off-by: Eric Dumazet <edumazet@google.com> Signed-off-by: Yuchung Cheng <ycheng@google.com> Acked-by: Soheil Hassas Yeganeh <soheil@google.com> Acked-by: Neal Cardwell <ncardwell@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent ccbf3bf commit 67db3e4

4 files changed

Lines changed: 42 additions & 32 deletions

File tree

‎include/linux/tcp.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,6 @@ struct tcp_sock {
176176
* sum(delta(snd_una)), or how many bytes
177177
* were acked.
178178
*/
179-
struct u64_stats_sync syncp; /* protects 64bit vars (cf tcp_get_info()) */
180-
181179
u32 snd_una; /* First byte we want an ack for */
182180
u32 snd_sml; /* Last byte of the most recently transmitted small packet */
183181
u32 rcv_tstamp; /* timestamp of last received ACK (for keepalives) */

‎net/ipv4/inet_diag.c‎

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -861,10 +861,11 @@ void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
861861
struct netlink_callback *cb,
862862
const struct inet_diag_req_v2 *r, struct nlattr *bc)
863863
{
864+
bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
864865
struct net *net = sock_net(skb->sk);
865-
int i, num, s_i, s_num;
866866
u32 idiag_states = r->idiag_states;
867-
bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
867+
int i, num, s_i, s_num;
868+
struct sock *sk;
868869

869870
if (idiag_states & TCPF_SYN_RECV)
870871
idiag_states |= TCPF_NEW_SYN_RECV;
@@ -877,7 +878,6 @@ void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
877878

878879
for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
879880
struct inet_listen_hashbucket *ilb;
880-
struct sock *sk;
881881

882882
num = 0;
883883
ilb = &hashinfo->listening_hash[i];
@@ -922,23 +922,27 @@ void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
922922
if (!(idiag_states & ~TCPF_LISTEN))
923923
goto out;
924924

925+
#define SKARR_SZ 16
925926
for (i = s_i; i <= hashinfo->ehash_mask; i++) {
926927
struct inet_ehash_bucket *head = &hashinfo->ehash[i];
927928
spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
928929
struct hlist_nulls_node *node;
929-
struct sock *sk;
930-
931-
num = 0;
930+
struct sock *sk_arr[SKARR_SZ];
931+
int num_arr[SKARR_SZ];
932+
int idx, accum, res;
932933

933934
if (hlist_nulls_empty(&head->chain))
934935
continue;
935936

936937
if (i > s_i)
937938
s_num = 0;
938939

940+
next_chunk:
941+
num = 0;
942+
accum = 0;
939943
spin_lock_bh(lock);
940944
sk_nulls_for_each(sk, node, &head->chain) {
941-
int state, res;
945+
int state;
942946

943947
if (!net_eq(sock_net(sk), net))
944948
continue;
@@ -962,21 +966,35 @@ void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
962966
if (!inet_diag_bc_sk(bc, sk))
963967
goto next_normal;
964968

965-
res = sk_diag_fill(sk, skb, r,
969+
sock_hold(sk);
970+
num_arr[accum] = num;
971+
sk_arr[accum] = sk;
972+
if (++accum == SKARR_SZ)
973+
break;
974+
next_normal:
975+
++num;
976+
}
977+
spin_unlock_bh(lock);
978+
res = 0;
979+
for (idx = 0; idx < accum; idx++) {
980+
if (res >= 0) {
981+
res = sk_diag_fill(sk_arr[idx], skb, r,
966982
sk_user_ns(NETLINK_CB(cb->skb).sk),
967983
NETLINK_CB(cb->skb).portid,
968984
cb->nlh->nlmsg_seq, NLM_F_MULTI,
969985
cb->nlh, net_admin);
970-
if (res < 0) {
971-
spin_unlock_bh(lock);
972-
goto done;
986+
if (res < 0)
987+
num = num_arr[idx];
973988
}
974-
next_normal:
975-
++num;
989+
sock_gen_put(sk_arr[idx]);
976990
}
977-
978-
spin_unlock_bh(lock);
991+
if (res < 0)
992+
break;
979993
cond_resched();
994+
if (accum == SKARR_SZ) {
995+
s_num = num + 1;
996+
goto next_chunk;
997+
}
980998
}
981999

9821000
done:

‎net/ipv4/tcp.c‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,6 @@ void tcp_init_sock(struct sock *sk)
405405
tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
406406
tp->snd_cwnd_clamp = ~0;
407407
tp->mss_cache = TCP_MSS_DEFAULT;
408-
u64_stats_init(&tp->syncp);
409408

410409
tp->reordering = sock_net(sk)->ipv4.sysctl_tcp_reordering;
411410
tcp_enable_early_retrans(tp);
@@ -2710,9 +2709,8 @@ void tcp_get_info(struct sock *sk, struct tcp_info *info)
27102709
const struct tcp_sock *tp = tcp_sk(sk); /* iff sk_type == SOCK_STREAM */
27112710
const struct inet_connection_sock *icsk = inet_csk(sk);
27122711
u32 now = tcp_time_stamp, intv;
2713-
unsigned int start;
2714-
int notsent_bytes;
27152712
u64 rate64;
2713+
bool slow;
27162714
u32 rate;
27172715

27182716
memset(info, 0, sizeof(*info));
@@ -2792,17 +2790,17 @@ void tcp_get_info(struct sock *sk, struct tcp_info *info)
27922790

27932791
info->tcpi_total_retrans = tp->total_retrans;
27942792

2795-
do {
2796-
start = u64_stats_fetch_begin_irq(&tp->syncp);
2797-
put_unaligned(tp->bytes_acked, &info->tcpi_bytes_acked);
2798-
put_unaligned(tp->bytes_received, &info->tcpi_bytes_received);
2799-
} while (u64_stats_fetch_retry_irq(&tp->syncp, start));
2793+
slow = lock_sock_fast(sk);
2794+
2795+
put_unaligned(tp->bytes_acked, &info->tcpi_bytes_acked);
2796+
put_unaligned(tp->bytes_received, &info->tcpi_bytes_received);
2797+
info->tcpi_notsent_bytes = max_t(int, 0, tp->write_seq - tp->snd_nxt);
2798+
2799+
unlock_sock_fast(sk, slow);
2800+
28002801
info->tcpi_segs_out = tp->segs_out;
28012802
info->tcpi_segs_in = tp->segs_in;
28022803

2803-
notsent_bytes = READ_ONCE(tp->write_seq) - READ_ONCE(tp->snd_nxt);
2804-
info->tcpi_notsent_bytes = max(0, notsent_bytes);
2805-
28062804
info->tcpi_min_rtt = tcp_min_rtt(tp);
28072805
info->tcpi_data_segs_in = tp->data_segs_in;
28082806
info->tcpi_data_segs_out = tp->data_segs_out;

‎net/ipv4/tcp_input.c‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3351,9 +3351,7 @@ static void tcp_snd_una_update(struct tcp_sock *tp, u32 ack)
33513351
u32 delta = ack - tp->snd_una;
33523352

33533353
sock_owned_by_me((struct sock *)tp);
3354-
u64_stats_update_begin_raw(&tp->syncp);
33553354
tp->bytes_acked += delta;
3356-
u64_stats_update_end_raw(&tp->syncp);
33573355
tp->snd_una = ack;
33583356
}
33593357

@@ -3363,9 +3361,7 @@ static void tcp_rcv_nxt_update(struct tcp_sock *tp, u32 seq)
33633361
u32 delta = seq - tp->rcv_nxt;
33643362

33653363
sock_owned_by_me((struct sock *)tp);
3366-
u64_stats_update_begin_raw(&tp->syncp);
33673364
tp->bytes_received += delta;
3368-
u64_stats_update_end_raw(&tp->syncp);
33693365
tp->rcv_nxt = seq;
33703366
}
33713367

0 commit comments

Comments
 (0)