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