blob: 28fb20e9f98454d100262f712184150944f19ec9 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Linux Magic System Request Key Hacks
4 *
5 * (c) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
6 * based on ideas by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>
7 *
8 * (c) 2000 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
9 * overhauled to use key registration
10 * based upon discusions in irc://irc.openprojects.net/#kernelnewbies
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070011 *
12 * Copyright (c) 2010 Dmitry Torokhov
13 * Input handler conversion
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070016#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
Ingo Molnar3f07c012017-02-08 18:51:30 +010018#include <linux/sched/signal.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060019#include <linux/sched/rt.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010020#include <linux/sched/debug.h>
Ingo Molnar29930022017-02-08 18:51:36 +010021#include <linux/sched/task.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/interrupt.h>
23#include <linux/mm.h>
24#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/mount.h>
26#include <linux/kdev_t.h>
27#include <linux/major.h>
28#include <linux/reboot.h>
29#include <linux/sysrq.h>
30#include <linux/kbd_kern.h>
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -070031#include <linux/proc_fs.h>
Ingo Molnarc1dc0b92009-08-02 11:28:21 +020032#include <linux/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/quotaops.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020034#include <linux/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/kernel.h>
36#include <linux/module.h>
37#include <linux/suspend.h>
38#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/swap.h>
40#include <linux/spinlock.h>
41#include <linux/vt_kern.h>
42#include <linux/workqueue.h>
Ingo Molnar88ad0bf62007-02-16 01:28:16 -080043#include <linux/hrtimer.h>
David Rientjes5a3135c22007-10-16 23:25:53 -070044#include <linux/oom.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070046#include <linux/input.h>
Al Viroff01bb42011-09-16 02:31:11 -040047#include <linux/uaccess.h>
Mathieu Poirier154b7a42013-01-06 23:23:33 -080048#include <linux/moduleparam.h>
Mathieu J. Poirier39030782013-04-01 22:14:19 -070049#include <linux/jiffies.h>
Mathieu J. Poirier3d289512013-06-05 22:51:46 -070050#include <linux/syscalls.h>
Mathieu J. Poirier4c076eb2013-08-03 17:22:08 -070051#include <linux/of.h>
Rik van Riel722773a2014-06-06 14:38:14 -070052#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#include <asm/ptrace.h>
Heiko Carstens2033b0c2006-10-06 16:38:42 +020055#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/* Whether we react on sysrq keys or just ignore them */
Ben Hutchings8eaede42013-10-07 01:05:46 +010058static int __read_mostly sysrq_enabled = CONFIG_MAGIC_SYSRQ_DEFAULT_ENABLE;
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070059static bool __read_mostly sysrq_always_enabled;
Ingo Molnar5d6f6472006-12-13 00:34:36 -080060
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070061static bool sysrq_on(void)
Ingo Molnar5d6f6472006-12-13 00:34:36 -080062{
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070063 return sysrq_enabled || sysrq_always_enabled;
Ingo Molnar5d6f6472006-12-13 00:34:36 -080064}
65
66/*
67 * A value of 1 means 'all', other nonzero values are an op mask:
68 */
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070069static bool sysrq_on_mask(int mask)
Ingo Molnar5d6f6472006-12-13 00:34:36 -080070{
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070071 return sysrq_always_enabled ||
72 sysrq_enabled == 1 ||
73 (sysrq_enabled & mask);
Ingo Molnar5d6f6472006-12-13 00:34:36 -080074}
75
76static int __init sysrq_always_enabled_setup(char *str)
77{
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070078 sysrq_always_enabled = true;
79 pr_info("sysrq always enabled.\n");
Ingo Molnar5d6f6472006-12-13 00:34:36 -080080
81 return 1;
82}
83
84__setup("sysrq_always_enabled", sysrq_always_enabled_setup);
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Dmitry Torokhov1495cc92010-08-17 21:15:46 -070087static void sysrq_handle_loglevel(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 int i;
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 i = key - '0';
Borislav Petkova8fe19e2014-06-04 16:11:46 -070092 console_loglevel = CONSOLE_LOGLEVEL_DEFAULT;
Michal Hocko401e4a72015-02-11 15:26:21 -080093 pr_info("Loglevel set to %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 console_loglevel = i;
Andrew Mortonbf36b902006-03-25 03:07:08 -080095}
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static struct sysrq_key_op sysrq_loglevel_op = {
97 .handler = sysrq_handle_loglevel,
Randy Dunlap208b95c2009-01-06 14:41:13 -080098 .help_msg = "loglevel(0-9)",
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 .action_msg = "Changing Loglevel",
100 .enable_mask = SYSRQ_ENABLE_LOG,
101};
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#ifdef CONFIG_VT
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700104static void sysrq_handle_SAK(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Eric W. Biederman8b6312f2007-02-10 01:44:34 -0800106 struct work_struct *SAK_work = &vc_cons[fg_console].SAK_work;
Eric W. Biederman8b6312f2007-02-10 01:44:34 -0800107 schedule_work(SAK_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109static struct sysrq_key_op sysrq_SAK_op = {
110 .handler = sysrq_handle_SAK,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800111 .help_msg = "sak(k)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 .action_msg = "SAK",
113 .enable_mask = SYSRQ_ENABLE_KEYBOARD,
114};
Andrew Mortonbf36b902006-03-25 03:07:08 -0800115#else
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700116#define sysrq_SAK_op (*(struct sysrq_key_op *)NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#endif
118
119#ifdef CONFIG_VT
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700120static void sysrq_handle_unraw(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Alan Cox079c9532012-02-28 14:49:23 +0000122 vt_reset_unicode(fg_console);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
Alan Cox079c9532012-02-28 14:49:23 +0000124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static struct sysrq_key_op sysrq_unraw_op = {
126 .handler = sysrq_handle_unraw,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800127 .help_msg = "unraw(r)",
Bill Nottingham2e8ecb92007-10-16 23:29:38 -0700128 .action_msg = "Keyboard mode set to system default",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 .enable_mask = SYSRQ_ENABLE_KEYBOARD,
130};
Andrew Mortonbf36b902006-03-25 03:07:08 -0800131#else
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700132#define sysrq_unraw_op (*(struct sysrq_key_op *)NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#endif /* CONFIG_VT */
134
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700135static void sysrq_handle_crash(int key)
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -0700136{
Matthias Kaehlcke8341f2f2018-09-20 10:12:53 -0700137 /* release the RCU read lock before crashing */
Ani Sinha984cf352015-12-17 17:15:10 -0800138 rcu_read_unlock();
Matthias Kaehlcke8341f2f2018-09-20 10:12:53 -0700139
140 panic("sysrq triggered crash\n");
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -0700141}
Hidetoshi Setocab8bd32009-07-29 15:04:14 -0700142static struct sysrq_key_op sysrq_crash_op = {
Neil Hormand6580a92009-06-17 16:28:17 -0700143 .handler = sysrq_handle_crash,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800144 .help_msg = "crash(c)",
Neil Hormand6580a92009-06-17 16:28:17 -0700145 .action_msg = "Trigger a crash",
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -0700146 .enable_mask = SYSRQ_ENABLE_DUMP,
147};
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -0700148
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700149static void sysrq_handle_reboot(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Peter Zijlstrab2e9c7d02006-09-30 23:28:02 -0700151 lockdep_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 local_irq_enable();
Eric W. Biederman4de8b9b2005-07-26 11:51:06 -0600153 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static struct sysrq_key_op sysrq_reboot_op = {
156 .handler = sysrq_handle_reboot,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800157 .help_msg = "reboot(b)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 .action_msg = "Resetting",
159 .enable_mask = SYSRQ_ENABLE_BOOT,
160};
161
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700162static void sysrq_handle_sync(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 emergency_sync();
165}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static struct sysrq_key_op sysrq_sync_op = {
167 .handler = sysrq_handle_sync,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800168 .help_msg = "sync(s)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 .action_msg = "Emergency Sync",
170 .enable_mask = SYSRQ_ENABLE_SYNC,
171};
172
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700173static void sysrq_handle_show_timers(int key)
Ingo Molnar88ad0bf62007-02-16 01:28:16 -0800174{
175 sysrq_timer_list_show();
176}
177
178static struct sysrq_key_op sysrq_show_timers_op = {
179 .handler = sysrq_handle_show_timers,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800180 .help_msg = "show-all-timers(q)",
Thomas Gleixner322acf62008-10-20 12:33:14 +0200181 .action_msg = "Show clockevent devices & pending hrtimers (no others)",
Ingo Molnar88ad0bf62007-02-16 01:28:16 -0800182};
183
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700184static void sysrq_handle_mountro(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 emergency_remount();
187}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188static struct sysrq_key_op sysrq_mountro_op = {
189 .handler = sysrq_handle_mountro,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800190 .help_msg = "unmount(u)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 .action_msg = "Emergency Remount R/O",
192 .enable_mask = SYSRQ_ENABLE_REMOUNT,
193};
194
Ingo Molnar8c645802006-07-03 00:24:56 -0700195#ifdef CONFIG_LOCKDEP
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700196static void sysrq_handle_showlocks(int key)
Ingo Molnarde5097c2006-01-09 15:59:21 -0800197{
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700198 debug_show_all_locks();
Ingo Molnarde5097c2006-01-09 15:59:21 -0800199}
Ingo Molnar8c645802006-07-03 00:24:56 -0700200
Ingo Molnarde5097c2006-01-09 15:59:21 -0800201static struct sysrq_key_op sysrq_showlocks_op = {
202 .handler = sysrq_handle_showlocks,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800203 .help_msg = "show-all-locks(d)",
Ingo Molnarde5097c2006-01-09 15:59:21 -0800204 .action_msg = "Show Locks Held",
205};
Andrew Mortonbf36b902006-03-25 03:07:08 -0800206#else
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700207#define sysrq_showlocks_op (*(struct sysrq_key_op *)NULL)
Ingo Molnarde5097c2006-01-09 15:59:21 -0800208#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Rik van Riel5045bca2008-04-29 00:59:21 -0700210#ifdef CONFIG_SMP
211static DEFINE_SPINLOCK(show_lock);
212
213static void showacpu(void *dummy)
214{
215 unsigned long flags;
216
217 /* Idle CPUs have no interesting backtrace. */
218 if (idle_cpu(smp_processor_id()))
219 return;
220
221 spin_lock_irqsave(&show_lock, flags);
Michal Hocko401e4a72015-02-11 15:26:21 -0800222 pr_info("CPU%d:\n", smp_processor_id());
Rik van Riel5045bca2008-04-29 00:59:21 -0700223 show_stack(NULL, NULL);
224 spin_unlock_irqrestore(&show_lock, flags);
225}
226
227static void sysrq_showregs_othercpus(struct work_struct *dummy)
228{
Ingo Molnar8b604d52008-06-27 11:52:45 +0200229 smp_call_function(showacpu, NULL, 0);
Rik van Riel5045bca2008-04-29 00:59:21 -0700230}
231
232static DECLARE_WORK(sysrq_showallcpus, sysrq_showregs_othercpus);
233
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700234static void sysrq_handle_showallcpus(int key)
Rik van Riel5045bca2008-04-29 00:59:21 -0700235{
Ingo Molnar47cab6a2009-08-03 09:31:54 +0200236 /*
237 * Fall back to the workqueue based printing if the
238 * backtrace printing did not succeed or the
239 * architecture has no support for it:
240 */
241 if (!trigger_all_cpu_backtrace()) {
Jibin Xub00bebb2017-09-10 20:11:42 -0700242 struct pt_regs *regs = NULL;
Ingo Molnar47cab6a2009-08-03 09:31:54 +0200243
Jibin Xub00bebb2017-09-10 20:11:42 -0700244 if (in_irq())
245 regs = get_irq_regs();
Ingo Molnar47cab6a2009-08-03 09:31:54 +0200246 if (regs) {
Michal Hocko401e4a72015-02-11 15:26:21 -0800247 pr_info("CPU%d:\n", smp_processor_id());
Ingo Molnar47cab6a2009-08-03 09:31:54 +0200248 show_regs(regs);
249 }
250 schedule_work(&sysrq_showallcpus);
251 }
Rik van Riel5045bca2008-04-29 00:59:21 -0700252}
253
254static struct sysrq_key_op sysrq_showallcpus_op = {
255 .handler = sysrq_handle_showallcpus,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800256 .help_msg = "show-backtrace-all-active-cpus(l)",
Rik van Riel5045bca2008-04-29 00:59:21 -0700257 .action_msg = "Show backtrace of all active CPUs",
258 .enable_mask = SYSRQ_ENABLE_DUMP,
259};
260#endif
261
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700262static void sysrq_handle_showregs(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Jibin Xub00bebb2017-09-10 20:11:42 -0700264 struct pt_regs *regs = NULL;
265
266 if (in_irq())
267 regs = get_irq_regs();
David Howells7d12e782006-10-05 14:55:46 +0100268 if (regs)
269 show_regs(regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200270 perf_event_print_debug();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272static struct sysrq_key_op sysrq_showregs_op = {
273 .handler = sysrq_handle_showregs,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800274 .help_msg = "show-registers(p)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 .action_msg = "Show Regs",
276 .enable_mask = SYSRQ_ENABLE_DUMP,
277};
278
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700279static void sysrq_handle_showstate(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 show_state();
Tejun Heo3494fc32015-03-09 09:22:28 -0400282 show_workqueue_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284static struct sysrq_key_op sysrq_showstate_op = {
285 .handler = sysrq_handle_showstate,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800286 .help_msg = "show-task-states(t)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 .action_msg = "Show State",
288 .enable_mask = SYSRQ_ENABLE_DUMP,
289};
290
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700291static void sysrq_handle_showstate_blocked(int key)
Ingo Molnare59e2ae2006-12-06 20:35:59 -0800292{
293 show_state_filter(TASK_UNINTERRUPTIBLE);
294}
295static struct sysrq_key_op sysrq_showstate_blocked_op = {
296 .handler = sysrq_handle_showstate_blocked,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800297 .help_msg = "show-blocked-tasks(w)",
Ingo Molnare59e2ae2006-12-06 20:35:59 -0800298 .action_msg = "Show Blocked State",
299 .enable_mask = SYSRQ_ENABLE_DUMP,
300};
301
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100302#ifdef CONFIG_TRACING
303#include <linux/ftrace.h>
304
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700305static void sysrq_ftrace_dump(int key)
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100306{
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200307 ftrace_dump(DUMP_ALL);
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100308}
309static struct sysrq_key_op sysrq_ftrace_dump_op = {
310 .handler = sysrq_ftrace_dump,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800311 .help_msg = "dump-ftrace-buffer(z)",
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100312 .action_msg = "Dump ftrace buffer",
313 .enable_mask = SYSRQ_ENABLE_DUMP,
314};
315#else
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700316#define sysrq_ftrace_dump_op (*(struct sysrq_key_op *)NULL)
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100317#endif
Ingo Molnare59e2ae2006-12-06 20:35:59 -0800318
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700319static void sysrq_handle_showmem(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Michal Hocko9af744d2017-02-22 15:46:16 -0800321 show_mem(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323static struct sysrq_key_op sysrq_showmem_op = {
324 .handler = sysrq_handle_showmem,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800325 .help_msg = "show-memory-usage(m)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 .action_msg = "Show Memory",
327 .enable_mask = SYSRQ_ENABLE_DUMP,
328};
329
Andrew Mortonbf36b902006-03-25 03:07:08 -0800330/*
331 * Signal sysrq helper function. Sends a signal to all user processes.
332 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333static void send_sig_all(int sig)
334{
335 struct task_struct *p;
336
Anton Vorontsove502bab2012-02-07 10:49:39 +0400337 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 for_each_process(p) {
Anton Vorontsovd3a532a2012-02-07 10:49:51 +0400339 if (p->flags & PF_KTHREAD)
340 continue;
341 if (is_global_init(p))
342 continue;
343
Eric W. Biedermanb16503b2018-07-21 11:35:55 -0500344 do_send_sig_info(sig, SEND_SIG_PRIV, p, PIDTYPE_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
Anton Vorontsove502bab2012-02-07 10:49:39 +0400346 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700349static void sysrq_handle_term(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 send_sig_all(SIGTERM);
Borislav Petkova8fe19e2014-06-04 16:11:46 -0700352 console_loglevel = CONSOLE_LOGLEVEL_DEBUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354static struct sysrq_key_op sysrq_term_op = {
355 .handler = sysrq_handle_term,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800356 .help_msg = "terminate-all-tasks(e)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 .action_msg = "Terminate All Tasks",
358 .enable_mask = SYSRQ_ENABLE_SIGNAL,
359};
360
David Howells65f27f32006-11-22 14:55:48 +0000361static void moom_callback(struct work_struct *ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
David Rientjes6e0fc462015-09-08 15:00:36 -0700363 const gfp_t gfp_mask = GFP_KERNEL;
364 struct oom_control oc = {
365 .zonelist = node_zonelist(first_memory_node, gfp_mask),
366 .nodemask = NULL,
Vladimir Davydov2a966b72016-07-26 15:22:33 -0700367 .memcg = NULL,
David Rientjes6e0fc462015-09-08 15:00:36 -0700368 .gfp_mask = gfp_mask,
David Rientjes54e9e292015-09-08 15:00:39 -0700369 .order = -1,
David Rientjes6e0fc462015-09-08 15:00:36 -0700370 };
371
Johannes Weinerdc564012015-06-24 16:57:19 -0700372 mutex_lock(&oom_lock);
David Rientjes6e0fc462015-09-08 15:00:36 -0700373 if (!out_of_memory(&oc))
Michal Hockod75da002017-05-03 14:54:57 -0700374 pr_info("OOM request ignored. No task eligible\n");
Johannes Weinerdc564012015-06-24 16:57:19 -0700375 mutex_unlock(&oom_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
David Howells65f27f32006-11-22 14:55:48 +0000378static DECLARE_WORK(moom_work, moom_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700380static void sysrq_handle_moom(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 schedule_work(&moom_work);
383}
384static struct sysrq_key_op sysrq_moom_op = {
385 .handler = sysrq_handle_moom,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800386 .help_msg = "memory-full-oom-kill(f)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 .action_msg = "Manual OOM execution",
Naohiro Ooiwab4236f82008-10-15 22:01:43 -0700388 .enable_mask = SYSRQ_ENABLE_SIGNAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389};
390
Eric Sandeenc2d75432009-03-31 15:23:46 -0700391#ifdef CONFIG_BLOCK
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700392static void sysrq_handle_thaw(int key)
Eric Sandeenc2d75432009-03-31 15:23:46 -0700393{
394 emergency_thaw_all();
395}
396static struct sysrq_key_op sysrq_thaw_op = {
397 .handler = sysrq_handle_thaw,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800398 .help_msg = "thaw-filesystems(j)",
Eric Sandeenc2d75432009-03-31 15:23:46 -0700399 .action_msg = "Emergency Thaw of all frozen filesystems",
400 .enable_mask = SYSRQ_ENABLE_SIGNAL,
401};
402#endif
403
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700404static void sysrq_handle_kill(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 send_sig_all(SIGKILL);
Borislav Petkova8fe19e2014-06-04 16:11:46 -0700407 console_loglevel = CONSOLE_LOGLEVEL_DEBUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409static struct sysrq_key_op sysrq_kill_op = {
410 .handler = sysrq_handle_kill,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800411 .help_msg = "kill-all-tasks(i)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 .action_msg = "Kill All Tasks",
413 .enable_mask = SYSRQ_ENABLE_SIGNAL,
414};
415
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700416static void sysrq_handle_unrt(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 normalize_rt_tasks();
419}
420static struct sysrq_key_op sysrq_unrt_op = {
421 .handler = sysrq_handle_unrt,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800422 .help_msg = "nice-all-RT-tasks(n)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 .action_msg = "Nice All RT Tasks",
424 .enable_mask = SYSRQ_ENABLE_RTNICE,
425};
426
427/* Key Operations table and lock */
428static DEFINE_SPINLOCK(sysrq_key_table_lock);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800429
430static struct sysrq_key_op *sysrq_key_table[36] = {
431 &sysrq_loglevel_op, /* 0 */
432 &sysrq_loglevel_op, /* 1 */
433 &sysrq_loglevel_op, /* 2 */
434 &sysrq_loglevel_op, /* 3 */
435 &sysrq_loglevel_op, /* 4 */
436 &sysrq_loglevel_op, /* 5 */
437 &sysrq_loglevel_op, /* 6 */
438 &sysrq_loglevel_op, /* 7 */
439 &sysrq_loglevel_op, /* 8 */
440 &sysrq_loglevel_op, /* 9 */
441
442 /*
Randy Dunlapd346cce2007-01-31 23:48:17 -0800443 * a: Don't use for system provided sysrqs, it is handled specially on
444 * sparc and will never arrive.
Andrew Mortonbf36b902006-03-25 03:07:08 -0800445 */
446 NULL, /* a */
447 &sysrq_reboot_op, /* b */
Eric Biggers5e351412017-04-03 22:50:20 -0700448 &sysrq_crash_op, /* c */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800449 &sysrq_showlocks_op, /* d */
450 &sysrq_term_op, /* e */
451 &sysrq_moom_op, /* f */
Jason Wessel364b5b72009-05-13 21:56:59 -0500452 /* g: May be registered for the kernel debugger */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800453 NULL, /* g */
Eric Sandeenc2d75432009-03-31 15:23:46 -0700454 NULL, /* h - reserved for help */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800455 &sysrq_kill_op, /* i */
Eric Sandeenc2d75432009-03-31 15:23:46 -0700456#ifdef CONFIG_BLOCK
457 &sysrq_thaw_op, /* j */
458#else
Andrew Mortonbf36b902006-03-25 03:07:08 -0800459 NULL, /* j */
Eric Sandeenc2d75432009-03-31 15:23:46 -0700460#endif
Andrew Mortonbf36b902006-03-25 03:07:08 -0800461 &sysrq_SAK_op, /* k */
Rik van Riel5045bca2008-04-29 00:59:21 -0700462#ifdef CONFIG_SMP
463 &sysrq_showallcpus_op, /* l */
464#else
Andrew Mortonbf36b902006-03-25 03:07:08 -0800465 NULL, /* l */
Rik van Riel5045bca2008-04-29 00:59:21 -0700466#endif
Andrew Mortonbf36b902006-03-25 03:07:08 -0800467 &sysrq_showmem_op, /* m */
468 &sysrq_unrt_op, /* n */
Randy Dunlapd346cce2007-01-31 23:48:17 -0800469 /* o: This will often be registered as 'Off' at init time */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800470 NULL, /* o */
471 &sysrq_showregs_op, /* p */
Ingo Molnar88ad0bf62007-02-16 01:28:16 -0800472 &sysrq_show_timers_op, /* q */
Randy Dunlapd346cce2007-01-31 23:48:17 -0800473 &sysrq_unraw_op, /* r */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800474 &sysrq_sync_op, /* s */
475 &sysrq_showstate_op, /* t */
476 &sysrq_mountro_op, /* u */
Jason Wessel364b5b72009-05-13 21:56:59 -0500477 /* v: May be registered for frame buffer console restore */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800478 NULL, /* v */
Randy Dunlapd346cce2007-01-31 23:48:17 -0800479 &sysrq_showstate_blocked_op, /* w */
James Hogand1e9a4f2015-05-19 09:50:29 +0100480 /* x: May be registered on mips for TLB dump */
Randy Dunlapd346cce2007-01-31 23:48:17 -0800481 /* x: May be registered on ppc/powerpc for xmon */
David S. Miller916ca142012-10-16 09:34:01 -0700482 /* x: May be registered on sparc64 for global PMU dump */
Randy Dunlapd346cce2007-01-31 23:48:17 -0800483 NULL, /* x */
David S. Miller93dae5b2008-05-19 23:46:00 -0700484 /* y: May be registered on sparc64 for global register dump */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800485 NULL, /* y */
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100486 &sysrq_ftrace_dump_op, /* z */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487};
488
489/* key2index calculation, -1 on invalid index */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800490static int sysrq_key_table_key2index(int key)
491{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 int retval;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800493
494 if ((key >= '0') && (key <= '9'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 retval = key - '0';
Andrew Mortonbf36b902006-03-25 03:07:08 -0800496 else if ((key >= 'a') && (key <= 'z'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 retval = key + 10 - 'a';
Andrew Mortonbf36b902006-03-25 03:07:08 -0800498 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 retval = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return retval;
501}
502
503/*
504 * get and put functions for the table, exposed to modules.
505 */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800506struct sysrq_key_op *__sysrq_get_key_op(int key)
507{
508 struct sysrq_key_op *op_p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 int i;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 i = sysrq_key_table_key2index(key);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800512 if (i != -1)
513 op_p = sysrq_key_table[i];
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return op_p;
516}
517
Andrew Mortonbf36b902006-03-25 03:07:08 -0800518static void __sysrq_put_key_op(int key, struct sysrq_key_op *op_p)
519{
520 int i = sysrq_key_table_key2index(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (i != -1)
523 sysrq_key_table[i] = op_p;
524}
525
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700526void __handle_sysrq(int key, bool check_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 struct sysrq_key_op *op_p;
529 int orig_log_level;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800530 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Rik van Riel722773a2014-06-06 14:38:14 -0700532 rcu_sysrq_start();
Rik van Riel984d74a2014-06-06 14:38:13 -0700533 rcu_read_lock();
Andy Whitcroftfb144ad2009-01-15 13:50:52 -0800534 /*
535 * Raise the apparent loglevel to maximum so that the sysrq header
536 * is shown to provide the user with positive feedback. We do not
537 * simply emit this at KERN_EMERG as that would change message
538 * routing in the consumers of /proc/kmsg.
539 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 orig_log_level = console_loglevel;
Borislav Petkova8fe19e2014-06-04 16:11:46 -0700541 console_loglevel = CONSOLE_LOGLEVEL_DEFAULT;
Michal Hocko401e4a72015-02-11 15:26:21 -0800542 pr_info("SysRq : ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 op_p = __sysrq_get_key_op(key);
545 if (op_p) {
Andrew Mortonbf36b902006-03-25 03:07:08 -0800546 /*
547 * Should we check for enabled operations (/proc/sysrq-trigger
548 * should not) and is the invoked operation enabled?
549 */
Ingo Molnar5d6f6472006-12-13 00:34:36 -0800550 if (!check_mask || sysrq_on_mask(op_p->enable_mask)) {
Michal Hocko401e4a72015-02-11 15:26:21 -0800551 pr_cont("%s\n", op_p->action_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 console_loglevel = orig_log_level;
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700553 op_p->handler(key);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800554 } else {
Michal Hocko401e4a72015-02-11 15:26:21 -0800555 pr_cont("This sysrq operation is disabled.\n");
Andrew Mortonbf36b902006-03-25 03:07:08 -0800556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 } else {
Michal Hocko401e4a72015-02-11 15:26:21 -0800558 pr_cont("HELP : ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 /* Only print the help msg once per handler */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800560 for (i = 0; i < ARRAY_SIZE(sysrq_key_table); i++) {
561 if (sysrq_key_table[i]) {
562 int j;
563
564 for (j = 0; sysrq_key_table[i] !=
565 sysrq_key_table[j]; j++)
566 ;
567 if (j != i)
568 continue;
Michal Hocko401e4a72015-02-11 15:26:21 -0800569 pr_cont("%s ", sysrq_key_table[i]->help_msg);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
Michal Hocko401e4a72015-02-11 15:26:21 -0800572 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 console_loglevel = orig_log_level;
574 }
Rik van Riel984d74a2014-06-06 14:38:13 -0700575 rcu_read_unlock();
Rik van Riel722773a2014-06-06 14:38:14 -0700576 rcu_sysrq_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700579void handle_sysrq(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Ingo Molnar5d6f6472006-12-13 00:34:36 -0800581 if (sysrq_on())
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700582 __handle_sysrq(key, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
Andrew Mortonbf36b902006-03-25 03:07:08 -0800584EXPORT_SYMBOL(handle_sysrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700586#ifdef CONFIG_INPUT
Arnd Bergmannffb6e0c2015-05-26 14:45:29 -0700587static int sysrq_reset_downtime_ms;
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700588
589/* Simple translation table for the SysRq keys */
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800590static const unsigned char sysrq_xlate[KEY_CNT] =
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700591 "\000\0331234567890-=\177\t" /* 0x00 - 0x0f */
592 "qwertyuiop[]\r\000as" /* 0x10 - 0x1f */
593 "dfghjkl;'`\000\\zxcv" /* 0x20 - 0x2f */
594 "bnm,./\000*\000 \000\201\202\203\204\205" /* 0x30 - 0x3f */
595 "\206\207\210\211\212\000\000789-456+1" /* 0x40 - 0x4f */
596 "230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
597 "\r\000/"; /* 0x60 - 0x6f */
598
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800599struct sysrq_state {
600 struct input_handle handle;
601 struct work_struct reinject_work;
602 unsigned long key_down[BITS_TO_LONGS(KEY_CNT)];
603 unsigned int alt;
604 unsigned int alt_use;
605 bool active;
606 bool need_reinject;
Dmitry Torokhov7ab7b5a2011-02-02 22:59:54 -0800607 bool reinjecting;
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800608
609 /* reset sequence handling */
610 bool reset_canceled;
Mathieu J. Poirier3d289512013-06-05 22:51:46 -0700611 bool reset_requested;
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800612 unsigned long reset_keybit[BITS_TO_LONGS(KEY_CNT)];
613 int reset_seq_len;
614 int reset_seq_cnt;
615 int reset_seq_version;
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700616 struct timer_list keyreset_timer;
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800617};
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700618
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800619#define SYSRQ_KEY_RESET_MAX 20 /* Should be plenty */
620static unsigned short sysrq_reset_seq[SYSRQ_KEY_RESET_MAX];
621static unsigned int sysrq_reset_seq_len;
622static unsigned int sysrq_reset_seq_version = 1;
623
624static void sysrq_parse_reset_sequence(struct sysrq_state *state)
625{
626 int i;
627 unsigned short key;
628
629 state->reset_seq_cnt = 0;
630
631 for (i = 0; i < sysrq_reset_seq_len; i++) {
632 key = sysrq_reset_seq[i];
633
634 if (key == KEY_RESERVED || key > KEY_MAX)
635 break;
636
637 __set_bit(key, state->reset_keybit);
638 state->reset_seq_len++;
639
640 if (test_bit(key, state->key_down))
641 state->reset_seq_cnt++;
642 }
643
644 /* Disable reset until old keys are not released */
645 state->reset_canceled = state->reset_seq_cnt != 0;
646
647 state->reset_seq_version = sysrq_reset_seq_version;
648}
649
Kees Cook8c318fa2017-10-16 16:28:51 -0700650static void sysrq_do_reset(struct timer_list *t)
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700651{
Kees Cook8c318fa2017-10-16 16:28:51 -0700652 struct sysrq_state *state = from_timer(state, t, keyreset_timer);
Mathieu J. Poirier3d289512013-06-05 22:51:46 -0700653
654 state->reset_requested = true;
655
Dominik Brodowski70f68ee2018-03-14 22:35:11 +0100656 ksys_sync();
Mathieu J. Poirier3d289512013-06-05 22:51:46 -0700657 kernel_restart(NULL);
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700658}
659
660static void sysrq_handle_reset_request(struct sysrq_state *state)
661{
Mathieu J. Poirier3d289512013-06-05 22:51:46 -0700662 if (state->reset_requested)
663 __handle_sysrq(sysrq_xlate[KEY_B], false);
664
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700665 if (sysrq_reset_downtime_ms)
666 mod_timer(&state->keyreset_timer,
667 jiffies + msecs_to_jiffies(sysrq_reset_downtime_ms));
668 else
Kees Cook8c318fa2017-10-16 16:28:51 -0700669 sysrq_do_reset(&state->keyreset_timer);
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700670}
671
672static void sysrq_detect_reset_sequence(struct sysrq_state *state,
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800673 unsigned int code, int value)
674{
675 if (!test_bit(code, state->reset_keybit)) {
676 /*
677 * Pressing any key _not_ in reset sequence cancels
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700678 * the reset sequence. Also cancelling the timer in
679 * case additional keys were pressed after a reset
680 * has been requested.
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800681 */
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700682 if (value && state->reset_seq_cnt) {
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800683 state->reset_canceled = true;
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700684 del_timer(&state->keyreset_timer);
685 }
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800686 } else if (value == 0) {
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700687 /*
688 * Key release - all keys in the reset sequence need
689 * to be pressed and held for the reset timeout
690 * to hold.
691 */
692 del_timer(&state->keyreset_timer);
693
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800694 if (--state->reset_seq_cnt == 0)
695 state->reset_canceled = false;
696 } else if (value == 1) {
697 /* key press, not autorepeat */
698 if (++state->reset_seq_cnt == state->reset_seq_len &&
699 !state->reset_canceled) {
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700700 sysrq_handle_reset_request(state);
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800701 }
702 }
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800703}
704
Mathieu J. Poirier4c076eb2013-08-03 17:22:08 -0700705#ifdef CONFIG_OF
706static void sysrq_of_get_keyreset_config(void)
707{
708 u32 key;
709 struct device_node *np;
710 struct property *prop;
711 const __be32 *p;
712
713 np = of_find_node_by_path("/chosen/linux,sysrq-reset-seq");
714 if (!np) {
715 pr_debug("No sysrq node found");
716 return;
717 }
718
719 /* Reset in case a __weak definition was present */
720 sysrq_reset_seq_len = 0;
721
722 of_property_for_each_u32(np, "keyset", prop, p, key) {
723 if (key == KEY_RESERVED || key > KEY_MAX ||
724 sysrq_reset_seq_len == SYSRQ_KEY_RESET_MAX)
725 break;
726
727 sysrq_reset_seq[sysrq_reset_seq_len++] = (unsigned short)key;
728 }
729
730 /* Get reset timeout if any. */
731 of_property_read_u32(np, "timeout-ms", &sysrq_reset_downtime_ms);
Yangtao Li279070b2018-11-21 08:12:14 -0500732
733 of_node_put(np);
Mathieu J. Poirier4c076eb2013-08-03 17:22:08 -0700734}
735#else
736static void sysrq_of_get_keyreset_config(void)
737{
738}
739#endif
740
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800741static void sysrq_reinject_alt_sysrq(struct work_struct *work)
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700742{
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800743 struct sysrq_state *sysrq =
744 container_of(work, struct sysrq_state, reinject_work);
745 struct input_handle *handle = &sysrq->handle;
746 unsigned int alt_code = sysrq->alt_use;
747
748 if (sysrq->need_reinject) {
Dmitry Torokhov7ab7b5a2011-02-02 22:59:54 -0800749 /* we do not want the assignment to be reordered */
750 sysrq->reinjecting = true;
751 mb();
752
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800753 /* Simulate press and release of Alt + SysRq */
754 input_inject_event(handle, EV_KEY, alt_code, 1);
755 input_inject_event(handle, EV_KEY, KEY_SYSRQ, 1);
756 input_inject_event(handle, EV_SYN, SYN_REPORT, 1);
757
758 input_inject_event(handle, EV_KEY, KEY_SYSRQ, 0);
759 input_inject_event(handle, EV_KEY, alt_code, 0);
760 input_inject_event(handle, EV_SYN, SYN_REPORT, 1);
Dmitry Torokhov7ab7b5a2011-02-02 22:59:54 -0800761
762 mb();
763 sysrq->reinjecting = false;
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800764 }
765}
766
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800767static bool sysrq_handle_keypress(struct sysrq_state *sysrq,
768 unsigned int code, int value)
769{
770 bool was_active = sysrq->active;
771 bool suppress;
772
773 switch (code) {
774
775 case KEY_LEFTALT:
776 case KEY_RIGHTALT:
777 if (!value) {
778 /* One of ALTs is being released */
779 if (sysrq->active && code == sysrq->alt_use)
780 sysrq->active = false;
781
782 sysrq->alt = KEY_RESERVED;
783
784 } else if (value != 2) {
785 sysrq->alt = code;
786 sysrq->need_reinject = false;
787 }
788 break;
789
790 case KEY_SYSRQ:
791 if (value == 1 && sysrq->alt != KEY_RESERVED) {
792 sysrq->active = true;
793 sysrq->alt_use = sysrq->alt;
794 /*
795 * If nothing else will be pressed we'll need
796 * to re-inject Alt-SysRq keysroke.
797 */
798 sysrq->need_reinject = true;
799 }
800
801 /*
802 * Pretend that sysrq was never pressed at all. This
803 * is needed to properly handle KGDB which will try
804 * to release all keys after exiting debugger. If we
805 * do not clear key bit it KGDB will end up sending
806 * release events for Alt and SysRq, potentially
807 * triggering print screen function.
808 */
809 if (sysrq->active)
810 clear_bit(KEY_SYSRQ, sysrq->handle.dev->key);
811
812 break;
813
814 default:
815 if (sysrq->active && value && value != 2) {
816 sysrq->need_reinject = false;
817 __handle_sysrq(sysrq_xlate[code], true);
818 }
819 break;
820 }
821
822 suppress = sysrq->active;
823
824 if (!sysrq->active) {
825
826 /*
827 * See if reset sequence has changed since the last time.
828 */
829 if (sysrq->reset_seq_version != sysrq_reset_seq_version)
830 sysrq_parse_reset_sequence(sysrq);
831
832 /*
833 * If we are not suppressing key presses keep track of
834 * keyboard state so we can release keys that have been
835 * pressed before entering SysRq mode.
836 */
837 if (value)
838 set_bit(code, sysrq->key_down);
839 else
840 clear_bit(code, sysrq->key_down);
841
842 if (was_active)
843 schedule_work(&sysrq->reinject_work);
844
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700845 /* Check for reset sequence */
846 sysrq_detect_reset_sequence(sysrq, code, value);
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800847
848 } else if (value == 0 && test_and_clear_bit(code, sysrq->key_down)) {
849 /*
850 * Pass on release events for keys that was pressed before
851 * entering SysRq mode.
852 */
853 suppress = false;
854 }
855
856 return suppress;
857}
858
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800859static bool sysrq_filter(struct input_handle *handle,
860 unsigned int type, unsigned int code, int value)
861{
862 struct sysrq_state *sysrq = handle->private;
Dmitry Torokhov1966cb22010-09-29 18:04:21 -0700863 bool suppress;
864
Dmitry Torokhov7ab7b5a2011-02-02 22:59:54 -0800865 /*
866 * Do not filter anything if we are in the process of re-injecting
867 * Alt+SysRq combination.
868 */
869 if (sysrq->reinjecting)
870 return false;
871
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800872 switch (type) {
Dmitry Torokhov1966cb22010-09-29 18:04:21 -0700873
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800874 case EV_SYN:
875 suppress = false;
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700876 break;
877
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800878 case EV_KEY:
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800879 suppress = sysrq_handle_keypress(sysrq, code, value);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700880 break;
881
882 default:
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800883 suppress = sysrq->active;
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700884 break;
885 }
886
Dmitry Torokhov1966cb22010-09-29 18:04:21 -0700887 return suppress;
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700888}
889
890static int sysrq_connect(struct input_handler *handler,
891 struct input_dev *dev,
892 const struct input_device_id *id)
893{
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800894 struct sysrq_state *sysrq;
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700895 int error;
896
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800897 sysrq = kzalloc(sizeof(struct sysrq_state), GFP_KERNEL);
898 if (!sysrq)
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700899 return -ENOMEM;
900
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800901 INIT_WORK(&sysrq->reinject_work, sysrq_reinject_alt_sysrq);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700902
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800903 sysrq->handle.dev = dev;
904 sysrq->handle.handler = handler;
905 sysrq->handle.name = "sysrq";
906 sysrq->handle.private = sysrq;
Kees Cook8c318fa2017-10-16 16:28:51 -0700907 timer_setup(&sysrq->keyreset_timer, sysrq_do_reset, 0);
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800908
909 error = input_register_handle(&sysrq->handle);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700910 if (error) {
911 pr_err("Failed to register input sysrq handler, error %d\n",
912 error);
913 goto err_free;
914 }
915
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800916 error = input_open_device(&sysrq->handle);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700917 if (error) {
918 pr_err("Failed to open input device, error %d\n", error);
919 goto err_unregister;
920 }
921
922 return 0;
923
924 err_unregister:
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800925 input_unregister_handle(&sysrq->handle);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700926 err_free:
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800927 kfree(sysrq);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700928 return error;
929}
930
931static void sysrq_disconnect(struct input_handle *handle)
932{
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800933 struct sysrq_state *sysrq = handle->private;
934
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700935 input_close_device(handle);
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800936 cancel_work_sync(&sysrq->reinject_work);
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700937 del_timer_sync(&sysrq->keyreset_timer);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700938 input_unregister_handle(handle);
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800939 kfree(sysrq);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700940}
941
942/*
Dmitry Torokhov1966cb22010-09-29 18:04:21 -0700943 * We are matching on KEY_LEFTALT instead of KEY_SYSRQ because not all
944 * keyboards have SysRq key predefined and so user may add it to keymap
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700945 * later, but we expect all such keyboards to have left alt.
946 */
947static const struct input_device_id sysrq_ids[] = {
948 {
949 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
950 INPUT_DEVICE_ID_MATCH_KEYBIT,
Akinobu Mita802c0382017-01-06 02:14:16 +0900951 .evbit = { [BIT_WORD(EV_KEY)] = BIT_MASK(EV_KEY) },
952 .keybit = { [BIT_WORD(KEY_LEFTALT)] = BIT_MASK(KEY_LEFTALT) },
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700953 },
954 { },
955};
956
957static struct input_handler sysrq_handler = {
958 .filter = sysrq_filter,
959 .connect = sysrq_connect,
960 .disconnect = sysrq_disconnect,
961 .name = "sysrq",
962 .id_table = sysrq_ids,
963};
964
965static bool sysrq_handler_registered;
966
967static inline void sysrq_register_handler(void)
968{
969 int error;
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800970
Mathieu J. Poirier4c076eb2013-08-03 17:22:08 -0700971 sysrq_of_get_keyreset_config();
972
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700973 error = input_register_handler(&sysrq_handler);
974 if (error)
975 pr_err("Failed to register input handler, error %d", error);
976 else
977 sysrq_handler_registered = true;
978}
979
980static inline void sysrq_unregister_handler(void)
981{
982 if (sysrq_handler_registered) {
983 input_unregister_handler(&sysrq_handler);
984 sysrq_handler_registered = false;
985 }
986}
987
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800988static int sysrq_reset_seq_param_set(const char *buffer,
989 const struct kernel_param *kp)
990{
991 unsigned long val;
992 int error;
993
Jingoo Han86b40562013-06-01 16:30:06 +0900994 error = kstrtoul(buffer, 0, &val);
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800995 if (error < 0)
996 return error;
997
998 if (val > KEY_MAX)
999 return -EINVAL;
1000
1001 *((unsigned short *)kp->arg) = val;
1002 sysrq_reset_seq_version++;
1003
1004 return 0;
1005}
1006
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09301007static const struct kernel_param_ops param_ops_sysrq_reset_seq = {
Mathieu Poirier154b7a42013-01-06 23:23:33 -08001008 .get = param_get_ushort,
1009 .set = sysrq_reset_seq_param_set,
1010};
1011
1012#define param_check_sysrq_reset_seq(name, p) \
1013 __param_check(name, p, unsigned short)
1014
Paul Gortmaker3bce6f62015-08-19 17:48:06 -04001015/*
1016 * not really modular, but the easiest way to keep compat with existing
1017 * bootargs behaviour is to continue using module_param here.
1018 */
Mathieu Poirier154b7a42013-01-06 23:23:33 -08001019module_param_array_named(reset_seq, sysrq_reset_seq, sysrq_reset_seq,
1020 &sysrq_reset_seq_len, 0644);
1021
Mathieu J. Poirier39030782013-04-01 22:14:19 -07001022module_param_named(sysrq_downtime_ms, sysrq_reset_downtime_ms, int, 0644);
1023
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -07001024#else
1025
1026static inline void sysrq_register_handler(void)
1027{
1028}
1029
1030static inline void sysrq_unregister_handler(void)
1031{
1032}
1033
1034#endif /* CONFIG_INPUT */
1035
1036int sysrq_toggle_support(int enable_mask)
1037{
1038 bool was_enabled = sysrq_on();
1039
1040 sysrq_enabled = enable_mask;
1041
1042 if (was_enabled != sysrq_on()) {
1043 if (sysrq_on())
1044 sysrq_register_handler();
1045 else
1046 sysrq_unregister_handler();
1047 }
1048
1049 return 0;
1050}
1051
Adrian Bunkcf62ddc2005-11-08 21:39:47 -08001052static int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p,
Andrew Mortonbf36b902006-03-25 03:07:08 -08001053 struct sysrq_key_op *remove_op_p)
1054{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Rik van Riel984d74a2014-06-06 14:38:13 -07001057 spin_lock(&sysrq_key_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (__sysrq_get_key_op(key) == remove_op_p) {
1059 __sysrq_put_key_op(key, insert_op_p);
1060 retval = 0;
1061 } else {
1062 retval = -1;
1063 }
Rik van Riel984d74a2014-06-06 14:38:13 -07001064 spin_unlock(&sysrq_key_table_lock);
1065
1066 /*
1067 * A concurrent __handle_sysrq either got the old op or the new op.
1068 * Wait for it to go away before returning, so the code for an old
1069 * op is not freed (eg. on module unload) while it is in use.
1070 */
1071 synchronize_rcu();
1072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return retval;
1074}
1075
1076int register_sysrq_key(int key, struct sysrq_key_op *op_p)
1077{
1078 return __sysrq_swap_key_ops(key, op_p, NULL);
1079}
Andrew Mortonbf36b902006-03-25 03:07:08 -08001080EXPORT_SYMBOL(register_sysrq_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082int unregister_sysrq_key(int key, struct sysrq_key_op *op_p)
1083{
1084 return __sysrq_swap_key_ops(key, NULL, op_p);
1085}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086EXPORT_SYMBOL(unregister_sysrq_key);
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -07001087
1088#ifdef CONFIG_PROC_FS
1089/*
1090 * writing 'C' to /proc/sysrq-trigger is like sysrq-C
1091 */
1092static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
1093 size_t count, loff_t *ppos)
1094{
1095 if (count) {
1096 char c;
1097
1098 if (get_user(c, buf))
1099 return -EFAULT;
Dmitry Torokhovf3353972010-08-17 21:15:47 -07001100 __handle_sysrq(c, false);
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -07001101 }
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -07001102
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -07001103 return count;
1104}
1105
1106static const struct file_operations proc_sysrq_trigger_operations = {
1107 .write = write_sysrq_trigger,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001108 .llseek = noop_llseek,
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -07001109};
1110
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -07001111static void sysrq_init_procfs(void)
1112{
1113 if (!proc_create("sysrq-trigger", S_IWUSR, NULL,
1114 &proc_sysrq_trigger_operations))
1115 pr_err("Failed to register proc interface\n");
1116}
1117
1118#else
1119
1120static inline void sysrq_init_procfs(void)
1121{
1122}
1123
1124#endif /* CONFIG_PROC_FS */
1125
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -07001126static int __init sysrq_init(void)
1127{
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -07001128 sysrq_init_procfs();
1129
1130 if (sysrq_on())
1131 sysrq_register_handler();
1132
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -07001133 return 0;
1134}
Paul Gortmaker3bce6f62015-08-19 17:48:06 -04001135device_initcall(sysrq_init);