mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Bernd Petrovitsch <bernd@firmix.at>
To: Linus Torvalds <torvalds@osdl.org>
Cc: Jeff Garzik <jeff@garzik.org>, Jens Axboe <axboe@suse.de>,
	Ingo Molnar <mingo@elte.hu>,
	linux-kernel@vger.kernel.org, akpm@osdl.org
Subject: Re: [PATCH] splice support #2
Date: Fri, 31 Mar 2006 14:46:26 +0200	[thread overview]
Message-ID: <1143809186.32767.33.camel@tara.firmix.at> (raw)
In-Reply-To: <Pine.LNX.4.64.0603301259220.27203@g5.osdl.org>

On Thu, 2006-03-30 at 13:16 -0800, Linus Torvalds wrote:
[...]
> splice() really can handle any fd->fd move.
> 
> The reason you want to have a pipe in the middle is that you have to 
> handle partial moves _some_ way. And the pipe being the buffer really does 
> allow that, and also handles the case of "what happens when we received 
> more data than we could write".
> 
> So the way to do copies is
> 
> 	int pipefd[2];
> 	unsigned long copied = 0;
> 
> 	if (pipe(pipefd) < 0)
> 		error
> 
> 	for (;;) {
> 		int nr = splice(in, pipefd[1], MAX_INT, 0);
> 		if (nr <= 0)
> 			break;
> 		do {
> 			int ret = splice(pipefd[0], out, nr, 0);
> 			if (ret <= 0) {
> 				error: couldn't write 'nr' bytes
> 				break;
> 			}
> 
> 			nr -= ret;
> 		} while (nr);
> 	}
> 	close(pipefd[0]);
> 	close(pipefd[1]);
> 
> which may _seem_ very complicated and the extra pipe seems "useless", but 
> it's (a) actually pretty efficient and (b) allows error recovery.
> 
> That (b) is the really important part. I can pretty much guarantee that 
> without the "useless" pipe, you simply couldn't do it.
> 
> In particular, what happens when you try to connect two streaming devices, 
> but the destination stops accepting data? You cannot put the received data 
> "back" into the streaming source any way - so if you actually want to be 
> able to handle error recovery, you _have_ to get access to the source 
> buffers.
> 
> Also, for signal handling, you need to have some way to keep the pipe 
> around for several iterations on the sender side, while still returning to 
> user space to do the signal handler. 
> 
> And guess what? That's exactly what you get with that "useless" pipe. For 
> error handling, you can decide to throw the extra data that the recipient 
> didn't want away (just close the pipe), and maybe that's going to be what 
> most people do. But you could also decide to just do a "read()" on the 
> pipefd, to just read the data into user space for debugging and/or error 
> recovery..
> 
> Similarly, for signals, the pipe _is_ that buffer that you need that is 
> consistent across multiple invocations.
> 
> So that pipe is not at all unnecessary, and in fact, it's critical. It may 

IOW the "pipe" above is in fact just a "handle" of a kernel-internal
buffer (of one page) and the two fd's can be used to tell sys-calls to
read from or write into the buffer. But it is much faster because it
avoids real copying of the data into a user-space buffer and out of the
user-space buffer since the kernel can manipulate the underlying data
directly as a typical implementation of the above code does (i.e. use
read(2) and write(2) instead of the two splice(2) calls).
Uuuuuh, real zero-copy seems possible.

	Bernd
-- 
Firmix Software GmbH                   http://www.firmix.at/
mobil: +43 664 4416156                 fax: +43 1 7890849-55
          Embedded Linux Development and Services


  parent reply	other threads:[~2006-03-31 12:47 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-03-30 10:06 Jens Axboe
2006-03-30 10:16 ` Andrew Morton
2006-03-30 10:24   ` Jens Axboe
2006-03-30 11:16     ` Andrew Morton
2006-03-30 11:55       ` Jens Axboe
2006-03-30 12:30         ` Jens Axboe
2006-03-30 12:30           ` Jens Axboe
2006-03-30 19:19           ` Andrew Morton
2006-03-30 12:00 ` Ingo Molnar
2006-03-30 12:05   ` Jens Axboe
2006-03-30 12:10     ` Ingo Molnar
2006-03-30 12:16       ` Jens Axboe
2006-03-30 12:38         ` Ingo Molnar
2006-03-30 12:42           ` Jens Axboe
2006-03-30 12:42             ` Ingo Molnar
2006-03-30 13:02               ` Jens Axboe
2006-03-30 14:20           ` Christoph Hellwig
2006-03-30 17:02     ` Linus Torvalds
2006-03-30 17:17       ` Linus Torvalds
2006-03-31 20:38         ` Hua Zhong
2006-03-31 20:49           ` Linus Torvalds
2006-03-30 20:48       ` Jeff Garzik
2006-03-30 21:16         ` Linus Torvalds
2006-03-31  0:59           ` Nick Piggin
2006-03-31  2:43             ` Andrew Morton
2006-03-31  2:51               ` Andrew Morton
2006-03-31  3:20                 ` Nick Piggin
2006-03-31  6:35                 ` Christoph Hellwig
2006-03-31  7:09           ` Ingo Molnar
2006-04-02 22:33             ` Pavel Machek
2006-03-31 12:46           ` Bernd Petrovitsch [this message]
2006-03-31  9:56       ` Jens Axboe
2006-03-31 12:18       ` Ingo Molnar
2006-03-31 12:23         ` Jens Axboe
2006-03-31 12:26           ` Jens Axboe
2006-03-31 12:47             ` Ingo Molnar
2006-03-31 18:18               ` Jens Axboe
2006-03-31 12:27       ` Ingo Molnar
2006-03-31  0:03 linux
2006-03-31  6:06 tridge
2006-03-31  6:59 ` Antonio Vargas
2006-03-31  7:37   ` tridge
2006-03-31  9:57 ` Jens Axboe
2006-03-31 19:11   ` Linus Torvalds
2006-03-31 19:40     ` Jens Axboe
2006-04-04 17:16       ` Andy Lutomirski
2006-04-04 17:34         ` Jens Axboe
     [not found] <5W2gv-Tp-19@gated-at.bofh.it>
     [not found] ` <5W48C-3KW-17@gated-at.bofh.it>
     [not found]   ` <5W48D-3KW-21@gated-at.bofh.it>
     [not found]     ` <5W8OT-2ms-17@gated-at.bofh.it>
     [not found]       ` <5WcfS-7x9-23@gated-at.bofh.it>
     [not found]         ` <5WcIT-8nr-13@gated-at.bofh.it>
     [not found]           ` <5Wm5I-53z-7@gated-at.bofh.it>
     [not found]             ` <5XjoS-8t9-11@gated-at.bofh.it>
2006-04-03 12:39               ` Bodo Eggert

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=1143809186.32767.33.camel@tara.firmix.at \
    --to=bernd@firmix.at \
    --cc=akpm@osdl.org \
    --cc=axboe@suse.de \
    --cc=jeff@garzik.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=torvalds@osdl.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®