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