Skip to contentSkip to conversation

splice: fix problem with sys_tee and SPLICE_F_NONBLOCK

PATCH In review Johann Felix Soden 3 participants 9 messages Feb 19, 2008 – Feb 20, 2008 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 splice: fix problem with sys_tee and SPLICE_F_NONBLOCK

From: Johann Felix Soden <[email protected]>

With SPLICE_F_NONBLOCK sys_tee should return number of duplicated bytes, not only -EAGAIN on success.

This patch also solves the problem, which is described on
http://article.gmane.org/gmane.linux.kernel/642502.

---
 fs/splice.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
fs/splice.c +1 -1
index 9b559ee..184fd66 100644
@@ -1711,7 +1711,7 @@ static long do_tee(struct file *in, struct file *out, size_t len,
 			ret = link_opipe_prep(opipe, flags);
 			if (!ret) {
 				ret = link_pipe(ipipe, opipe, len, flags);
-				if (!ret && (flags & SPLICE_F_NONBLOCK))
+				if (ret < 0 && (flags & SPLICE_F_NONBLOCK))
 					ret = -EAGAIN;
 			}
 		}
Signature · 1 line
1.5.4.2
#1 lore raw
Quoted 4 lines · Johann Felix Soden

From: Johann Felix Soden <[email protected]>

With SPLICE_F_NONBLOCK sys_tee should return number of duplicated bytes, not only -EAGAIN on success.

?

The current behaviour is to return bytes tee'd, or return -EAGAIN for zero bytes if SPLICE_F_NONBLOCK is set. It doesn't return "-EAGAIN on success", not sure what you mean there.

Quoted 13 lines

This patch also solves the problem, which is described on
http://article.gmane.org/gmane.linux.kernel/642502.

---
 fs/splice.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
fs/splice.c +0 -0
index 9b559ee..184fd66 100644

