blob: 44f46bc1140f42a882183f23c6d3573d476a31d6 [file] [log] [blame]
Steven Rostedt (VMware)bcea3f92018-08-16 11:23:53 -04001// SPDX-License-Identifier: GPL-2.0
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -04002/*
Srivatsa S. Bhat (VMware)0c3c86b2019-10-10 11:51:17 -07003 * trace_hwlat.c - A simple Hardware Latency detector.
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -04004 *
5 * Use this tracer to detect large system latencies induced by the behavior of
6 * certain underlying system hardware or firmware, independent of Linux itself.
7 * The code was developed originally to detect the presence of SMIs on Intel
8 * and AMD systems, although there is no dependency upon x86 herein.
9 *
10 * The classical example usage of this tracer is in detecting the presence of
11 * SMIs or System Management Interrupts on Intel and AMD systems. An SMI is a
12 * somewhat special form of hardware interrupt spawned from earlier CPU debug
13 * modes in which the (BIOS/EFI/etc.) firmware arranges for the South Bridge
14 * LPC (or other device) to generate a special interrupt under certain
15 * circumstances, for example, upon expiration of a special SMI timer device,
16 * due to certain external thermal readings, on certain I/O address accesses,
17 * and other situations. An SMI hits a special CPU pin, triggers a special
18 * SMI mode (complete with special memory map), and the OS is unaware.
19 *
20 * Although certain hardware-inducing latencies are necessary (for example,
21 * a modern system often requires an SMI handler for correct thermal control
22 * and remote management) they can wreak havoc upon any OS-level performance
23 * guarantees toward low-latency, especially when the OS is not even made
24 * aware of the presence of these interrupts. For this reason, we need a
25 * somewhat brute force mechanism to detect these interrupts. In this case,
26 * we do it by hogging all of the CPU(s) for configurable timer intervals,
27 * sampling the built-in CPU timer, looking for discontiguous readings.
28 *
29 * WARNING: This implementation necessarily introduces latencies. Therefore,
30 * you should NEVER use this tracer while running in a production
31 * environment requiring any kind of low-latency performance
32 * guarantee(s).
33 *
34 * Copyright (C) 2008-2009 Jon Masters, Red Hat, Inc. <jcm@redhat.com>
35 * Copyright (C) 2013-2016 Steven Rostedt, Red Hat, Inc. <srostedt@redhat.com>
36 *
Daniel Bristot de Oliveirabb1b24c2021-06-22 16:42:19 +020037 * Includes useful feedback from Clark Williams <williams@redhat.com>
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -040038 *
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -040039 */
40#include <linux/kthread.h>
41#include <linux/tracefs.h>
42#include <linux/uaccess.h>
Steven Rostedt (Red Hat)0330f7a2016-07-15 15:48:56 -040043#include <linux/cpumask.h>
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -040044#include <linux/delay.h>
Ingo Molnare6017572017-02-01 16:36:40 +010045#include <linux/sched/clock.h>
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -040046#include "trace.h"
47
48static struct trace_array *hwlat_trace;
49
50#define U64STR_SIZE 22 /* 20 digits max */
51
52#define BANNER "hwlat_detector: "
53#define DEFAULT_SAMPLE_WINDOW 1000000 /* 1s */
54#define DEFAULT_SAMPLE_WIDTH 500000 /* 0.5s */
55#define DEFAULT_LAT_THRESHOLD 10 /* 10us */
56
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -040057static struct dentry *hwlat_sample_width; /* sample width us */
58static struct dentry *hwlat_sample_window; /* sample window us */
Daniel Bristot de Oliveira8fa826b2021-06-22 16:42:20 +020059static struct dentry *hwlat_thread_mode; /* hwlat thread mode */
60
61enum {
62 MODE_NONE = 0,
63 MODE_ROUND_ROBIN,
Daniel Bristot de Oliveiraf46b1652021-06-22 16:42:22 +020064 MODE_PER_CPU,
Daniel Bristot de Oliveira8fa826b2021-06-22 16:42:20 +020065 MODE_MAX
66};
Daniel Bristot de Oliveiraf46b1652021-06-22 16:42:22 +020067static char *thread_mode_str[] = { "none", "round-robin", "per-cpu" };
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -040068
69/* Save the previous tracing_thresh value */
70static unsigned long save_tracing_thresh;
71
Daniel Bristot de Oliveiraf46b1652021-06-22 16:42:22 +020072/* runtime kthread data */
73struct hwlat_kthread_data {
74 struct task_struct *kthread;
75 /* NMI timestamp counters */
76 u64 nmi_ts_start;
77 u64 nmi_total_ts;
78 int nmi_count;
79 int nmi_cpu;
80};
81
82struct hwlat_kthread_data hwlat_single_cpu_data;
83DEFINE_PER_CPU(struct hwlat_kthread_data, hwlat_per_cpu_data);
Steven Rostedt (Red Hat)7b2c8622016-08-04 12:49:53 -040084
85/* Tells NMIs to call back to the hwlat tracer to record timestamps */
86bool trace_hwlat_callback_enabled;
87
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -040088/* If the user changed threshold, remember it */
89static u64 last_tracing_thresh = DEFAULT_LAT_THRESHOLD * NSEC_PER_USEC;
90
91/* Individual latency samples are stored here when detected. */
92struct hwlat_sample {
Deepa Dinamani51aad0a2017-05-08 15:59:13 -070093 u64 seqnum; /* unique sequence */
94 u64 duration; /* delta */
95 u64 outer_duration; /* delta (outer loop) */
96 u64 nmi_total_ts; /* Total time spent in NMIs */
97 struct timespec64 timestamp; /* wall time */
98 int nmi_count; /* # NMIs during this sample */
Ingo Molnarf2cc0202021-03-23 18:49:35 +010099 int count; /* # of iterations over thresh */
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400100};
101
102/* keep the global state somewhere. */
103static struct hwlat_data {
104
105 struct mutex lock; /* protect changes */
106
107 u64 count; /* total since reset */
108
109 u64 sample_window; /* total sampling window (on+off) */
110 u64 sample_width; /* active sampling portion of window */
111
Daniel Bristot de Oliveira8fa826b2021-06-22 16:42:20 +0200112 int thread_mode; /* thread mode */
113
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400114} hwlat_data = {
115 .sample_window = DEFAULT_SAMPLE_WINDOW,
116 .sample_width = DEFAULT_SAMPLE_WIDTH,
Daniel Bristot de Oliveira8fa826b2021-06-22 16:42:20 +0200117 .thread_mode = MODE_ROUND_ROBIN
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400118};
119
Daniel Bristot de Oliveiraf46b1652021-06-22 16:42:22 +0200120static struct hwlat_kthread_data *get_cpu_data(void)
121{
122 if (hwlat_data.thread_mode == MODE_PER_CPU)
123 return this_cpu_ptr(&hwlat_per_cpu_data);
124 else
125 return &hwlat_single_cpu_data;
126}
127
Daniel Bristot de Oliveira8fa826b2021-06-22 16:42:20 +0200128static bool hwlat_busy;
129
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400130static void trace_hwlat_sample(struct hwlat_sample *sample)
131{
132 struct trace_array *tr = hwlat_trace;
133 struct trace_event_call *call = &event_hwlat;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500134 struct trace_buffer *buffer = tr->array_buffer.buffer;
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400135 struct ring_buffer_event *event;
136 struct hwlat_entry *entry;
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400137
138 event = trace_buffer_lock_reserve(buffer, TRACE_HWLAT, sizeof(*entry),
Sebastian Andrzej Siewior36590c502021-01-25 20:45:08 +0100139 tracing_gen_ctx());
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400140 if (!event)
141 return;
142 entry = ring_buffer_event_data(event);
143 entry->seqnum = sample->seqnum;
144 entry->duration = sample->duration;
145 entry->outer_duration = sample->outer_duration;
146 entry->timestamp = sample->timestamp;
Steven Rostedt (Red Hat)7b2c8622016-08-04 12:49:53 -0400147 entry->nmi_total_ts = sample->nmi_total_ts;
148 entry->nmi_count = sample->nmi_count;
Steven Rostedt (VMware)b396bfd2020-02-12 12:21:03 -0500149 entry->count = sample->count;
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400150
151 if (!call_filter_check_discard(call, entry, buffer, event))
Steven Rostedt (Red Hat)52ffabe32016-11-23 20:28:38 -0500152 trace_buffer_unlock_commit_nostack(buffer, event);
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400153}
154
155/* Macros to encapsulate the time capturing infrastructure */
156#define time_type u64
157#define time_get() trace_clock_local()
158#define time_to_us(x) div_u64(x, 1000)
159#define time_sub(a, b) ((a) - (b))
160#define init_time(a, b) (a = b)
161#define time_u64(a) a
162
Steven Rostedt (Red Hat)7b2c8622016-08-04 12:49:53 -0400163void trace_hwlat_callback(bool enter)
164{
Daniel Bristot de Oliveiraf46b1652021-06-22 16:42:22 +0200165 struct hwlat_kthread_data *kdata = get_cpu_data();
166
167 if (!kdata->kthread)
Steven Rostedt (Red Hat)7b2c8622016-08-04 12:49:53 -0400168 return;
169
170 /*
171 * Currently trace_clock_local() calls sched_clock() and the
172 * generic version is not NMI safe.
173 */
174 if (!IS_ENABLED(CONFIG_GENERIC_SCHED_CLOCK)) {
175 if (enter)
Daniel Bristot de Oliveiraf46b1652021-06-22 16:42:22 +0200176 kdata->nmi_ts_start = time_get();
Steven Rostedt (Red Hat)7b2c8622016-08-04 12:49:53 -0400177 else
Daniel Bristot de Oliveiraf46b1652021-06-22 16:42:22 +0200178 kdata->nmi_total_ts += time_get() - kdata->nmi_ts_start;
Steven Rostedt (Red Hat)7b2c8622016-08-04 12:49:53 -0400179 }
180
181 if (enter)
Daniel Bristot de Oliveiraf46b1652021-06-22 16:42:22 +0200182 kdata->nmi_count++;
Steven Rostedt (Red Hat)7b2c8622016-08-04 12:49:53 -0400183}
184
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400185/**
186 * get_sample - sample the CPU TSC and look for likely hardware latencies
187 *
188 * Used to repeatedly capture the CPU TSC (or similar), looking for potential
189 * hardware-induced latency. Called with interrupts disabled and with
190 * hwlat_data.lock held.
191 */
192static int get_sample(void)
193{
Daniel Bristot de Oliveiraf46b1652021-06-22 16:42:22 +0200194 struct hwlat_kthread_data *kdata = get_cpu_data();
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400195 struct trace_array *tr = hwlat_trace;
Steven Rostedt (VMware)b396bfd2020-02-12 12:21:03 -0500196 struct hwlat_sample s;
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400197 time_type start, t1, t2, last_t2;
Steven Rostedt (VMware)b396bfd2020-02-12 12:21:03 -0500198 s64 diff, outer_diff, total, last_total = 0;
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400199 u64 sample = 0;
200 u64 thresh = tracing_thresh;
201 u64 outer_sample = 0;
202 int ret = -1;
Steven Rostedt (VMware)b396bfd2020-02-12 12:21:03 -0500203 unsigned int count = 0;
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400204
205 do_div(thresh, NSEC_PER_USEC); /* modifies interval value */
206
Daniel Bristot de Oliveiraf46b1652021-06-22 16:42:22 +0200207 kdata->nmi_total_ts = 0;
208 kdata->nmi_count = 0;
Steven Rostedt (Red Hat)7b2c8622016-08-04 12:49:53 -0400209 /* Make sure NMIs see this first */
210 barrier();
211
212 trace_hwlat_callback_enabled = true;
213
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400214 init_time(last_t2, 0);
215 start = time_get(); /* start timestamp */
Steven Rostedt (VMware)b396bfd2020-02-12 12:21:03 -0500216 outer_diff = 0;
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400217
218 do {
219
220 t1 = time_get(); /* we'll look for a discontinuity */
221 t2 = time_get();
222
223 if (time_u64(last_t2)) {
224 /* Check the delta from outer loop (t2 to next t1) */
Steven Rostedt (VMware)b396bfd2020-02-12 12:21:03 -0500225 outer_diff = time_to_us(time_sub(t1, last_t2));
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400226 /* This shouldn't happen */
Steven Rostedt (VMware)b396bfd2020-02-12 12:21:03 -0500227 if (outer_diff < 0) {
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400228 pr_err(BANNER "time running backwards\n");
229 goto out;
230 }
Steven Rostedt (VMware)b396bfd2020-02-12 12:21:03 -0500231 if (outer_diff > outer_sample)
232 outer_sample = outer_diff;
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400233 }
234 last_t2 = t2;
235
236 total = time_to_us(time_sub(t2, start)); /* sample width */
237
238 /* Check for possible overflows */
239 if (total < last_total) {
240 pr_err("Time total overflowed\n");
241 break;
242 }
243 last_total = total;
244
245 /* This checks the inner loop (t1 to t2) */
246 diff = time_to_us(time_sub(t2, t1)); /* current diff */
247
Steven Rostedt (VMware)b396bfd2020-02-12 12:21:03 -0500248 if (diff > thresh || outer_diff > thresh) {
249 if (!count)
250 ktime_get_real_ts64(&s.timestamp);
251 count++;
252 }
253
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400254 /* This shouldn't happen */
255 if (diff < 0) {
256 pr_err(BANNER "time running backwards\n");
257 goto out;
258 }
259
260 if (diff > sample)
261 sample = diff; /* only want highest value */
262
263 } while (total <= hwlat_data.sample_width);
264
Steven Rostedt (Red Hat)7b2c8622016-08-04 12:49:53 -0400265 barrier(); /* finish the above in the view for NMIs */
266 trace_hwlat_callback_enabled = false;
267 barrier(); /* Make sure nmi_total_ts is no longer updated */
268
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400269 ret = 0;
270
271 /* If we exceed the threshold value, we have found a hardware latency */
272 if (sample > thresh || outer_sample > thresh) {
Viktor Rosendahl (BMW)91edde22019-10-09 00:08:21 +0200273 u64 latency;
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400274
275 ret = 1;
276
Steven Rostedt (Red Hat)7b2c8622016-08-04 12:49:53 -0400277 /* We read in microseconds */
Daniel Bristot de Oliveiraf46b1652021-06-22 16:42:22 +0200278 if (kdata->nmi_total_ts)
279 do_div(kdata->nmi_total_ts, NSEC_PER_USEC);
Steven Rostedt (Red Hat)7b2c8622016-08-04 12:49:53 -0400280
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400281 hwlat_data.count++;
282 s.seqnum = hwlat_data.count;
283 s.duration = sample;
284 s.outer_duration = outer_sample;
Daniel Bristot de Oliveiraf46b1652021-06-22 16:42:22 +0200285 s.nmi_total_ts = kdata->nmi_total_ts;
286 s.nmi_count = kdata->nmi_count;
Steven Rostedt (VMware)b396bfd2020-02-12 12:21:03 -0500287 s.count = count;
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400288 trace_hwlat_sample(&s);
289
Viktor Rosendahl (BMW)91edde22019-10-09 00:08:21 +0200290 latency = max(sample, outer_sample);
291
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400292 /* Keep a running maximum ever recorded hardware latency */
Viktor Rosendahl (BMW)91edde22019-10-09 00:08:21 +0200293 if (latency > tr->max_latency) {
294 tr->max_latency = latency;
295 latency_fsnotify(tr);
296 }
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400297 }
298
299out:
300 return ret;
301}
302
Steven Rostedt (Red Hat)0330f7a2016-07-15 15:48:56 -0400303static struct cpumask save_cpumask;
Steven Rostedt (Red Hat)0330f7a2016-07-15 15:48:56 -0400304
Steven Rostedt (VMware)f447c192017-01-31 16:48:23 -0500305static void move_to_next_cpu(void)
Steven Rostedt (Red Hat)0330f7a2016-07-15 15:48:56 -0400306{
Steven Rostedt (VMware)f447c192017-01-31 16:48:23 -0500307 struct cpumask *current_mask = &save_cpumask;
Kevin Hao96b48332020-07-30 16:23:18 +0800308 struct trace_array *tr = hwlat_trace;
Steven Rostedt (Red Hat)0330f7a2016-07-15 15:48:56 -0400309 int next_cpu;
310
Steven Rostedt (Red Hat)0330f7a2016-07-15 15:48:56 -0400311 /*
312 * If for some reason the user modifies the CPU affinity
Srivatsa S. Bhat (VMware)0c3c86b2019-10-10 11:51:17 -0700313 * of this thread, then stop migrating for the duration
Steven Rostedt (Red Hat)0330f7a2016-07-15 15:48:56 -0400314 * of the current test.
315 */
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +0200316 if (!cpumask_equal(current_mask, current->cpus_ptr))
Daniel Bristot de Oliveira7bb7d802021-06-22 16:42:21 +0200317 goto change_mode;
Steven Rostedt (Red Hat)0330f7a2016-07-15 15:48:56 -0400318
319 get_online_cpus();
Kevin Hao96b48332020-07-30 16:23:18 +0800320 cpumask_and(current_mask, cpu_online_mask, tr->tracing_cpumask);
Steven Rostedt (Red Hat)0330f7a2016-07-15 15:48:56 -0400321 next_cpu = cpumask_next(smp_processor_id(), current_mask);
322 put_online_cpus();
323
324 if (next_cpu >= nr_cpu_ids)
325 next_cpu = cpumask_first(current_mask);
326
Steven Rostedt (Red Hat)0330f7a2016-07-15 15:48:56 -0400327 if (next_cpu >= nr_cpu_ids) /* Shouldn't happen! */
Daniel Bristot de Oliveira7bb7d802021-06-22 16:42:21 +0200328 goto change_mode;
Steven Rostedt (Red Hat)0330f7a2016-07-15 15:48:56 -0400329
330 cpumask_clear(current_mask);
331 cpumask_set_cpu(next_cpu, current_mask);
332
333 sched_setaffinity(0, current_mask);
334 return;
335
Daniel Bristot de Oliveira7bb7d802021-06-22 16:42:21 +0200336 change_mode:
337 hwlat_data.thread_mode = MODE_NONE;
338 pr_info(BANNER "cpumask changed while in round-robin mode, switching to mode none\n");
Steven Rostedt (Red Hat)0330f7a2016-07-15 15:48:56 -0400339}
340
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400341/*
342 * kthread_fn - The CPU time sampling/hardware latency detection kernel thread
343 *
344 * Used to periodically sample the CPU TSC via a call to get_sample. We
345 * disable interrupts, which does (intentionally) introduce latency since we
346 * need to ensure nothing else might be running (and thus preempting).
347 * Obviously this should never be used in production environments.
348 *
Luiz Capitulino8e0f1142017-02-13 12:25:17 -0500349 * Executes one loop interaction on each CPU in tracing_cpumask sysfs file.
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400350 */
351static int kthread_fn(void *data)
352{
353 u64 interval;
354
355 while (!kthread_should_stop()) {
356
Daniel Bristot de Oliveira8fa826b2021-06-22 16:42:20 +0200357 if (hwlat_data.thread_mode == MODE_ROUND_ROBIN)
358 move_to_next_cpu();
Steven Rostedt (Red Hat)0330f7a2016-07-15 15:48:56 -0400359
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400360 local_irq_disable();
361 get_sample();
362 local_irq_enable();
363
364 mutex_lock(&hwlat_data.lock);
365 interval = hwlat_data.sample_window - hwlat_data.sample_width;
366 mutex_unlock(&hwlat_data.lock);
367
368 do_div(interval, USEC_PER_MSEC); /* modifies interval value */
369
370 /* Always sleep for at least 1ms */
371 if (interval < 1)
372 interval = 1;
373
374 if (msleep_interruptible(interval))
375 break;
376 }
377
378 return 0;
379}
380
Daniel Bristot de Oliveira8fa826b2021-06-22 16:42:20 +0200381/*
Daniel Bristot de Oliveiraf46b1652021-06-22 16:42:22 +0200382 * stop_stop_kthread - Inform the hardware latency sampling/detector kthread to stop
383 *
384 * This kicks the running hardware latency sampling/detector kernel thread and
385 * tells it to stop sampling now. Use this on unload and at system shutdown.
386 */
387static void stop_single_kthread(void)
388{
389 struct hwlat_kthread_data *kdata = get_cpu_data();
390 struct task_struct *kthread = kdata->kthread;
391
392 if (!kthread)
393 return;
394
395 kthread_stop(kthread);
396 kdata->kthread = NULL;
397}
398
399
400/*
401 * start_single_kthread - Kick off the hardware latency sampling/detector kthread
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400402 *
403 * This starts the kernel thread that will sit and sample the CPU timestamp
404 * counter (TSC or similar) and look for potential hardware latencies.
405 */
Daniel Bristot de Oliveiraf46b1652021-06-22 16:42:22 +0200406static int start_single_kthread(struct trace_array *tr)
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400407{
Daniel Bristot de Oliveiraf46b1652021-06-22 16:42:22 +0200408 struct hwlat_kthread_data *kdata = get_cpu_data();
Steven Rostedt (VMware)f447c192017-01-31 16:48:23 -0500409 struct cpumask *current_mask = &save_cpumask;
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400410 struct task_struct *kthread;
Steven Rostedt (VMware)f447c192017-01-31 16:48:23 -0500411 int next_cpu;
412
Daniel Bristot de Oliveiraf46b1652021-06-22 16:42:22 +0200413 if (kdata->kthread)
Erica Bugden82fbc8c2018-08-01 12:45:54 +0200414 return 0;
415
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400416 kthread = kthread_create(kthread_fn, NULL, "hwlatd");
417 if (IS_ERR(kthread)) {
418 pr_err(BANNER "could not start sampling thread\n");
419 return -ENOMEM;
420 }
Steven Rostedt (VMware)f447c192017-01-31 16:48:23 -0500421
Daniel Bristot de Oliveira8fa826b2021-06-22 16:42:20 +0200422
423 /* Just pick the first CPU on first iteration */
424 get_online_cpus();
425 cpumask_and(current_mask, cpu_online_mask, tr->tracing_cpumask);
426 put_online_cpus();
427
428 if (hwlat_data.thread_mode == MODE_ROUND_ROBIN) {
429 next_cpu = cpumask_first(current_mask);
430 cpumask_clear(current_mask);
431 cpumask_set_cpu(next_cpu, current_mask);
432
433 }
434
Steven Rostedt (VMware)f447c192017-01-31 16:48:23 -0500435 sched_setaffinity(kthread->pid, current_mask);
436
Daniel Bristot de Oliveiraf46b1652021-06-22 16:42:22 +0200437 kdata->kthread = kthread;
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400438 wake_up_process(kthread);
439
440 return 0;
441}
442
Daniel Bristot de Oliveira8fa826b2021-06-22 16:42:20 +0200443/*
Daniel Bristot de Oliveiraf46b1652021-06-22 16:42:22 +0200444 * stop_cpu_kthread - Stop a hwlat cpu kthread
445 */
446static void stop_cpu_kthread(unsigned int cpu)
447{
448 struct task_struct *kthread;
449
450 kthread = per_cpu(hwlat_per_cpu_data, cpu).kthread;
451 if (kthread)
452 kthread_stop(kthread);
453}
454
455/*
456 * stop_per_cpu_kthreads - Inform the hardware latency sampling/detector kthread to stop
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400457 *
Daniel Bristot de Oliveiraf46b1652021-06-22 16:42:22 +0200458 * This kicks the running hardware latency sampling/detector kernel threads and
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400459 * tells it to stop sampling now. Use this on unload and at system shutdown.
460 */
Daniel Bristot de Oliveiraf46b1652021-06-22 16:42:22 +0200461static void stop_per_cpu_kthreads(void)
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400462{
Daniel Bristot de Oliveiraf46b1652021-06-22 16:42:22 +0200463 unsigned int cpu;
464
465 get_online_cpus();
466 for_each_online_cpu(cpu)
467 stop_cpu_kthread(cpu);
468 put_online_cpus();
469}
470
471/*
472 * start_cpu_kthread - Start a hwlat cpu kthread
473 */
474static int start_cpu_kthread(unsigned int cpu)
475{
476 struct task_struct *kthread;
477 char comm[24];
478
479 snprintf(comm, 24, "hwlatd/%d", cpu);
480
481 kthread = kthread_create_on_cpu(kthread_fn, NULL, cpu, comm);
482 if (IS_ERR(kthread)) {
483 pr_err(BANNER "could not start sampling thread\n");
484 return -ENOMEM;
485 }
486
487 per_cpu(hwlat_per_cpu_data, cpu).kthread = kthread;
488 wake_up_process(kthread);
489
490 return 0;
491}
492
493/*
494 * start_per_cpu_kthreads - Kick off the hardware latency sampling/detector kthreads
495 *
496 * This starts the kernel threads that will sit on potentially all cpus and
497 * sample the CPU timestamp counter (TSC or similar) and look for potential
498 * hardware latencies.
499 */
500static int start_per_cpu_kthreads(struct trace_array *tr)
501{
502 struct cpumask *current_mask = &save_cpumask;
503 unsigned int cpu;
504 int retval;
505
506 get_online_cpus();
507 /*
508 * Run only on CPUs in which hwlat is allowed to run.
509 */
510 cpumask_and(current_mask, cpu_online_mask, tr->tracing_cpumask);
511
512 for_each_online_cpu(cpu)
513 per_cpu(hwlat_per_cpu_data, cpu).kthread = NULL;
514
515 for_each_cpu(cpu, current_mask) {
516 retval = start_cpu_kthread(cpu);
517 if (retval)
518 goto out_error;
519 }
520 put_online_cpus();
521
522 return 0;
523
524out_error:
525 put_online_cpus();
526 stop_per_cpu_kthreads();
527 return retval;
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400528}
529
Daniel Bristot de Oliveira8fa826b2021-06-22 16:42:20 +0200530static void *s_mode_start(struct seq_file *s, loff_t *pos)
531{
532 int mode = *pos;
533
534 mutex_lock(&hwlat_data.lock);
535
536 if (mode >= MODE_MAX)
537 return NULL;
538
539 return pos;
540}
541
542static void *s_mode_next(struct seq_file *s, void *v, loff_t *pos)
543{
544 int mode = ++(*pos);
545
546 if (mode >= MODE_MAX)
547 return NULL;
548
549 return pos;
550}
551
552static int s_mode_show(struct seq_file *s, void *v)
553{
554 loff_t *pos = v;
555 int mode = *pos;
556
557 if (mode == hwlat_data.thread_mode)
558 seq_printf(s, "[%s]", thread_mode_str[mode]);
559 else
560 seq_printf(s, "%s", thread_mode_str[mode]);
561
562 if (mode != MODE_MAX)
563 seq_puts(s, " ");
564
565 return 0;
566}
567
568static void s_mode_stop(struct seq_file *s, void *v)
569{
570 seq_puts(s, "\n");
571 mutex_unlock(&hwlat_data.lock);
572}
573
574static const struct seq_operations thread_mode_seq_ops = {
575 .start = s_mode_start,
576 .next = s_mode_next,
577 .show = s_mode_show,
578 .stop = s_mode_stop
579};
580
581static int hwlat_mode_open(struct inode *inode, struct file *file)
582{
583 return seq_open(file, &thread_mode_seq_ops);
584};
585
586static void hwlat_tracer_start(struct trace_array *tr);
587static void hwlat_tracer_stop(struct trace_array *tr);
588
589/**
590 * hwlat_mode_write - Write function for "mode" entry
591 * @filp: The active open file structure
592 * @ubuf: The user buffer that contains the value to write
593 * @cnt: The maximum number of bytes to write to "file"
594 * @ppos: The current position in @file
595 *
596 * This function provides a write implementation for the "mode" interface
597 * to the hardware latency detector. hwlatd has different operation modes.
598 * The "none" sets the allowed cpumask for a single hwlatd thread at the
599 * startup and lets the scheduler handle the migration. The default mode is
600 * the "round-robin" one, in which a single hwlatd thread runs, migrating
Daniel Bristot de Oliveiraf46b1652021-06-22 16:42:22 +0200601 * among the allowed CPUs in a round-robin fashion. The "per-cpu" mode
602 * creates one hwlatd thread per allowed CPU.
Daniel Bristot de Oliveira8fa826b2021-06-22 16:42:20 +0200603 */
604static ssize_t hwlat_mode_write(struct file *filp, const char __user *ubuf,
605 size_t cnt, loff_t *ppos)
606{
607 struct trace_array *tr = hwlat_trace;
608 const char *mode;
609 char buf[64];
610 int ret, i;
611
612 if (cnt >= sizeof(buf))
613 return -EINVAL;
614
615 if (copy_from_user(buf, ubuf, cnt))
616 return -EFAULT;
617
618 buf[cnt] = 0;
619
620 mode = strstrip(buf);
621
622 ret = -EINVAL;
623
624 /*
625 * trace_types_lock is taken to avoid concurrency on start/stop
626 * and hwlat_busy.
627 */
628 mutex_lock(&trace_types_lock);
629 if (hwlat_busy)
630 hwlat_tracer_stop(tr);
631
632 mutex_lock(&hwlat_data.lock);
633
634 for (i = 0; i < MODE_MAX; i++) {
635 if (strcmp(mode, thread_mode_str[i]) == 0) {
636 hwlat_data.thread_mode = i;
637 ret = cnt;
638 }
639 }
640
641 mutex_unlock(&hwlat_data.lock);
642
643 if (hwlat_busy)
644 hwlat_tracer_start(tr);
645 mutex_unlock(&trace_types_lock);
646
647 *ppos += cnt;
648
649
650
651 return ret;
652}
653
Daniel Bristot de Oliveiraf27a1c92021-06-22 16:42:24 +0200654/*
655 * The width parameter is read/write using the generic trace_min_max_param
656 * method. The *val is protected by the hwlat_data lock and is upper
657 * bounded by the window parameter.
658 */
659static struct trace_min_max_param hwlat_width = {
660 .lock = &hwlat_data.lock,
661 .val = &hwlat_data.sample_width,
662 .max = &hwlat_data.sample_window,
663 .min = NULL,
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400664};
665
Daniel Bristot de Oliveiraf27a1c92021-06-22 16:42:24 +0200666/*
667 * The window parameter is read/write using the generic trace_min_max_param
668 * method. The *val is protected by the hwlat_data lock and is lower
669 * bounded by the width parameter.
670 */
671static struct trace_min_max_param hwlat_window = {
672 .lock = &hwlat_data.lock,
673 .val = &hwlat_data.sample_window,
674 .max = NULL,
675 .min = &hwlat_data.sample_width,
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400676};
677
Daniel Bristot de Oliveira8fa826b2021-06-22 16:42:20 +0200678static const struct file_operations thread_mode_fops = {
679 .open = hwlat_mode_open,
680 .read = seq_read,
681 .llseek = seq_lseek,
682 .release = seq_release,
683 .write = hwlat_mode_write
684};
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400685/**
686 * init_tracefs - A function to initialize the tracefs interface files
687 *
688 * This function creates entries in tracefs for "hwlat_detector".
689 * It creates the hwlat_detector directory in the tracing directory,
690 * and within that directory is the count, width and window files to
691 * change and view those values.
692 */
693static int init_tracefs(void)
694{
Wei Yang22c36b12020-07-12 09:10:36 +0800695 int ret;
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400696 struct dentry *top_dir;
697
Wei Yang22c36b12020-07-12 09:10:36 +0800698 ret = tracing_init_dentry();
699 if (ret)
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400700 return -ENOMEM;
701
Wei Yang22c36b12020-07-12 09:10:36 +0800702 top_dir = tracefs_create_dir("hwlat_detector", NULL);
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400703 if (!top_dir)
704 return -ENOMEM;
705
706 hwlat_sample_window = tracefs_create_file("window", 0640,
707 top_dir,
Daniel Bristot de Oliveiraf27a1c92021-06-22 16:42:24 +0200708 &hwlat_window,
709 &trace_min_max_fops);
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400710 if (!hwlat_sample_window)
711 goto err;
712
713 hwlat_sample_width = tracefs_create_file("width", 0644,
714 top_dir,
Daniel Bristot de Oliveiraf27a1c92021-06-22 16:42:24 +0200715 &hwlat_width,
716 &trace_min_max_fops);
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400717 if (!hwlat_sample_width)
718 goto err;
719
Daniel Bristot de Oliveira8fa826b2021-06-22 16:42:20 +0200720 hwlat_thread_mode = trace_create_file("mode", 0644,
721 top_dir,
722 NULL,
723 &thread_mode_fops);
724 if (!hwlat_thread_mode)
725 goto err;
726
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400727 return 0;
728
729 err:
Al Viroa3d1e7e2019-11-18 09:43:10 -0500730 tracefs_remove(top_dir);
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400731 return -ENOMEM;
732}
733
734static void hwlat_tracer_start(struct trace_array *tr)
735{
736 int err;
737
Daniel Bristot de Oliveiraf46b1652021-06-22 16:42:22 +0200738 if (hwlat_data.thread_mode == MODE_PER_CPU)
739 err = start_per_cpu_kthreads(tr);
740 else
741 err = start_single_kthread(tr);
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400742 if (err)
743 pr_err(BANNER "Cannot start hwlat kthread\n");
744}
745
746static void hwlat_tracer_stop(struct trace_array *tr)
747{
Daniel Bristot de Oliveiraf46b1652021-06-22 16:42:22 +0200748 if (hwlat_data.thread_mode == MODE_PER_CPU)
749 stop_per_cpu_kthreads();
750 else
751 stop_single_kthread();
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400752}
753
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400754static int hwlat_tracer_init(struct trace_array *tr)
755{
756 /* Only allow one instance to enable this */
757 if (hwlat_busy)
758 return -EBUSY;
759
760 hwlat_trace = tr;
761
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400762 hwlat_data.count = 0;
763 tr->max_latency = 0;
764 save_tracing_thresh = tracing_thresh;
765
766 /* tracing_thresh is in nsecs, we speak in usecs */
767 if (!tracing_thresh)
768 tracing_thresh = last_tracing_thresh;
769
770 if (tracer_tracing_is_on(tr))
771 hwlat_tracer_start(tr);
772
773 hwlat_busy = true;
774
775 return 0;
776}
777
778static void hwlat_tracer_reset(struct trace_array *tr)
779{
Daniel Bristot de Oliveiraf46b1652021-06-22 16:42:22 +0200780 hwlat_tracer_stop(tr);
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400781
782 /* the tracing threshold is static between runs */
783 last_tracing_thresh = tracing_thresh;
784
785 tracing_thresh = save_tracing_thresh;
786 hwlat_busy = false;
787}
788
789static struct tracer hwlat_tracer __read_mostly =
790{
791 .name = "hwlat",
792 .init = hwlat_tracer_init,
793 .reset = hwlat_tracer_reset,
794 .start = hwlat_tracer_start,
795 .stop = hwlat_tracer_stop,
796 .allow_instances = true,
797};
798
799__init static int init_hwlat_tracer(void)
800{
801 int ret;
802
803 mutex_init(&hwlat_data.lock);
804
805 ret = register_tracer(&hwlat_tracer);
806 if (ret)
807 return ret;
808
809 init_tracefs();
810
811 return 0;
812}
813late_initcall(init_hwlat_tracer);