xref: /linux-6.15/kernel/trace/trace_boot.c (revision e66ed86c)
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 *knode, *anode;
223 	const char *p;
224 	char sep;
225 
226 	knode = xbc_node_find_child(hnode, key);
227 	if (knode) {
228 		anode = xbc_node_get_child(knode);
229 		if (!anode) {
230 			pr_err("hist.%s requires value(s).\n", key);
231 			return -EINVAL;
232 		}
233 
234 		append_printf(bufp, end, ":%s", key);
235 		sep = '=';
236 		xbc_array_for_each_value(anode, p) {
237 			append_printf(bufp, end, "%c%s", sep, p);
238 			if (sep == '=')
239 				sep = ',';
240 		}
241 	} else
242 		return -ENOENT;
243 
244 	return 0;
245 }
246 
247 static int __init
248 trace_boot_hist_add_handler(struct xbc_node *hnode, char **bufp,
249 			    char *end, 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)", xbc_node_get_data(hnode), 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 /*
298  * Histogram boottime tracing syntax.
299  *
300  * ftrace.[instance.INSTANCE.]event.GROUP.EVENT.hist {
301  *	keys = <KEY>[,...]
302  *	values = <VAL>[,...]
303  *	sort = <SORT-KEY>[,...]
304  *	size = <ENTRIES>
305  *	name = <HISTNAME>
306  *	var { <VAR> = <EXPR> ... }
307  *	pause|continue|clear
308  *	onmax|onchange { var = <VAR>; <ACTION> [= <PARAM>] }
309  *	onmatch { event = <EVENT>; <ACTION> [= <PARAM>] }
310  *	filter = <FILTER>
311  * }
312  *
313  * Where <ACTION> are;
314  *
315  *	trace = <EVENT>, <ARG1>[, ...]
316  *	save = <ARG1>[, ...]
317  *	snapshot
318  */
319 static int __init
320 trace_boot_compose_hist_cmd(struct xbc_node *hnode, char *buf, size_t size)
321 {
322 	struct xbc_node *node, *knode;
323 	char *end = buf + size;
324 	const char *p;
325 	int ret = 0;
326 
327 	append_printf(&buf, end, "hist");
328 
329 	ret = trace_boot_hist_add_array(hnode, &buf, end, "keys");
330 	if (ret < 0) {
331 		if (ret == -ENOENT)
332 			pr_err("hist requires keys.\n");
333 		return -EINVAL;
334 	}
335 
336 	ret = trace_boot_hist_add_array(hnode, &buf, end, "values");
337 	if (ret == -EINVAL)
338 		return ret;
339 	ret = trace_boot_hist_add_array(hnode, &buf, end, "sort");
340 	if (ret == -EINVAL)
341 		return ret;
342 
343 	p = xbc_node_find_value(hnode, "size", NULL);
344 	if (p)
345 		append_printf(&buf, end, ":size=%s", p);
346 
347 	p = xbc_node_find_value(hnode, "name", NULL);
348 	if (p)
349 		append_printf(&buf, end, ":name=%s", p);
350 
351 	node = xbc_node_find_child(hnode, "var");
352 	if (node) {
353 		xbc_node_for_each_key_value(node, knode, p) {
354 			/* Expression must not include spaces. */
355 			append_printf(&buf, end, ":%s=",
356 				      xbc_node_get_data(knode));
357 			append_str_nospace(&buf, end, p);
358 		}
359 	}
360 
361 	/* Histogram control attributes (mutual exclusive) */
362 	if (xbc_node_find_child(hnode, "pause"))
363 		append_printf(&buf, end, ":pause");
364 	else if (xbc_node_find_child(hnode, "continue"))
365 		append_printf(&buf, end, ":continue");
366 	else if (xbc_node_find_child(hnode, "clear"))
367 		append_printf(&buf, end, ":clear");
368 
369 	/* Histogram handler and actions */
370 	node = xbc_node_find_child(hnode, "onmax");
371 	if (node && trace_boot_hist_add_handler(node, &buf, end, "var") < 0)
372 		return -EINVAL;
373 	node = xbc_node_find_child(hnode, "onchange");
374 	if (node && trace_boot_hist_add_handler(node, &buf, end, "var") < 0)
375 		return -EINVAL;
376 	node = xbc_node_find_child(hnode, "onmatch");
377 	if (node && trace_boot_hist_add_handler(node, &buf, end, "event") < 0)
378 		return -EINVAL;
379 
380 	p = xbc_node_find_value(hnode, "filter", NULL);
381 	if (p)
382 		append_printf(&buf, end, " if %s", p);
383 
384 	if (buf == end) {
385 		pr_err("hist exceeds the max command length.\n");
386 		return -E2BIG;
387 	}
388 
389 	return 0;
390 }
391 #else
392 static int __init
393 trace_boot_compose_hist_cmd(struct xbc_node *hnode, char *buf, size_t size)
394 {
395 	return -EOPNOTSUPP;
396 }
397 #endif
398 
399 static void __init
400 trace_boot_init_one_event(struct trace_array *tr, struct xbc_node *gnode,
401 			  struct xbc_node *enode)
402 {
403 	struct trace_event_file *file;
404 	struct xbc_node *anode;
405 	char buf[MAX_BUF_LEN];
406 	const char *p, *group, *event;
407 
408 	group = xbc_node_get_data(gnode);
409 	event = xbc_node_get_data(enode);
410 
411 	if (!strcmp(group, "kprobes"))
412 		if (trace_boot_add_kprobe_event(enode, event) < 0)
413 			return;
414 	if (!strcmp(group, "synthetic"))
415 		if (trace_boot_add_synth_event(enode, event) < 0)
416 			return;
417 
418 	mutex_lock(&event_mutex);
419 	file = find_event_file(tr, group, event);
420 	if (!file) {
421 		pr_err("Failed to find event: %s:%s\n", group, event);
422 		goto out;
423 	}
424 
425 	p = xbc_node_find_value(enode, "filter", NULL);
426 	if (p && *p != '\0') {
427 		if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf))
428 			pr_err("filter string is too long: %s\n", p);
429 		else if (apply_event_filter(file, buf) < 0)
430 			pr_err("Failed to apply filter: %s\n", buf);
431 	}
432 
433 	if (IS_ENABLED(CONFIG_HIST_TRIGGERS)) {
434 		xbc_node_for_each_array_value(enode, "actions", anode, p) {
435 			if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf))
436 				pr_err("action string is too long: %s\n", p);
437 			else if (trigger_process_regex(file, buf) < 0)
438 				pr_err("Failed to apply an action: %s\n", buf);
439 		}
440 		anode = xbc_node_find_child(enode, "hist");
441 		if (anode &&
442 		    trace_boot_compose_hist_cmd(anode, buf, ARRAY_SIZE(buf)) == 0) {
443 			if (trigger_process_regex(file, buf) < 0)
444 				pr_err("Failed to apply hist trigger: %s\n", buf);
445 		}
446 	} else if (xbc_node_find_value(enode, "actions", NULL))
447 		pr_err("Failed to apply event actions because CONFIG_HIST_TRIGGERS is not set.\n");
448 
449 	if (xbc_node_find_value(enode, "enable", NULL)) {
450 		if (trace_event_enable_disable(file, 1, 0) < 0)
451 			pr_err("Failed to enable event node: %s:%s\n",
452 				group, event);
453 	}
454 out:
455 	mutex_unlock(&event_mutex);
456 }
457 
458 static void __init
459 trace_boot_init_events(struct trace_array *tr, struct xbc_node *node)
460 {
461 	struct xbc_node *gnode, *enode;
462 	bool enable, enable_all = false;
463 	const char *data;
464 
465 	node = xbc_node_find_child(node, "event");
466 	if (!node)
467 		return;
468 	/* per-event key starts with "event.GROUP.EVENT" */
469 	xbc_node_for_each_child(node, gnode) {
470 		data = xbc_node_get_data(gnode);
471 		if (!strcmp(data, "enable")) {
472 			enable_all = true;
473 			continue;
474 		}
475 		enable = false;
476 		xbc_node_for_each_child(gnode, enode) {
477 			data = xbc_node_get_data(enode);
478 			if (!strcmp(data, "enable")) {
479 				enable = true;
480 				continue;
481 			}
482 			trace_boot_init_one_event(tr, gnode, enode);
483 		}
484 		/* Event enablement must be done after event settings */
485 		if (enable) {
486 			data = xbc_node_get_data(gnode);
487 			trace_array_set_clr_event(tr, data, NULL, true);
488 		}
489 	}
490 	/* Ditto */
491 	if (enable_all)
492 		trace_array_set_clr_event(tr, NULL, NULL, true);
493 }
494 #else
495 #define trace_boot_enable_events(tr, node) do {} while (0)
496 #define trace_boot_init_events(tr, node) do {} while (0)
497 #endif
498 
499 #ifdef CONFIG_DYNAMIC_FTRACE
500 static void __init
501 trace_boot_set_ftrace_filter(struct trace_array *tr, struct xbc_node *node)
502 {
503 	struct xbc_node *anode;
504 	const char *p;
505 	char *q;
506 
507 	xbc_node_for_each_array_value(node, "ftrace.filters", anode, p) {
508 		q = kstrdup(p, GFP_KERNEL);
509 		if (!q)
510 			return;
511 		if (ftrace_set_filter(tr->ops, q, strlen(q), 0) < 0)
512 			pr_err("Failed to add %s to ftrace filter\n", p);
513 		else
514 			ftrace_filter_param = true;
515 		kfree(q);
516 	}
517 	xbc_node_for_each_array_value(node, "ftrace.notraces", anode, p) {
518 		q = kstrdup(p, GFP_KERNEL);
519 		if (!q)
520 			return;
521 		if (ftrace_set_notrace(tr->ops, q, strlen(q), 0) < 0)
522 			pr_err("Failed to add %s to ftrace filter\n", p);
523 		else
524 			ftrace_filter_param = true;
525 		kfree(q);
526 	}
527 }
528 #else
529 #define trace_boot_set_ftrace_filter(tr, node) do {} while (0)
530 #endif
531 
532 static void __init
533 trace_boot_enable_tracer(struct trace_array *tr, struct xbc_node *node)
534 {
535 	const char *p;
536 
537 	trace_boot_set_ftrace_filter(tr, node);
538 
539 	p = xbc_node_find_value(node, "tracer", NULL);
540 	if (p && *p != '\0') {
541 		if (tracing_set_tracer(tr, p) < 0)
542 			pr_err("Failed to set given tracer: %s\n", p);
543 	}
544 
545 	/* Since tracer can free snapshot buffer, allocate snapshot here.*/
546 	if (xbc_node_find_value(node, "alloc_snapshot", NULL)) {
547 		if (tracing_alloc_snapshot_instance(tr) < 0)
548 			pr_err("Failed to allocate snapshot buffer\n");
549 	}
550 }
551 
552 static void __init
553 trace_boot_init_one_instance(struct trace_array *tr, struct xbc_node *node)
554 {
555 	trace_boot_set_instance_options(tr, node);
556 	trace_boot_init_events(tr, node);
557 	trace_boot_enable_events(tr, node);
558 	trace_boot_enable_tracer(tr, node);
559 }
560 
561 static void __init
562 trace_boot_init_instances(struct xbc_node *node)
563 {
564 	struct xbc_node *inode;
565 	struct trace_array *tr;
566 	const char *p;
567 
568 	node = xbc_node_find_child(node, "instance");
569 	if (!node)
570 		return;
571 
572 	xbc_node_for_each_child(node, inode) {
573 		p = xbc_node_get_data(inode);
574 		if (!p || *p == '\0')
575 			continue;
576 
577 		tr = trace_array_get_by_name(p);
578 		if (!tr) {
579 			pr_err("Failed to get trace instance %s\n", p);
580 			continue;
581 		}
582 		trace_boot_init_one_instance(tr, inode);
583 		trace_array_put(tr);
584 	}
585 }
586 
587 static int __init trace_boot_init(void)
588 {
589 	struct xbc_node *trace_node;
590 	struct trace_array *tr;
591 
592 	trace_node = xbc_find_node("ftrace");
593 	if (!trace_node)
594 		return 0;
595 
596 	tr = top_trace_array();
597 	if (!tr)
598 		return 0;
599 
600 	/* Global trace array is also one instance */
601 	trace_boot_init_one_instance(tr, trace_node);
602 	trace_boot_init_instances(trace_node);
603 
604 	disable_tracing_selftest("running boot-time tracing");
605 
606 	return 0;
607 }
608 /*
609  * Start tracing at the end of core-initcall, so that it starts tracing
610  * from the beginning of postcore_initcall.
611  */
612 core_initcall_sync(trace_boot_init);
613