1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
296fd7ce5SGreg Kroah-Hartman /*
396fd7ce5SGreg Kroah-Hartman * Linux Magic System Request Key Hacks
496fd7ce5SGreg Kroah-Hartman *
596fd7ce5SGreg Kroah-Hartman * (c) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
696fd7ce5SGreg Kroah-Hartman * based on ideas by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>
796fd7ce5SGreg Kroah-Hartman *
896fd7ce5SGreg Kroah-Hartman * (c) 2000 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
996fd7ce5SGreg Kroah-Hartman * overhauled to use key registration
1096fd7ce5SGreg Kroah-Hartman * based upon discusions in irc://irc.openprojects.net/#kernelnewbies
1196fd7ce5SGreg Kroah-Hartman *
1296fd7ce5SGreg Kroah-Hartman * Copyright (c) 2010 Dmitry Torokhov
1396fd7ce5SGreg Kroah-Hartman * Input handler conversion
1496fd7ce5SGreg Kroah-Hartman */
1596fd7ce5SGreg Kroah-Hartman
1696fd7ce5SGreg Kroah-Hartman #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1796fd7ce5SGreg Kroah-Hartman
183f07c014SIngo Molnar #include <linux/sched/signal.h>
198bd75c77SClark Williams #include <linux/sched/rt.h>
20b17b0153SIngo Molnar #include <linux/sched/debug.h>
2129930025SIngo Molnar #include <linux/sched/task.h>
22a27eb0cbSAndrzej Pietrasiewicz #include <linux/ctype.h>
2396fd7ce5SGreg Kroah-Hartman #include <linux/interrupt.h>
2496fd7ce5SGreg Kroah-Hartman #include <linux/mm.h>
2596fd7ce5SGreg Kroah-Hartman #include <linux/fs.h>
2696fd7ce5SGreg Kroah-Hartman #include <linux/mount.h>
2796fd7ce5SGreg Kroah-Hartman #include <linux/kdev_t.h>
2896fd7ce5SGreg Kroah-Hartman #include <linux/major.h>
2996fd7ce5SGreg Kroah-Hartman #include <linux/reboot.h>
3096fd7ce5SGreg Kroah-Hartman #include <linux/sysrq.h>
3196fd7ce5SGreg Kroah-Hartman #include <linux/kbd_kern.h>
3296fd7ce5SGreg Kroah-Hartman #include <linux/proc_fs.h>
3396fd7ce5SGreg Kroah-Hartman #include <linux/nmi.h>
3496fd7ce5SGreg Kroah-Hartman #include <linux/quotaops.h>
3596fd7ce5SGreg Kroah-Hartman #include <linux/perf_event.h>
3696fd7ce5SGreg Kroah-Hartman #include <linux/kernel.h>
3796fd7ce5SGreg Kroah-Hartman #include <linux/module.h>
3896fd7ce5SGreg Kroah-Hartman #include <linux/suspend.h>
3996fd7ce5SGreg Kroah-Hartman #include <linux/writeback.h>
4096fd7ce5SGreg Kroah-Hartman #include <linux/swap.h>
4196fd7ce5SGreg Kroah-Hartman #include <linux/spinlock.h>
4296fd7ce5SGreg Kroah-Hartman #include <linux/vt_kern.h>
4396fd7ce5SGreg Kroah-Hartman #include <linux/workqueue.h>
4496fd7ce5SGreg Kroah-Hartman #include <linux/hrtimer.h>
4596fd7ce5SGreg Kroah-Hartman #include <linux/oom.h>
4696fd7ce5SGreg Kroah-Hartman #include <linux/slab.h>
4796fd7ce5SGreg Kroah-Hartman #include <linux/input.h>
48ff01bb48SAl Viro #include <linux/uaccess.h>
49154b7a48SMathieu Poirier #include <linux/moduleparam.h>
5039030786SMathieu J. Poirier #include <linux/jiffies.h>
513d289517SMathieu J. Poirier #include <linux/syscalls.h>
524c076eb0SMathieu J. Poirier #include <linux/of.h>
53722773afSRik van Riel #include <linux/rcupdate.h>
5496fd7ce5SGreg Kroah-Hartman
5596fd7ce5SGreg Kroah-Hartman #include <asm/ptrace.h>
5696fd7ce5SGreg Kroah-Hartman #include <asm/irq_regs.h>
5796fd7ce5SGreg Kroah-Hartman
5896fd7ce5SGreg Kroah-Hartman /* Whether we react on sysrq keys or just ignore them */
598eaede49SBen Hutchings static int __read_mostly sysrq_enabled = CONFIG_MAGIC_SYSRQ_DEFAULT_ENABLE;
6096fd7ce5SGreg Kroah-Hartman static bool __read_mostly sysrq_always_enabled;
6196fd7ce5SGreg Kroah-Hartman
sysrq_on(void)6296fd7ce5SGreg Kroah-Hartman static bool sysrq_on(void)
6396fd7ce5SGreg Kroah-Hartman {
6496fd7ce5SGreg Kroah-Hartman return sysrq_enabled || sysrq_always_enabled;
6596fd7ce5SGreg Kroah-Hartman }
6696fd7ce5SGreg Kroah-Hartman
67eaee4172SDmitry Safonov /**
68eaee4172SDmitry Safonov * sysrq_mask - Getter for sysrq_enabled mask.
69eaee4172SDmitry Safonov *
70eaee4172SDmitry Safonov * Return: 1 if sysrq is always enabled, enabled sysrq_key_op mask otherwise.
71eaee4172SDmitry Safonov */
sysrq_mask(void)72eaee4172SDmitry Safonov int sysrq_mask(void)
73eaee4172SDmitry Safonov {
74eaee4172SDmitry Safonov if (sysrq_always_enabled)
75eaee4172SDmitry Safonov return 1;
76eaee4172SDmitry Safonov return sysrq_enabled;
77eaee4172SDmitry Safonov }
7866bb1c95SDmitry Safonov EXPORT_SYMBOL_GPL(sysrq_mask);
79eaee4172SDmitry Safonov
8096fd7ce5SGreg Kroah-Hartman /*
8196fd7ce5SGreg Kroah-Hartman * A value of 1 means 'all', other nonzero values are an op mask:
8296fd7ce5SGreg Kroah-Hartman */
sysrq_on_mask(int mask)8396fd7ce5SGreg Kroah-Hartman static bool sysrq_on_mask(int mask)
8496fd7ce5SGreg Kroah-Hartman {
8596fd7ce5SGreg Kroah-Hartman return sysrq_always_enabled ||
8696fd7ce5SGreg Kroah-Hartman sysrq_enabled == 1 ||
8796fd7ce5SGreg Kroah-Hartman (sysrq_enabled & mask);
8896fd7ce5SGreg Kroah-Hartman }
8996fd7ce5SGreg Kroah-Hartman
sysrq_always_enabled_setup(char * str)9096fd7ce5SGreg Kroah-Hartman static int __init sysrq_always_enabled_setup(char *str)
9196fd7ce5SGreg Kroah-Hartman {
9296fd7ce5SGreg Kroah-Hartman sysrq_always_enabled = true;
9396fd7ce5SGreg Kroah-Hartman pr_info("sysrq always enabled.\n");
9496fd7ce5SGreg Kroah-Hartman
9596fd7ce5SGreg Kroah-Hartman return 1;
9696fd7ce5SGreg Kroah-Hartman }
9796fd7ce5SGreg Kroah-Hartman
9896fd7ce5SGreg Kroah-Hartman __setup("sysrq_always_enabled", sysrq_always_enabled_setup);
9996fd7ce5SGreg Kroah-Hartman
10096fd7ce5SGreg Kroah-Hartman
sysrq_handle_loglevel(u8 key)101bcb48185SJiri Slaby static void sysrq_handle_loglevel(u8 key)
10296fd7ce5SGreg Kroah-Hartman {
10300ef7effSJiri Slaby u8 loglevel = key - '0';
10496fd7ce5SGreg Kroah-Hartman
105a8fe19ebSBorislav Petkov console_loglevel = CONSOLE_LOGLEVEL_DEFAULT;
10600ef7effSJiri Slaby pr_info("Loglevel set to %u\n", loglevel);
10700ef7effSJiri Slaby console_loglevel = loglevel;
10896fd7ce5SGreg Kroah-Hartman }
1097fffe31dSEmil Velikov static const struct sysrq_key_op sysrq_loglevel_op = {
11096fd7ce5SGreg Kroah-Hartman .handler = sysrq_handle_loglevel,
11196fd7ce5SGreg Kroah-Hartman .help_msg = "loglevel(0-9)",
11296fd7ce5SGreg Kroah-Hartman .action_msg = "Changing Loglevel",
11396fd7ce5SGreg Kroah-Hartman .enable_mask = SYSRQ_ENABLE_LOG,
11496fd7ce5SGreg Kroah-Hartman };
11596fd7ce5SGreg Kroah-Hartman
11696fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_VT
sysrq_handle_SAK(u8 key)117bcb48185SJiri Slaby static void sysrq_handle_SAK(u8 key)
11896fd7ce5SGreg Kroah-Hartman {
11996fd7ce5SGreg Kroah-Hartman struct work_struct *SAK_work = &vc_cons[fg_console].SAK_work;
1202c4a4cdeSXiaofei Tan
12196fd7ce5SGreg Kroah-Hartman schedule_work(SAK_work);
12296fd7ce5SGreg Kroah-Hartman }
1237fffe31dSEmil Velikov static const struct sysrq_key_op sysrq_SAK_op = {
12496fd7ce5SGreg Kroah-Hartman .handler = sysrq_handle_SAK,
125afa80ccbSzhangwei(Jovi) .help_msg = "sak(k)",
12696fd7ce5SGreg Kroah-Hartman .action_msg = "SAK",
12796fd7ce5SGreg Kroah-Hartman .enable_mask = SYSRQ_ENABLE_KEYBOARD,
12896fd7ce5SGreg Kroah-Hartman };
12996fd7ce5SGreg Kroah-Hartman #else
1307fffe31dSEmil Velikov #define sysrq_SAK_op (*(const struct sysrq_key_op *)NULL)
13196fd7ce5SGreg Kroah-Hartman #endif
13296fd7ce5SGreg Kroah-Hartman
13396fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_VT
sysrq_handle_unraw(u8 key)134bcb48185SJiri Slaby static void sysrq_handle_unraw(u8 key)
13596fd7ce5SGreg Kroah-Hartman {
136079c9534SAlan Cox vt_reset_unicode(fg_console);
13796fd7ce5SGreg Kroah-Hartman }
138079c9534SAlan Cox
1397fffe31dSEmil Velikov static const struct sysrq_key_op sysrq_unraw_op = {
14096fd7ce5SGreg Kroah-Hartman .handler = sysrq_handle_unraw,
141afa80ccbSzhangwei(Jovi) .help_msg = "unraw(r)",
14296fd7ce5SGreg Kroah-Hartman .action_msg = "Keyboard mode set to system default",
14396fd7ce5SGreg Kroah-Hartman .enable_mask = SYSRQ_ENABLE_KEYBOARD,
14496fd7ce5SGreg Kroah-Hartman };
14596fd7ce5SGreg Kroah-Hartman #else
1467fffe31dSEmil Velikov #define sysrq_unraw_op (*(const struct sysrq_key_op *)NULL)
14796fd7ce5SGreg Kroah-Hartman #endif /* CONFIG_VT */
14896fd7ce5SGreg Kroah-Hartman
sysrq_handle_crash(u8 key)149bcb48185SJiri Slaby static void sysrq_handle_crash(u8 key)
15096fd7ce5SGreg Kroah-Hartman {
1518341f2f2SMatthias Kaehlcke /* release the RCU read lock before crashing */
152984cf355SAni Sinha rcu_read_unlock();
1538341f2f2SMatthias Kaehlcke
1548341f2f2SMatthias Kaehlcke panic("sysrq triggered crash\n");
15596fd7ce5SGreg Kroah-Hartman }
1567fffe31dSEmil Velikov static const struct sysrq_key_op sysrq_crash_op = {
15796fd7ce5SGreg Kroah-Hartman .handler = sysrq_handle_crash,
158afa80ccbSzhangwei(Jovi) .help_msg = "crash(c)",
15996fd7ce5SGreg Kroah-Hartman .action_msg = "Trigger a crash",
16096fd7ce5SGreg Kroah-Hartman .enable_mask = SYSRQ_ENABLE_DUMP,
16196fd7ce5SGreg Kroah-Hartman };
16296fd7ce5SGreg Kroah-Hartman
sysrq_handle_reboot(u8 key)163bcb48185SJiri Slaby static void sysrq_handle_reboot(u8 key)
16496fd7ce5SGreg Kroah-Hartman {
16596fd7ce5SGreg Kroah-Hartman lockdep_off();
16696fd7ce5SGreg Kroah-Hartman local_irq_enable();
16796fd7ce5SGreg Kroah-Hartman emergency_restart();
16896fd7ce5SGreg Kroah-Hartman }
1697fffe31dSEmil Velikov static const struct sysrq_key_op sysrq_reboot_op = {
17096fd7ce5SGreg Kroah-Hartman .handler = sysrq_handle_reboot,
171afa80ccbSzhangwei(Jovi) .help_msg = "reboot(b)",
17296fd7ce5SGreg Kroah-Hartman .action_msg = "Resetting",
17396fd7ce5SGreg Kroah-Hartman .enable_mask = SYSRQ_ENABLE_BOOT,
17496fd7ce5SGreg Kroah-Hartman };
17596fd7ce5SGreg Kroah-Hartman
1767fffe31dSEmil Velikov const struct sysrq_key_op *__sysrq_reboot_op = &sysrq_reboot_op;
1770f1c9688SEmil Velikov
sysrq_handle_sync(u8 key)178bcb48185SJiri Slaby static void sysrq_handle_sync(u8 key)
17996fd7ce5SGreg Kroah-Hartman {
18096fd7ce5SGreg Kroah-Hartman emergency_sync();
18196fd7ce5SGreg Kroah-Hartman }
1827fffe31dSEmil Velikov static const struct sysrq_key_op sysrq_sync_op = {
18396fd7ce5SGreg Kroah-Hartman .handler = sysrq_handle_sync,
184afa80ccbSzhangwei(Jovi) .help_msg = "sync(s)",
18596fd7ce5SGreg Kroah-Hartman .action_msg = "Emergency Sync",
18696fd7ce5SGreg Kroah-Hartman .enable_mask = SYSRQ_ENABLE_SYNC,
18796fd7ce5SGreg Kroah-Hartman };
18896fd7ce5SGreg Kroah-Hartman
sysrq_handle_show_timers(u8 key)189bcb48185SJiri Slaby static void sysrq_handle_show_timers(u8 key)
19096fd7ce5SGreg Kroah-Hartman {
19196fd7ce5SGreg Kroah-Hartman sysrq_timer_list_show();
19296fd7ce5SGreg Kroah-Hartman }
19396fd7ce5SGreg Kroah-Hartman
1947fffe31dSEmil Velikov static const struct sysrq_key_op sysrq_show_timers_op = {
19596fd7ce5SGreg Kroah-Hartman .handler = sysrq_handle_show_timers,
196afa80ccbSzhangwei(Jovi) .help_msg = "show-all-timers(q)",
19796fd7ce5SGreg Kroah-Hartman .action_msg = "Show clockevent devices & pending hrtimers (no others)",
19896fd7ce5SGreg Kroah-Hartman };
19996fd7ce5SGreg Kroah-Hartman
sysrq_handle_mountro(u8 key)200bcb48185SJiri Slaby static void sysrq_handle_mountro(u8 key)
20196fd7ce5SGreg Kroah-Hartman {
20296fd7ce5SGreg Kroah-Hartman emergency_remount();
20396fd7ce5SGreg Kroah-Hartman }
2047fffe31dSEmil Velikov static const struct sysrq_key_op sysrq_mountro_op = {
20596fd7ce5SGreg Kroah-Hartman .handler = sysrq_handle_mountro,
206afa80ccbSzhangwei(Jovi) .help_msg = "unmount(u)",
20796fd7ce5SGreg Kroah-Hartman .action_msg = "Emergency Remount R/O",
20896fd7ce5SGreg Kroah-Hartman .enable_mask = SYSRQ_ENABLE_REMOUNT,
20996fd7ce5SGreg Kroah-Hartman };
21096fd7ce5SGreg Kroah-Hartman
21196fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_LOCKDEP
sysrq_handle_showlocks(u8 key)212bcb48185SJiri Slaby static void sysrq_handle_showlocks(u8 key)
21396fd7ce5SGreg Kroah-Hartman {
21496fd7ce5SGreg Kroah-Hartman debug_show_all_locks();
21596fd7ce5SGreg Kroah-Hartman }
21696fd7ce5SGreg Kroah-Hartman
2177fffe31dSEmil Velikov static const struct sysrq_key_op sysrq_showlocks_op = {
21896fd7ce5SGreg Kroah-Hartman .handler = sysrq_handle_showlocks,
219afa80ccbSzhangwei(Jovi) .help_msg = "show-all-locks(d)",
22096fd7ce5SGreg Kroah-Hartman .action_msg = "Show Locks Held",
22196fd7ce5SGreg Kroah-Hartman };
22296fd7ce5SGreg Kroah-Hartman #else
2237fffe31dSEmil Velikov #define sysrq_showlocks_op (*(const struct sysrq_key_op *)NULL)
22496fd7ce5SGreg Kroah-Hartman #endif
22596fd7ce5SGreg Kroah-Hartman
22696fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_SMP
2276ac972ddSJulien Grall static DEFINE_RAW_SPINLOCK(show_lock);
22896fd7ce5SGreg Kroah-Hartman
showacpu(void * dummy)22996fd7ce5SGreg Kroah-Hartman static void showacpu(void *dummy)
23096fd7ce5SGreg Kroah-Hartman {
23196fd7ce5SGreg Kroah-Hartman unsigned long flags;
23296fd7ce5SGreg Kroah-Hartman
23396fd7ce5SGreg Kroah-Hartman /* Idle CPUs have no interesting backtrace. */
2345390e7f4SChangbin Du if (idle_cpu(smp_processor_id())) {
2355390e7f4SChangbin Du pr_info("CPU%d: backtrace skipped as idling\n", smp_processor_id());
23696fd7ce5SGreg Kroah-Hartman return;
2375390e7f4SChangbin Du }
23896fd7ce5SGreg Kroah-Hartman
2396ac972ddSJulien Grall raw_spin_lock_irqsave(&show_lock, flags);
240401e4a7cSMichal Hocko pr_info("CPU%d:\n", smp_processor_id());
2419cb8f069SDmitry Safonov show_stack(NULL, NULL, KERN_INFO);
2426ac972ddSJulien Grall raw_spin_unlock_irqrestore(&show_lock, flags);
24396fd7ce5SGreg Kroah-Hartman }
24496fd7ce5SGreg Kroah-Hartman
sysrq_showregs_othercpus(struct work_struct * dummy)24596fd7ce5SGreg Kroah-Hartman static void sysrq_showregs_othercpus(struct work_struct *dummy)
24696fd7ce5SGreg Kroah-Hartman {
24796fd7ce5SGreg Kroah-Hartman smp_call_function(showacpu, NULL, 0);
24896fd7ce5SGreg Kroah-Hartman }
24996fd7ce5SGreg Kroah-Hartman
25096fd7ce5SGreg Kroah-Hartman static DECLARE_WORK(sysrq_showallcpus, sysrq_showregs_othercpus);
25196fd7ce5SGreg Kroah-Hartman
sysrq_handle_showallcpus(u8 key)252bcb48185SJiri Slaby static void sysrq_handle_showallcpus(u8 key)
25396fd7ce5SGreg Kroah-Hartman {
25496fd7ce5SGreg Kroah-Hartman /*
25596fd7ce5SGreg Kroah-Hartman * Fall back to the workqueue based printing if the
25696fd7ce5SGreg Kroah-Hartman * backtrace printing did not succeed or the
25796fd7ce5SGreg Kroah-Hartman * architecture has no support for it:
25896fd7ce5SGreg Kroah-Hartman */
25996fd7ce5SGreg Kroah-Hartman if (!trigger_all_cpu_backtrace()) {
260b00bebbcSJibin Xu struct pt_regs *regs = NULL;
26196fd7ce5SGreg Kroah-Hartman
2621143637fSChangbin Du if (in_hardirq())
263b00bebbcSJibin Xu regs = get_irq_regs();
2645390e7f4SChangbin Du
265aa267cfcSMuhammad Usama Anjum pr_info("CPU%d:\n", get_cpu());
2665390e7f4SChangbin Du if (regs)
26796fd7ce5SGreg Kroah-Hartman show_regs(regs);
2685390e7f4SChangbin Du else
2695390e7f4SChangbin Du show_stack(NULL, NULL, KERN_INFO);
2705390e7f4SChangbin Du
27196fd7ce5SGreg Kroah-Hartman schedule_work(&sysrq_showallcpus);
272aa267cfcSMuhammad Usama Anjum put_cpu();
27396fd7ce5SGreg Kroah-Hartman }
27496fd7ce5SGreg Kroah-Hartman }
27596fd7ce5SGreg Kroah-Hartman
2767fffe31dSEmil Velikov static const struct sysrq_key_op sysrq_showallcpus_op = {
27796fd7ce5SGreg Kroah-Hartman .handler = sysrq_handle_showallcpus,
278afa80ccbSzhangwei(Jovi) .help_msg = "show-backtrace-all-active-cpus(l)",
27996fd7ce5SGreg Kroah-Hartman .action_msg = "Show backtrace of all active CPUs",
28096fd7ce5SGreg Kroah-Hartman .enable_mask = SYSRQ_ENABLE_DUMP,
28196fd7ce5SGreg Kroah-Hartman };
2828ec8719fSJunwen Wu #else
2838ec8719fSJunwen Wu #define sysrq_showallcpus_op (*(const struct sysrq_key_op *)NULL)
28496fd7ce5SGreg Kroah-Hartman #endif
28596fd7ce5SGreg Kroah-Hartman
sysrq_handle_showregs(u8 key)286bcb48185SJiri Slaby static void sysrq_handle_showregs(u8 key)
28796fd7ce5SGreg Kroah-Hartman {
288b00bebbcSJibin Xu struct pt_regs *regs = NULL;
289b00bebbcSJibin Xu
2901143637fSChangbin Du if (in_hardirq())
291b00bebbcSJibin Xu regs = get_irq_regs();
29296fd7ce5SGreg Kroah-Hartman if (regs)
29396fd7ce5SGreg Kroah-Hartman show_regs(regs);
29496fd7ce5SGreg Kroah-Hartman perf_event_print_debug();
29596fd7ce5SGreg Kroah-Hartman }
2967fffe31dSEmil Velikov static const struct sysrq_key_op sysrq_showregs_op = {
29796fd7ce5SGreg Kroah-Hartman .handler = sysrq_handle_showregs,
298afa80ccbSzhangwei(Jovi) .help_msg = "show-registers(p)",
29996fd7ce5SGreg Kroah-Hartman .action_msg = "Show Regs",
30096fd7ce5SGreg Kroah-Hartman .enable_mask = SYSRQ_ENABLE_DUMP,
30196fd7ce5SGreg Kroah-Hartman };
30296fd7ce5SGreg Kroah-Hartman
sysrq_handle_showstate(u8 key)303bcb48185SJiri Slaby static void sysrq_handle_showstate(u8 key)
30496fd7ce5SGreg Kroah-Hartman {
30596fd7ce5SGreg Kroah-Hartman show_state();
30655df0933SImran Khan show_all_workqueues();
30796fd7ce5SGreg Kroah-Hartman }
3087fffe31dSEmil Velikov static const struct sysrq_key_op sysrq_showstate_op = {
30996fd7ce5SGreg Kroah-Hartman .handler = sysrq_handle_showstate,
310afa80ccbSzhangwei(Jovi) .help_msg = "show-task-states(t)",
31196fd7ce5SGreg Kroah-Hartman .action_msg = "Show State",
31296fd7ce5SGreg Kroah-Hartman .enable_mask = SYSRQ_ENABLE_DUMP,
31396fd7ce5SGreg Kroah-Hartman };
31496fd7ce5SGreg Kroah-Hartman
sysrq_handle_showstate_blocked(u8 key)315bcb48185SJiri Slaby static void sysrq_handle_showstate_blocked(u8 key)
31696fd7ce5SGreg Kroah-Hartman {
31796fd7ce5SGreg Kroah-Hartman show_state_filter(TASK_UNINTERRUPTIBLE);
31896fd7ce5SGreg Kroah-Hartman }
3197fffe31dSEmil Velikov static const struct sysrq_key_op sysrq_showstate_blocked_op = {
32096fd7ce5SGreg Kroah-Hartman .handler = sysrq_handle_showstate_blocked,
321afa80ccbSzhangwei(Jovi) .help_msg = "show-blocked-tasks(w)",
32296fd7ce5SGreg Kroah-Hartman .action_msg = "Show Blocked State",
32396fd7ce5SGreg Kroah-Hartman .enable_mask = SYSRQ_ENABLE_DUMP,
32496fd7ce5SGreg Kroah-Hartman };
32596fd7ce5SGreg Kroah-Hartman
32696fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_TRACING
32796fd7ce5SGreg Kroah-Hartman #include <linux/ftrace.h>
32896fd7ce5SGreg Kroah-Hartman
sysrq_ftrace_dump(u8 key)329bcb48185SJiri Slaby static void sysrq_ftrace_dump(u8 key)
33096fd7ce5SGreg Kroah-Hartman {
33196fd7ce5SGreg Kroah-Hartman ftrace_dump(DUMP_ALL);
33296fd7ce5SGreg Kroah-Hartman }
3337fffe31dSEmil Velikov static const struct sysrq_key_op sysrq_ftrace_dump_op = {
33496fd7ce5SGreg Kroah-Hartman .handler = sysrq_ftrace_dump,
335afa80ccbSzhangwei(Jovi) .help_msg = "dump-ftrace-buffer(z)",
33696fd7ce5SGreg Kroah-Hartman .action_msg = "Dump ftrace buffer",
33796fd7ce5SGreg Kroah-Hartman .enable_mask = SYSRQ_ENABLE_DUMP,
33896fd7ce5SGreg Kroah-Hartman };
33996fd7ce5SGreg Kroah-Hartman #else
3407fffe31dSEmil Velikov #define sysrq_ftrace_dump_op (*(const struct sysrq_key_op *)NULL)
34196fd7ce5SGreg Kroah-Hartman #endif
34296fd7ce5SGreg Kroah-Hartman
sysrq_handle_showmem(u8 key)343bcb48185SJiri Slaby static void sysrq_handle_showmem(u8 key)
34496fd7ce5SGreg Kroah-Hartman {
345527ed4f7SKefeng Wang show_mem();
34696fd7ce5SGreg Kroah-Hartman }
3477fffe31dSEmil Velikov static const struct sysrq_key_op sysrq_showmem_op = {
34896fd7ce5SGreg Kroah-Hartman .handler = sysrq_handle_showmem,
349afa80ccbSzhangwei(Jovi) .help_msg = "show-memory-usage(m)",
35096fd7ce5SGreg Kroah-Hartman .action_msg = "Show Memory",
35196fd7ce5SGreg Kroah-Hartman .enable_mask = SYSRQ_ENABLE_DUMP,
35296fd7ce5SGreg Kroah-Hartman };
35396fd7ce5SGreg Kroah-Hartman
35496fd7ce5SGreg Kroah-Hartman /*
35596fd7ce5SGreg Kroah-Hartman * Signal sysrq helper function. Sends a signal to all user processes.
35696fd7ce5SGreg Kroah-Hartman */
send_sig_all(int sig)35796fd7ce5SGreg Kroah-Hartman static void send_sig_all(int sig)
35896fd7ce5SGreg Kroah-Hartman {
35996fd7ce5SGreg Kroah-Hartman struct task_struct *p;
36096fd7ce5SGreg Kroah-Hartman
361e502babeSAnton Vorontsov read_lock(&tasklist_lock);
36296fd7ce5SGreg Kroah-Hartman for_each_process(p) {
363d3a532a9SAnton Vorontsov if (p->flags & PF_KTHREAD)
364d3a532a9SAnton Vorontsov continue;
365d3a532a9SAnton Vorontsov if (is_global_init(p))
366d3a532a9SAnton Vorontsov continue;
367d3a532a9SAnton Vorontsov
368b16503baSEric W. Biederman do_send_sig_info(sig, SEND_SIG_PRIV, p, PIDTYPE_MAX);
36996fd7ce5SGreg Kroah-Hartman }
370e502babeSAnton Vorontsov read_unlock(&tasklist_lock);
37196fd7ce5SGreg Kroah-Hartman }
37296fd7ce5SGreg Kroah-Hartman
sysrq_handle_term(u8 key)373bcb48185SJiri Slaby static void sysrq_handle_term(u8 key)
37496fd7ce5SGreg Kroah-Hartman {
37596fd7ce5SGreg Kroah-Hartman send_sig_all(SIGTERM);
376a8fe19ebSBorislav Petkov console_loglevel = CONSOLE_LOGLEVEL_DEBUG;
37796fd7ce5SGreg Kroah-Hartman }
3787fffe31dSEmil Velikov static const struct sysrq_key_op sysrq_term_op = {
37996fd7ce5SGreg Kroah-Hartman .handler = sysrq_handle_term,
380afa80ccbSzhangwei(Jovi) .help_msg = "terminate-all-tasks(e)",
38196fd7ce5SGreg Kroah-Hartman .action_msg = "Terminate All Tasks",
38296fd7ce5SGreg Kroah-Hartman .enable_mask = SYSRQ_ENABLE_SIGNAL,
38396fd7ce5SGreg Kroah-Hartman };
38496fd7ce5SGreg Kroah-Hartman
moom_callback(struct work_struct * ignored)38596fd7ce5SGreg Kroah-Hartman static void moom_callback(struct work_struct *ignored)
38696fd7ce5SGreg Kroah-Hartman {
3876e0fc46dSDavid Rientjes const gfp_t gfp_mask = GFP_KERNEL;
3886e0fc46dSDavid Rientjes struct oom_control oc = {
3896e0fc46dSDavid Rientjes .zonelist = node_zonelist(first_memory_node, gfp_mask),
3906e0fc46dSDavid Rientjes .nodemask = NULL,
3912a966b77SVladimir Davydov .memcg = NULL,
3926e0fc46dSDavid Rientjes .gfp_mask = gfp_mask,
39354e9e291SDavid Rientjes .order = -1,
3946e0fc46dSDavid Rientjes };
3956e0fc46dSDavid Rientjes
396dc56401fSJohannes Weiner mutex_lock(&oom_lock);
3976e0fc46dSDavid Rientjes if (!out_of_memory(&oc))
398d75da004SMichal Hocko pr_info("OOM request ignored. No task eligible\n");
399dc56401fSJohannes Weiner mutex_unlock(&oom_lock);
40096fd7ce5SGreg Kroah-Hartman }
40196fd7ce5SGreg Kroah-Hartman
40296fd7ce5SGreg Kroah-Hartman static DECLARE_WORK(moom_work, moom_callback);
40396fd7ce5SGreg Kroah-Hartman
sysrq_handle_moom(u8 key)404bcb48185SJiri Slaby static void sysrq_handle_moom(u8 key)
40596fd7ce5SGreg Kroah-Hartman {
40696fd7ce5SGreg Kroah-Hartman schedule_work(&moom_work);
40796fd7ce5SGreg Kroah-Hartman }
4087fffe31dSEmil Velikov static const struct sysrq_key_op sysrq_moom_op = {
40996fd7ce5SGreg Kroah-Hartman .handler = sysrq_handle_moom,
410afa80ccbSzhangwei(Jovi) .help_msg = "memory-full-oom-kill(f)",
41196fd7ce5SGreg Kroah-Hartman .action_msg = "Manual OOM execution",
41296fd7ce5SGreg Kroah-Hartman .enable_mask = SYSRQ_ENABLE_SIGNAL,
41396fd7ce5SGreg Kroah-Hartman };
41496fd7ce5SGreg Kroah-Hartman
4158ec8719fSJunwen Wu #ifdef CONFIG_BLOCK
sysrq_handle_thaw(u8 key)416bcb48185SJiri Slaby static void sysrq_handle_thaw(u8 key)
41796fd7ce5SGreg Kroah-Hartman {
41896fd7ce5SGreg Kroah-Hartman emergency_thaw_all();
41996fd7ce5SGreg Kroah-Hartman }
4207fffe31dSEmil Velikov static const struct sysrq_key_op sysrq_thaw_op = {
42196fd7ce5SGreg Kroah-Hartman .handler = sysrq_handle_thaw,
422afa80ccbSzhangwei(Jovi) .help_msg = "thaw-filesystems(j)",
42396fd7ce5SGreg Kroah-Hartman .action_msg = "Emergency Thaw of all frozen filesystems",
42496fd7ce5SGreg Kroah-Hartman .enable_mask = SYSRQ_ENABLE_SIGNAL,
42596fd7ce5SGreg Kroah-Hartman };
4268ec8719fSJunwen Wu #else
4278ec8719fSJunwen Wu #define sysrq_thaw_op (*(const struct sysrq_key_op *)NULL)
4288ec8719fSJunwen Wu #endif
42996fd7ce5SGreg Kroah-Hartman
sysrq_handle_kill(u8 key)430bcb48185SJiri Slaby static void sysrq_handle_kill(u8 key)
43196fd7ce5SGreg Kroah-Hartman {
43296fd7ce5SGreg Kroah-Hartman send_sig_all(SIGKILL);
433a8fe19ebSBorislav Petkov console_loglevel = CONSOLE_LOGLEVEL_DEBUG;
43496fd7ce5SGreg Kroah-Hartman }
4357fffe31dSEmil Velikov static const struct sysrq_key_op sysrq_kill_op = {
43696fd7ce5SGreg Kroah-Hartman .handler = sysrq_handle_kill,
437afa80ccbSzhangwei(Jovi) .help_msg = "kill-all-tasks(i)",
43896fd7ce5SGreg Kroah-Hartman .action_msg = "Kill All Tasks",
43996fd7ce5SGreg Kroah-Hartman .enable_mask = SYSRQ_ENABLE_SIGNAL,
44096fd7ce5SGreg Kroah-Hartman };
44196fd7ce5SGreg Kroah-Hartman
sysrq_handle_unrt(u8 key)442bcb48185SJiri Slaby static void sysrq_handle_unrt(u8 key)
44396fd7ce5SGreg Kroah-Hartman {
44496fd7ce5SGreg Kroah-Hartman normalize_rt_tasks();
44596fd7ce5SGreg Kroah-Hartman }
4467fffe31dSEmil Velikov static const struct sysrq_key_op sysrq_unrt_op = {
44796fd7ce5SGreg Kroah-Hartman .handler = sysrq_handle_unrt,
448afa80ccbSzhangwei(Jovi) .help_msg = "nice-all-RT-tasks(n)",
44996fd7ce5SGreg Kroah-Hartman .action_msg = "Nice All RT Tasks",
45096fd7ce5SGreg Kroah-Hartman .enable_mask = SYSRQ_ENABLE_RTNICE,
45196fd7ce5SGreg Kroah-Hartman };
45296fd7ce5SGreg Kroah-Hartman
45396fd7ce5SGreg Kroah-Hartman /* Key Operations table and lock */
45496fd7ce5SGreg Kroah-Hartman static DEFINE_SPINLOCK(sysrq_key_table_lock);
45596fd7ce5SGreg Kroah-Hartman
456a27eb0cbSAndrzej Pietrasiewicz static const struct sysrq_key_op *sysrq_key_table[62] = {
45796fd7ce5SGreg Kroah-Hartman &sysrq_loglevel_op, /* 0 */
45896fd7ce5SGreg Kroah-Hartman &sysrq_loglevel_op, /* 1 */
45996fd7ce5SGreg Kroah-Hartman &sysrq_loglevel_op, /* 2 */
46096fd7ce5SGreg Kroah-Hartman &sysrq_loglevel_op, /* 3 */
46196fd7ce5SGreg Kroah-Hartman &sysrq_loglevel_op, /* 4 */
46296fd7ce5SGreg Kroah-Hartman &sysrq_loglevel_op, /* 5 */
46396fd7ce5SGreg Kroah-Hartman &sysrq_loglevel_op, /* 6 */
46496fd7ce5SGreg Kroah-Hartman &sysrq_loglevel_op, /* 7 */
46596fd7ce5SGreg Kroah-Hartman &sysrq_loglevel_op, /* 8 */
46696fd7ce5SGreg Kroah-Hartman &sysrq_loglevel_op, /* 9 */
46796fd7ce5SGreg Kroah-Hartman
46896fd7ce5SGreg Kroah-Hartman /*
46996fd7ce5SGreg Kroah-Hartman * a: Don't use for system provided sysrqs, it is handled specially on
47096fd7ce5SGreg Kroah-Hartman * sparc and will never arrive.
47196fd7ce5SGreg Kroah-Hartman */
47296fd7ce5SGreg Kroah-Hartman NULL, /* a */
47396fd7ce5SGreg Kroah-Hartman &sysrq_reboot_op, /* b */
4745e351410SEric Biggers &sysrq_crash_op, /* c */
47596fd7ce5SGreg Kroah-Hartman &sysrq_showlocks_op, /* d */
47696fd7ce5SGreg Kroah-Hartman &sysrq_term_op, /* e */
47796fd7ce5SGreg Kroah-Hartman &sysrq_moom_op, /* f */
47896fd7ce5SGreg Kroah-Hartman /* g: May be registered for the kernel debugger */
47996fd7ce5SGreg Kroah-Hartman NULL, /* g */
48096fd7ce5SGreg Kroah-Hartman NULL, /* h - reserved for help */
48196fd7ce5SGreg Kroah-Hartman &sysrq_kill_op, /* i */
48296fd7ce5SGreg Kroah-Hartman &sysrq_thaw_op, /* j */
48396fd7ce5SGreg Kroah-Hartman &sysrq_SAK_op, /* k */
48496fd7ce5SGreg Kroah-Hartman &sysrq_showallcpus_op, /* l */
48596fd7ce5SGreg Kroah-Hartman &sysrq_showmem_op, /* m */
48696fd7ce5SGreg Kroah-Hartman &sysrq_unrt_op, /* n */
48796fd7ce5SGreg Kroah-Hartman /* o: This will often be registered as 'Off' at init time */
48896fd7ce5SGreg Kroah-Hartman NULL, /* o */
48996fd7ce5SGreg Kroah-Hartman &sysrq_showregs_op, /* p */
49096fd7ce5SGreg Kroah-Hartman &sysrq_show_timers_op, /* q */
49196fd7ce5SGreg Kroah-Hartman &sysrq_unraw_op, /* r */
49296fd7ce5SGreg Kroah-Hartman &sysrq_sync_op, /* s */
49396fd7ce5SGreg Kroah-Hartman &sysrq_showstate_op, /* t */
49496fd7ce5SGreg Kroah-Hartman &sysrq_mountro_op, /* u */
49596fd7ce5SGreg Kroah-Hartman /* v: May be registered for frame buffer console restore */
49696fd7ce5SGreg Kroah-Hartman NULL, /* v */
49796fd7ce5SGreg Kroah-Hartman &sysrq_showstate_blocked_op, /* w */
498d1e9a4f5SJames Hogan /* x: May be registered on mips for TLB dump */
49996fd7ce5SGreg Kroah-Hartman /* x: May be registered on ppc/powerpc for xmon */
500916ca14aSDavid S. Miller /* x: May be registered on sparc64 for global PMU dump */
50196fd7ce5SGreg Kroah-Hartman NULL, /* x */
50296fd7ce5SGreg Kroah-Hartman /* y: May be registered on sparc64 for global register dump */
50396fd7ce5SGreg Kroah-Hartman NULL, /* y */
50496fd7ce5SGreg Kroah-Hartman &sysrq_ftrace_dump_op, /* z */
505a27eb0cbSAndrzej Pietrasiewicz NULL, /* A */
506a27eb0cbSAndrzej Pietrasiewicz NULL, /* B */
507a27eb0cbSAndrzej Pietrasiewicz NULL, /* C */
508a27eb0cbSAndrzej Pietrasiewicz NULL, /* D */
509a27eb0cbSAndrzej Pietrasiewicz NULL, /* E */
510a27eb0cbSAndrzej Pietrasiewicz NULL, /* F */
511a27eb0cbSAndrzej Pietrasiewicz NULL, /* G */
512a27eb0cbSAndrzej Pietrasiewicz NULL, /* H */
513a27eb0cbSAndrzej Pietrasiewicz NULL, /* I */
514a27eb0cbSAndrzej Pietrasiewicz NULL, /* J */
515a27eb0cbSAndrzej Pietrasiewicz NULL, /* K */
516a27eb0cbSAndrzej Pietrasiewicz NULL, /* L */
517a27eb0cbSAndrzej Pietrasiewicz NULL, /* M */
518a27eb0cbSAndrzej Pietrasiewicz NULL, /* N */
519a27eb0cbSAndrzej Pietrasiewicz NULL, /* O */
520a27eb0cbSAndrzej Pietrasiewicz NULL, /* P */
521a27eb0cbSAndrzej Pietrasiewicz NULL, /* Q */
522a27eb0cbSAndrzej Pietrasiewicz NULL, /* R */
523a27eb0cbSAndrzej Pietrasiewicz NULL, /* S */
524a27eb0cbSAndrzej Pietrasiewicz NULL, /* T */
525a27eb0cbSAndrzej Pietrasiewicz NULL, /* U */
526a27eb0cbSAndrzej Pietrasiewicz NULL, /* V */
527a27eb0cbSAndrzej Pietrasiewicz NULL, /* W */
528a27eb0cbSAndrzej Pietrasiewicz NULL, /* X */
529a27eb0cbSAndrzej Pietrasiewicz NULL, /* Y */
530a27eb0cbSAndrzej Pietrasiewicz NULL, /* Z */
53196fd7ce5SGreg Kroah-Hartman };
53296fd7ce5SGreg Kroah-Hartman
53396fd7ce5SGreg Kroah-Hartman /* key2index calculation, -1 on invalid index */
sysrq_key_table_key2index(u8 key)5348ac20a03SJiri Slaby static int sysrq_key_table_key2index(u8 key)
53596fd7ce5SGreg Kroah-Hartman {
536a27f3b72SJiri Slaby switch (key) {
537a27f3b72SJiri Slaby case '0' ... '9':
538a27f3b72SJiri Slaby return key - '0';
539a27f3b72SJiri Slaby case 'a' ... 'z':
540a27f3b72SJiri Slaby return key - 'a' + 10;
541a27f3b72SJiri Slaby case 'A' ... 'Z':
542a27f3b72SJiri Slaby return key - 'A' + 10 + 26;
543a27f3b72SJiri Slaby default:
544a27f3b72SJiri Slaby return -1;
545a27f3b72SJiri Slaby }
54696fd7ce5SGreg Kroah-Hartman }
54796fd7ce5SGreg Kroah-Hartman
54896fd7ce5SGreg Kroah-Hartman /*
54996fd7ce5SGreg Kroah-Hartman * get and put functions for the table, exposed to modules.
55096fd7ce5SGreg Kroah-Hartman */
__sysrq_get_key_op(u8 key)5518ac20a03SJiri Slaby static const struct sysrq_key_op *__sysrq_get_key_op(u8 key)
55296fd7ce5SGreg Kroah-Hartman {
55323cbedf8SEmil Velikov const struct sysrq_key_op *op_p = NULL;
55496fd7ce5SGreg Kroah-Hartman int i;
55596fd7ce5SGreg Kroah-Hartman
55696fd7ce5SGreg Kroah-Hartman i = sysrq_key_table_key2index(key);
55796fd7ce5SGreg Kroah-Hartman if (i != -1)
55896fd7ce5SGreg Kroah-Hartman op_p = sysrq_key_table[i];
55996fd7ce5SGreg Kroah-Hartman
56096fd7ce5SGreg Kroah-Hartman return op_p;
56196fd7ce5SGreg Kroah-Hartman }
56296fd7ce5SGreg Kroah-Hartman
__sysrq_put_key_op(u8 key,const struct sysrq_key_op * op_p)5638ac20a03SJiri Slaby static void __sysrq_put_key_op(u8 key, const struct sysrq_key_op *op_p)
56496fd7ce5SGreg Kroah-Hartman {
56596fd7ce5SGreg Kroah-Hartman int i = sysrq_key_table_key2index(key);
56696fd7ce5SGreg Kroah-Hartman
56796fd7ce5SGreg Kroah-Hartman if (i != -1)
56896fd7ce5SGreg Kroah-Hartman sysrq_key_table[i] = op_p;
56996fd7ce5SGreg Kroah-Hartman }
57096fd7ce5SGreg Kroah-Hartman
__handle_sysrq(u8 key,bool check_mask)5718ac20a03SJiri Slaby void __handle_sysrq(u8 key, bool check_mask)
57296fd7ce5SGreg Kroah-Hartman {
57323cbedf8SEmil Velikov const struct sysrq_key_op *op_p;
57496fd7ce5SGreg Kroah-Hartman int orig_log_level;
575c39ea0b9SFeng Tang int orig_suppress_printk;
57696fd7ce5SGreg Kroah-Hartman int i;
57796fd7ce5SGreg Kroah-Hartman
578c39ea0b9SFeng Tang orig_suppress_printk = suppress_printk;
579c39ea0b9SFeng Tang suppress_printk = 0;
580c39ea0b9SFeng Tang
581722773afSRik van Riel rcu_sysrq_start();
582984d74a7SRik van Riel rcu_read_lock();
58396fd7ce5SGreg Kroah-Hartman /*
58496fd7ce5SGreg Kroah-Hartman * Raise the apparent loglevel to maximum so that the sysrq header
58596fd7ce5SGreg Kroah-Hartman * is shown to provide the user with positive feedback. We do not
58696fd7ce5SGreg Kroah-Hartman * simply emit this at KERN_EMERG as that would change message
58796fd7ce5SGreg Kroah-Hartman * routing in the consumers of /proc/kmsg.
58896fd7ce5SGreg Kroah-Hartman */
58996fd7ce5SGreg Kroah-Hartman orig_log_level = console_loglevel;
590a8fe19ebSBorislav Petkov console_loglevel = CONSOLE_LOGLEVEL_DEFAULT;
59196fd7ce5SGreg Kroah-Hartman
59296fd7ce5SGreg Kroah-Hartman op_p = __sysrq_get_key_op(key);
59396fd7ce5SGreg Kroah-Hartman if (op_p) {
59496fd7ce5SGreg Kroah-Hartman /*
59596fd7ce5SGreg Kroah-Hartman * Should we check for enabled operations (/proc/sysrq-trigger
59696fd7ce5SGreg Kroah-Hartman * should not) and is the invoked operation enabled?
59796fd7ce5SGreg Kroah-Hartman */
59896fd7ce5SGreg Kroah-Hartman if (!check_mask || sysrq_on_mask(op_p->enable_mask)) {
599c3fee609SPetr Mladek pr_info("%s\n", op_p->action_msg);
60096fd7ce5SGreg Kroah-Hartman console_loglevel = orig_log_level;
60196fd7ce5SGreg Kroah-Hartman op_p->handler(key);
60296fd7ce5SGreg Kroah-Hartman } else {
603c3fee609SPetr Mladek pr_info("This sysrq operation is disabled.\n");
604075e1a0cSPetr Mladek console_loglevel = orig_log_level;
60596fd7ce5SGreg Kroah-Hartman }
60696fd7ce5SGreg Kroah-Hartman } else {
607c3fee609SPetr Mladek pr_info("HELP : ");
60896fd7ce5SGreg Kroah-Hartman /* Only print the help msg once per handler */
60996fd7ce5SGreg Kroah-Hartman for (i = 0; i < ARRAY_SIZE(sysrq_key_table); i++) {
61096fd7ce5SGreg Kroah-Hartman if (sysrq_key_table[i]) {
61196fd7ce5SGreg Kroah-Hartman int j;
61296fd7ce5SGreg Kroah-Hartman
61396fd7ce5SGreg Kroah-Hartman for (j = 0; sysrq_key_table[i] !=
61496fd7ce5SGreg Kroah-Hartman sysrq_key_table[j]; j++)
61596fd7ce5SGreg Kroah-Hartman ;
61696fd7ce5SGreg Kroah-Hartman if (j != i)
61796fd7ce5SGreg Kroah-Hartman continue;
618401e4a7cSMichal Hocko pr_cont("%s ", sysrq_key_table[i]->help_msg);
61996fd7ce5SGreg Kroah-Hartman }
62096fd7ce5SGreg Kroah-Hartman }
621401e4a7cSMichal Hocko pr_cont("\n");
62296fd7ce5SGreg Kroah-Hartman console_loglevel = orig_log_level;
62396fd7ce5SGreg Kroah-Hartman }
624984d74a7SRik van Riel rcu_read_unlock();
625722773afSRik van Riel rcu_sysrq_end();
626c39ea0b9SFeng Tang
627c39ea0b9SFeng Tang suppress_printk = orig_suppress_printk;
62896fd7ce5SGreg Kroah-Hartman }
62996fd7ce5SGreg Kroah-Hartman
handle_sysrq(u8 key)6308ac20a03SJiri Slaby void handle_sysrq(u8 key)
63196fd7ce5SGreg Kroah-Hartman {
63296fd7ce5SGreg Kroah-Hartman if (sysrq_on())
63396fd7ce5SGreg Kroah-Hartman __handle_sysrq(key, true);
63496fd7ce5SGreg Kroah-Hartman }
63596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(handle_sysrq);
63696fd7ce5SGreg Kroah-Hartman
63796fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_INPUT
638ffb6e0c9SArnd Bergmann static int sysrq_reset_downtime_ms;
63996fd7ce5SGreg Kroah-Hartman
64096fd7ce5SGreg Kroah-Hartman /* Simple translation table for the SysRq keys */
641864ee6cbSLinus Torvalds static const unsigned char sysrq_xlate[KEY_CNT] =
64296fd7ce5SGreg Kroah-Hartman "\000\0331234567890-=\177\t" /* 0x00 - 0x0f */
64396fd7ce5SGreg Kroah-Hartman "qwertyuiop[]\r\000as" /* 0x10 - 0x1f */
64496fd7ce5SGreg Kroah-Hartman "dfghjkl;'`\000\\zxcv" /* 0x20 - 0x2f */
64596fd7ce5SGreg Kroah-Hartman "bnm,./\000*\000 \000\201\202\203\204\205" /* 0x30 - 0x3f */
64696fd7ce5SGreg Kroah-Hartman "\206\207\210\211\212\000\000789-456+1" /* 0x40 - 0x4f */
64796fd7ce5SGreg Kroah-Hartman "230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
64896fd7ce5SGreg Kroah-Hartman "\r\000/"; /* 0x60 - 0x6f */
64996fd7ce5SGreg Kroah-Hartman
650864ee6cbSLinus Torvalds struct sysrq_state {
651864ee6cbSLinus Torvalds struct input_handle handle;
652864ee6cbSLinus Torvalds struct work_struct reinject_work;
653864ee6cbSLinus Torvalds unsigned long key_down[BITS_TO_LONGS(KEY_CNT)];
654864ee6cbSLinus Torvalds unsigned int alt;
655864ee6cbSLinus Torvalds unsigned int alt_use;
656a27eb0cbSAndrzej Pietrasiewicz unsigned int shift;
657a27eb0cbSAndrzej Pietrasiewicz unsigned int shift_use;
658864ee6cbSLinus Torvalds bool active;
659864ee6cbSLinus Torvalds bool need_reinject;
66087450bd5SLinus Torvalds bool reinjecting;
661154b7a48SMathieu Poirier
662154b7a48SMathieu Poirier /* reset sequence handling */
663154b7a48SMathieu Poirier bool reset_canceled;
6643d289517SMathieu J. Poirier bool reset_requested;
665154b7a48SMathieu Poirier unsigned long reset_keybit[BITS_TO_LONGS(KEY_CNT)];
666154b7a48SMathieu Poirier int reset_seq_len;
667154b7a48SMathieu Poirier int reset_seq_cnt;
668154b7a48SMathieu Poirier int reset_seq_version;
66939030786SMathieu J. Poirier struct timer_list keyreset_timer;
670864ee6cbSLinus Torvalds };
67196fd7ce5SGreg Kroah-Hartman
672154b7a48SMathieu Poirier #define SYSRQ_KEY_RESET_MAX 20 /* Should be plenty */
673154b7a48SMathieu Poirier static unsigned short sysrq_reset_seq[SYSRQ_KEY_RESET_MAX];
674154b7a48SMathieu Poirier static unsigned int sysrq_reset_seq_len;
675154b7a48SMathieu Poirier static unsigned int sysrq_reset_seq_version = 1;
676154b7a48SMathieu Poirier
sysrq_parse_reset_sequence(struct sysrq_state * state)677154b7a48SMathieu Poirier static void sysrq_parse_reset_sequence(struct sysrq_state *state)
678154b7a48SMathieu Poirier {
679154b7a48SMathieu Poirier int i;
680154b7a48SMathieu Poirier unsigned short key;
681154b7a48SMathieu Poirier
682154b7a48SMathieu Poirier state->reset_seq_cnt = 0;
683154b7a48SMathieu Poirier
684154b7a48SMathieu Poirier for (i = 0; i < sysrq_reset_seq_len; i++) {
685154b7a48SMathieu Poirier key = sysrq_reset_seq[i];
686154b7a48SMathieu Poirier
687154b7a48SMathieu Poirier if (key == KEY_RESERVED || key > KEY_MAX)
688154b7a48SMathieu Poirier break;
689154b7a48SMathieu Poirier
690154b7a48SMathieu Poirier __set_bit(key, state->reset_keybit);
691154b7a48SMathieu Poirier state->reset_seq_len++;
692154b7a48SMathieu Poirier
693154b7a48SMathieu Poirier if (test_bit(key, state->key_down))
694154b7a48SMathieu Poirier state->reset_seq_cnt++;
695154b7a48SMathieu Poirier }
696154b7a48SMathieu Poirier
697154b7a48SMathieu Poirier /* Disable reset until old keys are not released */
698154b7a48SMathieu Poirier state->reset_canceled = state->reset_seq_cnt != 0;
699154b7a48SMathieu Poirier
700154b7a48SMathieu Poirier state->reset_seq_version = sysrq_reset_seq_version;
701154b7a48SMathieu Poirier }
702154b7a48SMathieu Poirier
sysrq_do_reset(struct timer_list * t)7038c318fa9SKees Cook static void sysrq_do_reset(struct timer_list *t)
70439030786SMathieu J. Poirier {
7058c318fa9SKees Cook struct sysrq_state *state = from_timer(state, t, keyreset_timer);
7063d289517SMathieu J. Poirier
7073d289517SMathieu J. Poirier state->reset_requested = true;
7083d289517SMathieu J. Poirier
7098fefbc6dSMark Tomlinson orderly_reboot();
71039030786SMathieu J. Poirier }
71139030786SMathieu J. Poirier
sysrq_handle_reset_request(struct sysrq_state * state)71239030786SMathieu J. Poirier static void sysrq_handle_reset_request(struct sysrq_state *state)
71339030786SMathieu J. Poirier {
7143d289517SMathieu J. Poirier if (state->reset_requested)
7153d289517SMathieu J. Poirier __handle_sysrq(sysrq_xlate[KEY_B], false);
7163d289517SMathieu J. Poirier
71739030786SMathieu J. Poirier if (sysrq_reset_downtime_ms)
71839030786SMathieu J. Poirier mod_timer(&state->keyreset_timer,
71939030786SMathieu J. Poirier jiffies + msecs_to_jiffies(sysrq_reset_downtime_ms));
72039030786SMathieu J. Poirier else
7218c318fa9SKees Cook sysrq_do_reset(&state->keyreset_timer);
72239030786SMathieu J. Poirier }
72339030786SMathieu J. Poirier
sysrq_detect_reset_sequence(struct sysrq_state * state,unsigned int code,int value)72439030786SMathieu J. Poirier static void sysrq_detect_reset_sequence(struct sysrq_state *state,
725154b7a48SMathieu Poirier unsigned int code, int value)
726154b7a48SMathieu Poirier {
727154b7a48SMathieu Poirier if (!test_bit(code, state->reset_keybit)) {
728154b7a48SMathieu Poirier /*
729154b7a48SMathieu Poirier * Pressing any key _not_ in reset sequence cancels
73039030786SMathieu J. Poirier * the reset sequence. Also cancelling the timer in
73139030786SMathieu J. Poirier * case additional keys were pressed after a reset
73239030786SMathieu J. Poirier * has been requested.
733154b7a48SMathieu Poirier */
73439030786SMathieu J. Poirier if (value && state->reset_seq_cnt) {
735154b7a48SMathieu Poirier state->reset_canceled = true;
73639030786SMathieu J. Poirier del_timer(&state->keyreset_timer);
73739030786SMathieu J. Poirier }
738154b7a48SMathieu Poirier } else if (value == 0) {
73939030786SMathieu J. Poirier /*
74039030786SMathieu J. Poirier * Key release - all keys in the reset sequence need
74139030786SMathieu J. Poirier * to be pressed and held for the reset timeout
74239030786SMathieu J. Poirier * to hold.
74339030786SMathieu J. Poirier */
74439030786SMathieu J. Poirier del_timer(&state->keyreset_timer);
74539030786SMathieu J. Poirier
746154b7a48SMathieu Poirier if (--state->reset_seq_cnt == 0)
747154b7a48SMathieu Poirier state->reset_canceled = false;
748154b7a48SMathieu Poirier } else if (value == 1) {
749154b7a48SMathieu Poirier /* key press, not autorepeat */
750154b7a48SMathieu Poirier if (++state->reset_seq_cnt == state->reset_seq_len &&
751154b7a48SMathieu Poirier !state->reset_canceled) {
75239030786SMathieu J. Poirier sysrq_handle_reset_request(state);
753154b7a48SMathieu Poirier }
754154b7a48SMathieu Poirier }
755154b7a48SMathieu Poirier }
756154b7a48SMathieu Poirier
7574c076eb0SMathieu J. Poirier #ifdef CONFIG_OF
sysrq_of_get_keyreset_config(void)7584c076eb0SMathieu J. Poirier static void sysrq_of_get_keyreset_config(void)
7594c076eb0SMathieu J. Poirier {
7604c076eb0SMathieu J. Poirier u32 key;
7614c076eb0SMathieu J. Poirier struct device_node *np;
7624c076eb0SMathieu J. Poirier
7634c076eb0SMathieu J. Poirier np = of_find_node_by_path("/chosen/linux,sysrq-reset-seq");
7644c076eb0SMathieu J. Poirier if (!np) {
7654c076eb0SMathieu J. Poirier pr_debug("No sysrq node found");
7664c076eb0SMathieu J. Poirier return;
7674c076eb0SMathieu J. Poirier }
7684c076eb0SMathieu J. Poirier
7694c076eb0SMathieu J. Poirier /* Reset in case a __weak definition was present */
7704c076eb0SMathieu J. Poirier sysrq_reset_seq_len = 0;
7714c076eb0SMathieu J. Poirier
772*914ef7d1SLuca Ceresoli of_property_for_each_u32(np, "keyset", key) {
7734c076eb0SMathieu J. Poirier if (key == KEY_RESERVED || key > KEY_MAX ||
7744c076eb0SMathieu J. Poirier sysrq_reset_seq_len == SYSRQ_KEY_RESET_MAX)
7754c076eb0SMathieu J. Poirier break;
7764c076eb0SMathieu J. Poirier
7774c076eb0SMathieu J. Poirier sysrq_reset_seq[sysrq_reset_seq_len++] = (unsigned short)key;
7784c076eb0SMathieu J. Poirier }
7794c076eb0SMathieu J. Poirier
7804c076eb0SMathieu J. Poirier /* Get reset timeout if any. */
7814c076eb0SMathieu J. Poirier of_property_read_u32(np, "timeout-ms", &sysrq_reset_downtime_ms);
782279070b9SYangtao Li
783279070b9SYangtao Li of_node_put(np);
7844c076eb0SMathieu J. Poirier }
7854c076eb0SMathieu J. Poirier #else
sysrq_of_get_keyreset_config(void)7864c076eb0SMathieu J. Poirier static void sysrq_of_get_keyreset_config(void)
7874c076eb0SMathieu J. Poirier {
7884c076eb0SMathieu J. Poirier }
7894c076eb0SMathieu J. Poirier #endif
7904c076eb0SMathieu J. Poirier
sysrq_reinject_alt_sysrq(struct work_struct * work)791864ee6cbSLinus Torvalds static void sysrq_reinject_alt_sysrq(struct work_struct *work)
79296fd7ce5SGreg Kroah-Hartman {
793864ee6cbSLinus Torvalds struct sysrq_state *sysrq =
794864ee6cbSLinus Torvalds container_of(work, struct sysrq_state, reinject_work);
795864ee6cbSLinus Torvalds struct input_handle *handle = &sysrq->handle;
796864ee6cbSLinus Torvalds unsigned int alt_code = sysrq->alt_use;
797864ee6cbSLinus Torvalds
798864ee6cbSLinus Torvalds if (sysrq->need_reinject) {
79987450bd5SLinus Torvalds /* we do not want the assignment to be reordered */
80087450bd5SLinus Torvalds sysrq->reinjecting = true;
80187450bd5SLinus Torvalds mb();
80287450bd5SLinus Torvalds
803864ee6cbSLinus Torvalds /* Simulate press and release of Alt + SysRq */
804864ee6cbSLinus Torvalds input_inject_event(handle, EV_KEY, alt_code, 1);
805864ee6cbSLinus Torvalds input_inject_event(handle, EV_KEY, KEY_SYSRQ, 1);
806864ee6cbSLinus Torvalds input_inject_event(handle, EV_SYN, SYN_REPORT, 1);
807864ee6cbSLinus Torvalds
808864ee6cbSLinus Torvalds input_inject_event(handle, EV_KEY, KEY_SYSRQ, 0);
809864ee6cbSLinus Torvalds input_inject_event(handle, EV_KEY, alt_code, 0);
810864ee6cbSLinus Torvalds input_inject_event(handle, EV_SYN, SYN_REPORT, 1);
81187450bd5SLinus Torvalds
81287450bd5SLinus Torvalds mb();
81387450bd5SLinus Torvalds sysrq->reinjecting = false;
814864ee6cbSLinus Torvalds }
815864ee6cbSLinus Torvalds }
816864ee6cbSLinus Torvalds
sysrq_handle_keypress(struct sysrq_state * sysrq,unsigned int code,int value)817154b7a48SMathieu Poirier static bool sysrq_handle_keypress(struct sysrq_state *sysrq,
818154b7a48SMathieu Poirier unsigned int code, int value)
819864ee6cbSLinus Torvalds {
820864ee6cbSLinus Torvalds bool was_active = sysrq->active;
82196fd7ce5SGreg Kroah-Hartman bool suppress;
82296fd7ce5SGreg Kroah-Hartman
82396fd7ce5SGreg Kroah-Hartman switch (code) {
82496fd7ce5SGreg Kroah-Hartman
82596fd7ce5SGreg Kroah-Hartman case KEY_LEFTALT:
82696fd7ce5SGreg Kroah-Hartman case KEY_RIGHTALT:
827864ee6cbSLinus Torvalds if (!value) {
828864ee6cbSLinus Torvalds /* One of ALTs is being released */
829864ee6cbSLinus Torvalds if (sysrq->active && code == sysrq->alt_use)
830864ee6cbSLinus Torvalds sysrq->active = false;
83196fd7ce5SGreg Kroah-Hartman
832864ee6cbSLinus Torvalds sysrq->alt = KEY_RESERVED;
833864ee6cbSLinus Torvalds
834864ee6cbSLinus Torvalds } else if (value != 2) {
835864ee6cbSLinus Torvalds sysrq->alt = code;
836864ee6cbSLinus Torvalds sysrq->need_reinject = false;
83796fd7ce5SGreg Kroah-Hartman }
83896fd7ce5SGreg Kroah-Hartman break;
83996fd7ce5SGreg Kroah-Hartman
840a27eb0cbSAndrzej Pietrasiewicz case KEY_LEFTSHIFT:
841a27eb0cbSAndrzej Pietrasiewicz case KEY_RIGHTSHIFT:
842a27eb0cbSAndrzej Pietrasiewicz if (!value)
843a27eb0cbSAndrzej Pietrasiewicz sysrq->shift = KEY_RESERVED;
844a27eb0cbSAndrzej Pietrasiewicz else if (value != 2)
845a27eb0cbSAndrzej Pietrasiewicz sysrq->shift = code;
8463aee752cSOskari Pirhonen if (sysrq->active)
8473aee752cSOskari Pirhonen sysrq->shift_use = sysrq->shift;
848a27eb0cbSAndrzej Pietrasiewicz break;
849a27eb0cbSAndrzej Pietrasiewicz
85096fd7ce5SGreg Kroah-Hartman case KEY_SYSRQ:
851864ee6cbSLinus Torvalds if (value == 1 && sysrq->alt != KEY_RESERVED) {
852864ee6cbSLinus Torvalds sysrq->active = true;
853864ee6cbSLinus Torvalds sysrq->alt_use = sysrq->alt;
854a27eb0cbSAndrzej Pietrasiewicz /* either RESERVED (for released) or actual code */
855a27eb0cbSAndrzej Pietrasiewicz sysrq->shift_use = sysrq->shift;
856864ee6cbSLinus Torvalds /*
857864ee6cbSLinus Torvalds * If nothing else will be pressed we'll need
85887450bd5SLinus Torvalds * to re-inject Alt-SysRq keysroke.
859864ee6cbSLinus Torvalds */
860864ee6cbSLinus Torvalds sysrq->need_reinject = true;
861864ee6cbSLinus Torvalds }
862864ee6cbSLinus Torvalds
863864ee6cbSLinus Torvalds /*
864864ee6cbSLinus Torvalds * Pretend that sysrq was never pressed at all. This
865864ee6cbSLinus Torvalds * is needed to properly handle KGDB which will try
866864ee6cbSLinus Torvalds * to release all keys after exiting debugger. If we
867864ee6cbSLinus Torvalds * do not clear key bit it KGDB will end up sending
868864ee6cbSLinus Torvalds * release events for Alt and SysRq, potentially
869864ee6cbSLinus Torvalds * triggering print screen function.
870864ee6cbSLinus Torvalds */
871864ee6cbSLinus Torvalds if (sysrq->active)
872154b7a48SMathieu Poirier clear_bit(KEY_SYSRQ, sysrq->handle.dev->key);
873864ee6cbSLinus Torvalds
874864ee6cbSLinus Torvalds break;
875864ee6cbSLinus Torvalds
876864ee6cbSLinus Torvalds default:
877864ee6cbSLinus Torvalds if (sysrq->active && value && value != 2) {
878a27eb0cbSAndrzej Pietrasiewicz unsigned char c = sysrq_xlate[code];
879a27eb0cbSAndrzej Pietrasiewicz
880864ee6cbSLinus Torvalds sysrq->need_reinject = false;
881a27eb0cbSAndrzej Pietrasiewicz if (sysrq->shift_use != KEY_RESERVED)
882a27eb0cbSAndrzej Pietrasiewicz c = toupper(c);
883a27eb0cbSAndrzej Pietrasiewicz __handle_sysrq(c, true);
884864ee6cbSLinus Torvalds }
885864ee6cbSLinus Torvalds break;
886864ee6cbSLinus Torvalds }
887864ee6cbSLinus Torvalds
888864ee6cbSLinus Torvalds suppress = sysrq->active;
889864ee6cbSLinus Torvalds
890864ee6cbSLinus Torvalds if (!sysrq->active) {
891154b7a48SMathieu Poirier
892154b7a48SMathieu Poirier /*
893154b7a48SMathieu Poirier * See if reset sequence has changed since the last time.
894154b7a48SMathieu Poirier */
895154b7a48SMathieu Poirier if (sysrq->reset_seq_version != sysrq_reset_seq_version)
896154b7a48SMathieu Poirier sysrq_parse_reset_sequence(sysrq);
897154b7a48SMathieu Poirier
898864ee6cbSLinus Torvalds /*
899864ee6cbSLinus Torvalds * If we are not suppressing key presses keep track of
900864ee6cbSLinus Torvalds * keyboard state so we can release keys that have been
901864ee6cbSLinus Torvalds * pressed before entering SysRq mode.
902864ee6cbSLinus Torvalds */
903864ee6cbSLinus Torvalds if (value)
904864ee6cbSLinus Torvalds set_bit(code, sysrq->key_down);
905864ee6cbSLinus Torvalds else
906864ee6cbSLinus Torvalds clear_bit(code, sysrq->key_down);
907864ee6cbSLinus Torvalds
908864ee6cbSLinus Torvalds if (was_active)
909864ee6cbSLinus Torvalds schedule_work(&sysrq->reinject_work);
910864ee6cbSLinus Torvalds
91139030786SMathieu J. Poirier /* Check for reset sequence */
91239030786SMathieu J. Poirier sysrq_detect_reset_sequence(sysrq, code, value);
913154b7a48SMathieu Poirier
914154b7a48SMathieu Poirier } else if (value == 0 && test_and_clear_bit(code, sysrq->key_down)) {
915864ee6cbSLinus Torvalds /*
916864ee6cbSLinus Torvalds * Pass on release events for keys that was pressed before
917864ee6cbSLinus Torvalds * entering SysRq mode.
918864ee6cbSLinus Torvalds */
919864ee6cbSLinus Torvalds suppress = false;
92096fd7ce5SGreg Kroah-Hartman }
921154b7a48SMathieu Poirier
922154b7a48SMathieu Poirier return suppress;
923154b7a48SMathieu Poirier }
924154b7a48SMathieu Poirier
sysrq_filter(struct input_handle * handle,unsigned int type,unsigned int code,int value)925154b7a48SMathieu Poirier static bool sysrq_filter(struct input_handle *handle,
926154b7a48SMathieu Poirier unsigned int type, unsigned int code, int value)
927154b7a48SMathieu Poirier {
928154b7a48SMathieu Poirier struct sysrq_state *sysrq = handle->private;
929154b7a48SMathieu Poirier bool suppress;
930154b7a48SMathieu Poirier
931154b7a48SMathieu Poirier /*
932154b7a48SMathieu Poirier * Do not filter anything if we are in the process of re-injecting
933154b7a48SMathieu Poirier * Alt+SysRq combination.
934154b7a48SMathieu Poirier */
935154b7a48SMathieu Poirier if (sysrq->reinjecting)
936154b7a48SMathieu Poirier return false;
937154b7a48SMathieu Poirier
938154b7a48SMathieu Poirier switch (type) {
939154b7a48SMathieu Poirier
940154b7a48SMathieu Poirier case EV_SYN:
941154b7a48SMathieu Poirier suppress = false;
942154b7a48SMathieu Poirier break;
943154b7a48SMathieu Poirier
944154b7a48SMathieu Poirier case EV_KEY:
945154b7a48SMathieu Poirier suppress = sysrq_handle_keypress(sysrq, code, value);
94696fd7ce5SGreg Kroah-Hartman break;
94796fd7ce5SGreg Kroah-Hartman
94896fd7ce5SGreg Kroah-Hartman default:
949864ee6cbSLinus Torvalds suppress = sysrq->active;
95096fd7ce5SGreg Kroah-Hartman break;
95196fd7ce5SGreg Kroah-Hartman }
95296fd7ce5SGreg Kroah-Hartman
95396fd7ce5SGreg Kroah-Hartman return suppress;
95496fd7ce5SGreg Kroah-Hartman }
95596fd7ce5SGreg Kroah-Hartman
sysrq_connect(struct input_handler * handler,struct input_dev * dev,const struct input_device_id * id)95696fd7ce5SGreg Kroah-Hartman static int sysrq_connect(struct input_handler *handler,
95796fd7ce5SGreg Kroah-Hartman struct input_dev *dev,
95896fd7ce5SGreg Kroah-Hartman const struct input_device_id *id)
95996fd7ce5SGreg Kroah-Hartman {
960864ee6cbSLinus Torvalds struct sysrq_state *sysrq;
96196fd7ce5SGreg Kroah-Hartman int error;
96296fd7ce5SGreg Kroah-Hartman
963864ee6cbSLinus Torvalds sysrq = kzalloc(sizeof(struct sysrq_state), GFP_KERNEL);
964864ee6cbSLinus Torvalds if (!sysrq)
96596fd7ce5SGreg Kroah-Hartman return -ENOMEM;
96696fd7ce5SGreg Kroah-Hartman
967864ee6cbSLinus Torvalds INIT_WORK(&sysrq->reinject_work, sysrq_reinject_alt_sysrq);
96896fd7ce5SGreg Kroah-Hartman
969864ee6cbSLinus Torvalds sysrq->handle.dev = dev;
970864ee6cbSLinus Torvalds sysrq->handle.handler = handler;
971864ee6cbSLinus Torvalds sysrq->handle.name = "sysrq";
972864ee6cbSLinus Torvalds sysrq->handle.private = sysrq;
9738c318fa9SKees Cook timer_setup(&sysrq->keyreset_timer, sysrq_do_reset, 0);
974864ee6cbSLinus Torvalds
975864ee6cbSLinus Torvalds error = input_register_handle(&sysrq->handle);
97696fd7ce5SGreg Kroah-Hartman if (error) {
97796fd7ce5SGreg Kroah-Hartman pr_err("Failed to register input sysrq handler, error %d\n",
97896fd7ce5SGreg Kroah-Hartman error);
97996fd7ce5SGreg Kroah-Hartman goto err_free;
98096fd7ce5SGreg Kroah-Hartman }
98196fd7ce5SGreg Kroah-Hartman
982864ee6cbSLinus Torvalds error = input_open_device(&sysrq->handle);
98396fd7ce5SGreg Kroah-Hartman if (error) {
98496fd7ce5SGreg Kroah-Hartman pr_err("Failed to open input device, error %d\n", error);
98596fd7ce5SGreg Kroah-Hartman goto err_unregister;
98696fd7ce5SGreg Kroah-Hartman }
98796fd7ce5SGreg Kroah-Hartman
98896fd7ce5SGreg Kroah-Hartman return 0;
98996fd7ce5SGreg Kroah-Hartman
99096fd7ce5SGreg Kroah-Hartman err_unregister:
991864ee6cbSLinus Torvalds input_unregister_handle(&sysrq->handle);
99296fd7ce5SGreg Kroah-Hartman err_free:
993864ee6cbSLinus Torvalds kfree(sysrq);
99496fd7ce5SGreg Kroah-Hartman return error;
99596fd7ce5SGreg Kroah-Hartman }
99696fd7ce5SGreg Kroah-Hartman
sysrq_disconnect(struct input_handle * handle)99796fd7ce5SGreg Kroah-Hartman static void sysrq_disconnect(struct input_handle *handle)
99896fd7ce5SGreg Kroah-Hartman {
999864ee6cbSLinus Torvalds struct sysrq_state *sysrq = handle->private;
1000864ee6cbSLinus Torvalds
100196fd7ce5SGreg Kroah-Hartman input_close_device(handle);
1002864ee6cbSLinus Torvalds cancel_work_sync(&sysrq->reinject_work);
1003292a089dSSteven Rostedt (Google) timer_shutdown_sync(&sysrq->keyreset_timer);
100496fd7ce5SGreg Kroah-Hartman input_unregister_handle(handle);
1005864ee6cbSLinus Torvalds kfree(sysrq);
100696fd7ce5SGreg Kroah-Hartman }
100796fd7ce5SGreg Kroah-Hartman
100896fd7ce5SGreg Kroah-Hartman /*
100996fd7ce5SGreg Kroah-Hartman * We are matching on KEY_LEFTALT instead of KEY_SYSRQ because not all
101096fd7ce5SGreg Kroah-Hartman * keyboards have SysRq key predefined and so user may add it to keymap
101196fd7ce5SGreg Kroah-Hartman * later, but we expect all such keyboards to have left alt.
101296fd7ce5SGreg Kroah-Hartman */
101396fd7ce5SGreg Kroah-Hartman static const struct input_device_id sysrq_ids[] = {
101496fd7ce5SGreg Kroah-Hartman {
101596fd7ce5SGreg Kroah-Hartman .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
101696fd7ce5SGreg Kroah-Hartman INPUT_DEVICE_ID_MATCH_KEYBIT,
1017802c0388SAkinobu Mita .evbit = { [BIT_WORD(EV_KEY)] = BIT_MASK(EV_KEY) },
1018802c0388SAkinobu Mita .keybit = { [BIT_WORD(KEY_LEFTALT)] = BIT_MASK(KEY_LEFTALT) },
101996fd7ce5SGreg Kroah-Hartman },
102096fd7ce5SGreg Kroah-Hartman { },
102196fd7ce5SGreg Kroah-Hartman };
102296fd7ce5SGreg Kroah-Hartman
102396fd7ce5SGreg Kroah-Hartman static struct input_handler sysrq_handler = {
102496fd7ce5SGreg Kroah-Hartman .filter = sysrq_filter,
102596fd7ce5SGreg Kroah-Hartman .connect = sysrq_connect,
102696fd7ce5SGreg Kroah-Hartman .disconnect = sysrq_disconnect,
102796fd7ce5SGreg Kroah-Hartman .name = "sysrq",
102896fd7ce5SGreg Kroah-Hartman .id_table = sysrq_ids,
102996fd7ce5SGreg Kroah-Hartman };
103096fd7ce5SGreg Kroah-Hartman
sysrq_register_handler(void)103196fd7ce5SGreg Kroah-Hartman static inline void sysrq_register_handler(void)
103296fd7ce5SGreg Kroah-Hartman {
103396fd7ce5SGreg Kroah-Hartman int error;
1034154b7a48SMathieu Poirier
10354c076eb0SMathieu J. Poirier sysrq_of_get_keyreset_config();
10364c076eb0SMathieu J. Poirier
103796fd7ce5SGreg Kroah-Hartman error = input_register_handler(&sysrq_handler);
103896fd7ce5SGreg Kroah-Hartman if (error)
103996fd7ce5SGreg Kroah-Hartman pr_err("Failed to register input handler, error %d", error);
104096fd7ce5SGreg Kroah-Hartman }
104196fd7ce5SGreg Kroah-Hartman
sysrq_unregister_handler(void)104296fd7ce5SGreg Kroah-Hartman static inline void sysrq_unregister_handler(void)
104396fd7ce5SGreg Kroah-Hartman {
104496fd7ce5SGreg Kroah-Hartman input_unregister_handler(&sysrq_handler);
104596fd7ce5SGreg Kroah-Hartman }
104696fd7ce5SGreg Kroah-Hartman
sysrq_reset_seq_param_set(const char * buffer,const struct kernel_param * kp)1047154b7a48SMathieu Poirier static int sysrq_reset_seq_param_set(const char *buffer,
1048154b7a48SMathieu Poirier const struct kernel_param *kp)
1049154b7a48SMathieu Poirier {
1050154b7a48SMathieu Poirier unsigned long val;
1051154b7a48SMathieu Poirier int error;
1052154b7a48SMathieu Poirier
105386b40567SJingoo Han error = kstrtoul(buffer, 0, &val);
1054154b7a48SMathieu Poirier if (error < 0)
1055154b7a48SMathieu Poirier return error;
1056154b7a48SMathieu Poirier
1057154b7a48SMathieu Poirier if (val > KEY_MAX)
1058154b7a48SMathieu Poirier return -EINVAL;
1059154b7a48SMathieu Poirier
1060154b7a48SMathieu Poirier *((unsigned short *)kp->arg) = val;
1061154b7a48SMathieu Poirier sysrq_reset_seq_version++;
1062154b7a48SMathieu Poirier
1063154b7a48SMathieu Poirier return 0;
1064154b7a48SMathieu Poirier }
1065154b7a48SMathieu Poirier
10669c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_sysrq_reset_seq = {
1067154b7a48SMathieu Poirier .get = param_get_ushort,
1068154b7a48SMathieu Poirier .set = sysrq_reset_seq_param_set,
1069154b7a48SMathieu Poirier };
1070154b7a48SMathieu Poirier
1071154b7a48SMathieu Poirier #define param_check_sysrq_reset_seq(name, p) \
1072154b7a48SMathieu Poirier __param_check(name, p, unsigned short)
1073154b7a48SMathieu Poirier
10743bce6f64SPaul Gortmaker /*
10753bce6f64SPaul Gortmaker * not really modular, but the easiest way to keep compat with existing
10763bce6f64SPaul Gortmaker * bootargs behaviour is to continue using module_param here.
10773bce6f64SPaul Gortmaker */
1078154b7a48SMathieu Poirier module_param_array_named(reset_seq, sysrq_reset_seq, sysrq_reset_seq,
1079154b7a48SMathieu Poirier &sysrq_reset_seq_len, 0644);
1080154b7a48SMathieu Poirier
108139030786SMathieu J. Poirier module_param_named(sysrq_downtime_ms, sysrq_reset_downtime_ms, int, 0644);
108239030786SMathieu J. Poirier
108396fd7ce5SGreg Kroah-Hartman #else
108496fd7ce5SGreg Kroah-Hartman
sysrq_register_handler(void)108596fd7ce5SGreg Kroah-Hartman static inline void sysrq_register_handler(void)
108696fd7ce5SGreg Kroah-Hartman {
108796fd7ce5SGreg Kroah-Hartman }
108896fd7ce5SGreg Kroah-Hartman
sysrq_unregister_handler(void)108996fd7ce5SGreg Kroah-Hartman static inline void sysrq_unregister_handler(void)
109096fd7ce5SGreg Kroah-Hartman {
109196fd7ce5SGreg Kroah-Hartman }
109296fd7ce5SGreg Kroah-Hartman
109396fd7ce5SGreg Kroah-Hartman #endif /* CONFIG_INPUT */
109496fd7ce5SGreg Kroah-Hartman
sysrq_toggle_support(int enable_mask)109596fd7ce5SGreg Kroah-Hartman int sysrq_toggle_support(int enable_mask)
109696fd7ce5SGreg Kroah-Hartman {
109796fd7ce5SGreg Kroah-Hartman bool was_enabled = sysrq_on();
109896fd7ce5SGreg Kroah-Hartman
109996fd7ce5SGreg Kroah-Hartman sysrq_enabled = enable_mask;
110096fd7ce5SGreg Kroah-Hartman
110196fd7ce5SGreg Kroah-Hartman if (was_enabled != sysrq_on()) {
110296fd7ce5SGreg Kroah-Hartman if (sysrq_on())
110396fd7ce5SGreg Kroah-Hartman sysrq_register_handler();
110496fd7ce5SGreg Kroah-Hartman else
110596fd7ce5SGreg Kroah-Hartman sysrq_unregister_handler();
110696fd7ce5SGreg Kroah-Hartman }
110796fd7ce5SGreg Kroah-Hartman
110896fd7ce5SGreg Kroah-Hartman return 0;
110996fd7ce5SGreg Kroah-Hartman }
111066bb1c95SDmitry Safonov EXPORT_SYMBOL_GPL(sysrq_toggle_support);
111196fd7ce5SGreg Kroah-Hartman
__sysrq_swap_key_ops(u8 key,const struct sysrq_key_op * insert_op_p,const struct sysrq_key_op * remove_op_p)11128ac20a03SJiri Slaby static int __sysrq_swap_key_ops(u8 key, const struct sysrq_key_op *insert_op_p,
111323cbedf8SEmil Velikov const struct sysrq_key_op *remove_op_p)
111496fd7ce5SGreg Kroah-Hartman {
111596fd7ce5SGreg Kroah-Hartman int retval;
111696fd7ce5SGreg Kroah-Hartman
1117984d74a7SRik van Riel spin_lock(&sysrq_key_table_lock);
111896fd7ce5SGreg Kroah-Hartman if (__sysrq_get_key_op(key) == remove_op_p) {
111996fd7ce5SGreg Kroah-Hartman __sysrq_put_key_op(key, insert_op_p);
112096fd7ce5SGreg Kroah-Hartman retval = 0;
112196fd7ce5SGreg Kroah-Hartman } else {
112296fd7ce5SGreg Kroah-Hartman retval = -1;
112396fd7ce5SGreg Kroah-Hartman }
1124984d74a7SRik van Riel spin_unlock(&sysrq_key_table_lock);
1125984d74a7SRik van Riel
1126984d74a7SRik van Riel /*
1127984d74a7SRik van Riel * A concurrent __handle_sysrq either got the old op or the new op.
1128984d74a7SRik van Riel * Wait for it to go away before returning, so the code for an old
1129984d74a7SRik van Riel * op is not freed (eg. on module unload) while it is in use.
1130984d74a7SRik van Riel */
1131984d74a7SRik van Riel synchronize_rcu();
1132984d74a7SRik van Riel
113396fd7ce5SGreg Kroah-Hartman return retval;
113496fd7ce5SGreg Kroah-Hartman }
113596fd7ce5SGreg Kroah-Hartman
register_sysrq_key(u8 key,const struct sysrq_key_op * op_p)11368ac20a03SJiri Slaby int register_sysrq_key(u8 key, const struct sysrq_key_op *op_p)
113796fd7ce5SGreg Kroah-Hartman {
113896fd7ce5SGreg Kroah-Hartman return __sysrq_swap_key_ops(key, op_p, NULL);
113996fd7ce5SGreg Kroah-Hartman }
114096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(register_sysrq_key);
114196fd7ce5SGreg Kroah-Hartman
unregister_sysrq_key(u8 key,const struct sysrq_key_op * op_p)11428ac20a03SJiri Slaby int unregister_sysrq_key(u8 key, const struct sysrq_key_op *op_p)
114396fd7ce5SGreg Kroah-Hartman {
114496fd7ce5SGreg Kroah-Hartman return __sysrq_swap_key_ops(key, NULL, op_p);
114596fd7ce5SGreg Kroah-Hartman }
114696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(unregister_sysrq_key);
114796fd7ce5SGreg Kroah-Hartman
114896fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_PROC_FS
114996fd7ce5SGreg Kroah-Hartman /*
115096fd7ce5SGreg Kroah-Hartman * writing 'C' to /proc/sysrq-trigger is like sysrq-C
115196fd7ce5SGreg Kroah-Hartman */
write_sysrq_trigger(struct file * file,const char __user * buf,size_t count,loff_t * ppos)115296fd7ce5SGreg Kroah-Hartman static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
115396fd7ce5SGreg Kroah-Hartman size_t count, loff_t *ppos)
115496fd7ce5SGreg Kroah-Hartman {
115596fd7ce5SGreg Kroah-Hartman if (count) {
115696fd7ce5SGreg Kroah-Hartman char c;
115796fd7ce5SGreg Kroah-Hartman
115896fd7ce5SGreg Kroah-Hartman if (get_user(c, buf))
115996fd7ce5SGreg Kroah-Hartman return -EFAULT;
116096fd7ce5SGreg Kroah-Hartman __handle_sysrq(c, false);
116196fd7ce5SGreg Kroah-Hartman }
116296fd7ce5SGreg Kroah-Hartman
116396fd7ce5SGreg Kroah-Hartman return count;
116496fd7ce5SGreg Kroah-Hartman }
116596fd7ce5SGreg Kroah-Hartman
116697a32539SAlexey Dobriyan static const struct proc_ops sysrq_trigger_proc_ops = {
116797a32539SAlexey Dobriyan .proc_write = write_sysrq_trigger,
116897a32539SAlexey Dobriyan .proc_lseek = noop_llseek,
116996fd7ce5SGreg Kroah-Hartman };
117096fd7ce5SGreg Kroah-Hartman
sysrq_init_procfs(void)117196fd7ce5SGreg Kroah-Hartman static void sysrq_init_procfs(void)
117296fd7ce5SGreg Kroah-Hartman {
117396fd7ce5SGreg Kroah-Hartman if (!proc_create("sysrq-trigger", S_IWUSR, NULL,
117497a32539SAlexey Dobriyan &sysrq_trigger_proc_ops))
117596fd7ce5SGreg Kroah-Hartman pr_err("Failed to register proc interface\n");
117696fd7ce5SGreg Kroah-Hartman }
117796fd7ce5SGreg Kroah-Hartman
117896fd7ce5SGreg Kroah-Hartman #else
117996fd7ce5SGreg Kroah-Hartman
sysrq_init_procfs(void)118096fd7ce5SGreg Kroah-Hartman static inline void sysrq_init_procfs(void)
118196fd7ce5SGreg Kroah-Hartman {
118296fd7ce5SGreg Kroah-Hartman }
118396fd7ce5SGreg Kroah-Hartman
118496fd7ce5SGreg Kroah-Hartman #endif /* CONFIG_PROC_FS */
118596fd7ce5SGreg Kroah-Hartman
sysrq_init(void)118696fd7ce5SGreg Kroah-Hartman static int __init sysrq_init(void)
118796fd7ce5SGreg Kroah-Hartman {
118896fd7ce5SGreg Kroah-Hartman sysrq_init_procfs();
118996fd7ce5SGreg Kroah-Hartman
119096fd7ce5SGreg Kroah-Hartman if (sysrq_on())
119196fd7ce5SGreg Kroah-Hartman sysrq_register_handler();
119296fd7ce5SGreg Kroah-Hartman
119396fd7ce5SGreg Kroah-Hartman return 0;
119496fd7ce5SGreg Kroah-Hartman }
11953bce6f64SPaul Gortmaker device_initcall(sysrq_init);
1196