Fix: timer_expire_entry changed in 4.19.312
[lttng-modules.git] / lttng-statedump-impl.c
1 /* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1)
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/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>
46
47 #ifdef CONFIG_LTTNG_HAS_LIST_IRQ
48 #include <linux/irq.h>
49 #endif
50
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>
57
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);
67 #endif
68 DEFINE_TRACE(lttng_statedump_process_ipc_ns);
69 #ifndef LTTNG_MNT_NS_MISSING_HEADER
70 DEFINE_TRACE(lttng_statedump_process_mnt_ns);
71 #endif
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);
78 #endif
79
80 struct lttng_fd_ctx {
81 char *page;
82 struct lttng_session *session;
83 struct task_struct *p;
84 struct files_struct *files;
85 };
86
87 /*
88 * Protected by the trace lock.
89 */
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;
93
94 enum lttng_thread_type {
95 LTTNG_USER_THREAD = 0,
96 LTTNG_KERNEL_THREAD = 1,
97 };
98
99 enum lttng_execution_mode {
100 LTTNG_USER_MODE = 0,
101 LTTNG_SYSCALL = 1,
102 LTTNG_TRAP = 2,
103 LTTNG_IRQ = 3,
104 LTTNG_SOFTIRQ = 4,
105 LTTNG_MODE_UNKNOWN = 5,
106 };
107
108 enum lttng_execution_submode {
109 LTTNG_NONE = 0,
110 LTTNG_UNKNOWN = 1,
111 };
112
113 enum lttng_process_status {
114 LTTNG_UNNAMED = 0,
115 LTTNG_WAIT_FORK = 1,
116 LTTNG_WAIT_CPU = 2,
117 LTTNG_EXIT = 3,
118 LTTNG_ZOMBIE = 4,
119 LTTNG_WAIT = 5,
120 LTTNG_RUN = 6,
121 LTTNG_DEAD = 7,
122 };
123
124 static
125 int lttng_enumerate_block_devices(struct lttng_session *session)
126 {
127 struct class *ptr_block_class;
128 struct device_type *ptr_disk_type;
129 struct class_dev_iter iter;
130 struct device *dev;
131
132 ptr_block_class = wrapper_get_block_class();
133 if (!ptr_block_class)
134 return -ENOSYS;
135 ptr_disk_type = wrapper_get_disk_type();
136 if (!ptr_disk_type) {
137 return -ENOSYS;
138 }
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;
144
145 /*
146 * Don't show empty devices or things that have been
147 * suppressed
148 */
149 if (get_capacity(disk) == 0 ||
150 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
151 continue;
152
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];
156 char *p;
157
158 p = wrapper_disk_name(disk, part->partno, name_buf);
159 if (!p) {
160 disk_part_iter_exit(&piter);
161 class_dev_iter_exit(&iter);
162 return -ENOSYS;
163 }
164 trace_lttng_statedump_block_device(session,
165 part_devt(part), name_buf);
166 }
167 disk_part_iter_exit(&piter);
168 }
169 class_dev_iter_exit(&iter);
170 return 0;
171 }
172
173 #ifdef CONFIG_INET
174
175 static
176 void lttng_enumerate_device(struct lttng_session *session,
177 struct net_device *dev)
178 {
179 struct in_device *in_dev;
180 struct in_ifaddr *ifa;
181
182 if (dev->flags & IFF_UP) {
183 in_dev = in_dev_get(dev);
184 if (in_dev) {
185 for (ifa = in_dev->ifa_list; ifa != NULL;
186 ifa = ifa->ifa_next) {
187 trace_lttng_statedump_network_interface(
188 session, dev, ifa);
189 }
190 in_dev_put(in_dev);
191 }
192 } else {
193 trace_lttng_statedump_network_interface(
194 session, dev, NULL);
195 }
196 }
197
198 static
199 int lttng_enumerate_network_ip_interface(struct lttng_session *session)
200 {
201 struct net_device *dev;
202
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);
207
208 return 0;
209 }
210 #else /* CONFIG_INET */
211 static inline
212 int lttng_enumerate_network_ip_interface(struct lttng_session *session)
213 {
214 return 0;
215 }
216 #endif /* CONFIG_INET */
217
218 static
219 int lttng_dump_one_fd(const void *p, struct file *file, unsigned int fd)
220 {
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;
224 struct fdtable *fdt;
225
226 /*
227 * We don't expose kernel internal flags, only userspace-visible
228 * flags.
229 */
230 flags &= ~FMODE_NONOTIFY;
231 fdt = files_fdtable(ctx->files);
232 /*
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.
240 */
241 if (fd < fdt->max_fds && lttng_close_on_exec(fd, fdt))
242 flags |= O_CLOEXEC;
243 if (IS_ERR(s)) {
244 struct dentry *dentry = file->f_path.dentry;
245
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);
251 goto end;
252 }
253 trace_lttng_statedump_file_descriptor(ctx->session, ctx->p, fd, s,
254 flags, file->f_mode);
255 end:
256 return 0;
257 }
258
259 static
260 void lttng_enumerate_task_fd(struct lttng_session *session,
261 struct task_struct *p, char *tmp)
262 {
263 struct lttng_fd_ctx ctx = { .page = tmp, .session = session, .p = p };
264 struct files_struct *files;
265
266 task_lock(p);
267 files = p->files;
268 if (!files)
269 goto end;
270 ctx.files = files;
271 lttng_iterate_fd(files, 0, lttng_dump_one_fd, &ctx);
272 end:
273 task_unlock(p);
274 }
275
276 static
277 int lttng_enumerate_file_descriptors(struct lttng_session *session)
278 {
279 struct task_struct *p;
280 char *tmp;
281
282 tmp = (char *) __get_free_page(GFP_KERNEL);
283 if (!tmp)
284 return -ENOMEM;
285
286 /* Enumerate active file descriptors */
287 rcu_read_lock();
288 for_each_process(p)
289 lttng_enumerate_task_fd(session, p, tmp);
290 rcu_read_unlock();
291 free_page((unsigned long) tmp);
292 return 0;
293 }
294
295 #ifdef LTTNG_HAVE_STATEDUMP_CPU_TOPOLOGY
296 static
297 int lttng_enumerate_cpu_topology(struct lttng_session *session)
298 {
299 int cpu;
300 const cpumask_t *cpumask = cpu_possible_mask;
301
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));
305 }
306
307 return 0;
308 }
309 #else
310 static
311 int lttng_enumerate_cpu_topology(struct lttng_session *session)
312 {
313 return 0;
314 }
315 #endif
316
317 #if 0
318 /*
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.
322 */
323 static
324 void lttng_enumerate_task_vm_maps(struct lttng_session *session,
325 struct task_struct *p)
326 {
327 struct mm_struct *mm;
328 struct vm_area_struct *map;
329 unsigned long ino;
330
331 /* get_task_mm does a task_lock... */
332 mm = get_task_mm(p);
333 if (!mm)
334 return;
335
336 map = mm->mmap;
337 if (map) {
338 down_read(&mm->mmap_sem);
339 while (map) {
340 if (map->vm_file)
341 ino = map->vm_file->lttng_f_dentry->d_inode->i_ino;
342 else
343 ino = 0;
344 trace_lttng_statedump_vm_map(session, p, map, ino);
345 map = map->vm_next;
346 }
347 up_read(&mm->mmap_sem);
348 }
349 mmput(mm);
350 }
351
352 static
353 int lttng_enumerate_vm_maps(struct lttng_session *session)
354 {
355 struct task_struct *p;
356
357 rcu_read_lock();
358 for_each_process(p)
359 lttng_enumerate_task_vm_maps(session, p);
360 rcu_read_unlock();
361 return 0;
362 }
363 #endif
364
365 #ifdef CONFIG_LTTNG_HAS_LIST_IRQ
366
367 static
368 int lttng_list_interrupts(struct lttng_session *session)
369 {
370 unsigned int irq;
371 unsigned long flags = 0;
372 struct irq_desc *desc;
373
374 #define irq_to_desc wrapper_irq_to_desc
375 /* needs irq_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";
380
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);
386 }
387 raw_spin_unlock(&desc->lock);
388 local_irq_restore(flags);
389 }
390 return 0;
391 #undef irq_to_desc
392 }
393 #else
394 static inline
395 int lttng_list_interrupts(struct lttng_session *session)
396 {
397 return 0;
398 }
399 #endif
400
401 /*
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
404 * recursively.
405 *
406 * Called with task lock held.
407 */
408 static
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)
415 {
416 struct nsproxy *proxy;
417 struct pid_namespace *pid_ns;
418 struct user_namespace *user_ns;
419
420 /*
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.
424 */
425 pid_ns = task_active_pid_ns(p);
426 do {
427 trace_lttng_statedump_process_pid_ns(session, p, pid_ns);
428 pid_ns = pid_ns ? pid_ns->parent : NULL;
429 } while (pid_ns);
430
431
432 user_ns = task_cred_xxx(p, user_ns);
433 do {
434 trace_lttng_statedump_process_user_ns(session, p, user_ns);
435 /*
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().
443 */
444 user_ns = user_ns ? user_ns->lttng_user_ns_parent : NULL;
445 } while (user_ns);
446
447 /*
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"
451 * for details.
452 */
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))
457 proxy = p->nsproxy;
458 #else
459 rcu_read_lock();
460 proxy = task_nsproxy(p);
461 #endif
462 if (proxy) {
463 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,6,0))
464 trace_lttng_statedump_process_cgroup_ns(session, p, proxy->cgroup_ns);
465 #endif
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);
469 #endif
470 trace_lttng_statedump_process_net_ns(session, p, proxy->net_ns);
471 trace_lttng_statedump_process_uts_ns(session, p, proxy->uts_ns);
472 }
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))
477 /* (nothing) */
478 #else
479 rcu_read_unlock();
480 #endif
481 }
482
483 static
484 int lttng_enumerate_process_states(struct lttng_session *session)
485 {
486 struct task_struct *g, *p;
487
488 rcu_read_lock();
489 for_each_process(g) {
490 p = g;
491 do {
492 enum lttng_execution_mode mode =
493 LTTNG_MODE_UNKNOWN;
494 enum lttng_execution_submode submode =
495 LTTNG_UNKNOWN;
496 enum lttng_process_status status;
497 enum lttng_thread_type type;
498
499 task_lock(p);
500 if (p->exit_state == EXIT_ZOMBIE)
501 status = LTTNG_ZOMBIE;
502 else if (p->exit_state == EXIT_DEAD)
503 status = LTTNG_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;
508 else
509 /*
510 * All tasks are considered as wait_cpu;
511 * the viewer will sort out if the task
512 * was really running at this time.
513 */
514 status = LTTNG_WAIT_CPU;
515 } else if (p->state &
516 (TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)) {
517 /* Task is waiting for something to complete */
518 status = LTTNG_WAIT;
519 } else
520 status = LTTNG_UNNAMED;
521 submode = LTTNG_NONE;
522
523 /*
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.
527 */
528 if (p->mm)
529 type = LTTNG_USER_THREAD;
530 else
531 type = LTTNG_KERNEL_THREAD;
532
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);
537 task_unlock(p);
538 } while_each_thread(g, p);
539 }
540 rcu_read_unlock();
541
542 return 0;
543 }
544
545 static
546 void lttng_statedump_work_func(struct work_struct *work)
547 {
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);
551 }
552
553 static
554 int do_lttng_statedump(struct lttng_session *session)
555 {
556 int cpu, ret;
557
558 trace_lttng_statedump_start(session);
559 ret = lttng_enumerate_process_states(session);
560 if (ret)
561 return ret;
562 ret = lttng_enumerate_file_descriptors(session);
563 if (ret)
564 return ret;
565 /*
566 * FIXME
567 * ret = lttng_enumerate_vm_maps(session);
568 * if (ret)
569 * return ret;
570 */
571 ret = lttng_list_interrupts(session);
572 if (ret)
573 return ret;
574 ret = lttng_enumerate_network_ip_interface(session);
575 if (ret)
576 return ret;
577 ret = lttng_enumerate_block_devices(session);
578 switch (ret) {
579 case 0:
580 break;
581 case -ENOSYS:
582 printk(KERN_WARNING "LTTng: block device enumeration is not supported by kernel\n");
583 break;
584 default:
585 return ret;
586 }
587 ret = lttng_enumerate_cpu_topology(session);
588 if (ret)
589 return ret;
590
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); */
595
596 /*
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).
600 */
601 get_online_cpus();
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);
606 }
607 /* Wait for all threads to run */
608 __wait_event(statedump_wq, (atomic_read(&kernel_threads_to_run) == 0));
609 put_online_cpus();
610 /* Our work is done */
611 trace_lttng_statedump_end(session);
612 return 0;
613 }
614
615 /*
616 * Called with session mutex held.
617 */
618 int lttng_statedump_start(struct lttng_session *session)
619 {
620 return do_lttng_statedump(session);
621 }
622 EXPORT_SYMBOL_GPL(lttng_statedump_start);
623
624 static
625 int __init lttng_statedump_init(void)
626 {
627 /*
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.
632 */
633 (void) wrapper_lttng_fixup_sig(THIS_MODULE);
634 return 0;
635 }
636
637 module_init(lttng_statedump_init);
638
639 static
640 void __exit lttng_statedump_exit(void)
641 {
642 }
643
644 module_exit(lttng_statedump_exit);
645
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);
This page took 0.041025 seconds and 4 git commands to generate.