Skip to contentSkip to conversation

tty: make tty const in tty_get_baud_rate()

PATCH In review Jiri Slaby (SUSE) 2 participants 10 messages Nov 27, 2023 – Nov 28, 2023

Subsystems (2)
lore
Share & tools
lore

Jiri Slaby (SUSE)

author maintainer
#0 lore raw

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.

---
 include/linux/tty.h | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
include/linux/tty.h +2 -3
index 7625fc98fef3..e96c85f4f91e 100644
@@ -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);
 }
Signature · 1 line
2.42.1
author maintainer
#1 lore raw

PATCH 2/5 tty: srmcons: make srmcons_do_write() return void

The return value of srmcons_do_write() is ignored as all characters are pushed. So make srmcons_do_write() to return void.

---

Notes:
[v2] reordered so that it makes sense

 arch/alpha/kernel/srmcons.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/alpha/kernel/srmcons.c +1 -2
index b68c5af083cd..de896fa9829e 100644
@@ -88,7 +88,7 @@ srmcons_receive_chars(struct timer_list *t)
 }
 
 /* called with callback_lock held */
-static int
+static void
 srmcons_do_write(struct tty_port *port, const char *buf, int count)
 {
 	static char str_cr[1] = "\r";
@@ -125,7 +125,6 @@ srmcons_do_write(struct tty_port *port, const char *buf, int count)
 				need_cr = 0;
 		}
 	}
-	return count;
 }
 
 static ssize_t
Signature · 1 line
2.42.1
author maintainer
#3 lore raw

PATCH 3/5 tty: srmcons: use 'count' directly in srmcons_do_write()

In reply to Jiri Slaby (SUSE)

Similarly to 'buf' in the previous patch, there is no need to have a separate counter ('remaining') in srmcons_do_write(). 'count' can be used directly which simplifies the code a bit.

Note that the type of the current count ('c') is changed from 'long' to 'size_t' so that:
1) it is prepared for the upcoming change of 'count's type, and
2) is unsigned.

---

Notes:
[v2] reordered so that it makes sense

 arch/alpha/kernel/srmcons.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/alpha/kernel/srmcons.c +4 -4
index de896fa9829e..32bc098de7da 100644
@@ -92,24 +92,24 @@ static void
 srmcons_do_write(struct tty_port *port, const char *buf, int count)
 {
 	static char str_cr[1] = "\r";
-	long c, remaining = count;
+	size_t c;
 	srmcons_result result;
 	int need_cr;
 
-	while (remaining > 0) {
+	while (count > 0) {
 		need_cr = 0;
 		/* 
 		 * Break it up into reasonable size chunks to allow a chance
 		 * for input to get in
 		 */
-		for (c = 0; c < min_t(long, 128L, remaining) && !need_cr; c++)
+		for (c = 0; c < min_t(size_t, 128U, count) && !need_cr; c++)
 			if (buf[c] == '\n')
 				need_cr = 1;
 		
 		while (c > 0) {
 			result.as_long = callback_puts(0, buf, c);
 			c -= result.bits.c;
-			remaining -= result.bits.c;
+			count -= result.bits.c;
 			buf += result.bits.c;
 
 			/*
Signature · 1 line
2.42.1
author maintainer
#5 lore raw

PATCH 4/5 tty: srmcons: switch need_cr to bool

In reply to Jiri Slaby (SUSE)

'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.

---
 arch/alpha/kernel/srmcons.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
arch/alpha/kernel/srmcons.c +3 -4
index 32bc098de7da..c6b821afbfd3 100644
@@ -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;
 		}
 	}
 }
Signature · 1 line
2.42.1
author maintainer
#7 lore raw

PATCH 5/5 tty: srmcons: remove 'str_cr' and use string directly

In reply to Jiri Slaby (SUSE)

'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.

Cc Richard Henderson <[email protected]>
Cc Ivan Kokshaysky <[email protected]>
Cc Matt Turner <[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(-)
arch/alpha/kernel/srmcons.c +1 -2
index c6b821afbfd3..42deea53beab 100644
@@ -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;
 		}
Signature · 1 line
2.42.1
Reviewed-by on v1 1/5
#9 lore raw
In reply to Jiri Slaby (SUSE)
Quoted 22 lines · Jiri Slaby (SUSE)

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.

---
 include/linux/tty.h | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
include/linux/tty.h +0 -0
index 7625fc98fef3..e96c85f4f91e 100644
@@ -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

include/linux/tty.h void tty_encode_baud_rate(struct tty_struct *tty, speed_t ibaud, · line 448

Quoted 10 lines
include/linux/tty.h +2 -3
  *
- * 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,

Signature · 1 line
 i.
Reviewed-by on v1 2/5
#2 lore raw

Re: [PATCH 2/5] tty: srmcons: make srmcons_do_write() return void

In reply to Jiri Slaby (SUSE)
Quoted 31 lines · Jiri Slaby (SUSE)

The return value of srmcons_do_write() is ignored as all characters are pushed. So make srmcons_do_write() to return void.

---

Notes:
[v2] reordered so that it makes sense

 arch/alpha/kernel/srmcons.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/alpha/kernel/srmcons.c +1 -1
index b68c5af083cd..de896fa9829e 100644
@@ -88,7 +88,7 @@ srmcons_receive_chars(struct timer_list *t)
 }
 
 /* called with callback_lock held */
-static int
+static void
 srmcons_do_write(struct tty_port *port, const char *buf, int count)
 {
 	static char str_cr[1] = "\r";

arch/alpha/kernel/srmcons.c srmcons_do_write(struct tty_port *port, const char *buf, int count) · line 130

Quoted 7 lines
arch/alpha/kernel/srmcons.c +0 -1
@@ -125,7 +125,6 @@ srmcons_do_write(struct tty_port *port, const char *buf, int count)
 				need_cr = 0;
 		}
 	}
-	return count;
 }
 
 static ssize_t
Signature · 1 line
 i.
Reviewed-by on v1 4/5
#6 lore raw

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

In reply to Jiri Slaby (SUSE)
Quoted 39 lines · Jiri Slaby (SUSE)

'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.

---
 arch/alpha/kernel/srmcons.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
arch/alpha/kernel/srmcons.c +2 -3
index 32bc098de7da..c6b821afbfd3 100644
@@ -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);

arch/alpha/kernel/srmcons.c srmcons_do_write(struct tty_port *port, const char *buf, int count) · line 127

Quoted 8 lines
arch/alpha/kernel/srmcons.c +1 -1
@@ -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;
 		}
 	}
 }
