Steven Rostedt (VMware) | bcea3f9 | 2018-08-16 11:23:53 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 2 | /* |
Srivatsa S. Bhat (VMware) | 0c3c86b | 2019-10-10 11:51:17 -0700 | [diff] [blame] | 3 | * trace_hwlat.c - A simple Hardware Latency detector. |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 4 | * |
| 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 Oliveira | bb1b24c | 2021-06-22 16:42:19 +0200 | [diff] [blame] | 37 | * Includes useful feedback from Clark Williams <williams@redhat.com> |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 38 | * |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 39 | */ |
| 40 | #include <linux/kthread.h> |
| 41 | #include <linux/tracefs.h> |
| 42 | #include <linux/uaccess.h> |
Steven Rostedt (Red Hat) | 0330f7a | 2016-07-15 15:48:56 -0400 | [diff] [blame] | 43 | #include <linux/cpumask.h> |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 44 | #include <linux/delay.h> |
Ingo Molnar | e601757 | 2017-02-01 16:36:40 +0100 | [diff] [blame] | 45 | #include <linux/sched/clock.h> |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 46 | #include "trace.h" |
| 47 | |
| 48 | static 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*/ |
| 58 | static struct task_struct *hwlat_kthread; |
| 59 | |
| 60 | static struct dentry *hwlat_sample_width; /* sample width us */ |
| 61 | static struct dentry *hwlat_sample_window; /* sample window us */ |
Daniel Bristot de Oliveira | 8fa826b | 2021-06-22 16:42:20 +0200 | [diff] [blame^] | 62 | static struct dentry *hwlat_thread_mode; /* hwlat thread mode */ |
| 63 | |
| 64 | enum { |
| 65 | MODE_NONE = 0, |
| 66 | MODE_ROUND_ROBIN, |
| 67 | MODE_MAX |
| 68 | }; |
| 69 | static char *thread_mode_str[] = { "none", "round-robin" }; |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 70 | |
| 71 | /* Save the previous tracing_thresh value */ |
| 72 | static unsigned long save_tracing_thresh; |
| 73 | |
Steven Rostedt (Red Hat) | 7b2c862 | 2016-08-04 12:49:53 -0400 | [diff] [blame] | 74 | /* NMI timestamp counters */ |
| 75 | static u64 nmi_ts_start; |
| 76 | static u64 nmi_total_ts; |
| 77 | static int nmi_count; |
| 78 | static int nmi_cpu; |
| 79 | |
| 80 | /* Tells NMIs to call back to the hwlat tracer to record timestamps */ |
| 81 | bool trace_hwlat_callback_enabled; |
| 82 | |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 83 | /* If the user changed threshold, remember it */ |
| 84 | static u64 last_tracing_thresh = DEFAULT_LAT_THRESHOLD * NSEC_PER_USEC; |
| 85 | |
| 86 | /* Individual latency samples are stored here when detected. */ |
| 87 | struct hwlat_sample { |
Deepa Dinamani | 51aad0a | 2017-05-08 15:59:13 -0700 | [diff] [blame] | 88 | 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 Molnar | f2cc020 | 2021-03-23 18:49:35 +0100 | [diff] [blame] | 94 | int count; /* # of iterations over thresh */ |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | /* keep the global state somewhere. */ |
| 98 | static 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 Oliveira | 8fa826b | 2021-06-22 16:42:20 +0200 | [diff] [blame^] | 107 | int thread_mode; /* thread mode */ |
| 108 | |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 109 | } hwlat_data = { |
| 110 | .sample_window = DEFAULT_SAMPLE_WINDOW, |
| 111 | .sample_width = DEFAULT_SAMPLE_WIDTH, |
Daniel Bristot de Oliveira | 8fa826b | 2021-06-22 16:42:20 +0200 | [diff] [blame^] | 112 | .thread_mode = MODE_ROUND_ROBIN |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 113 | }; |
| 114 | |
Daniel Bristot de Oliveira | 8fa826b | 2021-06-22 16:42:20 +0200 | [diff] [blame^] | 115 | static bool hwlat_busy; |
| 116 | |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 117 | static 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) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 121 | struct trace_buffer *buffer = tr->array_buffer.buffer; |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 122 | struct ring_buffer_event *event; |
| 123 | struct hwlat_entry *entry; |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 124 | |
| 125 | event = trace_buffer_lock_reserve(buffer, TRACE_HWLAT, sizeof(*entry), |
Sebastian Andrzej Siewior | 36590c50 | 2021-01-25 20:45:08 +0100 | [diff] [blame] | 126 | tracing_gen_ctx()); |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 127 | 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) | 7b2c862 | 2016-08-04 12:49:53 -0400 | [diff] [blame] | 134 | entry->nmi_total_ts = sample->nmi_total_ts; |
| 135 | entry->nmi_count = sample->nmi_count; |
Steven Rostedt (VMware) | b396bfd | 2020-02-12 12:21:03 -0500 | [diff] [blame] | 136 | entry->count = sample->count; |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 137 | |
| 138 | if (!call_filter_check_discard(call, entry, buffer, event)) |
Steven Rostedt (Red Hat) | 52ffabe3 | 2016-11-23 20:28:38 -0500 | [diff] [blame] | 139 | trace_buffer_unlock_commit_nostack(buffer, event); |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 140 | } |
| 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) | 7b2c862 | 2016-08-04 12:49:53 -0400 | [diff] [blame] | 150 | void 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) | 98dc19c | 2019-10-10 11:50:46 -0700 | [diff] [blame] | 163 | nmi_total_ts += time_get() - nmi_ts_start; |
Steven Rostedt (Red Hat) | 7b2c862 | 2016-08-04 12:49:53 -0400 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | if (enter) |
| 167 | nmi_count++; |
| 168 | } |
| 169 | |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 170 | /** |
| 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 | */ |
| 177 | static int get_sample(void) |
| 178 | { |
| 179 | struct trace_array *tr = hwlat_trace; |
Steven Rostedt (VMware) | b396bfd | 2020-02-12 12:21:03 -0500 | [diff] [blame] | 180 | struct hwlat_sample s; |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 181 | time_type start, t1, t2, last_t2; |
Steven Rostedt (VMware) | b396bfd | 2020-02-12 12:21:03 -0500 | [diff] [blame] | 182 | s64 diff, outer_diff, total, last_total = 0; |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 183 | u64 sample = 0; |
| 184 | u64 thresh = tracing_thresh; |
| 185 | u64 outer_sample = 0; |
| 186 | int ret = -1; |
Steven Rostedt (VMware) | b396bfd | 2020-02-12 12:21:03 -0500 | [diff] [blame] | 187 | unsigned int count = 0; |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 188 | |
| 189 | do_div(thresh, NSEC_PER_USEC); /* modifies interval value */ |
| 190 | |
Steven Rostedt (Red Hat) | 7b2c862 | 2016-08-04 12:49:53 -0400 | [diff] [blame] | 191 | 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) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 199 | init_time(last_t2, 0); |
| 200 | start = time_get(); /* start timestamp */ |
Steven Rostedt (VMware) | b396bfd | 2020-02-12 12:21:03 -0500 | [diff] [blame] | 201 | outer_diff = 0; |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 202 | |
| 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) | b396bfd | 2020-02-12 12:21:03 -0500 | [diff] [blame] | 210 | outer_diff = time_to_us(time_sub(t1, last_t2)); |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 211 | /* This shouldn't happen */ |
Steven Rostedt (VMware) | b396bfd | 2020-02-12 12:21:03 -0500 | [diff] [blame] | 212 | if (outer_diff < 0) { |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 213 | pr_err(BANNER "time running backwards\n"); |
| 214 | goto out; |
| 215 | } |
Steven Rostedt (VMware) | b396bfd | 2020-02-12 12:21:03 -0500 | [diff] [blame] | 216 | if (outer_diff > outer_sample) |
| 217 | outer_sample = outer_diff; |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 218 | } |
| 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) | b396bfd | 2020-02-12 12:21:03 -0500 | [diff] [blame] | 233 | if (diff > thresh || outer_diff > thresh) { |
| 234 | if (!count) |
| 235 | ktime_get_real_ts64(&s.timestamp); |
| 236 | count++; |
| 237 | } |
| 238 | |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 239 | /* 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) | 7b2c862 | 2016-08-04 12:49:53 -0400 | [diff] [blame] | 250 | 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) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 254 | 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) | 91edde2 | 2019-10-09 00:08:21 +0200 | [diff] [blame] | 258 | u64 latency; |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 259 | |
| 260 | ret = 1; |
| 261 | |
Steven Rostedt (Red Hat) | 7b2c862 | 2016-08-04 12:49:53 -0400 | [diff] [blame] | 262 | /* We read in microseconds */ |
| 263 | if (nmi_total_ts) |
| 264 | do_div(nmi_total_ts, NSEC_PER_USEC); |
| 265 | |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 266 | hwlat_data.count++; |
| 267 | s.seqnum = hwlat_data.count; |
| 268 | s.duration = sample; |
| 269 | s.outer_duration = outer_sample; |
Steven Rostedt (Red Hat) | 7b2c862 | 2016-08-04 12:49:53 -0400 | [diff] [blame] | 270 | s.nmi_total_ts = nmi_total_ts; |
| 271 | s.nmi_count = nmi_count; |
Steven Rostedt (VMware) | b396bfd | 2020-02-12 12:21:03 -0500 | [diff] [blame] | 272 | s.count = count; |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 273 | trace_hwlat_sample(&s); |
| 274 | |
Viktor Rosendahl (BMW) | 91edde2 | 2019-10-09 00:08:21 +0200 | [diff] [blame] | 275 | latency = max(sample, outer_sample); |
| 276 | |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 277 | /* Keep a running maximum ever recorded hardware latency */ |
Viktor Rosendahl (BMW) | 91edde2 | 2019-10-09 00:08:21 +0200 | [diff] [blame] | 278 | if (latency > tr->max_latency) { |
| 279 | tr->max_latency = latency; |
| 280 | latency_fsnotify(tr); |
| 281 | } |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | out: |
| 285 | return ret; |
| 286 | } |
| 287 | |
Steven Rostedt (Red Hat) | 0330f7a | 2016-07-15 15:48:56 -0400 | [diff] [blame] | 288 | static struct cpumask save_cpumask; |
| 289 | static bool disable_migrate; |
| 290 | |
Steven Rostedt (VMware) | f447c19 | 2017-01-31 16:48:23 -0500 | [diff] [blame] | 291 | static void move_to_next_cpu(void) |
Steven Rostedt (Red Hat) | 0330f7a | 2016-07-15 15:48:56 -0400 | [diff] [blame] | 292 | { |
Steven Rostedt (VMware) | f447c19 | 2017-01-31 16:48:23 -0500 | [diff] [blame] | 293 | struct cpumask *current_mask = &save_cpumask; |
Kevin Hao | 96b4833 | 2020-07-30 16:23:18 +0800 | [diff] [blame] | 294 | struct trace_array *tr = hwlat_trace; |
Steven Rostedt (Red Hat) | 0330f7a | 2016-07-15 15:48:56 -0400 | [diff] [blame] | 295 | int next_cpu; |
| 296 | |
| 297 | if (disable_migrate) |
| 298 | return; |
Steven Rostedt (Red Hat) | 0330f7a | 2016-07-15 15:48:56 -0400 | [diff] [blame] | 299 | /* |
| 300 | * If for some reason the user modifies the CPU affinity |
Srivatsa S. Bhat (VMware) | 0c3c86b | 2019-10-10 11:51:17 -0700 | [diff] [blame] | 301 | * of this thread, then stop migrating for the duration |
Steven Rostedt (Red Hat) | 0330f7a | 2016-07-15 15:48:56 -0400 | [diff] [blame] | 302 | * of the current test. |
| 303 | */ |
Sebastian Andrzej Siewior | 3bd3706 | 2019-04-23 16:26:36 +0200 | [diff] [blame] | 304 | if (!cpumask_equal(current_mask, current->cpus_ptr)) |
Steven Rostedt (Red Hat) | 0330f7a | 2016-07-15 15:48:56 -0400 | [diff] [blame] | 305 | goto disable; |
| 306 | |
| 307 | get_online_cpus(); |
Kevin Hao | 96b4833 | 2020-07-30 16:23:18 +0800 | [diff] [blame] | 308 | cpumask_and(current_mask, cpu_online_mask, tr->tracing_cpumask); |
Steven Rostedt (Red Hat) | 0330f7a | 2016-07-15 15:48:56 -0400 | [diff] [blame] | 309 | 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) | 0330f7a | 2016-07-15 15:48:56 -0400 | [diff] [blame] | 315 | 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) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 328 | /* |
| 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 Capitulino | 8e0f114 | 2017-02-13 12:25:17 -0500 | [diff] [blame] | 336 | * Executes one loop interaction on each CPU in tracing_cpumask sysfs file. |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 337 | */ |
| 338 | static int kthread_fn(void *data) |
| 339 | { |
| 340 | u64 interval; |
| 341 | |
| 342 | while (!kthread_should_stop()) { |
| 343 | |
Daniel Bristot de Oliveira | 8fa826b | 2021-06-22 16:42:20 +0200 | [diff] [blame^] | 344 | if (hwlat_data.thread_mode == MODE_ROUND_ROBIN) |
| 345 | move_to_next_cpu(); |
Steven Rostedt (Red Hat) | 0330f7a | 2016-07-15 15:48:56 -0400 | [diff] [blame] | 346 | |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 347 | 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 Oliveira | 8fa826b | 2021-06-22 16:42:20 +0200 | [diff] [blame^] | 368 | /* |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 369 | * 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 | */ |
| 374 | static int start_kthread(struct trace_array *tr) |
| 375 | { |
Steven Rostedt (VMware) | f447c19 | 2017-01-31 16:48:23 -0500 | [diff] [blame] | 376 | struct cpumask *current_mask = &save_cpumask; |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 377 | struct task_struct *kthread; |
Steven Rostedt (VMware) | f447c19 | 2017-01-31 16:48:23 -0500 | [diff] [blame] | 378 | int next_cpu; |
| 379 | |
Vasily Averin | 310e3a4 | 2020-11-18 15:05:20 +0300 | [diff] [blame] | 380 | if (hwlat_kthread) |
Erica Bugden | 82fbc8c | 2018-08-01 12:45:54 +0200 | [diff] [blame] | 381 | return 0; |
| 382 | |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 383 | |
| 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) | f447c19 | 2017-01-31 16:48:23 -0500 | [diff] [blame] | 389 | |
Daniel Bristot de Oliveira | 8fa826b | 2021-06-22 16:42:20 +0200 | [diff] [blame^] | 390 | |
| 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) | f447c19 | 2017-01-31 16:48:23 -0500 | [diff] [blame] | 403 | sched_setaffinity(kthread->pid, current_mask); |
| 404 | |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 405 | hwlat_kthread = kthread; |
| 406 | wake_up_process(kthread); |
| 407 | |
| 408 | return 0; |
| 409 | } |
| 410 | |
Daniel Bristot de Oliveira | 8fa826b | 2021-06-22 16:42:20 +0200 | [diff] [blame^] | 411 | /* |
Ingo Molnar | f2cc020 | 2021-03-23 18:49:35 +0100 | [diff] [blame] | 412 | * stop_kthread - Inform the hardware latency sampling/detector kthread to stop |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 413 | * |
| 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 | */ |
| 417 | static 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 | */ |
| 435 | static 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 | */ |
| 471 | static ssize_t |
| 472 | hwlat_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 Huang | 2b5894c | 2020-10-29 23:05:54 +0800 | [diff] [blame] | 503 | * to the hardware latency detector. The window is the total time |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 504 | * 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 Huang | 2b5894c | 2020-10-29 23:05:54 +0800 | [diff] [blame] | 507 | * is enforced that any value written must be greater than the sample width |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 508 | * size, or an error results. |
| 509 | */ |
| 510 | static ssize_t |
| 511 | hwlat_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 Oliveira | 8fa826b | 2021-06-22 16:42:20 +0200 | [diff] [blame^] | 534 | static 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 | |
| 546 | static 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 | |
| 556 | static 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 | |
| 572 | static void s_mode_stop(struct seq_file *s, void *v) |
| 573 | { |
| 574 | seq_puts(s, "\n"); |
| 575 | mutex_unlock(&hwlat_data.lock); |
| 576 | } |
| 577 | |
| 578 | static 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 | |
| 585 | static int hwlat_mode_open(struct inode *inode, struct file *file) |
| 586 | { |
| 587 | return seq_open(file, &thread_mode_seq_ops); |
| 588 | }; |
| 589 | |
| 590 | static void hwlat_tracer_start(struct trace_array *tr); |
| 591 | static 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 | */ |
| 607 | static 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) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 657 | static const struct file_operations width_fops = { |
| 658 | .open = tracing_open_generic, |
| 659 | .read = hwlat_read, |
| 660 | .write = hwlat_width_write, |
| 661 | }; |
| 662 | |
| 663 | static 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 Oliveira | 8fa826b | 2021-06-22 16:42:20 +0200 | [diff] [blame^] | 669 | static 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) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 676 | /** |
| 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 | */ |
| 684 | static int init_tracefs(void) |
| 685 | { |
Wei Yang | 22c36b1 | 2020-07-12 09:10:36 +0800 | [diff] [blame] | 686 | int ret; |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 687 | struct dentry *top_dir; |
| 688 | |
Wei Yang | 22c36b1 | 2020-07-12 09:10:36 +0800 | [diff] [blame] | 689 | ret = tracing_init_dentry(); |
| 690 | if (ret) |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 691 | return -ENOMEM; |
| 692 | |
Wei Yang | 22c36b1 | 2020-07-12 09:10:36 +0800 | [diff] [blame] | 693 | top_dir = tracefs_create_dir("hwlat_detector", NULL); |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 694 | 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 Oliveira | 8fa826b | 2021-06-22 16:42:20 +0200 | [diff] [blame^] | 711 | 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) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 718 | return 0; |
| 719 | |
| 720 | err: |
Al Viro | a3d1e7e | 2019-11-18 09:43:10 -0500 | [diff] [blame] | 721 | tracefs_remove(top_dir); |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 722 | return -ENOMEM; |
| 723 | } |
| 724 | |
| 725 | static 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 | |
| 734 | static void hwlat_tracer_stop(struct trace_array *tr) |
| 735 | { |
| 736 | stop_kthread(); |
| 737 | } |
| 738 | |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 739 | static 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) | 0330f7a | 2016-07-15 15:48:56 -0400 | [diff] [blame] | 747 | disable_migrate = false; |
Steven Rostedt (Red Hat) | e7c15cd | 2016-06-23 12:45:36 -0400 | [diff] [blame] | 748 | 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 | |
| 764 | static 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 | |
| 775 | static 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 | } |
| 799 | late_initcall(init_hwlat_tracer); |