serial: core: Use string length for SysRq magic sequence
PATCH 2/4 serial: core: Print escaped SysRq Magic sequence if enabled
In reply to Andy ShevchenkoIt is useful to see on the serial console the magic sequence itself to enable SysRq without rummaging source code.
--- drivers/tty/serial/serial_core.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/tty/serial/serial_core.c +4 -1
@@ -3191,8 +3191,11 @@ static const char sysrq_toggle_seq[] = CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE;static void uart_sysrq_on (struct work_struct *w) { +int sysrq_toggle_seq_len =strlen (sysrq_toggle_seq); +sysrq_toggle_support (1 ); -pr_info ("SysRq is enabled by magic sequence on serial\n" ); +pr_info ("SysRq is enabled by magic sequence '%*pE' on serial\n" , + sysrq_toggle_seq_len, sysrq_toggle_seq); } static DECLARE_WORK(sysrq_enable_work, uart_sysrq_on);
Signature · 1 line
2.25.1
PATCH 1/4 serial: core: Use string length for SysRq magic sequence
Compiler is not happy about using ARRAY_SIZE() in comparison to smaller type:
CC drivers/tty/serial/serial_core.o.../serial_core.c: In function ‘uart_try_toggle_sysrq’:.../serial_core.c:3222:24: warning: comparison is always false due to limited range of data type [-Wtype-limits]
3222 | if (++port->sysrq_seq < (ARRAY_SIZE(sysrq_toggle_seq) - 1)) {
| ^
Looking at the code it appears that there is an additional weirdness, i.e. use ARRAY_SIZE() against simple string literal. Yes, the idea probably was to allow '\0' in the sequence, but it's impractical: kernel configuration won't accept it to begin with followed by a comment about '\0' before comparison in question.
Drop all these by switching to strlen() and convert code accordingly.
--- drivers/tty/serial/serial_core.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/tty/serial/serial_core.c +4 -3
@@ -3209,7 +3209,9 @@ static DECLARE_WORK(sysrq_enable_work, uart_sysrq_on);*/ static bool uart_try_toggle_sysrq (struct uart_port *port,unsigned int ch) { -if (ARRAY_SIZE (sysrq_toggle_seq) <=1 ) +int sysrq_toggle_seq_len =strlen (sysrq_toggle_seq); + +if (!sysrq_toggle_seq_len)return false;BUILD_BUG_ON (ARRAY_SIZE (sysrq_toggle_seq) >=U8_MAX );
@@ -3218,8 +3220,7 @@ static bool uart_try_toggle_sysrq(struct uart_port *port, unsigned int ch)return false; } -/* Without the last \0 */ -if (++port->sysrq_seq < (ARRAY_SIZE (sysrq_toggle_seq) -1 )) { +if (++port->sysrq_seq < sysrq_toggle_seq_len) { port->sysrq = jiffies +SYSRQ_TIMEOUT ;return true; }
Signature · 1 line
2.25.1
PATCH 3/4 serial: core: Use uart_console() helper in SysRq code
Use uart_console() helper in SysRq code instead of open coded variant. This eliminates the conditional entirely for SERIAL_CORE_CONSOLE=n case. While here, refactor the conditional to be more compact.
--- drivers/tty/serial/serial_core.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)
drivers/tty/serial/serial_core.c +5 -7
@@ -3315,14 +3315,12 @@ int uart_handle_break(struct uart_port *port)if (port->handle_break) port->handle_break (port); -if (port->has_sysrq) { -if (port->cons && port->cons->index == port->line) { -if (!port->sysrq) { - port->sysrq = jiffies +SYSRQ_TIMEOUT ; -return 1 ; - } - port->sysrq =0 ; +if (port->has_sysrq &&uart_console (port)) { +if (!port->sysrq) { + port->sysrq = jiffies +SYSRQ_TIMEOUT ; +return 1 ; } + port->sysrq =0 ; }if (port->flags &UPF_SAK )
Signature · 1 line
2.25.1
PATCH 4/4 serial: core: Refactor uart_unlock_and_check_sysrq()
In reply to Andy ShevchenkoRefactor uart_unlock_and_check_sysrq() to:
- explicitly show that we release a port lock which makes static analyzers happy:
CHECK drivers/tty/serial/serial_core.c.../serial_core.c:3290:17: warning: context imbalance in 'uart_unlock_and_check_sysrq' - unexpected unlock
- use flags instead of irqflags to avoid confusion with IRQ flags
- provide one return point
- be more compact
--- drivers/tty/serial/serial_core.c | 22 ++++++++++------------ include/linux/serial_core.h | 3 +-- 2 files changed, 11 insertions(+), 14 deletions(-)
drivers/tty/serial/serial_core.c +10 -12
@@ -3286,22 +3286,20 @@ int uart_prepare_sysrq_char(struct uart_port *port, unsigned int ch) }EXPORT_SYMBOL_GPL (uart_prepare_sysrq_char); -void uart_unlock_and_check_sysrq (struct uart_port *port,unsigned long irqflags) +void uart_unlock_and_check_sysrq (struct uart_port *port,unsigned long flags) +__releases(&port->lock) {int sysrq_ch; -if (!port->has_sysrq) { -spin_unlock_irqrestore (&port->lock, irqflags); -return ; +if (port->has_sysrq) { + sysrq_ch = port->sysrq_ch; + port->sysrq_ch =0 ; +spin_unlock_irqrestore (&port->lock, flags); +if (sysrq_ch) +handle_sysrq (sysrq_ch); + }else { +spin_unlock_irqrestore (&port->lock, flags); } - - sysrq_ch = port->sysrq_ch; - port->sysrq_ch =0 ; - -spin_unlock_irqrestore (&port->lock, irqflags); - -if (sysrq_ch) -handle_sysrq (sysrq_ch); }EXPORT_SYMBOL_GPL (uart_unlock_and_check_sysrq);
include/linux/serial_core.h +1 -2
@@ -447,8 +447,7 @@ extern void uart_insert_char(struct uart_port *port, unsigned int status,extern int uart_handle_sysrq_char (struct uart_port *port,unsigned int ch);extern int uart_prepare_sysrq_char (struct uart_port *port,unsigned int ch); -extern void uart_unlock_and_check_sysrq (struct uart_port *port, -unsigned long irqflags); +extern void uart_unlock_and_check_sysrq (struct uart_port *port,unsigned long flags);extern int uart_handle_break (struct uart_port *port);/*
Signature · 1 line
2.25.1
Hi Andy,
thanks for the patch,
On 3/10/20 1:20 PM, Andy Shevchenko wrote:
[..]> @@ -3209,7 +3209,9 @@ static DECLARE_WORK(sysrq_enable_work, uart_sysrq_on);
drivers/tty/serial/serial_core.c static DECLARE_WORK(sysrq_enable_work, uart_sysrq_on); · line 3215
Quoted 8 lines
*/
static booluart_try_toggle_sysrq(struct uart_port *port, unsigned int ch){drivers/tty/serial/serial_core.c +3
-1-if (ARRAY_SIZE (sysrq_toggle_seq) <=1 ) +int sysrq_toggle_seq_len =strlen (sysrq_toggle_seq); + +if (!sysrq_toggle_seq_len)return false;
Eh, I wanted to avoid the strlen() call in runtime for every time sysrq is pressed. It's not very frequent moment surely, but..
Could you try
: int sysrq_toggle_seq_len = ARRAY_SIZE(sysrq_toggle_seq); :
: if (sysrq_toggle_seq_len <= 1)
: return false;
: /* ... */
: port->sysrq_seq++;
: if (port->sysrq_seq + 1 < sysrq_toggle_seq_len) {
if this will shut the warning instead?
BTW, is this gcc 10 you see the warning with?
I have gcc (GCC) 9.2.0 and I don't see a warning with/without the config string.
Thanks,
Dmitry
Re: [PATCH v1 2/4] serial: core: Print escaped SysRq Magic sequence if enabled
In reply to Andy ShevchenkoQuoted 4 lines · Andy Shevchenko
It is useful to see on the serial console the magic sequence itself to enable SysRq without rummaging source code.
Signed-off-by Andy Shevchenko <[email protected]>
Quoted 12 lines
--- drivers/tty/serial/serial_core.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)drivers/tty/serial/serial_core.c +0
-0@@ -3191,8 +3191,11 @@ static const char sysrq_toggle_seq[] = CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE;static void uart_sysrq_on (struct work_struct *w)
drivers/tty/serial/serial_core.c static const char sysrq_toggle_seq[] = CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE; · line 3201
Quoted 10 lines
drivers/tty/serial/serial_core.c +4
-1{ +int sysrq_toggle_seq_len =strlen (sysrq_toggle_seq); +sysrq_toggle_support (1 ); -pr_info ("SysRq is enabled by magic sequence on serial\n" ); +pr_info ("SysRq is enabled by magic sequence '%*pE' on serial\n" , + sysrq_toggle_seq_len, sysrq_toggle_seq); } static DECLARE_WORK(sysrq_enable_work, uart_sysrq_on);
Thanks,
Dmitry
Re: [PATCH v1 3/4] serial: core: Use uart_console() helper in SysRq code
In reply to Andy ShevchenkoQuoted 5 lines · Andy Shevchenko
Use
uart_console()helper in SysRq code instead of open coded variant. This eliminates the conditional entirely for SERIAL_CORE_CONSOLE=n case. While here, refactor the conditional to be more compact.Signed-off-by Andy Shevchenko <[email protected]>
Thanks,
Quoted 19 lines
--- drivers/tty/serial/serial_core.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)drivers/tty/serial/serial_core.c +0
-6@@ -3315,14 +3315,12 @@ int uart_handle_break(struct uart_port *port)if (port->handle_break) port->handle_break (port); -if (port->has_sysrq) { -if (port->cons && port->cons->index == port->line) { -if (!port->sysrq) { - port->sysrq = jiffies +SYSRQ_TIMEOUT ; -return 1 ; - }
drivers/tty/serial/serial_core.c int uart_handle_break(struct uart_port *port) · line 3326
Quoted 10 lines
drivers/tty/serial/serial_core.c +5
-1- port->sysrq =0 ; +if (port->has_sysrq &&uart_console (port)) { +if (!port->sysrq) { + port->sysrq = jiffies +SYSRQ_TIMEOUT ; +return 1 ; } + port->sysrq =0 ; }if (port->flags &UPF_SAK )
Thanks,
Dmitry
Re: [PATCH v1 4/4] serial: core: Refactor uart_unlock_and_check_sysrq()
In reply to Andy ShevchenkoOn 3/10/20 1:20 PM, Andy Shevchenko wrote:
[..]
drivers/tty/serial/serial_core.c int uart_prepare_sysrq_char(struct uart_port *port, unsigned int ch) · line 3292
Quoted 9 lines
drivers/tty/serial/serial_core.c +2
-1@@ -3286,22 +3286,20 @@ int uart_prepare_sysrq_char(struct uart_port *port, unsigned int ch) }EXPORT_SYMBOL_GPL (uart_prepare_sysrq_char); -void uart_unlock_and_check_sysrq (struct uart_port *port,unsigned long irqflags) +void uart_unlock_and_check_sysrq (struct uart_port *port,unsigned long flags) +__releases(&port->lock) {int sysrq_ch;
Probably, you could move it inside the condition it's used. Though, I wonder why you decided to rearrange the code. Otherwise, LGTM.
Quoted 13 lines
drivers/tty/serial/serial_core.c +8
-3-if (!port->has_sysrq) { -spin_unlock_irqrestore (&port->lock, irqflags); -return ; +if (port->has_sysrq) { + sysrq_ch = port->sysrq_ch; + port->sysrq_ch =0 ; +spin_unlock_irqrestore (&port->lock, flags); +if (sysrq_ch) +handle_sysrq (sysrq_ch); + }else { +spin_unlock_irqrestore (&port->lock, flags); }
drivers/tty/serial/serial_core.c int uart_prepare_sysrq_char(struct uart_port *port, unsigned int ch) · line 3304
Quoted 10 lines
drivers/tty/serial/serial_core.c +0
-8- - sysrq_ch = port->sysrq_ch; - port->sysrq_ch =0 ; - -spin_unlock_irqrestore (&port->lock, irqflags); - -if (sysrq_ch) -handle_sysrq (sysrq_ch); }EXPORT_SYMBOL_GPL (uart_unlock_and_check_sysrq);
Thanks,
Dmitry
Quoted 14 lines · Dmitry Safonov
On 3/10/20 1:20 PM, Andy Shevchenko wrote:
[..]> @@ -3209,7 +3209,9 @@ staticDECLARE_WORK(sysrq_enable_work, uart_sysrq_on);drivers/tty/serial/serial_core.c static DECLARE_WORK(sysrq_enable_work, uart_sysrq_on); · line 3215
Quoted 8 lines
*/
static booluart_try_toggle_sysrq(struct uart_port *port, unsigned int ch){drivers/tty/serial/serial_core.c +3
-1-if (ARRAY_SIZE (sysrq_toggle_seq) <=1 ) +int sysrq_toggle_seq_len =strlen (sysrq_toggle_seq); + +if (!sysrq_toggle_seq_len)return false;Eh, I wanted to avoid the
strlen()call in runtime for every time sysrq is pressed. It's not very frequent moment surely, but..
I really don't like ARRAY_SIZE() against plain strings. This will use \0 inclusively and confuse the understanding the code.
Quoted 1 line
Could you try
I even can tell you w/o trying, that it will fix this (yes, it does), but see above.
Quoted 8 lines
: int
sysrq_toggle_seq_len=ARRAY_SIZE(sysrq_toggle_seq); :
: if (sysrq_toggle_seq_len<= 1)
: return false;
: /* ... */
: port->sysrq_seq++;
: if (port->sysrq_seq+ 1 <sysrq_toggle_seq_len) {
Quoted 4 lines
if this will shut the warning instead?
BTW, is this gcc 10 you see the warning with?
I have gcc (GCC) 9.2.0 and I don't see a warning with/without the config string.
gcc (Debian 9.2.1-30) 9.2.1 20200224
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v1 4/4] serial: core: Refactor uart_unlock_and_check_sysrq()
In reply to Dmitry SafonovQuoted 6 lines · Dmitry Safonov
On 3/10/20 1:20 PM, Andy Shevchenko wrote:
[..]Quoted 4 lines
drivers/tty/serial/serial_core.c +0
-0@@ -3286,22 +3286,20 @@ int uart_prepare_sysrq_char(struct uart_port *port, unsigned int ch) }EXPORT_SYMBOL_GPL (uart_prepare_sysrq_char);
Quoted 7 lines
drivers/tty/serial/serial_core.c int uart_prepare_sysrq_char(struct uart_port *port, unsigned int ch) · line 3292
Quoted 5 lines
drivers/tty/serial/serial_core.c +2
-1-void uart_unlock_and_check_sysrq (struct uart_port *port,unsigned long irqflags) +void uart_unlock_and_check_sysrq (struct uart_port *port,unsigned long flags) +__releases(&port->lock) {int sysrq_ch;Probably, you could move it inside the condition it's used.
I can do this.
Quoted 1 line
Though, I wonder why you decided to rearrange the code.
I hope commit message sheds a light on this. Main reason to (easily) see how locks are being maintained.
Quoted 1 line
Otherwise, LGTM.
Thanks, I will send v2 when we get settlement on patch 1.
Quoted 25 lines
Quoted 12 lines
drivers/tty/serial/serial_core.c +8
-3-if (!port->has_sysrq) { -spin_unlock_irqrestore (&port->lock, irqflags); -return ; +if (port->has_sysrq) { + sysrq_ch = port->sysrq_ch; + port->sysrq_ch =0 ; +spin_unlock_irqrestore (&port->lock, flags); +if (sysrq_ch) +handle_sysrq (sysrq_ch); + }else { +spin_unlock_irqrestore (&port->lock, flags); }drivers/tty/serial/serial_core.c int uart_prepare_sysrq_char(struct uart_port *port, unsigned int ch) · line 3304
Quoted 10 lines
drivers/tty/serial/serial_core.c +0
-8- - sysrq_ch = port->sysrq_ch; - port->sysrq_ch =0 ; - -spin_unlock_irqrestore (&port->lock, irqflags); - -if (sysrq_ch) -handle_sysrq (sysrq_ch); }EXPORT_SYMBOL_GPL (uart_unlock_and_check_sysrq);Thanks,
Dmitry
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Dmitry Safonov
On 3/10/20 1:20 PM, Andy Shevchenko wrote:
...
Quoted 5 lines
Quoted 3 lines
BTW, is this gcc 10 you see the warning with?
I have gcc (GCC) 9.2.0 and I don't see a warning with/without the config string.gcc (Debian 9.2.1-30) 9.2.1 20200224
I think it will be even on older versions.
I usually run build with make O=... W=1 C=1 CF=-D__CHECK_ENDIAN__ -j64.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Hi Andy,
Quoted 16 lines · Andy Shevchenko
Quoted 14 lines · Dmitry Safonov
On 3/10/20 1:20 PM, Andy Shevchenko wrote:
[..]> @@ -3209,7 +3209,9 @@ staticDECLARE_WORK(sysrq_enable_work, uart_sysrq_on);drivers/tty/serial/serial_core.c static DECLARE_WORK(sysrq_enable_work, uart_sysrq_on); · line 3215
Quoted 8 lines
*/
static booluart_try_toggle_sysrq(struct uart_port *port, unsigned int ch){drivers/tty/serial/serial_core.c +3
-1-if (ARRAY_SIZE (sysrq_toggle_seq) <=1 ) +int sysrq_toggle_seq_len =strlen (sysrq_toggle_seq); + +if (!sysrq_toggle_seq_len)return false;Eh, I wanted to avoid the
strlen()call in runtime for every time sysrq is pressed. It's not very frequent moment surely, but..
Quoted 2 lines
I really don't like
ARRAY_SIZE()against plain strings. This will use \0 inclusively and confuse the understanding the code.
I still feel a bit awkward that handler will run strlen() for something that could be done compile-time, but I won't insist.
Thanks again for looking into warning,
Dmitry
Quoted 19 lines · Dmitry Safonov
Hi Andy,
Quoted 16 lines · Andy Shevchenko
Quoted 14 lines · Dmitry Safonov
On 3/10/20 1:20 PM, Andy Shevchenko wrote:
[..]> @@ -3209,7 +3209,9 @@ staticDECLARE_WORK(sysrq_enable_work, uart_sysrq_on);drivers/tty/serial/serial_core.c static DECLARE_WORK(sysrq_enable_work, uart_sysrq_on); · line 3215
Quoted 8 lines
*/
static booluart_try_toggle_sysrq(struct uart_port *port, unsigned int ch){drivers/tty/serial/serial_core.c +3
-1-if (ARRAY_SIZE (sysrq_toggle_seq) <=1 ) +int sysrq_toggle_seq_len =strlen (sysrq_toggle_seq); + +if (!sysrq_toggle_seq_len)return false;Eh, I wanted to avoid the
strlen()call in runtime for every time sysrq is pressed. It's not very frequent moment surely, but..
Quoted 5 lines
Quoted 2 lines
I really don't like
ARRAY_SIZE()against plain strings. This will use \0 inclusively and confuse the understanding the code.I still feel a bit awkward that handler will run
strlen()for something that could be done compile-time, but I won't insist.
Have you seen to the assembly?
For me it seems that GCC is clever enough to precalc length for constant literals.
With default string (empty) there is no uart_sysrq_on() at all in the code.
With "pqr" I see this function and in particular:
621: be 03 00 00 00 mov $0x3,%esi
With "pqrst"
621: be 05 00 00 00 mov $0x5,%esi
(Note, I compiled entire series, that's why uart_sysrq_on() has strlen() in)
Quoted 1 line
Thanks again for looking into warning,
You are welcome.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 8 lines · Andy Shevchenko
Have you seen to the assembly?
For me it seems that GCC is clever enough to precalc length for constant literals.
With default string (empty) there is no
uart_sysrq_on()at all in the code.
Quoted 8 lines
With "pqr" I see this function and in particular:
621: be 03 00 00 00 mov $0x3,%esiWith "pqrst"
621: be 05 00 00 00 mov $0x5,%esi(Note, I compiled entire series, that's why
uart_sysrq_on()hasstrlen()in)
Ah, that solves all worries I have.
Thanks again,
Dmitry
Quoted 14 lines · Andy Shevchenko
Compiler is not happy about using
ARRAY_SIZE()in comparison to smaller type:CC
drivers/tty/serial/serial_core.o.../serial_core.c: In function ‘uart_try_toggle_sysrq’:.../serial_core.c:3222:24: warning: comparison is always false due to limited range of data type [-Wtype-limits]
3222 | if (++port->sysrq_seq < (ARRAY_SIZE(sysrq_toggle_seq)- 1)) {
| ^Looking at the code it appears that there is an additional weirdness, i.e. use
ARRAY_SIZE()against simple string literal. Yes, the idea probably was to allow '\0' in the sequence, but it's impractical: kernel configuration won't accept it to begin with followed by a comment about '\0' before comparison in question.
Quoted 4 lines
Drop all these by switching to
strlen()and convert code accordingly.Fixes 68af43173d3f ("serial/sysrq: Add MAGIC_SYSRQ_SERIAL_SEQUENCE")Signed-off-by Andy Shevchenko <[email protected]>
Thanks!
Quoted 30 lines
--- drivers/tty/serial/serial_core.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)drivers/tty/serial/serial_core.c +4
-3@@ -3209,7 +3209,9 @@ static DECLARE_WORK(sysrq_enable_work, uart_sysrq_on);*/ static bool uart_try_toggle_sysrq (struct uart_port *port,unsigned int ch) { -if (ARRAY_SIZE (sysrq_toggle_seq) <=1 ) +int sysrq_toggle_seq_len =strlen (sysrq_toggle_seq); + +if (!sysrq_toggle_seq_len)return false;BUILD_BUG_ON (ARRAY_SIZE (sysrq_toggle_seq) >=U8_MAX );@@ -3218,8 +3220,7 @@ static bool uart_try_toggle_sysrq(struct uart_port *port, unsigned int ch)return false; } -/* Without the last \0 */ -if (++port->sysrq_seq < (ARRAY_SIZE (sysrq_toggle_seq) -1 )) { +if (++port->sysrq_seq < sysrq_toggle_seq_len) { port->sysrq = jiffies +SYSRQ_TIMEOUT ;return true; }
Signature · 1 line
Dima