From patchwork Thu Dec 21 03:09:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ahelenia_Ziemia=C5=84ska?= X-Patchwork-Id: 181961 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2483:b0:fb:cd0c:d3e with SMTP id q3csp156633dyi; Wed, 20 Dec 2023 19:11:44 -0800 (PST) X-Google-Smtp-Source: AGHT+IGBNROBXGWNwewP2sd/Htpaapq8/LiYzbKx5b6v/0pOaB0sjZwoglRlLrNZmm9IgTy0bwou X-Received: by 2002:a05:620a:1aa6:b0:781:1935:ac99 with SMTP id bl38-20020a05620a1aa600b007811935ac99mr1627544qkb.145.1703128303789; Wed, 20 Dec 2023 19:11:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703128303; cv=none; d=google.com; s=arc-20160816; b=omcUM4oX6XZP0h0eLq+uZnyPmWhzImiEzLk45MuLP5Br12l9wh8mu7xOPI0rjxNEfS fD8JfKT3UqQWKMBwvLsMY+4JRwdkLgUCo+P/Z7pwV4/dlZkpk7ej5dY5FH/e9adw4Pmi sA5Di+gUAnGj0rzwe8/2spY7m4nIWxpt2Bf9INU0Hz2oW8My1byoT3OgR5kLi+vaDZ6/ NAPnqGkAQGdRbeLpir0b0JP9vkOViq+rAgdsDyTkyza8fR4sVEQQ7JPbUOVnu4Mog3N/ /verGuGrzKNaebblpbZJWwq8BsMFAuox/qKEH29phQV0MxGLk+0pUN5U2TX0zFvJupkl 42HQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=user-agent:in-reply-to:content-disposition:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:references :message-id:subject:cc:from:date:dkim-signature; bh=vydXVEslYVpAN/0zrw5DqIn2EKp+ucLsLzH93kVEFvA=; fh=Fcup1ZaRn/JP8fZULIBkXdJo1W7flDPKDQKOsAjlEFk=; b=MKodNzAxwyJAl3SxdMnQ2zT9Z4jW1ALH5fPHM7l1egyj3YZ9jjLqpZBh3hmykpEbr9 Cf8IRD1xn1CT3ujFmLn9/Ym47c+XcxufrMQ3kx+5zTdn+sSWqLbap5q+4IA6p/rCVi1S ++oCWdoU+gSZOVmvOkFAqJLbbaJM63Rr3mS/YdRyn0nxVGBW1fz+qIgAJWFF/WdwM9BA T5Z8kEQ8yDmiPTtT4mpsto+2HdtA0vuaYZtcJ+cst1p5uQusJ23OwFOPZ0RKeffF7zOe oRoBCoYVL2X/Or94BGok+0KOVc0zEUTSLdQWzNXuvyshn4I7y8N3yduI7Ov9hkKwfLFd HXrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nabijaczleweli.xyz header.s=202305 header.b=pHDgVoMi; spf=pass (google.com: domain of linux-kernel+bounces-7781-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-7781-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=nabijaczleweli.xyz Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id d7-20020a05620a158700b0077d72f44c61si1148607qkk.754.2023.12.20.19.11.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Dec 2023 19:11:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-7781-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@nabijaczleweli.xyz header.s=202305 header.b=pHDgVoMi; spf=pass (google.com: domain of linux-kernel+bounces-7781-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-7781-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=nabijaczleweli.xyz Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 94E011C21F9A for ; Thu, 21 Dec 2023 03:11:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8A49E182A8; Thu, 21 Dec 2023 03:09:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=nabijaczleweli.xyz header.i=@nabijaczleweli.xyz header.b="pHDgVoMi" X-Original-To: linux-kernel@vger.kernel.org Received: from tarta.nabijaczleweli.xyz (tarta.nabijaczleweli.xyz [139.28.40.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 893EB15AE7; Thu, 21 Dec 2023 03:09:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nabijaczleweli.xyz Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=nabijaczleweli.xyz DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=nabijaczleweli.xyz; s=202305; t=1703128148; bh=fnKQoYZiI85fSTbAEaSEfBVSSLGdtMQnakutCuLUGCM=; h=Date:From:Cc:Subject:References:In-Reply-To:From; b=pHDgVoMizVeq6S9MWjn6uFDxllWq6WcSq2K7TB4c041J+twDbF2NTE2zhwr+5oIWW xUVV0a6x46nlrs3++bVB4Js/bq9at4BnjVdo47lP9XATAs1HW6dGqc6harEIdxtpmd jOhuYtMKj4wfmnBPgcRGljdUZb4XZuwCNYqr5jaC+WtzeauWCGDZg5wyqt4a0Q+7zD aGX3tGEKc9P6eFLqcyDq7T1iigA0PaarDSCMdYsn2C4CjU/6rAcHqh5juofkXC1su6 sFxQLeyNa77hwQHmvHZnzMHkOVZIJBgpYyAS4upsU1UAO1pi5r/lvKYw6bl0evc+r1 8GzCAWdNZe/5g== Received: from tarta.nabijaczleweli.xyz (unknown [192.168.1.250]) by tarta.nabijaczleweli.xyz (Postfix) with ESMTPSA id 9AA7313D42; Thu, 21 Dec 2023 04:09:08 +0100 (CET) Date: Thu, 21 Dec 2023 04:09:08 +0100 From: Ahelenia =?utf-8?q?Ziemia=C5=84ska?= Cc: Jens Axboe , Christian Brauner , Alexander Viro , linux-fsdevel@vger.kernel.org, Eric Dumazet , "David S. Miller" , David Ahern , Jakub Kicinski , Paolo Abeni , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 07/11] net/tcp: tcp_splice_read: always do non-blocking reads Message-ID: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20231103-116-3b855e-dirty X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785859464486895516 X-GMAIL-MSGID: 1785859464486895516 Otherwise we risk sleeping with the pipe locked for indeterminate lengths of time ‒ given: cat > tcp.c <<^D #define _GNU_SOURCE #include #include #include #include #include int main() { int s = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in addr = { .sin_family = AF_INET, .sin_addr = { htonl(INADDR_LOOPBACK) }, .sin_port = htons(getpid() % (0xFFFF - 1000) + 1000) }; bind(s, &addr, sizeof(addr)); listen(s, 1); if (!fork()) { connect(socket(AF_INET, SOCK_STREAM, 0), &addr, sizeof(addr)); sleep(100); return 0; } s = accept(s, NULL, NULL); for (;;) splice(s, 0, 1, 0, 128 * 1024 * 1024, 0); } ^D cc tcp.c -o tcp mkfifo fifo ./tcp > fifo & read -r _ < fifo & sleep 0.1 echo zupa > fifo tcp used to sleep in splice and the shell used to enter an uninterruptible sleep in open("fifo"); now the splice returns -EAGAIN and the whole program completes. sock_rcvtimeo() returns 0 if the second argument is true, so the explicit re-try loop for empty read conditions can be removed entirely. Signed-off-by: Ahelenia Ziemiańska --- net/ipv4/tcp.c | 32 +++----------------------------- 1 file changed, 3 insertions(+), 29 deletions(-) diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index ff6838ca2e58..17a0e2a766b7 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -782,7 +782,6 @@ ssize_t tcp_splice_read(struct socket *sock, loff_t *ppos, .len = len, .flags = flags, }; - long timeo; ssize_t spliced; int ret; @@ -797,7 +796,6 @@ ssize_t tcp_splice_read(struct socket *sock, loff_t *ppos, lock_sock(sk); - timeo = sock_rcvtimeo(sk, sock->file->f_flags & O_NONBLOCK); while (tss.len) { ret = __tcp_splice_read(sk, &tss); if (ret < 0) @@ -821,37 +819,13 @@ ssize_t tcp_splice_read(struct socket *sock, loff_t *ppos, ret = -ENOTCONN; break; } - if (!timeo) { - ret = -EAGAIN; - break; - } - /* if __tcp_splice_read() got nothing while we have - * an skb in receive queue, we do not want to loop. - * This might happen with URG data. - */ - if (!skb_queue_empty(&sk->sk_receive_queue)) - break; - ret = sk_wait_data(sk, &timeo, NULL); - if (ret < 0) - break; - if (signal_pending(current)) { - ret = sock_intr_errno(timeo); - break; - } - continue; + ret = -EAGAIN; + break; } tss.len -= ret; spliced += ret; - if (!tss.len || !timeo) - break; - release_sock(sk); - lock_sock(sk); - - if (sk->sk_err || sk->sk_state == TCP_CLOSE || - (sk->sk_shutdown & RCV_SHUTDOWN) || - signal_pending(current)) - break; + break; } release_sock(sk);