sysrq.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Linux Magic System Request Key Hacks
  4. *
  5. * (c) 1997 Martin Mares <[email protected]>
  6. * based on ideas by Pavel Machek <[email protected]>
  7. *
  8. * (c) 2000 Crutcher Dunnavant <[email protected]>
  9. * overhauled to use key registration
  10. * based upon discusions in irc://irc.openprojects.net/#kernelnewbies
  11. *
  12. * Copyright (c) 2010 Dmitry Torokhov
  13. * Input handler conversion
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/sched/signal.h>
  17. #include <linux/sched/rt.h>
  18. #include <linux/sched/debug.h>
  19. #include <linux/sched/task.h>
  20. #include <linux/ctype.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/mm.h>
  23. #include <linux/fs.h>
  24. #include <linux/mount.h>
  25. #include <linux/kdev_t.h>
  26. #include <linux/major.h>
  27. #include <linux/reboot.h>
  28. #include <linux/sysrq.h>
  29. #include <linux/kbd_kern.h>
  30. #include <linux/proc_fs.h>
  31. #include <linux/nmi.h>
  32. #include <linux/quotaops.h>
  33. #include <linux/perf_event.h>
  34. #include <linux/kernel.h>
  35. #include <linux/module.h>
  36. #include <linux/suspend.h>
  37. #include <linux/writeback.h>
  38. #include <linux/swap.h>
  39. #include <linux/spinlock.h>
  40. #include <linux/vt_kern.h>
  41. #include <linux/workqueue.h>
  42. #include <linux/hrtimer.h>
  43. #include <linux/oom.h>
  44. #include <linux/slab.h>
  45. #include <linux/input.h>
  46. #include <linux/uaccess.h>
  47. #include <linux/moduleparam.h>
  48. #include <linux/jiffies.h>
  49. #include <linux/syscalls.h>
  50. #include <linux/of.h>
  51. #include <linux/rcupdate.h>
  52. #include <asm/ptrace.h>
  53. #include <asm/irq_regs.h>
  54. #include <trace/hooks/sysrqcrash.h>
  55. /* Whether we react on sysrq keys or just ignore them */
  56. static int __read_mostly sysrq_enabled = CONFIG_MAGIC_SYSRQ_DEFAULT_ENABLE;
  57. static bool __read_mostly sysrq_always_enabled;
  58. static bool sysrq_on(void)
  59. {
  60. return sysrq_enabled || sysrq_always_enabled;
  61. }
  62. /**
  63. * sysrq_mask - Getter for sysrq_enabled mask.
  64. *
  65. * Return: 1 if sysrq is always enabled, enabled sysrq_key_op mask otherwise.
  66. */
  67. int sysrq_mask(void)
  68. {
  69. if (sysrq_always_enabled)
  70. return 1;
  71. return sysrq_enabled;
  72. }
  73. EXPORT_SYMBOL_GPL(sysrq_mask);
  74. /*
  75. * A value of 1 means 'all', other nonzero values are an op mask:
  76. */
  77. static bool sysrq_on_mask(int mask)
  78. {
  79. return sysrq_always_enabled ||
  80. sysrq_enabled == 1 ||
  81. (sysrq_enabled & mask);
  82. }
  83. static int __init sysrq_always_enabled_setup(char *str)
  84. {
  85. sysrq_always_enabled = true;
  86. pr_info("sysrq always enabled.\n");
  87. return 1;
  88. }
  89. __setup("sysrq_always_enabled", sysrq_always_enabled_setup);
  90. static void sysrq_handle_loglevel(int key)
  91. {
  92. int i;
  93. i = key - '0';
  94. console_loglevel = CONSOLE_LOGLEVEL_DEFAULT;
  95. pr_info("Loglevel set to %d\n", i);
  96. console_loglevel = i;
  97. }
  98. static const struct sysrq_key_op sysrq_loglevel_op = {
  99. .handler = sysrq_handle_loglevel,
  100. .help_msg = "loglevel(0-9)",
  101. .action_msg = "Changing Loglevel",
  102. .enable_mask = SYSRQ_ENABLE_LOG,
  103. };
  104. #ifdef CONFIG_VT
  105. static void sysrq_handle_SAK(int key)
  106. {
  107. struct work_struct *SAK_work = &vc_cons[fg_console].SAK_work;
  108. schedule_work(SAK_work);
  109. }
  110. static const struct sysrq_key_op sysrq_SAK_op = {
  111. .handler = sysrq_handle_SAK,
  112. .help_msg = "sak(k)",
  113. .action_msg = "SAK",
  114. .enable_mask = SYSRQ_ENABLE_KEYBOARD,
  115. };
  116. #else
  117. #define sysrq_SAK_op (*(const struct sysrq_key_op *)NULL)
  118. #endif
  119. #ifdef CONFIG_VT
  120. static void sysrq_handle_unraw(int key)
  121. {
  122. vt_reset_unicode(fg_console);
  123. }
  124. static const struct sysrq_key_op sysrq_unraw_op = {
  125. .handler = sysrq_handle_unraw,
  126. .help_msg = "unraw(r)",
  127. .action_msg = "Keyboard mode set to system default",
  128. .enable_mask = SYSRQ_ENABLE_KEYBOARD,
  129. };
  130. #else
  131. #define sysrq_unraw_op (*(const struct sysrq_key_op *)NULL)
  132. #endif /* CONFIG_VT */
  133. static void sysrq_handle_crash(int key)
  134. {
  135. /* release the RCU read lock before crashing */
  136. rcu_read_unlock();
  137. trace_android_vh_sysrq_crash(current);
  138. panic("sysrq triggered crash\n");
  139. }
  140. static const struct sysrq_key_op sysrq_crash_op = {
  141. .handler = sysrq_handle_crash,
  142. .help_msg = "crash(c)",
  143. .action_msg = "Trigger a crash",
  144. .enable_mask = SYSRQ_ENABLE_DUMP,
  145. };
  146. static void sysrq_handle_reboot(int key)
  147. {
  148. lockdep_off();
  149. local_irq_enable();
  150. emergency_restart();
  151. }
  152. static const struct sysrq_key_op sysrq_reboot_op = {
  153. .handler = sysrq_handle_reboot,
  154. .help_msg = "reboot(b)",
  155. .action_msg = "Resetting",
  156. .enable_mask = SYSRQ_ENABLE_BOOT,
  157. };
  158. const struct sysrq_key_op *__sysrq_reboot_op = &sysrq_reboot_op;
  159. static void sysrq_handle_sync(int key)
  160. {
  161. emergency_sync();
  162. }
  163. static const struct sysrq_key_op sysrq_sync_op = {
  164. .handler = sysrq_handle_sync,
  165. .help_msg = "sync(s)",
  166. .action_msg = "Emergency Sync",
  167. .enable_mask = SYSRQ_ENABLE_SYNC,
  168. };
  169. static void sysrq_handle_show_timers(int key)
  170. {
  171. sysrq_timer_list_show();
  172. }
  173. static const struct sysrq_key_op sysrq_show_timers_op = {
  174. .handler = sysrq_handle_show_timers,
  175. .help_msg = "show-all-timers(q)",
  176. .action_msg = "Show clockevent devices & pending hrtimers (no others)",
  177. };
  178. static void sysrq_handle_mountro(int key)
  179. {
  180. emergency_remount();
  181. }
  182. static const struct sysrq_key_op sysrq_mountro_op = {
  183. .handler = sysrq_handle_mountro,
  184. .help_msg = "unmount(u)",
  185. .action_msg = "Emergency Remount R/O",
  186. .enable_mask = SYSRQ_ENABLE_REMOUNT,
  187. };
  188. #ifdef CONFIG_LOCKDEP
  189. static void sysrq_handle_showlocks(int key)
  190. {
  191. debug_show_all_locks();
  192. }
  193. static const struct sysrq_key_op sysrq_showlocks_op = {
  194. .handler = sysrq_handle_showlocks,
  195. .help_msg = "show-all-locks(d)",
  196. .action_msg = "Show Locks Held",
  197. };
  198. #else
  199. #define sysrq_showlocks_op (*(const struct sysrq_key_op *)NULL)
  200. #endif
  201. #ifdef CONFIG_SMP
  202. static DEFINE_RAW_SPINLOCK(show_lock);
  203. static void showacpu(void *dummy)
  204. {
  205. unsigned long flags;
  206. /* Idle CPUs have no interesting backtrace. */
  207. if (idle_cpu(smp_processor_id())) {
  208. pr_info("CPU%d: backtrace skipped as idling\n", smp_processor_id());
  209. return;
  210. }
  211. raw_spin_lock_irqsave(&show_lock, flags);
  212. pr_info("CPU%d:\n", smp_processor_id());
  213. show_stack(NULL, NULL, KERN_INFO);
  214. raw_spin_unlock_irqrestore(&show_lock, flags);
  215. }
  216. static void sysrq_showregs_othercpus(struct work_struct *dummy)
  217. {
  218. smp_call_function(showacpu, NULL, 0);
  219. }
  220. static DECLARE_WORK(sysrq_showallcpus, sysrq_showregs_othercpus);
  221. static void sysrq_handle_showallcpus(int key)
  222. {
  223. /*
  224. * Fall back to the workqueue based printing if the
  225. * backtrace printing did not succeed or the
  226. * architecture has no support for it:
  227. */
  228. if (!trigger_all_cpu_backtrace()) {
  229. struct pt_regs *regs = NULL;
  230. if (in_hardirq())
  231. regs = get_irq_regs();
  232. pr_info("CPU%d:\n", get_cpu());
  233. if (regs)
  234. show_regs(regs);
  235. else
  236. show_stack(NULL, NULL, KERN_INFO);
  237. schedule_work(&sysrq_showallcpus);
  238. put_cpu();
  239. }
  240. }
  241. static const struct sysrq_key_op sysrq_showallcpus_op = {
  242. .handler = sysrq_handle_showallcpus,
  243. .help_msg = "show-backtrace-all-active-cpus(l)",
  244. .action_msg = "Show backtrace of all active CPUs",
  245. .enable_mask = SYSRQ_ENABLE_DUMP,
  246. };
  247. #else
  248. #define sysrq_showallcpus_op (*(const struct sysrq_key_op *)NULL)
  249. #endif
  250. static void sysrq_handle_showregs(int key)
  251. {
  252. struct pt_regs *regs = NULL;
  253. if (in_hardirq())
  254. regs = get_irq_regs();
  255. if (regs)
  256. show_regs(regs);
  257. perf_event_print_debug();
  258. }
  259. static const struct sysrq_key_op sysrq_showregs_op = {
  260. .handler = sysrq_handle_showregs,
  261. .help_msg = "show-registers(p)",
  262. .action_msg = "Show Regs",
  263. .enable_mask = SYSRQ_ENABLE_DUMP,
  264. };
  265. static void sysrq_handle_showstate(int key)
  266. {
  267. show_state();
  268. show_all_workqueues();
  269. }
  270. static const struct sysrq_key_op sysrq_showstate_op = {
  271. .handler = sysrq_handle_showstate,
  272. .help_msg = "show-task-states(t)",
  273. .action_msg = "Show State",
  274. .enable_mask = SYSRQ_ENABLE_DUMP,
  275. };
  276. static void sysrq_handle_showstate_blocked(int key)
  277. {
  278. show_state_filter(TASK_UNINTERRUPTIBLE);
  279. }
  280. static const struct sysrq_key_op sysrq_showstate_blocked_op = {
  281. .handler = sysrq_handle_showstate_blocked,
  282. .help_msg = "show-blocked-tasks(w)",
  283. .action_msg = "Show Blocked State",
  284. .enable_mask = SYSRQ_ENABLE_DUMP,
  285. };
  286. #ifdef CONFIG_TRACING
  287. #include <linux/ftrace.h>
  288. static void sysrq_ftrace_dump(int key)
  289. {
  290. ftrace_dump(DUMP_ALL);
  291. }
  292. static const struct sysrq_key_op sysrq_ftrace_dump_op = {
  293. .handler = sysrq_ftrace_dump,
  294. .help_msg = "dump-ftrace-buffer(z)",
  295. .action_msg = "Dump ftrace buffer",
  296. .enable_mask = SYSRQ_ENABLE_DUMP,
  297. };
  298. #else
  299. #define sysrq_ftrace_dump_op (*(const struct sysrq_key_op *)NULL)
  300. #endif
  301. static void sysrq_handle_showmem(int key)
  302. {
  303. show_mem(0, NULL);
  304. }
  305. static const struct sysrq_key_op sysrq_showmem_op = {
  306. .handler = sysrq_handle_showmem,
  307. .help_msg = "show-memory-usage(m)",
  308. .action_msg = "Show Memory",
  309. .enable_mask = SYSRQ_ENABLE_DUMP,
  310. };
  311. /*
  312. * Signal sysrq helper function. Sends a signal to all user processes.
  313. */
  314. static void send_sig_all(int sig)
  315. {
  316. struct task_struct *p;
  317. read_lock(&tasklist_lock);
  318. for_each_process(p) {
  319. if (p->flags & PF_KTHREAD)
  320. continue;
  321. if (is_global_init(p))
  322. continue;
  323. do_send_sig_info(sig, SEND_SIG_PRIV, p, PIDTYPE_MAX);
  324. }
  325. read_unlock(&tasklist_lock);
  326. }
  327. static void sysrq_handle_term(int key)
  328. {
  329. send_sig_all(SIGTERM);
  330. console_loglevel = CONSOLE_LOGLEVEL_DEBUG;
  331. }
  332. static const struct sysrq_key_op sysrq_term_op = {
  333. .handler = sysrq_handle_term,
  334. .help_msg = "terminate-all-tasks(e)",
  335. .action_msg = "Terminate All Tasks",
  336. .enable_mask = SYSRQ_ENABLE_SIGNAL,
  337. };
  338. static void moom_callback(struct work_struct *ignored)
  339. {
  340. const gfp_t gfp_mask = GFP_KERNEL;
  341. struct oom_control oc = {
  342. .zonelist = node_zonelist(first_memory_node, gfp_mask),
  343. .nodemask = NULL,
  344. .memcg = NULL,
  345. .gfp_mask = gfp_mask,
  346. .order = -1,
  347. };
  348. mutex_lock(&oom_lock);
  349. if (!out_of_memory(&oc))
  350. pr_info("OOM request ignored. No task eligible\n");
  351. mutex_unlock(&oom_lock);
  352. }
  353. static DECLARE_WORK(moom_work, moom_callback);
  354. static void sysrq_handle_moom(int key)
  355. {
  356. schedule_work(&moom_work);
  357. }
  358. static const struct sysrq_key_op sysrq_moom_op = {
  359. .handler = sysrq_handle_moom,
  360. .help_msg = "memory-full-oom-kill(f)",
  361. .action_msg = "Manual OOM execution",
  362. .enable_mask = SYSRQ_ENABLE_SIGNAL,
  363. };
  364. #ifdef CONFIG_BLOCK
  365. static void sysrq_handle_thaw(int key)
  366. {
  367. emergency_thaw_all();
  368. }
  369. static const struct sysrq_key_op sysrq_thaw_op = {
  370. .handler = sysrq_handle_thaw,
  371. .help_msg = "thaw-filesystems(j)",
  372. .action_msg = "Emergency Thaw of all frozen filesystems",
  373. .enable_mask = SYSRQ_ENABLE_SIGNAL,
  374. };
  375. #else
  376. #define sysrq_thaw_op (*(const struct sysrq_key_op *)NULL)
  377. #endif
  378. static void sysrq_handle_kill(int key)
  379. {
  380. send_sig_all(SIGKILL);
  381. console_loglevel = CONSOLE_LOGLEVEL_DEBUG;
  382. }
  383. static const struct sysrq_key_op sysrq_kill_op = {
  384. .handler = sysrq_handle_kill,
  385. .help_msg = "kill-all-tasks(i)",
  386. .action_msg = "Kill All Tasks",
  387. .enable_mask = SYSRQ_ENABLE_SIGNAL,
  388. };
  389. static void sysrq_handle_unrt(int key)
  390. {
  391. normalize_rt_tasks();
  392. }
  393. static const struct sysrq_key_op sysrq_unrt_op = {
  394. .handler = sysrq_handle_unrt,
  395. .help_msg = "nice-all-RT-tasks(n)",
  396. .action_msg = "Nice All RT Tasks",
  397. .enable_mask = SYSRQ_ENABLE_RTNICE,
  398. };
  399. /* Key Operations table and lock */
  400. static DEFINE_SPINLOCK(sysrq_key_table_lock);
  401. static const struct sysrq_key_op *sysrq_key_table[62] = {
  402. &sysrq_loglevel_op, /* 0 */
  403. &sysrq_loglevel_op, /* 1 */
  404. &sysrq_loglevel_op, /* 2 */
  405. &sysrq_loglevel_op, /* 3 */
  406. &sysrq_loglevel_op, /* 4 */
  407. &sysrq_loglevel_op, /* 5 */
  408. &sysrq_loglevel_op, /* 6 */
  409. &sysrq_loglevel_op, /* 7 */
  410. &sysrq_loglevel_op, /* 8 */
  411. &sysrq_loglevel_op, /* 9 */
  412. /*
  413. * a: Don't use for system provided sysrqs, it is handled specially on
  414. * sparc and will never arrive.
  415. */
  416. NULL, /* a */
  417. &sysrq_reboot_op, /* b */
  418. &sysrq_crash_op, /* c */
  419. &sysrq_showlocks_op, /* d */
  420. &sysrq_term_op, /* e */
  421. &sysrq_moom_op, /* f */
  422. /* g: May be registered for the kernel debugger */
  423. NULL, /* g */
  424. NULL, /* h - reserved for help */
  425. &sysrq_kill_op, /* i */
  426. &sysrq_thaw_op, /* j */
  427. &sysrq_SAK_op, /* k */
  428. &sysrq_showallcpus_op, /* l */
  429. &sysrq_showmem_op, /* m */
  430. &sysrq_unrt_op, /* n */
  431. /* o: This will often be registered as 'Off' at init time */
  432. NULL, /* o */
  433. &sysrq_showregs_op, /* p */
  434. &sysrq_show_timers_op, /* q */
  435. &sysrq_unraw_op, /* r */
  436. &sysrq_sync_op, /* s */
  437. &sysrq_showstate_op, /* t */
  438. &sysrq_mountro_op, /* u */
  439. /* v: May be registered for frame buffer console restore */
  440. NULL, /* v */
  441. &sysrq_showstate_blocked_op, /* w */
  442. /* x: May be registered on mips for TLB dump */
  443. /* x: May be registered on ppc/powerpc for xmon */
  444. /* x: May be registered on sparc64 for global PMU dump */
  445. NULL, /* x */
  446. /* y: May be registered on sparc64 for global register dump */
  447. NULL, /* y */
  448. &sysrq_ftrace_dump_op, /* z */
  449. NULL, /* A */
  450. NULL, /* B */
  451. NULL, /* C */
  452. NULL, /* D */
  453. NULL, /* E */
  454. NULL, /* F */
  455. NULL, /* G */
  456. NULL, /* H */
  457. NULL, /* I */
  458. NULL, /* J */
  459. NULL, /* K */
  460. NULL, /* L */
  461. NULL, /* M */
  462. NULL, /* N */
  463. NULL, /* O */
  464. NULL, /* P */
  465. NULL, /* Q */
  466. NULL, /* R */
  467. NULL, /* S */
  468. NULL, /* T */
  469. NULL, /* U */
  470. NULL, /* V */
  471. NULL, /* W */
  472. NULL, /* X */
  473. NULL, /* Y */
  474. NULL, /* Z */
  475. };
  476. /* key2index calculation, -1 on invalid index */
  477. static int sysrq_key_table_key2index(int key)
  478. {
  479. int retval;
  480. if ((key >= '0') && (key <= '9'))
  481. retval = key - '0';
  482. else if ((key >= 'a') && (key <= 'z'))
  483. retval = key + 10 - 'a';
  484. else if ((key >= 'A') && (key <= 'Z'))
  485. retval = key + 36 - 'A';
  486. else
  487. retval = -1;
  488. return retval;
  489. }
  490. /*
  491. * get and put functions for the table, exposed to modules.
  492. */
  493. static const struct sysrq_key_op *__sysrq_get_key_op(int key)
  494. {
  495. const struct sysrq_key_op *op_p = NULL;
  496. int i;
  497. i = sysrq_key_table_key2index(key);
  498. if (i != -1)
  499. op_p = sysrq_key_table[i];
  500. return op_p;
  501. }
  502. static void __sysrq_put_key_op(int key, const struct sysrq_key_op *op_p)
  503. {
  504. int i = sysrq_key_table_key2index(key);
  505. if (i != -1)
  506. sysrq_key_table[i] = op_p;
  507. }
  508. void __handle_sysrq(int key, bool check_mask)
  509. {
  510. const struct sysrq_key_op *op_p;
  511. int orig_log_level;
  512. int orig_suppress_printk;
  513. int i;
  514. orig_suppress_printk = suppress_printk;
  515. suppress_printk = 0;
  516. rcu_sysrq_start();
  517. rcu_read_lock();
  518. /*
  519. * Raise the apparent loglevel to maximum so that the sysrq header
  520. * is shown to provide the user with positive feedback. We do not
  521. * simply emit this at KERN_EMERG as that would change message
  522. * routing in the consumers of /proc/kmsg.
  523. */
  524. orig_log_level = console_loglevel;
  525. console_loglevel = CONSOLE_LOGLEVEL_DEFAULT;
  526. op_p = __sysrq_get_key_op(key);
  527. if (op_p) {
  528. /*
  529. * Should we check for enabled operations (/proc/sysrq-trigger
  530. * should not) and is the invoked operation enabled?
  531. */
  532. if (!check_mask || sysrq_on_mask(op_p->enable_mask)) {
  533. pr_info("%s\n", op_p->action_msg);
  534. console_loglevel = orig_log_level;
  535. op_p->handler(key);
  536. } else {
  537. pr_info("This sysrq operation is disabled.\n");
  538. console_loglevel = orig_log_level;
  539. }
  540. } else {
  541. pr_info("HELP : ");
  542. /* Only print the help msg once per handler */
  543. for (i = 0; i < ARRAY_SIZE(sysrq_key_table); i++) {
  544. if (sysrq_key_table[i]) {
  545. int j;
  546. for (j = 0; sysrq_key_table[i] !=
  547. sysrq_key_table[j]; j++)
  548. ;
  549. if (j != i)
  550. continue;
  551. pr_cont("%s ", sysrq_key_table[i]->help_msg);
  552. }
  553. }
  554. pr_cont("\n");
  555. console_loglevel = orig_log_level;
  556. }
  557. rcu_read_unlock();
  558. rcu_sysrq_end();
  559. suppress_printk = orig_suppress_printk;
  560. }
  561. void handle_sysrq(int key)
  562. {
  563. if (sysrq_on())
  564. __handle_sysrq(key, true);
  565. }
  566. EXPORT_SYMBOL(handle_sysrq);
  567. #ifdef CONFIG_INPUT
  568. static int sysrq_reset_downtime_ms;
  569. /* Simple translation table for the SysRq keys */
  570. static const unsigned char sysrq_xlate[KEY_CNT] =
  571. "\000\0331234567890-=\177\t" /* 0x00 - 0x0f */
  572. "qwertyuiop[]\r\000as" /* 0x10 - 0x1f */
  573. "dfghjkl;'`\000\\zxcv" /* 0x20 - 0x2f */
  574. "bnm,./\000*\000 \000\201\202\203\204\205" /* 0x30 - 0x3f */
  575. "\206\207\210\211\212\000\000789-456+1" /* 0x40 - 0x4f */
  576. "230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
  577. "\r\000/"; /* 0x60 - 0x6f */
  578. struct sysrq_state {
  579. struct input_handle handle;
  580. struct work_struct reinject_work;
  581. unsigned long key_down[BITS_TO_LONGS(KEY_CNT)];
  582. unsigned int alt;
  583. unsigned int alt_use;
  584. unsigned int shift;
  585. unsigned int shift_use;
  586. bool active;
  587. bool need_reinject;
  588. bool reinjecting;
  589. /* reset sequence handling */
  590. bool reset_canceled;
  591. bool reset_requested;
  592. unsigned long reset_keybit[BITS_TO_LONGS(KEY_CNT)];
  593. int reset_seq_len;
  594. int reset_seq_cnt;
  595. int reset_seq_version;
  596. struct timer_list keyreset_timer;
  597. };
  598. #define SYSRQ_KEY_RESET_MAX 20 /* Should be plenty */
  599. static unsigned short sysrq_reset_seq[SYSRQ_KEY_RESET_MAX];
  600. static unsigned int sysrq_reset_seq_len;
  601. static unsigned int sysrq_reset_seq_version = 1;
  602. static void sysrq_parse_reset_sequence(struct sysrq_state *state)
  603. {
  604. int i;
  605. unsigned short key;
  606. state->reset_seq_cnt = 0;
  607. for (i = 0; i < sysrq_reset_seq_len; i++) {
  608. key = sysrq_reset_seq[i];
  609. if (key == KEY_RESERVED || key > KEY_MAX)
  610. break;
  611. __set_bit(key, state->reset_keybit);
  612. state->reset_seq_len++;
  613. if (test_bit(key, state->key_down))
  614. state->reset_seq_cnt++;
  615. }
  616. /* Disable reset until old keys are not released */
  617. state->reset_canceled = state->reset_seq_cnt != 0;
  618. state->reset_seq_version = sysrq_reset_seq_version;
  619. }
  620. static void sysrq_do_reset(struct timer_list *t)
  621. {
  622. struct sysrq_state *state = from_timer(state, t, keyreset_timer);
  623. state->reset_requested = true;
  624. orderly_reboot();
  625. }
  626. static void sysrq_handle_reset_request(struct sysrq_state *state)
  627. {
  628. if (state->reset_requested)
  629. __handle_sysrq(sysrq_xlate[KEY_B], false);
  630. if (sysrq_reset_downtime_ms)
  631. mod_timer(&state->keyreset_timer,
  632. jiffies + msecs_to_jiffies(sysrq_reset_downtime_ms));
  633. else
  634. sysrq_do_reset(&state->keyreset_timer);
  635. }
  636. static void sysrq_detect_reset_sequence(struct sysrq_state *state,
  637. unsigned int code, int value)
  638. {
  639. if (!test_bit(code, state->reset_keybit)) {
  640. /*
  641. * Pressing any key _not_ in reset sequence cancels
  642. * the reset sequence. Also cancelling the timer in
  643. * case additional keys were pressed after a reset
  644. * has been requested.
  645. */
  646. if (value && state->reset_seq_cnt) {
  647. state->reset_canceled = true;
  648. del_timer(&state->keyreset_timer);
  649. }
  650. } else if (value == 0) {
  651. /*
  652. * Key release - all keys in the reset sequence need
  653. * to be pressed and held for the reset timeout
  654. * to hold.
  655. */
  656. del_timer(&state->keyreset_timer);
  657. if (--state->reset_seq_cnt == 0)
  658. state->reset_canceled = false;
  659. } else if (value == 1) {
  660. /* key press, not autorepeat */
  661. if (++state->reset_seq_cnt == state->reset_seq_len &&
  662. !state->reset_canceled) {
  663. sysrq_handle_reset_request(state);
  664. }
  665. }
  666. }
  667. #ifdef CONFIG_OF
  668. static void sysrq_of_get_keyreset_config(void)
  669. {
  670. u32 key;
  671. struct device_node *np;
  672. struct property *prop;
  673. const __be32 *p;
  674. np = of_find_node_by_path("/chosen/linux,sysrq-reset-seq");
  675. if (!np) {
  676. pr_debug("No sysrq node found");
  677. return;
  678. }
  679. /* Reset in case a __weak definition was present */
  680. sysrq_reset_seq_len = 0;
  681. of_property_for_each_u32(np, "keyset", prop, p, key) {
  682. if (key == KEY_RESERVED || key > KEY_MAX ||
  683. sysrq_reset_seq_len == SYSRQ_KEY_RESET_MAX)
  684. break;
  685. sysrq_reset_seq[sysrq_reset_seq_len++] = (unsigned short)key;
  686. }
  687. /* Get reset timeout if any. */
  688. of_property_read_u32(np, "timeout-ms", &sysrq_reset_downtime_ms);
  689. of_node_put(np);
  690. }
  691. #else
  692. static void sysrq_of_get_keyreset_config(void)
  693. {
  694. }
  695. #endif
  696. static void sysrq_reinject_alt_sysrq(struct work_struct *work)
  697. {
  698. struct sysrq_state *sysrq =
  699. container_of(work, struct sysrq_state, reinject_work);
  700. struct input_handle *handle = &sysrq->handle;
  701. unsigned int alt_code = sysrq->alt_use;
  702. if (sysrq->need_reinject) {
  703. /* we do not want the assignment to be reordered */
  704. sysrq->reinjecting = true;
  705. mb();
  706. /* Simulate press and release of Alt + SysRq */
  707. input_inject_event(handle, EV_KEY, alt_code, 1);
  708. input_inject_event(handle, EV_KEY, KEY_SYSRQ, 1);
  709. input_inject_event(handle, EV_SYN, SYN_REPORT, 1);
  710. input_inject_event(handle, EV_KEY, KEY_SYSRQ, 0);
  711. input_inject_event(handle, EV_KEY, alt_code, 0);
  712. input_inject_event(handle, EV_SYN, SYN_REPORT, 1);
  713. mb();
  714. sysrq->reinjecting = false;
  715. }
  716. }
  717. static bool sysrq_handle_keypress(struct sysrq_state *sysrq,
  718. unsigned int code, int value)
  719. {
  720. bool was_active = sysrq->active;
  721. bool suppress;
  722. switch (code) {
  723. case KEY_LEFTALT:
  724. case KEY_RIGHTALT:
  725. if (!value) {
  726. /* One of ALTs is being released */
  727. if (sysrq->active && code == sysrq->alt_use)
  728. sysrq->active = false;
  729. sysrq->alt = KEY_RESERVED;
  730. } else if (value != 2) {
  731. sysrq->alt = code;
  732. sysrq->need_reinject = false;
  733. }
  734. break;
  735. case KEY_LEFTSHIFT:
  736. case KEY_RIGHTSHIFT:
  737. if (!value)
  738. sysrq->shift = KEY_RESERVED;
  739. else if (value != 2)
  740. sysrq->shift = code;
  741. if (sysrq->active)
  742. sysrq->shift_use = sysrq->shift;
  743. break;
  744. case KEY_SYSRQ:
  745. if (value == 1 && sysrq->alt != KEY_RESERVED) {
  746. sysrq->active = true;
  747. sysrq->alt_use = sysrq->alt;
  748. /* either RESERVED (for released) or actual code */
  749. sysrq->shift_use = sysrq->shift;
  750. /*
  751. * If nothing else will be pressed we'll need
  752. * to re-inject Alt-SysRq keysroke.
  753. */
  754. sysrq->need_reinject = true;
  755. }
  756. /*
  757. * Pretend that sysrq was never pressed at all. This
  758. * is needed to properly handle KGDB which will try
  759. * to release all keys after exiting debugger. If we
  760. * do not clear key bit it KGDB will end up sending
  761. * release events for Alt and SysRq, potentially
  762. * triggering print screen function.
  763. */
  764. if (sysrq->active)
  765. clear_bit(KEY_SYSRQ, sysrq->handle.dev->key);
  766. break;
  767. default:
  768. if (sysrq->active && value && value != 2) {
  769. unsigned char c = sysrq_xlate[code];
  770. sysrq->need_reinject = false;
  771. if (sysrq->shift_use != KEY_RESERVED)
  772. c = toupper(c);
  773. __handle_sysrq(c, true);
  774. }
  775. break;
  776. }
  777. suppress = sysrq->active;
  778. if (!sysrq->active) {
  779. /*
  780. * See if reset sequence has changed since the last time.
  781. */
  782. if (sysrq->reset_seq_version != sysrq_reset_seq_version)
  783. sysrq_parse_reset_sequence(sysrq);
  784. /*
  785. * If we are not suppressing key presses keep track of
  786. * keyboard state so we can release keys that have been
  787. * pressed before entering SysRq mode.
  788. */
  789. if (value)
  790. set_bit(code, sysrq->key_down);
  791. else
  792. clear_bit(code, sysrq->key_down);
  793. if (was_active)
  794. schedule_work(&sysrq->reinject_work);
  795. /* Check for reset sequence */
  796. sysrq_detect_reset_sequence(sysrq, code, value);
  797. } else if (value == 0 && test_and_clear_bit(code, sysrq->key_down)) {
  798. /*
  799. * Pass on release events for keys that was pressed before
  800. * entering SysRq mode.
  801. */
  802. suppress = false;
  803. }
  804. return suppress;
  805. }
  806. static bool sysrq_filter(struct input_handle *handle,
  807. unsigned int type, unsigned int code, int value)
  808. {
  809. struct sysrq_state *sysrq = handle->private;
  810. bool suppress;
  811. /*
  812. * Do not filter anything if we are in the process of re-injecting
  813. * Alt+SysRq combination.
  814. */
  815. if (sysrq->reinjecting)
  816. return false;
  817. switch (type) {
  818. case EV_SYN:
  819. suppress = false;
  820. break;
  821. case EV_KEY:
  822. suppress = sysrq_handle_keypress(sysrq, code, value);
  823. break;
  824. default:
  825. suppress = sysrq->active;
  826. break;
  827. }
  828. return suppress;
  829. }
  830. static int sysrq_connect(struct input_handler *handler,
  831. struct input_dev *dev,
  832. const struct input_device_id *id)
  833. {
  834. struct sysrq_state *sysrq;
  835. int error;
  836. sysrq = kzalloc(sizeof(struct sysrq_state), GFP_KERNEL);
  837. if (!sysrq)
  838. return -ENOMEM;
  839. INIT_WORK(&sysrq->reinject_work, sysrq_reinject_alt_sysrq);
  840. sysrq->handle.dev = dev;
  841. sysrq->handle.handler = handler;
  842. sysrq->handle.name = "sysrq";
  843. sysrq->handle.private = sysrq;
  844. timer_setup(&sysrq->keyreset_timer, sysrq_do_reset, 0);
  845. error = input_register_handle(&sysrq->handle);
  846. if (error) {
  847. pr_err("Failed to register input sysrq handler, error %d\n",
  848. error);
  849. goto err_free;
  850. }
  851. error = input_open_device(&sysrq->handle);
  852. if (error) {
  853. pr_err("Failed to open input device, error %d\n", error);
  854. goto err_unregister;
  855. }
  856. return 0;
  857. err_unregister:
  858. input_unregister_handle(&sysrq->handle);
  859. err_free:
  860. kfree(sysrq);
  861. return error;
  862. }
  863. static void sysrq_disconnect(struct input_handle *handle)
  864. {
  865. struct sysrq_state *sysrq = handle->private;
  866. input_close_device(handle);
  867. cancel_work_sync(&sysrq->reinject_work);
  868. del_timer_sync(&sysrq->keyreset_timer);
  869. input_unregister_handle(handle);
  870. kfree(sysrq);
  871. }
  872. /*
  873. * We are matching on KEY_LEFTALT instead of KEY_SYSRQ because not all
  874. * keyboards have SysRq key predefined and so user may add it to keymap
  875. * later, but we expect all such keyboards to have left alt.
  876. */
  877. static const struct input_device_id sysrq_ids[] = {
  878. {
  879. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  880. INPUT_DEVICE_ID_MATCH_KEYBIT,
  881. .evbit = { [BIT_WORD(EV_KEY)] = BIT_MASK(EV_KEY) },
  882. .keybit = { [BIT_WORD(KEY_LEFTALT)] = BIT_MASK(KEY_LEFTALT) },
  883. },
  884. { },
  885. };
  886. static struct input_handler sysrq_handler = {
  887. .filter = sysrq_filter,
  888. .connect = sysrq_connect,
  889. .disconnect = sysrq_disconnect,
  890. .name = "sysrq",
  891. .id_table = sysrq_ids,
  892. };
  893. static inline void sysrq_register_handler(void)
  894. {
  895. int error;
  896. sysrq_of_get_keyreset_config();
  897. error = input_register_handler(&sysrq_handler);
  898. if (error)
  899. pr_err("Failed to register input handler, error %d", error);
  900. }
  901. static inline void sysrq_unregister_handler(void)
  902. {
  903. input_unregister_handler(&sysrq_handler);
  904. }
  905. static int sysrq_reset_seq_param_set(const char *buffer,
  906. const struct kernel_param *kp)
  907. {
  908. unsigned long val;
  909. int error;
  910. error = kstrtoul(buffer, 0, &val);
  911. if (error < 0)
  912. return error;
  913. if (val > KEY_MAX)
  914. return -EINVAL;
  915. *((unsigned short *)kp->arg) = val;
  916. sysrq_reset_seq_version++;
  917. return 0;
  918. }
  919. static const struct kernel_param_ops param_ops_sysrq_reset_seq = {
  920. .get = param_get_ushort,
  921. .set = sysrq_reset_seq_param_set,
  922. };
  923. #define param_check_sysrq_reset_seq(name, p) \
  924. __param_check(name, p, unsigned short)
  925. /*
  926. * not really modular, but the easiest way to keep compat with existing
  927. * bootargs behaviour is to continue using module_param here.
  928. */
  929. module_param_array_named(reset_seq, sysrq_reset_seq, sysrq_reset_seq,
  930. &sysrq_reset_seq_len, 0644);
  931. module_param_named(sysrq_downtime_ms, sysrq_reset_downtime_ms, int, 0644);
  932. #else
  933. static inline void sysrq_register_handler(void)
  934. {
  935. }
  936. static inline void sysrq_unregister_handler(void)
  937. {
  938. }
  939. #endif /* CONFIG_INPUT */
  940. int sysrq_toggle_support(int enable_mask)
  941. {
  942. bool was_enabled = sysrq_on();
  943. sysrq_enabled = enable_mask;
  944. if (was_enabled != sysrq_on()) {
  945. if (sysrq_on())
  946. sysrq_register_handler();
  947. else
  948. sysrq_unregister_handler();
  949. }
  950. return 0;
  951. }
  952. EXPORT_SYMBOL_GPL(sysrq_toggle_support);
  953. static int __sysrq_swap_key_ops(int key, const struct sysrq_key_op *insert_op_p,
  954. const struct sysrq_key_op *remove_op_p)
  955. {
  956. int retval;
  957. spin_lock(&sysrq_key_table_lock);
  958. if (__sysrq_get_key_op(key) == remove_op_p) {
  959. __sysrq_put_key_op(key, insert_op_p);
  960. retval = 0;
  961. } else {
  962. retval = -1;
  963. }
  964. spin_unlock(&sysrq_key_table_lock);
  965. /*
  966. * A concurrent __handle_sysrq either got the old op or the new op.
  967. * Wait for it to go away before returning, so the code for an old
  968. * op is not freed (eg. on module unload) while it is in use.
  969. */
  970. synchronize_rcu();
  971. return retval;
  972. }
  973. int register_sysrq_key(int key, const struct sysrq_key_op *op_p)
  974. {
  975. return __sysrq_swap_key_ops(key, op_p, NULL);
  976. }
  977. EXPORT_SYMBOL(register_sysrq_key);
  978. int unregister_sysrq_key(int key, const struct sysrq_key_op *op_p)
  979. {
  980. return __sysrq_swap_key_ops(key, NULL, op_p);
  981. }
  982. EXPORT_SYMBOL(unregister_sysrq_key);
  983. #ifdef CONFIG_PROC_FS
  984. /*
  985. * writing 'C' to /proc/sysrq-trigger is like sysrq-C
  986. */
  987. static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
  988. size_t count, loff_t *ppos)
  989. {
  990. if (count) {
  991. char c;
  992. if (get_user(c, buf))
  993. return -EFAULT;
  994. __handle_sysrq(c, false);
  995. }
  996. return count;
  997. }
  998. static const struct proc_ops sysrq_trigger_proc_ops = {
  999. .proc_write = write_sysrq_trigger,
  1000. .proc_lseek = noop_llseek,
  1001. };
  1002. static void sysrq_init_procfs(void)
  1003. {
  1004. if (!proc_create("sysrq-trigger", S_IWUSR, NULL,
  1005. &sysrq_trigger_proc_ops))
  1006. pr_err("Failed to register proc interface\n");
  1007. }
  1008. #else
  1009. static inline void sysrq_init_procfs(void)
  1010. {
  1011. }
  1012. #endif /* CONFIG_PROC_FS */
  1013. static int __init sysrq_init(void)
  1014. {
  1015. sysrq_init_procfs();
  1016. if (sysrq_on())
  1017. sysrq_register_handler();
  1018. return 0;
  1019. }
  1020. device_initcall(sysrq_init);