[lkml]   [2019]   [Mar]   [27]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
Patch in this message
Subject[PATCH AUTOSEL 4.9 20/87] fs: Make splice() and tee() take into account O_NONBLOCK flag on pipes
From: Slavomir Kaslev <>

[ Upstream commit ee5e001196d1345b8fee25925ff5f1d67936081e ]

The current implementation of splice() and tee() ignores O_NONBLOCK set
on pipe file descriptors and checks only the SPLICE_F_NONBLOCK flag for
blocking on pipe arguments. This is inconsistent since splice()-ing
from/to non-pipe file descriptors does take O_NONBLOCK into

Fix this by promoting O_NONBLOCK, when set on a pipe, to

Some context for how the current implementation of splice() leads to
inconsistent behavior. In the ongoing work[1] to add VM tracing
capability to trace-cmd we stream tracing data over named FIFOs or
vsockets from guests back to the host.

When we receive SIGINT from user to stop tracing, we set O_NONBLOCK on
the input file descriptor and set SPLICE_F_NONBLOCK for the next call to
splice(). If splice() was blocked waiting on data from the input FIFO,
after SIGINT splice() restarts with the same arguments (no
SPLICE_F_NONBLOCK) and blocks again instead of returning -EAGAIN when no
data is available.

This differs from the splice() behavior when reading from a vsocket or
when we're doing a traditional read()/write() loop (trace-cmd's
--nosplice argument).

With this patch applied we get the same behavior in all situations after
setting O_NONBLOCK which also matches the behavior of doing a
read()/write() loop instead of splice().

This change does have potential of breaking users who don't expect
EAGAIN from splice() when SPLICE_F_NONBLOCK is not set. OTOH programs
that set O_NONBLOCK and don't anticipate EAGAIN are arguably buggy[2].


Signed-off-by: Slavomir Kaslev <>
Reviewed-by: Steven Rostedt (VMware) <>
Signed-off-by: Linus Torvalds <>
Signed-off-by: Sasha Levin <>
fs/splice.c | 12 ++++++++++++
1 file changed, 12 insertions(+)

diff --git a/fs/splice.c b/fs/splice.c
index 01983bea760c..3f9e7a724d85 100644
--- a/fs/splice.c
+++ b/fs/splice.c
@@ -1137,6 +1137,9 @@ static long do_splice(struct file *in, loff_t __user *off_in,
if (ipipe == opipe)
return -EINVAL;

+ if ((in->f_flags | out->f_flags) & O_NONBLOCK)
return splice_pipe_to_pipe(ipipe, opipe, len, flags);

@@ -1162,6 +1165,9 @@ static long do_splice(struct file *in, loff_t __user *off_in,
if (unlikely(ret < 0))
return ret;

+ if (in->f_flags & O_NONBLOCK)
ret = do_splice_from(ipipe, out, &offset, len, flags);
@@ -1186,6 +1192,9 @@ static long do_splice(struct file *in, loff_t __user *off_in,
offset = in->f_pos;

+ if (out->f_flags & O_NONBLOCK)
ret = wait_for_space(opipe, flags);
if (!ret)
@@ -1718,6 +1727,9 @@ static long do_tee(struct file *in, struct file *out, size_t len,
* copying the data.
if (ipipe && opipe && ipipe != opipe) {
+ if ((in->f_flags | out->f_flags) & O_NONBLOCK)
* Keep going, unless we encounter an error. The ipipe/opipe
* ordering doesn't really matter.
 \ /
  Last update: 2019-03-27 19:44    [W:0.282 / U:5.064 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site