Commit 68af4317 authored by Dmitry Safonov's avatar Dmitry Safonov Committed by Greg Kroah-Hartman
Browse files

serial/sysrq: Add MAGIC_SYSRQ_SERIAL_SEQUENCE



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 732dbf3a ("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.

Based-on-patch-by: default avatarVasiliy Khoruzhick <vasilykh@arista.com>
Signed-off-by: default avatarDmitry Safonov <dima@arista.com>
Link: https://lore.kernel.org/r/20200302175135.269397-3-dima@arista.com


Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent eaee4172
Loading
Loading
Loading
Loading
+68 −7
Original line number Original line Diff line number Diff line
@@ -20,6 +20,7 @@
#include <linux/device.h>
#include <linux/device.h>
#include <linux/serial.h> /* for serial_state and serial_icounter_struct */
#include <linux/serial.h> /* for serial_state and serial_icounter_struct */
#include <linux/serial_core.h>
#include <linux/serial_core.h>
#include <linux/sysrq.h>
#include <linux/delay.h>
#include <linux/delay.h>
#include <linux/mutex.h>
#include <linux/mutex.h>
#include <linux/security.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 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,
static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
					struct ktermios *old_termios);
					struct ktermios *old_termios);
static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
@@ -3084,6 +3087,56 @@ void uart_insert_char(struct uart_port *port, unsigned int status,
}
}
EXPORT_SYMBOL_GPL(uart_insert_char);
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)
int uart_handle_sysrq_char(struct uart_port *port, unsigned int ch)
{
{
	if (!IS_ENABLED(CONFIG_MAGIC_SYSRQ_SERIAL))
	if (!IS_ENABLED(CONFIG_MAGIC_SYSRQ_SERIAL))
@@ -3093,10 +3146,14 @@ int uart_handle_sysrq_char(struct uart_port *port, unsigned int ch)
		return 0;
		return 0;


	if (ch && time_before(jiffies, port->sysrq)) {
	if (ch && time_before(jiffies, port->sysrq)) {
		if (sysrq_mask()) {
			handle_sysrq(ch);
			handle_sysrq(ch);
			port->sysrq = 0;
			port->sysrq = 0;
			return 1;
			return 1;
		}
		}
		if (uart_try_toggle_sysrq(port, ch))
			return 1;
	}
	port->sysrq = 0;
	port->sysrq = 0;


	return 0;
	return 0;
@@ -3112,10 +3169,14 @@ int uart_prepare_sysrq_char(struct uart_port *port, unsigned int ch)
		return 0;
		return 0;


	if (ch && time_before(jiffies, port->sysrq)) {
	if (ch && time_before(jiffies, port->sysrq)) {
		if (sysrq_mask()) {
			port->sysrq_ch = ch;
			port->sysrq_ch = ch;
			port->sysrq = 0;
			port->sysrq = 0;
			return 1;
			return 1;
		}
		}
		if (uart_try_toggle_sysrq(port, ch))
			return 1;
	}
	port->sysrq = 0;
	port->sysrq = 0;


	return 0;
	return 0;
@@ -3154,7 +3215,7 @@ int uart_handle_break(struct uart_port *port)
	if (port->has_sysrq) {
	if (port->has_sysrq) {
		if (port->cons && port->cons->index == port->line) {
		if (port->cons && port->cons->index == port->line) {
			if (!port->sysrq) {
			if (!port->sysrq) {
				port->sysrq = jiffies + HZ*5;
				port->sysrq = jiffies + SYSRQ_TIMEOUT;
				return 1;
				return 1;
			}
			}
			port->sysrq = 0;
			port->sysrq = 0;
+1 −0
Original line number Original line Diff line number Diff line
@@ -243,6 +243,7 @@ struct uart_port {
	unsigned long		sysrq;			/* sysrq timeout */
	unsigned long		sysrq;			/* sysrq timeout */
	unsigned int		sysrq_ch;		/* char for sysrq */
	unsigned int		sysrq_ch;		/* char for sysrq */
	unsigned char		has_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		hub6;			/* this should be in the 8250 driver */
	unsigned char		suspended;
	unsigned char		suspended;
+8 −0
Original line number Original line Diff line number Diff line
@@ -431,6 +431,14 @@ config MAGIC_SYSRQ_SERIAL
	  This option allows you to decide whether you want to enable the
	  This option allows you to decide whether you want to enable the
	  magic SysRq key.
	  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
config DEBUG_FS
	bool "Debug Filesystem"
	bool "Debug Filesystem"
	help
	help