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