2023-09-19 08:57:10

by Jiri Slaby

[permalink] [raw]
Subject: [PATCH 04/15] tty: n_tty: invert the condition in copy_from_read_buf()

Make "no numbers available" a fast quit from the function. And do the
heavy work outside the 'if'. This makes the code more understandable and
conforming to the common kernel coding style.

Signed-off-by: Jiri Slaby (SUSE) <[email protected]>
---
drivers/tty/n_tty.c | 38 ++++++++++++++++++++------------------
1 file changed, 20 insertions(+), 18 deletions(-)

diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c
index 6a112910c058..922fb61b587a 100644
--- a/drivers/tty/n_tty.c
+++ b/drivers/tty/n_tty.c
@@ -1966,24 +1966,26 @@ static bool copy_from_read_buf(const struct tty_struct *tty, u8 **kbp,
size_t tail = MASK(ldata->read_tail);

n = min3(head - ldata->read_tail, N_TTY_BUF_SIZE - tail, *nr);
- if (n) {
- u8 *from = read_buf_addr(ldata, tail);
- memcpy(*kbp, from, n);
- is_eof = n == 1 && *from == EOF_CHAR(tty);
- tty_audit_add_data(tty, from, n);
- zero_buffer(tty, from, n);
- smp_store_release(&ldata->read_tail, ldata->read_tail + n);
- /* Turn single EOF into zero-length read */
- if (L_EXTPROC(tty) && ldata->icanon && is_eof &&
- (head == ldata->read_tail))
- return false;
- *kbp += n;
- *nr -= n;
-
- /* If we have more to copy, let the caller know */
- return head != ldata->read_tail;
- }
- return false;
+ if (!n)
+ return false;
+
+ u8 *from = read_buf_addr(ldata, tail);
+ memcpy(*kbp, from, n);
+ is_eof = n == 1 && *from == EOF_CHAR(tty);
+ tty_audit_add_data(tty, from, n);
+ zero_buffer(tty, from, n);
+ smp_store_release(&ldata->read_tail, ldata->read_tail + n);
+
+ /* Turn single EOF into zero-length read */
+ if (L_EXTPROC(tty) && ldata->icanon && is_eof &&
+ head == ldata->read_tail)
+ return false;
+
+ *kbp += n;
+ *nr -= n;
+
+ /* If we have more to copy, let the caller know */
+ return head != ldata->read_tail;
}

/**
--
2.42.0


2023-09-19 10:53:31

by Jiri Slaby

[permalink] [raw]
Subject: Re: [PATCH 04/15] tty: n_tty: invert the condition in copy_from_read_buf()

On 19. 09. 23, 11:54, Ilpo Järvinen wrote:
> On Tue, 19 Sep 2023, Jiri Slaby (SUSE) wrote:
>
>> Make "no numbers available" a fast quit from the function. And do the
>
> Did you really intend to write "numbers" and not e.g. characters?

What was I thinking? "n must be numbers", apparently. But I definitely
meant "characters". Funny, that I missed it now, while re-reading and
preparing the commit logs for submission.

thanks,
--
js
suse labs

2023-09-19 14:00:36

by Ilpo Järvinen

[permalink] [raw]
Subject: Re: [PATCH 04/15] tty: n_tty: invert the condition in copy_from_read_buf()

On Tue, 19 Sep 2023, Jiri Slaby (SUSE) wrote:

> Make "no numbers available" a fast quit from the function. And do the

Did you really intend to write "numbers" and not e.g. characters?

The change itself looks good,

Reviewed-by: Ilpo J?rvinen <[email protected]>

--
i.

> heavy work outside the 'if'. This makes the code more understandable and
> conforming to the common kernel coding style.
>
> Signed-off-by: Jiri Slaby (SUSE) <[email protected]>
> ---
> drivers/tty/n_tty.c | 38 ++++++++++++++++++++------------------
> 1 file changed, 20 insertions(+), 18 deletions(-)
>
> diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c
> index 6a112910c058..922fb61b587a 100644
> --- a/drivers/tty/n_tty.c
> +++ b/drivers/tty/n_tty.c
> @@ -1966,24 +1966,26 @@ static bool copy_from_read_buf(const struct tty_struct *tty, u8 **kbp,
> size_t tail = MASK(ldata->read_tail);
>
> n = min3(head - ldata->read_tail, N_TTY_BUF_SIZE - tail, *nr);
> - if (n) {
> - u8 *from = read_buf_addr(ldata, tail);
> - memcpy(*kbp, from, n);
> - is_eof = n == 1 && *from == EOF_CHAR(tty);
> - tty_audit_add_data(tty, from, n);
> - zero_buffer(tty, from, n);
> - smp_store_release(&ldata->read_tail, ldata->read_tail + n);
> - /* Turn single EOF into zero-length read */
> - if (L_EXTPROC(tty) && ldata->icanon && is_eof &&
> - (head == ldata->read_tail))
> - return false;
> - *kbp += n;
> - *nr -= n;
> -
> - /* If we have more to copy, let the caller know */
> - return head != ldata->read_tail;
> - }
> - return false;
> + if (!n)
> + return false;
> +
> + u8 *from = read_buf_addr(ldata, tail);
> + memcpy(*kbp, from, n);
> + is_eof = n == 1 && *from == EOF_CHAR(tty);
> + tty_audit_add_data(tty, from, n);
> + zero_buffer(tty, from, n);
> + smp_store_release(&ldata->read_tail, ldata->read_tail + n);
> +
> + /* Turn single EOF into zero-length read */
> + if (L_EXTPROC(tty) && ldata->icanon && is_eof &&
> + head == ldata->read_tail)
> + return false;
> +
> + *kbp += n;
> + *nr -= n;
> +
> + /* If we have more to copy, let the caller know */
> + return head != ldata->read_tail;
> }
>
> /**
>