Cleanup: implement dedicated file operations for events and enablers
[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_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->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_session *session)
263 {
264
265 if (session->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_session *session = session_file->private_data;
492 const struct file_operations *fops = NULL;
493 const char *transport_name;
494 struct lttng_channel *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->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_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->name, name->name);
592 return 0;
593 }
594
595 static
596 int lttng_abi_session_set_creation_time(struct lttng_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->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_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 switch (cmd) {
797 case LTTNG_KERNEL_ABI_OLD_CHANNEL:
798 {
799 if (copy_from_user(&old_chan_param,
800 (struct lttng_kernel_abi_old_channel __user *) arg,
801 sizeof(struct lttng_kernel_abi_old_channel)))
802 return -EFAULT;
803 chan_param.overwrite = old_chan_param.overwrite;
804 chan_param.subbuf_size = old_chan_param.subbuf_size;
805 chan_param.num_subbuf = old_chan_param.num_subbuf;
806 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
807 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
808 chan_param.output = old_chan_param.output;
809
810 return lttng_abi_create_channel(file, &chan_param,
811 PER_CPU_CHANNEL);
812 }
813 case LTTNG_KERNEL_ABI_CHANNEL:
814 {
815 if (copy_from_user(&chan_param,
816 (struct lttng_kernel_abi_channel __user *) arg,
817 sizeof(struct lttng_kernel_abi_channel)))
818 return -EFAULT;
819 return lttng_abi_create_channel(file, &chan_param,
820 PER_CPU_CHANNEL);
821 }
822 case LTTNG_KERNEL_ABI_OLD_SESSION_START:
823 case LTTNG_KERNEL_ABI_OLD_ENABLE:
824 case LTTNG_KERNEL_ABI_SESSION_START:
825 case LTTNG_KERNEL_ABI_ENABLE:
826 return lttng_session_enable(session);
827 case LTTNG_KERNEL_ABI_OLD_SESSION_STOP:
828 case LTTNG_KERNEL_ABI_OLD_DISABLE:
829 case LTTNG_KERNEL_ABI_SESSION_STOP:
830 case LTTNG_KERNEL_ABI_DISABLE:
831 return lttng_session_disable(session);
832 case LTTNG_KERNEL_ABI_OLD_METADATA:
833 {
834 if (copy_from_user(&old_chan_param,
835 (struct lttng_kernel_abi_old_channel __user *) arg,
836 sizeof(struct lttng_kernel_abi_old_channel)))
837 return -EFAULT;
838 chan_param.overwrite = old_chan_param.overwrite;
839 chan_param.subbuf_size = old_chan_param.subbuf_size;
840 chan_param.num_subbuf = old_chan_param.num_subbuf;
841 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
842 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
843 chan_param.output = old_chan_param.output;
844
845 return lttng_abi_create_channel(file, &chan_param,
846 METADATA_CHANNEL);
847 }
848 case LTTNG_KERNEL_ABI_METADATA:
849 {
850 if (copy_from_user(&chan_param,
851 (struct lttng_kernel_abi_channel __user *) arg,
852 sizeof(struct lttng_kernel_abi_channel)))
853 return -EFAULT;
854 return lttng_abi_create_channel(file, &chan_param,
855 METADATA_CHANNEL);
856 }
857 case LTTNG_KERNEL_ABI_SESSION_TRACK_PID:
858 return lttng_session_track_id(session, TRACKER_PID, (int) arg);
859 case LTTNG_KERNEL_ABI_SESSION_UNTRACK_PID:
860 return lttng_session_untrack_id(session, TRACKER_PID, (int) arg);
861 case LTTNG_KERNEL_ABI_SESSION_TRACK_ID:
862 {
863 struct lttng_kernel_abi_tracker_args tracker;
864 enum tracker_type tracker_type;
865
866 if (copy_from_user(&tracker,
867 (struct lttng_kernel_abi_tracker_args __user *) arg,
868 sizeof(struct lttng_kernel_abi_tracker_args)))
869 return -EFAULT;
870 tracker_type = get_tracker_type(&tracker);
871 if (tracker_type == TRACKER_UNKNOWN)
872 return -EINVAL;
873 return lttng_session_track_id(session, tracker_type, tracker.id);
874 }
875 case LTTNG_KERNEL_ABI_SESSION_UNTRACK_ID:
876 {
877 struct lttng_kernel_abi_tracker_args tracker;
878 enum tracker_type tracker_type;
879
880 if (copy_from_user(&tracker,
881 (struct lttng_kernel_abi_tracker_args __user *) arg,
882 sizeof(struct lttng_kernel_abi_tracker_args)))
883 return -EFAULT;
884 tracker_type = get_tracker_type(&tracker);
885 if (tracker_type == TRACKER_UNKNOWN)
886 return -EINVAL;
887 return lttng_session_untrack_id(session, tracker_type,
888 tracker.id);
889 }
890 case LTTNG_KERNEL_ABI_SESSION_LIST_TRACKER_PIDS:
891 return lttng_session_list_tracker_ids(session, TRACKER_PID);
892 case LTTNG_KERNEL_ABI_SESSION_LIST_TRACKER_IDS:
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_list_tracker_ids(session, tracker_type);
905 }
906 case LTTNG_KERNEL_ABI_SESSION_METADATA_REGEN:
907 return lttng_session_metadata_regenerate(session);
908 case LTTNG_KERNEL_ABI_SESSION_STATEDUMP:
909 return lttng_session_statedump(session);
910 case LTTNG_KERNEL_ABI_SESSION_SET_NAME:
911 {
912 struct lttng_kernel_abi_session_name name;
913
914 if (copy_from_user(&name,
915 (struct lttng_kernel_abi_session_name __user *) arg,
916 sizeof(struct lttng_kernel_abi_session_name)))
917 return -EFAULT;
918 return lttng_abi_session_set_name(session, &name);
919 }
920 case LTTNG_KERNEL_ABI_SESSION_SET_CREATION_TIME:
921 {
922 struct lttng_kernel_abi_session_creation_time time;
923
924 if (copy_from_user(&time,
925 (struct lttng_kernel_abi_session_creation_time __user *) arg,
926 sizeof(struct lttng_kernel_abi_session_creation_time)))
927 return -EFAULT;
928 return lttng_abi_session_set_creation_time(session, &time);
929 }
930 default:
931 return -ENOIOCTLCMD;
932 }
933 }
934
935 /*
936 * Called when the last file reference is dropped.
937 *
938 * Big fat note: channels and events are invariant for the whole session after
939 * their creation. So this session destruction also destroys all channel and
940 * event structures specific to this session (they are not destroyed when their
941 * individual file is released).
942 */
943 static
944 int lttng_session_release(struct inode *inode, struct file *file)
945 {
946 struct lttng_session *session = file->private_data;
947
948 if (session)
949 lttng_session_destroy(session);
950 return 0;
951 }
952
953 static const struct file_operations lttng_session_fops = {
954 .owner = THIS_MODULE,
955 .release = lttng_session_release,
956 .unlocked_ioctl = lttng_session_ioctl,
957 #ifdef CONFIG_COMPAT
958 .compat_ioctl = lttng_session_ioctl,
959 #endif
960 };
961
962 /*
963 * When encountering empty buffer, flush current sub-buffer if non-empty
964 * and retry (if new data available to read after flush).
965 */
966 static
967 ssize_t lttng_event_notifier_group_notif_read(struct file *filp, char __user *user_buf,
968 size_t count, loff_t *ppos)
969 {
970 struct lttng_event_notifier_group *event_notifier_group = filp->private_data;
971 struct channel *chan = event_notifier_group->chan;
972 struct lib_ring_buffer *buf = event_notifier_group->buf;
973 ssize_t read_count = 0, len;
974 size_t read_offset;
975
976 might_sleep();
977 if (!lttng_access_ok(VERIFY_WRITE, user_buf, count))
978 return -EFAULT;
979
980 /* Finish copy of previous record */
981 if (*ppos != 0) {
982 if (read_count < count) {
983 len = chan->iter.len_left;
984 read_offset = *ppos;
985 goto skip_get_next;
986 }
987 }
988
989 while (read_count < count) {
990 size_t copy_len, space_left;
991
992 len = lib_ring_buffer_get_next_record(chan, buf);
993 len_test:
994 if (len < 0) {
995 /*
996 * Check if buffer is finalized (end of file).
997 */
998 if (len == -ENODATA) {
999 /* A 0 read_count will tell about end of file */
1000 goto nodata;
1001 }
1002 if (filp->f_flags & O_NONBLOCK) {
1003 if (!read_count)
1004 read_count = -EAGAIN;
1005 goto nodata;
1006 } else {
1007 int error;
1008
1009 /*
1010 * No data available at the moment, return what
1011 * we got.
1012 */
1013 if (read_count)
1014 goto nodata;
1015
1016 /*
1017 * Wait for returned len to be >= 0 or -ENODATA.
1018 */
1019 error = wait_event_interruptible(
1020 event_notifier_group->read_wait,
1021 ((len = lib_ring_buffer_get_next_record(
1022 chan, buf)), len != -EAGAIN));
1023 CHAN_WARN_ON(chan, len == -EBUSY);
1024 if (error) {
1025 read_count = error;
1026 goto nodata;
1027 }
1028 CHAN_WARN_ON(chan, len < 0 && len != -ENODATA);
1029 goto len_test;
1030 }
1031 }
1032 read_offset = buf->iter.read_offset;
1033 skip_get_next:
1034 space_left = count - read_count;
1035 if (len <= space_left) {
1036 copy_len = len;
1037 chan->iter.len_left = 0;
1038 *ppos = 0;
1039 } else {
1040 copy_len = space_left;
1041 chan->iter.len_left = len - copy_len;
1042 *ppos = read_offset + copy_len;
1043 }
1044 if (__lib_ring_buffer_copy_to_user(&buf->backend, read_offset,
1045 &user_buf[read_count],
1046 copy_len)) {
1047 /*
1048 * Leave the len_left and ppos values at their current
1049 * state, as we currently have a valid event to read.
1050 */
1051 return -EFAULT;
1052 }
1053 read_count += copy_len;
1054 }
1055 goto put_record;
1056
1057 nodata:
1058 *ppos = 0;
1059 chan->iter.len_left = 0;
1060
1061 put_record:
1062 lib_ring_buffer_put_current_record(buf);
1063 return read_count;
1064 }
1065
1066 /*
1067 * If the ring buffer is non empty (even just a partial subbuffer), return that
1068 * there is data available. Perform a ring buffer flush if we encounter a
1069 * non-empty ring buffer which does not have any consumeable subbuffer available.
1070 */
1071 static
1072 unsigned int lttng_event_notifier_group_notif_poll(struct file *filp,
1073 poll_table *wait)
1074 {
1075 unsigned int mask = 0;
1076 struct lttng_event_notifier_group *event_notifier_group = filp->private_data;
1077 struct channel *chan = event_notifier_group->chan;
1078 struct lib_ring_buffer *buf = event_notifier_group->buf;
1079 const struct lib_ring_buffer_config *config = &chan->backend.config;
1080 int finalized, disabled;
1081 unsigned long consumed, offset;
1082 size_t subbuffer_header_size = config->cb.subbuffer_header_size();
1083
1084 if (filp->f_mode & FMODE_READ) {
1085 poll_wait_set_exclusive(wait);
1086 poll_wait(filp, &event_notifier_group->read_wait, wait);
1087
1088 finalized = lib_ring_buffer_is_finalized(config, buf);
1089 disabled = lib_ring_buffer_channel_is_disabled(chan);
1090
1091 /*
1092 * lib_ring_buffer_is_finalized() contains a smp_rmb() ordering
1093 * finalized load before offsets loads.
1094 */
1095 WARN_ON(atomic_long_read(&buf->active_readers) != 1);
1096 retry:
1097 if (disabled)
1098 return POLLERR;
1099
1100 offset = lib_ring_buffer_get_offset(config, buf);
1101 consumed = lib_ring_buffer_get_consumed(config, buf);
1102
1103 /*
1104 * If there is no buffer available to consume.
1105 */
1106 if (subbuf_trunc(offset, chan) - subbuf_trunc(consumed, chan) == 0) {
1107 /*
1108 * If there is a non-empty subbuffer, flush and try again.
1109 */
1110 if (subbuf_offset(offset, chan) > subbuffer_header_size) {
1111 lib_ring_buffer_switch_remote(buf);
1112 goto retry;
1113 }
1114
1115 if (finalized)
1116 return POLLHUP;
1117 else {
1118 /*
1119 * The memory barriers
1120 * __wait_event()/wake_up_interruptible() take
1121 * care of "raw_spin_is_locked" memory ordering.
1122 */
1123 if (raw_spin_is_locked(&buf->raw_tick_nohz_spinlock))
1124 goto retry;
1125 else
1126 return 0;
1127 }
1128 } else {
1129 if (subbuf_trunc(offset, chan) - subbuf_trunc(consumed, chan)
1130 >= chan->backend.buf_size)
1131 return POLLPRI | POLLRDBAND;
1132 else
1133 return POLLIN | POLLRDNORM;
1134 }
1135 }
1136
1137 return mask;
1138 }
1139
1140 /**
1141 * lttng_event_notifier_group_notif_open - event_notifier ring buffer open file operation
1142 * @inode: opened inode
1143 * @file: opened file
1144 *
1145 * Open implementation. Makes sure only one open instance of a buffer is
1146 * done at a given moment.
1147 */
1148 static int lttng_event_notifier_group_notif_open(struct inode *inode, struct file *file)
1149 {
1150 struct lttng_event_notifier_group *event_notifier_group = inode->i_private;
1151 struct lib_ring_buffer *buf = event_notifier_group->buf;
1152
1153 file->private_data = event_notifier_group;
1154 return lib_ring_buffer_open(inode, file, buf);
1155 }
1156
1157 /**
1158 * lttng_event_notifier_group_notif_release - event_notifier ring buffer release file operation
1159 * @inode: opened inode
1160 * @file: opened file
1161 *
1162 * Release implementation.
1163 */
1164 static int lttng_event_notifier_group_notif_release(struct inode *inode, struct file *file)
1165 {
1166 struct lttng_event_notifier_group *event_notifier_group = file->private_data;
1167 struct lib_ring_buffer *buf = event_notifier_group->buf;
1168 int ret;
1169
1170 ret = lib_ring_buffer_release(inode, file, buf);
1171 if (ret)
1172 return ret;
1173 fput(event_notifier_group->file);
1174 return 0;
1175 }
1176
1177 static const struct file_operations lttng_event_notifier_group_notif_fops = {
1178 .owner = THIS_MODULE,
1179 .open = lttng_event_notifier_group_notif_open,
1180 .release = lttng_event_notifier_group_notif_release,
1181 .read = lttng_event_notifier_group_notif_read,
1182 .poll = lttng_event_notifier_group_notif_poll,
1183 };
1184
1185 /**
1186 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
1187 * @filp: the file
1188 * @wait: poll table
1189 *
1190 * Handles the poll operations for the metadata channels.
1191 */
1192 static
1193 unsigned int lttng_metadata_ring_buffer_poll(struct file *filp,
1194 poll_table *wait)
1195 {
1196 struct lttng_metadata_stream *stream = filp->private_data;
1197 struct lib_ring_buffer *buf = stream->priv;
1198 int finalized;
1199 unsigned int mask = 0;
1200
1201 if (filp->f_mode & FMODE_READ) {
1202 poll_wait_set_exclusive(wait);
1203 poll_wait(filp, &stream->read_wait, wait);
1204
1205 finalized = stream->finalized;
1206
1207 /*
1208 * lib_ring_buffer_is_finalized() contains a smp_rmb()
1209 * ordering finalized load before offsets loads.
1210 */
1211 WARN_ON(atomic_long_read(&buf->active_readers) != 1);
1212
1213 if (finalized)
1214 mask |= POLLHUP;
1215
1216 mutex_lock(&stream->metadata_cache->lock);
1217 if (stream->metadata_cache->metadata_written >
1218 stream->metadata_out)
1219 mask |= POLLIN;
1220 mutex_unlock(&stream->metadata_cache->lock);
1221 }
1222
1223 return mask;
1224 }
1225
1226 static
1227 void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file *filp,
1228 unsigned int cmd, unsigned long arg)
1229 {
1230 struct lttng_metadata_stream *stream = filp->private_data;
1231
1232 stream->metadata_out = stream->metadata_in;
1233 }
1234
1235 /*
1236 * Reset the counter of how much metadata has been consumed to 0. That way,
1237 * the consumer receives the content of the metadata cache unchanged. This is
1238 * different from the metadata_regenerate where the offset from epoch is
1239 * resampled, here we want the exact same content as the last time the metadata
1240 * was generated. This command is only possible if all the metadata written
1241 * in the cache has been output to the metadata stream to avoid corrupting the
1242 * metadata file.
1243 *
1244 * Return 0 on success, a negative value on error.
1245 */
1246 static
1247 int lttng_metadata_cache_dump(struct lttng_metadata_stream *stream)
1248 {
1249 int ret;
1250 struct lttng_metadata_cache *cache = stream->metadata_cache;
1251
1252 mutex_lock(&cache->lock);
1253 if (stream->metadata_out != cache->metadata_written) {
1254 ret = -EBUSY;
1255 goto end;
1256 }
1257 stream->metadata_out = 0;
1258 stream->metadata_in = 0;
1259 wake_up_interruptible(&stream->read_wait);
1260 ret = 0;
1261
1262 end:
1263 mutex_unlock(&cache->lock);
1264 return ret;
1265 }
1266
1267 static
1268 long lttng_metadata_ring_buffer_ioctl(struct file *filp,
1269 unsigned int cmd, unsigned long arg)
1270 {
1271 int ret;
1272 struct lttng_metadata_stream *stream = filp->private_data;
1273 struct lib_ring_buffer *buf = stream->priv;
1274 unsigned int rb_cmd;
1275 bool coherent;
1276
1277 if (cmd == LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK)
1278 rb_cmd = LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF;
1279 else
1280 rb_cmd = cmd;
1281
1282 switch (cmd) {
1283 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF:
1284 {
1285 struct lttng_metadata_stream *stream = filp->private_data;
1286 struct lib_ring_buffer *buf = stream->priv;
1287 struct channel *chan = buf->backend.chan;
1288
1289 ret = lttng_metadata_output_channel(stream, chan, NULL);
1290 if (ret > 0) {
1291 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
1292 ret = 0;
1293 } else if (ret < 0)
1294 goto err;
1295 break;
1296 }
1297 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_SUBBUF:
1298 {
1299 /*
1300 * Random access is not allowed for metadata channel.
1301 */
1302 return -ENOSYS;
1303 }
1304 case LTTNG_KERNEL_ABI_RING_BUFFER_FLUSH_EMPTY: /* Fall-through. */
1305 case LTTNG_KERNEL_ABI_RING_BUFFER_FLUSH:
1306 {
1307 struct lttng_metadata_stream *stream = filp->private_data;
1308 struct lib_ring_buffer *buf = stream->priv;
1309 struct channel *chan = buf->backend.chan;
1310
1311 /*
1312 * Before doing the actual ring buffer flush, write up to one
1313 * packet of metadata in the ring buffer.
1314 */
1315 ret = lttng_metadata_output_channel(stream, chan, NULL);
1316 if (ret < 0)
1317 goto err;
1318 break;
1319 }
1320 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_METADATA_VERSION:
1321 {
1322 struct lttng_metadata_stream *stream = filp->private_data;
1323
1324 return put_u64(stream->version, arg);
1325 }
1326 case LTTNG_KERNEL_ABI_RING_BUFFER_METADATA_CACHE_DUMP:
1327 {
1328 struct lttng_metadata_stream *stream = filp->private_data;
1329
1330 return lttng_metadata_cache_dump(stream);
1331 }
1332 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
1333 {
1334 struct lttng_metadata_stream *stream = filp->private_data;
1335 struct lib_ring_buffer *buf = stream->priv;
1336 struct channel *chan = buf->backend.chan;
1337
1338 ret = lttng_metadata_output_channel(stream, chan, &coherent);
1339 if (ret > 0) {
1340 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
1341 ret = 0;
1342 } else if (ret < 0) {
1343 goto err;
1344 }
1345 break;
1346 }
1347 default:
1348 break;
1349 }
1350 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
1351
1352 /* Performing lib ring buffer ioctl after our own. */
1353 ret = lib_ring_buffer_ioctl(filp, rb_cmd, arg, buf);
1354 if (ret < 0)
1355 goto err;
1356
1357 switch (cmd) {
1358 case LTTNG_KERNEL_ABI_RING_BUFFER_PUT_NEXT_SUBBUF:
1359 {
1360 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
1361 cmd, arg);
1362 break;
1363 }
1364 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
1365 {
1366 return put_u32(coherent, arg);
1367 }
1368 default:
1369 break;
1370 }
1371 err:
1372 return ret;
1373 }
1374
1375 #ifdef CONFIG_COMPAT
1376 static
1377 long lttng_metadata_ring_buffer_compat_ioctl(struct file *filp,
1378 unsigned int cmd, unsigned long arg)
1379 {
1380 int ret;
1381 struct lttng_metadata_stream *stream = filp->private_data;
1382 struct lib_ring_buffer *buf = stream->priv;
1383 unsigned int rb_cmd;
1384 bool coherent;
1385
1386 if (cmd == LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK)
1387 rb_cmd = LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF;
1388 else
1389 rb_cmd = cmd;
1390
1391 switch (cmd) {
1392 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF:
1393 {
1394 struct lttng_metadata_stream *stream = filp->private_data;
1395 struct lib_ring_buffer *buf = stream->priv;
1396 struct channel *chan = buf->backend.chan;
1397
1398 ret = lttng_metadata_output_channel(stream, chan, NULL);
1399 if (ret > 0) {
1400 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
1401 ret = 0;
1402 } else if (ret < 0)
1403 goto err;
1404 break;
1405 }
1406 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_SUBBUF:
1407 {
1408 /*
1409 * Random access is not allowed for metadata channel.
1410 */
1411 return -ENOSYS;
1412 }
1413 case LTTNG_KERNEL_ABI_RING_BUFFER_FLUSH_EMPTY: /* Fall-through. */
1414 case LTTNG_KERNEL_ABI_RING_BUFFER_FLUSH:
1415 {
1416 struct lttng_metadata_stream *stream = filp->private_data;
1417 struct lib_ring_buffer *buf = stream->priv;
1418 struct channel *chan = buf->backend.chan;
1419
1420 /*
1421 * Before doing the actual ring buffer flush, write up to one
1422 * packet of metadata in the ring buffer.
1423 */
1424 ret = lttng_metadata_output_channel(stream, chan, NULL);
1425 if (ret < 0)
1426 goto err;
1427 break;
1428 }
1429 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_METADATA_VERSION:
1430 {
1431 struct lttng_metadata_stream *stream = filp->private_data;
1432
1433 return put_u64(stream->version, arg);
1434 }
1435 case LTTNG_KERNEL_ABI_RING_BUFFER_METADATA_CACHE_DUMP:
1436 {
1437 struct lttng_metadata_stream *stream = filp->private_data;
1438
1439 return lttng_metadata_cache_dump(stream);
1440 }
1441 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
1442 {
1443 struct lttng_metadata_stream *stream = filp->private_data;
1444 struct lib_ring_buffer *buf = stream->priv;
1445 struct channel *chan = buf->backend.chan;
1446
1447 ret = lttng_metadata_output_channel(stream, chan, &coherent);
1448 if (ret > 0) {
1449 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
1450 ret = 0;
1451 } else if (ret < 0) {
1452 goto err;
1453 }
1454 break;
1455 }
1456 default:
1457 break;
1458 }
1459 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
1460
1461 /* Performing lib ring buffer ioctl after our own. */
1462 ret = lib_ring_buffer_compat_ioctl(filp, rb_cmd, arg, buf);
1463 if (ret < 0)
1464 goto err;
1465
1466 switch (cmd) {
1467 case LTTNG_KERNEL_ABI_RING_BUFFER_PUT_NEXT_SUBBUF:
1468 {
1469 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
1470 cmd, arg);
1471 break;
1472 }
1473 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
1474 {
1475 return put_u32(coherent, arg);
1476 }
1477 default:
1478 break;
1479 }
1480 err:
1481 return ret;
1482 }
1483 #endif
1484
1485 /*
1486 * This is not used by anonymous file descriptors. This code is left
1487 * there if we ever want to implement an inode with open() operation.
1488 */
1489 static
1490 int lttng_metadata_ring_buffer_open(struct inode *inode, struct file *file)
1491 {
1492 struct lttng_metadata_stream *stream = inode->i_private;
1493 struct lib_ring_buffer *buf = stream->priv;
1494
1495 file->private_data = buf;
1496 /*
1497 * Since life-time of metadata cache differs from that of
1498 * session, we need to keep our own reference on the transport.
1499 */
1500 if (!try_module_get(stream->transport->owner)) {
1501 printk(KERN_WARNING "LTTng: Can't lock transport module.\n");
1502 return -EBUSY;
1503 }
1504 return lib_ring_buffer_open(inode, file, buf);
1505 }
1506
1507 static
1508 int lttng_metadata_ring_buffer_release(struct inode *inode, struct file *file)
1509 {
1510 struct lttng_metadata_stream *stream = file->private_data;
1511 struct lib_ring_buffer *buf = stream->priv;
1512
1513 mutex_lock(&stream->metadata_cache->lock);
1514 list_del(&stream->list);
1515 mutex_unlock(&stream->metadata_cache->lock);
1516 kref_put(&stream->metadata_cache->refcount, metadata_cache_destroy);
1517 module_put(stream->transport->owner);
1518 kfree(stream);
1519 return lib_ring_buffer_release(inode, file, buf);
1520 }
1521
1522 static
1523 ssize_t lttng_metadata_ring_buffer_splice_read(struct file *in, loff_t *ppos,
1524 struct pipe_inode_info *pipe, size_t len,
1525 unsigned int flags)
1526 {
1527 struct lttng_metadata_stream *stream = in->private_data;
1528 struct lib_ring_buffer *buf = stream->priv;
1529
1530 return lib_ring_buffer_splice_read(in, ppos, pipe, len,
1531 flags, buf);
1532 }
1533
1534 static
1535 int lttng_metadata_ring_buffer_mmap(struct file *filp,
1536 struct vm_area_struct *vma)
1537 {
1538 struct lttng_metadata_stream *stream = filp->private_data;
1539 struct lib_ring_buffer *buf = stream->priv;
1540
1541 return lib_ring_buffer_mmap(filp, vma, buf);
1542 }
1543
1544 static
1545 const struct file_operations lttng_metadata_ring_buffer_file_operations = {
1546 .owner = THIS_MODULE,
1547 .open = lttng_metadata_ring_buffer_open,
1548 .release = lttng_metadata_ring_buffer_release,
1549 .poll = lttng_metadata_ring_buffer_poll,
1550 .splice_read = lttng_metadata_ring_buffer_splice_read,
1551 .mmap = lttng_metadata_ring_buffer_mmap,
1552 .unlocked_ioctl = lttng_metadata_ring_buffer_ioctl,
1553 .llseek = vfs_lib_ring_buffer_no_llseek,
1554 #ifdef CONFIG_COMPAT
1555 .compat_ioctl = lttng_metadata_ring_buffer_compat_ioctl,
1556 #endif
1557 };
1558
1559 static
1560 int lttng_abi_create_stream_fd(struct file *channel_file, void *stream_priv,
1561 const struct file_operations *fops, const char *name)
1562 {
1563 int stream_fd, ret;
1564 struct file *stream_file;
1565
1566 stream_fd = lttng_get_unused_fd();
1567 if (stream_fd < 0) {
1568 ret = stream_fd;
1569 goto fd_error;
1570 }
1571 stream_file = anon_inode_getfile(name, fops, stream_priv, O_RDWR);
1572 if (IS_ERR(stream_file)) {
1573 ret = PTR_ERR(stream_file);
1574 goto file_error;
1575 }
1576 /*
1577 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
1578 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
1579 * file descriptor, so we set FMODE_PREAD here.
1580 */
1581 stream_file->f_mode |= FMODE_PREAD;
1582 fd_install(stream_fd, stream_file);
1583 /*
1584 * The stream holds a reference to the channel within the generic ring
1585 * buffer library, so no need to hold a refcount on the channel and
1586 * session files here.
1587 */
1588 return stream_fd;
1589
1590 file_error:
1591 put_unused_fd(stream_fd);
1592 fd_error:
1593 return ret;
1594 }
1595
1596 static
1597 int lttng_abi_open_stream(struct file *channel_file)
1598 {
1599 struct lttng_channel *channel = channel_file->private_data;
1600 struct lib_ring_buffer *buf;
1601 int ret;
1602 void *stream_priv;
1603
1604 buf = channel->ops->buffer_read_open(channel->chan);
1605 if (!buf)
1606 return -ENOENT;
1607
1608 stream_priv = buf;
1609 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
1610 &lttng_stream_ring_buffer_file_operations,
1611 "[lttng_stream]");
1612 if (ret < 0)
1613 goto fd_error;
1614
1615 return ret;
1616
1617 fd_error:
1618 channel->ops->buffer_read_close(buf);
1619 return ret;
1620 }
1621
1622 static
1623 int lttng_abi_open_metadata_stream(struct file *channel_file)
1624 {
1625 struct lttng_channel *channel = channel_file->private_data;
1626 struct lttng_session *session = channel->session;
1627 struct lib_ring_buffer *buf;
1628 int ret;
1629 struct lttng_metadata_stream *metadata_stream;
1630 void *stream_priv;
1631
1632 buf = channel->ops->buffer_read_open(channel->chan);
1633 if (!buf)
1634 return -ENOENT;
1635
1636 metadata_stream = kzalloc(sizeof(struct lttng_metadata_stream),
1637 GFP_KERNEL);
1638 if (!metadata_stream) {
1639 ret = -ENOMEM;
1640 goto nomem;
1641 }
1642 metadata_stream->metadata_cache = session->metadata_cache;
1643 init_waitqueue_head(&metadata_stream->read_wait);
1644 metadata_stream->priv = buf;
1645 stream_priv = metadata_stream;
1646 metadata_stream->transport = channel->transport;
1647 /* Initial state is an empty metadata, considered as incoherent. */
1648 metadata_stream->coherent = false;
1649
1650 /*
1651 * Since life-time of metadata cache differs from that of
1652 * session, we need to keep our own reference on the transport.
1653 */
1654 if (!try_module_get(metadata_stream->transport->owner)) {
1655 printk(KERN_WARNING "LTTng: Can't lock transport module.\n");
1656 ret = -EINVAL;
1657 goto notransport;
1658 }
1659
1660 if (!lttng_kref_get(&session->metadata_cache->refcount)) {
1661 ret = -EOVERFLOW;
1662 goto kref_error;
1663 }
1664
1665 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
1666 &lttng_metadata_ring_buffer_file_operations,
1667 "[lttng_metadata_stream]");
1668 if (ret < 0)
1669 goto fd_error;
1670
1671 mutex_lock(&session->metadata_cache->lock);
1672 list_add(&metadata_stream->list,
1673 &session->metadata_cache->metadata_stream);
1674 mutex_unlock(&session->metadata_cache->lock);
1675 return ret;
1676
1677 fd_error:
1678 kref_put(&session->metadata_cache->refcount, metadata_cache_destroy);
1679 kref_error:
1680 module_put(metadata_stream->transport->owner);
1681 notransport:
1682 kfree(metadata_stream);
1683 nomem:
1684 channel->ops->buffer_read_close(buf);
1685 return ret;
1686 }
1687
1688 static
1689 int lttng_abi_open_event_notifier_group_stream(struct file *notif_file)
1690 {
1691 struct lttng_event_notifier_group *event_notifier_group = notif_file->private_data;
1692 struct channel *chan = event_notifier_group->chan;
1693 struct lib_ring_buffer *buf;
1694 int ret;
1695 void *stream_priv;
1696
1697 buf = event_notifier_group->ops->buffer_read_open(chan);
1698 if (!buf)
1699 return -ENOENT;
1700
1701 /* The event_notifier notification fd holds a reference on the event_notifier group */
1702 if (!atomic_long_add_unless(&notif_file->f_count, 1, LONG_MAX)) {
1703 ret = -EOVERFLOW;
1704 goto refcount_error;
1705 }
1706 event_notifier_group->buf = buf;
1707 stream_priv = event_notifier_group;
1708 ret = lttng_abi_create_stream_fd(notif_file, stream_priv,
1709 &lttng_event_notifier_group_notif_fops,
1710 "[lttng_event_notifier_stream]");
1711 if (ret < 0)
1712 goto fd_error;
1713
1714 return ret;
1715
1716 fd_error:
1717 atomic_long_dec(&notif_file->f_count);
1718 refcount_error:
1719 event_notifier_group->ops->buffer_read_close(buf);
1720 return ret;
1721 }
1722
1723 static
1724 int lttng_abi_validate_event_param(struct lttng_kernel_abi_event *event_param)
1725 {
1726 /* Limit ABI to implemented features. */
1727 switch (event_param->instrumentation) {
1728 case LTTNG_KERNEL_ABI_SYSCALL:
1729 switch (event_param->u.syscall.entryexit) {
1730 case LTTNG_KERNEL_ABI_SYSCALL_ENTRY: /* Fall-through */
1731 case LTTNG_KERNEL_ABI_SYSCALL_EXIT: /* Fall-through */
1732 case LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT:
1733 break;
1734 default:
1735 return -EINVAL;
1736 }
1737 switch (event_param->u.syscall.abi) {
1738 case LTTNG_KERNEL_ABI_SYSCALL_ABI_ALL:
1739 break;
1740 default:
1741 return -EINVAL;
1742 }
1743 switch (event_param->u.syscall.match) {
1744 case LTTNG_KERNEL_ABI_SYSCALL_MATCH_NAME:
1745 break;
1746 default:
1747 return -EINVAL;
1748 }
1749 break;
1750
1751 case LTTNG_KERNEL_ABI_KRETPROBE:
1752 switch (event_param->u.kretprobe.entryexit) {
1753 case LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT:
1754 break;
1755 case LTTNG_KERNEL_ABI_SYSCALL_ENTRY: /* Fall-through */
1756 case LTTNG_KERNEL_ABI_SYSCALL_EXIT: /* Fall-through */
1757 default:
1758 return -EINVAL;
1759 }
1760 break;
1761
1762 case LTTNG_KERNEL_ABI_TRACEPOINT: /* Fall-through */
1763 case LTTNG_KERNEL_ABI_KPROBE: /* Fall-through */
1764 case LTTNG_KERNEL_ABI_UPROBE:
1765 break;
1766
1767 case LTTNG_KERNEL_ABI_FUNCTION: /* Fall-through */
1768 case LTTNG_KERNEL_ABI_NOOP: /* Fall-through */
1769 default:
1770 return -EINVAL;
1771 }
1772 return 0;
1773 }
1774
1775 static
1776 int lttng_abi_create_event(struct file *channel_file,
1777 struct lttng_kernel_abi_event *event_param)
1778 {
1779 const struct file_operations *fops;
1780 struct lttng_channel *channel = channel_file->private_data;
1781 int event_fd, ret;
1782 struct file *event_file;
1783 void *priv;
1784
1785 event_param->name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0';
1786 switch (event_param->instrumentation) {
1787 case LTTNG_KERNEL_ABI_KRETPROBE:
1788 event_param->u.kretprobe.symbol_name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0';
1789 break;
1790 case LTTNG_KERNEL_ABI_KPROBE:
1791 event_param->u.kprobe.symbol_name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0';
1792 break;
1793 case LTTNG_KERNEL_ABI_FUNCTION:
1794 WARN_ON_ONCE(1);
1795 /* Not implemented. */
1796 break;
1797 default:
1798 break;
1799 }
1800
1801 switch (event_param->instrumentation) {
1802 case LTTNG_KERNEL_ABI_TRACEPOINT: /* Fall-through */
1803 case LTTNG_KERNEL_ABI_SYSCALL:
1804 fops = &lttng_event_recorder_enabler_fops;
1805 break;
1806 case LTTNG_KERNEL_ABI_KPROBE: /* Fall-through */
1807 case LTTNG_KERNEL_ABI_KRETPROBE: /* Fall-through */
1808 case LTTNG_KERNEL_ABI_UPROBE:
1809 fops = &lttng_event_recorder_event_fops;
1810 break;
1811
1812 case LTTNG_KERNEL_ABI_FUNCTION: /* Fall-through */
1813 case LTTNG_KERNEL_ABI_NOOP: /* Fall-through */
1814 default:
1815 return -EINVAL;
1816 }
1817
1818 event_fd = lttng_get_unused_fd();
1819 if (event_fd < 0) {
1820 ret = event_fd;
1821 goto fd_error;
1822 }
1823 event_file = anon_inode_getfile("[lttng_event]",
1824 fops, NULL, O_RDWR);
1825 if (IS_ERR(event_file)) {
1826 ret = PTR_ERR(event_file);
1827 goto file_error;
1828 }
1829 /* The event holds a reference on the channel */
1830 if (!atomic_long_add_unless(&channel_file->f_count, 1, LONG_MAX)) {
1831 ret = -EOVERFLOW;
1832 goto refcount_error;
1833 }
1834 ret = lttng_abi_validate_event_param(event_param);
1835 if (ret)
1836 goto event_error;
1837
1838 switch (event_param->instrumentation) {
1839 case LTTNG_KERNEL_ABI_TRACEPOINT: /* Fall-through */
1840 case LTTNG_KERNEL_ABI_SYSCALL:
1841 {
1842 struct lttng_event_enabler *event_enabler;
1843
1844 if (strutils_is_star_glob_pattern(event_param->name)) {
1845 /*
1846 * If the event name is a star globbing pattern,
1847 * we create the special star globbing enabler.
1848 */
1849 event_enabler = lttng_event_enabler_create(LTTNG_ENABLER_FORMAT_STAR_GLOB,
1850 event_param, channel);
1851 } else {
1852 event_enabler = lttng_event_enabler_create(LTTNG_ENABLER_FORMAT_NAME,
1853 event_param, channel);
1854 }
1855 priv = event_enabler;
1856 break;
1857 }
1858
1859 case LTTNG_KERNEL_ABI_KPROBE: /* Fall-through */
1860 case LTTNG_KERNEL_ABI_KRETPROBE: /* Fall-through */
1861 case LTTNG_KERNEL_ABI_UPROBE:
1862 {
1863 struct lttng_event *event;
1864
1865 /*
1866 * We tolerate no failure path after event creation. It
1867 * will stay invariant for the rest of the session.
1868 */
1869 event = lttng_event_create(channel, event_param,
1870 NULL, NULL,
1871 event_param->instrumentation);
1872 WARN_ON_ONCE(!event);
1873 if (IS_ERR(event)) {
1874 ret = PTR_ERR(event);
1875 goto event_error;
1876 }
1877 priv = event;
1878 break;
1879 }
1880
1881 case LTTNG_KERNEL_ABI_FUNCTION: /* Fall-through */
1882 case LTTNG_KERNEL_ABI_NOOP: /* Fall-through */
1883 default:
1884 ret = -EINVAL;
1885 goto event_error;
1886 }
1887 event_file->private_data = priv;
1888 fd_install(event_fd, event_file);
1889 return event_fd;
1890
1891 event_error:
1892 atomic_long_dec(&channel_file->f_count);
1893 refcount_error:
1894 fput(event_file);
1895 file_error:
1896 put_unused_fd(event_fd);
1897 fd_error:
1898 return ret;
1899 }
1900
1901 static
1902 long lttng_event_notifier_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1903 {
1904 struct lttng_event_notifier *event_notifier = file->private_data;
1905
1906 switch (cmd) {
1907 case LTTNG_KERNEL_ABI_ENABLE:
1908 return lttng_event_notifier_enable(event_notifier);
1909 case LTTNG_KERNEL_ABI_DISABLE:
1910 return lttng_event_notifier_disable(event_notifier);
1911 case LTTNG_KERNEL_ABI_FILTER:
1912 return -EINVAL;
1913 case LTTNG_KERNEL_ABI_CAPTURE:
1914 return -EINVAL;
1915 case LTTNG_KERNEL_ABI_ADD_CALLSITE:
1916 return lttng_event_notifier_add_callsite(event_notifier,
1917 (struct lttng_kernel_abi_event_callsite __user *) arg);
1918 default:
1919 return -ENOIOCTLCMD;
1920 }
1921 }
1922
1923 static
1924 long lttng_event_notifier_enabler_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1925 {
1926 struct lttng_event_notifier_enabler *event_notifier_enabler = file->private_data;
1927
1928 switch (cmd) {
1929 case LTTNG_KERNEL_ABI_ENABLE:
1930 return lttng_event_notifier_enabler_enable(event_notifier_enabler);
1931 case LTTNG_KERNEL_ABI_DISABLE:
1932 return lttng_event_notifier_enabler_disable(event_notifier_enabler);
1933 case LTTNG_KERNEL_ABI_FILTER:
1934 return lttng_event_notifier_enabler_attach_filter_bytecode(
1935 event_notifier_enabler,
1936 (struct lttng_kernel_abi_filter_bytecode __user *) arg);
1937 case LTTNG_KERNEL_ABI_CAPTURE:
1938 return lttng_event_notifier_enabler_attach_capture_bytecode(
1939 event_notifier_enabler,
1940 (struct lttng_kernel_abi_capture_bytecode __user *) arg);
1941 case LTTNG_KERNEL_ABI_ADD_CALLSITE:
1942 return -EINVAL;
1943 default:
1944 return -ENOIOCTLCMD;
1945 }
1946 }
1947
1948 static
1949 int lttng_event_notifier_event_release(struct inode *inode, struct file *file)
1950 {
1951 struct lttng_event_notifier *event_notifier = file->private_data;
1952
1953 if (event_notifier)
1954 fput(event_notifier->group->file);
1955 return 0;
1956 }
1957
1958 static
1959 int lttng_event_notifier_enabler_release(struct inode *inode, struct file *file)
1960 {
1961 struct lttng_event_notifier_enabler *event_notifier_enabler = file->private_data;
1962
1963 if (event_notifier_enabler)
1964 fput(event_notifier_enabler->group->file);
1965 return 0;
1966 }
1967
1968 static const struct file_operations lttng_event_notifier_event_fops = {
1969 .owner = THIS_MODULE,
1970 .release = lttng_event_notifier_event_release,
1971 .unlocked_ioctl = lttng_event_notifier_event_ioctl,
1972 #ifdef CONFIG_COMPAT
1973 .compat_ioctl = lttng_event_notifier_event_ioctl,
1974 #endif
1975 };
1976
1977 static const struct file_operations lttng_event_notifier_enabler_fops = {
1978 .owner = THIS_MODULE,
1979 .release = lttng_event_notifier_enabler_release,
1980 .unlocked_ioctl = lttng_event_notifier_enabler_ioctl,
1981 #ifdef CONFIG_COMPAT
1982 .compat_ioctl = lttng_event_notifier_enabler_ioctl,
1983 #endif
1984 };
1985
1986 static
1987 int lttng_abi_create_event_notifier(struct file *event_notifier_group_file,
1988 struct lttng_kernel_abi_event_notifier *event_notifier_param)
1989 {
1990 struct lttng_event_notifier_group *event_notifier_group =
1991 event_notifier_group_file->private_data;
1992 const struct file_operations *fops;
1993 int event_notifier_fd, ret;
1994 struct file *event_notifier_file;
1995 void *priv;
1996
1997 switch (event_notifier_param->event.instrumentation) {
1998 case LTTNG_KERNEL_ABI_TRACEPOINT:
1999 case LTTNG_KERNEL_ABI_UPROBE:
2000 break;
2001 case LTTNG_KERNEL_ABI_KPROBE:
2002 event_notifier_param->event.u.kprobe.symbol_name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0';
2003 break;
2004 case LTTNG_KERNEL_ABI_SYSCALL:
2005 break;
2006 case LTTNG_KERNEL_ABI_KRETPROBE:
2007 /* Placing an event notifier on kretprobe is not supported. */
2008 case LTTNG_KERNEL_ABI_FUNCTION:
2009 case LTTNG_KERNEL_ABI_NOOP:
2010 default:
2011 ret = -EINVAL;
2012 goto inval_instr;
2013 }
2014
2015 switch (event_notifier_param->event.instrumentation) {
2016 case LTTNG_KERNEL_ABI_TRACEPOINT: /* Fall-through */
2017 case LTTNG_KERNEL_ABI_SYSCALL:
2018 fops = &lttng_event_notifier_enabler_fops;
2019 break;
2020 case LTTNG_KERNEL_ABI_KPROBE: /* Fall-through */
2021 case LTTNG_KERNEL_ABI_KRETPROBE: /* Fall-through */
2022 case LTTNG_KERNEL_ABI_UPROBE:
2023 fops = &lttng_event_notifier_event_fops;
2024 break;
2025
2026 case LTTNG_KERNEL_ABI_FUNCTION: /* Fall-through */
2027 case LTTNG_KERNEL_ABI_NOOP: /* Fall-through */
2028 default:
2029 ret = -EINVAL;
2030 goto inval_instr;
2031 }
2032
2033 event_notifier_param->event.name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0';
2034
2035 event_notifier_fd = lttng_get_unused_fd();
2036 if (event_notifier_fd < 0) {
2037 ret = event_notifier_fd;
2038 goto fd_error;
2039 }
2040
2041 event_notifier_file = anon_inode_getfile("[lttng_event_notifier]",
2042 fops, NULL, O_RDWR);
2043 if (IS_ERR(event_notifier_file)) {
2044 ret = PTR_ERR(event_notifier_file);
2045 goto file_error;
2046 }
2047
2048 /* The event notifier holds a reference on the event notifier group. */
2049 if (!atomic_long_add_unless(&event_notifier_group_file->f_count, 1, LONG_MAX)) {
2050 ret = -EOVERFLOW;
2051 goto refcount_error;
2052 }
2053
2054 ret = lttng_abi_validate_event_param(&event_notifier_param->event);
2055 if (ret)
2056 goto event_notifier_error;
2057
2058 switch (event_notifier_param->event.instrumentation) {
2059 case LTTNG_KERNEL_ABI_TRACEPOINT: /* Fall-through */
2060 case LTTNG_KERNEL_ABI_SYSCALL:
2061 {
2062 struct lttng_event_notifier_enabler *enabler;
2063
2064 if (strutils_is_star_glob_pattern(event_notifier_param->event.name)) {
2065 /*
2066 * If the event name is a star globbing pattern,
2067 * we create the special star globbing enabler.
2068 */
2069 enabler = lttng_event_notifier_enabler_create(
2070 event_notifier_group,
2071 LTTNG_ENABLER_FORMAT_STAR_GLOB,
2072 event_notifier_param);
2073 } else {
2074 enabler = lttng_event_notifier_enabler_create(
2075 event_notifier_group,
2076 LTTNG_ENABLER_FORMAT_NAME,
2077 event_notifier_param);
2078 }
2079 priv = enabler;
2080 break;
2081 }
2082
2083 case LTTNG_KERNEL_ABI_KPROBE: /* Fall-through */
2084 case LTTNG_KERNEL_ABI_KRETPROBE: /* Fall-through */
2085 case LTTNG_KERNEL_ABI_UPROBE:
2086 {
2087 struct lttng_event_notifier *event_notifier;
2088
2089 /*
2090 * We tolerate no failure path after event notifier creation.
2091 * It will stay invariant for the rest of the session.
2092 */
2093 event_notifier = lttng_event_notifier_create(NULL,
2094 event_notifier_param->event.token,
2095 event_notifier_param->error_counter_index,
2096 event_notifier_group,
2097 event_notifier_param, NULL,
2098 event_notifier_param->event.instrumentation);
2099 WARN_ON_ONCE(!event_notifier);
2100 if (IS_ERR(event_notifier)) {
2101 ret = PTR_ERR(event_notifier);
2102 goto event_notifier_error;
2103 }
2104 priv = event_notifier;
2105 break;
2106 }
2107
2108 case LTTNG_KERNEL_ABI_FUNCTION: /* Fall-through */
2109 case LTTNG_KERNEL_ABI_NOOP: /* Fall-through */
2110 default:
2111 ret = -EINVAL;
2112 goto event_notifier_error;
2113 }
2114 event_notifier_file->private_data = priv;
2115 fd_install(event_notifier_fd, event_notifier_file);
2116 return event_notifier_fd;
2117
2118 event_notifier_error:
2119 atomic_long_dec(&event_notifier_group_file->f_count);
2120 refcount_error:
2121 fput(event_notifier_file);
2122 file_error:
2123 put_unused_fd(event_notifier_fd);
2124 fd_error:
2125 inval_instr:
2126 return ret;
2127 }
2128
2129 static
2130 long lttng_abi_event_notifier_group_create_error_counter(
2131 struct file *event_notifier_group_file,
2132 const struct lttng_kernel_abi_counter_conf *error_counter_conf)
2133 {
2134 int counter_fd, ret;
2135 char *counter_transport_name;
2136 size_t counter_len;
2137 struct lttng_counter *counter = NULL;
2138 struct file *counter_file;
2139 struct lttng_event_notifier_group *event_notifier_group =
2140 (struct lttng_event_notifier_group *) event_notifier_group_file->private_data;
2141
2142 if (error_counter_conf->arithmetic != LTTNG_KERNEL_ABI_COUNTER_ARITHMETIC_MODULAR) {
2143 printk(KERN_ERR "LTTng: event_notifier: Error counter of the wrong arithmetic type.\n");
2144 return -EINVAL;
2145 }
2146
2147 if (error_counter_conf->number_dimensions != 1) {
2148 printk(KERN_ERR "LTTng: event_notifier: Error counter has more than one dimension.\n");
2149 return -EINVAL;
2150 }
2151
2152 switch (error_counter_conf->bitness) {
2153 case LTTNG_KERNEL_ABI_COUNTER_BITNESS_64:
2154 counter_transport_name = "counter-per-cpu-64-modular";
2155 break;
2156 case LTTNG_KERNEL_ABI_COUNTER_BITNESS_32:
2157 counter_transport_name = "counter-per-cpu-32-modular";
2158 break;
2159 default:
2160 return -EINVAL;
2161 }
2162
2163 /*
2164 * Lock sessions to provide mutual exclusion against concurrent
2165 * modification of event_notifier group, which would result in
2166 * overwriting the error counter if set concurrently.
2167 */
2168 lttng_lock_sessions();
2169
2170 if (event_notifier_group->error_counter) {
2171 printk(KERN_ERR "Error counter already created in event_notifier group\n");
2172 ret = -EBUSY;
2173 goto fd_error;
2174 }
2175
2176 counter_fd = lttng_get_unused_fd();
2177 if (counter_fd < 0) {
2178 ret = counter_fd;
2179 goto fd_error;
2180 }
2181
2182 counter_file = anon_inode_getfile("[lttng_counter]",
2183 &lttng_counter_fops,
2184 NULL, O_RDONLY);
2185 if (IS_ERR(counter_file)) {
2186 ret = PTR_ERR(counter_file);
2187 goto file_error;
2188 }
2189
2190 counter_len = error_counter_conf->dimensions[0].size;
2191
2192 if (!atomic_long_add_unless(&event_notifier_group_file->f_count, 1, LONG_MAX)) {
2193 ret = -EOVERFLOW;
2194 goto refcount_error;
2195 }
2196
2197 counter = lttng_kernel_counter_create(counter_transport_name,
2198 1, &counter_len);
2199 if (!counter) {
2200 ret = -EINVAL;
2201 goto counter_error;
2202 }
2203
2204 event_notifier_group->error_counter_len = counter_len;
2205 /*
2206 * store-release to publish error counter matches load-acquire
2207 * in record_error. Ensures the counter is created and the
2208 * error_counter_len is set before they are used.
2209 */
2210 lttng_smp_store_release(&event_notifier_group->error_counter, counter);
2211
2212 counter->file = counter_file;
2213 counter->owner = event_notifier_group->file;
2214 counter_file->private_data = counter;
2215 /* Ownership transferred. */
2216 counter = NULL;
2217
2218 fd_install(counter_fd, counter_file);
2219 lttng_unlock_sessions();
2220
2221 return counter_fd;
2222
2223 counter_error:
2224 atomic_long_dec(&event_notifier_group_file->f_count);
2225 refcount_error:
2226 fput(counter_file);
2227 file_error:
2228 put_unused_fd(counter_fd);
2229 fd_error:
2230 lttng_unlock_sessions();
2231 return ret;
2232 }
2233
2234 static
2235 long lttng_event_notifier_group_ioctl(struct file *file, unsigned int cmd,
2236 unsigned long arg)
2237 {
2238 switch (cmd) {
2239 case LTTNG_KERNEL_ABI_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD:
2240 {
2241 return lttng_abi_open_event_notifier_group_stream(file);
2242 }
2243 case LTTNG_KERNEL_ABI_EVENT_NOTIFIER_CREATE:
2244 {
2245 struct lttng_kernel_abi_event_notifier uevent_notifier_param;
2246
2247 if (copy_from_user(&uevent_notifier_param,
2248 (struct lttng_kernel_abi_event_notifier __user *) arg,
2249 sizeof(uevent_notifier_param)))
2250 return -EFAULT;
2251 return lttng_abi_create_event_notifier(file, &uevent_notifier_param);
2252 }
2253 case LTTNG_KERNEL_ABI_COUNTER:
2254 {
2255 struct lttng_kernel_abi_counter_conf uerror_counter_conf;
2256
2257 if (copy_from_user(&uerror_counter_conf,
2258 (struct lttng_kernel_abi_counter_conf __user *) arg,
2259 sizeof(uerror_counter_conf)))
2260 return -EFAULT;
2261 return lttng_abi_event_notifier_group_create_error_counter(file,
2262 &uerror_counter_conf);
2263 }
2264 default:
2265 return -ENOIOCTLCMD;
2266 }
2267 return 0;
2268 }
2269
2270 static
2271 int lttng_event_notifier_group_release(struct inode *inode, struct file *file)
2272 {
2273 struct lttng_event_notifier_group *event_notifier_group =
2274 file->private_data;
2275
2276 if (event_notifier_group)
2277 lttng_event_notifier_group_destroy(event_notifier_group);
2278 return 0;
2279 }
2280
2281 static const struct file_operations lttng_event_notifier_group_fops = {
2282 .owner = THIS_MODULE,
2283 .release = lttng_event_notifier_group_release,
2284 .unlocked_ioctl = lttng_event_notifier_group_ioctl,
2285 #ifdef CONFIG_COMPAT
2286 .compat_ioctl = lttng_event_notifier_group_ioctl,
2287 #endif
2288 };
2289
2290 /**
2291 * lttng_channel_ioctl - lttng syscall through ioctl
2292 *
2293 * @file: the file
2294 * @cmd: the command
2295 * @arg: command arg
2296 *
2297 * This ioctl implements lttng commands:
2298 * LTTNG_KERNEL_ABI_STREAM
2299 * Returns an event stream file descriptor or failure.
2300 * (typically, one event stream records events from one CPU)
2301 * LTTNG_KERNEL_ABI_EVENT
2302 * Returns an event file descriptor or failure.
2303 * LTTNG_KERNEL_ABI_CONTEXT
2304 * Prepend a context field to each event in the channel
2305 * LTTNG_KERNEL_ABI_ENABLE
2306 * Enable recording for events in this channel (weak enable)
2307 * LTTNG_KERNEL_ABI_DISABLE
2308 * Disable recording for events in this channel (strong disable)
2309 *
2310 * Channel and event file descriptors also hold a reference on the session.
2311 */
2312 static
2313 long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2314 {
2315 struct lttng_channel *channel = file->private_data;
2316
2317 switch (cmd) {
2318 case LTTNG_KERNEL_ABI_OLD_STREAM:
2319 case LTTNG_KERNEL_ABI_STREAM:
2320 return lttng_abi_open_stream(file);
2321 case LTTNG_KERNEL_ABI_OLD_EVENT:
2322 {
2323 struct lttng_kernel_abi_event *uevent_param;
2324 struct lttng_kernel_abi_old_event *old_uevent_param;
2325 int ret;
2326
2327 uevent_param = kmalloc(sizeof(struct lttng_kernel_abi_event),
2328 GFP_KERNEL);
2329 if (!uevent_param) {
2330 ret = -ENOMEM;
2331 goto old_event_end;
2332 }
2333 old_uevent_param = kmalloc(
2334 sizeof(struct lttng_kernel_abi_old_event),
2335 GFP_KERNEL);
2336 if (!old_uevent_param) {
2337 ret = -ENOMEM;
2338 goto old_event_error_free_param;
2339 }
2340 if (copy_from_user(old_uevent_param,
2341 (struct lttng_kernel_abi_old_event __user *) arg,
2342 sizeof(struct lttng_kernel_abi_old_event))) {
2343 ret = -EFAULT;
2344 goto old_event_error_free_old_param;
2345 }
2346
2347 memcpy(uevent_param->name, old_uevent_param->name,
2348 sizeof(uevent_param->name));
2349 uevent_param->instrumentation =
2350 old_uevent_param->instrumentation;
2351
2352 switch (old_uevent_param->instrumentation) {
2353 case LTTNG_KERNEL_ABI_KPROBE:
2354 uevent_param->u.kprobe.addr =
2355 old_uevent_param->u.kprobe.addr;
2356 uevent_param->u.kprobe.offset =
2357 old_uevent_param->u.kprobe.offset;
2358 memcpy(uevent_param->u.kprobe.symbol_name,
2359 old_uevent_param->u.kprobe.symbol_name,
2360 sizeof(uevent_param->u.kprobe.symbol_name));
2361 break;
2362 case LTTNG_KERNEL_ABI_KRETPROBE:
2363 uevent_param->u.kretprobe.addr =
2364 old_uevent_param->u.kretprobe.addr;
2365 uevent_param->u.kretprobe.offset =
2366 old_uevent_param->u.kretprobe.offset;
2367 memcpy(uevent_param->u.kretprobe.symbol_name,
2368 old_uevent_param->u.kretprobe.symbol_name,
2369 sizeof(uevent_param->u.kretprobe.symbol_name));
2370 break;
2371 case LTTNG_KERNEL_ABI_FUNCTION:
2372 WARN_ON_ONCE(1);
2373 /* Not implemented. */
2374 break;
2375 default:
2376 break;
2377 }
2378 ret = lttng_abi_create_event(file, uevent_param);
2379
2380 old_event_error_free_old_param:
2381 kfree(old_uevent_param);
2382 old_event_error_free_param:
2383 kfree(uevent_param);
2384 old_event_end:
2385 return ret;
2386 }
2387 case LTTNG_KERNEL_ABI_EVENT:
2388 {
2389 struct lttng_kernel_abi_event uevent_param;
2390
2391 if (copy_from_user(&uevent_param,
2392 (struct lttng_kernel_abi_event __user *) arg,
2393 sizeof(uevent_param)))
2394 return -EFAULT;
2395 return lttng_abi_create_event(file, &uevent_param);
2396 }
2397 case LTTNG_KERNEL_ABI_OLD_CONTEXT:
2398 {
2399 struct lttng_kernel_abi_context *ucontext_param;
2400 struct lttng_kernel_abi_old_context *old_ucontext_param;
2401 int ret;
2402
2403 ucontext_param = kmalloc(sizeof(struct lttng_kernel_abi_context),
2404 GFP_KERNEL);
2405 if (!ucontext_param) {
2406 ret = -ENOMEM;
2407 goto old_ctx_end;
2408 }
2409 old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_abi_old_context),
2410 GFP_KERNEL);
2411 if (!old_ucontext_param) {
2412 ret = -ENOMEM;
2413 goto old_ctx_error_free_param;
2414 }
2415
2416 if (copy_from_user(old_ucontext_param,
2417 (struct lttng_kernel_abi_old_context __user *) arg,
2418 sizeof(struct lttng_kernel_abi_old_context))) {
2419 ret = -EFAULT;
2420 goto old_ctx_error_free_old_param;
2421 }
2422 ucontext_param->ctx = old_ucontext_param->ctx;
2423 memcpy(ucontext_param->padding, old_ucontext_param->padding,
2424 sizeof(ucontext_param->padding));
2425 /* only type that uses the union */
2426 if (old_ucontext_param->ctx == LTTNG_KERNEL_ABI_CONTEXT_PERF_COUNTER) {
2427 ucontext_param->u.perf_counter.type =
2428 old_ucontext_param->u.perf_counter.type;
2429 ucontext_param->u.perf_counter.config =
2430 old_ucontext_param->u.perf_counter.config;
2431 memcpy(ucontext_param->u.perf_counter.name,
2432 old_ucontext_param->u.perf_counter.name,
2433 sizeof(ucontext_param->u.perf_counter.name));
2434 }
2435
2436 ret = lttng_abi_add_context(file,
2437 ucontext_param,
2438 &channel->ctx, channel->session);
2439
2440 old_ctx_error_free_old_param:
2441 kfree(old_ucontext_param);
2442 old_ctx_error_free_param:
2443 kfree(ucontext_param);
2444 old_ctx_end:
2445 return ret;
2446 }
2447 case LTTNG_KERNEL_ABI_CONTEXT:
2448 {
2449 struct lttng_kernel_abi_context ucontext_param;
2450
2451 if (copy_from_user(&ucontext_param,
2452 (struct lttng_kernel_abi_context __user *) arg,
2453 sizeof(ucontext_param)))
2454 return -EFAULT;
2455 return lttng_abi_add_context(file,
2456 &ucontext_param,
2457 &channel->ctx, channel->session);
2458 }
2459 case LTTNG_KERNEL_ABI_OLD_ENABLE:
2460 case LTTNG_KERNEL_ABI_ENABLE:
2461 return lttng_channel_enable(channel);
2462 case LTTNG_KERNEL_ABI_OLD_DISABLE:
2463 case LTTNG_KERNEL_ABI_DISABLE:
2464 return lttng_channel_disable(channel);
2465 case LTTNG_KERNEL_ABI_SYSCALL_MASK:
2466 return lttng_channel_syscall_mask(channel,
2467 (struct lttng_kernel_abi_syscall_mask __user *) arg);
2468 default:
2469 return -ENOIOCTLCMD;
2470 }
2471 }
2472
2473 /**
2474 * lttng_metadata_ioctl - lttng syscall through ioctl
2475 *
2476 * @file: the file
2477 * @cmd: the command
2478 * @arg: command arg
2479 *
2480 * This ioctl implements lttng commands:
2481 * LTTNG_KERNEL_ABI_STREAM
2482 * Returns an event stream file descriptor or failure.
2483 *
2484 * Channel and event file descriptors also hold a reference on the session.
2485 */
2486 static
2487 long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2488 {
2489 switch (cmd) {
2490 case LTTNG_KERNEL_ABI_OLD_STREAM:
2491 case LTTNG_KERNEL_ABI_STREAM:
2492 return lttng_abi_open_metadata_stream(file);
2493 default:
2494 return -ENOIOCTLCMD;
2495 }
2496 }
2497
2498 /**
2499 * lttng_channel_poll - lttng stream addition/removal monitoring
2500 *
2501 * @file: the file
2502 * @wait: poll table
2503 */
2504 unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
2505 {
2506 struct lttng_channel *channel = file->private_data;
2507 unsigned int mask = 0;
2508
2509 if (file->f_mode & FMODE_READ) {
2510 poll_wait_set_exclusive(wait);
2511 poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan),
2512 wait);
2513
2514 if (channel->ops->is_disabled(channel->chan))
2515 return POLLERR;
2516 if (channel->ops->is_finalized(channel->chan))
2517 return POLLHUP;
2518 if (channel->ops->buffer_has_read_closed_stream(channel->chan))
2519 return POLLIN | POLLRDNORM;
2520 return 0;
2521 }
2522 return mask;
2523
2524 }
2525
2526 static
2527 int lttng_channel_release(struct inode *inode, struct file *file)
2528 {
2529 struct lttng_channel *channel = file->private_data;
2530
2531 if (channel)
2532 fput(channel->session->file);
2533 return 0;
2534 }
2535
2536 static
2537 int lttng_metadata_channel_release(struct inode *inode, struct file *file)
2538 {
2539 struct lttng_channel *channel = file->private_data;
2540
2541 if (channel) {
2542 fput(channel->session->file);
2543 lttng_metadata_channel_destroy(channel);
2544 }
2545
2546 return 0;
2547 }
2548
2549 static const struct file_operations lttng_channel_fops = {
2550 .owner = THIS_MODULE,
2551 .release = lttng_channel_release,
2552 .poll = lttng_channel_poll,
2553 .unlocked_ioctl = lttng_channel_ioctl,
2554 #ifdef CONFIG_COMPAT
2555 .compat_ioctl = lttng_channel_ioctl,
2556 #endif
2557 };
2558
2559 static const struct file_operations lttng_metadata_fops = {
2560 .owner = THIS_MODULE,
2561 .release = lttng_metadata_channel_release,
2562 .unlocked_ioctl = lttng_metadata_ioctl,
2563 #ifdef CONFIG_COMPAT
2564 .compat_ioctl = lttng_metadata_ioctl,
2565 #endif
2566 };
2567
2568 /**
2569 * lttng_event_recorder_event_ioctl - lttng syscall through ioctl
2570 *
2571 * @file: the file
2572 * @cmd: the command
2573 * @arg: command arg
2574 *
2575 * This ioctl implements lttng commands:
2576 * LTTNG_KERNEL_ABI_CONTEXT
2577 * Prepend a context field to each record of this event
2578 * LTTNG_KERNEL_ABI_ENABLE
2579 * Enable recording for this event (weak enable)
2580 * LTTNG_KERNEL_ABI_DISABLE
2581 * Disable recording for this event (strong disable)
2582 */
2583 static
2584 long lttng_event_recorder_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2585 {
2586 struct lttng_event *event = file->private_data;
2587
2588 switch (cmd) {
2589 case LTTNG_KERNEL_ABI_OLD_CONTEXT:
2590 {
2591 /* Not implemented */
2592 return -ENOSYS;
2593 }
2594 case LTTNG_KERNEL_ABI_CONTEXT:
2595 {
2596 /* Not implemented */
2597 return -ENOSYS;
2598 }
2599 case LTTNG_KERNEL_ABI_OLD_ENABLE:
2600 case LTTNG_KERNEL_ABI_ENABLE:
2601 return lttng_event_enable(event);
2602 case LTTNG_KERNEL_ABI_OLD_DISABLE:
2603 case LTTNG_KERNEL_ABI_DISABLE:
2604 return lttng_event_disable(event);
2605 case LTTNG_KERNEL_ABI_FILTER:
2606 return -EINVAL;
2607 case LTTNG_KERNEL_ABI_ADD_CALLSITE:
2608 return lttng_event_add_callsite(event,
2609 (struct lttng_kernel_abi_event_callsite __user *) arg);
2610 default:
2611 return -ENOIOCTLCMD;
2612 }
2613 }
2614
2615 /**
2616 * lttng_event_recorder_enabler_ioctl - lttng syscall through ioctl
2617 *
2618 * @file: the file
2619 * @cmd: the command
2620 * @arg: command arg
2621 *
2622 * This ioctl implements lttng commands:
2623 * LTTNG_KERNEL_ABI_CONTEXT
2624 * Prepend a context field to each record of this event
2625 * LTTNG_KERNEL_ABI_ENABLE
2626 * Enable recording for this event (weak enable)
2627 * LTTNG_KERNEL_ABI_DISABLE
2628 * Disable recording for this event (strong disable)
2629 */
2630 static
2631 long lttng_event_recorder_enabler_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2632 {
2633 struct lttng_event_enabler *event_enabler = file->private_data;
2634
2635 switch (cmd) {
2636 case LTTNG_KERNEL_ABI_OLD_CONTEXT:
2637 {
2638 /* Not implemented */
2639 return -ENOSYS;
2640 }
2641 case LTTNG_KERNEL_ABI_CONTEXT:
2642 {
2643 /* Not implemented */
2644 return -ENOSYS;
2645 }
2646 case LTTNG_KERNEL_ABI_OLD_ENABLE:
2647 case LTTNG_KERNEL_ABI_ENABLE:
2648 return lttng_event_enabler_enable(event_enabler);
2649 case LTTNG_KERNEL_ABI_OLD_DISABLE:
2650 case LTTNG_KERNEL_ABI_DISABLE:
2651 return lttng_event_enabler_disable(event_enabler);
2652 case LTTNG_KERNEL_ABI_FILTER:
2653 return lttng_event_enabler_attach_filter_bytecode(
2654 event_enabler,
2655 (struct lttng_kernel_abi_filter_bytecode __user *) arg);
2656 case LTTNG_KERNEL_ABI_ADD_CALLSITE:
2657 return -EINVAL;
2658 default:
2659 return -ENOIOCTLCMD;
2660 }
2661 }
2662
2663 static
2664 int lttng_event_recorder_event_release(struct inode *inode, struct file *file)
2665 {
2666 struct lttng_event *event = file->private_data;
2667
2668 if (event)
2669 fput(event->chan->file);
2670 return 0;
2671 }
2672
2673 static
2674 int lttng_event_recorder_enabler_release(struct inode *inode, struct file *file)
2675 {
2676 struct lttng_event_enabler *event_enabler = file->private_data;
2677
2678 if (event_enabler)
2679 fput(event_enabler->chan->file);
2680 return 0;
2681 }
2682
2683 static const struct file_operations lttng_event_recorder_event_fops = {
2684 .owner = THIS_MODULE,
2685 .release = lttng_event_recorder_event_release,
2686 .unlocked_ioctl = lttng_event_recorder_event_ioctl,
2687 #ifdef CONFIG_COMPAT
2688 .compat_ioctl = lttng_event_recorder_event_ioctl,
2689 #endif
2690 };
2691
2692 static const struct file_operations lttng_event_recorder_enabler_fops = {
2693 .owner = THIS_MODULE,
2694 .release = lttng_event_recorder_enabler_release,
2695 .unlocked_ioctl = lttng_event_recorder_enabler_ioctl,
2696 #ifdef CONFIG_COMPAT
2697 .compat_ioctl = lttng_event_recorder_enabler_ioctl,
2698 #endif
2699 };
2700
2701 static int put_u64(uint64_t val, unsigned long arg)
2702 {
2703 return put_user(val, (uint64_t __user *) arg);
2704 }
2705
2706 static int put_u32(uint32_t val, unsigned long arg)
2707 {
2708 return put_user(val, (uint32_t __user *) arg);
2709 }
2710
2711 static long lttng_stream_ring_buffer_ioctl(struct file *filp,
2712 unsigned int cmd, unsigned long arg)
2713 {
2714 struct lib_ring_buffer *buf = filp->private_data;
2715 struct channel *chan = buf->backend.chan;
2716 const struct lib_ring_buffer_config *config = &chan->backend.config;
2717 const struct lttng_channel_ops *ops = chan->backend.priv_ops;
2718 int ret;
2719
2720 if (atomic_read(&chan->record_disabled))
2721 return -EIO;
2722
2723 switch (cmd) {
2724 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_TIMESTAMP_BEGIN:
2725 {
2726 uint64_t ts;
2727
2728 ret = ops->timestamp_begin(config, buf, &ts);
2729 if (ret < 0)
2730 goto error;
2731 return put_u64(ts, arg);
2732 }
2733 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_TIMESTAMP_END:
2734 {
2735 uint64_t ts;
2736
2737 ret = ops->timestamp_end(config, buf, &ts);
2738 if (ret < 0)
2739 goto error;
2740 return put_u64(ts, arg);
2741 }
2742 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_EVENTS_DISCARDED:
2743 {
2744 uint64_t ed;
2745
2746 ret = ops->events_discarded(config, buf, &ed);
2747 if (ret < 0)
2748 goto error;
2749 return put_u64(ed, arg);
2750 }
2751 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_CONTENT_SIZE:
2752 {
2753 uint64_t cs;
2754
2755 ret = ops->content_size(config, buf, &cs);
2756 if (ret < 0)
2757 goto error;
2758 return put_u64(cs, arg);
2759 }
2760 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_PACKET_SIZE:
2761 {
2762 uint64_t ps;
2763
2764 ret = ops->packet_size(config, buf, &ps);
2765 if (ret < 0)
2766 goto error;
2767 return put_u64(ps, arg);
2768 }
2769 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_STREAM_ID:
2770 {
2771 uint64_t si;
2772
2773 ret = ops->stream_id(config, buf, &si);
2774 if (ret < 0)
2775 goto error;
2776 return put_u64(si, arg);
2777 }
2778 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_CURRENT_TIMESTAMP:
2779 {
2780 uint64_t ts;
2781
2782 ret = ops->current_timestamp(config, buf, &ts);
2783 if (ret < 0)
2784 goto error;
2785 return put_u64(ts, arg);
2786 }
2787 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_SEQ_NUM:
2788 {
2789 uint64_t seq;
2790
2791 ret = ops->sequence_number(config, buf, &seq);
2792 if (ret < 0)
2793 goto error;
2794 return put_u64(seq, arg);
2795 }
2796 case LTTNG_KERNEL_ABI_RING_BUFFER_INSTANCE_ID:
2797 {
2798 uint64_t id;
2799
2800 ret = ops->instance_id(config, buf, &id);
2801 if (ret < 0)
2802 goto error;
2803 return put_u64(id, arg);
2804 }
2805 default:
2806 return lib_ring_buffer_file_operations.unlocked_ioctl(filp,
2807 cmd, arg);
2808 }
2809
2810 error:
2811 return -ENOSYS;
2812 }
2813
2814 #ifdef CONFIG_COMPAT
2815 static long lttng_stream_ring_buffer_compat_ioctl(struct file *filp,
2816 unsigned int cmd, unsigned long arg)
2817 {
2818 struct lib_ring_buffer *buf = filp->private_data;
2819 struct channel *chan = buf->backend.chan;
2820 const struct lib_ring_buffer_config *config = &chan->backend.config;
2821 const struct lttng_channel_ops *ops = chan->backend.priv_ops;
2822 int ret;
2823
2824 if (atomic_read(&chan->record_disabled))
2825 return -EIO;
2826
2827 switch (cmd) {
2828 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN:
2829 {
2830 uint64_t ts;
2831
2832 ret = ops->timestamp_begin(config, buf, &ts);
2833 if (ret < 0)
2834 goto error;
2835 return put_u64(ts, arg);
2836 }
2837 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_TIMESTAMP_END:
2838 {
2839 uint64_t ts;
2840
2841 ret = ops->timestamp_end(config, buf, &ts);
2842 if (ret < 0)
2843 goto error;
2844 return put_u64(ts, arg);
2845 }
2846 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED:
2847 {
2848 uint64_t ed;
2849
2850 ret = ops->events_discarded(config, buf, &ed);
2851 if (ret < 0)
2852 goto error;
2853 return put_u64(ed, arg);
2854 }
2855 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_CONTENT_SIZE:
2856 {
2857 uint64_t cs;
2858
2859 ret = ops->content_size(config, buf, &cs);
2860 if (ret < 0)
2861 goto error;
2862 return put_u64(cs, arg);
2863 }
2864 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_PACKET_SIZE:
2865 {
2866 uint64_t ps;
2867
2868 ret = ops->packet_size(config, buf, &ps);
2869 if (ret < 0)
2870 goto error;
2871 return put_u64(ps, arg);
2872 }
2873 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_STREAM_ID:
2874 {
2875 uint64_t si;
2876
2877 ret = ops->stream_id(config, buf, &si);
2878 if (ret < 0)
2879 goto error;
2880 return put_u64(si, arg);
2881 }
2882 case LTTNG_KERNEL_ABI_RING_BUFFER_GET_CURRENT_TIMESTAMP:
2883 {
2884 uint64_t ts;
2885
2886 ret = ops->current_timestamp(config, buf, &ts);
2887 if (ret < 0)
2888 goto error;
2889 return put_u64(ts, arg);
2890 }
2891 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_GET_SEQ_NUM:
2892 {
2893 uint64_t seq;
2894
2895 ret = ops->sequence_number(config, buf, &seq);
2896 if (ret < 0)
2897 goto error;
2898 return put_u64(seq, arg);
2899 }
2900 case LTTNG_KERNEL_ABI_RING_BUFFER_COMPAT_INSTANCE_ID:
2901 {
2902 uint64_t id;
2903
2904 ret = ops->instance_id(config, buf, &id);
2905 if (ret < 0)
2906 goto error;
2907 return put_u64(id, arg);
2908 }
2909 default:
2910 return lib_ring_buffer_file_operations.compat_ioctl(filp,
2911 cmd, arg);
2912 }
2913
2914 error:
2915 return -ENOSYS;
2916 }
2917 #endif /* CONFIG_COMPAT */
2918
2919 static void lttng_stream_override_ring_buffer_fops(void)
2920 {
2921 lttng_stream_ring_buffer_file_operations.owner = THIS_MODULE;
2922 lttng_stream_ring_buffer_file_operations.open =
2923 lib_ring_buffer_file_operations.open;
2924 lttng_stream_ring_buffer_file_operations.release =
2925 lib_ring_buffer_file_operations.release;
2926 lttng_stream_ring_buffer_file_operations.poll =
2927 lib_ring_buffer_file_operations.poll;
2928 lttng_stream_ring_buffer_file_operations.splice_read =
2929 lib_ring_buffer_file_operations.splice_read;
2930 lttng_stream_ring_buffer_file_operations.mmap =
2931 lib_ring_buffer_file_operations.mmap;
2932 lttng_stream_ring_buffer_file_operations.unlocked_ioctl =
2933 lttng_stream_ring_buffer_ioctl;
2934 lttng_stream_ring_buffer_file_operations.llseek =
2935 lib_ring_buffer_file_operations.llseek;
2936 #ifdef CONFIG_COMPAT
2937 lttng_stream_ring_buffer_file_operations.compat_ioctl =
2938 lttng_stream_ring_buffer_compat_ioctl;
2939 #endif
2940 }
2941
2942 int __init lttng_abi_init(void)
2943 {
2944 int ret = 0;
2945
2946 wrapper_vmalloc_sync_mappings();
2947 lttng_clock_ref();
2948
2949 ret = lttng_tp_mempool_init();
2950 if (ret) {
2951 goto error;
2952 }
2953
2954 lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL,
2955 &lttng_proc_ops, NULL);
2956
2957 if (!lttng_proc_dentry) {
2958 printk(KERN_ERR "LTTng: Error creating control file\n");
2959 ret = -ENOMEM;
2960 goto error;
2961 }
2962 lttng_stream_override_ring_buffer_fops();
2963 return 0;
2964
2965 error:
2966 lttng_tp_mempool_destroy();
2967 lttng_clock_unref();
2968 return ret;
2969 }
2970
2971 /* No __exit annotation because used by init error path too. */
2972 void lttng_abi_exit(void)
2973 {
2974 lttng_tp_mempool_destroy();
2975 lttng_clock_unref();
2976 if (lttng_proc_dentry)
2977 remove_proc_entry("lttng", NULL);
2978 }
This page took 0.129035 seconds and 4 git commands to generate.