sysrq.c 28 KB

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