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