Fix: lttng ABI: lttng_counter_ioctl() tainted scalar
[lttng-modules.git] / src / lttng-abi.c
CommitLineData
b7cdc182 1/* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
9f36eaed 2 *
e8951e63 3 * lttng-abi.c
baf20995 4 *
e8951e63 5 * LTTng ABI
baf20995 6 *
886d51a3
MD
7 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
baf20995
MD
9 * Mimic system calls for:
10 * - session creation, returns a file descriptor or failure.
ad1c05e1
MD
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.
baf20995
MD
24 */
25
11b5a3c2 26#include <linux/module.h>
e6a17f26 27#include <linux/proc_fs.h>
11b5a3c2
MD
28#include <linux/anon_inodes.h>
29#include <linux/file.h>
30#include <linux/uaccess.h>
31#include <linux/slab.h>
abc0446a 32#include <linux/err.h>
263b6c88 33#include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_mappings() */
24591303
MD
34#include <ringbuffer/vfs.h>
35#include <ringbuffer/backend.h>
36#include <ringbuffer/frontend.h>
c190d76e 37#include <wrapper/compiler_attributes.h>
241ae9a8
MD
38#include <wrapper/poll.h>
39#include <wrapper/file.h>
40#include <wrapper/kref.h>
6657edec 41#include <wrapper/barrier.h>
2df37e95
MD
42#include <lttng/string-utils.h>
43#include <lttng/abi.h>
44#include <lttng/abi-old.h>
45#include <lttng/events.h>
92bc1e23 46#include <lttng/events-internal.h>
2df37e95
MD
47#include <lttng/tracer.h>
48#include <lttng/tp-mempool.h>
24591303 49#include <ringbuffer/frontend_types.h>
db2511b4 50#include <ringbuffer/iterator.h>
baf20995
MD
51
52/*
53 * This is LTTng's own personal way to create a system call as an external
80996790 54 * module. We use ioctl() on /proc/lttng.
baf20995
MD
55 */
56
e6a17f26 57static struct proc_dir_entry *lttng_proc_dentry;
059de147 58
5f4c791e 59#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,6,0))
059de147
MJ
60static const struct proc_ops lttng_proc_ops;
61#else
62static const struct file_operations lttng_proc_ops;
63#endif
64
ad1c05e1 65static const struct file_operations lttng_session_fops;
750b05f2 66static const struct file_operations lttng_event_notifier_group_fops;
ad1c05e1 67static const struct file_operations lttng_channel_fops;
5dbbdb43 68static const struct file_operations lttng_metadata_fops;
ef784b4d
MD
69static const struct file_operations lttng_event_recorder_event_fops;
70static const struct file_operations lttng_event_recorder_enabler_fops;
ed8d02d6 71static struct file_operations lttng_stream_ring_buffer_file_operations;
baf20995 72
9616f0bf 73static int put_u64(uint64_t val, unsigned long arg);
8b97fd42 74static int put_u32(uint32_t val, unsigned long arg);
9616f0bf 75
99f52fcc
FD
76static int validate_zeroed_padding(char *p, size_t len)
77{
78 size_t i;
79
80 for (i = 0; i < len; i++) {
81 if (p[i])
82 return -1;
83 }
84 return 0;
85}
86
a33c9927
MD
87/*
88 * Teardown management: opened file descriptors keep a refcount on the module,
89 * so it can only exit when all file descriptors are closed.
90 */
91
ad1c05e1 92static
baf20995
MD
93int lttng_abi_create_session(void)
94{
8cdc1a81 95 struct lttng_kernel_session *session;
c0e31d2e 96 struct file *session_file;
11b5a3c2 97 int session_fd, ret;
baf20995 98
a90917c3 99 session = lttng_session_create();
baf20995
MD
100 if (!session)
101 return -ENOMEM;
4ac10b76 102 session_fd = lttng_get_unused_fd();
baf20995
MD
103 if (session_fd < 0) {
104 ret = session_fd;
105 goto fd_error;
106 }
c0e31d2e 107 session_file = anon_inode_getfile("[lttng_session]",
ad1c05e1 108 &lttng_session_fops,
baf20995 109 session, O_RDWR);
c0e31d2e
MD
110 if (IS_ERR(session_file)) {
111 ret = PTR_ERR(session_file);
baf20995
MD
112 goto file_error;
113 }
8cdc1a81 114 session->priv->file = session_file;
c0e31d2e 115 fd_install(session_fd, session_file);
baf20995
MD
116 return session_fd;
117
118file_error:
119 put_unused_fd(session_fd);
120fd_error:
a90917c3 121 lttng_session_destroy(session);
baf20995
MD
122 return ret;
123}
124
21f58fb7
FD
125void event_notifier_send_notification_work_wakeup(struct irq_work *entry)
126{
127 struct lttng_event_notifier_group *event_notifier_group =
128 container_of(entry, struct lttng_event_notifier_group,
129 wakeup_pending);
130 wake_up_interruptible(&event_notifier_group->read_wait);
131}
132
750b05f2
FD
133static
134int lttng_abi_create_event_notifier_group(void)
135{
136 struct lttng_event_notifier_group *event_notifier_group;
137 struct file *event_notifier_group_file;
138 int event_notifier_group_fd, ret;
139
140 event_notifier_group = lttng_event_notifier_group_create();
141 if (!event_notifier_group)
142 return -ENOMEM;
143
144 event_notifier_group_fd = lttng_get_unused_fd();
145 if (event_notifier_group_fd < 0) {
146 ret = event_notifier_group_fd;
147 goto fd_error;
148 }
149 event_notifier_group_file = anon_inode_getfile("[lttng_event_notifier_group]",
150 &lttng_event_notifier_group_fops,
151 event_notifier_group, O_RDWR);
152 if (IS_ERR(event_notifier_group_file)) {
153 ret = PTR_ERR(event_notifier_group_file);
154 goto file_error;
155 }
156
157 event_notifier_group->file = event_notifier_group_file;
21f58fb7
FD
158 init_waitqueue_head(&event_notifier_group->read_wait);
159 init_irq_work(&event_notifier_group->wakeup_pending,
160 event_notifier_send_notification_work_wakeup);
750b05f2
FD
161 fd_install(event_notifier_group_fd, event_notifier_group_file);
162 return event_notifier_group_fd;
163
164file_error:
165 put_unused_fd(event_notifier_group_fd);
166fd_error:
167 lttng_event_notifier_group_destroy(event_notifier_group);
168 return ret;
169}
170
271b6681
MD
171static
172int lttng_abi_tracepoint_list(void)
173{
174 struct file *tracepoint_list_file;
175 int file_fd, ret;
176
4ac10b76 177 file_fd = lttng_get_unused_fd();
271b6681
MD
178 if (file_fd < 0) {
179 ret = file_fd;
180 goto fd_error;
181 }
30f18bf0 182
8ee099b6 183 tracepoint_list_file = anon_inode_getfile("[lttng_tracepoint_list]",
271b6681
MD
184 &lttng_tracepoint_list_fops,
185 NULL, O_RDWR);
186 if (IS_ERR(tracepoint_list_file)) {
187 ret = PTR_ERR(tracepoint_list_file);
188 goto file_error;
189 }
30f18bf0
MD
190 ret = lttng_tracepoint_list_fops.open(NULL, tracepoint_list_file);
191 if (ret < 0)
192 goto open_error;
271b6681
MD
193 fd_install(file_fd, tracepoint_list_file);
194 return file_fd;
195
30f18bf0
MD
196open_error:
197 fput(tracepoint_list_file);
271b6681
MD
198file_error:
199 put_unused_fd(file_fd);
200fd_error:
201 return ret;
202}
203
f127e61e
MD
204#ifndef CONFIG_HAVE_SYSCALL_TRACEPOINTS
205static inline
206int lttng_abi_syscall_list(void)
207{
208 return -ENOSYS;
209}
210#else
211static
212int lttng_abi_syscall_list(void)
213{
214 struct file *syscall_list_file;
215 int file_fd, ret;
216
217 file_fd = lttng_get_unused_fd();
218 if (file_fd < 0) {
219 ret = file_fd;
220 goto fd_error;
221 }
222
223 syscall_list_file = anon_inode_getfile("[lttng_syscall_list]",
224 &lttng_syscall_list_fops,
225 NULL, O_RDWR);
226 if (IS_ERR(syscall_list_file)) {
227 ret = PTR_ERR(syscall_list_file);
228 goto file_error;
229 }
230 ret = lttng_syscall_list_fops.open(NULL, syscall_list_file);
231 if (ret < 0)
232 goto open_error;
233 fd_install(file_fd, syscall_list_file);
f127e61e
MD
234 return file_fd;
235
236open_error:
237 fput(syscall_list_file);
238file_error:
239 put_unused_fd(file_fd);
240fd_error:
241 return ret;
242}
243#endif
244
80c16bcf 245static
606828e4 246void lttng_abi_tracer_version(struct lttng_kernel_abi_tracer_version *v)
80c16bcf 247{
6dccd6c1
JD
248 v->major = LTTNG_MODULES_MAJOR_VERSION;
249 v->minor = LTTNG_MODULES_MINOR_VERSION;
250 v->patchlevel = LTTNG_MODULES_PATCHLEVEL_VERSION;
80c16bcf
MD
251}
252
42cabb80 253static
606828e4 254void lttng_abi_tracer_abi_version(struct lttng_kernel_abi_tracer_abi_version *v)
42cabb80 255{
606828e4
MD
256 v->major = LTTNG_KERNEL_ABI_MAJOR_VERSION;
257 v->minor = LTTNG_KERNEL_ABI_MINOR_VERSION;
42cabb80
MD
258}
259
8070f5c0
MD
260static
261long lttng_abi_add_context(struct file *file,
606828e4 262 struct lttng_kernel_abi_context *context_param,
8cdc1a81 263 struct lttng_kernel_ctx **ctx, struct lttng_kernel_session *session)
8070f5c0 264{
8070f5c0 265
8cdc1a81 266 if (session->priv->been_active)
8070f5c0
MD
267 return -EPERM;
268
6dccd6c1 269 switch (context_param->ctx) {
606828e4 270 case LTTNG_KERNEL_ABI_CONTEXT_PID:
8070f5c0 271 return lttng_add_pid_to_ctx(ctx);
606828e4 272 case LTTNG_KERNEL_ABI_CONTEXT_PRIO:
a8ad3613 273 return lttng_add_prio_to_ctx(ctx);
606828e4 274 case LTTNG_KERNEL_ABI_CONTEXT_NICE:
53f1f0ca 275 return lttng_add_nice_to_ctx(ctx);
606828e4 276 case LTTNG_KERNEL_ABI_CONTEXT_VPID:
b64bc438 277 return lttng_add_vpid_to_ctx(ctx);
606828e4 278 case LTTNG_KERNEL_ABI_CONTEXT_TID:
b64bc438 279 return lttng_add_tid_to_ctx(ctx);
606828e4 280 case LTTNG_KERNEL_ABI_CONTEXT_VTID:
b64bc438 281 return lttng_add_vtid_to_ctx(ctx);
606828e4 282 case LTTNG_KERNEL_ABI_CONTEXT_PPID:
b64bc438 283 return lttng_add_ppid_to_ctx(ctx);
606828e4 284 case LTTNG_KERNEL_ABI_CONTEXT_VPPID:
b64bc438 285 return lttng_add_vppid_to_ctx(ctx);
606828e4
MD
286 case LTTNG_KERNEL_ABI_CONTEXT_PERF_COUNTER:
287 context_param->u.perf_counter.name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0';
6dccd6c1
JD
288 return lttng_add_perf_counter_to_ctx(context_param->u.perf_counter.type,
289 context_param->u.perf_counter.config,
290 context_param->u.perf_counter.name,
c24a0d71 291 ctx);
606828e4 292 case LTTNG_KERNEL_ABI_CONTEXT_PROCNAME:
a2563e83 293 return lttng_add_procname_to_ctx(ctx);
606828e4 294 case LTTNG_KERNEL_ABI_CONTEXT_HOSTNAME:
975da2c0 295 return lttng_add_hostname_to_ctx(ctx);
606828e4 296 case LTTNG_KERNEL_ABI_CONTEXT_CPU_ID:
b3699d90 297 return lttng_add_cpu_id_to_ctx(ctx);
606828e4 298 case LTTNG_KERNEL_ABI_CONTEXT_INTERRUPTIBLE:
79150a49 299 return lttng_add_interruptible_to_ctx(ctx);
606828e4 300 case LTTNG_KERNEL_ABI_CONTEXT_NEED_RESCHEDULE:
79150a49 301 return lttng_add_need_reschedule_to_ctx(ctx);
606828e4 302 case LTTNG_KERNEL_ABI_CONTEXT_PREEMPTIBLE:
79150a49 303 return lttng_add_preemptible_to_ctx(ctx);
606828e4 304 case LTTNG_KERNEL_ABI_CONTEXT_MIGRATABLE:
79150a49 305 return lttng_add_migratable_to_ctx(ctx);
606828e4
MD
306 case LTTNG_KERNEL_ABI_CONTEXT_CALLSTACK_KERNEL:
307 case LTTNG_KERNEL_ABI_CONTEXT_CALLSTACK_USER:
2fa2d39a 308 return lttng_add_callstack_to_ctx(ctx, context_param->ctx);
606828e4 309 case LTTNG_KERNEL_ABI_CONTEXT_CGROUP_NS:
a6cf40a4 310 return lttng_add_cgroup_ns_to_ctx(ctx);
606828e4 311 case LTTNG_KERNEL_ABI_CONTEXT_IPC_NS:
a6cf40a4 312 return lttng_add_ipc_ns_to_ctx(ctx);
606828e4 313 case LTTNG_KERNEL_ABI_CONTEXT_MNT_NS:
a6cf40a4 314 return lttng_add_mnt_ns_to_ctx(ctx);
606828e4 315 case LTTNG_KERNEL_ABI_CONTEXT_NET_NS:
a6cf40a4 316 return lttng_add_net_ns_to_ctx(ctx);
606828e4 317 case LTTNG_KERNEL_ABI_CONTEXT_PID_NS:
a6cf40a4 318 return lttng_add_pid_ns_to_ctx(ctx);
606828e4 319 case LTTNG_KERNEL_ABI_CONTEXT_USER_NS:
a6cf40a4 320 return lttng_add_user_ns_to_ctx(ctx);
606828e4 321 case LTTNG_KERNEL_ABI_CONTEXT_UTS_NS:
a6cf40a4 322 return lttng_add_uts_ns_to_ctx(ctx);
606828e4 323 case LTTNG_KERNEL_ABI_CONTEXT_UID:
dc923e75 324 return lttng_add_uid_to_ctx(ctx);
606828e4 325 case LTTNG_KERNEL_ABI_CONTEXT_EUID:
dc923e75 326 return lttng_add_euid_to_ctx(ctx);
606828e4 327 case LTTNG_KERNEL_ABI_CONTEXT_SUID:
dc923e75 328 return lttng_add_suid_to_ctx(ctx);
606828e4 329 case LTTNG_KERNEL_ABI_CONTEXT_GID:
dc923e75 330 return lttng_add_gid_to_ctx(ctx);
606828e4 331 case LTTNG_KERNEL_ABI_CONTEXT_EGID:
dc923e75 332 return lttng_add_egid_to_ctx(ctx);
606828e4 333 case LTTNG_KERNEL_ABI_CONTEXT_SGID:
dc923e75 334 return lttng_add_sgid_to_ctx(ctx);
606828e4 335 case LTTNG_KERNEL_ABI_CONTEXT_VUID:
dc923e75 336 return lttng_add_vuid_to_ctx(ctx);
606828e4 337 case LTTNG_KERNEL_ABI_CONTEXT_VEUID:
dc923e75 338 return lttng_add_veuid_to_ctx(ctx);
606828e4 339 case LTTNG_KERNEL_ABI_CONTEXT_VSUID:
dc923e75 340 return lttng_add_vsuid_to_ctx(ctx);
606828e4 341 case LTTNG_KERNEL_ABI_CONTEXT_VGID:
dc923e75 342 return lttng_add_vgid_to_ctx(ctx);
606828e4 343 case LTTNG_KERNEL_ABI_CONTEXT_VEGID:
dc923e75 344 return lttng_add_vegid_to_ctx(ctx);
606828e4 345 case LTTNG_KERNEL_ABI_CONTEXT_VSGID:
dc923e75 346 return lttng_add_vsgid_to_ctx(ctx);
606828e4 347 case LTTNG_KERNEL_ABI_CONTEXT_TIME_NS:
876e2e92 348 return lttng_add_time_ns_to_ctx(ctx);
8070f5c0
MD
349 default:
350 return -EINVAL;
351 }
352}
353
ad1c05e1
MD
354/**
355 * lttng_ioctl - lttng syscall through ioctl
356 *
c0e31d2e 357 * @file: the file
ad1c05e1
MD
358 * @cmd: the command
359 * @arg: command arg
360 *
361 * This ioctl implements lttng commands:
606828e4 362 * LTTNG_KERNEL_ABI_SESSION
ad1c05e1 363 * Returns a LTTng trace session file descriptor
606828e4 364 * LTTNG_KERNEL_ABI_TRACER_VERSION
271b6681 365 * Returns the LTTng kernel tracer version
606828e4 366 * LTTNG_KERNEL_ABI_TRACEPOINT_LIST
271b6681 367 * Returns a file descriptor listing available tracepoints
606828e4 368 * LTTNG_KERNEL_ABI_WAIT_QUIESCENT
360f38ea 369 * Returns after all previously running probes have completed
606828e4 370 * LTTNG_KERNEL_ABI_TRACER_ABI_VERSION
42cabb80 371 * Returns the LTTng kernel tracer ABI version
606828e4 372 * LTTNG_KERNEL_ABI_EVENT_NOTIFIER_GROUP_CREATE
750b05f2 373 * Returns a LTTng event notifier group file descriptor
ad1c05e1
MD
374 *
375 * The returned session will be deleted when its file descriptor is closed.
376 */
377static
c0e31d2e 378long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
ad1c05e1
MD
379{
380 switch (cmd) {
606828e4
MD
381 case LTTNG_KERNEL_ABI_OLD_SESSION:
382 case LTTNG_KERNEL_ABI_SESSION:
ad1c05e1 383 return lttng_abi_create_session();
606828e4 384 case LTTNG_KERNEL_ABI_EVENT_NOTIFIER_GROUP_CREATE:
750b05f2 385 return lttng_abi_create_event_notifier_group();
606828e4 386 case LTTNG_KERNEL_ABI_OLD_TRACER_VERSION:
6dccd6c1 387 {
606828e4
MD
388 struct lttng_kernel_abi_tracer_version v;
389 struct lttng_kernel_abi_old_tracer_version oldv;
390 struct lttng_kernel_abi_old_tracer_version *uversion =
391 (struct lttng_kernel_abi_old_tracer_version __user *) arg;
6dccd6c1
JD
392
393 lttng_abi_tracer_version(&v);
394 oldv.major = v.major;
395 oldv.minor = v.minor;
396 oldv.patchlevel = v.patchlevel;
397
398 if (copy_to_user(uversion, &oldv, sizeof(oldv)))
399 return -EFAULT;
400 return 0;
401 }
606828e4 402 case LTTNG_KERNEL_ABI_TRACER_VERSION:
6dccd6c1 403 {
606828e4
MD
404 struct lttng_kernel_abi_tracer_version version;
405 struct lttng_kernel_abi_tracer_version *uversion =
406 (struct lttng_kernel_abi_tracer_version __user *) arg;
6dccd6c1
JD
407
408 lttng_abi_tracer_version(&version);
42cabb80
MD
409
410 if (copy_to_user(uversion, &version, sizeof(version)))
411 return -EFAULT;
412 return 0;
413 }
606828e4 414 case LTTNG_KERNEL_ABI_TRACER_ABI_VERSION:
42cabb80 415 {
606828e4
MD
416 struct lttng_kernel_abi_tracer_abi_version version;
417 struct lttng_kernel_abi_tracer_abi_version *uversion =
418 (struct lttng_kernel_abi_tracer_abi_version __user *) arg;
42cabb80
MD
419
420 lttng_abi_tracer_abi_version(&version);
421
6dccd6c1
JD
422 if (copy_to_user(uversion, &version, sizeof(version)))
423 return -EFAULT;
424 return 0;
425 }
606828e4
MD
426 case LTTNG_KERNEL_ABI_OLD_TRACEPOINT_LIST:
427 case LTTNG_KERNEL_ABI_TRACEPOINT_LIST:
271b6681 428 return lttng_abi_tracepoint_list();
606828e4 429 case LTTNG_KERNEL_ABI_SYSCALL_LIST:
2d2464bd 430 return lttng_abi_syscall_list();
606828e4
MD
431 case LTTNG_KERNEL_ABI_OLD_WAIT_QUIESCENT:
432 case LTTNG_KERNEL_ABI_WAIT_QUIESCENT:
5f7f9078
MD
433 synchronize_trace();
434 return 0;
606828e4 435 case LTTNG_KERNEL_ABI_OLD_CALIBRATE:
6dccd6c1 436 {
606828e4
MD
437 struct lttng_kernel_abi_old_calibrate __user *ucalibrate =
438 (struct lttng_kernel_abi_old_calibrate __user *) arg;
439 struct lttng_kernel_abi_old_calibrate old_calibrate;
440 struct lttng_kernel_abi_calibrate calibrate;
6dccd6c1
JD
441 int ret;
442
443 if (copy_from_user(&old_calibrate, ucalibrate, sizeof(old_calibrate)))
444 return -EFAULT;
445 calibrate.type = old_calibrate.type;
446 ret = lttng_calibrate(&calibrate);
447 if (copy_to_user(ucalibrate, &old_calibrate, sizeof(old_calibrate)))
448 return -EFAULT;
449 return ret;
450 }
606828e4 451 case LTTNG_KERNEL_ABI_CALIBRATE:
57105fc2 452 {
606828e4
MD
453 struct lttng_kernel_abi_calibrate __user *ucalibrate =
454 (struct lttng_kernel_abi_calibrate __user *) arg;
455 struct lttng_kernel_abi_calibrate calibrate;
57105fc2
MD
456 int ret;
457
458 if (copy_from_user(&calibrate, ucalibrate, sizeof(calibrate)))
459 return -EFAULT;
460 ret = lttng_calibrate(&calibrate);
461 if (copy_to_user(ucalibrate, &calibrate, sizeof(calibrate)))
462 return -EFAULT;
463 return ret;
464 }
ad1c05e1
MD
465 default:
466 return -ENOIOCTLCMD;
467 }
468}
469
5f4c791e 470#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,6,0))
059de147
MJ
471static const struct proc_ops lttng_proc_ops = {
472 .proc_ioctl = lttng_ioctl,
473#ifdef CONFIG_COMPAT
474 .proc_compat_ioctl = lttng_ioctl,
475#endif /* CONFIG_COMPAT */
476};
477#else
478static const struct file_operations lttng_proc_ops = {
a33c9927 479 .owner = THIS_MODULE,
ad1c05e1
MD
480 .unlocked_ioctl = lttng_ioctl,
481#ifdef CONFIG_COMPAT
03037b98 482 .compat_ioctl = lttng_ioctl,
059de147 483#endif /* CONFIG_COMPAT */
11b5a3c2 484};
059de147 485#endif
ad1c05e1 486
5dbbdb43 487static
c0e31d2e 488int lttng_abi_create_channel(struct file *session_file,
606828e4 489 struct lttng_kernel_abi_channel *chan_param,
5dbbdb43 490 enum channel_type channel_type)
baf20995 491{
8cdc1a81 492 struct lttng_kernel_session *session = session_file->private_data;
88dfd899 493 const struct file_operations *fops = NULL;
5dbbdb43 494 const char *transport_name;
f7d06400 495 struct lttng_kernel_channel_buffer *chan;
c0e31d2e 496 struct file *chan_file;
baf20995 497 int chan_fd;
ad1c05e1 498 int ret = 0;
baf20995 499
4ac10b76 500 chan_fd = lttng_get_unused_fd();
baf20995
MD
501 if (chan_fd < 0) {
502 ret = chan_fd;
503 goto fd_error;
504 }
88dfd899
MD
505 switch (channel_type) {
506 case PER_CPU_CHANNEL:
507 fops = &lttng_channel_fops;
508 break;
509 case METADATA_CHANNEL:
510 fops = &lttng_metadata_fops;
511 break;
512 }
2470a237 513
c0e31d2e 514 chan_file = anon_inode_getfile("[lttng_channel]",
88dfd899 515 fops,
03037b98 516 NULL, O_RDWR);
c0e31d2e
MD
517 if (IS_ERR(chan_file)) {
518 ret = PTR_ERR(chan_file);
baf20995
MD
519 goto file_error;
520 }
5dbbdb43
MD
521 switch (channel_type) {
522 case PER_CPU_CHANNEL:
606828e4 523 if (chan_param->output == LTTNG_KERNEL_ABI_SPLICE) {
6dccd6c1 524 transport_name = chan_param->overwrite ?
96ba7208 525 "relay-overwrite" : "relay-discard";
606828e4 526 } else if (chan_param->output == LTTNG_KERNEL_ABI_MMAP) {
6dccd6c1 527 transport_name = chan_param->overwrite ?
96ba7208
JD
528 "relay-overwrite-mmap" : "relay-discard-mmap";
529 } else {
530 return -EINVAL;
531 }
5dbbdb43 532 break;
5dbbdb43 533 case METADATA_CHANNEL:
606828e4 534 if (chan_param->output == LTTNG_KERNEL_ABI_SPLICE)
96ba7208 535 transport_name = "relay-metadata";
606828e4 536 else if (chan_param->output == LTTNG_KERNEL_ABI_MMAP)
96ba7208
JD
537 transport_name = "relay-metadata-mmap";
538 else
539 return -EINVAL;
5dbbdb43
MD
540 break;
541 default:
542 transport_name = "<unknown>";
543 break;
544 }
98d7281c
MJ
545 if (!atomic_long_add_unless(&session_file->f_count, 1, LONG_MAX)) {
546 ret = -EOVERFLOW;
9c1f4643
MD
547 goto refcount_error;
548 }
03037b98
MD
549 /*
550 * We tolerate no failure path after channel creation. It will stay
551 * invariant for the rest of the session.
552 */
4e252d1e 553 chan = lttng_channel_buffer_create(session, transport_name, NULL,
6dccd6c1
JD
554 chan_param->subbuf_size,
555 chan_param->num_subbuf,
556 chan_param->switch_timer_interval,
d83004aa
JD
557 chan_param->read_timer_interval,
558 channel_type);
03037b98 559 if (!chan) {
f3d01b96 560 ret = -EINVAL;
03037b98
MD
561 goto chan_error;
562 }
f7d06400 563 chan->priv->parent.file = chan_file;
c0e31d2e
MD
564 chan_file->private_data = chan;
565 fd_install(chan_fd, chan_file);
ad1c05e1 566
baf20995
MD
567 return chan_fd;
568
03037b98 569chan_error:
9c1f4643
MD
570 atomic_long_dec(&session_file->f_count);
571refcount_error:
c0e31d2e 572 fput(chan_file);
baf20995
MD
573file_error:
574 put_unused_fd(chan_fd);
575fd_error:
baf20995
MD
576 return ret;
577}
578
7f859fbf 579static
8cdc1a81 580int lttng_abi_session_set_name(struct lttng_kernel_session *session,
606828e4 581 struct lttng_kernel_abi_session_name *name)
7f859fbf
JR
582{
583 size_t len;
584
606828e4 585 len = strnlen(name->name, LTTNG_KERNEL_ABI_SESSION_NAME_LEN);
7f859fbf 586
606828e4 587 if (len == LTTNG_KERNEL_ABI_SESSION_NAME_LEN) {
7f859fbf
JR
588 /* Name is too long/malformed */
589 return -EINVAL;
590 }
591
8cdc1a81 592 strcpy(session->priv->name, name->name);
7f859fbf
JR
593 return 0;
594}
595
1c88f269 596static
8cdc1a81 597int lttng_abi_session_set_creation_time(struct lttng_kernel_session *session,
606828e4 598 struct lttng_kernel_abi_session_creation_time *time)
1c88f269
JR
599{
600 size_t len;
601
606828e4 602 len = strnlen(time->iso8601, LTTNG_KERNEL_ABI_SESSION_CREATION_TIME_ISO8601_LEN);
1c88f269 603
606828e4 604 if (len == LTTNG_KERNEL_ABI_SESSION_CREATION_TIME_ISO8601_LEN) {
1c88f269
JR
605 /* Time is too long/malformed */
606 return -EINVAL;
607 }
608
8cdc1a81 609 strcpy(session->priv->creation_time, time->iso8601);
1c88f269
JR
610 return 0;
611}
612
99f52fcc
FD
613static
614int lttng_counter_release(struct inode *inode, struct file *file)
615{
616 struct lttng_counter *counter = file->private_data;
617
618 if (counter) {
619 /*
620 * Do not destroy the counter itself. Wait of the owner
621 * (event_notifier group) to be destroyed.
622 */
623 fput(counter->owner);
624 }
625
626 return 0;
627}
628
629static
630long lttng_counter_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
631{
632 struct lttng_counter *counter = file->private_data;
606828e4 633 size_t indexes[LTTNG_KERNEL_ABI_COUNTER_DIMENSION_MAX] = { 0 };
99f52fcc
FD
634 int i;
635
636 switch (cmd) {
606828e4 637 case LTTNG_KERNEL_ABI_COUNTER_READ:
99f52fcc 638 {
606828e4
MD
639 struct lttng_kernel_abi_counter_read local_counter_read;
640 struct lttng_kernel_abi_counter_read __user *ucounter_read =
641 (struct lttng_kernel_abi_counter_read __user *) arg;
99f52fcc
FD
642 bool overflow, underflow;
643 int64_t value;
644 int32_t cpu;
645 int ret;
646
647 if (copy_from_user(&local_counter_read, ucounter_read,
648 sizeof(local_counter_read)))
649 return -EFAULT;
650 if (validate_zeroed_padding(local_counter_read.padding,
651 sizeof(local_counter_read.padding)))
652 return -EINVAL;
20b5f0d8
MD
653 if (local_counter_read.index.number_dimensions > LTTNG_KERNEL_ABI_COUNTER_DIMENSION_MAX)
654 return -EINVAL;
99f52fcc
FD
655
656 /* Cast all indexes into size_t. */
657 for (i = 0; i < local_counter_read.index.number_dimensions; i++)
658 indexes[i] = (size_t) local_counter_read.index.dimension_indexes[i];
659 cpu = local_counter_read.cpu;
660
661 ret = lttng_kernel_counter_read(counter, indexes, cpu, &value,
662 &overflow, &underflow);
663 if (ret)
664 return ret;
665 local_counter_read.value.value = value;
666 local_counter_read.value.overflow = overflow;
667 local_counter_read.value.underflow = underflow;
668
669 if (copy_to_user(&ucounter_read->value, &local_counter_read.value,
670 sizeof(local_counter_read.value)))
671 return -EFAULT;
672
673 return 0;
674 }
606828e4 675 case LTTNG_KERNEL_ABI_COUNTER_AGGREGATE:
99f52fcc 676 {
606828e4
MD
677 struct lttng_kernel_abi_counter_aggregate local_counter_aggregate;
678 struct lttng_kernel_abi_counter_aggregate __user *ucounter_aggregate =
679 (struct lttng_kernel_abi_counter_aggregate __user *) arg;
99f52fcc
FD
680 bool overflow, underflow;
681 int64_t value;
682 int ret;
683
684 if (copy_from_user(&local_counter_aggregate, ucounter_aggregate,
685 sizeof(local_counter_aggregate)))
686 return -EFAULT;
687 if (validate_zeroed_padding(local_counter_aggregate.padding,
688 sizeof(local_counter_aggregate.padding)))
689 return -EINVAL;
20b5f0d8
MD
690 if (local_counter_aggregate.index.number_dimensions > LTTNG_KERNEL_ABI_COUNTER_DIMENSION_MAX)
691 return -EINVAL;
99f52fcc
FD
692
693 /* Cast all indexes into size_t. */
694 for (i = 0; i < local_counter_aggregate.index.number_dimensions; i++)
695 indexes[i] = (size_t) local_counter_aggregate.index.dimension_indexes[i];
696
697 ret = lttng_kernel_counter_aggregate(counter, indexes, &value,
698 &overflow, &underflow);
699 if (ret)
700 return ret;
701 local_counter_aggregate.value.value = value;
702 local_counter_aggregate.value.overflow = overflow;
703 local_counter_aggregate.value.underflow = underflow;
704
705 if (copy_to_user(&ucounter_aggregate->value, &local_counter_aggregate.value,
706 sizeof(local_counter_aggregate.value)))
707 return -EFAULT;
708
709 return 0;
710 }
606828e4 711 case LTTNG_KERNEL_ABI_COUNTER_CLEAR:
99f52fcc 712 {
606828e4
MD
713 struct lttng_kernel_abi_counter_clear local_counter_clear;
714 struct lttng_kernel_abi_counter_clear __user *ucounter_clear =
715 (struct lttng_kernel_abi_counter_clear __user *) arg;
99f52fcc
FD
716
717 if (copy_from_user(&local_counter_clear, ucounter_clear,
718 sizeof(local_counter_clear)))
719 return -EFAULT;
720 if (validate_zeroed_padding(local_counter_clear.padding,
721 sizeof(local_counter_clear.padding)))
722 return -EINVAL;
20b5f0d8
MD
723 if (local_counter_clear.index.number_dimensions > LTTNG_KERNEL_ABI_COUNTER_DIMENSION_MAX)
724 return -EINVAL;
99f52fcc
FD
725
726 /* Cast all indexes into size_t. */
727 for (i = 0; i < local_counter_clear.index.number_dimensions; i++)
728 indexes[i] = (size_t) local_counter_clear.index.dimension_indexes[i];
729
730 return lttng_kernel_counter_clear(counter, indexes);
731 }
732 default:
733 WARN_ON_ONCE(1);
734 return -ENOSYS;
735 }
736}
737
738static const struct file_operations lttng_counter_fops = {
739 .owner = THIS_MODULE,
740 .release = lttng_counter_release,
741 .unlocked_ioctl = lttng_counter_ioctl,
742#ifdef CONFIG_COMPAT
743 .compat_ioctl = lttng_counter_ioctl,
744#endif
745};
746
747
d1f652f8 748static
606828e4 749enum tracker_type get_tracker_type(struct lttng_kernel_abi_tracker_args *tracker)
d1f652f8
MD
750{
751 switch (tracker->type) {
606828e4 752 case LTTNG_KERNEL_ABI_TRACKER_PID:
d1f652f8 753 return TRACKER_PID;
606828e4 754 case LTTNG_KERNEL_ABI_TRACKER_VPID:
d1f652f8 755 return TRACKER_VPID;
606828e4 756 case LTTNG_KERNEL_ABI_TRACKER_UID:
d1f652f8 757 return TRACKER_UID;
606828e4 758 case LTTNG_KERNEL_ABI_TRACKER_VUID:
d1f652f8 759 return TRACKER_VUID;
606828e4 760 case LTTNG_KERNEL_ABI_TRACKER_GID:
d1f652f8 761 return TRACKER_GID;
606828e4 762 case LTTNG_KERNEL_ABI_TRACKER_VGID:
d1f652f8
MD
763 return TRACKER_VGID;
764 default:
765 return TRACKER_UNKNOWN;
766 }
767}
768
baf20995 769/**
ad1c05e1 770 * lttng_session_ioctl - lttng session fd ioctl
baf20995 771 *
c0e31d2e 772 * @file: the file
baf20995
MD
773 * @cmd: the command
774 * @arg: command arg
775 *
776 * This ioctl implements lttng commands:
606828e4 777 * LTTNG_KERNEL_ABI_CHANNEL
baf20995 778 * Returns a LTTng channel file descriptor
606828e4 779 * LTTNG_KERNEL_ABI_ENABLE
e64957da 780 * Enables tracing for a session (weak enable)
606828e4 781 * LTTNG_KERNEL_ABI_DISABLE
e64957da 782 * Disables tracing for a session (strong disable)
606828e4 783 * LTTNG_KERNEL_ABI_METADATA
8070f5c0 784 * Returns a LTTng metadata file descriptor
606828e4 785 * LTTNG_KERNEL_ABI_SESSION_TRACK_PID
d1f652f8 786 * Add PID to session PID tracker
606828e4 787 * LTTNG_KERNEL_ABI_SESSION_UNTRACK_PID
d1f652f8 788 * Remove PID from session PID tracker
606828e4 789 * LTTNG_KERNEL_ABI_SESSION_TRACK_ID
d1f652f8 790 * Add ID to tracker
606828e4 791 * LTTNG_KERNEL_ABI_SESSION_UNTRACK_ID
d1f652f8 792 * Remove ID from tracker
ad1c05e1
MD
793 *
794 * The returned channel will be deleted when its file descriptor is closed.
795 */
796static
c0e31d2e 797long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
ad1c05e1 798{
8cdc1a81 799 struct lttng_kernel_session *session = file->private_data;
606828e4
MD
800 struct lttng_kernel_abi_channel chan_param;
801 struct lttng_kernel_abi_old_channel old_chan_param;
c0e31d2e 802
8c71721f
MD
803 /*
804 * Handle backward compatibility. OLD commands have wrong
805 * directions, replace them by the correct direction.
806 */
807 switch (cmd) {
808 case LTTNG_KERNEL_ABI_OLD_SESSION_TRACK_PID:
809 cmd = LTTNG_KERNEL_ABI_SESSION_TRACK_PID;
810 break;
811 case LTTNG_KERNEL_ABI_OLD_SESSION_UNTRACK_PID:
812 cmd = LTTNG_KERNEL_ABI_SESSION_UNTRACK_PID;
813 break;
814 case LTTNG_KERNEL_ABI_OLD_SESSION_TRACK_ID:
815 cmd = LTTNG_KERNEL_ABI_SESSION_TRACK_ID;
816 break;
817 case LTTNG_KERNEL_ABI_OLD_SESSION_UNTRACK_ID:
818 cmd = LTTNG_KERNEL_ABI_SESSION_UNTRACK_ID;
819 break;
820 case LTTNG_KERNEL_ABI_OLD_SESSION_LIST_TRACKER_IDS:
821 cmd = LTTNG_KERNEL_ABI_SESSION_LIST_TRACKER_IDS;
822 break;
823 case LTTNG_KERNEL_ABI_OLD_SESSION_SET_NAME:
824 cmd = LTTNG_KERNEL_ABI_SESSION_SET_NAME;
825 break;
826 case LTTNG_KERNEL_ABI_OLD_SESSION_SET_CREATION_TIME:
827 cmd = LTTNG_KERNEL_ABI_SESSION_SET_CREATION_TIME;
828 break;
829 default:
830 /* Nothing to do. */
831 break;
832 }
833
ad1c05e1 834 switch (cmd) {
606828e4 835 case LTTNG_KERNEL_ABI_OLD_CHANNEL:
6dccd6c1 836 {
6dccd6c1 837 if (copy_from_user(&old_chan_param,
606828e4
MD
838 (struct lttng_kernel_abi_old_channel __user *) arg,
839 sizeof(struct lttng_kernel_abi_old_channel)))
6dccd6c1
JD
840 return -EFAULT;
841 chan_param.overwrite = old_chan_param.overwrite;
842 chan_param.subbuf_size = old_chan_param.subbuf_size;
843 chan_param.num_subbuf = old_chan_param.num_subbuf;
844 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
845 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
846 chan_param.output = old_chan_param.output;
847
848 return lttng_abi_create_channel(file, &chan_param,
849 PER_CPU_CHANNEL);
850 }
606828e4 851 case LTTNG_KERNEL_ABI_CHANNEL:
6dccd6c1 852 {
6dccd6c1 853 if (copy_from_user(&chan_param,
606828e4
MD
854 (struct lttng_kernel_abi_channel __user *) arg,
855 sizeof(struct lttng_kernel_abi_channel)))
6dccd6c1
JD
856 return -EFAULT;
857 return lttng_abi_create_channel(file, &chan_param,
5dbbdb43 858 PER_CPU_CHANNEL);
6dccd6c1 859 }
606828e4
MD
860 case LTTNG_KERNEL_ABI_OLD_SESSION_START:
861 case LTTNG_KERNEL_ABI_OLD_ENABLE:
862 case LTTNG_KERNEL_ABI_SESSION_START:
863 case LTTNG_KERNEL_ABI_ENABLE:
a90917c3 864 return lttng_session_enable(session);
606828e4
MD
865 case LTTNG_KERNEL_ABI_OLD_SESSION_STOP:
866 case LTTNG_KERNEL_ABI_OLD_DISABLE:
867 case LTTNG_KERNEL_ABI_SESSION_STOP:
868 case LTTNG_KERNEL_ABI_DISABLE:
a90917c3 869 return lttng_session_disable(session);
606828e4 870 case LTTNG_KERNEL_ABI_OLD_METADATA:
6dccd6c1 871 {
6dccd6c1 872 if (copy_from_user(&old_chan_param,
606828e4
MD
873 (struct lttng_kernel_abi_old_channel __user *) arg,
874 sizeof(struct lttng_kernel_abi_old_channel)))
6dccd6c1
JD
875 return -EFAULT;
876 chan_param.overwrite = old_chan_param.overwrite;
877 chan_param.subbuf_size = old_chan_param.subbuf_size;
878 chan_param.num_subbuf = old_chan_param.num_subbuf;
879 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
880 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
881 chan_param.output = old_chan_param.output;
882
883 return lttng_abi_create_channel(file, &chan_param,
884 METADATA_CHANNEL);
885 }
606828e4 886 case LTTNG_KERNEL_ABI_METADATA:
6dccd6c1 887 {
6dccd6c1 888 if (copy_from_user(&chan_param,
606828e4
MD
889 (struct lttng_kernel_abi_channel __user *) arg,
890 sizeof(struct lttng_kernel_abi_channel)))
6dccd6c1
JD
891 return -EFAULT;
892 return lttng_abi_create_channel(file, &chan_param,
5dbbdb43 893 METADATA_CHANNEL);
6dccd6c1 894 }
606828e4 895 case LTTNG_KERNEL_ABI_SESSION_TRACK_PID:
d1f652f8 896 return lttng_session_track_id(session, TRACKER_PID, (int) arg);
606828e4 897 case LTTNG_KERNEL_ABI_SESSION_UNTRACK_PID:
d1f652f8 898 return lttng_session_untrack_id(session, TRACKER_PID, (int) arg);
606828e4 899 case LTTNG_KERNEL_ABI_SESSION_TRACK_ID:
d1f652f8 900 {
606828e4 901 struct lttng_kernel_abi_tracker_args tracker;
d1f652f8
MD
902 enum tracker_type tracker_type;
903
904 if (copy_from_user(&tracker,
606828e4
MD
905 (struct lttng_kernel_abi_tracker_args __user *) arg,
906 sizeof(struct lttng_kernel_abi_tracker_args)))
d1f652f8
MD
907 return -EFAULT;
908 tracker_type = get_tracker_type(&tracker);
909 if (tracker_type == TRACKER_UNKNOWN)
910 return -EINVAL;
911 return lttng_session_track_id(session, tracker_type, tracker.id);
912 }
606828e4 913 case LTTNG_KERNEL_ABI_SESSION_UNTRACK_ID:
d1f652f8 914 {
606828e4 915 struct lttng_kernel_abi_tracker_args tracker;
d1f652f8
MD
916 enum tracker_type tracker_type;
917
918 if (copy_from_user(&tracker,
606828e4
MD
919 (struct lttng_kernel_abi_tracker_args __user *) arg,
920 sizeof(struct lttng_kernel_abi_tracker_args)))
d1f652f8
MD
921 return -EFAULT;
922 tracker_type = get_tracker_type(&tracker);
923 if (tracker_type == TRACKER_UNKNOWN)
924 return -EINVAL;
925 return lttng_session_untrack_id(session, tracker_type,
926 tracker.id);
927 }
606828e4 928 case LTTNG_KERNEL_ABI_SESSION_LIST_TRACKER_PIDS:
d1f652f8 929 return lttng_session_list_tracker_ids(session, TRACKER_PID);
606828e4 930 case LTTNG_KERNEL_ABI_SESSION_LIST_TRACKER_IDS:
d1f652f8 931 {
606828e4 932 struct lttng_kernel_abi_tracker_args tracker;
d1f652f8
MD
933 enum tracker_type tracker_type;
934
935 if (copy_from_user(&tracker,
606828e4
MD
936 (struct lttng_kernel_abi_tracker_args __user *) arg,
937 sizeof(struct lttng_kernel_abi_tracker_args)))
d1f652f8
MD
938 return -EFAULT;
939 tracker_type = get_tracker_type(&tracker);
940 if (tracker_type == TRACKER_UNKNOWN)
941 return -EINVAL;
942 return lttng_session_list_tracker_ids(session, tracker_type);
943 }
606828e4 944 case LTTNG_KERNEL_ABI_SESSION_METADATA_REGEN:
9616f0bf 945 return lttng_session_metadata_regenerate(session);
606828e4 946 case LTTNG_KERNEL_ABI_SESSION_STATEDUMP:
601252cf 947 return lttng_session_statedump(session);
606828e4 948 case LTTNG_KERNEL_ABI_SESSION_SET_NAME:
7f859fbf 949 {
606828e4 950 struct lttng_kernel_abi_session_name name;
7f859fbf
JR
951
952 if (copy_from_user(&name,
606828e4
MD
953 (struct lttng_kernel_abi_session_name __user *) arg,
954 sizeof(struct lttng_kernel_abi_session_name)))
7f859fbf
JR
955 return -EFAULT;
956 return lttng_abi_session_set_name(session, &name);
957 }
606828e4 958 case LTTNG_KERNEL_ABI_SESSION_SET_CREATION_TIME:
1c88f269 959 {
606828e4 960 struct lttng_kernel_abi_session_creation_time time;
1c88f269
JR
961
962 if (copy_from_user(&time,
606828e4
MD
963 (struct lttng_kernel_abi_session_creation_time __user *) arg,
964 sizeof(struct lttng_kernel_abi_session_creation_time)))
1c88f269
JR
965 return -EFAULT;
966 return lttng_abi_session_set_creation_time(session, &time);
967 }
ad1c05e1
MD
968 default:
969 return -ENOIOCTLCMD;
970 }
971}
972
03037b98
MD
973/*
974 * Called when the last file reference is dropped.
975 *
976 * Big fat note: channels and events are invariant for the whole session after
977 * their creation. So this session destruction also destroys all channel and
978 * event structures specific to this session (they are not destroyed when their
979 * individual file is released).
980 */
ad1c05e1 981static
03037b98 982int lttng_session_release(struct inode *inode, struct file *file)
ad1c05e1 983{
8cdc1a81 984 struct lttng_kernel_session *session = file->private_data;
c269fff4
MD
985
986 if (session)
a90917c3 987 lttng_session_destroy(session);
11b5a3c2 988 return 0;
ad1c05e1 989}
ad1c05e1
MD
990
991static const struct file_operations lttng_session_fops = {
a33c9927 992 .owner = THIS_MODULE,
03037b98 993 .release = lttng_session_release,
ad1c05e1
MD
994 .unlocked_ioctl = lttng_session_ioctl,
995#ifdef CONFIG_COMPAT
03037b98 996 .compat_ioctl = lttng_session_ioctl,
ad1c05e1 997#endif
11b5a3c2 998};
ad1c05e1 999
db2511b4
MD
1000/*
1001 * When encountering empty buffer, flush current sub-buffer if non-empty
1002 * and retry (if new data available to read after flush).
1003 */
1004static
1005ssize_t lttng_event_notifier_group_notif_read(struct file *filp, char __user *user_buf,
1006 size_t count, loff_t *ppos)
1007{
1008 struct lttng_event_notifier_group *event_notifier_group = filp->private_data;
860c213b 1009 struct lttng_kernel_ring_buffer_channel *chan = event_notifier_group->chan;
e20c0fec 1010 struct lttng_kernel_ring_buffer *buf = event_notifier_group->buf;
db2511b4
MD
1011 ssize_t read_count = 0, len;
1012 size_t read_offset;
1013
1014 might_sleep();
1015 if (!lttng_access_ok(VERIFY_WRITE, user_buf, count))
1016 return -EFAULT;
1017
1018 /* Finish copy of previous record */
1019 if (*ppos != 0) {
1020 if (read_count < count) {
1021 len = chan->iter.len_left;
1022 read_offset = *ppos;
1023 goto skip_get_next;
1024 }
1025 }
1026
1027 while (read_count < count) {
1028 size_t copy_len, space_left;
1029
1030 len = lib_ring_buffer_get_next_record(chan, buf);
1031len_test:
1032 if (len < 0) {
1033 /*
1034 * Check if buffer is finalized (end of file).
1035 */
1036 if (len == -ENODATA) {
1037 /* A 0 read_count will tell about end of file */
1038 goto nodata;
1039 }
1040 if (filp->f_flags & O_NONBLOCK) {
1041 if (!read_count)
1042 read_count = -EAGAIN;
1043 goto nodata;
1044 } else {
1045 int error;
1046
1047 /*
1048 * No data available at the moment, return what
1049 * we got.
1050 */
1051 if (read_count)
1052 goto nodata;
1053
1054 /*
1055 * Wait for returned len to be >= 0 or -ENODATA.
1056 */
1057 error = wait_event_interruptible(
1058 event_notifier_group->read_wait,
1059 ((len = lib_ring_buffer_get_next_record(
1060 chan, buf)), len != -EAGAIN));
1061 CHAN_WARN_ON(chan, len == -EBUSY);
1062 if (error) {
1063 read_count = error;
1064 goto nodata;
1065 }
1066 CHAN_WARN_ON(chan, len < 0 && len != -ENODATA);
1067 goto len_test;
1068 }
1069 }
1070 read_offset = buf->iter.read_offset;
1071skip_get_next:
1072 space_left = count - read_count;
1073 if (len <= space_left) {
1074 copy_len = len;
1075 chan->iter.len_left = 0;
1076 *ppos = 0;
1077 } else {
1078 copy_len = space_left;
1079 chan->iter.len_left = len - copy_len;
1080 *ppos = read_offset + copy_len;
1081 }
1082 if (__lib_ring_buffer_copy_to_user(&buf->backend, read_offset,
1083 &user_buf[read_count],
1084 copy_len)) {
1085 /*
1086 * Leave the len_left and ppos values at their current
1087 * state, as we currently have a valid event to read.
1088 */
1089 return -EFAULT;
1090 }
1091 read_count += copy_len;
1092 }
31c02fb7 1093 goto put_record;
db2511b4
MD
1094
1095nodata:
1096 *ppos = 0;
1097 chan->iter.len_left = 0;
31c02fb7
MD
1098
1099put_record:
1100 lib_ring_buffer_put_current_record(buf);
db2511b4
MD
1101 return read_count;
1102}
1103
1104/*
1105 * If the ring buffer is non empty (even just a partial subbuffer), return that
1106 * there is data available. Perform a ring buffer flush if we encounter a
1107 * non-empty ring buffer which does not have any consumeable subbuffer available.
1108 */
1109static
1110unsigned int lttng_event_notifier_group_notif_poll(struct file *filp,
1111 poll_table *wait)
1112{
1113 unsigned int mask = 0;
1114 struct lttng_event_notifier_group *event_notifier_group = filp->private_data;
860c213b 1115 struct lttng_kernel_ring_buffer_channel *chan = event_notifier_group->chan;
e20c0fec
MD
1116 struct lttng_kernel_ring_buffer *buf = event_notifier_group->buf;
1117 const struct lttng_kernel_ring_buffer_config *config = &chan->backend.config;
db2511b4
MD
1118 int finalized, disabled;
1119 unsigned long consumed, offset;
18f12d55 1120 size_t subbuffer_header_size = config->cb.subbuffer_header_size();
db2511b4
MD
1121
1122 if (filp->f_mode & FMODE_READ) {
1123 poll_wait_set_exclusive(wait);
1124 poll_wait(filp, &event_notifier_group->read_wait, wait);
1125
1126 finalized = lib_ring_buffer_is_finalized(config, buf);
1127 disabled = lib_ring_buffer_channel_is_disabled(chan);
1128
1129 /*
1130 * lib_ring_buffer_is_finalized() contains a smp_rmb() ordering
1131 * finalized load before offsets loads.
1132 */
1133 WARN_ON(atomic_long_read(&buf->active_readers) != 1);
1134retry:
1135 if (disabled)
1136 return POLLERR;
1137
1138 offset = lib_ring_buffer_get_offset(config, buf);
1139 consumed = lib_ring_buffer_get_consumed(config, buf);
1140
1141 /*
1142 * If there is no buffer available to consume.
1143 */
1144 if (subbuf_trunc(offset, chan) - subbuf_trunc(consumed, chan) == 0) {
1145 /*
1146 * If there is a non-empty subbuffer, flush and try again.
1147 */
18f12d55 1148 if (subbuf_offset(offset, chan) > subbuffer_header_size) {
db2511b4
MD
1149 lib_ring_buffer_switch_remote(buf);
1150 goto retry;
1151 }
1152
1153 if (finalized)
1154 return POLLHUP;
1155 else {
1156 /*
1157 * The memory barriers
1158 * __wait_event()/wake_up_interruptible() take
1159 * care of "raw_spin_is_locked" memory ordering.
1160 */
1161 if (raw_spin_is_locked(&buf->raw_tick_nohz_spinlock))
1162 goto retry;
1163 else
1164 return 0;
1165 }
1166 } else {
1167 if (subbuf_trunc(offset, chan) - subbuf_trunc(consumed, chan)
1168 >= chan->backend.buf_size)
1169 return POLLPRI | POLLRDBAND;
1170 else
1171 return POLLIN | POLLRDNORM;
1172 }
1173 }
1174
1175 return mask;
1176}
1177
1178/**
1179 * lttng_event_notifier_group_notif_open - event_notifier ring buffer open file operation
1180 * @inode: opened inode
1181 * @file: opened file
1182 *
1183 * Open implementation. Makes sure only one open instance of a buffer is
1184 * done at a given moment.
1185 */
1186static int lttng_event_notifier_group_notif_open(struct inode *inode, struct file *file)
1187{
1188 struct lttng_event_notifier_group *event_notifier_group = inode->i_private;
e20c0fec 1189 struct lttng_kernel_ring_buffer *buf = event_notifier_group->buf;
db2511b4
MD
1190
1191 file->private_data = event_notifier_group;
1192 return lib_ring_buffer_open(inode, file, buf);
1193}
1194
1195/**
1196 * lttng_event_notifier_group_notif_release - event_notifier ring buffer release file operation
1197 * @inode: opened inode
1198 * @file: opened file
1199 *
1200 * Release implementation.
1201 */
1202static int lttng_event_notifier_group_notif_release(struct inode *inode, struct file *file)
1203{
1204 struct lttng_event_notifier_group *event_notifier_group = file->private_data;
e20c0fec 1205 struct lttng_kernel_ring_buffer *buf = event_notifier_group->buf;
db2511b4
MD
1206 int ret;
1207
1208 ret = lib_ring_buffer_release(inode, file, buf);
1209 if (ret)
1210 return ret;
1211 fput(event_notifier_group->file);
1212 return 0;
1213}
1214
21f58fb7
FD
1215static const struct file_operations lttng_event_notifier_group_notif_fops = {
1216 .owner = THIS_MODULE,
db2511b4
MD
1217 .open = lttng_event_notifier_group_notif_open,
1218 .release = lttng_event_notifier_group_notif_release,
1219 .read = lttng_event_notifier_group_notif_read,
1220 .poll = lttng_event_notifier_group_notif_poll,
21f58fb7
FD
1221};
1222
d83004aa
JD
1223/**
1224 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
1225 * @filp: the file
1226 * @wait: poll table
1227 *
1228 * Handles the poll operations for the metadata channels.
1229 */
ad1c05e1 1230static
d83004aa
JD
1231unsigned int lttng_metadata_ring_buffer_poll(struct file *filp,
1232 poll_table *wait)
1233{
1234 struct lttng_metadata_stream *stream = filp->private_data;
e20c0fec 1235 struct lttng_kernel_ring_buffer *buf = stream->priv;
d83004aa
JD
1236 int finalized;
1237 unsigned int mask = 0;
1238
1239 if (filp->f_mode & FMODE_READ) {
1240 poll_wait_set_exclusive(wait);
1241 poll_wait(filp, &stream->read_wait, wait);
1242
1243 finalized = stream->finalized;
1244
1245 /*
1246 * lib_ring_buffer_is_finalized() contains a smp_rmb()
1247 * ordering finalized load before offsets loads.
1248 */
1249 WARN_ON(atomic_long_read(&buf->active_readers) != 1);
1250
1251 if (finalized)
1252 mask |= POLLHUP;
1253
92d9f5e6 1254 mutex_lock(&stream->metadata_cache->lock);
d83004aa 1255 if (stream->metadata_cache->metadata_written >
f613e3e6 1256 stream->metadata_out)
d83004aa 1257 mask |= POLLIN;
92d9f5e6 1258 mutex_unlock(&stream->metadata_cache->lock);
d83004aa
JD
1259 }
1260
1261 return mask;
1262}
1263
f613e3e6
MD
1264static
1265void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file *filp,
1266 unsigned int cmd, unsigned long arg)
1267{
1268 struct lttng_metadata_stream *stream = filp->private_data;
1269
1270 stream->metadata_out = stream->metadata_in;
1271}
1272
d1344afa
JD
1273/*
1274 * Reset the counter of how much metadata has been consumed to 0. That way,
1275 * the consumer receives the content of the metadata cache unchanged. This is
1276 * different from the metadata_regenerate where the offset from epoch is
1277 * resampled, here we want the exact same content as the last time the metadata
1278 * was generated. This command is only possible if all the metadata written
1279 * in the cache has been output to the metadata stream to avoid corrupting the
1280 * metadata file.
1281 *
1282 * Return 0 on success, a negative value on error.
1283 */
1284static
1285int lttng_metadata_cache_dump(struct lttng_metadata_stream *stream)
1286{
1287 int ret;
1288 struct lttng_metadata_cache *cache = stream->metadata_cache;
1289
1290 mutex_lock(&cache->lock);
1291 if (stream->metadata_out != cache->metadata_written) {
1292 ret = -EBUSY;
1293 goto end;
1294 }
1295 stream->metadata_out = 0;
1296 stream->metadata_in = 0;
1297 wake_up_interruptible(&stream->read_wait);
1298 ret = 0;
1299
1300end:
1301 mutex_unlock(&cache->lock);
1302 return ret;
1303}
1304
d83004aa
JD
1305static
1306long lttng_metadata_ring_buffer_ioctl(struct file *filp,
1307 unsigned int cmd, unsigned long arg)
1308{
1309 int ret;
1310 struct lttng_metadata_stream *stream = filp->private_data;
e20c0fec 1311 struct lttng_kernel_ring_buffer *buf = stream->priv;
8b97fd42
MD
1312 unsigned int rb_cmd;
1313 bool coherent;
1314
606828e4
MD
1315 if (cmd == LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK)
1316 rb_cmd = LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF;
8b97fd42
MD
1317 else
1318 rb_cmd = cmd;
d83004aa
JD
1319
1320 switch (cmd) {
606828e4 1321 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF:
d83004aa 1322 {
35097f36 1323 struct lttng_metadata_stream *stream = filp->private_data;
e20c0fec 1324 struct lttng_kernel_ring_buffer *buf = stream->priv;
860c213b 1325 struct lttng_kernel_ring_buffer_channel *chan = buf->backend.chan;
35097f36 1326
8b97fd42 1327 ret = lttng_metadata_output_channel(stream, chan, NULL);
35097f36
JD
1328 if (ret > 0) {
1329 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
1330 ret = 0;
1331 } else if (ret < 0)
d83004aa
JD
1332 goto err;
1333 break;
1334 }
606828e4 1335 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_SUBBUF:
f613e3e6
MD
1336 {
1337 /*
1338 * Random access is not allowed for metadata channel.
1339 */
1340 return -ENOSYS;
1341 }
c190d76e
MJ
1342 case LTTNG_KERNEL_ABI_RING_BUFFER_FLUSH_EMPTY:
1343 lttng_fallthrough;
606828e4 1344 case LTTNG_KERNEL_ABI_RING_BUFFER_FLUSH:
35097f36
JD
1345 {
1346 struct lttng_metadata_stream *stream = filp->private_data;
e20c0fec 1347 struct lttng_kernel_ring_buffer *buf = stream->priv;
860c213b 1348 struct lttng_kernel_ring_buffer_channel *chan = buf->backend.chan;
35097f36
JD
1349
1350 /*
1351 * Before doing the actual ring buffer flush, write up to one
1352 * packet of metadata in the ring buffer.
1353 */
8b97fd42 1354 ret = lttng_metadata_output_channel(stream, chan, NULL);
35097f36
JD
1355 if (ret < 0)
1356 goto err;
1357 break;
1358 }
606828e4 1359 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_METADATA_VERSION:
9616f0bf
JD
1360 {
1361 struct lttng_metadata_stream *stream = filp->private_data;
1362
1363 return put_u64(stream->version, arg);
1364 }
606828e4 1365 case LTTNG_KERNEL_ABI_RING_BUFFER_METADATA_CACHE_DUMP:
d1344afa
JD
1366 {
1367 struct lttng_metadata_stream *stream = filp->private_data;
1368
1369 return lttng_metadata_cache_dump(stream);
1370 }
606828e4 1371 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
8b97fd42
MD
1372 {
1373 struct lttng_metadata_stream *stream = filp->private_data;
e20c0fec 1374 struct lttng_kernel_ring_buffer *buf = stream->priv;
860c213b 1375 struct lttng_kernel_ring_buffer_channel *chan = buf->backend.chan;
8b97fd42
MD
1376
1377 ret = lttng_metadata_output_channel(stream, chan, &coherent);
1378 if (ret > 0) {
1379 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
1380 ret = 0;
1381 } else if (ret < 0) {
1382 goto err;
1383 }
1384 break;
1385 }
d83004aa
JD
1386 default:
1387 break;
1388 }
f613e3e6
MD
1389 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
1390
d83004aa 1391 /* Performing lib ring buffer ioctl after our own. */
8b97fd42 1392 ret = lib_ring_buffer_ioctl(filp, rb_cmd, arg, buf);
f613e3e6
MD
1393 if (ret < 0)
1394 goto err;
d83004aa 1395
f613e3e6 1396 switch (cmd) {
606828e4 1397 case LTTNG_KERNEL_ABI_RING_BUFFER_PUT_NEXT_SUBBUF:
f613e3e6
MD
1398 {
1399 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
1400 cmd, arg);
1401 break;
1402 }
606828e4 1403 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
8b97fd42
MD
1404 {
1405 return put_u32(coherent, arg);
1406 }
f613e3e6
MD
1407 default:
1408 break;
1409 }
d83004aa
JD
1410err:
1411 return ret;
1412}
1413
aeb9064d 1414#ifdef CONFIG_COMPAT
d83004aa
JD
1415static
1416long lttng_metadata_ring_buffer_compat_ioctl(struct file *filp,
1417 unsigned int cmd, unsigned long arg)
1418{
1419 int ret;
1420 struct lttng_metadata_stream *stream = filp->private_data;
e20c0fec 1421 struct lttng_kernel_ring_buffer *buf = stream->priv;
8b97fd42
MD
1422 unsigned int rb_cmd;
1423 bool coherent;
1424
606828e4
MD
1425 if (cmd == LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK)
1426 rb_cmd = LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF;
8b97fd42
MD
1427 else
1428 rb_cmd = cmd;
d83004aa
JD
1429
1430 switch (cmd) {
606828e4 1431 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF:
d83004aa 1432 {
35097f36 1433 struct lttng_metadata_stream *stream = filp->private_data;
e20c0fec 1434 struct lttng_kernel_ring_buffer *buf = stream->priv;
860c213b 1435 struct lttng_kernel_ring_buffer_channel *chan = buf->backend.chan;
35097f36 1436
8b97fd42 1437 ret = lttng_metadata_output_channel(stream, chan, NULL);
35097f36
JD
1438 if (ret > 0) {
1439 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
1440 ret = 0;
1441 } else if (ret < 0)
d83004aa
JD
1442 goto err;
1443 break;
1444 }
606828e4 1445 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_SUBBUF:
f613e3e6
MD
1446 {
1447 /*
1448 * Random access is not allowed for metadata channel.
1449 */
1450 return -ENOSYS;
1451 }
c190d76e
MJ
1452 case LTTNG_KERNEL_ABI_RING_BUFFER_FLUSH_EMPTY:
1453 lttng_fallthrough;
606828e4 1454 case LTTNG_KERNEL_ABI_RING_BUFFER_FLUSH:
96c55c2f
MD
1455 {
1456 struct lttng_metadata_stream *stream = filp->private_data;
e20c0fec 1457 struct lttng_kernel_ring_buffer *buf = stream->priv;
860c213b 1458 struct lttng_kernel_ring_buffer_channel *chan = buf->backend.chan;
96c55c2f
MD
1459
1460 /*
1461 * Before doing the actual ring buffer flush, write up to one
1462 * packet of metadata in the ring buffer.
1463 */
8b97fd42 1464 ret = lttng_metadata_output_channel(stream, chan, NULL);
96c55c2f
MD
1465 if (ret < 0)
1466 goto err;
1467 break;
1468 }
606828e4 1469 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_METADATA_VERSION:
96c55c2f
MD
1470 {
1471 struct lttng_metadata_stream *stream = filp->private_data;
1472
1473 return put_u64(stream->version, arg);
1474 }
606828e4 1475 case LTTNG_KERNEL_ABI_RING_BUFFER_METADATA_CACHE_DUMP:
d1344afa
JD
1476 {
1477 struct lttng_metadata_stream *stream = filp->private_data;
1478
1479 return lttng_metadata_cache_dump(stream);
1480 }
606828e4 1481 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
8b97fd42
MD
1482 {
1483 struct lttng_metadata_stream *stream = filp->private_data;
e20c0fec 1484 struct lttng_kernel_ring_buffer *buf = stream->priv;
860c213b 1485 struct lttng_kernel_ring_buffer_channel *chan = buf->backend.chan;
8b97fd42
MD
1486
1487 ret = lttng_metadata_output_channel(stream, chan, &coherent);
1488 if (ret > 0) {
1489 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
1490 ret = 0;
1491 } else if (ret < 0) {
1492 goto err;
1493 }
1494 break;
1495 }
d83004aa
JD
1496 default:
1497 break;
1498 }
f613e3e6
MD
1499 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
1500
d83004aa 1501 /* Performing lib ring buffer ioctl after our own. */
8b97fd42 1502 ret = lib_ring_buffer_compat_ioctl(filp, rb_cmd, arg, buf);
f613e3e6
MD
1503 if (ret < 0)
1504 goto err;
d83004aa 1505
f613e3e6 1506 switch (cmd) {
606828e4 1507 case LTTNG_KERNEL_ABI_RING_BUFFER_PUT_NEXT_SUBBUF:
f613e3e6
MD
1508 {
1509 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
1510 cmd, arg);
1511 break;
1512 }
606828e4 1513 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
8b97fd42
MD
1514 {
1515 return put_u32(coherent, arg);
1516 }
f613e3e6
MD
1517 default:
1518 break;
1519 }
d83004aa
JD
1520err:
1521 return ret;
1522}
aeb9064d 1523#endif
d83004aa 1524
b3b8072b
MD
1525/*
1526 * This is not used by anonymous file descriptors. This code is left
1527 * there if we ever want to implement an inode with open() operation.
1528 */
d83004aa
JD
1529static
1530int lttng_metadata_ring_buffer_open(struct inode *inode, struct file *file)
1531{
1532 struct lttng_metadata_stream *stream = inode->i_private;
e20c0fec 1533 struct lttng_kernel_ring_buffer *buf = stream->priv;
d83004aa
JD
1534
1535 file->private_data = buf;
b3b8072b
MD
1536 /*
1537 * Since life-time of metadata cache differs from that of
1538 * session, we need to keep our own reference on the transport.
1539 */
1540 if (!try_module_get(stream->transport->owner)) {
5a15f70c 1541 printk(KERN_WARNING "LTTng: Can't lock transport module.\n");
b3b8072b
MD
1542 return -EBUSY;
1543 }
d83004aa
JD
1544 return lib_ring_buffer_open(inode, file, buf);
1545}
1546
1547static
1548int lttng_metadata_ring_buffer_release(struct inode *inode, struct file *file)
1549{
1550 struct lttng_metadata_stream *stream = file->private_data;
e20c0fec 1551 struct lttng_kernel_ring_buffer *buf = stream->priv;
d83004aa 1552
92143b2c
MD
1553 mutex_lock(&stream->metadata_cache->lock);
1554 list_del(&stream->list);
1555 mutex_unlock(&stream->metadata_cache->lock);
d83004aa 1556 kref_put(&stream->metadata_cache->refcount, metadata_cache_destroy);
b3b8072b 1557 module_put(stream->transport->owner);
92143b2c 1558 kfree(stream);
d83004aa
JD
1559 return lib_ring_buffer_release(inode, file, buf);
1560}
1561
1562static
1563ssize_t lttng_metadata_ring_buffer_splice_read(struct file *in, loff_t *ppos,
1564 struct pipe_inode_info *pipe, size_t len,
1565 unsigned int flags)
1566{
1567 struct lttng_metadata_stream *stream = in->private_data;
e20c0fec 1568 struct lttng_kernel_ring_buffer *buf = stream->priv;
d83004aa
JD
1569
1570 return lib_ring_buffer_splice_read(in, ppos, pipe, len,
1571 flags, buf);
1572}
1573
1574static
1575int lttng_metadata_ring_buffer_mmap(struct file *filp,
1576 struct vm_area_struct *vma)
1577{
1578 struct lttng_metadata_stream *stream = filp->private_data;
e20c0fec 1579 struct lttng_kernel_ring_buffer *buf = stream->priv;
d83004aa
JD
1580
1581 return lib_ring_buffer_mmap(filp, vma, buf);
1582}
1583
1584static
1585const struct file_operations lttng_metadata_ring_buffer_file_operations = {
1586 .owner = THIS_MODULE,
1587 .open = lttng_metadata_ring_buffer_open,
1588 .release = lttng_metadata_ring_buffer_release,
1589 .poll = lttng_metadata_ring_buffer_poll,
1590 .splice_read = lttng_metadata_ring_buffer_splice_read,
1591 .mmap = lttng_metadata_ring_buffer_mmap,
1592 .unlocked_ioctl = lttng_metadata_ring_buffer_ioctl,
1593 .llseek = vfs_lib_ring_buffer_no_llseek,
1594#ifdef CONFIG_COMPAT
1595 .compat_ioctl = lttng_metadata_ring_buffer_compat_ioctl,
1596#endif
1597};
1598
1599static
1600int lttng_abi_create_stream_fd(struct file *channel_file, void *stream_priv,
a3fcd499 1601 const struct file_operations *fops, const char *name)
ad1c05e1 1602{
ad1c05e1 1603 int stream_fd, ret;
11b5a3c2 1604 struct file *stream_file;
ad1c05e1 1605
4ac10b76 1606 stream_fd = lttng_get_unused_fd();
ad1c05e1
MD
1607 if (stream_fd < 0) {
1608 ret = stream_fd;
1609 goto fd_error;
1610 }
a3fcd499 1611 stream_file = anon_inode_getfile(name, fops, stream_priv, O_RDWR);
c0e31d2e
MD
1612 if (IS_ERR(stream_file)) {
1613 ret = PTR_ERR(stream_file);
ad1c05e1
MD
1614 goto file_error;
1615 }
409453cb
MD
1616 /*
1617 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
1618 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
1619 * file descriptor, so we set FMODE_PREAD here.
1620 */
d7b6f197 1621 stream_file->f_mode |= FMODE_PREAD;
c0e31d2e 1622 fd_install(stream_fd, stream_file);
dda6a249
MD
1623 /*
1624 * The stream holds a reference to the channel within the generic ring
1625 * buffer library, so no need to hold a refcount on the channel and
1626 * session files here.
1627 */
ad1c05e1
MD
1628 return stream_fd;
1629
1630file_error:
1631 put_unused_fd(stream_fd);
d83004aa
JD
1632fd_error:
1633 return ret;
1634}
1635
1636static
1637int lttng_abi_open_stream(struct file *channel_file)
1638{
f7d06400 1639 struct lttng_kernel_channel_buffer *channel = channel_file->private_data;
e20c0fec 1640 struct lttng_kernel_ring_buffer *buf;
d83004aa
JD
1641 int ret;
1642 void *stream_priv;
1643
f7d06400 1644 buf = channel->ops->priv->buffer_read_open(channel->priv->rb_chan);
d83004aa
JD
1645 if (!buf)
1646 return -ENOENT;
1647
1648 stream_priv = buf;
1649 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
a3fcd499
MD
1650 &lttng_stream_ring_buffer_file_operations,
1651 "[lttng_stream]");
d83004aa
JD
1652 if (ret < 0)
1653 goto fd_error;
1654
1655 return ret;
1656
1657fd_error:
4a399b76 1658 channel->ops->priv->buffer_read_close(buf);
d83004aa
JD
1659 return ret;
1660}
1661
1662static
1663int lttng_abi_open_metadata_stream(struct file *channel_file)
1664{
f7d06400
MD
1665 struct lttng_kernel_channel_buffer *channel = channel_file->private_data;
1666 struct lttng_kernel_session *session = channel->parent.session;
e20c0fec 1667 struct lttng_kernel_ring_buffer *buf;
d83004aa
JD
1668 int ret;
1669 struct lttng_metadata_stream *metadata_stream;
1670 void *stream_priv;
1671
f7d06400 1672 buf = channel->ops->priv->buffer_read_open(channel->priv->rb_chan);
d83004aa
JD
1673 if (!buf)
1674 return -ENOENT;
1675
1676 metadata_stream = kzalloc(sizeof(struct lttng_metadata_stream),
1677 GFP_KERNEL);
b3b8072b
MD
1678 if (!metadata_stream) {
1679 ret = -ENOMEM;
1680 goto nomem;
1681 }
8cdc1a81 1682 metadata_stream->metadata_cache = session->priv->metadata_cache;
d83004aa
JD
1683 init_waitqueue_head(&metadata_stream->read_wait);
1684 metadata_stream->priv = buf;
1685 stream_priv = metadata_stream;
f7d06400 1686 metadata_stream->transport = channel->priv->transport;
8b97fd42
MD
1687 /* Initial state is an empty metadata, considered as incoherent. */
1688 metadata_stream->coherent = false;
b3b8072b
MD
1689
1690 /*
1691 * Since life-time of metadata cache differs from that of
1692 * session, we need to keep our own reference on the transport.
1693 */
1694 if (!try_module_get(metadata_stream->transport->owner)) {
5a15f70c 1695 printk(KERN_WARNING "LTTng: Can't lock transport module.\n");
b3b8072b
MD
1696 ret = -EINVAL;
1697 goto notransport;
1698 }
1699
8cdc1a81 1700 if (!lttng_kref_get(&session->priv->metadata_cache->refcount)) {
901aaa5f 1701 ret = -EOVERFLOW;
9c1f4643 1702 goto kref_error;
901aaa5f
FD
1703 }
1704
d83004aa 1705 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
a3fcd499
MD
1706 &lttng_metadata_ring_buffer_file_operations,
1707 "[lttng_metadata_stream]");
d83004aa
JD
1708 if (ret < 0)
1709 goto fd_error;
1710
8cdc1a81 1711 mutex_lock(&session->priv->metadata_cache->lock);
d83004aa 1712 list_add(&metadata_stream->list,
8cdc1a81
MD
1713 &session->priv->metadata_cache->metadata_stream);
1714 mutex_unlock(&session->priv->metadata_cache->lock);
d83004aa
JD
1715 return ret;
1716
ad1c05e1 1717fd_error:
8cdc1a81 1718 kref_put(&session->priv->metadata_cache->refcount, metadata_cache_destroy);
9c1f4643 1719kref_error:
b3b8072b
MD
1720 module_put(metadata_stream->transport->owner);
1721notransport:
1722 kfree(metadata_stream);
1723nomem:
4a399b76 1724 channel->ops->priv->buffer_read_close(buf);
ad1c05e1
MD
1725 return ret;
1726}
1727
21f58fb7
FD
1728static
1729int lttng_abi_open_event_notifier_group_stream(struct file *notif_file)
1730{
1731 struct lttng_event_notifier_group *event_notifier_group = notif_file->private_data;
860c213b 1732 struct lttng_kernel_ring_buffer_channel *chan = event_notifier_group->chan;
e20c0fec 1733 struct lttng_kernel_ring_buffer *buf;
21f58fb7
FD
1734 int ret;
1735 void *stream_priv;
1736
4a399b76 1737 buf = event_notifier_group->ops->priv->buffer_read_open(chan);
21f58fb7
FD
1738 if (!buf)
1739 return -ENOENT;
1740
1741 /* The event_notifier notification fd holds a reference on the event_notifier group */
1742 if (!atomic_long_add_unless(&notif_file->f_count, 1, LONG_MAX)) {
1743 ret = -EOVERFLOW;
1744 goto refcount_error;
1745 }
1746 event_notifier_group->buf = buf;
1747 stream_priv = event_notifier_group;
1748 ret = lttng_abi_create_stream_fd(notif_file, stream_priv,
1749 &lttng_event_notifier_group_notif_fops,
1750 "[lttng_event_notifier_stream]");
1751 if (ret < 0)
1752 goto fd_error;
1753
1754 return ret;
1755
1756fd_error:
1757 atomic_long_dec(&notif_file->f_count);
1758refcount_error:
4a399b76 1759 event_notifier_group->ops->priv->buffer_read_close(buf);
21f58fb7
FD
1760 return ret;
1761}
1762
badfe9f5 1763static
606828e4 1764int lttng_abi_validate_event_param(struct lttng_kernel_abi_event *event_param)
badfe9f5
MD
1765{
1766 /* Limit ABI to implemented features. */
1767 switch (event_param->instrumentation) {
606828e4 1768 case LTTNG_KERNEL_ABI_SYSCALL:
badfe9f5 1769 switch (event_param->u.syscall.entryexit) {
c190d76e
MJ
1770 case LTTNG_KERNEL_ABI_SYSCALL_ENTRY:
1771 lttng_fallthrough;
1772 case LTTNG_KERNEL_ABI_SYSCALL_EXIT:
1773 lttng_fallthrough;
606828e4 1774 case LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT:
badfe9f5
MD
1775 break;
1776 default:
1777 return -EINVAL;
1778 }
1779 switch (event_param->u.syscall.abi) {
606828e4 1780 case LTTNG_KERNEL_ABI_SYSCALL_ABI_ALL:
badfe9f5
MD
1781 break;
1782 default:
1783 return -EINVAL;
1784 }
1785 switch (event_param->u.syscall.match) {
606828e4 1786 case LTTNG_KERNEL_ABI_SYSCALL_MATCH_NAME:
badfe9f5
MD
1787 break;
1788 default:
1789 return -EINVAL;
1790 }
1791 break;
1792
606828e4 1793 case LTTNG_KERNEL_ABI_KRETPROBE:
88a82b17 1794 switch (event_param->u.kretprobe.entryexit) {
606828e4 1795 case LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT:
88a82b17 1796 break;
c190d76e
MJ
1797 case LTTNG_KERNEL_ABI_SYSCALL_ENTRY:
1798 lttng_fallthrough;
1799 case LTTNG_KERNEL_ABI_SYSCALL_EXIT:
1800 lttng_fallthrough;
88a82b17
MD
1801 default:
1802 return -EINVAL;
1803 }
1804 break;
1805
c190d76e
MJ
1806 case LTTNG_KERNEL_ABI_TRACEPOINT:
1807 lttng_fallthrough;
1808 case LTTNG_KERNEL_ABI_KPROBE:
1809 lttng_fallthrough;
606828e4 1810 case LTTNG_KERNEL_ABI_UPROBE:
badfe9f5
MD
1811 break;
1812
c190d76e
MJ
1813 case LTTNG_KERNEL_ABI_FUNCTION:
1814 lttng_fallthrough;
1815 case LTTNG_KERNEL_ABI_NOOP:
1816 lttng_fallthrough;
badfe9f5
MD
1817 default:
1818 return -EINVAL;
1819 }
1820 return 0;
1821}
1822
653fe716 1823static
c0e31d2e 1824int lttng_abi_create_event(struct file *channel_file,
606828e4 1825 struct lttng_kernel_abi_event *event_param)
653fe716 1826{
ef784b4d 1827 const struct file_operations *fops;
f7d06400 1828 struct lttng_kernel_channel_buffer *channel = channel_file->private_data;
653fe716 1829 int event_fd, ret;
11b5a3c2 1830 struct file *event_file;
3c997079 1831 void *priv;
653fe716 1832
606828e4 1833 event_param->name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0';
6dccd6c1 1834 switch (event_param->instrumentation) {
606828e4
MD
1835 case LTTNG_KERNEL_ABI_KRETPROBE:
1836 event_param->u.kretprobe.symbol_name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0';
7371f44c 1837 break;
606828e4
MD
1838 case LTTNG_KERNEL_ABI_KPROBE:
1839 event_param->u.kprobe.symbol_name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0';
e0a7a7c4 1840 break;
606828e4 1841 case LTTNG_KERNEL_ABI_FUNCTION:
e884017c
MD
1842 WARN_ON_ONCE(1);
1843 /* Not implemented. */
e0a7a7c4
MD
1844 break;
1845 default:
1846 break;
1847 }
ef784b4d
MD
1848
1849 switch (event_param->instrumentation) {
c190d76e
MJ
1850 case LTTNG_KERNEL_ABI_TRACEPOINT:
1851 lttng_fallthrough;
ef784b4d
MD
1852 case LTTNG_KERNEL_ABI_SYSCALL:
1853 fops = &lttng_event_recorder_enabler_fops;
1854 break;
c190d76e
MJ
1855 case LTTNG_KERNEL_ABI_KPROBE:
1856 lttng_fallthrough;
1857 case LTTNG_KERNEL_ABI_KRETPROBE:
1858 lttng_fallthrough;
ef784b4d
MD
1859 case LTTNG_KERNEL_ABI_UPROBE:
1860 fops = &lttng_event_recorder_event_fops;
1861 break;
1862
c190d76e
MJ
1863 case LTTNG_KERNEL_ABI_FUNCTION:
1864 lttng_fallthrough;
1865 case LTTNG_KERNEL_ABI_NOOP:
1866 lttng_fallthrough;
ef784b4d
MD
1867 default:
1868 return -EINVAL;
1869 }
1870
33a39a3c
MD
1871 event_fd = lttng_get_unused_fd();
1872 if (event_fd < 0) {
1873 ret = event_fd;
1874 goto fd_error;
1875 }
1876 event_file = anon_inode_getfile("[lttng_event]",
ef784b4d 1877 fops, NULL, O_RDWR);
33a39a3c
MD
1878 if (IS_ERR(event_file)) {
1879 ret = PTR_ERR(event_file);
1880 goto file_error;
1881 }
9c1f4643 1882 /* The event holds a reference on the channel */
98d7281c 1883 if (!atomic_long_add_unless(&channel_file->f_count, 1, LONG_MAX)) {
0d2c717f 1884 ret = -EOVERFLOW;
9c1f4643
MD
1885 goto refcount_error;
1886 }
badfe9f5
MD
1887 ret = lttng_abi_validate_event_param(event_param);
1888 if (ret)
1889 goto event_error;
684a1e4d
MD
1890
1891 switch (event_param->instrumentation) {
c190d76e
MJ
1892 case LTTNG_KERNEL_ABI_TRACEPOINT:
1893 lttng_fallthrough;
606828e4 1894 case LTTNG_KERNEL_ABI_SYSCALL:
684a1e4d 1895 {
1ae083ba 1896 struct lttng_event_recorder_enabler *event_enabler;
33a39a3c 1897
4993071a
PP
1898 if (strutils_is_star_glob_pattern(event_param->name)) {
1899 /*
1900 * If the event name is a star globbing pattern,
1901 * we create the special star globbing enabler.
1902 */
1ae083ba 1903 event_enabler = lttng_event_recorder_enabler_create(LTTNG_ENABLER_FORMAT_STAR_GLOB,
33a39a3c 1904 event_param, channel);
3c997079 1905 } else {
1ae083ba 1906 event_enabler = lttng_event_recorder_enabler_create(LTTNG_ENABLER_FORMAT_NAME,
33a39a3c 1907 event_param, channel);
1ec65de1 1908 }
def1e304
MD
1909 if (event_enabler)
1910 lttng_event_enabler_session_add(channel->parent.session, event_enabler);
b2bc0bc8 1911 priv = event_enabler;
684a1e4d
MD
1912 break;
1913 }
1914
c190d76e
MJ
1915 case LTTNG_KERNEL_ABI_KPROBE:
1916 lttng_fallthrough;
1917 case LTTNG_KERNEL_ABI_KRETPROBE:
1918 lttng_fallthrough;
606828e4 1919 case LTTNG_KERNEL_ABI_UPROBE:
684a1e4d 1920 {
83006ee0 1921 struct lttng_kernel_event_common *event;
1ae083ba 1922 struct lttng_event_recorder_enabler *event_enabler;
4ee2453d 1923
1ae083ba 1924 event_enabler = lttng_event_recorder_enabler_create(LTTNG_ENABLER_FORMAT_NAME,
def1e304
MD
1925 event_param, channel);
1926 if (!event_enabler) {
1927 ret = -ENOMEM;
1928 goto event_error;
1929 }
33a39a3c
MD
1930 /*
1931 * We tolerate no failure path after event creation. It
1932 * will stay invariant for the rest of the session.
1933 */
83006ee0 1934 event = lttng_kernel_event_create(&event_enabler->parent, NULL);
c3fb484e 1935 WARN_ON_ONCE(IS_ERR(event));
44b1abdc 1936 lttng_event_enabler_destroy(&event_enabler->parent);
33a39a3c
MD
1937 if (IS_ERR(event)) {
1938 ret = PTR_ERR(event);
1939 goto event_error;
80f87dd2 1940 }
83006ee0 1941 priv = container_of(event, struct lttng_kernel_event_recorder, parent);
684a1e4d
MD
1942 break;
1943 }
1944
c190d76e
MJ
1945 case LTTNG_KERNEL_ABI_FUNCTION:
1946 lttng_fallthrough;
1947 case LTTNG_KERNEL_ABI_NOOP:
1948 lttng_fallthrough;
684a1e4d
MD
1949 default:
1950 ret = -EINVAL;
1951 goto event_error;
03037b98 1952 }
33a39a3c
MD
1953 event_file->private_data = priv;
1954 fd_install(event_fd, event_file);
653fe716
MD
1955 return event_fd;
1956
03037b98 1957event_error:
9c1f4643
MD
1958 atomic_long_dec(&channel_file->f_count);
1959refcount_error:
c0e31d2e 1960 fput(event_file);
653fe716
MD
1961file_error:
1962 put_unused_fd(event_fd);
1963fd_error:
653fe716
MD
1964 return ret;
1965}
ad1c05e1 1966
dffef45d 1967static
ef784b4d 1968long lttng_event_notifier_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
dffef45d 1969{
a67ba386 1970 struct lttng_kernel_event_notifier *event_notifier = file->private_data;
dffef45d
FD
1971
1972 switch (cmd) {
606828e4 1973 case LTTNG_KERNEL_ABI_ENABLE:
9d993668 1974 return lttng_event_enable(&event_notifier->parent);
606828e4 1975 case LTTNG_KERNEL_ABI_DISABLE:
9d993668 1976 return lttng_event_disable(&event_notifier->parent);
606828e4 1977 case LTTNG_KERNEL_ABI_FILTER:
ef784b4d 1978 return -EINVAL;
606828e4 1979 case LTTNG_KERNEL_ABI_CAPTURE:
ef784b4d 1980 return -EINVAL;
606828e4 1981 case LTTNG_KERNEL_ABI_ADD_CALLSITE:
e2d5dbc7 1982 return lttng_event_add_callsite(&event_notifier->parent,
ef784b4d 1983 (struct lttng_kernel_abi_event_callsite __user *) arg);
dffef45d
FD
1984 default:
1985 return -ENOIOCTLCMD;
1986 }
1987}
1988
1989static
ef784b4d 1990long lttng_event_notifier_enabler_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
dffef45d 1991{
ef784b4d 1992 struct lttng_event_notifier_enabler *event_notifier_enabler = file->private_data;
dffef45d 1993
ef784b4d
MD
1994 switch (cmd) {
1995 case LTTNG_KERNEL_ABI_ENABLE:
7b25ab0e 1996 return lttng_event_enabler_enable(&event_notifier_enabler->parent);
ef784b4d 1997 case LTTNG_KERNEL_ABI_DISABLE:
7b25ab0e 1998 return lttng_event_enabler_disable(&event_notifier_enabler->parent);
ef784b4d 1999 case LTTNG_KERNEL_ABI_FILTER:
6048044f 2000 return lttng_event_enabler_attach_filter_bytecode(&event_notifier_enabler->parent,
ef784b4d
MD
2001 (struct lttng_kernel_abi_filter_bytecode __user *) arg);
2002 case LTTNG_KERNEL_ABI_CAPTURE:
2003 return lttng_event_notifier_enabler_attach_capture_bytecode(
2004 event_notifier_enabler,
2005 (struct lttng_kernel_abi_capture_bytecode __user *) arg);
2006 case LTTNG_KERNEL_ABI_ADD_CALLSITE:
2007 return -EINVAL;
dffef45d 2008 default:
ef784b4d 2009 return -ENOIOCTLCMD;
dffef45d 2010 }
ef784b4d 2011}
dffef45d 2012
ef784b4d
MD
2013static
2014int lttng_event_notifier_event_release(struct inode *inode, struct file *file)
2015{
a67ba386 2016 struct lttng_kernel_event_notifier *event_notifier = file->private_data;
ef784b4d
MD
2017
2018 if (event_notifier)
a67ba386 2019 fput(event_notifier->priv->group->file);
dffef45d
FD
2020 return 0;
2021}
2022
ef784b4d
MD
2023static
2024int lttng_event_notifier_enabler_release(struct inode *inode, struct file *file)
2025{
2026 struct lttng_event_notifier_enabler *event_notifier_enabler = file->private_data;
2027
2028 if (event_notifier_enabler)
2029 fput(event_notifier_enabler->group->file);
2030 return 0;
2031}
2032
2033static const struct file_operations lttng_event_notifier_event_fops = {
2034 .owner = THIS_MODULE,
2035 .release = lttng_event_notifier_event_release,
2036 .unlocked_ioctl = lttng_event_notifier_event_ioctl,
2037#ifdef CONFIG_COMPAT
2038 .compat_ioctl = lttng_event_notifier_event_ioctl,
2039#endif
2040};
2041
2042static const struct file_operations lttng_event_notifier_enabler_fops = {
dffef45d 2043 .owner = THIS_MODULE,
ef784b4d
MD
2044 .release = lttng_event_notifier_enabler_release,
2045 .unlocked_ioctl = lttng_event_notifier_enabler_ioctl,
dffef45d 2046#ifdef CONFIG_COMPAT
ef784b4d 2047 .compat_ioctl = lttng_event_notifier_enabler_ioctl,
dffef45d
FD
2048#endif
2049};
2050
2051static
2052int lttng_abi_create_event_notifier(struct file *event_notifier_group_file,
606828e4 2053 struct lttng_kernel_abi_event_notifier *event_notifier_param)
dffef45d
FD
2054{
2055 struct lttng_event_notifier_group *event_notifier_group =
2056 event_notifier_group_file->private_data;
ef784b4d 2057 const struct file_operations *fops;
dffef45d
FD
2058 int event_notifier_fd, ret;
2059 struct file *event_notifier_file;
2060 void *priv;
2061
2062 switch (event_notifier_param->event.instrumentation) {
606828e4
MD
2063 case LTTNG_KERNEL_ABI_TRACEPOINT:
2064 case LTTNG_KERNEL_ABI_UPROBE:
b01155ba 2065 break;
606828e4
MD
2066 case LTTNG_KERNEL_ABI_KPROBE:
2067 event_notifier_param->event.u.kprobe.symbol_name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0';
2b16f0c9 2068 break;
606828e4 2069 case LTTNG_KERNEL_ABI_SYSCALL:
8a8ac9a8 2070 break;
606828e4 2071 case LTTNG_KERNEL_ABI_KRETPROBE:
9de67196 2072 /* Placing an event notifier on kretprobe is not supported. */
606828e4
MD
2073 case LTTNG_KERNEL_ABI_FUNCTION:
2074 case LTTNG_KERNEL_ABI_NOOP:
dffef45d
FD
2075 default:
2076 ret = -EINVAL;
2077 goto inval_instr;
2078 }
2079
ef784b4d 2080 switch (event_notifier_param->event.instrumentation) {
c190d76e
MJ
2081 case LTTNG_KERNEL_ABI_TRACEPOINT:
2082 lttng_fallthrough;
ef784b4d
MD
2083 case LTTNG_KERNEL_ABI_SYSCALL:
2084 fops = &lttng_event_notifier_enabler_fops;
2085 break;
c190d76e
MJ
2086 case LTTNG_KERNEL_ABI_KPROBE:
2087 lttng_fallthrough;
2088 case LTTNG_KERNEL_ABI_KRETPROBE:
2089 lttng_fallthrough;
ef784b4d
MD
2090 case LTTNG_KERNEL_ABI_UPROBE:
2091 fops = &lttng_event_notifier_event_fops;
2092 break;
2093
c190d76e
MJ
2094 case LTTNG_KERNEL_ABI_FUNCTION:
2095 lttng_fallthrough;
2096 case LTTNG_KERNEL_ABI_NOOP:
2097 lttng_fallthrough;
ef784b4d
MD
2098 default:
2099 ret = -EINVAL;
2100 goto inval_instr;
2101 }
2102
606828e4 2103 event_notifier_param->event.name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0';
dffef45d
FD
2104
2105 event_notifier_fd = lttng_get_unused_fd();
2106 if (event_notifier_fd < 0) {
2107 ret = event_notifier_fd;
2108 goto fd_error;
2109 }
2110
2111 event_notifier_file = anon_inode_getfile("[lttng_event_notifier]",
ef784b4d 2112 fops, NULL, O_RDWR);
dffef45d
FD
2113 if (IS_ERR(event_notifier_file)) {
2114 ret = PTR_ERR(event_notifier_file);
2115 goto file_error;
2116 }
2117
2118 /* The event notifier holds a reference on the event notifier group. */
2119 if (!atomic_long_add_unless(&event_notifier_group_file->f_count, 1, LONG_MAX)) {
2120 ret = -EOVERFLOW;
2121 goto refcount_error;
2122 }
2123
56237619
MD
2124 ret = lttng_abi_validate_event_param(&event_notifier_param->event);
2125 if (ret)
2126 goto event_notifier_error;
2127
684a1e4d 2128 switch (event_notifier_param->event.instrumentation) {
c190d76e
MJ
2129 case LTTNG_KERNEL_ABI_TRACEPOINT:
2130 lttng_fallthrough;
606828e4 2131 case LTTNG_KERNEL_ABI_SYSCALL:
684a1e4d 2132 {
dffef45d
FD
2133 struct lttng_event_notifier_enabler *enabler;
2134
2135 if (strutils_is_star_glob_pattern(event_notifier_param->event.name)) {
2136 /*
2137 * If the event name is a star globbing pattern,
2138 * we create the special star globbing enabler.
2139 */
2140 enabler = lttng_event_notifier_enabler_create(
dffef45d 2141 LTTNG_ENABLER_FORMAT_STAR_GLOB,
1bbb5b2d
MD
2142 event_notifier_param,
2143 event_notifier_group);
dffef45d
FD
2144 } else {
2145 enabler = lttng_event_notifier_enabler_create(
dffef45d 2146 LTTNG_ENABLER_FORMAT_NAME,
1bbb5b2d
MD
2147 event_notifier_param,
2148 event_notifier_group);
dffef45d 2149 }
1bbb5b2d
MD
2150 if (enabler)
2151 lttng_event_notifier_enabler_group_add(event_notifier_group, enabler);
dffef45d 2152 priv = enabler;
684a1e4d
MD
2153 break;
2154 }
2155
c190d76e
MJ
2156 case LTTNG_KERNEL_ABI_KPROBE:
2157 lttng_fallthrough;
2158 case LTTNG_KERNEL_ABI_KRETPROBE:
2159 lttng_fallthrough;
606828e4 2160 case LTTNG_KERNEL_ABI_UPROBE:
684a1e4d 2161 {
83006ee0 2162 struct lttng_kernel_event_common *event;
1bbb5b2d 2163 struct lttng_event_notifier_enabler *event_notifier_enabler;
dffef45d 2164
1bbb5b2d
MD
2165 event_notifier_enabler = lttng_event_notifier_enabler_create(LTTNG_ENABLER_FORMAT_NAME,
2166 event_notifier_param, event_notifier_group);
2167 if (!event_notifier_enabler) {
2168 ret = -ENOMEM;
2169 goto event_notifier_error;
2170 }
83006ee0 2171 event = lttng_kernel_event_create(&event_notifier_enabler->parent, NULL);
c3fb484e 2172 WARN_ON_ONCE(IS_ERR(event));
1bbb5b2d 2173 lttng_event_enabler_destroy(&event_notifier_enabler->parent);
83006ee0
MD
2174 if (IS_ERR(event)) {
2175 ret = PTR_ERR(event);
dffef45d
FD
2176 goto event_notifier_error;
2177 }
83006ee0 2178 priv = container_of(event, struct lttng_kernel_event_notifier, parent);
684a1e4d
MD
2179 break;
2180 }
2181
c190d76e
MJ
2182 case LTTNG_KERNEL_ABI_FUNCTION:
2183 lttng_fallthrough;
2184 case LTTNG_KERNEL_ABI_NOOP:
2185 lttng_fallthrough;
684a1e4d
MD
2186 default:
2187 ret = -EINVAL;
2188 goto event_notifier_error;
dffef45d
FD
2189 }
2190 event_notifier_file->private_data = priv;
2191 fd_install(event_notifier_fd, event_notifier_file);
2192 return event_notifier_fd;
2193
2194event_notifier_error:
2195 atomic_long_dec(&event_notifier_group_file->f_count);
2196refcount_error:
2197 fput(event_notifier_file);
2198file_error:
2199 put_unused_fd(event_notifier_fd);
2200fd_error:
2201inval_instr:
2202 return ret;
2203}
2204
99f52fcc
FD
2205static
2206long lttng_abi_event_notifier_group_create_error_counter(
2207 struct file *event_notifier_group_file,
606828e4 2208 const struct lttng_kernel_abi_counter_conf *error_counter_conf)
99f52fcc
FD
2209{
2210 int counter_fd, ret;
2211 char *counter_transport_name;
2212 size_t counter_len;
2213 struct lttng_counter *counter = NULL;
2214 struct file *counter_file;
2215 struct lttng_event_notifier_group *event_notifier_group =
2216 (struct lttng_event_notifier_group *) event_notifier_group_file->private_data;
2217
606828e4 2218 if (error_counter_conf->arithmetic != LTTNG_KERNEL_ABI_COUNTER_ARITHMETIC_MODULAR) {
99f52fcc
FD
2219 printk(KERN_ERR "LTTng: event_notifier: Error counter of the wrong arithmetic type.\n");
2220 return -EINVAL;
2221 }
2222
2223 if (error_counter_conf->number_dimensions != 1) {
2224 printk(KERN_ERR "LTTng: event_notifier: Error counter has more than one dimension.\n");
2225 return -EINVAL;
2226 }
2227
2228 switch (error_counter_conf->bitness) {
606828e4 2229 case LTTNG_KERNEL_ABI_COUNTER_BITNESS_64:
99f52fcc
FD
2230 counter_transport_name = "counter-per-cpu-64-modular";
2231 break;
606828e4 2232 case LTTNG_KERNEL_ABI_COUNTER_BITNESS_32:
99f52fcc
FD
2233 counter_transport_name = "counter-per-cpu-32-modular";
2234 break;
2235 default:
2236 return -EINVAL;
2237 }
2238
2239 /*
2240 * Lock sessions to provide mutual exclusion against concurrent
2241 * modification of event_notifier group, which would result in
2242 * overwriting the error counter if set concurrently.
2243 */
2244 lttng_lock_sessions();
2245
2246 if (event_notifier_group->error_counter) {
2247 printk(KERN_ERR "Error counter already created in event_notifier group\n");
2248 ret = -EBUSY;
2249 goto fd_error;
2250 }
2251
2252 counter_fd = lttng_get_unused_fd();
2253 if (counter_fd < 0) {
2254 ret = counter_fd;
2255 goto fd_error;
2256 }
2257
2258 counter_file = anon_inode_getfile("[lttng_counter]",
2259 &lttng_counter_fops,
2260 NULL, O_RDONLY);
2261 if (IS_ERR(counter_file)) {
2262 ret = PTR_ERR(counter_file);
2263 goto file_error;
2264 }
2265
2266 counter_len = error_counter_conf->dimensions[0].size;
2267
2268 if (!atomic_long_add_unless(&event_notifier_group_file->f_count, 1, LONG_MAX)) {
2269 ret = -EOVERFLOW;
2270 goto refcount_error;
2271 }
2272
2273 counter = lttng_kernel_counter_create(counter_transport_name,
2274 1, &counter_len);
2275 if (!counter) {
2276 ret = -EINVAL;
2277 goto counter_error;
2278 }
2279
99f52fcc 2280 event_notifier_group->error_counter_len = counter_len;
ab04d7b1
MD
2281 /*
2282 * store-release to publish error counter matches load-acquire
2283 * in record_error. Ensures the counter is created and the
2284 * error_counter_len is set before they are used.
2285 */
6657edec 2286 lttng_smp_store_release(&event_notifier_group->error_counter, counter);
99f52fcc
FD
2287
2288 counter->file = counter_file;
2289 counter->owner = event_notifier_group->file;
2290 counter_file->private_data = counter;
2291 /* Ownership transferred. */
2292 counter = NULL;
2293
2294 fd_install(counter_fd, counter_file);
2295 lttng_unlock_sessions();
2296
2297 return counter_fd;
2298
2299counter_error:
2300 atomic_long_dec(&event_notifier_group_file->f_count);
2301refcount_error:
2302 fput(counter_file);
2303file_error:
2304 put_unused_fd(counter_fd);
2305fd_error:
2306 lttng_unlock_sessions();
2307 return ret;
2308}
2309
750b05f2
FD
2310static
2311long lttng_event_notifier_group_ioctl(struct file *file, unsigned int cmd,
2312 unsigned long arg)
2313{
2314 switch (cmd) {
606828e4 2315 case LTTNG_KERNEL_ABI_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD:
21f58fb7
FD
2316 {
2317 return lttng_abi_open_event_notifier_group_stream(file);
2318 }
606828e4 2319 case LTTNG_KERNEL_ABI_EVENT_NOTIFIER_CREATE:
dffef45d 2320 {
606828e4 2321 struct lttng_kernel_abi_event_notifier uevent_notifier_param;
dffef45d
FD
2322
2323 if (copy_from_user(&uevent_notifier_param,
606828e4 2324 (struct lttng_kernel_abi_event_notifier __user *) arg,
dffef45d
FD
2325 sizeof(uevent_notifier_param)))
2326 return -EFAULT;
2327 return lttng_abi_create_event_notifier(file, &uevent_notifier_param);
2328 }
606828e4 2329 case LTTNG_KERNEL_ABI_COUNTER:
99f52fcc 2330 {
606828e4 2331 struct lttng_kernel_abi_counter_conf uerror_counter_conf;
99f52fcc
FD
2332
2333 if (copy_from_user(&uerror_counter_conf,
606828e4 2334 (struct lttng_kernel_abi_counter_conf __user *) arg,
99f52fcc
FD
2335 sizeof(uerror_counter_conf)))
2336 return -EFAULT;
2337 return lttng_abi_event_notifier_group_create_error_counter(file,
2338 &uerror_counter_conf);
2339 }
750b05f2
FD
2340 default:
2341 return -ENOIOCTLCMD;
2342 }
2343 return 0;
2344}
2345
2346static
2347int lttng_event_notifier_group_release(struct inode *inode, struct file *file)
2348{
2349 struct lttng_event_notifier_group *event_notifier_group =
2350 file->private_data;
2351
2352 if (event_notifier_group)
2353 lttng_event_notifier_group_destroy(event_notifier_group);
2354 return 0;
2355}
2356
2357static const struct file_operations lttng_event_notifier_group_fops = {
2358 .owner = THIS_MODULE,
2359 .release = lttng_event_notifier_group_release,
2360 .unlocked_ioctl = lttng_event_notifier_group_ioctl,
2361#ifdef CONFIG_COMPAT
2362 .compat_ioctl = lttng_event_notifier_group_ioctl,
2363#endif
2364};
2365
ad1c05e1
MD
2366/**
2367 * lttng_channel_ioctl - lttng syscall through ioctl
2368 *
c0e31d2e 2369 * @file: the file
ad1c05e1
MD
2370 * @cmd: the command
2371 * @arg: command arg
2372 *
2373 * This ioctl implements lttng commands:
606828e4 2374 * LTTNG_KERNEL_ABI_STREAM
ad1c05e1
MD
2375 * Returns an event stream file descriptor or failure.
2376 * (typically, one event stream records events from one CPU)
606828e4 2377 * LTTNG_KERNEL_ABI_EVENT
ad1c05e1 2378 * Returns an event file descriptor or failure.
606828e4 2379 * LTTNG_KERNEL_ABI_CONTEXT
8070f5c0 2380 * Prepend a context field to each event in the channel
606828e4 2381 * LTTNG_KERNEL_ABI_ENABLE
e64957da 2382 * Enable recording for events in this channel (weak enable)
606828e4 2383 * LTTNG_KERNEL_ABI_DISABLE
e64957da 2384 * Disable recording for events in this channel (strong disable)
baf20995 2385 *
baf20995
MD
2386 * Channel and event file descriptors also hold a reference on the session.
2387 */
ad1c05e1 2388static
c0e31d2e 2389long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
baf20995 2390{
f7d06400 2391 struct lttng_kernel_channel_buffer *channel = file->private_data;
8070f5c0 2392
baf20995 2393 switch (cmd) {
606828e4
MD
2394 case LTTNG_KERNEL_ABI_OLD_STREAM:
2395 case LTTNG_KERNEL_ABI_STREAM:
c0e31d2e 2396 return lttng_abi_open_stream(file);
606828e4 2397 case LTTNG_KERNEL_ABI_OLD_EVENT:
6dccd6c1 2398 {
606828e4
MD
2399 struct lttng_kernel_abi_event *uevent_param;
2400 struct lttng_kernel_abi_old_event *old_uevent_param;
6dccd6c1
JD
2401 int ret;
2402
606828e4 2403 uevent_param = kmalloc(sizeof(struct lttng_kernel_abi_event),
6dccd6c1
JD
2404 GFP_KERNEL);
2405 if (!uevent_param) {
2406 ret = -ENOMEM;
2407 goto old_event_end;
2408 }
2409 old_uevent_param = kmalloc(
606828e4 2410 sizeof(struct lttng_kernel_abi_old_event),
6dccd6c1
JD
2411 GFP_KERNEL);
2412 if (!old_uevent_param) {
2413 ret = -ENOMEM;
2414 goto old_event_error_free_param;
2415 }
2416 if (copy_from_user(old_uevent_param,
606828e4
MD
2417 (struct lttng_kernel_abi_old_event __user *) arg,
2418 sizeof(struct lttng_kernel_abi_old_event))) {
6dccd6c1
JD
2419 ret = -EFAULT;
2420 goto old_event_error_free_old_param;
2421 }
2422
2423 memcpy(uevent_param->name, old_uevent_param->name,
2424 sizeof(uevent_param->name));
2425 uevent_param->instrumentation =
2426 old_uevent_param->instrumentation;
2427
2428 switch (old_uevent_param->instrumentation) {
606828e4 2429 case LTTNG_KERNEL_ABI_KPROBE:
6dccd6c1
JD
2430 uevent_param->u.kprobe.addr =
2431 old_uevent_param->u.kprobe.addr;
2432 uevent_param->u.kprobe.offset =
2433 old_uevent_param->u.kprobe.offset;
2434 memcpy(uevent_param->u.kprobe.symbol_name,
2435 old_uevent_param->u.kprobe.symbol_name,
2436 sizeof(uevent_param->u.kprobe.symbol_name));
2437 break;
606828e4 2438 case LTTNG_KERNEL_ABI_KRETPROBE:
6dccd6c1
JD
2439 uevent_param->u.kretprobe.addr =
2440 old_uevent_param->u.kretprobe.addr;
2441 uevent_param->u.kretprobe.offset =
2442 old_uevent_param->u.kretprobe.offset;
2443 memcpy(uevent_param->u.kretprobe.symbol_name,
2444 old_uevent_param->u.kretprobe.symbol_name,
2445 sizeof(uevent_param->u.kretprobe.symbol_name));
2446 break;
606828e4 2447 case LTTNG_KERNEL_ABI_FUNCTION:
e884017c
MD
2448 WARN_ON_ONCE(1);
2449 /* Not implemented. */
6dccd6c1
JD
2450 break;
2451 default:
2452 break;
2453 }
2454 ret = lttng_abi_create_event(file, uevent_param);
2455
2456old_event_error_free_old_param:
2457 kfree(old_uevent_param);
2458old_event_error_free_param:
2459 kfree(uevent_param);
2460old_event_end:
2461 return ret;
2462 }
606828e4 2463 case LTTNG_KERNEL_ABI_EVENT:
6dccd6c1 2464 {
606828e4 2465 struct lttng_kernel_abi_event uevent_param;
6dccd6c1
JD
2466
2467 if (copy_from_user(&uevent_param,
606828e4 2468 (struct lttng_kernel_abi_event __user *) arg,
6dccd6c1
JD
2469 sizeof(uevent_param)))
2470 return -EFAULT;
2471 return lttng_abi_create_event(file, &uevent_param);
2472 }
606828e4 2473 case LTTNG_KERNEL_ABI_OLD_CONTEXT:
6dccd6c1 2474 {
606828e4
MD
2475 struct lttng_kernel_abi_context *ucontext_param;
2476 struct lttng_kernel_abi_old_context *old_ucontext_param;
6dccd6c1
JD
2477 int ret;
2478
606828e4 2479 ucontext_param = kmalloc(sizeof(struct lttng_kernel_abi_context),
6dccd6c1
JD
2480 GFP_KERNEL);
2481 if (!ucontext_param) {
2482 ret = -ENOMEM;
2483 goto old_ctx_end;
2484 }
606828e4 2485 old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_abi_old_context),
6dccd6c1
JD
2486 GFP_KERNEL);
2487 if (!old_ucontext_param) {
2488 ret = -ENOMEM;
2489 goto old_ctx_error_free_param;
2490 }
2491
2492 if (copy_from_user(old_ucontext_param,
606828e4
MD
2493 (struct lttng_kernel_abi_old_context __user *) arg,
2494 sizeof(struct lttng_kernel_abi_old_context))) {
6dccd6c1
JD
2495 ret = -EFAULT;
2496 goto old_ctx_error_free_old_param;
2497 }
2498 ucontext_param->ctx = old_ucontext_param->ctx;
2499 memcpy(ucontext_param->padding, old_ucontext_param->padding,
2500 sizeof(ucontext_param->padding));
2501 /* only type that uses the union */
606828e4 2502 if (old_ucontext_param->ctx == LTTNG_KERNEL_ABI_CONTEXT_PERF_COUNTER) {
6dccd6c1
JD
2503 ucontext_param->u.perf_counter.type =
2504 old_ucontext_param->u.perf_counter.type;
2505 ucontext_param->u.perf_counter.config =
2506 old_ucontext_param->u.perf_counter.config;
2507 memcpy(ucontext_param->u.perf_counter.name,
2508 old_ucontext_param->u.perf_counter.name,
2509 sizeof(ucontext_param->u.perf_counter.name));
2510 }
2511
2512 ret = lttng_abi_add_context(file,
2513 ucontext_param,
f7d06400 2514 &channel->priv->ctx, channel->parent.session);
6dccd6c1
JD
2515
2516old_ctx_error_free_old_param:
2517 kfree(old_ucontext_param);
2518old_ctx_error_free_param:
2519 kfree(ucontext_param);
2520old_ctx_end:
2521 return ret;
2522 }
606828e4 2523 case LTTNG_KERNEL_ABI_CONTEXT:
6dccd6c1 2524 {
606828e4 2525 struct lttng_kernel_abi_context ucontext_param;
6dccd6c1
JD
2526
2527 if (copy_from_user(&ucontext_param,
606828e4 2528 (struct lttng_kernel_abi_context __user *) arg,
6dccd6c1
JD
2529 sizeof(ucontext_param)))
2530 return -EFAULT;
2531 return lttng_abi_add_context(file,
2532 &ucontext_param,
f7d06400 2533 &channel->priv->ctx, channel->parent.session);
6dccd6c1 2534 }
606828e4
MD
2535 case LTTNG_KERNEL_ABI_OLD_ENABLE:
2536 case LTTNG_KERNEL_ABI_ENABLE:
6b757027 2537 return lttng_channel_enable(&channel->parent);
606828e4
MD
2538 case LTTNG_KERNEL_ABI_OLD_DISABLE:
2539 case LTTNG_KERNEL_ABI_DISABLE:
6b757027 2540 return lttng_channel_disable(&channel->parent);
606828e4 2541 case LTTNG_KERNEL_ABI_SYSCALL_MASK:
c970b655 2542 return lttng_syscall_table_get_active_mask(&channel->priv->parent.syscall_table,
606828e4 2543 (struct lttng_kernel_abi_syscall_mask __user *) arg);
baf20995
MD
2544 default:
2545 return -ENOIOCTLCMD;
2546 }
2547}
2548
5dbbdb43
MD
2549/**
2550 * lttng_metadata_ioctl - lttng syscall through ioctl
2551 *
2552 * @file: the file
2553 * @cmd: the command
2554 * @arg: command arg
2555 *
2556 * This ioctl implements lttng commands:
606828e4 2557 * LTTNG_KERNEL_ABI_STREAM
5dbbdb43
MD
2558 * Returns an event stream file descriptor or failure.
2559 *
2560 * Channel and event file descriptors also hold a reference on the session.
2561 */
2562static
2563long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2564{
2565 switch (cmd) {
606828e4
MD
2566 case LTTNG_KERNEL_ABI_OLD_STREAM:
2567 case LTTNG_KERNEL_ABI_STREAM:
d83004aa 2568 return lttng_abi_open_metadata_stream(file);
5dbbdb43
MD
2569 default:
2570 return -ENOIOCTLCMD;
2571 }
2572}
2573
653fe716
MD
2574/**
2575 * lttng_channel_poll - lttng stream addition/removal monitoring
2576 *
c0e31d2e 2577 * @file: the file
653fe716
MD
2578 * @wait: poll table
2579 */
c0e31d2e 2580unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
653fe716 2581{
f7d06400 2582 struct lttng_kernel_channel_buffer *channel = file->private_data;
653fe716
MD
2583 unsigned int mask = 0;
2584
c0e31d2e 2585 if (file->f_mode & FMODE_READ) {
a33e44a6 2586 poll_wait_set_exclusive(wait);
f7d06400 2587 poll_wait(file, channel->ops->priv->get_hp_wait_queue(channel->priv->rb_chan),
24cedcfe 2588 wait);
653fe716 2589
f7d06400 2590 if (channel->ops->priv->is_disabled(channel->priv->rb_chan))
254ec7bc 2591 return POLLERR;
f7d06400 2592 if (channel->ops->priv->is_finalized(channel->priv->rb_chan))
653fe716 2593 return POLLHUP;
f7d06400 2594 if (channel->ops->priv->buffer_has_read_closed_stream(channel->priv->rb_chan))
653fe716 2595 return POLLIN | POLLRDNORM;
f71ecafa 2596 return 0;
653fe716
MD
2597 }
2598 return mask;
2599
2600}
2601
0a84a57f
MD
2602static
2603int lttng_channel_release(struct inode *inode, struct file *file)
2604{
f7d06400 2605 struct lttng_kernel_channel_buffer *channel = file->private_data;
c269fff4
MD
2606
2607 if (channel)
f7d06400 2608 fput(channel->parent.session->priv->file);
0a84a57f
MD
2609 return 0;
2610}
2611
d83004aa
JD
2612static
2613int lttng_metadata_channel_release(struct inode *inode, struct file *file)
2614{
f7d06400 2615 struct lttng_kernel_channel_buffer *channel = file->private_data;
d83004aa
JD
2616
2617 if (channel) {
f7d06400 2618 fput(channel->parent.session->priv->file);
a3381417 2619 lttng_metadata_channel_destroy(channel);
d83004aa
JD
2620 }
2621
2622 return 0;
2623}
2624
ad1c05e1 2625static const struct file_operations lttng_channel_fops = {
a33c9927 2626 .owner = THIS_MODULE,
03037b98 2627 .release = lttng_channel_release,
653fe716 2628 .poll = lttng_channel_poll,
ad1c05e1 2629 .unlocked_ioctl = lttng_channel_ioctl,
baf20995 2630#ifdef CONFIG_COMPAT
03037b98 2631 .compat_ioctl = lttng_channel_ioctl,
baf20995 2632#endif
11b5a3c2 2633};
baf20995 2634
5dbbdb43 2635static const struct file_operations lttng_metadata_fops = {
a33c9927 2636 .owner = THIS_MODULE,
d83004aa 2637 .release = lttng_metadata_channel_release,
5dbbdb43
MD
2638 .unlocked_ioctl = lttng_metadata_ioctl,
2639#ifdef CONFIG_COMPAT
2640 .compat_ioctl = lttng_metadata_ioctl,
2641#endif
2642};
2643
8070f5c0 2644/**
ef784b4d 2645 * lttng_event_recorder_event_ioctl - lttng syscall through ioctl
8070f5c0
MD
2646 *
2647 * @file: the file
2648 * @cmd: the command
2649 * @arg: command arg
2650 *
2651 * This ioctl implements lttng commands:
606828e4 2652 * LTTNG_KERNEL_ABI_CONTEXT
8070f5c0 2653 * Prepend a context field to each record of this event
606828e4 2654 * LTTNG_KERNEL_ABI_ENABLE
e64957da 2655 * Enable recording for this event (weak enable)
606828e4 2656 * LTTNG_KERNEL_ABI_DISABLE
e64957da 2657 * Disable recording for this event (strong disable)
8070f5c0
MD
2658 */
2659static
ef784b4d 2660long lttng_event_recorder_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
8070f5c0 2661{
e2d5dbc7 2662 struct lttng_kernel_event_recorder *event_recorder = file->private_data;
8070f5c0
MD
2663
2664 switch (cmd) {
606828e4 2665 case LTTNG_KERNEL_ABI_OLD_CONTEXT:
6dccd6c1 2666 {
3c997079
MD
2667 /* Not implemented */
2668 return -ENOSYS;
6dccd6c1 2669 }
606828e4 2670 case LTTNG_KERNEL_ABI_CONTEXT:
6dccd6c1 2671 {
3c997079
MD
2672 /* Not implemented */
2673 return -ENOSYS;
6dccd6c1 2674 }
606828e4
MD
2675 case LTTNG_KERNEL_ABI_OLD_ENABLE:
2676 case LTTNG_KERNEL_ABI_ENABLE:
9d993668 2677 return lttng_event_enable(&event_recorder->parent);
606828e4
MD
2678 case LTTNG_KERNEL_ABI_OLD_DISABLE:
2679 case LTTNG_KERNEL_ABI_DISABLE:
9d993668 2680 return lttng_event_disable(&event_recorder->parent);
606828e4 2681 case LTTNG_KERNEL_ABI_FILTER:
ef784b4d 2682 return -EINVAL;
606828e4 2683 case LTTNG_KERNEL_ABI_ADD_CALLSITE:
e2d5dbc7 2684 return lttng_event_add_callsite(&event_recorder->parent,
ef784b4d 2685 (struct lttng_kernel_abi_event_callsite __user *) arg);
8070f5c0
MD
2686 default:
2687 return -ENOIOCTLCMD;
2688 }
2689}
2690
ef784b4d
MD
2691/**
2692 * lttng_event_recorder_enabler_ioctl - lttng syscall through ioctl
2693 *
2694 * @file: the file
2695 * @cmd: the command
2696 * @arg: command arg
2697 *
2698 * This ioctl implements lttng commands:
2699 * LTTNG_KERNEL_ABI_CONTEXT
2700 * Prepend a context field to each record of this event
2701 * LTTNG_KERNEL_ABI_ENABLE
2702 * Enable recording for this event (weak enable)
2703 * LTTNG_KERNEL_ABI_DISABLE
2704 * Disable recording for this event (strong disable)
2705 */
0a84a57f 2706static
ef784b4d 2707long lttng_event_recorder_enabler_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
0a84a57f 2708{
1ae083ba 2709 struct lttng_event_recorder_enabler *event_enabler = file->private_data;
3c997079 2710
ef784b4d
MD
2711 switch (cmd) {
2712 case LTTNG_KERNEL_ABI_OLD_CONTEXT:
2713 {
2714 /* Not implemented */
2715 return -ENOSYS;
2716 }
2717 case LTTNG_KERNEL_ABI_CONTEXT:
2718 {
2719 /* Not implemented */
2720 return -ENOSYS;
2721 }
2722 case LTTNG_KERNEL_ABI_OLD_ENABLE:
2723 case LTTNG_KERNEL_ABI_ENABLE:
7b25ab0e 2724 return lttng_event_enabler_enable(&event_enabler->parent);
ef784b4d
MD
2725 case LTTNG_KERNEL_ABI_OLD_DISABLE:
2726 case LTTNG_KERNEL_ABI_DISABLE:
7b25ab0e 2727 return lttng_event_enabler_disable(&event_enabler->parent);
ef784b4d 2728 case LTTNG_KERNEL_ABI_FILTER:
6048044f 2729 return lttng_event_enabler_attach_filter_bytecode(&event_enabler->parent,
ef784b4d
MD
2730 (struct lttng_kernel_abi_filter_bytecode __user *) arg);
2731 case LTTNG_KERNEL_ABI_ADD_CALLSITE:
2732 return -EINVAL;
3c997079 2733 default:
ef784b4d 2734 return -ENOIOCTLCMD;
3c997079 2735 }
ef784b4d
MD
2736}
2737
2738static
2739int lttng_event_recorder_event_release(struct inode *inode, struct file *file)
2740{
a67ba386 2741 struct lttng_kernel_event_recorder *event = file->private_data;
ef784b4d
MD
2742
2743 if (event)
f7d06400 2744 fput(event->chan->priv->parent.file);
ef784b4d
MD
2745 return 0;
2746}
c269fff4 2747
ef784b4d
MD
2748static
2749int lttng_event_recorder_enabler_release(struct inode *inode, struct file *file)
2750{
1ae083ba 2751 struct lttng_event_recorder_enabler *event_enabler = file->private_data;
ef784b4d
MD
2752
2753 if (event_enabler)
f7d06400 2754 fput(event_enabler->chan->priv->parent.file);
0a84a57f
MD
2755 return 0;
2756}
2757
ef784b4d
MD
2758static const struct file_operations lttng_event_recorder_event_fops = {
2759 .owner = THIS_MODULE,
2760 .release = lttng_event_recorder_event_release,
2761 .unlocked_ioctl = lttng_event_recorder_event_ioctl,
2762#ifdef CONFIG_COMPAT
2763 .compat_ioctl = lttng_event_recorder_event_ioctl,
2764#endif
2765};
2766
2767static const struct file_operations lttng_event_recorder_enabler_fops = {
a33c9927 2768 .owner = THIS_MODULE,
ef784b4d
MD
2769 .release = lttng_event_recorder_enabler_release,
2770 .unlocked_ioctl = lttng_event_recorder_enabler_ioctl,
8070f5c0 2771#ifdef CONFIG_COMPAT
ef784b4d 2772 .compat_ioctl = lttng_event_recorder_enabler_ioctl,
8070f5c0 2773#endif
11b5a3c2 2774};
0a84a57f 2775
3b731ab1
JD
2776static int put_u64(uint64_t val, unsigned long arg)
2777{
2778 return put_user(val, (uint64_t __user *) arg);
2779}
2780
8b97fd42
MD
2781static int put_u32(uint32_t val, unsigned long arg)
2782{
2783 return put_user(val, (uint32_t __user *) arg);
2784}
2785
ed8d02d6
JD
2786static long lttng_stream_ring_buffer_ioctl(struct file *filp,
2787 unsigned int cmd, unsigned long arg)
2788{
e20c0fec 2789 struct lttng_kernel_ring_buffer *buf = filp->private_data;
860c213b 2790 struct lttng_kernel_ring_buffer_channel *chan = buf->backend.chan;
e20c0fec 2791 const struct lttng_kernel_ring_buffer_config *config = &chan->backend.config;
4a399b76 2792 const struct lttng_kernel_channel_buffer_ops *ops = chan->backend.priv_ops;
3b731ab1
JD
2793 int ret;
2794
2795 if (atomic_read(&chan->record_disabled))
2796 return -EIO;
2797
ed8d02d6 2798 switch (cmd) {
606828e4 2799 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_TIMESTAMP_BEGIN:
3b731ab1
JD
2800 {
2801 uint64_t ts;
2802
4a399b76 2803 ret = ops->priv->timestamp_begin(config, buf, &ts);
3b731ab1
JD
2804 if (ret < 0)
2805 goto error;
2806 return put_u64(ts, arg);
2807 }
606828e4 2808 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_TIMESTAMP_END:
3b731ab1
JD
2809 {
2810 uint64_t ts;
2811
4a399b76 2812 ret = ops->priv->timestamp_end(config, buf, &ts);
3b731ab1
JD
2813 if (ret < 0)
2814 goto error;
2815 return put_u64(ts, arg);
2816 }
606828e4 2817 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_EVENTS_DISCARDED:
3b731ab1
JD
2818 {
2819 uint64_t ed;
2820
4a399b76 2821 ret = ops->priv->events_discarded(config, buf, &ed);
3b731ab1
JD
2822 if (ret < 0)
2823 goto error;
2824 return put_u64(ed, arg);
2825 }
606828e4 2826 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_CONTENT_SIZE:
3b731ab1
JD
2827 {
2828 uint64_t cs;
2829
4a399b76 2830 ret = ops->priv->content_size(config, buf, &cs);
3b731ab1
JD
2831 if (ret < 0)
2832 goto error;
2833 return put_u64(cs, arg);
2834 }
606828e4 2835 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_PACKET_SIZE:
3b731ab1
JD
2836 {
2837 uint64_t ps;
2838
4a399b76 2839 ret = ops->priv->packet_size(config, buf, &ps);
3b731ab1
JD
2840 if (ret < 0)
2841 goto error;
2842 return put_u64(ps, arg);
2843 }
606828e4 2844 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_STREAM_ID:
3b731ab1
JD
2845 {
2846 uint64_t si;
2847
4a399b76 2848 ret = ops->priv->stream_id(config, buf, &si);
3b731ab1
JD
2849 if (ret < 0)
2850 goto error;
2851 return put_u64(si, arg);
2852 }
606828e4 2853 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_CURRENT_TIMESTAMP:
2348ca17
JD
2854 {
2855 uint64_t ts;
2856
4a399b76 2857 ret = ops->priv->current_timestamp(config, buf, &ts);
2348ca17
JD
2858 if (ret < 0)
2859 goto error;
2860 return put_u64(ts, arg);
2861 }
606828e4 2862 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_SEQ_NUM:
5b3cf4f9
JD
2863 {
2864 uint64_t seq;
2865
4a399b76 2866 ret = ops->priv->sequence_number(config, buf, &seq);
5b3cf4f9
JD
2867 if (ret < 0)
2868 goto error;
2869 return put_u64(seq, arg);
2870 }
606828e4 2871 case LTTNG_KERNEL_ABI_RING_BUFFER_INSTANCE_ID:
5594698f
JD
2872 {
2873 uint64_t id;
2874
4a399b76 2875 ret = ops->priv->instance_id(config, buf, &id);
5594698f
JD
2876 if (ret < 0)
2877 goto error;
2878 return put_u64(id, arg);
2879 }
3b731ab1
JD
2880 default:
2881 return lib_ring_buffer_file_operations.unlocked_ioctl(filp,
2882 cmd, arg);
ed8d02d6 2883 }
3b731ab1
JD
2884
2885error:
2886 return -ENOSYS;
ed8d02d6
JD
2887}
2888
2889#ifdef CONFIG_COMPAT
2890static long lttng_stream_ring_buffer_compat_ioctl(struct file *filp,
2891 unsigned int cmd, unsigned long arg)
2892{
e20c0fec 2893 struct lttng_kernel_ring_buffer *buf = filp->private_data;
860c213b 2894 struct lttng_kernel_ring_buffer_channel *chan = buf->backend.chan;
e20c0fec 2895 const struct lttng_kernel_ring_buffer_config *config = &chan->backend.config;
4a399b76 2896 const struct lttng_kernel_channel_buffer_ops *ops = chan->backend.priv_ops;
3b731ab1
JD
2897 int ret;
2898
2899 if (atomic_read(&chan->record_disabled))
2900 return -EIO;
2901
ed8d02d6 2902 switch (cmd) {
606828e4 2903 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN:
3b731ab1
JD
2904 {
2905 uint64_t ts;
2906
4a399b76 2907 ret = ops->priv->timestamp_begin(config, buf, &ts);
3b731ab1
JD
2908 if (ret < 0)
2909 goto error;
2910 return put_u64(ts, arg);
2911 }
606828e4 2912 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_TIMESTAMP_END:
3b731ab1
JD
2913 {
2914 uint64_t ts;
2915
4a399b76 2916 ret = ops->priv->timestamp_end(config, buf, &ts);
3b731ab1
JD
2917 if (ret < 0)
2918 goto error;
2919 return put_u64(ts, arg);
2920 }
606828e4 2921 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED:
3b731ab1
JD
2922 {
2923 uint64_t ed;
2924
4a399b76 2925 ret = ops->priv->events_discarded(config, buf, &ed);
3b731ab1
JD
2926 if (ret < 0)
2927 goto error;
2928 return put_u64(ed, arg);
ed8d02d6 2929 }
606828e4 2930 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_CONTENT_SIZE:
3b731ab1
JD
2931 {
2932 uint64_t cs;
2933
4a399b76 2934 ret = ops->priv->content_size(config, buf, &cs);
3b731ab1
JD
2935 if (ret < 0)
2936 goto error;
2937 return put_u64(cs, arg);
2938 }
606828e4 2939 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_PACKET_SIZE:
3b731ab1
JD
2940 {
2941 uint64_t ps;
2942
4a399b76 2943 ret = ops->priv->packet_size(config, buf, &ps);
3b731ab1
JD
2944 if (ret < 0)
2945 goto error;
2946 return put_u64(ps, arg);
2947 }
606828e4 2948 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_STREAM_ID:
3b731ab1
JD
2949 {
2950 uint64_t si;
2951
4a399b76 2952 ret = ops->priv->stream_id(config, buf, &si);
3b731ab1
JD
2953 if (ret < 0)
2954 goto error;
2955 return put_u64(si, arg);
2956 }
606828e4 2957 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_CURRENT_TIMESTAMP:
2348ca17
JD
2958 {
2959 uint64_t ts;
2960
4a399b76 2961 ret = ops->priv->current_timestamp(config, buf, &ts);
2348ca17
JD
2962 if (ret < 0)
2963 goto error;
2964 return put_u64(ts, arg);
2965 }
606828e4 2966 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_SEQ_NUM:
5b3cf4f9
JD
2967 {
2968 uint64_t seq;
2969
4a399b76 2970 ret = ops->priv->sequence_number(config, buf, &seq);
5b3cf4f9
JD
2971 if (ret < 0)
2972 goto error;
2973 return put_u64(seq, arg);
2974 }
606828e4 2975 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_INSTANCE_ID:
5594698f
JD
2976 {
2977 uint64_t id;
2978
4a399b76 2979 ret = ops->priv->instance_id(config, buf, &id);
5594698f
JD
2980 if (ret < 0)
2981 goto error;
2982 return put_u64(id, arg);
2983 }
3b731ab1
JD
2984 default:
2985 return lib_ring_buffer_file_operations.compat_ioctl(filp,
2986 cmd, arg);
2987 }
2988
2989error:
2990 return -ENOSYS;
ed8d02d6
JD
2991}
2992#endif /* CONFIG_COMPAT */
2993
2994static void lttng_stream_override_ring_buffer_fops(void)
2995{
2996 lttng_stream_ring_buffer_file_operations.owner = THIS_MODULE;
2997 lttng_stream_ring_buffer_file_operations.open =
2998 lib_ring_buffer_file_operations.open;
2999 lttng_stream_ring_buffer_file_operations.release =
3000 lib_ring_buffer_file_operations.release;
3001 lttng_stream_ring_buffer_file_operations.poll =
3002 lib_ring_buffer_file_operations.poll;
3003 lttng_stream_ring_buffer_file_operations.splice_read =
3004 lib_ring_buffer_file_operations.splice_read;
3005 lttng_stream_ring_buffer_file_operations.mmap =
3006 lib_ring_buffer_file_operations.mmap;
3007 lttng_stream_ring_buffer_file_operations.unlocked_ioctl =
3008 lttng_stream_ring_buffer_ioctl;
3009 lttng_stream_ring_buffer_file_operations.llseek =
3010 lib_ring_buffer_file_operations.llseek;
3011#ifdef CONFIG_COMPAT
3012 lttng_stream_ring_buffer_file_operations.compat_ioctl =
3013 lttng_stream_ring_buffer_compat_ioctl;
3014#endif
3015}
3016
80996790 3017int __init lttng_abi_init(void)
baf20995
MD
3018{
3019 int ret = 0;
3020
263b6c88 3021 wrapper_vmalloc_sync_mappings();
2754583e 3022 lttng_clock_ref();
f771eda6
JD
3023
3024 ret = lttng_tp_mempool_init();
3025 if (ret) {
3026 goto error;
3027 }
3028
d29348f7 3029 lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL,
059de147 3030 &lttng_proc_ops, NULL);
2470a237 3031
255e52a4 3032 if (!lttng_proc_dentry) {
5a15f70c 3033 printk(KERN_ERR "LTTng: Error creating control file\n");
baf20995
MD
3034 ret = -ENOMEM;
3035 goto error;
3036 }
ed8d02d6 3037 lttng_stream_override_ring_buffer_fops();
2754583e 3038 return 0;
ed8d02d6 3039
baf20995 3040error:
f771eda6 3041 lttng_tp_mempool_destroy();
2754583e 3042 lttng_clock_unref();
baf20995
MD
3043 return ret;
3044}
3045
e6e65fcd
MD
3046/* No __exit annotation because used by init error path too. */
3047void lttng_abi_exit(void)
baf20995 3048{
f771eda6 3049 lttng_tp_mempool_destroy();
2754583e 3050 lttng_clock_unref();
e6a17f26
MD
3051 if (lttng_proc_dentry)
3052 remove_proc_entry("lttng", NULL);
baf20995 3053}
This page took 0.224425 seconds and 4 git commands to generate.