eefc0b8fb2f11525d5c9befad3a7d2ba5f3c0e01
[lttng-modules.git] / lttng-statedump-impl.c
1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
2 *
3 * lttng-statedump.c
4 *
5 * Linux Trace Toolkit Next Generation Kernel State Dump
6 *
7 * Copyright 2005 Jean-Hugues Deschenes <jean-hugues.deschenes@polymtl.ca>
8 * Copyright 2006-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 *
10 * Changes:
11 * Eric Clement: Add listing of network IP interface
12 * 2006, 2007 Mathieu Desnoyers Fix kernel threads
13 * Various updates
14 */
15
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/netlink.h>
19 #include <linux/inet.h>
20 #include <linux/ip.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>
30 #include <linux/mm.h>
31 #include <linux/swap.h>
32 #include <linux/wait.h>
33 #include <linux/mutex.h>
34 #include <linux/device.h>
35 #include <linux/fdtable.h>
36
37 #include <lttng-events.h>
38 #include <lttng-tracer.h>
39 #include <wrapper/namespace.h>
40 #include <wrapper/irq.h>
41 #include <wrapper/genhd.h>
42 #include <wrapper/file.h>
43
44 #ifdef CONFIG_LTTNG_HAS_LIST_IRQ
45 #include <linux/irq.h>
46 #endif
47
48 /* Define the tracepoints, but do not build the probes */
49 #define CREATE_TRACE_POINTS
50 #define TRACE_INCLUDE_PATH instrumentation/events/lttng-module
51 #define TRACE_INCLUDE_FILE lttng-statedump
52 #define LTTNG_INSTRUMENTATION
53 #include <instrumentation/events/lttng-module/lttng-statedump.h>
54
55 DEFINE_TRACE(lttng_statedump_block_device);
56 DEFINE_TRACE(lttng_statedump_end);
57 DEFINE_TRACE(lttng_statedump_interrupt);
58 DEFINE_TRACE(lttng_statedump_file_descriptor);
59 DEFINE_TRACE(lttng_statedump_start);
60 DEFINE_TRACE(lttng_statedump_process_state);
61 DEFINE_TRACE(lttng_statedump_process_pid_ns);
62 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,6,0))
63 DEFINE_TRACE(lttng_statedump_process_cgroup_ns);
64 #endif
65 DEFINE_TRACE(lttng_statedump_process_ipc_ns);
66 #ifndef LTTNG_MNT_NS_MISSING_HEADER
67 DEFINE_TRACE(lttng_statedump_process_mnt_ns);
68 #endif
69 DEFINE_TRACE(lttng_statedump_process_net_ns);
70 DEFINE_TRACE(lttng_statedump_process_user_ns);
71 DEFINE_TRACE(lttng_statedump_process_uts_ns);
72 DEFINE_TRACE(lttng_statedump_network_interface);
73 #ifdef LTTNG_HAVE_STATEDUMP_CPU_TOPOLOGY
74 DEFINE_TRACE(lttng_statedump_cpu_topology);
75 #endif
76
77 struct lttng_fd_ctx {
78 char *page;
79 struct lttng_session *session;
80 struct files_struct *files;
81 };
82
83 /*
84 * Protected by the trace lock.
85 */
86 static struct delayed_work cpu_work[NR_CPUS];
87 static DECLARE_WAIT_QUEUE_HEAD(statedump_wq);
88 static atomic_t kernel_threads_to_run;
89
90 enum lttng_thread_type {
91 LTTNG_USER_THREAD = 0,
92 LTTNG_KERNEL_THREAD = 1,
93 };
94
95 enum lttng_execution_mode {
96 LTTNG_USER_MODE = 0,
97 LTTNG_SYSCALL = 1,
98 LTTNG_TRAP = 2,
99 LTTNG_IRQ = 3,
100 LTTNG_SOFTIRQ = 4,
101 LTTNG_MODE_UNKNOWN = 5,
102 };
103
104 enum lttng_execution_submode {
105 LTTNG_NONE = 0,
106 LTTNG_UNKNOWN = 1,
107 };
108
109 enum lttng_process_status {
110 LTTNG_UNNAMED = 0,
111 LTTNG_WAIT_FORK = 1,
112 LTTNG_WAIT_CPU = 2,
113 LTTNG_EXIT = 3,
114 LTTNG_ZOMBIE = 4,
115 LTTNG_WAIT = 5,
116 LTTNG_RUN = 6,
117 LTTNG_DEAD = 7,
118 };
119
120 static
121 int lttng_enumerate_block_devices(struct lttng_session *session)
122 {
123 struct class *ptr_block_class;
124 struct device_type *ptr_disk_type;
125 struct class_dev_iter iter;
126 struct device *dev;
127
128 ptr_block_class = wrapper_get_block_class();
129 if (!ptr_block_class)
130 return -ENOSYS;
131 ptr_disk_type = wrapper_get_disk_type();
132 if (!ptr_disk_type) {
133 return -ENOSYS;
134 }
135 class_dev_iter_init(&iter, ptr_block_class, NULL, ptr_disk_type);
136 while ((dev = class_dev_iter_next(&iter))) {
137 struct disk_part_iter piter;
138 struct gendisk *disk = dev_to_disk(dev);
139 struct hd_struct *part;
140
141 /*
142 * Don't show empty devices or things that have been
143 * suppressed
144 */
145 if (get_capacity(disk) == 0 ||
146 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
147 continue;
148
149 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
150 while ((part = disk_part_iter_next(&piter))) {
151 char name_buf[BDEVNAME_SIZE];
152 char *p;
153
154 p = wrapper_disk_name(disk, part->partno, name_buf);
155 if (!p) {
156 disk_part_iter_exit(&piter);
157 class_dev_iter_exit(&iter);
158 return -ENOSYS;
159 }
160 trace_lttng_statedump_block_device(session,
161 part_devt(part), name_buf);
162 }
163 disk_part_iter_exit(&piter);
164 }
165 class_dev_iter_exit(&iter);
166 return 0;
167 }
168
169 #ifdef CONFIG_INET
170
171 static
172 void lttng_enumerate_device(struct lttng_session *session,
173 struct net_device *dev)
174 {
175 struct in_device *in_dev;
176 struct in_ifaddr *ifa;
177
178 if (dev->flags & IFF_UP) {
179 in_dev = in_dev_get(dev);
180 if (in_dev) {
181 for (ifa = in_dev->ifa_list; ifa != NULL;
182 ifa = ifa->ifa_next) {
183 trace_lttng_statedump_network_interface(
184 session, dev, ifa);
185 }
186 in_dev_put(in_dev);
187 }
188 } else {
189 trace_lttng_statedump_network_interface(
190 session, dev, NULL);
191 }
192 }
193
194 static
195 int lttng_enumerate_network_ip_interface(struct lttng_session *session)
196 {
197 struct net_device *dev;
198
199 read_lock(&dev_base_lock);
200 for_each_netdev(&init_net, dev)
201 lttng_enumerate_device(session, dev);
202 read_unlock(&dev_base_lock);
203
204 return 0;
205 }
206 #else /* CONFIG_INET */
207 static inline
208 int lttng_enumerate_network_ip_interface(struct lttng_session *session)
209 {
210 return 0;
211 }
212 #endif /* CONFIG_INET */
213
214 static
215 int lttng_dump_one_fd(const void *p, struct file *file, unsigned int fd)
216 {
217 const struct lttng_fd_ctx *ctx = p;
218 const char *s = d_path(&file->f_path, ctx->page, PAGE_SIZE);
219 unsigned int flags = file->f_flags;
220 struct fdtable *fdt;
221
222 /*
223 * We don't expose kernel internal flags, only userspace-visible
224 * flags.
225 */
226 flags &= ~FMODE_NONOTIFY;
227 fdt = files_fdtable(ctx->files);
228 /*
229 * We need to check here again whether fd is within the fdt
230 * max_fds range, because we might be seeing a different
231 * files_fdtable() than iterate_fd(), assuming only RCU is
232 * protecting the read. In reality, iterate_fd() holds
233 * file_lock, which should ensure the fdt does not change while
234 * the lock is taken, but we are not aware whether this is
235 * guaranteed or not, so play safe.
236 */
237 if (fd < fdt->max_fds && close_on_exec(fd, fdt))
238 flags |= O_CLOEXEC;
239 if (IS_ERR(s)) {
240 struct dentry *dentry = file->f_path.dentry;
241
242 /* Make sure we give at least some info */
243 spin_lock(&dentry->d_lock);
244 trace_lttng_statedump_file_descriptor(ctx->session,
245 ctx->files, fd, dentry->d_name.name, flags,
246 file->f_mode);
247 spin_unlock(&dentry->d_lock);
248 goto end;
249 }
250 trace_lttng_statedump_file_descriptor(ctx->session,
251 ctx->files, fd, s, flags, file->f_mode);
252 end:
253 return 0;
254 }
255
256 /* Called with task lock held. */
257 static
258 void lttng_enumerate_files(struct lttng_session *session,
259 struct files_struct *files,
260 char *tmp)
261 {
262 struct lttng_fd_ctx ctx = { .page = tmp, .session = session, .files = files, };
263
264 iterate_fd(files, 0, lttng_dump_one_fd, &ctx);
265 }
266
267 #ifdef LTTNG_HAVE_STATEDUMP_CPU_TOPOLOGY
268 static
269 int lttng_enumerate_cpu_topology(struct lttng_session *session)
270 {
271 int cpu;
272 const cpumask_t *cpumask = cpu_possible_mask;
273
274 for (cpu = cpumask_first(cpumask); cpu < nr_cpu_ids;
275 cpu = cpumask_next(cpu, cpumask)) {
276 trace_lttng_statedump_cpu_topology(session, &cpu_data(cpu));
277 }
278
279 return 0;
280 }
281 #else
282 static
283 int lttng_enumerate_cpu_topology(struct lttng_session *session)
284 {
285 return 0;
286 }
287 #endif
288
289 #if 0
290 /*
291 * FIXME: we cannot take a mmap_sem while in a RCU read-side critical section
292 * (scheduling in atomic). Normally, the tasklist lock protects this kind of
293 * iteration, but it is not exported to modules.
294 */
295 static
296 void lttng_enumerate_task_vm_maps(struct lttng_session *session,
297 struct task_struct *p)
298 {
299 struct mm_struct *mm;
300 struct vm_area_struct *map;
301 unsigned long ino;
302
303 /* get_task_mm does a task_lock... */
304 mm = get_task_mm(p);
305 if (!mm)
306 return;
307
308 map = mm->mmap;
309 if (map) {
310 down_read(&mm->mmap_sem);
311 while (map) {
312 if (map->vm_file)
313 ino = map->vm_file->lttng_f_dentry->d_inode->i_ino;
314 else
315 ino = 0;
316 trace_lttng_statedump_vm_map(session, p, map, ino);
317 map = map->vm_next;
318 }
319 up_read(&mm->mmap_sem);
320 }
321 mmput(mm);
322 }
323
324 static
325 int lttng_enumerate_vm_maps(struct lttng_session *session)
326 {
327 struct task_struct *p;
328
329 rcu_read_lock();
330 for_each_process(p)
331 lttng_enumerate_task_vm_maps(session, p);
332 rcu_read_unlock();
333 return 0;
334 }
335 #endif
336
337 #ifdef CONFIG_LTTNG_HAS_LIST_IRQ
338
339 static
340 int lttng_list_interrupts(struct lttng_session *session)
341 {
342 unsigned int irq;
343 unsigned long flags = 0;
344 struct irq_desc *desc;
345
346 /* needs irq_desc */
347 for_each_irq_desc(irq, desc) {
348 struct irqaction *action;
349 const char *irq_chip_name =
350 irq_desc_get_chip(desc)->name ? : "unnamed_irq_chip";
351
352 local_irq_save(flags);
353 raw_spin_lock(&desc->lock);
354 for (action = desc->action; action; action = action->next) {
355 trace_lttng_statedump_interrupt(session,
356 irq, irq_chip_name, action);
357 }
358 raw_spin_unlock(&desc->lock);
359 local_irq_restore(flags);
360 }
361 return 0;
362 }
363 #else
364 static inline
365 int lttng_list_interrupts(struct lttng_session *session)
366 {
367 return 0;
368 }
369 #endif
370
371 /*
372 * Statedump the task's namespaces using the proc filesystem inode number as
373 * the unique identifier. The user and pid ns are nested and will be dumped
374 * recursively.
375 *
376 * Called with task lock held.
377 */
378 static
379 void lttng_statedump_process_ns(struct lttng_session *session,
380 struct task_struct *p,
381 enum lttng_thread_type type,
382 enum lttng_execution_mode mode,
383 enum lttng_execution_submode submode,
384 enum lttng_process_status status)
385 {
386 struct nsproxy *proxy;
387 struct pid_namespace *pid_ns;
388 struct user_namespace *user_ns;
389
390 /*
391 * The pid and user namespaces are special, they are nested and
392 * accessed with specific functions instead of the nsproxy struct
393 * like the other namespaces.
394 */
395 pid_ns = task_active_pid_ns(p);
396 do {
397 trace_lttng_statedump_process_pid_ns(session, p, pid_ns);
398 pid_ns = pid_ns ? pid_ns->parent : NULL;
399 } while (pid_ns);
400
401
402 user_ns = task_cred_xxx(p, user_ns);
403 do {
404 trace_lttng_statedump_process_user_ns(session, p, user_ns);
405 /*
406 * trace_lttng_statedump_process_user_ns() internally
407 * checks whether user_ns is NULL. While this does not
408 * appear to be a possible return value for
409 * task_cred_xxx(), err on the safe side and check
410 * for NULL here as well to be consistent with the
411 * paranoid behavior of
412 * trace_lttng_statedump_process_user_ns().
413 */
414 user_ns = user_ns ? user_ns->lttng_user_ns_parent : NULL;
415 } while (user_ns);
416
417 /*
418 * Back and forth on locking strategy within Linux upstream for nsproxy.
419 * See Linux upstream commit 728dba3a39c66b3d8ac889ddbe38b5b1c264aec3
420 * "namespaces: Use task_lock and not rcu to protect nsproxy"
421 * for details.
422 */
423 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0) || \
424 LTTNG_UBUNTU_KERNEL_RANGE(3,13,11,36, 3,14,0,0) || \
425 LTTNG_UBUNTU_KERNEL_RANGE(3,16,1,11, 3,17,0,0) || \
426 LTTNG_RHEL_KERNEL_RANGE(3,10,0,229,13,0, 3,11,0,0,0,0))
427 proxy = p->nsproxy;
428 #else
429 rcu_read_lock();
430 proxy = task_nsproxy(p);
431 #endif
432 if (proxy) {
433 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,6,0))
434 trace_lttng_statedump_process_cgroup_ns(session, p, proxy->cgroup_ns);
435 #endif
436 trace_lttng_statedump_process_ipc_ns(session, p, proxy->ipc_ns);
437 #ifndef LTTNG_MNT_NS_MISSING_HEADER
438 trace_lttng_statedump_process_mnt_ns(session, p, proxy->mnt_ns);
439 #endif
440 trace_lttng_statedump_process_net_ns(session, p, proxy->net_ns);
441 trace_lttng_statedump_process_uts_ns(session, p, proxy->uts_ns);
442 }
443 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0) || \
444 LTTNG_UBUNTU_KERNEL_RANGE(3,13,11,36, 3,14,0,0) || \
445 LTTNG_UBUNTU_KERNEL_RANGE(3,16,1,11, 3,17,0,0) || \
446 LTTNG_RHEL_KERNEL_RANGE(3,10,0,229,13,0, 3,11,0,0,0,0))
447 /* (nothing) */
448 #else
449 rcu_read_unlock();
450 #endif
451 }
452
453 static
454 int lttng_enumerate_process_states(struct lttng_session *session)
455 {
456 struct task_struct *g, *p;
457 char *tmp;
458
459 tmp = (char *) __get_free_page(GFP_KERNEL);
460 if (!tmp)
461 return -ENOMEM;
462
463 rcu_read_lock();
464 for_each_process(g) {
465 struct files_struct *prev_files = NULL;
466
467 p = g;
468 do {
469 enum lttng_execution_mode mode =
470 LTTNG_MODE_UNKNOWN;
471 enum lttng_execution_submode submode =
472 LTTNG_UNKNOWN;
473 enum lttng_process_status status;
474 enum lttng_thread_type type;
475 struct files_struct *files;
476
477 task_lock(p);
478 if (p->exit_state == EXIT_ZOMBIE)
479 status = LTTNG_ZOMBIE;
480 else if (p->exit_state == EXIT_DEAD)
481 status = LTTNG_DEAD;
482 else if (p->state == TASK_RUNNING) {
483 /* Is this a forked child that has not run yet? */
484 if (list_empty(&p->rt.run_list))
485 status = LTTNG_WAIT_FORK;
486 else
487 /*
488 * All tasks are considered as wait_cpu;
489 * the viewer will sort out if the task
490 * was really running at this time.
491 */
492 status = LTTNG_WAIT_CPU;
493 } else if (p->state &
494 (TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)) {
495 /* Task is waiting for something to complete */
496 status = LTTNG_WAIT;
497 } else
498 status = LTTNG_UNNAMED;
499 submode = LTTNG_NONE;
500
501 /*
502 * Verification of t->mm is to filter out kernel
503 * threads; Viewer will further filter out if a
504 * user-space thread was in syscall mode or not.
505 */
506 if (p->mm)
507 type = LTTNG_USER_THREAD;
508 else
509 type = LTTNG_KERNEL_THREAD;
510 files = p->files;
511
512 trace_lttng_statedump_process_state(session,
513 p, type, mode, submode, status, files);
514 lttng_statedump_process_ns(session,
515 p, type, mode, submode, status);
516 /*
517 * As an optimisation for the common case, do not
518 * repeat information for the same files_struct in
519 * two consecutive threads. This is the common case
520 * for threads sharing the same fd table. RCU guarantees
521 * that the same files_struct pointer is not re-used
522 * throughout processes/threads iteration.
523 */
524 if (files && files != prev_files) {
525 lttng_enumerate_files(session, files, tmp);
526 prev_files = files;
527 }
528 task_unlock(p);
529 } while_each_thread(g, p);
530 }
531 rcu_read_unlock();
532
533 free_page((unsigned long) tmp);
534
535 return 0;
536 }
537
538 static
539 void lttng_statedump_work_func(struct work_struct *work)
540 {
541 if (atomic_dec_and_test(&kernel_threads_to_run))
542 /* If we are the last thread, wake up do_lttng_statedump */
543 wake_up(&statedump_wq);
544 }
545
546 static
547 int do_lttng_statedump(struct lttng_session *session)
548 {
549 int cpu, ret;
550
551 trace_lttng_statedump_start(session);
552 ret = lttng_enumerate_process_states(session);
553 if (ret)
554 return ret;
555 /*
556 * FIXME
557 * ret = lttng_enumerate_vm_maps(session);
558 * if (ret)
559 * return ret;
560 */
561 ret = lttng_list_interrupts(session);
562 if (ret)
563 return ret;
564 ret = lttng_enumerate_network_ip_interface(session);
565 if (ret)
566 return ret;
567 ret = lttng_enumerate_block_devices(session);
568 switch (ret) {
569 case 0:
570 break;
571 case -ENOSYS:
572 printk(KERN_WARNING "LTTng: block device enumeration is not supported by kernel\n");
573 break;
574 default:
575 return ret;
576 }
577 ret = lttng_enumerate_cpu_topology(session);
578 if (ret)
579 return ret;
580
581 /* TODO lttng_dump_idt_table(session); */
582 /* TODO lttng_dump_softirq_vec(session); */
583 /* TODO lttng_list_modules(session); */
584 /* TODO lttng_dump_swap_files(session); */
585
586 /*
587 * Fire off a work queue on each CPU. Their sole purpose in life
588 * is to guarantee that each CPU has been in a state where is was in
589 * syscall mode (i.e. not in a trap, an IRQ or a soft IRQ).
590 */
591 get_online_cpus();
592 atomic_set(&kernel_threads_to_run, num_online_cpus());
593 for_each_online_cpu(cpu) {
594 INIT_DELAYED_WORK(&cpu_work[cpu], lttng_statedump_work_func);
595 schedule_delayed_work_on(cpu, &cpu_work[cpu], 0);
596 }
597 /* Wait for all threads to run */
598 __wait_event(statedump_wq, (atomic_read(&kernel_threads_to_run) == 0));
599 put_online_cpus();
600 /* Our work is done */
601 trace_lttng_statedump_end(session);
602 return 0;
603 }
604
605 /*
606 * Called with session mutex held.
607 */
608 int lttng_statedump_start(struct lttng_session *session)
609 {
610 return do_lttng_statedump(session);
611 }
612 EXPORT_SYMBOL_GPL(lttng_statedump_start);
613
614 static
615 int __init lttng_statedump_init(void)
616 {
617 return 0;
618 }
619
620 module_init(lttng_statedump_init);
621
622 static
623 void __exit lttng_statedump_exit(void)
624 {
625 }
626
627 module_exit(lttng_statedump_exit);
628
629 MODULE_LICENSE("GPL and additional rights");
630 MODULE_AUTHOR("Jean-Hugues Deschenes");
631 MODULE_DESCRIPTION("LTTng statedump provider");
632 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
633 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
634 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
635 LTTNG_MODULES_EXTRAVERSION);
This page took 0.039838 seconds and 3 git commands to generate.