mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Octavian Purdila <opurdila@ixiacom.com>
To: linux-kernel@vger.kernel.org
Cc: Octavian Purdila <opurdila@ixiacom.com>
Subject: [RFC] [PATCH] splice: add ->splice_read support for /dev/zero
Date: Thu,  7 Aug 2008 05:00:55 +0300	[thread overview]
Message-ID: <1218074455-17901-1-git-send-email-opurdila@ixiacom.com> (raw)

Useful for testing.

Preallocating the zero page is simpler and faster with the relatively
small (?) drawback of using an extra page of memory.

Maybe when CONFIG_EMBEDED is selected we can switch to dynamically
allocating the page?

Signed-off-by: Octavian Purdila <opurdila@ixiacom.com>
---
 drivers/char/mem.c |   68 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 68 insertions(+), 0 deletions(-)

diff --git a/drivers/char/mem.c b/drivers/char/mem.c
index 934ffaf..fb33f9a 100644
--- a/drivers/char/mem.c
+++ b/drivers/char/mem.c
@@ -737,6 +737,69 @@ static int mmap_zero(struct file * file, struct vm_area_struct * vma)
 	return 0;
 }
 
+static void zero_pipe_buf_release(struct pipe_inode_info *pipe,
+				  struct pipe_buffer *buf)
+{
+}
+
+static void zero_pipe_buf_get(struct pipe_inode_info *pipe,
+				struct pipe_buffer *buf)
+{
+}
+
+static int zero_pipe_buf_steal(struct pipe_inode_info *pipe,
+			       struct pipe_buffer *buf)
+{
+	return 1;
+}
+
+static struct pipe_buf_operations zero_pipe_buf_ops = {
+	.can_merge = 0,
+	.map = generic_pipe_buf_map,
+	.unmap = generic_pipe_buf_unmap,
+	.confirm = generic_pipe_buf_confirm,
+	.release = zero_pipe_buf_release,
+	.steal = zero_pipe_buf_steal,
+	.get = zero_pipe_buf_get,
+};
+
+static void zero_spd_release(struct splice_pipe_desc *spd, unsigned int nr)
+{
+}
+
+static struct page *zero_page;
+
+ssize_t splice_read_zero(struct file *in, loff_t *ppos,
+			 struct pipe_inode_info *pipe, size_t size,
+			 unsigned int flags)
+{
+	struct partial_page partial[PIPE_BUFFERS];
+	struct page *pages[PIPE_BUFFERS];
+	struct splice_pipe_desc spd = {
+		.pages = pages,
+		.partial = partial,
+		.flags = flags,
+		.ops = &zero_pipe_buf_ops,
+		.spd_release = zero_spd_release
+	};
+	int last_page_len = PAGE_SIZE, i;
+
+	spd.nr_pages = size/PAGE_SIZE;
+	if (spd.nr_pages > PIPE_BUFFERS)
+		spd.nr_pages = PIPE_BUFFERS;
+	if (size < PAGE_SIZE*PIPE_BUFFERS)
+		last_page_len = size % PAGE_SIZE;
+
+	for (i = 0; i < spd.nr_pages; i++) {
+		pages[i] = zero_page;
+		partial[i].len = PAGE_SIZE;
+		partial[i].offset = 0;
+	}
+	partial[spd.nr_pages].len = last_page_len;
+
+	return splice_to_pipe(pipe, &spd);
+}
+
 static ssize_t write_full(struct file * file, const char __user * buf,
 			  size_t count, loff_t *ppos)
 {
@@ -839,6 +902,7 @@ static const struct file_operations zero_fops = {
 	.read		= read_zero,
 	.write		= write_zero,
 	.mmap		= mmap_zero,
+	.splice_read 	= splice_read_zero,
 };
 
 /*
@@ -974,6 +1038,10 @@ static int __init chr_dev_init(void)
 	int i;
 	int err;
 
+	zero_page = alloc_pages(GFP_KERNEL | __GFP_ZERO, 0);
+	if (!zero_page)
+		return -ENOMEM;
+
 	err = bdi_init(&zero_bdi);
 	if (err)
 		return err;
-- 
1.5.6.2


             reply	other threads:[~2008-08-07  2:04 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-07  2:00 Octavian Purdila [this message]
2008-08-19  4:43 ` Andrew Morton
2008-08-19  9:07   ` Octavian Purdila

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=1218074455-17901-1-git-send-email-opurdila@ixiacom.com \
    --to=opurdila@ixiacom.com \
    --cc=linux-kernel@vger.kernel.org \
    /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®