blob: 1d4f317a0e4279de7c8b86d6bae1373c22e19c31 [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
Julien Grall6ac972d2019-03-13 11:40:34 +0000211static DEFINE_RAW_SPINLOCK(show_lock);
Rik van Riel5045bca2008-04-29 00:59:21 -0700212
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
Julien Grall6ac972d2019-03-13 11:40:34 +0000221 raw_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);
Julien Grall6ac972d2019-03-13 11:40:34 +0000224 raw_spin_unlock_irqrestore(&show_lock, flags);
Rik van Riel5045bca2008-04-29 00:59:21 -0700225}
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;
Feng Tangc39ea0b2019-05-14 15:45:34 -0700530 int orig_suppress_printk;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800531 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Feng Tangc39ea0b2019-05-14 15:45:34 -0700533 orig_suppress_printk = suppress_printk;
534 suppress_printk = 0;
535
Rik van Riel722773a2014-06-06 14:38:14 -0700536 rcu_sysrq_start();
Rik van Riel984d74a2014-06-06 14:38:13 -0700537 rcu_read_lock();
Andy Whitcroftfb144ad2009-01-15 13:50:52 -0800538 /*
539 * Raise the apparent loglevel to maximum so that the sysrq header
540 * is shown to provide the user with positive feedback. We do not
541 * simply emit this at KERN_EMERG as that would change message
542 * routing in the consumers of /proc/kmsg.
543 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 orig_log_level = console_loglevel;
Borislav Petkova8fe19e2014-06-04 16:11:46 -0700545 console_loglevel = CONSOLE_LOGLEVEL_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 op_p = __sysrq_get_key_op(key);
548 if (op_p) {
Andrew Mortonbf36b902006-03-25 03:07:08 -0800549 /*
550 * Should we check for enabled operations (/proc/sysrq-trigger
551 * should not) and is the invoked operation enabled?
552 */
Ingo Molnar5d6f6472006-12-13 00:34:36 -0800553 if (!check_mask || sysrq_on_mask(op_p->enable_mask)) {
Petr Mladekc3fee602019-01-11 17:20:37 +0100554 pr_info("%s\n", op_p->action_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 console_loglevel = orig_log_level;
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700556 op_p->handler(key);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800557 } else {
Petr Mladekc3fee602019-01-11 17:20:37 +0100558 pr_info("This sysrq operation is disabled.\n");
Petr Mladek075e1a02019-01-11 13:45:15 +0100559 console_loglevel = orig_log_level;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 } else {
Petr Mladekc3fee602019-01-11 17:20:37 +0100562 pr_info("HELP : ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /* Only print the help msg once per handler */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800564 for (i = 0; i < ARRAY_SIZE(sysrq_key_table); i++) {
565 if (sysrq_key_table[i]) {
566 int j;
567
568 for (j = 0; sysrq_key_table[i] !=
569 sysrq_key_table[j]; j++)
570 ;
571 if (j != i)
572 continue;
Michal Hocko401e4a72015-02-11 15:26:21 -0800573 pr_cont("%s ", sysrq_key_table[i]->help_msg);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
Michal Hocko401e4a72015-02-11 15:26:21 -0800576 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 console_loglevel = orig_log_level;
578 }
Rik van Riel984d74a2014-06-06 14:38:13 -0700579 rcu_read_unlock();
Rik van Riel722773a2014-06-06 14:38:14 -0700580 rcu_sysrq_end();
Feng Tangc39ea0b2019-05-14 15:45:34 -0700581
582 suppress_printk = orig_suppress_printk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700585void handle_sysrq(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Ingo Molnar5d6f6472006-12-13 00:34:36 -0800587 if (sysrq_on())
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700588 __handle_sysrq(key, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
Andrew Mortonbf36b902006-03-25 03:07:08 -0800590EXPORT_SYMBOL(handle_sysrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700592#ifdef CONFIG_INPUT
Arnd Bergmannffb6e0c2015-05-26 14:45:29 -0700593static int sysrq_reset_downtime_ms;
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700594
595/* Simple translation table for the SysRq keys */
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800596static const unsigned char sysrq_xlate[KEY_CNT] =
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700597 "\000\0331234567890-=\177\t" /* 0x00 - 0x0f */
598 "qwertyuiop[]\r\000as" /* 0x10 - 0x1f */
599 "dfghjkl;'`\000\\zxcv" /* 0x20 - 0x2f */
600 "bnm,./\000*\000 \000\201\202\203\204\205" /* 0x30 - 0x3f */
601 "\206\207\210\211\212\000\000789-456+1" /* 0x40 - 0x4f */
602 "230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
603 "\r\000/"; /* 0x60 - 0x6f */
604
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800605struct sysrq_state {
606 struct input_handle handle;
607 struct work_struct reinject_work;
608 unsigned long key_down[BITS_TO_LONGS(KEY_CNT)];
609 unsigned int alt;
610 unsigned int alt_use;
611 bool active;
612 bool need_reinject;
Dmitry Torokhov7ab7b5a2011-02-02 22:59:54 -0800613 bool reinjecting;
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800614
615 /* reset sequence handling */
616 bool reset_canceled;
Mathieu J. Poirier3d289512013-06-05 22:51:46 -0700617 bool reset_requested;
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800618 unsigned long reset_keybit[BITS_TO_LONGS(KEY_CNT)];
619 int reset_seq_len;
620 int reset_seq_cnt;
621 int reset_seq_version;
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700622 struct timer_list keyreset_timer;
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800623};
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700624
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800625#define SYSRQ_KEY_RESET_MAX 20 /* Should be plenty */
626static unsigned short sysrq_reset_seq[SYSRQ_KEY_RESET_MAX];
627static unsigned int sysrq_reset_seq_len;
628static unsigned int sysrq_reset_seq_version = 1;
629
630static void sysrq_parse_reset_sequence(struct sysrq_state *state)
631{
632 int i;
633 unsigned short key;
634
635 state->reset_seq_cnt = 0;
636
637 for (i = 0; i < sysrq_reset_seq_len; i++) {
638 key = sysrq_reset_seq[i];
639
640 if (key == KEY_RESERVED || key > KEY_MAX)
641 break;
642
643 __set_bit(key, state->reset_keybit);
644 state->reset_seq_len++;
645
646 if (test_bit(key, state->key_down))
647 state->reset_seq_cnt++;
648 }
649
650 /* Disable reset until old keys are not released */
651 state->reset_canceled = state->reset_seq_cnt != 0;
652
653 state->reset_seq_version = sysrq_reset_seq_version;
654}
655
Kees Cook8c318fa2017-10-16 16:28:51 -0700656static void sysrq_do_reset(struct timer_list *t)
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700657{
Kees Cook8c318fa2017-10-16 16:28:51 -0700658 struct sysrq_state *state = from_timer(state, t, keyreset_timer);
Mathieu J. Poirier3d289512013-06-05 22:51:46 -0700659
660 state->reset_requested = true;
661
Mark Tomlinson8fefbc62018-11-27 09:57:23 +1300662 orderly_reboot();
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700663}
664
665static void sysrq_handle_reset_request(struct sysrq_state *state)
666{
Mathieu J. Poirier3d289512013-06-05 22:51:46 -0700667 if (state->reset_requested)
668 __handle_sysrq(sysrq_xlate[KEY_B], false);
669
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700670 if (sysrq_reset_downtime_ms)
671 mod_timer(&state->keyreset_timer,
672 jiffies + msecs_to_jiffies(sysrq_reset_downtime_ms));
673 else
Kees Cook8c318fa2017-10-16 16:28:51 -0700674 sysrq_do_reset(&state->keyreset_timer);
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700675}
676
677static void sysrq_detect_reset_sequence(struct sysrq_state *state,
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800678 unsigned int code, int value)
679{
680 if (!test_bit(code, state->reset_keybit)) {
681 /*
682 * Pressing any key _not_ in reset sequence cancels
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700683 * the reset sequence. Also cancelling the timer in
684 * case additional keys were pressed after a reset
685 * has been requested.
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800686 */
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700687 if (value && state->reset_seq_cnt) {
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800688 state->reset_canceled = true;
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700689 del_timer(&state->keyreset_timer);
690 }
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800691 } else if (value == 0) {
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700692 /*
693 * Key release - all keys in the reset sequence need
694 * to be pressed and held for the reset timeout
695 * to hold.
696 */
697 del_timer(&state->keyreset_timer);
698
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800699 if (--state->reset_seq_cnt == 0)
700 state->reset_canceled = false;
701 } else if (value == 1) {
702 /* key press, not autorepeat */
703 if (++state->reset_seq_cnt == state->reset_seq_len &&
704 !state->reset_canceled) {
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700705 sysrq_handle_reset_request(state);
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800706 }
707 }
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800708}
709
Mathieu J. Poirier4c076eb2013-08-03 17:22:08 -0700710#ifdef CONFIG_OF
711static void sysrq_of_get_keyreset_config(void)
712{
713 u32 key;
714 struct device_node *np;
715 struct property *prop;
716 const __be32 *p;
717
718 np = of_find_node_by_path("/chosen/linux,sysrq-reset-seq");
719 if (!np) {
720 pr_debug("No sysrq node found");
721 return;
722 }
723
724 /* Reset in case a __weak definition was present */
725 sysrq_reset_seq_len = 0;
726
727 of_property_for_each_u32(np, "keyset", prop, p, key) {
728 if (key == KEY_RESERVED || key > KEY_MAX ||
729 sysrq_reset_seq_len == SYSRQ_KEY_RESET_MAX)
730 break;
731
732 sysrq_reset_seq[sysrq_reset_seq_len++] = (unsigned short)key;
733 }
734
735 /* Get reset timeout if any. */
736 of_property_read_u32(np, "timeout-ms", &sysrq_reset_downtime_ms);
Yangtao Li279070b2018-11-21 08:12:14 -0500737
738 of_node_put(np);
Mathieu J. Poirier4c076eb2013-08-03 17:22:08 -0700739}
740#else
741static void sysrq_of_get_keyreset_config(void)
742{
743}
744#endif
745
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800746static void sysrq_reinject_alt_sysrq(struct work_struct *work)
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700747{
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800748 struct sysrq_state *sysrq =
749 container_of(work, struct sysrq_state, reinject_work);
750 struct input_handle *handle = &sysrq->handle;
751 unsigned int alt_code = sysrq->alt_use;
752
753 if (sysrq->need_reinject) {
Dmitry Torokhov7ab7b5a2011-02-02 22:59:54 -0800754 /* we do not want the assignment to be reordered */
755 sysrq->reinjecting = true;
756 mb();
757
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800758 /* Simulate press and release of Alt + SysRq */
759 input_inject_event(handle, EV_KEY, alt_code, 1);
760 input_inject_event(handle, EV_KEY, KEY_SYSRQ, 1);
761 input_inject_event(handle, EV_SYN, SYN_REPORT, 1);
762
763 input_inject_event(handle, EV_KEY, KEY_SYSRQ, 0);
764 input_inject_event(handle, EV_KEY, alt_code, 0);
765 input_inject_event(handle, EV_SYN, SYN_REPORT, 1);
Dmitry Torokhov7ab7b5a2011-02-02 22:59:54 -0800766
767 mb();
768 sysrq->reinjecting = false;
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800769 }
770}
771
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800772static bool sysrq_handle_keypress(struct sysrq_state *sysrq,
773 unsigned int code, int value)
774{
775 bool was_active = sysrq->active;
776 bool suppress;
777
778 switch (code) {
779
780 case KEY_LEFTALT:
781 case KEY_RIGHTALT:
782 if (!value) {
783 /* One of ALTs is being released */
784 if (sysrq->active && code == sysrq->alt_use)
785 sysrq->active = false;
786
787 sysrq->alt = KEY_RESERVED;
788
789 } else if (value != 2) {
790 sysrq->alt = code;
791 sysrq->need_reinject = false;
792 }
793 break;
794
795 case KEY_SYSRQ:
796 if (value == 1 && sysrq->alt != KEY_RESERVED) {
797 sysrq->active = true;
798 sysrq->alt_use = sysrq->alt;
799 /*
800 * If nothing else will be pressed we'll need
801 * to re-inject Alt-SysRq keysroke.
802 */
803 sysrq->need_reinject = true;
804 }
805
806 /*
807 * Pretend that sysrq was never pressed at all. This
808 * is needed to properly handle KGDB which will try
809 * to release all keys after exiting debugger. If we
810 * do not clear key bit it KGDB will end up sending
811 * release events for Alt and SysRq, potentially
812 * triggering print screen function.
813 */
814 if (sysrq->active)
815 clear_bit(KEY_SYSRQ, sysrq->handle.dev->key);
816
817 break;
818
819 default:
820 if (sysrq->active && value && value != 2) {
821 sysrq->need_reinject = false;
822 __handle_sysrq(sysrq_xlate[code], true);
823 }
824 break;
825 }
826
827 suppress = sysrq->active;
828
829 if (!sysrq->active) {
830
831 /*
832 * See if reset sequence has changed since the last time.
833 */
834 if (sysrq->reset_seq_version != sysrq_reset_seq_version)
835 sysrq_parse_reset_sequence(sysrq);
836
837 /*
838 * If we are not suppressing key presses keep track of
839 * keyboard state so we can release keys that have been
840 * pressed before entering SysRq mode.
841 */
842 if (value)
843 set_bit(code, sysrq->key_down);
844 else
845 clear_bit(code, sysrq->key_down);
846
847 if (was_active)
848 schedule_work(&sysrq->reinject_work);
849
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700850 /* Check for reset sequence */
851 sysrq_detect_reset_sequence(sysrq, code, value);
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800852
853 } else if (value == 0 && test_and_clear_bit(code, sysrq->key_down)) {
854 /*
855 * Pass on release events for keys that was pressed before
856 * entering SysRq mode.
857 */
858 suppress = false;
859 }
860
861 return suppress;
862}
863
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800864static bool sysrq_filter(struct input_handle *handle,
865 unsigned int type, unsigned int code, int value)
866{
867 struct sysrq_state *sysrq = handle->private;
Dmitry Torokhov1966cb22010-09-29 18:04:21 -0700868 bool suppress;
869
Dmitry Torokhov7ab7b5a2011-02-02 22:59:54 -0800870 /*
871 * Do not filter anything if we are in the process of re-injecting
872 * Alt+SysRq combination.
873 */
874 if (sysrq->reinjecting)
875 return false;
876
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800877 switch (type) {
Dmitry Torokhov1966cb22010-09-29 18:04:21 -0700878
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800879 case EV_SYN:
880 suppress = false;
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700881 break;
882
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800883 case EV_KEY:
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800884 suppress = sysrq_handle_keypress(sysrq, code, value);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700885 break;
886
887 default:
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800888 suppress = sysrq->active;
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700889 break;
890 }
891
Dmitry Torokhov1966cb22010-09-29 18:04:21 -0700892 return suppress;
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700893}
894
895static int sysrq_connect(struct input_handler *handler,
896 struct input_dev *dev,
897 const struct input_device_id *id)
898{
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800899 struct sysrq_state *sysrq;
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700900 int error;
901
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800902 sysrq = kzalloc(sizeof(struct sysrq_state), GFP_KERNEL);
903 if (!sysrq)
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700904 return -ENOMEM;
905
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800906 INIT_WORK(&sysrq->reinject_work, sysrq_reinject_alt_sysrq);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700907
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800908 sysrq->handle.dev = dev;
909 sysrq->handle.handler = handler;
910 sysrq->handle.name = "sysrq";
911 sysrq->handle.private = sysrq;
Kees Cook8c318fa2017-10-16 16:28:51 -0700912 timer_setup(&sysrq->keyreset_timer, sysrq_do_reset, 0);
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800913
914 error = input_register_handle(&sysrq->handle);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700915 if (error) {
916 pr_err("Failed to register input sysrq handler, error %d\n",
917 error);
918 goto err_free;
919 }
920
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800921 error = input_open_device(&sysrq->handle);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700922 if (error) {
923 pr_err("Failed to open input device, error %d\n", error);
924 goto err_unregister;
925 }
926
927 return 0;
928
929 err_unregister:
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800930 input_unregister_handle(&sysrq->handle);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700931 err_free:
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800932 kfree(sysrq);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700933 return error;
934}
935
936static void sysrq_disconnect(struct input_handle *handle)
937{
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800938 struct sysrq_state *sysrq = handle->private;
939
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700940 input_close_device(handle);
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800941 cancel_work_sync(&sysrq->reinject_work);
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700942 del_timer_sync(&sysrq->keyreset_timer);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700943 input_unregister_handle(handle);
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800944 kfree(sysrq);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700945}
946
947/*
Dmitry Torokhov1966cb22010-09-29 18:04:21 -0700948 * We are matching on KEY_LEFTALT instead of KEY_SYSRQ because not all
949 * keyboards have SysRq key predefined and so user may add it to keymap
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700950 * later, but we expect all such keyboards to have left alt.
951 */
952static const struct input_device_id sysrq_ids[] = {
953 {
954 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
955 INPUT_DEVICE_ID_MATCH_KEYBIT,
Akinobu Mita802c0382017-01-06 02:14:16 +0900956 .evbit = { [BIT_WORD(EV_KEY)] = BIT_MASK(EV_KEY) },
957 .keybit = { [BIT_WORD(KEY_LEFTALT)] = BIT_MASK(KEY_LEFTALT) },
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700958 },
959 { },
960};
961
962static struct input_handler sysrq_handler = {
963 .filter = sysrq_filter,
964 .connect = sysrq_connect,
965 .disconnect = sysrq_disconnect,
966 .name = "sysrq",
967 .id_table = sysrq_ids,
968};
969
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700970static inline void sysrq_register_handler(void)
971{
972 int error;
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800973
Mathieu J. Poirier4c076eb2013-08-03 17:22:08 -0700974 sysrq_of_get_keyreset_config();
975
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700976 error = input_register_handler(&sysrq_handler);
977 if (error)
978 pr_err("Failed to register input handler, error %d", error);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700979}
980
981static inline void sysrq_unregister_handler(void)
982{
Dmitry Safonovf06327d2019-12-13 00:06:00 +0000983 input_unregister_handler(&sysrq_handler);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700984}
985
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800986static int sysrq_reset_seq_param_set(const char *buffer,
987 const struct kernel_param *kp)
988{
989 unsigned long val;
990 int error;
991
Jingoo Han86b40562013-06-01 16:30:06 +0900992 error = kstrtoul(buffer, 0, &val);
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800993 if (error < 0)
994 return error;
995
996 if (val > KEY_MAX)
997 return -EINVAL;
998
999 *((unsigned short *)kp->arg) = val;
1000 sysrq_reset_seq_version++;
1001
1002 return 0;
1003}
1004
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09301005static const struct kernel_param_ops param_ops_sysrq_reset_seq = {
Mathieu Poirier154b7a42013-01-06 23:23:33 -08001006 .get = param_get_ushort,
1007 .set = sysrq_reset_seq_param_set,
1008};
1009
1010#define param_check_sysrq_reset_seq(name, p) \
1011 __param_check(name, p, unsigned short)
1012
Paul Gortmaker3bce6f62015-08-19 17:48:06 -04001013/*
1014 * not really modular, but the easiest way to keep compat with existing
1015 * bootargs behaviour is to continue using module_param here.
1016 */
Mathieu Poirier154b7a42013-01-06 23:23:33 -08001017module_param_array_named(reset_seq, sysrq_reset_seq, sysrq_reset_seq,
1018 &sysrq_reset_seq_len, 0644);
1019
Mathieu J. Poirier39030782013-04-01 22:14:19 -07001020module_param_named(sysrq_downtime_ms, sysrq_reset_downtime_ms, int, 0644);
1021
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -07001022#else
1023
1024static inline void sysrq_register_handler(void)
1025{
1026}
1027
1028static inline void sysrq_unregister_handler(void)
1029{
1030}
1031
1032#endif /* CONFIG_INPUT */
1033
1034int sysrq_toggle_support(int enable_mask)
1035{
1036 bool was_enabled = sysrq_on();
1037
1038 sysrq_enabled = enable_mask;
1039
1040 if (was_enabled != sysrq_on()) {
1041 if (sysrq_on())
1042 sysrq_register_handler();
1043 else
1044 sysrq_unregister_handler();
1045 }
1046
1047 return 0;
1048}
1049
Adrian Bunkcf62ddc2005-11-08 21:39:47 -08001050static int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p,
Andrew Mortonbf36b902006-03-25 03:07:08 -08001051 struct sysrq_key_op *remove_op_p)
1052{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Rik van Riel984d74a2014-06-06 14:38:13 -07001055 spin_lock(&sysrq_key_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (__sysrq_get_key_op(key) == remove_op_p) {
1057 __sysrq_put_key_op(key, insert_op_p);
1058 retval = 0;
1059 } else {
1060 retval = -1;
1061 }
Rik van Riel984d74a2014-06-06 14:38:13 -07001062 spin_unlock(&sysrq_key_table_lock);
1063
1064 /*
1065 * A concurrent __handle_sysrq either got the old op or the new op.
1066 * Wait for it to go away before returning, so the code for an old
1067 * op is not freed (eg. on module unload) while it is in use.
1068 */
1069 synchronize_rcu();
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return retval;
1072}
1073
1074int register_sysrq_key(int key, struct sysrq_key_op *op_p)
1075{
1076 return __sysrq_swap_key_ops(key, op_p, NULL);
1077}
Andrew Mortonbf36b902006-03-25 03:07:08 -08001078EXPORT_SYMBOL(register_sysrq_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080int unregister_sysrq_key(int key, struct sysrq_key_op *op_p)
1081{
1082 return __sysrq_swap_key_ops(key, NULL, op_p);
1083}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084EXPORT_SYMBOL(unregister_sysrq_key);
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -07001085
1086#ifdef CONFIG_PROC_FS
1087/*
1088 * writing 'C' to /proc/sysrq-trigger is like sysrq-C
1089 */
1090static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
1091 size_t count, loff_t *ppos)
1092{
1093 if (count) {
1094 char c;
1095
1096 if (get_user(c, buf))
1097 return -EFAULT;
Dmitry Torokhovf3353972010-08-17 21:15:47 -07001098 __handle_sysrq(c, false);
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -07001099 }
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -07001100
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -07001101 return count;
1102}
1103
1104static const struct file_operations proc_sysrq_trigger_operations = {
1105 .write = write_sysrq_trigger,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001106 .llseek = noop_llseek,
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -07001107};
1108
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -07001109static void sysrq_init_procfs(void)
1110{
1111 if (!proc_create("sysrq-trigger", S_IWUSR, NULL,
1112 &proc_sysrq_trigger_operations))
1113 pr_err("Failed to register proc interface\n");
1114}
1115
1116#else
1117
1118static inline void sysrq_init_procfs(void)
1119{
1120}
1121
1122#endif /* CONFIG_PROC_FS */
1123
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -07001124static int __init sysrq_init(void)
1125{
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -07001126 sysrq_init_procfs();
1127
1128 if (sysrq_on())
1129 sysrq_register_handler();
1130
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -07001131 return 0;
1132}
Paul Gortmaker3bce6f62015-08-19 17:48:06 -04001133device_initcall(sysrq_init);