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