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