Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp1777857rdh; Tue, 26 Sep 2023 03:22:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFZCV3amx2Ld8RiOD4lbQ+0qo+/c6uN13IyYa5Tx/LQyRhDDF5HT5lHqEEgBaQn2P+AwbZX X-Received: by 2002:a05:6358:4429:b0:135:4003:784c with SMTP id z41-20020a056358442900b001354003784cmr12913878rwc.17.1695723744485; Tue, 26 Sep 2023 03:22:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695723744; cv=none; d=google.com; s=arc-20160816; b=KHXkZZZSTxyh8d+7hv+OATfd9oudHwBjerej/WV1P008Rv5pOa3U8fI9COZG8YY2FU XJIzkAeDZVdE68xyNuQDUOay9HXeyAe3BKzrFxqRYqfvwCIwn1LIzHfb2QR+13o4Lm/M Ivbfm6PKcQQxoRjDwYN24HuDBX7mlpBCgOyZNfczKUyBqzdX1gHBOJVuhvRFaoOZH+st FeK/eQXGcRNUXTdHeQ8bTdAkEYnlZLqrLIcJWV1aj1l+elgiEzn57ykOB9x+YISAQ6mj Gfuxpdo/gnQ4cfekF7qnQAoDLGqfME2sNzaX61s9hkdixwWEoxs6EKA0r0gBymFA3Lny 8rHw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=KreDf2vqHk/Hd5rmmn0d7G8cM8MDnpKHn6UOoJPSFwk=; fh=+NjysdgV1/D++Es3kqBlFDYea3mcts0pgXbeOWu5x7s=; b=vHLfM+of20udI5KEpyYVUbbNoQ82n1j8/wI2tTBrEikKcOYjlz20QEGPrkD50FJMQ6 6uSQThIJdJ8h7Na3Fvbz/fAx26pa9XOsMJBaAdcqpiuxG3Id2E6sTnDsVbMcNadBXvPu hwLlQPeGvcwtntU2vqucYxF6CpYkDluewkzaWW9HlQwgMD64afSverjo4QXjXTq9vCBA eOHzIltY6kx/tCMlCO/4k2W068BHuuqEFf9yYT4e5Az7DXTbzeRxJgvwA6PX38zKNtFJ ma9u/maSmyWt2XkL/AoAwKS89579fp0KXdck9Wqdyc3lGcgm4MSM2T1UHvw0plgwmDbl 1rdw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ionos.com header.s=google header.b=dtEv8mgH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=ionos.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id 19-20020a630c53000000b0055c95e91f67si1375104pgm.155.2023.09.26.03.22.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 03:22:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@ionos.com header.s=google header.b=dtEv8mgH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=ionos.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id BD3C380BD3BE; Mon, 25 Sep 2023 23:37:22 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233749AbjIZGg6 (ORCPT + 99 others); Tue, 26 Sep 2023 02:36:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53954 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233655AbjIZGg5 (ORCPT ); Tue, 26 Sep 2023 02:36:57 -0400 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 08CAAAF for ; Mon, 25 Sep 2023 23:36:50 -0700 (PDT) Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-40572aeb6d0so45048295e9.1 for ; Mon, 25 Sep 2023 23:36:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ionos.com; s=google; t=1695710208; x=1696315008; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=KreDf2vqHk/Hd5rmmn0d7G8cM8MDnpKHn6UOoJPSFwk=; b=dtEv8mgHJPdjmzfVbT04HtND1Su4mHTU2nsycQ295kRVY3zBadjWmZ39p1d+zR4hMl TzoXc7fbzz97+N/ITZA2w9xL0NZal6iU+f+IXq+oEIYaFBqcVV1vGusXkDe0o0Pmk8uI bIlikl1bBkd5JCEi4DFBYKWOq+a4AhW3X0VDZinvcg+fSikVscxX6k6B4s5y5cl/u2w9 9LbOEe2LXtz6RHFvi/oD/SN11Fwv/ztRQDY3JiH137O4nZRcdnFoIeV3X2bASjEsdWz3 mUHxDcNq0dH/1a/Eq43Vhbk1MFHEqw1WueH3tv7rxK5DHt4IXrsrUDgLLUkJYNTaer8+ Cp1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695710208; x=1696315008; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KreDf2vqHk/Hd5rmmn0d7G8cM8MDnpKHn6UOoJPSFwk=; b=bbvu8p2mCGAFilt4SmEh2GDJOAMe0S3MiNgJ5m3Z1lg4fzrqO0Cw7UxQWKKn9PCDQO 8c/QJXyqbXO+pgSxClBaDcqXRhJPKDxCP5yBSXp6PFOqiZESQfC6eIGlgKYEi1PjASSZ qfmYpNgZnMoE2sqp2yppSGrBye+1L/6bUhvGP0UFh7T8CsgiqoaG3hp0v9VjWd0UhSbS MSB2tKjELy0AGSrcnnGTOJGLvxxRTlPWaCL9RiPPdTmJhlBSxjsX/lUrnJkK2bYdYbGw MW0BaIEjc4wx4YIMx8PSPYqTUtJiLLLhPMe4IGMQkOehbTAu7/0khRqWye0xjZlZdsZ/ UF4Q== X-Gm-Message-State: AOJu0YwcXfHWkUzJV9LAxHEY6mqVx7W2xRfNTNDwVqWSfCUU8cnC6an+ /YfvjjQ3EYFsVFVDQBcSZSJU5CdZ+soRSg6bQuU= X-Received: by 2002:a05:600c:22d8:b0:3fb:b3aa:1c8a with SMTP id 24-20020a05600c22d800b003fbb3aa1c8amr7142927wmg.16.1695710208403; Mon, 25 Sep 2023 23:36:48 -0700 (PDT) Received: from heron.intern.cm-ag (p200300dc6f49a600529a4cfffe3dd983.dip0.t-ipconnect.de. [2003:dc:6f49:a600:529a:4cff:fe3d:d983]) by smtp.gmail.com with ESMTPSA id s16-20020a7bc390000000b00401c595fcc7sm11270639wmj.11.2023.09.25.23.36.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 23:36:48 -0700 (PDT) From: Max Kellermann To: Alexander Viro , Christian Brauner Cc: Max Kellermann , Jens Axboe , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] fs/splice: don't block splice_direct_to_actor() after data was read Date: Tue, 26 Sep 2023 08:36:09 +0200 Message-Id: <20230926063609.2451260-1-max.kellermann@ionos.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230925-erstklassig-flausen-48e1bc11be30@brauner> References: <20230925-erstklassig-flausen-48e1bc11be30@brauner> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Mon, 25 Sep 2023 23:37:22 -0700 (PDT) If userspace calls sendfile() with a very large "count" parameter, the kernel can block for a very long time until 2 GiB (0x7ffff000 bytes) have been read from the hard disk and pushed into the socket buffer. Usually, that is not a problem, because the socket write buffer gets filled quickly, and if the socket is non-blocking, the last direct_splice_actor() call will return -EAGAIN, causing splice_direct_to_actor() to break from the loop, and sendfile() will return a partial transfer. However, if the network happens to be faster than the hard disk, and the socket buffer keeps getting drained between two generic_file_read_iter() calls, the sendfile() system call can keep running for a long time, blocking for disk I/O over and over. That is undesirable, because it can block the calling process for too long. I discovered a problem where nginx would block for so long that it would drop the HTTP connection because the kernel had just transferred 2 GiB in one call, and the HTTP socket was not writable (EPOLLOUT) for more than 60 seconds, resulting in a timeout: sendfile(4, 12, [5518919528] => [5884939344], 1813448856) = 366019816 <3.033067> sendfile(4, 12, [5884939344], 1447429040) = -1 EAGAIN (Resource temporarily unavailable) <0.000037> epoll_wait(9, [{EPOLLOUT, {u32=2181955104, u64=140572166585888}}], 512, 60000) = 1 <0.003355> gettimeofday({tv_sec=1667508799, tv_usec=201201}, NULL) = 0 <0.000024> sendfile(4, 12, [5884939344] => [8032418896], 2147480496) = 2147479552 <10.727970> writev(4, [], 0) = 0 <0.000439> epoll_wait(9, [], 512, 60000) = 0 <60.060430> gettimeofday({tv_sec=1667508869, tv_usec=991046}, NULL) = 0 <0.000078> write(5, "10.40.5.23 - - [03/Nov/2022:21:5"..., 124) = 124 <0.001097> close(12) = 0 <0.000063> close(4) = 0 <0.000091> In newer nginx versions (since 1.21.4), this problem was worked around by defaulting "sendfile_max_chunk" to 2 MiB: https://github.com/nginx/nginx/commit/5636e7f7b4 Instead of asking userspace to provide an artificial upper limit, I'd like the kernel to block for disk I/O at most once, and then pass back control to userspace. There is prior art for this kind of behavior in filemap_read(): /* * If we've already successfully copied some data, then we * can no longer safely return -EIOCBQUEUED. Hence mark * an async read NOWAIT at that point. */ if ((iocb->ki_flags & IOCB_WAITQ) && already_read) iocb->ki_flags |= IOCB_NOWAIT; This modifies the caller-provided "struct kiocb", which has an effect on repeated filemap_read() calls. This effect however vanishes because the "struct kiocb" is not persistent; splice_direct_to_actor() doesn't have one, and each generic_file_splice_read() call initializes a new one, losing the "IOCB_NOWAIT" flag that was injected by filemap_read(). There was no way to make generic_file_splice_read() aware that IOCB_NOWAIT was desired because some data had already been transferred in a previous call: - checking whether the input file has O_NONBLOCK doesn't work because this should be fixed even if the input file is not non-blocking - the SPLICE_F_NONBLOCK flag is not appropriate because it affects only whether pipe operations are non-blocking, not whether file/socket operations are non-blocking Since there are no other parameters, I suggest adding the SPLICE_F_NOWAIT flag, which is similar to SPLICE_F_NONBLOCK, but affects the "non-pipe" file descriptor passed to sendfile() or splice(). It translates to IOCB_NOWAIT for regular files, just like RWF_NOWAIT does. Changes v1 -> v2: - value of SPLICE_F_NOWAIT changed to 0x10 - added SPLICE_F_NOWAIT to SPLICE_F_ALL to make it part of uapi Cc: Jens Axboe Signed-off-by: Max Kellermann --- fs/splice.c | 14 ++++++++++++++ include/linux/splice.h | 4 +++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/fs/splice.c b/fs/splice.c index d983d375ff11..c192321d5e37 100644 --- a/fs/splice.c +++ b/fs/splice.c @@ -361,6 +361,8 @@ ssize_t copy_splice_read(struct file *in, loff_t *ppos, iov_iter_bvec(&to, ITER_DEST, bv, npages, len); init_sync_kiocb(&kiocb, in); kiocb.ki_pos = *ppos; + if (flags & SPLICE_F_NOWAIT) + kiocb.ki_flags |= IOCB_NOWAIT; ret = call_read_iter(in, &kiocb, &to); if (ret > 0) { @@ -1070,6 +1072,18 @@ ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd, if (unlikely(ret <= 0)) goto read_failure; + /* + * After at least one byte was read from the input + * file, don't wait for blocking I/O in the following + * loop iterations; instead of blocking for arbitrary + * amounts of time in the kernel, let userspace decide + * how to proceed. This avoids excessive latency if + * the output is being consumed faster than the input + * file can fill it (e.g. sendfile() from a slow hard + * disk to a fast network). + */ + flags |= SPLICE_F_NOWAIT; + read_len = ret; sd->total_len = read_len; diff --git a/include/linux/splice.h b/include/linux/splice.h index 6c461573434d..06ce58b1f408 100644 --- a/include/linux/splice.h +++ b/include/linux/splice.h @@ -21,7 +21,9 @@ #define SPLICE_F_MORE (0x04) /* expect more data */ #define SPLICE_F_GIFT (0x08) /* pages passed in are a gift */ -#define SPLICE_F_ALL (SPLICE_F_MOVE|SPLICE_F_NONBLOCK|SPLICE_F_MORE|SPLICE_F_GIFT) +#define SPLICE_F_NOWAIT (0x10) /* do not wait for data which is not immediately available */ + +#define SPLICE_F_ALL (SPLICE_F_MOVE|SPLICE_F_NONBLOCK|SPLICE_F_MORE|SPLICE_F_GIFT|SPLICE_F_NOWAIT) /* * Passed to the actors -- 2.39.2