mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Kris Pan <kris.pan@intel.com>
To: gregkh@linuxfoundation.org
Cc: akpm@linux-foundation.org, hughd@google.com,
	viro@zeniv.linux.org.uk, brauner@kernel.org, arnd@arndb.de,
	max.kellermann@ionos.com, linux-fsdevel@vger.kernel.org,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	Kris Pan <kris.pan@intel.com>
Subject: [PATCH v2 1/2] fs: move splice_zeropage_into_pipe() and zero_pipe_buf_ops to fs/splice.c
Date: Sat, 29 Aug 2026 12:37:45 +0800	[thread overview]
Message-ID: <20260829043746.2929210-2-kris.pan@intel.com> (raw)
In-Reply-To: <20260829043746.2929210-1-kris.pan@intel.com>

The zero-page splice logic in mm/shmem.c (zero_pipe_buf_ops and
splice_zeropage_into_pipe()) is generally useful for any file that wants
to splice zero pages into a pipe, such as /dev/zero and /dev/full.

Move them to fs/splice.c next to the other pipe buffer operations and
declare them in include/linux/splice.h so they can be shared by other
callers without duplicating the boilerplate.

No functional change.

Signed-off-by: Kris Pan <kris.pan@intel.com>
---
 fs/splice.c            | 62 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/splice.h |  3 ++
 mm/shmem.c             | 45 ------------------------------
 3 files changed, 65 insertions(+), 45 deletions(-)

diff --git a/fs/splice.c b/fs/splice.c
index 9d8f63e2fd1ab..b6ea98cc62fd5 100644
--- a/fs/splice.c
+++ b/fs/splice.c
@@ -398,6 +398,68 @@ ssize_t copy_splice_read(struct file *in, loff_t *ppos,
 }
 EXPORT_SYMBOL(copy_splice_read);
 
+/*
+ * Pipe buffer operations for splicing zero pages into a pipe, used to
+ * optimize reads from sparse holes in files and from zero devices such as
+ * /dev/zero and /dev/full.
+ */
+static bool zero_pipe_buf_get(struct pipe_inode_info *pipe,
+			      struct pipe_buffer *buf)
+{
+	return true;
+}
+
+static void zero_pipe_buf_release(struct pipe_inode_info *pipe,
+				  struct pipe_buffer *buf)
+{
+}
+
+static bool zero_pipe_buf_try_steal(struct pipe_inode_info *pipe,
+				    struct pipe_buffer *buf)
+{
+	return false;
+}
+
+const struct pipe_buf_operations zero_pipe_buf_ops = {
+	.release	= zero_pipe_buf_release,
+	.try_steal	= zero_pipe_buf_try_steal,
+	.get		= zero_pipe_buf_get,
+};
+
+/**
+ * splice_zeropage_into_pipe - splice a zero page into a pipe
+ * @pipe: pipe to splice into
+ * @fpos: file position
+ * @size: number of bytes to splice
+ *
+ * Splice at most one page of zeroes into the pipe.  The zero page is not
+ * refcounted, so @zero_pipe_buf_ops must be used to avoid taking a
+ * reference on it.
+ *
+ * Return: the number of bytes spliced.
+ */
+size_t splice_zeropage_into_pipe(struct pipe_inode_info *pipe,
+				 loff_t fpos, size_t size)
+{
+	size_t offset = fpos & ~PAGE_MASK;
+
+	size = min_t(size_t, size, PAGE_SIZE - offset);
+
+	if (!pipe_is_full(pipe)) {
+		struct pipe_buffer *buf = pipe_head_buf(pipe);
+
+		*buf = (struct pipe_buffer) {
+			.ops	= &zero_pipe_buf_ops,
+			.page	= ZERO_PAGE(0),
+			.offset	= offset,
+			.len	= size,
+		};
+		pipe->head++;
+	}
+
+	return size;
+}
+
 const struct pipe_buf_operations default_pipe_buf_ops = {
 	.release	= generic_pipe_buf_release,
 	.try_steal	= generic_pipe_buf_try_steal,
diff --git a/include/linux/splice.h b/include/linux/splice.h
index 9dec4861d09f6..f1d4aefa4ec2c 100644
--- a/include/linux/splice.h
+++ b/include/linux/splice.h
@@ -108,4 +108,7 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
 
 extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
 extern const struct pipe_buf_operations default_pipe_buf_ops;
+extern const struct pipe_buf_operations zero_pipe_buf_ops;
+size_t splice_zeropage_into_pipe(struct pipe_inode_info *pipe, loff_t fpos,
+				 size_t size);
 #endif
diff --git a/mm/shmem.c b/mm/shmem.c
index 9001aaf3b7b94..39d8dfefbe9f5 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -3427,51 +3427,6 @@ static ssize_t shmem_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
 	return ret;
 }
 
-static bool zero_pipe_buf_get(struct pipe_inode_info *pipe,
-			      struct pipe_buffer *buf)
-{
-	return true;
-}
-
-static void zero_pipe_buf_release(struct pipe_inode_info *pipe,
-				  struct pipe_buffer *buf)
-{
-}
-
-static bool zero_pipe_buf_try_steal(struct pipe_inode_info *pipe,
-				    struct pipe_buffer *buf)
-{
-	return false;
-}
-
-static const struct pipe_buf_operations zero_pipe_buf_ops = {
-	.release	= zero_pipe_buf_release,
-	.try_steal	= zero_pipe_buf_try_steal,
-	.get		= zero_pipe_buf_get,
-};
-
-static size_t splice_zeropage_into_pipe(struct pipe_inode_info *pipe,
-					loff_t fpos, size_t size)
-{
-	size_t offset = fpos & ~PAGE_MASK;
-
-	size = min_t(size_t, size, PAGE_SIZE - offset);
-
-	if (!pipe_is_full(pipe)) {
-		struct pipe_buffer *buf = pipe_head_buf(pipe);
-
-		*buf = (struct pipe_buffer) {
-			.ops	= &zero_pipe_buf_ops,
-			.page	= ZERO_PAGE(0),
-			.offset	= offset,
-			.len	= size,
-		};
-		pipe->head++;
-	}
-
-	return size;
-}
-
 static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
 				      struct pipe_inode_info *pipe,
 				      size_t len, unsigned int flags)
-- 
2.43.0


  reply	other threads:[~2026-08-29  4:41 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-29  4:37 [PATCH v2 0/2] drivers/char/mem: splice the zero page for /dev/zero and /dev/full Kris Pan
2026-08-29  4:37 ` Kris Pan [this message]
2026-08-29  4:37 ` [PATCH v2 2/2] " Kris Pan

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=20260829043746.2929210-2-kris.pan@intel.com \
    --to=kris.pan@intel.com \
    --cc=akpm@linux-foundation.org \
    --cc=arnd@arndb.de \
    --cc=brauner@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=hughd@google.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=max.kellermann@ionos.com \
    --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®