Skip to contentSkip to conversation

serial/sysrq: Add MAGIC_SYSRQ_SERIAL_SEQUENCE

PATCH In review Dmitry Safonov 4 participants 9 messages Mar 2, 2020 – Apr 1, 2020

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v3 cover serial/sysrq: Add MAGIC_SYSRQ_SERIAL_SEQUENCE

Magic sysrq has proven for Arista usecases to be useful for debugging issues in field, over serial line when the switch is in such bad state that it can't accept network connections anymore.

Unfortunately, having sysrq always enabled doesn't work for some embedded boards that tend to generate garbage on serial line (including BREAKs). Since commit 732dbf3a6104 ("serial: do not accept sysrq characters via serial port"), it's possible to keep sysrq enabled, but over serial line.

Add a way to enable sysrq on a uart, where currently it can be constantly either on or off (CONFIG_MAGIC_SYSRQ_SERIAL). While doing so, cleanup __sysrq_enabled and serial_core header file.

Changes since v2 [2]:
- sysrq_get_mask() renamed to sysrq_mask() as there isn't sysrq_put_mask(); acquired kernel-doc (by Greg's review, thanks)
- uart_try_toggle_sysrq() now returns true/false instead 1/0 as it's a bool function (nits by Joe Perches, thanks!)
- Dropped "sizeof(port->sysrq_seq)*U8_MAX" and used U8_MAX (Randy Dunlap)

Changes since v1 [1]:
- Fix typo in pr_info() message (noticed by Randy Dunlap, thanks)
- Add SYSRQ_TIMEOUT define for timeout after BREAK and separate removing @unused member of uart_port into cleanup patch (by Greg's review, thanks)
- Add const qualifier, make uart_try_toggle_sysrq() bool function (Joe Perches, thanks)
- Fix !CONFIG_SYSRQ and CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE="" build failures (kudos to kbuild test robot)

[1]: https://lkml.kernel.org/r/[email protected]
[2]: https://lkml.kernel.org/r/[email protected]

Cc Greg Kroah-Hartman <[email protected]>
Cc Iurii Zaikin <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Joe Perches <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vasiliy Khoruzhick <[email protected]>

Thanks,
Dmitry

Dmitry Safonov (2):
sysctl/sysrq: Remove __sysrq_enabled copy
serial/sysrq: Add MAGIC_SYSRQ_SERIAL_SEQUENCE

 drivers/tty/serial/serial_core.c | 75 +++++++++++++++++++++++++++++---
 drivers/tty/sysrq.c              | 12 +++++
 include/linux/serial_core.h      |  1 +
 include/linux/sysrq.h            |  7 +++
 kernel/sysctl.c                  | 41 +++++++++--------
 lib/Kconfig.debug                |  8 ++++
 6 files changed, 118 insertions(+), 26 deletions(-)
Signature · 1 line
2.25.0
#1 lore raw

PATCH v3 1/2 sysctl/sysrq: Remove __sysrq_enabled copy

Many embedded boards have a disconnected TTL level serial which can generate some garbage that can lead to spurious false sysrq detects.

Currently, sysrq can be either completely disabled for serial console or always disabled (with CONFIG_MAGIC_SYSRQ_SERIAL), since
commit 732dbf3a6104 ("serial: do not accept sysrq characters via serial port")

At Arista, we have such boards that can generate BREAK and random garbage. While disabling sysrq for serial console would solve the problem with spurious false sysrq triggers, it's also desirable to have a way to enable sysrq back.

Having the way to enable sysrq was beneficial to debug lockups with a manual investigation in field and on the other side preventing false sysrq detections.

As a preparation to add sysrq_toggle_support() call into uart, remove a private copy of sysrq_enabled from sysctl - it should reflect the actual status of sysrq.

Furthermore, the private copy isn't correct already in case sysrq_always_enabled is true. So, remove __sysrq_enabled and use a getter-helper sysrq_mask() to check sysrq_key_op enabled status.

Cc Iurii Zaikin <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Luis Chamberlain <[email protected]>
Cc Kees Cook <[email protected]>
---
 drivers/tty/sysrq.c   | 12 ++++++++++++
 include/linux/sysrq.h |  7 +++++++
 kernel/sysctl.c       | 41 ++++++++++++++++++++++-------------------
 3 files changed, 41 insertions(+), 19 deletions(-)
drivers/tty/sysrq.c +12 -0
index f724962a5906..5e0d0813da55 100644
@@ -63,6 +63,18 @@ static bool sysrq_on(void)
 	return sysrq_enabled || sysrq_always_enabled;
 }
 
