On Fri, May 13, 2022 at 12:23 PM Andrew Morton akpm@linux-foundation.org wrote:
The quilt patch titled Subject: fs: sendfile handles O_NONBLOCK of out_fd has been removed from the -mm tree. Its filename was fs-sendfile-handles-o_nonblock-of-out_fd.patch
This patch was dropped because it was merged into the mm-nonmm-stable branch\nof git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Andrew, sorry for bothering you. I can't find this patch in mm-nonmm-stable and it has not been merged to the Linus' tree. Do I miss something?
From: Andrei Vagin avagin@gmail.com Subject: fs: sendfile handles O_NONBLOCK of out_fd
sendfile has to return EAGAIN if out_fd is nonblocking and the write into it would block.
Here is a small reproducer for the problem:
#define _GNU_SOURCE /* See feature_test_macros(7) */ #include <fcntl.h> #include <stdio.h> #include <unistd.h> #include <errno.h> #include <sys/stat.h> #include <sys/types.h> #include <sys/sendfile.h>
#define FILE_SIZE (1UL << 30) int main(int argc, char **argv) { int p[2], fd;
if (pipe2(p, O_NONBLOCK)) return 1; fd = open(argv[1], O_RDWR | O_TMPFILE, 0666); if (fd < 0) return 1; ftruncate(fd, FILE_SIZE); if (sendfile(p[1], fd, 0, FILE_SIZE) == -1) { fprintf(stderr, "FAIL\n"); } if (sendfile(p[1], fd, 0, FILE_SIZE) != -1 || errno != EAGAIN) { fprintf(stderr, "FAIL\n"); } return 0;
}
It worked before b964bf53e540, it is stuck after b964bf53e540, and it works again with this fix.
This regression occurred because do_splice_direct() calls pipe_write that handles O_NONBLOCK. Here is a trace log from the reproducer:
| __x64_sys_sendfile64() {
| do_sendfile() {
| __fdget()
| rw_verify_area()
| __fdget()
| rw_verify_area()
| do_splice_direct() {
| rw_verify_area()
| splice_direct_to_actor() {
| do_splice_to() {
| rw_verify_area()
| generic_file_splice_read()
- 74.153 us | }
| direct_splice_actor() {
| iter_file_splice_write() {
| __kmalloc()
- 0.148 us | pipe_lock();
- 0.153 us | splice_from_pipe_next.part.0();
- 0.162 us | page_cache_pipe_buf_confirm();
... 16 times
- 0.159 us | page_cache_pipe_buf_confirm();
| vfs_iter_write() {
| do_iter_write() {
| rw_verify_area()
| do_iter_readv_writev() {
| pipe_write() {
| mutex_lock()
- 0.153 us | mutex_unlock();
- 1.368 us | }
- 1.686 us | }
- 5.798 us | }
- 6.084 us | }
- 0.174 us | kfree();
- 0.152 us | pipe_unlock();
- 14.461 us | }
- 14.783 us | }
- 0.164 us | page_cache_pipe_buf_release();
... 16 times
- 0.161 us | page_cache_pipe_buf_release();
| touch_atime()
- 95.854 us | }
- 99.784 us | }
- ! 107.393 us | }
- ! 107.699 us | }
Link: https://lkml.kernel.org/r/20220415005015.525191-1-avagin@gmail.com Fixes: b964bf53e540 ("teach sendfile(2) to handle send-to-pipe directly") Signed-off-by: Andrei Vagin avagin@gmail.com Cc: Al Viro viro@zeniv.linux.org.uk Cc: stable@vger.kernel.org Signed-off-by: Andrew Morton akpm@linux-foundation.org
fs/read_write.c | 3 +++ 1 file changed, 3 insertions(+)
--- a/fs/read_write.c~fs-sendfile-handles-o_nonblock-of-out_fd +++ a/fs/read_write.c @@ -1247,6 +1247,9 @@ static ssize_t do_sendfile(int out_fd, i count, fl); file_end_write(out.file); } else {
if (out.file->f_flags & O_NONBLOCK)
fl |= SPLICE_F_NONBLOCK;
retval = splice_file_to_pipe(in.file, opipe, &pos, count, fl); }
_
Patches currently in -mm which might be from avagin@gmail.com are