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