Received: by 2002:ab2:7903:0:b0:1fb:b500:807b with SMTP id a3csp1466667lqj; Tue, 4 Jun 2024 02:24:59 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVJlySmJ3hnP+IjX/8TNGf+o1VfFYmRfLxdwm1aTw0/WMzRjBCvA+IgAx4APYInCIsZGnimfqqcsUrWu5WBg+OBNb5rU7XutQntiOBWfA== X-Google-Smtp-Source: AGHT+IHJQWnur8RG5bBuY5xQATIiEPIwo0u/fQtd8Ha5OGv8ZpnsAso1m/NQUP3jr7zAA+p1VdmM X-Received: by 2002:a05:6a20:734a:b0:1b2:101:7acd with SMTP id adf61e73a8af0-1b26f245b22mr13780147637.39.1717493099136; Tue, 04 Jun 2024 02:24:59 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717493099; cv=pass; d=google.com; s=arc-20160816; b=W05CqSOAW3hIi9DoNxY182dzWrvXFsWPaBHo233Zn1mZQkGbCeE2+ZiSvGsR6o2FX/ i3/lRF8yoYC9CaI/U9Z++6A0IHLW+i9WWPzIoKEA3Z8AplHr0htVULQyJ1nZIXVsBR6w qkzeu8m/LYO5v6LXeuNH84WfxFFukzKujlhRXYCfDZfa7Q3waaw1dr0SM6m6tNxEantv uAfUzVfaUnJwys4qsjX+x5JHud7IB5dAvJ3k/r869PtearizvUSAj/tCdXQrX6kuVCNe ULQMENIUYknofpfJn4/gNhn3XqHJMbcSUy+fUbNNKnE3VwmAHj2pb1nqpvPZ0zWbNbh0 D+GA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=51xd4qBif5qLulTsk7/5OKYHjeBwqmO8tvfG1w6sQz8=; fh=YcOavk5ZUFGB7quz7EISF6sBOq7+4hcTMzGEibzn64I=; b=QE18C3B/Xldl19obaCWdOTxreXx1l/XpGeA05V1QjCQDTrhYcZC0WsE2KUDLqjGqCG EVBDmsd4J/N08SE2X7hB7qWpu1xb1Dm6LiNdcU4yw8mxFzHHMwqXYYM4dLbPwByfrEtP ZOXffgUvL9uvsor9oYD7DadU9JaFI09kTIP0h97gkowU6AcVUkv7SaDfVNrxfTWQnuha TuMlbkNnlZSCYUnBaUkFZCYwwsE8sMhWBL2QkLxoDR+Ucs59rVY5/YuOcvQ0bKHTJSG7 hWCFa7JF/BSjr2XZ/ceHcglYTmxKALUqZazIVSuaM534E5Vu31nrzMqYTP598YE1ufDX CHmA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ionos.com header.s=google header.b=MVp24Rm2; arc=pass (i=1 spf=pass spfdomain=ionos.com dkim=pass dkdomain=ionos.com dmarc=pass fromdomain=ionos.com); spf=pass (google.com: domain of linux-kernel+bounces-200345-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-200345-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=ionos.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id d9443c01a7336-1f632418db0si3335635ad.603.2024.06.04.02.24.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Jun 2024 02:24:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-200345-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@ionos.com header.s=google header.b=MVp24Rm2; arc=pass (i=1 spf=pass spfdomain=ionos.com dkim=pass dkdomain=ionos.com dmarc=pass fromdomain=ionos.com); spf=pass (google.com: domain of linux-kernel+bounces-200345-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-200345-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=ionos.com 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id B6DA0288B26 for ; Tue, 4 Jun 2024 09:24:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BF162143C40; Tue, 4 Jun 2024 09:24:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ionos.com header.i=@ionos.com header.b="MVp24Rm2" Received: from mail-ed1-f68.google.com (mail-ed1-f68.google.com [209.85.208.68]) (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 8E7D414374B for ; Tue, 4 Jun 2024 09:24:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.68 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717493094; cv=none; b=l8rab7rVxKEWwuYQzHY/wkahXWyypAhBlSWkVGYQ8VsuwEzfmufnbXNtrultSJF/e8mVCJ3inRdXMAWz5JRnVwM9CS+MOkGVsF172QRyHQg5ELesaTG3FFcAp55zspo+2pYvzD38OVgDz/sDpphYsBcIlvd4nRfcRx5AkrMyy88= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717493094; c=relaxed/simple; bh=3M/8TKMtLd1tf0/P4+WGMCcoSzO6k4NIm/iHpw11Lhw=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=FpfRk8xOVuQ5BpLNwb9eHBpYP2+UMWJu5cGPZxfo/qHvovAVIkFyI+f6UC1cF2FwqDDue5ADAuLix3z/Uv52rlwrS0cwLQZHyjfcscyEtn/cZfEgY/FaHBYc2Qz0HxC6Qn/Wtyh1wCPPYtgXTRfetMEUUSJZwJCJr5hmXlj3GXs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=ionos.com; spf=pass smtp.mailfrom=ionos.com; dkim=pass (2048-bit key) header.d=ionos.com header.i=@ionos.com header.b=MVp24Rm2; arc=none smtp.client-ip=209.85.208.68 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=ionos.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ionos.com Received: by mail-ed1-f68.google.com with SMTP id 4fb4d7f45d1cf-57a20ccafc6so4717722a12.2 for ; Tue, 04 Jun 2024 02:24:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ionos.com; s=google; t=1717493091; x=1718097891; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=51xd4qBif5qLulTsk7/5OKYHjeBwqmO8tvfG1w6sQz8=; b=MVp24Rm24/RMCNNCfkrS+XU2VDrxb3bkeNPfdySDU7uoVNm0e9JtcKdlUl+hybQLnA fOklqlkGU5JMB2r1BTlLJ0r37y39EuyvLvYGDllGbWrrfI1dh6skbacaMea4mHJ1jnRg WQydq7aQUmxiBJ4n669Fq/NdilXL+5hq/y6i9keD5hzWQQgyqE+RLuOy+xzZOfXjKM7C gXkvgPK2MjwU/hlH4IKxt7oa4vAmbV0TZJUhAuOUN765P0C78mDZep1FML93qUl6NyWu PqGib1f1hOvRUZdUBhkTVcymBI8t/oCI1R/iK05W9gzsAB7EeTiluKXhvpeHSiKQ3PDD QpXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717493091; x=1718097891; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=51xd4qBif5qLulTsk7/5OKYHjeBwqmO8tvfG1w6sQz8=; b=OGr4cYjbmYTI5LqEBgKRcfmQGGtwail2Du6bxQp+PwwOK5kEfQ075r8mW7G7gxmAVx GujUbR/y+607h/riWW5mjw9aQ8twJDqOOGrx6Uz/JxnNgcLmSzdgPSq0H01l3ISm3Goy mLiXb3xbhv2lMBhXVDveWE2A9p7tRer8/17Fflv2alesndIspe54zfXRxiMIsvN83kD3 fnLFdhkzehsQ3peGQ0MTNaD9UghzaninZBJJgTb5gtq+Xxx0OxMA1xWRxO6B4Z9zq9sD vIzuIUzGkYZ2GOAuXGEaLpjGZErcDAS2g24DHvXhB4YvnFSZG2FoYiOomldXxDAhoNeT 0yVQ== X-Forwarded-Encrypted: i=1; AJvYcCX8zJHCyycNCQPPvDcXK4Xj28LR8dLAOnglkT6apDKYu0fpocL4MEpxBLdTzxxj6xgYlPcIxOV1Ojkw69Ud98bsG7ccrXr1EiFq0txU X-Gm-Message-State: AOJu0YwmLzDiC8g7RdYik00uY2ixF1KehZlcFjt/Q0bnB09+GivJLLZu m+TRB+F1Qr/T9IVDFB8tlsSV0OzlVKYSCS7PvIXlSeWC+iydik8l81bGLGvqwPQ= X-Received: by 2002:a50:d503:0:b0:57a:2f68:fe7b with SMTP id 4fb4d7f45d1cf-57a36419606mr7947320a12.31.1717493090867; Tue, 04 Jun 2024 02:24:50 -0700 (PDT) Received: from raven.blarg.de (p200300dc6f4f9200023064fffe740809.dip0.t-ipconnect.de. [2003:dc:6f4f:9200:230:64ff:fe74:809]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-57a7e6a8e14sm1226873a12.87.2024.06.04.02.24.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Jun 2024 02:24:50 -0700 (PDT) From: Max Kellermann To: axboe@kernel.dk, brauner@kernel.org, viro@zeniv.linux.org.uk, hch@infradead.org, jack@suse.cz, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Max Kellermann Subject: [PATCH v3] fs/splice: don't block splice_direct_to_actor() after data was read Date: Tue, 4 Jun 2024 11:24:31 +0200 Message-Id: <20240604092431.2183929-1-max.kellermann@ionos.com> X-Mailer: git-send-email 2.39.2 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 v2 -> v3: repost and rebase on linus/master 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 60aed8de21f8..6257fef93ec4 100644 --- a/fs/splice.c +++ b/fs/splice.c @@ -362,6 +362,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 = in->f_op->read_iter(&kiocb, &to); if (ret > 0) { @@ -1090,6 +1092,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 9dec4861d09f..0deb87fb8055 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