Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 06d68740 authored by Eric Dumazet's avatar Eric Dumazet Committed by Henrik Grimler
Browse files

tcp: fix a potential deadlock in tcp_get_info()



[ Upstream commit caed81e32f82b051bfaeb3816d6374c8149b79ad ]

Taking socket spinlock in tcp_get_info() can deadlock, as
inet_diag_dump_icsk() holds the &hashinfo->ehash_locks[i],
while packet processing can use the reverse locking order.

We could avoid this locking for TCP_LISTEN states, but lockdep would
certainly get confused as all TCP sockets share same lockdep classes.

[  523.722504] ======================================================
[  523.728706] [ INFO: possible circular locking dependency detected ]
[  523.734990] 4.1.0-dbg-DEV #1676 Not tainted
[  523.739202] -------------------------------------------------------
[  523.745474] ss/18032 is trying to acquire lock:
[  523.750002]  (slock-AF_INET){+.-...}, at: [<ffffffff81669d44>] tcp_get_info+0x2c4/0x360
[  523.758129]
[  523.758129] but task is already holding lock:
[  523.763968]  (&(&hashinfo->ehash_locks[i])->rlock){+.-...}, at: [<ffffffff816bcb75>] inet_diag_dump_icsk+0x1d5/0x6c0
[  523.774661]
[  523.774661] which lock already depends on the new lock.
[  523.774661]
[  523.782850]
[  523.782850] the existing dependency chain (in reverse order) is:
[  523.790326]
-> #1 (&(&hashinfo->ehash_locks[i])->rlock){+.-...}:
[  523.796599]        [<ffffffff811126bb>] lock_acquire+0xbb/0x270
[  523.802565]        [<ffffffff816f5868>] _raw_spin_lock+0x38/0x50
[  523.808628]        [<ffffffff81665af8>] __inet_hash_nolisten+0x78/0x110
[  523.815273]        [<ffffffff816819db>] tcp_v4_syn_recv_sock+0x24b/0x350
[  523.822067]        [<ffffffff81684d41>] tcp_check_req+0x3c1/0x500
[  523.828199]        [<ffffffff81682d09>] tcp_v4_do_rcv+0x239/0x3d0
[  523.834331]        [<ffffffff816842fe>] tcp_v4_rcv+0xa8e/0xc10
[  523.840202]        [<ffffffff81658fa3>] ip_local_deliver_finish+0x133/0x3e0
[  523.847214]        [<ffffffff81659a9a>] ip_local_deliver+0xaa/0xc0
[  523.853440]        [<ffffffff816593b8>] ip_rcv_finish+0x168/0x5c0
[  523.859624]        [<ffffffff81659db7>] ip_rcv+0x307/0x420

Lets use u64_sync infrastructure instead. As a bonus, 64bit
arches get optimized, as these are nop for them.

Fixes: 0df48c26d841 ("tcp: add tcpi_bytes_acked to tcp_info")
Signed-off-by: default avatarEric Dumazet <edumazet@google.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
Change-Id: I50c833b77f41111a6fa3a57834c6311f95573754
Signed-off-by: default avatarKevin F. Haggerty <haggertk@lineageos.org>
parent 5d82eed9
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -165,6 +165,8 @@ 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) */
+6 −4
Original line number Diff line number Diff line
@@ -2721,6 +2721,7 @@ void tcp_get_info(struct sock *sk, struct tcp_info *info)
	const struct tcp_sock *tp = tcp_sk(sk);
	const struct inet_connection_sock *icsk = inet_csk(sk);
	u32 now = tcp_time_stamp;
	unsigned int start;

	memset(info, 0, sizeof(*info));

@@ -2786,12 +2787,13 @@ void tcp_get_info(struct sock *sk, struct tcp_info *info)
	info->tcpi_max_pacing_rate = sk->sk_max_pacing_rate != ~0U ?
					sk->sk_max_pacing_rate : ~0ULL;

	spin_lock_bh(&sk->sk_lock.slock);
	do {
		start = u64_stats_fetch_begin_irq(&tp->syncp);
		info->tcpi_bytes_acked = tp->bytes_acked;
	info->tcpi_bytes_received = tp->bytes_received; /* RFC4898 tcpEStatsAppHCThruOctetsReceived */
		info->tcpi_bytes_received = tp->bytes_received;
	} while (u64_stats_fetch_retry_irq(&tp->syncp, start));
	info->tcpi_segs_out = tp->segs_out;
	info->tcpi_segs_in = tp->segs_in;
	spin_unlock_bh(&sk->sk_lock.slock);

	if (sk->sk_socket) {
		struct file *filep = sk->sk_socket->file;
+4 −0
Original line number Diff line number Diff line
@@ -3253,7 +3253,9 @@ static void tcp_snd_una_update(struct tcp_sock *tp, u32 ack)
{
	u32 delta = ack - tp->snd_una;

	u64_stats_update_begin(&tp->syncp);
	tp->bytes_acked += delta;
	u64_stats_update_end(&tp->syncp);
	tp->snd_una = ack;
}

@@ -3262,7 +3264,9 @@ static void tcp_rcv_nxt_update(struct tcp_sock *tp, u32 seq)
{
	u32 delta = seq - tp->rcv_nxt;

	u64_stats_update_begin(&tp->syncp);
	tp->bytes_received += delta;
	u64_stats_update_end(&tp->syncp);
	tp->rcv_nxt = seq;
}