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 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_network_interface
);
76 #ifdef LTTNG_HAVE_STATEDUMP_CPU_TOPOLOGY
77 DEFINE_TRACE(lttng_statedump_cpu_topology
);
82 struct lttng_session
*session
;
83 struct task_struct
*p
;
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 char name_buf
[BDEVNAME_SIZE
];
158 p
= wrapper_disk_name(disk
, part
->partno
, name_buf
);
160 disk_part_iter_exit(&piter
);
161 class_dev_iter_exit(&iter
);
164 trace_lttng_statedump_block_device(session
,
165 part_devt(part
), name_buf
);
167 disk_part_iter_exit(&piter
);
169 class_dev_iter_exit(&iter
);
176 void lttng_enumerate_device(struct lttng_session
*session
,
177 struct net_device
*dev
)
179 struct in_device
*in_dev
;
180 struct in_ifaddr
*ifa
;
182 if (dev
->flags
& IFF_UP
) {
183 in_dev
= in_dev_get(dev
);
185 for (ifa
= in_dev
->ifa_list
; ifa
!= NULL
;
186 ifa
= ifa
->ifa_next
) {
187 trace_lttng_statedump_network_interface(
193 trace_lttng_statedump_network_interface(
199 int lttng_enumerate_network_ip_interface(struct lttng_session
*session
)
201 struct net_device
*dev
;
203 read_lock(&dev_base_lock
);
204 for_each_netdev(&init_net
, dev
)
205 lttng_enumerate_device(session
, dev
);
206 read_unlock(&dev_base_lock
);
210 #else /* CONFIG_INET */
212 int lttng_enumerate_network_ip_interface(struct lttng_session
*session
)
216 #endif /* CONFIG_INET */
219 int lttng_dump_one_fd(const void *p
, struct file
*file
, unsigned int fd
)
221 const struct lttng_fd_ctx
*ctx
= p
;
222 const char *s
= d_path(&file
->f_path
, ctx
->page
, PAGE_SIZE
);
223 unsigned int flags
= file
->f_flags
;
227 * We don't expose kernel internal flags, only userspace-visible
230 flags
&= ~FMODE_NONOTIFY
;
231 fdt
= files_fdtable(ctx
->files
);
233 * We need to check here again whether fd is within the fdt
234 * max_fds range, because we might be seeing a different
235 * files_fdtable() than iterate_fd(), assuming only RCU is
236 * protecting the read. In reality, iterate_fd() holds
237 * file_lock, which should ensure the fdt does not change while
238 * the lock is taken, but we are not aware whether this is
239 * guaranteed or not, so play safe.
241 if (fd
< fdt
->max_fds
&& lttng_close_on_exec(fd
, fdt
))
244 struct dentry
*dentry
= file
->f_path
.dentry
;
246 /* Make sure we give at least some info */
247 spin_lock(&dentry
->d_lock
);
248 trace_lttng_statedump_file_descriptor(ctx
->session
, ctx
->p
, fd
,
249 dentry
->d_name
.name
, flags
, file
->f_mode
);
250 spin_unlock(&dentry
->d_lock
);
253 trace_lttng_statedump_file_descriptor(ctx
->session
, ctx
->p
, fd
, s
,
254 flags
, file
->f_mode
);
260 void lttng_enumerate_task_fd(struct lttng_session
*session
,
261 struct task_struct
*p
, char *tmp
)
263 struct lttng_fd_ctx ctx
= { .page
= tmp
, .session
= session
, .p
= p
};
264 struct files_struct
*files
;
271 lttng_iterate_fd(files
, 0, lttng_dump_one_fd
, &ctx
);
277 int lttng_enumerate_file_descriptors(struct lttng_session
*session
)
279 struct task_struct
*p
;
282 tmp
= (char *) __get_free_page(GFP_KERNEL
);
286 /* Enumerate active file descriptors */
289 lttng_enumerate_task_fd(session
, p
, tmp
);
291 free_page((unsigned long) tmp
);
295 #ifdef LTTNG_HAVE_STATEDUMP_CPU_TOPOLOGY
297 int lttng_enumerate_cpu_topology(struct lttng_session
*session
)
300 const cpumask_t
*cpumask
= cpu_possible_mask
;
302 for (cpu
= cpumask_first(cpumask
); cpu
< nr_cpu_ids
;
303 cpu
= cpumask_next(cpu
, cpumask
)) {
304 trace_lttng_statedump_cpu_topology(session
, &cpu_data(cpu
));
311 int lttng_enumerate_cpu_topology(struct lttng_session
*session
)
319 * FIXME: we cannot take a mmap_sem while in a RCU read-side critical section
320 * (scheduling in atomic). Normally, the tasklist lock protects this kind of
321 * iteration, but it is not exported to modules.
324 void lttng_enumerate_task_vm_maps(struct lttng_session
*session
,
325 struct task_struct
*p
)
327 struct mm_struct
*mm
;
328 struct vm_area_struct
*map
;
331 /* get_task_mm does a task_lock... */
338 down_read(&mm
->mmap_sem
);
341 ino
= map
->vm_file
->lttng_f_dentry
->d_inode
->i_ino
;
344 trace_lttng_statedump_vm_map(session
, p
, map
, ino
);
347 up_read(&mm
->mmap_sem
);
353 int lttng_enumerate_vm_maps(struct lttng_session
*session
)
355 struct task_struct
*p
;
359 lttng_enumerate_task_vm_maps(session
, p
);
365 #ifdef CONFIG_LTTNG_HAS_LIST_IRQ
368 int lttng_list_interrupts(struct lttng_session
*session
)
371 unsigned long flags
= 0;
372 struct irq_desc
*desc
;
374 #define irq_to_desc wrapper_irq_to_desc
376 for_each_irq_desc(irq
, desc
) {
377 struct irqaction
*action
;
378 const char *irq_chip_name
=
379 irq_desc_get_chip(desc
)->name
? : "unnamed_irq_chip";
381 local_irq_save(flags
);
382 raw_spin_lock(&desc
->lock
);
383 for (action
= desc
->action
; action
; action
= action
->next
) {
384 trace_lttng_statedump_interrupt(session
,
385 irq
, irq_chip_name
, action
);
387 raw_spin_unlock(&desc
->lock
);
388 local_irq_restore(flags
);
395 int lttng_list_interrupts(struct lttng_session
*session
)
402 * Statedump the task's namespaces using the proc filesystem inode number as
403 * the unique identifier. The user and pid ns are nested and will be dumped
406 * Called with task lock held.
409 void lttng_statedump_process_ns(struct lttng_session
*session
,
410 struct task_struct
*p
,
411 enum lttng_thread_type type
,
412 enum lttng_execution_mode mode
,
413 enum lttng_execution_submode submode
,
414 enum lttng_process_status status
)
416 struct nsproxy
*proxy
;
417 struct pid_namespace
*pid_ns
;
418 struct user_namespace
*user_ns
;
421 * The pid and user namespaces are special, they are nested and
422 * accessed with specific functions instead of the nsproxy struct
423 * like the other namespaces.
425 pid_ns
= task_active_pid_ns(p
);
427 trace_lttng_statedump_process_pid_ns(session
, p
, pid_ns
);
428 pid_ns
= pid_ns
? pid_ns
->parent
: NULL
;
432 user_ns
= task_cred_xxx(p
, user_ns
);
434 trace_lttng_statedump_process_user_ns(session
, p
, user_ns
);
436 * trace_lttng_statedump_process_user_ns() internally
437 * checks whether user_ns is NULL. While this does not
438 * appear to be a possible return value for
439 * task_cred_xxx(), err on the safe side and check
440 * for NULL here as well to be consistent with the
441 * paranoid behavior of
442 * trace_lttng_statedump_process_user_ns().
444 user_ns
= user_ns
? user_ns
->lttng_user_ns_parent
: NULL
;
448 * Back and forth on locking strategy within Linux upstream for nsproxy.
449 * See Linux upstream commit 728dba3a39c66b3d8ac889ddbe38b5b1c264aec3
450 * "namespaces: Use task_lock and not rcu to protect nsproxy"
453 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0) || \
454 LTTNG_UBUNTU_KERNEL_RANGE(3,13,11,36, 3,14,0,0) || \
455 LTTNG_UBUNTU_KERNEL_RANGE(3,16,1,11, 3,17,0,0) || \
456 LTTNG_RHEL_KERNEL_RANGE(3,10,0,229,13,0, 3,11,0,0,0,0))
460 proxy
= task_nsproxy(p
);
463 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,6,0))
464 trace_lttng_statedump_process_cgroup_ns(session
, p
, proxy
->cgroup_ns
);
466 trace_lttng_statedump_process_ipc_ns(session
, p
, proxy
->ipc_ns
);
467 #ifndef LTTNG_MNT_NS_MISSING_HEADER
468 trace_lttng_statedump_process_mnt_ns(session
, p
, proxy
->mnt_ns
);
470 trace_lttng_statedump_process_net_ns(session
, p
, proxy
->net_ns
);
471 trace_lttng_statedump_process_uts_ns(session
, p
, proxy
->uts_ns
);
473 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0) || \
474 LTTNG_UBUNTU_KERNEL_RANGE(3,13,11,36, 3,14,0,0) || \
475 LTTNG_UBUNTU_KERNEL_RANGE(3,16,1,11, 3,17,0,0) || \
476 LTTNG_RHEL_KERNEL_RANGE(3,10,0,229,13,0, 3,11,0,0,0,0))
484 int lttng_enumerate_process_states(struct lttng_session
*session
)
486 struct task_struct
*g
, *p
;
489 for_each_process(g
) {
492 enum lttng_execution_mode mode
=
494 enum lttng_execution_submode submode
=
496 enum lttng_process_status status
;
497 enum lttng_thread_type type
;
500 if (p
->exit_state
== EXIT_ZOMBIE
)
501 status
= LTTNG_ZOMBIE
;
502 else if (p
->exit_state
== EXIT_DEAD
)
504 else if (p
->state
== TASK_RUNNING
) {
505 /* Is this a forked child that has not run yet? */
506 if (list_empty(&p
->rt
.run_list
))
507 status
= LTTNG_WAIT_FORK
;
510 * All tasks are considered as wait_cpu;
511 * the viewer will sort out if the task
512 * was really running at this time.
514 status
= LTTNG_WAIT_CPU
;
515 } else if (p
->state
&
516 (TASK_INTERRUPTIBLE
| TASK_UNINTERRUPTIBLE
)) {
517 /* Task is waiting for something to complete */
520 status
= LTTNG_UNNAMED
;
521 submode
= LTTNG_NONE
;
524 * Verification of t->mm is to filter out kernel
525 * threads; Viewer will further filter out if a
526 * user-space thread was in syscall mode or not.
529 type
= LTTNG_USER_THREAD
;
531 type
= LTTNG_KERNEL_THREAD
;
533 trace_lttng_statedump_process_state(session
,
534 p
, type
, mode
, submode
, status
);
535 lttng_statedump_process_ns(session
,
536 p
, type
, mode
, submode
, status
);
538 } while_each_thread(g
, p
);
546 void lttng_statedump_work_func(struct work_struct
*work
)
548 if (atomic_dec_and_test(&kernel_threads_to_run
))
549 /* If we are the last thread, wake up do_lttng_statedump */
550 wake_up(&statedump_wq
);
554 int do_lttng_statedump(struct lttng_session
*session
)
558 trace_lttng_statedump_start(session
);
559 ret
= lttng_enumerate_process_states(session
);
562 ret
= lttng_enumerate_file_descriptors(session
);
567 * ret = lttng_enumerate_vm_maps(session);
571 ret
= lttng_list_interrupts(session
);
574 ret
= lttng_enumerate_network_ip_interface(session
);
577 ret
= lttng_enumerate_block_devices(session
);
582 printk(KERN_WARNING
"LTTng: block device enumeration is not supported by kernel\n");
587 ret
= lttng_enumerate_cpu_topology(session
);
591 /* TODO lttng_dump_idt_table(session); */
592 /* TODO lttng_dump_softirq_vec(session); */
593 /* TODO lttng_list_modules(session); */
594 /* TODO lttng_dump_swap_files(session); */
597 * Fire off a work queue on each CPU. Their sole purpose in life
598 * is to guarantee that each CPU has been in a state where is was in
599 * syscall mode (i.e. not in a trap, an IRQ or a soft IRQ).
602 atomic_set(&kernel_threads_to_run
, num_online_cpus());
603 for_each_online_cpu(cpu
) {
604 INIT_DELAYED_WORK(&cpu_work
[cpu
], lttng_statedump_work_func
);
605 schedule_delayed_work_on(cpu
, &cpu_work
[cpu
], 0);
607 /* Wait for all threads to run */
608 __wait_event(statedump_wq
, (atomic_read(&kernel_threads_to_run
) == 0));
610 /* Our work is done */
611 trace_lttng_statedump_end(session
);
616 * Called with session mutex held.
618 int lttng_statedump_start(struct lttng_session
*session
)
620 return do_lttng_statedump(session
);
622 EXPORT_SYMBOL_GPL(lttng_statedump_start
);
625 int __init
lttng_statedump_init(void)
628 * Allow module to load even if the fixup cannot be done. This
629 * will allow seemless transition when the underlying issue fix
630 * is merged into the Linux kernel, and when tracepoint.c
631 * "tracepoint_module_notify" is turned into a static function.
633 (void) wrapper_lttng_fixup_sig(THIS_MODULE
);
637 module_init(lttng_statedump_init
);
640 void __exit
lttng_statedump_exit(void)
644 module_exit(lttng_statedump_exit
);
646 MODULE_LICENSE("GPL and additional rights");
647 MODULE_AUTHOR("Jean-Hugues Deschenes");
648 MODULE_DESCRIPTION("LTTng statedump provider");
649 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
650 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
651 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
652 LTTNG_MODULES_EXTRAVERSION
);