1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * trace irqs off critical timings 4 * 5 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com> 6 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com> 7 * 8 * From code in the latency_tracer, that is: 9 * 10 * Copyright (C) 2004-2006 Ingo Molnar 11 * Copyright (C) 2004 Nadia Yvette Chambers 12 */ 13 #include <linux/kallsyms.h> 14 #include <linux/uaccess.h> 15 #include <linux/module.h> 16 #include <linux/ftrace.h> 17 18 #include "trace.h" 19 20 #include <trace/events/preemptirq.h> 21 22 #if defined(CONFIG_IRQSOFF_TRACER) || defined(CONFIG_PREEMPT_TRACER) 23 static struct trace_array *irqsoff_trace __read_mostly; 24 static int tracer_enabled __read_mostly; 25 26 static DEFINE_PER_CPU(int, tracing_cpu); 27 28 static DEFINE_RAW_SPINLOCK(max_trace_lock); 29 30 enum { 31 TRACER_IRQS_OFF = (1 << 1), 32 TRACER_PREEMPT_OFF = (1 << 2), 33 }; 34 35 static int trace_type __read_mostly; 36 37 static int save_flags; 38 39 static void stop_irqsoff_tracer(struct trace_array *tr, int graph); 40 static int start_irqsoff_tracer(struct trace_array *tr, int graph); 41 42 #ifdef CONFIG_PREEMPT_TRACER 43 static inline int 44 preempt_trace(int pc) 45 { 46 return ((trace_type & TRACER_PREEMPT_OFF) && pc); 47 } 48 #else 49 # define preempt_trace(pc) (0) 50 #endif 51 52 #ifdef CONFIG_IRQSOFF_TRACER 53 static inline int 54 irq_trace(void) 55 { 56 return ((trace_type & TRACER_IRQS_OFF) && 57 irqs_disabled()); 58 } 59 #else 60 # define irq_trace() (0) 61 #endif 62 63 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 64 static int irqsoff_display_graph(struct trace_array *tr, int set); 65 # define is_graph(tr) ((tr)->trace_flags & TRACE_ITER_DISPLAY_GRAPH) 66 #else 67 static inline int irqsoff_display_graph(struct trace_array *tr, int set) 68 { 69 return -EINVAL; 70 } 71 # define is_graph(tr) false 72 #endif 73 74 /* 75 * Sequence count - we record it when starting a measurement and 76 * skip the latency if the sequence has changed - some other section 77 * did a maximum and could disturb our measurement with serial console 78 * printouts, etc. Truly coinciding maximum latencies should be rare 79 * and what happens together happens separately as well, so this doesn't 80 * decrease the validity of the maximum found: 81 */ 82 static __cacheline_aligned_in_smp unsigned long max_sequence; 83 84 #ifdef CONFIG_FUNCTION_TRACER 85 /* 86 * Prologue for the preempt and irqs off function tracers. 87 * 88 * Returns 1 if it is OK to continue, and data->disabled is 89 * incremented. 90 * 0 if the trace is to be ignored, and data->disabled 91 * is kept the same. 92 * 93 * Note, this function is also used outside this ifdef but 94 * inside the #ifdef of the function graph tracer below. 95 * This is OK, since the function graph tracer is 96 * dependent on the function tracer. 97 */ 98 static int func_prolog_dec(struct trace_array *tr, 99 struct trace_array_cpu **data, 100 unsigned long *flags) 101 { 102 long disabled; 103 int cpu; 104 105 /* 106 * Does not matter if we preempt. We test the flags 107 * afterward, to see if irqs are disabled or not. 108 * If we preempt and get a false positive, the flags 109 * test will fail. 110 */ 111 cpu = raw_smp_processor_id(); 112 if (likely(!per_cpu(tracing_cpu, cpu))) 113 return 0; 114 115 local_save_flags(*flags); 116 /* 117 * Slight chance to get a false positive on tracing_cpu, 118 * although I'm starting to think there isn't a chance. 119 * Leave this for now just to be paranoid. 120 */ 121 if (!irqs_disabled_flags(*flags) && !preempt_count()) 122 return 0; 123 124 *data = per_cpu_ptr(tr->trace_buffer.data, cpu); 125 disabled = atomic_inc_return(&(*data)->disabled); 126 127 if (likely(disabled == 1)) 128 return 1; 129 130 atomic_dec(&(*data)->disabled); 131 132 return 0; 133 } 134 135 /* 136 * irqsoff uses its own tracer function to keep the overhead down: 137 */ 138 static void 139 irqsoff_tracer_call(unsigned long ip, unsigned long parent_ip, 140 struct ftrace_ops *op, struct pt_regs *pt_regs) 141 { 142 struct trace_array *tr = irqsoff_trace; 143 struct trace_array_cpu *data; 144 unsigned long flags; 145 146 if (!func_prolog_dec(tr, &data, &flags)) 147 return; 148 149 trace_function(tr, ip, parent_ip, flags, preempt_count()); 150 151 atomic_dec(&data->disabled); 152 } 153 #endif /* CONFIG_FUNCTION_TRACER */ 154 155 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 156 static int irqsoff_display_graph(struct trace_array *tr, int set) 157 { 158 int cpu; 159 160 if (!(is_graph(tr) ^ set)) 161 return 0; 162 163 stop_irqsoff_tracer(irqsoff_trace, !set); 164 165 for_each_possible_cpu(cpu) 166 per_cpu(tracing_cpu, cpu) = 0; 167 168 tr->max_latency = 0; 169 tracing_reset_online_cpus(&irqsoff_trace->trace_buffer); 170 171 return start_irqsoff_tracer(irqsoff_trace, set); 172 } 173 174 static int irqsoff_graph_entry(struct ftrace_graph_ent *trace) 175 { 176 struct trace_array *tr = irqsoff_trace; 177 struct trace_array_cpu *data; 178 unsigned long flags; 179 int ret; 180 int pc; 181 182 if (ftrace_graph_ignore_func(trace)) 183 return 0; 184 /* 185 * Do not trace a function if it's filtered by set_graph_notrace. 186 * Make the index of ret stack negative to indicate that it should 187 * ignore further functions. But it needs its own ret stack entry 188 * to recover the original index in order to continue tracing after 189 * returning from the function. 190 */ 191 if (ftrace_graph_notrace_addr(trace->func)) 192 return 1; 193 194 if (!func_prolog_dec(tr, &data, &flags)) 195 return 0; 196 197 pc = preempt_count(); 198 ret = __trace_graph_entry(tr, trace, flags, pc); 199 atomic_dec(&data->disabled); 200 201 return ret; 202 } 203 204 static void irqsoff_graph_return(struct ftrace_graph_ret *trace) 205 { 206 struct trace_array *tr = irqsoff_trace; 207 struct trace_array_cpu *data; 208 unsigned long flags; 209 int pc; 210 211 ftrace_graph_addr_finish(trace); 212 213 if (!func_prolog_dec(tr, &data, &flags)) 214 return; 215 216 pc = preempt_count(); 217 __trace_graph_return(tr, trace, flags, pc); 218 atomic_dec(&data->disabled); 219 } 220 221 static struct fgraph_ops fgraph_ops = { 222 .entryfunc = &irqsoff_graph_entry, 223 .retfunc = &irqsoff_graph_return, 224 }; 225 226 static void irqsoff_trace_open(struct trace_iterator *iter) 227 { 228 if (is_graph(iter->tr)) 229 graph_trace_open(iter); 230 231 } 232 233 static void irqsoff_trace_close(struct trace_iterator *iter) 234 { 235 if (iter->private) 236 graph_trace_close(iter); 237 } 238 239 #define GRAPH_TRACER_FLAGS (TRACE_GRAPH_PRINT_CPU | \ 240 TRACE_GRAPH_PRINT_PROC | \ 241 TRACE_GRAPH_PRINT_ABS_TIME | \ 242 TRACE_GRAPH_PRINT_DURATION) 243 244 static enum print_line_t irqsoff_print_line(struct trace_iterator *iter) 245 { 246 /* 247 * In graph mode call the graph tracer output function, 248 * otherwise go with the TRACE_FN event handler 249 */ 250 if (is_graph(iter->tr)) 251 return print_graph_function_flags(iter, GRAPH_TRACER_FLAGS); 252 253 return TRACE_TYPE_UNHANDLED; 254 } 255 256 static void irqsoff_print_header(struct seq_file *s) 257 { 258 struct trace_array *tr = irqsoff_trace; 259 260 if (is_graph(tr)) 261 print_graph_headers_flags(s, GRAPH_TRACER_FLAGS); 262 else 263 trace_default_header(s); 264 } 265 266 static void 267 __trace_function(struct trace_array *tr, 268 unsigned long ip, unsigned long parent_ip, 269 unsigned long flags, int pc) 270 { 271 if (is_graph(tr)) 272 trace_graph_function(tr, ip, parent_ip, flags, pc); 273 else 274 trace_function(tr, ip, parent_ip, flags, pc); 275 } 276 277 #else 278 #define __trace_function trace_function 279 280 static enum print_line_t irqsoff_print_line(struct trace_iterator *iter) 281 { 282 return TRACE_TYPE_UNHANDLED; 283 } 284 285 static void irqsoff_trace_open(struct trace_iterator *iter) { } 286 static void irqsoff_trace_close(struct trace_iterator *iter) { } 287 288 #ifdef CONFIG_FUNCTION_TRACER 289 static void irqsoff_print_header(struct seq_file *s) 290 { 291 trace_default_header(s); 292 } 293 #else 294 static void irqsoff_print_header(struct seq_file *s) 295 { 296 trace_latency_header(s); 297 } 298 #endif /* CONFIG_FUNCTION_TRACER */ 299 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ 300 301 /* 302 * Should this new latency be reported/recorded? 303 */ 304 static bool report_latency(struct trace_array *tr, u64 delta) 305 { 306 if (tracing_thresh) { 307 if (delta < tracing_thresh) 308 return false; 309 } else { 310 if (delta <= tr->max_latency) 311 return false; 312 } 313 return true; 314 } 315 316 static void 317 check_critical_timing(struct trace_array *tr, 318 struct trace_array_cpu *data, 319 unsigned long parent_ip, 320 int cpu) 321 { 322 u64 T0, T1, delta; 323 unsigned long flags; 324 int pc; 325 326 T0 = data->preempt_timestamp; 327 T1 = ftrace_now(cpu); 328 delta = T1-T0; 329 330 local_save_flags(flags); 331 332 pc = preempt_count(); 333 334 if (!report_latency(tr, delta)) 335 goto out; 336 337 raw_spin_lock_irqsave(&max_trace_lock, flags); 338 339 /* check if we are still the max latency */ 340 if (!report_latency(tr, delta)) 341 goto out_unlock; 342 343 __trace_function(tr, CALLER_ADDR0, parent_ip, flags, pc); 344 /* Skip 5 functions to get to the irq/preempt enable function */ 345 __trace_stack(tr, flags, 5, pc); 346 347 if (data->critical_sequence != max_sequence) 348 goto out_unlock; 349 350 data->critical_end = parent_ip; 351 352 if (likely(!is_tracing_stopped())) { 353 tr->max_latency = delta; 354 update_max_tr_single(tr, current, cpu); 355 } 356 357 max_sequence++; 358 359 out_unlock: 360 raw_spin_unlock_irqrestore(&max_trace_lock, flags); 361 362 out: 363 data->critical_sequence = max_sequence; 364 data->preempt_timestamp = ftrace_now(cpu); 365 __trace_function(tr, CALLER_ADDR0, parent_ip, flags, pc); 366 } 367 368 static inline void 369 start_critical_timing(unsigned long ip, unsigned long parent_ip, int pc) 370 { 371 int cpu; 372 struct trace_array *tr = irqsoff_trace; 373 struct trace_array_cpu *data; 374 unsigned long flags; 375 376 if (!tracer_enabled || !tracing_is_enabled()) 377 return; 378 379 cpu = raw_smp_processor_id(); 380 381 if (per_cpu(tracing_cpu, cpu)) 382 return; 383 384 data = per_cpu_ptr(tr->trace_buffer.data, cpu); 385 386 if (unlikely(!data) || atomic_read(&data->disabled)) 387 return; 388 389 atomic_inc(&data->disabled); 390 391 data->critical_sequence = max_sequence; 392 data->preempt_timestamp = ftrace_now(cpu); 393 data->critical_start = parent_ip ? : ip; 394 395 local_save_flags(flags); 396 397 __trace_function(tr, ip, parent_ip, flags, pc); 398 399 per_cpu(tracing_cpu, cpu) = 1; 400 401 atomic_dec(&data->disabled); 402 } 403 404 static inline void 405 stop_critical_timing(unsigned long ip, unsigned long parent_ip, int pc) 406 { 407 int cpu; 408 struct trace_array *tr = irqsoff_trace; 409 struct trace_array_cpu *data; 410 unsigned long flags; 411 412 cpu = raw_smp_processor_id(); 413 /* Always clear the tracing cpu on stopping the trace */ 414 if (unlikely(per_cpu(tracing_cpu, cpu))) 415 per_cpu(tracing_cpu, cpu) = 0; 416 else 417 return; 418 419 if (!tracer_enabled || !tracing_is_enabled()) 420 return; 421 422 data = per_cpu_ptr(tr->trace_buffer.data, cpu); 423 424 if (unlikely(!data) || 425 !data->critical_start || atomic_read(&data->disabled)) 426 return; 427 428 atomic_inc(&data->disabled); 429 430 local_save_flags(flags); 431 __trace_function(tr, ip, parent_ip, flags, pc); 432 check_critical_timing(tr, data, parent_ip ? : ip, cpu); 433 data->critical_start = 0; 434 atomic_dec(&data->disabled); 435 } 436 437 /* start and stop critical timings used to for stoppage (in idle) */ 438 void start_critical_timings(void) 439 { 440 int pc = preempt_count(); 441 442 if (preempt_trace(pc) || irq_trace()) 443 start_critical_timing(CALLER_ADDR0, CALLER_ADDR1, pc); 444 } 445 EXPORT_SYMBOL_GPL(start_critical_timings); 446 447 void stop_critical_timings(void) 448 { 449 int pc = preempt_count(); 450 451 if (preempt_trace(pc) || irq_trace()) 452 stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1, pc); 453 } 454 EXPORT_SYMBOL_GPL(stop_critical_timings); 455 456 #ifdef CONFIG_FUNCTION_TRACER 457 static bool function_enabled; 458 459 static int register_irqsoff_function(struct trace_array *tr, int graph, int set) 460 { 461 int ret; 462 463 /* 'set' is set if TRACE_ITER_FUNCTION is about to be set */ 464 if (function_enabled || (!set && !(tr->trace_flags & TRACE_ITER_FUNCTION))) 465 return 0; 466 467 if (graph) 468 ret = register_ftrace_graph(&fgraph_ops); 469 else 470 ret = register_ftrace_function(tr->ops); 471 472 if (!ret) 473 function_enabled = true; 474 475 return ret; 476 } 477 478 static void unregister_irqsoff_function(struct trace_array *tr, int graph) 479 { 480 if (!function_enabled) 481 return; 482 483 if (graph) 484 unregister_ftrace_graph(&fgraph_ops); 485 else 486 unregister_ftrace_function(tr->ops); 487 488 function_enabled = false; 489 } 490 491 static int irqsoff_function_set(struct trace_array *tr, u32 mask, int set) 492 { 493 if (!(mask & TRACE_ITER_FUNCTION)) 494 return 0; 495 496 if (set) 497 register_irqsoff_function(tr, is_graph(tr), 1); 498 else 499 unregister_irqsoff_function(tr, is_graph(tr)); 500 return 1; 501 } 502 #else 503 static int register_irqsoff_function(struct trace_array *tr, int graph, int set) 504 { 505 return 0; 506 } 507 static void unregister_irqsoff_function(struct trace_array *tr, int graph) { } 508 static inline int irqsoff_function_set(struct trace_array *tr, u32 mask, int set) 509 { 510 return 0; 511 } 512 #endif /* CONFIG_FUNCTION_TRACER */ 513 514 static int irqsoff_flag_changed(struct trace_array *tr, u32 mask, int set) 515 { 516 struct tracer *tracer = tr->current_trace; 517 518 if (irqsoff_function_set(tr, mask, set)) 519 return 0; 520 521 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 522 if (mask & TRACE_ITER_DISPLAY_GRAPH) 523 return irqsoff_display_graph(tr, set); 524 #endif 525 526 return trace_keep_overwrite(tracer, mask, set); 527 } 528 529 static int start_irqsoff_tracer(struct trace_array *tr, int graph) 530 { 531 int ret; 532 533 ret = register_irqsoff_function(tr, graph, 0); 534 535 if (!ret && tracing_is_enabled()) 536 tracer_enabled = 1; 537 else 538 tracer_enabled = 0; 539 540 return ret; 541 } 542 543 static void stop_irqsoff_tracer(struct trace_array *tr, int graph) 544 { 545 tracer_enabled = 0; 546 547 unregister_irqsoff_function(tr, graph); 548 } 549 550 static bool irqsoff_busy; 551 552 static int __irqsoff_tracer_init(struct trace_array *tr) 553 { 554 if (irqsoff_busy) 555 return -EBUSY; 556 557 save_flags = tr->trace_flags; 558 559 /* non overwrite screws up the latency tracers */ 560 set_tracer_flag(tr, TRACE_ITER_OVERWRITE, 1); 561 set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, 1); 562 563 tr->max_latency = 0; 564 irqsoff_trace = tr; 565 /* make sure that the tracer is visible */ 566 smp_wmb(); 567 568 ftrace_init_array_ops(tr, irqsoff_tracer_call); 569 570 /* Only toplevel instance supports graph tracing */ 571 if (start_irqsoff_tracer(tr, (tr->flags & TRACE_ARRAY_FL_GLOBAL && 572 is_graph(tr)))) 573 printk(KERN_ERR "failed to start irqsoff tracer\n"); 574 575 irqsoff_busy = true; 576 return 0; 577 } 578 579 static void __irqsoff_tracer_reset(struct trace_array *tr) 580 { 581 int lat_flag = save_flags & TRACE_ITER_LATENCY_FMT; 582 int overwrite_flag = save_flags & TRACE_ITER_OVERWRITE; 583 584 stop_irqsoff_tracer(tr, is_graph(tr)); 585 586 set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, lat_flag); 587 set_tracer_flag(tr, TRACE_ITER_OVERWRITE, overwrite_flag); 588 ftrace_reset_array_ops(tr); 589 590 irqsoff_busy = false; 591 } 592 593 static void irqsoff_tracer_start(struct trace_array *tr) 594 { 595 tracer_enabled = 1; 596 } 597 598 static void irqsoff_tracer_stop(struct trace_array *tr) 599 { 600 tracer_enabled = 0; 601 } 602 603 #ifdef CONFIG_IRQSOFF_TRACER 604 /* 605 * We are only interested in hardirq on/off events: 606 */ 607 void tracer_hardirqs_on(unsigned long a0, unsigned long a1) 608 { 609 unsigned int pc = preempt_count(); 610 611 if (!preempt_trace(pc) && irq_trace()) 612 stop_critical_timing(a0, a1, pc); 613 } 614 615 void tracer_hardirqs_off(unsigned long a0, unsigned long a1) 616 { 617 unsigned int pc = preempt_count(); 618 619 if (!preempt_trace(pc) && irq_trace()) 620 start_critical_timing(a0, a1, pc); 621 } 622 623 static int irqsoff_tracer_init(struct trace_array *tr) 624 { 625 trace_type = TRACER_IRQS_OFF; 626 627 return __irqsoff_tracer_init(tr); 628 } 629 630 static void irqsoff_tracer_reset(struct trace_array *tr) 631 { 632 __irqsoff_tracer_reset(tr); 633 } 634 635 static struct tracer irqsoff_tracer __read_mostly = 636 { 637 .name = "irqsoff", 638 .init = irqsoff_tracer_init, 639 .reset = irqsoff_tracer_reset, 640 .start = irqsoff_tracer_start, 641 .stop = irqsoff_tracer_stop, 642 .print_max = true, 643 .print_header = irqsoff_print_header, 644 .print_line = irqsoff_print_line, 645 .flag_changed = irqsoff_flag_changed, 646 #ifdef CONFIG_FTRACE_SELFTEST 647 .selftest = trace_selftest_startup_irqsoff, 648 #endif 649 .open = irqsoff_trace_open, 650 .close = irqsoff_trace_close, 651 .allow_instances = true, 652 .use_max_tr = true, 653 }; 654 #endif /* CONFIG_IRQSOFF_TRACER */ 655 656 #ifdef CONFIG_PREEMPT_TRACER 657 void tracer_preempt_on(unsigned long a0, unsigned long a1) 658 { 659 int pc = preempt_count(); 660 661 if (preempt_trace(pc) && !irq_trace()) 662 stop_critical_timing(a0, a1, pc); 663 } 664 665 void tracer_preempt_off(unsigned long a0, unsigned long a1) 666 { 667 int pc = preempt_count(); 668 669 if (preempt_trace(pc) && !irq_trace()) 670 start_critical_timing(a0, a1, pc); 671 } 672 673 static int preemptoff_tracer_init(struct trace_array *tr) 674 { 675 trace_type = TRACER_PREEMPT_OFF; 676 677 return __irqsoff_tracer_init(tr); 678 } 679 680 static void preemptoff_tracer_reset(struct trace_array *tr) 681 { 682 __irqsoff_tracer_reset(tr); 683 } 684 685 static struct tracer preemptoff_tracer __read_mostly = 686 { 687 .name = "preemptoff", 688 .init = preemptoff_tracer_init, 689 .reset = preemptoff_tracer_reset, 690 .start = irqsoff_tracer_start, 691 .stop = irqsoff_tracer_stop, 692 .print_max = true, 693 .print_header = irqsoff_print_header, 694 .print_line = irqsoff_print_line, 695 .flag_changed = irqsoff_flag_changed, 696 #ifdef CONFIG_FTRACE_SELFTEST 697 .selftest = trace_selftest_startup_preemptoff, 698 #endif 699 .open = irqsoff_trace_open, 700 .close = irqsoff_trace_close, 701 .allow_instances = true, 702 .use_max_tr = true, 703 }; 704 #endif /* CONFIG_PREEMPT_TRACER */ 705 706 #if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER) 707 708 static int preemptirqsoff_tracer_init(struct trace_array *tr) 709 { 710 trace_type = TRACER_IRQS_OFF | TRACER_PREEMPT_OFF; 711 712 return __irqsoff_tracer_init(tr); 713 } 714 715 static void preemptirqsoff_tracer_reset(struct trace_array *tr) 716 { 717 __irqsoff_tracer_reset(tr); 718 } 719 720 static struct tracer preemptirqsoff_tracer __read_mostly = 721 { 722 .name = "preemptirqsoff", 723 .init = preemptirqsoff_tracer_init, 724 .reset = preemptirqsoff_tracer_reset, 725 .start = irqsoff_tracer_start, 726 .stop = irqsoff_tracer_stop, 727 .print_max = true, 728 .print_header = irqsoff_print_header, 729 .print_line = irqsoff_print_line, 730 .flag_changed = irqsoff_flag_changed, 731 #ifdef CONFIG_FTRACE_SELFTEST 732 .selftest = trace_selftest_startup_preemptirqsoff, 733 #endif 734 .open = irqsoff_trace_open, 735 .close = irqsoff_trace_close, 736 .allow_instances = true, 737 .use_max_tr = true, 738 }; 739 #endif 740 741 __init static int init_irqsoff_tracer(void) 742 { 743 #ifdef CONFIG_IRQSOFF_TRACER 744 register_tracer(&irqsoff_tracer); 745 #endif 746 #ifdef CONFIG_PREEMPT_TRACER 747 register_tracer(&preemptoff_tracer); 748 #endif 749 #if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER) 750 register_tracer(&preemptirqsoff_tracer); 751 #endif 752 753 return 0; 754 } 755 core_initcall(init_irqsoff_tracer); 756 #endif /* IRQSOFF_TRACER || PREEMPTOFF_TRACER */ 757