1// SPDX-License-Identifier: GPL-2.0-only
2#include <linux/sched/debug.h>
3#include <linux/console.h>
4#include <linux/kernel.h>
5#include <linux/ftrace.h>
6#include <linux/sysctl.h>
7#include <linux/nmi.h>
8
9#include <linux/sys_info.h>
10
11struct sys_info_name {
12 unsigned long bit;
13 const char *name;
14};
15
16/*
17 * When 'si_names' gets updated, please make sure the 'sys_info_avail'
18 * below is updated accordingly.
19 */
20static const struct sys_info_name si_names[] = {
21 { SYS_INFO_TASKS, "tasks" },
22 { SYS_INFO_MEM, "mem" },
23 { SYS_INFO_TIMERS, "timers" },
24 { SYS_INFO_LOCKS, "locks" },
25 { SYS_INFO_FTRACE, "ftrace" },
26 { SYS_INFO_ALL_CPU_BT, "all_bt" },
27 { SYS_INFO_BLOCKED_TASKS, "blocked_tasks" },
28};
29
30/* Expecting string like "xxx_sys_info=tasks,mem,timers,locks,ftrace,..." */
31unsigned long sys_info_parse_param(char *str)
32{
33 unsigned long si_bits = 0;
34 char *s, *name;
35 int i;
36
37 s = str;
38 while ((name = strsep(&s, ",")) && *name) {
39 for (i = 0; i < ARRAY_SIZE(si_names); i++) {
40 if (!strcmp(name, si_names[i].name)) {
41 si_bits |= si_names[i].bit;
42 break;
43 }
44 }
45 }
46
47 return si_bits;
48}
49
50#ifdef CONFIG_SYSCTL
51
52static const char sys_info_avail[] __maybe_unused = "tasks,mem,timers,locks,ftrace,all_bt,blocked_tasks";
53
54int sysctl_sys_info_handler(const struct ctl_table *ro_table, int write,
55 void *buffer, size_t *lenp,
56 loff_t *ppos)
57{
58 char names[sizeof(sys_info_avail)];
59 struct ctl_table table;
60 unsigned long *si_bits_global;
61
62 si_bits_global = ro_table->data;
63
64 if (write) {
65 unsigned long si_bits;
66 int ret;
67
68 table = *ro_table;
69 table.data = names;
70 table.maxlen = sizeof(names);
71 ret = proc_dostring(&table, write, buffer, lenp, ppos);
72 if (ret)
73 return ret;
74
75 si_bits = sys_info_parse_param(str: names);
76 /* The access to the global value is not synchronized. */
77 WRITE_ONCE(*si_bits_global, si_bits);
78 return 0;
79 } else {
80 /* for 'read' operation */
81 char *delim = "";
82 int i, len = 0;
83
84 names[0] = '\0';
85 for (i = 0; i < ARRAY_SIZE(si_names); i++) {
86 if (*si_bits_global & si_names[i].bit) {
87 len += scnprintf(buf: names + len, size: sizeof(names) - len,
88 fmt: "%s%s", delim, si_names[i].name);
89 delim = ",";
90 }
91 }
92
93 table = *ro_table;
94 table.data = names;
95 table.maxlen = sizeof(names);
96 return proc_dostring(&table, write, buffer, lenp, ppos);
97 }
98}
99#endif
100
101void sys_info(unsigned long si_mask)
102{
103 if (si_mask & SYS_INFO_TASKS)
104 show_state();
105
106 if (si_mask & SYS_INFO_MEM)
107 show_mem();
108
109 if (si_mask & SYS_INFO_TIMERS)
110 sysrq_timer_list_show();
111
112 if (si_mask & SYS_INFO_LOCKS)
113 debug_show_all_locks();
114
115 if (si_mask & SYS_INFO_FTRACE)
116 ftrace_dump(oops_dump_mode: DUMP_ALL);
117
118 if (si_mask & SYS_INFO_ALL_CPU_BT)
119 trigger_all_cpu_backtrace();
120
121 if (si_mask & SYS_INFO_BLOCKED_TASKS)
122 show_state_filter(TASK_UNINTERRUPTIBLE);
123}
124