diff mbox series

[v6,2/2] tty/sysrq: Replay kernel log messages on consoles via sysrq

Message ID cc3b9b1aae60a236c6aed1dc7b0ffa2c7cd1f183.1710220326.git.sreenath.vijayan@sony.com
State New
Headers show
Series Add support to replay kernel log on consoles via sysrq | expand

Commit Message

Sreenath Vijayan March 13, 2024, 10:22 a.m. UTC
When terminal is unresponsive, one cannot use dmesg to view
the printk ring buffer messages. Also, syslog services may be
disabled, especially on embedded systems, to check the messages
after a reboot. In this scenario, replay the messages in printk
ring buffer on consoles via sysrq by pressing sysrq+R.

The console loglevel will determine which all kernel log messages
are displayed. The messages will be displayed only when
console_trylock() succeeds. Users could repeat the sysrq key when
it fails. If the owner of console subsystem lock is stuck,
repeating the key won't work.

Suggested-by: Petr Mladek <pmladek@suse.com>
Signed-off-by: Shimoyashiki Taichi <taichi.shimoyashiki@sony.com>
Reviewed-by: John Ogness <john.ogness@linutronix.de>
Signed-off-by: Sreenath Vijayan <sreenath.vijayan@sony.com>
---
 Documentation/admin-guide/sysrq.rst |  9 +++++++++
 drivers/tty/sysrq.c                 | 13 ++++++++++++-
 2 files changed, 21 insertions(+), 1 deletion(-)

Comments

