From patchwork Fri May 24 19:36:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 13673572 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 AF5687604D for ; Fri, 24 May 2024 19:36:35 +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=1716579397; cv=none; b=Xhpo0sM5rkyXlWoFtaD0HRKNaeqokU8RSg4uWt26qORkwjXFCr2x5ZoJmUMoULZcgRlQlNZDEL4AM1MPOBBja0rNtMBW0jyymbm017S90OpSuTPPU1J3XwsW5lJLVORtIEw/TX1baf9FMQ6qh+/vppEjK9r+DPVFFfRli9TjH6I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716579397; c=relaxed/simple; bh=NNKtVgAJ0fOb11+13x4CJW8Z2WkJKm/n/BQsKlvjR4I=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Yi+COovuoia5we1y7mT+vOO3AbykrtyZadwHutD79EZKQx4bPC5QCOWusIzkCgaVn6Q9MW/HyyulIlsk+aMPeIJt2qOaBgUCyI1I268Tl+IQ717QugsO6IHMS2FDy0jrzfglp8JX91swketPaDLbG2r+RXA1THn7TVic4P8Guvk= 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=ttD+Hyr/; 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="ttD+Hyr/" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-df4d82f868bso1174191276.3 for ; Fri, 24 May 2024 12:36:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1716579394; x=1717184194; 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=CqLo2nlXRPQhxezjJrQjfZ90CXKZrZSy/sGGf0J8h68=; b=ttD+Hyr/ob+URP3L6kFGsNsNymh/F2ZBnB2G/oMy7hSkXZKINRbIeXR9pMTyjLpe9U FUUec9NDja9U+tDWNNjY8PEC7ZAqNH2M6ETqab2xZzntrHQbp3QSl8MbOoOXHwN8K7lK sLyMwrucnNc4+LzwK6+PsAXyTJ+43nmP8vvISZXMl/mp5FYM0UL/UXBNlea8EDn1X2ja Kqihx0JLLi1NIc8qatZM4/isQ88dqW2q8ugRroUDUl1sWSzng2PS46mT/fWgX8E5479m /vILu+cXnemgvWXddE96jUeBNjrQm235FuSLSUem0AN2HjBXTEymweLW3bMn8WeFNoc0 SeRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716579394; x=1717184194; 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=CqLo2nlXRPQhxezjJrQjfZ90CXKZrZSy/sGGf0J8h68=; b=wu/EXZ56dhnyg4+8UADTXs2y7bBZQ0idEoMLC3sCe6fPAjWS311+KcVJMGDXrTNnxy MO+n/xEt3Oi9sNz+O7zqrWzapO9NkgWuWpwjNwy4cZCLjhvHXgQmd1CLyxQhx74MH+bV hRwswX0jzdZyanJZXGc8D5I5rRmmtapfC4NOoDu8fSQm8vxPfoE/ng4yBnQjZb1Rbk40 iuGqNmxt5nFKCiOj1LqSALhnOtMmPds1Qf4QIAdMccixh6EI5YhgY07sAWiXf/m8uFkK jxQ+VYIK5SCE6+wBxA0IQqgLrx5buyfb0+951VOj4O/NpmC/w4ql/ayIG52bpi0kzFFj x4qQ== X-Forwarded-Encrypted: i=1; AJvYcCVh8uLI1DGEuaPpY/IHpcsfAM3VmNqENHWuZcLZV3uMo/eRY/DRD7ZYkDnFpAggpbvrgenEGOMHuYEntFGmkziWSrJUWQj0 X-Gm-Message-State: AOJu0YzkW2k//ntUocvFMo3k41ALkCPJrO7DWOQ+YYXyV4cDHeAjNEaS 4lJ2Fk9J4KMVomYj8z8xNhqU3xreOBt2p3zV6tceRYyNP8OOm/pTWDIWmqxYJ4WhbJO4Sq4BnDr rB781XROs3w== X-Google-Smtp-Source: AGHT+IGYWRf8iYGrHrFV7yG9i/Yhow44ChFHzN7gUysYxRgx5R3cClwwgxfmyk0Jw3CPN7oDjT49aP81kGEFGA== X-Received: from edumazet1.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:395a]) (user=edumazet job=sendgmr) by 2002:a05:6902:c01:b0:df3:19c6:d344 with SMTP id 3f1490d57ef6-df7721560d4mr876719276.1.1716579394667; Fri, 24 May 2024 12:36:34 -0700 (PDT) Date: Fri, 24 May 2024 19:36:27 +0000 In-Reply-To: <20240524193630.2007563-1-edumazet@google.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240524193630.2007563-1-edumazet@google.com> X-Mailer: git-send-email 2.45.1.288.g0e0cd299f1-goog Message-ID: <20240524193630.2007563-2-edumazet@google.com> Subject: [PATCH net 1/4] tcp: add tcp_done_with_error() helper 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 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 --- include/net/tcp.h | 1 + net/ipv4/tcp.c | 2 +- net/ipv4/tcp_input.c | 25 +++++++++++++++++-------- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/include/net/tcp.h b/include/net/tcp.h index 060e95b331a286ad7c355be11dc03250d2944920..2e7150f6755a5f5bf7b45454da0b33c5fac78183 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); 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..5af716f1bc74e095d22f64d605624decfe27cefe 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -4436,6 +4436,22 @@ 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) +{ + /* Our caller wrote a value into sk->sk_err. + * 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); +} +EXPORT_SYMBOL(tcp_done_with_error); + /* When we get a reset we do this. */ void tcp_reset(struct sock *sk, struct sk_buff *skb) { @@ -4460,14 +4476,7 @@ void tcp_reset(struct sock *sk, struct sk_buff *skb) default: WRITE_ONCE(sk->sk_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); } /* From patchwork Fri May 24 19:36:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 13673573 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 499C27D417 for ; Fri, 24 May 2024 19:36:37 +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=1716579398; cv=none; b=VSfCVcdK4xYdGl7uBegZat7O2v42No8g7C8rPtmtxBWF8SygNUxYiqBDbVx3BDHguD+LfxFN8hFXJ4yCSHHMUoSI8Tn0ryAO0E3aR7AIpQVq+t2VqaVUlwkrCTu8ILEfrmycX6zFQKG5NZSm6M2kCNYvQcTe/1/k7Ur6H7+6WMY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716579398; c=relaxed/simple; bh=+szacDh+W2jKFCKUX8UxhahCd90OddhIR0HoBtOTpxE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=gYDWVGnGvB4O5cNEJdkOiVaZwFRchtNSy8WrsHEifavQSR49KAKiWd6bLy3vG/ty1CBU2wc93jXCS5fkOnA9QOGu0sGHlaxIKsYv4FDRQGMZ+nCkqeRunk+WHoOkO3PjVPpP6/HQgpp+dZJT1Y11J+j5gH6tJ08cH4mBxQCYNG8= 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=OesLBc+E; 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="OesLBc+E" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-629f8a92145so11217167b3.0 for ; Fri, 24 May 2024 12:36:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1716579396; x=1717184196; 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=z1ap8e/6CqnJSY60s8zHiniTznOnCIezUplYpbSVn2M=; b=OesLBc+ERr6lKO6yTI5iinEoqCdilDJhezMPTmhXJqZfY2R7dno4dDM+dYheSUf8QP cmGU2WZpLELDGsHojh8kh+2BimlzViFpTPfkZCJe35mN0wJd5YiFrRANKrey+PwB//7c 24kzHC/x5ebRAdWUYMAMuEvPh/QxmpFBpjSQjhDvH8x7R/2YsN07d8WTX4qPI11jbqYd kOY9CZbyvA8cB6bm0Hj2cwNSamJ5HivpFdcPi4ukLb3xKbXVE/vOHxEpK67e5IFuou5S b5clu4rISUUooxrXMBe08dmtnsCeDOe0MU8V+Mgq6H8XJeJM8Noa87+sm9M5a/PawvGR 1bdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716579396; x=1717184196; 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=z1ap8e/6CqnJSY60s8zHiniTznOnCIezUplYpbSVn2M=; b=ZVQFg61Q113y12WHDN6lulgnlaup1h1aDsJyTp6KtKmy1V9qb0s9zx9GTHJbEvEdcD XTvpMuYDeG0wGUDjSxMR0cvHIOhlkeQNss17miH5cwd1a0ILJcJcJPvkozNLM9eM5oBY dRzLutktS57/dF7gQl5KtWyJR0sD5/eOSztQUCQ5N6Fie/AiGjsgSgt/0gWL5gQefAxW UOA1ogPlv/3GEzRLhG7BNCiXUEte6yIFcXqFDdBygF+L44iE2qTgLn3DQnbGrcZ4qi3S FANmmPfq/4fill9rbMf5IkJqDoPitbIZgrKZgMTg41igtstTj+wTB7EdjjnI53/oMCip U2bA== X-Forwarded-Encrypted: i=1; AJvYcCW1NhNdSZ2sXjUadhST+7TSGTOGR/+Olzx45DWtF/rKGJzj2pOOLxTVxyJv4jhSgsgA7qb93VX/AHcyY1JsLBJ4cYnEpMrh X-Gm-Message-State: AOJu0YxnFk+uRasd6hKO0xDq8eaSQJ0DeVwBDtlkIVo04RuAGycgTPI1 eFXUwzdAOgmXsjjIA4UA5MwHEfHqAZqK9uFyOQ6uleR/0FKmeYBLKVnLe/Eu4Xw68g3U23Y0f8E 1VXs/yimNnQ== X-Google-Smtp-Source: AGHT+IGNKQK8Wo+Q6Avn9+/FtR1eWMe/oeS7H5oYqITuxIPRwq6+DSipGmAqDu8wYe7A+QciB+kxUpOxeUnE5g== X-Received: from edumazet1.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:395a]) (user=edumazet job=sendgmr) by 2002:a05:690c:62c8:b0:61b:e689:7347 with SMTP id 00721157ae682-62a08d701cbmr7634367b3.2.1716579396336; Fri, 24 May 2024 12:36:36 -0700 (PDT) Date: Fri, 24 May 2024 19:36:28 +0000 In-Reply-To: <20240524193630.2007563-1-edumazet@google.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240524193630.2007563-1-edumazet@google.com> X-Mailer: git-send-email 2.45.1.288.g0e0cd299f1-goog Message-ID: <20240524193630.2007563-3-edumazet@google.com> Subject: [PATCH net 2/4] tcp: fix race in tcp_write_err() 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 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 --- net/ipv4/tcp_timer.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/net/ipv4/tcp_timer.c b/net/ipv4/tcp_timer.c index 83fe7f62f7f10ab111512a3ef15a97a04c79cb4a..ed614fe67a587dc4238458155dda4c66d58d2687 100644 --- a/net/ipv4/tcp_timer.c +++ b/net/ipv4/tcp_timer.c @@ -75,10 +75,9 @@ 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); + __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONTIMEOUT); } From patchwork Fri May 24 19:36:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 13673574 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 E66FB82C6B for ; Fri, 24 May 2024 19:36:38 +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=1716579400; cv=none; b=G0s3I2M5MFJaQfvfkyA3HxECJTuk/fYXRzBB1AVGb0I2XpdmpDZD+D+hh9QRTrss0AS6rtgOsXNqjqV+S3N3vrKLpMWlofStz0lEJbGOjqoKlUUb8X6a/ORyYpO/vP2IlBT0Hd32YXvw2rckrYHI/n6BVlBykvNpZdfq1KweLis= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716579400; c=relaxed/simple; bh=lZqCcbBBSv/qSgtNoIFYGw2Ni6oSIM/K3urRq5cWbbs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DEJJM13gr1VDYGD87hSn+sxkQbZfXmp+udYHsylf/6D39LQ7I+ee9usf3qMN4d7Y6lHk0cVqAMF1mHyh8VUu/eVfZICsfawHK3Iio4+yGde9kdImEoJdv5aeM3GE3hWurEZVrNduuNTjEXR/n+1T8Li+A9jrAdnZoCu7Sj90ltQ= 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=JuQB0fyq; 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="JuQB0fyq" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-62a083d9346so9397907b3.2 for ; Fri, 24 May 2024 12:36:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1716579398; x=1717184198; 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=gsG7/RSHQ8OJJlmUs4RpmmIk8Jl50XRVFFzBF5rUTZ0=; b=JuQB0fyq8IM1IG++r3bgAwaYH535GoAj7GM6NKSLenztabYwcyjovpzQs6r5SejVTm EtoINNV7o3BG1JtFa+BrSlrdIzh3+j8hYI85vFudPs5WQusBISBcbtPRZJLC4YD3gR7D dJm+wAT8KaQcCNWq7Syv2+4iAfKeyjzytmsC53YtlPyQ5Q/qZALmFXlZcBGRzzTqb1Re deQfrjlhPTGaDj27ootSwQ0o0RI2PkY0NwLWYMj2nmSpJwniPPkgm9NuStYZ9ZL4XjLq 1VgVMX7BEVK7710CFpsXVkLjE3cg82s+iiHtrMh5BDZ+3ndno7MAbF6+OerF7VXTr9J1 TmQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716579398; x=1717184198; 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=gsG7/RSHQ8OJJlmUs4RpmmIk8Jl50XRVFFzBF5rUTZ0=; b=H3nxj2vvRj3JGauRatRVsbfkjNGCC2LtIZTbGTd8vkjO2KGCbL0tziqJdlMAT2yYHO py0JthjcfsPg018c5LY3vNULUJ4J/f/wl5BeX6eqfIEqxkdCfHV5cr+hqcz03o7eWDSL tQIcgDAqPucfxdoXLhNGI3skWJlCxdoDjc/VhuhSyMGLR0DEYuR9+36J6IlFFejQvaKO PL7KNlQr3W71SfwKF4gLyFMOV9TlGJvwymQQMvUqXqPBo9gsHQkywqKAUesyZxczDPFF bbRHamH0/oW8URY1QlOlHsDNrVgUuWsZTkOrG8u/2rOXW6tO0CjYK8+zThcwuxW14o+X ybiA== X-Forwarded-Encrypted: i=1; AJvYcCWfdO2bAVourplF8j7HZEPFtq0yWoTSxcOzo2ZuOK/0+VFqCK8uGI3wSkCz+7hB62fSMuC3gZfoOTxrCQVo4+GXqHHIUlXx X-Gm-Message-State: AOJu0YyN5zj1Frglh1P8ytUUCCgmxAFgMtm6OoBiLqO48+Kv4ZjmYh9h C9DBvLVvTo0qNTPDAwtV3X0Gd0ptI4l0D9RhqvaOY0KgQQ879aLZUE98tbiq3ufHkYaqg+8N6fq 1CU3ZLl4nVg== X-Google-Smtp-Source: AGHT+IGcwDx2vMkSrJqcDooXIkomI87Eo1saJUFpDFWk8OUZhyOzEZKOOMGmW5N+rlwpBbEIOgIaXhp1kAs7SQ== 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:7884:acc7 with SMTP id 3f1490d57ef6-df77214fd6dmr262134276.1.1716579397957; Fri, 24 May 2024 12:36:37 -0700 (PDT) Date: Fri, 24 May 2024 19:36:29 +0000 In-Reply-To: <20240524193630.2007563-1-edumazet@google.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240524193630.2007563-1-edumazet@google.com> X-Mailer: git-send-email 2.45.1.288.g0e0cd299f1-goog Message-ID: <20240524193630.2007563-4-edumazet@google.com> Subject: [PATCH net 3/4] tcp: fix races in tcp_abort() 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 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 --- net/ipv4/tcp.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 2a8f8d8676ff1d30ea9f8cd47ccf9236940eb299..741cd35d0d7d66bfafe5d85b4c4d97d042613a61 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -4577,13 +4577,10 @@ int tcp_abort(struct sock *sk, int err) 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); } bh_unlock_sock(sk); From patchwork Fri May 24 19:36:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 13673575 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 AB78984DFD for ; Fri, 24 May 2024 19:36:40 +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=1716579402; cv=none; b=RngzRlkMm7rSC+6Ep705rkCKr2Wk4tmWyXJ5PgNSfCoIKb4fp1kcLd08k0XJ8jJ1/WtazOs+Mj+Fm1XLjeP4Mfzqp4uUsEP03W16jPA8I7EQIbeM0nZcFG9uN5zWS+67pVlQ+qDkn03bFxywjLwlI4kACseL6ndorqRCDx7fT0c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716579402; c=relaxed/simple; bh=nwY4a6uCgWev5GNG+aDQeObLSEQw497lbarZGqgL6KI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=O7hSLTkxXqKFY4UYcPR0hDFVaJnk95MDDrzzoG6oKIZFBIf2ew1NBTKLJe8CV8kekiuQ5K6z8Cic6umAV9pPfS9SbSpot6ir6Ua7xYjtZTkefX5SaRTtfRXsrEoBct/QRyi0RsVqOpC2YhhvHZ3l6A/18pvE3y9NJn6V0P+JtrI= 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=vsCmZlFx; 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="vsCmZlFx" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-df4d631a4c1so961663276.1 for ; Fri, 24 May 2024 12:36:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1716579400; x=1717184200; 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=Da2shUIvj+ymaVnO7Kn/JRSXGfAaQ5BBjgrAnxRBjs0=; b=vsCmZlFxwQwIvybhvmngTo5eLbxpURq8fchpXfpuANm8MePRk1kLO/lrvDpaS2TB8n rrFQlo9EvmJvgSyXWqy442XtJJjBYidQDELzkEPSI19LPkt7gpxGRYmRIVJ/MPJ279c6 r3DD2r1uWm3H1k7J1AlX3IIj7LhMXLLJRYAsrky61o+eARQEgtMOneQ88HU+gHmDdRsh ae/elYcD1g65AQnnBA9Q1vMBcg/jaD4M0dfs9/1QgvXLCZLcNVL5+0VB5taEZm4jIC4Q k6izQWDwQjBo0ycYwLM6N3UP6cnYu18d5SOYJq0cnSog13ANqAERgic8CmcgZ7TzOnWk R12A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716579400; x=1717184200; 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=Da2shUIvj+ymaVnO7Kn/JRSXGfAaQ5BBjgrAnxRBjs0=; b=uvdfHFICcc+VVvWuFRXSf515cFSWdCKYvduQFOBkovR5osJivsarMP0RuNjSFQzwcI asQk8hcoYOPTk6bVpxodc/HyIBKlItxu3FiaVlUIjQvDL3IAxq1MfIaQt6L02QQamQ5H TI/hWNnLNB6k3Gz+NwhwMVLGkBuCwOsdU9LdxXC2ZsJMwB16FcCcTpDpN+Qmp4sZRbJi RxlqNU5xQEXNwwikER03wy7YauLZ035qx1PfSEJpYBxcQkUPyZzbEVMkRMuWs4JlJ7Q4 OXuVv+KrgtABvNuupfcPSHYOCyd5tXpIxBmkAkp/frJ8PSY5UQEm2mMNy0FiRq/3Ufnn aQ1w== X-Forwarded-Encrypted: i=1; AJvYcCWYgV/gOmdB0imQI8WVlKgcnjDJQN4WiyyHTgQuR/+Xn96E/1dcKXj7xrKeQac+MOTGXPFPio22LmqFVxkvAGmZhlhVb+Ar X-Gm-Message-State: AOJu0YwntX6nIrK746GagoXhDjmlDx4fDV16pTT64RKBEBTLqaxFTPEX VuuXm6/DccWdDHGtKZpnD22N5LTPvQm/M/bi3ZM/5k692/doT1yUmYVsFrIFet/FbIsDEtYs7f9 ZaXvjMLgipw== X-Google-Smtp-Source: AGHT+IEHQaBLUQxjsmT7W3h2zRuecy2h4RS6+Mw1rKDtuee+Kczg6ynNzVB3uX8qpwJANadP5u++ZU5n+YdBPA== X-Received: from edumazet1.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:395a]) (user=edumazet job=sendgmr) by 2002:a05:6902:154a:b0:df4:628c:3045 with SMTP id 3f1490d57ef6-df7721f39eamr261134276.8.1716579399767; Fri, 24 May 2024 12:36:39 -0700 (PDT) Date: Fri, 24 May 2024 19:36:30 +0000 In-Reply-To: <20240524193630.2007563-1-edumazet@google.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240524193630.2007563-1-edumazet@google.com> X-Mailer: git-send-email 2.45.1.288.g0e0cd299f1-goog Message-ID: <20240524193630.2007563-5-edumazet@google.com> Subject: [PATCH net 4/4] tcp: fix races in tcp_v[46]_err() 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 These functions have races when they: 1) Write sk->sk_err [A] 2) call sk_error_report(sk) [B] 3) call tcp_done(sk) As described in prior patches in this series: [A] An smp_wmb() is missing. [B] 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 --- net/ipv4/tcp_ipv4.c | 5 +---- net/ipv6/tcp_ipv6.c | 4 +--- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c index 30ef0c8f5e92d301c31ea1a05f662c1fc4cf37af..09192c786d145ee97058b6bab61d96274a883aef 100644 --- a/net/ipv4/tcp_ipv4.c +++ b/net/ipv4/tcp_ipv4.c @@ -613,10 +613,7 @@ int tcp_v4_err(struct sk_buff *skb, u32 info) if (!sock_owned_by_user(sk)) { WRITE_ONCE(sk->sk_err, err); - - sk_error_report(sk); - - tcp_done(sk); + tcp_done_with_error(sk); } else { WRITE_ONCE(sk->sk_err_soft, err); } diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c index 4c3605485b68e7c333a0144df3d685b3db9ff45d..41ddc7c768ce957ff64aac6de4a2a59227e7c7ff 100644 --- a/net/ipv6/tcp_ipv6.c +++ b/net/ipv6/tcp_ipv6.c @@ -492,9 +492,7 @@ static int tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, 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); + tcp_done_with_error(sk); } else { WRITE_ONCE(sk->sk_err_soft, err); }