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