6eb838752111ab207fe94dca18ece53fc85b0161
[lttng-modules.git] / src / lttng-abi.c
1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
2 *
3 * lttng-abi.c
4 *
5 * LTTng ABI
6 *
7 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
9 * Mimic system calls for:
10 * - session creation, returns a file descriptor or failure.
11 * - channel creation, returns a file descriptor or failure.
12 * - Operates on a session file descriptor
13 * - Takes all channel options as parameters.
14 * - stream get, returns a file descriptor or failure.
15 * - Operates on a channel file descriptor.
16 * - stream notifier get, returns a file descriptor or failure.
17 * - Operates on a channel file descriptor.
18 * - event creation, returns a file descriptor or failure.
19 * - Operates on a channel file descriptor
20 * - Takes an event name as parameter
21 * - Takes an instrumentation source as parameter
22 * - e.g. tracepoints, dynamic_probes...
23 * - Takes instrumentation source specific arguments.
24 */
25
26 #include <linux/module.h>
27 #include <linux/proc_fs.h>
28 #include <linux/anon_inodes.h>
29 #include <linux/file.h>
30 #include <linux/uaccess.h>
31 #include <linux/slab.h>
32 #include <linux/err.h>
33 #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_mappings() */
34 #include <ringbuffer/vfs.h>
35 #include <ringbuffer/backend.h>
36 #include <ringbuffer/frontend.h>
37 #include <wrapper/poll.h>
38 #include <wrapper/file.h>
39 #include <wrapper/kref.h>
40 #include <lttng/string-utils.h>
41 #include <lttng/abi.h>
42 #include <lttng/abi-old.h>
43 #include <lttng/events.h>
44 #include <lttng/tracer.h>
45 #include <lttng/tp-mempool.h>
46 #include <ringbuffer/frontend_types.h>
47
48 /*
49 * This is LTTng's own personal way to create a system call as an external
50 * module. We use ioctl() on /proc/lttng.
51 */
52
53 static struct proc_dir_entry *lttng_proc_dentry;
54
55 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0))
56 static const struct proc_ops lttng_proc_ops;
57 #else
58 static const struct file_operations lttng_proc_ops;
59 #endif
60
61 static const struct file_operations lttng_session_fops;
62 static const struct file_operations lttng_event_notifier_group_fops;
63 static const struct file_operations lttng_channel_fops;
64 static const struct file_operations lttng_metadata_fops;
65 static const struct file_operations lttng_event_fops;
66 static struct file_operations lttng_stream_ring_buffer_file_operations;
67
68 static int put_u64(uint64_t val, unsigned long arg);
69 static int put_u32(uint32_t val, unsigned long arg);
70
71 /*
72 * Teardown management: opened file descriptors keep a refcount on the module,
73 * so it can only exit when all file descriptors are closed.
74 */
75
76 static
77 int lttng_abi_create_session(void)
78 {
79 struct lttng_session *session;
80 struct file *session_file;
81 int session_fd, ret;
82
83 session = lttng_session_create();
84 if (!session)
85 return -ENOMEM;
86 session_fd = lttng_get_unused_fd();
87 if (session_fd < 0) {
88 ret = session_fd;
89 goto fd_error;
90 }
91 session_file = anon_inode_getfile("[lttng_session]",
92 &lttng_session_fops,
93 session, O_RDWR);
94 if (IS_ERR(session_file)) {
95 ret = PTR_ERR(session_file);
96 goto file_error;
97 }
98 session->file = session_file;
99 fd_install(session_fd, session_file);
100 return session_fd;
101
102 file_error:
103 put_unused_fd(session_fd);
104 fd_error:
105 lttng_session_destroy(session);
106 return ret;
107 }
108
109 void event_notifier_send_notification_work_wakeup(struct irq_work *entry)
110 {
111 struct lttng_event_notifier_group *event_notifier_group =
112 container_of(entry, struct lttng_event_notifier_group,
113 wakeup_pending);
114 wake_up_interruptible(&event_notifier_group->read_wait);
115 }
116
117 static
118 int lttng_abi_create_event_notifier_group(void)
119 {
120 struct lttng_event_notifier_group *event_notifier_group;
121 struct file *event_notifier_group_file;
122 int event_notifier_group_fd, ret;
123
124 event_notifier_group = lttng_event_notifier_group_create();
125 if (!event_notifier_group)
126 return -ENOMEM;
127
128 event_notifier_group_fd = lttng_get_unused_fd();
129 if (event_notifier_group_fd < 0) {
130 ret = event_notifier_group_fd;
131 goto fd_error;
132 }
133 event_notifier_group_file = anon_inode_getfile("[lttng_event_notifier_group]",
134 &lttng_event_notifier_group_fops,
135 event_notifier_group, O_RDWR);
136 if (IS_ERR(event_notifier_group_file)) {
137 ret = PTR_ERR(event_notifier_group_file);
138 goto file_error;
139 }
140
141 event_notifier_group->file = event_notifier_group_file;
142 init_waitqueue_head(&event_notifier_group->read_wait);
143 init_irq_work(&event_notifier_group->wakeup_pending,
144 event_notifier_send_notification_work_wakeup);
145 fd_install(event_notifier_group_fd, event_notifier_group_file);
146 return event_notifier_group_fd;
147
148 file_error:
149 put_unused_fd(event_notifier_group_fd);
150 fd_error:
151 lttng_event_notifier_group_destroy(event_notifier_group);
152 return ret;
153 }
154
155 static
156 int lttng_abi_tracepoint_list(void)
157 {
158 struct file *tracepoint_list_file;
159 int file_fd, ret;
160
161 file_fd = lttng_get_unused_fd();
162 if (file_fd < 0) {
163 ret = file_fd;
164 goto fd_error;
165 }
166
167 tracepoint_list_file = anon_inode_getfile("[lttng_tracepoint_list]",
168 &lttng_tracepoint_list_fops,
169 NULL, O_RDWR);
170 if (IS_ERR(tracepoint_list_file)) {
171 ret = PTR_ERR(tracepoint_list_file);
172 goto file_error;
173 }
174 ret = lttng_tracepoint_list_fops.open(NULL, tracepoint_list_file);
175 if (ret < 0)
176 goto open_error;
177 fd_install(file_fd, tracepoint_list_file);
178 return file_fd;
179
180 open_error:
181 fput(tracepoint_list_file);
182 file_error:
183 put_unused_fd(file_fd);
184 fd_error:
185 return ret;
186 }
187
188 #ifndef CONFIG_HAVE_SYSCALL_TRACEPOINTS
189 static inline
190 int lttng_abi_syscall_list(void)
191 {
192 return -ENOSYS;
193 }
194 #else
195 static
196 int lttng_abi_syscall_list(void)
197 {
198 struct file *syscall_list_file;
199 int file_fd, ret;
200
201 file_fd = lttng_get_unused_fd();
202 if (file_fd < 0) {
203 ret = file_fd;
204 goto fd_error;
205 }
206
207 syscall_list_file = anon_inode_getfile("[lttng_syscall_list]",
208 &lttng_syscall_list_fops,
209 NULL, O_RDWR);
210 if (IS_ERR(syscall_list_file)) {
211 ret = PTR_ERR(syscall_list_file);
212 goto file_error;
213 }
214 ret = lttng_syscall_list_fops.open(NULL, syscall_list_file);
215 if (ret < 0)
216 goto open_error;
217 fd_install(file_fd, syscall_list_file);
218 return file_fd;
219
220 open_error:
221 fput(syscall_list_file);
222 file_error:
223 put_unused_fd(file_fd);
224 fd_error:
225 return ret;
226 }
227 #endif
228
229 static
230 void lttng_abi_tracer_version(struct lttng_kernel_tracer_version *v)
231 {
232 v->major = LTTNG_MODULES_MAJOR_VERSION;
233 v->minor = LTTNG_MODULES_MINOR_VERSION;
234 v->patchlevel = LTTNG_MODULES_PATCHLEVEL_VERSION;
235 }
236
237 static
238 void lttng_abi_tracer_abi_version(struct lttng_kernel_tracer_abi_version *v)
239 {
240 v->major = LTTNG_MODULES_ABI_MAJOR_VERSION;
241 v->minor = LTTNG_MODULES_ABI_MINOR_VERSION;
242 }
243
244 static
245 long lttng_abi_add_context(struct file *file,
246 struct lttng_kernel_context *context_param,
247 struct lttng_ctx **ctx, struct lttng_session *session)
248 {
249
250 if (session->been_active)
251 return -EPERM;
252
253 switch (context_param->ctx) {
254 case LTTNG_KERNEL_CONTEXT_PID:
255 return lttng_add_pid_to_ctx(ctx);
256 case LTTNG_KERNEL_CONTEXT_PRIO:
257 return lttng_add_prio_to_ctx(ctx);
258 case LTTNG_KERNEL_CONTEXT_NICE:
259 return lttng_add_nice_to_ctx(ctx);
260 case LTTNG_KERNEL_CONTEXT_VPID:
261 return lttng_add_vpid_to_ctx(ctx);
262 case LTTNG_KERNEL_CONTEXT_TID:
263 return lttng_add_tid_to_ctx(ctx);
264 case LTTNG_KERNEL_CONTEXT_VTID:
265 return lttng_add_vtid_to_ctx(ctx);
266 case LTTNG_KERNEL_CONTEXT_PPID:
267 return lttng_add_ppid_to_ctx(ctx);
268 case LTTNG_KERNEL_CONTEXT_VPPID:
269 return lttng_add_vppid_to_ctx(ctx);
270 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER:
271 context_param->u.perf_counter.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
272 return lttng_add_perf_counter_to_ctx(context_param->u.perf_counter.type,
273 context_param->u.perf_counter.config,
274 context_param->u.perf_counter.name,
275 ctx);
276 case LTTNG_KERNEL_CONTEXT_PROCNAME:
277 return lttng_add_procname_to_ctx(ctx);
278 case LTTNG_KERNEL_CONTEXT_HOSTNAME:
279 return lttng_add_hostname_to_ctx(ctx);
280 case LTTNG_KERNEL_CONTEXT_CPU_ID:
281 return lttng_add_cpu_id_to_ctx(ctx);
282 case LTTNG_KERNEL_CONTEXT_INTERRUPTIBLE:
283 return lttng_add_interruptible_to_ctx(ctx);
284 case LTTNG_KERNEL_CONTEXT_NEED_RESCHEDULE:
285 return lttng_add_need_reschedule_to_ctx(ctx);
286 case LTTNG_KERNEL_CONTEXT_PREEMPTIBLE:
287 return lttng_add_preemptible_to_ctx(ctx);
288 case LTTNG_KERNEL_CONTEXT_MIGRATABLE:
289 return lttng_add_migratable_to_ctx(ctx);
290 case LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL:
291 case LTTNG_KERNEL_CONTEXT_CALLSTACK_USER:
292 return lttng_add_callstack_to_ctx(ctx, context_param->ctx);
293 case LTTNG_KERNEL_CONTEXT_CGROUP_NS:
294 return lttng_add_cgroup_ns_to_ctx(ctx);
295 case LTTNG_KERNEL_CONTEXT_IPC_NS:
296 return lttng_add_ipc_ns_to_ctx(ctx);
297 case LTTNG_KERNEL_CONTEXT_MNT_NS:
298 return lttng_add_mnt_ns_to_ctx(ctx);
299 case LTTNG_KERNEL_CONTEXT_NET_NS:
300 return lttng_add_net_ns_to_ctx(ctx);
301 case LTTNG_KERNEL_CONTEXT_PID_NS:
302 return lttng_add_pid_ns_to_ctx(ctx);
303 case LTTNG_KERNEL_CONTEXT_USER_NS:
304 return lttng_add_user_ns_to_ctx(ctx);
305 case LTTNG_KERNEL_CONTEXT_UTS_NS:
306 return lttng_add_uts_ns_to_ctx(ctx);
307 case LTTNG_KERNEL_CONTEXT_UID:
308 return lttng_add_uid_to_ctx(ctx);
309 case LTTNG_KERNEL_CONTEXT_EUID:
310 return lttng_add_euid_to_ctx(ctx);
311 case LTTNG_KERNEL_CONTEXT_SUID:
312 return lttng_add_suid_to_ctx(ctx);
313 case LTTNG_KERNEL_CONTEXT_GID:
314 return lttng_add_gid_to_ctx(ctx);
315 case LTTNG_KERNEL_CONTEXT_EGID:
316 return lttng_add_egid_to_ctx(ctx);
317 case LTTNG_KERNEL_CONTEXT_SGID:
318 return lttng_add_sgid_to_ctx(ctx);
319 case LTTNG_KERNEL_CONTEXT_VUID:
320 return lttng_add_vuid_to_ctx(ctx);
321 case LTTNG_KERNEL_CONTEXT_VEUID:
322 return lttng_add_veuid_to_ctx(ctx);
323 case LTTNG_KERNEL_CONTEXT_VSUID:
324 return lttng_add_vsuid_to_ctx(ctx);
325 case LTTNG_KERNEL_CONTEXT_VGID:
326 return lttng_add_vgid_to_ctx(ctx);
327 case LTTNG_KERNEL_CONTEXT_VEGID:
328 return lttng_add_vegid_to_ctx(ctx);
329 case LTTNG_KERNEL_CONTEXT_VSGID:
330 return lttng_add_vsgid_to_ctx(ctx);
331 case LTTNG_KERNEL_CONTEXT_TIME_NS:
332 return lttng_add_time_ns_to_ctx(ctx);
333 default:
334 return -EINVAL;
335 }
336 }
337
338 /**
339 * lttng_ioctl - lttng syscall through ioctl
340 *
341 * @file: the file
342 * @cmd: the command
343 * @arg: command arg
344 *
345 * This ioctl implements lttng commands:
346 * LTTNG_KERNEL_SESSION
347 * Returns a LTTng trace session file descriptor
348 * LTTNG_KERNEL_TRACER_VERSION
349 * Returns the LTTng kernel tracer version
350 * LTTNG_KERNEL_TRACEPOINT_LIST
351 * Returns a file descriptor listing available tracepoints
352 * LTTNG_KERNEL_WAIT_QUIESCENT
353 * Returns after all previously running probes have completed
354 * LTTNG_KERNEL_TRACER_ABI_VERSION
355 * Returns the LTTng kernel tracer ABI version
356 * LTTNG_KERNEL_EVENT_NOTIFIER_GROUP_CREATE
357 * Returns a LTTng event notifier group file descriptor
358 *
359 * The returned session will be deleted when its file descriptor is closed.
360 */
361 static
362 long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
363 {
364 switch (cmd) {
365 case LTTNG_KERNEL_OLD_SESSION:
366 case LTTNG_KERNEL_SESSION:
367 return lttng_abi_create_session();
368 case LTTNG_KERNEL_EVENT_NOTIFIER_GROUP_CREATE:
369 return lttng_abi_create_event_notifier_group();
370 case LTTNG_KERNEL_OLD_TRACER_VERSION:
371 {
372 struct lttng_kernel_tracer_version v;
373 struct lttng_kernel_old_tracer_version oldv;
374 struct lttng_kernel_old_tracer_version *uversion =
375 (struct lttng_kernel_old_tracer_version __user *) arg;
376
377 lttng_abi_tracer_version(&v);
378 oldv.major = v.major;
379 oldv.minor = v.minor;
380 oldv.patchlevel = v.patchlevel;
381
382 if (copy_to_user(uversion, &oldv, sizeof(oldv)))
383 return -EFAULT;
384 return 0;
385 }
386 case LTTNG_KERNEL_TRACER_VERSION:
387 {
388 struct lttng_kernel_tracer_version version;
389 struct lttng_kernel_tracer_version *uversion =
390 (struct lttng_kernel_tracer_version __user *) arg;
391
392 lttng_abi_tracer_version(&version);
393
394 if (copy_to_user(uversion, &version, sizeof(version)))
395 return -EFAULT;
396 return 0;
397 }
398 case LTTNG_KERNEL_TRACER_ABI_VERSION:
399 {
400 struct lttng_kernel_tracer_abi_version version;
401 struct lttng_kernel_tracer_abi_version *uversion =
402 (struct lttng_kernel_tracer_abi_version __user *) arg;
403
404 lttng_abi_tracer_abi_version(&version);
405
406 if (copy_to_user(uversion, &version, sizeof(version)))
407 return -EFAULT;
408 return 0;
409 }
410 case LTTNG_KERNEL_OLD_TRACEPOINT_LIST:
411 case LTTNG_KERNEL_TRACEPOINT_LIST:
412 return lttng_abi_tracepoint_list();
413 case LTTNG_KERNEL_SYSCALL_LIST:
414 return lttng_abi_syscall_list();
415 case LTTNG_KERNEL_OLD_WAIT_QUIESCENT:
416 case LTTNG_KERNEL_WAIT_QUIESCENT:
417 synchronize_trace();
418 return 0;
419 case LTTNG_KERNEL_OLD_CALIBRATE:
420 {
421 struct lttng_kernel_old_calibrate __user *ucalibrate =
422 (struct lttng_kernel_old_calibrate __user *) arg;
423 struct lttng_kernel_old_calibrate old_calibrate;
424 struct lttng_kernel_calibrate calibrate;
425 int ret;
426
427 if (copy_from_user(&old_calibrate, ucalibrate, sizeof(old_calibrate)))
428 return -EFAULT;
429 calibrate.type = old_calibrate.type;
430 ret = lttng_calibrate(&calibrate);
431 if (copy_to_user(ucalibrate, &old_calibrate, sizeof(old_calibrate)))
432 return -EFAULT;
433 return ret;
434 }
435 case LTTNG_KERNEL_CALIBRATE:
436 {
437 struct lttng_kernel_calibrate __user *ucalibrate =
438 (struct lttng_kernel_calibrate __user *) arg;
439 struct lttng_kernel_calibrate calibrate;
440 int ret;
441
442 if (copy_from_user(&calibrate, ucalibrate, sizeof(calibrate)))
443 return -EFAULT;
444 ret = lttng_calibrate(&calibrate);
445 if (copy_to_user(ucalibrate, &calibrate, sizeof(calibrate)))
446 return -EFAULT;
447 return ret;
448 }
449 default:
450 return -ENOIOCTLCMD;
451 }
452 }
453
454 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0))
455 static const struct proc_ops lttng_proc_ops = {
456 .proc_ioctl = lttng_ioctl,
457 #ifdef CONFIG_COMPAT
458 .proc_compat_ioctl = lttng_ioctl,
459 #endif /* CONFIG_COMPAT */
460 };
461 #else
462 static const struct file_operations lttng_proc_ops = {
463 .owner = THIS_MODULE,
464 .unlocked_ioctl = lttng_ioctl,
465 #ifdef CONFIG_COMPAT
466 .compat_ioctl = lttng_ioctl,
467 #endif /* CONFIG_COMPAT */
468 };
469 #endif
470
471 static
472 int lttng_abi_create_channel(struct file *session_file,
473 struct lttng_kernel_channel *chan_param,
474 enum channel_type channel_type)
475 {
476 struct lttng_session *session = session_file->private_data;
477 const struct file_operations *fops = NULL;
478 const char *transport_name;
479 struct lttng_channel *chan;
480 struct file *chan_file;
481 int chan_fd;
482 int ret = 0;
483
484 chan_fd = lttng_get_unused_fd();
485 if (chan_fd < 0) {
486 ret = chan_fd;
487 goto fd_error;
488 }
489 switch (channel_type) {
490 case PER_CPU_CHANNEL:
491 fops = &lttng_channel_fops;
492 break;
493 case METADATA_CHANNEL:
494 fops = &lttng_metadata_fops;
495 break;
496 }
497
498 chan_file = anon_inode_getfile("[lttng_channel]",
499 fops,
500 NULL, O_RDWR);
501 if (IS_ERR(chan_file)) {
502 ret = PTR_ERR(chan_file);
503 goto file_error;
504 }
505 switch (channel_type) {
506 case PER_CPU_CHANNEL:
507 if (chan_param->output == LTTNG_KERNEL_SPLICE) {
508 transport_name = chan_param->overwrite ?
509 "relay-overwrite" : "relay-discard";
510 } else if (chan_param->output == LTTNG_KERNEL_MMAP) {
511 transport_name = chan_param->overwrite ?
512 "relay-overwrite-mmap" : "relay-discard-mmap";
513 } else {
514 return -EINVAL;
515 }
516 break;
517 case METADATA_CHANNEL:
518 if (chan_param->output == LTTNG_KERNEL_SPLICE)
519 transport_name = "relay-metadata";
520 else if (chan_param->output == LTTNG_KERNEL_MMAP)
521 transport_name = "relay-metadata-mmap";
522 else
523 return -EINVAL;
524 break;
525 default:
526 transport_name = "<unknown>";
527 break;
528 }
529 if (!atomic_long_add_unless(&session_file->f_count, 1, LONG_MAX)) {
530 ret = -EOVERFLOW;
531 goto refcount_error;
532 }
533 /*
534 * We tolerate no failure path after channel creation. It will stay
535 * invariant for the rest of the session.
536 */
537 chan = lttng_channel_create(session, transport_name, NULL,
538 chan_param->subbuf_size,
539 chan_param->num_subbuf,
540 chan_param->switch_timer_interval,
541 chan_param->read_timer_interval,
542 channel_type);
543 if (!chan) {
544 ret = -EINVAL;
545 goto chan_error;
546 }
547 chan->file = chan_file;
548 chan_file->private_data = chan;
549 fd_install(chan_fd, chan_file);
550
551 return chan_fd;
552
553 chan_error:
554 atomic_long_dec(&session_file->f_count);
555 refcount_error:
556 fput(chan_file);
557 file_error:
558 put_unused_fd(chan_fd);
559 fd_error:
560 return ret;
561 }
562
563 static
564 int lttng_abi_session_set_name(struct lttng_session *session,
565 struct lttng_kernel_session_name *name)
566 {
567 size_t len;
568
569 len = strnlen(name->name, LTTNG_KERNEL_SESSION_NAME_LEN);
570
571 if (len == LTTNG_KERNEL_SESSION_NAME_LEN) {
572 /* Name is too long/malformed */
573 return -EINVAL;
574 }
575
576 strcpy(session->name, name->name);
577 return 0;
578 }
579
580 static
581 int lttng_abi_session_set_creation_time(struct lttng_session *session,
582 struct lttng_kernel_session_creation_time *time)
583 {
584 size_t len;
585
586 len = strnlen(time->iso8601, LTTNG_KERNEL_SESSION_CREATION_TIME_ISO8601_LEN);
587
588 if (len == LTTNG_KERNEL_SESSION_CREATION_TIME_ISO8601_LEN) {
589 /* Time is too long/malformed */
590 return -EINVAL;
591 }
592
593 strcpy(session->creation_time, time->iso8601);
594 return 0;
595 }
596
597 static
598 enum tracker_type get_tracker_type(struct lttng_kernel_tracker_args *tracker)
599 {
600 switch (tracker->type) {
601 case LTTNG_KERNEL_TRACKER_PID:
602 return TRACKER_PID;
603 case LTTNG_KERNEL_TRACKER_VPID:
604 return TRACKER_VPID;
605 case LTTNG_KERNEL_TRACKER_UID:
606 return TRACKER_UID;
607 case LTTNG_KERNEL_TRACKER_VUID:
608 return TRACKER_VUID;
609 case LTTNG_KERNEL_TRACKER_GID:
610 return TRACKER_GID;
611 case LTTNG_KERNEL_TRACKER_VGID:
612 return TRACKER_VGID;
613 default:
614 return TRACKER_UNKNOWN;
615 }
616 }
617
618 /**
619 * lttng_session_ioctl - lttng session fd ioctl
620 *
621 * @file: the file
622 * @cmd: the command
623 * @arg: command arg
624 *
625 * This ioctl implements lttng commands:
626 * LTTNG_KERNEL_CHANNEL
627 * Returns a LTTng channel file descriptor
628 * LTTNG_KERNEL_ENABLE
629 * Enables tracing for a session (weak enable)
630 * LTTNG_KERNEL_DISABLE
631 * Disables tracing for a session (strong disable)
632 * LTTNG_KERNEL_METADATA
633 * Returns a LTTng metadata file descriptor
634 * LTTNG_KERNEL_SESSION_TRACK_PID
635 * Add PID to session PID tracker
636 * LTTNG_KERNEL_SESSION_UNTRACK_PID
637 * Remove PID from session PID tracker
638 * LTTNG_KERNEL_SESSION_TRACK_ID
639 * Add ID to tracker
640 * LTTNG_KERNEL_SESSION_UNTRACK_ID
641 * Remove ID from tracker
642 *
643 * The returned channel will be deleted when its file descriptor is closed.
644 */
645 static
646 long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
647 {
648 struct lttng_session *session = file->private_data;
649 struct lttng_kernel_channel chan_param;
650 struct lttng_kernel_old_channel old_chan_param;
651
652 switch (cmd) {
653 case LTTNG_KERNEL_OLD_CHANNEL:
654 {
655 if (copy_from_user(&old_chan_param,
656 (struct lttng_kernel_old_channel __user *) arg,
657 sizeof(struct lttng_kernel_old_channel)))
658 return -EFAULT;
659 chan_param.overwrite = old_chan_param.overwrite;
660 chan_param.subbuf_size = old_chan_param.subbuf_size;
661 chan_param.num_subbuf = old_chan_param.num_subbuf;
662 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
663 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
664 chan_param.output = old_chan_param.output;
665
666 return lttng_abi_create_channel(file, &chan_param,
667 PER_CPU_CHANNEL);
668 }
669 case LTTNG_KERNEL_CHANNEL:
670 {
671 if (copy_from_user(&chan_param,
672 (struct lttng_kernel_channel __user *) arg,
673 sizeof(struct lttng_kernel_channel)))
674 return -EFAULT;
675 return lttng_abi_create_channel(file, &chan_param,
676 PER_CPU_CHANNEL);
677 }
678 case LTTNG_KERNEL_OLD_SESSION_START:
679 case LTTNG_KERNEL_OLD_ENABLE:
680 case LTTNG_KERNEL_SESSION_START:
681 case LTTNG_KERNEL_ENABLE:
682 return lttng_session_enable(session);
683 case LTTNG_KERNEL_OLD_SESSION_STOP:
684 case LTTNG_KERNEL_OLD_DISABLE:
685 case LTTNG_KERNEL_SESSION_STOP:
686 case LTTNG_KERNEL_DISABLE:
687 return lttng_session_disable(session);
688 case LTTNG_KERNEL_OLD_METADATA:
689 {
690 if (copy_from_user(&old_chan_param,
691 (struct lttng_kernel_old_channel __user *) arg,
692 sizeof(struct lttng_kernel_old_channel)))
693 return -EFAULT;
694 chan_param.overwrite = old_chan_param.overwrite;
695 chan_param.subbuf_size = old_chan_param.subbuf_size;
696 chan_param.num_subbuf = old_chan_param.num_subbuf;
697 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
698 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
699 chan_param.output = old_chan_param.output;
700
701 return lttng_abi_create_channel(file, &chan_param,
702 METADATA_CHANNEL);
703 }
704 case LTTNG_KERNEL_METADATA:
705 {
706 if (copy_from_user(&chan_param,
707 (struct lttng_kernel_channel __user *) arg,
708 sizeof(struct lttng_kernel_channel)))
709 return -EFAULT;
710 return lttng_abi_create_channel(file, &chan_param,
711 METADATA_CHANNEL);
712 }
713 case LTTNG_KERNEL_SESSION_TRACK_PID:
714 return lttng_session_track_id(session, TRACKER_PID, (int) arg);
715 case LTTNG_KERNEL_SESSION_UNTRACK_PID:
716 return lttng_session_untrack_id(session, TRACKER_PID, (int) arg);
717 case LTTNG_KERNEL_SESSION_TRACK_ID:
718 {
719 struct lttng_kernel_tracker_args tracker;
720 enum tracker_type tracker_type;
721
722 if (copy_from_user(&tracker,
723 (struct lttng_kernel_tracker_args __user *) arg,
724 sizeof(struct lttng_kernel_tracker_args)))
725 return -EFAULT;
726 tracker_type = get_tracker_type(&tracker);
727 if (tracker_type == TRACKER_UNKNOWN)
728 return -EINVAL;
729 return lttng_session_track_id(session, tracker_type, tracker.id);
730 }
731 case LTTNG_KERNEL_SESSION_UNTRACK_ID:
732 {
733 struct lttng_kernel_tracker_args tracker;
734 enum tracker_type tracker_type;
735
736 if (copy_from_user(&tracker,
737 (struct lttng_kernel_tracker_args __user *) arg,
738 sizeof(struct lttng_kernel_tracker_args)))
739 return -EFAULT;
740 tracker_type = get_tracker_type(&tracker);
741 if (tracker_type == TRACKER_UNKNOWN)
742 return -EINVAL;
743 return lttng_session_untrack_id(session, tracker_type,
744 tracker.id);
745 }
746 case LTTNG_KERNEL_SESSION_LIST_TRACKER_PIDS:
747 return lttng_session_list_tracker_ids(session, TRACKER_PID);
748 case LTTNG_KERNEL_SESSION_LIST_TRACKER_IDS:
749 {
750 struct lttng_kernel_tracker_args tracker;
751 enum tracker_type tracker_type;
752
753 if (copy_from_user(&tracker,
754 (struct lttng_kernel_tracker_args __user *) arg,
755 sizeof(struct lttng_kernel_tracker_args)))
756 return -EFAULT;
757 tracker_type = get_tracker_type(&tracker);
758 if (tracker_type == TRACKER_UNKNOWN)
759 return -EINVAL;
760 return lttng_session_list_tracker_ids(session, tracker_type);
761 }
762 case LTTNG_KERNEL_SESSION_METADATA_REGEN:
763 return lttng_session_metadata_regenerate(session);
764 case LTTNG_KERNEL_SESSION_STATEDUMP:
765 return lttng_session_statedump(session);
766 case LTTNG_KERNEL_SESSION_SET_NAME:
767 {
768 struct lttng_kernel_session_name name;
769
770 if (copy_from_user(&name,
771 (struct lttng_kernel_session_name __user *) arg,
772 sizeof(struct lttng_kernel_session_name)))
773 return -EFAULT;
774 return lttng_abi_session_set_name(session, &name);
775 }
776 case LTTNG_KERNEL_SESSION_SET_CREATION_TIME:
777 {
778 struct lttng_kernel_session_creation_time time;
779
780 if (copy_from_user(&time,
781 (struct lttng_kernel_session_creation_time __user *) arg,
782 sizeof(struct lttng_kernel_session_creation_time)))
783 return -EFAULT;
784 return lttng_abi_session_set_creation_time(session, &time);
785 }
786 default:
787 return -ENOIOCTLCMD;
788 }
789 }
790
791 /*
792 * Called when the last file reference is dropped.
793 *
794 * Big fat note: channels and events are invariant for the whole session after
795 * their creation. So this session destruction also destroys all channel and
796 * event structures specific to this session (they are not destroyed when their
797 * individual file is released).
798 */
799 static
800 int lttng_session_release(struct inode *inode, struct file *file)
801 {
802 struct lttng_session *session = file->private_data;
803
804 if (session)
805 lttng_session_destroy(session);
806 return 0;
807 }
808
809 static const struct file_operations lttng_session_fops = {
810 .owner = THIS_MODULE,
811 .release = lttng_session_release,
812 .unlocked_ioctl = lttng_session_ioctl,
813 #ifdef CONFIG_COMPAT
814 .compat_ioctl = lttng_session_ioctl,
815 #endif
816 };
817
818 static const struct file_operations lttng_event_notifier_group_notif_fops = {
819 .owner = THIS_MODULE,
820 };
821
822 /**
823 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
824 * @filp: the file
825 * @wait: poll table
826 *
827 * Handles the poll operations for the metadata channels.
828 */
829 static
830 unsigned int lttng_metadata_ring_buffer_poll(struct file *filp,
831 poll_table *wait)
832 {
833 struct lttng_metadata_stream *stream = filp->private_data;
834 struct lib_ring_buffer *buf = stream->priv;
835 int finalized;
836 unsigned int mask = 0;
837
838 if (filp->f_mode & FMODE_READ) {
839 poll_wait_set_exclusive(wait);
840 poll_wait(filp, &stream->read_wait, wait);
841
842 finalized = stream->finalized;
843
844 /*
845 * lib_ring_buffer_is_finalized() contains a smp_rmb()
846 * ordering finalized load before offsets loads.
847 */
848 WARN_ON(atomic_long_read(&buf->active_readers) != 1);
849
850 if (finalized)
851 mask |= POLLHUP;
852
853 mutex_lock(&stream->metadata_cache->lock);
854 if (stream->metadata_cache->metadata_written >
855 stream->metadata_out)
856 mask |= POLLIN;
857 mutex_unlock(&stream->metadata_cache->lock);
858 }
859
860 return mask;
861 }
862
863 static
864 void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file *filp,
865 unsigned int cmd, unsigned long arg)
866 {
867 struct lttng_metadata_stream *stream = filp->private_data;
868
869 stream->metadata_out = stream->metadata_in;
870 }
871
872 /*
873 * Reset the counter of how much metadata has been consumed to 0. That way,
874 * the consumer receives the content of the metadata cache unchanged. This is
875 * different from the metadata_regenerate where the offset from epoch is
876 * resampled, here we want the exact same content as the last time the metadata
877 * was generated. This command is only possible if all the metadata written
878 * in the cache has been output to the metadata stream to avoid corrupting the
879 * metadata file.
880 *
881 * Return 0 on success, a negative value on error.
882 */
883 static
884 int lttng_metadata_cache_dump(struct lttng_metadata_stream *stream)
885 {
886 int ret;
887 struct lttng_metadata_cache *cache = stream->metadata_cache;
888
889 mutex_lock(&cache->lock);
890 if (stream->metadata_out != cache->metadata_written) {
891 ret = -EBUSY;
892 goto end;
893 }
894 stream->metadata_out = 0;
895 stream->metadata_in = 0;
896 wake_up_interruptible(&stream->read_wait);
897 ret = 0;
898
899 end:
900 mutex_unlock(&cache->lock);
901 return ret;
902 }
903
904 static
905 long lttng_metadata_ring_buffer_ioctl(struct file *filp,
906 unsigned int cmd, unsigned long arg)
907 {
908 int ret;
909 struct lttng_metadata_stream *stream = filp->private_data;
910 struct lib_ring_buffer *buf = stream->priv;
911 unsigned int rb_cmd;
912 bool coherent;
913
914 if (cmd == RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK)
915 rb_cmd = RING_BUFFER_GET_NEXT_SUBBUF;
916 else
917 rb_cmd = cmd;
918
919 switch (cmd) {
920 case RING_BUFFER_GET_NEXT_SUBBUF:
921 {
922 struct lttng_metadata_stream *stream = filp->private_data;
923 struct lib_ring_buffer *buf = stream->priv;
924 struct channel *chan = buf->backend.chan;
925
926 ret = lttng_metadata_output_channel(stream, chan, NULL);
927 if (ret > 0) {
928 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
929 ret = 0;
930 } else if (ret < 0)
931 goto err;
932 break;
933 }
934 case RING_BUFFER_GET_SUBBUF:
935 {
936 /*
937 * Random access is not allowed for metadata channel.
938 */
939 return -ENOSYS;
940 }
941 case RING_BUFFER_FLUSH_EMPTY: /* Fall-through. */
942 case RING_BUFFER_FLUSH:
943 {
944 struct lttng_metadata_stream *stream = filp->private_data;
945 struct lib_ring_buffer *buf = stream->priv;
946 struct channel *chan = buf->backend.chan;
947
948 /*
949 * Before doing the actual ring buffer flush, write up to one
950 * packet of metadata in the ring buffer.
951 */
952 ret = lttng_metadata_output_channel(stream, chan, NULL);
953 if (ret < 0)
954 goto err;
955 break;
956 }
957 case RING_BUFFER_GET_METADATA_VERSION:
958 {
959 struct lttng_metadata_stream *stream = filp->private_data;
960
961 return put_u64(stream->version, arg);
962 }
963 case RING_BUFFER_METADATA_CACHE_DUMP:
964 {
965 struct lttng_metadata_stream *stream = filp->private_data;
966
967 return lttng_metadata_cache_dump(stream);
968 }
969 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
970 {
971 struct lttng_metadata_stream *stream = filp->private_data;
972 struct lib_ring_buffer *buf = stream->priv;
973 struct channel *chan = buf->backend.chan;
974
975 ret = lttng_metadata_output_channel(stream, chan, &coherent);
976 if (ret > 0) {
977 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
978 ret = 0;
979 } else if (ret < 0) {
980 goto err;
981 }
982 break;
983 }
984 default:
985 break;
986 }
987 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
988
989 /* Performing lib ring buffer ioctl after our own. */
990 ret = lib_ring_buffer_ioctl(filp, rb_cmd, arg, buf);
991 if (ret < 0)
992 goto err;
993
994 switch (cmd) {
995 case RING_BUFFER_PUT_NEXT_SUBBUF:
996 {
997 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
998 cmd, arg);
999 break;
1000 }
1001 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
1002 {
1003 return put_u32(coherent, arg);
1004 }
1005 default:
1006 break;
1007 }
1008 err:
1009 return ret;
1010 }
1011
1012 #ifdef CONFIG_COMPAT
1013 static
1014 long lttng_metadata_ring_buffer_compat_ioctl(struct file *filp,
1015 unsigned int cmd, unsigned long arg)
1016 {
1017 int ret;
1018 struct lttng_metadata_stream *stream = filp->private_data;
1019 struct lib_ring_buffer *buf = stream->priv;
1020 unsigned int rb_cmd;
1021 bool coherent;
1022
1023 if (cmd == RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK)
1024 rb_cmd = RING_BUFFER_GET_NEXT_SUBBUF;
1025 else
1026 rb_cmd = cmd;
1027
1028 switch (cmd) {
1029 case RING_BUFFER_GET_NEXT_SUBBUF:
1030 {
1031 struct lttng_metadata_stream *stream = filp->private_data;
1032 struct lib_ring_buffer *buf = stream->priv;
1033 struct channel *chan = buf->backend.chan;
1034
1035 ret = lttng_metadata_output_channel(stream, chan, NULL);
1036 if (ret > 0) {
1037 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
1038 ret = 0;
1039 } else if (ret < 0)
1040 goto err;
1041 break;
1042 }
1043 case RING_BUFFER_GET_SUBBUF:
1044 {
1045 /*
1046 * Random access is not allowed for metadata channel.
1047 */
1048 return -ENOSYS;
1049 }
1050 case RING_BUFFER_FLUSH_EMPTY: /* Fall-through. */
1051 case RING_BUFFER_FLUSH:
1052 {
1053 struct lttng_metadata_stream *stream = filp->private_data;
1054 struct lib_ring_buffer *buf = stream->priv;
1055 struct channel *chan = buf->backend.chan;
1056
1057 /*
1058 * Before doing the actual ring buffer flush, write up to one
1059 * packet of metadata in the ring buffer.
1060 */
1061 ret = lttng_metadata_output_channel(stream, chan, NULL);
1062 if (ret < 0)
1063 goto err;
1064 break;
1065 }
1066 case RING_BUFFER_GET_METADATA_VERSION:
1067 {
1068 struct lttng_metadata_stream *stream = filp->private_data;
1069
1070 return put_u64(stream->version, arg);
1071 }
1072 case RING_BUFFER_METADATA_CACHE_DUMP:
1073 {
1074 struct lttng_metadata_stream *stream = filp->private_data;
1075
1076 return lttng_metadata_cache_dump(stream);
1077 }
1078 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
1079 {
1080 struct lttng_metadata_stream *stream = filp->private_data;
1081 struct lib_ring_buffer *buf = stream->priv;
1082 struct channel *chan = buf->backend.chan;
1083
1084 ret = lttng_metadata_output_channel(stream, chan, &coherent);
1085 if (ret > 0) {
1086 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
1087 ret = 0;
1088 } else if (ret < 0) {
1089 goto err;
1090 }
1091 break;
1092 }
1093 default:
1094 break;
1095 }
1096 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
1097
1098 /* Performing lib ring buffer ioctl after our own. */
1099 ret = lib_ring_buffer_compat_ioctl(filp, rb_cmd, arg, buf);
1100 if (ret < 0)
1101 goto err;
1102
1103 switch (cmd) {
1104 case RING_BUFFER_PUT_NEXT_SUBBUF:
1105 {
1106 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
1107 cmd, arg);
1108 break;
1109 }
1110 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
1111 {
1112 return put_u32(coherent, arg);
1113 }
1114 default:
1115 break;
1116 }
1117 err:
1118 return ret;
1119 }
1120 #endif
1121
1122 /*
1123 * This is not used by anonymous file descriptors. This code is left
1124 * there if we ever want to implement an inode with open() operation.
1125 */
1126 static
1127 int lttng_metadata_ring_buffer_open(struct inode *inode, struct file *file)
1128 {
1129 struct lttng_metadata_stream *stream = inode->i_private;
1130 struct lib_ring_buffer *buf = stream->priv;
1131
1132 file->private_data = buf;
1133 /*
1134 * Since life-time of metadata cache differs from that of
1135 * session, we need to keep our own reference on the transport.
1136 */
1137 if (!try_module_get(stream->transport->owner)) {
1138 printk(KERN_WARNING "LTTng: Can't lock transport module.\n");
1139 return -EBUSY;
1140 }
1141 return lib_ring_buffer_open(inode, file, buf);
1142 }
1143
1144 static
1145 int lttng_metadata_ring_buffer_release(struct inode *inode, struct file *file)
1146 {
1147 struct lttng_metadata_stream *stream = file->private_data;
1148 struct lib_ring_buffer *buf = stream->priv;
1149
1150 mutex_lock(&stream->metadata_cache->lock);
1151 list_del(&stream->list);
1152 mutex_unlock(&stream->metadata_cache->lock);
1153 kref_put(&stream->metadata_cache->refcount, metadata_cache_destroy);
1154 module_put(stream->transport->owner);
1155 kfree(stream);
1156 return lib_ring_buffer_release(inode, file, buf);
1157 }
1158
1159 static
1160 ssize_t lttng_metadata_ring_buffer_splice_read(struct file *in, loff_t *ppos,
1161 struct pipe_inode_info *pipe, size_t len,
1162 unsigned int flags)
1163 {
1164 struct lttng_metadata_stream *stream = in->private_data;
1165 struct lib_ring_buffer *buf = stream->priv;
1166
1167 return lib_ring_buffer_splice_read(in, ppos, pipe, len,
1168 flags, buf);
1169 }
1170
1171 static
1172 int lttng_metadata_ring_buffer_mmap(struct file *filp,
1173 struct vm_area_struct *vma)
1174 {
1175 struct lttng_metadata_stream *stream = filp->private_data;
1176 struct lib_ring_buffer *buf = stream->priv;
1177
1178 return lib_ring_buffer_mmap(filp, vma, buf);
1179 }
1180
1181 static
1182 const struct file_operations lttng_metadata_ring_buffer_file_operations = {
1183 .owner = THIS_MODULE,
1184 .open = lttng_metadata_ring_buffer_open,
1185 .release = lttng_metadata_ring_buffer_release,
1186 .poll = lttng_metadata_ring_buffer_poll,
1187 .splice_read = lttng_metadata_ring_buffer_splice_read,
1188 .mmap = lttng_metadata_ring_buffer_mmap,
1189 .unlocked_ioctl = lttng_metadata_ring_buffer_ioctl,
1190 .llseek = vfs_lib_ring_buffer_no_llseek,
1191 #ifdef CONFIG_COMPAT
1192 .compat_ioctl = lttng_metadata_ring_buffer_compat_ioctl,
1193 #endif
1194 };
1195
1196 static
1197 int lttng_abi_create_stream_fd(struct file *channel_file, void *stream_priv,
1198 const struct file_operations *fops, const char *name)
1199 {
1200 int stream_fd, ret;
1201 struct file *stream_file;
1202
1203 stream_fd = lttng_get_unused_fd();
1204 if (stream_fd < 0) {
1205 ret = stream_fd;
1206 goto fd_error;
1207 }
1208 stream_file = anon_inode_getfile(name, fops, stream_priv, O_RDWR);
1209 if (IS_ERR(stream_file)) {
1210 ret = PTR_ERR(stream_file);
1211 goto file_error;
1212 }
1213 /*
1214 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
1215 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
1216 * file descriptor, so we set FMODE_PREAD here.
1217 */
1218 stream_file->f_mode |= FMODE_PREAD;
1219 fd_install(stream_fd, stream_file);
1220 /*
1221 * The stream holds a reference to the channel within the generic ring
1222 * buffer library, so no need to hold a refcount on the channel and
1223 * session files here.
1224 */
1225 return stream_fd;
1226
1227 file_error:
1228 put_unused_fd(stream_fd);
1229 fd_error:
1230 return ret;
1231 }
1232
1233 static
1234 int lttng_abi_open_stream(struct file *channel_file)
1235 {
1236 struct lttng_channel *channel = channel_file->private_data;
1237 struct lib_ring_buffer *buf;
1238 int ret;
1239 void *stream_priv;
1240
1241 buf = channel->ops->buffer_read_open(channel->chan);
1242 if (!buf)
1243 return -ENOENT;
1244
1245 stream_priv = buf;
1246 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
1247 &lttng_stream_ring_buffer_file_operations,
1248 "[lttng_stream]");
1249 if (ret < 0)
1250 goto fd_error;
1251
1252 return ret;
1253
1254 fd_error:
1255 channel->ops->buffer_read_close(buf);
1256 return ret;
1257 }
1258
1259 static
1260 int lttng_abi_open_metadata_stream(struct file *channel_file)
1261 {
1262 struct lttng_channel *channel = channel_file->private_data;
1263 struct lttng_session *session = channel->session;
1264 struct lib_ring_buffer *buf;
1265 int ret;
1266 struct lttng_metadata_stream *metadata_stream;
1267 void *stream_priv;
1268
1269 buf = channel->ops->buffer_read_open(channel->chan);
1270 if (!buf)
1271 return -ENOENT;
1272
1273 metadata_stream = kzalloc(sizeof(struct lttng_metadata_stream),
1274 GFP_KERNEL);
1275 if (!metadata_stream) {
1276 ret = -ENOMEM;
1277 goto nomem;
1278 }
1279 metadata_stream->metadata_cache = session->metadata_cache;
1280 init_waitqueue_head(&metadata_stream->read_wait);
1281 metadata_stream->priv = buf;
1282 stream_priv = metadata_stream;
1283 metadata_stream->transport = channel->transport;
1284 /* Initial state is an empty metadata, considered as incoherent. */
1285 metadata_stream->coherent = false;
1286
1287 /*
1288 * Since life-time of metadata cache differs from that of
1289 * session, we need to keep our own reference on the transport.
1290 */
1291 if (!try_module_get(metadata_stream->transport->owner)) {
1292 printk(KERN_WARNING "LTTng: Can't lock transport module.\n");
1293 ret = -EINVAL;
1294 goto notransport;
1295 }
1296
1297 if (!lttng_kref_get(&session->metadata_cache->refcount)) {
1298 ret = -EOVERFLOW;
1299 goto kref_error;
1300 }
1301
1302 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
1303 &lttng_metadata_ring_buffer_file_operations,
1304 "[lttng_metadata_stream]");
1305 if (ret < 0)
1306 goto fd_error;
1307
1308 mutex_lock(&session->metadata_cache->lock);
1309 list_add(&metadata_stream->list,
1310 &session->metadata_cache->metadata_stream);
1311 mutex_unlock(&session->metadata_cache->lock);
1312 return ret;
1313
1314 fd_error:
1315 kref_put(&session->metadata_cache->refcount, metadata_cache_destroy);
1316 kref_error:
1317 module_put(metadata_stream->transport->owner);
1318 notransport:
1319 kfree(metadata_stream);
1320 nomem:
1321 channel->ops->buffer_read_close(buf);
1322 return ret;
1323 }
1324
1325 static
1326 int lttng_abi_open_event_notifier_group_stream(struct file *notif_file)
1327 {
1328 struct lttng_event_notifier_group *event_notifier_group = notif_file->private_data;
1329 struct channel *chan = event_notifier_group->chan;
1330 struct lib_ring_buffer *buf;
1331 int ret;
1332 void *stream_priv;
1333
1334 buf = event_notifier_group->ops->buffer_read_open(chan);
1335 if (!buf)
1336 return -ENOENT;
1337
1338 /* The event_notifier notification fd holds a reference on the event_notifier group */
1339 if (!atomic_long_add_unless(&notif_file->f_count, 1, LONG_MAX)) {
1340 ret = -EOVERFLOW;
1341 goto refcount_error;
1342 }
1343 event_notifier_group->buf = buf;
1344 stream_priv = event_notifier_group;
1345 ret = lttng_abi_create_stream_fd(notif_file, stream_priv,
1346 &lttng_event_notifier_group_notif_fops,
1347 "[lttng_event_notifier_stream]");
1348 if (ret < 0)
1349 goto fd_error;
1350
1351 return ret;
1352
1353 fd_error:
1354 atomic_long_dec(&notif_file->f_count);
1355 refcount_error:
1356 event_notifier_group->ops->buffer_read_close(buf);
1357 return ret;
1358 }
1359
1360 static
1361 int lttng_abi_validate_event_param(struct lttng_kernel_event *event_param)
1362 {
1363 /* Limit ABI to implemented features. */
1364 switch (event_param->instrumentation) {
1365 case LTTNG_KERNEL_SYSCALL:
1366 switch (event_param->u.syscall.entryexit) {
1367 case LTTNG_KERNEL_SYSCALL_ENTRYEXIT:
1368 break;
1369 default:
1370 return -EINVAL;
1371 }
1372 switch (event_param->u.syscall.abi) {
1373 case LTTNG_KERNEL_SYSCALL_ABI_ALL:
1374 break;
1375 default:
1376 return -EINVAL;
1377 }
1378 switch (event_param->u.syscall.match) {
1379 case LTTNG_SYSCALL_MATCH_NAME:
1380 break;
1381 default:
1382 return -EINVAL;
1383 }
1384 break;
1385
1386 case LTTNG_KERNEL_TRACEPOINT: /* Fallthrough */
1387 case LTTNG_KERNEL_KPROBE: /* Fallthrough */
1388 case LTTNG_KERNEL_KRETPROBE: /* Fallthrough */
1389 case LTTNG_KERNEL_NOOP: /* Fallthrough */
1390 case LTTNG_KERNEL_UPROBE:
1391 break;
1392
1393 case LTTNG_KERNEL_FUNCTION: /* Fallthrough */
1394 default:
1395 return -EINVAL;
1396 }
1397 return 0;
1398 }
1399
1400 static
1401 int lttng_abi_create_event(struct file *channel_file,
1402 struct lttng_kernel_event *event_param)
1403 {
1404 struct lttng_channel *channel = channel_file->private_data;
1405 int event_fd, ret;
1406 struct file *event_file;
1407 void *priv;
1408
1409 event_param->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
1410 switch (event_param->instrumentation) {
1411 case LTTNG_KERNEL_KRETPROBE:
1412 event_param->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
1413 break;
1414 case LTTNG_KERNEL_KPROBE:
1415 event_param->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
1416 break;
1417 case LTTNG_KERNEL_FUNCTION:
1418 WARN_ON_ONCE(1);
1419 /* Not implemented. */
1420 break;
1421 default:
1422 break;
1423 }
1424 event_fd = lttng_get_unused_fd();
1425 if (event_fd < 0) {
1426 ret = event_fd;
1427 goto fd_error;
1428 }
1429 event_file = anon_inode_getfile("[lttng_event]",
1430 &lttng_event_fops,
1431 NULL, O_RDWR);
1432 if (IS_ERR(event_file)) {
1433 ret = PTR_ERR(event_file);
1434 goto file_error;
1435 }
1436 /* The event holds a reference on the channel */
1437 if (!atomic_long_add_unless(&channel_file->f_count, 1, LONG_MAX)) {
1438 ret = -EOVERFLOW;
1439 goto refcount_error;
1440 }
1441 ret = lttng_abi_validate_event_param(event_param);
1442 if (ret)
1443 goto event_error;
1444 if (event_param->instrumentation == LTTNG_KERNEL_TRACEPOINT
1445 || event_param->instrumentation == LTTNG_KERNEL_SYSCALL) {
1446 struct lttng_event_enabler *event_enabler;
1447
1448 if (strutils_is_star_glob_pattern(event_param->name)) {
1449 /*
1450 * If the event name is a star globbing pattern,
1451 * we create the special star globbing enabler.
1452 */
1453 event_enabler = lttng_event_enabler_create(LTTNG_ENABLER_FORMAT_STAR_GLOB,
1454 event_param, channel);
1455 } else {
1456 event_enabler = lttng_event_enabler_create(LTTNG_ENABLER_FORMAT_NAME,
1457 event_param, channel);
1458 }
1459 priv = event_enabler;
1460 } else {
1461 struct lttng_event *event;
1462
1463 /*
1464 * We tolerate no failure path after event creation. It
1465 * will stay invariant for the rest of the session.
1466 */
1467 event = lttng_event_create(channel, event_param,
1468 NULL, NULL,
1469 event_param->instrumentation);
1470 WARN_ON_ONCE(!event);
1471 if (IS_ERR(event)) {
1472 ret = PTR_ERR(event);
1473 goto event_error;
1474 }
1475 priv = event;
1476 }
1477 event_file->private_data = priv;
1478 fd_install(event_fd, event_file);
1479 return event_fd;
1480
1481 event_error:
1482 atomic_long_dec(&channel_file->f_count);
1483 refcount_error:
1484 fput(event_file);
1485 file_error:
1486 put_unused_fd(event_fd);
1487 fd_error:
1488 return ret;
1489 }
1490
1491 static
1492 long lttng_event_notifier_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1493 {
1494 struct lttng_event_notifier_enabler *event_notifier_enabler;
1495 enum lttng_event_type *evtype = file->private_data;
1496
1497 switch (cmd) {
1498 case LTTNG_KERNEL_ENABLE:
1499 switch (*evtype) {
1500 case LTTNG_TYPE_EVENT:
1501 return -EINVAL;
1502 case LTTNG_TYPE_ENABLER:
1503 event_notifier_enabler = file->private_data;
1504 return lttng_event_notifier_enabler_enable(event_notifier_enabler);
1505 default:
1506 WARN_ON_ONCE(1);
1507 return -ENOSYS;
1508 }
1509 case LTTNG_KERNEL_DISABLE:
1510 switch (*evtype) {
1511 case LTTNG_TYPE_EVENT:
1512 return -EINVAL;
1513 case LTTNG_TYPE_ENABLER:
1514 event_notifier_enabler = file->private_data;
1515 return lttng_event_notifier_enabler_disable(event_notifier_enabler);
1516 default:
1517 WARN_ON_ONCE(1);
1518 return -ENOSYS;
1519 }
1520 case LTTNG_KERNEL_FILTER:
1521 switch (*evtype) {
1522 case LTTNG_TYPE_EVENT:
1523 return -EINVAL;
1524 case LTTNG_TYPE_ENABLER:
1525 event_notifier_enabler = file->private_data;
1526 return lttng_event_notifier_enabler_attach_bytecode(event_notifier_enabler,
1527 (struct lttng_kernel_filter_bytecode __user *) arg);
1528 default:
1529 WARN_ON_ONCE(1);
1530 return -ENOSYS;
1531 }
1532 default:
1533 return -ENOIOCTLCMD;
1534 }
1535 }
1536
1537 static
1538 int lttng_event_notifier_release(struct inode *inode, struct file *file)
1539 {
1540 struct lttng_event_notifier *event_notifier;
1541 struct lttng_event_notifier_enabler *event_notifier_enabler;
1542 enum lttng_event_type *evtype = file->private_data;
1543
1544 if (!evtype)
1545 return 0;
1546
1547 switch (*evtype) {
1548 case LTTNG_TYPE_EVENT:
1549 event_notifier = file->private_data;
1550 if (event_notifier)
1551 fput(event_notifier->group->file);
1552 break;
1553 case LTTNG_TYPE_ENABLER:
1554 event_notifier_enabler = file->private_data;
1555 if (event_notifier_enabler)
1556 fput(event_notifier_enabler->group->file);
1557 break;
1558 default:
1559 WARN_ON_ONCE(1);
1560 break;
1561 }
1562
1563 return 0;
1564 }
1565
1566 static const struct file_operations lttng_event_notifier_fops = {
1567 .owner = THIS_MODULE,
1568 .release = lttng_event_notifier_release,
1569 .unlocked_ioctl = lttng_event_notifier_ioctl,
1570 #ifdef CONFIG_COMPAT
1571 .compat_ioctl = lttng_event_notifier_ioctl,
1572 #endif
1573 };
1574
1575 static
1576 int lttng_abi_create_event_notifier(struct file *event_notifier_group_file,
1577 struct lttng_kernel_event_notifier *event_notifier_param)
1578 {
1579 struct lttng_event_notifier_group *event_notifier_group =
1580 event_notifier_group_file->private_data;
1581 int event_notifier_fd, ret;
1582 struct file *event_notifier_file;
1583 void *priv;
1584
1585 switch (event_notifier_param->event.instrumentation) {
1586 case LTTNG_KERNEL_TRACEPOINT:
1587 case LTTNG_KERNEL_KPROBE:
1588 case LTTNG_KERNEL_UPROBE:
1589 case LTTNG_KERNEL_KRETPROBE:
1590 case LTTNG_KERNEL_FUNCTION:
1591 case LTTNG_KERNEL_NOOP:
1592 case LTTNG_KERNEL_SYSCALL:
1593 default:
1594 ret = -EINVAL;
1595 goto inval_instr;
1596 }
1597
1598 event_notifier_param->event.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
1599
1600 event_notifier_fd = lttng_get_unused_fd();
1601 if (event_notifier_fd < 0) {
1602 ret = event_notifier_fd;
1603 goto fd_error;
1604 }
1605
1606 event_notifier_file = anon_inode_getfile("[lttng_event_notifier]",
1607 &lttng_event_notifier_fops,
1608 NULL, O_RDWR);
1609 if (IS_ERR(event_notifier_file)) {
1610 ret = PTR_ERR(event_notifier_file);
1611 goto file_error;
1612 }
1613
1614 /* The event notifier holds a reference on the event notifier group. */
1615 if (!atomic_long_add_unless(&event_notifier_group_file->f_count, 1, LONG_MAX)) {
1616 ret = -EOVERFLOW;
1617 goto refcount_error;
1618 }
1619
1620 if (event_notifier_param->event.instrumentation == LTTNG_KERNEL_TRACEPOINT
1621 || event_notifier_param->event.instrumentation == LTTNG_KERNEL_SYSCALL) {
1622 struct lttng_event_notifier_enabler *enabler;
1623
1624 if (strutils_is_star_glob_pattern(event_notifier_param->event.name)) {
1625 /*
1626 * If the event name is a star globbing pattern,
1627 * we create the special star globbing enabler.
1628 */
1629 enabler = lttng_event_notifier_enabler_create(
1630 event_notifier_group,
1631 LTTNG_ENABLER_FORMAT_STAR_GLOB,
1632 event_notifier_param);
1633 } else {
1634 enabler = lttng_event_notifier_enabler_create(
1635 event_notifier_group,
1636 LTTNG_ENABLER_FORMAT_NAME,
1637 event_notifier_param);
1638 }
1639 priv = enabler;
1640 } else {
1641 struct lttng_event_notifier *event_notifier;
1642
1643 /*
1644 * We tolerate no failure path after event notifier creation.
1645 * It will stay invariant for the rest of the session.
1646 */
1647 event_notifier = lttng_event_notifier_create(NULL,
1648 event_notifier_param->event.token, event_notifier_group,
1649 event_notifier_param, NULL,
1650 event_notifier_param->event.instrumentation);
1651 WARN_ON_ONCE(!event_notifier);
1652 if (IS_ERR(event_notifier)) {
1653 ret = PTR_ERR(event_notifier);
1654 goto event_notifier_error;
1655 }
1656 priv = event_notifier;
1657 }
1658 event_notifier_file->private_data = priv;
1659 fd_install(event_notifier_fd, event_notifier_file);
1660 return event_notifier_fd;
1661
1662 event_notifier_error:
1663 atomic_long_dec(&event_notifier_group_file->f_count);
1664 refcount_error:
1665 fput(event_notifier_file);
1666 file_error:
1667 put_unused_fd(event_notifier_fd);
1668 fd_error:
1669 inval_instr:
1670 return ret;
1671 }
1672
1673 static
1674 long lttng_event_notifier_group_ioctl(struct file *file, unsigned int cmd,
1675 unsigned long arg)
1676 {
1677 switch (cmd) {
1678 case LTTNG_KERNEL_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD:
1679 {
1680 return lttng_abi_open_event_notifier_group_stream(file);
1681 }
1682 case LTTNG_KERNEL_EVENT_NOTIFIER_CREATE:
1683 {
1684 struct lttng_kernel_event_notifier uevent_notifier_param;
1685
1686 if (copy_from_user(&uevent_notifier_param,
1687 (struct lttng_kernel_event_notifier __user *) arg,
1688 sizeof(uevent_notifier_param)))
1689 return -EFAULT;
1690 return lttng_abi_create_event_notifier(file, &uevent_notifier_param);
1691 }
1692 default:
1693 return -ENOIOCTLCMD;
1694 }
1695 return 0;
1696 }
1697
1698 static
1699 int lttng_event_notifier_group_release(struct inode *inode, struct file *file)
1700 {
1701 struct lttng_event_notifier_group *event_notifier_group =
1702 file->private_data;
1703
1704 if (event_notifier_group)
1705 lttng_event_notifier_group_destroy(event_notifier_group);
1706 return 0;
1707 }
1708
1709 static const struct file_operations lttng_event_notifier_group_fops = {
1710 .owner = THIS_MODULE,
1711 .release = lttng_event_notifier_group_release,
1712 .unlocked_ioctl = lttng_event_notifier_group_ioctl,
1713 #ifdef CONFIG_COMPAT
1714 .compat_ioctl = lttng_event_notifier_group_ioctl,
1715 #endif
1716 };
1717
1718 /**
1719 * lttng_channel_ioctl - lttng syscall through ioctl
1720 *
1721 * @file: the file
1722 * @cmd: the command
1723 * @arg: command arg
1724 *
1725 * This ioctl implements lttng commands:
1726 * LTTNG_KERNEL_STREAM
1727 * Returns an event stream file descriptor or failure.
1728 * (typically, one event stream records events from one CPU)
1729 * LTTNG_KERNEL_EVENT
1730 * Returns an event file descriptor or failure.
1731 * LTTNG_KERNEL_CONTEXT
1732 * Prepend a context field to each event in the channel
1733 * LTTNG_KERNEL_ENABLE
1734 * Enable recording for events in this channel (weak enable)
1735 * LTTNG_KERNEL_DISABLE
1736 * Disable recording for events in this channel (strong disable)
1737 *
1738 * Channel and event file descriptors also hold a reference on the session.
1739 */
1740 static
1741 long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1742 {
1743 struct lttng_channel *channel = file->private_data;
1744
1745 switch (cmd) {
1746 case LTTNG_KERNEL_OLD_STREAM:
1747 case LTTNG_KERNEL_STREAM:
1748 return lttng_abi_open_stream(file);
1749 case LTTNG_KERNEL_OLD_EVENT:
1750 {
1751 struct lttng_kernel_event *uevent_param;
1752 struct lttng_kernel_old_event *old_uevent_param;
1753 int ret;
1754
1755 uevent_param = kmalloc(sizeof(struct lttng_kernel_event),
1756 GFP_KERNEL);
1757 if (!uevent_param) {
1758 ret = -ENOMEM;
1759 goto old_event_end;
1760 }
1761 old_uevent_param = kmalloc(
1762 sizeof(struct lttng_kernel_old_event),
1763 GFP_KERNEL);
1764 if (!old_uevent_param) {
1765 ret = -ENOMEM;
1766 goto old_event_error_free_param;
1767 }
1768 if (copy_from_user(old_uevent_param,
1769 (struct lttng_kernel_old_event __user *) arg,
1770 sizeof(struct lttng_kernel_old_event))) {
1771 ret = -EFAULT;
1772 goto old_event_error_free_old_param;
1773 }
1774
1775 memcpy(uevent_param->name, old_uevent_param->name,
1776 sizeof(uevent_param->name));
1777 uevent_param->instrumentation =
1778 old_uevent_param->instrumentation;
1779
1780 switch (old_uevent_param->instrumentation) {
1781 case LTTNG_KERNEL_KPROBE:
1782 uevent_param->u.kprobe.addr =
1783 old_uevent_param->u.kprobe.addr;
1784 uevent_param->u.kprobe.offset =
1785 old_uevent_param->u.kprobe.offset;
1786 memcpy(uevent_param->u.kprobe.symbol_name,
1787 old_uevent_param->u.kprobe.symbol_name,
1788 sizeof(uevent_param->u.kprobe.symbol_name));
1789 break;
1790 case LTTNG_KERNEL_KRETPROBE:
1791 uevent_param->u.kretprobe.addr =
1792 old_uevent_param->u.kretprobe.addr;
1793 uevent_param->u.kretprobe.offset =
1794 old_uevent_param->u.kretprobe.offset;
1795 memcpy(uevent_param->u.kretprobe.symbol_name,
1796 old_uevent_param->u.kretprobe.symbol_name,
1797 sizeof(uevent_param->u.kretprobe.symbol_name));
1798 break;
1799 case LTTNG_KERNEL_FUNCTION:
1800 WARN_ON_ONCE(1);
1801 /* Not implemented. */
1802 break;
1803 default:
1804 break;
1805 }
1806 ret = lttng_abi_create_event(file, uevent_param);
1807
1808 old_event_error_free_old_param:
1809 kfree(old_uevent_param);
1810 old_event_error_free_param:
1811 kfree(uevent_param);
1812 old_event_end:
1813 return ret;
1814 }
1815 case LTTNG_KERNEL_EVENT:
1816 {
1817 struct lttng_kernel_event uevent_param;
1818
1819 if (copy_from_user(&uevent_param,
1820 (struct lttng_kernel_event __user *) arg,
1821 sizeof(uevent_param)))
1822 return -EFAULT;
1823 return lttng_abi_create_event(file, &uevent_param);
1824 }
1825 case LTTNG_KERNEL_OLD_CONTEXT:
1826 {
1827 struct lttng_kernel_context *ucontext_param;
1828 struct lttng_kernel_old_context *old_ucontext_param;
1829 int ret;
1830
1831 ucontext_param = kmalloc(sizeof(struct lttng_kernel_context),
1832 GFP_KERNEL);
1833 if (!ucontext_param) {
1834 ret = -ENOMEM;
1835 goto old_ctx_end;
1836 }
1837 old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context),
1838 GFP_KERNEL);
1839 if (!old_ucontext_param) {
1840 ret = -ENOMEM;
1841 goto old_ctx_error_free_param;
1842 }
1843
1844 if (copy_from_user(old_ucontext_param,
1845 (struct lttng_kernel_old_context __user *) arg,
1846 sizeof(struct lttng_kernel_old_context))) {
1847 ret = -EFAULT;
1848 goto old_ctx_error_free_old_param;
1849 }
1850 ucontext_param->ctx = old_ucontext_param->ctx;
1851 memcpy(ucontext_param->padding, old_ucontext_param->padding,
1852 sizeof(ucontext_param->padding));
1853 /* only type that uses the union */
1854 if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) {
1855 ucontext_param->u.perf_counter.type =
1856 old_ucontext_param->u.perf_counter.type;
1857 ucontext_param->u.perf_counter.config =
1858 old_ucontext_param->u.perf_counter.config;
1859 memcpy(ucontext_param->u.perf_counter.name,
1860 old_ucontext_param->u.perf_counter.name,
1861 sizeof(ucontext_param->u.perf_counter.name));
1862 }
1863
1864 ret = lttng_abi_add_context(file,
1865 ucontext_param,
1866 &channel->ctx, channel->session);
1867
1868 old_ctx_error_free_old_param:
1869 kfree(old_ucontext_param);
1870 old_ctx_error_free_param:
1871 kfree(ucontext_param);
1872 old_ctx_end:
1873 return ret;
1874 }
1875 case LTTNG_KERNEL_CONTEXT:
1876 {
1877 struct lttng_kernel_context ucontext_param;
1878
1879 if (copy_from_user(&ucontext_param,
1880 (struct lttng_kernel_context __user *) arg,
1881 sizeof(ucontext_param)))
1882 return -EFAULT;
1883 return lttng_abi_add_context(file,
1884 &ucontext_param,
1885 &channel->ctx, channel->session);
1886 }
1887 case LTTNG_KERNEL_OLD_ENABLE:
1888 case LTTNG_KERNEL_ENABLE:
1889 return lttng_channel_enable(channel);
1890 case LTTNG_KERNEL_OLD_DISABLE:
1891 case LTTNG_KERNEL_DISABLE:
1892 return lttng_channel_disable(channel);
1893 case LTTNG_KERNEL_SYSCALL_MASK:
1894 return lttng_channel_syscall_mask(channel,
1895 (struct lttng_kernel_syscall_mask __user *) arg);
1896 default:
1897 return -ENOIOCTLCMD;
1898 }
1899 }
1900
1901 /**
1902 * lttng_metadata_ioctl - lttng syscall through ioctl
1903 *
1904 * @file: the file
1905 * @cmd: the command
1906 * @arg: command arg
1907 *
1908 * This ioctl implements lttng commands:
1909 * LTTNG_KERNEL_STREAM
1910 * Returns an event stream file descriptor or failure.
1911 *
1912 * Channel and event file descriptors also hold a reference on the session.
1913 */
1914 static
1915 long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1916 {
1917 switch (cmd) {
1918 case LTTNG_KERNEL_OLD_STREAM:
1919 case LTTNG_KERNEL_STREAM:
1920 return lttng_abi_open_metadata_stream(file);
1921 default:
1922 return -ENOIOCTLCMD;
1923 }
1924 }
1925
1926 /**
1927 * lttng_channel_poll - lttng stream addition/removal monitoring
1928 *
1929 * @file: the file
1930 * @wait: poll table
1931 */
1932 unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
1933 {
1934 struct lttng_channel *channel = file->private_data;
1935 unsigned int mask = 0;
1936
1937 if (file->f_mode & FMODE_READ) {
1938 poll_wait_set_exclusive(wait);
1939 poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan),
1940 wait);
1941
1942 if (channel->ops->is_disabled(channel->chan))
1943 return POLLERR;
1944 if (channel->ops->is_finalized(channel->chan))
1945 return POLLHUP;
1946 if (channel->ops->buffer_has_read_closed_stream(channel->chan))
1947 return POLLIN | POLLRDNORM;
1948 return 0;
1949 }
1950 return mask;
1951
1952 }
1953
1954 static
1955 int lttng_channel_release(struct inode *inode, struct file *file)
1956 {
1957 struct lttng_channel *channel = file->private_data;
1958
1959 if (channel)
1960 fput(channel->session->file);
1961 return 0;
1962 }
1963
1964 static
1965 int lttng_metadata_channel_release(struct inode *inode, struct file *file)
1966 {
1967 struct lttng_channel *channel = file->private_data;
1968
1969 if (channel) {
1970 fput(channel->session->file);
1971 lttng_metadata_channel_destroy(channel);
1972 }
1973
1974 return 0;
1975 }
1976
1977 static const struct file_operations lttng_channel_fops = {
1978 .owner = THIS_MODULE,
1979 .release = lttng_channel_release,
1980 .poll = lttng_channel_poll,
1981 .unlocked_ioctl = lttng_channel_ioctl,
1982 #ifdef CONFIG_COMPAT
1983 .compat_ioctl = lttng_channel_ioctl,
1984 #endif
1985 };
1986
1987 static const struct file_operations lttng_metadata_fops = {
1988 .owner = THIS_MODULE,
1989 .release = lttng_metadata_channel_release,
1990 .unlocked_ioctl = lttng_metadata_ioctl,
1991 #ifdef CONFIG_COMPAT
1992 .compat_ioctl = lttng_metadata_ioctl,
1993 #endif
1994 };
1995
1996 /**
1997 * lttng_event_ioctl - lttng syscall through ioctl
1998 *
1999 * @file: the file
2000 * @cmd: the command
2001 * @arg: command arg
2002 *
2003 * This ioctl implements lttng commands:
2004 * LTTNG_KERNEL_CONTEXT
2005 * Prepend a context field to each record of this event
2006 * LTTNG_KERNEL_ENABLE
2007 * Enable recording for this event (weak enable)
2008 * LTTNG_KERNEL_DISABLE
2009 * Disable recording for this event (strong disable)
2010 */
2011 static
2012 long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2013 {
2014 struct lttng_event *event;
2015 struct lttng_event_enabler *event_enabler;
2016 enum lttng_event_type *evtype = file->private_data;
2017
2018 switch (cmd) {
2019 case LTTNG_KERNEL_OLD_CONTEXT:
2020 {
2021 /* Not implemented */
2022 return -ENOSYS;
2023 }
2024 case LTTNG_KERNEL_CONTEXT:
2025 {
2026 /* Not implemented */
2027 return -ENOSYS;
2028 }
2029 case LTTNG_KERNEL_OLD_ENABLE:
2030 case LTTNG_KERNEL_ENABLE:
2031 switch (*evtype) {
2032 case LTTNG_TYPE_EVENT:
2033 event = file->private_data;
2034 return lttng_event_enable(event);
2035 case LTTNG_TYPE_ENABLER:
2036 event_enabler = file->private_data;
2037 return lttng_event_enabler_enable(event_enabler);
2038 default:
2039 WARN_ON_ONCE(1);
2040 return -ENOSYS;
2041 }
2042 case LTTNG_KERNEL_OLD_DISABLE:
2043 case LTTNG_KERNEL_DISABLE:
2044 switch (*evtype) {
2045 case LTTNG_TYPE_EVENT:
2046 event = file->private_data;
2047 return lttng_event_disable(event);
2048 case LTTNG_TYPE_ENABLER:
2049 event_enabler = file->private_data;
2050 return lttng_event_enabler_disable(event_enabler);
2051 default:
2052 WARN_ON_ONCE(1);
2053 return -ENOSYS;
2054 }
2055 case LTTNG_KERNEL_FILTER:
2056 switch (*evtype) {
2057 case LTTNG_TYPE_EVENT:
2058 return -EINVAL;
2059 case LTTNG_TYPE_ENABLER:
2060 {
2061 event_enabler = file->private_data;
2062 return lttng_event_enabler_attach_bytecode(event_enabler,
2063 (struct lttng_kernel_filter_bytecode __user *) arg);
2064 }
2065 default:
2066 WARN_ON_ONCE(1);
2067 return -ENOSYS;
2068 }
2069 case LTTNG_KERNEL_ADD_CALLSITE:
2070 switch (*evtype) {
2071 case LTTNG_TYPE_EVENT:
2072 event = file->private_data;
2073 return lttng_event_add_callsite(event,
2074 (struct lttng_kernel_event_callsite __user *) arg);
2075 case LTTNG_TYPE_ENABLER:
2076 return -EINVAL;
2077 default:
2078 WARN_ON_ONCE(1);
2079 return -ENOSYS;
2080 }
2081 default:
2082 return -ENOIOCTLCMD;
2083 }
2084 }
2085
2086 static
2087 int lttng_event_release(struct inode *inode, struct file *file)
2088 {
2089 struct lttng_event *event;
2090 struct lttng_event_enabler *event_enabler;
2091 enum lttng_event_type *evtype = file->private_data;
2092
2093 if (!evtype)
2094 return 0;
2095
2096 switch (*evtype) {
2097 case LTTNG_TYPE_EVENT:
2098 event = file->private_data;
2099 if (event)
2100 fput(event->chan->file);
2101 break;
2102 case LTTNG_TYPE_ENABLER:
2103 event_enabler = file->private_data;
2104 if (event_enabler)
2105 fput(event_enabler->chan->file);
2106 break;
2107 default:
2108 WARN_ON_ONCE(1);
2109 break;
2110 }
2111
2112 return 0;
2113 }
2114
2115 /* TODO: filter control ioctl */
2116 static const struct file_operations lttng_event_fops = {
2117 .owner = THIS_MODULE,
2118 .release = lttng_event_release,
2119 .unlocked_ioctl = lttng_event_ioctl,
2120 #ifdef CONFIG_COMPAT
2121 .compat_ioctl = lttng_event_ioctl,
2122 #endif
2123 };
2124
2125 static int put_u64(uint64_t val, unsigned long arg)
2126 {
2127 return put_user(val, (uint64_t __user *) arg);
2128 }
2129
2130 static int put_u32(uint32_t val, unsigned long arg)
2131 {
2132 return put_user(val, (uint32_t __user *) arg);
2133 }
2134
2135 static long lttng_stream_ring_buffer_ioctl(struct file *filp,
2136 unsigned int cmd, unsigned long arg)
2137 {
2138 struct lib_ring_buffer *buf = filp->private_data;
2139 struct channel *chan = buf->backend.chan;
2140 const struct lib_ring_buffer_config *config = &chan->backend.config;
2141 const struct lttng_channel_ops *ops = chan->backend.priv_ops;
2142 int ret;
2143
2144 if (atomic_read(&chan->record_disabled))
2145 return -EIO;
2146
2147 switch (cmd) {
2148 case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN:
2149 {
2150 uint64_t ts;
2151
2152 ret = ops->timestamp_begin(config, buf, &ts);
2153 if (ret < 0)
2154 goto error;
2155 return put_u64(ts, arg);
2156 }
2157 case LTTNG_RING_BUFFER_GET_TIMESTAMP_END:
2158 {
2159 uint64_t ts;
2160
2161 ret = ops->timestamp_end(config, buf, &ts);
2162 if (ret < 0)
2163 goto error;
2164 return put_u64(ts, arg);
2165 }
2166 case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED:
2167 {
2168 uint64_t ed;
2169
2170 ret = ops->events_discarded(config, buf, &ed);
2171 if (ret < 0)
2172 goto error;
2173 return put_u64(ed, arg);
2174 }
2175 case LTTNG_RING_BUFFER_GET_CONTENT_SIZE:
2176 {
2177 uint64_t cs;
2178
2179 ret = ops->content_size(config, buf, &cs);
2180 if (ret < 0)
2181 goto error;
2182 return put_u64(cs, arg);
2183 }
2184 case LTTNG_RING_BUFFER_GET_PACKET_SIZE:
2185 {
2186 uint64_t ps;
2187
2188 ret = ops->packet_size(config, buf, &ps);
2189 if (ret < 0)
2190 goto error;
2191 return put_u64(ps, arg);
2192 }
2193 case LTTNG_RING_BUFFER_GET_STREAM_ID:
2194 {
2195 uint64_t si;
2196
2197 ret = ops->stream_id(config, buf, &si);
2198 if (ret < 0)
2199 goto error;
2200 return put_u64(si, arg);
2201 }
2202 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP:
2203 {
2204 uint64_t ts;
2205
2206 ret = ops->current_timestamp(config, buf, &ts);
2207 if (ret < 0)
2208 goto error;
2209 return put_u64(ts, arg);
2210 }
2211 case LTTNG_RING_BUFFER_GET_SEQ_NUM:
2212 {
2213 uint64_t seq;
2214
2215 ret = ops->sequence_number(config, buf, &seq);
2216 if (ret < 0)
2217 goto error;
2218 return put_u64(seq, arg);
2219 }
2220 case LTTNG_RING_BUFFER_INSTANCE_ID:
2221 {
2222 uint64_t id;
2223
2224 ret = ops->instance_id(config, buf, &id);
2225 if (ret < 0)
2226 goto error;
2227 return put_u64(id, arg);
2228 }
2229 default:
2230 return lib_ring_buffer_file_operations.unlocked_ioctl(filp,
2231 cmd, arg);
2232 }
2233
2234 error:
2235 return -ENOSYS;
2236 }
2237
2238 #ifdef CONFIG_COMPAT
2239 static long lttng_stream_ring_buffer_compat_ioctl(struct file *filp,
2240 unsigned int cmd, unsigned long arg)
2241 {
2242 struct lib_ring_buffer *buf = filp->private_data;
2243 struct channel *chan = buf->backend.chan;
2244 const struct lib_ring_buffer_config *config = &chan->backend.config;
2245 const struct lttng_channel_ops *ops = chan->backend.priv_ops;
2246 int ret;
2247
2248 if (atomic_read(&chan->record_disabled))
2249 return -EIO;
2250
2251 switch (cmd) {
2252 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN:
2253 {
2254 uint64_t ts;
2255
2256 ret = ops->timestamp_begin(config, buf, &ts);
2257 if (ret < 0)
2258 goto error;
2259 return put_u64(ts, arg);
2260 }
2261 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END:
2262 {
2263 uint64_t ts;
2264
2265 ret = ops->timestamp_end(config, buf, &ts);
2266 if (ret < 0)
2267 goto error;
2268 return put_u64(ts, arg);
2269 }
2270 case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED:
2271 {
2272 uint64_t ed;
2273
2274 ret = ops->events_discarded(config, buf, &ed);
2275 if (ret < 0)
2276 goto error;
2277 return put_u64(ed, arg);
2278 }
2279 case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE:
2280 {
2281 uint64_t cs;
2282
2283 ret = ops->content_size(config, buf, &cs);
2284 if (ret < 0)
2285 goto error;
2286 return put_u64(cs, arg);
2287 }
2288 case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE:
2289 {
2290 uint64_t ps;
2291
2292 ret = ops->packet_size(config, buf, &ps);
2293 if (ret < 0)
2294 goto error;
2295 return put_u64(ps, arg);
2296 }
2297 case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID:
2298 {
2299 uint64_t si;
2300
2301 ret = ops->stream_id(config, buf, &si);
2302 if (ret < 0)
2303 goto error;
2304 return put_u64(si, arg);
2305 }
2306 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP:
2307 {
2308 uint64_t ts;
2309
2310 ret = ops->current_timestamp(config, buf, &ts);
2311 if (ret < 0)
2312 goto error;
2313 return put_u64(ts, arg);
2314 }
2315 case LTTNG_RING_BUFFER_COMPAT_GET_SEQ_NUM:
2316 {
2317 uint64_t seq;
2318
2319 ret = ops->sequence_number(config, buf, &seq);
2320 if (ret < 0)
2321 goto error;
2322 return put_u64(seq, arg);
2323 }
2324 case LTTNG_RING_BUFFER_COMPAT_INSTANCE_ID:
2325 {
2326 uint64_t id;
2327
2328 ret = ops->instance_id(config, buf, &id);
2329 if (ret < 0)
2330 goto error;
2331 return put_u64(id, arg);
2332 }
2333 default:
2334 return lib_ring_buffer_file_operations.compat_ioctl(filp,
2335 cmd, arg);
2336 }
2337
2338 error:
2339 return -ENOSYS;
2340 }
2341 #endif /* CONFIG_COMPAT */
2342
2343 static void lttng_stream_override_ring_buffer_fops(void)
2344 {
2345 lttng_stream_ring_buffer_file_operations.owner = THIS_MODULE;
2346 lttng_stream_ring_buffer_file_operations.open =
2347 lib_ring_buffer_file_operations.open;
2348 lttng_stream_ring_buffer_file_operations.release =
2349 lib_ring_buffer_file_operations.release;
2350 lttng_stream_ring_buffer_file_operations.poll =
2351 lib_ring_buffer_file_operations.poll;
2352 lttng_stream_ring_buffer_file_operations.splice_read =
2353 lib_ring_buffer_file_operations.splice_read;
2354 lttng_stream_ring_buffer_file_operations.mmap =
2355 lib_ring_buffer_file_operations.mmap;
2356 lttng_stream_ring_buffer_file_operations.unlocked_ioctl =
2357 lttng_stream_ring_buffer_ioctl;
2358 lttng_stream_ring_buffer_file_operations.llseek =
2359 lib_ring_buffer_file_operations.llseek;
2360 #ifdef CONFIG_COMPAT
2361 lttng_stream_ring_buffer_file_operations.compat_ioctl =
2362 lttng_stream_ring_buffer_compat_ioctl;
2363 #endif
2364 }
2365
2366 int __init lttng_abi_init(void)
2367 {
2368 int ret = 0;
2369
2370 wrapper_vmalloc_sync_mappings();
2371 lttng_clock_ref();
2372
2373 ret = lttng_tp_mempool_init();
2374 if (ret) {
2375 goto error;
2376 }
2377
2378 lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL,
2379 &lttng_proc_ops, NULL);
2380
2381 if (!lttng_proc_dentry) {
2382 printk(KERN_ERR "LTTng: Error creating control file\n");
2383 ret = -ENOMEM;
2384 goto error;
2385 }
2386 lttng_stream_override_ring_buffer_fops();
2387 return 0;
2388
2389 error:
2390 lttng_tp_mempool_destroy();
2391 lttng_clock_unref();
2392 return ret;
2393 }
2394
2395 /* No __exit annotation because used by init error path too. */
2396 void lttng_abi_exit(void)
2397 {
2398 lttng_tp_mempool_destroy();
2399 lttng_clock_unref();
2400 if (lttng_proc_dentry)
2401 remove_proc_entry("lttng", NULL);
2402 }
This page took 0.12963 seconds and 3 git commands to generate.