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_one_handler(struct xbc_node *hnode, char **bufp, 249 char *end, const char *handler, 250 const char *param) 251 { 252 struct xbc_node *knode, *anode; 253 const char *p; 254 char sep; 255 256 /* Compose 'handler' parameter */ 257 p = xbc_node_find_value(hnode, param, NULL); 258 if (!p) { 259 pr_err("hist.%s requires '%s' option.\n", 260 xbc_node_get_data(hnode), param); 261 return -EINVAL; 262 } 263 append_printf(bufp, end, ":%s(%s)", handler, p); 264 265 /* Compose 'action' parameter */ 266 knode = xbc_node_find_child(hnode, "trace"); 267 if (!knode) 268 knode = xbc_node_find_child(hnode, "save"); 269 270 if (knode) { 271 anode = xbc_node_get_child(knode); 272 if (!anode || !xbc_node_is_value(anode)) { 273 pr_err("hist.%s.%s requires value(s).\n", 274 xbc_node_get_data(hnode), 275 xbc_node_get_data(knode)); 276 return -EINVAL; 277 } 278 279 append_printf(bufp, end, ".%s", xbc_node_get_data(knode)); 280 sep = '('; 281 xbc_array_for_each_value(anode, p) { 282 append_printf(bufp, end, "%c%s", sep, p); 283 if (sep == '(') 284 sep = ','; 285 } 286 append_printf(bufp, end, ")"); 287 } else if (xbc_node_find_child(hnode, "snapshot")) { 288 append_printf(bufp, end, ".snapshot()"); 289 } else { 290 pr_err("hist.%s requires an action.\n", 291 xbc_node_get_data(hnode)); 292 return -EINVAL; 293 } 294 295 return 0; 296 } 297 298 static int __init 299 trace_boot_hist_add_handlers(struct xbc_node *hnode, char **bufp, 300 char *end, const char *param) 301 { 302 struct xbc_node *node; 303 const char *p, *handler; 304 int ret; 305 306 handler = xbc_node_get_data(hnode); 307 308 xbc_node_for_each_subkey(hnode, node) { 309 p = xbc_node_get_data(node); 310 if (!isdigit(p[0])) 311 continue; 312 /* All digit started node should be instances. */ 313 ret = trace_boot_hist_add_one_handler(node, bufp, end, handler, param); 314 if (ret < 0) 315 break; 316 } 317 318 if (xbc_node_find_child(hnode, param)) 319 ret = trace_boot_hist_add_one_handler(hnode, bufp, end, handler, param); 320 321 return ret; 322 } 323 324 /* 325 * Histogram boottime tracing syntax. 326 * 327 * ftrace.[instance.INSTANCE.]event.GROUP.EVENT.hist[.N] { 328 * keys = <KEY>[,...] 329 * values = <VAL>[,...] 330 * sort = <SORT-KEY>[,...] 331 * size = <ENTRIES> 332 * name = <HISTNAME> 333 * var { <VAR> = <EXPR> ... } 334 * pause|continue|clear 335 * onmax|onchange[.N] { var = <VAR>; <ACTION> [= <PARAM>] } 336 * onmatch[.N] { event = <EVENT>; <ACTION> [= <PARAM>] } 337 * filter = <FILTER> 338 * } 339 * 340 * Where <ACTION> are; 341 * 342 * trace = <EVENT>, <ARG1>[, ...] 343 * save = <ARG1>[, ...] 344 * snapshot 345 */ 346 static int __init 347 trace_boot_compose_hist_cmd(struct xbc_node *hnode, char *buf, size_t size) 348 { 349 struct xbc_node *node, *knode; 350 char *end = buf + size; 351 const char *p; 352 int ret = 0; 353 354 append_printf(&buf, end, "hist"); 355 356 ret = trace_boot_hist_add_array(hnode, &buf, end, "keys"); 357 if (ret < 0) { 358 if (ret == -ENOENT) 359 pr_err("hist requires keys.\n"); 360 return -EINVAL; 361 } 362 363 ret = trace_boot_hist_add_array(hnode, &buf, end, "values"); 364 if (ret == -EINVAL) 365 return ret; 366 ret = trace_boot_hist_add_array(hnode, &buf, end, "sort"); 367 if (ret == -EINVAL) 368 return ret; 369 370 p = xbc_node_find_value(hnode, "size", NULL); 371 if (p) 372 append_printf(&buf, end, ":size=%s", p); 373 374 p = xbc_node_find_value(hnode, "name", NULL); 375 if (p) 376 append_printf(&buf, end, ":name=%s", p); 377 378 node = xbc_node_find_child(hnode, "var"); 379 if (node) { 380 xbc_node_for_each_key_value(node, knode, p) { 381 /* Expression must not include spaces. */ 382 append_printf(&buf, end, ":%s=", 383 xbc_node_get_data(knode)); 384 append_str_nospace(&buf, end, p); 385 } 386 } 387 388 /* Histogram control attributes (mutual exclusive) */ 389 if (xbc_node_find_child(hnode, "pause")) 390 append_printf(&buf, end, ":pause"); 391 else if (xbc_node_find_child(hnode, "continue")) 392 append_printf(&buf, end, ":continue"); 393 else if (xbc_node_find_child(hnode, "clear")) 394 append_printf(&buf, end, ":clear"); 395 396 /* Histogram handler and actions */ 397 node = xbc_node_find_child(hnode, "onmax"); 398 if (node && trace_boot_hist_add_handlers(node, &buf, end, "var") < 0) 399 return -EINVAL; 400 node = xbc_node_find_child(hnode, "onchange"); 401 if (node && trace_boot_hist_add_handlers(node, &buf, end, "var") < 0) 402 return -EINVAL; 403 node = xbc_node_find_child(hnode, "onmatch"); 404 if (node && trace_boot_hist_add_handlers(node, &buf, end, "event") < 0) 405 return -EINVAL; 406 407 p = xbc_node_find_value(hnode, "filter", NULL); 408 if (p) 409 append_printf(&buf, end, " if %s", p); 410 411 if (buf == end) { 412 pr_err("hist exceeds the max command length.\n"); 413 return -E2BIG; 414 } 415 416 return 0; 417 } 418 419 static void __init 420 trace_boot_init_histograms(struct trace_event_file *file, 421 struct xbc_node *hnode, char *buf, size_t size) 422 { 423 struct xbc_node *node; 424 const char *p; 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 if (trigger_process_regex(file, buf) < 0) 433 pr_err("Failed to apply hist trigger: %s\n", buf); 434 } 435 } 436 437 if (xbc_node_find_child(hnode, "keys")) { 438 if (trace_boot_compose_hist_cmd(hnode, buf, size) == 0) 439 if (trigger_process_regex(file, buf) < 0) 440 pr_err("Failed to apply hist trigger: %s\n", buf); 441 } 442 } 443 #else 444 static void __init 445 trace_boot_init_histograms(struct trace_event_file *file, 446 struct xbc_node *hnode, char *buf, size_t size) 447 { 448 /* do nothing */ 449 } 450 #endif 451 452 static void __init 453 trace_boot_init_one_event(struct trace_array *tr, struct xbc_node *gnode, 454 struct xbc_node *enode) 455 { 456 struct trace_event_file *file; 457 struct xbc_node *anode; 458 char buf[MAX_BUF_LEN]; 459 const char *p, *group, *event; 460 461 group = xbc_node_get_data(gnode); 462 event = xbc_node_get_data(enode); 463 464 if (!strcmp(group, "kprobes")) 465 if (trace_boot_add_kprobe_event(enode, event) < 0) 466 return; 467 if (!strcmp(group, "synthetic")) 468 if (trace_boot_add_synth_event(enode, event) < 0) 469 return; 470 471 mutex_lock(&event_mutex); 472 file = find_event_file(tr, group, event); 473 if (!file) { 474 pr_err("Failed to find event: %s:%s\n", group, event); 475 goto out; 476 } 477 478 p = xbc_node_find_value(enode, "filter", NULL); 479 if (p && *p != '\0') { 480 if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf)) 481 pr_err("filter string is too long: %s\n", p); 482 else if (apply_event_filter(file, buf) < 0) 483 pr_err("Failed to apply filter: %s\n", buf); 484 } 485 486 if (IS_ENABLED(CONFIG_HIST_TRIGGERS)) { 487 xbc_node_for_each_array_value(enode, "actions", anode, p) { 488 if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf)) 489 pr_err("action string is too long: %s\n", p); 490 else if (trigger_process_regex(file, buf) < 0) 491 pr_err("Failed to apply an action: %s\n", buf); 492 } 493 anode = xbc_node_find_child(enode, "hist"); 494 if (anode) 495 trace_boot_init_histograms(file, anode, buf, ARRAY_SIZE(buf)); 496 } else if (xbc_node_find_value(enode, "actions", NULL)) 497 pr_err("Failed to apply event actions because CONFIG_HIST_TRIGGERS is not set.\n"); 498 499 if (xbc_node_find_value(enode, "enable", NULL)) { 500 if (trace_event_enable_disable(file, 1, 0) < 0) 501 pr_err("Failed to enable event node: %s:%s\n", 502 group, event); 503 } 504 out: 505 mutex_unlock(&event_mutex); 506 } 507 508 static void __init 509 trace_boot_init_events(struct trace_array *tr, struct xbc_node *node) 510 { 511 struct xbc_node *gnode, *enode; 512 bool enable, enable_all = false; 513 const char *data; 514 515 node = xbc_node_find_child(node, "event"); 516 if (!node) 517 return; 518 /* per-event key starts with "event.GROUP.EVENT" */ 519 xbc_node_for_each_child(node, gnode) { 520 data = xbc_node_get_data(gnode); 521 if (!strcmp(data, "enable")) { 522 enable_all = true; 523 continue; 524 } 525 enable = false; 526 xbc_node_for_each_child(gnode, enode) { 527 data = xbc_node_get_data(enode); 528 if (!strcmp(data, "enable")) { 529 enable = true; 530 continue; 531 } 532 trace_boot_init_one_event(tr, gnode, enode); 533 } 534 /* Event enablement must be done after event settings */ 535 if (enable) { 536 data = xbc_node_get_data(gnode); 537 trace_array_set_clr_event(tr, data, NULL, true); 538 } 539 } 540 /* Ditto */ 541 if (enable_all) 542 trace_array_set_clr_event(tr, NULL, NULL, true); 543 } 544 #else 545 #define trace_boot_enable_events(tr, node) do {} while (0) 546 #define trace_boot_init_events(tr, node) do {} while (0) 547 #endif 548 549 #ifdef CONFIG_DYNAMIC_FTRACE 550 static void __init 551 trace_boot_set_ftrace_filter(struct trace_array *tr, struct xbc_node *node) 552 { 553 struct xbc_node *anode; 554 const char *p; 555 char *q; 556 557 xbc_node_for_each_array_value(node, "ftrace.filters", anode, p) { 558 q = kstrdup(p, GFP_KERNEL); 559 if (!q) 560 return; 561 if (ftrace_set_filter(tr->ops, q, strlen(q), 0) < 0) 562 pr_err("Failed to add %s to ftrace filter\n", p); 563 else 564 ftrace_filter_param = true; 565 kfree(q); 566 } 567 xbc_node_for_each_array_value(node, "ftrace.notraces", anode, p) { 568 q = kstrdup(p, GFP_KERNEL); 569 if (!q) 570 return; 571 if (ftrace_set_notrace(tr->ops, q, strlen(q), 0) < 0) 572 pr_err("Failed to add %s to ftrace filter\n", p); 573 else 574 ftrace_filter_param = true; 575 kfree(q); 576 } 577 } 578 #else 579 #define trace_boot_set_ftrace_filter(tr, node) do {} while (0) 580 #endif 581 582 static void __init 583 trace_boot_enable_tracer(struct trace_array *tr, struct xbc_node *node) 584 { 585 const char *p; 586 587 trace_boot_set_ftrace_filter(tr, node); 588 589 p = xbc_node_find_value(node, "tracer", NULL); 590 if (p && *p != '\0') { 591 if (tracing_set_tracer(tr, p) < 0) 592 pr_err("Failed to set given tracer: %s\n", p); 593 } 594 595 /* Since tracer can free snapshot buffer, allocate snapshot here.*/ 596 if (xbc_node_find_value(node, "alloc_snapshot", NULL)) { 597 if (tracing_alloc_snapshot_instance(tr) < 0) 598 pr_err("Failed to allocate snapshot buffer\n"); 599 } 600 } 601 602 static void __init 603 trace_boot_init_one_instance(struct trace_array *tr, struct xbc_node *node) 604 { 605 trace_boot_set_instance_options(tr, node); 606 trace_boot_init_events(tr, node); 607 trace_boot_enable_events(tr, node); 608 trace_boot_enable_tracer(tr, node); 609 } 610 611 static void __init 612 trace_boot_init_instances(struct xbc_node *node) 613 { 614 struct xbc_node *inode; 615 struct trace_array *tr; 616 const char *p; 617 618 node = xbc_node_find_child(node, "instance"); 619 if (!node) 620 return; 621 622 xbc_node_for_each_child(node, inode) { 623 p = xbc_node_get_data(inode); 624 if (!p || *p == '\0') 625 continue; 626 627 tr = trace_array_get_by_name(p); 628 if (!tr) { 629 pr_err("Failed to get trace instance %s\n", p); 630 continue; 631 } 632 trace_boot_init_one_instance(tr, inode); 633 trace_array_put(tr); 634 } 635 } 636 637 static int __init trace_boot_init(void) 638 { 639 struct xbc_node *trace_node; 640 struct trace_array *tr; 641 642 trace_node = xbc_find_node("ftrace"); 643 if (!trace_node) 644 return 0; 645 646 tr = top_trace_array(); 647 if (!tr) 648 return 0; 649 650 /* Global trace array is also one instance */ 651 trace_boot_init_one_instance(tr, trace_node); 652 trace_boot_init_instances(trace_node); 653 654 disable_tracing_selftest("running boot-time tracing"); 655 656 return 0; 657 } 658 /* 659 * Start tracing at the end of core-initcall, so that it starts tracing 660 * from the beginning of postcore_initcall. 661 */ 662 core_initcall_sync(trace_boot_init); 663