trace_hwlat.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * trace_hwlat.c - A simple Hardware Latency detector.
  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. *
  37. * Includes useful feedback from Clark Williams <williams@redhat.com>
  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. static struct trace_array *hwlat_trace;
  48. #define U64STR_SIZE 22 /* 20 digits max */
  49. #define BANNER "hwlat_detector: "
  50. #define DEFAULT_SAMPLE_WINDOW 1000000 /* 1s */
  51. #define DEFAULT_SAMPLE_WIDTH 500000 /* 0.5s */
  52. #define DEFAULT_LAT_THRESHOLD 10 /* 10us */
  53. static struct dentry *hwlat_sample_width; /* sample width us */
  54. static struct dentry *hwlat_sample_window; /* sample window us */
  55. static struct dentry *hwlat_thread_mode; /* hwlat thread mode */
  56. enum {
  57. MODE_NONE = 0,
  58. MODE_ROUND_ROBIN,
  59. MODE_PER_CPU,
  60. MODE_MAX
  61. };
  62. static char *thread_mode_str[] = { "none", "round-robin", "per-cpu" };
  63. /* Save the previous tracing_thresh value */
  64. static unsigned long save_tracing_thresh;
  65. /* runtime kthread data */
  66. struct hwlat_kthread_data {
  67. struct task_struct *kthread;
  68. /* NMI timestamp counters */
  69. u64 nmi_ts_start;
  70. u64 nmi_total_ts;
  71. int nmi_count;
  72. int nmi_cpu;
  73. };
  74. static struct hwlat_kthread_data hwlat_single_cpu_data;
  75. static DEFINE_PER_CPU(struct hwlat_kthread_data, hwlat_per_cpu_data);
  76. /* Tells NMIs to call back to the hwlat tracer to record timestamps */
  77. bool trace_hwlat_callback_enabled;
  78. /* If the user changed threshold, remember it */
  79. static u64 last_tracing_thresh = DEFAULT_LAT_THRESHOLD * NSEC_PER_USEC;
  80. /* Individual latency samples are stored here when detected. */
  81. struct hwlat_sample {
  82. u64 seqnum; /* unique sequence */
  83. u64 duration; /* delta */
  84. u64 outer_duration; /* delta (outer loop) */
  85. u64 nmi_total_ts; /* Total time spent in NMIs */
  86. struct timespec64 timestamp; /* wall time */
  87. int nmi_count; /* # NMIs during this sample */
  88. int count; /* # of iterations over thresh */
  89. };
  90. /* keep the global state somewhere. */
  91. static struct hwlat_data {
  92. struct mutex lock; /* protect changes */
  93. atomic64_t count; /* total since reset */
  94. u64 sample_window; /* total sampling window (on+off) */
  95. u64 sample_width; /* active sampling portion of window */
  96. int thread_mode; /* thread mode */
  97. } hwlat_data = {
  98. .sample_window = DEFAULT_SAMPLE_WINDOW,
  99. .sample_width = DEFAULT_SAMPLE_WIDTH,
  100. .thread_mode = MODE_ROUND_ROBIN
  101. };
  102. static struct hwlat_kthread_data *get_cpu_data(void)
  103. {
  104. if (hwlat_data.thread_mode == MODE_PER_CPU)
  105. return this_cpu_ptr(&hwlat_per_cpu_data);
  106. else
  107. return &hwlat_single_cpu_data;
  108. }
  109. static bool hwlat_busy;
  110. static void trace_hwlat_sample(struct hwlat_sample *sample)
  111. {
  112. struct trace_array *tr = hwlat_trace;
  113. struct trace_buffer *buffer = tr->array_buffer.buffer;
  114. struct ring_buffer_event *event;
  115. struct hwlat_entry *entry;
  116. event = trace_buffer_lock_reserve(buffer, TRACE_HWLAT, sizeof(*entry),
  117. tracing_gen_ctx());
  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. entry->count = sample->count;
  128. trace_buffer_unlock_commit_nostack(buffer, event);
  129. }
  130. /* Macros to encapsulate the time capturing infrastructure */
  131. #define time_type u64
  132. #define time_get() trace_clock_local()
  133. #define time_to_us(x) div_u64(x, 1000)
  134. #define time_sub(a, b) ((a) - (b))
  135. #define init_time(a, b) (a = b)
  136. #define time_u64(a) a
  137. void trace_hwlat_callback(bool enter)
  138. {
  139. struct hwlat_kthread_data *kdata = get_cpu_data();
  140. if (!kdata->kthread)
  141. return;
  142. /*
  143. * Currently trace_clock_local() calls sched_clock() and the
  144. * generic version is not NMI safe.
  145. */
  146. if (!IS_ENABLED(CONFIG_GENERIC_SCHED_CLOCK)) {
  147. if (enter)
  148. kdata->nmi_ts_start = time_get();
  149. else
  150. kdata->nmi_total_ts += time_get() - kdata->nmi_ts_start;
  151. }
  152. if (enter)
  153. kdata->nmi_count++;
  154. }
  155. /*
  156. * hwlat_err - report a hwlat error.
  157. */
  158. #define hwlat_err(msg) ({ \
  159. struct trace_array *tr = hwlat_trace; \
  160. \
  161. trace_array_printk_buf(tr->array_buffer.buffer, _THIS_IP_, msg); \
  162. })
  163. /**
  164. * get_sample - sample the CPU TSC and look for likely hardware latencies
  165. *
  166. * Used to repeatedly capture the CPU TSC (or similar), looking for potential
  167. * hardware-induced latency. Called with interrupts disabled.
  168. */
  169. static int get_sample(void)
  170. {
  171. struct hwlat_kthread_data *kdata = get_cpu_data();
  172. struct trace_array *tr = hwlat_trace;
  173. struct hwlat_sample s;
  174. time_type start, t1, t2, last_t2;
  175. s64 diff, outer_diff, total, last_total = 0;
  176. u64 sample = 0;
  177. u64 sample_width = READ_ONCE(hwlat_data.sample_width);
  178. u64 thresh = tracing_thresh;
  179. u64 outer_sample = 0;
  180. int ret = -1;
  181. unsigned int count = 0;
  182. do_div(thresh, NSEC_PER_USEC); /* modifies interval value */
  183. kdata->nmi_total_ts = 0;
  184. kdata->nmi_count = 0;
  185. /* Make sure NMIs see this first */
  186. barrier();
  187. trace_hwlat_callback_enabled = true;
  188. init_time(last_t2, 0);
  189. start = time_get(); /* start timestamp */
  190. outer_diff = 0;
  191. do {
  192. t1 = time_get(); /* we'll look for a discontinuity */
  193. t2 = time_get();
  194. if (time_u64(last_t2)) {
  195. /* Check the delta from outer loop (t2 to next t1) */
  196. outer_diff = time_to_us(time_sub(t1, last_t2));
  197. /* This shouldn't happen */
  198. if (outer_diff < 0) {
  199. hwlat_err(BANNER "time running backwards\n");
  200. goto out;
  201. }
  202. if (outer_diff > outer_sample)
  203. outer_sample = outer_diff;
  204. }
  205. last_t2 = t2;
  206. total = time_to_us(time_sub(t2, start)); /* sample width */
  207. /* Check for possible overflows */
  208. if (total < last_total) {
  209. hwlat_err("Time total overflowed\n");
  210. break;
  211. }
  212. last_total = total;
  213. /* This checks the inner loop (t1 to t2) */
  214. diff = time_to_us(time_sub(t2, t1)); /* current diff */
  215. if (diff > thresh || outer_diff > thresh) {
  216. if (!count)
  217. ktime_get_real_ts64(&s.timestamp);
  218. count++;
  219. }
  220. /* This shouldn't happen */
  221. if (diff < 0) {
  222. hwlat_err(BANNER "time running backwards\n");
  223. goto out;
  224. }
  225. if (diff > sample)
  226. sample = diff; /* only want highest value */
  227. } while (total <= sample_width);
  228. barrier(); /* finish the above in the view for NMIs */
  229. trace_hwlat_callback_enabled = false;
  230. barrier(); /* Make sure nmi_total_ts is no longer updated */
  231. ret = 0;
  232. /* If we exceed the threshold value, we have found a hardware latency */
  233. if (sample > thresh || outer_sample > thresh) {
  234. u64 latency;
  235. ret = 1;
  236. /* We read in microseconds */
  237. if (kdata->nmi_total_ts)
  238. do_div(kdata->nmi_total_ts, NSEC_PER_USEC);
  239. s.seqnum = atomic64_inc_return(&hwlat_data.count);
  240. s.duration = sample;
  241. s.outer_duration = outer_sample;
  242. s.nmi_total_ts = kdata->nmi_total_ts;
  243. s.nmi_count = kdata->nmi_count;
  244. s.count = count;
  245. trace_hwlat_sample(&s);
  246. latency = max(sample, outer_sample);
  247. /* Keep a running maximum ever recorded hardware latency */
  248. if (latency > tr->max_latency) {
  249. tr->max_latency = latency;
  250. latency_fsnotify(tr);
  251. }
  252. }
  253. out:
  254. return ret;
  255. }
  256. static struct cpumask save_cpumask;
  257. static void move_to_next_cpu(void)
  258. {
  259. struct cpumask *current_mask = &save_cpumask;
  260. struct trace_array *tr = hwlat_trace;
  261. int next_cpu;
  262. /*
  263. * If for some reason the user modifies the CPU affinity
  264. * of this thread, then stop migrating for the duration
  265. * of the current test.
  266. */
  267. if (!cpumask_equal(current_mask, current->cpus_ptr))
  268. goto change_mode;
  269. cpus_read_lock();
  270. cpumask_and(current_mask, cpu_online_mask, tr->tracing_cpumask);
  271. next_cpu = cpumask_next_wrap(raw_smp_processor_id(), current_mask);
  272. cpus_read_unlock();
  273. if (next_cpu >= nr_cpu_ids) /* Shouldn't happen! */
  274. goto change_mode;
  275. cpumask_clear(current_mask);
  276. cpumask_set_cpu(next_cpu, current_mask);
  277. set_cpus_allowed_ptr(current, current_mask);
  278. return;
  279. change_mode:
  280. hwlat_data.thread_mode = MODE_NONE;
  281. pr_info(BANNER "cpumask changed while in round-robin mode, switching to mode none\n");
  282. }
  283. /*
  284. * kthread_fn - The CPU time sampling/hardware latency detection kernel thread
  285. *
  286. * Used to periodically sample the CPU TSC via a call to get_sample. We
  287. * disable interrupts, which does (intentionally) introduce latency since we
  288. * need to ensure nothing else might be running (and thus preempting).
  289. * Obviously this should never be used in production environments.
  290. *
  291. * Executes one loop interaction on each CPU in tracing_cpumask sysfs file.
  292. */
  293. static int kthread_fn(void *data)
  294. {
  295. u64 interval;
  296. while (!kthread_should_stop()) {
  297. if (hwlat_data.thread_mode == MODE_ROUND_ROBIN)
  298. move_to_next_cpu();
  299. local_irq_disable();
  300. get_sample();
  301. local_irq_enable();
  302. mutex_lock(&hwlat_data.lock);
  303. interval = hwlat_data.sample_window - hwlat_data.sample_width;
  304. mutex_unlock(&hwlat_data.lock);
  305. do_div(interval, USEC_PER_MSEC); /* modifies interval value */
  306. /* Always sleep for at least 1ms */
  307. if (interval < 1)
  308. interval = 1;
  309. if (msleep_interruptible(interval))
  310. break;
  311. }
  312. return 0;
  313. }
  314. /*
  315. * stop_stop_kthread - Inform the hardware latency sampling/detector kthread to stop
  316. *
  317. * This kicks the running hardware latency sampling/detector kernel thread and
  318. * tells it to stop sampling now. Use this on unload and at system shutdown.
  319. */
  320. static void stop_single_kthread(void)
  321. {
  322. struct hwlat_kthread_data *kdata = get_cpu_data();
  323. struct task_struct *kthread;
  324. cpus_read_lock();
  325. kthread = kdata->kthread;
  326. if (!kthread)
  327. goto out_put_cpus;
  328. kthread_stop(kthread);
  329. kdata->kthread = NULL;
  330. out_put_cpus:
  331. cpus_read_unlock();
  332. }
  333. /*
  334. * start_single_kthread - Kick off the hardware latency sampling/detector kthread
  335. *
  336. * This starts the kernel thread that will sit and sample the CPU timestamp
  337. * counter (TSC or similar) and look for potential hardware latencies.
  338. */
  339. static int start_single_kthread(struct trace_array *tr)
  340. {
  341. struct hwlat_kthread_data *kdata = get_cpu_data();
  342. struct cpumask *current_mask = &save_cpumask;
  343. struct task_struct *kthread;
  344. int next_cpu;
  345. cpus_read_lock();
  346. if (kdata->kthread)
  347. goto out_put_cpus;
  348. kthread = kthread_create(kthread_fn, NULL, "hwlatd");
  349. if (IS_ERR(kthread)) {
  350. pr_err(BANNER "could not start sampling thread\n");
  351. cpus_read_unlock();
  352. return -ENOMEM;
  353. }
  354. /* Just pick the first CPU on first iteration */
  355. cpumask_and(current_mask, cpu_online_mask, tr->tracing_cpumask);
  356. if (hwlat_data.thread_mode == MODE_ROUND_ROBIN) {
  357. next_cpu = cpumask_first(current_mask);
  358. cpumask_clear(current_mask);
  359. cpumask_set_cpu(next_cpu, current_mask);
  360. }
  361. set_cpus_allowed_ptr(kthread, current_mask);
  362. kdata->kthread = kthread;
  363. wake_up_process(kthread);
  364. out_put_cpus:
  365. cpus_read_unlock();
  366. return 0;
  367. }
  368. /*
  369. * stop_cpu_kthread - Stop a hwlat cpu kthread
  370. */
  371. static void stop_cpu_kthread(unsigned int cpu)
  372. {
  373. struct task_struct *kthread;
  374. kthread = per_cpu(hwlat_per_cpu_data, cpu).kthread;
  375. if (kthread)
  376. kthread_stop(kthread);
  377. per_cpu(hwlat_per_cpu_data, cpu).kthread = NULL;
  378. }
  379. /*
  380. * stop_per_cpu_kthreads - Inform the hardware latency sampling/detector kthread to stop
  381. *
  382. * This kicks the running hardware latency sampling/detector kernel threads and
  383. * tells it to stop sampling now. Use this on unload and at system shutdown.
  384. */
  385. static void stop_per_cpu_kthreads(void)
  386. {
  387. unsigned int cpu;
  388. cpus_read_lock();
  389. for_each_online_cpu(cpu)
  390. stop_cpu_kthread(cpu);
  391. cpus_read_unlock();
  392. }
  393. /*
  394. * start_cpu_kthread - Start a hwlat cpu kthread
  395. */
  396. static int start_cpu_kthread(unsigned int cpu)
  397. {
  398. struct task_struct *kthread;
  399. /* Do not start a new hwlatd thread if it is already running */
  400. if (per_cpu(hwlat_per_cpu_data, cpu).kthread)
  401. return 0;
  402. kthread = kthread_run_on_cpu(kthread_fn, NULL, cpu, "hwlatd/%u");
  403. if (IS_ERR(kthread)) {
  404. pr_err(BANNER "could not start sampling thread\n");
  405. return -ENOMEM;
  406. }
  407. per_cpu(hwlat_per_cpu_data, cpu).kthread = kthread;
  408. return 0;
  409. }
  410. #ifdef CONFIG_HOTPLUG_CPU
  411. static void hwlat_hotplug_workfn(struct work_struct *dummy)
  412. {
  413. struct trace_array *tr = hwlat_trace;
  414. unsigned int cpu = smp_processor_id();
  415. mutex_lock(&trace_types_lock);
  416. mutex_lock(&hwlat_data.lock);
  417. cpus_read_lock();
  418. if (!hwlat_busy || hwlat_data.thread_mode != MODE_PER_CPU)
  419. goto out_unlock;
  420. if (!cpu_online(cpu))
  421. goto out_unlock;
  422. if (!cpumask_test_cpu(cpu, tr->tracing_cpumask))
  423. goto out_unlock;
  424. start_cpu_kthread(cpu);
  425. out_unlock:
  426. cpus_read_unlock();
  427. mutex_unlock(&hwlat_data.lock);
  428. mutex_unlock(&trace_types_lock);
  429. }
  430. static DECLARE_WORK(hwlat_hotplug_work, hwlat_hotplug_workfn);
  431. /*
  432. * hwlat_cpu_init - CPU hotplug online callback function
  433. */
  434. static int hwlat_cpu_init(unsigned int cpu)
  435. {
  436. schedule_work_on(cpu, &hwlat_hotplug_work);
  437. return 0;
  438. }
  439. /*
  440. * hwlat_cpu_die - CPU hotplug offline callback function
  441. */
  442. static int hwlat_cpu_die(unsigned int cpu)
  443. {
  444. stop_cpu_kthread(cpu);
  445. return 0;
  446. }
  447. static void hwlat_init_hotplug_support(void)
  448. {
  449. int ret;
  450. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "trace/hwlat:online",
  451. hwlat_cpu_init, hwlat_cpu_die);
  452. if (ret < 0)
  453. pr_warn(BANNER "Error to init cpu hotplug support\n");
  454. return;
  455. }
  456. #else /* CONFIG_HOTPLUG_CPU */
  457. static void hwlat_init_hotplug_support(void)
  458. {
  459. return;
  460. }
  461. #endif /* CONFIG_HOTPLUG_CPU */
  462. /*
  463. * start_per_cpu_kthreads - Kick off the hardware latency sampling/detector kthreads
  464. *
  465. * This starts the kernel threads that will sit on potentially all cpus and
  466. * sample the CPU timestamp counter (TSC or similar) and look for potential
  467. * hardware latencies.
  468. */
  469. static int start_per_cpu_kthreads(struct trace_array *tr)
  470. {
  471. struct cpumask *current_mask = &save_cpumask;
  472. unsigned int cpu;
  473. int retval;
  474. cpus_read_lock();
  475. /*
  476. * Run only on CPUs in which hwlat is allowed to run.
  477. */
  478. cpumask_and(current_mask, cpu_online_mask, tr->tracing_cpumask);
  479. for_each_cpu(cpu, current_mask) {
  480. retval = start_cpu_kthread(cpu);
  481. if (retval)
  482. goto out_error;
  483. }
  484. cpus_read_unlock();
  485. return 0;
  486. out_error:
  487. cpus_read_unlock();
  488. stop_per_cpu_kthreads();
  489. return retval;
  490. }
  491. static void *s_mode_start(struct seq_file *s, loff_t *pos)
  492. {
  493. int mode = *pos;
  494. mutex_lock(&hwlat_data.lock);
  495. if (mode >= MODE_MAX)
  496. return NULL;
  497. return pos;
  498. }
  499. static void *s_mode_next(struct seq_file *s, void *v, loff_t *pos)
  500. {
  501. int mode = ++(*pos);
  502. if (mode >= MODE_MAX)
  503. return NULL;
  504. return pos;
  505. }
  506. static int s_mode_show(struct seq_file *s, void *v)
  507. {
  508. loff_t *pos = v;
  509. int mode = *pos;
  510. if (mode == hwlat_data.thread_mode)
  511. seq_printf(s, "[%s]", thread_mode_str[mode]);
  512. else
  513. seq_printf(s, "%s", thread_mode_str[mode]);
  514. if (mode < MODE_MAX - 1) /* if mode is any but last */
  515. seq_puts(s, " ");
  516. return 0;
  517. }
  518. static void s_mode_stop(struct seq_file *s, void *v)
  519. {
  520. seq_puts(s, "\n");
  521. mutex_unlock(&hwlat_data.lock);
  522. }
  523. static const struct seq_operations thread_mode_seq_ops = {
  524. .start = s_mode_start,
  525. .next = s_mode_next,
  526. .show = s_mode_show,
  527. .stop = s_mode_stop
  528. };
  529. static int hwlat_mode_open(struct inode *inode, struct file *file)
  530. {
  531. return seq_open(file, &thread_mode_seq_ops);
  532. };
  533. static void hwlat_tracer_start(struct trace_array *tr);
  534. static void hwlat_tracer_stop(struct trace_array *tr);
  535. /**
  536. * hwlat_mode_write - Write function for "mode" entry
  537. * @filp: The active open file structure
  538. * @ubuf: The user buffer that contains the value to write
  539. * @cnt: The maximum number of bytes to write to "file"
  540. * @ppos: The current position in @file
  541. *
  542. * This function provides a write implementation for the "mode" interface
  543. * to the hardware latency detector. hwlatd has different operation modes.
  544. * The "none" sets the allowed cpumask for a single hwlatd thread at the
  545. * startup and lets the scheduler handle the migration. The default mode is
  546. * the "round-robin" one, in which a single hwlatd thread runs, migrating
  547. * among the allowed CPUs in a round-robin fashion. The "per-cpu" mode
  548. * creates one hwlatd thread per allowed CPU.
  549. */
  550. static ssize_t hwlat_mode_write(struct file *filp, const char __user *ubuf,
  551. size_t cnt, loff_t *ppos)
  552. {
  553. struct trace_array *tr = hwlat_trace;
  554. const char *mode;
  555. char buf[64];
  556. int ret, i;
  557. if (cnt >= sizeof(buf))
  558. return -EINVAL;
  559. if (copy_from_user(buf, ubuf, cnt))
  560. return -EFAULT;
  561. buf[cnt] = 0;
  562. mode = strstrip(buf);
  563. ret = -EINVAL;
  564. /*
  565. * trace_types_lock is taken to avoid concurrency on start/stop
  566. * and hwlat_busy.
  567. */
  568. mutex_lock(&trace_types_lock);
  569. if (hwlat_busy)
  570. hwlat_tracer_stop(tr);
  571. mutex_lock(&hwlat_data.lock);
  572. for (i = 0; i < MODE_MAX; i++) {
  573. if (strcmp(mode, thread_mode_str[i]) == 0) {
  574. hwlat_data.thread_mode = i;
  575. ret = cnt;
  576. }
  577. }
  578. mutex_unlock(&hwlat_data.lock);
  579. if (hwlat_busy)
  580. hwlat_tracer_start(tr);
  581. mutex_unlock(&trace_types_lock);
  582. *ppos += cnt;
  583. return ret;
  584. }
  585. /*
  586. * The width parameter is read/write using the generic trace_min_max_param
  587. * method. The *val is protected by the hwlat_data lock and is upper
  588. * bounded by the window parameter.
  589. */
  590. static struct trace_min_max_param hwlat_width = {
  591. .lock = &hwlat_data.lock,
  592. .val = &hwlat_data.sample_width,
  593. .max = &hwlat_data.sample_window,
  594. .min = NULL,
  595. };
  596. /*
  597. * The window parameter is read/write using the generic trace_min_max_param
  598. * method. The *val is protected by the hwlat_data lock and is lower
  599. * bounded by the width parameter.
  600. */
  601. static struct trace_min_max_param hwlat_window = {
  602. .lock = &hwlat_data.lock,
  603. .val = &hwlat_data.sample_window,
  604. .max = NULL,
  605. .min = &hwlat_data.sample_width,
  606. };
  607. static const struct file_operations thread_mode_fops = {
  608. .open = hwlat_mode_open,
  609. .read = seq_read,
  610. .llseek = seq_lseek,
  611. .release = seq_release,
  612. .write = hwlat_mode_write
  613. };
  614. /**
  615. * init_tracefs - A function to initialize the tracefs interface files
  616. *
  617. * This function creates entries in tracefs for "hwlat_detector".
  618. * It creates the hwlat_detector directory in the tracing directory,
  619. * and within that directory is the count, width and window files to
  620. * change and view those values.
  621. */
  622. static int init_tracefs(void)
  623. {
  624. int ret;
  625. struct dentry *top_dir;
  626. ret = tracing_init_dentry();
  627. if (ret)
  628. return -ENOMEM;
  629. top_dir = tracefs_create_dir("hwlat_detector", NULL);
  630. if (!top_dir)
  631. return -ENOMEM;
  632. hwlat_sample_window = tracefs_create_file("window", TRACE_MODE_WRITE,
  633. top_dir,
  634. &hwlat_window,
  635. &trace_min_max_fops);
  636. if (!hwlat_sample_window)
  637. goto err;
  638. hwlat_sample_width = tracefs_create_file("width", TRACE_MODE_WRITE,
  639. top_dir,
  640. &hwlat_width,
  641. &trace_min_max_fops);
  642. if (!hwlat_sample_width)
  643. goto err;
  644. hwlat_thread_mode = trace_create_file("mode", TRACE_MODE_WRITE,
  645. top_dir,
  646. NULL,
  647. &thread_mode_fops);
  648. if (!hwlat_thread_mode)
  649. goto err;
  650. return 0;
  651. err:
  652. tracefs_remove(top_dir);
  653. return -ENOMEM;
  654. }
  655. static void hwlat_tracer_start(struct trace_array *tr)
  656. {
  657. int err;
  658. if (hwlat_data.thread_mode == MODE_PER_CPU)
  659. err = start_per_cpu_kthreads(tr);
  660. else
  661. err = start_single_kthread(tr);
  662. if (err)
  663. pr_err(BANNER "Cannot start hwlat kthread\n");
  664. }
  665. static void hwlat_tracer_stop(struct trace_array *tr)
  666. {
  667. if (hwlat_data.thread_mode == MODE_PER_CPU)
  668. stop_per_cpu_kthreads();
  669. else
  670. stop_single_kthread();
  671. }
  672. static int hwlat_tracer_init(struct trace_array *tr)
  673. {
  674. /* Only allow one instance to enable this */
  675. if (hwlat_busy)
  676. return -EBUSY;
  677. hwlat_trace = tr;
  678. atomic64_set(&hwlat_data.count, 0);
  679. tr->max_latency = 0;
  680. save_tracing_thresh = tracing_thresh;
  681. /* tracing_thresh is in nsecs, we speak in usecs */
  682. if (!tracing_thresh)
  683. tracing_thresh = last_tracing_thresh;
  684. if (tracer_tracing_is_on(tr))
  685. hwlat_tracer_start(tr);
  686. hwlat_busy = true;
  687. return 0;
  688. }
  689. static void hwlat_tracer_reset(struct trace_array *tr)
  690. {
  691. hwlat_tracer_stop(tr);
  692. /* the tracing threshold is static between runs */
  693. last_tracing_thresh = tracing_thresh;
  694. tracing_thresh = save_tracing_thresh;
  695. hwlat_busy = false;
  696. }
  697. static struct tracer hwlat_tracer __read_mostly =
  698. {
  699. .name = "hwlat",
  700. .init = hwlat_tracer_init,
  701. .reset = hwlat_tracer_reset,
  702. .start = hwlat_tracer_start,
  703. .stop = hwlat_tracer_stop,
  704. .allow_instances = true,
  705. };
  706. __init static int init_hwlat_tracer(void)
  707. {
  708. int ret;
  709. mutex_init(&hwlat_data.lock);
  710. ret = register_tracer(&hwlat_tracer);
  711. if (ret)
  712. return ret;
  713. hwlat_init_hotplug_support();
  714. init_tracefs();
  715. return 0;
  716. }
  717. late_initcall(init_hwlat_tracer);