blob: 43a436d85a01787d91981b805b94c0d44176fa45 [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
57/* sampling thread*/
58static struct task_struct *hwlat_kthread;
59
60static struct dentry *hwlat_sample_width; /* sample width us */
61static struct dentry *hwlat_sample_window; /* sample window us */
Daniel Bristot de Oliveira8fa826b2021-06-22 16:42:20 +020062static struct dentry *hwlat_thread_mode; /* hwlat thread mode */
63
64enum {
65 MODE_NONE = 0,
66 MODE_ROUND_ROBIN,
67 MODE_MAX
68};
69static char *thread_mode_str[] = { "none", "round-robin" };
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -040070
71/* Save the previous tracing_thresh value */
72static unsigned long save_tracing_thresh;
73
Steven Rostedt (Red Hat)7b2c8622016-08-04 12:49:53 -040074/* NMI timestamp counters */
75static u64 nmi_ts_start;
76static u64 nmi_total_ts;
77static int nmi_count;
78static int nmi_cpu;
79
80/* Tells NMIs to call back to the hwlat tracer to record timestamps */
81bool trace_hwlat_callback_enabled;
82
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -040083/* If the user changed threshold, remember it */
84static u64 last_tracing_thresh = DEFAULT_LAT_THRESHOLD * NSEC_PER_USEC;
85
86/* Individual latency samples are stored here when detected. */
87struct hwlat_sample {
Deepa Dinamani51aad0a2017-05-08 15:59:13 -070088 u64 seqnum; /* unique sequence */
89 u64 duration; /* delta */
90 u64 outer_duration; /* delta (outer loop) */
91 u64 nmi_total_ts; /* Total time spent in NMIs */
92 struct timespec64 timestamp; /* wall time */
93 int nmi_count; /* # NMIs during this sample */
Ingo Molnarf2cc0202021-03-23 18:49:35 +010094 int count; /* # of iterations over thresh */
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -040095};
96
97/* keep the global state somewhere. */
98static struct hwlat_data {
99
100 struct mutex lock; /* protect changes */
101
102 u64 count; /* total since reset */
103
104 u64 sample_window; /* total sampling window (on+off) */
105 u64 sample_width; /* active sampling portion of window */
106
Daniel Bristot de Oliveira8fa826b2021-06-22 16:42:20 +0200107 int thread_mode; /* thread mode */
108
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400109} hwlat_data = {
110 .sample_window = DEFAULT_SAMPLE_WINDOW,
111 .sample_width = DEFAULT_SAMPLE_WIDTH,
Daniel Bristot de Oliveira8fa826b2021-06-22 16:42:20 +0200112 .thread_mode = MODE_ROUND_ROBIN
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400113};
114
Daniel Bristot de Oliveira8fa826b2021-06-22 16:42:20 +0200115static bool hwlat_busy;
116
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400117static void trace_hwlat_sample(struct hwlat_sample *sample)
118{
119 struct trace_array *tr = hwlat_trace;
120 struct trace_event_call *call = &event_hwlat;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500121 struct trace_buffer *buffer = tr->array_buffer.buffer;
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400122 struct ring_buffer_event *event;
123 struct hwlat_entry *entry;
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400124
125 event = trace_buffer_lock_reserve(buffer, TRACE_HWLAT, sizeof(*entry),
Sebastian Andrzej Siewior36590c502021-01-25 20:45:08 +0100126 tracing_gen_ctx());
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400127 if (!event)
128 return;
129 entry = ring_buffer_event_data(event);
130 entry->seqnum = sample->seqnum;
131 entry->duration = sample->duration;
132 entry->outer_duration = sample->outer_duration;
133 entry->timestamp = sample->timestamp;
Steven Rostedt (Red Hat)7b2c8622016-08-04 12:49:53 -0400134 entry->nmi_total_ts = sample->nmi_total_ts;
135 entry->nmi_count = sample->nmi_count;
Steven Rostedt (VMware)b396bfd2020-02-12 12:21:03 -0500136 entry->count = sample->count;
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400137
138 if (!call_filter_check_discard(call, entry, buffer, event))
Steven Rostedt (Red Hat)52ffabe32016-11-23 20:28:38 -0500139 trace_buffer_unlock_commit_nostack(buffer, event);
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400140}
141
142/* Macros to encapsulate the time capturing infrastructure */
143#define time_type u64
144#define time_get() trace_clock_local()
145#define time_to_us(x) div_u64(x, 1000)
146#define time_sub(a, b) ((a) - (b))
147#define init_time(a, b) (a = b)
148#define time_u64(a) a
149
Steven Rostedt (Red Hat)7b2c8622016-08-04 12:49:53 -0400150void trace_hwlat_callback(bool enter)
151{
152 if (smp_processor_id() != nmi_cpu)
153 return;
154
155 /*
156 * Currently trace_clock_local() calls sched_clock() and the
157 * generic version is not NMI safe.
158 */
159 if (!IS_ENABLED(CONFIG_GENERIC_SCHED_CLOCK)) {
160 if (enter)
161 nmi_ts_start = time_get();
162 else
Srivatsa S. Bhat (VMware)98dc19c2019-10-10 11:50:46 -0700163 nmi_total_ts += time_get() - nmi_ts_start;
Steven Rostedt (Red Hat)7b2c8622016-08-04 12:49:53 -0400164 }
165
166 if (enter)
167 nmi_count++;
168}
169
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400170/**
171 * get_sample - sample the CPU TSC and look for likely hardware latencies
172 *
173 * Used to repeatedly capture the CPU TSC (or similar), looking for potential
174 * hardware-induced latency. Called with interrupts disabled and with
175 * hwlat_data.lock held.
176 */
177static int get_sample(void)
178{
179 struct trace_array *tr = hwlat_trace;
Steven Rostedt (VMware)b396bfd2020-02-12 12:21:03 -0500180 struct hwlat_sample s;
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400181 time_type start, t1, t2, last_t2;
Steven Rostedt (VMware)b396bfd2020-02-12 12:21:03 -0500182 s64 diff, outer_diff, total, last_total = 0;
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400183 u64 sample = 0;
184 u64 thresh = tracing_thresh;
185 u64 outer_sample = 0;
186 int ret = -1;
Steven Rostedt (VMware)b396bfd2020-02-12 12:21:03 -0500187 unsigned int count = 0;
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400188
189 do_div(thresh, NSEC_PER_USEC); /* modifies interval value */
190
Steven Rostedt (Red Hat)7b2c8622016-08-04 12:49:53 -0400191 nmi_cpu = smp_processor_id();
192 nmi_total_ts = 0;
193 nmi_count = 0;
194 /* Make sure NMIs see this first */
195 barrier();
196
197 trace_hwlat_callback_enabled = true;
198
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400199 init_time(last_t2, 0);
200 start = time_get(); /* start timestamp */
Steven Rostedt (VMware)b396bfd2020-02-12 12:21:03 -0500201 outer_diff = 0;
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400202
203 do {
204
205 t1 = time_get(); /* we'll look for a discontinuity */
206 t2 = time_get();
207
208 if (time_u64(last_t2)) {
209 /* Check the delta from outer loop (t2 to next t1) */
Steven Rostedt (VMware)b396bfd2020-02-12 12:21:03 -0500210 outer_diff = time_to_us(time_sub(t1, last_t2));
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400211 /* This shouldn't happen */
Steven Rostedt (VMware)b396bfd2020-02-12 12:21:03 -0500212 if (outer_diff < 0) {
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400213 pr_err(BANNER "time running backwards\n");
214 goto out;
215 }
Steven Rostedt (VMware)b396bfd2020-02-12 12:21:03 -0500216 if (outer_diff > outer_sample)
217 outer_sample = outer_diff;
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400218 }
219 last_t2 = t2;
220
221 total = time_to_us(time_sub(t2, start)); /* sample width */
222
223 /* Check for possible overflows */
224 if (total < last_total) {
225 pr_err("Time total overflowed\n");
226 break;
227 }
228 last_total = total;
229
230 /* This checks the inner loop (t1 to t2) */
231 diff = time_to_us(time_sub(t2, t1)); /* current diff */
232
Steven Rostedt (VMware)b396bfd2020-02-12 12:21:03 -0500233 if (diff > thresh || outer_diff > thresh) {
234 if (!count)
235 ktime_get_real_ts64(&s.timestamp);
236 count++;
237 }
238
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400239 /* This shouldn't happen */
240 if (diff < 0) {
241 pr_err(BANNER "time running backwards\n");
242 goto out;
243 }
244
245 if (diff > sample)
246 sample = diff; /* only want highest value */
247
248 } while (total <= hwlat_data.sample_width);
249
Steven Rostedt (Red Hat)7b2c8622016-08-04 12:49:53 -0400250 barrier(); /* finish the above in the view for NMIs */
251 trace_hwlat_callback_enabled = false;
252 barrier(); /* Make sure nmi_total_ts is no longer updated */
253
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400254 ret = 0;
255
256 /* If we exceed the threshold value, we have found a hardware latency */
257 if (sample > thresh || outer_sample > thresh) {
Viktor Rosendahl (BMW)91edde22019-10-09 00:08:21 +0200258 u64 latency;
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400259
260 ret = 1;
261
Steven Rostedt (Red Hat)7b2c8622016-08-04 12:49:53 -0400262 /* We read in microseconds */
263 if (nmi_total_ts)
264 do_div(nmi_total_ts, NSEC_PER_USEC);
265
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400266 hwlat_data.count++;
267 s.seqnum = hwlat_data.count;
268 s.duration = sample;
269 s.outer_duration = outer_sample;
Steven Rostedt (Red Hat)7b2c8622016-08-04 12:49:53 -0400270 s.nmi_total_ts = nmi_total_ts;
271 s.nmi_count = nmi_count;
Steven Rostedt (VMware)b396bfd2020-02-12 12:21:03 -0500272 s.count = count;
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400273 trace_hwlat_sample(&s);
274
Viktor Rosendahl (BMW)91edde22019-10-09 00:08:21 +0200275 latency = max(sample, outer_sample);
276
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400277 /* Keep a running maximum ever recorded hardware latency */
Viktor Rosendahl (BMW)91edde22019-10-09 00:08:21 +0200278 if (latency > tr->max_latency) {
279 tr->max_latency = latency;
280 latency_fsnotify(tr);
281 }
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400282 }
283
284out:
285 return ret;
286}
287
Steven Rostedt (Red Hat)0330f7a2016-07-15 15:48:56 -0400288static struct cpumask save_cpumask;
289static bool disable_migrate;
290
Steven Rostedt (VMware)f447c192017-01-31 16:48:23 -0500291static void move_to_next_cpu(void)
Steven Rostedt (Red Hat)0330f7a2016-07-15 15:48:56 -0400292{
Steven Rostedt (VMware)f447c192017-01-31 16:48:23 -0500293 struct cpumask *current_mask = &save_cpumask;
Kevin Hao96b48332020-07-30 16:23:18 +0800294 struct trace_array *tr = hwlat_trace;
Steven Rostedt (Red Hat)0330f7a2016-07-15 15:48:56 -0400295 int next_cpu;
296
297 if (disable_migrate)
298 return;
Steven Rostedt (Red Hat)0330f7a2016-07-15 15:48:56 -0400299 /*
300 * If for some reason the user modifies the CPU affinity
Srivatsa S. Bhat (VMware)0c3c86b2019-10-10 11:51:17 -0700301 * of this thread, then stop migrating for the duration
Steven Rostedt (Red Hat)0330f7a2016-07-15 15:48:56 -0400302 * of the current test.
303 */
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +0200304 if (!cpumask_equal(current_mask, current->cpus_ptr))
Steven Rostedt (Red Hat)0330f7a2016-07-15 15:48:56 -0400305 goto disable;
306
307 get_online_cpus();
Kevin Hao96b48332020-07-30 16:23:18 +0800308 cpumask_and(current_mask, cpu_online_mask, tr->tracing_cpumask);
Steven Rostedt (Red Hat)0330f7a2016-07-15 15:48:56 -0400309 next_cpu = cpumask_next(smp_processor_id(), current_mask);
310 put_online_cpus();
311
312 if (next_cpu >= nr_cpu_ids)
313 next_cpu = cpumask_first(current_mask);
314
Steven Rostedt (Red Hat)0330f7a2016-07-15 15:48:56 -0400315 if (next_cpu >= nr_cpu_ids) /* Shouldn't happen! */
316 goto disable;
317
318 cpumask_clear(current_mask);
319 cpumask_set_cpu(next_cpu, current_mask);
320
321 sched_setaffinity(0, current_mask);
322 return;
323
324 disable:
325 disable_migrate = true;
326}
327
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400328/*
329 * kthread_fn - The CPU time sampling/hardware latency detection kernel thread
330 *
331 * Used to periodically sample the CPU TSC via a call to get_sample. We
332 * disable interrupts, which does (intentionally) introduce latency since we
333 * need to ensure nothing else might be running (and thus preempting).
334 * Obviously this should never be used in production environments.
335 *
Luiz Capitulino8e0f1142017-02-13 12:25:17 -0500336 * Executes one loop interaction on each CPU in tracing_cpumask sysfs file.
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400337 */
338static int kthread_fn(void *data)
339{
340 u64 interval;
341
342 while (!kthread_should_stop()) {
343
Daniel Bristot de Oliveira8fa826b2021-06-22 16:42:20 +0200344 if (hwlat_data.thread_mode == MODE_ROUND_ROBIN)
345 move_to_next_cpu();
Steven Rostedt (Red Hat)0330f7a2016-07-15 15:48:56 -0400346
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400347 local_irq_disable();
348 get_sample();
349 local_irq_enable();
350
351 mutex_lock(&hwlat_data.lock);
352 interval = hwlat_data.sample_window - hwlat_data.sample_width;
353 mutex_unlock(&hwlat_data.lock);
354
355 do_div(interval, USEC_PER_MSEC); /* modifies interval value */
356
357 /* Always sleep for at least 1ms */
358 if (interval < 1)
359 interval = 1;
360
361 if (msleep_interruptible(interval))
362 break;
363 }
364
365 return 0;
366}
367
Daniel Bristot de Oliveira8fa826b2021-06-22 16:42:20 +0200368/*
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400369 * start_kthread - Kick off the hardware latency sampling/detector kthread
370 *
371 * This starts the kernel thread that will sit and sample the CPU timestamp
372 * counter (TSC or similar) and look for potential hardware latencies.
373 */
374static int start_kthread(struct trace_array *tr)
375{
Steven Rostedt (VMware)f447c192017-01-31 16:48:23 -0500376 struct cpumask *current_mask = &save_cpumask;
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400377 struct task_struct *kthread;
Steven Rostedt (VMware)f447c192017-01-31 16:48:23 -0500378 int next_cpu;
379
Vasily Averin310e3a42020-11-18 15:05:20 +0300380 if (hwlat_kthread)
Erica Bugden82fbc8c2018-08-01 12:45:54 +0200381 return 0;
382
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400383
384 kthread = kthread_create(kthread_fn, NULL, "hwlatd");
385 if (IS_ERR(kthread)) {
386 pr_err(BANNER "could not start sampling thread\n");
387 return -ENOMEM;
388 }
Steven Rostedt (VMware)f447c192017-01-31 16:48:23 -0500389
Daniel Bristot de Oliveira8fa826b2021-06-22 16:42:20 +0200390
391 /* Just pick the first CPU on first iteration */
392 get_online_cpus();
393 cpumask_and(current_mask, cpu_online_mask, tr->tracing_cpumask);
394 put_online_cpus();
395
396 if (hwlat_data.thread_mode == MODE_ROUND_ROBIN) {
397 next_cpu = cpumask_first(current_mask);
398 cpumask_clear(current_mask);
399 cpumask_set_cpu(next_cpu, current_mask);
400
401 }
402
Steven Rostedt (VMware)f447c192017-01-31 16:48:23 -0500403 sched_setaffinity(kthread->pid, current_mask);
404
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400405 hwlat_kthread = kthread;
406 wake_up_process(kthread);
407
408 return 0;
409}
410
Daniel Bristot de Oliveira8fa826b2021-06-22 16:42:20 +0200411/*
Ingo Molnarf2cc0202021-03-23 18:49:35 +0100412 * stop_kthread - Inform the hardware latency sampling/detector kthread to stop
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400413 *
414 * This kicks the running hardware latency sampling/detector kernel thread and
415 * tells it to stop sampling now. Use this on unload and at system shutdown.
416 */
417static void stop_kthread(void)
418{
419 if (!hwlat_kthread)
420 return;
421 kthread_stop(hwlat_kthread);
422 hwlat_kthread = NULL;
423}
424
425/*
426 * hwlat_read - Wrapper read function for reading both window and width
427 * @filp: The active open file structure
428 * @ubuf: The userspace provided buffer to read value into
429 * @cnt: The maximum number of bytes to read
430 * @ppos: The current "file" position
431 *
432 * This function provides a generic read implementation for the global state
433 * "hwlat_data" structure filesystem entries.
434 */
435static ssize_t hwlat_read(struct file *filp, char __user *ubuf,
436 size_t cnt, loff_t *ppos)
437{
438 char buf[U64STR_SIZE];
439 u64 *entry = filp->private_data;
440 u64 val;
441 int len;
442
443 if (!entry)
444 return -EFAULT;
445
446 if (cnt > sizeof(buf))
447 cnt = sizeof(buf);
448
449 val = *entry;
450
451 len = snprintf(buf, sizeof(buf), "%llu\n", val);
452
453 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
454}
455
456/**
457 * hwlat_width_write - Write function for "width" entry
458 * @filp: The active open file structure
459 * @ubuf: The user buffer that contains the value to write
460 * @cnt: The maximum number of bytes to write to "file"
461 * @ppos: The current position in @file
462 *
463 * This function provides a write implementation for the "width" interface
464 * to the hardware latency detector. It can be used to configure
465 * for how many us of the total window us we will actively sample for any
466 * hardware-induced latency periods. Obviously, it is not possible to
467 * sample constantly and have the system respond to a sample reader, or,
468 * worse, without having the system appear to have gone out to lunch. It
469 * is enforced that width is less that the total window size.
470 */
471static ssize_t
472hwlat_width_write(struct file *filp, const char __user *ubuf,
473 size_t cnt, loff_t *ppos)
474{
475 u64 val;
476 int err;
477
478 err = kstrtoull_from_user(ubuf, cnt, 10, &val);
479 if (err)
480 return err;
481
482 mutex_lock(&hwlat_data.lock);
483 if (val < hwlat_data.sample_window)
484 hwlat_data.sample_width = val;
485 else
486 err = -EINVAL;
487 mutex_unlock(&hwlat_data.lock);
488
489 if (err)
490 return err;
491
492 return cnt;
493}
494
495/**
496 * hwlat_window_write - Write function for "window" entry
497 * @filp: The active open file structure
498 * @ubuf: The user buffer that contains the value to write
499 * @cnt: The maximum number of bytes to write to "file"
500 * @ppos: The current position in @file
501 *
502 * This function provides a write implementation for the "window" interface
Qiujun Huang2b5894c2020-10-29 23:05:54 +0800503 * to the hardware latency detector. The window is the total time
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400504 * in us that will be considered one sample period. Conceptually, windows
505 * occur back-to-back and contain a sample width period during which
506 * actual sampling occurs. Can be used to write a new total window size. It
Qiujun Huang2b5894c2020-10-29 23:05:54 +0800507 * is enforced that any value written must be greater than the sample width
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400508 * size, or an error results.
509 */
510static ssize_t
511hwlat_window_write(struct file *filp, const char __user *ubuf,
512 size_t cnt, loff_t *ppos)
513{
514 u64 val;
515 int err;
516
517 err = kstrtoull_from_user(ubuf, cnt, 10, &val);
518 if (err)
519 return err;
520
521 mutex_lock(&hwlat_data.lock);
522 if (hwlat_data.sample_width < val)
523 hwlat_data.sample_window = val;
524 else
525 err = -EINVAL;
526 mutex_unlock(&hwlat_data.lock);
527
528 if (err)
529 return err;
530
531 return cnt;
532}
533
Daniel Bristot de Oliveira8fa826b2021-06-22 16:42:20 +0200534static void *s_mode_start(struct seq_file *s, loff_t *pos)
535{
536 int mode = *pos;
537
538 mutex_lock(&hwlat_data.lock);
539
540 if (mode >= MODE_MAX)
541 return NULL;
542
543 return pos;
544}
545
546static void *s_mode_next(struct seq_file *s, void *v, loff_t *pos)
547{
548 int mode = ++(*pos);
549
550 if (mode >= MODE_MAX)
551 return NULL;
552
553 return pos;
554}
555
556static int s_mode_show(struct seq_file *s, void *v)
557{
558 loff_t *pos = v;
559 int mode = *pos;
560
561 if (mode == hwlat_data.thread_mode)
562 seq_printf(s, "[%s]", thread_mode_str[mode]);
563 else
564 seq_printf(s, "%s", thread_mode_str[mode]);
565
566 if (mode != MODE_MAX)
567 seq_puts(s, " ");
568
569 return 0;
570}
571
572static void s_mode_stop(struct seq_file *s, void *v)
573{
574 seq_puts(s, "\n");
575 mutex_unlock(&hwlat_data.lock);
576}
577
578static const struct seq_operations thread_mode_seq_ops = {
579 .start = s_mode_start,
580 .next = s_mode_next,
581 .show = s_mode_show,
582 .stop = s_mode_stop
583};
584
585static int hwlat_mode_open(struct inode *inode, struct file *file)
586{
587 return seq_open(file, &thread_mode_seq_ops);
588};
589
590static void hwlat_tracer_start(struct trace_array *tr);
591static void hwlat_tracer_stop(struct trace_array *tr);
592
593/**
594 * hwlat_mode_write - Write function for "mode" entry
595 * @filp: The active open file structure
596 * @ubuf: The user buffer that contains the value to write
597 * @cnt: The maximum number of bytes to write to "file"
598 * @ppos: The current position in @file
599 *
600 * This function provides a write implementation for the "mode" interface
601 * to the hardware latency detector. hwlatd has different operation modes.
602 * The "none" sets the allowed cpumask for a single hwlatd thread at the
603 * startup and lets the scheduler handle the migration. The default mode is
604 * the "round-robin" one, in which a single hwlatd thread runs, migrating
605 * among the allowed CPUs in a round-robin fashion.
606 */
607static ssize_t hwlat_mode_write(struct file *filp, const char __user *ubuf,
608 size_t cnt, loff_t *ppos)
609{
610 struct trace_array *tr = hwlat_trace;
611 const char *mode;
612 char buf[64];
613 int ret, i;
614
615 if (cnt >= sizeof(buf))
616 return -EINVAL;
617
618 if (copy_from_user(buf, ubuf, cnt))
619 return -EFAULT;
620
621 buf[cnt] = 0;
622
623 mode = strstrip(buf);
624
625 ret = -EINVAL;
626
627 /*
628 * trace_types_lock is taken to avoid concurrency on start/stop
629 * and hwlat_busy.
630 */
631 mutex_lock(&trace_types_lock);
632 if (hwlat_busy)
633 hwlat_tracer_stop(tr);
634
635 mutex_lock(&hwlat_data.lock);
636
637 for (i = 0; i < MODE_MAX; i++) {
638 if (strcmp(mode, thread_mode_str[i]) == 0) {
639 hwlat_data.thread_mode = i;
640 ret = cnt;
641 }
642 }
643
644 mutex_unlock(&hwlat_data.lock);
645
646 if (hwlat_busy)
647 hwlat_tracer_start(tr);
648 mutex_unlock(&trace_types_lock);
649
650 *ppos += cnt;
651
652
653
654 return ret;
655}
656
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400657static const struct file_operations width_fops = {
658 .open = tracing_open_generic,
659 .read = hwlat_read,
660 .write = hwlat_width_write,
661};
662
663static const struct file_operations window_fops = {
664 .open = tracing_open_generic,
665 .read = hwlat_read,
666 .write = hwlat_window_write,
667};
668
Daniel Bristot de Oliveira8fa826b2021-06-22 16:42:20 +0200669static const struct file_operations thread_mode_fops = {
670 .open = hwlat_mode_open,
671 .read = seq_read,
672 .llseek = seq_lseek,
673 .release = seq_release,
674 .write = hwlat_mode_write
675};
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400676/**
677 * init_tracefs - A function to initialize the tracefs interface files
678 *
679 * This function creates entries in tracefs for "hwlat_detector".
680 * It creates the hwlat_detector directory in the tracing directory,
681 * and within that directory is the count, width and window files to
682 * change and view those values.
683 */
684static int init_tracefs(void)
685{
Wei Yang22c36b12020-07-12 09:10:36 +0800686 int ret;
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400687 struct dentry *top_dir;
688
Wei Yang22c36b12020-07-12 09:10:36 +0800689 ret = tracing_init_dentry();
690 if (ret)
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400691 return -ENOMEM;
692
Wei Yang22c36b12020-07-12 09:10:36 +0800693 top_dir = tracefs_create_dir("hwlat_detector", NULL);
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400694 if (!top_dir)
695 return -ENOMEM;
696
697 hwlat_sample_window = tracefs_create_file("window", 0640,
698 top_dir,
699 &hwlat_data.sample_window,
700 &window_fops);
701 if (!hwlat_sample_window)
702 goto err;
703
704 hwlat_sample_width = tracefs_create_file("width", 0644,
705 top_dir,
706 &hwlat_data.sample_width,
707 &width_fops);
708 if (!hwlat_sample_width)
709 goto err;
710
Daniel Bristot de Oliveira8fa826b2021-06-22 16:42:20 +0200711 hwlat_thread_mode = trace_create_file("mode", 0644,
712 top_dir,
713 NULL,
714 &thread_mode_fops);
715 if (!hwlat_thread_mode)
716 goto err;
717
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400718 return 0;
719
720 err:
Al Viroa3d1e7e2019-11-18 09:43:10 -0500721 tracefs_remove(top_dir);
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400722 return -ENOMEM;
723}
724
725static void hwlat_tracer_start(struct trace_array *tr)
726{
727 int err;
728
729 err = start_kthread(tr);
730 if (err)
731 pr_err(BANNER "Cannot start hwlat kthread\n");
732}
733
734static void hwlat_tracer_stop(struct trace_array *tr)
735{
736 stop_kthread();
737}
738
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400739static int hwlat_tracer_init(struct trace_array *tr)
740{
741 /* Only allow one instance to enable this */
742 if (hwlat_busy)
743 return -EBUSY;
744
745 hwlat_trace = tr;
746
Steven Rostedt (Red Hat)0330f7a2016-07-15 15:48:56 -0400747 disable_migrate = false;
Steven Rostedt (Red Hat)e7c15cd2016-06-23 12:45:36 -0400748 hwlat_data.count = 0;
749 tr->max_latency = 0;
750 save_tracing_thresh = tracing_thresh;
751
752 /* tracing_thresh is in nsecs, we speak in usecs */
753 if (!tracing_thresh)
754 tracing_thresh = last_tracing_thresh;
755
756 if (tracer_tracing_is_on(tr))
757 hwlat_tracer_start(tr);
758
759 hwlat_busy = true;
760
761 return 0;
762}
763
764static void hwlat_tracer_reset(struct trace_array *tr)
765{
766 stop_kthread();
767
768 /* the tracing threshold is static between runs */
769 last_tracing_thresh = tracing_thresh;
770
771 tracing_thresh = save_tracing_thresh;
772 hwlat_busy = false;
773}
774
775static struct tracer hwlat_tracer __read_mostly =
776{
777 .name = "hwlat",
778 .init = hwlat_tracer_init,
779 .reset = hwlat_tracer_reset,
780 .start = hwlat_tracer_start,
781 .stop = hwlat_tracer_stop,
782 .allow_instances = true,
783};
784
785__init static int init_hwlat_tracer(void)
786{
787 int ret;
788
789 mutex_init(&hwlat_data.lock);
790
791 ret = register_tracer(&hwlat_tracer);
792 if (ret)
793 return ret;
794
795 init_tracefs();
796
797 return 0;
798}
799late_initcall(init_hwlat_tracer);