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