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