[PATCH-RFC]: sysrq-a: graceful reboot via kernel_restart(), similar to sysrq-o

From: Eric Wheeler
Date: Thu Mar 10 2016 - 23:40:38 EST


Hello all,

We were having a discussion on the bcache list about the safest reboot
options via sysrq here:
http://thread.gmane.org/gmane.linux.kernel.bcache.devel/3559/focus=3586

The result of the discussion ended up in a patch for sysrq-a to call
kernel_restart much in the same way as sysrq-ocalls kernel_power_off.

Please comment on the patch and suggest any appropriate changes.

Thank you!

--
Eric Wheeler


===================================================
diff --git a/Documentation/sysrq.txt b/Documentation/sysrq.txt
index 0e307c9..cf43fc8 100644
--- a/Documentation/sysrq.txt
+++ b/Documentation/sysrq.txt
@@ -65,6 +65,11 @@ On all - write a character to /proc/sysrq-trigger. e.g.:

* What are the 'command' keys?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+'a' - Attempt to semi-gracefully reboot via kernel_restart(NULL). This
+ is in-kernel only (bypasses init), but is cleaner than a hard
+ reboot such as sysrq-b. sysrq-a is functionally similar to 'o' but
+ calls kernel_restart() instead of kernel_power_off().
+
'b' - Will immediately reboot the system without syncing or unmounting
your disks.

diff --git a/kernel/reboot.c b/kernel/reboot.c
index d20c85d..8181749 100644
--- a/kernel/reboot.c
+++ b/kernel/reboot.c
@@ -16,6 +16,7 @@
#include <linux/syscalls.h>
#include <linux/syscore_ops.h>
#include <linux/uaccess.h>
+#include <linux/sysrq.h>

/*
* this indicates whether you can reboot with ctrl-alt-del: the default is yes
@@ -555,3 +556,42 @@ static int __init reboot_setup(char *str)
return 1;
}
__setup("reboot=", reboot_setup);
+
+
+/*
+ * When the user hits Sys-Rq 'a' to kernel_restart the machine this is the
+ * callback we use.
+ */
+
+static void do_krestart(struct work_struct *dummy)
+{
+ /* are these two necessary? */
+ lockdep_off();
+ local_irq_enable();
+
+ /* restart */
+ kernel_restart(NULL);
+}
+
+static DECLARE_WORK(krestart_work, do_krestart);
+
+static void handle_krestart(int key)
+{
+ /* run sysrq krestart on boot cpu */
+ schedule_work_on(cpumask_first(cpu_online_mask), &krestart_work);
+}
+
+static struct sysrq_key_op sysrq_krestart_op = {
+ .handler = handle_krestart,
+ .help_msg = "kernel_restart(a)",
+ .action_msg = "Gracefullish Restart",
+ .enable_mask = SYSRQ_ENABLE_BOOT,
+};
+
+static int __init pm_sysrq_krestart_init(void)
+{
+ register_sysrq_key('a', &sysrq_krestart_op);
+ return 0;
+}
+
+subsys_initcall(pm_sysrq_krestart_init);
===================================================