1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
5 * Linux Trace Toolkit Next Generation Kernel State Dump
7 * Copyright 2005 Jean-Hugues Deschenes <jean-hugues.deschenes@polymtl.ca>
8 * Copyright 2006-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
11 * Eric Clement: Add listing of network IP interface
12 * 2006, 2007 Mathieu Desnoyers Fix kernel threads
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/netlink.h>
19 #include <linux/inet.h>
21 #include <linux/kthread.h>
22 #include <linux/proc_fs.h>
23 #include <linux/file.h>
24 #include <linux/interrupt.h>
25 #include <linux/irqnr.h>
26 #include <linux/cpu.h>
27 #include <linux/netdevice.h>
28 #include <linux/inetdevice.h>
29 #include <linux/sched.h>
31 #include <linux/swap.h>
32 #include <linux/wait.h>
33 #include <linux/mutex.h>
34 #include <linux/device.h>
36 #include <lttng/events.h>
37 #include <lttng/tracer.h>
38 #include <wrapper/irqdesc.h>
39 #include <wrapper/fdtable.h>
40 #include <wrapper/namespace.h>
41 #include <wrapper/irq.h>
42 #include <wrapper/tracepoint.h>
43 #include <wrapper/genhd.h>
44 #include <wrapper/file.h>
45 #include <wrapper/fdtable.h>
47 #ifdef CONFIG_LTTNG_HAS_LIST_IRQ
48 #include <linux/irq.h>
51 /* Define the tracepoints, but do not build the probes */
52 #define CREATE_TRACE_POINTS
53 #define TRACE_INCLUDE_PATH instrumentation/events
54 #define TRACE_INCLUDE_FILE lttng-statedump
55 #define LTTNG_INSTRUMENTATION
56 #include <instrumentation/events/lttng-statedump.h>
58 DEFINE_TRACE(lttng_statedump_block_device
);
59 DEFINE_TRACE(lttng_statedump_end
);
60 DEFINE_TRACE(lttng_statedump_interrupt
);
61 DEFINE_TRACE(lttng_statedump_file_descriptor
);
62 DEFINE_TRACE(lttng_statedump_start
);
63 DEFINE_TRACE(lttng_statedump_process_state
);
64 DEFINE_TRACE(lttng_statedump_process_pid_ns
);
65 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,6,0))
66 DEFINE_TRACE(lttng_statedump_process_cgroup_ns
);
68 DEFINE_TRACE(lttng_statedump_process_ipc_ns
);
69 #ifndef LTTNG_MNT_NS_MISSING_HEADER
70 DEFINE_TRACE(lttng_statedump_process_mnt_ns
);
72 DEFINE_TRACE(lttng_statedump_process_net_ns
);
73 DEFINE_TRACE(lttng_statedump_process_user_ns
);
74 DEFINE_TRACE(lttng_statedump_process_uts_ns
);
75 DEFINE_TRACE(lttng_statedump_process_time_ns
);
76 DEFINE_TRACE(lttng_statedump_network_interface
);
77 #ifdef LTTNG_HAVE_STATEDUMP_CPU_TOPOLOGY
78 DEFINE_TRACE(lttng_statedump_cpu_topology
);
83 struct lttng_session
*session
;
84 struct files_struct
*files
;
88 * Protected by the trace lock.
90 static struct delayed_work cpu_work
[NR_CPUS
];
91 static DECLARE_WAIT_QUEUE_HEAD(statedump_wq
);
92 static atomic_t kernel_threads_to_run
;
94 enum lttng_thread_type
{
95 LTTNG_USER_THREAD
= 0,
96 LTTNG_KERNEL_THREAD
= 1,
99 enum lttng_execution_mode
{
105 LTTNG_MODE_UNKNOWN
= 5,
108 enum lttng_execution_submode
{
113 enum lttng_process_status
{
125 int lttng_enumerate_block_devices(struct lttng_session
*session
)
127 struct class *ptr_block_class
;
128 struct device_type
*ptr_disk_type
;
129 struct class_dev_iter iter
;
132 ptr_block_class
= wrapper_get_block_class();
133 if (!ptr_block_class
)
135 ptr_disk_type
= wrapper_get_disk_type();
136 if (!ptr_disk_type
) {
139 class_dev_iter_init(&iter
, ptr_block_class
, NULL
, ptr_disk_type
);
140 while ((dev
= class_dev_iter_next(&iter
))) {
141 struct disk_part_iter piter
;
142 struct gendisk
*disk
= dev_to_disk(dev
);
143 struct hd_struct
*part
;
146 * Don't show empty devices or things that have been
149 if (get_capacity(disk
) == 0 ||
150 (disk
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
))
153 disk_part_iter_init(&piter
, disk
, DISK_PITER_INCL_PART0
);
154 while ((part
= disk_part_iter_next(&piter
))) {
155 struct block_device bdev
;
156 char name_buf
[BDEVNAME_SIZE
];
160 * Create a partial 'struct blockdevice' to use
161 * 'bdevname()' which is a simple wrapper over
162 * 'disk_name()' but has the honor to be EXPORT_SYMBOL.
167 p
= bdevname(&bdev
, name_buf
);
169 disk_part_iter_exit(&piter
);
170 class_dev_iter_exit(&iter
);
173 trace_lttng_statedump_block_device(session
,
174 part_devt(part
), name_buf
);
176 disk_part_iter_exit(&piter
);
178 class_dev_iter_exit(&iter
);
185 void lttng_enumerate_device(struct lttng_session
*session
,
186 struct net_device
*dev
)
188 struct in_device
*in_dev
;
189 struct in_ifaddr
*ifa
;
191 if (dev
->flags
& IFF_UP
) {
192 in_dev
= in_dev_get(dev
);
194 for (ifa
= in_dev
->ifa_list
; ifa
!= NULL
;
195 ifa
= ifa
->ifa_next
) {
196 trace_lttng_statedump_network_interface(
202 trace_lttng_statedump_network_interface(
208 int lttng_enumerate_network_ip_interface(struct lttng_session
*session
)
210 struct net_device
*dev
;
212 read_lock(&dev_base_lock
);
213 for_each_netdev(&init_net
, dev
)
214 lttng_enumerate_device(session
, dev
);
215 read_unlock(&dev_base_lock
);
219 #else /* CONFIG_INET */
221 int lttng_enumerate_network_ip_interface(struct lttng_session
*session
)
225 #endif /* CONFIG_INET */
228 int lttng_dump_one_fd(const void *p
, struct file
*file
, unsigned int fd
)
230 const struct lttng_fd_ctx
*ctx
= p
;
231 const char *s
= d_path(&file
->f_path
, ctx
->page
, PAGE_SIZE
);
232 unsigned int flags
= file
->f_flags
;
236 * We don't expose kernel internal flags, only userspace-visible
239 flags
&= ~FMODE_NONOTIFY
;
240 fdt
= files_fdtable(ctx
->files
);
242 * We need to check here again whether fd is within the fdt
243 * max_fds range, because we might be seeing a different
244 * files_fdtable() than iterate_fd(), assuming only RCU is
245 * protecting the read. In reality, iterate_fd() holds
246 * file_lock, which should ensure the fdt does not change while
247 * the lock is taken, but we are not aware whether this is
248 * guaranteed or not, so play safe.
250 if (fd
< fdt
->max_fds
&& lttng_close_on_exec(fd
, fdt
))
253 struct dentry
*dentry
= file
->f_path
.dentry
;
255 /* Make sure we give at least some info */
256 spin_lock(&dentry
->d_lock
);
257 trace_lttng_statedump_file_descriptor(ctx
->session
,
258 ctx
->files
, fd
, dentry
->d_name
.name
, flags
,
260 spin_unlock(&dentry
->d_lock
);
263 trace_lttng_statedump_file_descriptor(ctx
->session
,
264 ctx
->files
, fd
, s
, flags
, file
->f_mode
);
269 /* Called with task lock held. */
271 void lttng_enumerate_files(struct lttng_session
*session
,
272 struct files_struct
*files
,
275 struct lttng_fd_ctx ctx
= { .page
= tmp
, .session
= session
, .files
= files
, };
277 lttng_iterate_fd(files
, 0, lttng_dump_one_fd
, &ctx
);
280 #ifdef LTTNG_HAVE_STATEDUMP_CPU_TOPOLOGY
282 int lttng_enumerate_cpu_topology(struct lttng_session
*session
)
285 const cpumask_t
*cpumask
= cpu_possible_mask
;
287 for (cpu
= cpumask_first(cpumask
); cpu
< nr_cpu_ids
;
288 cpu
= cpumask_next(cpu
, cpumask
)) {
289 trace_lttng_statedump_cpu_topology(session
, &cpu_data(cpu
));
296 int lttng_enumerate_cpu_topology(struct lttng_session
*session
)
304 * FIXME: we cannot take a mmap_sem while in a RCU read-side critical section
305 * (scheduling in atomic). Normally, the tasklist lock protects this kind of
306 * iteration, but it is not exported to modules.
309 void lttng_enumerate_task_vm_maps(struct lttng_session
*session
,
310 struct task_struct
*p
)
312 struct mm_struct
*mm
;
313 struct vm_area_struct
*map
;
316 /* get_task_mm does a task_lock... */
323 down_read(&mm
->mmap_sem
);
326 ino
= map
->vm_file
->lttng_f_dentry
->d_inode
->i_ino
;
329 trace_lttng_statedump_vm_map(session
, p
, map
, ino
);
332 up_read(&mm
->mmap_sem
);
338 int lttng_enumerate_vm_maps(struct lttng_session
*session
)
340 struct task_struct
*p
;
344 lttng_enumerate_task_vm_maps(session
, p
);
350 #ifdef CONFIG_LTTNG_HAS_LIST_IRQ
353 int lttng_list_interrupts(struct lttng_session
*session
)
356 unsigned long flags
= 0;
357 struct irq_desc
*desc
;
359 #define irq_to_desc wrapper_irq_to_desc
361 for_each_irq_desc(irq
, desc
) {
362 struct irqaction
*action
;
363 const char *irq_chip_name
=
364 irq_desc_get_chip(desc
)->name
? : "unnamed_irq_chip";
366 local_irq_save(flags
);
367 raw_spin_lock(&desc
->lock
);
368 for (action
= desc
->action
; action
; action
= action
->next
) {
369 trace_lttng_statedump_interrupt(session
,
370 irq
, irq_chip_name
, action
);
372 raw_spin_unlock(&desc
->lock
);
373 local_irq_restore(flags
);
380 int lttng_list_interrupts(struct lttng_session
*session
)
387 * Statedump the task's namespaces using the proc filesystem inode number as
388 * the unique identifier. The user and pid ns are nested and will be dumped
391 * Called with task lock held.
394 void lttng_statedump_process_ns(struct lttng_session
*session
,
395 struct task_struct
*p
,
396 enum lttng_thread_type type
,
397 enum lttng_execution_mode mode
,
398 enum lttng_execution_submode submode
,
399 enum lttng_process_status status
)
401 struct nsproxy
*proxy
;
402 struct pid_namespace
*pid_ns
;
403 struct user_namespace
*user_ns
;
406 * The pid and user namespaces are special, they are nested and
407 * accessed with specific functions instead of the nsproxy struct
408 * like the other namespaces.
410 pid_ns
= task_active_pid_ns(p
);
412 trace_lttng_statedump_process_pid_ns(session
, p
, pid_ns
);
413 pid_ns
= pid_ns
? pid_ns
->parent
: NULL
;
417 user_ns
= task_cred_xxx(p
, user_ns
);
419 trace_lttng_statedump_process_user_ns(session
, p
, user_ns
);
421 * trace_lttng_statedump_process_user_ns() internally
422 * checks whether user_ns is NULL. While this does not
423 * appear to be a possible return value for
424 * task_cred_xxx(), err on the safe side and check
425 * for NULL here as well to be consistent with the
426 * paranoid behavior of
427 * trace_lttng_statedump_process_user_ns().
429 user_ns
= user_ns
? user_ns
->lttng_user_ns_parent
: NULL
;
433 * Back and forth on locking strategy within Linux upstream for nsproxy.
434 * See Linux upstream commit 728dba3a39c66b3d8ac889ddbe38b5b1c264aec3
435 * "namespaces: Use task_lock and not rcu to protect nsproxy"
438 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0) || \
439 LTTNG_UBUNTU_KERNEL_RANGE(3,13,11,36, 3,14,0,0) || \
440 LTTNG_UBUNTU_KERNEL_RANGE(3,16,1,11, 3,17,0,0) || \
441 LTTNG_RHEL_KERNEL_RANGE(3,10,0,229,13,0, 3,11,0,0,0,0))
445 proxy
= task_nsproxy(p
);
448 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,6,0))
449 trace_lttng_statedump_process_cgroup_ns(session
, p
, proxy
->cgroup_ns
);
451 trace_lttng_statedump_process_ipc_ns(session
, p
, proxy
->ipc_ns
);
452 #ifndef LTTNG_MNT_NS_MISSING_HEADER
453 trace_lttng_statedump_process_mnt_ns(session
, p
, proxy
->mnt_ns
);
455 trace_lttng_statedump_process_net_ns(session
, p
, proxy
->net_ns
);
456 trace_lttng_statedump_process_uts_ns(session
, p
, proxy
->uts_ns
);
457 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0))
458 trace_lttng_statedump_process_time_ns(session
, p
, proxy
->time_ns
);
461 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0) || \
462 LTTNG_UBUNTU_KERNEL_RANGE(3,13,11,36, 3,14,0,0) || \
463 LTTNG_UBUNTU_KERNEL_RANGE(3,16,1,11, 3,17,0,0) || \
464 LTTNG_RHEL_KERNEL_RANGE(3,10,0,229,13,0, 3,11,0,0,0,0))
472 int lttng_enumerate_process_states(struct lttng_session
*session
)
474 struct task_struct
*g
, *p
;
477 tmp
= (char *) __get_free_page(GFP_KERNEL
);
482 for_each_process(g
) {
483 struct files_struct
*prev_files
= NULL
;
487 enum lttng_execution_mode mode
=
489 enum lttng_execution_submode submode
=
491 enum lttng_process_status status
;
492 enum lttng_thread_type type
;
493 struct files_struct
*files
;
496 if (p
->exit_state
== EXIT_ZOMBIE
)
497 status
= LTTNG_ZOMBIE
;
498 else if (p
->exit_state
== EXIT_DEAD
)
500 else if (p
->state
== TASK_RUNNING
) {
501 /* Is this a forked child that has not run yet? */
502 if (list_empty(&p
->rt
.run_list
))
503 status
= LTTNG_WAIT_FORK
;
506 * All tasks are considered as wait_cpu;
507 * the viewer will sort out if the task
508 * was really running at this time.
510 status
= LTTNG_WAIT_CPU
;
511 } else if (p
->state
&
512 (TASK_INTERRUPTIBLE
| TASK_UNINTERRUPTIBLE
)) {
513 /* Task is waiting for something to complete */
516 status
= LTTNG_UNNAMED
;
517 submode
= LTTNG_NONE
;
520 * Verification of t->mm is to filter out kernel
521 * threads; Viewer will further filter out if a
522 * user-space thread was in syscall mode or not.
525 type
= LTTNG_USER_THREAD
;
527 type
= LTTNG_KERNEL_THREAD
;
530 trace_lttng_statedump_process_state(session
,
531 p
, type
, mode
, submode
, status
, files
);
532 lttng_statedump_process_ns(session
,
533 p
, type
, mode
, submode
, status
);
535 * As an optimisation for the common case, do not
536 * repeat information for the same files_struct in
537 * two consecutive threads. This is the common case
538 * for threads sharing the same fd table. RCU guarantees
539 * that the same files_struct pointer is not re-used
540 * throughout processes/threads iteration.
542 if (files
&& files
!= prev_files
) {
543 lttng_enumerate_files(session
, files
, tmp
);
547 } while_each_thread(g
, p
);
551 free_page((unsigned long) tmp
);
557 void lttng_statedump_work_func(struct work_struct
*work
)
559 if (atomic_dec_and_test(&kernel_threads_to_run
))
560 /* If we are the last thread, wake up do_lttng_statedump */
561 wake_up(&statedump_wq
);
565 int do_lttng_statedump(struct lttng_session
*session
)
569 trace_lttng_statedump_start(session
);
570 ret
= lttng_enumerate_process_states(session
);
575 * ret = lttng_enumerate_vm_maps(session);
579 ret
= lttng_list_interrupts(session
);
582 ret
= lttng_enumerate_network_ip_interface(session
);
585 ret
= lttng_enumerate_block_devices(session
);
590 printk(KERN_WARNING
"LTTng: block device enumeration is not supported by kernel\n");
595 ret
= lttng_enumerate_cpu_topology(session
);
599 /* TODO lttng_dump_idt_table(session); */
600 /* TODO lttng_dump_softirq_vec(session); */
601 /* TODO lttng_list_modules(session); */
602 /* TODO lttng_dump_swap_files(session); */
605 * Fire off a work queue on each CPU. Their sole purpose in life
606 * is to guarantee that each CPU has been in a state where is was in
607 * syscall mode (i.e. not in a trap, an IRQ or a soft IRQ).
610 atomic_set(&kernel_threads_to_run
, num_online_cpus());
611 for_each_online_cpu(cpu
) {
612 INIT_DELAYED_WORK(&cpu_work
[cpu
], lttng_statedump_work_func
);
613 schedule_delayed_work_on(cpu
, &cpu_work
[cpu
], 0);
615 /* Wait for all threads to run */
616 __wait_event(statedump_wq
, (atomic_read(&kernel_threads_to_run
) == 0));
618 /* Our work is done */
619 trace_lttng_statedump_end(session
);
624 * Called with session mutex held.
626 int lttng_statedump_start(struct lttng_session
*session
)
628 return do_lttng_statedump(session
);
630 EXPORT_SYMBOL_GPL(lttng_statedump_start
);
633 int __init
lttng_statedump_init(void)
636 * Allow module to load even if the fixup cannot be done. This
637 * will allow seemless transition when the underlying issue fix
638 * is merged into the Linux kernel, and when tracepoint.c
639 * "tracepoint_module_notify" is turned into a static function.
641 (void) wrapper_lttng_fixup_sig(THIS_MODULE
);
645 module_init(lttng_statedump_init
);
648 void __exit
lttng_statedump_exit(void)
652 module_exit(lttng_statedump_exit
);
654 MODULE_LICENSE("GPL and additional rights");
655 MODULE_AUTHOR("Jean-Hugues Deschenes");
656 MODULE_DESCRIPTION("LTTng statedump provider");
657 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
658 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
659 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
660 LTTNG_MODULES_EXTRAVERSION
);