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