Currently the magic SysRq functions can accessed by sending a break on
the serial port. Unfortunately some networked serial proxies make it
difficult to send a break meaning SysRq functions cannot be used. This
patch provides a workaround by allowing the (fairly unlikely) sequence
of ^B^R^K characters to emulate a real break.
This approach is very nearly as robust as normal sysrq/break handling
because all trigger recognition happens during interrupt handling. Only
major difference is that to emulate a break we must enter the ISR four
times (instead of twice) and manage an extra byte of state.
No means is provided to escape the trigger sequence (and pass ^B^R^K to
the underlying process) however the sequence is proved reasonably pretty
collision resistant in practice. The most significant consequence is
that ^B and ^B^R are delayed until a new character is observed.
The most significant collision I am aware of is with emacs-like
backward-char bindings (^B) because the character movement will become
lumpy (two characters every two key presses rather than one character
per key press). Arrow keys or ^B^B^F provide workarounds.
Special note for tmux users:
tmux defaults to using ^B as its escape character but does not have a
default binding for ^B^R. Likewise tmux had no visual indicator
showing the beginning of break sequence meaning delayed the delivery
of ^B is not observable. Thus serial break emulation does not interfere
with the use of tmux's default key bindings.
Signed-off-by: Daniel Thompson <daniel.thompson(a)linaro.org>
Cc: Greg Kroah-Hartman <gregkh(a)linuxfoundation.org>
Cc: Jiri Slaby <jslaby(a)suse.cz>
Cc: linux-serial(a)vger.kernel.org
---
Notes:
I've been sitting on this patch for a long time. However I realized
today just how frequently I end up using this and wondered if perhaps I
am not alone.
Yes! How did you guess? As it happens I do have quite a few broken
network to UART interfaces.
include/linux/serial_core.h | 83 +++++++++++++++++++++++++++++++++++----------
lib/Kconfig.debug | 15 ++++++++
2 files changed, 80 insertions(+), 18 deletions(-)
diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h
index cf3a1e7..dffb188 100644
--- a/include/linux/serial_core.h
+++ b/include/linux/serial_core.h
@@ -150,6 +150,9 @@ struct uart_port {
struct console *cons; /* struct console, if any */
#if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(SUPPORT_SYSRQ)
unsigned long sysrq; /* sysrq timeout */
+#ifdef CONFIG_MAGIC_SYSRQ_BREAK_EMULATION
+ char sysrq_emul; /* sysrq break emulation */
+#endif
#endif
upf_t flags;
@@ -364,24 +367,6 @@ extern void uart_handle_cts_change(struct uart_port *uport,
extern void uart_insert_char(struct uart_port *port, unsigned int status,
unsigned int overrun, unsigned int ch, unsigned int flag);
-#ifdef SUPPORT_SYSRQ
-static inline int
-uart_handle_sysrq_char(struct uart_port *port, unsigned int ch)
-{
- if (port->sysrq) {
- if (ch && time_before(jiffies, port->sysrq)) {
- handle_sysrq(ch);
- port->sysrq = 0;
- return 1;
- }
- port->sysrq = 0;
- }
- return 0;
-}
-#else
-#define uart_handle_sysrq_char(port,ch) ({ (void)port; 0; })
-#endif
-
/*
* We do the SysRQ and SAK checking like this...
*/
@@ -406,6 +391,68 @@ static inline int uart_handle_break(struct uart_port *port)
return 0;
}
+#if defined(SUPPORT_SYSRQ) && defined(CONFIG_MAGIC_SYSRQ_BREAK_EMULATION)
+/*
+ * Emulate a break if we are the serial console and receive ^B, ^R, ^K.
+ */
+static inline int
+uart_handle_sysrq_break_emulation(struct uart_port *port, unsigned int ch)
+{
+ const unsigned int ctrlb = 'B' & 31;
+ const unsigned int ctrlr = 'R' & 31;
+ const unsigned int ctrlk = 'K' & 31;
+
+ if (uart_console(port)) {
+ if ((port->sysrq_emul == 0 && ch == ctrlb) ||
+ (port->sysrq_emul == ctrlb && ch == ctrlr)) {
+ /* for either of the first two trigger characters
+ * update the state variable and move on.
+ */
+ port->sysrq_emul = ch;
+ return 1;
+ } else if (port->sysrq_emul == ctrlr && ch == ctrlk &&
+ uart_handle_break(port)) {
+ /* the break has already been emulated whilst
+ * evaluating the condition, tidy up and move on
+ */
+ port->sysrq_emul = 0;
+ return 1;
+ }
+ }
+
+ if (port->sysrq_emul) {
+ /* received a partial (false) trigger, tidy up and move on */
+ uart_insert_char(port, 0, 0, ctrlb, TTY_NORMAL);
+ if (port->sysrq_emul == ctrlr)
+ uart_insert_char(port, 0, 0, ctrlr, TTY_NORMAL);
+ port->sysrq_emul = 0;
+ }
+
+ return 0;
+}
+#else
+#define uart_handle_sysrq_break_emulation(port, ch) ({ (void)port; 0; })
+#endif
+
+#ifdef SUPPORT_SYSRQ
+static inline int
+uart_handle_sysrq_char(struct uart_port *port, unsigned int ch)
+{
+ if (port->sysrq) {
+ if (ch && time_before(jiffies, port->sysrq)) {
+ handle_sysrq(ch);
+ port->sysrq = 0;
+ return 1;
+ }
+ port->sysrq = 0;
+ }
+
+ return uart_handle_sysrq_break_emulation(port, ch);
+}
+#else
+#define uart_handle_sysrq_char(port,ch) ({ (void)port; 0; })
+#endif
+
/*
* UART_ENABLE_MS - determine if port should enable modem status irqs
*/
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index a285900..bcd9cf5 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -345,6 +345,21 @@ config MAGIC_SYSRQ_DEFAULT_ENABLE
This may be set to 1 or 0 to enable or disable them all, or
to a bitmask as described in Documentation/sysrq.txt.
+config MAGIC_SYSRQ_BREAK_EMULATION
+ bool "Enable magic SysRq serial break emulation"
+ depends on MAGIC_SYSRQ && SERIAL_CORE_CONSOLE
+ default n
+ help
+ If you say Y here, then you can use the character sequence ^B^R^K
+ to simulate a BREAK on the serial console. This is useful if for
+ some reason you cannot send a BREAK to your console's serial port.
+ For example, if you have a serial device server that cannot
+ send a BREAK. Enabling this feature can delay the delivery of
+ characters to the TTY because the ^B and a subsequent ^R will be
+ delayed until we know what the next character is.
+
+ If unsure, say N.
+
config DEBUG_KERNEL
bool "Kernel debugging"
help
--
1.9.3
Tree/Branch: master
Git describe: v3.17-rc4-168-g7ec62d4
Commit: 7ec62d421b Merge branch 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs
Build Time: 22 min 16 sec
Passed: 6 / 6 (100.00 %)
Failed: 0 / 6 ( 0.00 %)
Errors: 0
Warnings: 28
Section Mismatches: 0
-------------------------------------------------------------------------------
defconfigs with issues (other than build errors):
2 warnings 0 mismatches : arm64-allnoconfig
18 warnings 0 mismatches : arm-allmodconfig
1 warnings 0 mismatches : arm-multi_v7_defconfig
7 warnings 0 mismatches : arm64-allmodconfig
-------------------------------------------------------------------------------
Warnings Summary: 28
1 ../sound/soc/fsl/fsl_sai.c:337:7: warning: large integer implicitly truncated to unsigned type [-Woverflow]
1 ../net/rds/iw_rdma.c:200:1: warning: the frame size of 1056 bytes is larger than 1024 bytes [-Wframe-larger-than=]
1 ../drivers/tty/isicom.c:1058:240: warning: integer overflow in expression [-Woverflow]
1 ../drivers/staging/wlan-ng/prism2fw.c:792:1: warning: the frame size of 1072 bytes is larger than 1024 bytes [-Wframe-larger-than=]
1 ../drivers/staging/vt6655/device_main.c:3091:1: warning: the frame size of 1296 bytes is larger than 1024 bytes [-Wframe-larger-than=]
1 ../drivers/staging/rtl8192ee/pci.c:885:5: warning: format '%d' expects argument of type 'int', but argument 3 has type 'sk_buff_data_t' [-Wformat=]
1 ../drivers/staging/rtl8192ee/pci.c:885:5: warning: format '%d' expects argument of type 'int', but argument 2 has type 'sk_buff_data_t' [-Wformat=]
1 ../drivers/staging/dgnc/dgnc_tty.c:572:1: warning: the frame size of 1048 bytes is larger than 1024 bytes [-Wframe-larger-than=]
1 ../drivers/staging/bcm/CmHost.c:1564:3: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
1 ../drivers/staging/bcm/CmHost.c:1546:3: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
1 ../drivers/staging/bcm/CmHost.c:1503:3: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
1 ../drivers/scsi/ips.c:210:2: warning: #warning "This driver has only been tested on the x86/ia64/x86_64 platforms" [-Wcpp]
1 ../drivers/power/reset/xgene-reboot.c:80:17: warning: assignment from incompatible pointer type [enabled by default]
1 ../drivers/pci/host/pci-mvebu.c:887:39: warning: 'rtype' may be used uninitialized in this function [-Wmaybe-uninitialized]
1 ../drivers/net/ethernet/mellanox/mlx5/core/debugfs.c:467:46: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
1 ../drivers/net/ethernet/mellanox/mlx5/core/debugfs.c:307:11: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
1 ../drivers/net/ethernet/mellanox/mlx5/core/debugfs.c:303:11: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
1 ../drivers/net/ethernet/dec/tulip/winbond-840.c:910:2: warning: #warning Processor architecture undefined [-Wcpp]
1 ../drivers/net/ethernet/amd/nmclan_cs.c:624:3: warning: 'pcmcia_request_exclusive_irq' is deprecated (declared at ../include/pcmcia/ds.h:213) [-Wdeprecated-declarations]
1 ../drivers/mtd/chips/cfi_cmdset_0020.c:651:1: warning: the frame size of 1192 bytes is larger than 1024 bytes [-Wframe-larger-than=]
1 ../drivers/isdn/hardware/mISDN/w6692.c:1181:75: warning: unsupported argument to '__builtin_return_address'
1 ../drivers/isdn/hardware/mISDN/mISDNipac.c:759:75: warning: unsupported argument to '__builtin_return_address'
1 ../drivers/irqchip/irq-gic-v3.c:203:12: warning: 'gic_peek_irq' defined but not used [-Wunused-function]
1 ../drivers/irqchip/irq-gic-v3.c:132:13: warning: 'gic_write_sgi1r' defined but not used [-Wunused-function]
1 ../drivers/infiniband/hw/mlx5/mem.c:71:143: warning: comparison of distinct pointer types lacks a cast
1 ../drivers/block/drbd/drbd_bitmap.c:483:0: warning: "BITS_PER_PAGE_MASK" redefined [enabled by default]
1 ../arch/arm/mach-cns3xxx/pcie.c:311:1: warning: the frame size of 1072 bytes is larger than 1024 bytes [-Wframe-larger-than=]
1 ........................./drivers/block/drbd/drbd_bitmap.c:482:0: warning: "BITS_PER_PAGE" redefined [enabled by default]
===============================================================================
Detailed per-defconfig build reports below:
-------------------------------------------------------------------------------
arm64-allnoconfig : PASS, 0 errors, 2 warnings, 0 section mismatches
Warnings:
../drivers/irqchip/irq-gic-v3.c:132:13: warning: 'gic_write_sgi1r' defined but not used [-Wunused-function]
../drivers/irqchip/irq-gic-v3.c:203:12: warning: 'gic_peek_irq' defined but not used [-Wunused-function]
-------------------------------------------------------------------------------
arm-allmodconfig : PASS, 0 errors, 18 warnings, 0 section mismatches
Warnings:
../arch/arm/mach-cns3xxx/pcie.c:311:1: warning: the frame size of 1072 bytes is larger than 1024 bytes [-Wframe-larger-than=]
../net/rds/iw_rdma.c:200:1: warning: the frame size of 1056 bytes is larger than 1024 bytes [-Wframe-larger-than=]
../drivers/infiniband/hw/mlx5/mem.c:71:143: warning: comparison of distinct pointer types lacks a cast
../drivers/isdn/hardware/mISDN/w6692.c:1181:75: warning: unsupported argument to '__builtin_return_address'
../drivers/isdn/hardware/mISDN/mISDNipac.c:759:75: warning: unsupported argument to '__builtin_return_address'
../drivers/mtd/chips/cfi_cmdset_0020.c:651:1: warning: the frame size of 1192 bytes is larger than 1024 bytes [-Wframe-larger-than=]
../drivers/net/ethernet/amd/nmclan_cs.c:624:3: warning: 'pcmcia_request_exclusive_irq' is deprecated (declared at ../include/pcmcia/ds.h:213) [-Wdeprecated-declarations]
../drivers/net/ethernet/dec/tulip/winbond-840.c:910:2: warning: #warning Processor architecture undefined [-Wcpp]
../drivers/net/ethernet/mellanox/mlx5/core/debugfs.c:303:11: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
../drivers/net/ethernet/mellanox/mlx5/core/debugfs.c:307:11: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
../drivers/net/ethernet/mellanox/mlx5/core/debugfs.c:467:46: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
../drivers/staging/dgnc/dgnc_tty.c:572:1: warning: the frame size of 1048 bytes is larger than 1024 bytes [-Wframe-larger-than=]
../drivers/scsi/ips.c:210:2: warning: #warning "This driver has only been tested on the x86/ia64/x86_64 platforms" [-Wcpp]
../drivers/tty/isicom.c:1058:240: warning: integer overflow in expression [-Woverflow]
../drivers/staging/rtl8192ee/pci.c:885:5: warning: format '%d' expects argument of type 'int', but argument 2 has type 'sk_buff_data_t' [-Wformat=]
../drivers/staging/rtl8192ee/pci.c:885:5: warning: format '%d' expects argument of type 'int', but argument 3 has type 'sk_buff_data_t' [-Wformat=]
../drivers/staging/vt6655/device_main.c:3091:1: warning: the frame size of 1296 bytes is larger than 1024 bytes [-Wframe-larger-than=]
../drivers/staging/wlan-ng/prism2fw.c:792:1: warning: the frame size of 1072 bytes is larger than 1024 bytes [-Wframe-larger-than=]
-------------------------------------------------------------------------------
arm-multi_v7_defconfig : PASS, 0 errors, 1 warnings, 0 section mismatches
Warnings:
../drivers/pci/host/pci-mvebu.c:887:39: warning: 'rtype' may be used uninitialized in this function [-Wmaybe-uninitialized]
-------------------------------------------------------------------------------
arm64-allmodconfig : PASS, 0 errors, 7 warnings, 0 section mismatches
Warnings:
........................./drivers/block/drbd/drbd_bitmap.c:482:0: warning: "BITS_PER_PAGE" redefined [enabled by default]
../drivers/block/drbd/drbd_bitmap.c:483:0: warning: "BITS_PER_PAGE_MASK" redefined [enabled by default]
../sound/soc/fsl/fsl_sai.c:337:7: warning: large integer implicitly truncated to unsigned type [-Woverflow]
../drivers/power/reset/xgene-reboot.c:80:17: warning: assignment from incompatible pointer type [enabled by default]
../drivers/staging/bcm/CmHost.c:1503:3: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
../drivers/staging/bcm/CmHost.c:1546:3: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
../drivers/staging/bcm/CmHost.c:1564:3: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
-------------------------------------------------------------------------------
Passed with no errors, warnings or mismatches:
arm-allnoconfig
arm64-defconfig