blob: d008f5a7519718f9c9bc68c9d2a10277ec37b4f6 [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{
Neil Hormand6580a92009-06-17 16:28:17 -0700137 char *killer = NULL;
Hidetoshi Setocab8bd32009-07-29 15:04:14 -0700138
Ani Sinha984cf352015-12-17 17:15:10 -0800139 /* we need to release the RCU read lock here,
140 * otherwise we get an annoying
141 * 'BUG: sleeping function called from invalid context'
142 * complaint from the kernel before the panic.
143 */
144 rcu_read_unlock();
Hidetoshi Setocab8bd32009-07-29 15:04:14 -0700145 panic_on_oops = 1; /* force panic */
146 wmb();
Neil Hormand6580a92009-06-17 16:28:17 -0700147 *killer = 1;
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -0700148}
Hidetoshi Setocab8bd32009-07-29 15:04:14 -0700149static struct sysrq_key_op sysrq_crash_op = {
Neil Hormand6580a92009-06-17 16:28:17 -0700150 .handler = sysrq_handle_crash,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800151 .help_msg = "crash(c)",
Neil Hormand6580a92009-06-17 16:28:17 -0700152 .action_msg = "Trigger a crash",
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -0700153 .enable_mask = SYSRQ_ENABLE_DUMP,
154};
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -0700155
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700156static void sysrq_handle_reboot(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Peter Zijlstrab2e9c7d02006-09-30 23:28:02 -0700158 lockdep_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 local_irq_enable();
Eric W. Biederman4de8b9b2005-07-26 11:51:06 -0600160 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162static struct sysrq_key_op sysrq_reboot_op = {
163 .handler = sysrq_handle_reboot,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800164 .help_msg = "reboot(b)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 .action_msg = "Resetting",
166 .enable_mask = SYSRQ_ENABLE_BOOT,
167};
168
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700169static void sysrq_handle_sync(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 emergency_sync();
172}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173static struct sysrq_key_op sysrq_sync_op = {
174 .handler = sysrq_handle_sync,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800175 .help_msg = "sync(s)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 .action_msg = "Emergency Sync",
177 .enable_mask = SYSRQ_ENABLE_SYNC,
178};
179
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700180static void sysrq_handle_show_timers(int key)
Ingo Molnar88ad0bf62007-02-16 01:28:16 -0800181{
182 sysrq_timer_list_show();
183}
184
185static struct sysrq_key_op sysrq_show_timers_op = {
186 .handler = sysrq_handle_show_timers,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800187 .help_msg = "show-all-timers(q)",
Thomas Gleixner322acf62008-10-20 12:33:14 +0200188 .action_msg = "Show clockevent devices & pending hrtimers (no others)",
Ingo Molnar88ad0bf62007-02-16 01:28:16 -0800189};
190
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700191static void sysrq_handle_mountro(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 emergency_remount();
194}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195static struct sysrq_key_op sysrq_mountro_op = {
196 .handler = sysrq_handle_mountro,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800197 .help_msg = "unmount(u)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 .action_msg = "Emergency Remount R/O",
199 .enable_mask = SYSRQ_ENABLE_REMOUNT,
200};
201
Ingo Molnar8c645802006-07-03 00:24:56 -0700202#ifdef CONFIG_LOCKDEP
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700203static void sysrq_handle_showlocks(int key)
Ingo Molnarde5097c2006-01-09 15:59:21 -0800204{
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700205 debug_show_all_locks();
Ingo Molnarde5097c2006-01-09 15:59:21 -0800206}
Ingo Molnar8c645802006-07-03 00:24:56 -0700207
Ingo Molnarde5097c2006-01-09 15:59:21 -0800208static struct sysrq_key_op sysrq_showlocks_op = {
209 .handler = sysrq_handle_showlocks,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800210 .help_msg = "show-all-locks(d)",
Ingo Molnarde5097c2006-01-09 15:59:21 -0800211 .action_msg = "Show Locks Held",
212};
Andrew Mortonbf36b902006-03-25 03:07:08 -0800213#else
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700214#define sysrq_showlocks_op (*(struct sysrq_key_op *)NULL)
Ingo Molnarde5097c2006-01-09 15:59:21 -0800215#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Rik van Riel5045bca2008-04-29 00:59:21 -0700217#ifdef CONFIG_SMP
218static DEFINE_SPINLOCK(show_lock);
219
220static void showacpu(void *dummy)
221{
222 unsigned long flags;
223
224 /* Idle CPUs have no interesting backtrace. */
225 if (idle_cpu(smp_processor_id()))
226 return;
227
228 spin_lock_irqsave(&show_lock, flags);
Michal Hocko401e4a72015-02-11 15:26:21 -0800229 pr_info("CPU%d:\n", smp_processor_id());
Rik van Riel5045bca2008-04-29 00:59:21 -0700230 show_stack(NULL, NULL);
231 spin_unlock_irqrestore(&show_lock, flags);
232}
233
234static void sysrq_showregs_othercpus(struct work_struct *dummy)
235{
Ingo Molnar8b604d52008-06-27 11:52:45 +0200236 smp_call_function(showacpu, NULL, 0);
Rik van Riel5045bca2008-04-29 00:59:21 -0700237}
238
239static DECLARE_WORK(sysrq_showallcpus, sysrq_showregs_othercpus);
240
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700241static void sysrq_handle_showallcpus(int key)
Rik van Riel5045bca2008-04-29 00:59:21 -0700242{
Ingo Molnar47cab6a2009-08-03 09:31:54 +0200243 /*
244 * Fall back to the workqueue based printing if the
245 * backtrace printing did not succeed or the
246 * architecture has no support for it:
247 */
248 if (!trigger_all_cpu_backtrace()) {
249 struct pt_regs *regs = get_irq_regs();
250
251 if (regs) {
Michal Hocko401e4a72015-02-11 15:26:21 -0800252 pr_info("CPU%d:\n", smp_processor_id());
Ingo Molnar47cab6a2009-08-03 09:31:54 +0200253 show_regs(regs);
254 }
255 schedule_work(&sysrq_showallcpus);
256 }
Rik van Riel5045bca2008-04-29 00:59:21 -0700257}
258
259static struct sysrq_key_op sysrq_showallcpus_op = {
260 .handler = sysrq_handle_showallcpus,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800261 .help_msg = "show-backtrace-all-active-cpus(l)",
Rik van Riel5045bca2008-04-29 00:59:21 -0700262 .action_msg = "Show backtrace of all active CPUs",
263 .enable_mask = SYSRQ_ENABLE_DUMP,
264};
265#endif
266
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700267static void sysrq_handle_showregs(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
David Howells7d12e782006-10-05 14:55:46 +0100269 struct pt_regs *regs = get_irq_regs();
270 if (regs)
271 show_regs(regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200272 perf_event_print_debug();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274static struct sysrq_key_op sysrq_showregs_op = {
275 .handler = sysrq_handle_showregs,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800276 .help_msg = "show-registers(p)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 .action_msg = "Show Regs",
278 .enable_mask = SYSRQ_ENABLE_DUMP,
279};
280
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700281static void sysrq_handle_showstate(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 show_state();
Tejun Heo3494fc32015-03-09 09:22:28 -0400284 show_workqueue_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286static struct sysrq_key_op sysrq_showstate_op = {
287 .handler = sysrq_handle_showstate,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800288 .help_msg = "show-task-states(t)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 .action_msg = "Show State",
290 .enable_mask = SYSRQ_ENABLE_DUMP,
291};
292
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700293static void sysrq_handle_showstate_blocked(int key)
Ingo Molnare59e2ae2006-12-06 20:35:59 -0800294{
295 show_state_filter(TASK_UNINTERRUPTIBLE);
296}
297static struct sysrq_key_op sysrq_showstate_blocked_op = {
298 .handler = sysrq_handle_showstate_blocked,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800299 .help_msg = "show-blocked-tasks(w)",
Ingo Molnare59e2ae2006-12-06 20:35:59 -0800300 .action_msg = "Show Blocked State",
301 .enable_mask = SYSRQ_ENABLE_DUMP,
302};
303
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100304#ifdef CONFIG_TRACING
305#include <linux/ftrace.h>
306
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700307static void sysrq_ftrace_dump(int key)
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100308{
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200309 ftrace_dump(DUMP_ALL);
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100310}
311static struct sysrq_key_op sysrq_ftrace_dump_op = {
312 .handler = sysrq_ftrace_dump,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800313 .help_msg = "dump-ftrace-buffer(z)",
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100314 .action_msg = "Dump ftrace buffer",
315 .enable_mask = SYSRQ_ENABLE_DUMP,
316};
317#else
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700318#define sysrq_ftrace_dump_op (*(struct sysrq_key_op *)NULL)
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100319#endif
Ingo Molnare59e2ae2006-12-06 20:35:59 -0800320
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700321static void sysrq_handle_showmem(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Michal Hocko9af744d2017-02-22 15:46:16 -0800323 show_mem(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325static struct sysrq_key_op sysrq_showmem_op = {
326 .handler = sysrq_handle_showmem,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800327 .help_msg = "show-memory-usage(m)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 .action_msg = "Show Memory",
329 .enable_mask = SYSRQ_ENABLE_DUMP,
330};
331
Andrew Mortonbf36b902006-03-25 03:07:08 -0800332/*
333 * Signal sysrq helper function. Sends a signal to all user processes.
334 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335static void send_sig_all(int sig)
336{
337 struct task_struct *p;
338
Anton Vorontsove502bab2012-02-07 10:49:39 +0400339 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 for_each_process(p) {
Anton Vorontsovd3a532a2012-02-07 10:49:51 +0400341 if (p->flags & PF_KTHREAD)
342 continue;
343 if (is_global_init(p))
344 continue;
345
Anton Vorontsovb82c3282012-04-05 14:25:05 -0700346 do_send_sig_info(sig, SEND_SIG_FORCED, p, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
Anton Vorontsove502bab2012-02-07 10:49:39 +0400348 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700351static void sysrq_handle_term(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 send_sig_all(SIGTERM);
Borislav Petkova8fe19e2014-06-04 16:11:46 -0700354 console_loglevel = CONSOLE_LOGLEVEL_DEBUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356static struct sysrq_key_op sysrq_term_op = {
357 .handler = sysrq_handle_term,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800358 .help_msg = "terminate-all-tasks(e)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 .action_msg = "Terminate All Tasks",
360 .enable_mask = SYSRQ_ENABLE_SIGNAL,
361};
362
David Howells65f27f32006-11-22 14:55:48 +0000363static void moom_callback(struct work_struct *ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
David Rientjes6e0fc462015-09-08 15:00:36 -0700365 const gfp_t gfp_mask = GFP_KERNEL;
366 struct oom_control oc = {
367 .zonelist = node_zonelist(first_memory_node, gfp_mask),
368 .nodemask = NULL,
Vladimir Davydov2a966b72016-07-26 15:22:33 -0700369 .memcg = NULL,
David Rientjes6e0fc462015-09-08 15:00:36 -0700370 .gfp_mask = gfp_mask,
David Rientjes54e9e292015-09-08 15:00:39 -0700371 .order = -1,
David Rientjes6e0fc462015-09-08 15:00:36 -0700372 };
373
Johannes Weinerdc564012015-06-24 16:57:19 -0700374 mutex_lock(&oom_lock);
David Rientjes6e0fc462015-09-08 15:00:36 -0700375 if (!out_of_memory(&oc))
Michal Hockod75da002017-05-03 14:54:57 -0700376 pr_info("OOM request ignored. No task eligible\n");
Johannes Weinerdc564012015-06-24 16:57:19 -0700377 mutex_unlock(&oom_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
David Howells65f27f32006-11-22 14:55:48 +0000380static DECLARE_WORK(moom_work, moom_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700382static void sysrq_handle_moom(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
384 schedule_work(&moom_work);
385}
386static struct sysrq_key_op sysrq_moom_op = {
387 .handler = sysrq_handle_moom,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800388 .help_msg = "memory-full-oom-kill(f)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 .action_msg = "Manual OOM execution",
Naohiro Ooiwab4236f82008-10-15 22:01:43 -0700390 .enable_mask = SYSRQ_ENABLE_SIGNAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391};
392
Eric Sandeenc2d75432009-03-31 15:23:46 -0700393#ifdef CONFIG_BLOCK
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700394static void sysrq_handle_thaw(int key)
Eric Sandeenc2d75432009-03-31 15:23:46 -0700395{
396 emergency_thaw_all();
397}
398static struct sysrq_key_op sysrq_thaw_op = {
399 .handler = sysrq_handle_thaw,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800400 .help_msg = "thaw-filesystems(j)",
Eric Sandeenc2d75432009-03-31 15:23:46 -0700401 .action_msg = "Emergency Thaw of all frozen filesystems",
402 .enable_mask = SYSRQ_ENABLE_SIGNAL,
403};
404#endif
405
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700406static void sysrq_handle_kill(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
408 send_sig_all(SIGKILL);
Borislav Petkova8fe19e2014-06-04 16:11:46 -0700409 console_loglevel = CONSOLE_LOGLEVEL_DEBUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411static struct sysrq_key_op sysrq_kill_op = {
412 .handler = sysrq_handle_kill,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800413 .help_msg = "kill-all-tasks(i)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 .action_msg = "Kill All Tasks",
415 .enable_mask = SYSRQ_ENABLE_SIGNAL,
416};
417
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700418static void sysrq_handle_unrt(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 normalize_rt_tasks();
421}
422static struct sysrq_key_op sysrq_unrt_op = {
423 .handler = sysrq_handle_unrt,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800424 .help_msg = "nice-all-RT-tasks(n)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 .action_msg = "Nice All RT Tasks",
426 .enable_mask = SYSRQ_ENABLE_RTNICE,
427};
428
429/* Key Operations table and lock */
430static DEFINE_SPINLOCK(sysrq_key_table_lock);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800431
432static struct sysrq_key_op *sysrq_key_table[36] = {
433 &sysrq_loglevel_op, /* 0 */
434 &sysrq_loglevel_op, /* 1 */
435 &sysrq_loglevel_op, /* 2 */
436 &sysrq_loglevel_op, /* 3 */
437 &sysrq_loglevel_op, /* 4 */
438 &sysrq_loglevel_op, /* 5 */
439 &sysrq_loglevel_op, /* 6 */
440 &sysrq_loglevel_op, /* 7 */
441 &sysrq_loglevel_op, /* 8 */
442 &sysrq_loglevel_op, /* 9 */
443
444 /*
Randy Dunlapd346cce2007-01-31 23:48:17 -0800445 * a: Don't use for system provided sysrqs, it is handled specially on
446 * sparc and will never arrive.
Andrew Mortonbf36b902006-03-25 03:07:08 -0800447 */
448 NULL, /* a */
449 &sysrq_reboot_op, /* b */
Eric Biggers5e351412017-04-03 22:50:20 -0700450 &sysrq_crash_op, /* c */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800451 &sysrq_showlocks_op, /* d */
452 &sysrq_term_op, /* e */
453 &sysrq_moom_op, /* f */
Jason Wessel364b5b72009-05-13 21:56:59 -0500454 /* g: May be registered for the kernel debugger */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800455 NULL, /* g */
Eric Sandeenc2d75432009-03-31 15:23:46 -0700456 NULL, /* h - reserved for help */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800457 &sysrq_kill_op, /* i */
Eric Sandeenc2d75432009-03-31 15:23:46 -0700458#ifdef CONFIG_BLOCK
459 &sysrq_thaw_op, /* j */
460#else
Andrew Mortonbf36b902006-03-25 03:07:08 -0800461 NULL, /* j */
Eric Sandeenc2d75432009-03-31 15:23:46 -0700462#endif
Andrew Mortonbf36b902006-03-25 03:07:08 -0800463 &sysrq_SAK_op, /* k */
Rik van Riel5045bca2008-04-29 00:59:21 -0700464#ifdef CONFIG_SMP
465 &sysrq_showallcpus_op, /* l */
466#else
Andrew Mortonbf36b902006-03-25 03:07:08 -0800467 NULL, /* l */
Rik van Riel5045bca2008-04-29 00:59:21 -0700468#endif
Andrew Mortonbf36b902006-03-25 03:07:08 -0800469 &sysrq_showmem_op, /* m */
470 &sysrq_unrt_op, /* n */
Randy Dunlapd346cce2007-01-31 23:48:17 -0800471 /* o: This will often be registered as 'Off' at init time */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800472 NULL, /* o */
473 &sysrq_showregs_op, /* p */
Ingo Molnar88ad0bf62007-02-16 01:28:16 -0800474 &sysrq_show_timers_op, /* q */
Randy Dunlapd346cce2007-01-31 23:48:17 -0800475 &sysrq_unraw_op, /* r */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800476 &sysrq_sync_op, /* s */
477 &sysrq_showstate_op, /* t */
478 &sysrq_mountro_op, /* u */
Jason Wessel364b5b72009-05-13 21:56:59 -0500479 /* v: May be registered for frame buffer console restore */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800480 NULL, /* v */
Randy Dunlapd346cce2007-01-31 23:48:17 -0800481 &sysrq_showstate_blocked_op, /* w */
James Hogand1e9a4f2015-05-19 09:50:29 +0100482 /* x: May be registered on mips for TLB dump */
Randy Dunlapd346cce2007-01-31 23:48:17 -0800483 /* x: May be registered on ppc/powerpc for xmon */
David S. Miller916ca142012-10-16 09:34:01 -0700484 /* x: May be registered on sparc64 for global PMU dump */
Randy Dunlapd346cce2007-01-31 23:48:17 -0800485 NULL, /* x */
David S. Miller93dae5b2008-05-19 23:46:00 -0700486 /* y: May be registered on sparc64 for global register dump */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800487 NULL, /* y */
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100488 &sysrq_ftrace_dump_op, /* z */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489};
490
491/* key2index calculation, -1 on invalid index */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800492static int sysrq_key_table_key2index(int key)
493{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 int retval;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800495
496 if ((key >= '0') && (key <= '9'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 retval = key - '0';
Andrew Mortonbf36b902006-03-25 03:07:08 -0800498 else if ((key >= 'a') && (key <= 'z'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 retval = key + 10 - 'a';
Andrew Mortonbf36b902006-03-25 03:07:08 -0800500 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 retval = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return retval;
503}
504
505/*
506 * get and put functions for the table, exposed to modules.
507 */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800508struct sysrq_key_op *__sysrq_get_key_op(int key)
509{
510 struct sysrq_key_op *op_p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 int i;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 i = sysrq_key_table_key2index(key);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800514 if (i != -1)
515 op_p = sysrq_key_table[i];
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return op_p;
518}
519
Andrew Mortonbf36b902006-03-25 03:07:08 -0800520static void __sysrq_put_key_op(int key, struct sysrq_key_op *op_p)
521{
522 int i = sysrq_key_table_key2index(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (i != -1)
525 sysrq_key_table[i] = op_p;
526}
527
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700528void __handle_sysrq(int key, bool check_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 struct sysrq_key_op *op_p;
531 int orig_log_level;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800532 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Rik van Riel722773a2014-06-06 14:38:14 -0700534 rcu_sysrq_start();
Rik van Riel984d74a2014-06-06 14:38:13 -0700535 rcu_read_lock();
Andy Whitcroftfb144ad2009-01-15 13:50:52 -0800536 /*
537 * Raise the apparent loglevel to maximum so that the sysrq header
538 * is shown to provide the user with positive feedback. We do not
539 * simply emit this at KERN_EMERG as that would change message
540 * routing in the consumers of /proc/kmsg.
541 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 orig_log_level = console_loglevel;
Borislav Petkova8fe19e2014-06-04 16:11:46 -0700543 console_loglevel = CONSOLE_LOGLEVEL_DEFAULT;
Michal Hocko401e4a72015-02-11 15:26:21 -0800544 pr_info("SysRq : ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 op_p = __sysrq_get_key_op(key);
547 if (op_p) {
Andrew Mortonbf36b902006-03-25 03:07:08 -0800548 /*
549 * Should we check for enabled operations (/proc/sysrq-trigger
550 * should not) and is the invoked operation enabled?
551 */
Ingo Molnar5d6f6472006-12-13 00:34:36 -0800552 if (!check_mask || sysrq_on_mask(op_p->enable_mask)) {
Michal Hocko401e4a72015-02-11 15:26:21 -0800553 pr_cont("%s\n", op_p->action_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 console_loglevel = orig_log_level;
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700555 op_p->handler(key);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800556 } else {
Michal Hocko401e4a72015-02-11 15:26:21 -0800557 pr_cont("This sysrq operation is disabled.\n");
Andrew Mortonbf36b902006-03-25 03:07:08 -0800558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 } else {
Michal Hocko401e4a72015-02-11 15:26:21 -0800560 pr_cont("HELP : ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 /* Only print the help msg once per handler */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800562 for (i = 0; i < ARRAY_SIZE(sysrq_key_table); i++) {
563 if (sysrq_key_table[i]) {
564 int j;
565
566 for (j = 0; sysrq_key_table[i] !=
567 sysrq_key_table[j]; j++)
568 ;
569 if (j != i)
570 continue;
Michal Hocko401e4a72015-02-11 15:26:21 -0800571 pr_cont("%s ", sysrq_key_table[i]->help_msg);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
Michal Hocko401e4a72015-02-11 15:26:21 -0800574 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 console_loglevel = orig_log_level;
576 }
Rik van Riel984d74a2014-06-06 14:38:13 -0700577 rcu_read_unlock();
Rik van Riel722773a2014-06-06 14:38:14 -0700578 rcu_sysrq_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700581void handle_sysrq(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Ingo Molnar5d6f6472006-12-13 00:34:36 -0800583 if (sysrq_on())
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700584 __handle_sysrq(key, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
Andrew Mortonbf36b902006-03-25 03:07:08 -0800586EXPORT_SYMBOL(handle_sysrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700588#ifdef CONFIG_INPUT
Arnd Bergmannffb6e0c2015-05-26 14:45:29 -0700589static int sysrq_reset_downtime_ms;
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700590
591/* Simple translation table for the SysRq keys */
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800592static const unsigned char sysrq_xlate[KEY_CNT] =
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700593 "\000\0331234567890-=\177\t" /* 0x00 - 0x0f */
594 "qwertyuiop[]\r\000as" /* 0x10 - 0x1f */
595 "dfghjkl;'`\000\\zxcv" /* 0x20 - 0x2f */
596 "bnm,./\000*\000 \000\201\202\203\204\205" /* 0x30 - 0x3f */
597 "\206\207\210\211\212\000\000789-456+1" /* 0x40 - 0x4f */
598 "230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
599 "\r\000/"; /* 0x60 - 0x6f */
600
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800601struct sysrq_state {
602 struct input_handle handle;
603 struct work_struct reinject_work;
604 unsigned long key_down[BITS_TO_LONGS(KEY_CNT)];
605 unsigned int alt;
606 unsigned int alt_use;
607 bool active;
608 bool need_reinject;
Dmitry Torokhov7ab7b5a2011-02-02 22:59:54 -0800609 bool reinjecting;
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800610
611 /* reset sequence handling */
612 bool reset_canceled;
Mathieu J. Poirier3d289512013-06-05 22:51:46 -0700613 bool reset_requested;
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800614 unsigned long reset_keybit[BITS_TO_LONGS(KEY_CNT)];
615 int reset_seq_len;
616 int reset_seq_cnt;
617 int reset_seq_version;
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700618 struct timer_list keyreset_timer;
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800619};
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700620
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800621#define SYSRQ_KEY_RESET_MAX 20 /* Should be plenty */
622static unsigned short sysrq_reset_seq[SYSRQ_KEY_RESET_MAX];
623static unsigned int sysrq_reset_seq_len;
624static unsigned int sysrq_reset_seq_version = 1;
625
626static void sysrq_parse_reset_sequence(struct sysrq_state *state)
627{
628 int i;
629 unsigned short key;
630
631 state->reset_seq_cnt = 0;
632
633 for (i = 0; i < sysrq_reset_seq_len; i++) {
634 key = sysrq_reset_seq[i];
635
636 if (key == KEY_RESERVED || key > KEY_MAX)
637 break;
638
639 __set_bit(key, state->reset_keybit);
640 state->reset_seq_len++;
641
642 if (test_bit(key, state->key_down))
643 state->reset_seq_cnt++;
644 }
645
646 /* Disable reset until old keys are not released */
647 state->reset_canceled = state->reset_seq_cnt != 0;
648
649 state->reset_seq_version = sysrq_reset_seq_version;
650}
651
Mathieu J. Poirier3d289512013-06-05 22:51:46 -0700652static void sysrq_do_reset(unsigned long _state)
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700653{
Mathieu J. Poirier3d289512013-06-05 22:51:46 -0700654 struct sysrq_state *state = (struct sysrq_state *) _state;
655
656 state->reset_requested = true;
657
658 sys_sync();
659 kernel_restart(NULL);
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700660}
661
662static void sysrq_handle_reset_request(struct sysrq_state *state)
663{
Mathieu J. Poirier3d289512013-06-05 22:51:46 -0700664 if (state->reset_requested)
665 __handle_sysrq(sysrq_xlate[KEY_B], false);
666
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700667 if (sysrq_reset_downtime_ms)
668 mod_timer(&state->keyreset_timer,
669 jiffies + msecs_to_jiffies(sysrq_reset_downtime_ms));
670 else
Mathieu J. Poirier3d289512013-06-05 22:51:46 -0700671 sysrq_do_reset((unsigned long)state);
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700672}
673
674static void sysrq_detect_reset_sequence(struct sysrq_state *state,
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800675 unsigned int code, int value)
676{
677 if (!test_bit(code, state->reset_keybit)) {
678 /*
679 * Pressing any key _not_ in reset sequence cancels
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700680 * the reset sequence. Also cancelling the timer in
681 * case additional keys were pressed after a reset
682 * has been requested.
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800683 */
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700684 if (value && state->reset_seq_cnt) {
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800685 state->reset_canceled = true;
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700686 del_timer(&state->keyreset_timer);
687 }
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800688 } else if (value == 0) {
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700689 /*
690 * Key release - all keys in the reset sequence need
691 * to be pressed and held for the reset timeout
692 * to hold.
693 */
694 del_timer(&state->keyreset_timer);
695
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800696 if (--state->reset_seq_cnt == 0)
697 state->reset_canceled = false;
698 } else if (value == 1) {
699 /* key press, not autorepeat */
700 if (++state->reset_seq_cnt == state->reset_seq_len &&
701 !state->reset_canceled) {
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700702 sysrq_handle_reset_request(state);
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800703 }
704 }
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800705}
706
Mathieu J. Poirier4c076eb2013-08-03 17:22:08 -0700707#ifdef CONFIG_OF
708static void sysrq_of_get_keyreset_config(void)
709{
710 u32 key;
711 struct device_node *np;
712 struct property *prop;
713 const __be32 *p;
714
715 np = of_find_node_by_path("/chosen/linux,sysrq-reset-seq");
716 if (!np) {
717 pr_debug("No sysrq node found");
718 return;
719 }
720
721 /* Reset in case a __weak definition was present */
722 sysrq_reset_seq_len = 0;
723
724 of_property_for_each_u32(np, "keyset", prop, p, key) {
725 if (key == KEY_RESERVED || key > KEY_MAX ||
726 sysrq_reset_seq_len == SYSRQ_KEY_RESET_MAX)
727 break;
728
729 sysrq_reset_seq[sysrq_reset_seq_len++] = (unsigned short)key;
730 }
731
732 /* Get reset timeout if any. */
733 of_property_read_u32(np, "timeout-ms", &sysrq_reset_downtime_ms);
734}
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;
Mathieu J. Poirier3d289512013-06-05 22:51:46 -0700907 setup_timer(&sysrq->keyreset_timer,
908 sysrq_do_reset, (unsigned long)sysrq);
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800909
910 error = input_register_handle(&sysrq->handle);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700911 if (error) {
912 pr_err("Failed to register input sysrq handler, error %d\n",
913 error);
914 goto err_free;
915 }
916
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800917 error = input_open_device(&sysrq->handle);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700918 if (error) {
919 pr_err("Failed to open input device, error %d\n", error);
920 goto err_unregister;
921 }
922
923 return 0;
924
925 err_unregister:
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800926 input_unregister_handle(&sysrq->handle);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700927 err_free:
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800928 kfree(sysrq);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700929 return error;
930}
931
932static void sysrq_disconnect(struct input_handle *handle)
933{
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800934 struct sysrq_state *sysrq = handle->private;
935
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700936 input_close_device(handle);
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800937 cancel_work_sync(&sysrq->reinject_work);
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700938 del_timer_sync(&sysrq->keyreset_timer);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700939 input_unregister_handle(handle);
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800940 kfree(sysrq);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700941}
942
943/*
Dmitry Torokhov1966cb22010-09-29 18:04:21 -0700944 * We are matching on KEY_LEFTALT instead of KEY_SYSRQ because not all
945 * keyboards have SysRq key predefined and so user may add it to keymap
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700946 * later, but we expect all such keyboards to have left alt.
947 */
948static const struct input_device_id sysrq_ids[] = {
949 {
950 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
951 INPUT_DEVICE_ID_MATCH_KEYBIT,
Akinobu Mita802c0382017-01-06 02:14:16 +0900952 .evbit = { [BIT_WORD(EV_KEY)] = BIT_MASK(EV_KEY) },
953 .keybit = { [BIT_WORD(KEY_LEFTALT)] = BIT_MASK(KEY_LEFTALT) },
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700954 },
955 { },
956};
957
958static struct input_handler sysrq_handler = {
959 .filter = sysrq_filter,
960 .connect = sysrq_connect,
961 .disconnect = sysrq_disconnect,
962 .name = "sysrq",
963 .id_table = sysrq_ids,
964};
965
966static bool sysrq_handler_registered;
967
968static inline void sysrq_register_handler(void)
969{
970 int error;
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800971
Mathieu J. Poirier4c076eb2013-08-03 17:22:08 -0700972 sysrq_of_get_keyreset_config();
973
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700974 error = input_register_handler(&sysrq_handler);
975 if (error)
976 pr_err("Failed to register input handler, error %d", error);
977 else
978 sysrq_handler_registered = true;
979}
980
981static inline void sysrq_unregister_handler(void)
982{
983 if (sysrq_handler_registered) {
984 input_unregister_handler(&sysrq_handler);
985 sysrq_handler_registered = false;
986 }
987}
988
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800989static int sysrq_reset_seq_param_set(const char *buffer,
990 const struct kernel_param *kp)
991{
992 unsigned long val;
993 int error;
994
Jingoo Han86b40562013-06-01 16:30:06 +0900995 error = kstrtoul(buffer, 0, &val);
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800996 if (error < 0)
997 return error;
998
999 if (val > KEY_MAX)
1000 return -EINVAL;
1001
1002 *((unsigned short *)kp->arg) = val;
1003 sysrq_reset_seq_version++;
1004
1005 return 0;
1006}
1007
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09301008static const struct kernel_param_ops param_ops_sysrq_reset_seq = {
Mathieu Poirier154b7a42013-01-06 23:23:33 -08001009 .get = param_get_ushort,
1010 .set = sysrq_reset_seq_param_set,
1011};
1012
1013#define param_check_sysrq_reset_seq(name, p) \
1014 __param_check(name, p, unsigned short)
1015
Paul Gortmaker3bce6f62015-08-19 17:48:06 -04001016/*
1017 * not really modular, but the easiest way to keep compat with existing
1018 * bootargs behaviour is to continue using module_param here.
1019 */
Mathieu Poirier154b7a42013-01-06 23:23:33 -08001020module_param_array_named(reset_seq, sysrq_reset_seq, sysrq_reset_seq,
1021 &sysrq_reset_seq_len, 0644);
1022
Mathieu J. Poirier39030782013-04-01 22:14:19 -07001023module_param_named(sysrq_downtime_ms, sysrq_reset_downtime_ms, int, 0644);
1024
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -07001025#else
1026
1027static inline void sysrq_register_handler(void)
1028{
1029}
1030
1031static inline void sysrq_unregister_handler(void)
1032{
1033}
1034
1035#endif /* CONFIG_INPUT */
1036
1037int sysrq_toggle_support(int enable_mask)
1038{
1039 bool was_enabled = sysrq_on();
1040
1041 sysrq_enabled = enable_mask;
1042
1043 if (was_enabled != sysrq_on()) {
1044 if (sysrq_on())
1045 sysrq_register_handler();
1046 else
1047 sysrq_unregister_handler();
1048 }
1049
1050 return 0;
1051}
1052
Adrian Bunkcf62ddc2005-11-08 21:39:47 -08001053static int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p,
Andrew Mortonbf36b902006-03-25 03:07:08 -08001054 struct sysrq_key_op *remove_op_p)
1055{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Rik van Riel984d74a2014-06-06 14:38:13 -07001058 spin_lock(&sysrq_key_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (__sysrq_get_key_op(key) == remove_op_p) {
1060 __sysrq_put_key_op(key, insert_op_p);
1061 retval = 0;
1062 } else {
1063 retval = -1;
1064 }
Rik van Riel984d74a2014-06-06 14:38:13 -07001065 spin_unlock(&sysrq_key_table_lock);
1066
1067 /*
1068 * A concurrent __handle_sysrq either got the old op or the new op.
1069 * Wait for it to go away before returning, so the code for an old
1070 * op is not freed (eg. on module unload) while it is in use.
1071 */
1072 synchronize_rcu();
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return retval;
1075}
1076
1077int register_sysrq_key(int key, struct sysrq_key_op *op_p)
1078{
1079 return __sysrq_swap_key_ops(key, op_p, NULL);
1080}
Andrew Mortonbf36b902006-03-25 03:07:08 -08001081EXPORT_SYMBOL(register_sysrq_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083int unregister_sysrq_key(int key, struct sysrq_key_op *op_p)
1084{
1085 return __sysrq_swap_key_ops(key, NULL, op_p);
1086}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087EXPORT_SYMBOL(unregister_sysrq_key);
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -07001088
1089#ifdef CONFIG_PROC_FS
1090/*
1091 * writing 'C' to /proc/sysrq-trigger is like sysrq-C
1092 */
1093static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
1094 size_t count, loff_t *ppos)
1095{
1096 if (count) {
1097 char c;
1098
1099 if (get_user(c, buf))
1100 return -EFAULT;
Dmitry Torokhovf3353972010-08-17 21:15:47 -07001101 __handle_sysrq(c, false);
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -07001102 }
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -07001103
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -07001104 return count;
1105}
1106
1107static const struct file_operations proc_sysrq_trigger_operations = {
1108 .write = write_sysrq_trigger,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001109 .llseek = noop_llseek,
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -07001110};
1111
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -07001112static void sysrq_init_procfs(void)
1113{
1114 if (!proc_create("sysrq-trigger", S_IWUSR, NULL,
1115 &proc_sysrq_trigger_operations))
1116 pr_err("Failed to register proc interface\n");
1117}
1118
1119#else
1120
1121static inline void sysrq_init_procfs(void)
1122{
1123}
1124
1125#endif /* CONFIG_PROC_FS */
1126
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -07001127static int __init sysrq_init(void)
1128{
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -07001129 sysrq_init_procfs();
1130
1131 if (sysrq_on())
1132 sysrq_register_handler();
1133
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -07001134 return 0;
1135}
Paul Gortmaker3bce6f62015-08-19 17:48:06 -04001136device_initcall(sysrq_init);