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