Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754158AbcCKEka (ORCPT ); Thu, 10 Mar 2016 23:40:30 -0500 Received: from mx.ewheeler.net ([71.19.153.34]:52710 "EHLO mail.ewheeler.net" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753094AbcCKEk1 (ORCPT ); Thu, 10 Mar 2016 23:40:27 -0500 X-Greylist: delayed 300 seconds by postgrey-1.27 at vger.kernel.org; Thu, 10 Mar 2016 23:40:26 EST Date: Fri, 11 Mar 2016 04:35:21 +0000 (UTC) From: Eric Wheeler X-X-Sender: lists@mail.ewheeler.net To: linux-kernel@vger.kernel.org cc: Marc MERLIN Subject: [PATCH-RFC]: sysrq-a: graceful reboot via kernel_restart(), similar to sysrq-o Message-ID: User-Agent: Alpine 2.11 (LRH 23 2013-08-11) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2535 Lines: 90 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 #include #include +#include /* * 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); ===================================================