a6be48b24774475274e23633b98686b790c1b8c5
[linux-2.6-microblaze.git] / kernel / trace / trace_boot.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * trace_boot.c
4  * Tracing kernel boot-time
5  */
6
7 #define pr_fmt(fmt)     "trace_boot: " fmt
8
9 #include <linux/bootconfig.h>
10 #include <linux/cpumask.h>
11 #include <linux/ftrace.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/mutex.h>
15 #include <linux/string.h>
16 #include <linux/slab.h>
17 #include <linux/trace.h>
18 #include <linux/trace_events.h>
19
20 #include "trace.h"
21
22 #define MAX_BUF_LEN 256
23
24 static void __init
25 trace_boot_set_instance_options(struct trace_array *tr, struct xbc_node *node)
26 {
27         struct xbc_node *anode;
28         const char *p;
29         char buf[MAX_BUF_LEN];
30         unsigned long v = 0;
31
32         /* Common ftrace options */
33         xbc_node_for_each_array_value(node, "options", anode, p) {
34                 if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf)) {
35                         pr_err("String is too long: %s\n", p);
36                         continue;
37                 }
38
39                 if (trace_set_options(tr, buf) < 0)
40                         pr_err("Failed to set option: %s\n", buf);
41         }
42
43         p = xbc_node_find_value(node, "tracing_on", NULL);
44         if (p && *p != '\0') {
45                 if (kstrtoul(p, 10, &v))
46                         pr_err("Failed to set tracing on: %s\n", p);
47                 if (v)
48                         tracer_tracing_on(tr);
49                 else
50                         tracer_tracing_off(tr);
51         }
52
53         p = xbc_node_find_value(node, "trace_clock", NULL);
54         if (p && *p != '\0') {
55                 if (tracing_set_clock(tr, p) < 0)
56                         pr_err("Failed to set trace clock: %s\n", p);
57         }
58
59         p = xbc_node_find_value(node, "buffer_size", NULL);
60         if (p && *p != '\0') {
61                 v = memparse(p, NULL);
62                 if (v < PAGE_SIZE)
63                         pr_err("Buffer size is too small: %s\n", p);
64                 if (tracing_resize_ring_buffer(tr, v, RING_BUFFER_ALL_CPUS) < 0)
65                         pr_err("Failed to resize trace buffer to %s\n", p);
66         }
67
68         p = xbc_node_find_value(node, "cpumask", NULL);
69         if (p && *p != '\0') {
70                 cpumask_var_t new_mask;
71
72                 if (alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
73                         if (cpumask_parse(p, new_mask) < 0 ||
74                             tracing_set_cpumask(tr, new_mask) < 0)
75                                 pr_err("Failed to set new CPU mask %s\n", p);
76                         free_cpumask_var(new_mask);
77                 }
78         }
79 }
80
81 #ifdef CONFIG_EVENT_TRACING
82 static void __init
83 trace_boot_enable_events(struct trace_array *tr, struct xbc_node *node)
84 {
85         struct xbc_node *anode;
86         char buf[MAX_BUF_LEN];
87         const char *p;
88
89         xbc_node_for_each_array_value(node, "events", anode, p) {
90                 if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf)) {
91                         pr_err("String is too long: %s\n", p);
92                         continue;
93                 }
94
95                 if (ftrace_set_clr_event(tr, buf, 1) < 0)
96                         pr_err("Failed to enable event: %s\n", p);
97         }
98 }
99
100 #ifdef CONFIG_KPROBE_EVENTS
101 static int __init
102 trace_boot_add_kprobe_event(struct xbc_node *node, const char *event)
103 {
104         struct dynevent_cmd cmd;
105         struct xbc_node *anode;
106         char buf[MAX_BUF_LEN];
107         const char *val;
108         int ret = 0;
109
110         xbc_node_for_each_array_value(node, "probes", anode, val) {
111                 kprobe_event_cmd_init(&cmd, buf, MAX_BUF_LEN);
112
113                 ret = kprobe_event_gen_cmd_start(&cmd, event, val);
114                 if (ret) {
115                         pr_err("Failed to generate probe: %s\n", buf);
116                         break;
117                 }
118
119                 ret = kprobe_event_gen_cmd_end(&cmd);
120                 if (ret) {
121                         pr_err("Failed to add probe: %s\n", buf);
122                         break;
123                 }
124         }
125
126         return ret;
127 }
128 #else
129 static inline int __init
130 trace_boot_add_kprobe_event(struct xbc_node *node, const char *event)
131 {
132         pr_err("Kprobe event is not supported.\n");
133         return -ENOTSUPP;
134 }
135 #endif
136
137 #ifdef CONFIG_SYNTH_EVENTS
138 static int __init
139 trace_boot_add_synth_event(struct xbc_node *node, const char *event)
140 {
141         struct dynevent_cmd cmd;
142         struct xbc_node *anode;
143         char buf[MAX_BUF_LEN];
144         const char *p;
145         int ret;
146
147         synth_event_cmd_init(&cmd, buf, MAX_BUF_LEN);
148
149         ret = synth_event_gen_cmd_start(&cmd, event, NULL);
150         if (ret)
151                 return ret;
152
153         xbc_node_for_each_array_value(node, "fields", anode, p) {
154                 ret = synth_event_add_field_str(&cmd, p);
155                 if (ret)
156                         return ret;
157         }
158
159         ret = synth_event_gen_cmd_end(&cmd);
160         if (ret < 0)
161                 pr_err("Failed to add synthetic event: %s\n", buf);
162
163         return ret;
164 }
165 #else
166 static inline int __init
167 trace_boot_add_synth_event(struct xbc_node *node, const char *event)
168 {
169         pr_err("Synthetic event is not supported.\n");
170         return -ENOTSUPP;
171 }
172 #endif
173
174 #ifdef CONFIG_HIST_TRIGGERS
175 static int __init __printf(3, 4)
176 append_printf(char **bufp, char *end, const char *fmt, ...)
177 {
178         va_list args;
179         int ret;
180
181         if (*bufp == end)
182                 return -ENOSPC;
183
184         va_start(args, fmt);
185         ret = vsnprintf(*bufp, end - *bufp, fmt, args);
186         if (ret < end - *bufp) {
187                 *bufp += ret;
188         } else {
189                 *bufp = end;
190                 ret = -ERANGE;
191         }
192         va_end(args);
193
194         return ret;
195 }
196
197 static int __init
198 append_str_nospace(char **bufp, char *end, const char *str)
199 {
200         char *p = *bufp;
201         int len;
202
203         while (p < end - 1 && *str != '\0') {
204                 if (!isspace(*str))
205                         *(p++) = *str;
206                 str++;
207         }
208         *p = '\0';
209         if (p == end - 1) {
210                 *bufp = end;
211                 return -ENOSPC;
212         }
213         len = p - *bufp;
214         *bufp = p;
215         return (int)len;
216 }
217
218 static int __init
219 trace_boot_hist_add_array(struct xbc_node *hnode, char **bufp,
220                           char *end, const char *key)
221 {
222         struct xbc_node *anode;
223         const char *p;
224         char sep;
225
226         p = xbc_node_find_value(hnode, key, &anode);
227         if (p) {
228                 if (!anode) {
229                         pr_err("hist.%s requires value(s).\n", key);
230                         return -EINVAL;
231                 }
232
233                 append_printf(bufp, end, ":%s", key);
234                 sep = '=';
235                 xbc_array_for_each_value(anode, p) {
236                         append_printf(bufp, end, "%c%s", sep, p);
237                         if (sep == '=')
238                                 sep = ',';
239                 }
240         } else
241                 return -ENOENT;
242
243         return 0;
244 }
245
246 static int __init
247 trace_boot_hist_add_one_handler(struct xbc_node *hnode, char **bufp,
248                                 char *end, const char *handler,
249                                 const char *param)
250 {
251         struct xbc_node *knode, *anode;
252         const char *p;
253         char sep;
254
255         /* Compose 'handler' parameter */
256         p = xbc_node_find_value(hnode, param, NULL);
257         if (!p) {
258                 pr_err("hist.%s requires '%s' option.\n",
259                        xbc_node_get_data(hnode), param);
260                 return -EINVAL;
261         }
262         append_printf(bufp, end, ":%s(%s)", handler, p);
263
264         /* Compose 'action' parameter */
265         knode = xbc_node_find_child(hnode, "trace");
266         if (!knode)
267                 knode = xbc_node_find_child(hnode, "save");
268
269         if (knode) {
270                 anode = xbc_node_get_child(knode);
271                 if (!anode || !xbc_node_is_value(anode)) {
272                         pr_err("hist.%s.%s requires value(s).\n",
273                                xbc_node_get_data(hnode),
274                                xbc_node_get_data(knode));
275                         return -EINVAL;
276                 }
277
278                 append_printf(bufp, end, ".%s", xbc_node_get_data(knode));
279                 sep = '(';
280                 xbc_array_for_each_value(anode, p) {
281                         append_printf(bufp, end, "%c%s", sep, p);
282                         if (sep == '(')
283                                 sep = ',';
284                 }
285                 append_printf(bufp, end, ")");
286         } else if (xbc_node_find_child(hnode, "snapshot")) {
287                 append_printf(bufp, end, ".snapshot()");
288         } else {
289                 pr_err("hist.%s requires an action.\n",
290                        xbc_node_get_data(hnode));
291                 return -EINVAL;
292         }
293
294         return 0;
295 }
296
297 static int __init
298 trace_boot_hist_add_handlers(struct xbc_node *hnode, char **bufp,
299                              char *end, const char *param)
300 {
301         struct xbc_node *node;
302         const char *p, *handler;
303         int ret;
304
305         handler = xbc_node_get_data(hnode);
306
307         xbc_node_for_each_subkey(hnode, node) {
308                 p = xbc_node_get_data(node);
309                 if (!isdigit(p[0]))
310                         continue;
311                 /* All digit started node should be instances. */
312                 ret = trace_boot_hist_add_one_handler(node, bufp, end, handler, param);
313                 if (ret < 0)
314                         break;
315         }
316
317         if (xbc_node_find_child(hnode, param))
318                 ret = trace_boot_hist_add_one_handler(hnode, bufp, end, handler, param);
319
320         return ret;
321 }
322
323 /*
324  * Histogram boottime tracing syntax.
325  *
326  * ftrace.[instance.INSTANCE.]event.GROUP.EVENT.hist[.N] {
327  *      keys = <KEY>[,...]
328  *      values = <VAL>[,...]
329  *      sort = <SORT-KEY>[,...]
330  *      size = <ENTRIES>
331  *      name = <HISTNAME>
332  *      var { <VAR> = <EXPR> ... }
333  *      pause|continue|clear
334  *      onmax|onchange[.N] { var = <VAR>; <ACTION> [= <PARAM>] }
335  *      onmatch[.N] { event = <EVENT>; <ACTION> [= <PARAM>] }
336  *      filter = <FILTER>
337  * }
338  *
339  * Where <ACTION> are;
340  *
341  *      trace = <EVENT>, <ARG1>[, ...]
342  *      save = <ARG1>[, ...]
343  *      snapshot
344  */
345 static int __init
346 trace_boot_compose_hist_cmd(struct xbc_node *hnode, char *buf, size_t size)
347 {
348         struct xbc_node *node, *knode;
349         char *end = buf + size;
350         const char *p;
351         int ret = 0;
352
353         append_printf(&buf, end, "hist");
354
355         ret = trace_boot_hist_add_array(hnode, &buf, end, "keys");
356         if (ret < 0) {
357                 if (ret == -ENOENT)
358                         pr_err("hist requires keys.\n");
359                 return -EINVAL;
360         }
361
362         ret = trace_boot_hist_add_array(hnode, &buf, end, "values");
363         if (ret == -EINVAL)
364                 return ret;
365         ret = trace_boot_hist_add_array(hnode, &buf, end, "sort");
366         if (ret == -EINVAL)
367                 return ret;
368
369         p = xbc_node_find_value(hnode, "size", NULL);
370         if (p)
371                 append_printf(&buf, end, ":size=%s", p);
372
373         p = xbc_node_find_value(hnode, "name", NULL);
374         if (p)
375                 append_printf(&buf, end, ":name=%s", p);
376
377         node = xbc_node_find_child(hnode, "var");
378         if (node) {
379                 xbc_node_for_each_key_value(node, knode, p) {
380                         /* Expression must not include spaces. */
381                         append_printf(&buf, end, ":%s=",
382                                       xbc_node_get_data(knode));
383                         append_str_nospace(&buf, end, p);
384                 }
385         }
386
387         /* Histogram control attributes (mutual exclusive) */
388         if (xbc_node_find_child(hnode, "pause"))
389                 append_printf(&buf, end, ":pause");
390         else if (xbc_node_find_child(hnode, "continue"))
391                 append_printf(&buf, end, ":continue");
392         else if (xbc_node_find_child(hnode, "clear"))
393                 append_printf(&buf, end, ":clear");
394
395         /* Histogram handler and actions */
396         node = xbc_node_find_child(hnode, "onmax");
397         if (node && trace_boot_hist_add_handlers(node, &buf, end, "var") < 0)
398                 return -EINVAL;
399         node = xbc_node_find_child(hnode, "onchange");
400         if (node && trace_boot_hist_add_handlers(node, &buf, end, "var") < 0)
401                 return -EINVAL;
402         node = xbc_node_find_child(hnode, "onmatch");
403         if (node && trace_boot_hist_add_handlers(node, &buf, end, "event") < 0)
404                 return -EINVAL;
405
406         p = xbc_node_find_value(hnode, "filter", NULL);
407         if (p)
408                 append_printf(&buf, end, " if %s", p);
409
410         if (buf == end) {
411                 pr_err("hist exceeds the max command length.\n");
412                 return -E2BIG;
413         }
414
415         return 0;
416 }
417
418 static void __init
419 trace_boot_init_histograms(struct trace_event_file *file,
420                            struct xbc_node *hnode, char *buf, size_t size)
421 {
422         struct xbc_node *node;
423         const char *p;
424         char *tmp;
425
426         xbc_node_for_each_subkey(hnode, node) {
427                 p = xbc_node_get_data(node);
428                 if (!isdigit(p[0]))
429                         continue;
430                 /* All digit started node should be instances. */
431                 if (trace_boot_compose_hist_cmd(node, buf, size) == 0) {
432                         tmp = kstrdup(buf, GFP_KERNEL);
433                         if (trigger_process_regex(file, buf) < 0)
434                                 pr_err("Failed to apply hist trigger: %s\n", tmp);
435                         kfree(tmp);
436                 }
437         }
438
439         if (xbc_node_find_child(hnode, "keys")) {
440                 if (trace_boot_compose_hist_cmd(hnode, buf, size) == 0) {
441                         tmp = kstrdup(buf, GFP_KERNEL);
442                         if (trigger_process_regex(file, buf) < 0)
443                                 pr_err("Failed to apply hist trigger: %s\n", tmp);
444                         kfree(tmp);
445                 }
446         }
447 }
448 #else
449 static void __init
450 trace_boot_init_histograms(struct trace_event_file *file,
451                            struct xbc_node *hnode, char *buf, size_t size)
452 {
453         /* do nothing */
454 }
455 #endif
456
457 static void __init
458 trace_boot_init_one_event(struct trace_array *tr, struct xbc_node *gnode,
459                           struct xbc_node *enode)
460 {
461         struct trace_event_file *file;
462         struct xbc_node *anode;
463         char buf[MAX_BUF_LEN];
464         const char *p, *group, *event;
465
466         group = xbc_node_get_data(gnode);
467         event = xbc_node_get_data(enode);
468
469         if (!strcmp(group, "kprobes"))
470                 if (trace_boot_add_kprobe_event(enode, event) < 0)
471                         return;
472         if (!strcmp(group, "synthetic"))
473                 if (trace_boot_add_synth_event(enode, event) < 0)
474                         return;
475
476         mutex_lock(&event_mutex);
477         file = find_event_file(tr, group, event);
478         if (!file) {
479                 pr_err("Failed to find event: %s:%s\n", group, event);
480                 goto out;
481         }
482
483         p = xbc_node_find_value(enode, "filter", NULL);
484         if (p && *p != '\0') {
485                 if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf))
486                         pr_err("filter string is too long: %s\n", p);
487                 else if (apply_event_filter(file, buf) < 0)
488                         pr_err("Failed to apply filter: %s\n", buf);
489         }
490
491         if (IS_ENABLED(CONFIG_HIST_TRIGGERS)) {
492                 xbc_node_for_each_array_value(enode, "actions", anode, p) {
493                         if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf))
494                                 pr_err("action string is too long: %s\n", p);
495                         else if (trigger_process_regex(file, buf) < 0)
496                                 pr_err("Failed to apply an action: %s\n", p);
497                 }
498                 anode = xbc_node_find_child(enode, "hist");
499                 if (anode)
500                         trace_boot_init_histograms(file, anode, buf, ARRAY_SIZE(buf));
501         } else if (xbc_node_find_value(enode, "actions", NULL))
502                 pr_err("Failed to apply event actions because CONFIG_HIST_TRIGGERS is not set.\n");
503
504         if (xbc_node_find_value(enode, "enable", NULL)) {
505                 if (trace_event_enable_disable(file, 1, 0) < 0)
506                         pr_err("Failed to enable event node: %s:%s\n",
507                                 group, event);
508         }
509 out:
510         mutex_unlock(&event_mutex);
511 }
512
513 static void __init
514 trace_boot_init_events(struct trace_array *tr, struct xbc_node *node)
515 {
516         struct xbc_node *gnode, *enode;
517         bool enable, enable_all = false;
518         const char *data;
519
520         node = xbc_node_find_child(node, "event");
521         if (!node)
522                 return;
523         /* per-event key starts with "event.GROUP.EVENT" */
524         xbc_node_for_each_subkey(node, gnode) {
525                 data = xbc_node_get_data(gnode);
526                 if (!strcmp(data, "enable")) {
527                         enable_all = true;
528                         continue;
529                 }
530                 enable = false;
531                 xbc_node_for_each_subkey(gnode, enode) {
532                         data = xbc_node_get_data(enode);
533                         if (!strcmp(data, "enable")) {
534                                 enable = true;
535                                 continue;
536                         }
537                         trace_boot_init_one_event(tr, gnode, enode);
538                 }
539                 /* Event enablement must be done after event settings */
540                 if (enable) {
541                         data = xbc_node_get_data(gnode);
542                         trace_array_set_clr_event(tr, data, NULL, true);
543                 }
544         }
545         /* Ditto */
546         if (enable_all)
547                 trace_array_set_clr_event(tr, NULL, NULL, true);
548 }
549 #else
550 #define trace_boot_enable_events(tr, node) do {} while (0)
551 #define trace_boot_init_events(tr, node) do {} while (0)
552 #endif
553
554 #ifdef CONFIG_DYNAMIC_FTRACE
555 static void __init
556 trace_boot_set_ftrace_filter(struct trace_array *tr, struct xbc_node *node)
557 {
558         struct xbc_node *anode;
559         const char *p;
560         char *q;
561
562         xbc_node_for_each_array_value(node, "ftrace.filters", anode, p) {
563                 q = kstrdup(p, GFP_KERNEL);
564                 if (!q)
565                         return;
566                 if (ftrace_set_filter(tr->ops, q, strlen(q), 0) < 0)
567                         pr_err("Failed to add %s to ftrace filter\n", p);
568                 else
569                         ftrace_filter_param = true;
570                 kfree(q);
571         }
572         xbc_node_for_each_array_value(node, "ftrace.notraces", anode, p) {
573                 q = kstrdup(p, GFP_KERNEL);
574                 if (!q)
575                         return;
576                 if (ftrace_set_notrace(tr->ops, q, strlen(q), 0) < 0)
577                         pr_err("Failed to add %s to ftrace filter\n", p);
578                 else
579                         ftrace_filter_param = true;
580                 kfree(q);
581         }
582 }
583 #else
584 #define trace_boot_set_ftrace_filter(tr, node) do {} while (0)
585 #endif
586
587 static void __init
588 trace_boot_enable_tracer(struct trace_array *tr, struct xbc_node *node)
589 {
590         const char *p;
591
592         trace_boot_set_ftrace_filter(tr, node);
593
594         p = xbc_node_find_value(node, "tracer", NULL);
595         if (p && *p != '\0') {
596                 if (tracing_set_tracer(tr, p) < 0)
597                         pr_err("Failed to set given tracer: %s\n", p);
598         }
599
600         /* Since tracer can free snapshot buffer, allocate snapshot here.*/
601         if (xbc_node_find_value(node, "alloc_snapshot", NULL)) {
602                 if (tracing_alloc_snapshot_instance(tr) < 0)
603                         pr_err("Failed to allocate snapshot buffer\n");
604         }
605 }
606
607 static void __init
608 trace_boot_init_one_instance(struct trace_array *tr, struct xbc_node *node)
609 {
610         trace_boot_set_instance_options(tr, node);
611         trace_boot_init_events(tr, node);
612         trace_boot_enable_events(tr, node);
613         trace_boot_enable_tracer(tr, node);
614 }
615
616 static void __init
617 trace_boot_init_instances(struct xbc_node *node)
618 {
619         struct xbc_node *inode;
620         struct trace_array *tr;
621         const char *p;
622
623         node = xbc_node_find_child(node, "instance");
624         if (!node)
625                 return;
626
627         xbc_node_for_each_subkey(node, inode) {
628                 p = xbc_node_get_data(inode);
629                 if (!p || *p == '\0')
630                         continue;
631
632                 tr = trace_array_get_by_name(p);
633                 if (!tr) {
634                         pr_err("Failed to get trace instance %s\n", p);
635                         continue;
636                 }
637                 trace_boot_init_one_instance(tr, inode);
638                 trace_array_put(tr);
639         }
640 }
641
642 static int __init trace_boot_init(void)
643 {
644         struct xbc_node *trace_node;
645         struct trace_array *tr;
646
647         trace_node = xbc_find_node("ftrace");
648         if (!trace_node)
649                 return 0;
650
651         tr = top_trace_array();
652         if (!tr)
653                 return 0;
654
655         /* Global trace array is also one instance */
656         trace_boot_init_one_instance(tr, trace_node);
657         trace_boot_init_instances(trace_node);
658
659         disable_tracing_selftest("running boot-time tracing");
660
661         return 0;
662 }
663 /*
664  * Start tracing at the end of core-initcall, so that it starts tracing
665  * from the beginning of postcore_initcall.
666  */
667 core_initcall_sync(trace_boot_init);