mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/2] splice: handle zero-count sendfile to full pipes
@ 2026-11-28 13:42 Mehtab Zafar
  2026-11-28 13:42 ` [PATCH 1/2] splice: return immediately for zero-length file-to-pipe splices Mehtab Zafar
  2026-11-28 13:42 ` [PATCH 2/2] selftests/splice: cover zero-count sendfile to a full pipe Mehtab Zafar
  0 siblings, 2 replies; 3+ messages in thread
From: Mehtab Zafar @ 2026-11-28 13:42 UTC (permalink / raw)
  To: Al Viro, Christian Brauner, linux-fsdevel
  Cc: Jan Kara, Shuah Khan, linux-kselftest, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1876 bytes --]

sendfile() sends to a pipe through splice_file_to_pipe() directly. For a
zero-count operation, that helper currently waits for pipe space before the
lower-level read sees the zero length. On a full pipe this makes blocking
sendfile() wait and nonblocking sendfile() return -EAGAIN, while splice()
returns zero immediately.

Patch 1 returns before waiting when the helper receives a zero length.
Patch 2 adds kselftest coverage for full blocking and nonblocking pipes.

This bug was found with AI assistance, which also helped
prepare the fix, tests, and description.

Validation on x86_64, based on vfs/vfs.all at 3857587d9d6f:
- baseline, three fresh no-disk/no-NIC KVM boots: nonblocking returned
  -EAGAIN, blocking waited until interrupted, and the selftest passed 0/2
- patched, three fresh no-disk/no-NIC KVM boots: both calls returned zero
  immediately and the selftest passed 2/2
- positive-count full-pipe behavior remained -EAGAIN, invalid output fd
  remained -EBADF, an
d the input offset and pipe contents did not advance
- W=1 fs/splice.o and full bzImage builds passed
- strict checkpatch was clean for the fix; for the selftest it reported
  no errors, warnings, or checks with FILE_PATH_CHANGES ignored. The final
  selftest run also logged an spdxcheck.py traceback due to missing Python
  ply; unchanged source and SPDX text had passed earlier SPDX validation

Mehtab Zafar (2):
  splice: return immediately for zero-length file-to-pipe splices
  selftests/splice: cover zero-count sendfile to a full pipe

 fs/splice.c                                   |  3 +
 tools/testing/selftests/splice/.gitignore     |  1 +
 tools/testing/selftests/splice/Makefile       |  1 +
 .../testing/selftests/splice/sendfile_zero.c  | 93 +++++++++++++++++++
 4 files changed, 98 insertions(+)
 create mode 100644 tools/testing/selftests/splice/sendfile_zero.c

-- 
2.34.1


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 563 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/2] splice: return immediately for zero-length file-to-pipe splices
  2026-11-28 13:42 [PATCH 0/2] splice: handle zero-count sendfile to full pipes Mehtab Zafar
@ 2026-11-28 13:42 ` Mehtab Zafar
  2026-11-28 13:42 ` [PATCH 2/2] selftests/splice: cover zero-count sendfile to a full pipe Mehtab Zafar
  1 sibling, 0 replies; 3+ messages in thread
From: Mehtab Zafar @ 2026-11-28 13:42 UTC (permalink / raw)
  To: Al Viro, Christian Brauner, linux-fsdevel
  Cc: Jan Kara, Shuah Khan, linux-kselftest, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1429 bytes --]

sendfile() to a pipe calls splice_file_to_pipe() directly. Unlike
splice(), this path reaches wait_for_space() before the lower-level read
handles a zero length. A zero-count sendfile() therefore blocks on a
full blocking pipe and returns -EAGAIN on a full nonblocking pipe.

There is no data to enqueue for a zero-length transfer. Return 0 before
locking or waiting for pipe space, matching splice() behavior.

This bug was found with AI assistance, which also helped
prepare the fix, tests, and description.

Fixes: b964bf53e540 ("teach sendfile(2) to handle send-to-pipe directly")
Cc: stable@vger.kernel.org
Signed-off-by: Mehtab Zafar <hello@mzfr.me>
---
Tested on x86_64 against vfs/vfs.all at 3857587d9d6f in three fresh KVM
boots per kernel. Baseline: zero count returned -EAGAIN on a full
nonblocking pipe and waited until interrupted on a full blocking pipe.
Patched: both returned 0 immediately. Positive-count and invalid-fd
controls were unchanged. W=1 fs/splice.o and
 full bzImage builds passed;
strict checkpatch and git diff --check were clean.

 fs/splice.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/fs/splice.c b/fs/splice.c
index 5397ded643a7..afad10adf138 100644
--- a/fs/splice.c
+++ b/fs/splice.c
@@ -1239,6 +1239,9 @@ ssize_t splice_file_to_pipe(struct file *in,
 {
 	ssize_t ret;
 
+	if (!len)
+		return 0;
+
 	pipe_lock(opipe);
 	ret = wait_for_space(opipe, flags);
 	if (!ret)
-- 
2.34.1


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 563 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 2/2] selftests/splice: cover zero-count sendfile to a full pipe
  2026-11-28 13:42 [PATCH 0/2] splice: handle zero-count sendfile to full pipes Mehtab Zafar
  2026-11-28 13:42 ` [PATCH 1/2] splice: return immediately for zero-length file-to-pipe splices Mehtab Zafar
@ 2026-11-28 13:42 ` Mehtab Zafar
  1 sibling, 0 replies; 3+ messages in thread
From: Mehtab Zafar @ 2026-11-28 13:42 UTC (permalink / raw)
  To: Al Viro, Christian Brauner, linux-fsdevel
  Cc: Jan Kara, Shuah Khan, linux-kselftest, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 4173 bytes --]

