tracing/timerlat: Always wakeup the timerlat thread
[platform/kernel/linux-starfive.git] / kernel / trace / trace_osnoise.c
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  * trace_array of the enabled osnoise/timerlat instances.
53  */
54 struct osnoise_instance {
55         struct list_head        list;
56         struct trace_array      *tr;
57 };
58
59 static struct list_head osnoise_instances;
60
61 static bool osnoise_has_registered_instances(void)
62 {
63         return !!list_first_or_null_rcu(&osnoise_instances,
64                                         struct osnoise_instance,
65                                         list);
66 }
67
68 /*
69  * osnoise_instance_registered - check if a tr is already registered
70  */
71 static int osnoise_instance_registered(struct trace_array *tr)
72 {
73         struct osnoise_instance *inst;
74         int found = 0;
75
76         rcu_read_lock();
77         list_for_each_entry_rcu(inst, &osnoise_instances, list) {
78                 if (inst->tr == tr)
79                         found = 1;
80         }
81         rcu_read_unlock();
82
83         return found;
84 }
85
86 /*
87  * osnoise_register_instance - register a new trace instance
88  *
89  * Register a trace_array *tr in the list of instances running
90  * osnoise/timerlat tracers.
91  */
92 static int osnoise_register_instance(struct trace_array *tr)
93 {
94         struct osnoise_instance *inst;
95
96         /*
97          * register/unregister serialization is provided by trace's
98          * trace_types_lock.
99          */
100         lockdep_assert_held(&trace_types_lock);
101
102         inst = kmalloc(sizeof(*inst), GFP_KERNEL);
103         if (!inst)
104                 return -ENOMEM;
105
106         INIT_LIST_HEAD_RCU(&inst->list);
107         inst->tr = tr;
108         list_add_tail_rcu(&inst->list, &osnoise_instances);
109
110         return 0;
111 }
112
113 /*
114  *  osnoise_unregister_instance - unregister a registered trace instance
115  *
116  * Remove the trace_array *tr from the list of instances running
117  * osnoise/timerlat tracers.
118  */
119 static void osnoise_unregister_instance(struct trace_array *tr)
120 {
121         struct osnoise_instance *inst;
122         int found = 0;
123
124         /*
125          * register/unregister serialization is provided by trace's
126          * trace_types_lock.
127          */
128         list_for_each_entry_rcu(inst, &osnoise_instances, list,
129                                 lockdep_is_held(&trace_types_lock)) {
130                 if (inst->tr == tr) {
131                         list_del_rcu(&inst->list);
132                         found = 1;
133                         break;
134                 }
135         }
136
137         if (!found)
138                 return;
139
140         kvfree_rcu(inst);
141 }
142
143 /*
144  * NMI runtime info.
145  */
146 struct osn_nmi {
147         u64     count;
148         u64     delta_start;
149 };
150
151 /*
152  * IRQ runtime info.
153  */
154 struct osn_irq {
155         u64     count;
156         u64     arrival_time;
157         u64     delta_start;
158 };
159
160 #define IRQ_CONTEXT     0
161 #define THREAD_CONTEXT  1
162 /*
163  * sofirq runtime info.
164  */
165 struct osn_softirq {
166         u64     count;
167         u64     arrival_time;
168         u64     delta_start;
169 };
170
171 /*
172  * thread runtime info.
173  */
174 struct osn_thread {
175         u64     count;
176         u64     arrival_time;
177         u64     delta_start;
178 };
179
180 /*
181  * Runtime information: this structure saves the runtime information used by
182  * one sampling thread.
183  */
184 struct osnoise_variables {
185         struct task_struct      *kthread;
186         bool                    sampling;
187         pid_t                   pid;
188         struct osn_nmi          nmi;
189         struct osn_irq          irq;
190         struct osn_softirq      softirq;
191         struct osn_thread       thread;
192         local_t                 int_counter;
193 };
194
195 /*
196  * Per-cpu runtime information.
197  */
198 DEFINE_PER_CPU(struct osnoise_variables, per_cpu_osnoise_var);
199
200 /*
201  * this_cpu_osn_var - Return the per-cpu osnoise_variables on its relative CPU
202  */
203 static inline struct osnoise_variables *this_cpu_osn_var(void)
204 {
205         return this_cpu_ptr(&per_cpu_osnoise_var);
206 }
207
208 #ifdef CONFIG_TIMERLAT_TRACER
209 /*
210  * Runtime information for the timer mode.
211  */
212 struct timerlat_variables {
213         struct task_struct      *kthread;
214         struct hrtimer          timer;
215         u64                     rel_period;
216         u64                     abs_period;
217         bool                    tracing_thread;
218         u64                     count;
219 };
220
221 DEFINE_PER_CPU(struct timerlat_variables, per_cpu_timerlat_var);
222
223 /*
224  * this_cpu_tmr_var - Return the per-cpu timerlat_variables on its relative CPU
225  */
226 static inline struct timerlat_variables *this_cpu_tmr_var(void)
227 {
228         return this_cpu_ptr(&per_cpu_timerlat_var);
229 }
230
231 /*
232  * tlat_var_reset - Reset the values of the given timerlat_variables
233  */
234 static inline void tlat_var_reset(void)
235 {
236         struct timerlat_variables *tlat_var;
237         int cpu;
238         /*
239          * So far, all the values are initialized as 0, so
240          * zeroing the structure is perfect.
241          */
242         for_each_cpu(cpu, cpu_online_mask) {
243                 tlat_var = per_cpu_ptr(&per_cpu_timerlat_var, cpu);
244                 memset(tlat_var, 0, sizeof(*tlat_var));
245         }
246 }
247 #else /* CONFIG_TIMERLAT_TRACER */
248 #define tlat_var_reset()        do {} while (0)
249 #endif /* CONFIG_TIMERLAT_TRACER */
250
251 /*
252  * osn_var_reset - Reset the values of the given osnoise_variables
253  */
254 static inline void osn_var_reset(void)
255 {
256         struct osnoise_variables *osn_var;
257         int cpu;
258
259         /*
260          * So far, all the values are initialized as 0, so
261          * zeroing the structure is perfect.
262          */
263         for_each_cpu(cpu, cpu_online_mask) {
264                 osn_var = per_cpu_ptr(&per_cpu_osnoise_var, cpu);
265                 memset(osn_var, 0, sizeof(*osn_var));
266         }
267 }
268
269 /*
270  * osn_var_reset_all - Reset the value of all per-cpu osnoise_variables
271  */
272 static inline void osn_var_reset_all(void)
273 {
274         osn_var_reset();
275         tlat_var_reset();
276 }
277
278 /*
279  * Tells NMIs to call back to the osnoise tracer to record timestamps.
280  */
281 bool trace_osnoise_callback_enabled;
282
283 /*
284  * osnoise sample structure definition. Used to store the statistics of a
285  * sample run.
286  */
287 struct osnoise_sample {
288         u64                     runtime;        /* runtime */
289         u64                     noise;          /* noise */
290         u64                     max_sample;     /* max single noise sample */
291         int                     hw_count;       /* # HW (incl. hypervisor) interference */
292         int                     nmi_count;      /* # NMIs during this sample */
293         int                     irq_count;      /* # IRQs during this sample */
294         int                     softirq_count;  /* # softirqs during this sample */
295         int                     thread_count;   /* # threads during this sample */
296 };
297
298 #ifdef CONFIG_TIMERLAT_TRACER
299 /*
300  * timerlat sample structure definition. Used to store the statistics of
301  * a sample run.
302  */
303 struct timerlat_sample {
304         u64                     timer_latency;  /* timer_latency */
305         unsigned int            seqnum;         /* unique sequence */
306         int                     context;        /* timer context */
307 };
308 #endif
309
310 /*
311  * Protect the interface.
312  */
313 struct mutex interface_lock;
314
315 /*
316  * Tracer data.
317  */
318 static struct osnoise_data {
319         u64     sample_period;          /* total sampling period */
320         u64     sample_runtime;         /* active sampling portion of period */
321         u64     stop_tracing;           /* stop trace in the internal operation (loop/irq) */
322         u64     stop_tracing_total;     /* stop trace in the final operation (report/thread) */
323 #ifdef CONFIG_TIMERLAT_TRACER
324         u64     timerlat_period;        /* timerlat period */
325         u64     print_stack;            /* print IRQ stack if total > */
326         int     timerlat_tracer;        /* timerlat tracer */
327 #endif
328         bool    tainted;                /* infor users and developers about a problem */
329 } osnoise_data = {
330         .sample_period                  = DEFAULT_SAMPLE_PERIOD,
331         .sample_runtime                 = DEFAULT_SAMPLE_RUNTIME,
332         .stop_tracing                   = 0,
333         .stop_tracing_total             = 0,
334 #ifdef CONFIG_TIMERLAT_TRACER
335         .print_stack                    = 0,
336         .timerlat_period                = DEFAULT_TIMERLAT_PERIOD,
337         .timerlat_tracer                = 0,
338 #endif
339 };
340
341 #ifdef CONFIG_TIMERLAT_TRACER
342 static inline bool timerlat_enabled(void)
343 {
344         return osnoise_data.timerlat_tracer;
345 }
346
347 static inline int timerlat_softirq_exit(struct osnoise_variables *osn_var)
348 {
349         struct timerlat_variables *tlat_var = this_cpu_tmr_var();
350         /*
351          * If the timerlat is enabled, but the irq handler did
352          * not run yet enabling timerlat_tracer, do not trace.
353          */
354         if (!tlat_var->tracing_thread) {
355                 osn_var->softirq.arrival_time = 0;
356                 osn_var->softirq.delta_start = 0;
357                 return 0;
358         }
359         return 1;
360 }
361
362 static inline int timerlat_thread_exit(struct osnoise_variables *osn_var)
363 {
364         struct timerlat_variables *tlat_var = this_cpu_tmr_var();
365         /*
366          * If the timerlat is enabled, but the irq handler did
367          * not run yet enabling timerlat_tracer, do not trace.
368          */
369         if (!tlat_var->tracing_thread) {
370                 osn_var->thread.delta_start = 0;
371                 osn_var->thread.arrival_time = 0;
372                 return 0;
373         }
374         return 1;
375 }
376 #else /* CONFIG_TIMERLAT_TRACER */
377 static inline bool timerlat_enabled(void)
378 {
379         return false;
380 }
381
382 static inline int timerlat_softirq_exit(struct osnoise_variables *osn_var)
383 {
384         return 1;
385 }
386 static inline int timerlat_thread_exit(struct osnoise_variables *osn_var)
387 {
388         return 1;
389 }
390 #endif
391
392 #ifdef CONFIG_PREEMPT_RT
393 /*
394  * Print the osnoise header info.
395  */
396 static void print_osnoise_headers(struct seq_file *s)
397 {
398         if (osnoise_data.tainted)
399                 seq_puts(s, "# osnoise is tainted!\n");
400
401         seq_puts(s, "#                                _-------=> irqs-off\n");
402         seq_puts(s, "#                               / _------=> need-resched\n");
403         seq_puts(s, "#                              | / _-----=> need-resched-lazy\n");
404         seq_puts(s, "#                              || / _----=> hardirq/softirq\n");
405         seq_puts(s, "#                              ||| / _---=> preempt-depth\n");
406         seq_puts(s, "#                              |||| / _--=> preempt-lazy-depth\n");
407         seq_puts(s, "#                              ||||| / _-=> migrate-disable\n");
408
409         seq_puts(s, "#                              |||||| /          ");
410         seq_puts(s, "                                     MAX\n");
411
412         seq_puts(s, "#                              ||||| /                         ");
413         seq_puts(s, "                    SINGLE      Interference counters:\n");
414
415         seq_puts(s, "#                              |||||||               RUNTIME   ");
416         seq_puts(s, "   NOISE  %% OF CPU  NOISE    +-----------------------------+\n");
417
418         seq_puts(s, "#           TASK-PID      CPU# |||||||   TIMESTAMP    IN US    ");
419         seq_puts(s, "   IN US  AVAILABLE  IN US     HW    NMI    IRQ   SIRQ THREAD\n");
420
421         seq_puts(s, "#              | |         |   |||||||      |           |      ");
422         seq_puts(s, "       |    |            |      |      |      |      |      |\n");
423 }
424 #else /* CONFIG_PREEMPT_RT */
425 static void print_osnoise_headers(struct seq_file *s)
426 {
427         if (osnoise_data.tainted)
428                 seq_puts(s, "# osnoise is tainted!\n");
429
430         seq_puts(s, "#                                _-----=> irqs-off\n");
431         seq_puts(s, "#                               / _----=> need-resched\n");
432         seq_puts(s, "#                              | / _---=> hardirq/softirq\n");
433         seq_puts(s, "#                              || / _--=> preempt-depth\n");
434         seq_puts(s, "#                              ||| / _-=> migrate-disable     ");
435         seq_puts(s, "                    MAX\n");
436         seq_puts(s, "#                              |||| /     delay               ");
437         seq_puts(s, "                    SINGLE      Interference counters:\n");
438
439         seq_puts(s, "#                              |||||               RUNTIME   ");
440         seq_puts(s, "   NOISE  %% OF CPU  NOISE    +-----------------------------+\n");
441
442         seq_puts(s, "#           TASK-PID      CPU# |||||   TIMESTAMP    IN US    ");
443         seq_puts(s, "   IN US  AVAILABLE  IN US     HW    NMI    IRQ   SIRQ THREAD\n");
444
445         seq_puts(s, "#              | |         |   |||||      |           |      ");
446         seq_puts(s, "       |    |            |      |      |      |      |      |\n");
447 }
448 #endif /* CONFIG_PREEMPT_RT */
449
450 /*
451  * osnoise_taint - report an osnoise error.
452  */
453 #define osnoise_taint(msg) ({                                                   \
454         struct osnoise_instance *inst;                                          \
455         struct trace_buffer *buffer;                                            \
456                                                                                 \
457         rcu_read_lock();                                                        \
458         list_for_each_entry_rcu(inst, &osnoise_instances, list) {               \
459                 buffer = inst->tr->array_buffer.buffer;                         \
460                 trace_array_printk_buf(buffer, _THIS_IP_, msg);                 \
461         }                                                                       \
462         rcu_read_unlock();                                                      \
463         osnoise_data.tainted = true;                                            \
464 })
465
466 /*
467  * Record an osnoise_sample into the tracer buffer.
468  */
469 static void
470 __trace_osnoise_sample(struct osnoise_sample *sample, struct trace_buffer *buffer)
471 {
472         struct trace_event_call *call = &event_osnoise;
473         struct ring_buffer_event *event;
474         struct osnoise_entry *entry;
475
476         event = trace_buffer_lock_reserve(buffer, TRACE_OSNOISE, sizeof(*entry),
477                                           tracing_gen_ctx());
478         if (!event)
479                 return;
480         entry   = ring_buffer_event_data(event);
481         entry->runtime          = sample->runtime;
482         entry->noise            = sample->noise;
483         entry->max_sample       = sample->max_sample;
484         entry->hw_count         = sample->hw_count;
485         entry->nmi_count        = sample->nmi_count;
486         entry->irq_count        = sample->irq_count;
487         entry->softirq_count    = sample->softirq_count;
488         entry->thread_count     = sample->thread_count;
489
490         if (!call_filter_check_discard(call, entry, buffer, event))
491                 trace_buffer_unlock_commit_nostack(buffer, event);
492 }
493
494 /*
495  * Record an osnoise_sample on all osnoise instances.
496  */
497 static void trace_osnoise_sample(struct osnoise_sample *sample)
498 {
499         struct osnoise_instance *inst;
500         struct trace_buffer *buffer;
501
502         rcu_read_lock();
503         list_for_each_entry_rcu(inst, &osnoise_instances, list) {
504                 buffer = inst->tr->array_buffer.buffer;
505                 __trace_osnoise_sample(sample, buffer);
506         }
507         rcu_read_unlock();
508 }
509
510 #ifdef CONFIG_TIMERLAT_TRACER
511 /*
512  * Print the timerlat header info.
513  */
514 #ifdef CONFIG_PREEMPT_RT
515 static void print_timerlat_headers(struct seq_file *s)
516 {
517         seq_puts(s, "#                                _-------=> irqs-off\n");
518         seq_puts(s, "#                               / _------=> need-resched\n");
519         seq_puts(s, "#                              | / _-----=> need-resched-lazy\n");
520         seq_puts(s, "#                              || / _----=> hardirq/softirq\n");
521         seq_puts(s, "#                              ||| / _---=> preempt-depth\n");
522         seq_puts(s, "#                              |||| / _--=> preempt-lazy-depth\n");
523         seq_puts(s, "#                              ||||| / _-=> migrate-disable\n");
524         seq_puts(s, "#                              |||||| /\n");
525         seq_puts(s, "#                              |||||||             ACTIVATION\n");
526         seq_puts(s, "#           TASK-PID      CPU# |||||||   TIMESTAMP    ID     ");
527         seq_puts(s, "       CONTEXT                LATENCY\n");
528         seq_puts(s, "#              | |         |   |||||||      |         |      ");
529         seq_puts(s, "            |                       |\n");
530 }
531 #else /* CONFIG_PREEMPT_RT */
532 static void print_timerlat_headers(struct seq_file *s)
533 {
534         seq_puts(s, "#                                _-----=> irqs-off\n");
535         seq_puts(s, "#                               / _----=> need-resched\n");
536         seq_puts(s, "#                              | / _---=> hardirq/softirq\n");
537         seq_puts(s, "#                              || / _--=> preempt-depth\n");
538         seq_puts(s, "#                              ||| / _-=> migrate-disable\n");
539         seq_puts(s, "#                              |||| /     delay\n");
540         seq_puts(s, "#                              |||||            ACTIVATION\n");
541         seq_puts(s, "#           TASK-PID      CPU# |||||   TIMESTAMP   ID      ");
542         seq_puts(s, "      CONTEXT                 LATENCY\n");
543         seq_puts(s, "#              | |         |   |||||      |         |      ");
544         seq_puts(s, "            |                       |\n");
545 }
546 #endif /* CONFIG_PREEMPT_RT */
547
548 static void
549 __trace_timerlat_sample(struct timerlat_sample *sample, struct trace_buffer *buffer)
550 {
551         struct trace_event_call *call = &event_osnoise;
552         struct ring_buffer_event *event;
553         struct timerlat_entry *entry;
554
555         event = trace_buffer_lock_reserve(buffer, TRACE_TIMERLAT, sizeof(*entry),
556                                           tracing_gen_ctx());
557         if (!event)
558                 return;
559         entry   = ring_buffer_event_data(event);
560         entry->seqnum                   = sample->seqnum;
561         entry->context                  = sample->context;
562         entry->timer_latency            = sample->timer_latency;
563
564         if (!call_filter_check_discard(call, entry, buffer, event))
565                 trace_buffer_unlock_commit_nostack(buffer, event);
566 }
567
568 /*
569  * Record an timerlat_sample into the tracer buffer.
570  */
571 static void trace_timerlat_sample(struct timerlat_sample *sample)
572 {
573         struct osnoise_instance *inst;
574         struct trace_buffer *buffer;
575
576         rcu_read_lock();
577         list_for_each_entry_rcu(inst, &osnoise_instances, list) {
578                 buffer = inst->tr->array_buffer.buffer;
579                 __trace_timerlat_sample(sample, buffer);
580         }
581         rcu_read_unlock();
582 }
583
584 #ifdef CONFIG_STACKTRACE
585
586 #define MAX_CALLS       256
587
588 /*
589  * Stack trace will take place only at IRQ level, so, no need
590  * to control nesting here.
591  */
592 struct trace_stack {
593         int             stack_size;
594         int             nr_entries;
595         unsigned long   calls[MAX_CALLS];
596 };
597
598 static DEFINE_PER_CPU(struct trace_stack, trace_stack);
599
600 /*
601  * timerlat_save_stack - save a stack trace without printing
602  *
603  * Save the current stack trace without printing. The
604  * stack will be printed later, after the end of the measurement.
605  */
606 static void timerlat_save_stack(int skip)
607 {
608         unsigned int size, nr_entries;
609         struct trace_stack *fstack;
610
611         fstack = this_cpu_ptr(&trace_stack);
612
613         size = ARRAY_SIZE(fstack->calls);
614
615         nr_entries = stack_trace_save(fstack->calls, size, skip);
616
617         fstack->stack_size = nr_entries * sizeof(unsigned long);
618         fstack->nr_entries = nr_entries;
619
620         return;
621
622 }
623
624 static void
625 __timerlat_dump_stack(struct trace_buffer *buffer, struct trace_stack *fstack, unsigned int size)
626 {
627         struct trace_event_call *call = &event_osnoise;
628         struct ring_buffer_event *event;
629         struct stack_entry *entry;
630
631         event = trace_buffer_lock_reserve(buffer, TRACE_STACK, sizeof(*entry) + size,
632                                           tracing_gen_ctx());
633         if (!event)
634                 return;
635
636         entry = ring_buffer_event_data(event);
637
638         memcpy(&entry->caller, fstack->calls, size);
639         entry->size = fstack->nr_entries;
640
641         if (!call_filter_check_discard(call, entry, buffer, event))
642                 trace_buffer_unlock_commit_nostack(buffer, event);
643 }
644
645 /*
646  * timerlat_dump_stack - dump a stack trace previously saved
647  */
648 static void timerlat_dump_stack(u64 latency)
649 {
650         struct osnoise_instance *inst;
651         struct trace_buffer *buffer;
652         struct trace_stack *fstack;
653         unsigned int size;
654
655         /*
656          * trace only if latency > print_stack config, if enabled.
657          */
658         if (!osnoise_data.print_stack || osnoise_data.print_stack > latency)
659                 return;
660
661         preempt_disable_notrace();
662         fstack = this_cpu_ptr(&trace_stack);
663         size = fstack->stack_size;
664
665         rcu_read_lock();
666         list_for_each_entry_rcu(inst, &osnoise_instances, list) {
667                 buffer = inst->tr->array_buffer.buffer;
668                 __timerlat_dump_stack(buffer, fstack, size);
669
670         }
671         rcu_read_unlock();
672         preempt_enable_notrace();
673 }
674 #else /* CONFIG_STACKTRACE */
675 #define timerlat_dump_stack(u64 latency) do {} while (0)
676 #define timerlat_save_stack(a) do {} while (0)
677 #endif /* CONFIG_STACKTRACE */
678 #endif /* CONFIG_TIMERLAT_TRACER */
679
680 /*
681  * Macros to encapsulate the time capturing infrastructure.
682  */
683 #define time_get()      trace_clock_local()
684 #define time_to_us(x)   div_u64(x, 1000)
685 #define time_sub(a, b)  ((a) - (b))
686
687 /*
688  * cond_move_irq_delta_start - Forward the delta_start of a running IRQ
689  *
690  * If an IRQ is preempted by an NMI, its delta_start is pushed forward
691  * to discount the NMI interference.
692  *
693  * See get_int_safe_duration().
694  */
695 static inline void
696 cond_move_irq_delta_start(struct osnoise_variables *osn_var, u64 duration)
697 {
698         if (osn_var->irq.delta_start)
699                 osn_var->irq.delta_start += duration;
700 }
701
702 #ifndef CONFIG_PREEMPT_RT
703 /*
704  * cond_move_softirq_delta_start - Forward the delta_start of a running softirq.
705  *
706  * If a softirq is preempted by an IRQ or NMI, its delta_start is pushed
707  * forward to discount the interference.
708  *
709  * See get_int_safe_duration().
710  */
711 static inline void
712 cond_move_softirq_delta_start(struct osnoise_variables *osn_var, u64 duration)
713 {
714         if (osn_var->softirq.delta_start)
715                 osn_var->softirq.delta_start += duration;
716 }
717 #else /* CONFIG_PREEMPT_RT */
718 #define cond_move_softirq_delta_start(osn_var, duration) do {} while (0)
719 #endif
720
721 /*
722  * cond_move_thread_delta_start - Forward the delta_start of a running thread
723  *
724  * If a noisy thread is preempted by an softirq, IRQ or NMI, its delta_start
725  * is pushed forward to discount the interference.
726  *
727  * See get_int_safe_duration().
728  */
729 static inline void
730 cond_move_thread_delta_start(struct osnoise_variables *osn_var, u64 duration)
731 {
732         if (osn_var->thread.delta_start)
733                 osn_var->thread.delta_start += duration;
734 }
735
736 /*
737  * get_int_safe_duration - Get the duration of a window
738  *
739  * The irq, softirq and thread varaibles need to have its duration without
740  * the interference from higher priority interrupts. Instead of keeping a
741  * variable to discount the interrupt interference from these variables, the
742  * starting time of these variables are pushed forward with the interrupt's
743  * duration. In this way, a single variable is used to:
744  *
745  *   - Know if a given window is being measured.
746  *   - Account its duration.
747  *   - Discount the interference.
748  *
749  * To avoid getting inconsistent values, e.g.,:
750  *
751  *      now = time_get()
752  *              --->    interrupt!
753  *                      delta_start -= int duration;
754  *              <---
755  *      duration = now - delta_start;
756  *
757  *      result: negative duration if the variable duration before the
758  *      interrupt was smaller than the interrupt execution.
759  *
760  * A counter of interrupts is used. If the counter increased, try
761  * to capture an interference safe duration.
762  */
763 static inline s64
764 get_int_safe_duration(struct osnoise_variables *osn_var, u64 *delta_start)
765 {
766         u64 int_counter, now;
767         s64 duration;
768
769         do {
770                 int_counter = local_read(&osn_var->int_counter);
771                 /* synchronize with interrupts */
772                 barrier();
773
774                 now = time_get();
775                 duration = (now - *delta_start);
776
777                 /* synchronize with interrupts */
778                 barrier();
779         } while (int_counter != local_read(&osn_var->int_counter));
780
781         /*
782          * This is an evidence of race conditions that cause
783          * a value to be "discounted" too much.
784          */
785         if (duration < 0)
786                 osnoise_taint("Negative duration!\n");
787
788         *delta_start = 0;
789
790         return duration;
791 }
792
793 /*
794  *
795  * set_int_safe_time - Save the current time on *time, aware of interference
796  *
797  * Get the time, taking into consideration a possible interference from
798  * higher priority interrupts.
799  *
800  * See get_int_safe_duration() for an explanation.
801  */
802 static u64
803 set_int_safe_time(struct osnoise_variables *osn_var, u64 *time)
804 {
805         u64 int_counter;
806
807         do {
808                 int_counter = local_read(&osn_var->int_counter);
809                 /* synchronize with interrupts */
810                 barrier();
811
812                 *time = time_get();
813
814                 /* synchronize with interrupts */
815                 barrier();
816         } while (int_counter != local_read(&osn_var->int_counter));
817
818         return int_counter;
819 }
820
821 #ifdef CONFIG_TIMERLAT_TRACER
822 /*
823  * copy_int_safe_time - Copy *src into *desc aware of interference
824  */
825 static u64
826 copy_int_safe_time(struct osnoise_variables *osn_var, u64 *dst, u64 *src)
827 {
828         u64 int_counter;
829
830         do {
831                 int_counter = local_read(&osn_var->int_counter);
832                 /* synchronize with interrupts */
833                 barrier();
834
835                 *dst = *src;
836
837                 /* synchronize with interrupts */
838                 barrier();
839         } while (int_counter != local_read(&osn_var->int_counter));
840
841         return int_counter;
842 }
843 #endif /* CONFIG_TIMERLAT_TRACER */
844
845 /*
846  * trace_osnoise_callback - NMI entry/exit callback
847  *
848  * This function is called at the entry and exit NMI code. The bool enter
849  * distinguishes between either case. This function is used to note a NMI
850  * occurrence, compute the noise caused by the NMI, and to remove the noise
851  * it is potentially causing on other interference variables.
852  */
853 void trace_osnoise_callback(bool enter)
854 {
855         struct osnoise_variables *osn_var = this_cpu_osn_var();
856         u64 duration;
857
858         if (!osn_var->sampling)
859                 return;
860
861         /*
862          * Currently trace_clock_local() calls sched_clock() and the
863          * generic version is not NMI safe.
864          */
865         if (!IS_ENABLED(CONFIG_GENERIC_SCHED_CLOCK)) {
866                 if (enter) {
867                         osn_var->nmi.delta_start = time_get();
868                         local_inc(&osn_var->int_counter);
869                 } else {
870                         duration = time_get() - osn_var->nmi.delta_start;
871
872                         trace_nmi_noise(osn_var->nmi.delta_start, duration);
873
874                         cond_move_irq_delta_start(osn_var, duration);
875                         cond_move_softirq_delta_start(osn_var, duration);
876                         cond_move_thread_delta_start(osn_var, duration);
877                 }
878         }
879
880         if (enter)
881                 osn_var->nmi.count++;
882 }
883
884 /*
885  * osnoise_trace_irq_entry - Note the starting of an IRQ
886  *
887  * Save the starting time of an IRQ. As IRQs are non-preemptive to other IRQs,
888  * it is safe to use a single variable (ons_var->irq) to save the statistics.
889  * The arrival_time is used to report... the arrival time. The delta_start
890  * is used to compute the duration at the IRQ exit handler. See
891  * cond_move_irq_delta_start().
892  */
893 void osnoise_trace_irq_entry(int id)
894 {
895         struct osnoise_variables *osn_var = this_cpu_osn_var();
896
897         if (!osn_var->sampling)
898                 return;
899         /*
900          * This value will be used in the report, but not to compute
901          * the execution time, so it is safe to get it unsafe.
902          */
903         osn_var->irq.arrival_time = time_get();
904         set_int_safe_time(osn_var, &osn_var->irq.delta_start);
905         osn_var->irq.count++;
906
907         local_inc(&osn_var->int_counter);
908 }
909
910 /*
911  * osnoise_irq_exit - Note the end of an IRQ, sava data and trace
912  *
913  * Computes the duration of the IRQ noise, and trace it. Also discounts the
914  * interference from other sources of noise could be currently being accounted.
915  */
916 void osnoise_trace_irq_exit(int id, const char *desc)
917 {
918         struct osnoise_variables *osn_var = this_cpu_osn_var();
919         s64 duration;
920
921         if (!osn_var->sampling)
922                 return;
923
924         duration = get_int_safe_duration(osn_var, &osn_var->irq.delta_start);
925         trace_irq_noise(id, desc, osn_var->irq.arrival_time, duration);
926         osn_var->irq.arrival_time = 0;
927         cond_move_softirq_delta_start(osn_var, duration);
928         cond_move_thread_delta_start(osn_var, duration);
929 }
930
931 /*
932  * trace_irqentry_callback - Callback to the irq:irq_entry traceevent
933  *
934  * Used to note the starting of an IRQ occurece.
935  */
936 static void trace_irqentry_callback(void *data, int irq,
937                                     struct irqaction *action)
938 {
939         osnoise_trace_irq_entry(irq);
940 }
941
942 /*
943  * trace_irqexit_callback - Callback to the irq:irq_exit traceevent
944  *
945  * Used to note the end of an IRQ occurece.
946  */
947 static void trace_irqexit_callback(void *data, int irq,
948                                    struct irqaction *action, int ret)
949 {
950         osnoise_trace_irq_exit(irq, action->name);
951 }
952
953 /*
954  * arch specific register function.
955  */
956 int __weak osnoise_arch_register(void)
957 {
958         return 0;
959 }
960
961 /*
962  * arch specific unregister function.
963  */
964 void __weak osnoise_arch_unregister(void)
965 {
966         return;
967 }
968
969 /*
970  * hook_irq_events - Hook IRQ handling events
971  *
972  * This function hooks the IRQ related callbacks to the respective trace
973  * events.
974  */
975 static int hook_irq_events(void)
976 {
977         int ret;
978
979         ret = register_trace_irq_handler_entry(trace_irqentry_callback, NULL);
980         if (ret)
981                 goto out_err;
982
983         ret = register_trace_irq_handler_exit(trace_irqexit_callback, NULL);
984         if (ret)
985                 goto out_unregister_entry;
986
987         ret = osnoise_arch_register();
988         if (ret)
989                 goto out_irq_exit;
990
991         return 0;
992
993 out_irq_exit:
994         unregister_trace_irq_handler_exit(trace_irqexit_callback, NULL);
995 out_unregister_entry:
996         unregister_trace_irq_handler_entry(trace_irqentry_callback, NULL);
997 out_err:
998         return -EINVAL;
999 }
1000
1001 /*
1002  * unhook_irq_events - Unhook IRQ handling events
1003  *
1004  * This function unhooks the IRQ related callbacks to the respective trace
1005  * events.
1006  */
1007 static void unhook_irq_events(void)
1008 {
1009         osnoise_arch_unregister();
1010         unregister_trace_irq_handler_exit(trace_irqexit_callback, NULL);
1011         unregister_trace_irq_handler_entry(trace_irqentry_callback, NULL);
1012 }
1013
1014 #ifndef CONFIG_PREEMPT_RT
1015 /*
1016  * trace_softirq_entry_callback - Note the starting of a softirq
1017  *
1018  * Save the starting time of a softirq. As softirqs are non-preemptive to
1019  * other softirqs, it is safe to use a single variable (ons_var->softirq)
1020  * to save the statistics. The arrival_time is used to report... the
1021  * arrival time. The delta_start is used to compute the duration at the
1022  * softirq exit handler. See cond_move_softirq_delta_start().
1023  */
1024 static void trace_softirq_entry_callback(void *data, unsigned int vec_nr)
1025 {
1026         struct osnoise_variables *osn_var = this_cpu_osn_var();
1027
1028         if (!osn_var->sampling)
1029                 return;
1030         /*
1031          * This value will be used in the report, but not to compute
1032          * the execution time, so it is safe to get it unsafe.
1033          */
1034         osn_var->softirq.arrival_time = time_get();
1035         set_int_safe_time(osn_var, &osn_var->softirq.delta_start);
1036         osn_var->softirq.count++;
1037
1038         local_inc(&osn_var->int_counter);
1039 }
1040
1041 /*
1042  * trace_softirq_exit_callback - Note the end of an softirq
1043  *
1044  * Computes the duration of the softirq noise, and trace it. Also discounts the
1045  * interference from other sources of noise could be currently being accounted.
1046  */
1047 static void trace_softirq_exit_callback(void *data, unsigned int vec_nr)
1048 {
1049         struct osnoise_variables *osn_var = this_cpu_osn_var();
1050         s64 duration;
1051
1052         if (!osn_var->sampling)
1053                 return;
1054
1055         if (unlikely(timerlat_enabled()))
1056                 if (!timerlat_softirq_exit(osn_var))
1057                         return;
1058
1059         duration = get_int_safe_duration(osn_var, &osn_var->softirq.delta_start);
1060         trace_softirq_noise(vec_nr, osn_var->softirq.arrival_time, duration);
1061         cond_move_thread_delta_start(osn_var, duration);
1062         osn_var->softirq.arrival_time = 0;
1063 }
1064
1065 /*
1066  * hook_softirq_events - Hook softirq handling events
1067  *
1068  * This function hooks the softirq related callbacks to the respective trace
1069  * events.
1070  */
1071 static int hook_softirq_events(void)
1072 {
1073         int ret;
1074
1075         ret = register_trace_softirq_entry(trace_softirq_entry_callback, NULL);
1076         if (ret)
1077                 goto out_err;
1078
1079         ret = register_trace_softirq_exit(trace_softirq_exit_callback, NULL);
1080         if (ret)
1081                 goto out_unreg_entry;
1082
1083         return 0;
1084
1085 out_unreg_entry:
1086         unregister_trace_softirq_entry(trace_softirq_entry_callback, NULL);
1087 out_err:
1088         return -EINVAL;
1089 }
1090
1091 /*
1092  * unhook_softirq_events - Unhook softirq handling events
1093  *
1094  * This function hooks the softirq related callbacks to the respective trace
1095  * events.
1096  */
1097 static void unhook_softirq_events(void)
1098 {
1099         unregister_trace_softirq_entry(trace_softirq_entry_callback, NULL);
1100         unregister_trace_softirq_exit(trace_softirq_exit_callback, NULL);
1101 }
1102 #else /* CONFIG_PREEMPT_RT */
1103 /*
1104  * softirq are threads on the PREEMPT_RT mode.
1105  */
1106 static int hook_softirq_events(void)
1107 {
1108         return 0;
1109 }
1110 static void unhook_softirq_events(void)
1111 {
1112 }
1113 #endif
1114
1115 /*
1116  * thread_entry - Record the starting of a thread noise window
1117  *
1118  * It saves the context switch time for a noisy thread, and increments
1119  * the interference counters.
1120  */
1121 static void
1122 thread_entry(struct osnoise_variables *osn_var, struct task_struct *t)
1123 {
1124         if (!osn_var->sampling)
1125                 return;
1126         /*
1127          * The arrival time will be used in the report, but not to compute
1128          * the execution time, so it is safe to get it unsafe.
1129          */
1130         osn_var->thread.arrival_time = time_get();
1131
1132         set_int_safe_time(osn_var, &osn_var->thread.delta_start);
1133
1134         osn_var->thread.count++;
1135         local_inc(&osn_var->int_counter);
1136 }
1137
1138 /*
1139  * thread_exit - Report the end of a thread noise window
1140  *
1141  * It computes the total noise from a thread, tracing if needed.
1142  */
1143 static void
1144 thread_exit(struct osnoise_variables *osn_var, struct task_struct *t)
1145 {
1146         s64 duration;
1147
1148         if (!osn_var->sampling)
1149                 return;
1150
1151         if (unlikely(timerlat_enabled()))
1152                 if (!timerlat_thread_exit(osn_var))
1153                         return;
1154
1155         duration = get_int_safe_duration(osn_var, &osn_var->thread.delta_start);
1156
1157         trace_thread_noise(t, osn_var->thread.arrival_time, duration);
1158
1159         osn_var->thread.arrival_time = 0;
1160 }
1161
1162 /*
1163  * trace_sched_switch - sched:sched_switch trace event handler
1164  *
1165  * This function is hooked to the sched:sched_switch trace event, and it is
1166  * used to record the beginning and to report the end of a thread noise window.
1167  */
1168 static void
1169 trace_sched_switch_callback(void *data, bool preempt,
1170                             struct task_struct *p,
1171                             struct task_struct *n,
1172                             unsigned int prev_state)
1173 {
1174         struct osnoise_variables *osn_var = this_cpu_osn_var();
1175
1176         if (p->pid != osn_var->pid)
1177                 thread_exit(osn_var, p);
1178
1179         if (n->pid != osn_var->pid)
1180                 thread_entry(osn_var, n);
1181 }
1182
1183 /*
1184  * hook_thread_events - Hook the insturmentation for thread noise
1185  *
1186  * Hook the osnoise tracer callbacks to handle the noise from other
1187  * threads on the necessary kernel events.
1188  */
1189 static int hook_thread_events(void)
1190 {
1191         int ret;
1192
1193         ret = register_trace_sched_switch(trace_sched_switch_callback, NULL);
1194         if (ret)
1195                 return -EINVAL;
1196
1197         return 0;
1198 }
1199
1200 /*
1201  * unhook_thread_events - *nhook the insturmentation for thread noise
1202  *
1203  * Unook the osnoise tracer callbacks to handle the noise from other
1204  * threads on the necessary kernel events.
1205  */
1206 static void unhook_thread_events(void)
1207 {
1208         unregister_trace_sched_switch(trace_sched_switch_callback, NULL);
1209 }
1210
1211 /*
1212  * save_osn_sample_stats - Save the osnoise_sample statistics
1213  *
1214  * Save the osnoise_sample statistics before the sampling phase. These
1215  * values will be used later to compute the diff betwneen the statistics
1216  * before and after the osnoise sampling.
1217  */
1218 static void
1219 save_osn_sample_stats(struct osnoise_variables *osn_var, struct osnoise_sample *s)
1220 {
1221         s->nmi_count = osn_var->nmi.count;
1222         s->irq_count = osn_var->irq.count;
1223         s->softirq_count = osn_var->softirq.count;
1224         s->thread_count = osn_var->thread.count;
1225 }
1226
1227 /*
1228  * diff_osn_sample_stats - Compute the osnoise_sample statistics
1229  *
1230  * After a sample period, compute the difference on the osnoise_sample
1231  * statistics. The struct osnoise_sample *s contains the statistics saved via
1232  * save_osn_sample_stats() before the osnoise sampling.
1233  */
1234 static void
1235 diff_osn_sample_stats(struct osnoise_variables *osn_var, struct osnoise_sample *s)
1236 {
1237         s->nmi_count = osn_var->nmi.count - s->nmi_count;
1238         s->irq_count = osn_var->irq.count - s->irq_count;
1239         s->softirq_count = osn_var->softirq.count - s->softirq_count;
1240         s->thread_count = osn_var->thread.count - s->thread_count;
1241 }
1242
1243 /*
1244  * osnoise_stop_tracing - Stop tracing and the tracer.
1245  */
1246 static __always_inline void osnoise_stop_tracing(void)
1247 {
1248         struct osnoise_instance *inst;
1249         struct trace_array *tr;
1250
1251         rcu_read_lock();
1252         list_for_each_entry_rcu(inst, &osnoise_instances, list) {
1253                 tr = inst->tr;
1254                 trace_array_printk_buf(tr->array_buffer.buffer, _THIS_IP_,
1255                                 "stop tracing hit on cpu %d\n", smp_processor_id());
1256
1257                 tracer_tracing_off(tr);
1258         }
1259         rcu_read_unlock();
1260 }
1261
1262 /*
1263  * notify_new_max_latency - Notify a new max latency via fsnotify interface.
1264  */
1265 static void notify_new_max_latency(u64 latency)
1266 {
1267         struct osnoise_instance *inst;
1268         struct trace_array *tr;
1269
1270         rcu_read_lock();
1271         list_for_each_entry_rcu(inst, &osnoise_instances, list) {
1272                 tr = inst->tr;
1273                 if (tracer_tracing_is_on(tr) && tr->max_latency < latency) {
1274                         tr->max_latency = latency;
1275                         latency_fsnotify(tr);
1276                 }
1277         }
1278         rcu_read_unlock();
1279 }
1280
1281 /*
1282  * run_osnoise - Sample the time and look for osnoise
1283  *
1284  * Used to capture the time, looking for potential osnoise latency repeatedly.
1285  * Different from hwlat_detector, it is called with preemption and interrupts
1286  * enabled. This allows irqs, softirqs and threads to run, interfering on the
1287  * osnoise sampling thread, as they would do with a regular thread.
1288  */
1289 static int run_osnoise(void)
1290 {
1291         struct osnoise_variables *osn_var = this_cpu_osn_var();
1292         u64 start, sample, last_sample;
1293         u64 last_int_count, int_count;
1294         s64 noise = 0, max_noise = 0;
1295         s64 total, last_total = 0;
1296         struct osnoise_sample s;
1297         unsigned int threshold;
1298         u64 runtime, stop_in;
1299         u64 sum_noise = 0;
1300         int hw_count = 0;
1301         int ret = -1;
1302
1303         /*
1304          * Considers the current thread as the workload.
1305          */
1306         osn_var->pid = current->pid;
1307
1308         /*
1309          * Save the current stats for the diff
1310          */
1311         save_osn_sample_stats(osn_var, &s);
1312
1313         /*
1314          * if threshold is 0, use the default value of 5 us.
1315          */
1316         threshold = tracing_thresh ? : 5000;
1317
1318         /*
1319          * Make sure NMIs see sampling first
1320          */
1321         osn_var->sampling = true;
1322         barrier();
1323
1324         /*
1325          * Transform the *_us config to nanoseconds to avoid the
1326          * division on the main loop.
1327          */
1328         runtime = osnoise_data.sample_runtime * NSEC_PER_USEC;
1329         stop_in = osnoise_data.stop_tracing * NSEC_PER_USEC;
1330
1331         /*
1332          * Start timestemp
1333          */
1334         start = time_get();
1335
1336         /*
1337          * "previous" loop.
1338          */
1339         last_int_count = set_int_safe_time(osn_var, &last_sample);
1340
1341         do {
1342                 /*
1343                  * Get sample!
1344                  */
1345                 int_count = set_int_safe_time(osn_var, &sample);
1346
1347                 noise = time_sub(sample, last_sample);
1348
1349                 /*
1350                  * This shouldn't happen.
1351                  */
1352                 if (noise < 0) {
1353                         osnoise_taint("negative noise!");
1354                         goto out;
1355                 }
1356
1357                 /*
1358                  * Sample runtime.
1359                  */
1360                 total = time_sub(sample, start);
1361
1362                 /*
1363                  * Check for possible overflows.
1364                  */
1365                 if (total < last_total) {
1366                         osnoise_taint("total overflow!");
1367                         break;
1368                 }
1369
1370                 last_total = total;
1371
1372                 if (noise >= threshold) {
1373                         int interference = int_count - last_int_count;
1374
1375                         if (noise > max_noise)
1376                                 max_noise = noise;
1377
1378                         if (!interference)
1379                                 hw_count++;
1380
1381                         sum_noise += noise;
1382
1383                         trace_sample_threshold(last_sample, noise, interference);
1384
1385                         if (osnoise_data.stop_tracing)
1386                                 if (noise > stop_in)
1387                                         osnoise_stop_tracing();
1388                 }
1389
1390                 /*
1391                  * In some cases, notably when running on a nohz_full CPU with
1392                  * a stopped tick PREEMPT_RCU has no way to account for QSs.
1393                  * This will eventually cause unwarranted noise as PREEMPT_RCU
1394                  * will force preemption as the means of ending the current
1395                  * grace period. We avoid this problem by calling
1396                  * rcu_momentary_dyntick_idle(), which performs a zero duration
1397                  * EQS allowing PREEMPT_RCU to end the current grace period.
1398                  * This call shouldn't be wrapped inside an RCU critical
1399                  * section.
1400                  *
1401                  * Note that in non PREEMPT_RCU kernels QSs are handled through
1402                  * cond_resched()
1403                  */
1404                 if (IS_ENABLED(CONFIG_PREEMPT_RCU)) {
1405                         local_irq_disable();
1406                         rcu_momentary_dyntick_idle();
1407                         local_irq_enable();
1408                 }
1409
1410                 /*
1411                  * For the non-preemptive kernel config: let threads runs, if
1412                  * they so wish.
1413                  */
1414                 cond_resched();
1415
1416                 last_sample = sample;
1417                 last_int_count = int_count;
1418
1419         } while (total < runtime && !kthread_should_stop());
1420
1421         /*
1422          * Finish the above in the view for interrupts.
1423          */
1424         barrier();
1425
1426         osn_var->sampling = false;
1427
1428         /*
1429          * Make sure sampling data is no longer updated.
1430          */
1431         barrier();
1432
1433         /*
1434          * Save noise info.
1435          */
1436         s.noise = time_to_us(sum_noise);
1437         s.runtime = time_to_us(total);
1438         s.max_sample = time_to_us(max_noise);
1439         s.hw_count = hw_count;
1440
1441         /* Save interference stats info */
1442         diff_osn_sample_stats(osn_var, &s);
1443
1444         trace_osnoise_sample(&s);
1445
1446         notify_new_max_latency(max_noise);
1447
1448         if (osnoise_data.stop_tracing_total)
1449                 if (s.noise > osnoise_data.stop_tracing_total)
1450                         osnoise_stop_tracing();
1451
1452         return 0;
1453 out:
1454         return ret;
1455 }
1456
1457 static struct cpumask osnoise_cpumask;
1458 static struct cpumask save_cpumask;
1459
1460 /*
1461  * osnoise_sleep - sleep until the next period
1462  */
1463 static void osnoise_sleep(void)
1464 {
1465         u64 interval;
1466         ktime_t wake_time;
1467
1468         mutex_lock(&interface_lock);
1469         interval = osnoise_data.sample_period - osnoise_data.sample_runtime;
1470         mutex_unlock(&interface_lock);
1471
1472         /*
1473          * differently from hwlat_detector, the osnoise tracer can run
1474          * without a pause because preemption is on.
1475          */
1476         if (!interval) {
1477                 /* Let synchronize_rcu_tasks() make progress */
1478                 cond_resched_tasks_rcu_qs();
1479                 return;
1480         }
1481
1482         wake_time = ktime_add_us(ktime_get(), interval);
1483         __set_current_state(TASK_INTERRUPTIBLE);
1484
1485         while (schedule_hrtimeout_range(&wake_time, 0, HRTIMER_MODE_ABS)) {
1486                 if (kthread_should_stop())
1487                         break;
1488         }
1489 }
1490
1491 /*
1492  * osnoise_main - The osnoise detection kernel thread
1493  *
1494  * Calls run_osnoise() function to measure the osnoise for the configured runtime,
1495  * every period.
1496  */
1497 static int osnoise_main(void *data)
1498 {
1499
1500         while (!kthread_should_stop()) {
1501                 run_osnoise();
1502                 osnoise_sleep();
1503         }
1504
1505         return 0;
1506 }
1507
1508 #ifdef CONFIG_TIMERLAT_TRACER
1509 /*
1510  * timerlat_irq - hrtimer handler for timerlat.
1511  */
1512 static enum hrtimer_restart timerlat_irq(struct hrtimer *timer)
1513 {
1514         struct osnoise_variables *osn_var = this_cpu_osn_var();
1515         struct timerlat_variables *tlat;
1516         struct timerlat_sample s;
1517         u64 now;
1518         u64 diff;
1519
1520         /*
1521          * I am not sure if the timer was armed for this CPU. So, get
1522          * the timerlat struct from the timer itself, not from this
1523          * CPU.
1524          */
1525         tlat = container_of(timer, struct timerlat_variables, timer);
1526
1527         now = ktime_to_ns(hrtimer_cb_get_time(&tlat->timer));
1528
1529         /*
1530          * Enable the osnoise: events for thread an softirq.
1531          */
1532         tlat->tracing_thread = true;
1533
1534         osn_var->thread.arrival_time = time_get();
1535
1536         /*
1537          * A hardirq is running: the timer IRQ. It is for sure preempting
1538          * a thread, and potentially preempting a softirq.
1539          *
1540          * At this point, it is not interesting to know the duration of the
1541          * preempted thread (and maybe softirq), but how much time they will
1542          * delay the beginning of the execution of the timer thread.
1543          *
1544          * To get the correct (net) delay added by the softirq, its delta_start
1545          * is set as the IRQ one. In this way, at the return of the IRQ, the delta
1546          * start of the sofitrq will be zeroed, accounting then only the time
1547          * after that.
1548          *
1549          * The thread follows the same principle. However, if a softirq is
1550          * running, the thread needs to receive the softirq delta_start. The
1551          * reason being is that the softirq will be the last to be unfolded,
1552          * resseting the thread delay to zero.
1553          *
1554          * The PREEMPT_RT is a special case, though. As softirqs run as threads
1555          * on RT, moving the thread is enough.
1556          */
1557         if (!IS_ENABLED(CONFIG_PREEMPT_RT) && osn_var->softirq.delta_start) {
1558                 copy_int_safe_time(osn_var, &osn_var->thread.delta_start,
1559                                    &osn_var->softirq.delta_start);
1560
1561                 copy_int_safe_time(osn_var, &osn_var->softirq.delta_start,
1562                                     &osn_var->irq.delta_start);
1563         } else {
1564                 copy_int_safe_time(osn_var, &osn_var->thread.delta_start,
1565                                     &osn_var->irq.delta_start);
1566         }
1567
1568         /*
1569          * Compute the current time with the expected time.
1570          */
1571         diff = now - tlat->abs_period;
1572
1573         tlat->count++;
1574         s.seqnum = tlat->count;
1575         s.timer_latency = diff;
1576         s.context = IRQ_CONTEXT;
1577
1578         trace_timerlat_sample(&s);
1579
1580         if (osnoise_data.stop_tracing) {
1581                 if (time_to_us(diff) >= osnoise_data.stop_tracing) {
1582
1583                         /*
1584                          * At this point, if stop_tracing is set and <= print_stack,
1585                          * print_stack is set and would be printed in the thread handler.
1586                          *
1587                          * Thus, print the stack trace as it is helpful to define the
1588                          * root cause of an IRQ latency.
1589                          */
1590                         if (osnoise_data.stop_tracing <= osnoise_data.print_stack) {
1591                                 timerlat_save_stack(0);
1592                                 timerlat_dump_stack(time_to_us(diff));
1593                         }
1594
1595                         osnoise_stop_tracing();
1596                         notify_new_max_latency(diff);
1597
1598                         wake_up_process(tlat->kthread);
1599
1600                         return HRTIMER_NORESTART;
1601                 }
1602         }
1603
1604         wake_up_process(tlat->kthread);
1605
1606         if (osnoise_data.print_stack)
1607                 timerlat_save_stack(0);
1608
1609         return HRTIMER_NORESTART;
1610 }
1611
1612 /*
1613  * wait_next_period - Wait for the next period for timerlat
1614  */
1615 static int wait_next_period(struct timerlat_variables *tlat)
1616 {
1617         ktime_t next_abs_period, now;
1618         u64 rel_period = osnoise_data.timerlat_period * 1000;
1619
1620         now = hrtimer_cb_get_time(&tlat->timer);
1621         next_abs_period = ns_to_ktime(tlat->abs_period + rel_period);
1622
1623         /*
1624          * Save the next abs_period.
1625          */
1626         tlat->abs_period = (u64) ktime_to_ns(next_abs_period);
1627
1628         /*
1629          * If the new abs_period is in the past, skip the activation.
1630          */
1631         while (ktime_compare(now, next_abs_period) > 0) {
1632                 next_abs_period = ns_to_ktime(tlat->abs_period + rel_period);
1633                 tlat->abs_period = (u64) ktime_to_ns(next_abs_period);
1634         }
1635
1636         set_current_state(TASK_INTERRUPTIBLE);
1637
1638         hrtimer_start(&tlat->timer, next_abs_period, HRTIMER_MODE_ABS_PINNED_HARD);
1639         schedule();
1640         return 1;
1641 }
1642
1643 /*
1644  * timerlat_main- Timerlat main
1645  */
1646 static int timerlat_main(void *data)
1647 {
1648         struct osnoise_variables *osn_var = this_cpu_osn_var();
1649         struct timerlat_variables *tlat = this_cpu_tmr_var();
1650         struct timerlat_sample s;
1651         struct sched_param sp;
1652         u64 now, diff;
1653
1654         /*
1655          * Make the thread RT, that is how cyclictest is usually used.
1656          */
1657         sp.sched_priority = DEFAULT_TIMERLAT_PRIO;
1658         sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
1659
1660         tlat->count = 0;
1661         tlat->tracing_thread = false;
1662
1663         hrtimer_init(&tlat->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED_HARD);
1664         tlat->timer.function = timerlat_irq;
1665         tlat->kthread = current;
1666         osn_var->pid = current->pid;
1667         /*
1668          * Anotate the arrival time.
1669          */
1670         tlat->abs_period = hrtimer_cb_get_time(&tlat->timer);
1671
1672         wait_next_period(tlat);
1673
1674         osn_var->sampling = 1;
1675
1676         while (!kthread_should_stop()) {
1677                 now = ktime_to_ns(hrtimer_cb_get_time(&tlat->timer));
1678                 diff = now - tlat->abs_period;
1679
1680                 s.seqnum = tlat->count;
1681                 s.timer_latency = diff;
1682                 s.context = THREAD_CONTEXT;
1683
1684                 trace_timerlat_sample(&s);
1685
1686                 notify_new_max_latency(diff);
1687
1688                 timerlat_dump_stack(time_to_us(diff));
1689
1690                 tlat->tracing_thread = false;
1691                 if (osnoise_data.stop_tracing_total)
1692                         if (time_to_us(diff) >= osnoise_data.stop_tracing_total)
1693                                 osnoise_stop_tracing();
1694
1695                 wait_next_period(tlat);
1696         }
1697
1698         hrtimer_cancel(&tlat->timer);
1699         return 0;
1700 }
1701 #else /* CONFIG_TIMERLAT_TRACER */
1702 static int timerlat_main(void *data)
1703 {
1704         return 0;
1705 }
1706 #endif /* CONFIG_TIMERLAT_TRACER */
1707
1708 /*
1709  * stop_kthread - stop a workload thread
1710  */
1711 static void stop_kthread(unsigned int cpu)
1712 {
1713         struct task_struct *kthread;
1714
1715         kthread = per_cpu(per_cpu_osnoise_var, cpu).kthread;
1716         if (kthread)
1717                 kthread_stop(kthread);
1718         per_cpu(per_cpu_osnoise_var, cpu).kthread = NULL;
1719 }
1720
1721 /*
1722  * stop_per_cpu_kthread - Stop per-cpu threads
1723  *
1724  * Stop the osnoise sampling htread. Use this on unload and at system
1725  * shutdown.
1726  */
1727 static void stop_per_cpu_kthreads(void)
1728 {
1729         int cpu;
1730
1731         cpus_read_lock();
1732
1733         for_each_online_cpu(cpu)
1734                 stop_kthread(cpu);
1735
1736         cpus_read_unlock();
1737 }
1738
1739 /*
1740  * start_kthread - Start a workload tread
1741  */
1742 static int start_kthread(unsigned int cpu)
1743 {
1744         struct task_struct *kthread;
1745         void *main = osnoise_main;
1746         char comm[24];
1747
1748         if (timerlat_enabled()) {
1749                 snprintf(comm, 24, "timerlat/%d", cpu);
1750                 main = timerlat_main;
1751         } else {
1752                 snprintf(comm, 24, "osnoise/%d", cpu);
1753         }
1754
1755         kthread = kthread_run_on_cpu(main, NULL, cpu, comm);
1756
1757         if (IS_ERR(kthread)) {
1758                 pr_err(BANNER "could not start sampling thread\n");
1759                 stop_per_cpu_kthreads();
1760                 return -ENOMEM;
1761         }
1762
1763         per_cpu(per_cpu_osnoise_var, cpu).kthread = kthread;
1764
1765         return 0;
1766 }
1767
1768 /*
1769  * start_per_cpu_kthread - Kick off per-cpu osnoise sampling kthreads
1770  *
1771  * This starts the kernel thread that will look for osnoise on many
1772  * cpus.
1773  */
1774 static int start_per_cpu_kthreads(void)
1775 {
1776         struct cpumask *current_mask = &save_cpumask;
1777         int retval = 0;
1778         int cpu;
1779
1780         cpus_read_lock();
1781         /*
1782          * Run only on online CPUs in which osnoise is allowed to run.
1783          */
1784         cpumask_and(current_mask, cpu_online_mask, &osnoise_cpumask);
1785
1786         for_each_possible_cpu(cpu)
1787                 per_cpu(per_cpu_osnoise_var, cpu).kthread = NULL;
1788
1789         for_each_cpu(cpu, current_mask) {
1790                 retval = start_kthread(cpu);
1791                 if (retval) {
1792                         cpus_read_unlock();
1793                         stop_per_cpu_kthreads();
1794                         return retval;
1795                 }
1796         }
1797
1798         cpus_read_unlock();
1799
1800         return retval;
1801 }
1802
1803 #ifdef CONFIG_HOTPLUG_CPU
1804 static void osnoise_hotplug_workfn(struct work_struct *dummy)
1805 {
1806         unsigned int cpu = smp_processor_id();
1807
1808         mutex_lock(&trace_types_lock);
1809
1810         if (!osnoise_has_registered_instances())
1811                 goto out_unlock_trace;
1812
1813         mutex_lock(&interface_lock);
1814         cpus_read_lock();
1815
1816         if (!cpumask_test_cpu(cpu, &osnoise_cpumask))
1817                 goto out_unlock;
1818
1819         start_kthread(cpu);
1820
1821 out_unlock:
1822         cpus_read_unlock();
1823         mutex_unlock(&interface_lock);
1824 out_unlock_trace:
1825         mutex_unlock(&trace_types_lock);
1826 }
1827
1828 static DECLARE_WORK(osnoise_hotplug_work, osnoise_hotplug_workfn);
1829
1830 /*
1831  * osnoise_cpu_init - CPU hotplug online callback function
1832  */
1833 static int osnoise_cpu_init(unsigned int cpu)
1834 {
1835         schedule_work_on(cpu, &osnoise_hotplug_work);
1836         return 0;
1837 }
1838
1839 /*
1840  * osnoise_cpu_die - CPU hotplug offline callback function
1841  */
1842 static int osnoise_cpu_die(unsigned int cpu)
1843 {
1844         stop_kthread(cpu);
1845         return 0;
1846 }
1847
1848 static void osnoise_init_hotplug_support(void)
1849 {
1850         int ret;
1851
1852         ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "trace/osnoise:online",
1853                                 osnoise_cpu_init, osnoise_cpu_die);
1854         if (ret < 0)
1855                 pr_warn(BANNER "Error to init cpu hotplug support\n");
1856
1857         return;
1858 }
1859 #else /* CONFIG_HOTPLUG_CPU */
1860 static void osnoise_init_hotplug_support(void)
1861 {
1862         return;
1863 }
1864 #endif /* CONFIG_HOTPLUG_CPU */
1865
1866 /*
1867  * osnoise_cpus_read - Read function for reading the "cpus" file
1868  * @filp: The active open file structure
1869  * @ubuf: The userspace provided buffer to read value into
1870  * @cnt: The maximum number of bytes to read
1871  * @ppos: The current "file" position
1872  *
1873  * Prints the "cpus" output into the user-provided buffer.
1874  */
1875 static ssize_t
1876 osnoise_cpus_read(struct file *filp, char __user *ubuf, size_t count,
1877                   loff_t *ppos)
1878 {
1879         char *mask_str;
1880         int len;
1881
1882         mutex_lock(&interface_lock);
1883
1884         len = snprintf(NULL, 0, "%*pbl\n", cpumask_pr_args(&osnoise_cpumask)) + 1;
1885         mask_str = kmalloc(len, GFP_KERNEL);
1886         if (!mask_str) {
1887                 count = -ENOMEM;
1888                 goto out_unlock;
1889         }
1890
1891         len = snprintf(mask_str, len, "%*pbl\n", cpumask_pr_args(&osnoise_cpumask));
1892         if (len >= count) {
1893                 count = -EINVAL;
1894                 goto out_free;
1895         }
1896
1897         count = simple_read_from_buffer(ubuf, count, ppos, mask_str, len);
1898
1899 out_free:
1900         kfree(mask_str);
1901 out_unlock:
1902         mutex_unlock(&interface_lock);
1903
1904         return count;
1905 }
1906
1907 /*
1908  * osnoise_cpus_write - Write function for "cpus" entry
1909  * @filp: The active open file structure
1910  * @ubuf: The user buffer that contains the value to write
1911  * @cnt: The maximum number of bytes to write to "file"
1912  * @ppos: The current position in @file
1913  *
1914  * This function provides a write implementation for the "cpus"
1915  * interface to the osnoise trace. By default, it lists all  CPUs,
1916  * in this way, allowing osnoise threads to run on any online CPU
1917  * of the system. It serves to restrict the execution of osnoise to the
1918  * set of CPUs writing via this interface. Why not use "tracing_cpumask"?
1919  * Because the user might be interested in tracing what is running on
1920  * other CPUs. For instance, one might run osnoise in one HT CPU
1921  * while observing what is running on the sibling HT CPU.
1922  */
1923 static ssize_t
1924 osnoise_cpus_write(struct file *filp, const char __user *ubuf, size_t count,
1925                    loff_t *ppos)
1926 {
1927         cpumask_var_t osnoise_cpumask_new;
1928         int running, err;
1929         char buf[256];
1930
1931         if (count >= 256)
1932                 return -EINVAL;
1933
1934         if (copy_from_user(buf, ubuf, count))
1935                 return -EFAULT;
1936
1937         if (!zalloc_cpumask_var(&osnoise_cpumask_new, GFP_KERNEL))
1938                 return -ENOMEM;
1939
1940         err = cpulist_parse(buf, osnoise_cpumask_new);
1941         if (err)
1942                 goto err_free;
1943
1944         /*
1945          * trace_types_lock is taken to avoid concurrency on start/stop.
1946          */
1947         mutex_lock(&trace_types_lock);
1948         running = osnoise_has_registered_instances();
1949         if (running)
1950                 stop_per_cpu_kthreads();
1951
1952         mutex_lock(&interface_lock);
1953         /*
1954          * osnoise_cpumask is read by CPU hotplug operations.
1955          */
1956         cpus_read_lock();
1957
1958         cpumask_copy(&osnoise_cpumask, osnoise_cpumask_new);
1959
1960         cpus_read_unlock();
1961         mutex_unlock(&interface_lock);
1962
1963         if (running)
1964                 start_per_cpu_kthreads();
1965         mutex_unlock(&trace_types_lock);
1966
1967         free_cpumask_var(osnoise_cpumask_new);
1968         return count;
1969
1970 err_free:
1971         free_cpumask_var(osnoise_cpumask_new);
1972
1973         return err;
1974 }
1975
1976 /*
1977  * osnoise/runtime_us: cannot be greater than the period.
1978  */
1979 static struct trace_min_max_param osnoise_runtime = {
1980         .lock   = &interface_lock,
1981         .val    = &osnoise_data.sample_runtime,
1982         .max    = &osnoise_data.sample_period,
1983         .min    = NULL,
1984 };
1985
1986 /*
1987  * osnoise/period_us: cannot be smaller than the runtime.
1988  */
1989 static struct trace_min_max_param osnoise_period = {
1990         .lock   = &interface_lock,
1991         .val    = &osnoise_data.sample_period,
1992         .max    = NULL,
1993         .min    = &osnoise_data.sample_runtime,
1994 };
1995
1996 /*
1997  * osnoise/stop_tracing_us: no limit.
1998  */
1999 static struct trace_min_max_param osnoise_stop_tracing_in = {
2000         .lock   = &interface_lock,
2001         .val    = &osnoise_data.stop_tracing,
2002         .max    = NULL,
2003         .min    = NULL,
2004 };
2005
2006 /*
2007  * osnoise/stop_tracing_total_us: no limit.
2008  */
2009 static struct trace_min_max_param osnoise_stop_tracing_total = {
2010         .lock   = &interface_lock,
2011         .val    = &osnoise_data.stop_tracing_total,
2012         .max    = NULL,
2013         .min    = NULL,
2014 };
2015
2016 #ifdef CONFIG_TIMERLAT_TRACER
2017 /*
2018  * osnoise/print_stack: print the stacktrace of the IRQ handler if the total
2019  * latency is higher than val.
2020  */
2021 static struct trace_min_max_param osnoise_print_stack = {
2022         .lock   = &interface_lock,
2023         .val    = &osnoise_data.print_stack,
2024         .max    = NULL,
2025         .min    = NULL,
2026 };
2027
2028 /*
2029  * osnoise/timerlat_period: min 100 us, max 1 s
2030  */
2031 u64 timerlat_min_period = 100;
2032 u64 timerlat_max_period = 1000000;
2033 static struct trace_min_max_param timerlat_period = {
2034         .lock   = &interface_lock,
2035         .val    = &osnoise_data.timerlat_period,
2036         .max    = &timerlat_max_period,
2037         .min    = &timerlat_min_period,
2038 };
2039 #endif
2040
2041 static const struct file_operations cpus_fops = {
2042         .open           = tracing_open_generic,
2043         .read           = osnoise_cpus_read,
2044         .write          = osnoise_cpus_write,
2045         .llseek         = generic_file_llseek,
2046 };
2047
2048 #ifdef CONFIG_TIMERLAT_TRACER
2049 #ifdef CONFIG_STACKTRACE
2050 static int init_timerlat_stack_tracefs(struct dentry *top_dir)
2051 {
2052         struct dentry *tmp;
2053
2054         tmp = tracefs_create_file("print_stack", TRACE_MODE_WRITE, top_dir,
2055                                   &osnoise_print_stack, &trace_min_max_fops);
2056         if (!tmp)
2057                 return -ENOMEM;
2058
2059         return 0;
2060 }
2061 #else /* CONFIG_STACKTRACE */
2062 static int init_timerlat_stack_tracefs(struct dentry *top_dir)
2063 {
2064         return 0;
2065 }
2066 #endif /* CONFIG_STACKTRACE */
2067
2068 /*
2069  * init_timerlat_tracefs - A function to initialize the timerlat interface files
2070  */
2071 static int init_timerlat_tracefs(struct dentry *top_dir)
2072 {
2073         struct dentry *tmp;
2074
2075         tmp = tracefs_create_file("timerlat_period_us", TRACE_MODE_WRITE, top_dir,
2076                                   &timerlat_period, &trace_min_max_fops);
2077         if (!tmp)
2078                 return -ENOMEM;
2079
2080         return init_timerlat_stack_tracefs(top_dir);
2081 }
2082 #else /* CONFIG_TIMERLAT_TRACER */
2083 static int init_timerlat_tracefs(struct dentry *top_dir)
2084 {
2085         return 0;
2086 }
2087 #endif /* CONFIG_TIMERLAT_TRACER */
2088
2089 /*
2090  * init_tracefs - A function to initialize the tracefs interface files
2091  *
2092  * This function creates entries in tracefs for "osnoise" and "timerlat".
2093  * It creates these directories in the tracing directory, and within that
2094  * directory the use can change and view the configs.
2095  */
2096 static int init_tracefs(void)
2097 {
2098         struct dentry *top_dir;
2099         struct dentry *tmp;
2100         int ret;
2101
2102         ret = tracing_init_dentry();
2103         if (ret)
2104                 return -ENOMEM;
2105
2106         top_dir = tracefs_create_dir("osnoise", NULL);
2107         if (!top_dir)
2108                 return 0;
2109
2110         tmp = tracefs_create_file("period_us", TRACE_MODE_WRITE, top_dir,
2111                                   &osnoise_period, &trace_min_max_fops);
2112         if (!tmp)
2113                 goto err;
2114
2115         tmp = tracefs_create_file("runtime_us", TRACE_MODE_WRITE, top_dir,
2116                                   &osnoise_runtime, &trace_min_max_fops);
2117         if (!tmp)
2118                 goto err;
2119
2120         tmp = tracefs_create_file("stop_tracing_us", TRACE_MODE_WRITE, top_dir,
2121                                   &osnoise_stop_tracing_in, &trace_min_max_fops);
2122         if (!tmp)
2123                 goto err;
2124
2125         tmp = tracefs_create_file("stop_tracing_total_us", TRACE_MODE_WRITE, top_dir,
2126                                   &osnoise_stop_tracing_total, &trace_min_max_fops);
2127         if (!tmp)
2128                 goto err;
2129
2130         tmp = trace_create_file("cpus", TRACE_MODE_WRITE, top_dir, NULL, &cpus_fops);
2131         if (!tmp)
2132                 goto err;
2133
2134         ret = init_timerlat_tracefs(top_dir);
2135         if (ret)
2136                 goto err;
2137
2138         return 0;
2139
2140 err:
2141         tracefs_remove(top_dir);
2142         return -ENOMEM;
2143 }
2144
2145 static int osnoise_hook_events(void)
2146 {
2147         int retval;
2148
2149         /*
2150          * Trace is already hooked, we are re-enabling from
2151          * a stop_tracing_*.
2152          */
2153         if (trace_osnoise_callback_enabled)
2154                 return 0;
2155
2156         retval = hook_irq_events();
2157         if (retval)
2158                 return -EINVAL;
2159
2160         retval = hook_softirq_events();
2161         if (retval)
2162                 goto out_unhook_irq;
2163
2164         retval = hook_thread_events();
2165         /*
2166          * All fine!
2167          */
2168         if (!retval)
2169                 return 0;
2170
2171         unhook_softirq_events();
2172 out_unhook_irq:
2173         unhook_irq_events();
2174         return -EINVAL;
2175 }
2176
2177 static void osnoise_unhook_events(void)
2178 {
2179         unhook_thread_events();
2180         unhook_softirq_events();
2181         unhook_irq_events();
2182 }
2183
2184 /*
2185  * osnoise_workload_start - start the workload and hook to events
2186  */
2187 static int osnoise_workload_start(void)
2188 {
2189         int retval;
2190
2191         /*
2192          * Instances need to be registered after calling workload
2193          * start. Hence, if there is already an instance, the
2194          * workload was already registered. Otherwise, this
2195          * code is on the way to register the first instance,
2196          * and the workload will start.
2197          */
2198         if (osnoise_has_registered_instances())
2199                 return 0;
2200
2201         osn_var_reset_all();
2202
2203         retval = osnoise_hook_events();
2204         if (retval)
2205                 return retval;
2206
2207         /*
2208          * Make sure that ftrace_nmi_enter/exit() see reset values
2209          * before enabling trace_osnoise_callback_enabled.
2210          */
2211         barrier();
2212         trace_osnoise_callback_enabled = true;
2213
2214         retval = start_per_cpu_kthreads();
2215         if (retval) {
2216                 trace_osnoise_callback_enabled = false;
2217                 /*
2218                  * Make sure that ftrace_nmi_enter/exit() see
2219                  * trace_osnoise_callback_enabled as false before continuing.
2220                  */
2221                 barrier();
2222
2223                 osnoise_unhook_events();
2224                 return retval;
2225         }
2226
2227         return 0;
2228 }
2229
2230 /*
2231  * osnoise_workload_stop - stop the workload and unhook the events
2232  */
2233 static void osnoise_workload_stop(void)
2234 {
2235         /*
2236          * Instances need to be unregistered before calling
2237          * stop. Hence, if there is a registered instance, more
2238          * than one instance is running, and the workload will not
2239          * yet stop. Otherwise, this code is on the way to disable
2240          * the last instance, and the workload can stop.
2241          */
2242         if (osnoise_has_registered_instances())
2243                 return;
2244
2245         /*
2246          * If callbacks were already disabled in a previous stop
2247          * call, there is no need to disable then again.
2248          *
2249          * For instance, this happens when tracing is stopped via:
2250          * echo 0 > tracing_on
2251          * echo nop > current_tracer.
2252          */
2253         if (!trace_osnoise_callback_enabled)
2254                 return;
2255
2256         trace_osnoise_callback_enabled = false;
2257         /*
2258          * Make sure that ftrace_nmi_enter/exit() see
2259          * trace_osnoise_callback_enabled as false before continuing.
2260          */
2261         barrier();
2262
2263         stop_per_cpu_kthreads();
2264
2265         osnoise_unhook_events();
2266 }
2267
2268 static void osnoise_tracer_start(struct trace_array *tr)
2269 {
2270         int retval;
2271
2272         /*
2273          * If the instance is already registered, there is no need to
2274          * register it again.
2275          */
2276         if (osnoise_instance_registered(tr))
2277                 return;
2278
2279         retval = osnoise_workload_start();
2280         if (retval)
2281                 pr_err(BANNER "Error starting osnoise tracer\n");
2282
2283         osnoise_register_instance(tr);
2284 }
2285
2286 static void osnoise_tracer_stop(struct trace_array *tr)
2287 {
2288         osnoise_unregister_instance(tr);
2289         osnoise_workload_stop();
2290 }
2291
2292 static int osnoise_tracer_init(struct trace_array *tr)
2293 {
2294         /*
2295          * Only allow osnoise tracer if timerlat tracer is not running
2296          * already.
2297          */
2298         if (timerlat_enabled())
2299                 return -EBUSY;
2300
2301         tr->max_latency = 0;
2302
2303         osnoise_tracer_start(tr);
2304         return 0;
2305 }
2306
2307 static void osnoise_tracer_reset(struct trace_array *tr)
2308 {
2309         osnoise_tracer_stop(tr);
2310 }
2311
2312 static struct tracer osnoise_tracer __read_mostly = {
2313         .name           = "osnoise",
2314         .init           = osnoise_tracer_init,
2315         .reset          = osnoise_tracer_reset,
2316         .start          = osnoise_tracer_start,
2317         .stop           = osnoise_tracer_stop,
2318         .print_header   = print_osnoise_headers,
2319         .allow_instances = true,
2320 };
2321
2322 #ifdef CONFIG_TIMERLAT_TRACER
2323 static void timerlat_tracer_start(struct trace_array *tr)
2324 {
2325         int retval;
2326
2327         /*
2328          * If the instance is already registered, there is no need to
2329          * register it again.
2330          */
2331         if (osnoise_instance_registered(tr))
2332                 return;
2333
2334         retval = osnoise_workload_start();
2335         if (retval)
2336                 pr_err(BANNER "Error starting timerlat tracer\n");
2337
2338         osnoise_register_instance(tr);
2339
2340         return;
2341 }
2342
2343 static void timerlat_tracer_stop(struct trace_array *tr)
2344 {
2345         int cpu;
2346
2347         osnoise_unregister_instance(tr);
2348
2349         /*
2350          * Instruct the threads to stop only if this is the last instance.
2351          */
2352         if (!osnoise_has_registered_instances()) {
2353                 for_each_online_cpu(cpu)
2354                         per_cpu(per_cpu_osnoise_var, cpu).sampling = 0;
2355         }
2356
2357         osnoise_workload_stop();
2358 }
2359
2360 static int timerlat_tracer_init(struct trace_array *tr)
2361 {
2362         /*
2363          * Only allow timerlat tracer if osnoise tracer is not running already.
2364          */
2365         if (osnoise_has_registered_instances() && !osnoise_data.timerlat_tracer)
2366                 return -EBUSY;
2367
2368         /*
2369          * If this is the first instance, set timerlat_tracer to block
2370          * osnoise tracer start.
2371          */
2372         if (!osnoise_has_registered_instances())
2373                 osnoise_data.timerlat_tracer = 1;
2374
2375         tr->max_latency = 0;
2376         timerlat_tracer_start(tr);
2377
2378         return 0;
2379 }
2380
2381 static void timerlat_tracer_reset(struct trace_array *tr)
2382 {
2383         timerlat_tracer_stop(tr);
2384
2385         /*
2386          * If this is the last instance, reset timerlat_tracer allowing
2387          * osnoise to be started.
2388          */
2389         if (!osnoise_has_registered_instances())
2390                 osnoise_data.timerlat_tracer = 0;
2391 }
2392
2393 static struct tracer timerlat_tracer __read_mostly = {
2394         .name           = "timerlat",
2395         .init           = timerlat_tracer_init,
2396         .reset          = timerlat_tracer_reset,
2397         .start          = timerlat_tracer_start,
2398         .stop           = timerlat_tracer_stop,
2399         .print_header   = print_timerlat_headers,
2400         .allow_instances = true,
2401 };
2402
2403 __init static int init_timerlat_tracer(void)
2404 {
2405         return register_tracer(&timerlat_tracer);
2406 }
2407 #else /* CONFIG_TIMERLAT_TRACER */
2408 __init static int init_timerlat_tracer(void)
2409 {
2410         return 0;
2411 }
2412 #endif /* CONFIG_TIMERLAT_TRACER */
2413
2414 __init static int init_osnoise_tracer(void)
2415 {
2416         int ret;
2417
2418         mutex_init(&interface_lock);
2419
2420         cpumask_copy(&osnoise_cpumask, cpu_all_mask);
2421
2422         ret = register_tracer(&osnoise_tracer);
2423         if (ret) {
2424                 pr_err(BANNER "Error registering osnoise!\n");
2425                 return ret;
2426         }
2427
2428         ret = init_timerlat_tracer();
2429         if (ret) {
2430                 pr_err(BANNER "Error registering timerlat!\n");
2431                 return ret;
2432         }
2433
2434         osnoise_init_hotplug_support();
2435
2436         INIT_LIST_HEAD_RCU(&osnoise_instances);
2437
2438         init_tracefs();
2439
2440         return 0;
2441 }
2442 late_initcall(init_osnoise_tracer);