vmsplice: fix some problems in my previous vmsplice patchset
Subsystems (6)
v2 cover vmsplice: fix some problems in my previous vmsplice patchset
This patchset is for VFS. Of course, it depends on my previous vmsplice patchset ( https://lore.kernel.org/all/[email protected]/ ).
I fix some problems in my previous patchset.
1. Fix problem with CLASS(fd, f)(fd). See first patch in this patchset for details. This is probably not so important, but I fix it anyway.
2. Change "unsigned long" back to "int". See second patch for details. Again, this is probably not important, but I want to fix this anyway.
3. Fix that LTP vmsplice01 bug.
4. libfuse relies on sharing vmsplice behavior. So we detect particular combination of flags to pipe2(2) and vmsplice(2) and return -EINVAL. This forces libfuse to fail back to non-vmsplice code path. I. e. we fix libfuse-related regression [1].
I did debian code search for regex "vmsplice.*SPLICE_F_NONBLOCK" and I found no other packages with this particular combination of flags except for fuse itself. (Okay, other packages are fio and stress-ng, but these are merely testers.) So, I think this is okay to return EINVAL here, breakage will be minimal.
5. Set FMODE_NOWAIT for named FIFOs. CRIU relies on ability to do vmsplice(SPLICE_F_NONBLOCK) on named FIFOs. So, I fix this CRIU-related regression [2]. But there is another CRIU-related regression, which I do not fix [3]: CRIU behavior in splice mode becomes so slow that splice mode becomes useless. I personally still believe that removing vmsplice is right thing to do. Other option is doing nothing. Yet another option is to implement some deprecation period [3]. Let other developers decide.
See patches for details.
Please, run that LTP vmsplice01 test again.
Notes:
- I want to repeat: I change behavior around SPLICE_F_NONBLOCK. Previously, vmsplice ignored whether pipe itself was opened as non-blocking file. Now it is not ignored. And in my opinion new behavior is better.
- vmsplice(2) now is in fs/read_write.c . It is very similar to preadv2 and pwritev2 now, so I think it belongs to fs/read_write.c now.
Please, review this patchset carefully. I'm still new contributor. In particular, please, review that do-while loop, I'm not sure I did everything right.
Tested in Qemu.
[1] https://lore.kernel.org/all/CAJnrk1Y9egYizkx1H9K0cqxSYuB+7vLvQbV7Tf4C5eHFqnnC-A@mail.gmail.com/
[2] https://lore.kernel.org/all/CANaxB-zK5q=Xw6UZTmeFtXsDZjUsPkFk=p485m-wtNTBnf4hgg@mail.gmail.com/
[3] https://lore.kernel.org/all/CANaxB-xUrLQYGiRJZc4Boi+KX=0TJSWymErNovANVko20fMDVA@mail.gmail.com/
v1: https://lore.kernel.org/lkml/[email protected]/
Changes since v1: fix fuse-related and CRIU-related regressions (see above).
Askar Safin (7):
vmsplice: open-code do_writev and do_readv
vmsplice: change argument type back to "int"
splice: turn wait_for_space flags argument into bool
pipe: move wait_for_space to fs/pipe.c and rename it
vmsplice: make sure we don't wait after writing some data
vmsplice: return -EINVAL for particular combination of flags
pipe: set FMODE_NOWAIT for named FIFOs
fs/pipe.c | 23 +++++++++++++ fs/read_write.c | 71 +++++++++++++++++++++++++++++++++++---- fs/splice.c | 19 +---------- include/linux/pipe_fs_i.h | 2 ++ include/linux/syscalls.h | 2 +- 5 files changed, 91 insertions(+), 26 deletions(-)
base-commit: 8d86fcfc2857d64af85f5c87c193c25655c970af
Signature · 1 line
2.47.3
PATCH v2 1/7 vmsplice: open-code do_writev and do_readv
My previous vmsplice patch did the following mistake: I did "CLASS(fd, f)(fd)", then did some checks on resulting "struct file", then passed numeric (!) file descriptor to a function.
This is somewhat okay in this particular case, but I still think this is code smell, so I fix this by open-coding do_writev and do_readv.
Also I insert a comment to warn other developers to keep do_writev and do_readv in sync with vmsplice(2).
--- fs/read_write.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-)
fs/read_write.c +17 -5
@@ -1070,6 +1070,7 @@ static ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,static ssize_t do_readv (unsigned long fd,const struct iovec __user *vec,unsigned long vlen,rwf_t flags) { +/* All future changes to this function should be kept in sync with vmsplice(2). */ CLASS (fd_pos, f)(fd);ssize_t ret = -EBADF ;
@@ -1093,6 +1094,7 @@ static ssize_t do_readv(unsigned long fd, const struct iovec __user *vec,static ssize_t do_writev (unsigned long fd,const struct iovec __user *vec,unsigned long vlen,rwf_t flags) { +/* All future changes to this function should be kept in sync with vmsplice(2). */ CLASS (fd_pos, f)(fd);ssize_t ret = -EBADF ;
@@ -1226,14 +1228,24 @@ SYSCALL_DEFINE4(vmsplice, unsigned long, fd, const struct iovec __user *, vec,if (fd_empty (f))return -EBADF ; -/* We do do_writev/do_readv, so it is okay to pass "false" here */ +/* We do vfs_writev/vfs_readv, so it is okay to pass "false" here */ if (!get_pipe_info (fd_file (f),/* for_splice = */ false))return -EBADF ; -if (fd_file (f)->f_mode &FMODE_WRITE ) -return do_writev (fd, vec, vlen, (flags &SPLICE_F_NONBLOCK ) ?RWF_NOWAIT :0 ); -else -return do_readv (fd, vec, vlen, (flags &SPLICE_F_NONBLOCK ) ?RWF_NOWAIT :0 ); +if (fd_file (f)->f_mode &FMODE_WRITE ) { +ssize_t ret =vfs_writev (fd_file (f), vec, vlen,NULL , (flags &SPLICE_F_NONBLOCK ) ?RWF_NOWAIT :0 ); +if (ret >0 ) +add_wchar (current, ret); +inc_syscw (current); +return ret; + }else { +ssize_t ret =vfs_readv (fd_file (f), vec, vlen,NULL , (flags &SPLICE_F_NONBLOCK ) ?RWF_NOWAIT :0 ); + +if (ret >0 ) +add_rchar (current, ret); +inc_syscr (current); +return ret; + } }/*
Signature · 1 line
2.47.3
PATCH v2 2/7 vmsplice: change argument type back to "int"
In reply to Askar SafinMy previous vmsplice patchset changed vmsplice argument from "int" to "unsigned long". This may cause problems, so let's change it back.
--- fs/read_write.c | 2 +- include/linux/syscalls.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
fs/read_write.c +1 -1
@@ -1218,7 +1218,7 @@ SYSCALL_DEFINE6(pwritev2, unsigned long, fd, const struct iovec __user *, vec,/* * Legacy preadv2/pwritev2 wrapper. */ -SYSCALL_DEFINE4(vmsplice, unsigned long, fd, const struct iovec __user *, vec, +SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, vec, unsigned long, vlen, unsigned int, flags) {if (unlikely (flags & ~SPLICE_F_ALL ))
include/linux/syscalls.h +1 -1
@@ -514,7 +514,7 @@ asmlinkage long sys_ppoll_time32(struct pollfd __user *, unsigned int, struct old_timespec32 __user *, const sigset_t __user *, size_t);asmlinkage long sys_signalfd4 (int ufd,sigset_t __user *user_mask,size_t sizemask,int flags); -asmlinkage long sys_vmsplice (unsigned long fd,const struct iovec __user *vec, +asmlinkage long sys_vmsplice (int fd,const struct iovec __user *vec,unsigned long vlen,unsigned int flags); asmlinkage long sys_splice(int fd_in, loff_t __user *off_in, int fd_out, loff_t __user *off_out,
Signature · 1 line
2.47.3
PATCH v2 3/7 splice: turn wait_for_space flags argument into bool
In reply to Askar SafinI want to do this, because I will move this function to fs/pipe.c.
--- fs/splice.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
fs/splice.c +3 -3
@@ -1239,7 +1239,7 @@ ssize_t splice_file_range(struct file *in, loff_t *ppos, struct file *out, }EXPORT_SYMBOL (splice_file_range); -static int wait_for_space (struct pipe_inode_info *pipe,unsigned flags) +static int wait_for_space (struct pipe_inode_info *pipe,bool non_block) {for (;;) {if (unlikely (!pipe->readers)) {
@@ -1248,7 +1248,7 @@ static int wait_for_space(struct pipe_inode_info *pipe, unsigned flags) }if (!pipe_is_full (pipe))return 0 ; -if (flags &SPLICE_F_NONBLOCK ) +if (non_block)return -EAGAIN ;if (signal_pending (current))return -ERESTARTSYS ;
@@ -1268,7 +1268,7 @@ ssize_t splice_file_to_pipe(struct file *in,ssize_t ret;pipe_lock (opipe); - ret =wait_for_space (opipe, flags); + ret =wait_for_space (opipe, flags &SPLICE_F_NONBLOCK );if (!ret) ret =do_splice_read (in, offset, opipe, len, flags);pipe_unlock (opipe);
Signature · 1 line
2.47.3
PATCH v2 4/7 pipe: move wait_for_space to fs/pipe.c and rename it
In reply to Askar SafinThis is needed, because I plan to use it in fs/read_write.c.
--- fs/pipe.c | 17 +++++++++++++++++ fs/splice.c | 19 +------------------ include/linux/pipe_fs_i.h | 2 ++ 3 files changed, 20 insertions(+), 18 deletions(-)
fs/pipe.c +17 -0
@@ -1451,6 +1451,23 @@ long pipe_fcntl(struct file *file, unsigned int cmd, unsigned int arg)return ret; } +int pipe_wait_for_space (struct pipe_inode_info *pipe,bool non_block) +{ +for (;;) { +if (unlikely (!pipe->readers)) { +send_sig (SIGPIPE , current,0 ); +return -EPIPE ; + } +if (!pipe_is_full (pipe)) +return 0 ; +if (non_block) +return -EAGAIN ; +if (signal_pending (current)) +return -ERESTARTSYS ; +pipe_wait_writable (pipe); + } +} +static const struct super_operations pipefs_ops = { .destroy_inode = free_inode_nonrcu, .statfs = simple_statfs,
fs/splice.c +1 -18
@@ -1239,23 +1239,6 @@ ssize_t splice_file_range(struct file *in, loff_t *ppos, struct file *out, }EXPORT_SYMBOL (splice_file_range); -static int wait_for_space (struct pipe_inode_info *pipe,bool non_block) -{ -for (;;) { -if (unlikely (!pipe->readers)) { -send_sig (SIGPIPE , current,0 ); -return -EPIPE ; - } -if (!pipe_is_full (pipe)) -return 0 ; -if (non_block) -return -EAGAIN ; -if (signal_pending (current)) -return -ERESTARTSYS ; -pipe_wait_writable (pipe); - } -} -static int splice_pipe_to_pipe (struct pipe_inode_info *ipipe,struct pipe_inode_info *opipe,size_t len,unsigned int flags);
@@ -1268,7 +1251,7 @@ ssize_t splice_file_to_pipe(struct file *in,ssize_t ret;pipe_lock (opipe); - ret =wait_for_space (opipe, flags &SPLICE_F_NONBLOCK ); + ret =pipe_wait_for_space (opipe, flags &SPLICE_F_NONBLOCK );if (!ret) ret =do_splice_read (in, offset, opipe, len, flags);pipe_unlock (opipe);
include/linux/pipe_fs_i.h +2 -0
@@ -335,4 +335,6 @@ struct pipe_inode_info *get_pipe_info(struct file *file, bool for_splice);int create_pipe_files (struct file **,int );unsigned int round_pipe_size (unsigned int size); +int pipe_wait_for_space (struct pipe_inode_info *pipe,bool non_block); +#endif
Signature · 1 line
2.47.3
PATCH v2 5/7 vmsplice: make sure we don't wait after writing some data
In reply to Askar SafinMake sure we don't wait for space in pipe after writing some data. This is needed for compatibility with previous version of vmsplice. Found by LTP vmsplice01.
See comments in the code and links below for details.
--- fs/read_write.c | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-)
fs/read_write.c +37 -2
@@ -1221,6 +1221,8 @@ SYSCALL_DEFINE6(pwritev2, unsigned long, fd, const struct iovec __user *, vec, SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, vec, unsigned long, vlen, unsigned int, flags) { +struct pipe_inode_info *pipe; +if (unlikely (flags & ~SPLICE_F_ALL ))return -EINVAL ;
@@ -1229,11 +1231,44 @@ SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, vec,return -EBADF ;/* We do vfs_writev/vfs_readv, so it is okay to pass "false" here */ -if (!get_pipe_info (fd_file (f),/* for_splice = */ false)) + pipe =get_pipe_info (fd_file (f),/* for_splice = */ false); + +if (!pipe)return -EBADF ;if (fd_file (f)->f_mode &FMODE_WRITE ) { -ssize_t ret =vfs_writev (fd_file (f), vec, vlen,NULL , (flags &SPLICE_F_NONBLOCK ) ?RWF_NOWAIT :0 ); +/* +* When writing to the pipe, previous implementation of vmsplice +* first waited for space in the pipe to appear +* (depending on whether SPLICE_F_NONBLOCK was passed), +* then did unconditional non-blocking write to the pipe. +* +* This differs from what pwritev2 does. +* +* For compatibility we do the same thing previous +* implementation did. +* +* We lock the pipe, do pipe_wait_for_space, then unlock +* the pipe, and then do vfs_writev. vfs_writev internally +* locks the pipe again. This may cause TOCTOU: when we +* do vfs_writev, the pipe may become full again. So we +* do a loop. +*/ + +bool non_block = (flags &SPLICE_F_NONBLOCK ) || (fd_file (f)->f_flags &O_NONBLOCK ); +ssize_t ret; + +do { +pipe_lock (pipe); + ret =pipe_wait_for_space (pipe, non_block); +pipe_unlock (pipe); + +if (ret <0 ) +break ; + + ret =vfs_writev (fd_file (f), vec, vlen,NULL ,RWF_NOWAIT ); + }while (!non_block && ret == -EAGAIN ); +if (ret >0 )add_wchar (current, ret);inc_syscw (current);
Signature · 1 line
2.47.3
PATCH v2 6/7 vmsplice: return -EINVAL for particular combination of flags
In reply to Askar SafinSee comment for details.
--- fs/read_write.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
fs/read_write.c +10 -0
@@ -1258,6 +1258,16 @@ SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, vec,bool non_block = (flags &SPLICE_F_NONBLOCK ) || (fd_file (f)->f_flags &O_NONBLOCK );ssize_t ret; +/* +* libfuse relies on sharing vmsplice behavior. +* So we detect particular combination of flags to +* pipe2(2) and vmsplice(2) and return -EINVAL. +* This forces libfuse to fail back to non-vmsplice +* code path. +*/ +if ((flags ==SPLICE_F_NONBLOCK ) && (fd_file (f)->f_flags &O_NONBLOCK )) +return -EINVAL ; + do { pipe_lock(pipe); ret = pipe_wait_for_space(pipe, non_block);
Signature · 1 line
2.47.3
PATCH v2 7/7 pipe: set FMODE_NOWAIT for named FIFOs
In reply to Askar SafinCRIU relies on ability to do vmsplice(SPLICE_F_NONBLOCK) on named FIFOs.
--- fs/pipe.c | 6 ++++++ 1 file changed, 6 insertions(+)
fs/pipe.c +6 -0
@@ -1156,6 +1156,12 @@ static int fifo_open(struct inode *inode, struct file *filp)/* We can only do regular read/write on fifos */ stream_open (inode, filp); +/* +* CRIU relies on ability to do vmsplice(SPLICE_F_NONBLOCK) +* on named FIFOs. +*/ + filp->f_mode |=FMODE_NOWAIT ; +switch (filp->f_mode & (FMODE_READ |FMODE_WRITE )) {case FMODE_READ :/*
Signature · 1 line
2.47.3
Quoted 4 lines · Askar Safin
This patchset is for VFS. Of course, it depends on my previous vmsplice patchset ( https://lore.kernel.org/all/[email protected]/ ).
I fix some problems in my previous patchset.
I think we concluded that we cannot rip out vmsplice that way at this point, and I suspect that Christian will drop that topic branch from -next after -rc1.
Signature · 3 lines
Cheers, David
"David Hildenbrand (Arm)" <[email protected]>:
Quoted 2 lines
I think we concluded that we cannot rip out vmsplice that way at this point, and I suspect that Christian will drop that topic branch from -next after -rc1.
I think my patches still have a chance.
On fuse regression: I return EINVAL for particular combination of flags used by fuse. This causes fuse to fail-back to non-vmsplice code path. I did Debian code search, and I found none significant packages, which use same combination of options.
So I think I was able to deal with fuse regression.
On CRIU named fifo "Not supported" regression: it is handled.
On CRIU major performance regression: it is NOT handled. But I still think my approach is right. (See cover letter for details.)
(I wrote about all these in cover letter for this v2 patchset.)
So all regressions found so far (except for CRIU major performance regression) are handled.
Other option is to introduce some deprecation period (as suggested by Andrei Vagin). I can do this, if needed.
Signature · 1 line
Askar Safin
Quoted 5 lines · Askar Safin
"David Hildenbrand (Arm)" <[email protected]>:
Quoted 2 lines
I think we concluded that we cannot rip out vmsplice that way at this point, and I suspect that Christian will drop that topic branch from -next after -rc1.
I think my patches still have a chance.
I talked to Christian and it doesn't sound like it.
Signature · 3 lines
Cheers, David
Christian Brauner
maintainerQuoted 8 lines · David Hildenbrand (Arm)
Quoted 5 lines · Askar Safin
"David Hildenbrand (Arm)" <[email protected]>:
Quoted 2 lines
I think we concluded that we cannot rip out vmsplice that way at this point, and I suspect that Christian will drop that topic branch from -next after -rc1.
I think my patches still have a chance.
I talked to Christian and it doesn't sound like it.
The amount of regression reports that we got in short succession doesn't make it likely that we can merge a plain degradation.
Quoted 2 lines · Christian Brauner
The amount of regression reports that we got in short succession doesn't make it likely that we can merge a plain degradation.
Let me repeat: this v2 patchset fixes all regressions found so far, except for major CRIU performance regression
Signature · 1 line
Askar Safin
Quoted 7 lines · Askar Safin
Quoted 2 lines · Christian Brauner
The amount of regression reports that we got in short succession doesn't make it likely that we can merge a plain degradation.
Let me repeat: this v2 patchset fixes all regressions found so far, except for major CRIU performance regression
Askar,
As previously mentioned, this isn't just a major performance issue for CRIU; it completely breaks the current pre-dump implementation.
The other proposed fixes look like workarounds that target specific projects. Since few projects run tests on linux-next kernels, we should expect more projects to be affected by these changes.
Thanks,
Andrei