mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mehtab Zafar <hello@mzfr.me>
To: Al Viro <viro@zeniv.linux.org.uk>,
	Christian Brauner <brauner@kernel.org>,
	linux-fsdevel@vger.kernel.org
Cc: Jan Kara <jack@suse.cz>, Shuah Khan <shuah@kernel.org>,
	linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 2/2] selftests/splice: cover zero-count sendfile to a full pipe
Date: Sat, 28 Nov 2026 13:42:48 +0000	[thread overview]
Message-ID: <20261128134221.64884-3-hello@mzfr.me> (raw)
In-Reply-To: <20261128134221.64884-1-hello@mzfr.me>

[-- 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 --]

      parent reply	other threads:[~2026-11-28 13:42 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261128134221.64884-3-hello@mzfr.me \
    --to=hello@mzfr.me \
    --cc=brauner@kernel.org \
    --cc=jack@suse.cz \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=shuah@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®