trace_irqsoff.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * trace irqs off critical timings
  3. *
  4. * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
  5. * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
  6. *
  7. * From code in the latency_tracer, that is:
  8. *
  9. * Copyright (C) 2004-2006 Ingo Molnar
  10. * Copyright (C) 2004 William Lee Irwin III
  11. */
  12. #include <linux/kallsyms.h>
  13. #include <linux/debugfs.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/module.h>
  16. #include <linux/ftrace.h>
  17. #include <linux/fs.h>
  18. #include "trace.h"
  19. static struct trace_array *irqsoff_trace __read_mostly;
  20. static int tracer_enabled __read_mostly;
  21. static DEFINE_PER_CPU(int, tracing_cpu);
  22. static DEFINE_SPINLOCK(max_trace_lock);
  23. enum {
  24. TRACER_IRQS_OFF = (1 << 1),
  25. TRACER_PREEMPT_OFF = (1 << 2),
  26. };
  27. static int trace_type __read_mostly;
  28. static int save_lat_flag;
  29. #ifdef CONFIG_PREEMPT_TRACER
  30. static inline int
  31. preempt_trace(void)
  32. {
  33. return ((trace_type & TRACER_PREEMPT_OFF) && preempt_count());
  34. }
  35. #else
  36. # define preempt_trace() (0)
  37. #endif
  38. #ifdef CONFIG_IRQSOFF_TRACER
  39. static inline int
  40. irq_trace(void)
  41. {
  42. return ((trace_type & TRACER_IRQS_OFF) &&
  43. irqs_disabled());
  44. }
  45. #else
  46. # define irq_trace() (0)
  47. #endif
  48. /*
  49. * Sequence count - we record it when starting a measurement and
  50. * skip the latency if the sequence has changed - some other section
  51. * did a maximum and could disturb our measurement with serial console
  52. * printouts, etc. Truly coinciding maximum latencies should be rare
  53. * and what happens together happens separately as well, so this doesnt
  54. * decrease the validity of the maximum found:
  55. */
  56. static __cacheline_aligned_in_smp unsigned long max_sequence;
  57. #ifdef CONFIG_FUNCTION_TRACER
  58. /*
  59. * irqsoff uses its own tracer function to keep the overhead down:
  60. */
  61. static void
  62. irqsoff_tracer_call(unsigned long ip, unsigned long parent_ip)
  63. {
  64. struct trace_array *tr = irqsoff_trace;
  65. struct trace_array_cpu *data;
  66. unsigned long flags;
  67. long disabled;
  68. int cpu;
  69. /*
  70. * Does not matter if we preempt. We test the flags
  71. * afterward, to see if irqs are disabled or not.
  72. * If we preempt and get a false positive, the flags
  73. * test will fail.
  74. */
  75. cpu = raw_smp_processor_id();
  76. if (likely(!per_cpu(tracing_cpu, cpu)))
  77. return;
  78. local_save_flags(flags);
  79. /* slight chance to get a false positive on tracing_cpu */
  80. if (!irqs_disabled_flags(flags))
  81. return;
  82. data = tr->data[cpu];
  83. disabled = atomic_inc_return(&data->disabled);
  84. if (likely(disabled == 1))
  85. trace_function(tr, ip, parent_ip, flags, preempt_count());
  86. atomic_dec(&data->disabled);
  87. }
  88. static struct ftrace_ops trace_ops __read_mostly =
  89. {
  90. .func = irqsoff_tracer_call,
  91. };
  92. #endif /* CONFIG_FUNCTION_TRACER */
  93. /*
  94. * Should this new latency be reported/recorded?
  95. */
  96. static int report_latency(cycle_t delta)
  97. {
  98. if (tracing_thresh) {
  99. if (delta < tracing_thresh)
  100. return 0;
  101. } else {
  102. if (delta <= tracing_max_latency)
  103. return 0;
  104. }
  105. return 1;
  106. }
  107. static void
  108. check_critical_timing(struct trace_array *tr,
  109. struct trace_array_cpu *data,
  110. unsigned long parent_ip,
  111. int cpu)
  112. {
  113. unsigned long latency, t0, t1;
  114. cycle_t T0, T1, delta;
  115. unsigned long flags;
  116. int pc;
  117. /*
  118. * usecs conversion is slow so we try to delay the conversion
  119. * as long as possible:
  120. */
  121. T0 = data->preempt_timestamp;
  122. T1 = ftrace_now(cpu);
  123. delta = T1-T0;
  124. local_save_flags(flags);
  125. pc = preempt_count();
  126. if (!report_latency(delta))
  127. goto out;
  128. spin_lock_irqsave(&max_trace_lock, flags);
  129. /* check if we are still the max latency */
  130. if (!report_latency(delta))
  131. goto out_unlock;
  132. trace_function(tr, CALLER_ADDR0, parent_ip, flags, pc);
  133. latency = nsecs_to_usecs(delta);
  134. if (data->critical_sequence != max_sequence)
  135. goto out_unlock;
  136. tracing_max_latency = delta;
  137. t0 = nsecs_to_usecs(T0);
  138. t1 = nsecs_to_usecs(T1);
  139. data->critical_end = parent_ip;
  140. update_max_tr_single(tr, current, cpu);
  141. max_sequence++;
  142. out_unlock:
  143. spin_unlock_irqrestore(&max_trace_lock, flags);
  144. out:
  145. data->critical_sequence = max_sequence;
  146. data->preempt_timestamp = ftrace_now(cpu);
  147. tracing_reset(tr, cpu);
  148. trace_function(tr, CALLER_ADDR0, parent_ip, flags, pc);
  149. }
  150. static inline void
  151. start_critical_timing(unsigned long ip, unsigned long parent_ip)
  152. {
  153. int cpu;
  154. struct trace_array *tr = irqsoff_trace;
  155. struct trace_array_cpu *data;
  156. unsigned long flags;
  157. if (likely(!tracer_enabled))
  158. return;
  159. cpu = raw_smp_processor_id();
  160. if (per_cpu(tracing_cpu, cpu))
  161. return;
  162. data = tr->data[cpu];
  163. if (unlikely(!data) || atomic_read(&data->disabled))
  164. return;
  165. atomic_inc(&data->disabled);
  166. data->critical_sequence = max_sequence;
  167. data->preempt_timestamp = ftrace_now(cpu);
  168. data->critical_start = parent_ip ? : ip;
  169. tracing_reset(tr, cpu);
  170. local_save_flags(flags);
  171. trace_function(tr, ip, parent_ip, flags, preempt_count());
  172. per_cpu(tracing_cpu, cpu) = 1;
  173. atomic_dec(&data->disabled);
  174. }
  175. static inline void
  176. stop_critical_timing(unsigned long ip, unsigned long parent_ip)
  177. {
  178. int cpu;
  179. struct trace_array *tr = irqsoff_trace;
  180. struct trace_array_cpu *data;
  181. unsigned long flags;
  182. cpu = raw_smp_processor_id();
  183. /* Always clear the tracing cpu on stopping the trace */
  184. if (unlikely(per_cpu(tracing_cpu, cpu)))
  185. per_cpu(tracing_cpu, cpu) = 0;
  186. else
  187. return;
  188. if (!tracer_enabled)
  189. return;
  190. data = tr->data[cpu];
  191. if (unlikely(!data) ||
  192. !data->critical_start || atomic_read(&data->disabled))
  193. return;
  194. atomic_inc(&data->disabled);
  195. local_save_flags(flags);
  196. trace_function(tr, ip, parent_ip, flags, preempt_count());
  197. check_critical_timing(tr, data, parent_ip ? : ip, cpu);
  198. data->critical_start = 0;
  199. atomic_dec(&data->disabled);
  200. }
  201. /* start and stop critical timings used to for stoppage (in idle) */
  202. void start_critical_timings(void)
  203. {
  204. if (preempt_trace() || irq_trace())
  205. start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  206. }
  207. EXPORT_SYMBOL_GPL(start_critical_timings);
  208. void stop_critical_timings(void)
  209. {
  210. if (preempt_trace() || irq_trace())
  211. stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  212. }
  213. EXPORT_SYMBOL_GPL(stop_critical_timings);
  214. #ifdef CONFIG_IRQSOFF_TRACER
  215. #ifdef CONFIG_PROVE_LOCKING
  216. void time_hardirqs_on(unsigned long a0, unsigned long a1)
  217. {
  218. if (!preempt_trace() && irq_trace())
  219. stop_critical_timing(a0, a1);
  220. }
  221. void time_hardirqs_off(unsigned long a0, unsigned long a1)
  222. {
  223. if (!preempt_trace() && irq_trace())
  224. start_critical_timing(a0, a1);
  225. }
  226. #else /* !CONFIG_PROVE_LOCKING */
  227. /*
  228. * Stubs:
  229. */
  230. void early_boot_irqs_off(void)
  231. {
  232. }
  233. void early_boot_irqs_on(void)
  234. {
  235. }
  236. void trace_softirqs_on(unsigned long ip)
  237. {
  238. }
  239. void trace_softirqs_off(unsigned long ip)
  240. {
  241. }
  242. inline void print_irqtrace_events(struct task_struct *curr)
  243. {
  244. }
  245. /*
  246. * We are only interested in hardirq on/off events:
  247. */
  248. void trace_hardirqs_on(void)
  249. {
  250. if (!preempt_trace() && irq_trace())
  251. stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  252. }
  253. EXPORT_SYMBOL(trace_hardirqs_on);
  254. void trace_hardirqs_off(void)
  255. {
  256. if (!preempt_trace() && irq_trace())
  257. start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  258. }
  259. EXPORT_SYMBOL(trace_hardirqs_off);
  260. void trace_hardirqs_on_caller(unsigned long caller_addr)
  261. {
  262. if (!preempt_trace() && irq_trace())
  263. stop_critical_timing(CALLER_ADDR0, caller_addr);
  264. }
  265. EXPORT_SYMBOL(trace_hardirqs_on_caller);
  266. void trace_hardirqs_off_caller(unsigned long caller_addr)
  267. {
  268. if (!preempt_trace() && irq_trace())
  269. start_critical_timing(CALLER_ADDR0, caller_addr);
  270. }
  271. EXPORT_SYMBOL(trace_hardirqs_off_caller);
  272. #endif /* CONFIG_PROVE_LOCKING */
  273. #endif /* CONFIG_IRQSOFF_TRACER */
  274. #ifdef CONFIG_PREEMPT_TRACER
  275. void trace_preempt_on(unsigned long a0, unsigned long a1)
  276. {
  277. if (preempt_trace())
  278. stop_critical_timing(a0, a1);
  279. }
  280. void trace_preempt_off(unsigned long a0, unsigned long a1)
  281. {
  282. if (preempt_trace())
  283. start_critical_timing(a0, a1);
  284. }
  285. #endif /* CONFIG_PREEMPT_TRACER */
  286. static void start_irqsoff_tracer(struct trace_array *tr)
  287. {
  288. register_ftrace_function(&trace_ops);
  289. if (tracing_is_enabled())
  290. tracer_enabled = 1;
  291. else
  292. tracer_enabled = 0;
  293. }
  294. static void stop_irqsoff_tracer(struct trace_array *tr)
  295. {
  296. tracer_enabled = 0;
  297. unregister_ftrace_function(&trace_ops);
  298. }
  299. static void __irqsoff_tracer_init(struct trace_array *tr)
  300. {
  301. save_lat_flag = trace_flags & TRACE_ITER_LATENCY_FMT;
  302. trace_flags |= TRACE_ITER_LATENCY_FMT;
  303. tracing_max_latency = 0;
  304. irqsoff_trace = tr;
  305. /* make sure that the tracer is visible */
  306. smp_wmb();
  307. start_irqsoff_tracer(tr);
  308. }
  309. static void irqsoff_tracer_reset(struct trace_array *tr)
  310. {
  311. stop_irqsoff_tracer(tr);
  312. if (!save_lat_flag)
  313. trace_flags &= ~TRACE_ITER_LATENCY_FMT;
  314. }
  315. static void irqsoff_tracer_start(struct trace_array *tr)
  316. {
  317. tracer_enabled = 1;
  318. }
  319. static void irqsoff_tracer_stop(struct trace_array *tr)
  320. {
  321. tracer_enabled = 0;
  322. }
  323. #ifdef CONFIG_IRQSOFF_TRACER
  324. static int irqsoff_tracer_init(struct trace_array *tr)
  325. {
  326. trace_type = TRACER_IRQS_OFF;
  327. __irqsoff_tracer_init(tr);
  328. return 0;
  329. }
  330. static struct tracer irqsoff_tracer __read_mostly =
  331. {
  332. .name = "irqsoff",
  333. .init = irqsoff_tracer_init,
  334. .reset = irqsoff_tracer_reset,
  335. .start = irqsoff_tracer_start,
  336. .stop = irqsoff_tracer_stop,
  337. .print_max = 1,
  338. #ifdef CONFIG_FTRACE_SELFTEST
  339. .selftest = trace_selftest_startup_irqsoff,
  340. #endif
  341. };
  342. # define register_irqsoff(trace) register_tracer(&trace)
  343. #else
  344. # define register_irqsoff(trace) do { } while (0)
  345. #endif
  346. #ifdef CONFIG_PREEMPT_TRACER
  347. static int preemptoff_tracer_init(struct trace_array *tr)
  348. {
  349. trace_type = TRACER_PREEMPT_OFF;
  350. __irqsoff_tracer_init(tr);
  351. return 0;
  352. }
  353. static struct tracer preemptoff_tracer __read_mostly =
  354. {
  355. .name = "preemptoff",
  356. .init = preemptoff_tracer_init,
  357. .reset = irqsoff_tracer_reset,
  358. .start = irqsoff_tracer_start,
  359. .stop = irqsoff_tracer_stop,
  360. .print_max = 1,
  361. #ifdef CONFIG_FTRACE_SELFTEST
  362. .selftest = trace_selftest_startup_preemptoff,
  363. #endif
  364. };
  365. # define register_preemptoff(trace) register_tracer(&trace)
  366. #else
  367. # define register_preemptoff(trace) do { } while (0)
  368. #endif
  369. #if defined(CONFIG_IRQSOFF_TRACER) && \
  370. defined(CONFIG_PREEMPT_TRACER)
  371. static int preemptirqsoff_tracer_init(struct trace_array *tr)
  372. {
  373. trace_type = TRACER_IRQS_OFF | TRACER_PREEMPT_OFF;
  374. __irqsoff_tracer_init(tr);
  375. return 0;
  376. }
  377. static struct tracer preemptirqsoff_tracer __read_mostly =
  378. {
  379. .name = "preemptirqsoff",
  380. .init = preemptirqsoff_tracer_init,
  381. .reset = irqsoff_tracer_reset,
  382. .start = irqsoff_tracer_start,
  383. .stop = irqsoff_tracer_stop,
  384. .print_max = 1,
  385. #ifdef CONFIG_FTRACE_SELFTEST
  386. .selftest = trace_selftest_startup_preemptirqsoff,
  387. #endif
  388. };
  389. # define register_preemptirqsoff(trace) register_tracer(&trace)
  390. #else
  391. # define register_preemptirqsoff(trace) do { } while (0)
  392. #endif
  393. __init static int init_irqsoff_tracer(void)
  394. {
  395. register_irqsoff(irqsoff_tracer);
  396. register_preemptoff(preemptoff_tracer);
  397. register_preemptirqsoff(preemptirqsoff_tracer);
  398. return 0;
  399. }
  400. device_initcall(init_irqsoff_tracer);