1 /* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1)
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/lttng-module
54 #define TRACE_INCLUDE_FILE lttng-statedump
55 #define LTTNG_INSTRUMENTATION
56 #include <instrumentation/events/lttng-module/lttng-statedump.h>
58 LTTNG_DEFINE_TRACE(lttng_statedump_block_device
,
59 TP_PROTO(struct lttng_session
*session
,
60 dev_t dev
, const char *diskname
),
61 TP_ARGS(session
, dev
, diskname
));
63 LTTNG_DEFINE_TRACE(lttng_statedump_end
,
64 TP_PROTO(struct lttng_session
*session
),
67 LTTNG_DEFINE_TRACE(lttng_statedump_interrupt
,
68 TP_PROTO(struct lttng_session
*session
,
69 unsigned int irq
, const char *chip_name
,
70 struct irqaction
*action
),
71 TP_ARGS(session
, irq
, chip_name
, action
));
73 LTTNG_DEFINE_TRACE(lttng_statedump_file_descriptor
,
74 TP_PROTO(struct lttng_session
*session
,
75 struct files_struct
*files
,
76 int fd
, const char *filename
,
77 unsigned int flags
, fmode_t fmode
),
78 TP_ARGS(session
, files
, fd
, filename
, flags
, fmode
));
80 LTTNG_DEFINE_TRACE(lttng_statedump_start
,
81 TP_PROTO(struct lttng_session
*session
),
84 LTTNG_DEFINE_TRACE(lttng_statedump_process_state
,
85 TP_PROTO(struct lttng_session
*session
,
86 struct task_struct
*p
,
87 int type
, int mode
, int submode
, int status
,
88 struct files_struct
*files
),
89 TP_ARGS(session
, p
, type
, mode
, submode
, status
, files
));
91 LTTNG_DEFINE_TRACE(lttng_statedump_process_pid_ns
,
92 TP_PROTO(struct lttng_session
*session
,
93 struct task_struct
*p
,
94 struct pid_namespace
*pid_ns
),
95 TP_ARGS(session
, p
, pid_ns
));
97 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,6,0))
98 LTTNG_DEFINE_TRACE(lttng_statedump_process_cgroup_ns
,
99 TP_PROTO(struct lttng_session
*session
,
100 struct task_struct
*p
,
101 struct cgroup_namespace
*cgroup_ns
),
102 TP_ARGS(session
, p
, cgroup_ns
));
105 LTTNG_DEFINE_TRACE(lttng_statedump_process_ipc_ns
,
106 TP_PROTO(struct lttng_session
*session
,
107 struct task_struct
*p
,
108 struct ipc_namespace
*ipc_ns
),
109 TP_ARGS(session
, p
, ipc_ns
));
111 #ifndef LTTNG_MNT_NS_MISSING_HEADER
112 LTTNG_DEFINE_TRACE(lttng_statedump_process_mnt_ns
,
113 TP_PROTO(struct lttng_session
*session
,
114 struct task_struct
*p
,
115 struct mnt_namespace
*mnt_ns
),
116 TP_ARGS(session
, p
, mnt_ns
));
119 LTTNG_DEFINE_TRACE(lttng_statedump_process_net_ns
,
120 TP_PROTO(struct lttng_session
*session
,
121 struct task_struct
*p
,
123 TP_ARGS(session
, p
, net_ns
));
125 LTTNG_DEFINE_TRACE(lttng_statedump_process_user_ns
,
126 TP_PROTO(struct lttng_session
*session
,
127 struct task_struct
*p
,
128 struct user_namespace
*user_ns
),
129 TP_ARGS(session
, p
, user_ns
));
131 LTTNG_DEFINE_TRACE(lttng_statedump_process_uts_ns
,
132 TP_PROTO(struct lttng_session
*session
,
133 struct task_struct
*p
,
134 struct uts_namespace
*uts_ns
),
135 TP_ARGS(session
, p
, uts_ns
));
137 LTTNG_DEFINE_TRACE(lttng_statedump_network_interface
,
138 TP_PROTO(struct lttng_session
*session
,
139 struct net_device
*dev
, struct in_ifaddr
*ifa
),
140 TP_ARGS(session
, dev
, ifa
));
142 #ifdef LTTNG_HAVE_STATEDUMP_CPU_TOPOLOGY
143 LTTNG_DEFINE_TRACE(lttng_statedump_cpu_topology
,
144 TP_PROTO(struct lttng_session
*session
, struct cpuinfo_x86
*c
),
145 TP_ARGS(session
, c
));
148 struct lttng_fd_ctx
{
150 struct lttng_session
*session
;
151 struct files_struct
*files
;
155 * Protected by the trace lock.
157 static struct delayed_work cpu_work
[NR_CPUS
];
158 static DECLARE_WAIT_QUEUE_HEAD(statedump_wq
);
159 static atomic_t kernel_threads_to_run
;
161 enum lttng_thread_type
{
162 LTTNG_USER_THREAD
= 0,
163 LTTNG_KERNEL_THREAD
= 1,
166 enum lttng_execution_mode
{
172 LTTNG_MODE_UNKNOWN
= 5,
175 enum lttng_execution_submode
{
180 enum lttng_process_status
{
192 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,11,0))
194 #define LTTNG_PART_STRUCT_TYPE struct block_device
197 int lttng_get_part_name(struct gendisk
*disk
, struct block_device
*part
, char *name_buf
)
201 p
= bdevname(part
, name_buf
);
209 dev_t
lttng_get_part_devt(struct block_device
*part
)
216 #define LTTNG_PART_STRUCT_TYPE struct hd_struct
219 int lttng_get_part_name(struct gendisk
*disk
, struct hd_struct
*part
, char *name_buf
)
222 struct block_device bdev
;
225 * Create a partial 'struct blockdevice' to use
226 * 'bdevname()' which is a simple wrapper over
227 * 'disk_name()' but has the honor to be EXPORT_SYMBOL.
232 p
= bdevname(&bdev
, name_buf
);
240 dev_t
lttng_get_part_devt(struct hd_struct
*part
)
242 return part_devt(part
);
247 int lttng_enumerate_block_devices(struct lttng_session
*session
)
249 struct class *ptr_block_class
;
250 struct device_type
*ptr_disk_type
;
251 struct class_dev_iter iter
;
255 ptr_block_class
= wrapper_get_block_class();
256 if (!ptr_block_class
) {
260 ptr_disk_type
= wrapper_get_disk_type();
261 if (!ptr_disk_type
) {
265 class_dev_iter_init(&iter
, ptr_block_class
, NULL
, ptr_disk_type
);
266 while ((dev
= class_dev_iter_next(&iter
))) {
267 struct disk_part_iter piter
;
268 struct gendisk
*disk
= dev_to_disk(dev
);
269 LTTNG_PART_STRUCT_TYPE
*part
;
272 * Don't show empty devices or things that have been
275 if (get_capacity(disk
) == 0 ||
276 (disk
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
))
280 * The original 'disk_part_iter_init' returns void, but our
281 * wrapper can fail to lookup the original symbol.
283 if (wrapper_disk_part_iter_init(&piter
, disk
, DISK_PITER_INCL_PART0
) < 0) {
288 while ((part
= wrapper_disk_part_iter_next(&piter
))) {
289 char name_buf
[BDEVNAME_SIZE
];
291 if (lttng_get_part_name(disk
, part
, name_buf
) == -ENOSYS
) {
292 wrapper_disk_part_iter_exit(&piter
);
296 trace_lttng_statedump_block_device(session
,
297 lttng_get_part_devt(part
), name_buf
);
299 wrapper_disk_part_iter_exit(&piter
);
302 class_dev_iter_exit(&iter
);
310 void lttng_enumerate_device(struct lttng_session
*session
,
311 struct net_device
*dev
)
313 struct in_device
*in_dev
;
314 struct in_ifaddr
*ifa
;
316 if (dev
->flags
& IFF_UP
) {
317 in_dev
= in_dev_get(dev
);
319 for (ifa
= in_dev
->ifa_list
; ifa
!= NULL
;
320 ifa
= ifa
->ifa_next
) {
321 trace_lttng_statedump_network_interface(
327 trace_lttng_statedump_network_interface(
333 int lttng_enumerate_network_ip_interface(struct lttng_session
*session
)
335 struct net_device
*dev
;
337 read_lock(&dev_base_lock
);
338 for_each_netdev(&init_net
, dev
)
339 lttng_enumerate_device(session
, dev
);
340 read_unlock(&dev_base_lock
);
344 #else /* CONFIG_INET */
346 int lttng_enumerate_network_ip_interface(struct lttng_session
*session
)
350 #endif /* CONFIG_INET */
353 int lttng_dump_one_fd(const void *p
, struct file
*file
, unsigned int fd
)
355 const struct lttng_fd_ctx
*ctx
= p
;
356 const char *s
= d_path(&file
->f_path
, ctx
->page
, PAGE_SIZE
);
357 unsigned int flags
= file
->f_flags
;
361 * We don't expose kernel internal flags, only userspace-visible
364 flags
&= ~FMODE_NONOTIFY
;
365 fdt
= files_fdtable(ctx
->files
);
367 * We need to check here again whether fd is within the fdt
368 * max_fds range, because we might be seeing a different
369 * files_fdtable() than iterate_fd(), assuming only RCU is
370 * protecting the read. In reality, iterate_fd() holds
371 * file_lock, which should ensure the fdt does not change while
372 * the lock is taken, but we are not aware whether this is
373 * guaranteed or not, so play safe.
375 if (fd
< fdt
->max_fds
&& lttng_close_on_exec(fd
, fdt
))
378 struct dentry
*dentry
= file
->f_path
.dentry
;
380 /* Make sure we give at least some info */
381 spin_lock(&dentry
->d_lock
);
382 trace_lttng_statedump_file_descriptor(ctx
->session
,
383 ctx
->files
, fd
, dentry
->d_name
.name
, flags
,
385 spin_unlock(&dentry
->d_lock
);
388 trace_lttng_statedump_file_descriptor(ctx
->session
,
389 ctx
->files
, fd
, s
, flags
, file
->f_mode
);
394 /* Called with task lock held. */
396 void lttng_enumerate_files(struct lttng_session
*session
,
397 struct files_struct
*files
,
400 struct lttng_fd_ctx ctx
= { .page
= tmp
, .session
= session
, .files
= files
, };
402 lttng_iterate_fd(files
, 0, lttng_dump_one_fd
, &ctx
);
405 #ifdef LTTNG_HAVE_STATEDUMP_CPU_TOPOLOGY
407 int lttng_enumerate_cpu_topology(struct lttng_session
*session
)
410 const cpumask_t
*cpumask
= cpu_possible_mask
;
412 for (cpu
= cpumask_first(cpumask
); cpu
< nr_cpu_ids
;
413 cpu
= cpumask_next(cpu
, cpumask
)) {
414 trace_lttng_statedump_cpu_topology(session
, &cpu_data(cpu
));
421 int lttng_enumerate_cpu_topology(struct lttng_session
*session
)
429 * FIXME: we cannot take a mmap_sem while in a RCU read-side critical section
430 * (scheduling in atomic). Normally, the tasklist lock protects this kind of
431 * iteration, but it is not exported to modules.
434 void lttng_enumerate_task_vm_maps(struct lttng_session
*session
,
435 struct task_struct
*p
)
437 struct mm_struct
*mm
;
438 struct vm_area_struct
*map
;
441 /* get_task_mm does a task_lock... */
448 down_read(&mm
->mmap_sem
);
451 ino
= map
->vm_file
->lttng_f_dentry
->d_inode
->i_ino
;
454 trace_lttng_statedump_vm_map(session
, p
, map
, ino
);
457 up_read(&mm
->mmap_sem
);
463 int lttng_enumerate_vm_maps(struct lttng_session
*session
)
465 struct task_struct
*p
;
469 lttng_enumerate_task_vm_maps(session
, p
);
475 #ifdef CONFIG_LTTNG_HAS_LIST_IRQ
478 int lttng_list_interrupts(struct lttng_session
*session
)
481 unsigned long flags
= 0;
482 struct irq_desc
*desc
;
484 #define irq_to_desc wrapper_irq_to_desc
486 for_each_irq_desc(irq
, desc
) {
487 struct irqaction
*action
;
488 const char *irq_chip_name
=
489 irq_desc_get_chip(desc
)->name
? : "unnamed_irq_chip";
491 local_irq_save(flags
);
492 raw_spin_lock(&desc
->lock
);
493 for (action
= desc
->action
; action
; action
= action
->next
) {
494 trace_lttng_statedump_interrupt(session
,
495 irq
, irq_chip_name
, action
);
497 raw_spin_unlock(&desc
->lock
);
498 local_irq_restore(flags
);
505 int lttng_list_interrupts(struct lttng_session
*session
)
512 * Statedump the task's namespaces using the proc filesystem inode number as
513 * the unique identifier. The user and pid ns are nested and will be dumped
516 * Called with task lock held.
519 void lttng_statedump_process_ns(struct lttng_session
*session
,
520 struct task_struct
*p
,
521 enum lttng_thread_type type
,
522 enum lttng_execution_mode mode
,
523 enum lttng_execution_submode submode
,
524 enum lttng_process_status status
)
526 struct nsproxy
*proxy
;
527 struct pid_namespace
*pid_ns
;
528 struct user_namespace
*user_ns
;
531 * The pid and user namespaces are special, they are nested and
532 * accessed with specific functions instead of the nsproxy struct
533 * like the other namespaces.
535 pid_ns
= task_active_pid_ns(p
);
537 trace_lttng_statedump_process_pid_ns(session
, p
, pid_ns
);
538 pid_ns
= pid_ns
? pid_ns
->parent
: NULL
;
542 user_ns
= task_cred_xxx(p
, user_ns
);
544 trace_lttng_statedump_process_user_ns(session
, p
, user_ns
);
546 * trace_lttng_statedump_process_user_ns() internally
547 * checks whether user_ns is NULL. While this does not
548 * appear to be a possible return value for
549 * task_cred_xxx(), err on the safe side and check
550 * for NULL here as well to be consistent with the
551 * paranoid behavior of
552 * trace_lttng_statedump_process_user_ns().
554 user_ns
= user_ns
? user_ns
->lttng_user_ns_parent
: NULL
;
558 * Back and forth on locking strategy within Linux upstream for nsproxy.
559 * See Linux upstream commit 728dba3a39c66b3d8ac889ddbe38b5b1c264aec3
560 * "namespaces: Use task_lock and not rcu to protect nsproxy"
563 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(3,17,0) || \
564 LTTNG_UBUNTU_KERNEL_RANGE(3,13,11,36, 3,14,0,0) || \
565 LTTNG_UBUNTU_KERNEL_RANGE(3,16,1,11, 3,17,0,0) || \
566 LTTNG_RHEL_KERNEL_RANGE(3,10,0,229,13,0, 3,11,0,0,0,0))
570 proxy
= task_nsproxy(p
);
573 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,6,0))
574 trace_lttng_statedump_process_cgroup_ns(session
, p
, proxy
->cgroup_ns
);
576 trace_lttng_statedump_process_ipc_ns(session
, p
, proxy
->ipc_ns
);
577 #ifndef LTTNG_MNT_NS_MISSING_HEADER
578 trace_lttng_statedump_process_mnt_ns(session
, p
, proxy
->mnt_ns
);
580 trace_lttng_statedump_process_net_ns(session
, p
, proxy
->net_ns
);
581 trace_lttng_statedump_process_uts_ns(session
, p
, proxy
->uts_ns
);
583 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(3,17,0) || \
584 LTTNG_UBUNTU_KERNEL_RANGE(3,13,11,36, 3,14,0,0) || \
585 LTTNG_UBUNTU_KERNEL_RANGE(3,16,1,11, 3,17,0,0) || \
586 LTTNG_RHEL_KERNEL_RANGE(3,10,0,229,13,0, 3,11,0,0,0,0))
594 int lttng_enumerate_process_states(struct lttng_session
*session
)
596 struct task_struct
*g
, *p
;
599 tmp
= (char *) __get_free_page(GFP_KERNEL
);
604 for_each_process(g
) {
605 struct files_struct
*prev_files
= NULL
;
609 enum lttng_execution_mode mode
=
611 enum lttng_execution_submode submode
=
613 enum lttng_process_status status
;
614 enum lttng_thread_type type
;
615 struct files_struct
*files
;
618 if (p
->exit_state
== EXIT_ZOMBIE
)
619 status
= LTTNG_ZOMBIE
;
620 else if (p
->exit_state
== EXIT_DEAD
)
622 else if (p
->state
== TASK_RUNNING
) {
623 /* Is this a forked child that has not run yet? */
624 if (list_empty(&p
->rt
.run_list
))
625 status
= LTTNG_WAIT_FORK
;
628 * All tasks are considered as wait_cpu;
629 * the viewer will sort out if the task
630 * was really running at this time.
632 status
= LTTNG_WAIT_CPU
;
633 } else if (p
->state
&
634 (TASK_INTERRUPTIBLE
| TASK_UNINTERRUPTIBLE
)) {
635 /* Task is waiting for something to complete */
638 status
= LTTNG_UNNAMED
;
639 submode
= LTTNG_NONE
;
642 * Verification of t->mm is to filter out kernel
643 * threads; Viewer will further filter out if a
644 * user-space thread was in syscall mode or not.
647 type
= LTTNG_USER_THREAD
;
649 type
= LTTNG_KERNEL_THREAD
;
652 trace_lttng_statedump_process_state(session
,
653 p
, type
, mode
, submode
, status
, files
);
654 lttng_statedump_process_ns(session
,
655 p
, type
, mode
, submode
, status
);
657 * As an optimisation for the common case, do not
658 * repeat information for the same files_struct in
659 * two consecutive threads. This is the common case
660 * for threads sharing the same fd table. RCU guarantees
661 * that the same files_struct pointer is not re-used
662 * throughout processes/threads iteration.
664 if (files
&& files
!= prev_files
) {
665 lttng_enumerate_files(session
, files
, tmp
);
669 } while_each_thread(g
, p
);
673 free_page((unsigned long) tmp
);
679 void lttng_statedump_work_func(struct work_struct
*work
)
681 if (atomic_dec_and_test(&kernel_threads_to_run
))
682 /* If we are the last thread, wake up do_lttng_statedump */
683 wake_up(&statedump_wq
);
687 int do_lttng_statedump(struct lttng_session
*session
)
691 trace_lttng_statedump_start(session
);
692 ret
= lttng_enumerate_process_states(session
);
697 * ret = lttng_enumerate_vm_maps(session);
701 ret
= lttng_list_interrupts(session
);
704 ret
= lttng_enumerate_network_ip_interface(session
);
707 ret
= lttng_enumerate_block_devices(session
);
712 printk(KERN_WARNING
"LTTng: block device enumeration is not supported by kernel\n");
717 ret
= lttng_enumerate_cpu_topology(session
);
721 /* TODO lttng_dump_idt_table(session); */
722 /* TODO lttng_dump_softirq_vec(session); */
723 /* TODO lttng_list_modules(session); */
724 /* TODO lttng_dump_swap_files(session); */
727 * Fire off a work queue on each CPU. Their sole purpose in life
728 * is to guarantee that each CPU has been in a state where is was in
729 * syscall mode (i.e. not in a trap, an IRQ or a soft IRQ).
732 atomic_set(&kernel_threads_to_run
, num_online_cpus());
733 for_each_online_cpu(cpu
) {
734 INIT_DELAYED_WORK(&cpu_work
[cpu
], lttng_statedump_work_func
);
735 schedule_delayed_work_on(cpu
, &cpu_work
[cpu
], 0);
737 /* Wait for all threads to run */
738 __wait_event(statedump_wq
, (atomic_read(&kernel_threads_to_run
) == 0));
740 /* Our work is done */
741 trace_lttng_statedump_end(session
);
746 * Called with session mutex held.
748 int lttng_statedump_start(struct lttng_session
*session
)
750 return do_lttng_statedump(session
);
752 EXPORT_SYMBOL_GPL(lttng_statedump_start
);
755 int __init
lttng_statedump_init(void)
758 * Allow module to load even if the fixup cannot be done. This
759 * will allow seemless transition when the underlying issue fix
760 * is merged into the Linux kernel, and when tracepoint.c
761 * "tracepoint_module_notify" is turned into a static function.
763 (void) wrapper_lttng_fixup_sig(THIS_MODULE
);
767 module_init(lttng_statedump_init
);
770 void __exit
lttng_statedump_exit(void)
774 module_exit(lttng_statedump_exit
);
776 MODULE_LICENSE("GPL and additional rights");
777 MODULE_AUTHOR("Jean-Hugues Deschenes");
778 MODULE_DESCRIPTION("LTTng statedump provider");
779 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
780 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
781 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
782 LTTNG_MODULES_EXTRAVERSION
);