timer_list.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * List pending timers
  4. *
  5. * Copyright(C) 2006, Red Hat, Inc., Ingo Molnar
  6. */
  7. #include <linux/proc_fs.h>
  8. #include <linux/module.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/sched.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/kallsyms.h>
  13. #include <linux/nmi.h>
  14. #include <linux/uaccess.h>
  15. #include "tick-internal.h"
  16. struct timer_list_iter {
  17. int cpu;
  18. bool second_pass;
  19. u64 now;
  20. };
  21. /*
  22. * This allows printing both to /proc/timer_list and
  23. * to the console (on SysRq-Q):
  24. */
  25. __printf(2, 3)
  26. static void SEQ_printf(struct seq_file *m, const char *fmt, ...)
  27. {
  28. va_list args;
  29. va_start(args, fmt);
  30. if (m)
  31. seq_vprintf(m, fmt, args);
  32. else
  33. vprintk(fmt, args);
  34. va_end(args);
  35. }
  36. static void print_name_offset(struct seq_file *m, void *sym)
  37. {
  38. char symname[KSYM_NAME_LEN];
  39. if (lookup_symbol_name((unsigned long)sym, symname) < 0)
  40. SEQ_printf(m, "<%pK>", sym);
  41. else
  42. SEQ_printf(m, "%s", symname);
  43. }
  44. static void
  45. print_timer(struct seq_file *m, struct hrtimer *taddr, struct hrtimer *timer,
  46. int idx, u64 now)
  47. {
  48. SEQ_printf(m, " #%d: ", idx);
  49. print_name_offset(m, taddr);
  50. SEQ_printf(m, ", ");
  51. print_name_offset(m, timer->function);
  52. SEQ_printf(m, ", S:%02x", timer->state);
  53. SEQ_printf(m, "\n");
  54. SEQ_printf(m, " # expires at %Lu-%Lu nsecs [in %Ld to %Ld nsecs]\n",
  55. (unsigned long long)ktime_to_ns(hrtimer_get_softexpires(timer)),
  56. (unsigned long long)ktime_to_ns(hrtimer_get_expires(timer)),
  57. (long long)(ktime_to_ns(hrtimer_get_softexpires(timer)) - now),
  58. (long long)(ktime_to_ns(hrtimer_get_expires(timer)) - now));
  59. }
  60. static void
  61. print_active_timers(struct seq_file *m, struct hrtimer_clock_base *base,
  62. u64 now)
  63. {
  64. struct hrtimer *timer, tmp;
  65. unsigned long next = 0, i;
  66. struct timerqueue_node *curr;
  67. unsigned long flags;
  68. next_one:
  69. i = 0;
  70. touch_nmi_watchdog();
  71. raw_spin_lock_irqsave(&base->cpu_base->lock, flags);
  72. curr = timerqueue_getnext(&base->active);
  73. /*
  74. * Crude but we have to do this O(N*N) thing, because
  75. * we have to unlock the base when printing:
  76. */
  77. while (curr && i < next) {
  78. curr = timerqueue_iterate_next(curr);
  79. i++;
  80. }
  81. if (curr) {
  82. timer = container_of(curr, struct hrtimer, node);
  83. tmp = *timer;
  84. raw_spin_unlock_irqrestore(&base->cpu_base->lock, flags);
  85. print_timer(m, timer, &tmp, i, now);
  86. next++;
  87. goto next_one;
  88. }
  89. raw_spin_unlock_irqrestore(&base->cpu_base->lock, flags);
  90. }
  91. static void
  92. print_base(struct seq_file *m, struct hrtimer_clock_base *base, u64 now)
  93. {
  94. SEQ_printf(m, " .base: %pK\n", base);
  95. SEQ_printf(m, " .index: %d\n", base->index);
  96. SEQ_printf(m, " .resolution: %u nsecs\n", hrtimer_resolution);
  97. SEQ_printf(m, " .get_time: ");
  98. print_name_offset(m, base->get_time);
  99. SEQ_printf(m, "\n");
  100. #ifdef CONFIG_HIGH_RES_TIMERS
  101. SEQ_printf(m, " .offset: %Lu nsecs\n",
  102. (unsigned long long) ktime_to_ns(base->offset));
  103. #endif
  104. SEQ_printf(m, "active timers:\n");
  105. print_active_timers(m, base, now + ktime_to_ns(base->offset));
  106. }
  107. static void print_cpu(struct seq_file *m, int cpu, u64 now)
  108. {
  109. struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
  110. int i;
  111. SEQ_printf(m, "cpu: %d\n", cpu);
  112. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  113. SEQ_printf(m, " clock %d:\n", i);
  114. print_base(m, cpu_base->clock_base + i, now);
  115. }
  116. #define P(x) \
  117. SEQ_printf(m, " .%-15s: %Lu\n", #x, \
  118. (unsigned long long)(cpu_base->x))
  119. #define P_ns(x) \
  120. SEQ_printf(m, " .%-15s: %Lu nsecs\n", #x, \
  121. (unsigned long long)(ktime_to_ns(cpu_base->x)))
  122. #ifdef CONFIG_HIGH_RES_TIMERS
  123. P_ns(expires_next);
  124. P(hres_active);
  125. P(nr_events);
  126. P(nr_retries);
  127. P(nr_hangs);
  128. P(max_hang_time);
  129. #endif
  130. #undef P
  131. #undef P_ns
  132. #ifdef CONFIG_TICK_ONESHOT
  133. # define P(x) \
  134. SEQ_printf(m, " .%-15s: %Lu\n", #x, \
  135. (unsigned long long)(ts->x))
  136. # define P_ns(x) \
  137. SEQ_printf(m, " .%-15s: %Lu nsecs\n", #x, \
  138. (unsigned long long)(ktime_to_ns(ts->x)))
  139. {
  140. struct tick_sched *ts = tick_get_tick_sched(cpu);
  141. P(nohz_mode);
  142. P_ns(last_tick);
  143. P(tick_stopped);
  144. P(idle_jiffies);
  145. P(idle_calls);
  146. P(idle_sleeps);
  147. P_ns(idle_entrytime);
  148. P_ns(idle_waketime);
  149. P_ns(idle_exittime);
  150. P_ns(idle_sleeptime);
  151. P_ns(iowait_sleeptime);
  152. P(last_jiffies);
  153. P(next_timer);
  154. P_ns(idle_expires);
  155. SEQ_printf(m, "jiffies: %Lu\n",
  156. (unsigned long long)jiffies);
  157. }
  158. #endif
  159. #undef P
  160. #undef P_ns
  161. SEQ_printf(m, "\n");
  162. }
  163. #ifdef CONFIG_GENERIC_CLOCKEVENTS
  164. static void
  165. print_tickdevice(struct seq_file *m, struct tick_device *td, int cpu)
  166. {
  167. struct clock_event_device *dev = td->evtdev;
  168. touch_nmi_watchdog();
  169. SEQ_printf(m, "Tick Device: mode: %d\n", td->mode);
  170. if (cpu < 0)
  171. SEQ_printf(m, "Broadcast device\n");
  172. else
  173. SEQ_printf(m, "Per CPU device: %d\n", cpu);
  174. SEQ_printf(m, "Clock Event Device: ");
  175. if (!dev) {
  176. SEQ_printf(m, "<NULL>\n");
  177. return;
  178. }
  179. SEQ_printf(m, "%s\n", dev->name);
  180. SEQ_printf(m, " max_delta_ns: %llu\n",
  181. (unsigned long long) dev->max_delta_ns);
  182. SEQ_printf(m, " min_delta_ns: %llu\n",
  183. (unsigned long long) dev->min_delta_ns);
  184. SEQ_printf(m, " mult: %u\n", dev->mult);
  185. SEQ_printf(m, " shift: %u\n", dev->shift);
  186. SEQ_printf(m, " mode: %d\n", clockevent_get_state(dev));
  187. SEQ_printf(m, " next_event: %Ld nsecs\n",
  188. (unsigned long long) ktime_to_ns(dev->next_event));
  189. SEQ_printf(m, " set_next_event: ");
  190. print_name_offset(m, dev->set_next_event);
  191. SEQ_printf(m, "\n");
  192. if (dev->set_state_shutdown) {
  193. SEQ_printf(m, " shutdown: ");
  194. print_name_offset(m, dev->set_state_shutdown);
  195. SEQ_printf(m, "\n");
  196. }
  197. if (dev->set_state_periodic) {
  198. SEQ_printf(m, " periodic: ");
  199. print_name_offset(m, dev->set_state_periodic);
  200. SEQ_printf(m, "\n");
  201. }
  202. if (dev->set_state_oneshot) {
  203. SEQ_printf(m, " oneshot: ");
  204. print_name_offset(m, dev->set_state_oneshot);
  205. SEQ_printf(m, "\n");
  206. }
  207. if (dev->set_state_oneshot_stopped) {
  208. SEQ_printf(m, " oneshot stopped: ");
  209. print_name_offset(m, dev->set_state_oneshot_stopped);
  210. SEQ_printf(m, "\n");
  211. }
  212. if (dev->tick_resume) {
  213. SEQ_printf(m, " resume: ");
  214. print_name_offset(m, dev->tick_resume);
  215. SEQ_printf(m, "\n");
  216. }
  217. SEQ_printf(m, " event_handler: ");
  218. print_name_offset(m, dev->event_handler);
  219. SEQ_printf(m, "\n");
  220. SEQ_printf(m, " retries: %lu\n", dev->retries);
  221. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  222. if (cpu >= 0) {
  223. const struct clock_event_device *wd = tick_get_wakeup_device(cpu);
  224. SEQ_printf(m, "Wakeup Device: %s\n", wd ? wd->name : "<NULL>");
  225. }
  226. #endif
  227. SEQ_printf(m, "\n");
  228. }
  229. static void timer_list_show_tickdevices_header(struct seq_file *m)
  230. {
  231. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  232. print_tickdevice(m, tick_get_broadcast_device(), -1);
  233. SEQ_printf(m, "tick_broadcast_mask: %*pb\n",
  234. cpumask_pr_args(tick_get_broadcast_mask()));
  235. #ifdef CONFIG_TICK_ONESHOT
  236. SEQ_printf(m, "tick_broadcast_oneshot_mask: %*pb\n",
  237. cpumask_pr_args(tick_get_broadcast_oneshot_mask()));
  238. #endif
  239. SEQ_printf(m, "\n");
  240. #endif
  241. }
  242. #endif
  243. static inline void timer_list_header(struct seq_file *m, u64 now)
  244. {
  245. SEQ_printf(m, "Timer List Version: v0.9\n");
  246. SEQ_printf(m, "HRTIMER_MAX_CLOCK_BASES: %d\n", HRTIMER_MAX_CLOCK_BASES);
  247. SEQ_printf(m, "now at %Ld nsecs\n", (unsigned long long)now);
  248. SEQ_printf(m, "\n");
  249. }
  250. void sysrq_timer_list_show(void)
  251. {
  252. u64 now = ktime_to_ns(ktime_get());
  253. int cpu;
  254. timer_list_header(NULL, now);
  255. for_each_online_cpu(cpu)
  256. print_cpu(NULL, cpu, now);
  257. #ifdef CONFIG_GENERIC_CLOCKEVENTS
  258. timer_list_show_tickdevices_header(NULL);
  259. for_each_online_cpu(cpu)
  260. print_tickdevice(NULL, tick_get_device(cpu), cpu);
  261. #endif
  262. return;
  263. }
  264. #ifdef CONFIG_PROC_FS
  265. static int timer_list_show(struct seq_file *m, void *v)
  266. {
  267. struct timer_list_iter *iter = v;
  268. if (iter->cpu == -1 && !iter->second_pass)
  269. timer_list_header(m, iter->now);
  270. else if (!iter->second_pass)
  271. print_cpu(m, iter->cpu, iter->now);
  272. #ifdef CONFIG_GENERIC_CLOCKEVENTS
  273. else if (iter->cpu == -1 && iter->second_pass)
  274. timer_list_show_tickdevices_header(m);
  275. else
  276. print_tickdevice(m, tick_get_device(iter->cpu), iter->cpu);
  277. #endif
  278. return 0;
  279. }
  280. static void *move_iter(struct timer_list_iter *iter, loff_t offset)
  281. {
  282. for (; offset; offset--) {
  283. iter->cpu = cpumask_next(iter->cpu, cpu_online_mask);
  284. if (iter->cpu >= nr_cpu_ids) {
  285. #ifdef CONFIG_GENERIC_CLOCKEVENTS
  286. if (!iter->second_pass) {
  287. iter->cpu = -1;
  288. iter->second_pass = true;
  289. } else
  290. return NULL;
  291. #else
  292. return NULL;
  293. #endif
  294. }
  295. }
  296. return iter;
  297. }
  298. static void *timer_list_start(struct seq_file *file, loff_t *offset)
  299. {
  300. struct timer_list_iter *iter = file->private;
  301. if (!*offset)
  302. iter->now = ktime_to_ns(ktime_get());
  303. iter->cpu = -1;
  304. iter->second_pass = false;
  305. return move_iter(iter, *offset);
  306. }
  307. static void *timer_list_next(struct seq_file *file, void *v, loff_t *offset)
  308. {
  309. struct timer_list_iter *iter = file->private;
  310. ++*offset;
  311. return move_iter(iter, 1);
  312. }
  313. static void timer_list_stop(struct seq_file *seq, void *v)
  314. {
  315. }
  316. static const struct seq_operations timer_list_sops = {
  317. .start = timer_list_start,
  318. .next = timer_list_next,
  319. .stop = timer_list_stop,
  320. .show = timer_list_show,
  321. };
  322. static int __init init_timer_list_procfs(void)
  323. {
  324. struct proc_dir_entry *pe;
  325. pe = proc_create_seq_private("timer_list", 0400, NULL, &timer_list_sops,
  326. sizeof(struct timer_list_iter), NULL);
  327. if (!pe)
  328. return -ENOMEM;
  329. return 0;
  330. }
  331. __initcall(init_timer_list_procfs);
  332. #endif