1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40#include <linux/kthread.h>
41#include <linux/tracefs.h>
42#include <linux/uaccess.h>
43#include <linux/cpumask.h>
44#include <linux/delay.h>
45#include <linux/sched/clock.h>
46#include "trace.h"
47
48static struct trace_array *hwlat_trace;
49
50#define U64STR_SIZE 22
51
52#define BANNER "hwlat_detector: "
53#define DEFAULT_SAMPLE_WINDOW 1000000
54#define DEFAULT_SAMPLE_WIDTH 500000
55#define DEFAULT_LAT_THRESHOLD 10
56
57
58static struct task_struct *hwlat_kthread;
59
60static struct dentry *hwlat_sample_width;
61static struct dentry *hwlat_sample_window;
62
63
64static unsigned long save_tracing_thresh;
65
66
67static u64 nmi_ts_start;
68static u64 nmi_total_ts;
69static int nmi_count;
70static int nmi_cpu;
71
72
73bool trace_hwlat_callback_enabled;
74
75
76static u64 last_tracing_thresh = DEFAULT_LAT_THRESHOLD * NSEC_PER_USEC;
77
78
79struct hwlat_sample {
80 u64 seqnum;
81 u64 duration;
82 u64 outer_duration;
83 u64 nmi_total_ts;
84 struct timespec64 timestamp;
85 int nmi_count;
86};
87
88
89static struct hwlat_data {
90
91 struct mutex lock;
92
93 u64 count;
94
95 u64 sample_window;
96 u64 sample_width;
97
98} hwlat_data = {
99 .sample_window = DEFAULT_SAMPLE_WINDOW,
100 .sample_width = DEFAULT_SAMPLE_WIDTH,
101};
102
103static void trace_hwlat_sample(struct hwlat_sample *sample)
104{
105 struct trace_array *tr = hwlat_trace;
106 struct trace_event_call *call = &event_hwlat;
107 struct ring_buffer *buffer = tr->trace_buffer.buffer;
108 struct ring_buffer_event *event;
109 struct hwlat_entry *entry;
110 unsigned long flags;
111 int pc;
112
113 pc = preempt_count();
114 local_save_flags(flags);
115
116 event = trace_buffer_lock_reserve(buffer, TRACE_HWLAT, sizeof(*entry),
117 flags, pc);
118 if (!event)
119 return;
120 entry = ring_buffer_event_data(event);
121 entry->seqnum = sample->seqnum;
122 entry->duration = sample->duration;
123 entry->outer_duration = sample->outer_duration;
124 entry->timestamp = sample->timestamp;
125 entry->nmi_total_ts = sample->nmi_total_ts;
126 entry->nmi_count = sample->nmi_count;
127
128 if (!call_filter_check_discard(call, entry, buffer, event))
129 trace_buffer_unlock_commit_nostack(buffer, event);
130}
131
132
133#define time_type u64
134#define time_get() trace_clock_local()
135#define time_to_us(x) div_u64(x, 1000)
136#define time_sub(a, b) ((a) - (b))
137#define init_time(a, b) (a = b)
138#define time_u64(a) a
139
140void trace_hwlat_callback(bool enter)
141{
142 if (smp_processor_id() != nmi_cpu)
143 return;
144
145
146
147
148
149 if (!IS_ENABLED(CONFIG_GENERIC_SCHED_CLOCK)) {
150 if (enter)
151 nmi_ts_start = time_get();
152 else
153 nmi_total_ts = time_get() - nmi_ts_start;
154 }
155
156 if (enter)
157 nmi_count++;
158}
159
160
161
162
163
164
165
166
167static int get_sample(void)
168{
169 struct trace_array *tr = hwlat_trace;
170 time_type start, t1, t2, last_t2;
171 s64 diff, total, last_total = 0;
172 u64 sample = 0;
173 u64 thresh = tracing_thresh;
174 u64 outer_sample = 0;
175 int ret = -1;
176
177 do_div(thresh, NSEC_PER_USEC);
178
179 nmi_cpu = smp_processor_id();
180 nmi_total_ts = 0;
181 nmi_count = 0;
182
183 barrier();
184
185 trace_hwlat_callback_enabled = true;
186
187 init_time(last_t2, 0);
188 start = time_get();
189
190 do {
191
192 t1 = time_get();
193 t2 = time_get();
194
195 if (time_u64(last_t2)) {
196
197 diff = time_to_us(time_sub(t1, last_t2));
198
199 if (diff < 0) {
200 pr_err(BANNER "time running backwards\n");
201 goto out;
202 }
203 if (diff > outer_sample)
204 outer_sample = diff;
205 }
206 last_t2 = t2;
207
208 total = time_to_us(time_sub(t2, start));
209
210
211 if (total < last_total) {
212 pr_err("Time total overflowed\n");
213 break;
214 }
215 last_total = total;
216
217
218 diff = time_to_us(time_sub(t2, t1));
219
220
221 if (diff < 0) {
222 pr_err(BANNER "time running backwards\n");
223 goto out;
224 }
225
226 if (diff > sample)
227 sample = diff;
228
229 } while (total <= hwlat_data.sample_width);
230
231 barrier();
232 trace_hwlat_callback_enabled = false;
233 barrier();
234
235 ret = 0;
236
237
238 if (sample > thresh || outer_sample > thresh) {
239 struct hwlat_sample s;
240
241 ret = 1;
242
243
244 if (nmi_total_ts)
245 do_div(nmi_total_ts, NSEC_PER_USEC);
246
247 hwlat_data.count++;
248 s.seqnum = hwlat_data.count;
249 s.duration = sample;
250 s.outer_duration = outer_sample;
251 ktime_get_real_ts64(&s.timestamp);
252 s.nmi_total_ts = nmi_total_ts;
253 s.nmi_count = nmi_count;
254 trace_hwlat_sample(&s);
255
256
257 if (sample > tr->max_latency)
258 tr->max_latency = sample;
259 }
260
261out:
262 return ret;
263}
264
265static struct cpumask save_cpumask;
266static bool disable_migrate;
267
268static void move_to_next_cpu(void)
269{
270 struct cpumask *current_mask = &save_cpumask;
271 int next_cpu;
272
273 if (disable_migrate)
274 return;
275
276
277
278
279
280 if (!cpumask_equal(current_mask, current->cpus_ptr))
281 goto disable;
282
283 get_online_cpus();
284 cpumask_and(current_mask, cpu_online_mask, tracing_buffer_mask);
285 next_cpu = cpumask_next(smp_processor_id(), current_mask);
286 put_online_cpus();
287
288 if (next_cpu >= nr_cpu_ids)
289 next_cpu = cpumask_first(current_mask);
290
291 if (next_cpu >= nr_cpu_ids)
292 goto disable;
293
294 cpumask_clear(current_mask);
295 cpumask_set_cpu(next_cpu, current_mask);
296
297 sched_setaffinity(0, current_mask);
298 return;
299
300 disable:
301 disable_migrate = true;
302}
303
304
305
306
307
308
309
310
311
312
313
314static int kthread_fn(void *data)
315{
316 u64 interval;
317
318 while (!kthread_should_stop()) {
319
320 move_to_next_cpu();
321
322 local_irq_disable();
323 get_sample();
324 local_irq_enable();
325
326 mutex_lock(&hwlat_data.lock);
327 interval = hwlat_data.sample_window - hwlat_data.sample_width;
328 mutex_unlock(&hwlat_data.lock);
329
330 do_div(interval, USEC_PER_MSEC);
331
332
333 if (interval < 1)
334 interval = 1;
335
336 if (msleep_interruptible(interval))
337 break;
338 }
339
340 return 0;
341}
342
343
344
345
346
347
348
349static int start_kthread(struct trace_array *tr)
350{
351 struct cpumask *current_mask = &save_cpumask;
352 struct task_struct *kthread;
353 int next_cpu;
354
355 if (WARN_ON(hwlat_kthread))
356 return 0;
357
358
359 current_mask = &save_cpumask;
360 get_online_cpus();
361 cpumask_and(current_mask, cpu_online_mask, tracing_buffer_mask);
362 put_online_cpus();
363 next_cpu = cpumask_first(current_mask);
364
365 kthread = kthread_create(kthread_fn, NULL, "hwlatd");
366 if (IS_ERR(kthread)) {
367 pr_err(BANNER "could not start sampling thread\n");
368 return -ENOMEM;
369 }
370
371 cpumask_clear(current_mask);
372 cpumask_set_cpu(next_cpu, current_mask);
373 sched_setaffinity(kthread->pid, current_mask);
374
375 hwlat_kthread = kthread;
376 wake_up_process(kthread);
377
378 return 0;
379}
380
381
382
383
384
385
386
387static void stop_kthread(void)
388{
389 if (!hwlat_kthread)
390 return;
391 kthread_stop(hwlat_kthread);
392 hwlat_kthread = NULL;
393}
394
395
396
397
398
399
400
401
402
403
404
405static ssize_t hwlat_read(struct file *filp, char __user *ubuf,
406 size_t cnt, loff_t *ppos)
407{
408 char buf[U64STR_SIZE];
409 u64 *entry = filp->private_data;
410 u64 val;
411 int len;
412
413 if (!entry)
414 return -EFAULT;
415
416 if (cnt > sizeof(buf))
417 cnt = sizeof(buf);
418
419 val = *entry;
420
421 len = snprintf(buf, sizeof(buf), "%llu\n", val);
422
423 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
424}
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441static ssize_t
442hwlat_width_write(struct file *filp, const char __user *ubuf,
443 size_t cnt, loff_t *ppos)
444{
445 u64 val;
446 int err;
447
448 err = kstrtoull_from_user(ubuf, cnt, 10, &val);
449 if (err)
450 return err;
451
452 mutex_lock(&hwlat_data.lock);
453 if (val < hwlat_data.sample_window)
454 hwlat_data.sample_width = val;
455 else
456 err = -EINVAL;
457 mutex_unlock(&hwlat_data.lock);
458
459 if (err)
460 return err;
461
462 return cnt;
463}
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480static ssize_t
481hwlat_window_write(struct file *filp, const char __user *ubuf,
482 size_t cnt, loff_t *ppos)
483{
484 u64 val;
485 int err;
486
487 err = kstrtoull_from_user(ubuf, cnt, 10, &val);
488 if (err)
489 return err;
490
491 mutex_lock(&hwlat_data.lock);
492 if (hwlat_data.sample_width < val)
493 hwlat_data.sample_window = val;
494 else
495 err = -EINVAL;
496 mutex_unlock(&hwlat_data.lock);
497
498 if (err)
499 return err;
500
501 return cnt;
502}
503
504static const struct file_operations width_fops = {
505 .open = tracing_open_generic,
506 .read = hwlat_read,
507 .write = hwlat_width_write,
508};
509
510static const struct file_operations window_fops = {
511 .open = tracing_open_generic,
512 .read = hwlat_read,
513 .write = hwlat_window_write,
514};
515
516
517
518
519
520
521
522
523
524static int init_tracefs(void)
525{
526 struct dentry *d_tracer;
527 struct dentry *top_dir;
528
529 d_tracer = tracing_init_dentry();
530 if (IS_ERR(d_tracer))
531 return -ENOMEM;
532
533 top_dir = tracefs_create_dir("hwlat_detector", d_tracer);
534 if (!top_dir)
535 return -ENOMEM;
536
537 hwlat_sample_window = tracefs_create_file("window", 0640,
538 top_dir,
539 &hwlat_data.sample_window,
540 &window_fops);
541 if (!hwlat_sample_window)
542 goto err;
543
544 hwlat_sample_width = tracefs_create_file("width", 0644,
545 top_dir,
546 &hwlat_data.sample_width,
547 &width_fops);
548 if (!hwlat_sample_width)
549 goto err;
550
551 return 0;
552
553 err:
554 tracefs_remove_recursive(top_dir);
555 return -ENOMEM;
556}
557
558static void hwlat_tracer_start(struct trace_array *tr)
559{
560 int err;
561
562 err = start_kthread(tr);
563 if (err)
564 pr_err(BANNER "Cannot start hwlat kthread\n");
565}
566
567static void hwlat_tracer_stop(struct trace_array *tr)
568{
569 stop_kthread();
570}
571
572static bool hwlat_busy;
573
574static int hwlat_tracer_init(struct trace_array *tr)
575{
576
577 if (hwlat_busy)
578 return -EBUSY;
579
580 hwlat_trace = tr;
581
582 disable_migrate = false;
583 hwlat_data.count = 0;
584 tr->max_latency = 0;
585 save_tracing_thresh = tracing_thresh;
586
587
588 if (!tracing_thresh)
589 tracing_thresh = last_tracing_thresh;
590
591 if (tracer_tracing_is_on(tr))
592 hwlat_tracer_start(tr);
593
594 hwlat_busy = true;
595
596 return 0;
597}
598
599static void hwlat_tracer_reset(struct trace_array *tr)
600{
601 stop_kthread();
602
603
604 last_tracing_thresh = tracing_thresh;
605
606 tracing_thresh = save_tracing_thresh;
607 hwlat_busy = false;
608}
609
610static struct tracer hwlat_tracer __read_mostly =
611{
612 .name = "hwlat",
613 .init = hwlat_tracer_init,
614 .reset = hwlat_tracer_reset,
615 .start = hwlat_tracer_start,
616 .stop = hwlat_tracer_stop,
617 .allow_instances = true,
618};
619
620__init static int init_hwlat_tracer(void)
621{
622 int ret;
623
624 mutex_init(&hwlat_data.lock);
625
626 ret = register_tracer(&hwlat_tracer);
627 if (ret)
628 return ret;
629
630 init_tracefs();
631
632 return 0;
633}
634late_initcall(init_hwlat_tracer);
635