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/fdtable.h>
39 #include <wrapper/namespace.h>
40 #include <wrapper/irq.h>
41 #include <wrapper/tracepoint.h>
42 #include <wrapper/genhd.h>
43 #include <wrapper/file.h>
44 #include <wrapper/fdtable.h>
46 #ifdef CONFIG_LTTNG_HAS_LIST_IRQ
47 #include <linux/irq.h>
50 /* Define the tracepoints, but do not build the probes */
51 #define CREATE_TRACE_POINTS
52 #define TRACE_INCLUDE_PATH instrumentation/events/lttng-module
53 #define TRACE_INCLUDE_FILE lttng-statedump
54 #define LTTNG_INSTRUMENTATION
55 #include <instrumentation/events/lttng-module/lttng-statedump.h>
57 DEFINE_TRACE(lttng_statedump_block_device
);
58 DEFINE_TRACE(lttng_statedump_end
);
59 DEFINE_TRACE(lttng_statedump_interrupt
);
60 DEFINE_TRACE(lttng_statedump_file_descriptor
);
61 DEFINE_TRACE(lttng_statedump_start
);
62 DEFINE_TRACE(lttng_statedump_process_state
);
63 DEFINE_TRACE(lttng_statedump_process_pid_ns
);
64 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,6,0))
65 DEFINE_TRACE(lttng_statedump_process_cgroup_ns
);
67 DEFINE_TRACE(lttng_statedump_process_ipc_ns
);
68 #ifndef LTTNG_MNT_NS_MISSING_HEADER
69 DEFINE_TRACE(lttng_statedump_process_mnt_ns
);
71 DEFINE_TRACE(lttng_statedump_process_net_ns
);
72 DEFINE_TRACE(lttng_statedump_process_user_ns
);
73 DEFINE_TRACE(lttng_statedump_process_uts_ns
);
74 DEFINE_TRACE(lttng_statedump_network_interface
);
75 #ifdef LTTNG_HAVE_STATEDUMP_CPU_TOPOLOGY
76 DEFINE_TRACE(lttng_statedump_cpu_topology
);
81 struct lttng_session
*session
;
82 struct files_struct
*files
;
86 * Protected by the trace lock.
88 static struct delayed_work cpu_work
[NR_CPUS
];
89 static DECLARE_WAIT_QUEUE_HEAD(statedump_wq
);
90 static atomic_t kernel_threads_to_run
;
92 enum lttng_thread_type
{
93 LTTNG_USER_THREAD
= 0,
94 LTTNG_KERNEL_THREAD
= 1,
97 enum lttng_execution_mode
{
103 LTTNG_MODE_UNKNOWN
= 5,
106 enum lttng_execution_submode
{
111 enum lttng_process_status
{
123 int lttng_enumerate_block_devices(struct lttng_session
*session
)
125 struct class *ptr_block_class
;
126 struct device_type
*ptr_disk_type
;
127 struct class_dev_iter iter
;
130 ptr_block_class
= wrapper_get_block_class();
131 if (!ptr_block_class
)
133 ptr_disk_type
= wrapper_get_disk_type();
134 if (!ptr_disk_type
) {
137 class_dev_iter_init(&iter
, ptr_block_class
, NULL
, ptr_disk_type
);
138 while ((dev
= class_dev_iter_next(&iter
))) {
139 struct disk_part_iter piter
;
140 struct gendisk
*disk
= dev_to_disk(dev
);
141 struct hd_struct
*part
;
144 * Don't show empty devices or things that have been
147 if (get_capacity(disk
) == 0 ||
148 (disk
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
))
151 disk_part_iter_init(&piter
, disk
, DISK_PITER_INCL_PART0
);
152 while ((part
= disk_part_iter_next(&piter
))) {
153 char name_buf
[BDEVNAME_SIZE
];
156 p
= wrapper_disk_name(disk
, part
->partno
, name_buf
);
158 disk_part_iter_exit(&piter
);
159 class_dev_iter_exit(&iter
);
162 trace_lttng_statedump_block_device(session
,
163 part_devt(part
), name_buf
);
165 disk_part_iter_exit(&piter
);
167 class_dev_iter_exit(&iter
);
174 void lttng_enumerate_device(struct lttng_session
*session
,
175 struct net_device
*dev
)
177 struct in_device
*in_dev
;
178 struct in_ifaddr
*ifa
;
180 if (dev
->flags
& IFF_UP
) {
181 in_dev
= in_dev_get(dev
);
183 for (ifa
= in_dev
->ifa_list
; ifa
!= NULL
;
184 ifa
= ifa
->ifa_next
) {
185 trace_lttng_statedump_network_interface(
191 trace_lttng_statedump_network_interface(
197 int lttng_enumerate_network_ip_interface(struct lttng_session
*session
)
199 struct net_device
*dev
;
201 read_lock(&dev_base_lock
);
202 for_each_netdev(&init_net
, dev
)
203 lttng_enumerate_device(session
, dev
);
204 read_unlock(&dev_base_lock
);
208 #else /* CONFIG_INET */
210 int lttng_enumerate_network_ip_interface(struct lttng_session
*session
)
214 #endif /* CONFIG_INET */
217 int lttng_dump_one_fd(const void *p
, struct file
*file
, unsigned int fd
)
219 const struct lttng_fd_ctx
*ctx
= p
;
220 const char *s
= d_path(&file
->f_path
, ctx
->page
, PAGE_SIZE
);
221 unsigned int flags
= file
->f_flags
;
225 * We don't expose kernel internal flags, only userspace-visible
228 flags
&= ~FMODE_NONOTIFY
;
229 fdt
= files_fdtable(ctx
->files
);
231 * We need to check here again whether fd is within the fdt
232 * max_fds range, because we might be seeing a different
233 * files_fdtable() than iterate_fd(), assuming only RCU is
234 * protecting the read. In reality, iterate_fd() holds
235 * file_lock, which should ensure the fdt does not change while
236 * the lock is taken, but we are not aware whether this is
237 * guaranteed or not, so play safe.
239 if (fd
< fdt
->max_fds
&& lttng_close_on_exec(fd
, fdt
))
242 struct dentry
*dentry
= file
->f_path
.dentry
;
244 /* Make sure we give at least some info */
245 spin_lock(&dentry
->d_lock
);
246 trace_lttng_statedump_file_descriptor(ctx
->session
,
247 ctx
->files
, fd
, dentry
->d_name
.name
, flags
,
249 spin_unlock(&dentry
->d_lock
);
252 trace_lttng_statedump_file_descriptor(ctx
->session
,
253 ctx
->files
, fd
, s
, flags
, file
->f_mode
);
258 /* Called with task lock held. */
260 void lttng_enumerate_files(struct lttng_session
*session
,
261 struct files_struct
*files
,
264 struct lttng_fd_ctx ctx
= { .page
= tmp
, .session
= session
, .files
= files
, };
266 lttng_iterate_fd(files
, 0, lttng_dump_one_fd
, &ctx
);
269 #ifdef LTTNG_HAVE_STATEDUMP_CPU_TOPOLOGY
271 int lttng_enumerate_cpu_topology(struct lttng_session
*session
)
274 const cpumask_t
*cpumask
= cpu_possible_mask
;
276 for (cpu
= cpumask_first(cpumask
); cpu
< nr_cpu_ids
;
277 cpu
= cpumask_next(cpu
, cpumask
)) {
278 trace_lttng_statedump_cpu_topology(session
, &cpu_data(cpu
));
285 int lttng_enumerate_cpu_topology(struct lttng_session
*session
)
293 * FIXME: we cannot take a mmap_sem while in a RCU read-side critical section
294 * (scheduling in atomic). Normally, the tasklist lock protects this kind of
295 * iteration, but it is not exported to modules.
298 void lttng_enumerate_task_vm_maps(struct lttng_session
*session
,
299 struct task_struct
*p
)
301 struct mm_struct
*mm
;
302 struct vm_area_struct
*map
;
305 /* get_task_mm does a task_lock... */
312 down_read(&mm
->mmap_sem
);
315 ino
= map
->vm_file
->lttng_f_dentry
->d_inode
->i_ino
;
318 trace_lttng_statedump_vm_map(session
, p
, map
, ino
);
321 up_read(&mm
->mmap_sem
);
327 int lttng_enumerate_vm_maps(struct lttng_session
*session
)
329 struct task_struct
*p
;
333 lttng_enumerate_task_vm_maps(session
, p
);
339 #ifdef CONFIG_LTTNG_HAS_LIST_IRQ
342 int lttng_list_interrupts(struct lttng_session
*session
)
345 unsigned long flags
= 0;
346 struct irq_desc
*desc
;
349 for_each_irq_desc(irq
, desc
) {
350 struct irqaction
*action
;
351 const char *irq_chip_name
=
352 irq_desc_get_chip(desc
)->name
? : "unnamed_irq_chip";
354 local_irq_save(flags
);
355 raw_spin_lock(&desc
->lock
);
356 for (action
= desc
->action
; action
; action
= action
->next
) {
357 trace_lttng_statedump_interrupt(session
,
358 irq
, irq_chip_name
, action
);
360 raw_spin_unlock(&desc
->lock
);
361 local_irq_restore(flags
);
367 int lttng_list_interrupts(struct lttng_session
*session
)
374 * Statedump the task's namespaces using the proc filesystem inode number as
375 * the unique identifier. The user and pid ns are nested and will be dumped
378 * Called with task lock held.
381 void lttng_statedump_process_ns(struct lttng_session
*session
,
382 struct task_struct
*p
,
383 enum lttng_thread_type type
,
384 enum lttng_execution_mode mode
,
385 enum lttng_execution_submode submode
,
386 enum lttng_process_status status
)
388 struct nsproxy
*proxy
;
389 struct pid_namespace
*pid_ns
;
390 struct user_namespace
*user_ns
;
393 * The pid and user namespaces are special, they are nested and
394 * accessed with specific functions instead of the nsproxy struct
395 * like the other namespaces.
397 pid_ns
= task_active_pid_ns(p
);
399 trace_lttng_statedump_process_pid_ns(session
, p
, pid_ns
);
400 pid_ns
= pid_ns
? pid_ns
->parent
: NULL
;
404 user_ns
= task_cred_xxx(p
, user_ns
);
406 trace_lttng_statedump_process_user_ns(session
, p
, user_ns
);
408 * trace_lttng_statedump_process_user_ns() internally
409 * checks whether user_ns is NULL. While this does not
410 * appear to be a possible return value for
411 * task_cred_xxx(), err on the safe side and check
412 * for NULL here as well to be consistent with the
413 * paranoid behavior of
414 * trace_lttng_statedump_process_user_ns().
416 user_ns
= user_ns
? user_ns
->lttng_user_ns_parent
: NULL
;
420 * Back and forth on locking strategy within Linux upstream for nsproxy.
421 * See Linux upstream commit 728dba3a39c66b3d8ac889ddbe38b5b1c264aec3
422 * "namespaces: Use task_lock and not rcu to protect nsproxy"
425 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0) || \
426 LTTNG_UBUNTU_KERNEL_RANGE(3,13,11,36, 3,14,0,0) || \
427 LTTNG_UBUNTU_KERNEL_RANGE(3,16,1,11, 3,17,0,0) || \
428 LTTNG_RHEL_KERNEL_RANGE(3,10,0,229,13,0, 3,11,0,0,0,0))
432 proxy
= task_nsproxy(p
);
435 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,6,0))
436 trace_lttng_statedump_process_cgroup_ns(session
, p
, proxy
->cgroup_ns
);
438 trace_lttng_statedump_process_ipc_ns(session
, p
, proxy
->ipc_ns
);
439 #ifndef LTTNG_MNT_NS_MISSING_HEADER
440 trace_lttng_statedump_process_mnt_ns(session
, p
, proxy
->mnt_ns
);
442 trace_lttng_statedump_process_net_ns(session
, p
, proxy
->net_ns
);
443 trace_lttng_statedump_process_uts_ns(session
, p
, proxy
->uts_ns
);
445 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0) || \
446 LTTNG_UBUNTU_KERNEL_RANGE(3,13,11,36, 3,14,0,0) || \
447 LTTNG_UBUNTU_KERNEL_RANGE(3,16,1,11, 3,17,0,0) || \
448 LTTNG_RHEL_KERNEL_RANGE(3,10,0,229,13,0, 3,11,0,0,0,0))
456 int lttng_enumerate_process_states(struct lttng_session
*session
)
458 struct task_struct
*g
, *p
;
461 tmp
= (char *) __get_free_page(GFP_KERNEL
);
466 for_each_process(g
) {
467 struct files_struct
*prev_files
= NULL
;
471 enum lttng_execution_mode mode
=
473 enum lttng_execution_submode submode
=
475 enum lttng_process_status status
;
476 enum lttng_thread_type type
;
477 struct files_struct
*files
;
480 if (p
->exit_state
== EXIT_ZOMBIE
)
481 status
= LTTNG_ZOMBIE
;
482 else if (p
->exit_state
== EXIT_DEAD
)
484 else if (p
->state
== TASK_RUNNING
) {
485 /* Is this a forked child that has not run yet? */
486 if (list_empty(&p
->rt
.run_list
))
487 status
= LTTNG_WAIT_FORK
;
490 * All tasks are considered as wait_cpu;
491 * the viewer will sort out if the task
492 * was really running at this time.
494 status
= LTTNG_WAIT_CPU
;
495 } else if (p
->state
&
496 (TASK_INTERRUPTIBLE
| TASK_UNINTERRUPTIBLE
)) {
497 /* Task is waiting for something to complete */
500 status
= LTTNG_UNNAMED
;
501 submode
= LTTNG_NONE
;
504 * Verification of t->mm is to filter out kernel
505 * threads; Viewer will further filter out if a
506 * user-space thread was in syscall mode or not.
509 type
= LTTNG_USER_THREAD
;
511 type
= LTTNG_KERNEL_THREAD
;
514 trace_lttng_statedump_process_state(session
,
515 p
, type
, mode
, submode
, status
, files
);
516 lttng_statedump_process_ns(session
,
517 p
, type
, mode
, submode
, status
);
519 * As an optimisation for the common case, do not
520 * repeat information for the same files_struct in
521 * two consecutive threads. This is the common case
522 * for threads sharing the same fd table. RCU guarantees
523 * that the same files_struct pointer is not re-used
524 * throughout processes/threads iteration.
526 if (files
&& files
!= prev_files
) {
527 lttng_enumerate_files(session
, files
, tmp
);
531 } while_each_thread(g
, p
);
535 free_page((unsigned long) tmp
);
541 void lttng_statedump_work_func(struct work_struct
*work
)
543 if (atomic_dec_and_test(&kernel_threads_to_run
))
544 /* If we are the last thread, wake up do_lttng_statedump */
545 wake_up(&statedump_wq
);
549 int do_lttng_statedump(struct lttng_session
*session
)
553 trace_lttng_statedump_start(session
);
554 ret
= lttng_enumerate_process_states(session
);
559 * ret = lttng_enumerate_vm_maps(session);
563 ret
= lttng_list_interrupts(session
);
566 ret
= lttng_enumerate_network_ip_interface(session
);
569 ret
= lttng_enumerate_block_devices(session
);
574 printk(KERN_WARNING
"LTTng: block device enumeration is not supported by kernel\n");
579 ret
= lttng_enumerate_cpu_topology(session
);
583 /* TODO lttng_dump_idt_table(session); */
584 /* TODO lttng_dump_softirq_vec(session); */
585 /* TODO lttng_list_modules(session); */
586 /* TODO lttng_dump_swap_files(session); */
589 * Fire off a work queue on each CPU. Their sole purpose in life
590 * is to guarantee that each CPU has been in a state where is was in
591 * syscall mode (i.e. not in a trap, an IRQ or a soft IRQ).
594 atomic_set(&kernel_threads_to_run
, num_online_cpus());
595 for_each_online_cpu(cpu
) {
596 INIT_DELAYED_WORK(&cpu_work
[cpu
], lttng_statedump_work_func
);
597 schedule_delayed_work_on(cpu
, &cpu_work
[cpu
], 0);
599 /* Wait for all threads to run */
600 __wait_event(statedump_wq
, (atomic_read(&kernel_threads_to_run
) == 0));
602 /* Our work is done */
603 trace_lttng_statedump_end(session
);
608 * Called with session mutex held.
610 int lttng_statedump_start(struct lttng_session
*session
)
612 return do_lttng_statedump(session
);
614 EXPORT_SYMBOL_GPL(lttng_statedump_start
);
617 int __init
lttng_statedump_init(void)
620 * Allow module to load even if the fixup cannot be done. This
621 * will allow seemless transition when the underlying issue fix
622 * is merged into the Linux kernel, and when tracepoint.c
623 * "tracepoint_module_notify" is turned into a static function.
625 (void) wrapper_lttng_fixup_sig(THIS_MODULE
);
629 module_init(lttng_statedump_init
);
632 void __exit
lttng_statedump_exit(void)
636 module_exit(lttng_statedump_exit
);
638 MODULE_LICENSE("GPL and additional rights");
639 MODULE_AUTHOR("Jean-Hugues Deschenes");
640 MODULE_DESCRIPTION("LTTng statedump provider");
641 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
642 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
643 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
644 LTTNG_MODULES_EXTRAVERSION
);