Fix: dev_base_lock removed in linux 6.9-rc1
[lttng-modules.git] / src / 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/netdevice.h>
27 #include <linux/inetdevice.h>
28 #include <linux/mm.h>
29 #include <linux/swap.h>
30 #include <linux/wait.h>
31 #include <linux/mutex.h>
32 #include <linux/device.h>
33
34 #include <linux/blkdev.h>
35
36 #include <lttng/events.h>
37 #include <lttng/tracer.h>
38 #include <wrapper/cpu.h>
39 #include <wrapper/irqdesc.h>
40 #include <wrapper/fdtable.h>
41 #include <wrapper/namespace.h>
42 #include <wrapper/irq.h>
43 #include <wrapper/tracepoint.h>
44 #include <wrapper/genhd.h>
45 #include <wrapper/file.h>
46 #include <wrapper/fdtable.h>
47 #include <wrapper/sched.h>
48
49 #ifdef CONFIG_LTTNG_HAS_LIST_IRQ
50 #include <linux/irq.h>
51 #endif
52
53 /* Define the tracepoints, but do not build the probes */
54 #define CREATE_TRACE_POINTS
55 #define TRACE_INCLUDE_PATH instrumentation/events
56 #define TRACE_INCLUDE_FILE lttng-statedump
57 #define LTTNG_INSTRUMENTATION
58 #include <instrumentation/events/lttng-statedump.h>
59
60 LTTNG_DEFINE_TRACE(lttng_statedump_block_device,
61 TP_PROTO(struct lttng_kernel_session *session,
62 dev_t dev, const char *diskname),
63 TP_ARGS(session, dev, diskname));
64
65 LTTNG_DEFINE_TRACE(lttng_statedump_end,
66 TP_PROTO(struct lttng_kernel_session *session),
67 TP_ARGS(session));
68
69 LTTNG_DEFINE_TRACE(lttng_statedump_interrupt,
70 TP_PROTO(struct lttng_kernel_session *session,
71 unsigned int irq, const char *chip_name,
72 struct irqaction *action),
73 TP_ARGS(session, irq, chip_name, action));
74
75 LTTNG_DEFINE_TRACE(lttng_statedump_file_descriptor,
76 TP_PROTO(struct lttng_kernel_session *session,
77 struct files_struct *files,
78 int fd, const char *filename,
79 unsigned int flags, fmode_t fmode),
80 TP_ARGS(session, files, fd, filename, flags, fmode));
81
82 LTTNG_DEFINE_TRACE(lttng_statedump_start,
83 TP_PROTO(struct lttng_kernel_session *session),
84 TP_ARGS(session));
85
86 LTTNG_DEFINE_TRACE(lttng_statedump_process_state,
87 TP_PROTO(struct lttng_kernel_session *session,
88 struct task_struct *p,
89 int type, int mode, int submode, int status,
90 struct files_struct *files),
91 TP_ARGS(session, p, type, mode, submode, status, files));
92
93 LTTNG_DEFINE_TRACE(lttng_statedump_process_pid_ns,
94 TP_PROTO(struct lttng_kernel_session *session,
95 struct task_struct *p,
96 struct pid_namespace *pid_ns),
97 TP_ARGS(session, p, pid_ns));
98
99 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,6,0))
100 LTTNG_DEFINE_TRACE(lttng_statedump_process_cgroup_ns,
101 TP_PROTO(struct lttng_kernel_session *session,
102 struct task_struct *p,
103 struct cgroup_namespace *cgroup_ns),
104 TP_ARGS(session, p, cgroup_ns));
105 #endif
106
107 LTTNG_DEFINE_TRACE(lttng_statedump_process_ipc_ns,
108 TP_PROTO(struct lttng_kernel_session *session,
109 struct task_struct *p,
110 struct ipc_namespace *ipc_ns),
111 TP_ARGS(session, p, ipc_ns));
112
113 #ifndef LTTNG_MNT_NS_MISSING_HEADER
114 LTTNG_DEFINE_TRACE(lttng_statedump_process_mnt_ns,
115 TP_PROTO(struct lttng_kernel_session *session,
116 struct task_struct *p,
117 struct mnt_namespace *mnt_ns),
118 TP_ARGS(session, p, mnt_ns));
119 #endif
120
121 LTTNG_DEFINE_TRACE(lttng_statedump_process_net_ns,
122 TP_PROTO(struct lttng_kernel_session *session,
123 struct task_struct *p,
124 struct net *net_ns),
125 TP_ARGS(session, p, net_ns));
126
127 LTTNG_DEFINE_TRACE(lttng_statedump_process_user_ns,
128 TP_PROTO(struct lttng_kernel_session *session,
129 struct task_struct *p,
130 struct user_namespace *user_ns),
131 TP_ARGS(session, p, user_ns));
132
133 LTTNG_DEFINE_TRACE(lttng_statedump_process_uts_ns,
134 TP_PROTO(struct lttng_kernel_session *session,
135 struct task_struct *p,
136 struct uts_namespace *uts_ns),
137 TP_ARGS(session, p, uts_ns));
138
139 LTTNG_DEFINE_TRACE(lttng_statedump_process_time_ns,
140 TP_PROTO(struct lttng_kernel_session *session,
141 struct task_struct *p,
142 struct time_namespace *time_ns),
143 TP_ARGS(session, p, time_ns));
144
145 LTTNG_DEFINE_TRACE(lttng_statedump_network_interface,
146 TP_PROTO(struct lttng_kernel_session *session,
147 struct net_device *dev, struct in_ifaddr *ifa),
148 TP_ARGS(session, dev, ifa));
149
150 #ifdef LTTNG_HAVE_STATEDUMP_CPU_TOPOLOGY
151 LTTNG_DEFINE_TRACE(lttng_statedump_cpu_topology,
152 TP_PROTO(struct lttng_kernel_session *session, struct cpuinfo_x86 *c),
153 TP_ARGS(session, c));
154 #endif
155
156 struct lttng_fd_ctx {
157 char *page;
158 struct lttng_kernel_session *session;
159 struct files_struct *files;
160 };
161
162 /*
163 * Protected by the trace lock.
164 */
165 static struct delayed_work cpu_work[NR_CPUS];
166 static DECLARE_WAIT_QUEUE_HEAD(statedump_wq);
167 static atomic_t kernel_threads_to_run;
168
169 enum lttng_thread_type {
170 LTTNG_USER_THREAD = 0,
171 LTTNG_KERNEL_THREAD = 1,
172 };
173
174 enum lttng_execution_mode {
175 LTTNG_USER_MODE = 0,
176 LTTNG_SYSCALL = 1,
177 LTTNG_TRAP = 2,
178 LTTNG_IRQ = 3,
179 LTTNG_SOFTIRQ = 4,
180 LTTNG_MODE_UNKNOWN = 5,
181 };
182
183 enum lttng_execution_submode {
184 LTTNG_NONE = 0,
185 LTTNG_UNKNOWN = 1,
186 };
187
188 enum lttng_process_status {
189 LTTNG_UNNAMED = 0,
190 LTTNG_WAIT_FORK = 1,
191 LTTNG_WAIT_CPU = 2,
192 LTTNG_EXIT = 3,
193 LTTNG_ZOMBIE = 4,
194 LTTNG_WAIT = 5,
195 LTTNG_RUN = 6,
196 LTTNG_DEAD = 7,
197 };
198
199
200 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(6,0,0) || \
201 LTTNG_RHEL_KERNEL_RANGE(5,14,0,163,0,0, 5,15,0,0,0,0))
202
203 #define LTTNG_PART_STRUCT_TYPE struct block_device
204
205 static
206 int lttng_get_part_name(struct gendisk *disk, struct block_device *part, char *name_buf)
207 {
208 int ret;
209
210 ret = snprintf(name_buf, BDEVNAME_SIZE, "%pg", part);
211 if (ret < 0 || ret >= BDEVNAME_SIZE)
212 return -ENOSYS;
213
214 return 0;
215 }
216
217 static
218 dev_t lttng_get_part_devt(struct block_device *part)
219 {
220 return part->bd_dev;
221 }
222
223 #elif (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,11,0))
224
225 #define LTTNG_PART_STRUCT_TYPE struct block_device
226
227 static
228 int lttng_get_part_name(struct gendisk *disk, struct block_device *part, char *name_buf)
229 {
230 const char *p;
231
232 p = bdevname(part, name_buf);
233 if (!p)
234 return -ENOSYS;
235
236 return 0;
237 }
238
239 static
240 dev_t lttng_get_part_devt(struct block_device *part)
241 {
242 return part->bd_dev;
243 }
244
245 #else
246
247 #define LTTNG_PART_STRUCT_TYPE struct hd_struct
248
249 static
250 int lttng_get_part_name(struct gendisk *disk, struct hd_struct *part, char *name_buf)
251 {
252 const char *p;
253 struct block_device bdev;
254
255 /*
256 * Create a partial 'struct blockdevice' to use
257 * 'bdevname()' which is a simple wrapper over
258 * 'disk_name()' but has the honor to be EXPORT_SYMBOL.
259 */
260 bdev.bd_disk = disk;
261 bdev.bd_part = part;
262
263 p = bdevname(&bdev, name_buf);
264 if (!p)
265 return -ENOSYS;
266
267 return 0;
268 }
269
270 static
271 dev_t lttng_get_part_devt(struct hd_struct *part)
272 {
273 return part_devt(part);
274 }
275 #endif
276
277 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,12,0))
278 static
279 int lttng_statedump_each_block_device(struct lttng_kernel_session *session, struct gendisk *disk)
280 {
281 struct block_device *part;
282 unsigned long idx;
283 int ret = 0;
284
285 /* Include partition 0 */
286 idx = 0;
287
288 rcu_read_lock();
289 xa_for_each(&disk->part_tbl, idx, part) {
290 char name_buf[BDEVNAME_SIZE];
291
292 /* Exclude non-partitions bdev and empty partitions. */
293 if (bdev_is_partition(part) && !bdev_nr_sectors(part))
294 continue;
295
296 if (lttng_get_part_name(disk, part, name_buf) == -ENOSYS) {
297 ret = -ENOSYS;
298 goto end;
299 }
300 trace_lttng_statedump_block_device(session, lttng_get_part_devt(part),
301 name_buf);
302 }
303 end:
304 rcu_read_unlock();
305 return ret;
306 }
307 #else
308 static
309 int lttng_statedump_each_block_device(struct lttng_kernel_session *session, struct gendisk *disk)
310 {
311 struct disk_part_iter piter;
312 LTTNG_PART_STRUCT_TYPE *part;
313
314 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
315
316 while ((part = disk_part_iter_next(&piter))) {
317 char name_buf[BDEVNAME_SIZE];
318
319 if (lttng_get_part_name(disk, part, name_buf) == -ENOSYS) {
320 disk_part_iter_exit(&piter);
321 return -ENOSYS;
322 }
323 trace_lttng_statedump_block_device(session, lttng_get_part_devt(part),
324 name_buf);
325 }
326 disk_part_iter_exit(&piter);
327
328 return 0;
329 }
330 #endif
331
332 static
333 int lttng_enumerate_block_devices(struct lttng_kernel_session *session)
334 {
335 struct class *ptr_block_class;
336 struct device_type *ptr_disk_type;
337 struct class_dev_iter iter;
338 struct device *dev;
339 int ret = 0;
340
341 ptr_block_class = wrapper_get_block_class();
342 if (!ptr_block_class) {
343 ret = -ENOSYS;
344 goto end;
345 }
346 ptr_disk_type = wrapper_get_disk_type();
347 if (!ptr_disk_type) {
348 ret = -ENOSYS;
349 goto end;
350 }
351 class_dev_iter_init(&iter, ptr_block_class, NULL, ptr_disk_type);
352 while ((dev = class_dev_iter_next(&iter))) {
353 struct gendisk *disk = dev_to_disk(dev);
354
355 /*
356 * Don't show empty devices or things that have been
357 * suppressed
358 */
359 if (get_capacity(disk) == 0 ||
360 (disk->flags & LTTNG_GENHD_FL_HIDDEN))
361 continue;
362
363 ret = lttng_statedump_each_block_device(session, disk);
364 }
365 class_dev_iter_exit(&iter);
366 end:
367 return ret;
368 }
369
370 #ifdef CONFIG_INET
371
372 static
373 void lttng_enumerate_device(struct lttng_kernel_session *session,
374 struct net_device *dev)
375 {
376 struct in_device *in_dev;
377 struct in_ifaddr *ifa;
378
379 if (dev->flags & IFF_UP) {
380 in_dev = in_dev_get(dev);
381 if (in_dev) {
382 for (ifa = in_dev->ifa_list; ifa != NULL;
383 ifa = ifa->ifa_next) {
384 trace_lttng_statedump_network_interface(
385 session, dev, ifa);
386 }
387 in_dev_put(in_dev);
388 }
389 } else {
390 trace_lttng_statedump_network_interface(
391 session, dev, NULL);
392 }
393 }
394
395 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(6,9,0))
396 static
397 int lttng_enumerate_network_ip_interface(struct lttng_kernel_session *session)
398 {
399 struct net_device *dev;
400
401 rtnl_lock();
402 for_each_netdev(&init_net, dev)
403 lttng_enumerate_device(session, dev);
404 rtnl_unlock();
405
406 return 0;
407 }
408 #else
409 static
410 int lttng_enumerate_network_ip_interface(struct lttng_kernel_session *session)
411 {
412 struct net_device *dev;
413
414 read_lock(&dev_base_lock);
415 for_each_netdev(&init_net, dev)
416 lttng_enumerate_device(session, dev);
417 read_unlock(&dev_base_lock);
418
419 return 0;
420 }
421 #endif /* (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(6,9,0)) */
422 #else /* CONFIG_INET */
423 static inline
424 int lttng_enumerate_network_ip_interface(struct lttng_kernel_session *session)
425 {
426 return 0;
427 }
428 #endif /* CONFIG_INET */
429
430 static
431 int lttng_dump_one_fd(const void *p, struct file *file, unsigned int fd)
432 {
433 const struct lttng_fd_ctx *ctx = p;
434 const char *s = d_path(&file->f_path, ctx->page, PAGE_SIZE);
435 unsigned int flags = file->f_flags;
436 struct fdtable *fdt;
437
438 /*
439 * We don't expose kernel internal flags, only userspace-visible
440 * flags.
441 */
442 flags &= ~FMODE_NONOTIFY;
443 fdt = files_fdtable(ctx->files);
444 /*
445 * We need to check here again whether fd is within the fdt
446 * max_fds range, because we might be seeing a different
447 * files_fdtable() than iterate_fd(), assuming only RCU is
448 * protecting the read. In reality, iterate_fd() holds
449 * file_lock, which should ensure the fdt does not change while
450 * the lock is taken, but we are not aware whether this is
451 * guaranteed or not, so play safe.
452 */
453 if (fd < fdt->max_fds && lttng_close_on_exec(fd, fdt))
454 flags |= O_CLOEXEC;
455 if (IS_ERR(s)) {
456 struct dentry *dentry = file->f_path.dentry;
457
458 /* Make sure we give at least some info */
459 spin_lock(&dentry->d_lock);
460 trace_lttng_statedump_file_descriptor(ctx->session,
461 ctx->files, fd, dentry->d_name.name, flags,
462 file->f_mode);
463 spin_unlock(&dentry->d_lock);
464 goto end;
465 }
466 trace_lttng_statedump_file_descriptor(ctx->session,
467 ctx->files, fd, s, flags, file->f_mode);
468 end:
469 return 0;
470 }
471
472 /* Called with task lock held. */
473 static
474 void lttng_enumerate_files(struct lttng_kernel_session *session,
475 struct files_struct *files,
476 char *tmp)
477 {
478 struct lttng_fd_ctx ctx = { .page = tmp, .session = session, .files = files, };
479
480 lttng_iterate_fd(files, 0, lttng_dump_one_fd, &ctx);
481 }
482
483 #ifdef LTTNG_HAVE_STATEDUMP_CPU_TOPOLOGY
484 static
485 int lttng_enumerate_cpu_topology(struct lttng_kernel_session *session)
486 {
487 int cpu;
488 const cpumask_t *cpumask = cpu_possible_mask;
489
490 for (cpu = cpumask_first(cpumask); cpu < nr_cpu_ids;
491 cpu = cpumask_next(cpu, cpumask)) {
492 trace_lttng_statedump_cpu_topology(session, &cpu_data(cpu));
493 }
494
495 return 0;
496 }
497 #else
498 static
499 int lttng_enumerate_cpu_topology(struct lttng_kernel_session *session)
500 {
501 return 0;
502 }
503 #endif
504
505 #if 0
506 /*
507 * FIXME: we cannot take a mmap_sem while in a RCU read-side critical section
508 * (scheduling in atomic). Normally, the tasklist lock protects this kind of
509 * iteration, but it is not exported to modules.
510 */
511 static
512 void lttng_enumerate_task_vm_maps(struct lttng_kernel_session *session,
513 struct task_struct *p)
514 {
515 struct mm_struct *mm;
516 struct vm_area_struct *map;
517 unsigned long ino;
518
519 /* get_task_mm does a task_lock... */
520 mm = get_task_mm(p);
521 if (!mm)
522 return;
523
524 map = mm->mmap;
525 if (map) {
526 down_read(&mm->mmap_sem);
527 while (map) {
528 if (map->vm_file)
529 ino = map->vm_file->lttng_f_dentry->d_inode->i_ino;
530 else
531 ino = 0;
532 trace_lttng_statedump_vm_map(session, p, map, ino);
533 map = map->vm_next;
534 }
535 up_read(&mm->mmap_sem);
536 }
537 mmput(mm);
538 }
539
540 static
541 int lttng_enumerate_vm_maps(struct lttng_kernel_session *session)
542 {
543 struct task_struct *p;
544
545 rcu_read_lock();
546 for_each_process(p)
547 lttng_enumerate_task_vm_maps(session, p);
548 rcu_read_unlock();
549 return 0;
550 }
551 #endif
552
553 #ifdef CONFIG_LTTNG_HAS_LIST_IRQ
554
555 static
556 int lttng_list_interrupts(struct lttng_kernel_session *session)
557 {
558 unsigned int irq;
559 unsigned long flags = 0;
560 struct irq_desc *desc;
561
562 #define irq_to_desc wrapper_irq_to_desc
563 /* needs irq_desc */
564 for_each_irq_desc(irq, desc) {
565 struct irqaction *action;
566 const char *irq_chip_name =
567 irq_desc_get_chip(desc)->name ? : "unnamed_irq_chip";
568
569 local_irq_save(flags);
570 raw_spin_lock(&desc->lock);
571 for (action = desc->action; action; action = action->next) {
572 trace_lttng_statedump_interrupt(session,
573 irq, irq_chip_name, action);
574 }
575 raw_spin_unlock(&desc->lock);
576 local_irq_restore(flags);
577 }
578 return 0;
579 #undef irq_to_desc
580 }
581 #else
582 static inline
583 int lttng_list_interrupts(struct lttng_kernel_session *session)
584 {
585 return 0;
586 }
587 #endif
588
589 /*
590 * Statedump the task's namespaces using the proc filesystem inode number as
591 * the unique identifier. The user and pid ns are nested and will be dumped
592 * recursively.
593 *
594 * Called with task lock held.
595 */
596 static
597 void lttng_statedump_process_ns(struct lttng_kernel_session *session,
598 struct task_struct *p,
599 enum lttng_thread_type type,
600 enum lttng_execution_mode mode,
601 enum lttng_execution_submode submode,
602 enum lttng_process_status status)
603 {
604 struct nsproxy *proxy;
605 struct pid_namespace *pid_ns;
606 struct user_namespace *user_ns;
607
608 /*
609 * The pid and user namespaces are special, they are nested and
610 * accessed with specific functions instead of the nsproxy struct
611 * like the other namespaces.
612 */
613 pid_ns = task_active_pid_ns(p);
614 do {
615 trace_lttng_statedump_process_pid_ns(session, p, pid_ns);
616 pid_ns = pid_ns ? pid_ns->parent : NULL;
617 } while (pid_ns);
618
619
620 user_ns = task_cred_xxx(p, user_ns);
621 do {
622 trace_lttng_statedump_process_user_ns(session, p, user_ns);
623 /*
624 * trace_lttng_statedump_process_user_ns() internally
625 * checks whether user_ns is NULL. While this does not
626 * appear to be a possible return value for
627 * task_cred_xxx(), err on the safe side and check
628 * for NULL here as well to be consistent with the
629 * paranoid behavior of
630 * trace_lttng_statedump_process_user_ns().
631 */
632 user_ns = user_ns ? user_ns->lttng_user_ns_parent : NULL;
633 } while (user_ns);
634
635 /*
636 * Back and forth on locking strategy within Linux upstream for nsproxy.
637 * See Linux upstream commit 728dba3a39c66b3d8ac889ddbe38b5b1c264aec3
638 * "namespaces: Use task_lock and not rcu to protect nsproxy"
639 * for details.
640 */
641 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(3,17,0) || \
642 LTTNG_UBUNTU_KERNEL_RANGE(3,13,11,36, 3,14,0,0) || \
643 LTTNG_UBUNTU_KERNEL_RANGE(3,16,1,11, 3,17,0,0) || \
644 LTTNG_RHEL_KERNEL_RANGE(3,10,0,229,13,0, 3,11,0,0,0,0))
645 proxy = p->nsproxy;
646 #else
647 rcu_read_lock();
648 proxy = task_nsproxy(p);
649 #endif
650 if (proxy) {
651 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,6,0))
652 trace_lttng_statedump_process_cgroup_ns(session, p, proxy->cgroup_ns);
653 #endif
654 trace_lttng_statedump_process_ipc_ns(session, p, proxy->ipc_ns);
655 #ifndef LTTNG_MNT_NS_MISSING_HEADER
656 trace_lttng_statedump_process_mnt_ns(session, p, proxy->mnt_ns);
657 #endif
658 trace_lttng_statedump_process_net_ns(session, p, proxy->net_ns);
659 trace_lttng_statedump_process_uts_ns(session, p, proxy->uts_ns);
660 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,6,0) || \
661 LTTNG_RHEL_KERNEL_RANGE(4,18,0,305,0,0, 4,19,0,0,0,0))
662 trace_lttng_statedump_process_time_ns(session, p, proxy->time_ns);
663 #endif
664 }
665 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(3,17,0) || \
666 LTTNG_UBUNTU_KERNEL_RANGE(3,13,11,36, 3,14,0,0) || \
667 LTTNG_UBUNTU_KERNEL_RANGE(3,16,1,11, 3,17,0,0) || \
668 LTTNG_RHEL_KERNEL_RANGE(3,10,0,229,13,0, 3,11,0,0,0,0))
669 /* (nothing) */
670 #else
671 rcu_read_unlock();
672 #endif
673 }
674
675 static
676 int lttng_enumerate_process_states(struct lttng_kernel_session *session)
677 {
678 struct task_struct *g, *p;
679 char *tmp;
680
681 tmp = (char *) __get_free_page(GFP_KERNEL);
682 if (!tmp)
683 return -ENOMEM;
684
685 rcu_read_lock();
686 for_each_process(g) {
687 struct files_struct *prev_files = NULL;
688
689 p = g;
690 do {
691 enum lttng_execution_mode mode =
692 LTTNG_MODE_UNKNOWN;
693 enum lttng_execution_submode submode =
694 LTTNG_UNKNOWN;
695 enum lttng_process_status status;
696 enum lttng_thread_type type;
697 struct files_struct *files;
698
699 task_lock(p);
700 if (p->exit_state == EXIT_ZOMBIE)
701 status = LTTNG_ZOMBIE;
702 else if (p->exit_state == EXIT_DEAD)
703 status = LTTNG_DEAD;
704 else if (lttng_task_is_running(p)) {
705 /* Is this a forked child that has not run yet? */
706 if (list_empty(&p->rt.run_list))
707 status = LTTNG_WAIT_FORK;
708 else
709 /*
710 * All tasks are considered as wait_cpu;
711 * the viewer will sort out if the task
712 * was really running at this time.
713 */
714 status = LTTNG_WAIT_CPU;
715 } else if (lttng_get_task_state(p) &
716 (TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)) {
717 /* Task is waiting for something to complete */
718 status = LTTNG_WAIT;
719 } else
720 status = LTTNG_UNNAMED;
721 submode = LTTNG_NONE;
722
723 /*
724 * Verification of t->mm is to filter out kernel
725 * threads; Viewer will further filter out if a
726 * user-space thread was in syscall mode or not.
727 */
728 if (p->mm)
729 type = LTTNG_USER_THREAD;
730 else
731 type = LTTNG_KERNEL_THREAD;
732 files = p->files;
733
734 trace_lttng_statedump_process_state(session,
735 p, type, mode, submode, status, files);
736 lttng_statedump_process_ns(session,
737 p, type, mode, submode, status);
738 /*
739 * As an optimisation for the common case, do not
740 * repeat information for the same files_struct in
741 * two consecutive threads. This is the common case
742 * for threads sharing the same fd table. RCU guarantees
743 * that the same files_struct pointer is not re-used
744 * throughout processes/threads iteration.
745 */
746 if (files && files != prev_files) {
747 lttng_enumerate_files(session, files, tmp);
748 prev_files = files;
749 }
750 task_unlock(p);
751 } while_each_thread(g, p);
752 }
753 rcu_read_unlock();
754
755 free_page((unsigned long) tmp);
756
757 return 0;
758 }
759
760 static
761 void lttng_statedump_work_func(struct work_struct *work)
762 {
763 if (atomic_dec_and_test(&kernel_threads_to_run))
764 /* If we are the last thread, wake up do_lttng_statedump */
765 wake_up(&statedump_wq);
766 }
767
768 static
769 int do_lttng_statedump(struct lttng_kernel_session *session)
770 {
771 int cpu, ret;
772
773 trace_lttng_statedump_start(session);
774 ret = lttng_enumerate_process_states(session);
775 if (ret)
776 return ret;
777 /*
778 * FIXME
779 * ret = lttng_enumerate_vm_maps(session);
780 * if (ret)
781 * return ret;
782 */
783 ret = lttng_list_interrupts(session);
784 if (ret)
785 return ret;
786 ret = lttng_enumerate_network_ip_interface(session);
787 if (ret)
788 return ret;
789 ret = lttng_enumerate_block_devices(session);
790 switch (ret) {
791 case 0:
792 break;
793 case -ENOSYS:
794 printk(KERN_WARNING "LTTng: block device enumeration is not supported by kernel\n");
795 break;
796 default:
797 return ret;
798 }
799 ret = lttng_enumerate_cpu_topology(session);
800 if (ret)
801 return ret;
802
803 /* TODO lttng_dump_idt_table(session); */
804 /* TODO lttng_dump_softirq_vec(session); */
805 /* TODO lttng_list_modules(session); */
806 /* TODO lttng_dump_swap_files(session); */
807
808 /*
809 * Fire off a work queue on each CPU. Their sole purpose in life
810 * is to guarantee that each CPU has been in a state where is was in
811 * syscall mode (i.e. not in a trap, an IRQ or a soft IRQ).
812 */
813 lttng_cpus_read_lock();
814 atomic_set(&kernel_threads_to_run, num_online_cpus());
815 for_each_online_cpu(cpu) {
816 INIT_DELAYED_WORK(&cpu_work[cpu], lttng_statedump_work_func);
817 schedule_delayed_work_on(cpu, &cpu_work[cpu], 0);
818 }
819 /* Wait for all threads to run */
820 __wait_event(statedump_wq, (atomic_read(&kernel_threads_to_run) == 0));
821 lttng_cpus_read_unlock();
822 /* Our work is done */
823 trace_lttng_statedump_end(session);
824 return 0;
825 }
826
827 /*
828 * Called with session mutex held.
829 */
830 int lttng_statedump_start(struct lttng_kernel_session *session)
831 {
832 return do_lttng_statedump(session);
833 }
834 EXPORT_SYMBOL_GPL(lttng_statedump_start);
835
836 static
837 int __init lttng_statedump_init(void)
838 {
839 /*
840 * Allow module to load even if the fixup cannot be done. This
841 * will allow seemless transition when the underlying issue fix
842 * is merged into the Linux kernel, and when tracepoint.c
843 * "tracepoint_module_notify" is turned into a static function.
844 */
845 (void) wrapper_lttng_fixup_sig(THIS_MODULE);
846 return 0;
847 }
848
849 module_init(lttng_statedump_init);
850
851 static
852 void __exit lttng_statedump_exit(void)
853 {
854 }
855
856 module_exit(lttng_statedump_exit);
857
858 MODULE_LICENSE("GPL and additional rights");
859 MODULE_AUTHOR("Jean-Hugues Deschenes");
860 MODULE_DESCRIPTION("LTTng statedump provider");
861 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
862 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
863 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
864 LTTNG_MODULES_EXTRAVERSION);
This page took 0.046544 seconds and 4 git commands to generate.