+/**
+ * sysrq_mask - Getter for sysrq_enabled mask.
+ *
+ * Return: 1 if sysrq is always enabled, enabled sysrq_key_op mask otherwise.
+ */
+int sysrq_mask(void)
+{
+	if (sysrq_always_enabled)
+		return 1;
+	return sysrq_enabled;
+}
+
 /*
  * A value of 1 means 'all', other nonzero values are an op mask:
  */
include/linux/sysrq.h +7 -0
index 8c71874e8485..8e159e16850f 100644
@@ -50,6 +50,7 @@ int unregister_sysrq_key(int key, struct sysrq_key_op *op);
 struct sysrq_key_op *__sysrq_get_key_op(int key);
 
 int sysrq_toggle_support(int enable_mask);
+int sysrq_mask(void);
 
 #else
 
@@ -71,6 +72,12 @@ static inline int unregister_sysrq_key(int key, struct sysrq_key_op *op)
 	return -EINVAL;
 }
 
+static inline int sysrq_mask(void)
+{
+	/* Magic SysRq disabled mask */
+	return 0;
+}
+
 #endif
 
 #endif /* _LINUX_SYSRQ_H */
kernel/sysctl.c +22 -19

60 lines not on this page · raw message

Signature · 1 line
2.25.0
#4 lore raw

PATCH v3 2/2 serial/sysrq: Add MAGIC_SYSRQ_SERIAL_SEQUENCE

In reply to Dmitry Safonov

Many embedded boards have a disconnected TTL level serial which can generate some garbage that can lead to spurious false sysrq detects.

Currently, sysrq can be either completely disabled for serial console or always disabled (with CONFIG_MAGIC_SYSRQ_SERIAL), since
commit 732dbf3a6104 ("serial: do not accept sysrq characters via serial port")

At Arista, we have such boards that can generate BREAK and random garbage. While disabling sysrq for serial console would solve the problem with spurious false sysrq triggers, it's also desirable to have a way to enable sysrq back.

As a measure of balance between on and off options, add MAGIC_SYSRQ_SERIAL_SEQUENCE which is a string sequence that can enable sysrq if it follows BREAK on a serial line. The longer the string - the less likely it may be in the garbage.

Having the way to enable sysrq was beneficial to debug lockups with a manual investigation in field and on the other side preventing false sysrq detections.

---
 drivers/tty/serial/serial_core.c | 75 +++++++++++++++++++++++++++++---
 include/linux/serial_core.h      |  1 +
 lib/Kconfig.debug                |  8 ++++
 3 files changed, 77 insertions(+), 7 deletions(-)
drivers/tty/serial/serial_core.c +68 -7
index 76e506ee335c..83151da74a15 100644
@@ -20,6 +20,7 @@
 #include <linux/device.h>
 #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
 #include <linux/serial_core.h>
+#include <linux/sysrq.h>
 #include <linux/delay.h>
 #include <linux/mutex.h>
 #include <linux/security.h>
@@ -40,6 +41,8 @@ static struct lock_class_key port_lock_key;
 
 #define HIGH_BITS_OFFSET	((sizeof(long)-sizeof(int))*8)
 
+#define SYSRQ_TIMEOUT	(HZ * 5)
+
 static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
 					struct ktermios *old_termios);
 static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
@@ -3082,6 +3085,56 @@ void uart_insert_char(struct uart_port *port, unsigned int status,
 }
 EXPORT_SYMBOL_GPL(uart_insert_char);
 
