|
3 | 3 | #include <linux/console.h> |
4 | 4 | #include <linux/kernel.h> |
5 | 5 | #include <linux/ftrace.h> |
| 6 | +#include <linux/sysctl.h> |
6 | 7 | #include <linux/nmi.h> |
7 | 8 |
|
8 | 9 | #include <linux/sys_info.h> |
9 | 10 |
|
| 11 | +struct 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 | + */ |
| 20 | +static 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,..." */ |
| 31 | +unsigned 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 | + |
| 52 | +static const char sys_info_avail[] __maybe_unused = "tasks,mem,timers,locks,ftrace,all_bt,blocked_tasks"; |
| 53 | + |
| 54 | +int 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) + 1]; |
| 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(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 | + for (i = 0; i < ARRAY_SIZE(si_names); i++) { |
| 85 | + if (*si_bits_global & si_names[i].bit) { |
| 86 | + len += scnprintf(names + len, sizeof(names) - len, |
| 87 | + "%s%s", delim, si_names[i].name); |
| 88 | + delim = ","; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + table = *ro_table; |
| 93 | + table.data = names; |
| 94 | + table.maxlen = sizeof(names); |
| 95 | + return proc_dostring(&table, write, buffer, lenp, ppos); |
| 96 | + } |
| 97 | +} |
| 98 | +#endif |
| 99 | + |
10 | 100 | void sys_info(unsigned long si_mask) |
11 | 101 | { |
12 | 102 | if (si_mask & SYS_INFO_TASKS) |
|
0 commit comments