vfs: Make sendfile(2) killable even better
Subsystems (2)
Jan Kara
reviewerPATCH 1/2 vfs: Make sendfile(2) killable even better
Commit 296291cdd162 (mm: make sendfile(2) killable) fixed an issue where sendfile(2) was doing a lot of tiny writes into a filesystem and thus was unkillable for a long time. However sendfile(2) can be (mis)used to issue lots of writes into arbitrary file descriptor such as evenfd or similar special file descriptors which never hit the standard filesystem write path and thus are still unkillable. E.g. the following example from Dmitry burns CPU for ~16s on my test system without possibility to be killed:
int r1 = eventfd(0, 0);
int r2 = memfd_create("", 0);
unsigned long n = 1<<30;
fallocate(r2, 0, 0, n);
sendfile(r1, r2, 0, n);There are actually quite a few tests for pending signals in sendfile code however we data to write is always available none of them seems to trigger. So fix the problem by adding a test for pending signal into splice_from_pipe_next() also before the loop waiting for pipe buffers to be available. This should fix all the lockup issues with sendfile of the do-ton-of-tiny-writes nature.
--- fs/splice.c | 7 +++++++ 1 file changed, 7 insertions(+)
fs/splice.c +7 -0
@@ -809,6 +809,13 @@ static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_des*/ static int splice_from_pipe_next (struct pipe_inode_info *pipe,struct splice_desc *sd) { +/* +* Check for signal early to make process killable when there are +* always buffers available +*/ +if (signal_pending (current)) +return -ERESTARTSYS ; +while (!pipe->nrbufs) {if (!pipe->writers)return 0 ;
Signature · 1 line
2.1.4
Jan Kara
reviewerPATCH 2/2 vfs: Avoid softlockups with sendfile(2)
The following test program from Dmitry can cause softlockups or RCU stalls as it copies 1GB from tmpfs into eventfd and we don't have any scheduling point at that path in sendfile(2) implementation:
int r1 = eventfd(0, 0);
int r2 = memfd_create("", 0);
unsigned long n = 1<<30;
fallocate(r2, 0, 0, n);
sendfile(r1, r2, 0, n);Add cond_resched() into __splice_from_pipe() to fix the problem.
--- fs/splice.c | 1 + 1 file changed, 1 insertion(+)
fs/splice.c +1 -0
@@ -891,6 +891,7 @@ ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,splice_from_pipe_begin (sd); do { + cond_resched(); ret = splice_from_pipe_next(pipe, sd); if (ret > 0) ret = splice_from_pipe_feed(pipe, sd, actor);
Signature · 1 line
2.1.4
Re: [PATCH 2/2] vfs: Avoid softlockups with sendfile(2)
Fixed the issue for me.
Quoted 34 lines · Jan Kara
The following test program from Dmitry can cause softlockups or RCU stalls as it copies 1GB from tmpfs into eventfd and we don't have any scheduling point at that path in sendfile(2) implementation:
int r1 = eventfd(0, 0); int r2 = memfd_create("", 0); unsigned long n = 1<<30; fallocate(r2, 0, 0, n); sendfile(r1, r2, 0, n);Add
cond_resched()into__splice_from_pipe()to fix the problem.--- fs/splice.c | 1 + 1 file changed, 1 insertion(+)fs/splice.c +1
-1@@ -891,6 +891,7 @@ ssize_t __splice_from_pipe(struct pipe_inode_info *pipe, struct splice_desc *sd,splice_from_pipe_begin (sd); do { + cond_resched(); ret = splice_from_pipe_next(pipe, sd); if (ret > 0) ret = splice_from_pipe_feed(pipe, sd, actor); --2.1.4
Fixed the issue for me.
Thanks for quick fixes!
Quoted 50 lines · Jan Kara
Commit
296291cdd162(mm: make sendfile(2) killable) fixed an issue where sendfile(2) was doing a lot of tiny writes into a filesystem and thus was unkillable for a long time. However sendfile(2) can be (mis)used to issue lots of writes into arbitrary file descriptor such as evenfd or similar special file descriptors which never hit the standard filesystem write path and thus are still unkillable. E.g. the following example from Dmitry burns CPU for ~16s on my test system without possibility to be killed:int r1 = eventfd(0, 0); int r2 = memfd_create("", 0); unsigned long n = 1<<30; fallocate(r2, 0, 0, n); sendfile(r1, r2, 0, n);There are actually quite a few tests for pending signals in sendfile code however we data to write is always available none of them seems to trigger. So fix the problem by adding a test for pending signal into
splice_from_pipe_next()also before the loop waiting for pipe buffers to be available. This should fix all the lockup issues with sendfile of the do-ton-of-tiny-writes nature.Reported-by Dmitry Vyukov <[email protected]>Signed-off-by Jan Kara <[email protected]>--- fs/splice.c | 7 +++++++ 1 file changed, 7 insertions(+)fs/splice.c +7
-1@@ -809,6 +809,13 @@ static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_des*/ static int splice_from_pipe_next (struct pipe_inode_info *pipe,struct splice_desc *sd) { +/* +* Check for signal early to make process killable when there are +* always buffers available +*/ +if (signal_pending (current)) +return -ERESTARTSYS ; +while (!pipe->nrbufs) {if (!pipe->writers)return 0 ; --2.1.4