From: Jiri Slaby <jirislaby@kernel.org>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
linux-serial@vger.kernel.org
Cc: hch@lst.de, viro@zeniv.linux.org.uk,
linux-kernel@vger.kernel.org, ohw.giles@gmail.com,
r.karszniewicz@phytec.de,
Linus Torvalds <torvalds@linux-foundation.org>
Subject: Re: [PATCH 2/6] tty: convert tty_ldisc_ops 'read()' function to take a kernel pointer
Date: Thu, 21 Jan 2021 12:02:03 +0100 [thread overview]
Message-ID: <ff6709dc-bb42-1e52-b348-c52036960bdd@kernel.org> (raw)
In-Reply-To: <20210121090020.3147058-2-gregkh@linuxfoundation.org>
On 21. 01. 21, 10:00, Greg Kroah-Hartman wrote:
> From: Linus Torvalds <torvalds@linux-foundation.org>
>
> 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
> ttys.
>
> 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.
>
> Cc: Christoph Hellwig <hch@lst.de>
> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Cc: Al Viro <viro@zeniv.linux.org.uk>
> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
...
> --- a/drivers/tty/tty_io.c
> +++ b/drivers/tty/tty_io.c
> @@ -833,6 +833,65 @@ static void tty_update_time(struct timespec64 *time)
> time->tv_sec = sec;
> }
>
> +/*
> + * Iterate on the ldisc ->read() function until we've gotten all
> + * the data the ldisc has for us.
> + *
> + * The "cookie" is something that the ldisc read function can fill
> + * in to let us know that there is more data to be had.
> + *
> + * We promise to continue to call the ldisc until it stops returning
> + * 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)
> +{
> + int retval = 0;
> + void *cookie = NULL;
> + unsigned long offset = 0;
> + char kernel_buf[64];
> +
> + do {
> + int size, uncopied;
> +
> + size = count > sizeof(kernel_buf) ? sizeof(kernel_buf) : count;
Or simply
size = min(count, sizeof(kernel_buf));
> + size = ld->ops->read(tty, file, kernel_buf, size, &cookie, offset);
> + if (!size)
> + break;
> +
> + /*
> + * A ldisc read error return will override any previously copied
> + * data (eg -EOVERFLOW from HDLC)
> + */
> + if (size < 0) {
> + memzero_explicit(kernel_buf, sizeof(kernel_buf));
> + return size;
> + }
> +
> + uncopied = copy_to_user(buf+offset, kernel_buf, size);
> + size -= uncopied;
> + offset += size;
> + count -= size;
> +
> + /*
> + * If the user copy failed, we still need to do another ->read()
> + * call if we had a cookie to let the ldisc clear up.
> + *
> + * But make sure size is zeroed.
> + */
> + if (unlikely(uncopied)) {
> + count = 0;
> + retval = -EFAULT;
n_hdlc_tty_read will return EOVERFLOW when size is 0, so this EFAULT is
never propagated, if I am looking correctly? n_tty seems to be fine
(returns zero for zeroed size).
> + }
> + } while (cookie);
> +
> + /* We always clear tty buffer in case they contained passwords */
> + memzero_explicit(kernel_buf, sizeof(kernel_buf));
> + return offset ? offset : retval;
> +}
thanks,
--
js
next prev parent reply other threads:[~2021-01-21 11:03 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-01-21 9:00 [PATCH 1/6] tty: implement write_iter Greg Kroah-Hartman
2021-01-21 9:00 ` [PATCH 2/6] tty: convert tty_ldisc_ops 'read()' function to take a kernel pointer Greg Kroah-Hartman
2021-01-21 11:02 ` Jiri Slaby [this message]
2021-01-21 17:46 ` Linus Torvalds
2021-01-21 17:57 ` Greg Kroah-Hartman
2021-01-21 9:00 ` [PATCH 3/6] tty: implement read_iter Greg Kroah-Hartman
2021-01-21 10:44 ` Jiri Slaby
2021-01-21 9:00 ` [PATCH 4/6] tty: clean up legacy leftovers from n_tty line discipline Greg Kroah-Hartman
2021-01-21 9:00 ` [PATCH 5/6] tty: teach n_tty line discipline about the new "cookie continuations" Greg Kroah-Hartman
2021-01-21 9:00 ` [PATCH 6/6] tty: teach the n_tty ICANON case about the new "cookie continuations" too Greg Kroah-Hartman
2021-01-21 9:39 ` [PATCH 1/6] tty: implement write_iter Jiri Slaby
2021-01-21 17:44 ` Linus Torvalds
2021-01-21 17:57 ` Greg Kroah-Hartman
2021-01-21 18:42 ` Linus Torvalds
2021-01-21 19:43 ` Greg Kroah-Hartman
2021-01-21 21:09 ` Linus Torvalds
2021-01-22 7:07 ` Jiri Slaby
2021-01-22 7:33 ` Jiri Slaby
2021-01-22 7:43 ` Greg Kroah-Hartman
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ff6709dc-bb42-1e52-b348-c52036960bdd@kernel.org \
--to=jirislaby@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=hch@lst.de \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-serial@vger.kernel.org \
--cc=ohw.giles@gmail.com \
--cc=r.karszniewicz@phytec.de \
--cc=torvalds@linux-foundation.org \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox