From patchwork Tue May 28 12:52:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 13676619 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (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 BE35816D31F for ; Tue, 28 May 2024 12:53:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716900783; cv=none; b=Zxg7bdiQlkygoPPkoIP/K1qKEOjPh3INpMbq8s3Lp1Pu+iozj1zFPQFR8mKWID1gB/aLrnHyP12dJdhgtx5eRzUhe4NgSrBd1r21Oq04GzcYh0F+qgkHIDmC1gh0gd9IW3kQldD8VQlWhAChLJyOeIWukyONJHpuMt6O2uAIA2Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716900783; c=relaxed/simple; bh=MAoKDpoXetUM99sFDHvBUGkL1Z7tBtSD7nhqH9eF8Hs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XONeHs5FAphxo607gT2BFRY4i/fiibfbah26SlChGQI5eoppEj/KHt8Gb3hUhoT1aAEngdlwUPFpY05WtlChRf3pEJqyy98fTow0zzXnaXcCf0xKvUx7lVPfbhRcjGjfkoRAMLyg1hwCFtsDu2ir5lOkmC9V5Cx4OIwkdunUqmY= 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=NiIQPBPp; arc=none smtp.client-ip=209.85.128.201 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="NiIQPBPp" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-62a083e617aso13228757b3.2 for ; Tue, 28 May 2024 05:53:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1716900781; x=1717505581; 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=/1lGXVmkLky4THEpPNXaUJIFX3a/teJc1Gpnh3zc3z0=; b=NiIQPBPpQURR8zU/rjEGqFnHaVvfrR0+W9ilurWfGLExPKLBDYBQhy9GDMMDgbqsil NRadsmMTKibedT851yumSWAXoZxdTH07ZhsG9S9q78Vxn1DxqPHX4goNZLJNFU7YDxsi YZ2hP3A9xkzP4Nmj1oOlL/tNZ4DjvQmr7RNAzOMopJB1AQSvY2DoGzTbARd2tS5nwriJ iSHlN9hDnChW10Xclu6bZwXGRMUN4b+f4Zwjg+s2GiboXV4GDA4qEJfgYprl6ag5zCvB F7pvn2fVRjauycUsjN7FuAPL6kaA5GLhl3gNTQiSt57887sHgM5ByK66ZPzuHEsVaKz7 4PKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716900781; x=1717505581; 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=/1lGXVmkLky4THEpPNXaUJIFX3a/teJc1Gpnh3zc3z0=; b=EyzlAGTaRrr4jGwgj7sKfXvZTLjxiB0vl62tPgGmfv32OlPo5V3u39BfmPrwlUs/Q/ 2pJ5CdJHAiofc94VT3exnCbLNWU9dFMHeVn/BANAOpkCklGSzCJkdYNXLkKuXeqZ1rn4 QRzVphWf4pQLU1rOGL61VUbQtUrdFCiOb5bBGbGWicXn6VpXr2qce3ow4egU0+7giRh7 HWd70WAj0lDpLVSomSVw9WDu1C3qz3Eu5FolXc+pdyTjU7ZfLeXMIlWz06Ct6RBaXbt+ SaAzr0PnOkpyJ4wbL5ytFw8fEmdSz5Obgolomp9SQsBRqcomNhk0NowyaRi8kZFc4QUN wxxQ== X-Forwarded-Encrypted: i=1; AJvYcCVMEZsFFakhsiEXQk8d/UEqOGX2DbdfrNpgffqI0PsKoP6LYSRKM0/G9+rfGte5fFnfmlNAS1PAcsJ9VgSRWkfRGai+9kHZ X-Gm-Message-State: AOJu0YxrhOqL22m1hu/j38Ip7dWNQivP6NiWkmDVCQZE6UTroT2jIctD o25bI+nC5BswT9i/+QtNrOG6dsapIV+eYAcnyLPVHITni66fP+c5o4ztjMH+mEuQkOumiUvNZEU Q8twtsiZB+g== X-Google-Smtp-Source: AGHT+IGeIrikSjo5bUl9Wn4tTFPc47oRSEL4TX3Cn5/TOsf/u6YNxPJ1u9wGAqiknVnvbco8i0sOdAND3RR8MA== X-Received: from edumazet1.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:395a]) (user=edumazet job=sendgmr) by 2002:a05:6902:2b03:b0:de4:5ce2:7d2 with SMTP id 3f1490d57ef6-df7721dc43cmr3463586276.4.1716900780840; Tue, 28 May 2024 05:53:00 -0700 (PDT) Date: Tue, 28 May 2024 12:52:50 +0000 In-Reply-To: <20240528125253.1966136-1-edumazet@google.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240528125253.1966136-1-edumazet@google.com> X-Mailer: git-send-email 2.45.1.288.g0e0cd299f1-goog Message-ID: <20240528125253.1966136-2-edumazet@google.com> Subject: [PATCH v2 net-next 1/4] tcp: add tcp_done_with_error() helper From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: Neal Cardwell , David Laight , netdev@vger.kernel.org, eric.dumazet@gmail.com, Eric Dumazet X-Patchwork-Delegate: kuba@kernel.org tcp_reset() ends with a sequence that is carefuly ordered. We need to fix [e]poll bugs in the following patches, it makes sense to use a common helper. Suggested-by: Neal Cardwell Signed-off-by: Eric Dumazet Acked-by: Neal Cardwell --- include/net/tcp.h | 1 + net/ipv4/tcp.c | 2 +- net/ipv4/tcp_input.c | 32 +++++++++++++++++++++----------- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/include/net/tcp.h b/include/net/tcp.h index 060e95b331a286ad7c355be11dc03250d2944920..32815a40dea16637d2cc49d46863b532a44fbab3 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -677,6 +677,7 @@ void tcp_skb_collapse_tstamp(struct sk_buff *skb, /* tcp_input.c */ void tcp_rearm_rto(struct sock *sk); void tcp_synack_rtt_meas(struct sock *sk, struct request_sock *req); +void tcp_done_with_error(struct sock *sk, int err); void tcp_reset(struct sock *sk, struct sk_buff *skb); void tcp_fin(struct sock *sk); void tcp_check_space(struct sock *sk); diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 681b54e1f3a64387787738ab6495531b8abe1771..2a8f8d8676ff1d30ea9f8cd47ccf9236940eb299 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -598,7 +598,7 @@ __poll_t tcp_poll(struct file *file, struct socket *sock, poll_table *wait) */ mask |= EPOLLOUT | EPOLLWRNORM; } - /* This barrier is coupled with smp_wmb() in tcp_reset() */ + /* This barrier is coupled with smp_wmb() in tcp_done_with_error() */ smp_rmb(); if (READ_ONCE(sk->sk_err) || !skb_queue_empty_lockless(&sk->sk_error_queue)) diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index 9c04a9c8be9dfaa0ec2437b3748284e57588b216..5aadf64e554d8009b2739613c279bbf82a05bbdd 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -4436,9 +4436,26 @@ static enum skb_drop_reason tcp_sequence(const struct tcp_sock *tp, return SKB_NOT_DROPPED_YET; } + +void tcp_done_with_error(struct sock *sk, int err) +{ + /* This barrier is coupled with smp_rmb() in tcp_poll() */ + WRITE_ONCE(sk->sk_err, err); + smp_wmb(); + + tcp_write_queue_purge(sk); + tcp_done(sk); + + if (!sock_flag(sk, SOCK_DEAD)) + sk_error_report(sk); +} +EXPORT_SYMBOL(tcp_done_with_error); + /* When we get a reset we do this. */ void tcp_reset(struct sock *sk, struct sk_buff *skb) { + int err; + trace_tcp_receive_reset(sk); /* mptcp can't tell us to ignore reset pkts, @@ -4450,24 +4467,17 @@ void tcp_reset(struct sock *sk, struct sk_buff *skb) /* We want the right error as BSD sees it (and indeed as we do). */ switch (sk->sk_state) { case TCP_SYN_SENT: - WRITE_ONCE(sk->sk_err, ECONNREFUSED); + err = ECONNREFUSED; break; case TCP_CLOSE_WAIT: - WRITE_ONCE(sk->sk_err, EPIPE); + err = EPIPE; break; case TCP_CLOSE: return; default: - WRITE_ONCE(sk->sk_err, ECONNRESET); + err = ECONNRESET; } - /* This barrier is coupled with smp_rmb() in tcp_poll() */ - smp_wmb(); - - tcp_write_queue_purge(sk); - tcp_done(sk); - - if (!sock_flag(sk, SOCK_DEAD)) - sk_error_report(sk); + tcp_done_with_error(sk, err); } /* From patchwork Tue May 28 12:52:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 13676620 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (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 9992016D9CF for ; Tue, 28 May 2024 12:53:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716900785; cv=none; b=ErCMlhgQhsV0NDK6DaXZC88l7Xem3oeVskp4984MKMbZOP/c2bLMcOpq+waIChhsYSU/2qQ/bmTRuxkUq1eEaFHmfJRVNbOLxX3u48ULd3RCxJhwOVoL3hdQqyC6+jaCVcjg6cxDaD6L75sV008HB3dB72rXkVj9//+PSeEYEZE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716900785; c=relaxed/simple; bh=2bEM0Vdj69RbU+Ng2J1vFnI6UGxQv0T6zWDf22KxmvA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=TFlb42fmz3oI46QvBsXoLsE0OW1FfSNkrCnNfyapVFmmxIcxt2ifvwo5wivBQBcf5h8CxhAVmBHA0V3BHuDKaHB/zUaOKUaZJCxl7mNt1xO5Ue13dpO8RzWI2UNU5BoKripGXRfhRTle/OnG0LPGB6kdYnUb/o3QVjgY6nZi8as= 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=hVOT3/0V; arc=none smtp.client-ip=209.85.219.201 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="hVOT3/0V" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-df7bdb0455bso1173994276.2 for ; Tue, 28 May 2024 05:53:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1716900782; x=1717505582; 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=enO1CN/0applm4uZNOehy0RY7PCIzFl6GbDmIfliayI=; b=hVOT3/0VrJeJiJrKl+XKJRUcQEkPaex1MoD/2rCubj+/TuAgi+SlJFlQlfy1OjcM56 0tsorZmlbQpyxSJJllMnqT0MqRLv3OT0yPvBaaNypRKca9xYMGy5T7Mgc3MR8k8vE/9W AjNJ0qoWDdwgUO+4USkzzNea83yOkAAKb0zFwm8E/EhPrWNmSVqpsWHorUaev58LUqr5 mjB3g5YOkqCZY5mPBqFhVK9sLo5fFs1cV6kf46p1Utg1bakSrIn6UmwTpj++e/whxWFa owdZ7eeZiSh0L+S9BbjJ92zg7W1rJYK+Ck8b2LnB5nSEuKUY2D7Twjlc246P+7k14Mlk zCpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716900782; x=1717505582; 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=enO1CN/0applm4uZNOehy0RY7PCIzFl6GbDmIfliayI=; b=a1bGzlOJ7VcOIqmpBK4/OQ04DaFpzFzPiw0+xHvYb9vzGGPC3ZTiKce2JiOG1LfaFq BwPzmZ7IP7YWF6HMUxJLcljog0PLhmZFgH6LKU828+mZyjYnfU+rkqNLmRwUlGlDWd9q SLDY9/VwvDWKbjAOMlunPp7nYanLaWtvOrSQixnymolTtwozxsC8B+enuxiQfL3ISGG1 C7P2d3uPY7T/2yezZ6O+2NmER2wGPRe1PcY0y002/fyrrVG/T0XpNWOHcN0tdFQp1TaX wv8YT9NGl2wYv+9EejsNX9vwXIOdbgkZHK26YHweFHEEqRvGX/WPH1Xq02S69vLYYgZr ti1g== X-Forwarded-Encrypted: i=1; AJvYcCUon7WYT7sUqBjqji6v0ZBbSwBVUW+PhGLk1d3+yBjiJHEJIjgMY71Fyck2A7e69lzrNsjbtE7Ri78BJzFHQlsc+xI7Acdm X-Gm-Message-State: AOJu0YxNVe9QFtMsZG4/UiHC/f1CoqBzIGE0Mp2yECGcVN7L6X6dJwPx NNTdc5d22Ni4362LFkOlMtEorE18Z/eQPUvSpto62s2K/QO0n/URn01c5LU5pWaxINWP7k5bcL1 /K0TJ1BGo1A== X-Google-Smtp-Source: AGHT+IEPQtEs1LLWFkn9N9fQSALKMHsbtz+5CSElIvKwqzOIirhCdfxkhbQoLMpNl8GESyADKZDZxBzYz9RoXQ== X-Received: from edumazet1.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:395a]) (user=edumazet job=sendgmr) by 2002:a05:6902:1207:b0:dee:615c:ac3a with SMTP id 3f1490d57ef6-df77218837cmr836706276.5.1716900782506; Tue, 28 May 2024 05:53:02 -0700 (PDT) Date: Tue, 28 May 2024 12:52:51 +0000 In-Reply-To: <20240528125253.1966136-1-edumazet@google.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240528125253.1966136-1-edumazet@google.com> X-Mailer: git-send-email 2.45.1.288.g0e0cd299f1-goog Message-ID: <20240528125253.1966136-3-edumazet@google.com> Subject: [PATCH v2 net-next 2/4] tcp: fix race in tcp_write_err() From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: Neal Cardwell , David Laight , netdev@vger.kernel.org, eric.dumazet@gmail.com, Eric Dumazet X-Patchwork-Delegate: kuba@kernel.org I noticed flakes in a packetdrill test, expecting an epoll_wait() to return EPOLLERR | EPOLLHUP on a failed connect() attempt, after multiple SYN retransmits. It sometimes return EPOLLERR only. The issue is that tcp_write_err(): 1) writes an error in sk->sk_err, 2) calls sk_error_report(), 3) then calls tcp_done(). tcp_done() is writing SHUTDOWN_MASK into sk->sk_shutdown, among other things. Problem is that the awaken user thread (from 2) sk_error_report()) might call tcp_poll() before tcp_done() has written sk->sk_shutdown. tcp_poll() only sees a non zero sk->sk_err and returns EPOLLERR. This patch fixes the issue by making sure to call sk_error_report() after tcp_done(). tcp_write_err() also lacks an smp_wmb(). We can reuse tcp_done_with_error() to factor out the details, as Neal suggested. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Eric Dumazet Acked-by: Neal Cardwell --- net/ipv4/tcp_timer.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/net/ipv4/tcp_timer.c b/net/ipv4/tcp_timer.c index 83fe7f62f7f10ab111512a3ef15a97a04c79cb4a..3e8604ae7d06c5b010a2034e3295675a7d358f13 100644 --- a/net/ipv4/tcp_timer.c +++ b/net/ipv4/tcp_timer.c @@ -74,11 +74,7 @@ u32 tcp_clamp_probe0_to_user_timeout(const struct sock *sk, u32 when) static void tcp_write_err(struct sock *sk) { - WRITE_ONCE(sk->sk_err, READ_ONCE(sk->sk_err_soft) ? : ETIMEDOUT); - sk_error_report(sk); - - tcp_write_queue_purge(sk); - tcp_done(sk); + tcp_done_with_error(sk, READ_ONCE(sk->sk_err_soft) ? : ETIMEDOUT); __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONTIMEOUT); } From patchwork Tue May 28 12:52:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 13676621 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (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 EBB0916D4F3 for ; Tue, 28 May 2024 12:53:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716900786; cv=none; b=XyjcvbaZSiyglHKPHgLX4fepzroDKT7m9B2QimiVGAHxwmTnZgZr4bu2+A8d0YY1KNyMAi6fgb+IkgM3zZS6sI8RqPOi0x8n+Tf7GTkxkKuInAPrJYzkT+VwJ7bFxD/8081GgRRCH2r6rk44bCUl5t0Z1/peugIxw14T2UnGmoY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716900786; c=relaxed/simple; bh=pYrpnj9qEJNrzPZs5QvSoB9k+98J83la6B/Kzh7YfGM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mr2GcxZsMYDgxZo2M6NnozPXNTIBlBrQz28ZHGKoBUU70SFXsyZj3hrDQxDpavJUDHUTJzBBtQCYd3IR+6wdxrc7x5SPfDIj8PfjyHbogUSNEGwo8IY6hKQHevWYRrlN2+l0Xz39n/hgwXbGmyDoHGuHBLykXH/2AvIwNHeZs48= 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=glx0VssF; arc=none smtp.client-ip=209.85.219.201 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="glx0VssF" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-df789a425d3so1262578276.2 for ; Tue, 28 May 2024 05:53:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1716900784; x=1717505584; 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=fx1W689gp3VBSmd/ZEx43mJ41Ke2ptpCRI0QsLbql2c=; b=glx0VssF/HImCdoiAetRxRkIFraNLg6vqV6jceDXboHeg7uYwPLqy0/AOGrMf12Ro2 2rSl1/sgsinj+8deEEnTOByjavyWdzeLNqiStmEACxsS7QRP1DR7spux0U4jWX0IVUVt W3DVD0Zu1XJeHGSceENQDAIlXKaNlMuxEJ5WI1ix4SLfkoAG5BbRUbyz8Tdy0K5TmaQS NqpYaG+2LLlTW33NZzOBHOLxB3jVfHPpYjBCSsT+IAGYzHkZqgh1MEsHBKNL3y91f08w iT8IsxRTL/Us8sYOIRbh97Kgs7ED85tHUmkzKQUhX2SN01tmb6FEIDXFmjJa7Iql4XyE YvpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716900784; x=1717505584; 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=fx1W689gp3VBSmd/ZEx43mJ41Ke2ptpCRI0QsLbql2c=; b=uO+vRXkFRvg+xYAmdeBP3mRcrrVpMctEJjgziyzVNiEm3vwOFjScvo+vUZ2XFAF21t DzL7oZ/Y9cXzXZeCgB9r6J+noV4f0v0zn0i07A8KH0nNIqS/WVEY3lU7h772snmWab2s IPCkM2txyxDg1r0vXhzMhL/bRHxS9xer1dYXmfXQ+GlNZKBd0peRWn7IbN+pOeZU6pFF N+1aPC9elmZAXI2gGBeNlmdmr0YKuRxJTVzAnMGLuuvvb3RwgVE1kthiQhzQyP9IMuVK Y9gm6B3wDprLO336p+7+FTZHqJ1OtEf8EbAeZs4cDuIqT8Hy9qhq2Q/Uk/akQ0xtSX8B I8BQ== X-Forwarded-Encrypted: i=1; AJvYcCXGTAvR7nxDAMiCUzI1DiBge9Et8TOxr5EPzWwmgVyvDuThw8sPh3Uet9sP88qa/NuU3uSP4hcM2sAYSAwME2x1HzJWlsPA X-Gm-Message-State: AOJu0YxPTn3XwZsCGAXEdv1E5mRZ35tgl30efPrRBdadyKQyvT/VVn07 Zps+inI2Vy8E3/K90U9mPhyro11mgZ7LLnfhQ5Y+yHc9obA0LHBqC6XeW6nJcJP7vXnPU3kIeiT gNhTpaTq90g== X-Google-Smtp-Source: AGHT+IGb4UztoKRBDvTfhBRs1hgXQHdUpsqBw1d/oWFbDxP33sd+IOeyYh5Qdwfci/90gtyk9cj1NCI76WCpSg== X-Received: from edumazet1.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:395a]) (user=edumazet job=sendgmr) by 2002:a05:6902:2b92:b0:de5:bc2f:72bb with SMTP id 3f1490d57ef6-df77223c965mr1121270276.12.1716900783997; Tue, 28 May 2024 05:53:03 -0700 (PDT) Date: Tue, 28 May 2024 12:52:52 +0000 In-Reply-To: <20240528125253.1966136-1-edumazet@google.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240528125253.1966136-1-edumazet@google.com> X-Mailer: git-send-email 2.45.1.288.g0e0cd299f1-goog Message-ID: <20240528125253.1966136-4-edumazet@google.com> Subject: [PATCH v2 net-next 3/4] tcp: fix races in tcp_abort() From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: Neal Cardwell , David Laight , netdev@vger.kernel.org, eric.dumazet@gmail.com, Eric Dumazet X-Patchwork-Delegate: kuba@kernel.org tcp_abort() has the same issue than the one fixed in the prior patch in tcp_write_err(). In order to get consistent results from tcp_poll(), we must call sk_error_report() after tcp_done(). We can use tcp_done_with_error() to centralize this logic. Fixes: c1e64e298b8c ("net: diag: Support destroying TCP sockets.") Signed-off-by: Eric Dumazet Acked-by: Neal Cardwell --- net/ipv4/tcp.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 2a8f8d8676ff1d30ea9f8cd47ccf9236940eb299..5fa68e7f6ddbfd325523365cb41de07d5b938e47 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -4576,14 +4576,10 @@ int tcp_abort(struct sock *sk, int err) bh_lock_sock(sk); if (!sock_flag(sk, SOCK_DEAD)) { - WRITE_ONCE(sk->sk_err, err); - /* This barrier is coupled with smp_rmb() in tcp_poll() */ - smp_wmb(); - sk_error_report(sk); if (tcp_need_reset(sk->sk_state)) tcp_send_active_reset(sk, GFP_ATOMIC, SK_RST_REASON_NOT_SPECIFIED); - tcp_done(sk); + tcp_done_with_error(sk, err); } bh_unlock_sock(sk); From patchwork Tue May 28 12:52:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 13676622 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (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 870C016D9DC for ; Tue, 28 May 2024 12:53:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716900787; cv=none; b=QjbYbKbwwxmb1mkIvfN0gQOFTO0rNnf0Xa1rUoQgw9b/mtDAApuZw8sjhz/e1Sf1pUD0IQEgmli6KwXta3tILDKvu3Nx0jkT8B1bZi9dLr9jhNIKy5CrEzMebrQq7qEfjI6IJRZUSgkbuQi/e9Nx0zEN9ryeRWtelfpj7ZMQ/BQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716900787; c=relaxed/simple; bh=trtAk8lizIse2l/32VYpD13iiRP2HGWcygfLxzAMAM0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=diu+pZhgRSQhVrQLOagqlrFLOj+uL7XNucDNWfnnsW5YeVuPCooZVAOLt/U8OhKk6VOvK1NjpLU6PYcUi/h9ma1o5dLky54w6q0L4LJbL7VBwH1JNFriCX5Pg8/sutqFyNIQt86FyrsNFg4KW35HG28eqUjyqnwYgsJtC92sVTk= 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=C4/g/Gw4; arc=none smtp.client-ip=209.85.128.201 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="C4/g/Gw4" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-62a0827391aso13666517b3.1 for ; Tue, 28 May 2024 05:53:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1716900785; x=1717505585; 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=CYbSWIKPeossOaeDLXogCd/TcTOGKtxRUHT/8R+xsns=; b=C4/g/Gw4kH3mMdDTcpgHP1CAxjT7A8m6A+zuQNLtODjRhzTuZlh5u/7IVgPRuFTRry 5xUNsDl2uW7hX2kR9k/3BMBRyeXUiux2FoSf81YBUKLEiiPL6tQrdBSnxMfkiVOGDJ8k gKyadlythSqFy1F9xa/Q/QPlwJ199X/uw+3T9wxM2PmwQiYVgWzTi1QxaFqrcPeAPCpa mjsWaIyv6LRz30DB/bwK1cf1QgSA9oMfqCmMMcc15m1qkS7ISbth0u7ATnqM8jgaCer+ zTSFvExFLfBSEirWvYAujFyHpqtPGNCZTqnBOrmmcYq/lc5aUHcD7PQV5R6Wvtr5xVFB XSRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716900785; x=1717505585; 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=CYbSWIKPeossOaeDLXogCd/TcTOGKtxRUHT/8R+xsns=; b=IGD+azPvculNPQVWYNX8Jrb1FhcjMtYyL2H44X22qsRiC03Lu/do4SW0NF4WqdFVrq 2EFPIGgMZ79ysBsVb5+sDniqZakbC+jIYdv2x8XkJSZtN7sgl7KyhdwkzryCywW8ccM7 3si1IGbo59W2Q4gk+UouNkQNx3kGipJmIhmwrZSeqmdFgEhNtxjEH0GojZ/3XHC0x2Gl 9rp90GnIzzuI6N5EA/9/SVbhUdO7QF9Ech6PKZosuG9R5pqca7WvFma1nrgj522QrVtW YteKgL+uf89bfSV72J6+NYPuuzyjpGf9xnR5vOK+hQMXC32aRAhwc5kt1Q3cXkKiW/Rj v+Xw== X-Forwarded-Encrypted: i=1; AJvYcCXaNiLkdW/uWx9g5o8AhTZZ4Hp8Xn9gDhP++/Opq+aQUoITY046rEIxJn+uOh+V/4/GVqBQiA0g/JFlCotLeauXzWf6oDpV X-Gm-Message-State: AOJu0Yw/NDgWEb4v8wRofeDlL/w2soWMQnyRVpCeb9HEHIvyOQ7TQqB3 4+t0C9T2RyE+m7uiY544urMFp9JAXvV1rZrIfGyf1LrPQOLoZhcG3hRPKjxxMg4ZXBBboyZpGcH 9d0A8lEUFIg== X-Google-Smtp-Source: AGHT+IEiAG5DGE+HHCNs6311h6T81J2Y85qtiyzM4Wa0h6BmUIc5i2s6iAxOsYhaX3qp7Hc089sg6ijVHV5Jgg== X-Received: from edumazet1.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:395a]) (user=edumazet job=sendgmr) by 2002:a05:690c:7402:b0:61b:1dbf:e3f with SMTP id 00721157ae682-62a08dc705bmr33818617b3.4.1716900785707; Tue, 28 May 2024 05:53:05 -0700 (PDT) Date: Tue, 28 May 2024 12:52:53 +0000 In-Reply-To: <20240528125253.1966136-1-edumazet@google.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240528125253.1966136-1-edumazet@google.com> X-Mailer: git-send-email 2.45.1.288.g0e0cd299f1-goog Message-ID: <20240528125253.1966136-5-edumazet@google.com> Subject: [PATCH v2 net-next 4/4] tcp: fix races in tcp_v[46]_err() From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: Neal Cardwell , David Laight , netdev@vger.kernel.org, eric.dumazet@gmail.com, Eric Dumazet X-Patchwork-Delegate: kuba@kernel.org These functions have races when they: 1) Write sk->sk_err 2) call sk_error_report(sk) 3) call tcp_done(sk) As described in prior patches in this series: An smp_wmb() is missing. We should call tcp_done() before sk_error_report(sk) to have consistent tcp_poll() results on SMP hosts. Use tcp_done_with_error() where we centralized the correct sequence. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Eric Dumazet Acked-by: Neal Cardwell --- net/ipv4/tcp_ipv4.c | 11 +++-------- net/ipv6/tcp_ipv6.c | 10 +++------- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c index 30ef0c8f5e92d301c31ea1a05f662c1fc4cf37af..158fad0cbe43d2029461f6cef3ee698c2acce528 100644 --- a/net/ipv4/tcp_ipv4.c +++ b/net/ipv4/tcp_ipv4.c @@ -611,15 +611,10 @@ int tcp_v4_err(struct sk_buff *skb, u32 info) ip_icmp_error(sk, skb, err, th->dest, info, (u8 *)th); - if (!sock_owned_by_user(sk)) { - WRITE_ONCE(sk->sk_err, err); - - sk_error_report(sk); - - tcp_done(sk); - } else { + if (!sock_owned_by_user(sk)) + tcp_done_with_error(sk, err); + else WRITE_ONCE(sk->sk_err_soft, err); - } goto out; } diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c index 4c3605485b68e7c333a0144df3d685b3db9ff45d..10c804b3638a99a9ae4cf3d81bd4481ac06703f9 100644 --- a/net/ipv6/tcp_ipv6.c +++ b/net/ipv6/tcp_ipv6.c @@ -490,14 +490,10 @@ static int tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, ipv6_icmp_error(sk, skb, err, th->dest, ntohl(info), (u8 *)th); - if (!sock_owned_by_user(sk)) { - WRITE_ONCE(sk->sk_err, err); - sk_error_report(sk); /* Wake people up to see the error (see connect in sock.c) */ - - tcp_done(sk); - } else { + if (!sock_owned_by_user(sk)) + tcp_done_with_error(sk, err); + else WRITE_ONCE(sk->sk_err_soft, err); - } goto out; case TCP_LISTEN: break;