preemptirq_long.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020-2021 The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/ftrace.h>
  6. #include <linux/sched.h>
  7. #include <linux/sysctl.h>
  8. #include <linux/printk.h>
  9. #include <linux/sched.h>
  10. #include <linux/sched/clock.h>
  11. #include <trace/hooks/preemptirq.h>
  12. #define CREATE_TRACE_POINTS
  13. #include "preemptirq_long.h"
  14. #define IRQSOFF_SENTINEL 0x0fffDEAD
  15. static unsigned int sysctl_preemptoff_tracing_threshold_ns = 1000000;
  16. static unsigned int sysctl_irqsoff_tracing_threshold_ns = 5000000;
  17. static unsigned int sysctl_irqsoff_dmesg_output_enabled;
  18. static unsigned int sysctl_irqsoff_crash_sentinel_value;
  19. static unsigned int sysctl_irqsoff_crash_threshold_ns = 10000000;
  20. static unsigned int half_million = 500000;
  21. static unsigned int one_hundred_million = 100000000;
  22. static unsigned int one_million = 1000000;
  23. static DEFINE_PER_CPU(u64, irq_disabled_ts);
  24. /*
  25. * preemption disable tracking require additional context
  26. * to rule out false positives. see the comment in
  27. * test_preempt_disable_long() for more details.
  28. */
  29. struct preempt_store {
  30. u64 ts;
  31. int pid;
  32. unsigned long ncsw;
  33. };
  34. static DEFINE_PER_CPU(struct preempt_store, the_ps);
  35. static void note_irq_disable(void *u1, unsigned long u2, unsigned long u3)
  36. {
  37. if (is_idle_task(current))
  38. return;
  39. /*
  40. * We just have to note down the time stamp here. We
  41. * use stacktrace trigger feature to print the stacktrace.
  42. */
  43. this_cpu_write(irq_disabled_ts, sched_clock());
  44. }
  45. static void test_irq_disable_long(void *u1, unsigned long ip, unsigned long parent_ip)
  46. {
  47. u64 ts = this_cpu_read(irq_disabled_ts);
  48. if (!ts)
  49. return;
  50. this_cpu_write(irq_disabled_ts, 0);
  51. ts = sched_clock() - ts;
  52. if (ts > sysctl_irqsoff_tracing_threshold_ns) {
  53. trace_irq_disable_long(ts, ip, parent_ip, CALLER_ADDR4, CALLER_ADDR5);
  54. if (sysctl_irqsoff_dmesg_output_enabled == IRQSOFF_SENTINEL)
  55. printk_deferred("irqs off exceeds thresh delta=%llu C:(%ps<-%ps<-%ps<-%ps)\n",
  56. ts, (void *)CALLER_ADDR2,
  57. (void *)CALLER_ADDR3,
  58. (void *)CALLER_ADDR4,
  59. (void *)CALLER_ADDR5);
  60. }
  61. if (sysctl_irqsoff_crash_sentinel_value == IRQSOFF_SENTINEL &&
  62. ts > sysctl_irqsoff_crash_threshold_ns) {
  63. printk_deferred("delta=%llu(ns) > crash_threshold=%u(ns) Task=%s\n",
  64. ts, sysctl_irqsoff_crash_threshold_ns,
  65. current->comm);
  66. BUG_ON(1);
  67. }
  68. }
  69. static void note_preempt_disable(void *u1, unsigned long u2, unsigned long u3)
  70. {
  71. struct preempt_store *ps = &per_cpu(the_ps, raw_smp_processor_id());
  72. ps->ts = sched_clock();
  73. ps->pid = current->pid;
  74. ps->ncsw = current->nvcsw + current->nivcsw;
  75. }
  76. static void test_preempt_disable_long(void *u1, unsigned long ip,
  77. unsigned long parent_ip)
  78. {
  79. struct preempt_store *ps = &per_cpu(the_ps, raw_smp_processor_id());
  80. u64 delta = 0;
  81. if (!ps->ts)
  82. return;
  83. /*
  84. * schedule() calls __schedule() with preemption disabled.
  85. * if we had entered idle and exiting idle now, we think
  86. * preemption is disabled the whole time. Detect this by
  87. * checking if the preemption is disabled across the same
  88. * task. There is a possiblity that the same task is scheduled
  89. * after idle. To rule out this possibility, compare the
  90. * context switch count also.
  91. */
  92. if (ps->pid == current->pid && (ps->ncsw == current->nvcsw +
  93. current->nivcsw))
  94. delta = sched_clock() - ps->ts;
  95. ps->ts = 0;
  96. if (delta > sysctl_preemptoff_tracing_threshold_ns)
  97. trace_preempt_disable_long(delta, ip, parent_ip, CALLER_ADDR4, CALLER_ADDR5);
  98. }
  99. static struct ctl_table preemptirq_long_table[] = {
  100. {
  101. .procname = "preemptoff_tracing_threshold_ns",
  102. .data = &sysctl_preemptoff_tracing_threshold_ns,
  103. .maxlen = sizeof(unsigned int),
  104. .mode = 0644,
  105. .proc_handler = proc_dointvec,
  106. },
  107. {
  108. .procname = "irqsoff_tracing_threshold_ns",
  109. .data = &sysctl_irqsoff_tracing_threshold_ns,
  110. .maxlen = sizeof(unsigned int),
  111. .mode = 0644,
  112. .proc_handler = proc_douintvec_minmax,
  113. .extra1 = &half_million,
  114. .extra2 = &one_hundred_million,
  115. },
  116. {
  117. .procname = "irqsoff_dmesg_output_enabled",
  118. .data = &sysctl_irqsoff_dmesg_output_enabled,
  119. .maxlen = sizeof(unsigned int),
  120. .mode = 0644,
  121. .proc_handler = proc_dointvec,
  122. },
  123. {
  124. .procname = "irqsoff_crash_sentinel_value",
  125. .data = &sysctl_irqsoff_crash_sentinel_value,
  126. .maxlen = sizeof(unsigned int),
  127. .mode = 0644,
  128. .proc_handler = proc_dointvec,
  129. },
  130. {
  131. .procname = "irqsoff_crash_threshold_ns",
  132. .data = &sysctl_irqsoff_crash_threshold_ns,
  133. .maxlen = sizeof(unsigned int),
  134. .mode = 0644,
  135. .proc_handler = proc_douintvec_minmax,
  136. .extra1 = &one_million,
  137. .extra2 = &one_hundred_million,
  138. },
  139. { }
  140. };
  141. int preemptirq_long_init(void)
  142. {
  143. if (!register_sysctl("preemptirq", preemptirq_long_table)) {
  144. pr_err("Fail to register sysctl table\n");
  145. return -EPERM;
  146. }
  147. register_trace_android_rvh_irqs_disable(note_irq_disable, NULL);
  148. register_trace_android_rvh_irqs_enable(test_irq_disable_long, NULL);
  149. register_trace_android_rvh_preempt_disable(note_preempt_disable, NULL);
  150. register_trace_android_rvh_preempt_enable(test_preempt_disable_long,
  151. NULL);
  152. return 0;
  153. }