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 (LINUX_VERSION_CODE >= 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 int lttng_enumerate_block_devices(struct lttng_session
*session
)
194 struct class *ptr_block_class
;
195 struct device_type
*ptr_disk_type
;
196 struct class_dev_iter iter
;
199 ptr_block_class
= wrapper_get_block_class();
200 if (!ptr_block_class
)
202 ptr_disk_type
= wrapper_get_disk_type();
203 if (!ptr_disk_type
) {
206 class_dev_iter_init(&iter
, ptr_block_class
, NULL
, ptr_disk_type
);
207 while ((dev
= class_dev_iter_next(&iter
))) {
208 struct disk_part_iter piter
;
209 struct gendisk
*disk
= dev_to_disk(dev
);
210 struct hd_struct
*part
;
213 * Don't show empty devices or things that have been
216 if (get_capacity(disk
) == 0 ||
217 (disk
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
))
220 disk_part_iter_init(&piter
, disk
, DISK_PITER_INCL_PART0
);
221 while ((part
= disk_part_iter_next(&piter
))) {
222 struct block_device bdev
;
223 char name_buf
[BDEVNAME_SIZE
];
227 * Create a partial 'struct blockdevice' to use
228 * 'bdevname()' which is a simple wrapper over
229 * 'disk_name()' but has the honor to be EXPORT_SYMBOL.
234 p
= bdevname(&bdev
, name_buf
);
236 disk_part_iter_exit(&piter
);
237 class_dev_iter_exit(&iter
);
240 trace_lttng_statedump_block_device(session
,
241 part_devt(part
), name_buf
);
243 disk_part_iter_exit(&piter
);
245 class_dev_iter_exit(&iter
);
252 void lttng_enumerate_device(struct lttng_session
*session
,
253 struct net_device
*dev
)
255 struct in_device
*in_dev
;
256 struct in_ifaddr
*ifa
;
258 if (dev
->flags
& IFF_UP
) {
259 in_dev
= in_dev_get(dev
);
261 for (ifa
= in_dev
->ifa_list
; ifa
!= NULL
;
262 ifa
= ifa
->ifa_next
) {
263 trace_lttng_statedump_network_interface(
269 trace_lttng_statedump_network_interface(
275 int lttng_enumerate_network_ip_interface(struct lttng_session
*session
)
277 struct net_device
*dev
;
279 read_lock(&dev_base_lock
);
280 for_each_netdev(&init_net
, dev
)
281 lttng_enumerate_device(session
, dev
);
282 read_unlock(&dev_base_lock
);
286 #else /* CONFIG_INET */
288 int lttng_enumerate_network_ip_interface(struct lttng_session
*session
)
292 #endif /* CONFIG_INET */
295 int lttng_dump_one_fd(const void *p
, struct file
*file
, unsigned int fd
)
297 const struct lttng_fd_ctx
*ctx
= p
;
298 const char *s
= d_path(&file
->f_path
, ctx
->page
, PAGE_SIZE
);
299 unsigned int flags
= file
->f_flags
;
303 * We don't expose kernel internal flags, only userspace-visible
306 flags
&= ~FMODE_NONOTIFY
;
307 fdt
= files_fdtable(ctx
->files
);
309 * We need to check here again whether fd is within the fdt
310 * max_fds range, because we might be seeing a different
311 * files_fdtable() than iterate_fd(), assuming only RCU is
312 * protecting the read. In reality, iterate_fd() holds
313 * file_lock, which should ensure the fdt does not change while
314 * the lock is taken, but we are not aware whether this is
315 * guaranteed or not, so play safe.
317 if (fd
< fdt
->max_fds
&& lttng_close_on_exec(fd
, fdt
))
320 struct dentry
*dentry
= file
->f_path
.dentry
;
322 /* Make sure we give at least some info */
323 spin_lock(&dentry
->d_lock
);
324 trace_lttng_statedump_file_descriptor(ctx
->session
,
325 ctx
->files
, fd
, dentry
->d_name
.name
, flags
,
327 spin_unlock(&dentry
->d_lock
);
330 trace_lttng_statedump_file_descriptor(ctx
->session
,
331 ctx
->files
, fd
, s
, flags
, file
->f_mode
);
336 /* Called with task lock held. */
338 void lttng_enumerate_files(struct lttng_session
*session
,
339 struct files_struct
*files
,
342 struct lttng_fd_ctx ctx
= { .page
= tmp
, .session
= session
, .files
= files
, };
344 lttng_iterate_fd(files
, 0, lttng_dump_one_fd
, &ctx
);
347 #ifdef LTTNG_HAVE_STATEDUMP_CPU_TOPOLOGY
349 int lttng_enumerate_cpu_topology(struct lttng_session
*session
)
352 const cpumask_t
*cpumask
= cpu_possible_mask
;
354 for (cpu
= cpumask_first(cpumask
); cpu
< nr_cpu_ids
;
355 cpu
= cpumask_next(cpu
, cpumask
)) {
356 trace_lttng_statedump_cpu_topology(session
, &cpu_data(cpu
));
363 int lttng_enumerate_cpu_topology(struct lttng_session
*session
)
371 * FIXME: we cannot take a mmap_sem while in a RCU read-side critical section
372 * (scheduling in atomic). Normally, the tasklist lock protects this kind of
373 * iteration, but it is not exported to modules.
376 void lttng_enumerate_task_vm_maps(struct lttng_session
*session
,
377 struct task_struct
*p
)
379 struct mm_struct
*mm
;
380 struct vm_area_struct
*map
;
383 /* get_task_mm does a task_lock... */
390 down_read(&mm
->mmap_sem
);
393 ino
= map
->vm_file
->lttng_f_dentry
->d_inode
->i_ino
;
396 trace_lttng_statedump_vm_map(session
, p
, map
, ino
);
399 up_read(&mm
->mmap_sem
);
405 int lttng_enumerate_vm_maps(struct lttng_session
*session
)
407 struct task_struct
*p
;
411 lttng_enumerate_task_vm_maps(session
, p
);
417 #ifdef CONFIG_LTTNG_HAS_LIST_IRQ
420 int lttng_list_interrupts(struct lttng_session
*session
)
423 unsigned long flags
= 0;
424 struct irq_desc
*desc
;
426 #define irq_to_desc wrapper_irq_to_desc
428 for_each_irq_desc(irq
, desc
) {
429 struct irqaction
*action
;
430 const char *irq_chip_name
=
431 irq_desc_get_chip(desc
)->name
? : "unnamed_irq_chip";
433 local_irq_save(flags
);
434 raw_spin_lock(&desc
->lock
);
435 for (action
= desc
->action
; action
; action
= action
->next
) {
436 trace_lttng_statedump_interrupt(session
,
437 irq
, irq_chip_name
, action
);
439 raw_spin_unlock(&desc
->lock
);
440 local_irq_restore(flags
);
447 int lttng_list_interrupts(struct lttng_session
*session
)
454 * Statedump the task's namespaces using the proc filesystem inode number as
455 * the unique identifier. The user and pid ns are nested and will be dumped
458 * Called with task lock held.
461 void lttng_statedump_process_ns(struct lttng_session
*session
,
462 struct task_struct
*p
,
463 enum lttng_thread_type type
,
464 enum lttng_execution_mode mode
,
465 enum lttng_execution_submode submode
,
466 enum lttng_process_status status
)
468 struct nsproxy
*proxy
;
469 struct pid_namespace
*pid_ns
;
470 struct user_namespace
*user_ns
;
473 * The pid and user namespaces are special, they are nested and
474 * accessed with specific functions instead of the nsproxy struct
475 * like the other namespaces.
477 pid_ns
= task_active_pid_ns(p
);
479 trace_lttng_statedump_process_pid_ns(session
, p
, pid_ns
);
480 pid_ns
= pid_ns
? pid_ns
->parent
: NULL
;
484 user_ns
= task_cred_xxx(p
, user_ns
);
486 trace_lttng_statedump_process_user_ns(session
, p
, user_ns
);
488 * trace_lttng_statedump_process_user_ns() internally
489 * checks whether user_ns is NULL. While this does not
490 * appear to be a possible return value for
491 * task_cred_xxx(), err on the safe side and check
492 * for NULL here as well to be consistent with the
493 * paranoid behavior of
494 * trace_lttng_statedump_process_user_ns().
496 user_ns
= user_ns
? user_ns
->lttng_user_ns_parent
: NULL
;
500 * Back and forth on locking strategy within Linux upstream for nsproxy.
501 * See Linux upstream commit 728dba3a39c66b3d8ac889ddbe38b5b1c264aec3
502 * "namespaces: Use task_lock and not rcu to protect nsproxy"
505 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0) || \
506 LTTNG_UBUNTU_KERNEL_RANGE(3,13,11,36, 3,14,0,0) || \
507 LTTNG_UBUNTU_KERNEL_RANGE(3,16,1,11, 3,17,0,0) || \
508 LTTNG_RHEL_KERNEL_RANGE(3,10,0,229,13,0, 3,11,0,0,0,0))
512 proxy
= task_nsproxy(p
);
515 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,6,0))
516 trace_lttng_statedump_process_cgroup_ns(session
, p
, proxy
->cgroup_ns
);
518 trace_lttng_statedump_process_ipc_ns(session
, p
, proxy
->ipc_ns
);
519 #ifndef LTTNG_MNT_NS_MISSING_HEADER
520 trace_lttng_statedump_process_mnt_ns(session
, p
, proxy
->mnt_ns
);
522 trace_lttng_statedump_process_net_ns(session
, p
, proxy
->net_ns
);
523 trace_lttng_statedump_process_uts_ns(session
, p
, proxy
->uts_ns
);
525 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0) || \
526 LTTNG_UBUNTU_KERNEL_RANGE(3,13,11,36, 3,14,0,0) || \
527 LTTNG_UBUNTU_KERNEL_RANGE(3,16,1,11, 3,17,0,0) || \
528 LTTNG_RHEL_KERNEL_RANGE(3,10,0,229,13,0, 3,11,0,0,0,0))
536 int lttng_enumerate_process_states(struct lttng_session
*session
)
538 struct task_struct
*g
, *p
;
541 tmp
= (char *) __get_free_page(GFP_KERNEL
);
546 for_each_process(g
) {
547 struct files_struct
*prev_files
= NULL
;
551 enum lttng_execution_mode mode
=
553 enum lttng_execution_submode submode
=
555 enum lttng_process_status status
;
556 enum lttng_thread_type type
;
557 struct files_struct
*files
;
560 if (p
->exit_state
== EXIT_ZOMBIE
)
561 status
= LTTNG_ZOMBIE
;
562 else if (p
->exit_state
== EXIT_DEAD
)
564 else if (p
->state
== TASK_RUNNING
) {
565 /* Is this a forked child that has not run yet? */
566 if (list_empty(&p
->rt
.run_list
))
567 status
= LTTNG_WAIT_FORK
;
570 * All tasks are considered as wait_cpu;
571 * the viewer will sort out if the task
572 * was really running at this time.
574 status
= LTTNG_WAIT_CPU
;
575 } else if (p
->state
&
576 (TASK_INTERRUPTIBLE
| TASK_UNINTERRUPTIBLE
)) {
577 /* Task is waiting for something to complete */
580 status
= LTTNG_UNNAMED
;
581 submode
= LTTNG_NONE
;
584 * Verification of t->mm is to filter out kernel
585 * threads; Viewer will further filter out if a
586 * user-space thread was in syscall mode or not.
589 type
= LTTNG_USER_THREAD
;
591 type
= LTTNG_KERNEL_THREAD
;
594 trace_lttng_statedump_process_state(session
,
595 p
, type
, mode
, submode
, status
, files
);
596 lttng_statedump_process_ns(session
,
597 p
, type
, mode
, submode
, status
);
599 * As an optimisation for the common case, do not
600 * repeat information for the same files_struct in
601 * two consecutive threads. This is the common case
602 * for threads sharing the same fd table. RCU guarantees
603 * that the same files_struct pointer is not re-used
604 * throughout processes/threads iteration.
606 if (files
&& files
!= prev_files
) {
607 lttng_enumerate_files(session
, files
, tmp
);
611 } while_each_thread(g
, p
);
615 free_page((unsigned long) tmp
);
621 void lttng_statedump_work_func(struct work_struct
*work
)
623 if (atomic_dec_and_test(&kernel_threads_to_run
))
624 /* If we are the last thread, wake up do_lttng_statedump */
625 wake_up(&statedump_wq
);
629 int do_lttng_statedump(struct lttng_session
*session
)
633 trace_lttng_statedump_start(session
);
634 ret
= lttng_enumerate_process_states(session
);
639 * ret = lttng_enumerate_vm_maps(session);
643 ret
= lttng_list_interrupts(session
);
646 ret
= lttng_enumerate_network_ip_interface(session
);
649 ret
= lttng_enumerate_block_devices(session
);
654 printk(KERN_WARNING
"LTTng: block device enumeration is not supported by kernel\n");
659 ret
= lttng_enumerate_cpu_topology(session
);
663 /* TODO lttng_dump_idt_table(session); */
664 /* TODO lttng_dump_softirq_vec(session); */
665 /* TODO lttng_list_modules(session); */
666 /* TODO lttng_dump_swap_files(session); */
669 * Fire off a work queue on each CPU. Their sole purpose in life
670 * is to guarantee that each CPU has been in a state where is was in
671 * syscall mode (i.e. not in a trap, an IRQ or a soft IRQ).
674 atomic_set(&kernel_threads_to_run
, num_online_cpus());
675 for_each_online_cpu(cpu
) {
676 INIT_DELAYED_WORK(&cpu_work
[cpu
], lttng_statedump_work_func
);
677 schedule_delayed_work_on(cpu
, &cpu_work
[cpu
], 0);
679 /* Wait for all threads to run */
680 __wait_event(statedump_wq
, (atomic_read(&kernel_threads_to_run
) == 0));
682 /* Our work is done */
683 trace_lttng_statedump_end(session
);
688 * Called with session mutex held.
690 int lttng_statedump_start(struct lttng_session
*session
)
692 return do_lttng_statedump(session
);
694 EXPORT_SYMBOL_GPL(lttng_statedump_start
);
697 int __init
lttng_statedump_init(void)
700 * Allow module to load even if the fixup cannot be done. This
701 * will allow seemless transition when the underlying issue fix
702 * is merged into the Linux kernel, and when tracepoint.c
703 * "tracepoint_module_notify" is turned into a static function.
705 (void) wrapper_lttng_fixup_sig(THIS_MODULE
);
709 module_init(lttng_statedump_init
);
712 void __exit
lttng_statedump_exit(void)
716 module_exit(lttng_statedump_exit
);
718 MODULE_LICENSE("GPL and additional rights");
719 MODULE_AUTHOR("Jean-Hugues Deschenes");
720 MODULE_DESCRIPTION("LTTng statedump provider");
721 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
722 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
723 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
724 LTTNG_MODULES_EXTRAVERSION
);