From patchwork Wed Oct 2 17:30:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 13820152 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BA6E91D1739 for ; Wed, 2 Oct 2024 17:30:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727890250; cv=none; b=Z/PNojTZdQRjc/jtkiS5A+FXlOzmSiohJtEzLnvGLhxXKJkJ97GZ4UWOoS3LmaYMbubJ3RyTbXaiUpnKluj/lLB/kAGjoWRNlGLNFiFPKL/VTb8fZDIvwU/CXbPGfR7nLemEpL9Ma7l0O3FY3+cI11ot4Wq77K0S3WhQUhD/r7g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727890250; c=relaxed/simple; bh=n1U+CJK2WNZaCFS+gqoMVyeDBpRHZzR4gIu1IpvHnJk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=EU5Y6KxfqLfi2y10S/XG4yVmhUbMX3ekztRczEbUyZQt8BWTUsBPGCl7Dfa0j5Q4eZK5T95GHmmWvHcg2QUnKWqgJFPeXphYga2zAmy4bBf1ugZGIxvqcc9/HBT2EYzhoOWJhG1gIu6MEwoT7bUPqm5UUBocmMWaP59jzKyl/Eg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Hfbsl98u; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Hfbsl98u" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6e24e37f1eeso881647b3.0 for ; Wed, 02 Oct 2024 10:30:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727890247; x=1728495047; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=iPC1n2jYybOYlpIvWYJSdmwmrMEoQH22CK9d9IpbAls=; b=Hfbsl98uIS2gfi6940lY26GZAcu4gj1+aHXb6b26jpKxA3TnUNC6cuP1sfCYK16L8M yKXKBPC9w9RPaos5JOrHiB6pMXE9CbVrPl8wHIErwxvuJSYq/sxxXTN2w976HUhkm1mC WDfH2Xvfu1DRuBDjyifOWE+wzi64itvF0INvzwbPF76d+w8EcGQA/mvjbIeJggALcYrS 5YsiTc9CvwZQ8QUi8D3ymRUYTog1GiDEOIO7JtcgkDwv5JQuRpgiL9rD6id+KGF3gsYv 6wS/AY38XP/RXxmimS+pRmfcJdhNC9USJT5ixQh+1KE2+V1dHTht1mMiUsyrLN/qxnWI nPOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727890247; x=1728495047; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=iPC1n2jYybOYlpIvWYJSdmwmrMEoQH22CK9d9IpbAls=; b=Qjfe7rHZyZNdn/7R9HJZFr4s7KE1BQxEh8sHb9S5AgjEUd72hgffI+Ir5aI+y6fuFW wqTvpmYzsA1A/UMSF20m1+D5ofdm9YroPwSANfww3GW5SkdwaMwEHpz71LYLrX4s6HZL jATwsLJMX8xVJxWQzUFXa3JyvjWZJ9oQ94p+SZHpeAp4Z1Xh/uNG3KYBkItXB4j5gmsu 0s0xph77u85+97IpmCuRBq21tDOeEB/BJTi3X73iG7zzGBRiHG3v2MnDxdxpckFF92is KsF1ddIVv6MQeQafjfn+1m3PwfNjIzYNboBmL2Oa+SojmzGCwTz726nOsguoQIXY3J1Z y06w== X-Forwarded-Encrypted: i=1; AJvYcCXKo+i+n+F24gKSx3FIpuJZOan1XbYwn85hMGq1+oiujpH7fUTV2U/VroqOTs5hlNDJrik6JrA=@vger.kernel.org X-Gm-Message-State: AOJu0YwA+MU9XOOCn/btaGIIS6tKeoyaACaamjSCFuxsqQO2sE7dk4Uw j1S7yoC/SD/AK69yu8h90zK64/Y6G9sbehKuiIkZjKxcdg+Ubf6iGP08CvcGHxpe5mQIEZbZceF Hn63NUxhYmQ== X-Google-Smtp-Source: AGHT+IGhYVNyZjbY3jNQLsjtMcZaFe7rn6xyouzytvUuOSM9AMcaoPIqHbTUQ4CtDg8gGhso+x5qzxyYS5H0yQ== X-Received: from edumazet1.c.googlers.com ([fda3:e722:ac3:cc00:f7:ea0b:ac12:11d6]) (user=edumazet job=sendgmr) by 2002:a05:690c:4609:b0:648:fc8a:cd23 with SMTP id 00721157ae682-6e2a2ad8dfemr356707b3.2.1727890247652; Wed, 02 Oct 2024 10:30:47 -0700 (PDT) Date: Wed, 2 Oct 2024 17:30:40 +0000 In-Reply-To: <20241002173042.917928-1-edumazet@google.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241002173042.917928-1-edumazet@google.com> X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241002173042.917928-2-edumazet@google.com> Subject: [PATCH net-next 1/3] tcp: annotate data-races around icsk->icsk_pending From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: Neal Cardwell , netdev@vger.kernel.org, eric.dumazet@gmail.com, Eric Dumazet X-Patchwork-Delegate: kuba@kernel.org icsk->icsk_pending can be read locklessly already. Following patch in the series will add another lockless read. Add smp_load_acquire() and smp_store_release() annotations because following patch will add a test in tcp_write_timer(), and READ_ONCE()/WRITE_ONCE() alone would possibly lead to races. Signed-off-by: Eric Dumazet --- include/net/inet_connection_sock.h | 4 ++-- net/ipv4/inet_connection_sock.c | 6 ++++-- net/ipv4/inet_diag.c | 10 ++++++---- net/ipv4/tcp_ipv4.c | 10 ++++++---- net/ipv4/tcp_output.c | 4 ++-- net/ipv4/tcp_timer.c | 4 ++-- net/ipv6/tcp_ipv6.c | 10 ++++++---- 7 files changed, 28 insertions(+), 20 deletions(-) diff --git a/include/net/inet_connection_sock.h b/include/net/inet_connection_sock.h index c0deaafebfdc0bc5b7f9e1b2a881f797a1a82ace..914d1977270449241f6fc6da2055f3af02a75f99 100644 --- a/include/net/inet_connection_sock.h +++ b/include/net/inet_connection_sock.h @@ -197,7 +197,7 @@ static inline void inet_csk_clear_xmit_timer(struct sock *sk, const int what) struct inet_connection_sock *icsk = inet_csk(sk); if (what == ICSK_TIME_RETRANS || what == ICSK_TIME_PROBE0) { - icsk->icsk_pending = 0; + smp_store_release(&icsk->icsk_pending, 0); #ifdef INET_CSK_CLEAR_TIMERS sk_stop_timer(sk, &icsk->icsk_retransmit_timer); #endif @@ -229,7 +229,7 @@ static inline void inet_csk_reset_xmit_timer(struct sock *sk, const int what, if (what == ICSK_TIME_RETRANS || what == ICSK_TIME_PROBE0 || what == ICSK_TIME_LOSS_PROBE || what == ICSK_TIME_REO_TIMEOUT) { - icsk->icsk_pending = what; + smp_store_release(&icsk->icsk_pending, what); icsk->icsk_timeout = jiffies + when; sk_reset_timer(sk, &icsk->icsk_retransmit_timer, icsk->icsk_timeout); } else if (what == ICSK_TIME_DACK) { diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c index 2c5632d4fddbe8ad96f6c35b9ed770d09126eb5d..8c53385cc808c61097898514fd91a322e3a08d31 100644 --- a/net/ipv4/inet_connection_sock.c +++ b/net/ipv4/inet_connection_sock.c @@ -775,7 +775,8 @@ void inet_csk_clear_xmit_timers(struct sock *sk) { struct inet_connection_sock *icsk = inet_csk(sk); - icsk->icsk_pending = icsk->icsk_ack.pending = 0; + smp_store_release(&icsk->icsk_pending, 0); + icsk->icsk_ack.pending = 0; sk_stop_timer(sk, &icsk->icsk_retransmit_timer); sk_stop_timer(sk, &icsk->icsk_delack_timer); @@ -790,7 +791,8 @@ void inet_csk_clear_xmit_timers_sync(struct sock *sk) /* ongoing timer handlers need to acquire socket lock. */ sock_not_owned_by_me(sk); - icsk->icsk_pending = icsk->icsk_ack.pending = 0; + smp_store_release(&icsk->icsk_pending, 0); + icsk->icsk_ack.pending = 0; sk_stop_timer_sync(sk, &icsk->icsk_retransmit_timer); sk_stop_timer_sync(sk, &icsk->icsk_delack_timer); diff --git a/net/ipv4/inet_diag.c b/net/ipv4/inet_diag.c index 67639309163d05c034fad80fc9a6096c3b79d42f..321acc8abf17e8c7d6a4e3326615123fff19deab 100644 --- a/net/ipv4/inet_diag.c +++ b/net/ipv4/inet_diag.c @@ -247,6 +247,7 @@ int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk, struct nlmsghdr *nlh; struct nlattr *attr; void *info = NULL; + u8 icsk_pending; int protocol; cb_data = cb->data; @@ -307,14 +308,15 @@ int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk, goto out; } - if (icsk->icsk_pending == ICSK_TIME_RETRANS || - icsk->icsk_pending == ICSK_TIME_REO_TIMEOUT || - icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) { + icsk_pending = smp_load_acquire(&icsk->icsk_pending); + if (icsk_pending == ICSK_TIME_RETRANS || + icsk_pending == ICSK_TIME_REO_TIMEOUT || + icsk_pending == ICSK_TIME_LOSS_PROBE) { r->idiag_timer = 1; r->idiag_retrans = icsk->icsk_retransmits; r->idiag_expires = jiffies_delta_to_msecs(icsk->icsk_timeout - jiffies); - } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) { + } else if (icsk_pending == ICSK_TIME_PROBE0) { r->idiag_timer = 4; r->idiag_retrans = icsk->icsk_probes_out; r->idiag_expires = diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c index 5afe5e57c89b5c28dfada2bf2e01fa7b3afa61f0..985028434f644c399e51d12ba8d9c2c5740dc6e1 100644 --- a/net/ipv4/tcp_ipv4.c +++ b/net/ipv4/tcp_ipv4.c @@ -2900,15 +2900,17 @@ static void get_tcp4_sock(struct sock *sk, struct seq_file *f, int i) __be32 src = inet->inet_rcv_saddr; __u16 destp = ntohs(inet->inet_dport); __u16 srcp = ntohs(inet->inet_sport); + u8 icsk_pending; int rx_queue; int state; - if (icsk->icsk_pending == ICSK_TIME_RETRANS || - icsk->icsk_pending == ICSK_TIME_REO_TIMEOUT || - icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) { + icsk_pending = smp_load_acquire(&icsk->icsk_pending); + if (icsk_pending == ICSK_TIME_RETRANS || + icsk_pending == ICSK_TIME_REO_TIMEOUT || + icsk_pending == ICSK_TIME_LOSS_PROBE) { timer_active = 1; timer_expires = icsk->icsk_timeout; - } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) { + } else if (icsk_pending == ICSK_TIME_PROBE0) { timer_active = 4; timer_expires = icsk->icsk_timeout; } else if (timer_pending(&sk->sk_timer)) { diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c index 4fd746bd4d54f621601b20c3821e71370a4a615a..4d04073016035dcf62ba5e0ad23aac86e54e65c7 100644 --- a/net/ipv4/tcp_output.c +++ b/net/ipv4/tcp_output.c @@ -2960,7 +2960,7 @@ void tcp_send_loss_probe(struct sock *sk) WARN_ONCE(tp->packets_out, "invalid inflight: %u state %u cwnd %u mss %d\n", tp->packets_out, sk->sk_state, tcp_snd_cwnd(tp), mss); - inet_csk(sk)->icsk_pending = 0; + smp_store_release(&inet_csk(sk)->icsk_pending, 0); return; } @@ -2993,7 +2993,7 @@ void tcp_send_loss_probe(struct sock *sk) NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPLOSSPROBES); /* Reset s.t. tcp_rearm_rto will restart timer from now */ - inet_csk(sk)->icsk_pending = 0; + smp_store_release(&inet_csk(sk)->icsk_pending, 0); rearm_timer: tcp_rearm_rto(sk); } diff --git a/net/ipv4/tcp_timer.c b/net/ipv4/tcp_timer.c index 79064580c8c0d55daa2ab4dc6d27a5ab8802e599..56c597e763ac7a8cebeba324f84e57b1eeeae977 100644 --- a/net/ipv4/tcp_timer.c +++ b/net/ipv4/tcp_timer.c @@ -701,11 +701,11 @@ void tcp_write_timer_handler(struct sock *sk) tcp_send_loss_probe(sk); break; case ICSK_TIME_RETRANS: - icsk->icsk_pending = 0; + smp_store_release(&icsk->icsk_pending, 0); tcp_retransmit_timer(sk); break; case ICSK_TIME_PROBE0: - icsk->icsk_pending = 0; + smp_store_release(&icsk->icsk_pending, 0); tcp_probe_timer(sk); break; } diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c index d71ab4e1efe1c6598cf3d3e4334adf0881064ce9..7634c0be6acbdb67bb378cc81bdbf184552d2afc 100644 --- a/net/ipv6/tcp_ipv6.c +++ b/net/ipv6/tcp_ipv6.c @@ -2177,6 +2177,7 @@ static void get_tcp6_sock(struct seq_file *seq, struct sock *sp, int i) const struct tcp_sock *tp = tcp_sk(sp); const struct inet_connection_sock *icsk = inet_csk(sp); const struct fastopen_queue *fastopenq = &icsk->icsk_accept_queue.fastopenq; + u8 icsk_pending; int rx_queue; int state; @@ -2185,12 +2186,13 @@ static void get_tcp6_sock(struct seq_file *seq, struct sock *sp, int i) destp = ntohs(inet->inet_dport); srcp = ntohs(inet->inet_sport); - if (icsk->icsk_pending == ICSK_TIME_RETRANS || - icsk->icsk_pending == ICSK_TIME_REO_TIMEOUT || - icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) { + icsk_pending = smp_load_acquire(&icsk->icsk_pending); + if (icsk_pending == ICSK_TIME_RETRANS || + icsk_pending == ICSK_TIME_REO_TIMEOUT || + icsk_pending == ICSK_TIME_LOSS_PROBE) { timer_active = 1; timer_expires = icsk->icsk_timeout; - } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) { + } else if (icsk_pending == ICSK_TIME_PROBE0) { timer_active = 4; timer_expires = icsk->icsk_timeout; } else if (timer_pending(&sp->sk_timer)) { From patchwork Wed Oct 2 17:30:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 13820153 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7F5271D173C for ; Wed, 2 Oct 2024 17:30:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727890252; cv=none; b=hRRyFxtWsgMcvvYDoFWGsJ/hhZGo8lqlIpN814vMst5G3EdCoXOFKlQYU8AFFkHoEBGnqAyBK/HmVHeUIyB5HJK6+2fhadwfW8kQdxSgHk3AJ3IhTnzPig0k2MWpZ1AlkLTsQrB/0GHErySxp4ulDe9q8xk3t3SMVD2tXWxL1zk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727890252; c=relaxed/simple; bh=Mx9NAXyY/1panWYdtBpAMYDxh3+0RQEKEHcQ7fZZYhU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PlCq294xrVqwNGCheSUaNI4b9I2NX1+VX9xmVkncBjcal25L9eIUGqYJC7sXuhhQQYNv7AQ58z7kjh43PuSqTVZWO8j8xN1VodiHQebwcpT3OM29+Yugel1HSCk+IgyZq6tmcEwVU5KQHmlWe5AXbsrAp+PTUWV1r/s/kzqm7H0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=LKAhsgiE; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="LKAhsgiE" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e28690bc290so59780276.1 for ; Wed, 02 Oct 2024 10:30:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727890249; x=1728495049; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=6krIsvtX8XuYRz/GzAA29M9JLrUvABMa7apKXA969/8=; b=LKAhsgiE2rhQNJDeKMHXU2wrTdYNHRgaKviwWwAga44G/QvWuXAxNXNrhOUowwxROk n3k2zRg3PF5+ehyAmTv4f603I6cSgY4VOHHjBqAaR33KiBokPJxjo7wA/APGKHKltAdM uO8dc2lyOIbmh2eqNCeX6TwsK42ylv4tfOkuhmLY0LyUZ4Qbu3VwYfN8wJ+HnyYEhweV NG7ywmq26YfUCn8wh0KZDW8Z2fscetPYai3FXvDiHpVzyvUdIoimGt4rxWbnG5siKHKR 9Sf5D2aVKq8iCIQjNxJKSzeGMuaTD3DVKOsO8cbJH8V1bCZpbrB3vwpIu80cRtrWQals hvGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727890249; x=1728495049; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=6krIsvtX8XuYRz/GzAA29M9JLrUvABMa7apKXA969/8=; b=JlptfsEyxB+Z3I/+Ji7NJ/lQP/cZl4JJ+atNJTtM35zzKoUwtAFKEvapDIA9TNhamI pFNPiG7JHF7fw/ACGU1HMWL60KCJby55jJyPWV5koOrzrOkLrREWdVAqLp4FGRSub0Ze ihX8w/gK8fGXC/uk3oYeIIk5Mq7DYimPquIUa8AJGaEIFWcx5Ux1pmZVcfsKPHksnuxF AS8Mk5PvLuiiAfgalTohJqpeB4MJd1W7skEoUHSYwa2hwmLtH/VM9KbEskxNRklKTgsW pha7ITz18d+l6O1/PQh1VLqKU+dhsc34SI6VrrlrSjM8iTsN2bvgl1o2/er0vYJ4inW0 9PLQ== X-Forwarded-Encrypted: i=1; AJvYcCXsfqxK25swTkKvTusq3Vnd6d18fLdo+od229w7xDlTW5iTzpGUy6W+MUjTdl/kIz6K+AJklKY=@vger.kernel.org X-Gm-Message-State: AOJu0YzqKi/1EUq79GbnNb7LFWzmdnEMz7Ju9CMCtM3l/hG/UhR/7Lq1 CtJ2NdpQKlqxIZeOx+2KGF2hIXz//Ej64sRGdqcsP0ZNdTLcUon5dpjK68TFP4jvOTkuyoEwqlH S6hKtQjHPYQ== X-Google-Smtp-Source: AGHT+IGqSoiL1LzssJ3SQ9XcexQ6smWfeWWTWro7wOAO9TZpxvhW01YNT1aM3CXbgVtNHjocMKbm5gKC1fASiw== X-Received: from edumazet1.c.googlers.com ([fda3:e722:ac3:cc00:f7:ea0b:ac12:11d6]) (user=edumazet job=sendgmr) by 2002:a25:e088:0:b0:e24:96b1:696 with SMTP id 3f1490d57ef6-e2638382b0cmr2237276.1.1727890249270; Wed, 02 Oct 2024 10:30:49 -0700 (PDT) Date: Wed, 2 Oct 2024 17:30:41 +0000 In-Reply-To: <20241002173042.917928-1-edumazet@google.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241002173042.917928-1-edumazet@google.com> X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241002173042.917928-3-edumazet@google.com> Subject: [PATCH net-next 2/3] tcp: add a fast path in tcp_write_timer() From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: Neal Cardwell , netdev@vger.kernel.org, eric.dumazet@gmail.com, Eric Dumazet X-Patchwork-Delegate: kuba@kernel.org retransmit timer is not stopped from inet_csk_clear_xmit_timer() because we do not define INET_CSK_CLEAR_TIMERS. This is a conscious choice : for active TCP flows, it is better to only call mod_timer(), because there is more chances of keeping the timer unchanged. Also inet_csk_clear_xmit_timer() is often called from another cpu, and calling del_timer() would cause false sharing and lock contention. This means that very often, tcp_write_timer() is called at the timer expiration, while there is nothing to retransmit. This can be detected very early, avoiding the socket spinlock. Signed-off-by: Eric Dumazet --- net/ipv4/tcp_timer.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/net/ipv4/tcp_timer.c b/net/ipv4/tcp_timer.c index 56c597e763ac7a8cebeba324f84e57b1eeeae977..b7266b9101ce5933776bd38d086287667e3a7f18 100644 --- a/net/ipv4/tcp_timer.c +++ b/net/ipv4/tcp_timer.c @@ -717,6 +717,10 @@ static void tcp_write_timer(struct timer_list *t) from_timer(icsk, t, icsk_retransmit_timer); struct sock *sk = &icsk->icsk_inet.sk; + /* Avoid locking the socket when there is no pending event. */ + if (!smp_load_acquire(&icsk->icsk_pending)) + goto out; + bh_lock_sock(sk); if (!sock_owned_by_user(sk)) { tcp_write_timer_handler(sk); @@ -726,6 +730,7 @@ static void tcp_write_timer(struct timer_list *t) sock_hold(sk); } bh_unlock_sock(sk); +out: sock_put(sk); } From patchwork Wed Oct 2 17:30:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 13820154 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 02EAD1D1739 for ; Wed, 2 Oct 2024 17:30:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727890253; cv=none; b=mO3VTbsZ+GLGVAsT8VPV+NSkdA+fdfejAQ5vC84m5XuHYXpb1F+cUILrwKD4Tt0fzXmkTSZwDo40FQWCZUjP8euPTXtXDeUdXdX5FS1dCknbdxeawDgyne3afDvPcTclUxroraGhG4xUNu5gveE/DeQlcRtqoWoa/IP3MGdf5aQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727890253; c=relaxed/simple; bh=1C7LtANCjqvXjkSMmkDHQhTROZHZm5Z9JHfUnZf50zQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=c8YNPtEM16I7z6D2tXngw7CVrKYmmMqlo9opex53wQjaJUThGbH0DS2Ak8l/+IwI2TSjTAwTqwKhpBBBb/+gaYnE6ZjfZ7yGQVsxFWxHIs9QQVJ01/LVbUa4cW82RHq5PA6dLUptvOffr4Fhb1e0d55dC1XQi7I2WCpWb8YeHZU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=UzSNJaWb; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="UzSNJaWb" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6e25a6fddb0so1144587b3.0 for ; Wed, 02 Oct 2024 10:30:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727890251; x=1728495051; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=wxWBwsiwhkxidE9d07ZP7OQ4yl5Vlsy/f7najGyn8KU=; b=UzSNJaWbBW3dyslJBvKllIreElHU2xYjTeuQ7uNwShVzo2LxeANoQw29S9ZMhMOFCF DbXxqFDG1IsSXMuH8DpBdXlZkfEXP3AaiE41gkyw8GnlyiHGLSjcnro/RWS39cSzaaAl D7Jn2bHtIfr0KVwWGm1U6Xgt/p5CAxmhwTdWFR2v+tvwG10uewQ9KTcVvPO84Npwyix9 hQV0Q2ivfBpy69/ivo1IVyT5GyKutYw9GuOHVtYewJsdOI0nczI7ebgvs/3EnBYnL1Z8 eYFjDon/tNkxDpRUFdh/FboSNnG0ReHShDGB7vA4VuQDAmPIQfhs7vF+4v4opcRBnZQZ 7hog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727890251; x=1728495051; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=wxWBwsiwhkxidE9d07ZP7OQ4yl5Vlsy/f7najGyn8KU=; b=B7LHl0D5vWgE9AkjV95E3RfpNx8YAZpoz61Z6GSF4FL5pGdclNZXKxKYqASLt3BTrm m1n7vFHQidCH4wIVLVVzEpcRKYQ1Zf2rCmxzCD6Hdav2co67akAMhvO/h+gh4nFsfyZK I8vsgu2BitnzqJoswsE1Tt8vqtitRC14GdXIHXgBtzWiGZrC5D7oakMflmMTk4c07las zBf0zjWIrrV4d4qHtZU9dFkO9iLhirW8vlOkIJzunn527GTFq9bO20thi5BVtWJCsW59 /UeaTQsFHVcS+DmHio5/AxeXU6BnlRojb14RniScLyc1cXVNiQHTiDTdgJmBz1j3GSGF m1cg== X-Forwarded-Encrypted: i=1; AJvYcCXisTFfgEk3qFUnNuYpy7ijpGpRaBCXFPabFxi9O/5sehh4i26w6whqH+avWs4pQaHkuI3g4fs=@vger.kernel.org X-Gm-Message-State: AOJu0YyM1Ev6L6T6IAvRlEHsD6tpXmRC+QtTfVkd3J6XmToIJySqPTNl 43lJv2kMXpMKEonmTpIM5a8M48jNnIRjpDjTDrdgmEn8jbhE7YMiFQXDTcOWMG57jhxkAAXAx8E fyfGKN76vvg== X-Google-Smtp-Source: AGHT+IEDjeeMErPSqjj8QsgH5ugDTe/WngCiK5d+ijrTWl1SmTPvI3GxDfxWSI6GIqiUQxnyMPVeHAlJvWPK4w== X-Received: from edumazet1.c.googlers.com ([fda3:e722:ac3:cc00:f7:ea0b:ac12:11d6]) (user=edumazet job=sendgmr) by 2002:a05:690c:7603:b0:69b:c01:82a5 with SMTP id 00721157ae682-6e2a3049ddbmr254307b3.7.1727890250879; Wed, 02 Oct 2024 10:30:50 -0700 (PDT) Date: Wed, 2 Oct 2024 17:30:42 +0000 In-Reply-To: <20241002173042.917928-1-edumazet@google.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241002173042.917928-1-edumazet@google.com> X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241002173042.917928-4-edumazet@google.com> Subject: [PATCH net-next 3/3] tcp: add a fast path in tcp_delack_timer() From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: Neal Cardwell , netdev@vger.kernel.org, eric.dumazet@gmail.com, Eric Dumazet X-Patchwork-Delegate: kuba@kernel.org delack timer is not stopped from inet_csk_clear_xmit_timer() because we do not define INET_CSK_CLEAR_TIMERS. This is a conscious choice : inet_csk_clear_xmit_timer() is often called from another cpu. Calling del_timer() would cause false sharing and lock contention. This means that very often, tcp_delack_timer() is called at the timer expiration, while there is no ACK to transmit. This can be detected very early, avoiding the socket spinlock. Notes: - test about tp->compressed_ack is racy, but in the unlikely case there is a race, the dedicated compressed_ack_timer hrtimer would close it. - Even if the fast path is not taken, reading icsk->icsk_ack.pending and tp->compressed_ack before acquiring the socket spinlock reduces acquisition time and chances of contention. Signed-off-by: Eric Dumazet --- include/net/inet_connection_sock.h | 5 +++-- net/ipv4/inet_connection_sock.c | 4 ++-- net/ipv4/tcp_output.c | 3 ++- net/ipv4/tcp_timer.c | 9 +++++++++ net/mptcp/protocol.c | 3 ++- 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/include/net/inet_connection_sock.h b/include/net/inet_connection_sock.h index 914d1977270449241f6fc6da2055f3af02a75f99..3c82fad904d4c6c51069e2e703673d667bb36d06 100644 --- a/include/net/inet_connection_sock.h +++ b/include/net/inet_connection_sock.h @@ -202,7 +202,7 @@ static inline void inet_csk_clear_xmit_timer(struct sock *sk, const int what) sk_stop_timer(sk, &icsk->icsk_retransmit_timer); #endif } else if (what == ICSK_TIME_DACK) { - icsk->icsk_ack.pending = 0; + smp_store_release(&icsk->icsk_ack.pending, 0); icsk->icsk_ack.retry = 0; #ifdef INET_CSK_CLEAR_TIMERS sk_stop_timer(sk, &icsk->icsk_delack_timer); @@ -233,7 +233,8 @@ static inline void inet_csk_reset_xmit_timer(struct sock *sk, const int what, icsk->icsk_timeout = jiffies + when; sk_reset_timer(sk, &icsk->icsk_retransmit_timer, icsk->icsk_timeout); } else if (what == ICSK_TIME_DACK) { - icsk->icsk_ack.pending |= ICSK_ACK_TIMER; + smp_store_release(&icsk->icsk_ack.pending, + icsk->icsk_ack.pending | ICSK_ACK_TIMER); icsk->icsk_ack.timeout = jiffies + when; sk_reset_timer(sk, &icsk->icsk_delack_timer, icsk->icsk_ack.timeout); } else { diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c index 8c53385cc808c61097898514fd91a322e3a08d31..12e975ed4910d8c7cca79b1812f365589a5d469a 100644 --- a/net/ipv4/inet_connection_sock.c +++ b/net/ipv4/inet_connection_sock.c @@ -776,7 +776,7 @@ void inet_csk_clear_xmit_timers(struct sock *sk) struct inet_connection_sock *icsk = inet_csk(sk); smp_store_release(&icsk->icsk_pending, 0); - icsk->icsk_ack.pending = 0; + smp_store_release(&icsk->icsk_ack.pending, 0); sk_stop_timer(sk, &icsk->icsk_retransmit_timer); sk_stop_timer(sk, &icsk->icsk_delack_timer); @@ -792,7 +792,7 @@ void inet_csk_clear_xmit_timers_sync(struct sock *sk) sock_not_owned_by_me(sk); smp_store_release(&icsk->icsk_pending, 0); - icsk->icsk_ack.pending = 0; + smp_store_release(&icsk->icsk_ack.pending, 0); sk_stop_timer_sync(sk, &icsk->icsk_retransmit_timer); sk_stop_timer_sync(sk, &icsk->icsk_delack_timer); diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c index 4d04073016035dcf62ba5e0ad23aac86e54e65c7..08772395690d13a0c3309a273543a51aa0dd3fdc 100644 --- a/net/ipv4/tcp_output.c +++ b/net/ipv4/tcp_output.c @@ -4224,7 +4224,8 @@ void tcp_send_delayed_ack(struct sock *sk) if (!time_before(timeout, icsk->icsk_ack.timeout)) timeout = icsk->icsk_ack.timeout; } - icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER; + smp_store_release(&icsk->icsk_ack.pending, + icsk->icsk_ack.pending | ICSK_ACK_SCHED | ICSK_ACK_TIMER); icsk->icsk_ack.timeout = timeout; sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout); } diff --git a/net/ipv4/tcp_timer.c b/net/ipv4/tcp_timer.c index b7266b9101ce5933776bd38d086287667e3a7f18..c3a7442332d4926a6089812f789e89ee23081306 100644 --- a/net/ipv4/tcp_timer.c +++ b/net/ipv4/tcp_timer.c @@ -361,6 +361,14 @@ static void tcp_delack_timer(struct timer_list *t) from_timer(icsk, t, icsk_delack_timer); struct sock *sk = &icsk->icsk_inet.sk; + /* Avoid taking socket spinlock if there is no ACK to send. + * The compressed_ack check is racy, but a separate hrtimer + * will take care of it eventually. + */ + if (!(smp_load_acquire(&icsk->icsk_ack.pending) & ICSK_ACK_TIMER) && + !READ_ONCE(tcp_sk(sk)->compressed_ack)) + goto out; + bh_lock_sock(sk); if (!sock_owned_by_user(sk)) { tcp_delack_timer_handler(sk); @@ -371,6 +379,7 @@ static void tcp_delack_timer(struct timer_list *t) sock_hold(sk); } bh_unlock_sock(sk); +out: sock_put(sk); } diff --git a/net/mptcp/protocol.c b/net/mptcp/protocol.c index c2317919fc148a67a81ded795359bd613c9b0dff..e85862352084907582ec884dcb96832356419fa5 100644 --- a/net/mptcp/protocol.c +++ b/net/mptcp/protocol.c @@ -3504,7 +3504,8 @@ static void schedule_3rdack_retransmission(struct sock *ssk) timeout += jiffies; WARN_ON_ONCE(icsk->icsk_ack.pending & ICSK_ACK_TIMER); - icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER; + smp_store_release(&icsk->icsk_ack.pending, + icsk->icsk_ack.pending | ICSK_ACK_SCHED | ICSK_ACK_TIMER); icsk->icsk_ack.timeout = timeout; sk_reset_timer(ssk, &icsk->icsk_delack_timer, timeout); }