1 // SPDX-License-Identifier: GPL-2.0
3 * Linux Magic System Request Key Hacks
5 * (c) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
6 * based on ideas by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>
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
12 * Copyright (c) 2010 Dmitry Torokhov
13 * Input handler conversion
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/sched/signal.h>
19 #include <linux/sched/rt.h>
20 #include <linux/sched/debug.h>
21 #include <linux/sched/task.h>
22 #include <linux/interrupt.h>
25 #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>
31 #include <linux/proc_fs.h>
32 #include <linux/nmi.h>
33 #include <linux/quotaops.h>
34 #include <linux/perf_event.h>
35 #include <linux/kernel.h>
36 #include <linux/module.h>
37 #include <linux/suspend.h>
38 #include <linux/writeback.h>
39 #include <linux/swap.h>
40 #include <linux/spinlock.h>
41 #include <linux/vt_kern.h>
42 #include <linux/workqueue.h>
43 #include <linux/hrtimer.h>
44 #include <linux/oom.h>
45 #include <linux/slab.h>
46 #include <linux/input.h>
47 #include <linux/uaccess.h>
48 #include <linux/moduleparam.h>
49 #include <linux/jiffies.h>
50 #include <linux/syscalls.h>
52 #include <linux/rcupdate.h>
54 #include <asm/ptrace.h>
55 #include <asm/irq_regs.h>
57 /* Whether we react on sysrq keys or just ignore them */
58 static int __read_mostly sysrq_enabled
= CONFIG_MAGIC_SYSRQ_DEFAULT_ENABLE
;
59 static bool __read_mostly sysrq_always_enabled
;
61 static bool sysrq_on(void)
63 return sysrq_enabled
|| sysrq_always_enabled
;
67 * A value of 1 means 'all', other nonzero values are an op mask:
69 static bool sysrq_on_mask(int mask
)
71 return sysrq_always_enabled
||
73 (sysrq_enabled
& mask
);
76 static int __init
sysrq_always_enabled_setup(char *str
)
78 sysrq_always_enabled
= true;
79 pr_info("sysrq always enabled.\n");
84 __setup("sysrq_always_enabled", sysrq_always_enabled_setup
);
87 static void sysrq_handle_loglevel(int key
)
92 console_loglevel
= CONSOLE_LOGLEVEL_DEFAULT
;
93 pr_info("Loglevel set to %d\n", i
);
96 static struct sysrq_key_op sysrq_loglevel_op
= {
97 .handler
= sysrq_handle_loglevel
,
98 .help_msg
= "loglevel(0-9)",
99 .action_msg
= "Changing Loglevel",
100 .enable_mask
= SYSRQ_ENABLE_LOG
,
104 static void sysrq_handle_SAK(int key
)
106 struct work_struct
*SAK_work
= &vc_cons
[fg_console
].SAK_work
;
107 schedule_work(SAK_work
);
109 static struct sysrq_key_op sysrq_SAK_op
= {
110 .handler
= sysrq_handle_SAK
,
111 .help_msg
= "sak(k)",
113 .enable_mask
= SYSRQ_ENABLE_KEYBOARD
,
116 #define sysrq_SAK_op (*(struct sysrq_key_op *)NULL)
120 static void sysrq_handle_unraw(int key
)
122 vt_reset_unicode(fg_console
);
125 static struct sysrq_key_op sysrq_unraw_op
= {
126 .handler
= sysrq_handle_unraw
,
127 .help_msg
= "unraw(r)",
128 .action_msg
= "Keyboard mode set to system default",
129 .enable_mask
= SYSRQ_ENABLE_KEYBOARD
,
132 #define sysrq_unraw_op (*(struct sysrq_key_op *)NULL)
133 #endif /* CONFIG_VT */
135 static void sysrq_handle_crash(int key
)
139 /* we need to release the RCU read lock here,
140 * otherwise we get an annoying
141 * 'BUG: sleeping function called from invalid context'
142 * complaint from the kernel before the panic.
145 panic_on_oops
= 1; /* force panic */
149 static struct sysrq_key_op sysrq_crash_op
= {
150 .handler
= sysrq_handle_crash
,
151 .help_msg
= "crash(c)",
152 .action_msg
= "Trigger a crash",
153 .enable_mask
= SYSRQ_ENABLE_DUMP
,
156 static void sysrq_handle_reboot(int key
)
162 static struct sysrq_key_op sysrq_reboot_op
= {
163 .handler
= sysrq_handle_reboot
,
164 .help_msg
= "reboot(b)",
165 .action_msg
= "Resetting",
166 .enable_mask
= SYSRQ_ENABLE_BOOT
,
169 static void sysrq_handle_sync(int key
)
173 static struct sysrq_key_op sysrq_sync_op
= {
174 .handler
= sysrq_handle_sync
,
175 .help_msg
= "sync(s)",
176 .action_msg
= "Emergency Sync",
177 .enable_mask
= SYSRQ_ENABLE_SYNC
,
180 static void sysrq_handle_show_timers(int key
)
182 sysrq_timer_list_show();
185 static struct sysrq_key_op sysrq_show_timers_op
= {
186 .handler
= sysrq_handle_show_timers
,
187 .help_msg
= "show-all-timers(q)",
188 .action_msg
= "Show clockevent devices & pending hrtimers (no others)",
191 static void sysrq_handle_mountro(int key
)
195 static struct sysrq_key_op sysrq_mountro_op
= {
196 .handler
= sysrq_handle_mountro
,
197 .help_msg
= "unmount(u)",
198 .action_msg
= "Emergency Remount R/O",
199 .enable_mask
= SYSRQ_ENABLE_REMOUNT
,
202 #ifdef CONFIG_LOCKDEP
203 static void sysrq_handle_showlocks(int key
)
205 debug_show_all_locks();
208 static struct sysrq_key_op sysrq_showlocks_op
= {
209 .handler
= sysrq_handle_showlocks
,
210 .help_msg
= "show-all-locks(d)",
211 .action_msg
= "Show Locks Held",
214 #define sysrq_showlocks_op (*(struct sysrq_key_op *)NULL)
218 static DEFINE_SPINLOCK(show_lock
);
220 static void showacpu(void *dummy
)
224 /* Idle CPUs have no interesting backtrace. */
225 if (idle_cpu(smp_processor_id()))
228 spin_lock_irqsave(&show_lock
, flags
);
229 pr_info("CPU%d:\n", smp_processor_id());
230 show_stack(NULL
, NULL
);
231 spin_unlock_irqrestore(&show_lock
, flags
);
234 static void sysrq_showregs_othercpus(struct work_struct
*dummy
)
236 smp_call_function(showacpu
, NULL
, 0);
239 static DECLARE_WORK(sysrq_showallcpus
, sysrq_showregs_othercpus
);
241 static void sysrq_handle_showallcpus(int key
)
244 * Fall back to the workqueue based printing if the
245 * backtrace printing did not succeed or the
246 * architecture has no support for it:
248 if (!trigger_all_cpu_backtrace()) {
249 struct pt_regs
*regs
= get_irq_regs();
252 pr_info("CPU%d:\n", smp_processor_id());
255 schedule_work(&sysrq_showallcpus
);
259 static struct sysrq_key_op sysrq_showallcpus_op
= {
260 .handler
= sysrq_handle_showallcpus
,
261 .help_msg
= "show-backtrace-all-active-cpus(l)",
262 .action_msg
= "Show backtrace of all active CPUs",
263 .enable_mask
= SYSRQ_ENABLE_DUMP
,
267 static void sysrq_handle_showregs(int key
)
269 struct pt_regs
*regs
= get_irq_regs();
272 perf_event_print_debug();
274 static struct sysrq_key_op sysrq_showregs_op
= {
275 .handler
= sysrq_handle_showregs
,
276 .help_msg
= "show-registers(p)",
277 .action_msg
= "Show Regs",
278 .enable_mask
= SYSRQ_ENABLE_DUMP
,
281 static void sysrq_handle_showstate(int key
)
284 show_workqueue_state();
286 static struct sysrq_key_op sysrq_showstate_op
= {
287 .handler
= sysrq_handle_showstate
,
288 .help_msg
= "show-task-states(t)",
289 .action_msg
= "Show State",
290 .enable_mask
= SYSRQ_ENABLE_DUMP
,
293 static void sysrq_handle_showstate_blocked(int key
)
295 show_state_filter(TASK_UNINTERRUPTIBLE
);
297 static struct sysrq_key_op sysrq_showstate_blocked_op
= {
298 .handler
= sysrq_handle_showstate_blocked
,
299 .help_msg
= "show-blocked-tasks(w)",
300 .action_msg
= "Show Blocked State",
301 .enable_mask
= SYSRQ_ENABLE_DUMP
,
304 #ifdef CONFIG_TRACING
305 #include <linux/ftrace.h>
307 static void sysrq_ftrace_dump(int key
)
309 ftrace_dump(DUMP_ALL
);
311 static struct sysrq_key_op sysrq_ftrace_dump_op
= {
312 .handler
= sysrq_ftrace_dump
,
313 .help_msg
= "dump-ftrace-buffer(z)",
314 .action_msg
= "Dump ftrace buffer",
315 .enable_mask
= SYSRQ_ENABLE_DUMP
,
318 #define sysrq_ftrace_dump_op (*(struct sysrq_key_op *)NULL)
321 static void sysrq_handle_showmem(int key
)
325 static struct sysrq_key_op sysrq_showmem_op
= {
326 .handler
= sysrq_handle_showmem
,
327 .help_msg
= "show-memory-usage(m)",
328 .action_msg
= "Show Memory",
329 .enable_mask
= SYSRQ_ENABLE_DUMP
,
333 * Signal sysrq helper function. Sends a signal to all user processes.
335 static void send_sig_all(int sig
)
337 struct task_struct
*p
;
339 read_lock(&tasklist_lock
);
340 for_each_process(p
) {
341 if (p
->flags
& PF_KTHREAD
)
343 if (is_global_init(p
))
346 do_send_sig_info(sig
, SEND_SIG_FORCED
, p
, true);
348 read_unlock(&tasklist_lock
);
351 static void sysrq_handle_term(int key
)
353 send_sig_all(SIGTERM
);
354 console_loglevel
= CONSOLE_LOGLEVEL_DEBUG
;
356 static struct sysrq_key_op sysrq_term_op
= {
357 .handler
= sysrq_handle_term
,
358 .help_msg
= "terminate-all-tasks(e)",
359 .action_msg
= "Terminate All Tasks",
360 .enable_mask
= SYSRQ_ENABLE_SIGNAL
,
363 static void moom_callback(struct work_struct
*ignored
)
365 const gfp_t gfp_mask
= GFP_KERNEL
;
366 struct oom_control oc
= {
367 .zonelist
= node_zonelist(first_memory_node
, gfp_mask
),
370 .gfp_mask
= gfp_mask
,
374 mutex_lock(&oom_lock
);
375 if (!out_of_memory(&oc
))
376 pr_info("OOM request ignored. No task eligible\n");
377 mutex_unlock(&oom_lock
);
380 static DECLARE_WORK(moom_work
, moom_callback
);
382 static void sysrq_handle_moom(int key
)
384 schedule_work(&moom_work
);
386 static struct sysrq_key_op sysrq_moom_op
= {
387 .handler
= sysrq_handle_moom
,
388 .help_msg
= "memory-full-oom-kill(f)",
389 .action_msg
= "Manual OOM execution",
390 .enable_mask
= SYSRQ_ENABLE_SIGNAL
,
394 static void sysrq_handle_thaw(int key
)
396 emergency_thaw_all();
398 static struct sysrq_key_op sysrq_thaw_op
= {
399 .handler
= sysrq_handle_thaw
,
400 .help_msg
= "thaw-filesystems(j)",
401 .action_msg
= "Emergency Thaw of all frozen filesystems",
402 .enable_mask
= SYSRQ_ENABLE_SIGNAL
,
406 static void sysrq_handle_kill(int key
)
408 send_sig_all(SIGKILL
);
409 console_loglevel
= CONSOLE_LOGLEVEL_DEBUG
;
411 static struct sysrq_key_op sysrq_kill_op
= {
412 .handler
= sysrq_handle_kill
,
413 .help_msg
= "kill-all-tasks(i)",
414 .action_msg
= "Kill All Tasks",
415 .enable_mask
= SYSRQ_ENABLE_SIGNAL
,
418 static void sysrq_handle_unrt(int key
)
420 normalize_rt_tasks();
422 static struct sysrq_key_op sysrq_unrt_op
= {
423 .handler
= sysrq_handle_unrt
,
424 .help_msg
= "nice-all-RT-tasks(n)",
425 .action_msg
= "Nice All RT Tasks",
426 .enable_mask
= SYSRQ_ENABLE_RTNICE
,
429 /* Key Operations table and lock */
430 static DEFINE_SPINLOCK(sysrq_key_table_lock
);
432 static struct sysrq_key_op
*sysrq_key_table
[36] = {
433 &sysrq_loglevel_op
, /* 0 */
434 &sysrq_loglevel_op
, /* 1 */
435 &sysrq_loglevel_op
, /* 2 */
436 &sysrq_loglevel_op
, /* 3 */
437 &sysrq_loglevel_op
, /* 4 */
438 &sysrq_loglevel_op
, /* 5 */
439 &sysrq_loglevel_op
, /* 6 */
440 &sysrq_loglevel_op
, /* 7 */
441 &sysrq_loglevel_op
, /* 8 */
442 &sysrq_loglevel_op
, /* 9 */
445 * a: Don't use for system provided sysrqs, it is handled specially on
446 * sparc and will never arrive.
449 &sysrq_reboot_op
, /* b */
450 &sysrq_crash_op
, /* c */
451 &sysrq_showlocks_op
, /* d */
452 &sysrq_term_op
, /* e */
453 &sysrq_moom_op
, /* f */
454 /* g: May be registered for the kernel debugger */
456 NULL
, /* h - reserved for help */
457 &sysrq_kill_op
, /* i */
459 &sysrq_thaw_op
, /* j */
463 &sysrq_SAK_op
, /* k */
465 &sysrq_showallcpus_op
, /* l */
469 &sysrq_showmem_op
, /* m */
470 &sysrq_unrt_op
, /* n */
471 /* o: This will often be registered as 'Off' at init time */
473 &sysrq_showregs_op
, /* p */
474 &sysrq_show_timers_op
, /* q */
475 &sysrq_unraw_op
, /* r */
476 &sysrq_sync_op
, /* s */
477 &sysrq_showstate_op
, /* t */
478 &sysrq_mountro_op
, /* u */
479 /* v: May be registered for frame buffer console restore */
481 &sysrq_showstate_blocked_op
, /* w */
482 /* x: May be registered on mips for TLB dump */
483 /* x: May be registered on ppc/powerpc for xmon */
484 /* x: May be registered on sparc64 for global PMU dump */
486 /* y: May be registered on sparc64 for global register dump */
488 &sysrq_ftrace_dump_op
, /* z */
491 /* key2index calculation, -1 on invalid index */
492 static int sysrq_key_table_key2index(int key
)
496 if ((key
>= '0') && (key
<= '9'))
498 else if ((key
>= 'a') && (key
<= 'z'))
499 retval
= key
+ 10 - 'a';
506 * get and put functions for the table, exposed to modules.
508 struct sysrq_key_op
*__sysrq_get_key_op(int key
)
510 struct sysrq_key_op
*op_p
= NULL
;
513 i
= sysrq_key_table_key2index(key
);
515 op_p
= sysrq_key_table
[i
];
520 static void __sysrq_put_key_op(int key
, struct sysrq_key_op
*op_p
)
522 int i
= sysrq_key_table_key2index(key
);
525 sysrq_key_table
[i
] = op_p
;
528 void __handle_sysrq(int key
, bool check_mask
)
530 struct sysrq_key_op
*op_p
;
537 * Raise the apparent loglevel to maximum so that the sysrq header
538 * is shown to provide the user with positive feedback. We do not
539 * simply emit this at KERN_EMERG as that would change message
540 * routing in the consumers of /proc/kmsg.
542 orig_log_level
= console_loglevel
;
543 console_loglevel
= CONSOLE_LOGLEVEL_DEFAULT
;
546 op_p
= __sysrq_get_key_op(key
);
549 * Should we check for enabled operations (/proc/sysrq-trigger
550 * should not) and is the invoked operation enabled?
552 if (!check_mask
|| sysrq_on_mask(op_p
->enable_mask
)) {
553 pr_cont("%s\n", op_p
->action_msg
);
554 console_loglevel
= orig_log_level
;
557 pr_cont("This sysrq operation is disabled.\n");
561 /* Only print the help msg once per handler */
562 for (i
= 0; i
< ARRAY_SIZE(sysrq_key_table
); i
++) {
563 if (sysrq_key_table
[i
]) {
566 for (j
= 0; sysrq_key_table
[i
] !=
567 sysrq_key_table
[j
]; j
++)
571 pr_cont("%s ", sysrq_key_table
[i
]->help_msg
);
575 console_loglevel
= orig_log_level
;
581 void handle_sysrq(int key
)
584 __handle_sysrq(key
, true);
586 EXPORT_SYMBOL(handle_sysrq
);
589 static int sysrq_reset_downtime_ms
;
591 /* Simple translation table for the SysRq keys */
592 static const unsigned char sysrq_xlate
[KEY_CNT
] =
593 "\000\0331234567890-=\177\t" /* 0x00 - 0x0f */
594 "qwertyuiop[]\r\000as" /* 0x10 - 0x1f */
595 "dfghjkl;'`\000\\zxcv" /* 0x20 - 0x2f */
596 "bnm,./\000*\000 \000\201\202\203\204\205" /* 0x30 - 0x3f */
597 "\206\207\210\211\212\000\000789-456+1" /* 0x40 - 0x4f */
598 "230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
599 "\r\000/"; /* 0x60 - 0x6f */
602 struct input_handle handle
;
603 struct work_struct reinject_work
;
604 unsigned long key_down
[BITS_TO_LONGS(KEY_CNT
)];
606 unsigned int alt_use
;
611 /* reset sequence handling */
613 bool reset_requested
;
614 unsigned long reset_keybit
[BITS_TO_LONGS(KEY_CNT
)];
617 int reset_seq_version
;
618 struct timer_list keyreset_timer
;
621 #define SYSRQ_KEY_RESET_MAX 20 /* Should be plenty */
622 static unsigned short sysrq_reset_seq
[SYSRQ_KEY_RESET_MAX
];
623 static unsigned int sysrq_reset_seq_len
;
624 static unsigned int sysrq_reset_seq_version
= 1;
626 static void sysrq_parse_reset_sequence(struct sysrq_state
*state
)
631 state
->reset_seq_cnt
= 0;
633 for (i
= 0; i
< sysrq_reset_seq_len
; i
++) {
634 key
= sysrq_reset_seq
[i
];
636 if (key
== KEY_RESERVED
|| key
> KEY_MAX
)
639 __set_bit(key
, state
->reset_keybit
);
640 state
->reset_seq_len
++;
642 if (test_bit(key
, state
->key_down
))
643 state
->reset_seq_cnt
++;
646 /* Disable reset until old keys are not released */
647 state
->reset_canceled
= state
->reset_seq_cnt
!= 0;
649 state
->reset_seq_version
= sysrq_reset_seq_version
;
652 static void sysrq_do_reset(unsigned long _state
)
654 struct sysrq_state
*state
= (struct sysrq_state
*) _state
;
656 state
->reset_requested
= true;
659 kernel_restart(NULL
);
662 static void sysrq_handle_reset_request(struct sysrq_state
*state
)
664 if (state
->reset_requested
)
665 __handle_sysrq(sysrq_xlate
[KEY_B
], false);
667 if (sysrq_reset_downtime_ms
)
668 mod_timer(&state
->keyreset_timer
,
669 jiffies
+ msecs_to_jiffies(sysrq_reset_downtime_ms
));
671 sysrq_do_reset((unsigned long)state
);
674 static void sysrq_detect_reset_sequence(struct sysrq_state
*state
,
675 unsigned int code
, int value
)
677 if (!test_bit(code
, state
->reset_keybit
)) {
679 * Pressing any key _not_ in reset sequence cancels
680 * the reset sequence. Also cancelling the timer in
681 * case additional keys were pressed after a reset
682 * has been requested.
684 if (value
&& state
->reset_seq_cnt
) {
685 state
->reset_canceled
= true;
686 del_timer(&state
->keyreset_timer
);
688 } else if (value
== 0) {
690 * Key release - all keys in the reset sequence need
691 * to be pressed and held for the reset timeout
694 del_timer(&state
->keyreset_timer
);
696 if (--state
->reset_seq_cnt
== 0)
697 state
->reset_canceled
= false;
698 } else if (value
== 1) {
699 /* key press, not autorepeat */
700 if (++state
->reset_seq_cnt
== state
->reset_seq_len
&&
701 !state
->reset_canceled
) {
702 sysrq_handle_reset_request(state
);
708 static void sysrq_of_get_keyreset_config(void)
711 struct device_node
*np
;
712 struct property
*prop
;
715 np
= of_find_node_by_path("/chosen/linux,sysrq-reset-seq");
717 pr_debug("No sysrq node found");
721 /* Reset in case a __weak definition was present */
722 sysrq_reset_seq_len
= 0;
724 of_property_for_each_u32(np
, "keyset", prop
, p
, key
) {
725 if (key
== KEY_RESERVED
|| key
> KEY_MAX
||
726 sysrq_reset_seq_len
== SYSRQ_KEY_RESET_MAX
)
729 sysrq_reset_seq
[sysrq_reset_seq_len
++] = (unsigned short)key
;
732 /* Get reset timeout if any. */
733 of_property_read_u32(np
, "timeout-ms", &sysrq_reset_downtime_ms
);
736 static void sysrq_of_get_keyreset_config(void)
741 static void sysrq_reinject_alt_sysrq(struct work_struct
*work
)
743 struct sysrq_state
*sysrq
=
744 container_of(work
, struct sysrq_state
, reinject_work
);
745 struct input_handle
*handle
= &sysrq
->handle
;
746 unsigned int alt_code
= sysrq
->alt_use
;
748 if (sysrq
->need_reinject
) {
749 /* we do not want the assignment to be reordered */
750 sysrq
->reinjecting
= true;
753 /* Simulate press and release of Alt + SysRq */
754 input_inject_event(handle
, EV_KEY
, alt_code
, 1);
755 input_inject_event(handle
, EV_KEY
, KEY_SYSRQ
, 1);
756 input_inject_event(handle
, EV_SYN
, SYN_REPORT
, 1);
758 input_inject_event(handle
, EV_KEY
, KEY_SYSRQ
, 0);
759 input_inject_event(handle
, EV_KEY
, alt_code
, 0);
760 input_inject_event(handle
, EV_SYN
, SYN_REPORT
, 1);
763 sysrq
->reinjecting
= false;
767 static bool sysrq_handle_keypress(struct sysrq_state
*sysrq
,
768 unsigned int code
, int value
)
770 bool was_active
= sysrq
->active
;
778 /* One of ALTs is being released */
779 if (sysrq
->active
&& code
== sysrq
->alt_use
)
780 sysrq
->active
= false;
782 sysrq
->alt
= KEY_RESERVED
;
784 } else if (value
!= 2) {
786 sysrq
->need_reinject
= false;
791 if (value
== 1 && sysrq
->alt
!= KEY_RESERVED
) {
792 sysrq
->active
= true;
793 sysrq
->alt_use
= sysrq
->alt
;
795 * If nothing else will be pressed we'll need
796 * to re-inject Alt-SysRq keysroke.
798 sysrq
->need_reinject
= true;
802 * Pretend that sysrq was never pressed at all. This
803 * is needed to properly handle KGDB which will try
804 * to release all keys after exiting debugger. If we
805 * do not clear key bit it KGDB will end up sending
806 * release events for Alt and SysRq, potentially
807 * triggering print screen function.
810 clear_bit(KEY_SYSRQ
, sysrq
->handle
.dev
->key
);
815 if (sysrq
->active
&& value
&& value
!= 2) {
816 sysrq
->need_reinject
= false;
817 __handle_sysrq(sysrq_xlate
[code
], true);
822 suppress
= sysrq
->active
;
824 if (!sysrq
->active
) {
827 * See if reset sequence has changed since the last time.
829 if (sysrq
->reset_seq_version
!= sysrq_reset_seq_version
)
830 sysrq_parse_reset_sequence(sysrq
);
833 * If we are not suppressing key presses keep track of
834 * keyboard state so we can release keys that have been
835 * pressed before entering SysRq mode.
838 set_bit(code
, sysrq
->key_down
);
840 clear_bit(code
, sysrq
->key_down
);
843 schedule_work(&sysrq
->reinject_work
);
845 /* Check for reset sequence */
846 sysrq_detect_reset_sequence(sysrq
, code
, value
);
848 } else if (value
== 0 && test_and_clear_bit(code
, sysrq
->key_down
)) {
850 * Pass on release events for keys that was pressed before
851 * entering SysRq mode.
859 static bool sysrq_filter(struct input_handle
*handle
,
860 unsigned int type
, unsigned int code
, int value
)
862 struct sysrq_state
*sysrq
= handle
->private;
866 * Do not filter anything if we are in the process of re-injecting
867 * Alt+SysRq combination.
869 if (sysrq
->reinjecting
)
879 suppress
= sysrq_handle_keypress(sysrq
, code
, value
);
883 suppress
= sysrq
->active
;
890 static int sysrq_connect(struct input_handler
*handler
,
891 struct input_dev
*dev
,
892 const struct input_device_id
*id
)
894 struct sysrq_state
*sysrq
;
897 sysrq
= kzalloc(sizeof(struct sysrq_state
), GFP_KERNEL
);
901 INIT_WORK(&sysrq
->reinject_work
, sysrq_reinject_alt_sysrq
);
903 sysrq
->handle
.dev
= dev
;
904 sysrq
->handle
.handler
= handler
;
905 sysrq
->handle
.name
= "sysrq";
906 sysrq
->handle
.private = sysrq
;
907 setup_timer(&sysrq
->keyreset_timer
,
908 sysrq_do_reset
, (unsigned long)sysrq
);
910 error
= input_register_handle(&sysrq
->handle
);
912 pr_err("Failed to register input sysrq handler, error %d\n",
917 error
= input_open_device(&sysrq
->handle
);
919 pr_err("Failed to open input device, error %d\n", error
);
926 input_unregister_handle(&sysrq
->handle
);
932 static void sysrq_disconnect(struct input_handle
*handle
)
934 struct sysrq_state
*sysrq
= handle
->private;
936 input_close_device(handle
);
937 cancel_work_sync(&sysrq
->reinject_work
);
938 del_timer_sync(&sysrq
->keyreset_timer
);
939 input_unregister_handle(handle
);
944 * We are matching on KEY_LEFTALT instead of KEY_SYSRQ because not all
945 * keyboards have SysRq key predefined and so user may add it to keymap
946 * later, but we expect all such keyboards to have left alt.
948 static const struct input_device_id sysrq_ids
[] = {
950 .flags
= INPUT_DEVICE_ID_MATCH_EVBIT
|
951 INPUT_DEVICE_ID_MATCH_KEYBIT
,
952 .evbit
= { [BIT_WORD(EV_KEY
)] = BIT_MASK(EV_KEY
) },
953 .keybit
= { [BIT_WORD(KEY_LEFTALT
)] = BIT_MASK(KEY_LEFTALT
) },
958 static struct input_handler sysrq_handler
= {
959 .filter
= sysrq_filter
,
960 .connect
= sysrq_connect
,
961 .disconnect
= sysrq_disconnect
,
963 .id_table
= sysrq_ids
,
966 static bool sysrq_handler_registered
;
968 static inline void sysrq_register_handler(void)
972 sysrq_of_get_keyreset_config();
974 error
= input_register_handler(&sysrq_handler
);
976 pr_err("Failed to register input handler, error %d", error
);
978 sysrq_handler_registered
= true;
981 static inline void sysrq_unregister_handler(void)
983 if (sysrq_handler_registered
) {
984 input_unregister_handler(&sysrq_handler
);
985 sysrq_handler_registered
= false;
989 static int sysrq_reset_seq_param_set(const char *buffer
,
990 const struct kernel_param
*kp
)
995 error
= kstrtoul(buffer
, 0, &val
);
1002 *((unsigned short *)kp
->arg
) = val
;
1003 sysrq_reset_seq_version
++;
1008 static const struct kernel_param_ops param_ops_sysrq_reset_seq
= {
1009 .get
= param_get_ushort
,
1010 .set
= sysrq_reset_seq_param_set
,
1013 #define param_check_sysrq_reset_seq(name, p) \
1014 __param_check(name, p, unsigned short)
1017 * not really modular, but the easiest way to keep compat with existing
1018 * bootargs behaviour is to continue using module_param here.
1020 module_param_array_named(reset_seq
, sysrq_reset_seq
, sysrq_reset_seq
,
1021 &sysrq_reset_seq_len
, 0644);
1023 module_param_named(sysrq_downtime_ms
, sysrq_reset_downtime_ms
, int, 0644);
1027 static inline void sysrq_register_handler(void)
1031 static inline void sysrq_unregister_handler(void)
1035 #endif /* CONFIG_INPUT */
1037 int sysrq_toggle_support(int enable_mask
)
1039 bool was_enabled
= sysrq_on();
1041 sysrq_enabled
= enable_mask
;
1043 if (was_enabled
!= sysrq_on()) {
1045 sysrq_register_handler();
1047 sysrq_unregister_handler();
1053 static int __sysrq_swap_key_ops(int key
, struct sysrq_key_op
*insert_op_p
,
1054 struct sysrq_key_op
*remove_op_p
)
1058 spin_lock(&sysrq_key_table_lock
);
1059 if (__sysrq_get_key_op(key
) == remove_op_p
) {
1060 __sysrq_put_key_op(key
, insert_op_p
);
1065 spin_unlock(&sysrq_key_table_lock
);
1068 * A concurrent __handle_sysrq either got the old op or the new op.
1069 * Wait for it to go away before returning, so the code for an old
1070 * op is not freed (eg. on module unload) while it is in use.
1077 int register_sysrq_key(int key
, struct sysrq_key_op
*op_p
)
1079 return __sysrq_swap_key_ops(key
, op_p
, NULL
);
1081 EXPORT_SYMBOL(register_sysrq_key
);
1083 int unregister_sysrq_key(int key
, struct sysrq_key_op
*op_p
)
1085 return __sysrq_swap_key_ops(key
, NULL
, op_p
);
1087 EXPORT_SYMBOL(unregister_sysrq_key
);
1089 #ifdef CONFIG_PROC_FS
1091 * writing 'C' to /proc/sysrq-trigger is like sysrq-C
1093 static ssize_t
write_sysrq_trigger(struct file
*file
, const char __user
*buf
,
1094 size_t count
, loff_t
*ppos
)
1099 if (get_user(c
, buf
))
1101 __handle_sysrq(c
, false);
1107 static const struct file_operations proc_sysrq_trigger_operations
= {
1108 .write
= write_sysrq_trigger
,
1109 .llseek
= noop_llseek
,
1112 static void sysrq_init_procfs(void)
1114 if (!proc_create("sysrq-trigger", S_IWUSR
, NULL
,
1115 &proc_sysrq_trigger_operations
))
1116 pr_err("Failed to register proc interface\n");
1121 static inline void sysrq_init_procfs(void)
1125 #endif /* CONFIG_PROC_FS */
1127 static int __init
sysrq_init(void)
1129 sysrq_init_procfs();
1132 sysrq_register_handler();
1136 device_initcall(sysrq_init
);