Skip to contentSkip to conversation

splice: only read in as much information as there is pipe buffer space

PATCH Superseded Darrick J. Wong 4 participants 6 messages Oct 14, 2019 – Oct 16, 2019 VFS

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 splice: only read in as much information as there is pipe buffer space

From: Darrick J. Wong <[email protected]>

Andreas Grünbacher reports that on the two filesystems that support iomap directio, it's possible for splice() to return -EAGAIN (instead of a short splice) if the pipe being written to has less space available in its pipe buffers than the length supplied by the calling process.

Months ago we fixed splice_direct_to_actor to clamp the length of the read request to the size of the splice pipe. Do the same to do_splice.

Fixes 17614445576b6 ("splice: don't read more than available pipe space")
Reported-by [email protected]
---
 fs/splice.c |   14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
fs/splice.c +11 -3
index 98412721f056..e509239d7e06 100644
@@ -945,12 +945,13 @@ ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
 	WARN_ON_ONCE(pipe->nrbufs != 0);
 
 	while (len) {
+		unsigned int pipe_pages;
 		size_t read_len;
 		loff_t pos = sd->pos, prev_pos = pos;
 
 		/* Don't try to read more the pipe has space for. */
-		read_len = min_t(size_t, len,
-				 (pipe->buffers - pipe->nrbufs) << PAGE_SHIFT);
+		pipe_pages = pipe->buffers - pipe->nrbufs;
+		read_len = min(len, (size_t)pipe_pages << PAGE_SHIFT);
 		ret = do_splice_to(in, &pos, pipe, read_len, flags);
 		if (unlikely(ret <= 0))
 			goto out_release;
@@ -1180,8 +1181,15 @@ static long do_splice(struct file *in, loff_t __user *off_in,
 
 		pipe_lock(opipe);
 		ret = wait_for_space(opipe, flags);
-		if (!ret)
+		if (!ret) {
+			unsigned int pipe_pages;
+
+			/* Don't try to read more the pipe has space for. */
+			pipe_pages = opipe->buffers - opipe->nrbufs;
+			len = min(len, (size_t)pipe_pages << PAGE_SHIFT);
+
 			ret = do_splice_to(in, &offset, opipe, len, flags);
+		}
 		pipe_unlock(opipe);
 		if (ret > 0)
 			wakeup_pipe_readers(opipe);
#1 lore raw

Hi Darrick,

Am Di., 15. Okt. 2019 um 07:33 Uhr schrieb Darrick J. Wong <[email protected]>:

Quoted 7 lines

From: Darrick J. Wong <[email protected]>

Andreas Grünbacher reports that on the two filesystems that support iomap directio, it's possible for splice() to return -EAGAIN (instead of a short splice) if the pipe being written to has less space available in its pipe buffers than the length supplied by the calling process.

Quoted 7 lines

Months ago we fixed splice_direct_to_actor to clamp the length of the read request to the size of the splice pipe. Do the same to do_splice.

Fixes 17614445576b6 ("splice: don't read more than available pipe space")
Reported-by [email protected]

I've done some minimal testing on top of 5.4-rc3. This patch fixes the splice issue and also passes the syzbot reproducer. I'll add it to the set of patches I regularly run fstests on now, but we already know fstests doesn't cover splice in the greatest depth possible, so that's unlikely to reveal anything new.

Thanks,
Andreas

Quoted 41 lines
---
 fs/splice.c |   14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
fs/splice.c +11 -3
index 98412721f056..e509239d7e06 100644
@@ -945,12 +945,13 @@ ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
        WARN_ON_ONCE(pipe->nrbufs != 0);
 
        while (len) {
+               unsigned int pipe_pages;
                size_t read_len;
                loff_t pos = sd->pos, prev_pos = pos;
 
                /* Don't try to read more the pipe has space for. */
-               read_len = min_t(size_t, len,
-                                (pipe->buffers - pipe->nrbufs) << PAGE_SHIFT);
+               pipe_pages = pipe->buffers - pipe->nrbufs;
+               read_len = min(len, (size_t)pipe_pages << PAGE_SHIFT);
                ret = do_splice_to(in, &pos, pipe, read_len, flags);
                if (unlikely(ret <= 0))
                        goto out_release;
@@ -1180,8 +1181,15 @@ static long do_splice(struct file *in, loff_t __user *off_in,
 
                pipe_lock(opipe);
                ret = wait_for_space(opipe, flags);
-               if (!ret)
+               if (!ret) {
+                       unsigned int pipe_pages;
+
+                       /* Don't try to read more the pipe has space for. */
+                       pipe_pages = opipe->buffers - opipe->nrbufs;
+                       len = min(len, (size_t)pipe_pages << PAGE_SHIFT);
+
                        ret = do_splice_to(in, &offset, opipe, len, flags);
+               }
                pipe_unlock(opipe);
                if (ret > 0)
                        wakeup_pipe_readers(opipe);
#2 lore raw
In reply to Darrick J. Wong
Quoted 23 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Andreas Grünbacher reports that on the two filesystems that support iomap directio, it's possible for splice() to return -EAGAIN (instead of a short splice) if the pipe being written to has less space available in its pipe buffers than the length supplied by the calling process.

Months ago we fixed splice_direct_to_actor to clamp the length of the read request to the size of the splice pipe. Do the same to do_splice.

Fixes 17614445576b6 ("splice: don't read more than available pipe space")
Reported-by [email protected]
---
 fs/splice.c |   14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
fs/splice.c +0 -0
index 98412721f056..e509239d7e06 100644

fs/splice.c ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd, · line 948

Quoted 4 lines
fs/splice.c +1 -0
@@ -945,12 +945,13 @@ ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
 	WARN_ON_ONCE(pipe->nrbufs != 0);
 
 	while (len) {
+		unsigned int pipe_pages;

define this as a size_t...

fs/splice.c ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd, · line 954

Quoted 8 lines
fs/splice.c +2 -2
 		size_t read_len;
 		loff_t pos = sd->pos, prev_pos = pos;
 
 		/* Don't try to read more the pipe has space for. */
-		read_len = min_t(size_t, len,
-				 (pipe->buffers - pipe->nrbufs) << PAGE_SHIFT);
+		pipe_pages = pipe->buffers - pipe->nrbufs;
+		read_len = min(len, (size_t)pipe_pages << PAGE_SHIFT);
		read_len = min_t(size_t, len, pipe_pages << PAGER_SHIFT);
Quoted 4 lines
fs/splice.c +0 -0
 		ret = do_splice_to(in, &pos, pipe, read_len, flags);
 		if (unlikely(ret <= 0))
 			goto out_release;

fs/splice.c static long do_splice(struct file *in, loff_t __user *off_in, · line 1189

Quoted 10 lines
fs/splice.c +6 -1
@@ -1180,8 +1181,15 @@ static long do_splice(struct file *in, loff_t __user *off_in,
 
 		pipe_lock(opipe);
 		ret = wait_for_space(opipe, flags);
-		if (!ret)
+		if (!ret) {
+			unsigned int pipe_pages;
+
+			/* Don't try to read more the pipe has space for. */
+			pipe_pages = opipe->buffers - opipe->nrbufs;
+			len = min(len, (size_t)pipe_pages << PAGE_SHIFT);

And same here...

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
#3 lore raw
Quoted 34 lines · Dave Chinner
Quoted 23 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Andreas Grünbacher reports that on the two filesystems that support iomap directio, it's possible for splice() to return -EAGAIN (instead of a short splice) if the pipe being written to has less space available in its pipe buffers than the length supplied by the calling process.

Months ago we fixed splice_direct_to_actor to clamp the length of the read request to the size of the splice pipe. Do the same to do_splice.

Fixes 17614445576b6 ("splice: don't read more than available pipe space")
Reported-by [email protected]
---
 fs/splice.c |   14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
fs/splice.c +0 -0
index 98412721f056..e509239d7e06 100644

fs/splice.c ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd, · line 948

Quoted 4 lines
fs/splice.c +1 -0
@@ -945,12 +945,13 @@ ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,
 	WARN_ON_ONCE(pipe->nrbufs != 0);
 
 	while (len) {
+		unsigned int pipe_pages;

define this as a size_t...

Quoted 3 lines
fs/splice.c +0 -0
 		size_t read_len;
 		loff_t pos = sd->pos, prev_pos = pos;
 
Quoted 7 lines

fs/splice.c ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd, · line 954

Quoted 5 lines
fs/splice.c +2 -2
 		/* Don't try to read more the pipe has space for. */
-		read_len = min_t(size_t, len,
-				 (pipe->buffers - pipe->nrbufs) << PAGE_SHIFT);
+		pipe_pages = pipe->buffers - pipe->nrbufs;
+		read_len = min(len, (size_t)pipe_pages << PAGE_SHIFT);
		read_len = min_t(size_t, len, pipe_pages << PAGER_SHIFT);

If we make pipe_pages have type size_t then we don't need min_t here, right? Since len and read_len are already size_t.

--D

Quoted 24 lines
Quoted 4 lines
fs/splice.c +0 -0
 		ret = do_splice_to(in, &pos, pipe, read_len, flags);
 		if (unlikely(ret <= 0))
 			goto out_release;

fs/splice.c static long do_splice(struct file *in, loff_t __user *off_in, · line 1189

Quoted 10 lines
fs/splice.c +6 -1
@@ -1180,8 +1181,15 @@ static long do_splice(struct file *in, loff_t __user *off_in,
 
 		pipe_lock(opipe);
 		ret = wait_for_space(opipe, flags);
-		if (!ret)
+		if (!ret) {
+			unsigned int pipe_pages;
+
+			/* Don't try to read more the pipe has space for. */
+			pipe_pages = opipe->buffers - opipe->nrbufs;
+			len = min(len, (size_t)pipe_pages << PAGE_SHIFT);

And same here...

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
#4 lore raw
In reply to Darrick J. Wong
Quoted 7 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Andreas Grünbacher reports that on the two filesystems that support iomap directio, it's possible for splice() to return -EAGAIN (instead of a short splice) if the pipe being written to has less space available in its pipe buffers than the length supplied by the calling process.

Quoted 5 lines

Months ago we fixed splice_direct_to_actor to clamp the length of the read request to the size of the splice pipe. Do the same to do_splice.

Fixes 17614445576b6 ("splice: don't read more than available pipe space")
Reported-by [email protected]

I already invalidated this syzbot report when the previous version of this patch was dropped, as that was what the report appeared to be for. So you don't need this Reported-by line. It's not a big deal, but including it could mislead people into thinking that syzbot found a problem with the commit in the Fixes:
line, rather than a prior version of this patch.

- Eric

#5 lore raw
Quoted 11 lines · Eric Biggers
Quoted 10 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Andreas Grünbacher reports that on the two filesystems that support iomap directio, it's possible for splice() to return -EAGAIN (instead of a short splice) if the pipe being written to has less space available in its pipe buffers than the length supplied by the calling process.

Months ago we fixed splice_direct_to_actor to clamp the length of the read request to the size of the splice pipe. Do the same to do_splice.

Quoted 8 lines
Quoted 2 lines
Fixes 17614445576b6 ("splice: don't read more than available pipe space")
Reported-by [email protected]

I already invalidated this syzbot report when the previous version of this patch was dropped, as that was what the report appeared to be for. So you don't need this Reported-by line. It's not a big deal, but including it could mislead people into thinking that syzbot found a problem with the commit in the Fixes:
line, rather than a prior version of this patch.

Ok, will drop for v4. Thanks for taking care of the report.

--D

Quoted 1 line

- Eric