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