+#ifdef CONFIG_MAGIC_SYSRQ_SERIAL
+static const char sysrq_toggle_seq[] = CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE;
+
+static void uart_sysrq_on(struct work_struct *w)
+{
+	sysrq_toggle_support(1);
+	pr_info("SysRq is enabled by magic sequence on serial\n");
+}
+static DECLARE_WORK(sysrq_enable_work, uart_sysrq_on);
+
+/**
+ *	uart_try_toggle_sysrq - Enables SysRq from serial line
+ *	@port: uart_port structure where char(s) after BREAK met
+ *	@ch: new character in the sequence after received BREAK
+ *
+ *	Enables magic SysRq when the required sequence is met on port
+ *	(see CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE).
+ *
+ *	Returns false if @ch is out of enabling sequence and should be
+ *	handled some other way, true if @ch was consumed.
+ */
+static bool uart_try_toggle_sysrq(struct uart_port *port, unsigned int ch)
+{
+	if (ARRAY_SIZE(sysrq_toggle_seq) <= 1)
+		return false;
+
+	BUILD_BUG_ON(ARRAY_SIZE(sysrq_toggle_seq) >= U8_MAX);
+	if (sysrq_toggle_seq[port->sysrq_seq] != ch) {
+		port->sysrq_seq = 0;
+		return false;
+	}
+
+	/* Without the last \0 */
+	if (++port->sysrq_seq < (ARRAY_SIZE(sysrq_toggle_seq) - 1)) {
+		port->sysrq = jiffies + SYSRQ_TIMEOUT;
+		return true;
+	}
+
+	schedule_work(&sysrq_enable_work);
+
+	port->sysrq = 0;
+	return true;
+}
+#else
+static inline bool uart_try_toggle_sysrq(struct uart_port *port, unsigned int ch)
+{
+	return false;
+}
+#endif
+
 int uart_handle_sysrq_char(struct uart_port *port, unsigned int ch)
 {
 	if (!IS_ENABLED(CONFIG_MAGIC_SYSRQ_SERIAL))
@@ -3091,9 +3144,13 @@ int uart_handle_sysrq_char(struct uart_port *port, unsigned int ch)
 		return 0;
 
 	if (ch && time_before(jiffies, port->sysrq)) {
-		handle_sysrq(ch);
-		port->sysrq = 0;
-		return 1;
+		if (sysrq_mask()) {
+			handle_sysrq(ch);
+			port->sysrq = 0;
+			return 1;
+		}
+		if (uart_try_toggle_sysrq(port, ch))
+			return 1;
 	}
 	port->sysrq = 0;
 
@@ -3110,9 +3167,13 @@ int uart_prepare_sysrq_char(struct uart_port *port, unsigned int ch)
 		return 0;
 
 	if (ch && time_before(jiffies, port->sysrq)) {
-		port->sysrq_ch = ch;
-		port->sysrq = 0;
-		return 1;
+		if (sysrq_mask()) {
+			port->sysrq_ch = ch;
+			port->sysrq = 0;
+			return 1;
+		}
+		if (uart_try_toggle_sysrq(port, ch))
+			return 1;
 	}
 	port->sysrq = 0;
 
@@ -3152,7 +3213,7 @@ int uart_handle_break(struct uart_port *port)
 	if (port->has_sysrq) {
 		if (port->cons && port->cons->index == port->line) {
 			if (!port->sysrq) {
-				port->sysrq = jiffies + HZ*5;
+				port->sysrq = jiffies + SYSRQ_TIMEOUT;
 				return 1;
 			}
 			port->sysrq = 0;
include/linux/serial_core.h +1 -0
index 52404ef1694e..1f4443db5474 100644
@@ -243,6 +243,7 @@ struct uart_port {
 	unsigned long		sysrq;			/* sysrq timeout */
 	unsigned int		sysrq_ch;		/* char for sysrq */
 	unsigned char		has_sysrq;
+	unsigned char		sysrq_seq;		/* index in sysrq_toggle_seq */
 
 	unsigned char		hub6;			/* this should be in the 8250 driver */
 	unsigned char		suspended;
lib/Kconfig.debug +8 -0
index 69def4a9df00..38a8f3c99579 100644
@@ -431,6 +431,14 @@ config MAGIC_SYSRQ_SERIAL
 	  This option allows you to decide whether you want to enable the
 	  magic SysRq key.
 
+config MAGIC_SYSRQ_SERIAL_SEQUENCE
+	string "Char sequence that enables magic SysRq over serial"
+	depends on MAGIC_SYSRQ_SERIAL
+	default ""
+	help
+	  Specifies a sequence of characters that can follow BREAK to enable
+	  SysRq on a serial console.
+
 config DEBUG_FS
 	bool "Debug Filesystem"
 	help
Signature · 1 line
2.25.0
#5 lore raw
Thank you for the patch! Perhaps something to improve:

Hi Dmitry,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on tty/tty-testing]
[also build test WARNING on usb/usb-testing linus/master v5.6-rc4 next-20200302] [cannot apply to linux/master]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Dmitry-Safonov/serial-sysrq-Add-MAGIC_SYSRQ_SERIAL_SEQUENCE/20200303-041809
base:   https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty.git tty-testing

If you fix the issue, kindly add following tag

New smatch warnings:
drivers/tty/serial/serial_core.c:3123 uart_try_toggle_sysrq() warn: unsigned '++port->sysrq_seq' is never less than zero.

Old smatch warnings:
drivers/tty/serial/serial_core.c:298 uart_shutdown() error: we previously assumed 'uport' could be null (see line 294) drivers/tty/serial/serial_core.c:2741 iomem_base_show() warn: argument 4 to %lX specifier is cast from pointer

vim +3123 drivers/tty/serial/serial_core.c

3099
3100 /**
3101 * uart_try_toggle_sysrq - Enables SysRq from serial line
3102 * @port: uart_port structure where char(s) after BREAK met
3103 * @ch: new character in the sequence after received BREAK
3104 *
3105 * Enables magic SysRq when the required sequence is met on port
3106 * (see CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE).
3107 *
3108 * Returns false if @ch is out of enabling sequence and should be
3109 * handled some other way, true if @ch was consumed.
3110 */
3111 static bool uart_try_toggle_sysrq(struct uart_port *port, unsigned int ch)
3112 {
3113 if (ARRAY_SIZE(sysrq_toggle_seq) <= 1)
3114 return false;
3115
3116 BUILD_BUG_ON(ARRAY_SIZE(sysrq_toggle_seq) >= U8_MAX);
3117 if (sysrq_toggle_seq[port->sysrq_seq] != ch) {
3118 port->sysrq_seq = 0;
3119 return false;
3120 }
3121
3122 /* Without the last \0 */

> 3123		if (++port->sysrq_seq < (ARRAY_SIZE(sysrq_toggle_seq) - 1)) {

3124 port->sysrq = jiffies + SYSRQ_TIMEOUT;
3125 return true;
3126 }
3127
3128 schedule_work(&sysrq_enable_work);
3129
3130 port->sysrq = 0;
3131 return true;
3132 }
3133 #else
3134 static inline bool uart_try_toggle_sysrq(struct uart_port *port, unsigned int ch)
3135 {
3136 return false;
3137 }
3138 #endif
3139

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#6 lore raw
Quoted 1 line · kbuild test robot

Hi Dmitry,

[..]

Quoted 2 lines

New smatch warnings:
drivers/tty/serial/serial_core.c:3123 uart_try_toggle_sysrq() warn: unsigned '++port->sysrq_seq' is never less than zero.

False-positive as just a few lines above:
: if (ARRAY_SIZE(sysrq_toggle_seq) <= 1)
: return false;

I hope it's fine just to ignore this rather than complicate anyhow code to suppress the warning.

Thanks,
Dmitry

#7 lore raw
In reply to Dmitry Safonov
Quoted 5 lines · Dmitry Safonov
lib/Kconfig.debug +0 -0
index 69def4a9df00..38a8f3c99579 100644

lib/Kconfig.debug config MAGIC_SYSRQ_SERIAL · line 440

Quoted 10 lines
lib/Kconfig.debug +7 -0
@@ -431,6 +431,14 @@ config MAGIC_SYSRQ_SERIAL
 	  This option allows you to decide whether you want to enable the
 	  magic SysRq key.
 
+config MAGIC_SYSRQ_SERIAL_SEQUENCE
+	string "Char sequence that enables magic SysRq over serial"
+	depends on MAGIC_SYSRQ_SERIAL
+	default ""
+	help
+	  Specifies a sequence of characters that can follow BREAK to enable
+	  SysRq on a serial console.

Can you send a follow-on patch that adds some more text here. Something like :
If you do not know what to do, just select an empty string and the option will not be enabled.

or something like that to make it more obvious what almost everyone will want to do here.

thanks,

greg k-h

#8 lore raw
Quoted 23 lines · Greg Kroah-Hartman
Quoted 5 lines · Dmitry Safonov
lib/Kconfig.debug +0 -0
index 69def4a9df00..38a8f3c99579 100644

lib/Kconfig.debug config MAGIC_SYSRQ_SERIAL · line 440

Quoted 10 lines
lib/Kconfig.debug +7 -0
@@ -431,6 +431,14 @@ config MAGIC_SYSRQ_SERIAL
 	  This option allows you to decide whether you want to enable the
 	  magic SysRq key.
 
+config MAGIC_SYSRQ_SERIAL_SEQUENCE
+	string "Char sequence that enables magic SysRq over serial"
+	depends on MAGIC_SYSRQ_SERIAL
+	default ""
+	help
+	  Specifies a sequence of characters that can follow BREAK to enable
+	  SysRq on a serial console.

Can you send a follow-on patch that adds some more text here. Something like :
If you do not know what to do, just select an empty string and the option will not be enabled.

Quoted 2 lines

or something like that to make it more obvious what almost everyone will want to do here.

Oh yes, makes sense, will send a patch.

Thanks,
Dmitry

#2 lore raw

Re: [PATCHv3 1/2] sysctl/sysrq: Remove __sysrq_enabled copy

In reply to Dmitry Safonov
Quoted 45 lines · Dmitry Safonov

Many embedded boards have a disconnected TTL level serial which can generate some garbage that can lead to spurious false sysrq detects.

Currently, sysrq can be either completely disabled for serial console or always disabled (with CONFIG_MAGIC_SYSRQ_SERIAL), since
commit 732dbf3a6104 ("serial: do not accept sysrq characters via serial port")

At Arista, we have such boards that can generate BREAK and random garbage. While disabling sysrq for serial console would solve the problem with spurious false sysrq triggers, it's also desirable to have a way to enable sysrq back.

Having the way to enable sysrq was beneficial to debug lockups with a manual investigation in field and on the other side preventing false sysrq detections.

As a preparation to add sysrq_toggle_support() call into uart, remove a private copy of sysrq_enabled from sysctl - it should reflect the actual status of sysrq.

Furthermore, the private copy isn't correct already in case sysrq_always_enabled is true. So, remove __sysrq_enabled and use a getter-helper sysrq_mask() to check sysrq_key_op enabled status.

Cc Iurii Zaikin <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Luis Chamberlain <[email protected]>
Cc Kees Cook <[email protected]>
---
 drivers/tty/sysrq.c   | 12 ++++++++++++
 include/linux/sysrq.h |  7 +++++++
 kernel/sysctl.c       | 41 ++++++++++++++++++++++-------------------
 3 files changed, 41 insertions(+), 19 deletions(-)
drivers/tty/sysrq.c +1 -0
index f724962a5906..5e0d0813da55 100644
@@ -63,6 +63,18 @@ static bool sysrq_on(void)
 	return sysrq_enabled || sysrq_always_enabled;
 }
 
+/**

drivers/tty/sysrq.c static bool sysrq_on(void) · line 76

Quoted 10 lines
drivers/tty/sysrq.c +10 -0
+ * sysrq_mask - Getter for sysrq_enabled mask.
+ *
+ * Return: 1 if sysrq is always enabled, enabled sysrq_key_op mask otherwise.
+ */
+int sysrq_mask(void)
+{
+	if (sysrq_always_enabled)
+		return 1;
+	return sysrq_enabled;
+}

This seems to have broken several configs, when serial_core is modular, with:

ERROR: modpost: "sysrq_mask" [drivers/tty/serial/serial_core.ko] undefined!

See:

http://kisskb.ellerman.id.au/kisskb/buildresult/14169386/

It's also being reported by the kernelci bot:

https://lore.kernel.org/linux-next/[email protected]/

cheers

#3 lore raw

Re: [PATCHv3 1/2] sysctl/sysrq: Remove __sysrq_enabled copy

Hi Michael,

On 4/1/20 1:22 PM, Michael Ellerman wrote:
[..]

Quoted 17 lines

drivers/tty/sysrq.c static bool sysrq_on(void) · line 76

Quoted 12 lines
drivers/tty/sysrq.c +11 -0
+/**
+ * sysrq_mask - Getter for sysrq_enabled mask.
+ *
+ * Return: 1 if sysrq is always enabled, enabled sysrq_key_op mask otherwise.
+ */
+int sysrq_mask(void)
+{
+	if (sysrq_always_enabled)
+		return 1;
+	return sysrq_enabled;
+}

This seems to have broken several configs, when serial_core is modular, with:

ERROR: modpost: "sysrq_mask" [drivers/tty/serial/serial_core.ko] undefined!

Quoted 7 lines

See:

http://kisskb.ellerman.id.au/kisskb/buildresult/14169386/

It's also being reported by the kernelci bot:

https://lore.kernel.org/linux-next/[email protected]/

Thanks for reporting this,

I've reproduced it and sent a fix:
https://lkml.kernel.org/r/[email protected]

Thanks,
Dmitry