wrapper: Remove irqdesc wrapper
[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
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>
45
46 #ifdef CONFIG_LTTNG_HAS_LIST_IRQ
47 #include <linux/irq.h>
48 #endif
49
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>
56
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);
66 #endif
67 DEFINE_TRACE(lttng_statedump_process_ipc_ns);
68 #ifndef LTTNG_MNT_NS_MISSING_HEADER
69 DEFINE_TRACE(lttng_statedump_process_mnt_ns);
70 #endif
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);
77 #endif
78
79 struct lttng_fd_ctx {
80 char *page;
81 struct lttng_session *session;
82 struct files_struct *files;
83 };
84
85 /*
86 * Protected by the trace lock.
87 */
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;
91
92 enum lttng_thread_type {
93 LTTNG_USER_THREAD = 0,
94 LTTNG_KERNEL_THREAD = 1,
95 };
96
97 enum lttng_execution_mode {
98 LTTNG_USER_MODE = 0,
99 LTTNG_SYSCALL = 1,
100 LTTNG_TRAP = 2,
101 LTTNG_IRQ = 3,
102 LTTNG_SOFTIRQ = 4,
103 LTTNG_MODE_UNKNOWN = 5,
104 };
105
106 enum lttng_execution_submode {
107 LTTNG_NONE = 0,
108 LTTNG_UNKNOWN = 1,
109 };
110
111 enum lttng_process_status {
112 LTTNG_UNNAMED = 0,
113 LTTNG_WAIT_FORK = 1,
114 LTTNG_WAIT_CPU = 2,
115 LTTNG_EXIT = 3,
116 LTTNG_ZOMBIE = 4,
117 LTTNG_WAIT = 5,
118 LTTNG_RUN = 6,
119 LTTNG_DEAD = 7,
120 };
121
122 static
123 int lttng_enumerate_block_devices(struct lttng_session *session)
124 {
125 struct class *ptr_block_class;
126 struct device_type *ptr_disk_type;
127 struct class_dev_iter iter;
128 struct device *dev;
129
130 ptr_block_class = wrapper_get_block_class();
131 if (!ptr_block_class)
132 return -ENOSYS;
133 ptr_disk_type = wrapper_get_disk_type();
134 if (!ptr_disk_type) {
135 return -ENOSYS;
136 }
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;
142
143 /*
144 * Don't show empty devices or things that have been
145 * suppressed
146 */
147 if (get_capacity(disk) == 0 ||
148 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
149 continue;
150
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];
154 char *p;
155
156 p = wrapper_disk_name(disk, part->partno, name_buf);
157 if (!p) {
158 disk_part_iter_exit(&piter);
159 class_dev_iter_exit(&iter);
160 return -ENOSYS;
161 }
162 trace_lttng_statedump_block_device(session,
163 part_devt(part), name_buf);
164 }
165 disk_part_iter_exit(&piter);
166 }
167 class_dev_iter_exit(&iter);
168 return 0;
169 }
170
171 #ifdef CONFIG_INET
172
173 static
174 void lttng_enumerate_device(struct lttng_session *session,
175 struct net_device *dev)
176 {
177 struct in_device *in_dev;
178 struct in_ifaddr *ifa;
179
180 if (dev->flags & IFF_UP) {
181 in_dev = in_dev_get(dev);
182 if (in_dev) {
183 for (ifa = in_dev->ifa_list; ifa != NULL;
184 ifa = ifa->ifa_next) {
185 trace_lttng_statedump_network_interface(
186 session, dev, ifa);
187 }
188 in_dev_put(in_dev);
189 }
190 } else {
191 trace_lttng_statedump_network_interface(
192 session, dev, NULL);
193 }
194 }
195
196 static
197 int lttng_enumerate_network_ip_interface(struct lttng_session *session)
198 {
199 struct net_device *dev;
200
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);
205
206 return 0;
207 }
208 #else /* CONFIG_INET */
209 static inline
210 int lttng_enumerate_network_ip_interface(struct lttng_session *session)
211 {
212 return 0;
213 }
214 #endif /* CONFIG_INET */
215
216 static
217 int lttng_dump_one_fd(const void *p, struct file *file, unsigned int fd)
218 {
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;
222 struct fdtable *fdt;
223
224 /*
225 * We don't expose kernel internal flags, only userspace-visible
226 * flags.
227 */
228 flags &= ~FMODE_NONOTIFY;
229 fdt = files_fdtable(ctx->files);
230 /*
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.
238 */
239 if (fd < fdt->max_fds && lttng_close_on_exec(fd, fdt))
240 flags |= O_CLOEXEC;
241 if (IS_ERR(s)) {
242 struct dentry *dentry = file->f_path.dentry;
243
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,
248 file->f_mode);
249 spin_unlock(&dentry->d_lock);
250 goto end;
251 }
252 trace_lttng_statedump_file_descriptor(ctx->session,
253 ctx->files, fd, s, flags, file->f_mode);
254 end:
255 return 0;
256 }
257
258 /* Called with task lock held. */
259 static
260 void lttng_enumerate_files(struct lttng_session *session,
261 struct files_struct *files,
262 char *tmp)
263 {
264 struct lttng_fd_ctx ctx = { .page = tmp, .session = session, .files = files, };
265
266 lttng_iterate_fd(files, 0, lttng_dump_one_fd, &ctx);
267 }
268
269 #ifdef LTTNG_HAVE_STATEDUMP_CPU_TOPOLOGY
270 static
271 int lttng_enumerate_cpu_topology(struct lttng_session *session)
272 {
273 int cpu;
274 const cpumask_t *cpumask = cpu_possible_mask;
275
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));
279 }
280
281 return 0;
282 }
283 #else
284 static
285 int lttng_enumerate_cpu_topology(struct lttng_session *session)
286 {
287 return 0;
288 }
289 #endif
290
291 #if 0
292 /*
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.
296 */
297 static
298 void lttng_enumerate_task_vm_maps(struct lttng_session *session,
299 struct task_struct *p)
300 {
301 struct mm_struct *mm;
302 struct vm_area_struct *map;
303 unsigned long ino;
304
305 /* get_task_mm does a task_lock... */
306 mm = get_task_mm(p);
307 if (!mm)
308 return;
309
310 map = mm->mmap;
311 if (map) {
312 down_read(&mm->mmap_sem);
313 while (map) {
314 if (map->vm_file)
315 ino = map->vm_file->lttng_f_dentry->d_inode->i_ino;
316 else
317 ino = 0;
318 trace_lttng_statedump_vm_map(session, p, map, ino);
319 map = map->vm_next;
320 }
321 up_read(&mm->mmap_sem);
322 }
323 mmput(mm);
324 }
325
326 static
327 int lttng_enumerate_vm_maps(struct lttng_session *session)
328 {
329 struct task_struct *p;
330
331 rcu_read_lock();
332 for_each_process(p)
333 lttng_enumerate_task_vm_maps(session, p);
334 rcu_read_unlock();
335 return 0;
336 }
337 #endif
338
339 #ifdef CONFIG_LTTNG_HAS_LIST_IRQ
340
341 static
342 int lttng_list_interrupts(struct lttng_session *session)
343 {
344 unsigned int irq;
345 unsigned long flags = 0;
346 struct irq_desc *desc;
347
348 /* needs irq_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";
353
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);
359 }
360 raw_spin_unlock(&desc->lock);
361 local_irq_restore(flags);
362 }
363 return 0;
364 }
365 #else
366 static inline
367 int lttng_list_interrupts(struct lttng_session *session)
368 {
369 return 0;
370 }
371 #endif
372
373 /*
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
376 * recursively.
377 *
378 * Called with task lock held.
379 */
380 static
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)
387 {
388 struct nsproxy *proxy;
389 struct pid_namespace *pid_ns;
390 struct user_namespace *user_ns;
391
392 /*
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.
396 */
397 pid_ns = task_active_pid_ns(p);
398 do {
399 trace_lttng_statedump_process_pid_ns(session, p, pid_ns);
400 pid_ns = pid_ns ? pid_ns->parent : NULL;
401 } while (pid_ns);
402
403
404 user_ns = task_cred_xxx(p, user_ns);
405 do {
406 trace_lttng_statedump_process_user_ns(session, p, user_ns);
407 /*
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().
415 */
416 user_ns = user_ns ? user_ns->lttng_user_ns_parent : NULL;
417 } while (user_ns);
418
419 /*
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"
423 * for details.
424 */
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))
429 proxy = p->nsproxy;
430 #else
431 rcu_read_lock();
432 proxy = task_nsproxy(p);
433 #endif
434 if (proxy) {
435 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,6,0))
436 trace_lttng_statedump_process_cgroup_ns(session, p, proxy->cgroup_ns);
437 #endif
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);
441 #endif
442 trace_lttng_statedump_process_net_ns(session, p, proxy->net_ns);
443 trace_lttng_statedump_process_uts_ns(session, p, proxy->uts_ns);
444 }
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))
449 /* (nothing) */
450 #else
451 rcu_read_unlock();
452 #endif
453 }
454
455 static
456 int lttng_enumerate_process_states(struct lttng_session *session)
457 {
458 struct task_struct *g, *p;
459 char *tmp;
460
461 tmp = (char *) __get_free_page(GFP_KERNEL);
462 if (!tmp)
463 return -ENOMEM;
464
465 rcu_read_lock();
466 for_each_process(g) {
467 struct files_struct *prev_files = NULL;
468
469 p = g;
470 do {
471 enum lttng_execution_mode mode =
472 LTTNG_MODE_UNKNOWN;
473 enum lttng_execution_submode submode =
474 LTTNG_UNKNOWN;
475 enum lttng_process_status status;
476 enum lttng_thread_type type;
477 struct files_struct *files;
478
479 task_lock(p);
480 if (p->exit_state == EXIT_ZOMBIE)
481 status = LTTNG_ZOMBIE;
482 else if (p->exit_state == EXIT_DEAD)
483 status = LTTNG_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;
488 else
489 /*
490 * All tasks are considered as wait_cpu;
491 * the viewer will sort out if the task
492 * was really running at this time.
493 */
494 status = LTTNG_WAIT_CPU;
495 } else if (p->state &
496 (TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)) {
497 /* Task is waiting for something to complete */
498 status = LTTNG_WAIT;
499 } else
500 status = LTTNG_UNNAMED;
501 submode = LTTNG_NONE;
502
503 /*
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.
507 */
508 if (p->mm)
509 type = LTTNG_USER_THREAD;
510 else
511 type = LTTNG_KERNEL_THREAD;
512 files = p->files;
513
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);
518 /*
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.
525 */
526 if (files && files != prev_files) {
527 lttng_enumerate_files(session, files, tmp);
528 prev_files = files;
529 }
530 task_unlock(p);
531 } while_each_thread(g, p);
532 }
533 rcu_read_unlock();
534
535 free_page((unsigned long) tmp);
536
537 return 0;
538 }
539
540 static
541 void lttng_statedump_work_func(struct work_struct *work)
542 {
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);
546 }
547
548 static
549 int do_lttng_statedump(struct lttng_session *session)
550 {
551 int cpu, ret;
552
553 trace_lttng_statedump_start(session);
554 ret = lttng_enumerate_process_states(session);
555 if (ret)
556 return ret;
557 /*
558 * FIXME
559 * ret = lttng_enumerate_vm_maps(session);
560 * if (ret)
561 * return ret;
562 */
563 ret = lttng_list_interrupts(session);
564 if (ret)
565 return ret;
566 ret = lttng_enumerate_network_ip_interface(session);
567 if (ret)
568 return ret;
569 ret = lttng_enumerate_block_devices(session);
570 switch (ret) {
571 case 0:
572 break;
573 case -ENOSYS:
574 printk(KERN_WARNING "LTTng: block device enumeration is not supported by kernel\n");
575 break;
576 default:
577 return ret;
578 }
579 ret = lttng_enumerate_cpu_topology(session);
580 if (ret)
581 return ret;
582
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); */
587
588 /*
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).
592 */
593 get_online_cpus();
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);
598 }
599 /* Wait for all threads to run */
600 __wait_event(statedump_wq, (atomic_read(&kernel_threads_to_run) == 0));
601 put_online_cpus();
602 /* Our work is done */
603 trace_lttng_statedump_end(session);
604 return 0;
605 }
606
607 /*
608 * Called with session mutex held.
609 */
610 int lttng_statedump_start(struct lttng_session *session)
611 {
612 return do_lttng_statedump(session);
613 }
614 EXPORT_SYMBOL_GPL(lttng_statedump_start);
615
616 static
617 int __init lttng_statedump_init(void)
618 {
619 /*
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.
624 */
625 (void) wrapper_lttng_fixup_sig(THIS_MODULE);
626 return 0;
627 }
628
629 module_init(lttng_statedump_init);
630
631 static
632 void __exit lttng_statedump_exit(void)
633 {
634 }
635
636 module_exit(lttng_statedump_exit);
637
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);
This page took 0.047101 seconds and 5 git commands to generate.