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