Splicing to/from a tty
Subsystems (8)
Commit 36e2c7421f02 (fs: don't allow splice read/write without explicit ops) broke my userspace application which talks to an SSL VPN by splice()ing between "openssl s_client" and "pppd". The latter operates over a pty, and since that commit there is no fallback for splice()ing between a pipe and a pty, or any tty for that matter.
The above commit mentions switching them to the iter ops and using generic_file_splice_read. IIUC, this would require implementing iter ops also on the line disciplines, which sounds pretty disruptive.
For my case, I attempted to instead implement splice_write and splice_read in tty_fops; I managed to get splice_write working calling ld->ops->write, but splice_read is not so simple because the tty_ldisc_ops read method expects a userspace buffer. So I cannot see how to implement this without either (a) using set_fs, or (b) implementing iter ops on all line disciplines.
Is splice()ing between a tty and a pipe worth supporting at all? Not a big deal for my use case at least, but it used to work.
Quoted 17 lines · Oliver Giles
Commit
36e2c7421f02(fs: don't allow splice read/write without explicit ops) broke my userspace application which talks to an SSL VPN by splice()ing between "openssls_client" and "pppd". The latter operates over a pty, and since that commit there is no fallback for splice()ing between a pipe and a pty, or any tty for that matter.The above commit mentions switching them to the iter ops and using
generic_file_splice_read. IIUC, this would require implementing iter ops also on the line disciplines, which sounds pretty disruptive.For my case, I attempted to instead implement
splice_writeandsplice_readintty_fops; I managed to getsplice_writeworking callingld->ops->write, butsplice_readis not so simple because thetty_ldisc_opsread method expects a userspace buffer. So I cannot see how to implement this without either (a) usingset_fs, or (b) implementing iter ops on all line disciplines.
Quoted 2 lines
Is splice()ing between a tty and a pipe worth supporting at all? Not a big deal for my use case at least, but it used to work.
Is it even strictly related to the tty?
I was just now looking into why my cgit/fcgi/nginx setup no longer works, and the reason is getting -EINVAL from sendfile() when the input is a file and the output is a pipe().
So I wrote a simple test program (below) and that errors out on kernel 5.10.4, while it works fine on the 5.9.16 I currently have. Haven't tried reverting anything yet, but now that I haev a test program it should be simple to even bisect.
johannes
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/sendfile.h>
#include <stdio.h>
#include <assert.h>
int main(int argc, char **argv)
{
int in = open(argv[0], O_RDONLY);
int p[2], out;
off_t off = 0;
int err;
assert(in >= 0);
assert(pipe(p) >= 0);
out = p[1];
err = sendfile(out, in, &off, 1024);
if (err < 0)
perror("sendfile");
assert(err == 1024);
return 0;
}Quoted 58 lines · Johannes Berg
Quoted 17 lines · Oliver Giles
Commit
36e2c7421f02(fs: don't allow splice read/write without explicit ops) broke my userspace application which talks to an SSL VPN by splice()ing between "openssls_client" and "pppd". The latter operates over a pty, and since that commit there is no fallback for splice()ing between a pipe and a pty, or any tty for that matter.The above commit mentions switching them to the iter ops and using
generic_file_splice_read. IIUC, this would require implementing iter ops also on the line disciplines, which sounds pretty disruptive.For my case, I attempted to instead implement
splice_writeandsplice_readintty_fops; I managed to getsplice_writeworking callingld->ops->write, butsplice_readis not so simple because thetty_ldisc_opsread method expects a userspace buffer. So I cannot see how to implement this without either (a) usingset_fs, or (b) implementing iter ops on all line disciplines.Quoted 2 lines
Is splice()ing between a tty and a pipe worth supporting at all? Not a big deal for my use case at least, but it used to work.
Is it even strictly related to the tty?
I was just now looking into why my cgit/fcgi/nginx setup no longer works, and the reason is getting
-EINVALfromsendfile()when the input is a file and the output is apipe().So I wrote a simple test program (below) and that errors out on kernel 5.10.4, while it works fine on the 5.9.16 I currently have. Haven't tried reverting anything yet, but now that I haev a test program it should be simple to even bisect.
johannes
#include <
unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/sendfile.h>
#include <stdio.h>
#include <assert.h>int
main(int argc, char **argv)
{
int in =open(argv[0], O_RDONLY);
int p[2], out;off_toff = 0;
int err;assert(in >= 0);
assert(pipe(p) >= 0);
out = p[1];
err =sendfile(out, in, &off, 1024);
if (err < 0)perror("sendfile");
assert(err == 1024);
Quoted 2 lines
return 0;
}
I can confirm the behaviour you see, and that it starts occurring from the same commit 36e2c7421f02a22 (fs: don't allow splice read/write without explicit ops).
In my tty case, it is clear that removing the default fallback would cause this to fail, but assuming the sendfile() source is on a regular filesystem, I am unsure why splice cannot find the appropriate splice_write method. Could be connected to the fact that the message from warn_unsupported in fs/splice.c outputs "splice write not supported for file (pid: 983819 comm: test-sendfile)", i.e. the file path is blank. In my case of directly calling splice on a pty, I do see a path such as /ptyp0 in that error before implementing splice_(read/write) callbacks. Maybe splice is getting a bogus file pointer from sendfile?
Quoted 7 lines · Johannes Berg
Quoted 7 lines
For my case, I attempted to instead implement
splice_writeandsplice_readintty_fops; I managed to getsplice_writeworking callingld->ops->write, butsplice_readis not so simple because thetty_ldisc_opsread method expects a userspace buffer. So I cannot see how to implement this without either (a) usingset_fs, or (b) implementing iter ops on all line disciplines.
Quoted 8 lines
Quoted 2 lines
Is splice()ing between a tty and a pipe worth supporting at all? Not a big deal for my use case at least, but it used to work.
Is it even strictly related to the tty?
I was just now looking into why my cgit/fcgi/nginx setup no longer works, and the reason is getting
-EINVALfromsendfile()when the input is a file and the output is apipe().
Yes, pipes do not support ->splice_write currenly. I think just wiring
up iter_file_splice_write would work. Al?
Quoted 17 lines
So I wrote a simple test program (below) and that errors out on kernel 5.10.4, while it works fine on the 5.9.16 I currently have. Haven't tried reverting anything yet, but now that I haev a test program it should be simple to even bisect.
johannes
#include <
unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/sendfile.h>
#include <stdio.h>
#include <assert.h>int
main(int argc, char **argv)
Quoted 17 lines
{
int in = open(argv[0], O_RDONLY);
int p[2], out;
off_t off = 0;
int err;
assert(in >= 0);
assert(pipe(p) >= 0);
out = p[1];
err = sendfile(out, in, &off, 1024);
if (err < 0)
perror("sendfile");
assert(err == 1024);
return 0;
}---end quoted text---
Quoted 11 lines · Christoph Hellwig
Quoted 10 lines · Johannes Berg
Quoted 9 lines
For my case, I attempted to instead implement
splice_writeandsplice_readintty_fops; I managed to getsplice_writeworking callingld->ops->write, butsplice_readis not so simple because thetty_ldisc_opsread method expects a userspace buffer. So I cannot see how to implement this without either (a) usingset_fs, or (b) implementing iter ops on all line disciplines.Is splice()ing between a tty and a pipe worth supporting at all? Not a big deal for my use case at least, but it used to work.
Quoted 7 lines
Quoted 5 lines
Is it even strictly related to the tty?
I was just now looking into why my cgit/fcgi/nginx setup no longer works, and the reason is getting
-EINVALfromsendfile()when the input is a file and the output is apipe().Yes, pipes do not support ->splice_write currenly.
Well, it clearly used to work :-)
Quoted 2 lines
I think just wiring
upiter_file_splice_writewould work. Al?
Seems to work for the simple test case that I had, at least:
fs/pipe.c +1 -0
@@ -1206,6 +1206,7 @@ const struct file_operations pipefifo_fops = { .unlocked_ioctl = pipe_ioctl, .release = pipe_release, .fasync = pipe_fasync, + .splice_write = iter_file_splice_write, };/*
johannes
Quoted 4 lines · Johannes Berg
Quoted 2 lines
I think just wiring up
iter_file_splice_writewould work. Al?Seems to work for the simple test case that I had, at least:
Mind sending me a signed-off patch for this?
Yeah, I know it's a trivial one-liner, but I much prefer having an author with a patch and a sign-off to just doing it personally and reaping all that glory for it..
Linus
Quoted 5 lines · Linus Torvalds
Quoted 3 lines · Johannes Berg
Quoted 2 lines
I think just wiring up
iter_file_splice_writewould work. Al?
Quoted 7 lines
Quoted 1 line
Seems to work for the simple test case that I had, at least:
Mind sending me a signed-off patch for this?
Yeah, I know it's a trivial one-liner, but I much prefer having an author with a patch and a sign-off to just doing it personally and reaping all that glory for it..
IMO it's a wrong way to handle that. Look: do_sendfile() callsdo_splice_direct(), which calls splice_direct_to_actor(). There we allocate an internal pipe and go through "feed from source into that pipe, then shove what's there into destination". Which is insane for the case when destination (or source, for that matter) is a pipe itself.
do_splice_direct() does something that do_splice() won't - it handles non-pipe to non-pipe case. Which is how sendfile(2) is normally used, of course.
I'll look into that in more details, but IMO bothering with internal pipe is just plain wrong for those cases.
Quoted 4 lines · Al Viro
do_splice_direct()does something thatdo_splice()won't - it handles non-pipe to non-pipe case. Which is how sendfile(2) is normally used, of course.
Yeah, I agree, it's better if we do the pipe case specially, but if it's too painful for a stable backport I'll certainly take the one-liner..
Btw, your email setup is broken. Your emails now have a "From:" line like this:
From: Al Viro <viro@zeniv-ca>
and that is not a valid email address.
Linus
Al Viro
maintainerQuoted 10 lines · Linus Torvalds
Quoted 4 lines · Al Viro
do_splice_direct()does something thatdo_splice()won't - it handles non-pipe to non-pipe case. Which is how sendfile(2) is normally used, of course.Yeah, I agree, it's better if we do the pipe case specially, but if it's too painful for a stable backport I'll certainly take the one-liner..
Quoted 5 lines
Btw, your email setup is broken. Your emails now have a "From:" line like this:
From: Al Viro <viro@zeniv-ca>
and that is not a valid email address.
Just noticed, should be fixed now...
Al,
coming back to this because rc5 is imminent..
Quoted 7 lines · Al Viro
do_splice_direct()does something thatdo_splice()won't - it handles non-pipe to non-pipe case. Which is how sendfile(2) is normally used, of course.I'll look into that in more details, but IMO bothering with internal pipe is just plain wrong for those cases.
You clearly thought about this, with the emails about odd error cases, but I get the feeling that for fixing the current "you can't sendfile() to a pipe" regression (including stable) we should do the one-liner. No?
I agree that it would be better fixed by just having sendfile() basically turn into splice() for the pipe target case, but I haven't seen any patches from you, so I assume it wasn't 100% trivial.
Hmm?
Linus
PATCH 1/1 fs/pipe: allow sendfile() to pipe again
After commit 36e2c7421f02 ("fs: don't allow splice read/write without explicit ops") sendfile() could no longer send data from a real file to a pipe, breaking for example certain cgit setups (e.g. when running behind fcgiwrap), because in this case cgit will try to do exactly this: sendfile() to a pipe.
Fix this by using iter_file_splice_write for the splice_write method of pipes, as suggested by Christoph.
---
Mostly for the record, since it looks like we'll have a proper fix without another intermediate pipe. However, this fixes the regression for now.
--- fs/pipe.c | 1 + 1 file changed, 1 insertion(+)
fs/pipe.c +1 -0
@@ -1206,6 +1206,7 @@ const struct file_operations pipefifo_fops = { .unlocked_ioctl = pipe_ioctl, .release = pipe_release, .fasync = pipe_fasync, + .splice_write = iter_file_splice_write, };/*
Signature · 1 line
2.26.2
Re: [PATCH] fs/pipe: allow sendfile() to pipe again
Quoted 4 lines · Johannes Berg
Mostly for the record, since it looks like we'll have a proper fix without another intermediate pipe. However, this fixes the regression for now.
I've applied this anyway.
We'll see how big Al's proper fix ends up being, and depending on that this one-liner might just be the right thing for stable anyway.
And if Al's patch ends up being small, simple and clean, this one-liner won't matter, but it's not like there's any real downside to it.
Linus
Re: [PATCH] fs/pipe: allow sendfile() to pipe again
In reply to Johannes BergLooks good,
Al Viro
maintainerQuoted 17 lines · Linus Torvalds
Al,
coming back to this because rc5 is imminent..Quoted 7 lines · Al Viro
do_splice_direct()does something thatdo_splice()won't - it handles non-pipe to non-pipe case. Which is how sendfile(2) is normally used, of course.I'll look into that in more details, but IMO bothering with internal pipe is just plain wrong for those cases.
You clearly thought about this, with the emails about odd error cases, but I get the feeling that for fixing the current "you can't
sendfile()to a pipe" regression (including stable) we should do the one-liner. No?
Quoted 5 lines
I agree that it would be better fixed by just having
sendfile()basically turn intosplice()for the pipe target case, but I haven't seen any patches from you, so I assume it wasn't 100% trivial.Hmm?
Just to make clear - sendfile() regular-to-pipe is *not* the same issue as splice to/from tty. The latter needs ->splice_read() and ->splice_write() in tty_fops; the former can be dealt with by teaching do_sendifile() to use the guts of do_splice() in case when in or out happens to be a pipe (with some rearrangement of checks) instead of bothering with do_splice_direct().
The only commonality between these two is that both got broken by the same patch series. Johannes' patch is an attempt to deal with regular-to-pipe sendfile(2), and it's not a good way to handle that. Neither it, nor the variant I proposed would do a damn thing for tty (and sendfile(2) never worked for source other than regular or block anyway). FWIW, the real check in do_splice_direct() should be "has FMODE_PREAD", regardless of the position we are asking to read from - do_splice_direct() is basically
while left to copy
splice_read from in to internal pipe splice_write from internal pipe to out
and if splice_write ends up with short copy, we advance the position by the amount written and discard everything left in the internal pipe. Using it for something non-seekable would end up with data silently lost on short copy.
Note that decision against splice(2) between non-pipes appears to have been deliberate and unless Jens (and mingo?) decide they are OK with that change, I'm *not* adding that functionality to do_splice().
Anyway, the series is in vfs.git #work.sendfile, 5.11-rc1-based
Shortlog:
Al Viro (3):
do_splice_to(): move the logics for limiting the read length in take the guts of file-to-pipe splice into a helper function teach sendfile(2) to handle send-to-pipe directly
Diffstat:
fs/internal.h | 9 +++++++++ fs/read_write.c | 19 +++++++++++++------ fs/splice.c | 42 +++++++++++++++++++++++------------------- 3 files changed, 45 insertions(+), 25 deletions(-)
Individual patches in followups; first two are equivalent transformations (fairly obvious cleanup and taking a part of do_splice() into a helper), while the third one makes do_sendfile() use that new helper for file-to-pipe case.
Al Viro
maintainerPATCH 1/3 do_splice_to(): move the logics for limiting the read length in
Both callers have the identical logics limiting the amount of data we try to read into pipe - no more than would fit into that pipe. Move that into do_splice_to() itself.
--- fs/splice.c | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-)
fs/splice.c +7 -14
@@ -771,11 +771,16 @@ static long do_splice_to(struct file *in, loff_t *ppos, struct pipe_inode_info *pipe, size_t len, unsigned int flags) { + unsigned int p_space;int ret;if (unlikely (!(in->f_mode &FMODE_READ )))return -EBADF ; +/* Don't try to read more the pipe has space for. */ + p_space = pipe->max_usage -pipe_occupancy (pipe->head, pipe->tail); + len =min_t (size_t, len, p_space <<PAGE_SHIFT ); + ret =rw_verify_area (READ , in, ppos, len);if (unlikely (ret <0 ))return ret;
@@ -856,15 +861,10 @@ ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd,WARN_ON_ONCE (!pipe_empty (pipe->head, pipe->tail));while (len) { -unsigned int p_space;size_t read_len;loff_t pos = sd->pos, prev_pos = pos; -/* Don't try to read more the pipe has space for. */ - p_space = pipe->max_usage - -pipe_occupancy (pipe->head, pipe->tail); - read_len =min_t (size_t, len, p_space <<PAGE_SHIFT ); - ret =do_splice_to (in, &pos, pipe, read_len, flags); + ret =do_splice_to (in, &pos, pipe, len, flags);if (unlikely (ret <=0 )) goto out_release;
@@ -1083,15 +1083,8 @@ long do_splice(struct file *in, loff_t *off_in, struct file *out,pipe_lock (opipe); ret =wait_for_space (opipe, flags); -if (!ret) { -unsigned int p_space; - -/* Don't try to read more the pipe has space for. */ - p_space = opipe->max_usage -pipe_occupancy (opipe->head, opipe->tail); - len =min_t (size_t, len, p_space <<PAGE_SHIFT ); - +if (!ret) ret =do_splice_to (in, &offset, opipe, len, flags); - }pipe_unlock (opipe);if (ret >0 )wakeup_pipe_readers (opipe);
Signature · 1 line
2.11.0
Al Viro
maintainerPATCH 2/3 take the guts of file-to-pipe splice into a helper function
In reply to Al Viro--- fs/splice.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-)
fs/splice.c +18 -7
@@ -1002,6 +1002,23 @@ static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,struct pipe_inode_info *opipe, size_t len, unsigned int flags); +static long splice_file_to_pipe (struct file *in, +struct pipe_inode_info *opipe, +loff_t *offset, +size_t len,unsigned int flags) +{ +long ret; + +pipe_lock (opipe); + ret =wait_for_space (opipe, flags); +if (!ret) + ret =do_splice_to (in, offset, opipe, len, flags); +pipe_unlock (opipe); +if (ret >0 ) +wakeup_pipe_readers (opipe); +return ret; +} +/* * Determine where to splice to/from. */
@@ -1081,13 +1098,7 @@ long do_splice(struct file *in, loff_t *off_in, struct file *out,if (out->f_flags &O_NONBLOCK ) flags |=SPLICE_F_NONBLOCK ; -pipe_lock (opipe); - ret =wait_for_space (opipe, flags); -if (!ret) - ret =do_splice_to (in, &offset, opipe, len, flags); -pipe_unlock (opipe); -if (ret >0 ) -wakeup_pipe_readers (opipe); + ret =splice_file_to_pipe (in, opipe, &offset, len, flags);if (!off_in) in->f_pos = offset;else
Signature · 1 line
2.11.0
Al Viro
maintainerPATCH 3/3 teach sendfile(2) to handle send-to-pipe directly
In reply to Al Virono point going through the intermediate pipe
--- fs/internal.h | 9 +++++++++ fs/read_write.c | 19 +++++++++++++------ fs/splice.c | 2 +- 3 files changed, 23 insertions(+), 7 deletions(-)
fs/internal.h +9 -0
@@ -15,6 +15,7 @@ struct mount;struct shrink_control ;struct fs_context ;struct user_namespace ; +struct pipe_inode_info ;/* * block_dev.c
@@ -193,3 +194,11 @@ int sb_init_dio_done_wq(struct super_block *sb);*/ int do_statx (int dfd,const char __user *filename,unsigned flags,unsigned int mask,struct statx __user *buffer); + +/* +* fs/splice.c: +*/ +long splice_file_to_pipe (struct file *in, +struct pipe_inode_info *opipe, +loff_t *offset, +size_t len,unsigned int flags);
fs/read_write.c +13 -6
@@ -1188,6 +1188,7 @@ static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos, {struct fd in, out;struct inode *in_inode, *out_inode; +struct pipe_inode_info *opipe;loff_t pos;loff_t out_pos;ssize_t retval;
@@ -1228,9 +1229,6 @@ static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos, in_inode =file_inode (in.file); out_inode =file_inode (out.file); out_pos = out.file->f_pos; - retval =rw_verify_area (WRITE , out.file, &out_pos, count); -if (retval <0 ) - goto fput_out;if (!max) max =min (in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
@@ -1253,9 +1251,18 @@ static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,if (in.file->f_flags &O_NONBLOCK ) fl =SPLICE_F_NONBLOCK ;#endif -file_start_write (out.file); - retval =do_splice_direct (in.file, &pos, out.file, &out_pos, count, fl); -file_end_write (out.file); + opipe =get_pipe_info (out.file, true); +if (!opipe) { + retval =rw_verify_area (WRITE , out.file, &out_pos, count); +if (retval <0 ) + goto fput_out; +file_start_write (out.file); + retval =do_splice_direct (in.file, &pos, out.file, &out_pos, + count, fl); +file_end_write (out.file); + }else { + retval =splice_file_to_pipe (in.file, opipe, &pos, count, fl); + }if (retval >0 ) {add_rchar (current, retval);
fs/splice.c +1 -1
@@ -1002,7 +1002,7 @@ static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,struct pipe_inode_info *opipe, size_t len, unsigned int flags); -static long splice_file_to_pipe (struct file *in, +long splice_file_to_pipe (struct file *in,struct pipe_inode_info *opipe,loff_t *offset,size_t len,unsigned int flags)
Signature · 1 line
2.11.0
Re: [PATCH 3/3] teach sendfile(2) to handle send-to-pipe directly
Quoted 2 lines · Al Viro
no point going through the intermediate pipe
Ok, this series looks fine to me.
Al - do you want me to apply this as patches for 5.11 (and I'll revert the then unnecessary one-liner), or should I expect a pull request or what?
Linus
Al Viro
maintainerRe: [PATCH 3/3] teach sendfile(2) to handle send-to-pipe directly
Quoted 9 lines · Linus Torvalds
Quoted 2 lines · Al Viro
no point going through the intermediate pipe
Ok, this series looks fine to me.
Al - do you want me to apply this as patches for 5.11 (and I'll revert the then unnecessary one-liner), or should I expect a pull request or what?
I'm going to put it into #for-next for a few days, then send a pull request your way (assuming no problems show up, obviously).
Quoted 9 lines · Al Viro
Quoted 4 lines · Linus Torvalds
I agree that it would be better fixed by just having
sendfile()basically turn intosplice()for the pipe target case, but I haven't seen any patches from you, so I assume it wasn't 100% trivial.Just to make clear -
sendfile()regular-to-pipe is *not* the same issue as splice to/from tty.
That's not what I meant.
sendfile() to a pipe is basically the same thing as splice() to a pipe.
Except I think it might have different looping behavior. And as you noted earlier, the error returns may be randomly different.
Linus
Al Viro
maintainerQuoted 11 lines · Linus Torvalds
Quoted 9 lines · Al Viro
Quoted 4 lines · Linus Torvalds
I agree that it would be better fixed by just having
sendfile()basically turn intosplice()for the pipe target case, but I haven't seen any patches from you, so I assume it wasn't 100% trivial.Just to make clear -
sendfile()regular-to-pipe is *not* the same issue as splice to/from tty.
Quoted 6 lines
That's not what I meant.
sendfile()to a pipe is basically the same thing assplice()to a pipe.Except I think it might have different looping behavior. And as you noted earlier, the error returns may be randomly different.
Sure. What I'm saying is that there'd been two different regressions discussed in that thread, and the one dealt with by this patch series is not the one brought up in the original posting. IOW, it's an alternative not to "let's add ->splice_{read,write}() for tty" stuff but to "let's add ->splice_write() for pipes" one-liner.
I do not doubt that you know that. But the thread had been confusing enough (hell, the sendfile-related subthread has kept the original subject explicitly refering to tty), so I wanted to avoid any confusion for somebody looking through it a year or two down the road.
Quoted 11 lines · Christoph Hellwig
Quoted 10 lines · Johannes Berg
Quoted 9 lines
For my case, I attempted to instead implement
splice_writeandsplice_readintty_fops; I managed to getsplice_writeworking callingld->ops->write, butsplice_readis not so simple because thetty_ldisc_opsread method expects a userspace buffer. So I cannot see how to implement this without either (a) usingset_fs, or (b) implementing iter ops on all line disciplines.Is splice()ing between a tty and a pipe worth supporting at all? Not a big deal for my use case at least, but it used to work.
Quoted 8 lines
Quoted 5 lines
Is it even strictly related to the tty?
I was just now looking into why my cgit/fcgi/nginx setup no longer works, and the reason is getting
-EINVALfromsendfile()when the input is a file and the output is apipe().Yes, pipes do not support ->splice_write currenly. I think just wiring
upiter_file_splice_writewould work. Al?
I'd rather have sendfile(2) do what splice(2) does and handle pipes directly. Let me take a look,,,
Quoted 3 lines · Al Viro
I'd rather have sendfile(2) do what splice(2) does and handle pipes directly. Let me take a look,,,
It's not technically just the sendfile() thing. Some things do sendfile "internally" (ie they use do_splice_direct()).
Although maybe they always happen just on non-pipe destinations (ie file-to-file copy). I didn't check.
But particularly if it can be handled in do_splice_direct(), that would be a good thing.
Linus
Quoted 5 lines · Linus Torvalds
Quoted 3 lines · Al Viro
I'd rather have sendfile(2) do what splice(2) does and handle pipes directly. Let me take a look,,,
Quoted 8 lines
It's not technically just the
sendfile()thing. Some things do sendfile "internally" (ie they usedo_splice_direct()).Although maybe they always happen just on non-pipe destinations (ie file-to-file copy). I didn't check.
But particularly if it can be handled in
do_splice_direct(), that would be a good thing.
FWIW, it might make sense to merge do_splice_direct() and do_splice(); interfaces are very similar and do_splice() is basically
if both are pipes .... else if input is pipe .... else if output is pipe .... else return -EINVAL
with do_splice_direct() being fairly close to the missing case.
Al Viro
maintainerQuoted 25 lines · Al Viro
Quoted 5 lines · Linus Torvalds
Quoted 3 lines · Al Viro
I'd rather have sendfile(2) do what splice(2) does and handle pipes directly. Let me take a look,,,
Quoted 8 lines
It's not technically just the
sendfile()thing. Some things do sendfile "internally" (ie they usedo_splice_direct()).Although maybe they always happen just on non-pipe destinations (ie file-to-file copy). I didn't check.
But particularly if it can be handled in
do_splice_direct(), that would be a good thing.FWIW, it might make sense to merge
do_splice_direct()anddo_splice(); interfaces are very similar anddo_splice()is basicallyif both are pipes .... else if input is pipe .... else if output is pipe .... else return -EINVAL
Quoted 1 line
with
do_splice_direct()being fairly close to the missing case.
Yecchhh... The situation with checks is interesting.
do_splice():
in needs FMODE_READ, out - FMODE_WRITE [EBADF]
if in is a pipe, off_in must be NULL. [ESPIPE]
if out is a pipe, off_out must be NULL. [ESPIPE]
if in is not a pipe
non-NULL off_in is allowed only with FMODE_PREAD [EINVAL] rw_verify_area done on in
if out is not a pipe
non-NULL off_out is allowed only with FMODE_PWRITE [EINVAL] no O_APPEND on out [EINVAL]
rw_verify_area done on out
either in or out must be a pipe [EINVAL]
do_splice_direct():
out needs FMODE_WRITE [EBADF]
rw_verify_area done on out
no O_APPEND on out [EINVAL]
Callers:
__do_splice() -> do_splice():
if in is a pipe, off_in must be NULL. [ESPIPE, duplicate] if out is a pipe, off_out must be NULL. [ESPIPE, duplicate]
io_uring io_splice() -> do_splice():
no extra checks
do_sendfile() -> do_splice_direct():
in needs FMODE_READ [EBADF]
out needs FMODE_WRITE [EBADF, duplicate] non-NULL off_in is allowed only with FMODE_PREAD [EINVAL] for out we act as splice with NULL off_out (== use ->f_pos) rw_verify_area done on in
rw_verify_area done on out [duplicate]
vfs_copy_file_range() -> do_copy_file_range() ->
-> generic_copy_file_range() or __ceph_copy_file_range() -> -> do_splice_direct():
both in and out must be regular [EINVAL or EISDIR] in needs FMODE_READ [EBADF]
out needs FMODE_WRITE [EBADF]
no O_APPEND on out [EBADF]
on immutable for out [EPERM]
rw_verify_area done on in
rw_verify_area done on out [duplicate] FMODE_P{READ,WRITE} is ignored. I wonder what happens if
somebody starts playing with copy_file_range(2) on e.g. procfs or sysfs...
ovl_copy_up_date() -> do_splice_direct():
both in and out are hopefully regular - I'm not sure that copyup
logics is sufficiently protected, TBH.
FMODE_READ on in and FMODE_WRITE on out are guaranteed lack of O_APPEND on out is guaranteed FMODE_P{READ,WRITE} is completely ignored. Should be OK, modulo
the same issues with protection against manipulation of layers.
Checks in vfs_copy_file_range() need to remain there - do_splice_direct() is not guaranteed downstream of that.
AFAICS, we have the following:
in must have FMODE_READ
out must have FMODE_WRITE
no O_APPEND on out, unless a pipe[1]
for non-pipe out we do rw_verify_area
for non-pipe in we do rw_verify_area
no offsets for pipes
The lack of offsets for pipes should've been "not without FMODE_P{READ,WRITE}", but that's not entirely consistent - we have splice(2) fail with EINVAL in case of non-NULL off_in on a file that doesn't allow pread(), except that when this file happens to be a pipe (which obviously doesn't allow pread()) we fail with ESPIPE instead. Note that for pread(2)/pwrite(2)/sendfile(2) we fail with consistent ESPIPE in all such cases.
We also do not check FMODE_PREAD/FMODE_PWRITE for copy_file_range()/copyup. Which is probably wrong.
Another inconsistency is that rw_verify_area() in copyup is done for output (once for each chunk), but not for input. The tricky part here is that there's an LSM hook in the end of rw_verify_area()...
Can we turn "not a pipe, but lacks FMODE_PREAD" error value for splice(2) from EINVAL to ESPIPE? Would make the logics easier to consolidate... Right now the checks are spread over many layers of several call chains and rather hard to follow.
[1] yes, it is possible to have O_APPEND opened pipes - open a FIFO with O_APPEND and you've got it. We are not quite consistent in handling those - sendfile() to such is rejected, splice() is not.
Al Viro
maintainerQuoted 3 lines · Al Viro
[1] yes, it is possible to have
O_APPENDopened pipes - open a FIFO withO_APPENDand you've got it. We are not quite consistent in handling those -sendfile()to such is rejected,splice()is not.
BTW, according to manpages of splice(2) and sendfile(2), we have
EINVAL [snip] target file is opened in append mode [snip]
and
EINVAL out_fd has the O_APPEND flag set. This is not currently supported by sendfile().
However, splice(2) to FIFO opened with O_APPEND works just fine. So it doesn't match the manpage either.
Why do we care about O_APPEND on anything without FMODE_PWRITE (including pipes), anyway? All writes there ignore position, after all...
Quoted 3 lines · Al Viro
Why do we care about
O_APPENDon anything withoutFMODE_PWRITE(including pipes), anyway? All writes there ignore position, after all...
We shouldn't care.
Also, I think we should try to move away from FMODE_PWRITE/PREAD entirely, and use FMODE_STREAM as the primary "this thing doesn't have a position at all".
That's what gets rid of all the f_pos locking etc after all. The FMODE_PWRITE/PREAD flags are I think legacy (although we do seem to have the seq_file case that normally allows position on reads, but not on writes, so we may need to keep all three bits).
Anyway, I think that with FMODE_STREAM, O_APPEND definitely should be a no-op.
Linus
From: Linus Torvalds
Quoted 12 lines
Sent: 20 January 2021 19:27
Quoted 3 lines · Al Viro
Why do we care about
O_APPENDon anything withoutFMODE_PWRITE(including pipes), anyway? All writes there ignore position, after all...We shouldn't care.
Also, I think we should try to move away from FMODE_PWRITE/PREAD entirely, and use
FMODE_STREAMas the primary "this thing doesn't have a position at all".
Quoted 6 lines
That's what gets rid of all the
f_poslocking etc after all. The FMODE_PWRITE/PREAD flags are I think legacy (although we do seem to have theseq_filecase that normally allows position on reads, but not on writes, so we may need to keep all three bits).Anyway, I think that with
FMODE_STREAM,O_APPENDdefinitely should be a no-op.
I also wonder if pread/pwrite with offset == 0 should be valid on things where the offset makes no sense.
I'm rather surprised the offset isn't just silently ignored for devices where seeking is non-sensical.
You might want to error it for mag tapes, but not pipes, ttys, sockets etc.
I really can't remember what SYSV, Solaris or NetBSD do.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Al Viro
maintainerQuoted 9 lines · David Laight
I also wonder if pread/pwrite with offset == 0 should be valid on things where the offset makes no sense.
I'm rather surprised the offset isn't just silently ignored for devices where seeking is non-sensical.
You might want to error it for mag tapes, but not pipes, ttys, sockets etc.I really can't remember what SYSV, Solaris or NetBSD do.
... nor can you be arsed to RTFPOSIX. Why am I not surprised?
In https://pubs.opengroup.org/onlinepubs/9699919799/functions/write.html
(located by arcane action known as googling for pwrite POSIX):
==============================
The pwrite() function shall fail if:
[EINVAL]
The file is a regular file or block special file, and the offset argument is negative.
The file offset shall remain unchanged.
[ESPIPE]
The file is incapable of seeking.
==============================
Al Viro
maintainerQuoted 7 lines · Linus Torvalds
Quoted 3 lines · Al Viro
Why do we care about
O_APPENDon anything withoutFMODE_PWRITE(including pipes), anyway? All writes there ignore position, after all...We shouldn't care.
Quoted 8 lines
Also, I think we should try to move away from FMODE_PWRITE/PREAD entirely, and use
FMODE_STREAMas the primary "this thing doesn't have a position at all".That's what gets rid of all the
f_poslocking etc after all. The FMODE_PWRITE/PREAD flags are I think legacy (although we do seem to have theseq_filecase that normally allows position on reads, but not on writes, so we may need to keep all three bits).
Umm... Why do we clear FMODE_PWRITE there, anyway? It came in commit 915a29ec1c5e34283a6231af1036114e4d612cb0
Author: Linus Torvalds <[email protected]> Date: Sat Aug 7 02:08:23 2004 -0700
Add pread/pwrite support bits to match the lseek bit.
This also removes the ESPIPE logic from pipes and seq_files, since the VFS layer now supports it.
with seq_read() losing the special-cased pread prevention and seq_open() getting a ban on both pread and pwrite. With pread() support added in 2009, and (pointless) pwrite prohibition left in place.
Quoted 2 lines · Al Viro
Umm... Why do we clear
FMODE_PWRITEthere [seq_open- ed], anyway?
I think it's pointless and historical, and comes from "several /proc files supported the simple single-write model, nothing ever supported moving around and writing".
The seq_file stuff was always about reading, and then the writing part was generally random special-case hacks on the side.
So I think that "clear PWRITE" thing is to make sure we get sane error cases if somebody tries something funny, knowing that none of the hacky stuff support it.
And then the very special kernfs thing adds it back in, because it does in fact allow seeking writes.
Linus
Al Viro
maintainerQuoted 11 lines · Linus Torvalds
Quoted 2 lines · Al Viro
Umm... Why do we clear
FMODE_PWRITEthere [seq_open- ed], anyway?I think it's pointless and historical, and comes from "several /proc files supported the simple single-write model, nothing ever supported moving around and writing".
The
seq_filestuff was always about reading, and then the writing part was generally random special-case hacks on the side.
Quoted 6 lines
So I think that "clear PWRITE" thing is to make sure we get sane error cases if somebody tries something funny, knowing that none of the hacky stuff support it.
And then the very special kernfs thing adds it back in, because it does in fact allow seeking writes.
OK... I wonder how many debugfs writable files allow pwrite() with BS results...
Anyway, possibly more interesting question is why do we care about O_APPEND at all - why not treat it the same way we do in write()? Hell, even our pwrite() just goes ahead and writes to the end of file, whatever position it had been given. Yes, for pwrite(2) that's contrary to POSIX, but it's probably cast in stone by that point anyway...
Looking through the instances of ->splice_write(), iter_file_splice_write() will end up appending the data to EOF and so will gfs2_file_splice_write(). For sockets (generic_splice_sendpage()) we definitely don't give a toss about O_APPEND (F_SETFL can set it, so that case is possible to hit), ditto for splice_write_null() and port_fops_splice_write(). Which leaves only one instance: fuse_dev_splice_write(), which also should ignore O_APPEND (IMO fuse_dev_open() ought to call nonseekable_open() anyway).
So... why do we ban O_APPEND on destination for splice() or for sendfile()? AFAICS, if we simply remove that test, we'll end up with write going to the end of O_APPEND file. same as for write()/pwrite().
Comments?
Quoted 3 lines · Al Viro
OK... I wonder how many debugfs writable files allow
pwrite()with BS results...
I hope some of them check for "pos == 0" when they start parsing integers.
But honestly, I don't think it's a big deal. We've had these things that just basically assume that whenever you write, the offset just doesn't matter at all, and as long as some number comes in one single write call, we accept it.
Because even if you end up doing something like just
echo $SOMETHING > /sys/xyz/abc
and that "$SOMETHING" could be done multiple writes, in practice it all works out just fine and it never really is. You almost have to try to screw up with something like
(echo -n 3; echo -n 4) > /sys/xyz/abc
to actually see two writes of "3" and "4" instead of one write with "34". And honestly, if somebody does something like that, do we really care? They might get 3, they might get 4, and they might get 34. They get what they deserve.
Quoted 2 lines
Anyway, possibly more interesting question is why do we care about
O_APPENDat all - why not treat it the same way we do inwrite()?
The whole point of O_APPEND is that the position shouldn't matter.
And the whole point of "pwrite()" is that you specify a position.
So the two just do not go together - although we may have legacy issues, of course.
In contrast, the whole point of just a plain "write()" is that the position is the "current file position", with O_APPEND is just a special rule for what the current position for a write is.
Now, splice() is able to do *both* write() and pwrite(), because unlike pwrite() it doesn't take a "pos" argument, it takes a _pointer_ to pos. So with a NULL pointer, it's like read/write, and with a non-NULL pointer it is like pread/pwrite.
So I do think that "splice with non-NULL off_out and O_APPEND" should cause an error in general.
That said, we probabyl have legacy behavior with splice and pipes in particular, and that legacy behavior would override any "this is conceptually the sane model".
Quoted 1 line
So... why do we ban
O_APPENDon destination forsplice()or forsendfile()?
sendfile() shouldn't be an issue. The offset pointer for sendfile is for the _source_, not the destination.
For splice(), I do think that O_APPEND and a position pointer don't make sense as a combination, although if we do allow it for regular file pwrite() (I didn't check), maybe we could allow it for splice() too just to be erqually inconsistent.
Honestly, I don't think it's a huge deal. O_APPEND isn't that interesting, but I do hope that if we allow O_APPEND and a file position, then O_APPEND always overrides it.
Linus
Al Viro
maintainerQuoted 6 lines · Linus Torvalds
The whole point of
O_APPENDis that the position shouldn't matter.And the whole point of "
pwrite()" is that you specify a position.So the two just do not go together - although we may have legacy issues, of course.
Our pwrite(2):
BUGS
POSIX requires that opening a file with the O_APPEND flag should have no effect on the location at which pwrite() writes data. However, on Linux, if a file is opened with O_APPEND, pwrite() appends data to the end of the file, regardless of the value of offset.
POSIX pwrite(2):
The pwrite() function shall be equivalent to write(), except that it writes into a given position and does not change the file offset (regardless of whether O_APPEND is set). The first three arguments to pwrite() are the same as write() with the addition of a fourth argument offset for the desired position inside the file. An attempt to perform a pwrite() on a file that is incapable of seeking shall result in an error.
I don't believe that we could change behaviour of our pwrite(2) without breaking userland, even if we wanted to. It's been that way since 2.1.60 when pwrite() had been first introduced - 23 years ago is more than enough to have it cast in stone. We do allow pwrite(2) with O_APPEND and on such descriptors it acts like write(2) on the same.
Quoted 7 lines
Now,
splice()is able to do *both*write()andpwrite(), because unlikepwrite()it doesn't take a "pos" argument, it takes a _pointer_ to pos. So with a NULL pointer, it's like read/write, and with a non-NULL pointer it is like pread/pwrite.So I do think that "splice with non-NULL
off_outandO_APPEND" should cause an error in general.
splice() triggers an error for seekable destination with O_APPEND and with NULL off_out. Same for splice() to socket with
fcntl(sock_fd, F_SETFL, O_APPEND);
done first.
Quoted 3 lines
Honestly, I don't think it's a huge deal.
O_APPENDisn't that interesting, but I do hope that if we allowO_APPENDand a file position, thenO_APPENDalways overrides it.
It does, when it is allowed.
Quoted 3 lines · Al Viro
splice()triggers an error for seekable destination withO_APPENDand with NULLoff_out.
Ok, that's just broken.
Quoted 3 lines
Same for
splice()to socket withfcntl(sock_fd, F_SETFL, O_APPEND);done first.
Same.
As long as you don't pass a position pointer, I think both should just work.
Not that I imagine it matters for a lot of people..
Linus
Linus
Quoted 11 lines · Linus Torvalds
Quoted 3 lines · Al Viro
splice()triggers an error for seekable destination withO_APPENDand with NULLoff_out.Ok, that's just broken.
Quoted 3 lines
Same for
splice()to socket withfcntl(sock_fd, F_SETFL, O_APPEND);done first.
Quoted 5 lines
Same.
As long as you don't pass a position pointer, I think both should just work.
Not that I imagine it matters for a lot of people..
I think that most users of splice() on sockets got used to falling back to recv/send on splice failure due to various cases not being supported historically (UNIX family sockets immediately come to my mind but I seem to remember other combinations). Thus I guess that most users of splice() detect that it doesn't work either due to lower than expected performance or while running strace.
Willy
Quoted 4 lines · Willy Tarreau
I think that most users of
splice()on sockets got used to falling back to recv/send on splice failure due to various cases not being supported historically (UNIX family sockets immediately come to my mind but I seem to remember other combinations).
Note, however, that I got here because cgit, if using sendfile(), does *not* fall back if it fails (and thus my git tree view is currently down because I haven't downgraded the kernel so far). That may not be common for splice() though.
johannes
From: Linus Torvalds
Quoted 21 lines
Sent: 21 January 2021 01:04
Quoted 3 lines · Al Viro
OK... I wonder how many debugfs writable files allow
pwrite()with BS results...I hope some of them check for "pos == 0" when they start parsing integers.
But honestly, I don't think it's a big deal. We've had these things that just basically assume that whenever you write, the offset just doesn't matter at all, and as long as some number comes in one single write call, we accept it.
Because even if you end up doing something like just
echo $SOMETHING >
/sys/xyz/abcand that "$SOMETHING" could be done multiple writes, in practice it all works out just fine and it never really is. You almost have to try to screw up with something like
Quoted 6 lines
(echo -n 3; echo -n 4) >
/sys/xyz/abcto actually see two writes of "3" and "4" instead of one write with "34". And honestly, if somebody does something like that, do we really care? They might get 3, they might get 4, and they might get 34. They get what they deserve.
Or worse:
echo abc >/sys/xyz/abc
echo x | dd bs=1 count=2 oseek=1 conv=notrunc of=/sys/xyz/abc
which (if I got the dd command right) would generate "axc" on a real file.
OTOH multiple short reads are quite likely.
Best not done on a counter...
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Quoted 5 lines · Oliver Giles
Commit
36e2c7421f02(fs: don't allow splice read/write without explicit ops) broke my userspace application which talks to an SSL VPN by splice()ing between "openssls_client" and "pppd". The latter operates over a pty, and since that commit there is no fallback for splice()ing between a pipe and a pty, or any tty for that matter.The above commit mentions switching them to the iter ops and using
generic_file_splice_read. IIUC, this would require implementing iter ops also on the line disciplines, which sounds pretty disruptive.For my case, I attempted to instead implement
splice_writeandsplice_readintty_fops; I managed to getsplice_writeworking callingld->ops->write, butsplice_readis not so simple because thetty_ldisc_opsread method expects a userspace buffer. So I cannot see how to implement this without either (a) usingset_fs, or (b) implementing iter ops on all line disciplines.
set_fs is gone for all the important platforms. So yes, you basically need to convert to iov_iter or have a huge splice_write parallel infrastucture.
Quoted 2 lines
Is splice()ing between a tty and a pipe worth supporting at all? Not a big deal for my use case at least, but it used to work.
Our normal policy is no regressions for exiting userspace. By that we'd have to fix it. Let me see if I can help you with this in any way.
Quoted 7 lines · Christoph Hellwig
Quoted 1 line · Oliver Giles
For my case, I attempted to instead implement
splice_writeandsplice_readintty_fops; I managed to getsplice_writeworking callingld->ops->write, butsplice_readis not so simple because thetty_ldisc_opsread method expects a userspace buffer. So I cannot see how to implement this without either (a) usingset_fs, or (b) implementing iter ops on all line disciplines.
set_fsis gone for all the important platforms. So yes, you basically need to convert toiov_iteror have a hugesplice_writeparallel infrastucture.
It might ok to try to limit it to just the pty cases and ldisc ops that need it, apparently in this case pty (and presumably just one or two line disciplines)
Of course, it probably would be really nice to try to convert tty_read() to use the same model that we have for tty_write(), and then make the ld->ops->read() function actually take a kernel buffer instead.
I wonder how painful that would be.
Linus
Quoted 7 lines · Linus Torvalds
Of course, it probably would be really nice to try to convert
tty_read()to use the same model that we have fortty_write(), and then make theld->ops->read()function actually take a kernel buffer instead.I wonder how painful that would be.
Oh, that seems _much_ less painful than I thought it would be.
This is a COMPLETELY BROKEN patch that builds cleanly for me. I say that it's completely broken for three reasons:
(a) that extra "int dummy" argument is there purely to force build
errors if some user hasn't been converted.
(b) I intentionally made the conversion truly stupid. It's not
"broken", but it needs cleanup for nasty variable names (ie things like me changing that already badly named "b" variable to "kbp" to again catch all users at build time)
(c) the buffer handling in tty_read() is actively broken. Things like
HDLC rely on getting a proper buffer that can hold the whole packet, and the max packet size is actually potentially quite big.
But the only real problem does seem to be HDLC, which has a default maximum frame size of 4k, but it can be set all the way up to 64kB as a module parameter.
So that tty_read() conversion in this patch is complete garbage, but I really just wrote this to see how many painful places there are. And there are not many.
The HDLC case could probably be done fairly well by
- have a small on-stack buffer for the "small read" case
- have a kmalloc() buffer that defaults to one page for bigger reads
- grow the kmalloc() buffer if the ->read function returns -EOVERFLOW
Comments?
NOTE! This does *not* do the actual splice_read() implementation. No, this literally is just the preparatory stage for that by starting the whole "make the tty ldisc read path use a kernel buffer instead".
Anybody want to play with this? I'd suggest keeping that "dummy" parameter around for a while - I did an allmodconfig build with this, but if there are any architecture-specific non-x86-64 cases I wouldn't have seen them.
Linus
Quoted 5 lines · Linus Torvalds
Anybody want to play with this? I'd suggest keeping that "dummy" parameter around for a while - I did an allmodconfig build with this, but if there are any architecture-specific non-x86-64 cases I wouldn't have seen them.
Not architecture-specific, but I did find by some grepping that I missed one line discipline driver: the Siemens R3964.
The reason I missed that is because it's been marked BROKEN in the Kconfig for almost two years, so it didn't show up in my allmodconfig coverage.
But the fact that I found it makes me a bit happier about my patch actually covering all the cases.
My grep exercise failed on the bluetooth hci_ldisc.c pattern, which I feel is because the bluetooth code used the wrong pattern to initialize the 'struct tty_ldisc_ops', so I fixed that up too.
So here's a slightly updated version of that patch, but apart from slightly better coverage - even if it's a driver that is disabled anyway - I'd like to point out that all my previous caveats still apply.
Linus
Quoted 5 lines · Linus Torvalds
So here's a slightly updated version of that patch, but apart from slightly better coverage - even if it's a driver that is disabled anyway - I'd like to point out that all my previous caveats still apply.
I have now booted that version to see that I didn't make any horribly obvious mistakes.
And I must have screwed something up. I can actually use the machine normally (I'm writing this running that kernel), but when I decided to test the actual virtual console (as opposed to the GUI terminal windows that use pty's), I can't even log in.
That *may* just be due to the inexcusably lazy and stupid "chunk things up into 32 byte pieces" I did. Most programs shouldn't care, tty's can return partial results anyway, but it's obviously a fairly fundamental and silly change.
But it might well be some other conversion bug of mine even if I tried to keep it fairly minimal and straight-forward.
So I suggest taking that patch with a lot of salt, and really treating it as a very rough starting point (which was the point of it - trying to actually boot it as-is was more of a "let's see if it survives at all" thing).
Linus
Quoted 3 lines · Linus Torvalds
But it might well be some other conversion bug of mine even if I tried to keep it fairly minimal and straight-forward.
Duh. I completely forgot to handle the canon_copy_from_read_buf() case. So ICANON mode was entirely scrogged and would just return -EFAULT.
That would do it. I'm surprised how well everything I did actually worked - because all my normal terminal apps (shell, editor etc) obviously end up not using icanon at all.
I'll have a third patch in a moment, but while it's ready I want to actually reboot and confirm it first.
Linus
Quoted 3 lines · Linus Torvalds
I'll have a third patch in a moment, but while it's ready I want to actually reboot and confirm it first.
Here. This is an actually usable and tested starting point for this all.
Again - it doesn't handle HDLC correctly, because it will chop all reads up into 32-byte chunks.
And some other apps that think they'll get whole lines might end up being confused too.
So it's not a "real" patch, but with improved buffer handling in tty_read(), I think this is actually quite close.
NOTE: due to security reasons (ie password data), we do want to clear that buffer after we've copied the data to user space. Again, not something I did in my patch, so it would be part of that "improved buffer handling"
Linus
Quoted 3 lines · Linus Torvalds
So it's not a "real" patch, but with improved buffer handling in
tty_read(), I think this is actually quite close.
Hmm.
I somehow ended up working on this all because it's a Monday, and I don't see a lot of pull requests early in the week.
And I think I have a solution for the HDLC "we may need to copy a packet that might be up to 64kB" issue, that isn't really all that ugly.
We can just iterate over a random "cookie" that the line discipline can use any way it wants to. In the case of n_hdlc, it can just put the 'rbuf' thing it has into that cookie, and then it can copy it all piece-meal until it is all used up. And if it runs out of space in the middle, it will return -EOVERFLOW, and we're all good.
The only other thing such a line discipline needs is the offset into the cookie, but the iterator has to maintain that anyway, so that's simple enough.
So here's a fourth patch for this thing today, this time with what I think is actually a working model for the buffer handling.
Other line disciplines *could* use the cookie if they want to. I didn't do any of that, though.
The normal n_tty line discipline, for example, could easily just loop over the data. It doesn't need an offset or that 'rbuf' pointer, but it still needs to know whether the call is the first one or not, because the first time the n_tty line discipline is called it may need to wait for a minimum number of characters or whatever the termios settings say - but obviously once it has waited for it once, it shouldn't wait for it again the next time around (only on the next actual full read()). IOW, it would be wrong if the termios said "wait for 5 characters", and then it saw 68 characters, copied the first 64, in the first iteration, and than saw "oh, now there are only 4 characters left so now I have to wait for a fifth".
So n_tty could use the cookie purely to see whether it's the first iteration or not, and it could just set the cookie to a random value (it always starts out as NULL) to just show what state it is in.
I did *NOT* do that, because it's not technically necessary - unlike the hdlc packet case, n_tty returning a partial result is not wrong per se even if we might have reasons to improve on it later.
What do people think about this?
Also, does anybody have any test-code for the HDLC case? I did find an interesting comment when doing a Debian code search:
/* Bloody hell... readv doesn't work with N_HDLC line discipline... GRR! */
and yes, this model would allow us to handle readv() properly for hdlc (and no, the old one did not, because it really wanted to see the whole packet in *one* user buffer).
But I have no idea if hdlc is even relevant any more, and if anybody really cares.
Anybody?
Linus
From 526258e18d13ebb113638d26eba18ca05a51dc2a Mon Sep 17 00:00:00 2001
From: Linus Torvalds <[email protected]>
Date: Mon, 18 Jan 2021 13:31:30 -0800
Subject: [PATCH] tty: convert tty_ldisc_ops 'read()' function to take a kernel
pointer
The tty line discipline .read() function was passed the final user pointer destination as an argument, which doesn't match the 'write()' function, and makes it very inconvenient to do a splice method for tty's.
This is a conversion to use a kernel buffer instead.
NOTE! It does this by passing the tty line discipline ->read() function an additional "cookie" to fill in, and an offset into the cookie data.
The line discipline can fill in the cookie data with its own private information, and then the reader will repeat the read until either the cookie is cleared or it runs out of data.
The only real user of this is N_HDLC, which can use this to handle big packets, even if the kernel buffer is smaller than the whole packet.
--- drivers/bluetooth/hci_ldisc.c | 34 +++++++-------- drivers/input/serio/serport.c | 4 +- drivers/net/ppp/ppp_async.c | 3 +- drivers/net/ppp/ppp_synctty.c | 3 +- drivers/tty/n_gsm.c | 3 +- drivers/tty/n_hdlc.c | 60 +++++++++++++++++-------- drivers/tty/n_null.c | 3 +- drivers/tty/n_r3964.c | 10 ++--- drivers/tty/n_tracerouter.c | 4 +- drivers/tty/n_tracesink.c | 4 +- drivers/tty/n_tty.c | 82 +++++++++++++++-------------------- drivers/tty/tty_io.c | 64 +++++++++++++++++++++++++-- include/linux/tty_ldisc.h | 3 +- net/nfc/nci/uart.c | 3 +- 14 files changed, 178 insertions(+), 102 deletions(-)
drivers/bluetooth/hci_ldisc.c +17 -17
@@ -802,7 +802,8 @@ static int hci_uart_tty_ioctl(struct tty_struct *tty, struct file *file,* We don't provide read/write/poll interface for user space. */ static ssize_t hci_uart_tty_read (struct tty_struct *tty,struct file *file, -unsigned char __user *buf,size_t nr) +unsigned char *buf,size_t nr, +void **cookie,unsigned long offset) {return 0 ; }
@@ -819,29 +820,28 @@ static __poll_t hci_uart_tty_poll(struct tty_struct *tty,return 0 ; } +static struct tty_ldisc_ops hci_uart_ldisc = { + .owner =THIS_MODULE , + .magic =TTY_LDISC_MAGIC , + .name ="n_hci" , + .open = hci_uart_tty_open, + .close = hci_uart_tty_close, + .read = hci_uart_tty_read, + .write = hci_uart_tty_write, + .ioctl = hci_uart_tty_ioctl, + .compat_ioctl = hci_uart_tty_ioctl, + .poll = hci_uart_tty_poll, + .receive_buf = hci_uart_tty_receive, + .write_wakeup = hci_uart_tty_wakeup, +}; +static int __init hci_uart_init (void ) { -static struct tty_ldisc_ops hci_uart_ldisc;int err;BT_INFO ("HCI UART driver ver %s" ,VERSION );/* Register the tty discipline */ - -memset (&hci_uart_ldisc,0 ,sizeof (hci_uart_ldisc)); - hci_uart_ldisc.magic =TTY_LDISC_MAGIC ; - hci_uart_ldisc.name ="n_hci" ; - hci_uart_ldisc.open = hci_uart_tty_open; - hci_uart_ldisc.close = hci_uart_tty_close; - hci_uart_ldisc.read = hci_uart_tty_read; - hci_uart_ldisc.write = hci_uart_tty_write; - hci_uart_ldisc.ioctl = hci_uart_tty_ioctl; - hci_uart_ldisc.compat_ioctl = hci_uart_tty_ioctl; - hci_uart_ldisc.poll = hci_uart_tty_poll; - hci_uart_ldisc.receive_buf = hci_uart_tty_receive; - hci_uart_ldisc.write_wakeup = hci_uart_tty_wakeup; - hci_uart_ldisc.owner =THIS_MODULE ; - err =tty_register_ldisc (N_HCI , &hci_uart_ldisc);if (err) {BT_ERR ("HCI line discipline registration failed. (%d)" , err);
drivers/input/serio/serport.c +3 -1
@@ -156,7 +156,9 @@ static void serport_ldisc_receive(struct tty_struct *tty, const unsigned char *c* returning 0 characters. */ -static ssize_t serport_ldisc_read (struct tty_struct * tty,struct file * file,unsigned char __user * buf,size_t nr) +static ssize_t serport_ldisc_read (struct tty_struct * tty,struct file * file, +unsigned char *kbuf,size_t nr, +void **cookie,unsigned long offset) {struct serport *serport = (struct serport *) tty->disc_data;struct serio *serio;
drivers/net/ppp/ppp_async.c +2 -1
@@ -259,7 +259,8 @@ static int ppp_asynctty_hangup(struct tty_struct *tty)*/ static ssize_t ppp_asynctty_read (struct tty_struct *tty,struct file *file, -unsigned char __user *buf,size_t count) +unsigned char *buf,size_t count, +void **cookie,unsigned long offset) {return -EAGAIN ; }
drivers/net/ppp/ppp_synctty.c +2 -1
@@ -257,7 +257,8 @@ static int ppp_sync_hangup(struct tty_struct *tty)*/ static ssize_t ppp_sync_read (struct tty_struct *tty,struct file *file, -unsigned char __user *buf,size_t count) +unsigned char *buf,size_t count, +void **cookie,unsigned long offset) {return -EAGAIN ; }
drivers/tty/n_gsm.c +2 -1
@@ -2559,7 +2559,8 @@ static void gsmld_write_wakeup(struct tty_struct *tty)*/ static ssize_t gsmld_read (struct tty_struct *tty,struct file *file, -unsigned char __user *buf,size_t nr) +unsigned char *buf,size_t nr, +void **cookie,unsigned long offset) {return -EOPNOTSUPP ; }
drivers/tty/n_hdlc.c +41 -19
85 lines not on this page · raw message
drivers/tty/n_null.c +2 -1
@@ -20,7 +20,8 @@ static void n_null_close(struct tty_struct *tty) }static ssize_t n_null_read (struct tty_struct *tty,struct file *file, -unsigned char __user * buf,size_t nr) +unsigned char *buf,size_t nr, +void **cookie,unsigned long offset) {return -EOPNOTSUPP ; }
drivers/tty/n_r3964.c +4 -6
@@ -129,7 +129,7 @@ static void remove_client_block(struct r3964_info *pInfo,static int r3964_open (struct tty_struct *tty);static void r3964_close (struct tty_struct *tty);static ssize_t r3964_read (struct tty_struct *tty,struct file *file, -unsigned char __user * buf,size_t nr); +void *cookie,unsigned char *buf,size_t nr);static ssize_t r3964_write (struct tty_struct *tty,struct file *file,const unsigned char *buf,size_t nr); static int r3964_ioctl(struct tty_struct *tty, struct file *file,
@@ -1058,7 +1058,8 @@ static void r3964_close(struct tty_struct *tty) }static ssize_t r3964_read (struct tty_struct *tty,struct file *file, -unsigned char __user * buf,size_t nr) +unsigned char *kbuf,size_t nr, +void **cookie,unsigned long offset) {struct r3964_info *pInfo = tty->disc_data;struct r3964_client_info *pClient;
@@ -1109,10 +1110,7 @@ static ssize_t r3964_read(struct tty_struct *tty, struct file *file,kfree (pMsg);TRACE_M ("r3964_read - msg kfree %p" , pMsg); -if (copy_to_user (buf, &theMsg, ret)) { - ret = -EFAULT ; - goto unlock; - } +memcpy (kbuf, &theMsg, ret);TRACE_PS ("read - return %d" , ret); goto unlock;
drivers/tty/n_tracerouter.c +3 -1
@@ -118,7 +118,9 @@ static void n_tracerouter_close(struct tty_struct *tty)* -EINVAL */ static ssize_t n_tracerouter_read (struct tty_struct *tty,struct file *file, -unsigned char __user *buf,size_t nr) { +unsigned char *buf,size_t nr, +void **cookie,unsigned long offset) +{return -EINVAL ; }
drivers/tty/n_tracesink.c +3 -1
@@ -115,7 +115,9 @@ static void n_tracesink_close(struct tty_struct *tty)* -EINVAL */ static ssize_t n_tracesink_read (struct tty_struct *tty,struct file *file, -unsigned char __user *buf,size_t nr) { +unsigned char *buf,size_t nr, +void **cookie,unsigned long offset) +{return -EINVAL ; }
drivers/tty/n_tty.c +34 -48
202 lines not on this page · raw message
drivers/tty/tty_io.c +61 -3
77 lines not on this page · raw message
include/linux/tty_ldisc.h +2 -1
@@ -185,7 +185,8 @@ struct tty_ldisc_ops {void (*close)(struct tty_struct *);void (*flush_buffer)(struct tty_struct *tty);ssize_t (*read)(struct tty_struct *tty,struct file *file, -unsigned char __user *buf,size_t nr); +unsigned char *buf,size_t nr, +void **cookie,unsigned long offset);ssize_t (*write)(struct tty_struct *tty,struct file *file,const unsigned char *buf,size_t nr); int (*ioctl)(struct tty_struct *tty, struct file *file,
net/nfc/nci/uart.c +2 -1
@@ -292,7 +292,8 @@ static int nci_uart_tty_ioctl(struct tty_struct *tty, struct file *file,/* We don't provide read/write/poll interface for user space. */ static ssize_t nci_uart_tty_read (struct tty_struct *tty,struct file *file, -unsigned char __user *buf,size_t nr) +unsigned char *buf,size_t nr, +void **cookie,unsigned long offset) {return 0 ; }
Signature · 1 line
2.29.2.157.g1d47791a39
Greg Kroah-Hartman
maintainerQuoted 58 lines · Linus Torvalds
Quoted 3 lines · Linus Torvalds
So it's not a "real" patch, but with improved buffer handling in
tty_read(), I think this is actually quite close.Hmm.
I somehow ended up working on this all because it's a Monday, and I don't see a lot of pull requests early in the week.
And I think I have a solution for the HDLC "we may need to copy a packet that might be up to 64kB" issue, that isn't really all that ugly.
We can just iterate over a random "cookie" that the line discipline can use any way it wants to. In the case of
n_hdlc, it can just put the 'rbuf' thing it has into that cookie, and then it can copy it all piece-meal until it is all used up. And if it runs out of space in the middle, it will return-EOVERFLOW, and we're all good.The only other thing such a line discipline needs is the offset into the cookie, but the iterator has to maintain that anyway, so that's simple enough.
So here's a fourth patch for this thing today, this time with what I think is actually a working model for the buffer handling.
Other line disciplines *could* use the cookie if they want to. I didn't do any of that, though.
The normal
n_ttyline discipline, for example, could easily just loop over the data. It doesn't need an offset or that 'rbuf' pointer, but it still needs to know whether the call is the first one or not, because the first time then_ttyline discipline is called it may need to wait for a minimum number of characters or whatever the termios settings say - but obviously once it has waited for it once, it shouldn't wait for it again the next time around (only on the next actual fullread()). IOW, it would be wrong if the termios said "wait for 5 characters", and then it saw 68 characters, copied the first 64, in the first iteration, and than saw "oh, now there are only 4 characters left so now I have to wait for a fifth".So
n_ttycould use the cookie purely to see whether it's the first iteration or not, and it could just set the cookie to a random value (it always starts out as NULL) to just show what state it is in.I did *NOT* do that, because it's not technically necessary - unlike the hdlc packet case,
n_ttyreturning a partial result is not wrong per se even if we might have reasons to improve on it later.What do people think about this?
Also, does anybody have any test-code for the HDLC case? I did find an interesting comment when doing a Debian code search:
/* Bloody hell... readv doesn't work with
N_HDLCline discipline... GRR! */
Quoted 8 lines
and yes, this model would allow us to handle
readv()properly for hdlc (and no, the old one did not, because it really wanted to see the whole packet in *one* user buffer).But I have no idea if hdlc is even relevant any more, and if anybody really cares.
Anybody?
This looks sane, but I'm still missing what the goal of this is here. It's nice from a "don't make the ldisc do the userspace copy", point of view, but what is the next step in order to tie that into splice?
I ask as I also have reports that sysfs binary files are now failing for this same reason, so I need to make the same change for them and it's not excatly obvious what to do:
https://lore.kernel.org/r/[email protected]
thanks,
greg k-h
Quoted 8 lines · Greg Kroah-Hartman
This looks sane, but I'm still missing what the goal of this is here. It's nice from a "don't make the ldisc do the userspace copy", point of view, but what is the next step in order to tie that into splice?
I ask as I also have reports that sysfs binary files are now failing for this same reason, so I need to make the same change for them and it's not excatly obvious what to do:
https://lore.kernel.org/r/[email protected]
I would like to confirm this. We are using firmwared and it returns EINVAL on sendfile(), too. I have tried setting the .splice_write callback as in the linked thread, but it didn't help, it just EINVAL'd in a different place.
I have bisected this issue down to this commit:4d03e3cc5982 ("fs: don't allow kernel reads and writes without iter ops")
Another case I've also noticed is writing to a serial connection:
kernel write not supported for file /ttymxc0 (pid: 252 comm: cat)
(Which still prints, though, because cat falls back to write(2), I suppose.)
Thank you,
Robert
Quoted 2 lines · Robert Karszniewicz
Another case I've also noticed is writing to a serial connection:
kernel write not supported for file /ttymxc0 (pid: 252 comm: cat)
Which is actually a TTY and I just failed to see the forest for all the trees...
Cheers.
Quoted 8 lines · Robert Karszniewicz
I have bisected this issue down to this commit:
4d03e3cc5982("fs: don't allow kernel reads and writes without iter ops")Another case I've also noticed is writing to a serial connection:
kernel write not supported for file /ttymxc0 (pid: 252 comm: cat)
Tangentially, I hit the same thing when hacking on this. Here's a diff making the implementation match the comment:
fs/read_write.c +1 -1
@@ -541,7 +541,7 @@ ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t* Also fail if ->write_iter and ->write are both wired up as that * implies very convoluted semantics. */ -if (unlikely (!file->f_op->write_iter || file->f_op->write)) +if (unlikely (file->f_op->write_iter && file->f_op->write))return warn_unsupported (file,"write" );init_sync_kiocb (&kiocb, file);
Quoted 4 lines · Greg Kroah-Hartman
This looks sane, but I'm still missing what the goal of this is here. It's nice from a "don't make the ldisc do the userspace copy", point of view, but what is the next step in order to tie that into splice?
I'll cook something up. With this, it should be fairly easy to add both the splice and iov_iter versions, because now it only needs the wrappers in tty_io.c, not for each line discipline.
I hope. Let's see..
Linus
Quoted 4 lines · Greg Kroah-Hartman
This looks sane, but I'm still missing what the goal of this is here. It's nice from a "don't make the ldisc do the userspace copy", point of view, but what is the next step in order to tie that into splice?
Ok, so here's a series of four patches that make ttys possible sources and destinations for splice() again.
Well, the first patch is just the pipe one for sendfile() - and it's the hacky one-liner, not the proper one that Al will hopefully add.
NOTE! I've signed off on these, because I think they are fine for testing - but they are really meant for testing ONLY.
I'm running a kernel with these in place, so they kind of work. And yes, I verified that sendfile() now works with a pipe or tty target. I didn't actually check splicing _from_ a tty, nor did I check that readv/writev now works properly, but it all LoosGood(tm) to me.
HOWEVER.
The reason these are for testing only is that
(a) my tests are pretty limited, and I'd like the actual people who
reported this to really test them out
(b) the new read iterator model is going to be quite horribly slow
for big pty transfers because the n_tty ldisc isn't doing the cookie batching
(c) I really really want Al to take a look at that iov_iter_revert()
thing in do_tty_write() (in "[PATCH 2/4] tty: implement write_iter")
Note that I'm more than happy to do (b) as a patch on top of this, but I'd like (a) and (c) to be clarified before I do that.
Quoted 3 lines
I ask as I also have reports that sysfs binary files are now failing for this same reason, so I need to make the same change for them and it's not excatly obvious what to do:
Ok, so that would require those kernfs_fop_{read,write}() functions to also be converted to read_iter/write_iter.
That doesn't look horrendous: it's not all that dissimilar from the two patches to do that for tty's ("tty: implement {read,write}_iter"). The seq_file part already has a iter version for reading, and the main change to kernfs_file_direct_read() and kernfs_fop_write() is to do that
(a) change the arguments from file/buf/count/ppos to kiocb/iov_iter
(b) change the copy_to/from_user() calls to copy_to/from_iter()
Note that (b) involves changing the semantics of the return value:
"copy_to/from_user()" returns the number of bytes that were *NOT* copied, while "copy_to/from_iter()" returns the number of bytes that WERE copied.
So the error case check does from
if (copy_to/from_user()) **ERROR**
to
if (copy_to/from_iter(n) != n) **ERROR**
but that is fairly straightforward.
The two "tty: implement write/read_iter" patches (patch 2 and 4) can be used as examples. That said, I want to again stress that they haven't seen all that much testing, and I do want Al to spray his holy penguin pee on that iov_iter_revert() thing in patch 2.
I'm honestly not that motivated on those sysfs files: the tty layer was an interesting test-case that I wanted to look at just because the conversion to kernel pointers was nontrivial for the read side.
But that sysfs binary file case really isn't interesting, and just more of a "Christoph broke it, I think he should just fix it". Christoph?
Anyway, anybody willing to test these tty/pipe patches on the loads that failed? Oliver?
Linus
From 95713b6e8b2247c55dd0a04174a55ea9a7fde7f6 Mon Sep 17 00:00:00 2001
From: Linus Torvalds <[email protected]>
Date: Tue, 19 Jan 2021 09:26:15 -0800
Subject: [PATCH 1/4] pipe: allow sendfile() destination with splice_write
Note that Al Viro is 100% right that this isn't needed for regular splicing (that treats pipes specially, since pipes _are_ the splice buffers).
So the correct thing to do is to teach do_splice_direct() the same "hey, it's already a pipe", and fix sendfile() with a pipe destination that way.
But this is the one-liner "make it work" thing, rather than the "do it properly" thing that Al will hopefully do.
--- fs/pipe.c | 1 + 1 file changed, 1 insertion(+)
fs/pipe.c +1 -0
@@ -1206,6 +1206,7 @@ const struct file_operations pipefifo_fops = { .unlocked_ioctl = pipe_ioctl, .release = pipe_release, .fasync = pipe_fasync, + .splice_write = iter_file_splice_write, };/*
Signature · 1 line
2.29.2.157.g1d47791a39
From 0dce8c5ef15f0aa7b4525721b86a20b7c4df8ca0 Mon Sep 17 00:00:00 2001
From: Linus Torvalds <[email protected]>
Date: Tue, 19 Jan 2021 11:41:16 -0800
Subject: [PATCH 2/4] tty: implement write_iter
This makes the tty layer use the .write_iter() function instead of the traditional .write() functionality.
That allows writev(), but more importantly also makes it possible to enable .splice_write() for ttys, reinstating the "splice to tty" functionality that was lost in commit 36e2c7421f02 ("fs: don't allow splice read/write without explicit ops").
--- drivers/tty/tty_io.c | 48 ++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 22 deletions(-)
drivers/tty/tty_io.c +26 -22
126 lines not on this page · raw message
Signature · 1 line
2.29.2.157.g1d47791a39
From 8b7bacf932d1090ea87fd9ad218715055d3eb66e Mon Sep 17 00:00:00 2001
From: Linus Torvalds <[email protected]>
Date: Mon, 18 Jan 2021 13:31:30 -0800
Subject: [PATCH 3/4] tty: convert tty_ldisc_ops 'read()' function to take a
kernel pointer
The tty line discipline .read() function was passed the final user pointer destination as an argument, which doesn't match the 'write()' function, and makes it very inconvenient to do a splice method for tty's.
This is a conversion to use a kernel buffer instead.
NOTE! It does this by passing the tty line discipline ->read() function an additional "cookie" to fill in, and an offset into the cookie data.
The line discipline can fill in the cookie data with its own private information, and then the reader will repeat the read until either the cookie is cleared or it runs out of data.
The only real user of this is N_HDLC, which can use this to handle big packets, even if the kernel buffer is smaller than the whole packet.
--- drivers/bluetooth/hci_ldisc.c | 34 +++++++-------- drivers/input/serio/serport.c | 4 +- drivers/net/ppp/ppp_async.c | 3 +- drivers/net/ppp/ppp_synctty.c | 3 +- drivers/tty/n_gsm.c | 3 +- drivers/tty/n_hdlc.c | 60 +++++++++++++++++-------- drivers/tty/n_null.c | 3 +- drivers/tty/n_r3964.c | 10 ++--- drivers/tty/n_tracerouter.c | 4 +- drivers/tty/n_tracesink.c | 4 +- drivers/tty/n_tty.c | 82 +++++++++++++++-------------------- drivers/tty/tty_io.c | 64 +++++++++++++++++++++++++-- include/linux/tty_ldisc.h | 3 +- net/nfc/nci/uart.c | 3 +- 14 files changed, 178 insertions(+), 102 deletions(-)
drivers/bluetooth/hci_ldisc.c +17 -17
@@ -802,7 +802,8 @@ static int hci_uart_tty_ioctl(struct tty_struct *tty, struct file *file,* We don't provide read/write/poll interface for user space. */ static ssize_t hci_uart_tty_read (struct tty_struct *tty,struct file *file, -unsigned char __user *buf,size_t nr) +unsigned char *buf,size_t nr, +void **cookie,unsigned long offset) {return 0 ; }
@@ -819,29 +820,28 @@ static __poll_t hci_uart_tty_poll(struct tty_struct *tty,return 0 ; } +static struct tty_ldisc_ops hci_uart_ldisc = { + .owner =THIS_MODULE , + .magic =TTY_LDISC_MAGIC , + .name ="n_hci" , + .open = hci_uart_tty_open, + .close = hci_uart_tty_close, + .read = hci_uart_tty_read, + .write = hci_uart_tty_write, + .ioctl = hci_uart_tty_ioctl, + .compat_ioctl = hci_uart_tty_ioctl, + .poll = hci_uart_tty_poll, + .receive_buf = hci_uart_tty_receive, + .write_wakeup = hci_uart_tty_wakeup, +}; +static int __init hci_uart_init (void ) { -static struct tty_ldisc_ops hci_uart_ldisc;int err;BT_INFO ("HCI UART driver ver %s" ,VERSION );/* Register the tty discipline */ - -memset (&hci_uart_ldisc,0 ,sizeof (hci_uart_ldisc)); - hci_uart_ldisc.magic =TTY_LDISC_MAGIC ; - hci_uart_ldisc.name ="n_hci" ; - hci_uart_ldisc.open = hci_uart_tty_open; - hci_uart_ldisc.close = hci_uart_tty_close; - hci_uart_ldisc.read = hci_uart_tty_read; - hci_uart_ldisc.write = hci_uart_tty_write; - hci_uart_ldisc.ioctl = hci_uart_tty_ioctl; - hci_uart_ldisc.compat_ioctl = hci_uart_tty_ioctl; - hci_uart_ldisc.poll = hci_uart_tty_poll; - hci_uart_ldisc.receive_buf = hci_uart_tty_receive; - hci_uart_ldisc.write_wakeup = hci_uart_tty_wakeup; - hci_uart_ldisc.owner =THIS_MODULE ; - err =tty_register_ldisc (N_HCI , &hci_uart_ldisc);if (err) {BT_ERR ("HCI line discipline registration failed. (%d)" , err);
drivers/input/serio/serport.c +3 -1
@@ -156,7 +156,9 @@ static void serport_ldisc_receive(struct tty_struct *tty, const unsigned char *c* returning 0 characters. */ -static ssize_t serport_ldisc_read (struct tty_struct * tty,struct file * file,unsigned char __user * buf,size_t nr) +static ssize_t serport_ldisc_read (struct tty_struct * tty,struct file * file, +unsigned char *kbuf,size_t nr, +void **cookie,unsigned long offset) {struct serport *serport = (struct serport *) tty->disc_data;struct serio *serio;
drivers/net/ppp/ppp_async.c +2 -1
@@ -259,7 +259,8 @@ static int ppp_asynctty_hangup(struct tty_struct *tty)*/ static ssize_t ppp_asynctty_read (struct tty_struct *tty,struct file *file, -unsigned char __user *buf,size_t count) +unsigned char *buf,size_t count, +void **cookie,unsigned long offset) {return -EAGAIN ; }
drivers/net/ppp/ppp_synctty.c +2 -1
@@ -257,7 +257,8 @@ static int ppp_sync_hangup(struct tty_struct *tty)*/ static ssize_t ppp_sync_read (struct tty_struct *tty,struct file *file, -unsigned char __user *buf,size_t count) +unsigned char *buf,size_t count, +void **cookie,unsigned long offset) {return -EAGAIN ; }
drivers/tty/n_gsm.c +2 -1
@@ -2559,7 +2559,8 @@ static void gsmld_write_wakeup(struct tty_struct *tty)*/ static ssize_t gsmld_read (struct tty_struct *tty,struct file *file, -unsigned char __user *buf,size_t nr) +unsigned char *buf,size_t nr, +void **cookie,unsigned long offset) {return -EOPNOTSUPP ; }
drivers/tty/n_hdlc.c +41 -19
85 lines not on this page · raw message
drivers/tty/n_null.c +2 -1
@@ -20,7 +20,8 @@ static void n_null_close(struct tty_struct *tty) }static ssize_t n_null_read (struct tty_struct *tty,struct file *file, -unsigned char __user * buf,size_t nr) +unsigned char *buf,size_t nr, +void **cookie,unsigned long offset) {return -EOPNOTSUPP ; }
drivers/tty/n_r3964.c +4 -6
@@ -129,7 +129,7 @@ static void remove_client_block(struct r3964_info *pInfo,static int r3964_open (struct tty_struct *tty);static void r3964_close (struct tty_struct *tty);static ssize_t r3964_read (struct tty_struct *tty,struct file *file, -unsigned char __user * buf,size_t nr); +void *cookie,unsigned char *buf,size_t nr);static ssize_t r3964_write (struct tty_struct *tty,struct file *file,const unsigned char *buf,size_t nr); static int r3964_ioctl(struct tty_struct *tty, struct file *file,
@@ -1058,7 +1058,8 @@ static void r3964_close(struct tty_struct *tty) }static ssize_t r3964_read (struct tty_struct *tty,struct file *file, -unsigned char __user * buf,size_t nr) +unsigned char *kbuf,size_t nr, +void **cookie,unsigned long offset) {struct r3964_info *pInfo = tty->disc_data;struct r3964_client_info *pClient;
@@ -1109,10 +1110,7 @@ static ssize_t r3964_read(struct tty_struct *tty, struct file *file,kfree (pMsg);TRACE_M ("r3964_read - msg kfree %p" , pMsg); -if (copy_to_user (buf, &theMsg, ret)) { - ret = -EFAULT ; - goto unlock; - } +memcpy (kbuf, &theMsg, ret);TRACE_PS ("read - return %d" , ret); goto unlock;
drivers/tty/n_tracerouter.c +3 -1
@@ -118,7 +118,9 @@ static void n_tracerouter_close(struct tty_struct *tty)* -EINVAL */ static ssize_t n_tracerouter_read (struct tty_struct *tty,struct file *file, -unsigned char __user *buf,size_t nr) { +unsigned char *buf,size_t nr, +void **cookie,unsigned long offset) +{return -EINVAL ; }
drivers/tty/n_tracesink.c +3 -1
@@ -115,7 +115,9 @@ static void n_tracesink_close(struct tty_struct *tty)* -EINVAL */ static ssize_t n_tracesink_read (struct tty_struct *tty,struct file *file, -unsigned char __user *buf,size_t nr) { +unsigned char *buf,size_t nr, +void **cookie,unsigned long offset) +{return -EINVAL ; }
drivers/tty/n_tty.c +34 -48
202 lines not on this page · raw message
drivers/tty/tty_io.c +61 -3
77 lines not on this page · raw message
include/linux/tty_ldisc.h +2 -1
@@ -185,7 +185,8 @@ struct tty_ldisc_ops {void (*close)(struct tty_struct *);void (*flush_buffer)(struct tty_struct *tty);ssize_t (*read)(struct tty_struct *tty,struct file *file, -unsigned char __user *buf,size_t nr); +unsigned char *buf,size_t nr, +void **cookie,unsigned long offset);ssize_t (*write)(struct tty_struct *tty,struct file *file,const unsigned char *buf,size_t nr); int (*ioctl)(struct tty_struct *tty, struct file *file,
net/nfc/nci/uart.c +2 -1
@@ -292,7 +292,8 @@ static int nci_uart_tty_ioctl(struct tty_struct *tty, struct file *file,/* We don't provide read/write/poll interface for user space. */ static ssize_t nci_uart_tty_read (struct tty_struct *tty,struct file *file, -unsigned char __user *buf,size_t nr) +unsigned char *buf,size_t nr, +void **cookie,unsigned long offset) {return 0 ; }
Signature · 1 line
2.29.2.157.g1d47791a39
From 08cb81c888e88b152f49ad2c90146b8f0c9ce6b3 Mon Sep 17 00:00:00 2001
From: Linus Torvalds <[email protected]>
Date: Tue, 19 Jan 2021 10:49:19 -0800
Subject: [PATCH 4/4] tty: implement read_iter
Now that the ldisc read() function takes kernel pointers, it's fairly straightforward to make the tty file operations use .read_iter() instead of .read().
That automatically gives us vread() and friends, and also makes it possible to do .splice_read() on tty's again.
--- drivers/tty/tty_io.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-)
drivers/tty/tty_io.c +18 -18
@@ -142,7 +142,7 @@ LIST_HEAD(tty_drivers); /* linked list of tty drivers *//* Mutex to protect creating and releasing a tty */ DEFINE_MUTEX (tty_mutex); -static ssize_t tty_read (struct file *,char __user *,size_t ,loff_t *); +static ssize_t tty_read (struct kiocb *,struct iov_iter *);static ssize_t tty_write (struct kiocb *,struct iov_iter *);ssize_t redirected_tty_write (struct kiocb *,struct iov_iter *);static __poll_t tty_poll (struct file *,poll_table *);
@@ -476,8 +476,9 @@ static void tty_show_fdinfo(struct seq_file *m, struct file *file)static const struct file_operations tty_fops = { .llseek = no_llseek, - .read = tty_read, + .read_iter = tty_read, .write_iter = tty_write, + .splice_read = generic_file_splice_read, .splice_write = iter_file_splice_write, .poll = tty_poll, .unlocked_ioctl = tty_ioctl,
@@ -490,8 +491,9 @@ static const struct file_operations tty_fops = {static const struct file_operations console_fops = { .llseek = no_llseek, - .read = tty_read, + .read_iter = tty_read, .write_iter = redirected_tty_write, + .splice_read = generic_file_splice_read, .splice_write = iter_file_splice_write, .poll = tty_poll, .unlocked_ioctl = tty_ioctl,
@@ -843,16 +845,17 @@ static void tty_update_time(struct timespec64 *time)* data or clears the cookie. The cookie may be something that the * ldisc maintains state for and needs to free. */ -static int iterate_tty_read (struct tty_ldisc *ld,struct tty_struct *tty,struct file *file, -char __user *buf,size_t count) +static int iterate_tty_read (struct tty_ldisc *ld,struct tty_struct *tty, +struct file *file,struct iov_iter *to) {int retval =0 ;void *cookie =NULL ;unsigned long offset =0 ;char kernel_buf[64 ]; +size_t count =iov_iter_count (to); do { - int size, uncopied; + int size, copied; size = count > sizeof(kernel_buf) ? sizeof(kernel_buf) : count; size = ld->ops->read(tty, file, kernel_buf, size, &cookie, offset);
@@ -868,10 +871,9 @@ static int iterate_tty_read(struct tty_ldisc *ld, struct tty_struct *tty, structreturn size; } - uncopied =copy_to_user (buf+offset, kernel_buf, size); - size -= uncopied; - offset += size; - count -= size; + copied =copy_to_iter (kernel_buf, size, to); + offset += copied; + count -= copied;/* * If the user copy failed, we still need to do another ->read()
@@ -879,7 +881,7 @@ static int iterate_tty_read(struct tty_ldisc *ld, struct tty_struct *tty, struct* * But make sure size is zeroed. */ -if (unlikely (uncopied)) { +if (unlikely (copied != size)) { count =0 ; retval = -EFAULT ; }
@@ -906,10 +908,10 @@ static int iterate_tty_read(struct tty_ldisc *ld, struct tty_struct *tty, struct* read calls may be outstanding in parallel. */ -static ssize_t tty_read (struct file *file,char __user *buf,size_t count, -loff_t *ppos) +static ssize_t tty_read (struct kiocb *iocb,struct iov_iter *to) {int i; +struct file *file = iocb->ki_filp;struct inode *inode =file_inode (file);struct tty_struct *tty =file_tty (file);struct tty_ldisc *ld;
@@ -922,11 +924,9 @@ static ssize_t tty_read(struct file *file, char __user *buf, size_t count,/* We want to wait for the line discipline to sort out in this situation */ ld =tty_ldisc_ref_wait (tty); -if (!ld) -return hung_up_tty_read (file, buf, count, ppos); i = -EIO ; -if (ld->ops->read) - i =iterate_tty_read (ld, tty, file, buf, count); +if (ld && ld->ops->read) + i =iterate_tty_read (ld, tty, file, to);tty_ldisc_deref (ld);if (i >0 )
@@ -2929,7 +2929,7 @@ static long tty_compat_ioctl(struct file *file, unsigned int cmd,static int this_tty (const void *t,struct file *file,unsigned fd) { -if (likely (file->f_op->read != tty_read)) +if (likely (file->f_op->read_iter != tty_read))return 0 ;return file_tty (file) != t ?0 : fd +1 ; }
Signature · 1 line
2.29.2.157.g1d47791a39
Quoted 3 lines · Linus Torvalds
But that sysfs binary file case really isn't interesting, and just more of a "Christoph broke it, I think he should just fix it". Christoph?
I'll give it a spin tomorrow. Should be simple enough.
Quoted 2 lines · Linus Torvalds
Anyway, anybody willing to test these tty/pipe patches on the loads that failed? Oliver?
Writing this from a kernel with those patches in; happily splice()ing to and from a pty.
Quoted 3 lines · Oliver Giles
Writing this from a kernel with those patches in; happily splice()ing to and from a pty.
Ok, good.
I have a couple of improvement patches on top of those, that I'm attaching here.
The first four patches worked fine for me (and apparently for you too), but due to the small buffer, the regular N_TTY case for tty read() calls are now limited to 64 bytes each.
That is unfortunate for performance, but it might also cause some actual breakage: anybody doing "read()" calls on a tty file descriptor _should_ be perfectly fine with short reads since they happen for signals etc, but I could well imagine that not everybody is.
And that new kernel buffer interface was _designed_ to allow stitching small buffers together efficiently (since the hdlc case needed it), so this implements that for the non-icanon case for n_tty too.
I wasted an embarrasing amount of time today on that final patch - I spent something like 6 hours chasing a truly stupid one-liner bug I had initially, and couldn't see what was wrong.
Which is why this only does the non-icanon case, because after I finally had my "Duh!" moment, I was so annoyed with it that I had to just walk away.
I'll come back to this tomorrow and do the line-buffered icanon case too (unless pull requests pile up), and then I'll be happy with the tty changes, and I think I can submit this series for real to Greg.
But in the meantime, here's two more patches to try on top of the previous four. They shouldn't matter, other than making the non-icanon throughput a lot better. But the more coverage they get, the happier I'll be.
Linus
From b12a1652c91648e96ae11946f7489515dd063789 Mon Sep 17 00:00:00 2001
From: Linus Torvalds <[email protected]>
Date: Tue, 19 Jan 2021 13:46:28 -0800
Subject: [PATCH 5/6] tty: clean up legacy leftovers from n_tty line discipline
Back when the line disciplines did their own direct user accesses, they had to deal with the data copy possibly failing in the middle.
Now that the user copy is done by the tty_io.c code, that failure case no longer exists.
Remove the left-over error handling code that cannot trigger.
--- drivers/tty/n_tty.c | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-)
drivers/tty/n_tty.c +4 -12
@@ -1957,19 +1957,17 @@ static inline int input_available_p(struct tty_struct *tty, int poll)* read_tail published */ -static int copy_from_read_buf (struct tty_struct *tty, +static void copy_from_read_buf (struct tty_struct *tty,unsigned char **kbp,size_t *nr) {struct n_tty_data *ldata = tty->disc_data; -int retval;size_t n;bool is_eof;size_t head =smp_load_acquire (&ldata->commit_head);size_t tail = ldata->read_tail & (N_TTY_BUF_SIZE -1 ); - retval =0 ; n =min (head - ldata->read_tail,N_TTY_BUF_SIZE - tail); n =min (*nr, n);if (n) {
@@ -1986,7 +1984,6 @@ static int copy_from_read_buf(struct tty_struct *tty, *kbp += n; *nr -= n; } -return retval; }/**
@@ -2228,20 +2225,15 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,if (retval)break ; }else { -int uncopied; -/* Deal with packet mode. */ if (packet && kb == kbuf) { *kb++ =TIOCPKT_DATA ; nr--; } - uncopied =copy_from_read_buf (tty, &kb, &nr); - uncopied +=copy_from_read_buf (tty, &kb, &nr); -if (uncopied) { - retval = -EFAULT ; -break ; - } +/* See comment above copy_from_read_buf() why twice */ +copy_from_read_buf (tty, &kb, &nr); +copy_from_read_buf (tty, &kb, &nr); }n_tty_check_unthrottle (tty);
Signature · 1 line
2.29.2.157.g1d47791a39
From e724cc9c4b101a4de1a56bcca6b5ec1d3493b173 Mon Sep 17 00:00:00 2001
From: Linus Torvalds <[email protected]>
Date: Tue, 19 Jan 2021 18:14:20 -0800
Subject: [PATCH 6/6] tty: teach n_tty line discipline about the new "cookie
continuations"
With the conversion to do the tty ldisc read operations in small chunks, the n_tty line discipline became noticeably slower for throughput oriented loads, because rather than read things in up to 2kB chunks, it would return at most 64 bytes per read() system call.
The cost is mainly all in the "do system calls over and over", not really in the new "copy to an extra kernel buffer".
This can be fixed by teaching the n_tty line discipline about the "cookie continuation" model, which the chunking code supports because things like hdlc need to be able to handle packets up to 64kB in size.
Doing that doesn't just get us back to the old performace, but to much better performance: my stupid "copy 10MB of data over a pty" test program is now almost twice as fast as it used to be (going down from 0.1s to 0.054s).
This is entirely because it now creates maximal chunks (which happens to be "one byte less than one page" due to how we do the circular tty buffers).
NOTE! This case only handles the simpler non-icanon case, which is the one where people may care about throughput. I'm going to do the icanon case later too, because while performance isn't a major issue for that, there may be programs that think they'll always get a full line and don't like the 64-byte chunking for that reason.
Such programs are arguably buggy (signals etc can cause random partial results from tty reads anyway), and good programs will handle such partial reads, but expecting everybody to write "good programs" has never been a winning policy for the kernel..
--- drivers/tty/n_tty.c | 52 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 10 deletions(-)
drivers/tty/n_tty.c +42 -10
86 lines not on this page · raw message
Signature · 1 line
2.29.2.157.g1d47791a39
Quoted 6 lines · Linus Torvalds
But in the meantime, here's two more patches to try on top of the previous four. They shouldn't matter, other than making the non-icanon throughput a lot better. But the more coverage they get, the happier I'll be.
I tried these out too, my use case is still working well.
tty splice branch (was "Re: Splicing to/from a tty")
In reply to Linus TorvaldsQuoted 4 lines · Linus Torvalds
I'll come back to this tomorrow and do the line-buffered icanon case too (unless pull requests pile up), and then I'll be happy with the tty changes, and I think I can submit this series for real to Greg.
Greg, I don't know how you want to handle this.
I have a branch with my tty splice patches at
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git tty-splice
and that now includes doing that "cookie continuation" thing even for the N_TTY icanon modes.
It passes my local tests, and I did try a few rather odd things. And Oliver tested an ealier version without that final commit on his load. But...
That tty splice thing is clearly a regression, but it's not like we have seen a lot of reports of it, so it's clearly a very special thing.
End result: I'm leaving it to you to decide how you want to handle it. You can tell me to just merge it myself as a regression fix, despite it being fairly late in the 5.11 series. Or you can pull it into your tty tree for linux-next and 5.12. And we can just plan to backport it (for 5.10 and 5.11) later when it has had more wide testing.
Another alternative is to do just that first patch immediately (the "tty: implement write_iter" one), because that one should be the simple case that gets sendfile() and splice() working when the _destination_ is a tty. The "source is a tty" is the much more complex case that the other patches deal with.
Linus
Greg Kroah-Hartman
maintainerRe: tty splice branch (was "Re: Splicing to/from a tty")
Quoted 30 lines · Linus Torvalds
Quoted 4 lines · Linus Torvalds
I'll come back to this tomorrow and do the line-buffered icanon case too (unless pull requests pile up), and then I'll be happy with the tty changes, and I think I can submit this series for real to Greg.
Greg, I don't know how you want to handle this.
I have a branch with my tty splice patches at
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.gittty-spliceand that now includes doing that "cookie continuation" thing even for the
N_TTYicanon modes.It passes my local tests, and I did try a few rather odd things. And Oliver tested an ealier version without that final commit on his load. But...
That tty splice thing is clearly a regression, but it's not like we have seen a lot of reports of it, so it's clearly a very special thing.
End result: I'm leaving it to you to decide how you want to handle it. You can tell me to just merge it myself as a regression fix, despite it being fairly late in the 5.11 series. Or you can pull it into your tty tree for linux-next and 5.12. And we can just plan to backport it (for 5.10 and 5.11) later when it has had more wide testing.
Quoted 5 lines
Another alternative is to do just that first patch immediately (the "tty: implement
write_iter" one), because that one should be the simple case that getssendfile()andsplice()working when the _destination_ is a tty. The "source is a tty" is the much more complex case that the other patches deal with.
Let me do this last thing. I've taken your one patch into my "tty-linus" branch and will go beat on it for a day and then ask you to pull it in for the next 5.11-rc release, and I've taken your full series into my "tty-next" branch so it will get much wider testing in linux-next for a few weeks. If it turns out that we get reports of the "splice/sendfile from a tty", we can always merge them into 5.11 and 5.10 as needed.
Thanks for doing this work,
greg k-h
Jiri Slaby
maintainerRe: tty splice branch (was "Re: Splicing to/from a tty")
In reply to Linus TorvaldsQuoted 11 lines · Linus Torvalds
Quoted 4 lines · Linus Torvalds
I'll come back to this tomorrow and do the line-buffered icanon case too (unless pull requests pile up), and then I'll be happy with the tty changes, and I think I can submit this series for real to Greg.
Greg, I don't know how you want to handle this.
I have a branch with my tty splice patches at
Quoted 8 lines
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.gittty-spliceand that now includes doing that "cookie continuation" thing even for the
N_TTYicanon modes.It passes my local tests, and I did try a few rather odd things. And Oliver tested an ealier version without that final commit on his load. But...
Hm, I would like to review this first. I noticed the changes only because a new branch appeared when I grabbed your tree and the branch has "tty" in its name.
So for example:
Review static ssize_t tty_write(struct file *file, const char __user *buf, · line 1052
Quoted 13 lines
(quoted diff) +2
-4@@ -1038,18 +1045,15 @@ static ssize_t tty_write(struct file *file, const char __user *buf, if (tty->ops->write_room == NULL) tty_err(tty, "missing write_room method\n"); ld = tty_ldisc_ref_wait(tty); - if (!ld) - return hung_up_tty_write(file, buf, count, ppos); - if (!ld->ops->write) + if (!ld || !ld->ops->write) ret = -EIO; else - ret = do_tty_write(ld->ops->write, tty, file, buf, count); + ret = do_tty_write(ld->ops->write, tty, file, from); tty_ldisc_deref(ld);
if ld == NULL => crash here.
So can you send the patches to the list and Cc me too?
Review static ssize_t tty_write(struct file *file, const char __user *buf, · line 1054
Quoted 2 lines
(quoted diff) +0
-0return ret; }
thanks,
Signature · 1 line
js
Jiri Slaby
maintainerRe: tty splice branch (was "Re: Splicing to/from a tty")
Quoted 1 line · Jiri Slaby
Hm, I would like to review this first. I noticed the changes only because a new branch appeared when I grabbed your tree and the branch has "tty" in its name.
Which is weird as you Cced me. Let me check what is wrong with my e-mail setup.
thanks,
Signature · 1 line
js
Re: tty splice branch (was "Re: Splicing to/from a tty")
Quoted 3 lines · Jiri Slaby
Which is weird as you Cced me. Let me check what is wrong with my e-mail setup.
Yeah, I think you were cc'd on not just the patches, but on all the discussion that preceded them.
But better late than never. I was actually much more nervous about the much more subtle "cookie continuation" stuff, not the trivial conversion patches.
Which just goes to show that most bugs are when you don't really think about it because it's "trivial".
Linus
Greg Kroah-Hartman
maintainerRe: tty splice branch (was "Re: Splicing to/from a tty")
In reply to Jiri SlabyQuoted 43 lines · Jiri Slaby
Quoted 11 lines · Linus Torvalds
Quoted 4 lines · Linus Torvalds
I'll come back to this tomorrow and do the line-buffered icanon case too (unless pull requests pile up), and then I'll be happy with the tty changes, and I think I can submit this series for real to Greg.
Greg, I don't know how you want to handle this.
I have a branch with my tty splice patches at
Quoted 8 lines
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.gittty-spliceand that now includes doing that "cookie continuation" thing even for the
N_TTYicanon modes.It passes my local tests, and I did try a few rather odd things. And Oliver tested an ealier version without that final commit on his load. But...
Hm, I would like to review this first. I noticed the changes only because a new branch appeared when I grabbed your tree and the branch has "tty" in its name.
So for example:
Review static ssize_t tty_write(struct file *file, const char __user *buf, · line 1052
Quoted 13 lines
(quoted diff) +2
-4@@ -1038,18 +1045,15 @@ static ssize_t tty_write(struct file *file, const char __user *buf, if (tty->ops->write_room == NULL) tty_err(tty, "missing write_room method\n"); ld = tty_ldisc_ref_wait(tty); - if (!ld) - return hung_up_tty_write(file, buf, count, ppos); - if (!ld->ops->write) + if (!ld || !ld->ops->write) ret = -EIO; else - ret = do_tty_write(ld->ops->write, tty, file, buf, count); + ret = do_tty_write(ld->ops->write, tty, file, from); tty_ldisc_deref(ld);if ld == NULL => crash here.
Quoted 1 line
So can you send the patches to the list and Cc me too?
I'll send them out right now, I've already merged them to my branches.
greg k-h
Quoted 7 lines · Linus Torvalds
Quoted 3 lines · Oliver Giles
Writing this from a kernel with those patches in; happily splice()ing to and from a pty.
Ok, good.
Quoted 8 lines
I have a couple of improvement patches on top of those, that I'm attaching here.
[...]
But in the meantime, here's two more patches to try on top of the previous four. They shouldn't matter, other than making the non-icanon throughput a lot better. But the more coverage they get, the happier I'll be.
I confirm that the 4 patches, as well as the 4+2 patches, fix the regression I noticed with cat failing on sendfile() to ttymxc0.
Thanks,
Robert
Quoted 3 lines · Robert Karszniewicz
I confirm that the 4 patches, as well as the 4+2 patches, fix the regression I noticed with cat failing on
sendfile()to ttymxc0.
Thanks. Now we just need to find somebody with HDLC use cases and we'll actually have this series properly tested...
Linus
Greg Kroah-Hartman
maintainerQuoted 8 lines · Linus Torvalds
Quoted 5 lines · Linus Torvalds
Anybody want to play with this? I'd suggest keeping that "dummy" parameter around for a while - I did an allmodconfig build with this, but if there are any architecture-specific non-x86-64 cases I wouldn't have seen them.
Quoted 6 lines
Not architecture-specific, but I did find by some grepping that I missed one line discipline driver: the Siemens R3964.
The reason I missed that is because it's been marked BROKEN in the
Kconfigfor almost two years, so it didn't show up in my allmodconfig coverage.
I need to just delete that thing now, obviously no one uses it anymore, sorry for it getting in the way...
greg k-h