1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * OS Noise Tracer: computes the OS Noise suffered by a running thread. 4 * Timerlat Tracer: measures the wakeup latency of a timer triggered IRQ and thread. 5 * 6 * Based on "hwlat_detector" tracer by: 7 * Copyright (C) 2008-2009 Jon Masters, Red Hat, Inc. <jcm@redhat.com> 8 * Copyright (C) 2013-2016 Steven Rostedt, Red Hat, Inc. <srostedt@redhat.com> 9 * With feedback from Clark Williams <williams@redhat.com> 10 * 11 * And also based on the rtsl tracer presented on: 12 * DE OLIVEIRA, Daniel Bristot, et al. Demystifying the real-time linux 13 * scheduling latency. In: 32nd Euromicro Conference on Real-Time Systems 14 * (ECRTS 2020). Schloss Dagstuhl-Leibniz-Zentrum fur Informatik, 2020. 15 * 16 * Copyright (C) 2021 Daniel Bristot de Oliveira, Red Hat, Inc. <bristot@redhat.com> 17 */ 18 19 #include <linux/kthread.h> 20 #include <linux/tracefs.h> 21 #include <linux/uaccess.h> 22 #include <linux/cpumask.h> 23 #include <linux/delay.h> 24 #include <linux/sched/clock.h> 25 #include <uapi/linux/sched/types.h> 26 #include <linux/sched.h> 27 #include "trace.h" 28 29 #ifdef CONFIG_X86_LOCAL_APIC 30 #include <asm/trace/irq_vectors.h> 31 #undef TRACE_INCLUDE_PATH 32 #undef TRACE_INCLUDE_FILE 33 #endif /* CONFIG_X86_LOCAL_APIC */ 34 35 #include <trace/events/irq.h> 36 #include <trace/events/sched.h> 37 38 #define CREATE_TRACE_POINTS 39 #include <trace/events/osnoise.h> 40 41 /* 42 * Default values. 43 */ 44 #define BANNER "osnoise: " 45 #define DEFAULT_SAMPLE_PERIOD 1000000 /* 1s */ 46 #define DEFAULT_SAMPLE_RUNTIME 1000000 /* 1s */ 47 48 #define DEFAULT_TIMERLAT_PERIOD 1000 /* 1ms */ 49 #define DEFAULT_TIMERLAT_PRIO 95 /* FIFO 95 */ 50 51 /* 52 * osnoise/options entries. 53 */ 54 enum osnoise_options_index { 55 OSN_DEFAULTS = 0, 56 OSN_WORKLOAD, 57 OSN_PANIC_ON_STOP, 58 OSN_PREEMPT_DISABLE, 59 OSN_IRQ_DISABLE, 60 OSN_MAX 61 }; 62 63 static const char * const osnoise_options_str[OSN_MAX] = { 64 "DEFAULTS", 65 "OSNOISE_WORKLOAD", 66 "PANIC_ON_STOP", 67 "OSNOISE_PREEMPT_DISABLE", 68 "OSNOISE_IRQ_DISABLE" }; 69 70 #define OSN_DEFAULT_OPTIONS 0x2 71 static unsigned long osnoise_options = OSN_DEFAULT_OPTIONS; 72 73 /* 74 * trace_array of the enabled osnoise/timerlat instances. 75 */ 76 struct osnoise_instance { 77 struct list_head list; 78 struct trace_array *tr; 79 }; 80 81 static struct list_head osnoise_instances; 82 83 static bool osnoise_has_registered_instances(void) 84 { 85 return !!list_first_or_null_rcu(&osnoise_instances, 86 struct osnoise_instance, 87 list); 88 } 89 90 /* 91 * osnoise_instance_registered - check if a tr is already registered 92 */ 93 static int osnoise_instance_registered(struct trace_array *tr) 94 { 95 struct osnoise_instance *inst; 96 int found = 0; 97 98 rcu_read_lock(); 99 list_for_each_entry_rcu(inst, &osnoise_instances, list) { 100 if (inst->tr == tr) 101 found = 1; 102 } 103 rcu_read_unlock(); 104 105 return found; 106 } 107 108 /* 109 * osnoise_register_instance - register a new trace instance 110 * 111 * Register a trace_array *tr in the list of instances running 112 * osnoise/timerlat tracers. 113 */ 114 static int osnoise_register_instance(struct trace_array *tr) 115 { 116 struct osnoise_instance *inst; 117 118 /* 119 * register/unregister serialization is provided by trace's 120 * trace_types_lock. 121 */ 122 lockdep_assert_held(&trace_types_lock); 123 124 inst = kmalloc(sizeof(*inst), GFP_KERNEL); 125 if (!inst) 126 return -ENOMEM; 127 128 INIT_LIST_HEAD_RCU(&inst->list); 129 inst->tr = tr; 130 list_add_tail_rcu(&inst->list, &osnoise_instances); 131 132 return 0; 133 } 134 135 /* 136 * osnoise_unregister_instance - unregister a registered trace instance 137 * 138 * Remove the trace_array *tr from the list of instances running 139 * osnoise/timerlat tracers. 140 */ 141 static void osnoise_unregister_instance(struct trace_array *tr) 142 { 143 struct osnoise_instance *inst; 144 int found = 0; 145 146 /* 147 * register/unregister serialization is provided by trace's 148 * trace_types_lock. 149 */ 150 list_for_each_entry_rcu(inst, &osnoise_instances, list, 151 lockdep_is_held(&trace_types_lock)) { 152 if (inst->tr == tr) { 153 list_del_rcu(&inst->list); 154 found = 1; 155 break; 156 } 157 } 158 159 if (!found) 160 return; 161 162 kvfree_rcu_mightsleep(inst); 163 } 164 165 /* 166 * NMI runtime info. 167 */ 168 struct osn_nmi { 169 u64 count; 170 u64 delta_start; 171 }; 172 173 /* 174 * IRQ runtime info. 175 */ 176 struct osn_irq { 177 u64 count; 178 u64 arrival_time; 179 u64 delta_start; 180 }; 181 182 #define IRQ_CONTEXT 0 183 #define THREAD_CONTEXT 1 184 #define THREAD_URET 2 185 /* 186 * sofirq runtime info. 187 */ 188 struct osn_softirq { 189 u64 count; 190 u64 arrival_time; 191 u64 delta_start; 192 }; 193 194 /* 195 * thread runtime info. 196 */ 197 struct osn_thread { 198 u64 count; 199 u64 arrival_time; 200 u64 delta_start; 201 }; 202 203 /* 204 * Runtime information: this structure saves the runtime information used by 205 * one sampling thread. 206 */ 207 struct osnoise_variables { 208 struct task_struct *kthread; 209 bool sampling; 210 pid_t pid; 211 struct osn_nmi nmi; 212 struct osn_irq irq; 213 struct osn_softirq softirq; 214 struct osn_thread thread; 215 local_t int_counter; 216 }; 217 218 /* 219 * Per-cpu runtime information. 220 */ 221 static DEFINE_PER_CPU(struct osnoise_variables, per_cpu_osnoise_var); 222 223 /* 224 * this_cpu_osn_var - Return the per-cpu osnoise_variables on its relative CPU 225 */ 226 static inline struct osnoise_variables *this_cpu_osn_var(void) 227 { 228 return this_cpu_ptr(&per_cpu_osnoise_var); 229 } 230 231 /* 232 * Protect the interface. 233 */ 234 static struct mutex interface_lock; 235 236 #ifdef CONFIG_TIMERLAT_TRACER 237 /* 238 * Runtime information for the timer mode. 239 */ 240 struct timerlat_variables { 241 struct task_struct *kthread; 242 struct hrtimer timer; 243 u64 rel_period; 244 u64 abs_period; 245 bool tracing_thread; 246 u64 count; 247 bool uthread_migrate; 248 }; 249 250 static DEFINE_PER_CPU(struct timerlat_variables, per_cpu_timerlat_var); 251 252 /* 253 * this_cpu_tmr_var - Return the per-cpu timerlat_variables on its relative CPU 254 */ 255 static inline struct timerlat_variables *this_cpu_tmr_var(void) 256 { 257 return this_cpu_ptr(&per_cpu_timerlat_var); 258 } 259 260 /* 261 * tlat_var_reset - Reset the values of the given timerlat_variables 262 */ 263 static inline void tlat_var_reset(void) 264 { 265 struct timerlat_variables *tlat_var; 266 int cpu; 267 268 /* Synchronize with the timerlat interfaces */ 269 mutex_lock(&interface_lock); 270 /* 271 * So far, all the values are initialized as 0, so 272 * zeroing the structure is perfect. 273 */ 274 for_each_cpu(cpu, cpu_online_mask) { 275 tlat_var = per_cpu_ptr(&per_cpu_timerlat_var, cpu); 276 if (tlat_var->kthread) 277 hrtimer_cancel(&tlat_var->timer); 278 memset(tlat_var, 0, sizeof(*tlat_var)); 279 } 280 mutex_unlock(&interface_lock); 281 } 282 #else /* CONFIG_TIMERLAT_TRACER */ 283 #define tlat_var_reset() do {} while (0) 284 #endif /* CONFIG_TIMERLAT_TRACER */ 285 286 /* 287 * osn_var_reset - Reset the values of the given osnoise_variables 288 */ 289 static inline void osn_var_reset(void) 290 { 291 struct osnoise_variables *osn_var; 292 int cpu; 293 294 /* 295 * So far, all the values are initialized as 0, so 296 * zeroing the structure is perfect. 297 */ 298 for_each_cpu(cpu, cpu_online_mask) { 299 osn_var = per_cpu_ptr(&per_cpu_osnoise_var, cpu); 300 memset(osn_var, 0, sizeof(*osn_var)); 301 } 302 } 303 304 /* 305 * osn_var_reset_all - Reset the value of all per-cpu osnoise_variables 306 */ 307 static inline void osn_var_reset_all(void) 308 { 309 osn_var_reset(); 310 tlat_var_reset(); 311 } 312 313 /* 314 * Tells NMIs to call back to the osnoise tracer to record timestamps. 315 */ 316 bool trace_osnoise_callback_enabled; 317 318 /* 319 * osnoise sample structure definition. Used to store the statistics of a 320 * sample run. 321 */ 322 struct osnoise_sample { 323 u64 runtime; /* runtime */ 324 u64 noise; /* noise */ 325 u64 max_sample; /* max single noise sample */ 326 int hw_count; /* # HW (incl. hypervisor) interference */ 327 int nmi_count; /* # NMIs during this sample */ 328 int irq_count; /* # IRQs during this sample */ 329 int softirq_count; /* # softirqs during this sample */ 330 int thread_count; /* # threads during this sample */ 331 }; 332 333 #ifdef CONFIG_TIMERLAT_TRACER 334 /* 335 * timerlat sample structure definition. Used to store the statistics of 336 * a sample run. 337 */ 338 struct timerlat_sample { 339 u64 timer_latency; /* timer_latency */ 340 unsigned int seqnum; /* unique sequence */ 341 int context; /* timer context */ 342 }; 343 #endif 344 345 /* 346 * Tracer data. 347 */ 348 static struct osnoise_data { 349 u64 sample_period; /* total sampling period */ 350 u64 sample_runtime; /* active sampling portion of period */ 351 u64 stop_tracing; /* stop trace in the internal operation (loop/irq) */ 352 u64 stop_tracing_total; /* stop trace in the final operation (report/thread) */ 353 #ifdef CONFIG_TIMERLAT_TRACER 354 u64 timerlat_period; /* timerlat period */ 355 u64 print_stack; /* print IRQ stack if total > */ 356 int timerlat_tracer; /* timerlat tracer */ 357 #endif 358 bool tainted; /* infor users and developers about a problem */ 359 } osnoise_data = { 360 .sample_period = DEFAULT_SAMPLE_PERIOD, 361 .sample_runtime = DEFAULT_SAMPLE_RUNTIME, 362 .stop_tracing = 0, 363 .stop_tracing_total = 0, 364 #ifdef CONFIG_TIMERLAT_TRACER 365 .print_stack = 0, 366 .timerlat_period = DEFAULT_TIMERLAT_PERIOD, 367 .timerlat_tracer = 0, 368 #endif 369 }; 370 371 #ifdef CONFIG_TIMERLAT_TRACER 372 static inline bool timerlat_enabled(void) 373 { 374 return osnoise_data.timerlat_tracer; 375 } 376 377 static inline int timerlat_softirq_exit(struct osnoise_variables *osn_var) 378 { 379 struct timerlat_variables *tlat_var = this_cpu_tmr_var(); 380 /* 381 * If the timerlat is enabled, but the irq handler did 382 * not run yet enabling timerlat_tracer, do not trace. 383 */ 384 if (!tlat_var->tracing_thread) { 385 osn_var->softirq.arrival_time = 0; 386 osn_var->softirq.delta_start = 0; 387 return 0; 388 } 389 return 1; 390 } 391 392 static inline int timerlat_thread_exit(struct osnoise_variables *osn_var) 393 { 394 struct timerlat_variables *tlat_var = this_cpu_tmr_var(); 395 /* 396 * If the timerlat is enabled, but the irq handler did 397 * not run yet enabling timerlat_tracer, do not trace. 398 */ 399 if (!tlat_var->tracing_thread) { 400 osn_var->thread.delta_start = 0; 401 osn_var->thread.arrival_time = 0; 402 return 0; 403 } 404 return 1; 405 } 406 #else /* CONFIG_TIMERLAT_TRACER */ 407 static inline bool timerlat_enabled(void) 408 { 409 return false; 410 } 411 412 static inline int timerlat_softirq_exit(struct osnoise_variables *osn_var) 413 { 414 return 1; 415 } 416 static inline int timerlat_thread_exit(struct osnoise_variables *osn_var) 417 { 418 return 1; 419 } 420 #endif 421 422 #ifdef CONFIG_PREEMPT_RT 423 /* 424 * Print the osnoise header info. 425 */ 426 static void print_osnoise_headers(struct seq_file *s) 427 { 428 if (osnoise_data.tainted) 429 seq_puts(s, "# osnoise is tainted!\n"); 430 431 seq_puts(s, "# _-------=> irqs-off\n"); 432 seq_puts(s, "# / _------=> need-resched\n"); 433 seq_puts(s, "# | / _-----=> need-resched-lazy\n"); 434 seq_puts(s, "# || / _----=> hardirq/softirq\n"); 435 seq_puts(s, "# ||| / _---=> preempt-depth\n"); 436 seq_puts(s, "# |||| / _--=> preempt-lazy-depth\n"); 437 seq_puts(s, "# ||||| / _-=> migrate-disable\n"); 438 439 seq_puts(s, "# |||||| / "); 440 seq_puts(s, " MAX\n"); 441 442 seq_puts(s, "# ||||| / "); 443 seq_puts(s, " SINGLE Interference counters:\n"); 444 445 seq_puts(s, "# ||||||| RUNTIME "); 446 seq_puts(s, " NOISE %% OF CPU NOISE +-----------------------------+\n"); 447 448 seq_puts(s, "# TASK-PID CPU# ||||||| TIMESTAMP IN US "); 449 seq_puts(s, " IN US AVAILABLE IN US HW NMI IRQ SIRQ THREAD\n"); 450 451 seq_puts(s, "# | | | ||||||| | | "); 452 seq_puts(s, " | | | | | | | |\n"); 453 } 454 #else /* CONFIG_PREEMPT_RT */ 455 static void print_osnoise_headers(struct seq_file *s) 456 { 457 if (osnoise_data.tainted) 458 seq_puts(s, "# osnoise is tainted!\n"); 459 460 seq_puts(s, "# _-----=> irqs-off\n"); 461 seq_puts(s, "# / _----=> need-resched\n"); 462 seq_puts(s, "# | / _---=> hardirq/softirq\n"); 463 seq_puts(s, "# || / _--=> preempt-depth\n"); 464 seq_puts(s, "# ||| / _-=> migrate-disable "); 465 seq_puts(s, " MAX\n"); 466 seq_puts(s, "# |||| / delay "); 467 seq_puts(s, " SINGLE Interference counters:\n"); 468 469 seq_puts(s, "# ||||| RUNTIME "); 470 seq_puts(s, " NOISE %% OF CPU NOISE +-----------------------------+\n"); 471 472 seq_puts(s, "# TASK-PID CPU# ||||| TIMESTAMP IN US "); 473 seq_puts(s, " IN US AVAILABLE IN US HW NMI IRQ SIRQ THREAD\n"); 474 475 seq_puts(s, "# | | | ||||| | | "); 476 seq_puts(s, " | | | | | | | |\n"); 477 } 478 #endif /* CONFIG_PREEMPT_RT */ 479 480 /* 481 * osnoise_taint - report an osnoise error. 482 */ 483 #define osnoise_taint(msg) ({ \ 484 struct osnoise_instance *inst; \ 485 struct trace_buffer *buffer; \ 486 \ 487 rcu_read_lock(); \ 488 list_for_each_entry_rcu(inst, &osnoise_instances, list) { \ 489 buffer = inst->tr->array_buffer.buffer; \ 490 trace_array_printk_buf(buffer, _THIS_IP_, msg); \ 491 } \ 492 rcu_read_unlock(); \ 493 osnoise_data.tainted = true; \ 494 }) 495 496 /* 497 * Record an osnoise_sample into the tracer buffer. 498 */ 499 static void 500 __trace_osnoise_sample(struct osnoise_sample *sample, struct trace_buffer *buffer) 501 { 502 struct ring_buffer_event *event; 503 struct osnoise_entry *entry; 504 505 event = trace_buffer_lock_reserve(buffer, TRACE_OSNOISE, sizeof(*entry), 506 tracing_gen_ctx()); 507 if (!event) 508 return; 509 entry = ring_buffer_event_data(event); 510 entry->runtime = sample->runtime; 511 entry->noise = sample->noise; 512 entry->max_sample = sample->max_sample; 513 entry->hw_count = sample->hw_count; 514 entry->nmi_count = sample->nmi_count; 515 entry->irq_count = sample->irq_count; 516 entry->softirq_count = sample->softirq_count; 517 entry->thread_count = sample->thread_count; 518 519 trace_buffer_unlock_commit_nostack(buffer, event); 520 } 521 522 /* 523 * Record an osnoise_sample on all osnoise instances. 524 */ 525 static void trace_osnoise_sample(struct osnoise_sample *sample) 526 { 527 struct osnoise_instance *inst; 528 struct trace_buffer *buffer; 529 530 rcu_read_lock(); 531 list_for_each_entry_rcu(inst, &osnoise_instances, list) { 532 buffer = inst->tr->array_buffer.buffer; 533 __trace_osnoise_sample(sample, buffer); 534 } 535 rcu_read_unlock(); 536 } 537 538 #ifdef CONFIG_TIMERLAT_TRACER 539 /* 540 * Print the timerlat header info. 541 */ 542 #ifdef CONFIG_PREEMPT_RT 543 static void print_timerlat_headers(struct seq_file *s) 544 { 545 seq_puts(s, "# _-------=> irqs-off\n"); 546 seq_puts(s, "# / _------=> need-resched\n"); 547 seq_puts(s, "# | / _-----=> need-resched-lazy\n"); 548 seq_puts(s, "# || / _----=> hardirq/softirq\n"); 549 seq_puts(s, "# ||| / _---=> preempt-depth\n"); 550 seq_puts(s, "# |||| / _--=> preempt-lazy-depth\n"); 551 seq_puts(s, "# ||||| / _-=> migrate-disable\n"); 552 seq_puts(s, "# |||||| /\n"); 553 seq_puts(s, "# ||||||| ACTIVATION\n"); 554 seq_puts(s, "# TASK-PID CPU# ||||||| TIMESTAMP ID "); 555 seq_puts(s, " CONTEXT LATENCY\n"); 556 seq_puts(s, "# | | | ||||||| | | "); 557 seq_puts(s, " | |\n"); 558 } 559 #else /* CONFIG_PREEMPT_RT */ 560 static void print_timerlat_headers(struct seq_file *s) 561 { 562 seq_puts(s, "# _-----=> irqs-off\n"); 563 seq_puts(s, "# / _----=> need-resched\n"); 564 seq_puts(s, "# | / _---=> hardirq/softirq\n"); 565 seq_puts(s, "# || / _--=> preempt-depth\n"); 566 seq_puts(s, "# ||| / _-=> migrate-disable\n"); 567 seq_puts(s, "# |||| / delay\n"); 568 seq_puts(s, "# ||||| ACTIVATION\n"); 569 seq_puts(s, "# TASK-PID CPU# ||||| TIMESTAMP ID "); 570 seq_puts(s, " CONTEXT LATENCY\n"); 571 seq_puts(s, "# | | | ||||| | | "); 572 seq_puts(s, " | |\n"); 573 } 574 #endif /* CONFIG_PREEMPT_RT */ 575 576 static void 577 __trace_timerlat_sample(struct timerlat_sample *sample, struct trace_buffer *buffer) 578 { 579 struct ring_buffer_event *event; 580 struct timerlat_entry *entry; 581 582 event = trace_buffer_lock_reserve(buffer, TRACE_TIMERLAT, sizeof(*entry), 583 tracing_gen_ctx()); 584 if (!event) 585 return; 586 entry = ring_buffer_event_data(event); 587 entry->seqnum = sample->seqnum; 588 entry->context = sample->context; 589 entry->timer_latency = sample->timer_latency; 590 591 trace_buffer_unlock_commit_nostack(buffer, event); 592 } 593 594 /* 595 * Record an timerlat_sample into the tracer buffer. 596 */ 597 static void trace_timerlat_sample(struct timerlat_sample *sample) 598 { 599 struct osnoise_instance *inst; 600 struct trace_buffer *buffer; 601 602 rcu_read_lock(); 603 list_for_each_entry_rcu(inst, &osnoise_instances, list) { 604 buffer = inst->tr->array_buffer.buffer; 605 __trace_timerlat_sample(sample, buffer); 606 } 607 rcu_read_unlock(); 608 } 609 610 #ifdef CONFIG_STACKTRACE 611 612 #define MAX_CALLS 256 613 614 /* 615 * Stack trace will take place only at IRQ level, so, no need 616 * to control nesting here. 617 */ 618 struct trace_stack { 619 int stack_size; 620 int nr_entries; 621 unsigned long calls[MAX_CALLS]; 622 }; 623 624 static DEFINE_PER_CPU(struct trace_stack, trace_stack); 625 626 /* 627 * timerlat_save_stack - save a stack trace without printing 628 * 629 * Save the current stack trace without printing. The 630 * stack will be printed later, after the end of the measurement. 631 */ 632 static void timerlat_save_stack(int skip) 633 { 634 unsigned int size, nr_entries; 635 struct trace_stack *fstack; 636 637 fstack = this_cpu_ptr(&trace_stack); 638 639 size = ARRAY_SIZE(fstack->calls); 640 641 nr_entries = stack_trace_save(fstack->calls, size, skip); 642 643 fstack->stack_size = nr_entries * sizeof(unsigned long); 644 fstack->nr_entries = nr_entries; 645 646 return; 647 648 } 649 650 static void 651 __timerlat_dump_stack(struct trace_buffer *buffer, struct trace_stack *fstack, unsigned int size) 652 { 653 struct ring_buffer_event *event; 654 struct stack_entry *entry; 655 656 event = trace_buffer_lock_reserve(buffer, TRACE_STACK, sizeof(*entry) + size, 657 tracing_gen_ctx()); 658 if (!event) 659 return; 660 661 entry = ring_buffer_event_data(event); 662 663 memcpy(&entry->caller, fstack->calls, size); 664 entry->size = fstack->nr_entries; 665 666 trace_buffer_unlock_commit_nostack(buffer, event); 667 } 668 669 /* 670 * timerlat_dump_stack - dump a stack trace previously saved 671 */ 672 static void timerlat_dump_stack(u64 latency) 673 { 674 struct osnoise_instance *inst; 675 struct trace_buffer *buffer; 676 struct trace_stack *fstack; 677 unsigned int size; 678 679 /* 680 * trace only if latency > print_stack config, if enabled. 681 */ 682 if (!osnoise_data.print_stack || osnoise_data.print_stack > latency) 683 return; 684 685 preempt_disable_notrace(); 686 fstack = this_cpu_ptr(&trace_stack); 687 size = fstack->stack_size; 688 689 rcu_read_lock(); 690 list_for_each_entry_rcu(inst, &osnoise_instances, list) { 691 buffer = inst->tr->array_buffer.buffer; 692 __timerlat_dump_stack(buffer, fstack, size); 693 694 } 695 rcu_read_unlock(); 696 preempt_enable_notrace(); 697 } 698 #else /* CONFIG_STACKTRACE */ 699 #define timerlat_dump_stack(u64 latency) do {} while (0) 700 #define timerlat_save_stack(a) do {} while (0) 701 #endif /* CONFIG_STACKTRACE */ 702 #endif /* CONFIG_TIMERLAT_TRACER */ 703 704 /* 705 * Macros to encapsulate the time capturing infrastructure. 706 */ 707 #define time_get() trace_clock_local() 708 #define time_to_us(x) div_u64(x, 1000) 709 #define time_sub(a, b) ((a) - (b)) 710 711 /* 712 * cond_move_irq_delta_start - Forward the delta_start of a running IRQ 713 * 714 * If an IRQ is preempted by an NMI, its delta_start is pushed forward 715 * to discount the NMI interference. 716 * 717 * See get_int_safe_duration(). 718 */ 719 static inline void 720 cond_move_irq_delta_start(struct osnoise_variables *osn_var, u64 duration) 721 { 722 if (osn_var->irq.delta_start) 723 osn_var->irq.delta_start += duration; 724 } 725 726 #ifndef CONFIG_PREEMPT_RT 727 /* 728 * cond_move_softirq_delta_start - Forward the delta_start of a running softirq. 729 * 730 * If a softirq is preempted by an IRQ or NMI, its delta_start is pushed 731 * forward to discount the interference. 732 * 733 * See get_int_safe_duration(). 734 */ 735 static inline void 736 cond_move_softirq_delta_start(struct osnoise_variables *osn_var, u64 duration) 737 { 738 if (osn_var->softirq.delta_start) 739 osn_var->softirq.delta_start += duration; 740 } 741 #else /* CONFIG_PREEMPT_RT */ 742 #define cond_move_softirq_delta_start(osn_var, duration) do {} while (0) 743 #endif 744 745 /* 746 * cond_move_thread_delta_start - Forward the delta_start of a running thread 747 * 748 * If a noisy thread is preempted by an softirq, IRQ or NMI, its delta_start 749 * is pushed forward to discount the interference. 750 * 751 * See get_int_safe_duration(). 752 */ 753 static inline void 754 cond_move_thread_delta_start(struct osnoise_variables *osn_var, u64 duration) 755 { 756 if (osn_var->thread.delta_start) 757 osn_var->thread.delta_start += duration; 758 } 759 760 /* 761 * get_int_safe_duration - Get the duration of a window 762 * 763 * The irq, softirq and thread varaibles need to have its duration without 764 * the interference from higher priority interrupts. Instead of keeping a 765 * variable to discount the interrupt interference from these variables, the 766 * starting time of these variables are pushed forward with the interrupt's 767 * duration. In this way, a single variable is used to: 768 * 769 * - Know if a given window is being measured. 770 * - Account its duration. 771 * - Discount the interference. 772 * 773 * To avoid getting inconsistent values, e.g.,: 774 * 775 * now = time_get() 776 * ---> interrupt! 777 * delta_start -= int duration; 778 * <--- 779 * duration = now - delta_start; 780 * 781 * result: negative duration if the variable duration before the 782 * interrupt was smaller than the interrupt execution. 783 * 784 * A counter of interrupts is used. If the counter increased, try 785 * to capture an interference safe duration. 786 */ 787 static inline s64 788 get_int_safe_duration(struct osnoise_variables *osn_var, u64 *delta_start) 789 { 790 u64 int_counter, now; 791 s64 duration; 792 793 do { 794 int_counter = local_read(&osn_var->int_counter); 795 /* synchronize with interrupts */ 796 barrier(); 797 798 now = time_get(); 799 duration = (now - *delta_start); 800 801 /* synchronize with interrupts */ 802 barrier(); 803 } while (int_counter != local_read(&osn_var->int_counter)); 804 805 /* 806 * This is an evidence of race conditions that cause 807 * a value to be "discounted" too much. 808 */ 809 if (duration < 0) 810 osnoise_taint("Negative duration!\n"); 811 812 *delta_start = 0; 813 814 return duration; 815 } 816 817 /* 818 * 819 * set_int_safe_time - Save the current time on *time, aware of interference 820 * 821 * Get the time, taking into consideration a possible interference from 822 * higher priority interrupts. 823 * 824 * See get_int_safe_duration() for an explanation. 825 */ 826 static u64 827 set_int_safe_time(struct osnoise_variables *osn_var, u64 *time) 828 { 829 u64 int_counter; 830 831 do { 832 int_counter = local_read(&osn_var->int_counter); 833 /* synchronize with interrupts */ 834 barrier(); 835 836 *time = time_get(); 837 838 /* synchronize with interrupts */ 839 barrier(); 840 } while (int_counter != local_read(&osn_var->int_counter)); 841 842 return int_counter; 843 } 844 845 #ifdef CONFIG_TIMERLAT_TRACER 846 /* 847 * copy_int_safe_time - Copy *src into *desc aware of interference 848 */ 849 static u64 850 copy_int_safe_time(struct osnoise_variables *osn_var, u64 *dst, u64 *src) 851 { 852 u64 int_counter; 853 854 do { 855 int_counter = local_read(&osn_var->int_counter); 856 /* synchronize with interrupts */ 857 barrier(); 858 859 *dst = *src; 860 861 /* synchronize with interrupts */ 862 barrier(); 863 } while (int_counter != local_read(&osn_var->int_counter)); 864 865 return int_counter; 866 } 867 #endif /* CONFIG_TIMERLAT_TRACER */ 868 869 /* 870 * trace_osnoise_callback - NMI entry/exit callback 871 * 872 * This function is called at the entry and exit NMI code. The bool enter 873 * distinguishes between either case. This function is used to note a NMI 874 * occurrence, compute the noise caused by the NMI, and to remove the noise 875 * it is potentially causing on other interference variables. 876 */ 877 void trace_osnoise_callback(bool enter) 878 { 879 struct osnoise_variables *osn_var = this_cpu_osn_var(); 880 u64 duration; 881 882 if (!osn_var->sampling) 883 return; 884 885 /* 886 * Currently trace_clock_local() calls sched_clock() and the 887 * generic version is not NMI safe. 888 */ 889 if (!IS_ENABLED(CONFIG_GENERIC_SCHED_CLOCK)) { 890 if (enter) { 891 osn_var->nmi.delta_start = time_get(); 892 local_inc(&osn_var->int_counter); 893 } else { 894 duration = time_get() - osn_var->nmi.delta_start; 895 896 trace_nmi_noise(osn_var->nmi.delta_start, duration); 897 898 cond_move_irq_delta_start(osn_var, duration); 899 cond_move_softirq_delta_start(osn_var, duration); 900 cond_move_thread_delta_start(osn_var, duration); 901 } 902 } 903 904 if (enter) 905 osn_var->nmi.count++; 906 } 907 908 /* 909 * osnoise_trace_irq_entry - Note the starting of an IRQ 910 * 911 * Save the starting time of an IRQ. As IRQs are non-preemptive to other IRQs, 912 * it is safe to use a single variable (ons_var->irq) to save the statistics. 913 * The arrival_time is used to report... the arrival time. The delta_start 914 * is used to compute the duration at the IRQ exit handler. See 915 * cond_move_irq_delta_start(). 916 */ 917 void osnoise_trace_irq_entry(int id) 918 { 919 struct osnoise_variables *osn_var = this_cpu_osn_var(); 920 921 if (!osn_var->sampling) 922 return; 923 /* 924 * This value will be used in the report, but not to compute 925 * the execution time, so it is safe to get it unsafe. 926 */ 927 osn_var->irq.arrival_time = time_get(); 928 set_int_safe_time(osn_var, &osn_var->irq.delta_start); 929 osn_var->irq.count++; 930 931 local_inc(&osn_var->int_counter); 932 } 933 934 /* 935 * osnoise_irq_exit - Note the end of an IRQ, sava data and trace 936 * 937 * Computes the duration of the IRQ noise, and trace it. Also discounts the 938 * interference from other sources of noise could be currently being accounted. 939 */ 940 void osnoise_trace_irq_exit(int id, const char *desc) 941 { 942 struct osnoise_variables *osn_var = this_cpu_osn_var(); 943 s64 duration; 944 945 if (!osn_var->sampling) 946 return; 947 948 duration = get_int_safe_duration(osn_var, &osn_var->irq.delta_start); 949 trace_irq_noise(id, desc, osn_var->irq.arrival_time, duration); 950 osn_var->irq.arrival_time = 0; 951 cond_move_softirq_delta_start(osn_var, duration); 952 cond_move_thread_delta_start(osn_var, duration); 953 } 954 955 /* 956 * trace_irqentry_callback - Callback to the irq:irq_entry traceevent 957 * 958 * Used to note the starting of an IRQ occurece. 959 */ 960 static void trace_irqentry_callback(void *data, int irq, 961 struct irqaction *action) 962 { 963 osnoise_trace_irq_entry(irq); 964 } 965 966 /* 967 * trace_irqexit_callback - Callback to the irq:irq_exit traceevent 968 * 969 * Used to note the end of an IRQ occurece. 970 */ 971 static void trace_irqexit_callback(void *data, int irq, 972 struct irqaction *action, int ret) 973 { 974 osnoise_trace_irq_exit(irq, action->name); 975 } 976 977 /* 978 * arch specific register function. 979 */ 980 int __weak osnoise_arch_register(void) 981 { 982 return 0; 983 } 984 985 /* 986 * arch specific unregister function. 987 */ 988 void __weak osnoise_arch_unregister(void) 989 { 990 return; 991 } 992 993 /* 994 * hook_irq_events - Hook IRQ handling events 995 * 996 * This function hooks the IRQ related callbacks to the respective trace 997 * events. 998 */ 999 static int hook_irq_events(void) 1000 { 1001 int ret; 1002 1003 ret = register_trace_irq_handler_entry(trace_irqentry_callback, NULL); 1004 if (ret) 1005 goto out_err; 1006 1007 ret = register_trace_irq_handler_exit(trace_irqexit_callback, NULL); 1008 if (ret) 1009 goto out_unregister_entry; 1010 1011 ret = osnoise_arch_register(); 1012 if (ret) 1013 goto out_irq_exit; 1014 1015 return 0; 1016 1017 out_irq_exit: 1018 unregister_trace_irq_handler_exit(trace_irqexit_callback, NULL); 1019 out_unregister_entry: 1020 unregister_trace_irq_handler_entry(trace_irqentry_callback, NULL); 1021 out_err: 1022 return -EINVAL; 1023 } 1024 1025 /* 1026 * unhook_irq_events - Unhook IRQ handling events 1027 * 1028 * This function unhooks the IRQ related callbacks to the respective trace 1029 * events. 1030 */ 1031 static void unhook_irq_events(void) 1032 { 1033 osnoise_arch_unregister(); 1034 unregister_trace_irq_handler_exit(trace_irqexit_callback, NULL); 1035 unregister_trace_irq_handler_entry(trace_irqentry_callback, NULL); 1036 } 1037 1038 #ifndef CONFIG_PREEMPT_RT 1039 /* 1040 * trace_softirq_entry_callback - Note the starting of a softirq 1041 * 1042 * Save the starting time of a softirq. As softirqs are non-preemptive to 1043 * other softirqs, it is safe to use a single variable (ons_var->softirq) 1044 * to save the statistics. The arrival_time is used to report... the 1045 * arrival time. The delta_start is used to compute the duration at the 1046 * softirq exit handler. See cond_move_softirq_delta_start(). 1047 */ 1048 static void trace_softirq_entry_callback(void *data, unsigned int vec_nr) 1049 { 1050 struct osnoise_variables *osn_var = this_cpu_osn_var(); 1051 1052 if (!osn_var->sampling) 1053 return; 1054 /* 1055 * This value will be used in the report, but not to compute 1056 * the execution time, so it is safe to get it unsafe. 1057 */ 1058 osn_var->softirq.arrival_time = time_get(); 1059 set_int_safe_time(osn_var, &osn_var->softirq.delta_start); 1060 osn_var->softirq.count++; 1061 1062 local_inc(&osn_var->int_counter); 1063 } 1064 1065 /* 1066 * trace_softirq_exit_callback - Note the end of an softirq 1067 * 1068 * Computes the duration of the softirq noise, and trace it. Also discounts the 1069 * interference from other sources of noise could be currently being accounted. 1070 */ 1071 static void trace_softirq_exit_callback(void *data, unsigned int vec_nr) 1072 { 1073 struct osnoise_variables *osn_var = this_cpu_osn_var(); 1074 s64 duration; 1075 1076 if (!osn_var->sampling) 1077 return; 1078 1079 if (unlikely(timerlat_enabled())) 1080 if (!timerlat_softirq_exit(osn_var)) 1081 return; 1082 1083 duration = get_int_safe_duration(osn_var, &osn_var->softirq.delta_start); 1084 trace_softirq_noise(vec_nr, osn_var->softirq.arrival_time, duration); 1085 cond_move_thread_delta_start(osn_var, duration); 1086 osn_var->softirq.arrival_time = 0; 1087 } 1088 1089 /* 1090 * hook_softirq_events - Hook softirq handling events 1091 * 1092 * This function hooks the softirq related callbacks to the respective trace 1093 * events. 1094 */ 1095 static int hook_softirq_events(void) 1096 { 1097 int ret; 1098 1099 ret = register_trace_softirq_entry(trace_softirq_entry_callback, NULL); 1100 if (ret) 1101 goto out_err; 1102 1103 ret = register_trace_softirq_exit(trace_softirq_exit_callback, NULL); 1104 if (ret) 1105 goto out_unreg_entry; 1106 1107 return 0; 1108 1109 out_unreg_entry: 1110 unregister_trace_softirq_entry(trace_softirq_entry_callback, NULL); 1111 out_err: 1112 return -EINVAL; 1113 } 1114 1115 /* 1116 * unhook_softirq_events - Unhook softirq handling events 1117 * 1118 * This function hooks the softirq related callbacks to the respective trace 1119 * events. 1120 */ 1121 static void unhook_softirq_events(void) 1122 { 1123 unregister_trace_softirq_entry(trace_softirq_entry_callback, NULL); 1124 unregister_trace_softirq_exit(trace_softirq_exit_callback, NULL); 1125 } 1126 #else /* CONFIG_PREEMPT_RT */ 1127 /* 1128 * softirq are threads on the PREEMPT_RT mode. 1129 */ 1130 static int hook_softirq_events(void) 1131 { 1132 return 0; 1133 } 1134 static void unhook_softirq_events(void) 1135 { 1136 } 1137 #endif 1138 1139 /* 1140 * thread_entry - Record the starting of a thread noise window 1141 * 1142 * It saves the context switch time for a noisy thread, and increments 1143 * the interference counters. 1144 */ 1145 static void 1146 thread_entry(struct osnoise_variables *osn_var, struct task_struct *t) 1147 { 1148 if (!osn_var->sampling) 1149 return; 1150 /* 1151 * The arrival time will be used in the report, but not to compute 1152 * the execution time, so it is safe to get it unsafe. 1153 */ 1154 osn_var->thread.arrival_time = time_get(); 1155 1156 set_int_safe_time(osn_var, &osn_var->thread.delta_start); 1157 1158 osn_var->thread.count++; 1159 local_inc(&osn_var->int_counter); 1160 } 1161 1162 /* 1163 * thread_exit - Report the end of a thread noise window 1164 * 1165 * It computes the total noise from a thread, tracing if needed. 1166 */ 1167 static void 1168 thread_exit(struct osnoise_variables *osn_var, struct task_struct *t) 1169 { 1170 s64 duration; 1171 1172 if (!osn_var->sampling) 1173 return; 1174 1175 if (unlikely(timerlat_enabled())) 1176 if (!timerlat_thread_exit(osn_var)) 1177 return; 1178 1179 duration = get_int_safe_duration(osn_var, &osn_var->thread.delta_start); 1180 1181 trace_thread_noise(t, osn_var->thread.arrival_time, duration); 1182 1183 osn_var->thread.arrival_time = 0; 1184 } 1185 1186 #ifdef CONFIG_TIMERLAT_TRACER 1187 /* 1188 * osnoise_stop_exception - Stop tracing and the tracer. 1189 */ 1190 static __always_inline void osnoise_stop_exception(char *msg, int cpu) 1191 { 1192 struct osnoise_instance *inst; 1193 struct trace_array *tr; 1194 1195 rcu_read_lock(); 1196 list_for_each_entry_rcu(inst, &osnoise_instances, list) { 1197 tr = inst->tr; 1198 trace_array_printk_buf(tr->array_buffer.buffer, _THIS_IP_, 1199 "stop tracing hit on cpu %d due to exception: %s\n", 1200 smp_processor_id(), 1201 msg); 1202 1203 if (test_bit(OSN_PANIC_ON_STOP, &osnoise_options)) 1204 panic("tracer hit on cpu %d due to exception: %s\n", 1205 smp_processor_id(), 1206 msg); 1207 1208 tracer_tracing_off(tr); 1209 } 1210 rcu_read_unlock(); 1211 } 1212 1213 /* 1214 * trace_sched_migrate_callback - sched:sched_migrate_task trace event handler 1215 * 1216 * his function is hooked to the sched:sched_migrate_task trace event, and monitors 1217 * timerlat user-space thread migration. 1218 */ 1219 static void trace_sched_migrate_callback(void *data, struct task_struct *p, int dest_cpu) 1220 { 1221 struct osnoise_variables *osn_var; 1222 long cpu = task_cpu(p); 1223 1224 osn_var = per_cpu_ptr(&per_cpu_osnoise_var, cpu); 1225 if (osn_var->pid == p->pid && dest_cpu != cpu) { 1226 per_cpu_ptr(&per_cpu_timerlat_var, cpu)->uthread_migrate = 1; 1227 osnoise_taint("timerlat user-thread migrated\n"); 1228 osnoise_stop_exception("timerlat user-thread migrated", cpu); 1229 } 1230 } 1231 1232 static bool monitor_enabled; 1233 1234 static int register_migration_monitor(void) 1235 { 1236 int ret = 0; 1237 1238 /* 1239 * Timerlat thread migration check is only required when running timerlat in user-space. 1240 * Thus, enable callback only if timerlat is set with no workload. 1241 */ 1242 if (timerlat_enabled() && !test_bit(OSN_WORKLOAD, &osnoise_options)) { 1243 if (WARN_ON_ONCE(monitor_enabled)) 1244 return 0; 1245 1246 ret = register_trace_sched_migrate_task(trace_sched_migrate_callback, NULL); 1247 if (!ret) 1248 monitor_enabled = true; 1249 } 1250 1251 return ret; 1252 } 1253 1254 static void unregister_migration_monitor(void) 1255 { 1256 if (!monitor_enabled) 1257 return; 1258 1259 unregister_trace_sched_migrate_task(trace_sched_migrate_callback, NULL); 1260 monitor_enabled = false; 1261 } 1262 #else 1263 static int register_migration_monitor(void) 1264 { 1265 return 0; 1266 } 1267 static void unregister_migration_monitor(void) {} 1268 #endif 1269 /* 1270 * trace_sched_switch - sched:sched_switch trace event handler 1271 * 1272 * This function is hooked to the sched:sched_switch trace event, and it is 1273 * used to record the beginning and to report the end of a thread noise window. 1274 */ 1275 static void 1276 trace_sched_switch_callback(void *data, bool preempt, 1277 struct task_struct *p, 1278 struct task_struct *n, 1279 unsigned int prev_state) 1280 { 1281 struct osnoise_variables *osn_var = this_cpu_osn_var(); 1282 int workload = test_bit(OSN_WORKLOAD, &osnoise_options); 1283 1284 if ((p->pid != osn_var->pid) || !workload) 1285 thread_exit(osn_var, p); 1286 1287 if ((n->pid != osn_var->pid) || !workload) 1288 thread_entry(osn_var, n); 1289 } 1290 1291 /* 1292 * hook_thread_events - Hook the instrumentation for thread noise 1293 * 1294 * Hook the osnoise tracer callbacks to handle the noise from other 1295 * threads on the necessary kernel events. 1296 */ 1297 static int hook_thread_events(void) 1298 { 1299 int ret; 1300 1301 ret = register_trace_sched_switch(trace_sched_switch_callback, NULL); 1302 if (ret) 1303 return -EINVAL; 1304 1305 ret = register_migration_monitor(); 1306 if (ret) 1307 goto out_unreg; 1308 1309 return 0; 1310 1311 out_unreg: 1312 unregister_trace_sched_switch(trace_sched_switch_callback, NULL); 1313 return -EINVAL; 1314 } 1315 1316 /* 1317 * unhook_thread_events - unhook the instrumentation for thread noise 1318 * 1319 * Unook the osnoise tracer callbacks to handle the noise from other 1320 * threads on the necessary kernel events. 1321 */ 1322 static void unhook_thread_events(void) 1323 { 1324 unregister_trace_sched_switch(trace_sched_switch_callback, NULL); 1325 unregister_migration_monitor(); 1326 } 1327 1328 /* 1329 * save_osn_sample_stats - Save the osnoise_sample statistics 1330 * 1331 * Save the osnoise_sample statistics before the sampling phase. These 1332 * values will be used later to compute the diff betwneen the statistics 1333 * before and after the osnoise sampling. 1334 */ 1335 static void 1336 save_osn_sample_stats(struct osnoise_variables *osn_var, struct osnoise_sample *s) 1337 { 1338 s->nmi_count = osn_var->nmi.count; 1339 s->irq_count = osn_var->irq.count; 1340 s->softirq_count = osn_var->softirq.count; 1341 s->thread_count = osn_var->thread.count; 1342 } 1343 1344 /* 1345 * diff_osn_sample_stats - Compute the osnoise_sample statistics 1346 * 1347 * After a sample period, compute the difference on the osnoise_sample 1348 * statistics. The struct osnoise_sample *s contains the statistics saved via 1349 * save_osn_sample_stats() before the osnoise sampling. 1350 */ 1351 static void 1352 diff_osn_sample_stats(struct osnoise_variables *osn_var, struct osnoise_sample *s) 1353 { 1354 s->nmi_count = osn_var->nmi.count - s->nmi_count; 1355 s->irq_count = osn_var->irq.count - s->irq_count; 1356 s->softirq_count = osn_var->softirq.count - s->softirq_count; 1357 s->thread_count = osn_var->thread.count - s->thread_count; 1358 } 1359 1360 /* 1361 * osnoise_stop_tracing - Stop tracing and the tracer. 1362 */ 1363 static __always_inline void osnoise_stop_tracing(void) 1364 { 1365 struct osnoise_instance *inst; 1366 struct trace_array *tr; 1367 1368 rcu_read_lock(); 1369 list_for_each_entry_rcu(inst, &osnoise_instances, list) { 1370 tr = inst->tr; 1371 trace_array_printk_buf(tr->array_buffer.buffer, _THIS_IP_, 1372 "stop tracing hit on cpu %d\n", smp_processor_id()); 1373 1374 if (test_bit(OSN_PANIC_ON_STOP, &osnoise_options)) 1375 panic("tracer hit stop condition on CPU %d\n", smp_processor_id()); 1376 1377 tracer_tracing_off(tr); 1378 } 1379 rcu_read_unlock(); 1380 } 1381 1382 /* 1383 * osnoise_has_tracing_on - Check if there is at least one instance on 1384 */ 1385 static __always_inline int osnoise_has_tracing_on(void) 1386 { 1387 struct osnoise_instance *inst; 1388 int trace_is_on = 0; 1389 1390 rcu_read_lock(); 1391 list_for_each_entry_rcu(inst, &osnoise_instances, list) 1392 trace_is_on += tracer_tracing_is_on(inst->tr); 1393 rcu_read_unlock(); 1394 1395 return trace_is_on; 1396 } 1397 1398 /* 1399 * notify_new_max_latency - Notify a new max latency via fsnotify interface. 1400 */ 1401 static void notify_new_max_latency(u64 latency) 1402 { 1403 struct osnoise_instance *inst; 1404 struct trace_array *tr; 1405 1406 rcu_read_lock(); 1407 list_for_each_entry_rcu(inst, &osnoise_instances, list) { 1408 tr = inst->tr; 1409 if (tracer_tracing_is_on(tr) && tr->max_latency < latency) { 1410 tr->max_latency = latency; 1411 latency_fsnotify(tr); 1412 } 1413 } 1414 rcu_read_unlock(); 1415 } 1416 1417 /* 1418 * run_osnoise - Sample the time and look for osnoise 1419 * 1420 * Used to capture the time, looking for potential osnoise latency repeatedly. 1421 * Different from hwlat_detector, it is called with preemption and interrupts 1422 * enabled. This allows irqs, softirqs and threads to run, interfering on the 1423 * osnoise sampling thread, as they would do with a regular thread. 1424 */ 1425 static int run_osnoise(void) 1426 { 1427 bool disable_irq = test_bit(OSN_IRQ_DISABLE, &osnoise_options); 1428 struct osnoise_variables *osn_var = this_cpu_osn_var(); 1429 u64 start, sample, last_sample; 1430 u64 last_int_count, int_count; 1431 s64 noise = 0, max_noise = 0; 1432 s64 total, last_total = 0; 1433 struct osnoise_sample s; 1434 bool disable_preemption; 1435 unsigned int threshold; 1436 u64 runtime, stop_in; 1437 u64 sum_noise = 0; 1438 int hw_count = 0; 1439 int ret = -1; 1440 1441 /* 1442 * Disabling preemption is only required if IRQs are enabled, 1443 * and the options is set on. 1444 */ 1445 disable_preemption = !disable_irq && test_bit(OSN_PREEMPT_DISABLE, &osnoise_options); 1446 1447 /* 1448 * Considers the current thread as the workload. 1449 */ 1450 osn_var->pid = current->pid; 1451 1452 /* 1453 * Save the current stats for the diff 1454 */ 1455 save_osn_sample_stats(osn_var, &s); 1456 1457 /* 1458 * if threshold is 0, use the default value of 1 us. 1459 */ 1460 threshold = tracing_thresh ? : 1000; 1461 1462 /* 1463 * Apply PREEMPT and IRQ disabled options. 1464 */ 1465 if (disable_irq) 1466 local_irq_disable(); 1467 1468 if (disable_preemption) 1469 preempt_disable(); 1470 1471 /* 1472 * Make sure NMIs see sampling first 1473 */ 1474 osn_var->sampling = true; 1475 barrier(); 1476 1477 /* 1478 * Transform the *_us config to nanoseconds to avoid the 1479 * division on the main loop. 1480 */ 1481 runtime = osnoise_data.sample_runtime * NSEC_PER_USEC; 1482 stop_in = osnoise_data.stop_tracing * NSEC_PER_USEC; 1483 1484 /* 1485 * Start timestemp 1486 */ 1487 start = time_get(); 1488 1489 /* 1490 * "previous" loop. 1491 */ 1492 last_int_count = set_int_safe_time(osn_var, &last_sample); 1493 1494 do { 1495 /* 1496 * Get sample! 1497 */ 1498 int_count = set_int_safe_time(osn_var, &sample); 1499 1500 noise = time_sub(sample, last_sample); 1501 1502 /* 1503 * This shouldn't happen. 1504 */ 1505 if (noise < 0) { 1506 osnoise_taint("negative noise!"); 1507 goto out; 1508 } 1509 1510 /* 1511 * Sample runtime. 1512 */ 1513 total = time_sub(sample, start); 1514 1515 /* 1516 * Check for possible overflows. 1517 */ 1518 if (total < last_total) { 1519 osnoise_taint("total overflow!"); 1520 break; 1521 } 1522 1523 last_total = total; 1524 1525 if (noise >= threshold) { 1526 int interference = int_count - last_int_count; 1527 1528 if (noise > max_noise) 1529 max_noise = noise; 1530 1531 if (!interference) 1532 hw_count++; 1533 1534 sum_noise += noise; 1535 1536 trace_sample_threshold(last_sample, noise, interference); 1537 1538 if (osnoise_data.stop_tracing) 1539 if (noise > stop_in) 1540 osnoise_stop_tracing(); 1541 } 1542 1543 /* 1544 * In some cases, notably when running on a nohz_full CPU with 1545 * a stopped tick PREEMPT_RCU or PREEMPT_LAZY have no way to 1546 * account for QSs. This will eventually cause unwarranted 1547 * noise as RCU forces preemption as the means of ending the 1548 * current grace period. We avoid this by calling 1549 * rcu_momentary_eqs(), which performs a zero duration EQS 1550 * allowing RCU to end the current grace period. This call 1551 * shouldn't be wrapped inside an RCU critical section. 1552 * 1553 * Normally QSs for other cases are handled through cond_resched(). 1554 * For simplicity, however, we call rcu_momentary_eqs() for all 1555 * configurations here. 1556 */ 1557 if (!disable_irq) 1558 local_irq_disable(); 1559 1560 rcu_momentary_eqs(); 1561 1562 if (!disable_irq) 1563 local_irq_enable(); 1564 1565 /* 1566 * For the non-preemptive kernel config: let threads runs, if 1567 * they so wish, unless set not do to so. 1568 */ 1569 if (!disable_irq && !disable_preemption) 1570 cond_resched(); 1571 1572 last_sample = sample; 1573 last_int_count = int_count; 1574 1575 } while (total < runtime && !kthread_should_stop()); 1576 1577 /* 1578 * Finish the above in the view for interrupts. 1579 */ 1580 barrier(); 1581 1582 osn_var->sampling = false; 1583 1584 /* 1585 * Make sure sampling data is no longer updated. 1586 */ 1587 barrier(); 1588 1589 /* 1590 * Return to the preemptive state. 1591 */ 1592 if (disable_preemption) 1593 preempt_enable(); 1594 1595 if (disable_irq) 1596 local_irq_enable(); 1597 1598 /* 1599 * Save noise info. 1600 */ 1601 s.noise = time_to_us(sum_noise); 1602 s.runtime = time_to_us(total); 1603 s.max_sample = time_to_us(max_noise); 1604 s.hw_count = hw_count; 1605 1606 /* Save interference stats info */ 1607 diff_osn_sample_stats(osn_var, &s); 1608 1609 trace_osnoise_sample(&s); 1610 1611 notify_new_max_latency(max_noise); 1612 1613 if (osnoise_data.stop_tracing_total) 1614 if (s.noise > osnoise_data.stop_tracing_total) 1615 osnoise_stop_tracing(); 1616 1617 return 0; 1618 out: 1619 return ret; 1620 } 1621 1622 static struct cpumask osnoise_cpumask; 1623 static struct cpumask save_cpumask; 1624 static struct cpumask kthread_cpumask; 1625 1626 /* 1627 * osnoise_sleep - sleep until the next period 1628 */ 1629 static void osnoise_sleep(bool skip_period) 1630 { 1631 u64 interval; 1632 ktime_t wake_time; 1633 1634 mutex_lock(&interface_lock); 1635 if (skip_period) 1636 interval = osnoise_data.sample_period; 1637 else 1638 interval = osnoise_data.sample_period - osnoise_data.sample_runtime; 1639 mutex_unlock(&interface_lock); 1640 1641 /* 1642 * differently from hwlat_detector, the osnoise tracer can run 1643 * without a pause because preemption is on. 1644 */ 1645 if (!interval) { 1646 /* Let synchronize_rcu_tasks() make progress */ 1647 cond_resched_tasks_rcu_qs(); 1648 return; 1649 } 1650 1651 wake_time = ktime_add_us(ktime_get(), interval); 1652 __set_current_state(TASK_INTERRUPTIBLE); 1653 1654 while (schedule_hrtimeout(&wake_time, HRTIMER_MODE_ABS)) { 1655 if (kthread_should_stop()) 1656 break; 1657 } 1658 } 1659 1660 /* 1661 * osnoise_migration_pending - checks if the task needs to migrate 1662 * 1663 * osnoise/timerlat threads are per-cpu. If there is a pending request to 1664 * migrate the thread away from the current CPU, something bad has happened. 1665 * Play the good citizen and leave. 1666 * 1667 * Returns 0 if it is safe to continue, 1 otherwise. 1668 */ 1669 static inline int osnoise_migration_pending(void) 1670 { 1671 if (!current->migration_pending) 1672 return 0; 1673 1674 /* 1675 * If migration is pending, there is a task waiting for the 1676 * tracer to enable migration. The tracer does not allow migration, 1677 * thus: taint and leave to unblock the blocked thread. 1678 */ 1679 osnoise_taint("migration requested to osnoise threads, leaving."); 1680 1681 /* 1682 * Unset this thread from the threads managed by the interface. 1683 * The tracers are responsible for cleaning their env before 1684 * exiting. 1685 */ 1686 mutex_lock(&interface_lock); 1687 this_cpu_osn_var()->kthread = NULL; 1688 cpumask_clear_cpu(smp_processor_id(), &kthread_cpumask); 1689 mutex_unlock(&interface_lock); 1690 1691 return 1; 1692 } 1693 1694 /* 1695 * osnoise_main - The osnoise detection kernel thread 1696 * 1697 * Calls run_osnoise() function to measure the osnoise for the configured runtime, 1698 * every period. 1699 */ 1700 static int osnoise_main(void *data) 1701 { 1702 unsigned long flags; 1703 1704 /* 1705 * This thread was created pinned to the CPU using PF_NO_SETAFFINITY. 1706 * The problem is that cgroup does not allow PF_NO_SETAFFINITY thread. 1707 * 1708 * To work around this limitation, disable migration and remove the 1709 * flag. 1710 */ 1711 migrate_disable(); 1712 raw_spin_lock_irqsave(¤t->pi_lock, flags); 1713 current->flags &= ~(PF_NO_SETAFFINITY); 1714 raw_spin_unlock_irqrestore(¤t->pi_lock, flags); 1715 1716 while (!kthread_should_stop()) { 1717 if (osnoise_migration_pending()) 1718 break; 1719 1720 /* skip a period if tracing is off on all instances */ 1721 if (!osnoise_has_tracing_on()) { 1722 osnoise_sleep(true); 1723 continue; 1724 } 1725 1726 run_osnoise(); 1727 osnoise_sleep(false); 1728 } 1729 1730 migrate_enable(); 1731 return 0; 1732 } 1733 1734 #ifdef CONFIG_TIMERLAT_TRACER 1735 /* 1736 * timerlat_irq - hrtimer handler for timerlat. 1737 */ 1738 static enum hrtimer_restart timerlat_irq(struct hrtimer *timer) 1739 { 1740 struct osnoise_variables *osn_var = this_cpu_osn_var(); 1741 struct timerlat_variables *tlat; 1742 struct timerlat_sample s; 1743 u64 now; 1744 u64 diff; 1745 1746 /* 1747 * I am not sure if the timer was armed for this CPU. So, get 1748 * the timerlat struct from the timer itself, not from this 1749 * CPU. 1750 */ 1751 tlat = container_of(timer, struct timerlat_variables, timer); 1752 1753 now = ktime_to_ns(hrtimer_cb_get_time(&tlat->timer)); 1754 1755 /* 1756 * Enable the osnoise: events for thread an softirq. 1757 */ 1758 tlat->tracing_thread = true; 1759 1760 osn_var->thread.arrival_time = time_get(); 1761 1762 /* 1763 * A hardirq is running: the timer IRQ. It is for sure preempting 1764 * a thread, and potentially preempting a softirq. 1765 * 1766 * At this point, it is not interesting to know the duration of the 1767 * preempted thread (and maybe softirq), but how much time they will 1768 * delay the beginning of the execution of the timer thread. 1769 * 1770 * To get the correct (net) delay added by the softirq, its delta_start 1771 * is set as the IRQ one. In this way, at the return of the IRQ, the delta 1772 * start of the sofitrq will be zeroed, accounting then only the time 1773 * after that. 1774 * 1775 * The thread follows the same principle. However, if a softirq is 1776 * running, the thread needs to receive the softirq delta_start. The 1777 * reason being is that the softirq will be the last to be unfolded, 1778 * resseting the thread delay to zero. 1779 * 1780 * The PREEMPT_RT is a special case, though. As softirqs run as threads 1781 * on RT, moving the thread is enough. 1782 */ 1783 if (!IS_ENABLED(CONFIG_PREEMPT_RT) && osn_var->softirq.delta_start) { 1784 copy_int_safe_time(osn_var, &osn_var->thread.delta_start, 1785 &osn_var->softirq.delta_start); 1786 1787 copy_int_safe_time(osn_var, &osn_var->softirq.delta_start, 1788 &osn_var->irq.delta_start); 1789 } else { 1790 copy_int_safe_time(osn_var, &osn_var->thread.delta_start, 1791 &osn_var->irq.delta_start); 1792 } 1793 1794 /* 1795 * Compute the current time with the expected time. 1796 */ 1797 diff = now - tlat->abs_period; 1798 1799 tlat->count++; 1800 s.seqnum = tlat->count; 1801 s.timer_latency = diff; 1802 s.context = IRQ_CONTEXT; 1803 1804 trace_timerlat_sample(&s); 1805 1806 if (osnoise_data.stop_tracing) { 1807 if (time_to_us(diff) >= osnoise_data.stop_tracing) { 1808 1809 /* 1810 * At this point, if stop_tracing is set and <= print_stack, 1811 * print_stack is set and would be printed in the thread handler. 1812 * 1813 * Thus, print the stack trace as it is helpful to define the 1814 * root cause of an IRQ latency. 1815 */ 1816 if (osnoise_data.stop_tracing <= osnoise_data.print_stack) { 1817 timerlat_save_stack(0); 1818 timerlat_dump_stack(time_to_us(diff)); 1819 } 1820 1821 osnoise_stop_tracing(); 1822 notify_new_max_latency(diff); 1823 1824 wake_up_process(tlat->kthread); 1825 1826 return HRTIMER_NORESTART; 1827 } 1828 } 1829 1830 wake_up_process(tlat->kthread); 1831 1832 if (osnoise_data.print_stack) 1833 timerlat_save_stack(0); 1834 1835 return HRTIMER_NORESTART; 1836 } 1837 1838 /* 1839 * wait_next_period - Wait for the next period for timerlat 1840 */ 1841 static int wait_next_period(struct timerlat_variables *tlat) 1842 { 1843 ktime_t next_abs_period, now; 1844 u64 rel_period = osnoise_data.timerlat_period * 1000; 1845 1846 now = hrtimer_cb_get_time(&tlat->timer); 1847 next_abs_period = ns_to_ktime(tlat->abs_period + rel_period); 1848 1849 /* 1850 * Save the next abs_period. 1851 */ 1852 tlat->abs_period = (u64) ktime_to_ns(next_abs_period); 1853 1854 /* 1855 * If the new abs_period is in the past, skip the activation. 1856 */ 1857 while (ktime_compare(now, next_abs_period) > 0) { 1858 next_abs_period = ns_to_ktime(tlat->abs_period + rel_period); 1859 tlat->abs_period = (u64) ktime_to_ns(next_abs_period); 1860 } 1861 1862 set_current_state(TASK_INTERRUPTIBLE); 1863 1864 hrtimer_start(&tlat->timer, next_abs_period, HRTIMER_MODE_ABS_PINNED_HARD); 1865 schedule(); 1866 return 1; 1867 } 1868 1869 /* 1870 * timerlat_main- Timerlat main 1871 */ 1872 static int timerlat_main(void *data) 1873 { 1874 struct osnoise_variables *osn_var = this_cpu_osn_var(); 1875 struct timerlat_variables *tlat = this_cpu_tmr_var(); 1876 struct timerlat_sample s; 1877 struct sched_param sp; 1878 unsigned long flags; 1879 u64 now, diff; 1880 1881 /* 1882 * Make the thread RT, that is how cyclictest is usually used. 1883 */ 1884 sp.sched_priority = DEFAULT_TIMERLAT_PRIO; 1885 sched_setscheduler_nocheck(current, SCHED_FIFO, &sp); 1886 1887 /* 1888 * This thread was created pinned to the CPU using PF_NO_SETAFFINITY. 1889 * The problem is that cgroup does not allow PF_NO_SETAFFINITY thread. 1890 * 1891 * To work around this limitation, disable migration and remove the 1892 * flag. 1893 */ 1894 migrate_disable(); 1895 raw_spin_lock_irqsave(¤t->pi_lock, flags); 1896 current->flags &= ~(PF_NO_SETAFFINITY); 1897 raw_spin_unlock_irqrestore(¤t->pi_lock, flags); 1898 1899 tlat->count = 0; 1900 tlat->tracing_thread = false; 1901 1902 hrtimer_setup(&tlat->timer, timerlat_irq, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED_HARD); 1903 tlat->kthread = current; 1904 osn_var->pid = current->pid; 1905 /* 1906 * Anotate the arrival time. 1907 */ 1908 tlat->abs_period = hrtimer_cb_get_time(&tlat->timer); 1909 1910 wait_next_period(tlat); 1911 1912 osn_var->sampling = 1; 1913 1914 while (!kthread_should_stop()) { 1915 1916 now = ktime_to_ns(hrtimer_cb_get_time(&tlat->timer)); 1917 diff = now - tlat->abs_period; 1918 1919 s.seqnum = tlat->count; 1920 s.timer_latency = diff; 1921 s.context = THREAD_CONTEXT; 1922 1923 trace_timerlat_sample(&s); 1924 1925 notify_new_max_latency(diff); 1926 1927 timerlat_dump_stack(time_to_us(diff)); 1928 1929 tlat->tracing_thread = false; 1930 if (osnoise_data.stop_tracing_total) 1931 if (time_to_us(diff) >= osnoise_data.stop_tracing_total) 1932 osnoise_stop_tracing(); 1933 1934 if (osnoise_migration_pending()) 1935 break; 1936 1937 wait_next_period(tlat); 1938 } 1939 1940 hrtimer_cancel(&tlat->timer); 1941 migrate_enable(); 1942 return 0; 1943 } 1944 #else /* CONFIG_TIMERLAT_TRACER */ 1945 static int timerlat_main(void *data) 1946 { 1947 return 0; 1948 } 1949 #endif /* CONFIG_TIMERLAT_TRACER */ 1950 1951 /* 1952 * stop_kthread - stop a workload thread 1953 */ 1954 static void stop_kthread(unsigned int cpu) 1955 { 1956 struct task_struct *kthread; 1957 1958 kthread = xchg_relaxed(&(per_cpu(per_cpu_osnoise_var, cpu).kthread), NULL); 1959 if (kthread) { 1960 if (cpumask_test_and_clear_cpu(cpu, &kthread_cpumask) && 1961 !WARN_ON(!test_bit(OSN_WORKLOAD, &osnoise_options))) { 1962 kthread_stop(kthread); 1963 } else if (!WARN_ON(test_bit(OSN_WORKLOAD, &osnoise_options))) { 1964 /* 1965 * This is a user thread waiting on the timerlat_fd. We need 1966 * to close all users, and the best way to guarantee this is 1967 * by killing the thread. NOTE: this is a purpose specific file. 1968 */ 1969 kill_pid(kthread->thread_pid, SIGKILL, 1); 1970 put_task_struct(kthread); 1971 } 1972 } else { 1973 /* if no workload, just return */ 1974 if (!test_bit(OSN_WORKLOAD, &osnoise_options)) { 1975 /* 1976 * This is set in the osnoise tracer case. 1977 */ 1978 per_cpu(per_cpu_osnoise_var, cpu).sampling = false; 1979 barrier(); 1980 } 1981 } 1982 } 1983 1984 /* 1985 * stop_per_cpu_kthread - Stop per-cpu threads 1986 * 1987 * Stop the osnoise sampling htread. Use this on unload and at system 1988 * shutdown. 1989 */ 1990 static void stop_per_cpu_kthreads(void) 1991 { 1992 int cpu; 1993 1994 cpus_read_lock(); 1995 1996 for_each_online_cpu(cpu) 1997 stop_kthread(cpu); 1998 1999 cpus_read_unlock(); 2000 } 2001 2002 /* 2003 * start_kthread - Start a workload tread 2004 */ 2005 static int start_kthread(unsigned int cpu) 2006 { 2007 struct task_struct *kthread; 2008 void *main = osnoise_main; 2009 char comm[24]; 2010 2011 /* Do not start a new thread if it is already running */ 2012 if (per_cpu(per_cpu_osnoise_var, cpu).kthread) 2013 return 0; 2014 2015 if (timerlat_enabled()) { 2016 snprintf(comm, 24, "timerlat/%d", cpu); 2017 main = timerlat_main; 2018 } else { 2019 /* if no workload, just return */ 2020 if (!test_bit(OSN_WORKLOAD, &osnoise_options)) { 2021 per_cpu(per_cpu_osnoise_var, cpu).sampling = true; 2022 barrier(); 2023 return 0; 2024 } 2025 snprintf(comm, 24, "osnoise/%d", cpu); 2026 } 2027 2028 kthread = kthread_run_on_cpu(main, NULL, cpu, comm); 2029 2030 if (IS_ERR(kthread)) { 2031 pr_err(BANNER "could not start sampling thread\n"); 2032 stop_per_cpu_kthreads(); 2033 return -ENOMEM; 2034 } 2035 2036 per_cpu(per_cpu_osnoise_var, cpu).kthread = kthread; 2037 cpumask_set_cpu(cpu, &kthread_cpumask); 2038 2039 return 0; 2040 } 2041 2042 /* 2043 * start_per_cpu_kthread - Kick off per-cpu osnoise sampling kthreads 2044 * 2045 * This starts the kernel thread that will look for osnoise on many 2046 * cpus. 2047 */ 2048 static int start_per_cpu_kthreads(void) 2049 { 2050 struct cpumask *current_mask = &save_cpumask; 2051 int retval = 0; 2052 int cpu; 2053 2054 if (!test_bit(OSN_WORKLOAD, &osnoise_options)) { 2055 if (timerlat_enabled()) 2056 return 0; 2057 } 2058 2059 cpus_read_lock(); 2060 /* 2061 * Run only on online CPUs in which osnoise is allowed to run. 2062 */ 2063 cpumask_and(current_mask, cpu_online_mask, &osnoise_cpumask); 2064 2065 for_each_possible_cpu(cpu) { 2066 if (cpumask_test_and_clear_cpu(cpu, &kthread_cpumask)) { 2067 struct task_struct *kthread; 2068 2069 kthread = xchg_relaxed(&(per_cpu(per_cpu_osnoise_var, cpu).kthread), NULL); 2070 if (!WARN_ON(!kthread)) 2071 kthread_stop(kthread); 2072 } 2073 } 2074 2075 for_each_cpu(cpu, current_mask) { 2076 retval = start_kthread(cpu); 2077 if (retval) { 2078 cpus_read_unlock(); 2079 stop_per_cpu_kthreads(); 2080 return retval; 2081 } 2082 } 2083 2084 cpus_read_unlock(); 2085 2086 return retval; 2087 } 2088 2089 #ifdef CONFIG_HOTPLUG_CPU 2090 static void osnoise_hotplug_workfn(struct work_struct *dummy) 2091 { 2092 unsigned int cpu = smp_processor_id(); 2093 2094 guard(mutex)(&trace_types_lock); 2095 2096 if (!osnoise_has_registered_instances()) 2097 return; 2098 2099 guard(mutex)(&interface_lock); 2100 guard(cpus_read_lock)(); 2101 2102 if (!cpu_online(cpu)) 2103 return; 2104 2105 if (!cpumask_test_cpu(cpu, &osnoise_cpumask)) 2106 return; 2107 2108 start_kthread(cpu); 2109 } 2110 2111 static DECLARE_WORK(osnoise_hotplug_work, osnoise_hotplug_workfn); 2112 2113 /* 2114 * osnoise_cpu_init - CPU hotplug online callback function 2115 */ 2116 static int osnoise_cpu_init(unsigned int cpu) 2117 { 2118 schedule_work_on(cpu, &osnoise_hotplug_work); 2119 return 0; 2120 } 2121 2122 /* 2123 * osnoise_cpu_die - CPU hotplug offline callback function 2124 */ 2125 static int osnoise_cpu_die(unsigned int cpu) 2126 { 2127 stop_kthread(cpu); 2128 return 0; 2129 } 2130 2131 static void osnoise_init_hotplug_support(void) 2132 { 2133 int ret; 2134 2135 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "trace/osnoise:online", 2136 osnoise_cpu_init, osnoise_cpu_die); 2137 if (ret < 0) 2138 pr_warn(BANNER "Error to init cpu hotplug support\n"); 2139 2140 return; 2141 } 2142 #else /* CONFIG_HOTPLUG_CPU */ 2143 static void osnoise_init_hotplug_support(void) 2144 { 2145 return; 2146 } 2147 #endif /* CONFIG_HOTPLUG_CPU */ 2148 2149 /* 2150 * seq file functions for the osnoise/options file. 2151 */ 2152 static void *s_options_start(struct seq_file *s, loff_t *pos) 2153 { 2154 int option = *pos; 2155 2156 mutex_lock(&interface_lock); 2157 2158 if (option >= OSN_MAX) 2159 return NULL; 2160 2161 return pos; 2162 } 2163 2164 static void *s_options_next(struct seq_file *s, void *v, loff_t *pos) 2165 { 2166 int option = ++(*pos); 2167 2168 if (option >= OSN_MAX) 2169 return NULL; 2170 2171 return pos; 2172 } 2173 2174 static int s_options_show(struct seq_file *s, void *v) 2175 { 2176 loff_t *pos = v; 2177 int option = *pos; 2178 2179 if (option == OSN_DEFAULTS) { 2180 if (osnoise_options == OSN_DEFAULT_OPTIONS) 2181 seq_printf(s, "%s", osnoise_options_str[option]); 2182 else 2183 seq_printf(s, "NO_%s", osnoise_options_str[option]); 2184 goto out; 2185 } 2186 2187 if (test_bit(option, &osnoise_options)) 2188 seq_printf(s, "%s", osnoise_options_str[option]); 2189 else 2190 seq_printf(s, "NO_%s", osnoise_options_str[option]); 2191 2192 out: 2193 if (option != OSN_MAX) 2194 seq_puts(s, " "); 2195 2196 return 0; 2197 } 2198 2199 static void s_options_stop(struct seq_file *s, void *v) 2200 { 2201 seq_puts(s, "\n"); 2202 mutex_unlock(&interface_lock); 2203 } 2204 2205 static const struct seq_operations osnoise_options_seq_ops = { 2206 .start = s_options_start, 2207 .next = s_options_next, 2208 .show = s_options_show, 2209 .stop = s_options_stop 2210 }; 2211 2212 static int osnoise_options_open(struct inode *inode, struct file *file) 2213 { 2214 return seq_open(file, &osnoise_options_seq_ops); 2215 }; 2216 2217 /** 2218 * osnoise_options_write - Write function for "options" entry 2219 * @filp: The active open file structure 2220 * @ubuf: The user buffer that contains the value to write 2221 * @cnt: The maximum number of bytes to write to "file" 2222 * @ppos: The current position in @file 2223 * 2224 * Writing the option name sets the option, writing the "NO_" 2225 * prefix in front of the option name disables it. 2226 * 2227 * Writing "DEFAULTS" resets the option values to the default ones. 2228 */ 2229 static ssize_t osnoise_options_write(struct file *filp, const char __user *ubuf, 2230 size_t cnt, loff_t *ppos) 2231 { 2232 int running, option, enable, retval; 2233 char buf[256], *option_str; 2234 2235 if (cnt >= 256) 2236 return -EINVAL; 2237 2238 if (copy_from_user(buf, ubuf, cnt)) 2239 return -EFAULT; 2240 2241 buf[cnt] = 0; 2242 2243 if (strncmp(buf, "NO_", 3)) { 2244 option_str = strstrip(buf); 2245 enable = true; 2246 } else { 2247 option_str = strstrip(&buf[3]); 2248 enable = false; 2249 } 2250 2251 option = match_string(osnoise_options_str, OSN_MAX, option_str); 2252 if (option < 0) 2253 return -EINVAL; 2254 2255 /* 2256 * trace_types_lock is taken to avoid concurrency on start/stop. 2257 */ 2258 mutex_lock(&trace_types_lock); 2259 running = osnoise_has_registered_instances(); 2260 if (running) 2261 stop_per_cpu_kthreads(); 2262 2263 mutex_lock(&interface_lock); 2264 /* 2265 * avoid CPU hotplug operations that might read options. 2266 */ 2267 cpus_read_lock(); 2268 2269 retval = cnt; 2270 2271 if (enable) { 2272 if (option == OSN_DEFAULTS) 2273 osnoise_options = OSN_DEFAULT_OPTIONS; 2274 else 2275 set_bit(option, &osnoise_options); 2276 } else { 2277 if (option == OSN_DEFAULTS) 2278 retval = -EINVAL; 2279 else 2280 clear_bit(option, &osnoise_options); 2281 } 2282 2283 cpus_read_unlock(); 2284 mutex_unlock(&interface_lock); 2285 2286 if (running) 2287 start_per_cpu_kthreads(); 2288 mutex_unlock(&trace_types_lock); 2289 2290 return retval; 2291 } 2292 2293 /* 2294 * osnoise_cpus_read - Read function for reading the "cpus" file 2295 * @filp: The active open file structure 2296 * @ubuf: The userspace provided buffer to read value into 2297 * @cnt: The maximum number of bytes to read 2298 * @ppos: The current "file" position 2299 * 2300 * Prints the "cpus" output into the user-provided buffer. 2301 */ 2302 static ssize_t 2303 osnoise_cpus_read(struct file *filp, char __user *ubuf, size_t count, 2304 loff_t *ppos) 2305 { 2306 char *mask_str __free(kfree) = NULL; 2307 int len; 2308 2309 guard(mutex)(&interface_lock); 2310 2311 len = snprintf(NULL, 0, "%*pbl\n", cpumask_pr_args(&osnoise_cpumask)) + 1; 2312 mask_str = kmalloc(len, GFP_KERNEL); 2313 if (!mask_str) 2314 return -ENOMEM; 2315 2316 len = snprintf(mask_str, len, "%*pbl\n", cpumask_pr_args(&osnoise_cpumask)); 2317 if (len >= count) 2318 return -EINVAL; 2319 2320 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, len); 2321 2322 return count; 2323 } 2324 2325 /* 2326 * osnoise_cpus_write - Write function for "cpus" entry 2327 * @filp: The active open file structure 2328 * @ubuf: The user buffer that contains the value to write 2329 * @cnt: The maximum number of bytes to write to "file" 2330 * @ppos: The current position in @file 2331 * 2332 * This function provides a write implementation for the "cpus" 2333 * interface to the osnoise trace. By default, it lists all CPUs, 2334 * in this way, allowing osnoise threads to run on any online CPU 2335 * of the system. It serves to restrict the execution of osnoise to the 2336 * set of CPUs writing via this interface. Why not use "tracing_cpumask"? 2337 * Because the user might be interested in tracing what is running on 2338 * other CPUs. For instance, one might run osnoise in one HT CPU 2339 * while observing what is running on the sibling HT CPU. 2340 */ 2341 static ssize_t 2342 osnoise_cpus_write(struct file *filp, const char __user *ubuf, size_t count, 2343 loff_t *ppos) 2344 { 2345 cpumask_var_t osnoise_cpumask_new; 2346 int running, err; 2347 char buf[256]; 2348 2349 if (count >= 256) 2350 return -EINVAL; 2351 2352 if (copy_from_user(buf, ubuf, count)) 2353 return -EFAULT; 2354 2355 if (!zalloc_cpumask_var(&osnoise_cpumask_new, GFP_KERNEL)) 2356 return -ENOMEM; 2357 2358 err = cpulist_parse(buf, osnoise_cpumask_new); 2359 if (err) 2360 goto err_free; 2361 2362 /* 2363 * trace_types_lock is taken to avoid concurrency on start/stop. 2364 */ 2365 mutex_lock(&trace_types_lock); 2366 running = osnoise_has_registered_instances(); 2367 if (running) 2368 stop_per_cpu_kthreads(); 2369 2370 mutex_lock(&interface_lock); 2371 /* 2372 * osnoise_cpumask is read by CPU hotplug operations. 2373 */ 2374 cpus_read_lock(); 2375 2376 cpumask_copy(&osnoise_cpumask, osnoise_cpumask_new); 2377 2378 cpus_read_unlock(); 2379 mutex_unlock(&interface_lock); 2380 2381 if (running) 2382 start_per_cpu_kthreads(); 2383 mutex_unlock(&trace_types_lock); 2384 2385 free_cpumask_var(osnoise_cpumask_new); 2386 return count; 2387 2388 err_free: 2389 free_cpumask_var(osnoise_cpumask_new); 2390 2391 return err; 2392 } 2393 2394 #ifdef CONFIG_TIMERLAT_TRACER 2395 static int timerlat_fd_open(struct inode *inode, struct file *file) 2396 { 2397 struct osnoise_variables *osn_var; 2398 struct timerlat_variables *tlat; 2399 long cpu = (long) inode->i_cdev; 2400 2401 mutex_lock(&interface_lock); 2402 2403 /* 2404 * This file is accessible only if timerlat is enabled, and 2405 * NO_OSNOISE_WORKLOAD is set. 2406 */ 2407 if (!timerlat_enabled() || test_bit(OSN_WORKLOAD, &osnoise_options)) { 2408 mutex_unlock(&interface_lock); 2409 return -EINVAL; 2410 } 2411 2412 migrate_disable(); 2413 2414 osn_var = this_cpu_osn_var(); 2415 2416 /* 2417 * The osn_var->pid holds the single access to this file. 2418 */ 2419 if (osn_var->pid) { 2420 mutex_unlock(&interface_lock); 2421 migrate_enable(); 2422 return -EBUSY; 2423 } 2424 2425 /* 2426 * timerlat tracer is a per-cpu tracer. Check if the user-space too 2427 * is pinned to a single CPU. The tracer laters monitor if the task 2428 * migrates and then disables tracer if it does. However, it is 2429 * worth doing this basic acceptance test to avoid obviusly wrong 2430 * setup. 2431 */ 2432 if (current->nr_cpus_allowed > 1 || cpu != smp_processor_id()) { 2433 mutex_unlock(&interface_lock); 2434 migrate_enable(); 2435 return -EPERM; 2436 } 2437 2438 /* 2439 * From now on, it is good to go. 2440 */ 2441 file->private_data = inode->i_cdev; 2442 2443 get_task_struct(current); 2444 2445 osn_var->kthread = current; 2446 osn_var->pid = current->pid; 2447 2448 /* 2449 * Setup is done. 2450 */ 2451 mutex_unlock(&interface_lock); 2452 2453 tlat = this_cpu_tmr_var(); 2454 tlat->count = 0; 2455 2456 hrtimer_setup(&tlat->timer, timerlat_irq, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED_HARD); 2457 2458 migrate_enable(); 2459 return 0; 2460 }; 2461 2462 /* 2463 * timerlat_fd_read - Read function for "timerlat_fd" file 2464 * @file: The active open file structure 2465 * @ubuf: The userspace provided buffer to read value into 2466 * @cnt: The maximum number of bytes to read 2467 * @ppos: The current "file" position 2468 * 2469 * Prints 1 on timerlat, the number of interferences on osnoise, -1 on error. 2470 */ 2471 static ssize_t 2472 timerlat_fd_read(struct file *file, char __user *ubuf, size_t count, 2473 loff_t *ppos) 2474 { 2475 long cpu = (long) file->private_data; 2476 struct osnoise_variables *osn_var; 2477 struct timerlat_variables *tlat; 2478 struct timerlat_sample s; 2479 s64 diff; 2480 u64 now; 2481 2482 migrate_disable(); 2483 2484 tlat = this_cpu_tmr_var(); 2485 2486 /* 2487 * While in user-space, the thread is migratable. There is nothing 2488 * we can do about it. 2489 * So, if the thread is running on another CPU, stop the machinery. 2490 */ 2491 if (cpu == smp_processor_id()) { 2492 if (tlat->uthread_migrate) { 2493 migrate_enable(); 2494 return -EINVAL; 2495 } 2496 } else { 2497 per_cpu_ptr(&per_cpu_timerlat_var, cpu)->uthread_migrate = 1; 2498 osnoise_taint("timerlat user thread migrate\n"); 2499 osnoise_stop_tracing(); 2500 migrate_enable(); 2501 return -EINVAL; 2502 } 2503 2504 osn_var = this_cpu_osn_var(); 2505 2506 /* 2507 * The timerlat in user-space runs in a different order: 2508 * the read() starts from the execution of the previous occurrence, 2509 * sleeping for the next occurrence. 2510 * 2511 * So, skip if we are entering on read() before the first wakeup 2512 * from timerlat IRQ: 2513 */ 2514 if (likely(osn_var->sampling)) { 2515 now = ktime_to_ns(hrtimer_cb_get_time(&tlat->timer)); 2516 diff = now - tlat->abs_period; 2517 2518 /* 2519 * it was not a timer firing, but some other signal? 2520 */ 2521 if (diff < 0) 2522 goto out; 2523 2524 s.seqnum = tlat->count; 2525 s.timer_latency = diff; 2526 s.context = THREAD_URET; 2527 2528 trace_timerlat_sample(&s); 2529 2530 notify_new_max_latency(diff); 2531 2532 tlat->tracing_thread = false; 2533 if (osnoise_data.stop_tracing_total) 2534 if (time_to_us(diff) >= osnoise_data.stop_tracing_total) 2535 osnoise_stop_tracing(); 2536 } else { 2537 tlat->tracing_thread = false; 2538 tlat->kthread = current; 2539 2540 /* Annotate now to drift new period */ 2541 tlat->abs_period = hrtimer_cb_get_time(&tlat->timer); 2542 2543 osn_var->sampling = 1; 2544 } 2545 2546 /* wait for the next period */ 2547 wait_next_period(tlat); 2548 2549 /* This is the wakeup from this cycle */ 2550 now = ktime_to_ns(hrtimer_cb_get_time(&tlat->timer)); 2551 diff = now - tlat->abs_period; 2552 2553 /* 2554 * it was not a timer firing, but some other signal? 2555 */ 2556 if (diff < 0) 2557 goto out; 2558 2559 s.seqnum = tlat->count; 2560 s.timer_latency = diff; 2561 s.context = THREAD_CONTEXT; 2562 2563 trace_timerlat_sample(&s); 2564 2565 if (osnoise_data.stop_tracing_total) { 2566 if (time_to_us(diff) >= osnoise_data.stop_tracing_total) { 2567 timerlat_dump_stack(time_to_us(diff)); 2568 notify_new_max_latency(diff); 2569 osnoise_stop_tracing(); 2570 } 2571 } 2572 2573 out: 2574 migrate_enable(); 2575 return 0; 2576 } 2577 2578 static int timerlat_fd_release(struct inode *inode, struct file *file) 2579 { 2580 struct osnoise_variables *osn_var; 2581 struct timerlat_variables *tlat_var; 2582 long cpu = (long) file->private_data; 2583 2584 migrate_disable(); 2585 mutex_lock(&interface_lock); 2586 2587 osn_var = per_cpu_ptr(&per_cpu_osnoise_var, cpu); 2588 tlat_var = per_cpu_ptr(&per_cpu_timerlat_var, cpu); 2589 2590 if (tlat_var->kthread) 2591 hrtimer_cancel(&tlat_var->timer); 2592 memset(tlat_var, 0, sizeof(*tlat_var)); 2593 2594 osn_var->sampling = 0; 2595 osn_var->pid = 0; 2596 2597 /* 2598 * We are leaving, not being stopped... see stop_kthread(); 2599 */ 2600 if (osn_var->kthread) { 2601 put_task_struct(osn_var->kthread); 2602 osn_var->kthread = NULL; 2603 } 2604 2605 mutex_unlock(&interface_lock); 2606 migrate_enable(); 2607 return 0; 2608 } 2609 #endif 2610 2611 /* 2612 * osnoise/runtime_us: cannot be greater than the period. 2613 */ 2614 static struct trace_min_max_param osnoise_runtime = { 2615 .lock = &interface_lock, 2616 .val = &osnoise_data.sample_runtime, 2617 .max = &osnoise_data.sample_period, 2618 .min = NULL, 2619 }; 2620 2621 /* 2622 * osnoise/period_us: cannot be smaller than the runtime. 2623 */ 2624 static struct trace_min_max_param osnoise_period = { 2625 .lock = &interface_lock, 2626 .val = &osnoise_data.sample_period, 2627 .max = NULL, 2628 .min = &osnoise_data.sample_runtime, 2629 }; 2630 2631 /* 2632 * osnoise/stop_tracing_us: no limit. 2633 */ 2634 static struct trace_min_max_param osnoise_stop_tracing_in = { 2635 .lock = &interface_lock, 2636 .val = &osnoise_data.stop_tracing, 2637 .max = NULL, 2638 .min = NULL, 2639 }; 2640 2641 /* 2642 * osnoise/stop_tracing_total_us: no limit. 2643 */ 2644 static struct trace_min_max_param osnoise_stop_tracing_total = { 2645 .lock = &interface_lock, 2646 .val = &osnoise_data.stop_tracing_total, 2647 .max = NULL, 2648 .min = NULL, 2649 }; 2650 2651 #ifdef CONFIG_TIMERLAT_TRACER 2652 /* 2653 * osnoise/print_stack: print the stacktrace of the IRQ handler if the total 2654 * latency is higher than val. 2655 */ 2656 static struct trace_min_max_param osnoise_print_stack = { 2657 .lock = &interface_lock, 2658 .val = &osnoise_data.print_stack, 2659 .max = NULL, 2660 .min = NULL, 2661 }; 2662 2663 /* 2664 * osnoise/timerlat_period: min 100 us, max 1 s 2665 */ 2666 static u64 timerlat_min_period = 100; 2667 static u64 timerlat_max_period = 1000000; 2668 static struct trace_min_max_param timerlat_period = { 2669 .lock = &interface_lock, 2670 .val = &osnoise_data.timerlat_period, 2671 .max = &timerlat_max_period, 2672 .min = &timerlat_min_period, 2673 }; 2674 2675 static const struct file_operations timerlat_fd_fops = { 2676 .open = timerlat_fd_open, 2677 .read = timerlat_fd_read, 2678 .release = timerlat_fd_release, 2679 .llseek = generic_file_llseek, 2680 }; 2681 #endif 2682 2683 static const struct file_operations cpus_fops = { 2684 .open = tracing_open_generic, 2685 .read = osnoise_cpus_read, 2686 .write = osnoise_cpus_write, 2687 .llseek = generic_file_llseek, 2688 }; 2689 2690 static const struct file_operations osnoise_options_fops = { 2691 .open = osnoise_options_open, 2692 .read = seq_read, 2693 .llseek = seq_lseek, 2694 .release = seq_release, 2695 .write = osnoise_options_write 2696 }; 2697 2698 #ifdef CONFIG_TIMERLAT_TRACER 2699 #ifdef CONFIG_STACKTRACE 2700 static int init_timerlat_stack_tracefs(struct dentry *top_dir) 2701 { 2702 struct dentry *tmp; 2703 2704 tmp = tracefs_create_file("print_stack", TRACE_MODE_WRITE, top_dir, 2705 &osnoise_print_stack, &trace_min_max_fops); 2706 if (!tmp) 2707 return -ENOMEM; 2708 2709 return 0; 2710 } 2711 #else /* CONFIG_STACKTRACE */ 2712 static int init_timerlat_stack_tracefs(struct dentry *top_dir) 2713 { 2714 return 0; 2715 } 2716 #endif /* CONFIG_STACKTRACE */ 2717 2718 static int osnoise_create_cpu_timerlat_fd(struct dentry *top_dir) 2719 { 2720 struct dentry *timerlat_fd; 2721 struct dentry *per_cpu; 2722 struct dentry *cpu_dir; 2723 char cpu_str[30]; /* see trace.c: tracing_init_tracefs_percpu() */ 2724 long cpu; 2725 2726 /* 2727 * Why not using tracing instance per_cpu/ dir? 2728 * 2729 * Because osnoise/timerlat have a single workload, having 2730 * multiple files like these are wast of memory. 2731 */ 2732 per_cpu = tracefs_create_dir("per_cpu", top_dir); 2733 if (!per_cpu) 2734 return -ENOMEM; 2735 2736 for_each_possible_cpu(cpu) { 2737 snprintf(cpu_str, 30, "cpu%ld", cpu); 2738 cpu_dir = tracefs_create_dir(cpu_str, per_cpu); 2739 if (!cpu_dir) 2740 goto out_clean; 2741 2742 timerlat_fd = trace_create_file("timerlat_fd", TRACE_MODE_READ, 2743 cpu_dir, NULL, &timerlat_fd_fops); 2744 if (!timerlat_fd) 2745 goto out_clean; 2746 2747 /* Record the CPU */ 2748 d_inode(timerlat_fd)->i_cdev = (void *)(cpu); 2749 } 2750 2751 return 0; 2752 2753 out_clean: 2754 tracefs_remove(per_cpu); 2755 return -ENOMEM; 2756 } 2757 2758 /* 2759 * init_timerlat_tracefs - A function to initialize the timerlat interface files 2760 */ 2761 static int init_timerlat_tracefs(struct dentry *top_dir) 2762 { 2763 struct dentry *tmp; 2764 int retval; 2765 2766 tmp = tracefs_create_file("timerlat_period_us", TRACE_MODE_WRITE, top_dir, 2767 &timerlat_period, &trace_min_max_fops); 2768 if (!tmp) 2769 return -ENOMEM; 2770 2771 retval = osnoise_create_cpu_timerlat_fd(top_dir); 2772 if (retval) 2773 return retval; 2774 2775 return init_timerlat_stack_tracefs(top_dir); 2776 } 2777 #else /* CONFIG_TIMERLAT_TRACER */ 2778 static int init_timerlat_tracefs(struct dentry *top_dir) 2779 { 2780 return 0; 2781 } 2782 #endif /* CONFIG_TIMERLAT_TRACER */ 2783 2784 /* 2785 * init_tracefs - A function to initialize the tracefs interface files 2786 * 2787 * This function creates entries in tracefs for "osnoise" and "timerlat". 2788 * It creates these directories in the tracing directory, and within that 2789 * directory the use can change and view the configs. 2790 */ 2791 static int init_tracefs(void) 2792 { 2793 struct dentry *top_dir; 2794 struct dentry *tmp; 2795 int ret; 2796 2797 ret = tracing_init_dentry(); 2798 if (ret) 2799 return -ENOMEM; 2800 2801 top_dir = tracefs_create_dir("osnoise", NULL); 2802 if (!top_dir) 2803 return 0; 2804 2805 tmp = tracefs_create_file("period_us", TRACE_MODE_WRITE, top_dir, 2806 &osnoise_period, &trace_min_max_fops); 2807 if (!tmp) 2808 goto err; 2809 2810 tmp = tracefs_create_file("runtime_us", TRACE_MODE_WRITE, top_dir, 2811 &osnoise_runtime, &trace_min_max_fops); 2812 if (!tmp) 2813 goto err; 2814 2815 tmp = tracefs_create_file("stop_tracing_us", TRACE_MODE_WRITE, top_dir, 2816 &osnoise_stop_tracing_in, &trace_min_max_fops); 2817 if (!tmp) 2818 goto err; 2819 2820 tmp = tracefs_create_file("stop_tracing_total_us", TRACE_MODE_WRITE, top_dir, 2821 &osnoise_stop_tracing_total, &trace_min_max_fops); 2822 if (!tmp) 2823 goto err; 2824 2825 tmp = trace_create_file("cpus", TRACE_MODE_WRITE, top_dir, NULL, &cpus_fops); 2826 if (!tmp) 2827 goto err; 2828 2829 tmp = trace_create_file("options", TRACE_MODE_WRITE, top_dir, NULL, 2830 &osnoise_options_fops); 2831 if (!tmp) 2832 goto err; 2833 2834 ret = init_timerlat_tracefs(top_dir); 2835 if (ret) 2836 goto err; 2837 2838 return 0; 2839 2840 err: 2841 tracefs_remove(top_dir); 2842 return -ENOMEM; 2843 } 2844 2845 static int osnoise_hook_events(void) 2846 { 2847 int retval; 2848 2849 /* 2850 * Trace is already hooked, we are re-enabling from 2851 * a stop_tracing_*. 2852 */ 2853 if (trace_osnoise_callback_enabled) 2854 return 0; 2855 2856 retval = hook_irq_events(); 2857 if (retval) 2858 return -EINVAL; 2859 2860 retval = hook_softirq_events(); 2861 if (retval) 2862 goto out_unhook_irq; 2863 2864 retval = hook_thread_events(); 2865 /* 2866 * All fine! 2867 */ 2868 if (!retval) 2869 return 0; 2870 2871 unhook_softirq_events(); 2872 out_unhook_irq: 2873 unhook_irq_events(); 2874 return -EINVAL; 2875 } 2876 2877 static void osnoise_unhook_events(void) 2878 { 2879 unhook_thread_events(); 2880 unhook_softirq_events(); 2881 unhook_irq_events(); 2882 } 2883 2884 /* 2885 * osnoise_workload_start - start the workload and hook to events 2886 */ 2887 static int osnoise_workload_start(void) 2888 { 2889 int retval; 2890 2891 /* 2892 * Instances need to be registered after calling workload 2893 * start. Hence, if there is already an instance, the 2894 * workload was already registered. Otherwise, this 2895 * code is on the way to register the first instance, 2896 * and the workload will start. 2897 */ 2898 if (osnoise_has_registered_instances()) 2899 return 0; 2900 2901 osn_var_reset_all(); 2902 2903 retval = osnoise_hook_events(); 2904 if (retval) 2905 return retval; 2906 2907 /* 2908 * Make sure that ftrace_nmi_enter/exit() see reset values 2909 * before enabling trace_osnoise_callback_enabled. 2910 */ 2911 barrier(); 2912 trace_osnoise_callback_enabled = true; 2913 2914 retval = start_per_cpu_kthreads(); 2915 if (retval) { 2916 trace_osnoise_callback_enabled = false; 2917 /* 2918 * Make sure that ftrace_nmi_enter/exit() see 2919 * trace_osnoise_callback_enabled as false before continuing. 2920 */ 2921 barrier(); 2922 2923 osnoise_unhook_events(); 2924 return retval; 2925 } 2926 2927 return 0; 2928 } 2929 2930 /* 2931 * osnoise_workload_stop - stop the workload and unhook the events 2932 */ 2933 static void osnoise_workload_stop(void) 2934 { 2935 /* 2936 * Instances need to be unregistered before calling 2937 * stop. Hence, if there is a registered instance, more 2938 * than one instance is running, and the workload will not 2939 * yet stop. Otherwise, this code is on the way to disable 2940 * the last instance, and the workload can stop. 2941 */ 2942 if (osnoise_has_registered_instances()) 2943 return; 2944 2945 /* 2946 * If callbacks were already disabled in a previous stop 2947 * call, there is no need to disable then again. 2948 * 2949 * For instance, this happens when tracing is stopped via: 2950 * echo 0 > tracing_on 2951 * echo nop > current_tracer. 2952 */ 2953 if (!trace_osnoise_callback_enabled) 2954 return; 2955 2956 trace_osnoise_callback_enabled = false; 2957 /* 2958 * Make sure that ftrace_nmi_enter/exit() see 2959 * trace_osnoise_callback_enabled as false before continuing. 2960 */ 2961 barrier(); 2962 2963 stop_per_cpu_kthreads(); 2964 2965 osnoise_unhook_events(); 2966 } 2967 2968 static void osnoise_tracer_start(struct trace_array *tr) 2969 { 2970 int retval; 2971 2972 /* 2973 * If the instance is already registered, there is no need to 2974 * register it again. 2975 */ 2976 if (osnoise_instance_registered(tr)) 2977 return; 2978 2979 retval = osnoise_workload_start(); 2980 if (retval) 2981 pr_err(BANNER "Error starting osnoise tracer\n"); 2982 2983 osnoise_register_instance(tr); 2984 } 2985 2986 static void osnoise_tracer_stop(struct trace_array *tr) 2987 { 2988 osnoise_unregister_instance(tr); 2989 osnoise_workload_stop(); 2990 } 2991 2992 static int osnoise_tracer_init(struct trace_array *tr) 2993 { 2994 /* 2995 * Only allow osnoise tracer if timerlat tracer is not running 2996 * already. 2997 */ 2998 if (timerlat_enabled()) 2999 return -EBUSY; 3000 3001 tr->max_latency = 0; 3002 3003 osnoise_tracer_start(tr); 3004 return 0; 3005 } 3006 3007 static void osnoise_tracer_reset(struct trace_array *tr) 3008 { 3009 osnoise_tracer_stop(tr); 3010 } 3011 3012 static struct tracer osnoise_tracer __read_mostly = { 3013 .name = "osnoise", 3014 .init = osnoise_tracer_init, 3015 .reset = osnoise_tracer_reset, 3016 .start = osnoise_tracer_start, 3017 .stop = osnoise_tracer_stop, 3018 .print_header = print_osnoise_headers, 3019 .allow_instances = true, 3020 }; 3021 3022 #ifdef CONFIG_TIMERLAT_TRACER 3023 static void timerlat_tracer_start(struct trace_array *tr) 3024 { 3025 int retval; 3026 3027 /* 3028 * If the instance is already registered, there is no need to 3029 * register it again. 3030 */ 3031 if (osnoise_instance_registered(tr)) 3032 return; 3033 3034 retval = osnoise_workload_start(); 3035 if (retval) 3036 pr_err(BANNER "Error starting timerlat tracer\n"); 3037 3038 osnoise_register_instance(tr); 3039 3040 return; 3041 } 3042 3043 static void timerlat_tracer_stop(struct trace_array *tr) 3044 { 3045 int cpu; 3046 3047 osnoise_unregister_instance(tr); 3048 3049 /* 3050 * Instruct the threads to stop only if this is the last instance. 3051 */ 3052 if (!osnoise_has_registered_instances()) { 3053 for_each_online_cpu(cpu) 3054 per_cpu(per_cpu_osnoise_var, cpu).sampling = 0; 3055 } 3056 3057 osnoise_workload_stop(); 3058 } 3059 3060 static int timerlat_tracer_init(struct trace_array *tr) 3061 { 3062 /* 3063 * Only allow timerlat tracer if osnoise tracer is not running already. 3064 */ 3065 if (osnoise_has_registered_instances() && !osnoise_data.timerlat_tracer) 3066 return -EBUSY; 3067 3068 /* 3069 * If this is the first instance, set timerlat_tracer to block 3070 * osnoise tracer start. 3071 */ 3072 if (!osnoise_has_registered_instances()) 3073 osnoise_data.timerlat_tracer = 1; 3074 3075 tr->max_latency = 0; 3076 timerlat_tracer_start(tr); 3077 3078 return 0; 3079 } 3080 3081 static void timerlat_tracer_reset(struct trace_array *tr) 3082 { 3083 timerlat_tracer_stop(tr); 3084 3085 /* 3086 * If this is the last instance, reset timerlat_tracer allowing 3087 * osnoise to be started. 3088 */ 3089 if (!osnoise_has_registered_instances()) 3090 osnoise_data.timerlat_tracer = 0; 3091 } 3092 3093 static struct tracer timerlat_tracer __read_mostly = { 3094 .name = "timerlat", 3095 .init = timerlat_tracer_init, 3096 .reset = timerlat_tracer_reset, 3097 .start = timerlat_tracer_start, 3098 .stop = timerlat_tracer_stop, 3099 .print_header = print_timerlat_headers, 3100 .allow_instances = true, 3101 }; 3102 3103 __init static int init_timerlat_tracer(void) 3104 { 3105 return register_tracer(&timerlat_tracer); 3106 } 3107 #else /* CONFIG_TIMERLAT_TRACER */ 3108 __init static int init_timerlat_tracer(void) 3109 { 3110 return 0; 3111 } 3112 #endif /* CONFIG_TIMERLAT_TRACER */ 3113 3114 __init static int init_osnoise_tracer(void) 3115 { 3116 int ret; 3117 3118 mutex_init(&interface_lock); 3119 3120 cpumask_copy(&osnoise_cpumask, cpu_all_mask); 3121 3122 ret = register_tracer(&osnoise_tracer); 3123 if (ret) { 3124 pr_err(BANNER "Error registering osnoise!\n"); 3125 return ret; 3126 } 3127 3128 ret = init_timerlat_tracer(); 3129 if (ret) { 3130 pr_err(BANNER "Error registering timerlat!\n"); 3131 return ret; 3132 } 3133 3134 osnoise_init_hotplug_support(); 3135 3136 INIT_LIST_HEAD_RCU(&osnoise_instances); 3137 3138 init_tracefs(); 3139 3140 return 0; 3141 } 3142 late_initcall(init_osnoise_tracer); 3143