trace_hwlat.c 22 KB

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