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