Document last supported kernel version for stable-2.11 branch
[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/mm.h>
30 #include <linux/swap.h>
31 #include <linux/wait.h>
32 #include <linux/mutex.h>
33 #include <linux/device.h>
34
35 #include <lttng-events.h>
36 #include <lttng-tracer.h>
37 #include <wrapper/irqdesc.h>
38 #include <wrapper/spinlock.h>
39 #include <wrapper/fdtable.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 #include <wrapper/sched.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 LTTNG_DEFINE_TRACE(lttng_statedump_block_device,
59 TP_PROTO(struct lttng_session *session,
60 dev_t dev, const char *diskname),
61 TP_ARGS(session, dev, diskname));
62
63 LTTNG_DEFINE_TRACE(lttng_statedump_end,
64 TP_PROTO(struct lttng_session *session),
65 TP_ARGS(session));
66
67 LTTNG_DEFINE_TRACE(lttng_statedump_interrupt,
68 TP_PROTO(struct lttng_session *session,
69 unsigned int irq, const char *chip_name,
70 struct irqaction *action),
71 TP_ARGS(session, irq, chip_name, action));
72
73 LTTNG_DEFINE_TRACE(lttng_statedump_file_descriptor,
74 TP_PROTO(struct lttng_session *session,
75 struct task_struct *p, int fd, const char *filename,
76 unsigned int flags, fmode_t fmode),
77 TP_ARGS(session, p, fd, filename, flags, fmode));
78
79 LTTNG_DEFINE_TRACE(lttng_statedump_start,
80 TP_PROTO(struct lttng_session *session),
81 TP_ARGS(session));
82
83 LTTNG_DEFINE_TRACE(lttng_statedump_process_state,
84 TP_PROTO(struct lttng_session *session,
85 struct task_struct *p,
86 int type, int mode, int submode, int status,
87 struct pid_namespace *pid_ns),
88 TP_ARGS(session, p, type, mode, submode, status, pid_ns));
89
90 LTTNG_DEFINE_TRACE(lttng_statedump_network_interface,
91 TP_PROTO(struct lttng_session *session,
92 struct net_device *dev, struct in_ifaddr *ifa),
93 TP_ARGS(session, dev, ifa));
94
95 struct lttng_fd_ctx {
96 char *page;
97 struct lttng_session *session;
98 struct task_struct *p;
99 struct files_struct *files;
100 };
101
102 /*
103 * Protected by the trace lock.
104 */
105 static struct delayed_work cpu_work[NR_CPUS];
106 static DECLARE_WAIT_QUEUE_HEAD(statedump_wq);
107 static atomic_t kernel_threads_to_run;
108
109 enum lttng_thread_type {
110 LTTNG_USER_THREAD = 0,
111 LTTNG_KERNEL_THREAD = 1,
112 };
113
114 enum lttng_execution_mode {
115 LTTNG_USER_MODE = 0,
116 LTTNG_SYSCALL = 1,
117 LTTNG_TRAP = 2,
118 LTTNG_IRQ = 3,
119 LTTNG_SOFTIRQ = 4,
120 LTTNG_MODE_UNKNOWN = 5,
121 };
122
123 enum lttng_execution_submode {
124 LTTNG_NONE = 0,
125 LTTNG_UNKNOWN = 1,
126 };
127
128 enum lttng_process_status {
129 LTTNG_UNNAMED = 0,
130 LTTNG_WAIT_FORK = 1,
131 LTTNG_WAIT_CPU = 2,
132 LTTNG_EXIT = 3,
133 LTTNG_ZOMBIE = 4,
134 LTTNG_WAIT = 5,
135 LTTNG_RUN = 6,
136 LTTNG_DEAD = 7,
137 };
138
139
140 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,11,0))
141
142 #define LTTNG_PART_STRUCT_TYPE struct block_device
143
144 static
145 int lttng_get_part_name(struct gendisk *disk, struct block_device *part, char *name_buf)
146 {
147 const char *p;
148
149 p = bdevname(part, name_buf);
150 if (!p)
151 return -ENOSYS;
152
153 return 0;
154 }
155
156 static
157 dev_t lttng_get_part_devt(struct block_device *part)
158 {
159 return part->bd_dev;
160 }
161
162 #else
163
164 #define LTTNG_PART_STRUCT_TYPE struct hd_struct
165
166 static
167 int lttng_get_part_name(struct gendisk *disk, struct hd_struct *part, char *name_buf)
168 {
169 const char *p;
170 struct block_device bdev;
171
172 /*
173 * Create a partial 'struct blockdevice' to use
174 * 'bdevname()' which is a simple wrapper over
175 * 'disk_name()' but has the honor to be EXPORT_SYMBOL.
176 */
177 bdev.bd_disk = disk;
178 bdev.bd_part = part;
179
180 p = bdevname(&bdev, name_buf);
181 if (!p)
182 return -ENOSYS;
183
184 return 0;
185 }
186
187 static
188 dev_t lttng_get_part_devt(struct hd_struct *part)
189 {
190 return part_devt(part);
191 }
192 #endif
193
194 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,12,0))
195 static
196 int lttng_statedump_each_block_device(struct lttng_session *session, struct gendisk *disk)
197 {
198 struct block_device *part;
199 unsigned long idx;
200 int ret = 0;
201
202 /* Include partition 0 */
203 idx = 0;
204
205 rcu_read_lock();
206 xa_for_each(&disk->part_tbl, idx, part) {
207 char name_buf[BDEVNAME_SIZE];
208
209 /* Exclude non-partitions bdev and empty partitions. */
210 if (bdev_is_partition(part) && !bdev_nr_sectors(part))
211 continue;
212
213 if (lttng_get_part_name(disk, part, name_buf) == -ENOSYS) {
214 ret = -ENOSYS;
215 goto end;
216 }
217 trace_lttng_statedump_block_device(session, lttng_get_part_devt(part),
218 name_buf);
219 }
220 end:
221 rcu_read_unlock();
222 return ret;
223 }
224 #else
225 static
226 int lttng_statedump_each_block_device(struct lttng_session *session, struct gendisk *disk)
227 {
228 struct disk_part_iter piter;
229 LTTNG_PART_STRUCT_TYPE *part;
230
231 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
232
233 while ((part = disk_part_iter_next(&piter))) {
234 char name_buf[BDEVNAME_SIZE];
235
236 if (lttng_get_part_name(disk, part, name_buf) == -ENOSYS) {
237 disk_part_iter_exit(&piter);
238 return -ENOSYS;
239 }
240 trace_lttng_statedump_block_device(session, lttng_get_part_devt(part),
241 name_buf);
242 }
243 disk_part_iter_exit(&piter);
244
245 return 0;
246 }
247 #endif
248
249 static
250 int lttng_enumerate_block_devices(struct lttng_session *session)
251 {
252 struct class *ptr_block_class;
253 struct device_type *ptr_disk_type;
254 struct class_dev_iter iter;
255 struct device *dev;
256 int ret = 0;
257
258 ptr_block_class = wrapper_get_block_class();
259 if (!ptr_block_class) {
260 ret = -ENOSYS;
261 goto end;
262 }
263 ptr_disk_type = wrapper_get_disk_type();
264 if (!ptr_disk_type) {
265 ret = -ENOSYS;
266 goto end;
267 }
268 class_dev_iter_init(&iter, ptr_block_class, NULL, ptr_disk_type);
269 while ((dev = class_dev_iter_next(&iter))) {
270 struct gendisk *disk = dev_to_disk(dev);
271
272 /*
273 * Don't show empty devices or things that have been
274 * suppressed
275 */
276 if (get_capacity(disk) == 0 ||
277 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
278 continue;
279
280 ret = lttng_statedump_each_block_device(session, disk);
281 }
282 class_dev_iter_exit(&iter);
283 end:
284 return ret;
285 }
286
287 #ifdef CONFIG_INET
288
289 static
290 void lttng_enumerate_device(struct lttng_session *session,
291 struct net_device *dev)
292 {
293 struct in_device *in_dev;
294 struct in_ifaddr *ifa;
295
296 if (dev->flags & IFF_UP) {
297 in_dev = in_dev_get(dev);
298 if (in_dev) {
299 for (ifa = in_dev->ifa_list; ifa != NULL;
300 ifa = ifa->ifa_next) {
301 trace_lttng_statedump_network_interface(
302 session, dev, ifa);
303 }
304 in_dev_put(in_dev);
305 }
306 } else {
307 trace_lttng_statedump_network_interface(
308 session, dev, NULL);
309 }
310 }
311
312 static
313 int lttng_enumerate_network_ip_interface(struct lttng_session *session)
314 {
315 struct net_device *dev;
316
317 read_lock(&dev_base_lock);
318 for_each_netdev(&init_net, dev)
319 lttng_enumerate_device(session, dev);
320 read_unlock(&dev_base_lock);
321
322 return 0;
323 }
324 #else /* CONFIG_INET */
325 static inline
326 int lttng_enumerate_network_ip_interface(struct lttng_session *session)
327 {
328 return 0;
329 }
330 #endif /* CONFIG_INET */
331
332 static
333 int lttng_dump_one_fd(const void *p, struct file *file, unsigned int fd)
334 {
335 const struct lttng_fd_ctx *ctx = p;
336 const char *s = d_path(&file->f_path, ctx->page, PAGE_SIZE);
337 unsigned int flags = file->f_flags;
338 struct fdtable *fdt;
339
340 /*
341 * We don't expose kernel internal flags, only userspace-visible
342 * flags.
343 */
344 flags &= ~FMODE_NONOTIFY;
345 fdt = files_fdtable(ctx->files);
346 /*
347 * We need to check here again whether fd is within the fdt
348 * max_fds range, because we might be seeing a different
349 * files_fdtable() than iterate_fd(), assuming only RCU is
350 * protecting the read. In reality, iterate_fd() holds
351 * file_lock, which should ensure the fdt does not change while
352 * the lock is taken, but we are not aware whether this is
353 * guaranteed or not, so play safe.
354 */
355 if (fd < fdt->max_fds && lttng_close_on_exec(fd, fdt))
356 flags |= O_CLOEXEC;
357 if (IS_ERR(s)) {
358 struct dentry *dentry = file->f_path.dentry;
359
360 /* Make sure we give at least some info */
361 spin_lock(&dentry->d_lock);
362 trace_lttng_statedump_file_descriptor(ctx->session, ctx->p, fd,
363 dentry->d_name.name, flags, file->f_mode);
364 spin_unlock(&dentry->d_lock);
365 goto end;
366 }
367 trace_lttng_statedump_file_descriptor(ctx->session, ctx->p, fd, s,
368 flags, file->f_mode);
369 end:
370 return 0;
371 }
372
373 static
374 void lttng_enumerate_task_fd(struct lttng_session *session,
375 struct task_struct *p, char *tmp)
376 {
377 struct lttng_fd_ctx ctx = { .page = tmp, .session = session, .p = p };
378 struct files_struct *files;
379
380 task_lock(p);
381 files = p->files;
382 if (!files)
383 goto end;
384 ctx.files = files;
385 lttng_iterate_fd(files, 0, lttng_dump_one_fd, &ctx);
386 end:
387 task_unlock(p);
388 }
389
390 static
391 int lttng_enumerate_file_descriptors(struct lttng_session *session)
392 {
393 struct task_struct *p;
394 char *tmp;
395
396 tmp = (char *) __get_free_page(GFP_KERNEL);
397 if (!tmp)
398 return -ENOMEM;
399
400 /* Enumerate active file descriptors */
401 rcu_read_lock();
402 for_each_process(p)
403 lttng_enumerate_task_fd(session, p, tmp);
404 rcu_read_unlock();
405 free_page((unsigned long) tmp);
406 return 0;
407 }
408
409 #if 0
410 /*
411 * FIXME: we cannot take a mmap_sem while in a RCU read-side critical section
412 * (scheduling in atomic). Normally, the tasklist lock protects this kind of
413 * iteration, but it is not exported to modules.
414 */
415 static
416 void lttng_enumerate_task_vm_maps(struct lttng_session *session,
417 struct task_struct *p)
418 {
419 struct mm_struct *mm;
420 struct vm_area_struct *map;
421 unsigned long ino;
422
423 /* get_task_mm does a task_lock... */
424 mm = get_task_mm(p);
425 if (!mm)
426 return;
427
428 map = mm->mmap;
429 if (map) {
430 down_read(&mm->mmap_sem);
431 while (map) {
432 if (map->vm_file)
433 ino = map->vm_file->lttng_f_dentry->d_inode->i_ino;
434 else
435 ino = 0;
436 trace_lttng_statedump_vm_map(session, p, map, ino);
437 map = map->vm_next;
438 }
439 up_read(&mm->mmap_sem);
440 }
441 mmput(mm);
442 }
443
444 static
445 int lttng_enumerate_vm_maps(struct lttng_session *session)
446 {
447 struct task_struct *p;
448
449 rcu_read_lock();
450 for_each_process(p)
451 lttng_enumerate_task_vm_maps(session, p);
452 rcu_read_unlock();
453 return 0;
454 }
455 #endif
456
457 #ifdef CONFIG_LTTNG_HAS_LIST_IRQ
458
459 #if (LTTNG_LINUX_VERSION_CODE < LTTNG_KERNEL_VERSION(2,6,39))
460 #define irq_desc_get_chip(desc) get_irq_desc_chip(desc)
461 #endif
462
463 static
464 int lttng_list_interrupts(struct lttng_session *session)
465 {
466 unsigned int irq;
467 unsigned long flags = 0;
468 struct irq_desc *desc;
469
470 #define irq_to_desc wrapper_irq_to_desc
471 /* needs irq_desc */
472 for_each_irq_desc(irq, desc) {
473 struct irqaction *action;
474 const char *irq_chip_name =
475 irq_desc_get_chip(desc)->name ? : "unnamed_irq_chip";
476
477 local_irq_save(flags);
478 wrapper_desc_spin_lock(&desc->lock);
479 for (action = desc->action; action; action = action->next) {
480 trace_lttng_statedump_interrupt(session,
481 irq, irq_chip_name, action);
482 }
483 wrapper_desc_spin_unlock(&desc->lock);
484 local_irq_restore(flags);
485 }
486 return 0;
487 #undef irq_to_desc
488 }
489 #else
490 static inline
491 int lttng_list_interrupts(struct lttng_session *session)
492 {
493 return 0;
494 }
495 #endif
496
497 /*
498 * Called with task lock held.
499 */
500 static
501 void lttng_statedump_process_ns(struct lttng_session *session,
502 struct task_struct *p,
503 enum lttng_thread_type type,
504 enum lttng_execution_mode mode,
505 enum lttng_execution_submode submode,
506 enum lttng_process_status status)
507 {
508 struct pid_namespace *pid_ns;
509
510 pid_ns = task_active_pid_ns(p);
511 do {
512 trace_lttng_statedump_process_state(session,
513 p, type, mode, submode, status, pid_ns);
514 pid_ns = pid_ns ? pid_ns->parent : NULL;
515 } while (pid_ns);
516 }
517
518 static
519 int lttng_enumerate_process_states(struct lttng_session *session)
520 {
521 struct task_struct *g, *p;
522
523 rcu_read_lock();
524 for_each_process(g) {
525 p = g;
526 do {
527 enum lttng_execution_mode mode =
528 LTTNG_MODE_UNKNOWN;
529 enum lttng_execution_submode submode =
530 LTTNG_UNKNOWN;
531 enum lttng_process_status status;
532 enum lttng_thread_type type;
533
534 task_lock(p);
535 if (p->exit_state == EXIT_ZOMBIE)
536 status = LTTNG_ZOMBIE;
537 else if (p->exit_state == EXIT_DEAD)
538 status = LTTNG_DEAD;
539 else if (lttng_task_is_running(p)) {
540 /* Is this a forked child that has not run yet? */
541 if (list_empty(&p->rt.run_list))
542 status = LTTNG_WAIT_FORK;
543 else
544 /*
545 * All tasks are considered as wait_cpu;
546 * the viewer will sort out if the task
547 * was really running at this time.
548 */
549 status = LTTNG_WAIT_CPU;
550 } else if (lttng_get_task_state(p) &
551 (TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)) {
552 /* Task is waiting for something to complete */
553 status = LTTNG_WAIT;
554 } else
555 status = LTTNG_UNNAMED;
556 submode = LTTNG_NONE;
557
558 /*
559 * Verification of t->mm is to filter out kernel
560 * threads; Viewer will further filter out if a
561 * user-space thread was in syscall mode or not.
562 */
563 if (p->mm)
564 type = LTTNG_USER_THREAD;
565 else
566 type = LTTNG_KERNEL_THREAD;
567 lttng_statedump_process_ns(session,
568 p, type, mode, submode, status);
569 task_unlock(p);
570 } while_each_thread(g, p);
571 }
572 rcu_read_unlock();
573
574 return 0;
575 }
576
577 static
578 void lttng_statedump_work_func(struct work_struct *work)
579 {
580 if (atomic_dec_and_test(&kernel_threads_to_run))
581 /* If we are the last thread, wake up do_lttng_statedump */
582 wake_up(&statedump_wq);
583 }
584
585 static
586 int do_lttng_statedump(struct lttng_session *session)
587 {
588 int cpu, ret;
589
590 trace_lttng_statedump_start(session);
591 ret = lttng_enumerate_process_states(session);
592 if (ret)
593 return ret;
594 ret = lttng_enumerate_file_descriptors(session);
595 if (ret)
596 return ret;
597 /*
598 * FIXME
599 * ret = lttng_enumerate_vm_maps(session);
600 * if (ret)
601 * return ret;
602 */
603 ret = lttng_list_interrupts(session);
604 if (ret)
605 return ret;
606 ret = lttng_enumerate_network_ip_interface(session);
607 if (ret)
608 return ret;
609 ret = lttng_enumerate_block_devices(session);
610 switch (ret) {
611 case 0:
612 break;
613 case -ENOSYS:
614 printk(KERN_WARNING "LTTng: block device enumeration is not supported by kernel\n");
615 break;
616 default:
617 return ret;
618 }
619
620 /* TODO lttng_dump_idt_table(session); */
621 /* TODO lttng_dump_softirq_vec(session); */
622 /* TODO lttng_list_modules(session); */
623 /* TODO lttng_dump_swap_files(session); */
624
625 /*
626 * Fire off a work queue on each CPU. Their sole purpose in life
627 * is to guarantee that each CPU has been in a state where is was in
628 * syscall mode (i.e. not in a trap, an IRQ or a soft IRQ).
629 */
630 get_online_cpus();
631 atomic_set(&kernel_threads_to_run, num_online_cpus());
632 for_each_online_cpu(cpu) {
633 INIT_DELAYED_WORK(&cpu_work[cpu], lttng_statedump_work_func);
634 schedule_delayed_work_on(cpu, &cpu_work[cpu], 0);
635 }
636 /* Wait for all threads to run */
637 __wait_event(statedump_wq, (atomic_read(&kernel_threads_to_run) == 0));
638 put_online_cpus();
639 /* Our work is done */
640 trace_lttng_statedump_end(session);
641 return 0;
642 }
643
644 /*
645 * Called with session mutex held.
646 */
647 int lttng_statedump_start(struct lttng_session *session)
648 {
649 return do_lttng_statedump(session);
650 }
651 EXPORT_SYMBOL_GPL(lttng_statedump_start);
652
653 static
654 int __init lttng_statedump_init(void)
655 {
656 /*
657 * Allow module to load even if the fixup cannot be done. This
658 * will allow seemless transition when the underlying issue fix
659 * is merged into the Linux kernel, and when tracepoint.c
660 * "tracepoint_module_notify" is turned into a static function.
661 */
662 (void) wrapper_lttng_fixup_sig(THIS_MODULE);
663 return 0;
664 }
665
666 module_init(lttng_statedump_init);
667
668 static
669 void __exit lttng_statedump_exit(void)
670 {
671 }
672
673 module_exit(lttng_statedump_exit);
674
675 MODULE_LICENSE("GPL and additional rights");
676 MODULE_AUTHOR("Jean-Hugues Deschenes");
677 MODULE_DESCRIPTION("LTTng statedump provider");
678 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
679 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
680 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
681 LTTNG_MODULES_EXTRAVERSION);
This page took 0.042092 seconds and 4 git commands to generate.