Fix: do not warn on unknown counter ioctl
[lttng-modules.git] / src / lttng-abi.c
CommitLineData
b7cdc182 1/* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
9f36eaed 2 *
e8951e63 3 * lttng-abi.c
baf20995 4 *
e8951e63 5 * LTTng ABI
baf20995 6 *
886d51a3
MD
7 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
baf20995
MD
9 * Mimic system calls for:
10 * - session creation, returns a file descriptor or failure.
ad1c05e1
MD
11 * - channel creation, returns a file descriptor or failure.
12 * - Operates on a session file descriptor
13 * - Takes all channel options as parameters.
14 * - stream get, returns a file descriptor or failure.
15 * - Operates on a channel file descriptor.
16 * - stream notifier get, returns a file descriptor or failure.
17 * - Operates on a channel file descriptor.
18 * - event creation, returns a file descriptor or failure.
19 * - Operates on a channel file descriptor
20 * - Takes an event name as parameter
21 * - Takes an instrumentation source as parameter
22 * - e.g. tracepoints, dynamic_probes...
23 * - Takes instrumentation source specific arguments.
baf20995
MD
24 */
25
11b5a3c2 26#include <linux/module.h>
e6a17f26 27#include <linux/proc_fs.h>
11b5a3c2
MD
28#include <linux/anon_inodes.h>
29#include <linux/file.h>
30#include <linux/uaccess.h>
31#include <linux/slab.h>
abc0446a 32#include <linux/err.h>
263b6c88 33#include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_mappings() */
24591303
MD
34#include <ringbuffer/vfs.h>
35#include <ringbuffer/backend.h>
36#include <ringbuffer/frontend.h>
c570be0d 37#include <wrapper/compiler_attributes.h>
241ae9a8
MD
38#include <wrapper/poll.h>
39#include <wrapper/file.h>
40#include <wrapper/kref.h>
6657edec 41#include <wrapper/barrier.h>
2df37e95
MD
42#include <lttng/string-utils.h>
43#include <lttng/abi.h>
44#include <lttng/abi-old.h>
45#include <lttng/events.h>
92bc1e23 46#include <lttng/events-internal.h>
2df37e95
MD
47#include <lttng/tracer.h>
48#include <lttng/tp-mempool.h>
24591303 49#include <ringbuffer/frontend_types.h>
db2511b4 50#include <ringbuffer/iterator.h>
baf20995
MD
51
52/*
53 * This is LTTng's own personal way to create a system call as an external
80996790 54 * module. We use ioctl() on /proc/lttng.
baf20995
MD
55 */
56
e6a17f26 57static struct proc_dir_entry *lttng_proc_dentry;
059de147 58
5f4c791e 59#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,6,0))
059de147
MJ
60static const struct proc_ops lttng_proc_ops;
61#else
62static const struct file_operations lttng_proc_ops;
63#endif
64
ad1c05e1 65static const struct file_operations lttng_session_fops;
750b05f2 66static const struct file_operations lttng_event_notifier_group_fops;
ad1c05e1 67static const struct file_operations lttng_channel_fops;
5dbbdb43 68static const struct file_operations lttng_metadata_fops;
ef784b4d
MD
69static const struct file_operations lttng_event_recorder_event_fops;
70static const struct file_operations lttng_event_recorder_enabler_fops;
ed8d02d6 71static struct file_operations lttng_stream_ring_buffer_file_operations;
baf20995 72
9616f0bf 73static int put_u64(uint64_t val, unsigned long arg);
8b97fd42 74static int put_u32(uint32_t val, unsigned long arg);
9616f0bf 75
99f52fcc
FD
76static int validate_zeroed_padding(char *p, size_t len)
77{
78 size_t i;
79
80 for (i = 0; i < len; i++) {
81 if (p[i])
82 return -1;
83 }
84 return 0;
85}
86
a33c9927
MD
87/*
88 * Teardown management: opened file descriptors keep a refcount on the module,
89 * so it can only exit when all file descriptors are closed.
90 */
91
ad1c05e1 92static
baf20995
MD
93int lttng_abi_create_session(void)
94{
a40e3229 95 struct lttng_kernel_session *session;
c0e31d2e 96 struct file *session_file;
11b5a3c2 97 int session_fd, ret;
baf20995 98
a90917c3 99 session = lttng_session_create();
baf20995
MD
100 if (!session)
101 return -ENOMEM;
4ac10b76 102 session_fd = lttng_get_unused_fd();
baf20995
MD
103 if (session_fd < 0) {
104 ret = session_fd;
105 goto fd_error;
106 }
c0e31d2e 107 session_file = anon_inode_getfile("[lttng_session]",
ad1c05e1 108 &lttng_session_fops,
baf20995 109 session, O_RDWR);
c0e31d2e
MD
110 if (IS_ERR(session_file)) {
111 ret = PTR_ERR(session_file);
baf20995
MD
112 goto file_error;
113 }
a40e3229 114 session->priv->file = session_file;
c0e31d2e 115 fd_install(session_fd, session_file);
baf20995
MD
116 return session_fd;
117
118file_error:
119 put_unused_fd(session_fd);
120fd_error:
a90917c3 121 lttng_session_destroy(session);
baf20995
MD
122 return ret;
123}
124
21f58fb7
FD
125void event_notifier_send_notification_work_wakeup(struct irq_work *entry)
126{
127 struct lttng_event_notifier_group *event_notifier_group =
128 container_of(entry, struct lttng_event_notifier_group,
129 wakeup_pending);
130 wake_up_interruptible(&event_notifier_group->read_wait);
131}
132
750b05f2
FD
133static
134int lttng_abi_create_event_notifier_group(void)
135{
136 struct lttng_event_notifier_group *event_notifier_group;
137 struct file *event_notifier_group_file;
138 int event_notifier_group_fd, ret;
139
140 event_notifier_group = lttng_event_notifier_group_create();
141 if (!event_notifier_group)
142 return -ENOMEM;
143
144 event_notifier_group_fd = lttng_get_unused_fd();
145 if (event_notifier_group_fd < 0) {
146 ret = event_notifier_group_fd;
147 goto fd_error;
148 }
149 event_notifier_group_file = anon_inode_getfile("[lttng_event_notifier_group]",
150 &lttng_event_notifier_group_fops,
151 event_notifier_group, O_RDWR);
152 if (IS_ERR(event_notifier_group_file)) {
153 ret = PTR_ERR(event_notifier_group_file);
154 goto file_error;
155 }
156
157 event_notifier_group->file = event_notifier_group_file;
21f58fb7
FD
158 init_waitqueue_head(&event_notifier_group->read_wait);
159 init_irq_work(&event_notifier_group->wakeup_pending,
160 event_notifier_send_notification_work_wakeup);
750b05f2
FD
161 fd_install(event_notifier_group_fd, event_notifier_group_file);
162 return event_notifier_group_fd;
163
164file_error:
165 put_unused_fd(event_notifier_group_fd);
166fd_error:
167 lttng_event_notifier_group_destroy(event_notifier_group);
168 return ret;
169}
170
271b6681
MD
171static
172int lttng_abi_tracepoint_list(void)
173{
174 struct file *tracepoint_list_file;
175 int file_fd, ret;
176
4ac10b76 177 file_fd = lttng_get_unused_fd();
271b6681
MD
178 if (file_fd < 0) {
179 ret = file_fd;
180 goto fd_error;
181 }
30f18bf0 182
8ee099b6 183 tracepoint_list_file = anon_inode_getfile("[lttng_tracepoint_list]",
271b6681
MD
184 &lttng_tracepoint_list_fops,
185 NULL, O_RDWR);
186 if (IS_ERR(tracepoint_list_file)) {
187 ret = PTR_ERR(tracepoint_list_file);
188 goto file_error;
189 }
30f18bf0
MD
190 ret = lttng_tracepoint_list_fops.open(NULL, tracepoint_list_file);
191 if (ret < 0)
192 goto open_error;
271b6681
MD
193 fd_install(file_fd, tracepoint_list_file);
194 return file_fd;
195
30f18bf0
MD
196open_error:
197 fput(tracepoint_list_file);
271b6681
MD
198file_error:
199 put_unused_fd(file_fd);
200fd_error:
201 return ret;
202}
203
f127e61e
MD
204#ifndef CONFIG_HAVE_SYSCALL_TRACEPOINTS
205static inline
206int lttng_abi_syscall_list(void)
207{
208 return -ENOSYS;
209}
210#else
211static
212int lttng_abi_syscall_list(void)
213{
214 struct file *syscall_list_file;
215 int file_fd, ret;
216
217 file_fd = lttng_get_unused_fd();
218 if (file_fd < 0) {
219 ret = file_fd;
220 goto fd_error;
221 }
222
223 syscall_list_file = anon_inode_getfile("[lttng_syscall_list]",
224 &lttng_syscall_list_fops,
225 NULL, O_RDWR);
226 if (IS_ERR(syscall_list_file)) {
227 ret = PTR_ERR(syscall_list_file);
228 goto file_error;
229 }
230 ret = lttng_syscall_list_fops.open(NULL, syscall_list_file);
231 if (ret < 0)
232 goto open_error;
233 fd_install(file_fd, syscall_list_file);
f127e61e
MD
234 return file_fd;
235
236open_error:
237 fput(syscall_list_file);
238file_error:
239 put_unused_fd(file_fd);
240fd_error:
241 return ret;
242}
243#endif
244
80c16bcf 245static
606828e4 246void lttng_abi_tracer_version(struct lttng_kernel_abi_tracer_version *v)
80c16bcf 247{
6dccd6c1
JD
248 v->major = LTTNG_MODULES_MAJOR_VERSION;
249 v->minor = LTTNG_MODULES_MINOR_VERSION;
250 v->patchlevel = LTTNG_MODULES_PATCHLEVEL_VERSION;
80c16bcf
MD
251}
252
42cabb80 253static
606828e4 254void lttng_abi_tracer_abi_version(struct lttng_kernel_abi_tracer_abi_version *v)
42cabb80 255{
606828e4
MD
256 v->major = LTTNG_KERNEL_ABI_MAJOR_VERSION;
257 v->minor = LTTNG_KERNEL_ABI_MINOR_VERSION;
42cabb80
MD
258}
259
8070f5c0
MD
260static
261long lttng_abi_add_context(struct file *file,
606828e4 262 struct lttng_kernel_abi_context *context_param,
a40e3229 263 struct lttng_kernel_ctx **ctx, struct lttng_kernel_session *session)
8070f5c0 264{
8070f5c0 265
a40e3229 266 if (session->priv->been_active)
8070f5c0
MD
267 return -EPERM;
268
6dccd6c1 269 switch (context_param->ctx) {
606828e4 270 case LTTNG_KERNEL_ABI_CONTEXT_PID:
8070f5c0 271 return lttng_add_pid_to_ctx(ctx);
606828e4 272 case LTTNG_KERNEL_ABI_CONTEXT_PRIO:
a8ad3613 273 return lttng_add_prio_to_ctx(ctx);
606828e4 274 case LTTNG_KERNEL_ABI_CONTEXT_NICE:
53f1f0ca 275 return lttng_add_nice_to_ctx(ctx);
606828e4 276 case LTTNG_KERNEL_ABI_CONTEXT_VPID:
b64bc438 277 return lttng_add_vpid_to_ctx(ctx);
606828e4 278 case LTTNG_KERNEL_ABI_CONTEXT_TID:
b64bc438 279 return lttng_add_tid_to_ctx(ctx);
606828e4 280 case LTTNG_KERNEL_ABI_CONTEXT_VTID:
b64bc438 281 return lttng_add_vtid_to_ctx(ctx);
606828e4 282 case LTTNG_KERNEL_ABI_CONTEXT_PPID:
b64bc438 283 return lttng_add_ppid_to_ctx(ctx);
606828e4 284 case LTTNG_KERNEL_ABI_CONTEXT_VPPID:
b64bc438 285 return lttng_add_vppid_to_ctx(ctx);
606828e4
MD
286 case LTTNG_KERNEL_ABI_CONTEXT_PERF_COUNTER:
287 context_param->u.perf_counter.name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0';
6dccd6c1
JD
288 return lttng_add_perf_counter_to_ctx(context_param->u.perf_counter.type,
289 context_param->u.perf_counter.config,
290 context_param->u.perf_counter.name,
c24a0d71 291 ctx);
606828e4 292 case LTTNG_KERNEL_ABI_CONTEXT_PROCNAME:
a2563e83 293 return lttng_add_procname_to_ctx(ctx);
606828e4 294 case LTTNG_KERNEL_ABI_CONTEXT_HOSTNAME:
975da2c0 295 return lttng_add_hostname_to_ctx(ctx);
606828e4 296 case LTTNG_KERNEL_ABI_CONTEXT_CPU_ID:
b3699d90 297 return lttng_add_cpu_id_to_ctx(ctx);
606828e4 298 case LTTNG_KERNEL_ABI_CONTEXT_INTERRUPTIBLE:
79150a49 299 return lttng_add_interruptible_to_ctx(ctx);
606828e4 300 case LTTNG_KERNEL_ABI_CONTEXT_NEED_RESCHEDULE:
79150a49 301 return lttng_add_need_reschedule_to_ctx(ctx);
606828e4 302 case LTTNG_KERNEL_ABI_CONTEXT_PREEMPTIBLE:
79150a49 303 return lttng_add_preemptible_to_ctx(ctx);
606828e4 304 case LTTNG_KERNEL_ABI_CONTEXT_MIGRATABLE:
79150a49 305 return lttng_add_migratable_to_ctx(ctx);
606828e4
MD
306 case LTTNG_KERNEL_ABI_CONTEXT_CALLSTACK_KERNEL:
307 case LTTNG_KERNEL_ABI_CONTEXT_CALLSTACK_USER:
2fa2d39a 308 return lttng_add_callstack_to_ctx(ctx, context_param->ctx);
606828e4 309 case LTTNG_KERNEL_ABI_CONTEXT_CGROUP_NS:
a6cf40a4 310 return lttng_add_cgroup_ns_to_ctx(ctx);
606828e4 311 case LTTNG_KERNEL_ABI_CONTEXT_IPC_NS:
a6cf40a4 312 return lttng_add_ipc_ns_to_ctx(ctx);
606828e4 313 case LTTNG_KERNEL_ABI_CONTEXT_MNT_NS:
a6cf40a4 314 return lttng_add_mnt_ns_to_ctx(ctx);
606828e4 315 case LTTNG_KERNEL_ABI_CONTEXT_NET_NS:
a6cf40a4 316 return lttng_add_net_ns_to_ctx(ctx);
606828e4 317 case LTTNG_KERNEL_ABI_CONTEXT_PID_NS:
a6cf40a4 318 return lttng_add_pid_ns_to_ctx(ctx);
606828e4 319 case LTTNG_KERNEL_ABI_CONTEXT_USER_NS:
a6cf40a4 320 return lttng_add_user_ns_to_ctx(ctx);
606828e4 321 case LTTNG_KERNEL_ABI_CONTEXT_UTS_NS:
a6cf40a4 322 return lttng_add_uts_ns_to_ctx(ctx);
606828e4 323 case LTTNG_KERNEL_ABI_CONTEXT_UID:
dc923e75 324 return lttng_add_uid_to_ctx(ctx);
606828e4 325 case LTTNG_KERNEL_ABI_CONTEXT_EUID:
dc923e75 326 return lttng_add_euid_to_ctx(ctx);
606828e4 327 case LTTNG_KERNEL_ABI_CONTEXT_SUID:
dc923e75 328 return lttng_add_suid_to_ctx(ctx);
606828e4 329 case LTTNG_KERNEL_ABI_CONTEXT_GID:
dc923e75 330 return lttng_add_gid_to_ctx(ctx);
606828e4 331 case LTTNG_KERNEL_ABI_CONTEXT_EGID:
dc923e75 332 return lttng_add_egid_to_ctx(ctx);
606828e4 333 case LTTNG_KERNEL_ABI_CONTEXT_SGID:
dc923e75 334 return lttng_add_sgid_to_ctx(ctx);
606828e4 335 case LTTNG_KERNEL_ABI_CONTEXT_VUID:
dc923e75 336 return lttng_add_vuid_to_ctx(ctx);
606828e4 337 case LTTNG_KERNEL_ABI_CONTEXT_VEUID:
dc923e75 338 return lttng_add_veuid_to_ctx(ctx);
606828e4 339 case LTTNG_KERNEL_ABI_CONTEXT_VSUID:
dc923e75 340 return lttng_add_vsuid_to_ctx(ctx);
606828e4 341 case LTTNG_KERNEL_ABI_CONTEXT_VGID:
dc923e75 342 return lttng_add_vgid_to_ctx(ctx);
606828e4 343 case LTTNG_KERNEL_ABI_CONTEXT_VEGID:
dc923e75 344 return lttng_add_vegid_to_ctx(ctx);
606828e4 345 case LTTNG_KERNEL_ABI_CONTEXT_VSGID:
dc923e75 346 return lttng_add_vsgid_to_ctx(ctx);
606828e4 347 case LTTNG_KERNEL_ABI_CONTEXT_TIME_NS:
876e2e92 348 return lttng_add_time_ns_to_ctx(ctx);
8070f5c0
MD
349 default:
350 return -EINVAL;
351 }
352}
353
ad1c05e1
MD
354/**
355 * lttng_ioctl - lttng syscall through ioctl
356 *
c0e31d2e 357 * @file: the file
ad1c05e1
MD
358 * @cmd: the command
359 * @arg: command arg
360 *
361 * This ioctl implements lttng commands:
606828e4 362 * LTTNG_KERNEL_ABI_SESSION
ad1c05e1 363 * Returns a LTTng trace session file descriptor
606828e4 364 * LTTNG_KERNEL_ABI_TRACER_VERSION
271b6681 365 * Returns the LTTng kernel tracer version
606828e4 366 * LTTNG_KERNEL_ABI_TRACEPOINT_LIST
271b6681 367 * Returns a file descriptor listing available tracepoints
606828e4 368 * LTTNG_KERNEL_ABI_WAIT_QUIESCENT
360f38ea 369 * Returns after all previously running probes have completed
606828e4 370 * LTTNG_KERNEL_ABI_TRACER_ABI_VERSION
42cabb80 371 * Returns the LTTng kernel tracer ABI version
606828e4 372 * LTTNG_KERNEL_ABI_EVENT_NOTIFIER_GROUP_CREATE
750b05f2 373 * Returns a LTTng event notifier group file descriptor
ad1c05e1
MD
374 *
375 * The returned session will be deleted when its file descriptor is closed.
376 */
377static
c0e31d2e 378long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
ad1c05e1
MD
379{
380 switch (cmd) {
606828e4
MD
381 case LTTNG_KERNEL_ABI_OLD_SESSION:
382 case LTTNG_KERNEL_ABI_SESSION:
ad1c05e1 383 return lttng_abi_create_session();
606828e4 384 case LTTNG_KERNEL_ABI_EVENT_NOTIFIER_GROUP_CREATE:
750b05f2 385 return lttng_abi_create_event_notifier_group();
606828e4 386 case LTTNG_KERNEL_ABI_OLD_TRACER_VERSION:
6dccd6c1 387 {
606828e4
MD
388 struct lttng_kernel_abi_tracer_version v;
389 struct lttng_kernel_abi_old_tracer_version oldv;
390 struct lttng_kernel_abi_old_tracer_version *uversion =
391 (struct lttng_kernel_abi_old_tracer_version __user *) arg;
6dccd6c1
JD
392
393 lttng_abi_tracer_version(&v);
394 oldv.major = v.major;
395 oldv.minor = v.minor;
396 oldv.patchlevel = v.patchlevel;
397
398 if (copy_to_user(uversion, &oldv, sizeof(oldv)))
399 return -EFAULT;
400 return 0;
401 }
606828e4 402 case LTTNG_KERNEL_ABI_TRACER_VERSION:
6dccd6c1 403 {
606828e4
MD
404 struct lttng_kernel_abi_tracer_version version;
405 struct lttng_kernel_abi_tracer_version *uversion =
406 (struct lttng_kernel_abi_tracer_version __user *) arg;
6dccd6c1
JD
407
408 lttng_abi_tracer_version(&version);
42cabb80
MD
409
410 if (copy_to_user(uversion, &version, sizeof(version)))
411 return -EFAULT;
412 return 0;
413 }
606828e4 414 case LTTNG_KERNEL_ABI_TRACER_ABI_VERSION:
42cabb80 415 {
606828e4
MD
416 struct lttng_kernel_abi_tracer_abi_version version;
417 struct lttng_kernel_abi_tracer_abi_version *uversion =
418 (struct lttng_kernel_abi_tracer_abi_version __user *) arg;
42cabb80
MD
419
420 lttng_abi_tracer_abi_version(&version);
421
6dccd6c1
JD
422 if (copy_to_user(uversion, &version, sizeof(version)))
423 return -EFAULT;
424 return 0;
425 }
606828e4
MD
426 case LTTNG_KERNEL_ABI_OLD_TRACEPOINT_LIST:
427 case LTTNG_KERNEL_ABI_TRACEPOINT_LIST:
271b6681 428 return lttng_abi_tracepoint_list();
606828e4 429 case LTTNG_KERNEL_ABI_SYSCALL_LIST:
2d2464bd 430 return lttng_abi_syscall_list();
606828e4
MD
431 case LTTNG_KERNEL_ABI_OLD_WAIT_QUIESCENT:
432 case LTTNG_KERNEL_ABI_WAIT_QUIESCENT:
5f7f9078
MD
433 synchronize_trace();
434 return 0;
606828e4 435 case LTTNG_KERNEL_ABI_OLD_CALIBRATE:
6dccd6c1 436 {
606828e4
MD
437 struct lttng_kernel_abi_old_calibrate __user *ucalibrate =
438 (struct lttng_kernel_abi_old_calibrate __user *) arg;
439 struct lttng_kernel_abi_old_calibrate old_calibrate;
440 struct lttng_kernel_abi_calibrate calibrate;
6dccd6c1
JD
441 int ret;
442
443 if (copy_from_user(&old_calibrate, ucalibrate, sizeof(old_calibrate)))
444 return -EFAULT;
445 calibrate.type = old_calibrate.type;
446 ret = lttng_calibrate(&calibrate);
447 if (copy_to_user(ucalibrate, &old_calibrate, sizeof(old_calibrate)))
448 return -EFAULT;
449 return ret;
450 }
606828e4 451 case LTTNG_KERNEL_ABI_CALIBRATE:
57105fc2 452 {
606828e4
MD
453 struct lttng_kernel_abi_calibrate __user *ucalibrate =
454 (struct lttng_kernel_abi_calibrate __user *) arg;
455 struct lttng_kernel_abi_calibrate calibrate;
57105fc2
MD
456 int ret;
457
458 if (copy_from_user(&calibrate, ucalibrate, sizeof(calibrate)))
459 return -EFAULT;
460 ret = lttng_calibrate(&calibrate);
461 if (copy_to_user(ucalibrate, &calibrate, sizeof(calibrate)))
462 return -EFAULT;
463 return ret;
464 }
ad1c05e1
MD
465 default:
466 return -ENOIOCTLCMD;
467 }
468}
469
5f4c791e 470#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,6,0))
059de147
MJ
471static const struct proc_ops lttng_proc_ops = {
472 .proc_ioctl = lttng_ioctl,
473#ifdef CONFIG_COMPAT
474 .proc_compat_ioctl = lttng_ioctl,
475#endif /* CONFIG_COMPAT */
476};
477#else
478static const struct file_operations lttng_proc_ops = {
a33c9927 479 .owner = THIS_MODULE,
ad1c05e1
MD
480 .unlocked_ioctl = lttng_ioctl,
481#ifdef CONFIG_COMPAT
03037b98 482 .compat_ioctl = lttng_ioctl,
059de147 483#endif /* CONFIG_COMPAT */
11b5a3c2 484};
059de147 485#endif
ad1c05e1 486
5dbbdb43 487static
c0e31d2e 488int lttng_abi_create_channel(struct file *session_file,
606828e4 489 struct lttng_kernel_abi_channel *chan_param,
5dbbdb43 490 enum channel_type channel_type)
baf20995 491{
a40e3229 492 struct lttng_kernel_session *session = session_file->private_data;
88dfd899 493 const struct file_operations *fops = NULL;
5dbbdb43 494 const char *transport_name;
ac847066 495 struct lttng_kernel_channel_buffer *chan;
c0e31d2e 496 struct file *chan_file;
baf20995 497 int chan_fd;
ad1c05e1 498 int ret = 0;
baf20995 499
4ac10b76 500 chan_fd = lttng_get_unused_fd();
baf20995
MD
501 if (chan_fd < 0) {
502 ret = chan_fd;
503 goto fd_error;
504 }
88dfd899
MD
505 switch (channel_type) {
506 case PER_CPU_CHANNEL:
507 fops = &lttng_channel_fops;
508 break;
509 case METADATA_CHANNEL:
510 fops = &lttng_metadata_fops;
511 break;
512 }
2470a237 513
c0e31d2e 514 chan_file = anon_inode_getfile("[lttng_channel]",
88dfd899 515 fops,
03037b98 516 NULL, O_RDWR);
c0e31d2e
MD
517 if (IS_ERR(chan_file)) {
518 ret = PTR_ERR(chan_file);
baf20995
MD
519 goto file_error;
520 }
5dbbdb43
MD
521 switch (channel_type) {
522 case PER_CPU_CHANNEL:
606828e4 523 if (chan_param->output == LTTNG_KERNEL_ABI_SPLICE) {
6dccd6c1 524 transport_name = chan_param->overwrite ?
96ba7208 525 "relay-overwrite" : "relay-discard";
606828e4 526 } else if (chan_param->output == LTTNG_KERNEL_ABI_MMAP) {
6dccd6c1 527 transport_name = chan_param->overwrite ?
96ba7208
JD
528 "relay-overwrite-mmap" : "relay-discard-mmap";
529 } else {
530 return -EINVAL;
531 }
5dbbdb43 532 break;
5dbbdb43 533 case METADATA_CHANNEL:
606828e4 534 if (chan_param->output == LTTNG_KERNEL_ABI_SPLICE)
96ba7208 535 transport_name = "relay-metadata";
606828e4 536 else if (chan_param->output == LTTNG_KERNEL_ABI_MMAP)
96ba7208
JD
537 transport_name = "relay-metadata-mmap";
538 else
539 return -EINVAL;
5dbbdb43
MD
540 break;
541 default:
542 transport_name = "<unknown>";
543 break;
544 }
98d7281c
MJ
545 if (!atomic_long_add_unless(&session_file->f_count, 1, LONG_MAX)) {
546 ret = -EOVERFLOW;
9c1f4643
MD
547 goto refcount_error;
548 }
03037b98
MD
549 /*
550 * We tolerate no failure path after channel creation. It will stay
551 * invariant for the rest of the session.
552 */
a90917c3 553 chan = lttng_channel_create(session, transport_name, NULL,
6dccd6c1
JD
554 chan_param->subbuf_size,
555 chan_param->num_subbuf,
556 chan_param->switch_timer_interval,
d83004aa
JD
557 chan_param->read_timer_interval,
558 channel_type);
03037b98 559 if (!chan) {
f3d01b96 560 ret = -EINVAL;
03037b98
MD
561 goto chan_error;
562 }
ac847066 563 chan->priv->parent.file = chan_file;
c0e31d2e
MD
564 chan_file->private_data = chan;
565 fd_install(chan_fd, chan_file);
ad1c05e1 566
baf20995
MD
567 return chan_fd;
568
03037b98 569chan_error:
9c1f4643
MD
570 atomic_long_dec(&session_file->f_count);
571refcount_error:
c0e31d2e 572 fput(chan_file);
baf20995
MD
573file_error:
574 put_unused_fd(chan_fd);
575fd_error:
baf20995
MD
576 return ret;
577}
578
7f859fbf 579static
a40e3229 580int lttng_abi_session_set_name(struct lttng_kernel_session *session,
606828e4 581 struct lttng_kernel_abi_session_name *name)
7f859fbf
JR
582{
583 size_t len;
584
606828e4 585 len = strnlen(name->name, LTTNG_KERNEL_ABI_SESSION_NAME_LEN);
7f859fbf 586
606828e4 587 if (len == LTTNG_KERNEL_ABI_SESSION_NAME_LEN) {
7f859fbf
JR
588 /* Name is too long/malformed */
589 return -EINVAL;
590 }
591
a40e3229 592 strcpy(session->priv->name, name->name);
7f859fbf
JR
593 return 0;
594}
595
1c88f269 596static
a40e3229 597int lttng_abi_session_set_creation_time(struct lttng_kernel_session *session,
606828e4 598 struct lttng_kernel_abi_session_creation_time *time)
1c88f269
JR
599{
600 size_t len;
601
606828e4 602 len = strnlen(time->iso8601, LTTNG_KERNEL_ABI_SESSION_CREATION_TIME_ISO8601_LEN);
1c88f269 603
606828e4 604 if (len == LTTNG_KERNEL_ABI_SESSION_CREATION_TIME_ISO8601_LEN) {
1c88f269
JR
605 /* Time is too long/malformed */
606 return -EINVAL;
607 }
608
a40e3229 609 strcpy(session->priv->creation_time, time->iso8601);
1c88f269
JR
610 return 0;
611}
612
99f52fcc
FD
613static
614int lttng_counter_release(struct inode *inode, struct file *file)
615{
616 struct lttng_counter *counter = file->private_data;
617
618 if (counter) {
619 /*
620 * Do not destroy the counter itself. Wait of the owner
621 * (event_notifier group) to be destroyed.
622 */
623 fput(counter->owner);
624 }
625
626 return 0;
627}
628
629static
630long lttng_counter_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
631{
632 struct lttng_counter *counter = file->private_data;
606828e4 633 size_t indexes[LTTNG_KERNEL_ABI_COUNTER_DIMENSION_MAX] = { 0 };
99f52fcc
FD
634 int i;
635
636 switch (cmd) {
606828e4 637 case LTTNG_KERNEL_ABI_COUNTER_READ:
99f52fcc 638 {
606828e4
MD
639 struct lttng_kernel_abi_counter_read local_counter_read;
640 struct lttng_kernel_abi_counter_read __user *ucounter_read =
641 (struct lttng_kernel_abi_counter_read __user *) arg;
99f52fcc
FD
642 bool overflow, underflow;
643 int64_t value;
644 int32_t cpu;
645 int ret;
646
647 if (copy_from_user(&local_counter_read, ucounter_read,
648 sizeof(local_counter_read)))
649 return -EFAULT;
650 if (validate_zeroed_padding(local_counter_read.padding,
651 sizeof(local_counter_read.padding)))
652 return -EINVAL;
7d42b129
MD
653 if (local_counter_read.index.number_dimensions > LTTNG_KERNEL_ABI_COUNTER_DIMENSION_MAX)
654 return -EINVAL;
99f52fcc
FD
655
656 /* Cast all indexes into size_t. */
657 for (i = 0; i < local_counter_read.index.number_dimensions; i++)
658 indexes[i] = (size_t) local_counter_read.index.dimension_indexes[i];
659 cpu = local_counter_read.cpu;
660
661 ret = lttng_kernel_counter_read(counter, indexes, cpu, &value,
662 &overflow, &underflow);
663 if (ret)
664 return ret;
665 local_counter_read.value.value = value;
666 local_counter_read.value.overflow = overflow;
667 local_counter_read.value.underflow = underflow;
668
669 if (copy_to_user(&ucounter_read->value, &local_counter_read.value,
670 sizeof(local_counter_read.value)))
671 return -EFAULT;
672
673 return 0;
674 }
606828e4 675 case LTTNG_KERNEL_ABI_COUNTER_AGGREGATE:
99f52fcc 676 {
606828e4
MD
677 struct lttng_kernel_abi_counter_aggregate local_counter_aggregate;
678 struct lttng_kernel_abi_counter_aggregate __user *ucounter_aggregate =
679 (struct lttng_kernel_abi_counter_aggregate __user *) arg;
99f52fcc
FD
680 bool overflow, underflow;
681 int64_t value;
682 int ret;
683
684 if (copy_from_user(&local_counter_aggregate, ucounter_aggregate,
685 sizeof(local_counter_aggregate)))
686 return -EFAULT;
687 if (validate_zeroed_padding(local_counter_aggregate.padding,
688 sizeof(local_counter_aggregate.padding)))
689 return -EINVAL;
7d42b129
MD
690 if (local_counter_aggregate.index.number_dimensions > LTTNG_KERNEL_ABI_COUNTER_DIMENSION_MAX)
691 return -EINVAL;
99f52fcc
FD
692
693 /* Cast all indexes into size_t. */
694 for (i = 0; i < local_counter_aggregate.index.number_dimensions; i++)
695 indexes[i] = (size_t) local_counter_aggregate.index.dimension_indexes[i];
696
697 ret = lttng_kernel_counter_aggregate(counter, indexes, &value,
698 &overflow, &underflow);
699 if (ret)
700 return ret;
701 local_counter_aggregate.value.value = value;
702 local_counter_aggregate.value.overflow = overflow;
703 local_counter_aggregate.value.underflow = underflow;
704
705 if (copy_to_user(&ucounter_aggregate->value, &local_counter_aggregate.value,
706 sizeof(local_counter_aggregate.value)))
707 return -EFAULT;
708
709 return 0;
710 }
606828e4 711 case LTTNG_KERNEL_ABI_COUNTER_CLEAR:
99f52fcc 712 {
606828e4
MD
713 struct lttng_kernel_abi_counter_clear local_counter_clear;
714 struct lttng_kernel_abi_counter_clear __user *ucounter_clear =
715 (struct lttng_kernel_abi_counter_clear __user *) arg;
99f52fcc
FD
716
717 if (copy_from_user(&local_counter_clear, ucounter_clear,
718 sizeof(local_counter_clear)))
719 return -EFAULT;
720 if (validate_zeroed_padding(local_counter_clear.padding,
721 sizeof(local_counter_clear.padding)))
722 return -EINVAL;
7d42b129
MD
723 if (local_counter_clear.index.number_dimensions > LTTNG_KERNEL_ABI_COUNTER_DIMENSION_MAX)
724 return -EINVAL;
99f52fcc
FD
725
726 /* Cast all indexes into size_t. */
727 for (i = 0; i < local_counter_clear.index.number_dimensions; i++)
728 indexes[i] = (size_t) local_counter_clear.index.dimension_indexes[i];
729
730 return lttng_kernel_counter_clear(counter, indexes);
731 }
732 default:
99f52fcc
FD
733 return -ENOSYS;
734 }
735}
736
737static const struct file_operations lttng_counter_fops = {
738 .owner = THIS_MODULE,
739 .release = lttng_counter_release,
740 .unlocked_ioctl = lttng_counter_ioctl,
741#ifdef CONFIG_COMPAT
742 .compat_ioctl = lttng_counter_ioctl,
743#endif
744};
745
746
d1f652f8 747static
606828e4 748enum tracker_type get_tracker_type(struct lttng_kernel_abi_tracker_args *tracker)
d1f652f8
MD
749{
750 switch (tracker->type) {
606828e4 751 case LTTNG_KERNEL_ABI_TRACKER_PID:
d1f652f8 752 return TRACKER_PID;
606828e4 753 case LTTNG_KERNEL_ABI_TRACKER_VPID:
d1f652f8 754 return TRACKER_VPID;
606828e4 755 case LTTNG_KERNEL_ABI_TRACKER_UID:
d1f652f8 756 return TRACKER_UID;
606828e4 757 case LTTNG_KERNEL_ABI_TRACKER_VUID:
d1f652f8 758 return TRACKER_VUID;
606828e4 759 case LTTNG_KERNEL_ABI_TRACKER_GID:
d1f652f8 760 return TRACKER_GID;
606828e4 761 case LTTNG_KERNEL_ABI_TRACKER_VGID:
d1f652f8
MD
762 return TRACKER_VGID;
763 default:
764 return TRACKER_UNKNOWN;
765 }
766}
767
baf20995 768/**
ad1c05e1 769 * lttng_session_ioctl - lttng session fd ioctl
baf20995 770 *
c0e31d2e 771 * @file: the file
baf20995
MD
772 * @cmd: the command
773 * @arg: command arg
774 *
775 * This ioctl implements lttng commands:
606828e4 776 * LTTNG_KERNEL_ABI_CHANNEL
baf20995 777 * Returns a LTTng channel file descriptor
606828e4 778 * LTTNG_KERNEL_ABI_ENABLE
e64957da 779 * Enables tracing for a session (weak enable)
606828e4 780 * LTTNG_KERNEL_ABI_DISABLE
e64957da 781 * Disables tracing for a session (strong disable)
606828e4 782 * LTTNG_KERNEL_ABI_METADATA
8070f5c0 783 * Returns a LTTng metadata file descriptor
606828e4 784 * LTTNG_KERNEL_ABI_SESSION_TRACK_PID
d1f652f8 785 * Add PID to session PID tracker
606828e4 786 * LTTNG_KERNEL_ABI_SESSION_UNTRACK_PID
d1f652f8 787 * Remove PID from session PID tracker
606828e4 788 * LTTNG_KERNEL_ABI_SESSION_TRACK_ID
d1f652f8 789 * Add ID to tracker
606828e4 790 * LTTNG_KERNEL_ABI_SESSION_UNTRACK_ID
d1f652f8 791 * Remove ID from tracker
ad1c05e1
MD
792 *
793 * The returned channel will be deleted when its file descriptor is closed.
794 */
795static
c0e31d2e 796long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
ad1c05e1 797{
a40e3229 798 struct lttng_kernel_session *session = file->private_data;
606828e4
MD
799 struct lttng_kernel_abi_channel chan_param;
800 struct lttng_kernel_abi_old_channel old_chan_param;
c0e31d2e 801
8c71721f
MD
802 /*
803 * Handle backward compatibility. OLD commands have wrong
804 * directions, replace them by the correct direction.
805 */
806 switch (cmd) {
807 case LTTNG_KERNEL_ABI_OLD_SESSION_TRACK_PID:
808 cmd = LTTNG_KERNEL_ABI_SESSION_TRACK_PID;
809 break;
810 case LTTNG_KERNEL_ABI_OLD_SESSION_UNTRACK_PID:
811 cmd = LTTNG_KERNEL_ABI_SESSION_UNTRACK_PID;
812 break;
813 case LTTNG_KERNEL_ABI_OLD_SESSION_TRACK_ID:
814 cmd = LTTNG_KERNEL_ABI_SESSION_TRACK_ID;
815 break;
816 case LTTNG_KERNEL_ABI_OLD_SESSION_UNTRACK_ID:
817 cmd = LTTNG_KERNEL_ABI_SESSION_UNTRACK_ID;
818 break;
819 case LTTNG_KERNEL_ABI_OLD_SESSION_LIST_TRACKER_IDS:
820 cmd = LTTNG_KERNEL_ABI_SESSION_LIST_TRACKER_IDS;
821 break;
822 case LTTNG_KERNEL_ABI_OLD_SESSION_SET_NAME:
823 cmd = LTTNG_KERNEL_ABI_SESSION_SET_NAME;
824 break;
825 case LTTNG_KERNEL_ABI_OLD_SESSION_SET_CREATION_TIME:
826 cmd = LTTNG_KERNEL_ABI_SESSION_SET_CREATION_TIME;
827 break;
828 default:
829 /* Nothing to do. */
830 break;
831 }
832
ad1c05e1 833 switch (cmd) {
606828e4 834 case LTTNG_KERNEL_ABI_OLD_CHANNEL:
6dccd6c1 835 {
6dccd6c1 836 if (copy_from_user(&old_chan_param,
606828e4
MD
837 (struct lttng_kernel_abi_old_channel __user *) arg,
838 sizeof(struct lttng_kernel_abi_old_channel)))
6dccd6c1
JD
839 return -EFAULT;
840 chan_param.overwrite = old_chan_param.overwrite;
841 chan_param.subbuf_size = old_chan_param.subbuf_size;
842 chan_param.num_subbuf = old_chan_param.num_subbuf;
843 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
844 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
845 chan_param.output = old_chan_param.output;
846
847 return lttng_abi_create_channel(file, &chan_param,
848 PER_CPU_CHANNEL);
849 }
606828e4 850 case LTTNG_KERNEL_ABI_CHANNEL:
6dccd6c1 851 {
6dccd6c1 852 if (copy_from_user(&chan_param,
606828e4
MD
853 (struct lttng_kernel_abi_channel __user *) arg,
854 sizeof(struct lttng_kernel_abi_channel)))
6dccd6c1
JD
855 return -EFAULT;
856 return lttng_abi_create_channel(file, &chan_param,
5dbbdb43 857 PER_CPU_CHANNEL);
6dccd6c1 858 }
606828e4
MD
859 case LTTNG_KERNEL_ABI_OLD_SESSION_START:
860 case LTTNG_KERNEL_ABI_OLD_ENABLE:
861 case LTTNG_KERNEL_ABI_SESSION_START:
862 case LTTNG_KERNEL_ABI_ENABLE:
a90917c3 863 return lttng_session_enable(session);
606828e4
MD
864 case LTTNG_KERNEL_ABI_OLD_SESSION_STOP:
865 case LTTNG_KERNEL_ABI_OLD_DISABLE:
866 case LTTNG_KERNEL_ABI_SESSION_STOP:
867 case LTTNG_KERNEL_ABI_DISABLE:
a90917c3 868 return lttng_session_disable(session);
606828e4 869 case LTTNG_KERNEL_ABI_OLD_METADATA:
6dccd6c1 870 {
6dccd6c1 871 if (copy_from_user(&old_chan_param,
606828e4
MD
872 (struct lttng_kernel_abi_old_channel __user *) arg,
873 sizeof(struct lttng_kernel_abi_old_channel)))
6dccd6c1
JD
874 return -EFAULT;
875 chan_param.overwrite = old_chan_param.overwrite;
876 chan_param.subbuf_size = old_chan_param.subbuf_size;
877 chan_param.num_subbuf = old_chan_param.num_subbuf;
878 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
879 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
880 chan_param.output = old_chan_param.output;
881
882 return lttng_abi_create_channel(file, &chan_param,
883 METADATA_CHANNEL);
884 }
606828e4 885 case LTTNG_KERNEL_ABI_METADATA:
6dccd6c1 886 {
6dccd6c1 887 if (copy_from_user(&chan_param,
606828e4
MD
888 (struct lttng_kernel_abi_channel __user *) arg,
889 sizeof(struct lttng_kernel_abi_channel)))
6dccd6c1
JD
890 return -EFAULT;
891 return lttng_abi_create_channel(file, &chan_param,
5dbbdb43 892 METADATA_CHANNEL);
6dccd6c1 893 }
606828e4 894 case LTTNG_KERNEL_ABI_SESSION_TRACK_PID:
d1f652f8 895 return lttng_session_track_id(session, TRACKER_PID, (int) arg);
606828e4 896 case LTTNG_KERNEL_ABI_SESSION_UNTRACK_PID:
d1f652f8 897 return lttng_session_untrack_id(session, TRACKER_PID, (int) arg);
606828e4 898 case LTTNG_KERNEL_ABI_SESSION_TRACK_ID:
d1f652f8 899 {
606828e4 900 struct lttng_kernel_abi_tracker_args tracker;
d1f652f8
MD
901 enum tracker_type tracker_type;
902
903 if (copy_from_user(&tracker,
606828e4
MD
904 (struct lttng_kernel_abi_tracker_args __user *) arg,
905 sizeof(struct lttng_kernel_abi_tracker_args)))
d1f652f8
MD
906 return -EFAULT;
907 tracker_type = get_tracker_type(&tracker);
908 if (tracker_type == TRACKER_UNKNOWN)
909 return -EINVAL;
910 return lttng_session_track_id(session, tracker_type, tracker.id);
911 }
606828e4 912 case LTTNG_KERNEL_ABI_SESSION_UNTRACK_ID:
d1f652f8 913 {
606828e4 914 struct lttng_kernel_abi_tracker_args tracker;
d1f652f8
MD
915 enum tracker_type tracker_type;
916
917 if (copy_from_user(&tracker,
606828e4
MD
918 (struct lttng_kernel_abi_tracker_args __user *) arg,
919 sizeof(struct lttng_kernel_abi_tracker_args)))
d1f652f8
MD
920 return -EFAULT;
921 tracker_type = get_tracker_type(&tracker);
922 if (tracker_type == TRACKER_UNKNOWN)
923 return -EINVAL;
924 return lttng_session_untrack_id(session, tracker_type,
925 tracker.id);
926 }
606828e4 927 case LTTNG_KERNEL_ABI_SESSION_LIST_TRACKER_PIDS:
d1f652f8 928 return lttng_session_list_tracker_ids(session, TRACKER_PID);
606828e4 929 case LTTNG_KERNEL_ABI_SESSION_LIST_TRACKER_IDS:
d1f652f8 930 {
606828e4 931 struct lttng_kernel_abi_tracker_args tracker;
d1f652f8
MD
932 enum tracker_type tracker_type;
933
934 if (copy_from_user(&tracker,
606828e4
MD
935 (struct lttng_kernel_abi_tracker_args __user *) arg,
936 sizeof(struct lttng_kernel_abi_tracker_args)))
d1f652f8
MD
937 return -EFAULT;
938 tracker_type = get_tracker_type(&tracker);
939 if (tracker_type == TRACKER_UNKNOWN)
940 return -EINVAL;
941 return lttng_session_list_tracker_ids(session, tracker_type);
942 }
606828e4 943 case LTTNG_KERNEL_ABI_SESSION_METADATA_REGEN:
9616f0bf 944 return lttng_session_metadata_regenerate(session);
606828e4 945 case LTTNG_KERNEL_ABI_SESSION_STATEDUMP:
601252cf 946 return lttng_session_statedump(session);
606828e4 947 case LTTNG_KERNEL_ABI_SESSION_SET_NAME:
7f859fbf 948 {
606828e4 949 struct lttng_kernel_abi_session_name name;
7f859fbf
JR
950
951 if (copy_from_user(&name,
606828e4
MD
952 (struct lttng_kernel_abi_session_name __user *) arg,
953 sizeof(struct lttng_kernel_abi_session_name)))
7f859fbf
JR
954 return -EFAULT;
955 return lttng_abi_session_set_name(session, &name);
956 }
606828e4 957 case LTTNG_KERNEL_ABI_SESSION_SET_CREATION_TIME:
1c88f269 958 {
606828e4 959 struct lttng_kernel_abi_session_creation_time time;
1c88f269
JR
960
961 if (copy_from_user(&time,
606828e4
MD
962 (struct lttng_kernel_abi_session_creation_time __user *) arg,
963 sizeof(struct lttng_kernel_abi_session_creation_time)))
1c88f269
JR
964 return -EFAULT;
965 return lttng_abi_session_set_creation_time(session, &time);
966 }
ad1c05e1
MD
967 default:
968 return -ENOIOCTLCMD;
969 }
970}
971
03037b98
MD
972/*
973 * Called when the last file reference is dropped.
974 *
975 * Big fat note: channels and events are invariant for the whole session after
976 * their creation. So this session destruction also destroys all channel and
977 * event structures specific to this session (they are not destroyed when their
978 * individual file is released).
979 */
ad1c05e1 980static
03037b98 981int lttng_session_release(struct inode *inode, struct file *file)
ad1c05e1 982{
a40e3229 983 struct lttng_kernel_session *session = file->private_data;
c269fff4
MD
984
985 if (session)
a90917c3 986 lttng_session_destroy(session);
11b5a3c2 987 return 0;
ad1c05e1 988}
ad1c05e1
MD
989
990static const struct file_operations lttng_session_fops = {
a33c9927 991 .owner = THIS_MODULE,
03037b98 992 .release = lttng_session_release,
ad1c05e1
MD
993 .unlocked_ioctl = lttng_session_ioctl,
994#ifdef CONFIG_COMPAT
03037b98 995 .compat_ioctl = lttng_session_ioctl,
ad1c05e1 996#endif
11b5a3c2 997};
ad1c05e1 998
db2511b4
MD
999/*
1000 * When encountering empty buffer, flush current sub-buffer if non-empty
1001 * and retry (if new data available to read after flush).
1002 */
1003static
1004ssize_t lttng_event_notifier_group_notif_read(struct file *filp, char __user *user_buf,
1005 size_t count, loff_t *ppos)
1006{
1007 struct lttng_event_notifier_group *event_notifier_group = filp->private_data;
4071a628 1008 struct lttng_kernel_ring_buffer_channel *chan = event_notifier_group->chan;
a2a1b5ab 1009 struct lttng_kernel_ring_buffer *buf = event_notifier_group->buf;
db2511b4
MD
1010 ssize_t read_count = 0, len;
1011 size_t read_offset;
1012
1013 might_sleep();
1014 if (!lttng_access_ok(VERIFY_WRITE, user_buf, count))
1015 return -EFAULT;
1016
1017 /* Finish copy of previous record */
1018 if (*ppos != 0) {
1019 if (read_count < count) {
1020 len = chan->iter.len_left;
1021 read_offset = *ppos;
1022 goto skip_get_next;
1023 }
1024 }
1025
1026 while (read_count < count) {
1027 size_t copy_len, space_left;
1028
1029 len = lib_ring_buffer_get_next_record(chan, buf);
1030len_test:
1031 if (len < 0) {
1032 /*
1033 * Check if buffer is finalized (end of file).
1034 */
1035 if (len == -ENODATA) {
1036 /* A 0 read_count will tell about end of file */
1037 goto nodata;
1038 }
1039 if (filp->f_flags & O_NONBLOCK) {
1040 if (!read_count)
1041 read_count = -EAGAIN;
1042 goto nodata;
1043 } else {
1044 int error;
1045
1046 /*
1047 * No data available at the moment, return what
1048 * we got.
1049 */
1050 if (read_count)
1051 goto nodata;
1052
1053 /*
1054 * Wait for returned len to be >= 0 or -ENODATA.
1055 */
1056 error = wait_event_interruptible(
1057 event_notifier_group->read_wait,
1058 ((len = lib_ring_buffer_get_next_record(
1059 chan, buf)), len != -EAGAIN));
1060 CHAN_WARN_ON(chan, len == -EBUSY);
1061 if (error) {
1062 read_count = error;
1063 goto nodata;
1064 }
1065 CHAN_WARN_ON(chan, len < 0 && len != -ENODATA);
1066 goto len_test;
1067 }
1068 }
1069 read_offset = buf->iter.read_offset;
1070skip_get_next:
1071 space_left = count - read_count;
1072 if (len <= space_left) {
1073 copy_len = len;
1074 chan->iter.len_left = 0;
1075 *ppos = 0;
1076 } else {
1077 copy_len = space_left;
1078 chan->iter.len_left = len - copy_len;
1079 *ppos = read_offset + copy_len;
1080 }
1081 if (__lib_ring_buffer_copy_to_user(&buf->backend, read_offset,
1082 &user_buf[read_count],
1083 copy_len)) {
1084 /*
1085 * Leave the len_left and ppos values at their current
1086 * state, as we currently have a valid event to read.
1087 */
1088 return -EFAULT;
1089 }
1090 read_count += copy_len;
1091 }
31c02fb7 1092 goto put_record;
db2511b4
MD
1093
1094nodata:
1095 *ppos = 0;
1096 chan->iter.len_left = 0;
31c02fb7
MD
1097
1098put_record:
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;
4071a628 1114 struct lttng_kernel_ring_buffer_channel *chan = event_notifier_group->chan;
a2a1b5ab
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;
a2a1b5ab 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;
a2a1b5ab 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;
a2a1b5ab 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;
a2a1b5ab 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;
a2a1b5ab 1323 struct lttng_kernel_ring_buffer *buf = stream->priv;
4071a628 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 }
c570be0d
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;
a2a1b5ab 1346 struct lttng_kernel_ring_buffer *buf = stream->priv;
4071a628 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;
a2a1b5ab 1373 struct lttng_kernel_ring_buffer *buf = stream->priv;
4071a628 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;
a2a1b5ab 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;
a2a1b5ab 1433 struct lttng_kernel_ring_buffer *buf = stream->priv;
4071a628 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 }
c570be0d
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;
a2a1b5ab 1456 struct lttng_kernel_ring_buffer *buf = stream->priv;
4071a628 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;
a2a1b5ab 1483 struct lttng_kernel_ring_buffer *buf = stream->priv;
4071a628 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;
a2a1b5ab 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;
a2a1b5ab 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;
a2a1b5ab 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;
a2a1b5ab 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
4ac10b76 1605 stream_fd = lttng_get_unused_fd();
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{
ac847066 1638 struct lttng_kernel_channel_buffer *channel = channel_file->private_data;
a2a1b5ab 1639 struct lttng_kernel_ring_buffer *buf;
d83004aa
JD
1640 int ret;
1641 void *stream_priv;
1642
ac847066 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:
da1fe18f 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{
ac847066
MD
1664 struct lttng_kernel_channel_buffer *channel = channel_file->private_data;
1665 struct lttng_kernel_session *session = channel->parent.session;
a2a1b5ab 1666 struct lttng_kernel_ring_buffer *buf;
d83004aa
JD
1667 int ret;
1668 struct lttng_metadata_stream *metadata_stream;
1669 void *stream_priv;
1670
ac847066 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 }
a40e3229 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;
ac847066 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
a40e3229 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
a40e3229 1710 mutex_lock(&session->priv->metadata_cache->lock);
d83004aa 1711 list_add(&metadata_stream->list,
a40e3229
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:
a40e3229 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:
da1fe18f 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;
4071a628 1731 struct lttng_kernel_ring_buffer_channel *chan = event_notifier_group->chan;
a2a1b5ab 1732 struct lttng_kernel_ring_buffer *buf;
21f58fb7
FD
1733 int ret;
1734 void *stream_priv;
1735
da1fe18f 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:
da1fe18f 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) {
c570be0d
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;
c570be0d
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
c570be0d
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
c570be0d
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;
ac847066 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) {
c570be0d
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;
c570be0d
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
c570be0d
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
33a39a3c
MD
1870 event_fd = lttng_get_unused_fd();
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) {
c570be0d
MJ
1891 case LTTNG_KERNEL_ABI_TRACEPOINT:
1892 lttng_fallthrough;
606828e4 1893 case LTTNG_KERNEL_ABI_SYSCALL:
684a1e4d 1894 {
b2bc0bc8 1895 struct lttng_event_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 */
b2bc0bc8 1902 event_enabler = lttng_event_enabler_create(LTTNG_ENABLER_FORMAT_STAR_GLOB,
33a39a3c 1903 event_param, channel);
3c997079 1904 } else {
b2bc0bc8 1905 event_enabler = lttng_event_enabler_create(LTTNG_ENABLER_FORMAT_NAME,
33a39a3c 1906 event_param, channel);
1ec65de1 1907 }
b2bc0bc8 1908 priv = event_enabler;
684a1e4d
MD
1909 break;
1910 }
1911
c570be0d
MJ
1912 case LTTNG_KERNEL_ABI_KPROBE:
1913 lttng_fallthrough;
1914 case LTTNG_KERNEL_ABI_KRETPROBE:
1915 lttng_fallthrough;
606828e4 1916 case LTTNG_KERNEL_ABI_UPROBE:
684a1e4d 1917 {
a67ba386 1918 struct lttng_kernel_event_recorder *event;
4ee2453d 1919
33a39a3c
MD
1920 /*
1921 * We tolerate no failure path after event creation. It
1922 * will stay invariant for the rest of the session.
1923 */
a67ba386
MD
1924 event = lttng_kernel_event_recorder_create(channel, event_param,
1925 NULL, event_param->instrumentation);
33a39a3c
MD
1926 WARN_ON_ONCE(!event);
1927 if (IS_ERR(event)) {
1928 ret = PTR_ERR(event);
1929 goto event_error;
80f87dd2 1930 }
33a39a3c 1931 priv = event;
684a1e4d
MD
1932 break;
1933 }
1934
c570be0d
MJ
1935 case LTTNG_KERNEL_ABI_FUNCTION:
1936 lttng_fallthrough;
1937 case LTTNG_KERNEL_ABI_NOOP:
1938 lttng_fallthrough;
684a1e4d
MD
1939 default:
1940 ret = -EINVAL;
1941 goto event_error;
03037b98 1942 }
33a39a3c
MD
1943 event_file->private_data = priv;
1944 fd_install(event_fd, event_file);
653fe716
MD
1945 return event_fd;
1946
03037b98 1947event_error:
9c1f4643
MD
1948 atomic_long_dec(&channel_file->f_count);
1949refcount_error:
c0e31d2e 1950 fput(event_file);
653fe716
MD
1951file_error:
1952 put_unused_fd(event_fd);
1953fd_error:
653fe716
MD
1954 return ret;
1955}
ad1c05e1 1956
dffef45d 1957static
ef784b4d 1958long lttng_event_notifier_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
dffef45d 1959{
a67ba386 1960 struct lttng_kernel_event_notifier *event_notifier = file->private_data;
dffef45d
FD
1961
1962 switch (cmd) {
606828e4 1963 case LTTNG_KERNEL_ABI_ENABLE:
9d993668 1964 return lttng_event_enable(&event_notifier->parent);
606828e4 1965 case LTTNG_KERNEL_ABI_DISABLE:
9d993668 1966 return lttng_event_disable(&event_notifier->parent);
606828e4 1967 case LTTNG_KERNEL_ABI_FILTER:
ef784b4d 1968 return -EINVAL;
606828e4 1969 case LTTNG_KERNEL_ABI_CAPTURE:
ef784b4d 1970 return -EINVAL;
606828e4 1971 case LTTNG_KERNEL_ABI_ADD_CALLSITE:
e2d5dbc7 1972 return lttng_event_add_callsite(&event_notifier->parent,
ef784b4d 1973 (struct lttng_kernel_abi_event_callsite __user *) arg);
dffef45d
FD
1974 default:
1975 return -ENOIOCTLCMD;
1976 }
1977}
1978
1979static
ef784b4d 1980long lttng_event_notifier_enabler_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
dffef45d 1981{
ef784b4d 1982 struct lttng_event_notifier_enabler *event_notifier_enabler = file->private_data;
dffef45d 1983
ef784b4d
MD
1984 switch (cmd) {
1985 case LTTNG_KERNEL_ABI_ENABLE:
1986 return lttng_event_notifier_enabler_enable(event_notifier_enabler);
1987 case LTTNG_KERNEL_ABI_DISABLE:
1988 return lttng_event_notifier_enabler_disable(event_notifier_enabler);
1989 case LTTNG_KERNEL_ABI_FILTER:
1990 return lttng_event_notifier_enabler_attach_filter_bytecode(
1991 event_notifier_enabler,
1992 (struct lttng_kernel_abi_filter_bytecode __user *) arg);
1993 case LTTNG_KERNEL_ABI_CAPTURE:
1994 return lttng_event_notifier_enabler_attach_capture_bytecode(
1995 event_notifier_enabler,
1996 (struct lttng_kernel_abi_capture_bytecode __user *) arg);
1997 case LTTNG_KERNEL_ABI_ADD_CALLSITE:
1998 return -EINVAL;
dffef45d 1999 default:
ef784b4d 2000 return -ENOIOCTLCMD;
dffef45d 2001 }
ef784b4d 2002}
dffef45d 2003
ef784b4d
MD
2004static
2005int lttng_event_notifier_event_release(struct inode *inode, struct file *file)
2006{
a67ba386 2007 struct lttng_kernel_event_notifier *event_notifier = file->private_data;
ef784b4d
MD
2008
2009 if (event_notifier)
a67ba386 2010 fput(event_notifier->priv->group->file);
dffef45d
FD
2011 return 0;
2012}
2013
ef784b4d
MD
2014static
2015int lttng_event_notifier_enabler_release(struct inode *inode, struct file *file)
2016{
2017 struct lttng_event_notifier_enabler *event_notifier_enabler = file->private_data;
2018
2019 if (event_notifier_enabler)
2020 fput(event_notifier_enabler->group->file);
2021 return 0;
2022}
2023
2024static const struct file_operations lttng_event_notifier_event_fops = {
2025 .owner = THIS_MODULE,
2026 .release = lttng_event_notifier_event_release,
2027 .unlocked_ioctl = lttng_event_notifier_event_ioctl,
2028#ifdef CONFIG_COMPAT
2029 .compat_ioctl = lttng_event_notifier_event_ioctl,
2030#endif
2031};
2032
2033static const struct file_operations lttng_event_notifier_enabler_fops = {
dffef45d 2034 .owner = THIS_MODULE,
ef784b4d
MD
2035 .release = lttng_event_notifier_enabler_release,
2036 .unlocked_ioctl = lttng_event_notifier_enabler_ioctl,
dffef45d 2037#ifdef CONFIG_COMPAT
ef784b4d 2038 .compat_ioctl = lttng_event_notifier_enabler_ioctl,
dffef45d
FD
2039#endif
2040};
2041
2042static
2043int lttng_abi_create_event_notifier(struct file *event_notifier_group_file,
606828e4 2044 struct lttng_kernel_abi_event_notifier *event_notifier_param)
dffef45d
FD
2045{
2046 struct lttng_event_notifier_group *event_notifier_group =
2047 event_notifier_group_file->private_data;
ef784b4d 2048 const struct file_operations *fops;
dffef45d
FD
2049 int event_notifier_fd, ret;
2050 struct file *event_notifier_file;
2051 void *priv;
2052
2053 switch (event_notifier_param->event.instrumentation) {
606828e4
MD
2054 case LTTNG_KERNEL_ABI_TRACEPOINT:
2055 case LTTNG_KERNEL_ABI_UPROBE:
b01155ba 2056 break;
606828e4
MD
2057 case LTTNG_KERNEL_ABI_KPROBE:
2058 event_notifier_param->event.u.kprobe.symbol_name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0';
2b16f0c9 2059 break;
606828e4 2060 case LTTNG_KERNEL_ABI_SYSCALL:
8a8ac9a8 2061 break;
606828e4 2062 case LTTNG_KERNEL_ABI_KRETPROBE:
9de67196 2063 /* Placing an event notifier on kretprobe is not supported. */
606828e4
MD
2064 case LTTNG_KERNEL_ABI_FUNCTION:
2065 case LTTNG_KERNEL_ABI_NOOP:
dffef45d
FD
2066 default:
2067 ret = -EINVAL;
2068 goto inval_instr;
2069 }
2070
ef784b4d 2071 switch (event_notifier_param->event.instrumentation) {
c570be0d
MJ
2072 case LTTNG_KERNEL_ABI_TRACEPOINT:
2073 lttng_fallthrough;
ef784b4d
MD
2074 case LTTNG_KERNEL_ABI_SYSCALL:
2075 fops = &lttng_event_notifier_enabler_fops;
2076 break;
c570be0d
MJ
2077 case LTTNG_KERNEL_ABI_KPROBE:
2078 lttng_fallthrough;
2079 case LTTNG_KERNEL_ABI_KRETPROBE:
2080 lttng_fallthrough;
ef784b4d
MD
2081 case LTTNG_KERNEL_ABI_UPROBE:
2082 fops = &lttng_event_notifier_event_fops;
2083 break;
2084
c570be0d
MJ
2085 case LTTNG_KERNEL_ABI_FUNCTION:
2086 lttng_fallthrough;
2087 case LTTNG_KERNEL_ABI_NOOP:
2088 lttng_fallthrough;
ef784b4d
MD
2089 default:
2090 ret = -EINVAL;
2091 goto inval_instr;
2092 }
2093
606828e4 2094 event_notifier_param->event.name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0';
dffef45d
FD
2095
2096 event_notifier_fd = lttng_get_unused_fd();
2097 if (event_notifier_fd < 0) {
2098 ret = event_notifier_fd;
2099 goto fd_error;
2100 }
2101
2102 event_notifier_file = anon_inode_getfile("[lttng_event_notifier]",
ef784b4d 2103 fops, NULL, O_RDWR);
dffef45d
FD
2104 if (IS_ERR(event_notifier_file)) {
2105 ret = PTR_ERR(event_notifier_file);
2106 goto file_error;
2107 }
2108
2109 /* The event notifier holds a reference on the event notifier group. */
2110 if (!atomic_long_add_unless(&event_notifier_group_file->f_count, 1, LONG_MAX)) {
2111 ret = -EOVERFLOW;
2112 goto refcount_error;
2113 }
2114
56237619
MD
2115 ret = lttng_abi_validate_event_param(&event_notifier_param->event);
2116 if (ret)
2117 goto event_notifier_error;
2118
684a1e4d 2119 switch (event_notifier_param->event.instrumentation) {
c570be0d
MJ
2120 case LTTNG_KERNEL_ABI_TRACEPOINT:
2121 lttng_fallthrough;
606828e4 2122 case LTTNG_KERNEL_ABI_SYSCALL:
684a1e4d 2123 {
dffef45d
FD
2124 struct lttng_event_notifier_enabler *enabler;
2125
2126 if (strutils_is_star_glob_pattern(event_notifier_param->event.name)) {
2127 /*
2128 * If the event name is a star globbing pattern,
2129 * we create the special star globbing enabler.
2130 */
2131 enabler = lttng_event_notifier_enabler_create(
2132 event_notifier_group,
2133 LTTNG_ENABLER_FORMAT_STAR_GLOB,
2134 event_notifier_param);
2135 } else {
2136 enabler = lttng_event_notifier_enabler_create(
2137 event_notifier_group,
2138 LTTNG_ENABLER_FORMAT_NAME,
2139 event_notifier_param);
2140 }
2141 priv = enabler;
684a1e4d
MD
2142 break;
2143 }
2144
c570be0d
MJ
2145 case LTTNG_KERNEL_ABI_KPROBE:
2146 lttng_fallthrough;
2147 case LTTNG_KERNEL_ABI_KRETPROBE:
2148 lttng_fallthrough;
606828e4 2149 case LTTNG_KERNEL_ABI_UPROBE:
684a1e4d 2150 {
a67ba386 2151 struct lttng_kernel_event_notifier *event_notifier;
dffef45d
FD
2152
2153 /*
2154 * We tolerate no failure path after event notifier creation.
2155 * It will stay invariant for the rest of the session.
2156 */
2157 event_notifier = lttng_event_notifier_create(NULL,
99f52fcc
FD
2158 event_notifier_param->event.token,
2159 event_notifier_param->error_counter_index,
2160 event_notifier_group,
a67ba386 2161 event_notifier_param,
dffef45d
FD
2162 event_notifier_param->event.instrumentation);
2163 WARN_ON_ONCE(!event_notifier);
2164 if (IS_ERR(event_notifier)) {
2165 ret = PTR_ERR(event_notifier);
2166 goto event_notifier_error;
2167 }
2168 priv = event_notifier;
684a1e4d
MD
2169 break;
2170 }
2171
c570be0d
MJ
2172 case LTTNG_KERNEL_ABI_FUNCTION:
2173 lttng_fallthrough;
2174 case LTTNG_KERNEL_ABI_NOOP:
2175 lttng_fallthrough;
684a1e4d
MD
2176 default:
2177 ret = -EINVAL;
2178 goto event_notifier_error;
dffef45d
FD
2179 }
2180 event_notifier_file->private_data = priv;
2181 fd_install(event_notifier_fd, event_notifier_file);
2182 return event_notifier_fd;
2183
2184event_notifier_error:
2185 atomic_long_dec(&event_notifier_group_file->f_count);
2186refcount_error:
2187 fput(event_notifier_file);
2188file_error:
2189 put_unused_fd(event_notifier_fd);
2190fd_error:
2191inval_instr:
2192 return ret;
2193}
2194
99f52fcc
FD
2195static
2196long lttng_abi_event_notifier_group_create_error_counter(
2197 struct file *event_notifier_group_file,
606828e4 2198 const struct lttng_kernel_abi_counter_conf *error_counter_conf)
99f52fcc
FD
2199{
2200 int counter_fd, ret;
2201 char *counter_transport_name;
2202 size_t counter_len;
2203 struct lttng_counter *counter = NULL;
2204 struct file *counter_file;
2205 struct lttng_event_notifier_group *event_notifier_group =
2206 (struct lttng_event_notifier_group *) event_notifier_group_file->private_data;
2207
606828e4 2208 if (error_counter_conf->arithmetic != LTTNG_KERNEL_ABI_COUNTER_ARITHMETIC_MODULAR) {
99f52fcc
FD
2209 printk(KERN_ERR "LTTng: event_notifier: Error counter of the wrong arithmetic type.\n");
2210 return -EINVAL;
2211 }
2212
2213 if (error_counter_conf->number_dimensions != 1) {
2214 printk(KERN_ERR "LTTng: event_notifier: Error counter has more than one dimension.\n");
2215 return -EINVAL;
2216 }
2217
2218 switch (error_counter_conf->bitness) {
606828e4 2219 case LTTNG_KERNEL_ABI_COUNTER_BITNESS_64:
99f52fcc
FD
2220 counter_transport_name = "counter-per-cpu-64-modular";
2221 break;
606828e4 2222 case LTTNG_KERNEL_ABI_COUNTER_BITNESS_32:
99f52fcc
FD
2223 counter_transport_name = "counter-per-cpu-32-modular";
2224 break;
2225 default:
2226 return -EINVAL;
2227 }
2228
2229 /*
2230 * Lock sessions to provide mutual exclusion against concurrent
2231 * modification of event_notifier group, which would result in
2232 * overwriting the error counter if set concurrently.
2233 */
2234 lttng_lock_sessions();
2235
2236 if (event_notifier_group->error_counter) {
2237 printk(KERN_ERR "Error counter already created in event_notifier group\n");
2238 ret = -EBUSY;
2239 goto fd_error;
2240 }
2241
2242 counter_fd = lttng_get_unused_fd();
2243 if (counter_fd < 0) {
2244 ret = counter_fd;
2245 goto fd_error;
2246 }
2247
2248 counter_file = anon_inode_getfile("[lttng_counter]",
2249 &lttng_counter_fops,
2250 NULL, O_RDONLY);
2251 if (IS_ERR(counter_file)) {
2252 ret = PTR_ERR(counter_file);
2253 goto file_error;
2254 }
2255
2256 counter_len = error_counter_conf->dimensions[0].size;
2257
2258 if (!atomic_long_add_unless(&event_notifier_group_file->f_count, 1, LONG_MAX)) {
2259 ret = -EOVERFLOW;
2260 goto refcount_error;
2261 }
2262
2263 counter = lttng_kernel_counter_create(counter_transport_name,
2264 1, &counter_len);
2265 if (!counter) {
2266 ret = -EINVAL;
2267 goto counter_error;
2268 }
2269
99f52fcc 2270 event_notifier_group->error_counter_len = counter_len;
ab04d7b1
MD
2271 /*
2272 * store-release to publish error counter matches load-acquire
2273 * in record_error. Ensures the counter is created and the
2274 * error_counter_len is set before they are used.
2275 */
6657edec 2276 lttng_smp_store_release(&event_notifier_group->error_counter, counter);
99f52fcc
FD
2277
2278 counter->file = counter_file;
2279 counter->owner = event_notifier_group->file;
2280 counter_file->private_data = counter;
2281 /* Ownership transferred. */
2282 counter = NULL;
2283
2284 fd_install(counter_fd, counter_file);
2285 lttng_unlock_sessions();
2286
2287 return counter_fd;
2288
2289counter_error:
2290 atomic_long_dec(&event_notifier_group_file->f_count);
2291refcount_error:
2292 fput(counter_file);
2293file_error:
2294 put_unused_fd(counter_fd);
2295fd_error:
2296 lttng_unlock_sessions();
2297 return ret;
2298}
2299
750b05f2
FD
2300static
2301long lttng_event_notifier_group_ioctl(struct file *file, unsigned int cmd,
2302 unsigned long arg)
2303{
2304 switch (cmd) {
606828e4 2305 case LTTNG_KERNEL_ABI_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD:
21f58fb7
FD
2306 {
2307 return lttng_abi_open_event_notifier_group_stream(file);
2308 }
606828e4 2309 case LTTNG_KERNEL_ABI_EVENT_NOTIFIER_CREATE:
dffef45d 2310 {
606828e4 2311 struct lttng_kernel_abi_event_notifier uevent_notifier_param;
dffef45d
FD
2312
2313 if (copy_from_user(&uevent_notifier_param,
606828e4 2314 (struct lttng_kernel_abi_event_notifier __user *) arg,
dffef45d
FD
2315 sizeof(uevent_notifier_param)))
2316 return -EFAULT;
2317 return lttng_abi_create_event_notifier(file, &uevent_notifier_param);
2318 }
606828e4 2319 case LTTNG_KERNEL_ABI_COUNTER:
99f52fcc 2320 {
606828e4 2321 struct lttng_kernel_abi_counter_conf uerror_counter_conf;
99f52fcc
FD
2322
2323 if (copy_from_user(&uerror_counter_conf,
606828e4 2324 (struct lttng_kernel_abi_counter_conf __user *) arg,
99f52fcc
FD
2325 sizeof(uerror_counter_conf)))
2326 return -EFAULT;
2327 return lttng_abi_event_notifier_group_create_error_counter(file,
2328 &uerror_counter_conf);
2329 }
750b05f2
FD
2330 default:
2331 return -ENOIOCTLCMD;
2332 }
2333 return 0;
2334}
2335
2336static
2337int lttng_event_notifier_group_release(struct inode *inode, struct file *file)
2338{
2339 struct lttng_event_notifier_group *event_notifier_group =
2340 file->private_data;
2341
2342 if (event_notifier_group)
2343 lttng_event_notifier_group_destroy(event_notifier_group);
2344 return 0;
2345}
2346
2347static const struct file_operations lttng_event_notifier_group_fops = {
2348 .owner = THIS_MODULE,
2349 .release = lttng_event_notifier_group_release,
2350 .unlocked_ioctl = lttng_event_notifier_group_ioctl,
2351#ifdef CONFIG_COMPAT
2352 .compat_ioctl = lttng_event_notifier_group_ioctl,
2353#endif
2354};
2355
ad1c05e1
MD
2356/**
2357 * lttng_channel_ioctl - lttng syscall through ioctl
2358 *
c0e31d2e 2359 * @file: the file
ad1c05e1
MD
2360 * @cmd: the command
2361 * @arg: command arg
2362 *
2363 * This ioctl implements lttng commands:
606828e4 2364 * LTTNG_KERNEL_ABI_STREAM
ad1c05e1
MD
2365 * Returns an event stream file descriptor or failure.
2366 * (typically, one event stream records events from one CPU)
606828e4 2367 * LTTNG_KERNEL_ABI_EVENT
ad1c05e1 2368 * Returns an event file descriptor or failure.
606828e4 2369 * LTTNG_KERNEL_ABI_CONTEXT
8070f5c0 2370 * Prepend a context field to each event in the channel
606828e4 2371 * LTTNG_KERNEL_ABI_ENABLE
e64957da 2372 * Enable recording for events in this channel (weak enable)
606828e4 2373 * LTTNG_KERNEL_ABI_DISABLE
e64957da 2374 * Disable recording for events in this channel (strong disable)
baf20995 2375 *
baf20995
MD
2376 * Channel and event file descriptors also hold a reference on the session.
2377 */
ad1c05e1 2378static
c0e31d2e 2379long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
baf20995 2380{
ac847066 2381 struct lttng_kernel_channel_buffer *channel = file->private_data;
8070f5c0 2382
baf20995 2383 switch (cmd) {
606828e4
MD
2384 case LTTNG_KERNEL_ABI_OLD_STREAM:
2385 case LTTNG_KERNEL_ABI_STREAM:
c0e31d2e 2386 return lttng_abi_open_stream(file);
606828e4 2387 case LTTNG_KERNEL_ABI_OLD_EVENT:
6dccd6c1 2388 {
606828e4
MD
2389 struct lttng_kernel_abi_event *uevent_param;
2390 struct lttng_kernel_abi_old_event *old_uevent_param;
6dccd6c1
JD
2391 int ret;
2392
606828e4 2393 uevent_param = kmalloc(sizeof(struct lttng_kernel_abi_event),
6dccd6c1
JD
2394 GFP_KERNEL);
2395 if (!uevent_param) {
2396 ret = -ENOMEM;
2397 goto old_event_end;
2398 }
2399 old_uevent_param = kmalloc(
606828e4 2400 sizeof(struct lttng_kernel_abi_old_event),
6dccd6c1
JD
2401 GFP_KERNEL);
2402 if (!old_uevent_param) {
2403 ret = -ENOMEM;
2404 goto old_event_error_free_param;
2405 }
2406 if (copy_from_user(old_uevent_param,
606828e4
MD
2407 (struct lttng_kernel_abi_old_event __user *) arg,
2408 sizeof(struct lttng_kernel_abi_old_event))) {
6dccd6c1
JD
2409 ret = -EFAULT;
2410 goto old_event_error_free_old_param;
2411 }
2412
2413 memcpy(uevent_param->name, old_uevent_param->name,
2414 sizeof(uevent_param->name));
2415 uevent_param->instrumentation =
2416 old_uevent_param->instrumentation;
2417
2418 switch (old_uevent_param->instrumentation) {
606828e4 2419 case LTTNG_KERNEL_ABI_KPROBE:
6dccd6c1
JD
2420 uevent_param->u.kprobe.addr =
2421 old_uevent_param->u.kprobe.addr;
2422 uevent_param->u.kprobe.offset =
2423 old_uevent_param->u.kprobe.offset;
2424 memcpy(uevent_param->u.kprobe.symbol_name,
2425 old_uevent_param->u.kprobe.symbol_name,
2426 sizeof(uevent_param->u.kprobe.symbol_name));
2427 break;
606828e4 2428 case LTTNG_KERNEL_ABI_KRETPROBE:
6dccd6c1
JD
2429 uevent_param->u.kretprobe.addr =
2430 old_uevent_param->u.kretprobe.addr;
2431 uevent_param->u.kretprobe.offset =
2432 old_uevent_param->u.kretprobe.offset;
2433 memcpy(uevent_param->u.kretprobe.symbol_name,
2434 old_uevent_param->u.kretprobe.symbol_name,
2435 sizeof(uevent_param->u.kretprobe.symbol_name));
2436 break;
606828e4 2437 case LTTNG_KERNEL_ABI_FUNCTION:
e884017c
MD
2438 WARN_ON_ONCE(1);
2439 /* Not implemented. */
6dccd6c1
JD
2440 break;
2441 default:
2442 break;
2443 }
2444 ret = lttng_abi_create_event(file, uevent_param);
2445
2446old_event_error_free_old_param:
2447 kfree(old_uevent_param);
2448old_event_error_free_param:
2449 kfree(uevent_param);
2450old_event_end:
2451 return ret;
2452 }
606828e4 2453 case LTTNG_KERNEL_ABI_EVENT:
6dccd6c1 2454 {
606828e4 2455 struct lttng_kernel_abi_event uevent_param;
6dccd6c1
JD
2456
2457 if (copy_from_user(&uevent_param,
606828e4 2458 (struct lttng_kernel_abi_event __user *) arg,
6dccd6c1
JD
2459 sizeof(uevent_param)))
2460 return -EFAULT;
2461 return lttng_abi_create_event(file, &uevent_param);
2462 }
606828e4 2463 case LTTNG_KERNEL_ABI_OLD_CONTEXT:
6dccd6c1 2464 {
606828e4
MD
2465 struct lttng_kernel_abi_context *ucontext_param;
2466 struct lttng_kernel_abi_old_context *old_ucontext_param;
6dccd6c1
JD
2467 int ret;
2468
606828e4 2469 ucontext_param = kmalloc(sizeof(struct lttng_kernel_abi_context),
6dccd6c1
JD
2470 GFP_KERNEL);
2471 if (!ucontext_param) {
2472 ret = -ENOMEM;
2473 goto old_ctx_end;
2474 }
606828e4 2475 old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_abi_old_context),
6dccd6c1
JD
2476 GFP_KERNEL);
2477 if (!old_ucontext_param) {
2478 ret = -ENOMEM;
2479 goto old_ctx_error_free_param;
2480 }
2481
2482 if (copy_from_user(old_ucontext_param,
606828e4
MD
2483 (struct lttng_kernel_abi_old_context __user *) arg,
2484 sizeof(struct lttng_kernel_abi_old_context))) {
6dccd6c1
JD
2485 ret = -EFAULT;
2486 goto old_ctx_error_free_old_param;
2487 }
2488 ucontext_param->ctx = old_ucontext_param->ctx;
2489 memcpy(ucontext_param->padding, old_ucontext_param->padding,
2490 sizeof(ucontext_param->padding));
2491 /* only type that uses the union */
606828e4 2492 if (old_ucontext_param->ctx == LTTNG_KERNEL_ABI_CONTEXT_PERF_COUNTER) {
6dccd6c1
JD
2493 ucontext_param->u.perf_counter.type =
2494 old_ucontext_param->u.perf_counter.type;
2495 ucontext_param->u.perf_counter.config =
2496 old_ucontext_param->u.perf_counter.config;
2497 memcpy(ucontext_param->u.perf_counter.name,
2498 old_ucontext_param->u.perf_counter.name,
2499 sizeof(ucontext_param->u.perf_counter.name));
2500 }
2501
2502 ret = lttng_abi_add_context(file,
2503 ucontext_param,
ac847066 2504 &channel->priv->ctx, channel->parent.session);
6dccd6c1
JD
2505
2506old_ctx_error_free_old_param:
2507 kfree(old_ucontext_param);
2508old_ctx_error_free_param:
2509 kfree(ucontext_param);
2510old_ctx_end:
2511 return ret;
2512 }
606828e4 2513 case LTTNG_KERNEL_ABI_CONTEXT:
6dccd6c1 2514 {
606828e4 2515 struct lttng_kernel_abi_context ucontext_param;
6dccd6c1
JD
2516
2517 if (copy_from_user(&ucontext_param,
606828e4 2518 (struct lttng_kernel_abi_context __user *) arg,
6dccd6c1
JD
2519 sizeof(ucontext_param)))
2520 return -EFAULT;
2521 return lttng_abi_add_context(file,
2522 &ucontext_param,
ac847066 2523 &channel->priv->ctx, channel->parent.session);
6dccd6c1 2524 }
606828e4
MD
2525 case LTTNG_KERNEL_ABI_OLD_ENABLE:
2526 case LTTNG_KERNEL_ABI_ENABLE:
a90917c3 2527 return lttng_channel_enable(channel);
606828e4
MD
2528 case LTTNG_KERNEL_ABI_OLD_DISABLE:
2529 case LTTNG_KERNEL_ABI_DISABLE:
a90917c3 2530 return lttng_channel_disable(channel);
606828e4 2531 case LTTNG_KERNEL_ABI_SYSCALL_MASK:
12e579db 2532 return lttng_channel_syscall_mask(channel,
606828e4 2533 (struct lttng_kernel_abi_syscall_mask __user *) arg);
baf20995
MD
2534 default:
2535 return -ENOIOCTLCMD;
2536 }
2537}
2538
5dbbdb43
MD
2539/**
2540 * lttng_metadata_ioctl - lttng syscall through ioctl
2541 *
2542 * @file: the file
2543 * @cmd: the command
2544 * @arg: command arg
2545 *
2546 * This ioctl implements lttng commands:
606828e4 2547 * LTTNG_KERNEL_ABI_STREAM
5dbbdb43
MD
2548 * Returns an event stream file descriptor or failure.
2549 *
2550 * Channel and event file descriptors also hold a reference on the session.
2551 */
2552static
2553long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2554{
2555 switch (cmd) {
606828e4
MD
2556 case LTTNG_KERNEL_ABI_OLD_STREAM:
2557 case LTTNG_KERNEL_ABI_STREAM:
d83004aa 2558 return lttng_abi_open_metadata_stream(file);
5dbbdb43
MD
2559 default:
2560 return -ENOIOCTLCMD;
2561 }
2562}
2563
653fe716
MD
2564/**
2565 * lttng_channel_poll - lttng stream addition/removal monitoring
2566 *
c0e31d2e 2567 * @file: the file
653fe716
MD
2568 * @wait: poll table
2569 */
c0e31d2e 2570unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
653fe716 2571{
ac847066 2572 struct lttng_kernel_channel_buffer *channel = file->private_data;
653fe716
MD
2573 unsigned int mask = 0;
2574
c0e31d2e 2575 if (file->f_mode & FMODE_READ) {
a33e44a6 2576 poll_wait_set_exclusive(wait);
ac847066 2577 poll_wait(file, channel->ops->priv->get_hp_wait_queue(channel->priv->rb_chan),
24cedcfe 2578 wait);
653fe716 2579
ac847066 2580 if (channel->ops->priv->is_disabled(channel->priv->rb_chan))
254ec7bc 2581 return POLLERR;
ac847066 2582 if (channel->ops->priv->is_finalized(channel->priv->rb_chan))
653fe716 2583 return POLLHUP;
ac847066 2584 if (channel->ops->priv->buffer_has_read_closed_stream(channel->priv->rb_chan))
653fe716 2585 return POLLIN | POLLRDNORM;
f71ecafa 2586 return 0;
653fe716
MD
2587 }
2588 return mask;
2589
2590}
2591
0a84a57f
MD
2592static
2593int lttng_channel_release(struct inode *inode, struct file *file)
2594{
ac847066 2595 struct lttng_kernel_channel_buffer *channel = file->private_data;
c269fff4
MD
2596
2597 if (channel)
ac847066 2598 fput(channel->parent.session->priv->file);
0a84a57f
MD
2599 return 0;
2600}
2601
d83004aa
JD
2602static
2603int lttng_metadata_channel_release(struct inode *inode, struct file *file)
2604{
ac847066 2605 struct lttng_kernel_channel_buffer *channel = file->private_data;
d83004aa
JD
2606
2607 if (channel) {
ac847066 2608 fput(channel->parent.session->priv->file);
a3381417 2609 lttng_metadata_channel_destroy(channel);
d83004aa
JD
2610 }
2611
2612 return 0;
2613}
2614
ad1c05e1 2615static const struct file_operations lttng_channel_fops = {
a33c9927 2616 .owner = THIS_MODULE,
03037b98 2617 .release = lttng_channel_release,
653fe716 2618 .poll = lttng_channel_poll,
ad1c05e1 2619 .unlocked_ioctl = lttng_channel_ioctl,
baf20995 2620#ifdef CONFIG_COMPAT
03037b98 2621 .compat_ioctl = lttng_channel_ioctl,
baf20995 2622#endif
11b5a3c2 2623};
baf20995 2624
5dbbdb43 2625static const struct file_operations lttng_metadata_fops = {
a33c9927 2626 .owner = THIS_MODULE,
d83004aa 2627 .release = lttng_metadata_channel_release,
5dbbdb43
MD
2628 .unlocked_ioctl = lttng_metadata_ioctl,
2629#ifdef CONFIG_COMPAT
2630 .compat_ioctl = lttng_metadata_ioctl,
2631#endif
2632};
2633
8070f5c0 2634/**
ef784b4d 2635 * lttng_event_recorder_event_ioctl - lttng syscall through ioctl
8070f5c0
MD
2636 *
2637 * @file: the file
2638 * @cmd: the command
2639 * @arg: command arg
2640 *
2641 * This ioctl implements lttng commands:
606828e4 2642 * LTTNG_KERNEL_ABI_CONTEXT
8070f5c0 2643 * Prepend a context field to each record of this event
606828e4 2644 * LTTNG_KERNEL_ABI_ENABLE
e64957da 2645 * Enable recording for this event (weak enable)
606828e4 2646 * LTTNG_KERNEL_ABI_DISABLE
e64957da 2647 * Disable recording for this event (strong disable)
8070f5c0
MD
2648 */
2649static
ef784b4d 2650long lttng_event_recorder_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
8070f5c0 2651{
e2d5dbc7 2652 struct lttng_kernel_event_recorder *event_recorder = file->private_data;
8070f5c0
MD
2653
2654 switch (cmd) {
606828e4 2655 case LTTNG_KERNEL_ABI_OLD_CONTEXT:
6dccd6c1 2656 {
3c997079
MD
2657 /* Not implemented */
2658 return -ENOSYS;
6dccd6c1 2659 }
606828e4 2660 case LTTNG_KERNEL_ABI_CONTEXT:
6dccd6c1 2661 {
3c997079
MD
2662 /* Not implemented */
2663 return -ENOSYS;
6dccd6c1 2664 }
606828e4
MD
2665 case LTTNG_KERNEL_ABI_OLD_ENABLE:
2666 case LTTNG_KERNEL_ABI_ENABLE:
9d993668 2667 return lttng_event_enable(&event_recorder->parent);
606828e4
MD
2668 case LTTNG_KERNEL_ABI_OLD_DISABLE:
2669 case LTTNG_KERNEL_ABI_DISABLE:
9d993668 2670 return lttng_event_disable(&event_recorder->parent);
606828e4 2671 case LTTNG_KERNEL_ABI_FILTER:
ef784b4d 2672 return -EINVAL;
606828e4 2673 case LTTNG_KERNEL_ABI_ADD_CALLSITE:
e2d5dbc7 2674 return lttng_event_add_callsite(&event_recorder->parent,
ef784b4d 2675 (struct lttng_kernel_abi_event_callsite __user *) arg);
8070f5c0
MD
2676 default:
2677 return -ENOIOCTLCMD;
2678 }
2679}
2680
ef784b4d
MD
2681/**
2682 * lttng_event_recorder_enabler_ioctl - lttng syscall through ioctl
2683 *
2684 * @file: the file
2685 * @cmd: the command
2686 * @arg: command arg
2687 *
2688 * This ioctl implements lttng commands:
2689 * LTTNG_KERNEL_ABI_CONTEXT
2690 * Prepend a context field to each record of this event
2691 * LTTNG_KERNEL_ABI_ENABLE
2692 * Enable recording for this event (weak enable)
2693 * LTTNG_KERNEL_ABI_DISABLE
2694 * Disable recording for this event (strong disable)
2695 */
0a84a57f 2696static
ef784b4d 2697long lttng_event_recorder_enabler_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
0a84a57f 2698{
ef784b4d 2699 struct lttng_event_enabler *event_enabler = file->private_data;
3c997079 2700
ef784b4d
MD
2701 switch (cmd) {
2702 case LTTNG_KERNEL_ABI_OLD_CONTEXT:
2703 {
2704 /* Not implemented */
2705 return -ENOSYS;
2706 }
2707 case LTTNG_KERNEL_ABI_CONTEXT:
2708 {
2709 /* Not implemented */
2710 return -ENOSYS;
2711 }
2712 case LTTNG_KERNEL_ABI_OLD_ENABLE:
2713 case LTTNG_KERNEL_ABI_ENABLE:
2714 return lttng_event_enabler_enable(event_enabler);
2715 case LTTNG_KERNEL_ABI_OLD_DISABLE:
2716 case LTTNG_KERNEL_ABI_DISABLE:
2717 return lttng_event_enabler_disable(event_enabler);
2718 case LTTNG_KERNEL_ABI_FILTER:
2719 return lttng_event_enabler_attach_filter_bytecode(
2720 event_enabler,
2721 (struct lttng_kernel_abi_filter_bytecode __user *) arg);
2722 case LTTNG_KERNEL_ABI_ADD_CALLSITE:
2723 return -EINVAL;
3c997079 2724 default:
ef784b4d 2725 return -ENOIOCTLCMD;
3c997079 2726 }
ef784b4d
MD
2727}
2728
2729static
2730int lttng_event_recorder_event_release(struct inode *inode, struct file *file)
2731{
a67ba386 2732 struct lttng_kernel_event_recorder *event = file->private_data;
ef784b4d
MD
2733
2734 if (event)
ac847066 2735 fput(event->chan->priv->parent.file);
ef784b4d
MD
2736 return 0;
2737}
c269fff4 2738
ef784b4d
MD
2739static
2740int lttng_event_recorder_enabler_release(struct inode *inode, struct file *file)
2741{
2742 struct lttng_event_enabler *event_enabler = file->private_data;
2743
2744 if (event_enabler)
ac847066 2745 fput(event_enabler->chan->priv->parent.file);
0a84a57f
MD
2746 return 0;
2747}
2748
ef784b4d
MD
2749static const struct file_operations lttng_event_recorder_event_fops = {
2750 .owner = THIS_MODULE,
2751 .release = lttng_event_recorder_event_release,
2752 .unlocked_ioctl = lttng_event_recorder_event_ioctl,
2753#ifdef CONFIG_COMPAT
2754 .compat_ioctl = lttng_event_recorder_event_ioctl,
2755#endif
2756};
2757
2758static const struct file_operations lttng_event_recorder_enabler_fops = {
a33c9927 2759 .owner = THIS_MODULE,
ef784b4d
MD
2760 .release = lttng_event_recorder_enabler_release,
2761 .unlocked_ioctl = lttng_event_recorder_enabler_ioctl,
8070f5c0 2762#ifdef CONFIG_COMPAT
ef784b4d 2763 .compat_ioctl = lttng_event_recorder_enabler_ioctl,
8070f5c0 2764#endif
11b5a3c2 2765};
0a84a57f 2766
3b731ab1
JD
2767static int put_u64(uint64_t val, unsigned long arg)
2768{
2769 return put_user(val, (uint64_t __user *) arg);
2770}
2771
8b97fd42
MD
2772static int put_u32(uint32_t val, unsigned long arg)
2773{
2774 return put_user(val, (uint32_t __user *) arg);
2775}
2776
ed8d02d6
JD
2777static long lttng_stream_ring_buffer_ioctl(struct file *filp,
2778 unsigned int cmd, unsigned long arg)
2779{
a2a1b5ab 2780 struct lttng_kernel_ring_buffer *buf = filp->private_data;
4071a628 2781 struct lttng_kernel_ring_buffer_channel *chan = buf->backend.chan;
a2a1b5ab 2782 const struct lttng_kernel_ring_buffer_config *config = &chan->backend.config;
da1fe18f 2783 const struct lttng_kernel_channel_buffer_ops *ops = chan->backend.priv_ops;
3b731ab1
JD
2784 int ret;
2785
2786 if (atomic_read(&chan->record_disabled))
2787 return -EIO;
2788
ed8d02d6 2789 switch (cmd) {
606828e4 2790 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_TIMESTAMP_BEGIN:
3b731ab1
JD
2791 {
2792 uint64_t ts;
2793
da1fe18f 2794 ret = ops->priv->timestamp_begin(config, buf, &ts);
3b731ab1
JD
2795 if (ret < 0)
2796 goto error;
2797 return put_u64(ts, arg);
2798 }
606828e4 2799 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_TIMESTAMP_END:
3b731ab1
JD
2800 {
2801 uint64_t ts;
2802
da1fe18f 2803 ret = ops->priv->timestamp_end(config, buf, &ts);
3b731ab1
JD
2804 if (ret < 0)
2805 goto error;
2806 return put_u64(ts, arg);
2807 }
606828e4 2808 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_EVENTS_DISCARDED:
3b731ab1
JD
2809 {
2810 uint64_t ed;
2811
da1fe18f 2812 ret = ops->priv->events_discarded(config, buf, &ed);
3b731ab1
JD
2813 if (ret < 0)
2814 goto error;
2815 return put_u64(ed, arg);
2816 }
606828e4 2817 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_CONTENT_SIZE:
3b731ab1
JD
2818 {
2819 uint64_t cs;
2820
da1fe18f 2821 ret = ops->priv->content_size(config, buf, &cs);
3b731ab1
JD
2822 if (ret < 0)
2823 goto error;
2824 return put_u64(cs, arg);
2825 }
606828e4 2826 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_PACKET_SIZE:
3b731ab1
JD
2827 {
2828 uint64_t ps;
2829
da1fe18f 2830 ret = ops->priv->packet_size(config, buf, &ps);
3b731ab1
JD
2831 if (ret < 0)
2832 goto error;
2833 return put_u64(ps, arg);
2834 }
606828e4 2835 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_STREAM_ID:
3b731ab1
JD
2836 {
2837 uint64_t si;
2838
da1fe18f 2839 ret = ops->priv->stream_id(config, buf, &si);
3b731ab1
JD
2840 if (ret < 0)
2841 goto error;
2842 return put_u64(si, arg);
2843 }
606828e4 2844 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_CURRENT_TIMESTAMP:
2348ca17
JD
2845 {
2846 uint64_t ts;
2847
da1fe18f 2848 ret = ops->priv->current_timestamp(config, buf, &ts);
2348ca17
JD
2849 if (ret < 0)
2850 goto error;
2851 return put_u64(ts, arg);
2852 }
606828e4 2853 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_SEQ_NUM:
5b3cf4f9
JD
2854 {
2855 uint64_t seq;
2856
da1fe18f 2857 ret = ops->priv->sequence_number(config, buf, &seq);
5b3cf4f9
JD
2858 if (ret < 0)
2859 goto error;
2860 return put_u64(seq, arg);
2861 }
606828e4 2862 case LTTNG_KERNEL_ABI_RING_BUFFER_INSTANCE_ID:
5594698f
JD
2863 {
2864 uint64_t id;
2865
da1fe18f 2866 ret = ops->priv->instance_id(config, buf, &id);
5594698f
JD
2867 if (ret < 0)
2868 goto error;
2869 return put_u64(id, arg);
2870 }
3b731ab1
JD
2871 default:
2872 return lib_ring_buffer_file_operations.unlocked_ioctl(filp,
2873 cmd, arg);
ed8d02d6 2874 }
3b731ab1
JD
2875
2876error:
2877 return -ENOSYS;
ed8d02d6
JD
2878}
2879
2880#ifdef CONFIG_COMPAT
2881static long lttng_stream_ring_buffer_compat_ioctl(struct file *filp,
2882 unsigned int cmd, unsigned long arg)
2883{
a2a1b5ab 2884 struct lttng_kernel_ring_buffer *buf = filp->private_data;
4071a628 2885 struct lttng_kernel_ring_buffer_channel *chan = buf->backend.chan;
a2a1b5ab 2886 const struct lttng_kernel_ring_buffer_config *config = &chan->backend.config;
da1fe18f 2887 const struct lttng_kernel_channel_buffer_ops *ops = chan->backend.priv_ops;
3b731ab1
JD
2888 int ret;
2889
2890 if (atomic_read(&chan->record_disabled))
2891 return -EIO;
2892
ed8d02d6 2893 switch (cmd) {
606828e4 2894 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN:
3b731ab1
JD
2895 {
2896 uint64_t ts;
2897
da1fe18f 2898 ret = ops->priv->timestamp_begin(config, buf, &ts);
3b731ab1
JD
2899 if (ret < 0)
2900 goto error;
2901 return put_u64(ts, arg);
2902 }
606828e4 2903 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_TIMESTAMP_END:
3b731ab1
JD
2904 {
2905 uint64_t ts;
2906
da1fe18f 2907 ret = ops->priv->timestamp_end(config, buf, &ts);
3b731ab1
JD
2908 if (ret < 0)
2909 goto error;
2910 return put_u64(ts, arg);
2911 }
606828e4 2912 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED:
3b731ab1
JD
2913 {
2914 uint64_t ed;
2915
da1fe18f 2916 ret = ops->priv->events_discarded(config, buf, &ed);
3b731ab1
JD
2917 if (ret < 0)
2918 goto error;
2919 return put_u64(ed, arg);
ed8d02d6 2920 }
606828e4 2921 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_CONTENT_SIZE:
3b731ab1
JD
2922 {
2923 uint64_t cs;
2924
da1fe18f 2925 ret = ops->priv->content_size(config, buf, &cs);
3b731ab1
JD
2926 if (ret < 0)
2927 goto error;
2928 return put_u64(cs, arg);
2929 }
606828e4 2930 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_PACKET_SIZE:
3b731ab1
JD
2931 {
2932 uint64_t ps;
2933
da1fe18f 2934 ret = ops->priv->packet_size(config, buf, &ps);
3b731ab1
JD
2935 if (ret < 0)
2936 goto error;
2937 return put_u64(ps, arg);
2938 }
606828e4 2939 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_STREAM_ID:
3b731ab1
JD
2940 {
2941 uint64_t si;
2942
da1fe18f 2943 ret = ops->priv->stream_id(config, buf, &si);
3b731ab1
JD
2944 if (ret < 0)
2945 goto error;
2946 return put_u64(si, arg);
2947 }
606828e4 2948 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_CURRENT_TIMESTAMP:
2348ca17
JD
2949 {
2950 uint64_t ts;
2951
da1fe18f 2952 ret = ops->priv->current_timestamp(config, buf, &ts);
2348ca17
JD
2953 if (ret < 0)
2954 goto error;
2955 return put_u64(ts, arg);
2956 }
606828e4 2957 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_SEQ_NUM:
5b3cf4f9
JD
2958 {
2959 uint64_t seq;
2960
da1fe18f 2961 ret = ops->priv->sequence_number(config, buf, &seq);
5b3cf4f9
JD
2962 if (ret < 0)
2963 goto error;
2964 return put_u64(seq, arg);
2965 }
606828e4 2966 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_INSTANCE_ID:
5594698f
JD
2967 {
2968 uint64_t id;
2969
da1fe18f 2970 ret = ops->priv->instance_id(config, buf, &id);
5594698f
JD
2971 if (ret < 0)
2972 goto error;
2973 return put_u64(id, arg);
2974 }
3b731ab1
JD
2975 default:
2976 return lib_ring_buffer_file_operations.compat_ioctl(filp,
2977 cmd, arg);
2978 }
2979
2980error:
2981 return -ENOSYS;
ed8d02d6
JD
2982}
2983#endif /* CONFIG_COMPAT */
2984
2985static void lttng_stream_override_ring_buffer_fops(void)
2986{
2987 lttng_stream_ring_buffer_file_operations.owner = THIS_MODULE;
2988 lttng_stream_ring_buffer_file_operations.open =
2989 lib_ring_buffer_file_operations.open;
2990 lttng_stream_ring_buffer_file_operations.release =
2991 lib_ring_buffer_file_operations.release;
2992 lttng_stream_ring_buffer_file_operations.poll =
2993 lib_ring_buffer_file_operations.poll;
2994 lttng_stream_ring_buffer_file_operations.splice_read =
2995 lib_ring_buffer_file_operations.splice_read;
2996 lttng_stream_ring_buffer_file_operations.mmap =
2997 lib_ring_buffer_file_operations.mmap;
2998 lttng_stream_ring_buffer_file_operations.unlocked_ioctl =
2999 lttng_stream_ring_buffer_ioctl;
3000 lttng_stream_ring_buffer_file_operations.llseek =
3001 lib_ring_buffer_file_operations.llseek;
3002#ifdef CONFIG_COMPAT
3003 lttng_stream_ring_buffer_file_operations.compat_ioctl =
3004 lttng_stream_ring_buffer_compat_ioctl;
3005#endif
3006}
3007
80996790 3008int __init lttng_abi_init(void)
baf20995
MD
3009{
3010 int ret = 0;
3011
263b6c88 3012 wrapper_vmalloc_sync_mappings();
2754583e 3013 lttng_clock_ref();
f771eda6
JD
3014
3015 ret = lttng_tp_mempool_init();
3016 if (ret) {
3017 goto error;
3018 }
3019
d29348f7 3020 lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL,
059de147 3021 &lttng_proc_ops, NULL);
2470a237 3022
255e52a4 3023 if (!lttng_proc_dentry) {
5a15f70c 3024 printk(KERN_ERR "LTTng: Error creating control file\n");
baf20995
MD
3025 ret = -ENOMEM;
3026 goto error;
3027 }
ed8d02d6 3028 lttng_stream_override_ring_buffer_fops();
2754583e 3029 return 0;
ed8d02d6 3030
baf20995 3031error:
f771eda6 3032 lttng_tp_mempool_destroy();
2754583e 3033 lttng_clock_unref();
baf20995
MD
3034 return ret;
3035}
3036
e6e65fcd
MD
3037/* No __exit annotation because used by init error path too. */
3038void lttng_abi_exit(void)
baf20995 3039{
f771eda6 3040 lttng_tp_mempool_destroy();
2754583e 3041 lttng_clock_unref();
e6a17f26
MD
3042 if (lttng_proc_dentry)
3043 remove_proc_entry("lttng", NULL);
baf20995 3044}
This page took 0.21941 seconds and 4 git commands to generate.