fs/splice.c static long do_tee(struct file *in, struct file *out, size_t len, · line 1717

Quoted 8 lines
fs/splice.c +1 -1
@@ -1711,7 +1711,7 @@ static long do_tee(struct file *in, struct file *out, size_t len,
 			ret = link_opipe_prep(opipe, flags);
 			if (!ret) {
 				ret = link_pipe(ipipe, opipe, len, flags);
-				if (!ret && (flags & SPLICE_F_NONBLOCK))
+				if (ret < 0 && (flags & SPLICE_F_NONBLOCK))
 					ret = -EAGAIN;
 			}
 		}

Perhaps it's just me, but this doesn't make a lot of sense. You override any other error with EAGAIN, hm? In fact the only < 0 value that link_pipe() will return is -EPIPE, which is perfectly in sync with that a pipe write will return if there are no readers attached.

Signature · 1 line
Jens Axboe
#2 lore raw
Quoted 12 lines · Johann Felix Soden
Quoted 4 lines

From: Johann Felix Soden <[email protected]>

With SPLICE_F_NONBLOCK sys_tee should return number of duplicated bytes, not only -EAGAIN on success.

?

The current behaviour is to return bytes tee'd, or return -EAGAIN for zero bytes if SPLICE_F_NONBLOCK is set. It doesn't return "-EAGAIN on success", not sure what you mean there.

Sorry, my patch description was not correct.

The new behavior of sys_tee with my patch is:
- return -EAGAIN if there are no data in the pipe, but writer connected to the pipe,
- return 0 if there are not writers connected
- else return number of duplicated byte

The old behavior was: return -EAGAIN or the number (>0) of duplicated bytes.

Quoted 18 lines
Quoted 18 lines

This patch also solves the problem, which is described on
http://article.gmane.org/gmane.linux.kernel/642502.

---
 fs/splice.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
fs/splice.c +1 -1
index 9b559ee..184fd66 100644
@@ -1711,7 +1711,7 @@ static long do_tee(struct file *in, struct file *out, size_t len,
 			ret = link_opipe_prep(opipe, flags);
 			if (!ret) {
 				ret = link_pipe(ipipe, opipe, len, flags);
-				if (!ret && (flags & SPLICE_F_NONBLOCK))
+				if (ret < 0 && (flags & SPLICE_F_NONBLOCK))
Quoted 8 lines

fs/splice.c static long do_tee(struct file *in, struct file *out, size_t len, · line 1717

Quoted 3 lines
fs/splice.c +0 -0
 					ret = -EAGAIN;
 			}
 		}

Perhaps it's just me, but this doesn't make a lot of sense. You override any other error with EAGAIN, hm? In fact the only < 0 value that link_pipe() will return is -EPIPE, which is perfectly in sync with that a pipe write will return if there are no readers attached.

The reason, why I wrote the patch, was, that the example program mentioned in manpage of tee(2) doesn't stop, if it used like cat textfile | tee_example outfile | wc because sys_tee returns only -EAGAIN or the duplicated bytes, not something like EOF.

See also http://article.gmane.org/gmane.linux.kernel/642502.
Patrick McManus described the problem there.

--

Johann Felix Soden

#3 lore raw
Quoted 14 lines · Johann Felix Soden
Quoted 12 lines · Johann Felix Soden
Quoted 4 lines

From: Johann Felix Soden <[email protected]>

With SPLICE_F_NONBLOCK sys_tee should return number of duplicated bytes, not only -EAGAIN on success.

?

The current behaviour is to return bytes tee'd, or return -EAGAIN for zero bytes if SPLICE_F_NONBLOCK is set. It doesn't return "-EAGAIN on success", not sure what you mean there.

Sorry, my patch description was not correct.

Quoted 8 lines

The new behavior of sys_tee with my patch is:
- return -EAGAIN if there are no data in the pipe, but writer connected to the pipe,
- return 0 if there are not writers connected
- else return number of duplicated byte

The old behavior was: return -EAGAIN or the number (>0) of duplicated bytes.

Your patch has an odd way of achieving that goal, modify the real location of the assignment instead of overriding something. That has the potential to turn into another confusing bug later on, wondering why the heck your return value isn't being passed back.

Improvement is welcome though, you can't distuingish -EAGAIN on the input side from the output side currently.

Signature · 1 line
Jens Axboe
#4 lore raw

Am Dienstag, den 19.02.2008, 22:25 +0100 schrieb Jens Axboe:

Quoted 24 lines · Johann Felix Soden
Quoted 14 lines
Quoted 12 lines · Johann Felix Soden
Quoted 4 lines

From: Johann Felix Soden <[email protected]>

With SPLICE_F_NONBLOCK sys_tee should return number of duplicated bytes, not only -EAGAIN on success.

?

The current behaviour is to return bytes tee'd, or return -EAGAIN for zero bytes if SPLICE_F_NONBLOCK is set. It doesn't return "-EAGAIN on success", not sure what you mean there.

Sorry, my patch description was not correct.

Quoted 8 lines

The new behavior of sys_tee with my patch is:
- return -EAGAIN if there are no data in the pipe, but writer connected to the pipe,
- return 0 if there are not writers connected
- else return number of duplicated byte

The old behavior was: return -EAGAIN or the number (>0) of duplicated bytes.

Quoted 8 lines

Your patch has an odd way of achieving that goal, modify the real location of the assignment instead of overriding something. That has the potential to turn into another confusing bug later on, wondering why the heck your return value isn't being passed back.

Improvement is welcome though, you can't distuingish -EAGAIN on the input side from the output side currently.

I thought again about the problem and my patch: you are right, the patch is nonsense. I have learnt, that the correctness of a patch is not guaranteed by the (bad, but anyhow working) solution of the problem the patch was written for.
Sorry for wasting your time.
--
J. F. Soden

#5 lore raw
Quoted 35 lines · Johann Felix Soden

Am Dienstag, den 19.02.2008, 22:25 +0100 schrieb Jens Axboe:

Quoted 24 lines · Johann Felix Soden
Quoted 14 lines
Quoted 12 lines · Johann Felix Soden
Quoted 4 lines

From: Johann Felix Soden <[email protected]>

With SPLICE_F_NONBLOCK sys_tee should return number of duplicated bytes, not only -EAGAIN on success.

?

The current behaviour is to return bytes tee'd, or return -EAGAIN for zero bytes if SPLICE_F_NONBLOCK is set. It doesn't return "-EAGAIN on success", not sure what you mean there.

Sorry, my patch description was not correct.

Quoted 8 lines

The new behavior of sys_tee with my patch is:
- return -EAGAIN if there are no data in the pipe, but writer connected to the pipe,
- return 0 if there are not writers connected
- else return number of duplicated byte

The old behavior was: return -EAGAIN or the number (>0) of duplicated bytes.

Quoted 8 lines

Your patch has an odd way of achieving that goal, modify the real location of the assignment instead of overriding something. That has the potential to turn into another confusing bug later on, wondering why the heck your return value isn't being passed back.

Improvement is welcome though, you can't distuingish -EAGAIN on the input side from the output side currently.

Quoted 5 lines

I thought again about the problem and my patch: you are right, the patch is nonsense. I have learnt, that the correctness of a patch is not guaranteed by the (bad, but anyhow working) solution of the problem the patch was written for.
Sorry for wasting your time.

Don't worry, it's not a waste of time even though your solution isn't the correct one.

When non-blocking is set, ideally we want to return 0 if there's no hope of anymore data and EAGAIN if trying later may yield some data. So how about this instead?

fs/splice.c +8 -4
index 9b559ee..0670c91 100644
@@ -1669,6 +1669,13 @@ static int link_pipe(struct pipe_inode_info *ipipe,
 		i++;
 	} while (len);
 
+	/*
+	 * return EAGAIN if we have the potential of some data in the
+	 * future, otherwise just return 0
+	 */
+	if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
+		ret = -EAGAIN;
+
 	inode_double_unlock(ipipe->inode, opipe->inode);
 
 	/*
@@ -1709,11 +1716,8 @@ static long do_tee(struct file *in, struct file *out, size_t len,
 		ret = link_ipipe_prep(ipipe, flags);
 		if (!ret) {
 			ret = link_opipe_prep(opipe, flags);
-			if (!ret) {
+			if (!ret)
 				ret = link_pipe(ipipe, opipe, len, flags);
-				if (!ret && (flags & SPLICE_F_NONBLOCK))
-					ret = -EAGAIN;
-			}
 		}
 	}
 
Signature · 1 line
Jens Axboe
#6 lore raw
Quoted 60 lines · Jens Axboe
Quoted 34 lines · Johann Felix Soden

Am Dienstag, den 19.02.2008, 22:25 +0100 schrieb Jens Axboe:

Quoted 32 lines · Johann Felix Soden
Quoted 14 lines
Quoted 12 lines · Johann Felix Soden
Quoted 4 lines

From: Johann Felix Soden <[email protected]>

With SPLICE_F_NONBLOCK sys_tee should return number of duplicated bytes, not only -EAGAIN on success.

?

The current behaviour is to return bytes tee'd, or return -EAGAIN for zero bytes if SPLICE_F_NONBLOCK is set. It doesn't return "-EAGAIN on success", not sure what you mean there.

Sorry, my patch description was not correct.

Quoted 8 lines

The new behavior of sys_tee with my patch is:
- return -EAGAIN if there are no data in the pipe, but writer connected to the pipe,
- return 0 if there are not writers connected
- else return number of duplicated byte

The old behavior was: return -EAGAIN or the number (>0) of duplicated bytes.

Your patch has an odd way of achieving that goal, modify the real location of the assignment instead of overriding something. That has the potential to turn into another confusing bug later on, wondering why the heck your return value isn't being passed back.

Improvement is welcome though, you can't distuingish -EAGAIN on the input side from the output side currently.

When non-blocking is set, ideally we want to return 0 if there's no hope of anymore data and EAGAIN if trying later may yield some data. So how about this instead?

fs/splice.c +7 -0
index 9b559ee..0670c91 100644
@@ -1669,6 +1669,13 @@ static int link_pipe(struct pipe_inode_info *ipipe,
 		i++;
 	} while (len);
 
+	/*
+	 * return EAGAIN if we have the potential of some data in the
+	 * future, otherwise just return 0
+	 */
+	if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
+		ret = -EAGAIN;
+
 	inode_double_unlock(ipipe->inode, opipe->inode);
 
 	/*
@@ -1709,11 +1716,8 @@ static long do_tee(struct file *in, struct file *out, size_t len,
 		ret = link_ipipe_prep(ipipe, flags);
 		if (!ret) {

fs/splice.c static long do_tee(struct file *in, struct file *out, size_t len, · line 1723

Quoted 10 lines
fs/splice.c +1 -4
 			ret = link_opipe_prep(opipe, flags);
-			if (!ret) {
+			if (!ret)
 				ret = link_pipe(ipipe, opipe, len, flags);
-				if (!ret && (flags & SPLICE_F_NONBLOCK))
-					ret = -EAGAIN;
-			}
 		}
 	}
 

Thanks! This works great.
Add if you want: Tested-by: Johann Felix Soden <[email protected]>

---
Johann Felix Soden

#7 lore raw
Quoted 66 lines · Johann Felix v. Soden-Fr.
Quoted 65 lines · Jens Axboe
Quoted 34 lines · Johann Felix Soden

Am Dienstag, den 19.02.2008, 22:25 +0100 schrieb Jens Axboe:

Quoted 32 lines · Johann Felix Soden
Quoted 14 lines
Quoted 12 lines · Johann Felix Soden
Quoted 4 lines

From: Johann Felix Soden <[email protected]>

With SPLICE_F_NONBLOCK sys_tee should return number of duplicated bytes, not only -EAGAIN on success.

?

The current behaviour is to return bytes tee'd, or return -EAGAIN for zero bytes if SPLICE_F_NONBLOCK is set. It doesn't return "-EAGAIN on success", not sure what you mean there.

Sorry, my patch description was not correct.

Quoted 8 lines

The new behavior of sys_tee with my patch is:
- return -EAGAIN if there are no data in the pipe, but writer connected to the pipe,
- return 0 if there are not writers connected
- else return number of duplicated byte

The old behavior was: return -EAGAIN or the number (>0) of duplicated bytes.

Your patch has an odd way of achieving that goal, modify the real location of the assignment instead of overriding something. That has the potential to turn into another confusing bug later on, wondering why the heck your return value isn't being passed back.

Improvement is welcome though, you can't distuingish -EAGAIN on the input side from the output side currently.

When non-blocking is set, ideally we want to return 0 if there's no hope of anymore data and EAGAIN if trying later may yield some data. So how about this instead?

fs/splice.c +8 -2
index 9b559ee..0670c91 100644
@@ -1669,6 +1669,13 @@ static int link_pipe(struct pipe_inode_info *ipipe,
 		i++;
 	} while (len);
 
+	/*
+	 * return EAGAIN if we have the potential of some data in the
+	 * future, otherwise just return 0
+	 */
+	if (!ret && ipipe->waiting_writers && (flags & SPLICE_F_NONBLOCK))
+		ret = -EAGAIN;
+
 	inode_double_unlock(ipipe->inode, opipe->inode);
 
 	/*
@@ -1709,11 +1716,8 @@ static long do_tee(struct file *in, struct file *out, size_t len,
 		ret = link_ipipe_prep(ipipe, flags);
 		if (!ret) {
 			ret = link_opipe_prep(opipe, flags);
-			if (!ret) {
+			if (!ret)
 				ret = link_pipe(ipipe, opipe, len, flags);
-				if (!ret && (flags & SPLICE_F_NONBLOCK))
Quoted 8 lines

fs/splice.c static long do_tee(struct file *in, struct file *out, size_t len, · line 1723

Quoted 5 lines
fs/splice.c +0 -2
-					ret = -EAGAIN;
-			}
 		}
 	}
 

Thanks! This works great.
Add if you want: Tested-by: Johann Felix Soden <[email protected]>

Thanks for testing that it works as expected, I'll commit and add your tested-by.

Signature · 1 line
Jens Axboe
#8 lore raw
In reply to Jens Axboe
Quoted 4 lines · Jens Axboe

When non-blocking is set, ideally we want to return 0 if there's no hope of anymore data and EAGAIN if trying later may yield some data. So how about this instead?

Thank you Jens and Johann.

-Patrick