Fix restoring pipes with full buffers
PATCH 1/1 Fix restoring pipes with full buffers
While this fixes restoring pipes that were completely full, it actually corrects a potential issue with restoring any pipe buffers. By using splice() to do this work when we are reading the image from another pipe, we depend on userspace setting up the buffers in the pipe perfectly such that the data to be restored is oriented in the pipe in the same way as it is expected (or required) to be in the restored pipe. The "full" case is the hardest to get right, but userspace could break things if it loaded up the inbound pipe with lots of small buffers which would cause splice() to hit the PIPE_BUFFERS limit before having read the requested amount of data.
Instead, drop the optimization and just read() and write() data into the pipe.
--- fs/pipe.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 46 insertions(+), 3 deletions(-)
fs/pipe.c +46 -3
@@ -926,17 +926,60 @@ static int pipe_file_checkpoint(struct ckpt_ctx *ctx, struct file *file)return ret; } +static int restore_pipe_buffer (struct ckpt_ctx *ctx, +struct file *dest, +int len) +{ +char *buf; +int ret; +int nread; +int nwrote; +int nleft = len; + + buf =kmalloc (PAGE_SIZE ,GFP_KERNEL ); +if (!buf) +return -ENOMEM ; + +for (nleft = len; nleft >0 ; nleft -= nread) { +int size = nleft <PAGE_SIZE ? nleft :PAGE_SIZE ; +loff_t pos; + + pos =file_pos_read (ctx->file); + nread =kernel_read (ctx->file, pos, buf, size); +if (nread <0 ) { + ret = nread; + goto out; + } +file_pos_write (ctx->file, pos + nread); + + pos =file_pos_read (dest); + nwrote =kernel_write (dest, pos, buf, nread); +if (nwrote <0 ) { + ret = nwrote; + goto out; + } +file_pos_write (dest, pos + nwrote); + +if (nwrote != nread) { + ret = -EPIPE ; + goto out; + } + } + ret = len; + out: +kfree (buf); +return ret; +} +static int restore_pipe (struct ckpt_ctx *ctx,struct file *file) { -struct pipe_inode_info *pipe;int len, ret; len =_ckpt_read_obj_type (ctx,NULL ,0 ,CKPT_HDR_PIPE_BUF );if (len <=0 )return len; - pipe = file->f_dentry->d_inode->i_pipe; - ret =do_splice_to (ctx->file, &ctx->file->f_pos, pipe, len,0 ); + ret =restore_pipe_buffer (ctx, file, len);if (ret >=0 && ret != len) ret = -EPIPE ;/* can occur due to an error in source file */
Signature · 1 line
1.7.2.2
Dan,
Thanks for pointing this out and the patch.
I think it would be simpler to use ckpt_kread(), no ? If so, I'll go ahead and fix then import.
Oren.
Quoted 87 lines · Dan Smith
While this fixes restoring pipes that were completely full, it actually corrects a potential issue with restoring any pipe buffers. By using
splice()to do this work when we are reading the image from another pipe, we depend on userspace setting up the buffers in the pipe perfectly such that the data to be restored is oriented in the pipe in the same way as it is expected (or required) to be in the restored pipe. The "full" case is the hardest to get right, but userspace could break things if it loaded up the inbound pipe with lots of small buffers which would causesplice()to hit thePIPE_BUFFERSlimit before having read the requested amount of data.Instead, drop the optimization and just
read()andwrite()data into the pipe.Signed-off-by Dan Smith <danms-r/[email protected]>--- fs/pipe.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 46 insertions(+), 3 deletions(-)fs/pipe.c +46
-3@@ -926,17 +926,60 @@ static int pipe_file_checkpoint(struct ckpt_ctx *ctx, struct file *file)return ret; } +static int restore_pipe_buffer (struct ckpt_ctx *ctx, +struct file *dest, +int len) +{ +char *buf; +int ret; +int nread; +int nwrote; +int nleft = len; + + buf =kmalloc (PAGE_SIZE ,GFP_KERNEL ); +if (!buf) +return -ENOMEM ; + +for (nleft = len; nleft >0 ; nleft -= nread) { +int size = nleft <PAGE_SIZE ? nleft :PAGE_SIZE ; +loff_t pos; + + pos =file_pos_read (ctx->file); + nread =kernel_read (ctx->file, pos, buf, size); +if (nread <0 ) { + ret = nread; + goto out; + } +file_pos_write (ctx->file, pos + nread); + + pos =file_pos_read (dest); + nwrote =kernel_write (dest, pos, buf, nread); +if (nwrote <0 ) { + ret = nwrote; + goto out; + } +file_pos_write (dest, pos + nwrote); + +if (nwrote != nread) { + ret = -EPIPE ; + goto out; + } + } + ret = len; + out: +kfree (buf); +return ret; +} +static int restore_pipe (struct ckpt_ctx *ctx,struct file *file) { -struct pipe_inode_info *pipe;int len, ret; len =_ckpt_read_obj_type (ctx,NULL ,0 ,CKPT_HDR_PIPE_BUF );if (len <=0 )return len; - pipe = file->f_dentry->d_inode->i_pipe; - ret =do_splice_to (ctx->file, &ctx->file->f_pos, pipe, len,0 ); + ret =restore_pipe_buffer (ctx, file, len);if (ret >=0 && ret != len) ret = -EPIPE ;/* can occur due to an error in source file */
Matt Helsley
Reviewed-byQuoted 11 lines · Dan Smith
While this fixes restoring pipes that were completely full, it actually corrects a potential issue with restoring any pipe buffers. By using
splice()to do this work when we are reading the image from another pipe, we depend on userspace setting up the buffers in the pipe perfectly such that the data to be restored is oriented in the pipe in the same way as it is expected (or required) to be in the restored pipe. The "full" case is the hardest to get right, but userspace could break things if it loaded up the inbound pipe with lots of small buffers which would causesplice()to hit thePIPE_BUFFERSlimit before having read the requested amount of data.
Quoted 2 lines
Instead, drop the optimization and just
read()andwrite()data into the pipe.
Looks good:
While it seems like it should be possible to modify splice internals to handle this Oren's checkpoint/restart tree doesn't seem like the right place to attempt that work.
Quoted 48 lines
Signed-off-by Dan Smith <danms-r/[email protected]>--- fs/pipe.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 46 insertions(+), 3 deletions(-)fs/pipe.c +34
-0@@ -926,17 +926,60 @@ static int pipe_file_checkpoint(struct ckpt_ctx *ctx, struct file *file)return ret; } +static int restore_pipe_buffer (struct ckpt_ctx *ctx, +struct file *dest, +int len) +{ +char *buf; +int ret; +int nread; +int nwrote; +int nleft = len; + + buf =kmalloc (PAGE_SIZE ,GFP_KERNEL ); +if (!buf) +return -ENOMEM ; + +for (nleft = len; nleft >0 ; nleft -= nread) { +int size = nleft <PAGE_SIZE ? nleft :PAGE_SIZE ; +loff_t pos; + + pos =file_pos_read (ctx->file); + nread =kernel_read (ctx->file, pos, buf, size); +if (nread <0 ) { + ret = nread; + goto out; + } +file_pos_write (ctx->file, pos + nread); + + pos =file_pos_read (dest); + nwrote =kernel_write (dest, pos, buf, nread); +if (nwrote <0 ) { + ret = nwrote; + goto out; + } +file_pos_write (dest, pos + nwrote); +
fs/pipe.c static int pipe_file_checkpoint(struct ckpt_ctx *ctx, struct file *file) · line 972
Quoted 10 lines
fs/pipe.c +10
-0+if (nwrote != nread) { + ret = -EPIPE ; + goto out; + } + } + ret = len; + out: +kfree (buf); +return ret; +}
Could you make this more generic and just take a source struct file * as a parameter too (instead of the struct ckpt_ctx *)? Perhaps rename it copy_pipe_to_pipe(src_file, dest_file, len) or some such? Seems like a helper like this should already exist somewhere...
Cheers,
-Matt Helsley
OL> I think it would be simpler to use ckpt_kread(), no ? If so, I'll OL> go ahead and fix then import.
Yeah, I can't remember why I didn't do that. I swear I had a reason, but I must be thinking of something else.
Thanks!
Signature · 3 lines
Dan Smith IBM Linux Technology Center email: danms-r/[email protected]
Quoted 58 lines · Matt Helsley
Quoted 11 lines · Dan Smith
While this fixes restoring pipes that were completely full, it actually corrects a potential issue with restoring any pipe buffers. By using
splice()to do this work when we are reading the image from another pipe, we depend on userspace setting up the buffers in the pipe perfectly such that the data to be restored is oriented in the pipe in the same way as it is expected (or required) to be in the restored pipe. The "full" case is the hardest to get right, but userspace could break things if it loaded up the inbound pipe with lots of small buffers which would causesplice()to hit thePIPE_BUFFERSlimit before having read the requested amount of data.Quoted 2 lines
Instead, drop the optimization and just
read()andwrite()data into the pipe.Looks good:
Reviewed-by Matt Helsley <matthltc-r/[email protected]>While it seems like it should be possible to modify splice internals to handle this Oren's checkpoint/restart tree doesn't seem like the right place to attempt that work.
Quoted 34 lines
Signed-off-by Dan Smith <danms-r/[email protected]>--- fs/pipe.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 46 insertions(+), 3 deletions(-)fs/pipe.c +20
-0@@ -926,17 +926,60 @@ static int pipe_file_checkpoint(struct ckpt_ctx *ctx, struct file *file)return ret; } +static int restore_pipe_buffer (struct ckpt_ctx *ctx, +struct file *dest, +int len) +{ +char *buf; +int ret; +int nread; +int nwrote; +int nleft = len; + + buf =kmalloc (PAGE_SIZE ,GFP_KERNEL ); +if (!buf) +return -ENOMEM ; + +for (nleft = len; nleft >0 ; nleft -= nread) { +int size = nleft <PAGE_SIZE ? nleft :PAGE_SIZE ; +loff_t pos; + + pos =file_pos_read (ctx->file); + nread =kernel_read (ctx->file, pos, buf, size);
Quoted 8 lines
fs/pipe.c static int pipe_file_checkpoint(struct ckpt_ctx *ctx, struct file *file) · line 956
Quoted 8 lines
fs/pipe.c +8
-0+if (nread <0 ) { + ret = nread; + goto out; + } +file_pos_write (ctx->file, pos + nread); + + pos =file_pos_read (dest); + nwrote =kernel_write (dest, pos, buf, nread);
Actually, this is wrong: it works if checkpoint first found the write-end and then the read-end; but the opposite case fails because the code will try to write to the read-end file. (It did work before because we used the pipe directly).
Quoted 13 lines
Quoted 13 lines
fs/pipe.c +13
-0+if (nwrote <0 ) { + ret = nwrote; + goto out; + } +file_pos_write (dest, pos + nwrote); + +if (nwrote != nread) { + ret = -EPIPE ; + goto out; + } + } + ret = len; + out:
Quoted 8 lines
fs/pipe.c static int pipe_file_checkpoint(struct ckpt_ctx *ctx, struct file *file) · line 972
Quoted 3 lines
fs/pipe.c +3
-0+kfree (buf); +return ret; +}Could you make this more generic and just take a source struct file * as a parameter too (instead of the struct
ckpt_ctx*)? Perhaps rename itcopy_pipe_to_pipe(src_file, dest_file, len)or some such? Seems like a helper like this should already exist somewhere...
Yes - like ckpt_write, and _ckpt_write()...
Oren.
From 7d3f62995ed4e83bb43deafa362c78624099e495 Mon Sep 17 00:00:00 2001
From: Oren Laadan <[email protected]>
Date: Fri, 28 Jan 2011 19:23:17 -0500
Subject: [PATCH] c/r: fix restoring pipes with full buffers
Dan Smith pointed out a problem with the use of splice to restore a pipe's contents: since a pipe can have at most PIPE_BUFFERs buffers, if it happens that the input fed through the userspace feeder is split into more pieces, the splice will eventually block. This bad behavior is more likely to occur if the pipe buffer had been nearly full when checkpointed.
Dan's patch fixes the problem by dropping the optimization (we don't expect pipe buffers to be a dominant component of the image), but is inocorrect for cases when the read-end of the pipe was saved first during checkpoint. This version fixes that problem and makes use of c/r's ckpt_kread() helpers.
--- fs/pipe.c | 49 +++++++++++++++++++++++++++---------------- include/linux/checkpoint.h | 2 + kernel/checkpoint/sys.c | 4 +- 3 files changed, 35 insertions(+), 20 deletions(-)
fs/pipe.c +31 -18
@@ -928,20 +928,27 @@ static int pipe_file_checkpoint(struct ckpt_ctx *ctx, struct file *file)static int restore_pipe (struct ckpt_ctx *ctx,struct file *file) { -struct pipe_inode_info *pipe; -int len, ret; +int n, len, ret; len =_ckpt_read_obj_type (ctx,NULL ,0 ,CKPT_HDR_PIPE_BUF );if (len <0 )return len; - pipe = file->f_dentry->d_inode->i_pipe; - ret =do_splice_to (ctx->file, &ctx->file->f_pos, pipe, len,0 ); - -if (ret >=0 && ret != len) - ret = -EPIPE ;/* can occur due to an error in source file */ +while (len) { + n =min (len, (int )PAGE_SIZE ); + ret =ckpt_kread (ctx, ctx->scratch_page, n); +if (ret <0 ) +return ret; + ret =_ckpt_kwrite (file, ctx->scratch_page, n); +if (ret <0 ) +return ret; +/* this should not happen normally ! */ +if (ret != n) +return -EPIPE ; + len -= n; + } -return ret; +return 0 ; }struct file *pipe_file_restore (struct ckpt_ctx *ctx,struct ckpt_hdr_file *ptr)
@@ -971,6 +978,14 @@ struct file *pipe_file_restore(struct ckpt_ctx *ctx, struct ckpt_hdr_file *ptr)if (ret <0 )return file; +/* use the "write" side to restore the pipe's contents */ + file =fget (fds[1 ]); +if (!file)/* this should _never_ happen ! */ +return ERR_PTR (-EBADF ); + ret =restore_pipe (ctx, file); +if (ret <0 ) + goto out; + which = (ptr->f_flags &O_WRONLY ?1 :0 );/* * Below we return the file corersponding to one side
@@ -978,25 +993,23 @@ struct file *pipe_file_restore(struct ckpt_ctx *ctx, struct ckpt_hdr_file *ptr)* other side of the pipe to the hash, to be picked up * when that side is restored. */ - file =fget (fds[1 -which]);/* the 'other' side */ -if (!file)/* this should _never_ happen ! */ -return ERR_PTR (-EBADF ); +if (which ==1 ) {/* the 'other' size */ +fput (file); + file =fget (fds[0 ]); +if (!file)/* this should _never_ happen ! */ +return ERR_PTR (-EBADF ); + } ret =ckpt_obj_insert (ctx, file, h->pipe_objref,CKPT_OBJ_FILE );if (ret <0 ) goto out; - ret =restore_pipe (ctx, file); -fput (file); -if (ret <0 ) -return ERR_PTR (ret); - file =fget (fds[which]);/* 'this' side */ if (!file)/* this should _never_ happen ! */ return ERR_PTR (-EBADF );/* get rid of the file descriptors (caller sets that) */ -sys_close (fds[which]); -sys_close (fds[1 -which]); +sys_close (fds[0 ]); +sys_close (fds[1 ]); }else {return file; }
include/linux/checkpoint.h +2 -0
@@ -76,6 +76,8 @@ extern int walk_task_subtree(struct task_struct *task, void *data);extern void exit_checkpoint (struct task_struct *tsk); +extern ssize_t _ckpt_kwrite (struct file *file,void *buf,size_t count); +extern ssize_t _ckpt_kread (struct file *file,void *buf,size_t count);extern int ckpt_kwrite (struct ckpt_ctx *ctx,void *buf,size_t count);extern int ckpt_kread (struct ckpt_ctx *ctx,void *buf,size_t count);
kernel/checkpoint/sys.c +2 -2
@@ -51,7 +51,7 @@ int ckpt_unpriv_allowed = 1; /* default: unpriv checkpoint not restart */* and return 0, or negative error otherwise. */ -static ssize_t _ckpt_kwrite (struct file *file,void *addr,size_t count) +ssize_t _ckpt_kwrite (struct file *file,void *addr,size_t count) {loff_t pos;int ret;
@@ -82,7 +82,7 @@ int ckpt_kwrite(struct ckpt_ctx *ctx, void *addr, size_t count)return 0 ; } -static ssize_t _ckpt_kread (struct file *file,void *addr,size_t count) +ssize_t _ckpt_kread (struct file *file,void *addr,size_t count) {loff_t pos;int ret;
Signature · 1 line
1.7.1
Dan Smith
Tested-byOL> Cc: Dan Smith <danms-r/[email protected]> OL> Signed-off-by: Oren Laadan <[email protected]>
Signature · 3 lines
Dan Smith IBM Linux Technology Center email: danms-r/[email protected]
thanks, pushed to v23-rc1.
Quoted 5 lines · Dan Smith
OL> Cc: Dan Smith <danms-r/[email protected]> OL> Signed-off-by: Oren Laadan <[email protected]>
Tested-by Dan Smith <danms-r/[email protected]>