sysrq.c 26 KB

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