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