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 2/2] drivers/char/mem: splice the zero page for /dev/zero and /dev/full
Date: Sat, 29 Aug 2026 12:37:46 +0800 [thread overview]
Message-ID: <20260829043746.2929210-3-kris.pan@intel.com> (raw)
In-Reply-To: <20260829043746.2929210-1-kris.pan@intel.com>
Implement a splice_read handler for /dev/zero and /dev/full that pushes
references to the global zero page into the pipe, instead of allocating
and zeroing a fresh page for every pipe buffer.
This is much faster than copy_splice_read() for splicing zeroes, since it
avoids the page allocation and zeroing on every pipe buffer.
Use the shared splice_zeropage_into_pipe() helper from fs/splice.c.
Signed-off-by: Kris Pan <kris.pan@intel.com>
Reviewed-by: Max Kellermann <max.kellermann@ionos.com>
---
drivers/char/mem.c | 29 +++++++++++++++++++++++++++--
1 file changed, 27 insertions(+), 2 deletions(-)
diff --git a/drivers/char/mem.c b/drivers/char/mem.c
index 63253d1de5d70..6d3ef359b2d7b 100644
--- a/drivers/char/mem.c
+++ b/drivers/char/mem.c
@@ -471,6 +471,31 @@ static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter)
return written;
}
+static ssize_t splice_read_zero(struct file *in, loff_t *ppos,
+ struct pipe_inode_info *pipe, size_t len,
+ unsigned int flags)
+{
+ size_t total = 0;
+ loff_t pos = 0;
+ size_t used, npages;
+
+ used = pipe_buf_usage(pipe);
+ if (used >= pipe->max_usage)
+ return -EAGAIN;
+ npages = pipe->max_usage - used;
+ len = min_t(size_t, len, npages * PAGE_SIZE);
+
+ while (len) {
+ size_t n = splice_zeropage_into_pipe(pipe, pos, len);
+
+ pos += n;
+ total += n;
+ len -= n;
+ }
+
+ return total;
+}
+
static ssize_t read_zero(struct file *file, char __user *buf,
size_t count, loff_t *ppos)
{
@@ -669,7 +694,7 @@ static const struct file_operations zero_fops = {
.read_iter = read_iter_zero,
.read = read_zero,
.write_iter = write_iter_zero,
- .splice_read = copy_splice_read,
+ .splice_read = splice_read_zero,
.splice_write = splice_write_zero,
.mmap_prepare = mmap_zero_prepare,
.get_unmapped_area = get_unmapped_area_zero,
@@ -682,7 +707,7 @@ static const struct file_operations full_fops = {
.llseek = full_lseek,
.read_iter = read_iter_zero,
.write = write_full,
- .splice_read = copy_splice_read,
+ .splice_read = splice_read_zero,
};
static const struct memdev {
--
2.43.0
prev parent 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] " Kris Pan
2026-08-29 4:37 ` [PATCH v2 1/2] fs: move splice_zeropage_into_pipe() and zero_pipe_buf_ops to fs/splice.c Kris Pan
2026-08-29 4:37 ` Kris Pan [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=20260829043746.2929210-3-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®