Signature · 1 line
 i.
Reviewed-by on v1 3/5
#4 lore raw

Re: [PATCH 3/5] tty: srmcons: use 'count' directly in srmcons_do_write()

In reply to Jiri Slaby (SUSE)
Quoted 46 lines · Jiri Slaby (SUSE)

Similarly to 'buf' in the previous patch, there is no need to have a separate counter ('remaining') in srmcons_do_write(). 'count' can be used directly which simplifies the code a bit.

Note that the type of the current count ('c') is changed from 'long' to 'size_t' so that:
1) it is prepared for the upcoming change of 'count's type, and
2) is unsigned.

---

Notes:
[v2] reordered so that it makes sense

 arch/alpha/kernel/srmcons.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/alpha/kernel/srmcons.c +3 -3
index de896fa9829e..32bc098de7da 100644
@@ -92,24 +92,24 @@ static void
 srmcons_do_write(struct tty_port *port, const char *buf, int count)
 {
 	static char str_cr[1] = "\r";
-	long c, remaining = count;
+	size_t c;
 	srmcons_result result;
 	int need_cr;
 
-	while (remaining > 0) {
+	while (count > 0) {
 		need_cr = 0;
 		/* 
 		 * Break it up into reasonable size chunks to allow a chance
 		 * for input to get in
 		 */
-		for (c = 0; c < min_t(long, 128L, remaining) && !need_cr; c++)
+		for (c = 0; c < min_t(size_t, 128U, count) && !need_cr; c++)
 			if (buf[c] == '\n')

arch/alpha/kernel/srmcons.c static void · line 115

Quoted 10 lines
arch/alpha/kernel/srmcons.c +1 -1
 				need_cr = 1;
 
 		while (c > 0) {
 			result.as_long = callback_puts(0, buf, c);
 			c -= result.bits.c;
-			remaining -= result.bits.c;
+			count -= result.bits.c;
 			buf += result.bits.c;
 
 			/*
Signature · 1 line
 i.
Reviewed-by on v1 5/5
#8 lore raw

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

In reply to Jiri Slaby (SUSE)
Quoted 34 lines · Jiri Slaby (SUSE)

'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.

Cc Richard Henderson <[email protected]>
Cc Ivan Kokshaysky <[email protected]>
Cc Matt Turner <[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(-)
arch/alpha/kernel/srmcons.c +0 -1
index c6b821afbfd3..42deea53beab 100644
@@ -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;
 

arch/alpha/kernel/srmcons.c srmcons_do_write(struct tty_port *port, const char *buf, int count) · line 124

Quoted 8 lines
arch/alpha/kernel/srmcons.c +1 -1
@@ -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;
 		}
Signature · 1 line
 i.