Add blocking and nonblocking cases for sendfile() with count zero and
a full output pipe. Both calls must return zero without waiting for pipe
space.

This regression test was developed with AI assistance.

Signed-off-by: Mehtab Zafar <hello@mzfr.me>
---
The selftest passed 0/2 cases on baseline and 2/2 on the patched kernel
in each of three fresh x86_64 KVM boots per kernel. The static test binary
was built with warnings treated as errors; git diff --check was clean.
With the generic FILE_PATH_CHANGES message ignored, final strict
checkpatch reported no errors, warnings, or checks. Its final run also
logged an spdxcheck.py traceback because Python ply was missing; the
source and SPDX line were unchanged from the earlier successful SPDX
validation.

 tools/testing/selftests/splice/.gitignore     |  1 +
 tools/testing/selftests/splice/Makefile       |  1 +
 .../testing/selftests/splice/sendfile_zero.c  | 93 +++++++++++++++++++
 3 files changed, 95 insertions(+)
 cre
ate mode 100644 tools/testing/selftests/splice/sendfile_zero.c

diff --git a/tools/testing/selftests/splice/.gitignore b/tools/testing/selftests/splice/.gitignore
index be8266f5d04c..0b7414d3bbf1 100644
--- a/tools/testing/selftests/splice/.gitignore
+++ b/tools/testing/selftests/splice/.gitignore
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-only
 default_file_splice_read
+sendfile_zero
 splice_read
diff --git a/tools/testing/selftests/splice/Makefile b/tools/testing/selftests/splice/Makefile
index 541cd826d5a5..b77c12767846 100644
--- a/tools/testing/selftests/splice/Makefile
+++ b/tools/testing/selftests/splice/Makefile
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 TEST_PROGS := default_file_splice_read.sh short_splice_read.sh
+TEST_GEN_PROGS := sendfile_zero
 TEST_GEN_PROGS_EXTENDED := default_file_splice_read splice_read
 
 include ../lib.mk
diff --git a/tools/testing/selftests/splice/sendfile_zero.c b/tools/testing/selftests/splice/sendfile_zero
.c
new file mode 100644
index 000000000000..b597420be687
--- /dev/null
+++ b/tools/testing/selftests/splice/sendfile_zero.c
@@ -0,0 +1,93 @@
+// SPDX-License-Identifier: GPL-2.0
+#define _GNU_SOURCE
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/sendfile.h>
+#include <unistd.h>
+
+#include "../kselftest_harness.h"
+
+FIXTURE(sendfile_zero) {
+	int input_fd;
+	int pipefd[2];
+};
+
+FIXTURE_SETUP(sendfile_zero)
+{
+	char path[] = "/tmp/sendfile_zero.XXXXXX";
+	char buf[4096] = {};
+	ssize_t ret;
+
+	self->input_fd = -1;
+	self->pipefd[0] = -1;
+	self->pipefd[1] = -1;
+
+	self->input_fd = mkstemp(path);
+	ASSERT_GE(self->input_fd, 0);
+	ASSERT_EQ(0, unlink(path));
+	ASSERT_EQ(1, write(self->input_fd, "x", 1));
+	ASSERT_EQ(0, lseek(self->input_fd, 0, SEEK_SET));
+
+	ASSERT_EQ(0, pipe2(self->pipefd, O_NONBLOCK));
+	while ((ret = write(self->pipefd[1], buf, sizeof(buf))) > 0)
+		;

+	ASSERT_EQ(-1, ret);
+	ASSERT_EQ(EAGAIN, errno);
+}
+
+FIXTURE_TEARDOWN(sendfile_zero)
+{
+	if (self->pipefd[0] >= 0)
+		close(self->pipefd[0]);
+	if (self->pipefd[1] >= 0)
+		close(self->pipefd[1]);
+	if (self->input_fd >= 0)
+		close(self->input_fd);
+}
+
+TEST_F(sendfile_zero, nonblocking_full_pipe)
+{
+	off_t offset = 0;
+	ssize_t ret;
+
+	ret = sendfile(self->pipefd[1], self->input_fd, &offset, 0);
+	ASSERT_EQ(0, ret) TH_LOG("sendfile: %s", strerror(errno));
+	ASSERT_EQ(0, offset);
+}
+
+static void alarm_handler(int signal)
+{
+	(void)signal;
+}
+
+TEST_F(sendfile_zero, blocking_full_pipe)
+{
+	struct sigaction action = {
+		.sa_handler = alarm_handler,
+	};
+	struct sigaction old_action;
+	off_t offset = 0;
+	ssize_t ret;
+	int error;
+	int flags;
+
+	flags = fcntl(self->pipefd[1], F_GETFL);
+	ASSERT_GE(flags, 0);
+	ASSERT_EQ(0, fcntl(self->pipefd[1], F_SETFL,
+			   flags & ~O_NONBLOCK));
+	ASSERT_EQ(0, sigaction(SIGALRM, &action, &
old_action));
+
+	alarm(1);
+	ret = sendfile(self->pipefd[1], self->input_fd, &offset, 0);
+	error = errno;
+	alarm(0);
+	ASSERT_EQ(0, sigaction(SIGALRM, &old_action, NULL));
+
+	ASSERT_EQ(0, ret) TH_LOG("sendfile: %s", strerror(error));
+	ASSERT_EQ(0, offset);
+}
+
+TEST_HARNESS_MAIN
-- 
2.34.1


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 563 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-28 13:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-28 13:42 [PATCH 0/2] splice: handle zero-count sendfile to full pipes Mehtab Zafar
2026-11-28 13:42 ` [PATCH 1/2] splice: return immediately for zero-length file-to-pipe splices Mehtab Zafar
2026-11-28 13:42 ` [PATCH 2/2] selftests/splice: cover zero-count sendfile to a full pipe Mehtab Zafar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®