blob: 5e0d0813da5570f78f4ef247fe0977374b6770bc [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
Dmitry Safonoveaee4172020-03-02 17:51:34 +000066/**
67 * sysrq_mask - Getter for sysrq_enabled mask.
68 *
69 * Return: 1 if sysrq is always enabled, enabled sysrq_key_op mask otherwise.
70 */
71int sysrq_mask(void)
72{
73 if (sysrq_always_enabled)
74 return 1;
75 return sysrq_enabled;
76}
77
Ingo Molnar5d6f6472006-12-13 00:34:36 -080078/*
79 * A value of 1 means 'all', other nonzero values are an op mask:
80 */
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070081static bool sysrq_on_mask(int mask)
Ingo Molnar5d6f6472006-12-13 00:34:36 -080082{
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070083 return sysrq_always_enabled ||
84 sysrq_enabled == 1 ||
85 (sysrq_enabled & mask);
Ingo Molnar5d6f6472006-12-13 00:34:36 -080086}
87
88static int __init sysrq_always_enabled_setup(char *str)
89{
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070090 sysrq_always_enabled = true;
91 pr_info("sysrq always enabled.\n");
Ingo Molnar5d6f6472006-12-13 00:34:36 -080092
93 return 1;
94}
95
96__setup("sysrq_always_enabled", sysrq_always_enabled_setup);
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Dmitry Torokhov1495cc92010-08-17 21:15:46 -070099static void sysrq_handle_loglevel(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 int i;
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 i = key - '0';
Borislav Petkova8fe19e2014-06-04 16:11:46 -0700104 console_loglevel = CONSOLE_LOGLEVEL_DEFAULT;
Michal Hocko401e4a72015-02-11 15:26:21 -0800105 pr_info("Loglevel set to %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 console_loglevel = i;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800107}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static struct sysrq_key_op sysrq_loglevel_op = {
109 .handler = sysrq_handle_loglevel,
Randy Dunlap208b95c2009-01-06 14:41:13 -0800110 .help_msg = "loglevel(0-9)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 .action_msg = "Changing Loglevel",
112 .enable_mask = SYSRQ_ENABLE_LOG,
113};
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#ifdef CONFIG_VT
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700116static void sysrq_handle_SAK(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Eric W. Biederman8b6312f2007-02-10 01:44:34 -0800118 struct work_struct *SAK_work = &vc_cons[fg_console].SAK_work;
Eric W. Biederman8b6312f2007-02-10 01:44:34 -0800119 schedule_work(SAK_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121static struct sysrq_key_op sysrq_SAK_op = {
122 .handler = sysrq_handle_SAK,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800123 .help_msg = "sak(k)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 .action_msg = "SAK",
125 .enable_mask = SYSRQ_ENABLE_KEYBOARD,
126};
Andrew Mortonbf36b902006-03-25 03:07:08 -0800127#else
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700128#define sysrq_SAK_op (*(struct sysrq_key_op *)NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#endif
130
131#ifdef CONFIG_VT
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700132static void sysrq_handle_unraw(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Alan Cox079c9532012-02-28 14:49:23 +0000134 vt_reset_unicode(fg_console);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
Alan Cox079c9532012-02-28 14:49:23 +0000136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137static struct sysrq_key_op sysrq_unraw_op = {
138 .handler = sysrq_handle_unraw,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800139 .help_msg = "unraw(r)",
Bill Nottingham2e8ecb92007-10-16 23:29:38 -0700140 .action_msg = "Keyboard mode set to system default",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 .enable_mask = SYSRQ_ENABLE_KEYBOARD,
142};
Andrew Mortonbf36b902006-03-25 03:07:08 -0800143#else
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700144#define sysrq_unraw_op (*(struct sysrq_key_op *)NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#endif /* CONFIG_VT */
146
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700147static void sysrq_handle_crash(int key)
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -0700148{
Matthias Kaehlcke8341f2f2018-09-20 10:12:53 -0700149 /* release the RCU read lock before crashing */
Ani Sinha984cf352015-12-17 17:15:10 -0800150 rcu_read_unlock();
Matthias Kaehlcke8341f2f2018-09-20 10:12:53 -0700151
152 panic("sysrq triggered crash\n");
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -0700153}
Hidetoshi Setocab8bd32009-07-29 15:04:14 -0700154static struct sysrq_key_op sysrq_crash_op = {
Neil Hormand6580a92009-06-17 16:28:17 -0700155 .handler = sysrq_handle_crash,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800156 .help_msg = "crash(c)",
Neil Hormand6580a92009-06-17 16:28:17 -0700157 .action_msg = "Trigger a crash",
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -0700158 .enable_mask = SYSRQ_ENABLE_DUMP,
159};
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -0700160
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700161static void sysrq_handle_reboot(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Peter Zijlstrab2e9c7d02006-09-30 23:28:02 -0700163 lockdep_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 local_irq_enable();
Eric W. Biederman4de8b9b2005-07-26 11:51:06 -0600165 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static struct sysrq_key_op sysrq_reboot_op = {
168 .handler = sysrq_handle_reboot,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800169 .help_msg = "reboot(b)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 .action_msg = "Resetting",
171 .enable_mask = SYSRQ_ENABLE_BOOT,
172};
173
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700174static void sysrq_handle_sync(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 emergency_sync();
177}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178static struct sysrq_key_op sysrq_sync_op = {
179 .handler = sysrq_handle_sync,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800180 .help_msg = "sync(s)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 .action_msg = "Emergency Sync",
182 .enable_mask = SYSRQ_ENABLE_SYNC,
183};
184
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700185static void sysrq_handle_show_timers(int key)
Ingo Molnar88ad0bf62007-02-16 01:28:16 -0800186{
187 sysrq_timer_list_show();
188}
189
190static struct sysrq_key_op sysrq_show_timers_op = {
191 .handler = sysrq_handle_show_timers,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800192 .help_msg = "show-all-timers(q)",
Thomas Gleixner322acf62008-10-20 12:33:14 +0200193 .action_msg = "Show clockevent devices & pending hrtimers (no others)",
Ingo Molnar88ad0bf62007-02-16 01:28:16 -0800194};
195
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700196static void sysrq_handle_mountro(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 emergency_remount();
199}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200static struct sysrq_key_op sysrq_mountro_op = {
201 .handler = sysrq_handle_mountro,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800202 .help_msg = "unmount(u)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 .action_msg = "Emergency Remount R/O",
204 .enable_mask = SYSRQ_ENABLE_REMOUNT,
205};
206
Ingo Molnar8c645802006-07-03 00:24:56 -0700207#ifdef CONFIG_LOCKDEP
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700208static void sysrq_handle_showlocks(int key)
Ingo Molnarde5097c2006-01-09 15:59:21 -0800209{
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700210 debug_show_all_locks();
Ingo Molnarde5097c2006-01-09 15:59:21 -0800211}
Ingo Molnar8c645802006-07-03 00:24:56 -0700212
Ingo Molnarde5097c2006-01-09 15:59:21 -0800213static struct sysrq_key_op sysrq_showlocks_op = {
214 .handler = sysrq_handle_showlocks,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800215 .help_msg = "show-all-locks(d)",
Ingo Molnarde5097c2006-01-09 15:59:21 -0800216 .action_msg = "Show Locks Held",
217};
Andrew Mortonbf36b902006-03-25 03:07:08 -0800218#else
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700219#define sysrq_showlocks_op (*(struct sysrq_key_op *)NULL)
Ingo Molnarde5097c2006-01-09 15:59:21 -0800220#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Rik van Riel5045bca2008-04-29 00:59:21 -0700222#ifdef CONFIG_SMP
Julien Grall6ac972d2019-03-13 11:40:34 +0000223static DEFINE_RAW_SPINLOCK(show_lock);
Rik van Riel5045bca2008-04-29 00:59:21 -0700224
225static void showacpu(void *dummy)
226{
227 unsigned long flags;
228
229 /* Idle CPUs have no interesting backtrace. */
230 if (idle_cpu(smp_processor_id()))
231 return;
232
Julien Grall6ac972d2019-03-13 11:40:34 +0000233 raw_spin_lock_irqsave(&show_lock, flags);
Michal Hocko401e4a72015-02-11 15:26:21 -0800234 pr_info("CPU%d:\n", smp_processor_id());
Rik van Riel5045bca2008-04-29 00:59:21 -0700235 show_stack(NULL, NULL);
Julien Grall6ac972d2019-03-13 11:40:34 +0000236 raw_spin_unlock_irqrestore(&show_lock, flags);
Rik van Riel5045bca2008-04-29 00:59:21 -0700237}
238
239static void sysrq_showregs_othercpus(struct work_struct *dummy)
240{
Ingo Molnar8b604d52008-06-27 11:52:45 +0200241 smp_call_function(showacpu, NULL, 0);
Rik van Riel5045bca2008-04-29 00:59:21 -0700242}
243
244static DECLARE_WORK(sysrq_showallcpus, sysrq_showregs_othercpus);
245
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700246static void sysrq_handle_showallcpus(int key)
Rik van Riel5045bca2008-04-29 00:59:21 -0700247{
Ingo Molnar47cab6a2009-08-03 09:31:54 +0200248 /*
249 * Fall back to the workqueue based printing if the
250 * backtrace printing did not succeed or the
251 * architecture has no support for it:
252 */
253 if (!trigger_all_cpu_backtrace()) {
Jibin Xub00bebb2017-09-10 20:11:42 -0700254 struct pt_regs *regs = NULL;
Ingo Molnar47cab6a2009-08-03 09:31:54 +0200255
Jibin Xub00bebb2017-09-10 20:11:42 -0700256 if (in_irq())
257 regs = get_irq_regs();
Ingo Molnar47cab6a2009-08-03 09:31:54 +0200258 if (regs) {
Michal Hocko401e4a72015-02-11 15:26:21 -0800259 pr_info("CPU%d:\n", smp_processor_id());
Ingo Molnar47cab6a2009-08-03 09:31:54 +0200260 show_regs(regs);
261 }
262 schedule_work(&sysrq_showallcpus);
263 }
Rik van Riel5045bca2008-04-29 00:59:21 -0700264}
265
266static struct sysrq_key_op sysrq_showallcpus_op = {
267 .handler = sysrq_handle_showallcpus,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800268 .help_msg = "show-backtrace-all-active-cpus(l)",
Rik van Riel5045bca2008-04-29 00:59:21 -0700269 .action_msg = "Show backtrace of all active CPUs",
270 .enable_mask = SYSRQ_ENABLE_DUMP,
271};
272#endif
273
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700274static void sysrq_handle_showregs(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Jibin Xub00bebb2017-09-10 20:11:42 -0700276 struct pt_regs *regs = NULL;
277
278 if (in_irq())
279 regs = get_irq_regs();
David Howells7d12e782006-10-05 14:55:46 +0100280 if (regs)
281 show_regs(regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200282 perf_event_print_debug();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284static struct sysrq_key_op sysrq_showregs_op = {
285 .handler = sysrq_handle_showregs,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800286 .help_msg = "show-registers(p)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 .action_msg = "Show Regs",
288 .enable_mask = SYSRQ_ENABLE_DUMP,
289};
290
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700291static void sysrq_handle_showstate(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 show_state();
Tejun Heo3494fc32015-03-09 09:22:28 -0400294 show_workqueue_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296static struct sysrq_key_op sysrq_showstate_op = {
297 .handler = sysrq_handle_showstate,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800298 .help_msg = "show-task-states(t)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 .action_msg = "Show State",
300 .enable_mask = SYSRQ_ENABLE_DUMP,
301};
302
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700303static void sysrq_handle_showstate_blocked(int key)
Ingo Molnare59e2ae2006-12-06 20:35:59 -0800304{
305 show_state_filter(TASK_UNINTERRUPTIBLE);
306}
307static struct sysrq_key_op sysrq_showstate_blocked_op = {
308 .handler = sysrq_handle_showstate_blocked,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800309 .help_msg = "show-blocked-tasks(w)",
Ingo Molnare59e2ae2006-12-06 20:35:59 -0800310 .action_msg = "Show Blocked State",
311 .enable_mask = SYSRQ_ENABLE_DUMP,
312};
313
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100314#ifdef CONFIG_TRACING
315#include <linux/ftrace.h>
316
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700317static void sysrq_ftrace_dump(int key)
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100318{
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200319 ftrace_dump(DUMP_ALL);
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100320}
321static struct sysrq_key_op sysrq_ftrace_dump_op = {
322 .handler = sysrq_ftrace_dump,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800323 .help_msg = "dump-ftrace-buffer(z)",
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100324 .action_msg = "Dump ftrace buffer",
325 .enable_mask = SYSRQ_ENABLE_DUMP,
326};
327#else
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700328#define sysrq_ftrace_dump_op (*(struct sysrq_key_op *)NULL)
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100329#endif
Ingo Molnare59e2ae2006-12-06 20:35:59 -0800330
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700331static void sysrq_handle_showmem(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Michal Hocko9af744d2017-02-22 15:46:16 -0800333 show_mem(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335static struct sysrq_key_op sysrq_showmem_op = {
336 .handler = sysrq_handle_showmem,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800337 .help_msg = "show-memory-usage(m)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 .action_msg = "Show Memory",
339 .enable_mask = SYSRQ_ENABLE_DUMP,
340};
341
Andrew Mortonbf36b902006-03-25 03:07:08 -0800342/*
343 * Signal sysrq helper function. Sends a signal to all user processes.
344 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345static void send_sig_all(int sig)
346{
347 struct task_struct *p;
348
Anton Vorontsove502bab2012-02-07 10:49:39 +0400349 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 for_each_process(p) {
Anton Vorontsovd3a532a2012-02-07 10:49:51 +0400351 if (p->flags & PF_KTHREAD)
352 continue;
353 if (is_global_init(p))
354 continue;
355
Eric W. Biedermanb16503b2018-07-21 11:35:55 -0500356 do_send_sig_info(sig, SEND_SIG_PRIV, p, PIDTYPE_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
Anton Vorontsove502bab2012-02-07 10:49:39 +0400358 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700361static void sysrq_handle_term(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 send_sig_all(SIGTERM);
Borislav Petkova8fe19e2014-06-04 16:11:46 -0700364 console_loglevel = CONSOLE_LOGLEVEL_DEBUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366static struct sysrq_key_op sysrq_term_op = {
367 .handler = sysrq_handle_term,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800368 .help_msg = "terminate-all-tasks(e)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 .action_msg = "Terminate All Tasks",
370 .enable_mask = SYSRQ_ENABLE_SIGNAL,
371};
372
David Howells65f27f32006-11-22 14:55:48 +0000373static void moom_callback(struct work_struct *ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
David Rientjes6e0fc462015-09-08 15:00:36 -0700375 const gfp_t gfp_mask = GFP_KERNEL;
376 struct oom_control oc = {
377 .zonelist = node_zonelist(first_memory_node, gfp_mask),
378 .nodemask = NULL,
Vladimir Davydov2a966b72016-07-26 15:22:33 -0700379 .memcg = NULL,
David Rientjes6e0fc462015-09-08 15:00:36 -0700380 .gfp_mask = gfp_mask,
David Rientjes54e9e292015-09-08 15:00:39 -0700381 .order = -1,
David Rientjes6e0fc462015-09-08 15:00:36 -0700382 };
383
Johannes Weinerdc564012015-06-24 16:57:19 -0700384 mutex_lock(&oom_lock);
David Rientjes6e0fc462015-09-08 15:00:36 -0700385 if (!out_of_memory(&oc))
Michal Hockod75da002017-05-03 14:54:57 -0700386 pr_info("OOM request ignored. No task eligible\n");
Johannes Weinerdc564012015-06-24 16:57:19 -0700387 mutex_unlock(&oom_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
David Howells65f27f32006-11-22 14:55:48 +0000390static DECLARE_WORK(moom_work, moom_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700392static void sysrq_handle_moom(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 schedule_work(&moom_work);
395}
396static struct sysrq_key_op sysrq_moom_op = {
397 .handler = sysrq_handle_moom,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800398 .help_msg = "memory-full-oom-kill(f)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 .action_msg = "Manual OOM execution",
Naohiro Ooiwab4236f82008-10-15 22:01:43 -0700400 .enable_mask = SYSRQ_ENABLE_SIGNAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401};
402
Eric Sandeenc2d75432009-03-31 15:23:46 -0700403#ifdef CONFIG_BLOCK
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700404static void sysrq_handle_thaw(int key)
Eric Sandeenc2d75432009-03-31 15:23:46 -0700405{
406 emergency_thaw_all();
407}
408static struct sysrq_key_op sysrq_thaw_op = {
409 .handler = sysrq_handle_thaw,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800410 .help_msg = "thaw-filesystems(j)",
Eric Sandeenc2d75432009-03-31 15:23:46 -0700411 .action_msg = "Emergency Thaw of all frozen filesystems",
412 .enable_mask = SYSRQ_ENABLE_SIGNAL,
413};
414#endif
415
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700416static void sysrq_handle_kill(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 send_sig_all(SIGKILL);
Borislav Petkova8fe19e2014-06-04 16:11:46 -0700419 console_loglevel = CONSOLE_LOGLEVEL_DEBUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421static struct sysrq_key_op sysrq_kill_op = {
422 .handler = sysrq_handle_kill,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800423 .help_msg = "kill-all-tasks(i)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 .action_msg = "Kill All Tasks",
425 .enable_mask = SYSRQ_ENABLE_SIGNAL,
426};
427
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700428static void sysrq_handle_unrt(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
430 normalize_rt_tasks();
431}
432static struct sysrq_key_op sysrq_unrt_op = {
433 .handler = sysrq_handle_unrt,
zhangwei(Jovi)afa80cc2013-03-07 17:00:02 +0800434 .help_msg = "nice-all-RT-tasks(n)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 .action_msg = "Nice All RT Tasks",
436 .enable_mask = SYSRQ_ENABLE_RTNICE,
437};
438
439/* Key Operations table and lock */
440static DEFINE_SPINLOCK(sysrq_key_table_lock);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800441
442static struct sysrq_key_op *sysrq_key_table[36] = {
443 &sysrq_loglevel_op, /* 0 */
444 &sysrq_loglevel_op, /* 1 */
445 &sysrq_loglevel_op, /* 2 */
446 &sysrq_loglevel_op, /* 3 */
447 &sysrq_loglevel_op, /* 4 */
448 &sysrq_loglevel_op, /* 5 */
449 &sysrq_loglevel_op, /* 6 */
450 &sysrq_loglevel_op, /* 7 */
451 &sysrq_loglevel_op, /* 8 */
452 &sysrq_loglevel_op, /* 9 */
453
454 /*
Randy Dunlapd346cce2007-01-31 23:48:17 -0800455 * a: Don't use for system provided sysrqs, it is handled specially on
456 * sparc and will never arrive.
Andrew Mortonbf36b902006-03-25 03:07:08 -0800457 */
458 NULL, /* a */
459 &sysrq_reboot_op, /* b */
Eric Biggers5e351412017-04-03 22:50:20 -0700460 &sysrq_crash_op, /* c */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800461 &sysrq_showlocks_op, /* d */
462 &sysrq_term_op, /* e */
463 &sysrq_moom_op, /* f */
Jason Wessel364b5b72009-05-13 21:56:59 -0500464 /* g: May be registered for the kernel debugger */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800465 NULL, /* g */
Eric Sandeenc2d75432009-03-31 15:23:46 -0700466 NULL, /* h - reserved for help */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800467 &sysrq_kill_op, /* i */
Eric Sandeenc2d75432009-03-31 15:23:46 -0700468#ifdef CONFIG_BLOCK
469 &sysrq_thaw_op, /* j */
470#else
Andrew Mortonbf36b902006-03-25 03:07:08 -0800471 NULL, /* j */
Eric Sandeenc2d75432009-03-31 15:23:46 -0700472#endif
Andrew Mortonbf36b902006-03-25 03:07:08 -0800473 &sysrq_SAK_op, /* k */
Rik van Riel5045bca2008-04-29 00:59:21 -0700474#ifdef CONFIG_SMP
475 &sysrq_showallcpus_op, /* l */
476#else
Andrew Mortonbf36b902006-03-25 03:07:08 -0800477 NULL, /* l */
Rik van Riel5045bca2008-04-29 00:59:21 -0700478#endif
Andrew Mortonbf36b902006-03-25 03:07:08 -0800479 &sysrq_showmem_op, /* m */
480 &sysrq_unrt_op, /* n */
Randy Dunlapd346cce2007-01-31 23:48:17 -0800481 /* o: This will often be registered as 'Off' at init time */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800482 NULL, /* o */
483 &sysrq_showregs_op, /* p */
Ingo Molnar88ad0bf62007-02-16 01:28:16 -0800484 &sysrq_show_timers_op, /* q */
Randy Dunlapd346cce2007-01-31 23:48:17 -0800485 &sysrq_unraw_op, /* r */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800486 &sysrq_sync_op, /* s */
487 &sysrq_showstate_op, /* t */
488 &sysrq_mountro_op, /* u */
Jason Wessel364b5b72009-05-13 21:56:59 -0500489 /* v: May be registered for frame buffer console restore */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800490 NULL, /* v */
Randy Dunlapd346cce2007-01-31 23:48:17 -0800491 &sysrq_showstate_blocked_op, /* w */
James Hogand1e9a4f2015-05-19 09:50:29 +0100492 /* x: May be registered on mips for TLB dump */
Randy Dunlapd346cce2007-01-31 23:48:17 -0800493 /* x: May be registered on ppc/powerpc for xmon */
David S. Miller916ca142012-10-16 09:34:01 -0700494 /* x: May be registered on sparc64 for global PMU dump */
Randy Dunlapd346cce2007-01-31 23:48:17 -0800495 NULL, /* x */
David S. Miller93dae5b2008-05-19 23:46:00 -0700496 /* y: May be registered on sparc64 for global register dump */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800497 NULL, /* y */
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100498 &sysrq_ftrace_dump_op, /* z */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499};
500
501/* key2index calculation, -1 on invalid index */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800502static int sysrq_key_table_key2index(int key)
503{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 int retval;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800505
506 if ((key >= '0') && (key <= '9'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 retval = key - '0';
Andrew Mortonbf36b902006-03-25 03:07:08 -0800508 else if ((key >= 'a') && (key <= 'z'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 retval = key + 10 - 'a';
Andrew Mortonbf36b902006-03-25 03:07:08 -0800510 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 retval = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return retval;
513}
514
515/*
516 * get and put functions for the table, exposed to modules.
517 */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800518struct sysrq_key_op *__sysrq_get_key_op(int key)
519{
520 struct sysrq_key_op *op_p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 int i;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 i = sysrq_key_table_key2index(key);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800524 if (i != -1)
525 op_p = sysrq_key_table[i];
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return op_p;
528}
529
Andrew Mortonbf36b902006-03-25 03:07:08 -0800530static void __sysrq_put_key_op(int key, struct sysrq_key_op *op_p)
531{
532 int i = sysrq_key_table_key2index(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (i != -1)
535 sysrq_key_table[i] = op_p;
536}
537
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700538void __handle_sysrq(int key, bool check_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 struct sysrq_key_op *op_p;
541 int orig_log_level;
Feng Tangc39ea0b2019-05-14 15:45:34 -0700542 int orig_suppress_printk;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800543 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Feng Tangc39ea0b2019-05-14 15:45:34 -0700545 orig_suppress_printk = suppress_printk;
546 suppress_printk = 0;
547
Rik van Riel722773a2014-06-06 14:38:14 -0700548 rcu_sysrq_start();
Rik van Riel984d74a2014-06-06 14:38:13 -0700549 rcu_read_lock();
Andy Whitcroftfb144ad2009-01-15 13:50:52 -0800550 /*
551 * Raise the apparent loglevel to maximum so that the sysrq header
552 * is shown to provide the user with positive feedback. We do not
553 * simply emit this at KERN_EMERG as that would change message
554 * routing in the consumers of /proc/kmsg.
555 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 orig_log_level = console_loglevel;
Borislav Petkova8fe19e2014-06-04 16:11:46 -0700557 console_loglevel = CONSOLE_LOGLEVEL_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 op_p = __sysrq_get_key_op(key);
560 if (op_p) {
Andrew Mortonbf36b902006-03-25 03:07:08 -0800561 /*
562 * Should we check for enabled operations (/proc/sysrq-trigger
563 * should not) and is the invoked operation enabled?
564 */
Ingo Molnar5d6f6472006-12-13 00:34:36 -0800565 if (!check_mask || sysrq_on_mask(op_p->enable_mask)) {
Petr Mladekc3fee602019-01-11 17:20:37 +0100566 pr_info("%s\n", op_p->action_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 console_loglevel = orig_log_level;
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700568 op_p->handler(key);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800569 } else {
Petr Mladekc3fee602019-01-11 17:20:37 +0100570 pr_info("This sysrq operation is disabled.\n");
Petr Mladek075e1a02019-01-11 13:45:15 +0100571 console_loglevel = orig_log_level;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 } else {
Petr Mladekc3fee602019-01-11 17:20:37 +0100574 pr_info("HELP : ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 /* Only print the help msg once per handler */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800576 for (i = 0; i < ARRAY_SIZE(sysrq_key_table); i++) {
577 if (sysrq_key_table[i]) {
578 int j;
579
580 for (j = 0; sysrq_key_table[i] !=
581 sysrq_key_table[j]; j++)
582 ;
583 if (j != i)
584 continue;
Michal Hocko401e4a72015-02-11 15:26:21 -0800585 pr_cont("%s ", sysrq_key_table[i]->help_msg);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
Michal Hocko401e4a72015-02-11 15:26:21 -0800588 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 console_loglevel = orig_log_level;
590 }
Rik van Riel984d74a2014-06-06 14:38:13 -0700591 rcu_read_unlock();
Rik van Riel722773a2014-06-06 14:38:14 -0700592 rcu_sysrq_end();
Feng Tangc39ea0b2019-05-14 15:45:34 -0700593
594 suppress_printk = orig_suppress_printk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700597void handle_sysrq(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Ingo Molnar5d6f6472006-12-13 00:34:36 -0800599 if (sysrq_on())
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700600 __handle_sysrq(key, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
Andrew Mortonbf36b902006-03-25 03:07:08 -0800602EXPORT_SYMBOL(handle_sysrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700604#ifdef CONFIG_INPUT
Arnd Bergmannffb6e0c2015-05-26 14:45:29 -0700605static int sysrq_reset_downtime_ms;
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700606
607/* Simple translation table for the SysRq keys */
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800608static const unsigned char sysrq_xlate[KEY_CNT] =
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700609 "\000\0331234567890-=\177\t" /* 0x00 - 0x0f */
610 "qwertyuiop[]\r\000as" /* 0x10 - 0x1f */
611 "dfghjkl;'`\000\\zxcv" /* 0x20 - 0x2f */
612 "bnm,./\000*\000 \000\201\202\203\204\205" /* 0x30 - 0x3f */
613 "\206\207\210\211\212\000\000789-456+1" /* 0x40 - 0x4f */
614 "230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
615 "\r\000/"; /* 0x60 - 0x6f */
616
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800617struct sysrq_state {
618 struct input_handle handle;
619 struct work_struct reinject_work;
620 unsigned long key_down[BITS_TO_LONGS(KEY_CNT)];
621 unsigned int alt;
622 unsigned int alt_use;
623 bool active;
624 bool need_reinject;
Dmitry Torokhov7ab7b5a2011-02-02 22:59:54 -0800625 bool reinjecting;
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800626
627 /* reset sequence handling */
628 bool reset_canceled;
Mathieu J. Poirier3d289512013-06-05 22:51:46 -0700629 bool reset_requested;
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800630 unsigned long reset_keybit[BITS_TO_LONGS(KEY_CNT)];
631 int reset_seq_len;
632 int reset_seq_cnt;
633 int reset_seq_version;
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700634 struct timer_list keyreset_timer;
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800635};
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700636
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800637#define SYSRQ_KEY_RESET_MAX 20 /* Should be plenty */
638static unsigned short sysrq_reset_seq[SYSRQ_KEY_RESET_MAX];
639static unsigned int sysrq_reset_seq_len;
640static unsigned int sysrq_reset_seq_version = 1;
641
642static void sysrq_parse_reset_sequence(struct sysrq_state *state)
643{
644 int i;
645 unsigned short key;
646
647 state->reset_seq_cnt = 0;
648
649 for (i = 0; i < sysrq_reset_seq_len; i++) {
650 key = sysrq_reset_seq[i];
651
652 if (key == KEY_RESERVED || key > KEY_MAX)
653 break;
654
655 __set_bit(key, state->reset_keybit);
656 state->reset_seq_len++;
657
658 if (test_bit(key, state->key_down))
659 state->reset_seq_cnt++;
660 }
661
662 /* Disable reset until old keys are not released */
663 state->reset_canceled = state->reset_seq_cnt != 0;
664
665 state->reset_seq_version = sysrq_reset_seq_version;
666}
667
Kees Cook8c318fa2017-10-16 16:28:51 -0700668static void sysrq_do_reset(struct timer_list *t)
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700669{
Kees Cook8c318fa2017-10-16 16:28:51 -0700670 struct sysrq_state *state = from_timer(state, t, keyreset_timer);
Mathieu J. Poirier3d289512013-06-05 22:51:46 -0700671
672 state->reset_requested = true;
673
Mark Tomlinson8fefbc62018-11-27 09:57:23 +1300674 orderly_reboot();
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700675}
676
677static void sysrq_handle_reset_request(struct sysrq_state *state)
678{
Mathieu J. Poirier3d289512013-06-05 22:51:46 -0700679 if (state->reset_requested)
680 __handle_sysrq(sysrq_xlate[KEY_B], false);
681
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700682 if (sysrq_reset_downtime_ms)
683 mod_timer(&state->keyreset_timer,
684 jiffies + msecs_to_jiffies(sysrq_reset_downtime_ms));
685 else
Kees Cook8c318fa2017-10-16 16:28:51 -0700686 sysrq_do_reset(&state->keyreset_timer);
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700687}
688
689static void sysrq_detect_reset_sequence(struct sysrq_state *state,
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800690 unsigned int code, int value)
691{
692 if (!test_bit(code, state->reset_keybit)) {
693 /*
694 * Pressing any key _not_ in reset sequence cancels
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700695 * the reset sequence. Also cancelling the timer in
696 * case additional keys were pressed after a reset
697 * has been requested.
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800698 */
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700699 if (value && state->reset_seq_cnt) {
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800700 state->reset_canceled = true;
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700701 del_timer(&state->keyreset_timer);
702 }
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800703 } else if (value == 0) {
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700704 /*
705 * Key release - all keys in the reset sequence need
706 * to be pressed and held for the reset timeout
707 * to hold.
708 */
709 del_timer(&state->keyreset_timer);
710
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800711 if (--state->reset_seq_cnt == 0)
712 state->reset_canceled = false;
713 } else if (value == 1) {
714 /* key press, not autorepeat */
715 if (++state->reset_seq_cnt == state->reset_seq_len &&
716 !state->reset_canceled) {
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700717 sysrq_handle_reset_request(state);
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800718 }
719 }
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800720}
721
Mathieu J. Poirier4c076eb2013-08-03 17:22:08 -0700722#ifdef CONFIG_OF
723static void sysrq_of_get_keyreset_config(void)
724{
725 u32 key;
726 struct device_node *np;
727 struct property *prop;
728 const __be32 *p;
729
730 np = of_find_node_by_path("/chosen/linux,sysrq-reset-seq");
731 if (!np) {
732 pr_debug("No sysrq node found");
733 return;
734 }
735
736 /* Reset in case a __weak definition was present */
737 sysrq_reset_seq_len = 0;
738
739 of_property_for_each_u32(np, "keyset", prop, p, key) {
740 if (key == KEY_RESERVED || key > KEY_MAX ||
741 sysrq_reset_seq_len == SYSRQ_KEY_RESET_MAX)
742 break;
743
744 sysrq_reset_seq[sysrq_reset_seq_len++] = (unsigned short)key;
745 }
746
747 /* Get reset timeout if any. */
748 of_property_read_u32(np, "timeout-ms", &sysrq_reset_downtime_ms);
Yangtao Li279070b2018-11-21 08:12:14 -0500749
750 of_node_put(np);
Mathieu J. Poirier4c076eb2013-08-03 17:22:08 -0700751}
752#else
753static void sysrq_of_get_keyreset_config(void)
754{
755}
756#endif
757
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800758static void sysrq_reinject_alt_sysrq(struct work_struct *work)
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700759{
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800760 struct sysrq_state *sysrq =
761 container_of(work, struct sysrq_state, reinject_work);
762 struct input_handle *handle = &sysrq->handle;
763 unsigned int alt_code = sysrq->alt_use;
764
765 if (sysrq->need_reinject) {
Dmitry Torokhov7ab7b5a2011-02-02 22:59:54 -0800766 /* we do not want the assignment to be reordered */
767 sysrq->reinjecting = true;
768 mb();
769
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800770 /* Simulate press and release of Alt + SysRq */
771 input_inject_event(handle, EV_KEY, alt_code, 1);
772 input_inject_event(handle, EV_KEY, KEY_SYSRQ, 1);
773 input_inject_event(handle, EV_SYN, SYN_REPORT, 1);
774
775 input_inject_event(handle, EV_KEY, KEY_SYSRQ, 0);
776 input_inject_event(handle, EV_KEY, alt_code, 0);
777 input_inject_event(handle, EV_SYN, SYN_REPORT, 1);
Dmitry Torokhov7ab7b5a2011-02-02 22:59:54 -0800778
779 mb();
780 sysrq->reinjecting = false;
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800781 }
782}
783
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800784static bool sysrq_handle_keypress(struct sysrq_state *sysrq,
785 unsigned int code, int value)
786{
787 bool was_active = sysrq->active;
788 bool suppress;
789
790 switch (code) {
791
792 case KEY_LEFTALT:
793 case KEY_RIGHTALT:
794 if (!value) {
795 /* One of ALTs is being released */
796 if (sysrq->active && code == sysrq->alt_use)
797 sysrq->active = false;
798
799 sysrq->alt = KEY_RESERVED;
800
801 } else if (value != 2) {
802 sysrq->alt = code;
803 sysrq->need_reinject = false;
804 }
805 break;
806
807 case KEY_SYSRQ:
808 if (value == 1 && sysrq->alt != KEY_RESERVED) {
809 sysrq->active = true;
810 sysrq->alt_use = sysrq->alt;
811 /*
812 * If nothing else will be pressed we'll need
813 * to re-inject Alt-SysRq keysroke.
814 */
815 sysrq->need_reinject = true;
816 }
817
818 /*
819 * Pretend that sysrq was never pressed at all. This
820 * is needed to properly handle KGDB which will try
821 * to release all keys after exiting debugger. If we
822 * do not clear key bit it KGDB will end up sending
823 * release events for Alt and SysRq, potentially
824 * triggering print screen function.
825 */
826 if (sysrq->active)
827 clear_bit(KEY_SYSRQ, sysrq->handle.dev->key);
828
829 break;
830
831 default:
832 if (sysrq->active && value && value != 2) {
833 sysrq->need_reinject = false;
834 __handle_sysrq(sysrq_xlate[code], true);
835 }
836 break;
837 }
838
839 suppress = sysrq->active;
840
841 if (!sysrq->active) {
842
843 /*
844 * See if reset sequence has changed since the last time.
845 */
846 if (sysrq->reset_seq_version != sysrq_reset_seq_version)
847 sysrq_parse_reset_sequence(sysrq);
848
849 /*
850 * If we are not suppressing key presses keep track of
851 * keyboard state so we can release keys that have been
852 * pressed before entering SysRq mode.
853 */
854 if (value)
855 set_bit(code, sysrq->key_down);
856 else
857 clear_bit(code, sysrq->key_down);
858
859 if (was_active)
860 schedule_work(&sysrq->reinject_work);
861
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700862 /* Check for reset sequence */
863 sysrq_detect_reset_sequence(sysrq, code, value);
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800864
865 } else if (value == 0 && test_and_clear_bit(code, sysrq->key_down)) {
866 /*
867 * Pass on release events for keys that was pressed before
868 * entering SysRq mode.
869 */
870 suppress = false;
871 }
872
873 return suppress;
874}
875
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800876static bool sysrq_filter(struct input_handle *handle,
877 unsigned int type, unsigned int code, int value)
878{
879 struct sysrq_state *sysrq = handle->private;
Dmitry Torokhov1966cb22010-09-29 18:04:21 -0700880 bool suppress;
881
Dmitry Torokhov7ab7b5a2011-02-02 22:59:54 -0800882 /*
883 * Do not filter anything if we are in the process of re-injecting
884 * Alt+SysRq combination.
885 */
886 if (sysrq->reinjecting)
887 return false;
888
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800889 switch (type) {
Dmitry Torokhov1966cb22010-09-29 18:04:21 -0700890
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800891 case EV_SYN:
892 suppress = false;
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700893 break;
894
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800895 case EV_KEY:
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800896 suppress = sysrq_handle_keypress(sysrq, code, value);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700897 break;
898
899 default:
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800900 suppress = sysrq->active;
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700901 break;
902 }
903
Dmitry Torokhov1966cb22010-09-29 18:04:21 -0700904 return suppress;
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700905}
906
907static int sysrq_connect(struct input_handler *handler,
908 struct input_dev *dev,
909 const struct input_device_id *id)
910{
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800911 struct sysrq_state *sysrq;
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700912 int error;
913
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800914 sysrq = kzalloc(sizeof(struct sysrq_state), GFP_KERNEL);
915 if (!sysrq)
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700916 return -ENOMEM;
917
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800918 INIT_WORK(&sysrq->reinject_work, sysrq_reinject_alt_sysrq);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700919
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800920 sysrq->handle.dev = dev;
921 sysrq->handle.handler = handler;
922 sysrq->handle.name = "sysrq";
923 sysrq->handle.private = sysrq;
Kees Cook8c318fa2017-10-16 16:28:51 -0700924 timer_setup(&sysrq->keyreset_timer, sysrq_do_reset, 0);
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800925
926 error = input_register_handle(&sysrq->handle);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700927 if (error) {
928 pr_err("Failed to register input sysrq handler, error %d\n",
929 error);
930 goto err_free;
931 }
932
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800933 error = input_open_device(&sysrq->handle);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700934 if (error) {
935 pr_err("Failed to open input device, error %d\n", error);
936 goto err_unregister;
937 }
938
939 return 0;
940
941 err_unregister:
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800942 input_unregister_handle(&sysrq->handle);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700943 err_free:
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800944 kfree(sysrq);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700945 return error;
946}
947
948static void sysrq_disconnect(struct input_handle *handle)
949{
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800950 struct sysrq_state *sysrq = handle->private;
951
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700952 input_close_device(handle);
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800953 cancel_work_sync(&sysrq->reinject_work);
Mathieu J. Poirier39030782013-04-01 22:14:19 -0700954 del_timer_sync(&sysrq->keyreset_timer);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700955 input_unregister_handle(handle);
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800956 kfree(sysrq);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700957}
958
959/*
Dmitry Torokhov1966cb22010-09-29 18:04:21 -0700960 * We are matching on KEY_LEFTALT instead of KEY_SYSRQ because not all
961 * keyboards have SysRq key predefined and so user may add it to keymap
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700962 * later, but we expect all such keyboards to have left alt.
963 */
964static const struct input_device_id sysrq_ids[] = {
965 {
966 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
967 INPUT_DEVICE_ID_MATCH_KEYBIT,
Akinobu Mita802c0382017-01-06 02:14:16 +0900968 .evbit = { [BIT_WORD(EV_KEY)] = BIT_MASK(EV_KEY) },
969 .keybit = { [BIT_WORD(KEY_LEFTALT)] = BIT_MASK(KEY_LEFTALT) },
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700970 },
971 { },
972};
973
974static struct input_handler sysrq_handler = {
975 .filter = sysrq_filter,
976 .connect = sysrq_connect,
977 .disconnect = sysrq_disconnect,
978 .name = "sysrq",
979 .id_table = sysrq_ids,
980};
981
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700982static inline void sysrq_register_handler(void)
983{
984 int error;
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800985
Mathieu J. Poirier4c076eb2013-08-03 17:22:08 -0700986 sysrq_of_get_keyreset_config();
987
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700988 error = input_register_handler(&sysrq_handler);
989 if (error)
990 pr_err("Failed to register input handler, error %d", error);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700991}
992
993static inline void sysrq_unregister_handler(void)
994{
Dmitry Safonovf06327d2019-12-13 00:06:00 +0000995 input_unregister_handler(&sysrq_handler);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700996}
997
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800998static int sysrq_reset_seq_param_set(const char *buffer,
999 const struct kernel_param *kp)
1000{
1001 unsigned long val;
1002 int error;
1003
Jingoo Han86b40562013-06-01 16:30:06 +09001004 error = kstrtoul(buffer, 0, &val);
Mathieu Poirier154b7a42013-01-06 23:23:33 -08001005 if (error < 0)
1006 return error;
1007
1008 if (val > KEY_MAX)
1009 return -EINVAL;
1010
1011 *((unsigned short *)kp->arg) = val;
1012 sysrq_reset_seq_version++;
1013
1014 return 0;
1015}
1016
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09301017static const struct kernel_param_ops param_ops_sysrq_reset_seq = {
Mathieu Poirier154b7a42013-01-06 23:23:33 -08001018 .get = param_get_ushort,
1019 .set = sysrq_reset_seq_param_set,
1020};
1021
1022#define param_check_sysrq_reset_seq(name, p) \
1023 __param_check(name, p, unsigned short)
1024
Paul Gortmaker3bce6f62015-08-19 17:48:06 -04001025/*
1026 * not really modular, but the easiest way to keep compat with existing
1027 * bootargs behaviour is to continue using module_param here.
1028 */
Mathieu Poirier154b7a42013-01-06 23:23:33 -08001029module_param_array_named(reset_seq, sysrq_reset_seq, sysrq_reset_seq,
1030 &sysrq_reset_seq_len, 0644);
1031
Mathieu J. Poirier39030782013-04-01 22:14:19 -07001032module_param_named(sysrq_downtime_ms, sysrq_reset_downtime_ms, int, 0644);
1033
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -07001034#else
1035
1036static inline void sysrq_register_handler(void)
1037{
1038}
1039
1040static inline void sysrq_unregister_handler(void)
1041{
1042}
1043
1044#endif /* CONFIG_INPUT */
1045
1046int sysrq_toggle_support(int enable_mask)
1047{
1048 bool was_enabled = sysrq_on();
1049
1050 sysrq_enabled = enable_mask;
1051
1052 if (was_enabled != sysrq_on()) {
1053 if (sysrq_on())
1054 sysrq_register_handler();
1055 else
1056 sysrq_unregister_handler();
1057 }
1058
1059 return 0;
1060}
1061
Adrian Bunkcf62ddc2005-11-08 21:39:47 -08001062static int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p,
Andrew Mortonbf36b902006-03-25 03:07:08 -08001063 struct sysrq_key_op *remove_op_p)
1064{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Rik van Riel984d74a2014-06-06 14:38:13 -07001067 spin_lock(&sysrq_key_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (__sysrq_get_key_op(key) == remove_op_p) {
1069 __sysrq_put_key_op(key, insert_op_p);
1070 retval = 0;
1071 } else {
1072 retval = -1;
1073 }
Rik van Riel984d74a2014-06-06 14:38:13 -07001074 spin_unlock(&sysrq_key_table_lock);
1075
1076 /*
1077 * A concurrent __handle_sysrq either got the old op or the new op.
1078 * Wait for it to go away before returning, so the code for an old
1079 * op is not freed (eg. on module unload) while it is in use.
1080 */
1081 synchronize_rcu();
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return retval;
1084}
1085
1086int register_sysrq_key(int key, struct sysrq_key_op *op_p)
1087{
1088 return __sysrq_swap_key_ops(key, op_p, NULL);
1089}
Andrew Mortonbf36b902006-03-25 03:07:08 -08001090EXPORT_SYMBOL(register_sysrq_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092int unregister_sysrq_key(int key, struct sysrq_key_op *op_p)
1093{
1094 return __sysrq_swap_key_ops(key, NULL, op_p);
1095}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096EXPORT_SYMBOL(unregister_sysrq_key);
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -07001097
1098#ifdef CONFIG_PROC_FS
1099/*
1100 * writing 'C' to /proc/sysrq-trigger is like sysrq-C
1101 */
1102static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
1103 size_t count, loff_t *ppos)
1104{
1105 if (count) {
1106 char c;
1107
1108 if (get_user(c, buf))
1109 return -EFAULT;
Dmitry Torokhovf3353972010-08-17 21:15:47 -07001110 __handle_sysrq(c, false);
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -07001111 }
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -07001112
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -07001113 return count;
1114}
1115
Alexey Dobriyan97a32532020-02-03 17:37:17 -08001116static const struct proc_ops sysrq_trigger_proc_ops = {
1117 .proc_write = write_sysrq_trigger,
1118 .proc_lseek = noop_llseek,
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -07001119};
1120
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -07001121static void sysrq_init_procfs(void)
1122{
1123 if (!proc_create("sysrq-trigger", S_IWUSR, NULL,
Alexey Dobriyan97a32532020-02-03 17:37:17 -08001124 &sysrq_trigger_proc_ops))
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -07001125 pr_err("Failed to register proc interface\n");
1126}
1127
1128#else
1129
1130static inline void sysrq_init_procfs(void)
1131{
1132}
1133
1134#endif /* CONFIG_PROC_FS */
1135
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -07001136static int __init sysrq_init(void)
1137{
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -07001138 sysrq_init_procfs();
1139
1140 if (sysrq_on())
1141 sysrq_register_handler();
1142
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -07001143 return 0;
1144}
Paul Gortmaker3bce6f62015-08-19 17:48:06 -04001145device_initcall(sysrq_init);