2023-11-27 12:37:34

by Jiri Slaby

[permalink] [raw]
Subject: [PATCH 1/5] tty: make tty const in tty_get_baud_rate()

After commit 87888fb9ac0c ("tty: Remove baudrate dead code & make
ktermios params const"), the 'tty' parameter is only read in
tty_get_baud_rate(). Therefore, we can make 'tty' accepted in the
function 'const' for clarity.

The "the terminal bit flags may be updated." part of the
tty_get_baud_rate()'s kernel-doc is dropped as it is no longer true.
Because of the same commit above. And it was misplaced anyway.

Signed-off-by: Jiri Slaby (SUSE) <[email protected]>
Cc: Ilpo Järvinen <[email protected]>
---
include/linux/tty.h | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/include/linux/tty.h b/include/linux/tty.h
index 7625fc98fef3..e96c85f4f91e 100644
--- a/include/linux/tty.h
+++ b/include/linux/tty.h
@@ -440,10 +440,9 @@ void tty_encode_baud_rate(struct tty_struct *tty, speed_t ibaud,
*
* Returns: the baud rate as an integer for this terminal
*
- * Locking: The termios lock must be held by the caller and the terminal bit
- * flags may be updated.
+ * Locking: The termios lock must be held by the caller.
*/
-static inline speed_t tty_get_baud_rate(struct tty_struct *tty)
+static inline speed_t tty_get_baud_rate(const struct tty_struct *tty)
{
return tty_termios_baud_rate(&tty->termios);
}
--
2.42.1


2023-11-27 12:37:43

by Jiri Slaby

[permalink] [raw]
Subject: [PATCH 5/5] tty: srmcons: remove 'str_cr' and use string directly

'str_cr' contains a single character: \r. There is no need to declare it
as array. Instead, pass the character (as a string) to callback_puts()
directly. This ensures the string is in proper .rodata (const) section
and makes the code more obvious.

Signed-off-by: Jiri Slaby (SUSE) <[email protected]>
Cc: Richard Henderson <[email protected]>
Cc: Ivan Kokshaysky <[email protected]>
Cc: Matt Turner <[email protected]>
Cc: [email protected]
---

Notes:
[v2]
pass "\r" directly to callback_puts() as Richard suggests
spell correct \r in the commit log as Ilpo noticed

arch/alpha/kernel/srmcons.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/arch/alpha/kernel/srmcons.c b/arch/alpha/kernel/srmcons.c
index c6b821afbfd3..42deea53beab 100644
--- a/arch/alpha/kernel/srmcons.c
+++ b/arch/alpha/kernel/srmcons.c
@@ -91,7 +91,6 @@ srmcons_receive_chars(struct timer_list *t)
static void
srmcons_do_write(struct tty_port *port, const char *buf, int count)
{
- static char str_cr[1] = "\r";
size_t c;
srmcons_result result;

@@ -119,7 +118,7 @@ srmcons_do_write(struct tty_port *port, const char *buf, int count)
}

while (need_cr) {
- result.as_long = callback_puts(0, str_cr, 1);
+ result.as_long = callback_puts(0, "\r", 1);
if (result.bits.c > 0)
need_cr = false;
}
--
2.42.1

2023-11-27 12:37:49

by Jiri Slaby

[permalink] [raw]
Subject: [PATCH 4/5] tty: srmcons: switch need_cr to bool

'need_cr' is a flag, so type it properly to be a 'bool'. Move the
declaration into the loop too. That ensures the variable is initialized
properly even if the code was moved somehow.

Signed-off-by: Jiri Slaby (SUSE) <[email protected]>
Reviewed-by: Richard Henderson <[email protected]>
Cc: Ivan Kokshaysky <[email protected]>
Cc: Matt Turner <[email protected]>
Cc: [email protected]
---
arch/alpha/kernel/srmcons.c | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/arch/alpha/kernel/srmcons.c b/arch/alpha/kernel/srmcons.c
index 32bc098de7da..c6b821afbfd3 100644
--- a/arch/alpha/kernel/srmcons.c
+++ b/arch/alpha/kernel/srmcons.c
@@ -94,17 +94,16 @@ srmcons_do_write(struct tty_port *port, const char *buf, int count)
static char str_cr[1] = "\r";
size_t c;
srmcons_result result;
- int need_cr;

while (count > 0) {
- need_cr = 0;
+ bool need_cr = false;
/*
* Break it up into reasonable size chunks to allow a chance
* for input to get in
*/
for (c = 0; c < min_t(size_t, 128U, count) && !need_cr; c++)
if (buf[c] == '\n')
- need_cr = 1;
+ need_cr = true;

while (c > 0) {
result.as_long = callback_puts(0, buf, c);
@@ -122,7 +121,7 @@ srmcons_do_write(struct tty_port *port, const char *buf, int count)
while (need_cr) {
result.as_long = callback_puts(0, str_cr, 1);
if (result.bits.c > 0)
- need_cr = 0;
+ need_cr = false;
}
}
}
--
2.42.1

2023-11-28 10:21:09

by Ilpo Järvinen

[permalink] [raw]
Subject: Re: [PATCH 1/5] tty: make tty const in tty_get_baud_rate()

On Mon, 27 Nov 2023, Jiri Slaby (SUSE) wrote:

> After commit 87888fb9ac0c ("tty: Remove baudrate dead code & make
> ktermios params const"), the 'tty' parameter is only read in
> tty_get_baud_rate(). Therefore, we can make 'tty' accepted in the
> function 'const' for clarity.
>
> The "the terminal bit flags may be updated." part of the
> tty_get_baud_rate()'s kernel-doc is dropped as it is no longer true.
> Because of the same commit above. And it was misplaced anyway.
>
> Signed-off-by: Jiri Slaby (SUSE) <[email protected]>
> Cc: Ilpo Järvinen <[email protected]>
> ---
> include/linux/tty.h | 5 ++---
> 1 file changed, 2 insertions(+), 3 deletions(-)
>
> diff --git a/include/linux/tty.h b/include/linux/tty.h
> index 7625fc98fef3..e96c85f4f91e 100644
> --- a/include/linux/tty.h
> +++ b/include/linux/tty.h
> @@ -440,10 +440,9 @@ void tty_encode_baud_rate(struct tty_struct *tty, speed_t ibaud,
> *
> * Returns: the baud rate as an integer for this terminal
> *
> - * Locking: The termios lock must be held by the caller and the terminal bit
> - * flags may be updated.
> + * Locking: The termios lock must be held by the caller.
> */
> -static inline speed_t tty_get_baud_rate(struct tty_struct *tty)
> +static inline speed_t tty_get_baud_rate(const struct tty_struct *tty)
> {
> return tty_termios_baud_rate(&tty->termios);
> }

Thanks,

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

--
i.

2023-11-28 13:26:42

by Ilpo Järvinen

[permalink] [raw]
Subject: Re: [PATCH 4/5] tty: srmcons: switch need_cr to bool

On Mon, 27 Nov 2023, Jiri Slaby (SUSE) wrote:

> 'need_cr' is a flag, so type it properly to be a 'bool'. Move the
> declaration into the loop too. That ensures the variable is initialized
> properly even if the code was moved somehow.
>
> Signed-off-by: Jiri Slaby (SUSE) <[email protected]>
> Reviewed-by: Richard Henderson <[email protected]>
> Cc: Ivan Kokshaysky <[email protected]>
> Cc: Matt Turner <[email protected]>
> Cc: [email protected]
> ---
> arch/alpha/kernel/srmcons.c | 7 +++----
> 1 file changed, 3 insertions(+), 4 deletions(-)
>
> diff --git a/arch/alpha/kernel/srmcons.c b/arch/alpha/kernel/srmcons.c
> index 32bc098de7da..c6b821afbfd3 100644
> --- a/arch/alpha/kernel/srmcons.c
> +++ b/arch/alpha/kernel/srmcons.c
> @@ -94,17 +94,16 @@ srmcons_do_write(struct tty_port *port, const char *buf, int count)
> static char str_cr[1] = "\r";
> size_t c;
> srmcons_result result;
> - int need_cr;
>
> while (count > 0) {
> - need_cr = 0;
> + bool need_cr = false;
> /*
> * Break it up into reasonable size chunks to allow a chance
> * for input to get in
> */
> for (c = 0; c < min_t(size_t, 128U, count) && !need_cr; c++)
> if (buf[c] == '\n')
> - need_cr = 1;
> + need_cr = true;
>
> while (c > 0) {
> result.as_long = callback_puts(0, buf, c);
> @@ -122,7 +121,7 @@ srmcons_do_write(struct tty_port *port, const char *buf, int count)
> while (need_cr) {
> result.as_long = callback_puts(0, str_cr, 1);
> if (result.bits.c > 0)
> - need_cr = 0;
> + need_cr = false;
> }
> }
> }
>

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

--
i.

2023-11-28 13:27:12

by Ilpo Järvinen

[permalink] [raw]
Subject: Re: [PATCH 5/5] tty: srmcons: remove 'str_cr' and use string directly

On Mon, 27 Nov 2023, Jiri Slaby (SUSE) wrote:

> 'str_cr' contains a single character: \r. There is no need to declare it
> as array. Instead, pass the character (as a string) to callback_puts()
> directly. This ensures the string is in proper .rodata (const) section
> and makes the code more obvious.
>
> Signed-off-by: Jiri Slaby (SUSE) <[email protected]>
> Cc: Richard Henderson <[email protected]>
> Cc: Ivan Kokshaysky <[email protected]>
> Cc: Matt Turner <[email protected]>
> Cc: [email protected]
> ---
>
> Notes:
> [v2]
> pass "\r" directly to callback_puts() as Richard suggests
> spell correct \r in the commit log as Ilpo noticed
>
> arch/alpha/kernel/srmcons.c | 3 +--
> 1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/arch/alpha/kernel/srmcons.c b/arch/alpha/kernel/srmcons.c
> index c6b821afbfd3..42deea53beab 100644
> --- a/arch/alpha/kernel/srmcons.c
> +++ b/arch/alpha/kernel/srmcons.c
> @@ -91,7 +91,6 @@ srmcons_receive_chars(struct timer_list *t)
> static void
> srmcons_do_write(struct tty_port *port, const char *buf, int count)
> {
> - static char str_cr[1] = "\r";
> size_t c;
> srmcons_result result;
>
> @@ -119,7 +118,7 @@ srmcons_do_write(struct tty_port *port, const char *buf, int count)
> }
>
> while (need_cr) {
> - result.as_long = callback_puts(0, str_cr, 1);
> + result.as_long = callback_puts(0, "\r", 1);
> if (result.bits.c > 0)
> need_cr = false;
> }
>

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

--
i.