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