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