Sreenath Vijayan April 8, 2024, 5:53 a.m. UTC | #1
On Wed, Mar 13, 2024 at 03:52:52PM +0530, Sreenath Vijayan wrote:
> When terminal is unresponsive, one cannot use dmesg to view
> the printk ring buffer messages. Also, syslog services may be
> disabled, especially on embedded systems, to check the messages
> after a reboot. In this scenario, replay the messages in printk
> ring buffer on consoles via sysrq by pressing sysrq+R.
> 
> The console loglevel will determine which all kernel log messages
> are displayed. The messages will be displayed only when
> console_trylock() succeeds. Users could repeat the sysrq key when
> it fails. If the owner of console subsystem lock is stuck,
> repeating the key won't work.
> 
> Suggested-by: Petr Mladek <pmladek@suse.com>
> Signed-off-by: Shimoyashiki Taichi <taichi.shimoyashiki@sony.com>
> Reviewed-by: John Ogness <john.ogness@linutronix.de>
> Signed-off-by: Sreenath Vijayan <sreenath.vijayan@sony.com>
> ---
>  Documentation/admin-guide/sysrq.rst |  9 +++++++++
>  drivers/tty/sysrq.c                 | 13 ++++++++++++-
>  2 files changed, 21 insertions(+), 1 deletion(-)
> 
> diff --git a/Documentation/admin-guide/sysrq.rst b/Documentation/admin-guide/sysrq.rst
> index 2f2e5bd440f9..a85b3384d1e7 100644
> --- a/Documentation/admin-guide/sysrq.rst
> +++ b/Documentation/admin-guide/sysrq.rst
> @@ -161,6 +161,8 @@ Command	    Function
>              will be printed to your console. (``0``, for example would make
>              it so that only emergency messages like PANICs or OOPSes would
>              make it to your console.)
> +
> +``R``	    Replay the kernel log messages on consoles.
>  =========== ===================================================================
>  
>  Okay, so what can I use them for?
> @@ -211,6 +213,13 @@ processes.
>  "just thaw ``it(j)``" is useful if your system becomes unresponsive due to a
>  frozen (probably root) filesystem via the FIFREEZE ioctl.
>  
> +``Replay logs(R)`` is useful to view the kernel log messages when system is hung
> +or you are not able to use dmesg command to view the messages in printk buffer.
> +User may have to press the key combination multiple times if console system is
> +busy. If it is completely locked up, then messages won't be printed. Output
> +messages depend on current console loglevel, which can be modified using
> +sysrq[0-9] (see above).
> +
>  Sometimes SysRq seems to get 'stuck' after using it, what can I do?
>  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>  
> diff --git a/drivers/tty/sysrq.c b/drivers/tty/sysrq.c
> index 02217e3c916b..e5974b8239c9 100644
> --- a/drivers/tty/sysrq.c
> +++ b/drivers/tty/sysrq.c
> @@ -450,6 +450,17 @@ static const struct sysrq_key_op sysrq_unrt_op = {
>  	.enable_mask	= SYSRQ_ENABLE_RTNICE,
>  };
>  
> +static void sysrq_handle_replay_logs(u8 key)
> +{
> +	console_replay_all();
> +}
> +static struct sysrq_key_op sysrq_replay_logs_op = {
> +	.handler        = sysrq_handle_replay_logs,
> +	.help_msg       = "replay-kernel-logs(R)",
> +	.action_msg     = "Replay kernel logs on consoles",
> +	.enable_mask    = SYSRQ_ENABLE_DUMP,
> +};
> +
>  /* Key Operations table and lock */
>  static DEFINE_SPINLOCK(sysrq_key_table_lock);
>  
> @@ -519,7 +530,7 @@ static const struct sysrq_key_op *sysrq_key_table[62] = {
>  	NULL,				/* O */
>  	NULL,				/* P */
>  	NULL,				/* Q */
> -	NULL,				/* R */
> +	&sysrq_replay_logs_op,		/* R */
>  	NULL,				/* S */
>  	NULL,				/* T */
>  	NULL,				/* U */
> -- 
> 2.25.1
> 

Hi,

Kindly let me know if there are any review comments for the above changes.

Regards,
Sreenath
Petr Mladek May 22, 2024, 3:31 p.m. UTC | #2
On Wed 2024-03-13 15:52:52, Sreenath Vijayan wrote:
> When terminal is unresponsive, one cannot use dmesg to view
> the printk ring buffer messages. Also, syslog services may be
> disabled, especially on embedded systems, to check the messages
> after a reboot. In this scenario, replay the messages in printk
> ring buffer on consoles via sysrq by pressing sysrq+R.
> 
> The console loglevel will determine which all kernel log messages
> are displayed. The messages will be displayed only when
> console_trylock() succeeds. Users could repeat the sysrq key when
> it fails. If the owner of console subsystem lock is stuck,
> repeating the key won't work.
> 
> Suggested-by: Petr Mladek <pmladek@suse.com>
> Signed-off-by: Shimoyashiki Taichi <taichi.shimoyashiki@sony.com>
> Reviewed-by: John Ogness <john.ogness@linutronix.de>
> Signed-off-by: Sreenath Vijayan <sreenath.vijayan@sony.com>

Reviewed-by: Petr Mladek <pmladek@suse.com>

Best Regards,
Petr
diff mbox series

Patch

diff --git a/Documentation/admin-guide/sysrq.rst b/Documentation/admin-guide/sysrq.rst
index 2f2e5bd440f9..a85b3384d1e7 100644
--- a/Documentation/admin-guide/sysrq.rst
+++ b/Documentation/admin-guide/sysrq.rst
@@ -161,6 +161,8 @@  Command	    Function
             will be printed to your console. (``0``, for example would make
             it so that only emergency messages like PANICs or OOPSes would
             make it to your console.)
+
+``R``	    Replay the kernel log messages on consoles.
 =========== ===================================================================
 
 Okay, so what can I use them for?
@@ -211,6 +213,13 @@  processes.
 "just thaw ``it(j)``" is useful if your system becomes unresponsive due to a
 frozen (probably root) filesystem via the FIFREEZE ioctl.
 
+``Replay logs(R)`` is useful to view the kernel log messages when system is hung
+or you are not able to use dmesg command to view the messages in printk buffer.
+User may have to press the key combination multiple times if console system is
+busy. If it is completely locked up, then messages won't be printed. Output
+messages depend on current console loglevel, which can be modified using
+sysrq[0-9] (see above).
+
 Sometimes SysRq seems to get 'stuck' after using it, what can I do?
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
diff --git a/drivers/tty/sysrq.c b/drivers/tty/sysrq.c
index 02217e3c916b..e5974b8239c9 100644
--- a/drivers/tty/sysrq.c
+++ b/drivers/tty/sysrq.c
@@ -450,6 +450,17 @@  static const struct sysrq_key_op sysrq_unrt_op = {
 	.enable_mask	= SYSRQ_ENABLE_RTNICE,
 };
 
+static void sysrq_handle_replay_logs(u8 key)
+{
+	console_replay_all();
+}
+static struct sysrq_key_op sysrq_replay_logs_op = {
+	.handler        = sysrq_handle_replay_logs,
+	.help_msg       = "replay-kernel-logs(R)",
+	.action_msg     = "Replay kernel logs on consoles",
+	.enable_mask    = SYSRQ_ENABLE_DUMP,
+};
+
 /* Key Operations table and lock */
 static DEFINE_SPINLOCK(sysrq_key_table_lock);
 
@@ -519,7 +530,7 @@  static const struct sysrq_key_op *sysrq_key_table[62] = {
 	NULL,				/* O */
 	NULL,				/* P */
 	NULL,				/* Q */
-	NULL,				/* R */
+	&sysrq_replay_logs_op,		/* R */
 	NULL,				/* S */
 	NULL,				/* T */
 	NULL,				/* U */