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