Version 2.12.3
[lttng-modules.git] / lttng-abi.c
1 /* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1)
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 <wrapper/ringbuffer/vfs.h>
35 #include <wrapper/ringbuffer/backend.h>
36 #include <wrapper/ringbuffer/frontend.h>
37 #include <wrapper/poll.h>
38 #include <wrapper/file.h>
39 #include <wrapper/kref.h>
40 #include <lttng-string-utils.h>
41 #include <lttng-abi.h>
42 #include <lttng-abi-old.h>
43 #include <lttng-events.h>
44 #include <lttng-tracer.h>
45 #include <lttng-tp-mempool.h>
46 #include <lib/ringbuffer/frontend_types.h>
47
48 /*
49 * This is LTTng's own personal way to create a system call as an external
50 * module. We use ioctl() on /proc/lttng.
51 */
52
53 static struct proc_dir_entry *lttng_proc_dentry;
54
55 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0))
56 static const struct proc_ops lttng_proc_ops;
57 #else
58 static const struct file_operations lttng_proc_ops;
59 #endif
60
61 static const struct file_operations lttng_session_fops;
62 static const struct file_operations lttng_channel_fops;
63 static const struct file_operations lttng_metadata_fops;
64 static const struct file_operations lttng_event_fops;
65 static struct file_operations lttng_stream_ring_buffer_file_operations;
66
67 static int put_u64(uint64_t val, unsigned long arg);
68 static int put_u32(uint32_t val, unsigned long arg);
69
70 /*
71 * Teardown management: opened file descriptors keep a refcount on the module,
72 * so it can only exit when all file descriptors are closed.
73 */
74
75 static
76 int lttng_abi_create_session(void)
77 {
78 struct lttng_session *session;
79 struct file *session_file;
80 int session_fd, ret;
81
82 session = lttng_session_create();
83 if (!session)
84 return -ENOMEM;
85 session_fd = lttng_get_unused_fd();
86 if (session_fd < 0) {
87 ret = session_fd;
88 goto fd_error;
89 }
90 session_file = anon_inode_getfile("[lttng_session]",
91 &lttng_session_fops,
92 session, O_RDWR);
93 if (IS_ERR(session_file)) {
94 ret = PTR_ERR(session_file);
95 goto file_error;
96 }
97 session->file = session_file;
98 fd_install(session_fd, session_file);
99 return session_fd;
100
101 file_error:
102 put_unused_fd(session_fd);
103 fd_error:
104 lttng_session_destroy(session);
105 return ret;
106 }
107
108 static
109 int lttng_abi_tracepoint_list(void)
110 {
111 struct file *tracepoint_list_file;
112 int file_fd, ret;
113
114 file_fd = lttng_get_unused_fd();
115 if (file_fd < 0) {
116 ret = file_fd;
117 goto fd_error;
118 }
119
120 tracepoint_list_file = anon_inode_getfile("[lttng_tracepoint_list]",
121 &lttng_tracepoint_list_fops,
122 NULL, O_RDWR);
123 if (IS_ERR(tracepoint_list_file)) {
124 ret = PTR_ERR(tracepoint_list_file);
125 goto file_error;
126 }
127 ret = lttng_tracepoint_list_fops.open(NULL, tracepoint_list_file);
128 if (ret < 0)
129 goto open_error;
130 fd_install(file_fd, tracepoint_list_file);
131 return file_fd;
132
133 open_error:
134 fput(tracepoint_list_file);
135 file_error:
136 put_unused_fd(file_fd);
137 fd_error:
138 return ret;
139 }
140
141 #ifndef CONFIG_HAVE_SYSCALL_TRACEPOINTS
142 static inline
143 int lttng_abi_syscall_list(void)
144 {
145 return -ENOSYS;
146 }
147 #else
148 static
149 int lttng_abi_syscall_list(void)
150 {
151 struct file *syscall_list_file;
152 int file_fd, ret;
153
154 file_fd = lttng_get_unused_fd();
155 if (file_fd < 0) {
156 ret = file_fd;
157 goto fd_error;
158 }
159
160 syscall_list_file = anon_inode_getfile("[lttng_syscall_list]",
161 &lttng_syscall_list_fops,
162 NULL, O_RDWR);
163 if (IS_ERR(syscall_list_file)) {
164 ret = PTR_ERR(syscall_list_file);
165 goto file_error;
166 }
167 ret = lttng_syscall_list_fops.open(NULL, syscall_list_file);
168 if (ret < 0)
169 goto open_error;
170 fd_install(file_fd, syscall_list_file);
171 return file_fd;
172
173 open_error:
174 fput(syscall_list_file);
175 file_error:
176 put_unused_fd(file_fd);
177 fd_error:
178 return ret;
179 }
180 #endif
181
182 static
183 void lttng_abi_tracer_version(struct lttng_kernel_tracer_version *v)
184 {
185 v->major = LTTNG_MODULES_MAJOR_VERSION;
186 v->minor = LTTNG_MODULES_MINOR_VERSION;
187 v->patchlevel = LTTNG_MODULES_PATCHLEVEL_VERSION;
188 }
189
190 static
191 void lttng_abi_tracer_abi_version(struct lttng_kernel_tracer_abi_version *v)
192 {
193 v->major = LTTNG_MODULES_ABI_MAJOR_VERSION;
194 v->minor = LTTNG_MODULES_ABI_MINOR_VERSION;
195 }
196
197 static
198 long lttng_abi_add_context(struct file *file,
199 struct lttng_kernel_context *context_param,
200 struct lttng_ctx **ctx, struct lttng_session *session)
201 {
202
203 if (session->been_active)
204 return -EPERM;
205
206 switch (context_param->ctx) {
207 case LTTNG_KERNEL_CONTEXT_PID:
208 return lttng_add_pid_to_ctx(ctx);
209 case LTTNG_KERNEL_CONTEXT_PRIO:
210 return lttng_add_prio_to_ctx(ctx);
211 case LTTNG_KERNEL_CONTEXT_NICE:
212 return lttng_add_nice_to_ctx(ctx);
213 case LTTNG_KERNEL_CONTEXT_VPID:
214 return lttng_add_vpid_to_ctx(ctx);
215 case LTTNG_KERNEL_CONTEXT_TID:
216 return lttng_add_tid_to_ctx(ctx);
217 case LTTNG_KERNEL_CONTEXT_VTID:
218 return lttng_add_vtid_to_ctx(ctx);
219 case LTTNG_KERNEL_CONTEXT_PPID:
220 return lttng_add_ppid_to_ctx(ctx);
221 case LTTNG_KERNEL_CONTEXT_VPPID:
222 return lttng_add_vppid_to_ctx(ctx);
223 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER:
224 context_param->u.perf_counter.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
225 return lttng_add_perf_counter_to_ctx(context_param->u.perf_counter.type,
226 context_param->u.perf_counter.config,
227 context_param->u.perf_counter.name,
228 ctx);
229 case LTTNG_KERNEL_CONTEXT_PROCNAME:
230 return lttng_add_procname_to_ctx(ctx);
231 case LTTNG_KERNEL_CONTEXT_HOSTNAME:
232 return lttng_add_hostname_to_ctx(ctx);
233 case LTTNG_KERNEL_CONTEXT_CPU_ID:
234 return lttng_add_cpu_id_to_ctx(ctx);
235 case LTTNG_KERNEL_CONTEXT_INTERRUPTIBLE:
236 return lttng_add_interruptible_to_ctx(ctx);
237 case LTTNG_KERNEL_CONTEXT_NEED_RESCHEDULE:
238 return lttng_add_need_reschedule_to_ctx(ctx);
239 case LTTNG_KERNEL_CONTEXT_PREEMPTIBLE:
240 return lttng_add_preemptible_to_ctx(ctx);
241 case LTTNG_KERNEL_CONTEXT_MIGRATABLE:
242 return lttng_add_migratable_to_ctx(ctx);
243 case LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL:
244 case LTTNG_KERNEL_CONTEXT_CALLSTACK_USER:
245 return lttng_add_callstack_to_ctx(ctx, context_param->ctx);
246 case LTTNG_KERNEL_CONTEXT_CGROUP_NS:
247 return lttng_add_cgroup_ns_to_ctx(ctx);
248 case LTTNG_KERNEL_CONTEXT_IPC_NS:
249 return lttng_add_ipc_ns_to_ctx(ctx);
250 case LTTNG_KERNEL_CONTEXT_MNT_NS:
251 return lttng_add_mnt_ns_to_ctx(ctx);
252 case LTTNG_KERNEL_CONTEXT_NET_NS:
253 return lttng_add_net_ns_to_ctx(ctx);
254 case LTTNG_KERNEL_CONTEXT_PID_NS:
255 return lttng_add_pid_ns_to_ctx(ctx);
256 case LTTNG_KERNEL_CONTEXT_USER_NS:
257 return lttng_add_user_ns_to_ctx(ctx);
258 case LTTNG_KERNEL_CONTEXT_UTS_NS:
259 return lttng_add_uts_ns_to_ctx(ctx);
260 case LTTNG_KERNEL_CONTEXT_UID:
261 return lttng_add_uid_to_ctx(ctx);
262 case LTTNG_KERNEL_CONTEXT_EUID:
263 return lttng_add_euid_to_ctx(ctx);
264 case LTTNG_KERNEL_CONTEXT_SUID:
265 return lttng_add_suid_to_ctx(ctx);
266 case LTTNG_KERNEL_CONTEXT_GID:
267 return lttng_add_gid_to_ctx(ctx);
268 case LTTNG_KERNEL_CONTEXT_EGID:
269 return lttng_add_egid_to_ctx(ctx);
270 case LTTNG_KERNEL_CONTEXT_SGID:
271 return lttng_add_sgid_to_ctx(ctx);
272 case LTTNG_KERNEL_CONTEXT_VUID:
273 return lttng_add_vuid_to_ctx(ctx);
274 case LTTNG_KERNEL_CONTEXT_VEUID:
275 return lttng_add_veuid_to_ctx(ctx);
276 case LTTNG_KERNEL_CONTEXT_VSUID:
277 return lttng_add_vsuid_to_ctx(ctx);
278 case LTTNG_KERNEL_CONTEXT_VGID:
279 return lttng_add_vgid_to_ctx(ctx);
280 case LTTNG_KERNEL_CONTEXT_VEGID:
281 return lttng_add_vegid_to_ctx(ctx);
282 case LTTNG_KERNEL_CONTEXT_VSGID:
283 return lttng_add_vsgid_to_ctx(ctx);
284 default:
285 return -EINVAL;
286 }
287 }
288
289 /**
290 * lttng_ioctl - lttng syscall through ioctl
291 *
292 * @file: the file
293 * @cmd: the command
294 * @arg: command arg
295 *
296 * This ioctl implements lttng commands:
297 * LTTNG_KERNEL_SESSION
298 * Returns a LTTng trace session file descriptor
299 * LTTNG_KERNEL_TRACER_VERSION
300 * Returns the LTTng kernel tracer version
301 * LTTNG_KERNEL_TRACEPOINT_LIST
302 * Returns a file descriptor listing available tracepoints
303 * LTTNG_KERNEL_WAIT_QUIESCENT
304 * Returns after all previously running probes have completed
305 * LTTNG_KERNEL_TRACER_ABI_VERSION
306 * Returns the LTTng kernel tracer ABI version
307 *
308 * The returned session will be deleted when its file descriptor is closed.
309 */
310 static
311 long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
312 {
313 switch (cmd) {
314 case LTTNG_KERNEL_OLD_SESSION:
315 case LTTNG_KERNEL_SESSION:
316 return lttng_abi_create_session();
317 case LTTNG_KERNEL_OLD_TRACER_VERSION:
318 {
319 struct lttng_kernel_tracer_version v;
320 struct lttng_kernel_old_tracer_version oldv;
321 struct lttng_kernel_old_tracer_version *uversion =
322 (struct lttng_kernel_old_tracer_version __user *) arg;
323
324 lttng_abi_tracer_version(&v);
325 oldv.major = v.major;
326 oldv.minor = v.minor;
327 oldv.patchlevel = v.patchlevel;
328
329 if (copy_to_user(uversion, &oldv, sizeof(oldv)))
330 return -EFAULT;
331 return 0;
332 }
333 case LTTNG_KERNEL_TRACER_VERSION:
334 {
335 struct lttng_kernel_tracer_version version;
336 struct lttng_kernel_tracer_version *uversion =
337 (struct lttng_kernel_tracer_version __user *) arg;
338
339 lttng_abi_tracer_version(&version);
340
341 if (copy_to_user(uversion, &version, sizeof(version)))
342 return -EFAULT;
343 return 0;
344 }
345 case LTTNG_KERNEL_TRACER_ABI_VERSION:
346 {
347 struct lttng_kernel_tracer_abi_version version;
348 struct lttng_kernel_tracer_abi_version *uversion =
349 (struct lttng_kernel_tracer_abi_version __user *) arg;
350
351 lttng_abi_tracer_abi_version(&version);
352
353 if (copy_to_user(uversion, &version, sizeof(version)))
354 return -EFAULT;
355 return 0;
356 }
357 case LTTNG_KERNEL_OLD_TRACEPOINT_LIST:
358 case LTTNG_KERNEL_TRACEPOINT_LIST:
359 return lttng_abi_tracepoint_list();
360 case LTTNG_KERNEL_SYSCALL_LIST:
361 return lttng_abi_syscall_list();
362 case LTTNG_KERNEL_OLD_WAIT_QUIESCENT:
363 case LTTNG_KERNEL_WAIT_QUIESCENT:
364 synchronize_trace();
365 return 0;
366 case LTTNG_KERNEL_OLD_CALIBRATE:
367 {
368 struct lttng_kernel_old_calibrate __user *ucalibrate =
369 (struct lttng_kernel_old_calibrate __user *) arg;
370 struct lttng_kernel_old_calibrate old_calibrate;
371 struct lttng_kernel_calibrate calibrate;
372 int ret;
373
374 if (copy_from_user(&old_calibrate, ucalibrate, sizeof(old_calibrate)))
375 return -EFAULT;
376 calibrate.type = old_calibrate.type;
377 ret = lttng_calibrate(&calibrate);
378 if (copy_to_user(ucalibrate, &old_calibrate, sizeof(old_calibrate)))
379 return -EFAULT;
380 return ret;
381 }
382 case LTTNG_KERNEL_CALIBRATE:
383 {
384 struct lttng_kernel_calibrate __user *ucalibrate =
385 (struct lttng_kernel_calibrate __user *) arg;
386 struct lttng_kernel_calibrate calibrate;
387 int ret;
388
389 if (copy_from_user(&calibrate, ucalibrate, sizeof(calibrate)))
390 return -EFAULT;
391 ret = lttng_calibrate(&calibrate);
392 if (copy_to_user(ucalibrate, &calibrate, sizeof(calibrate)))
393 return -EFAULT;
394 return ret;
395 }
396 default:
397 return -ENOIOCTLCMD;
398 }
399 }
400
401 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0))
402 static const struct proc_ops lttng_proc_ops = {
403 .proc_ioctl = lttng_ioctl,
404 #ifdef CONFIG_COMPAT
405 .proc_compat_ioctl = lttng_ioctl,
406 #endif /* CONFIG_COMPAT */
407 };
408 #else
409 static const struct file_operations lttng_proc_ops = {
410 .owner = THIS_MODULE,
411 .unlocked_ioctl = lttng_ioctl,
412 #ifdef CONFIG_COMPAT
413 .compat_ioctl = lttng_ioctl,
414 #endif /* CONFIG_COMPAT */
415 };
416 #endif
417
418 static
419 int lttng_abi_create_channel(struct file *session_file,
420 struct lttng_kernel_channel *chan_param,
421 enum channel_type channel_type)
422 {
423 struct lttng_session *session = session_file->private_data;
424 const struct file_operations *fops = NULL;
425 const char *transport_name;
426 struct lttng_channel *chan;
427 struct file *chan_file;
428 int chan_fd;
429 int ret = 0;
430
431 chan_fd = lttng_get_unused_fd();
432 if (chan_fd < 0) {
433 ret = chan_fd;
434 goto fd_error;
435 }
436 switch (channel_type) {
437 case PER_CPU_CHANNEL:
438 fops = &lttng_channel_fops;
439 break;
440 case METADATA_CHANNEL:
441 fops = &lttng_metadata_fops;
442 break;
443 }
444
445 chan_file = anon_inode_getfile("[lttng_channel]",
446 fops,
447 NULL, O_RDWR);
448 if (IS_ERR(chan_file)) {
449 ret = PTR_ERR(chan_file);
450 goto file_error;
451 }
452 switch (channel_type) {
453 case PER_CPU_CHANNEL:
454 if (chan_param->output == LTTNG_KERNEL_SPLICE) {
455 transport_name = chan_param->overwrite ?
456 "relay-overwrite" : "relay-discard";
457 } else if (chan_param->output == LTTNG_KERNEL_MMAP) {
458 transport_name = chan_param->overwrite ?
459 "relay-overwrite-mmap" : "relay-discard-mmap";
460 } else {
461 return -EINVAL;
462 }
463 break;
464 case METADATA_CHANNEL:
465 if (chan_param->output == LTTNG_KERNEL_SPLICE)
466 transport_name = "relay-metadata";
467 else if (chan_param->output == LTTNG_KERNEL_MMAP)
468 transport_name = "relay-metadata-mmap";
469 else
470 return -EINVAL;
471 break;
472 default:
473 transport_name = "<unknown>";
474 break;
475 }
476 if (!atomic_long_add_unless(&session_file->f_count, 1, LONG_MAX)) {
477 ret = -EOVERFLOW;
478 goto refcount_error;
479 }
480 /*
481 * We tolerate no failure path after channel creation. It will stay
482 * invariant for the rest of the session.
483 */
484 chan = lttng_channel_create(session, transport_name, NULL,
485 chan_param->subbuf_size,
486 chan_param->num_subbuf,
487 chan_param->switch_timer_interval,
488 chan_param->read_timer_interval,
489 channel_type);
490 if (!chan) {
491 ret = -EINVAL;
492 goto chan_error;
493 }
494 chan->file = chan_file;
495 chan_file->private_data = chan;
496 fd_install(chan_fd, chan_file);
497
498 return chan_fd;
499
500 chan_error:
501 atomic_long_dec(&session_file->f_count);
502 refcount_error:
503 fput(chan_file);
504 file_error:
505 put_unused_fd(chan_fd);
506 fd_error:
507 return ret;
508 }
509
510 static
511 int lttng_abi_session_set_name(struct lttng_session *session,
512 struct lttng_kernel_session_name *name)
513 {
514 size_t len;
515
516 len = strnlen(name->name, LTTNG_KERNEL_SESSION_NAME_LEN);
517
518 if (len == LTTNG_KERNEL_SESSION_NAME_LEN) {
519 /* Name is too long/malformed */
520 return -EINVAL;
521 }
522
523 strcpy(session->name, name->name);
524 return 0;
525 }
526
527 static
528 int lttng_abi_session_set_creation_time(struct lttng_session *session,
529 struct lttng_kernel_session_creation_time *time)
530 {
531 size_t len;
532
533 len = strnlen(time->iso8601, LTTNG_KERNEL_SESSION_CREATION_TIME_ISO8601_LEN);
534
535 if (len == LTTNG_KERNEL_SESSION_CREATION_TIME_ISO8601_LEN) {
536 /* Time is too long/malformed */
537 return -EINVAL;
538 }
539
540 strcpy(session->creation_time, time->iso8601);
541 return 0;
542 }
543
544 static
545 enum tracker_type get_tracker_type(struct lttng_kernel_tracker_args *tracker)
546 {
547 switch (tracker->type) {
548 case LTTNG_KERNEL_TRACKER_PID:
549 return TRACKER_PID;
550 case LTTNG_KERNEL_TRACKER_VPID:
551 return TRACKER_VPID;
552 case LTTNG_KERNEL_TRACKER_UID:
553 return TRACKER_UID;
554 case LTTNG_KERNEL_TRACKER_VUID:
555 return TRACKER_VUID;
556 case LTTNG_KERNEL_TRACKER_GID:
557 return TRACKER_GID;
558 case LTTNG_KERNEL_TRACKER_VGID:
559 return TRACKER_VGID;
560 default:
561 return TRACKER_UNKNOWN;
562 }
563 }
564
565 /**
566 * lttng_session_ioctl - lttng session fd ioctl
567 *
568 * @file: the file
569 * @cmd: the command
570 * @arg: command arg
571 *
572 * This ioctl implements lttng commands:
573 * LTTNG_KERNEL_CHANNEL
574 * Returns a LTTng channel file descriptor
575 * LTTNG_KERNEL_ENABLE
576 * Enables tracing for a session (weak enable)
577 * LTTNG_KERNEL_DISABLE
578 * Disables tracing for a session (strong disable)
579 * LTTNG_KERNEL_METADATA
580 * Returns a LTTng metadata file descriptor
581 * LTTNG_KERNEL_SESSION_TRACK_PID
582 * Add PID to session PID tracker
583 * LTTNG_KERNEL_SESSION_UNTRACK_PID
584 * Remove PID from session PID tracker
585 * LTTNG_KERNEL_SESSION_TRACK_ID
586 * Add ID to tracker
587 * LTTNG_KERNEL_SESSION_UNTRACK_ID
588 * Remove ID from tracker
589 *
590 * The returned channel will be deleted when its file descriptor is closed.
591 */
592 static
593 long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
594 {
595 struct lttng_session *session = file->private_data;
596 struct lttng_kernel_channel chan_param;
597 struct lttng_kernel_old_channel old_chan_param;
598
599 switch (cmd) {
600 case LTTNG_KERNEL_OLD_CHANNEL:
601 {
602 if (copy_from_user(&old_chan_param,
603 (struct lttng_kernel_old_channel __user *) arg,
604 sizeof(struct lttng_kernel_old_channel)))
605 return -EFAULT;
606 chan_param.overwrite = old_chan_param.overwrite;
607 chan_param.subbuf_size = old_chan_param.subbuf_size;
608 chan_param.num_subbuf = old_chan_param.num_subbuf;
609 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
610 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
611 chan_param.output = old_chan_param.output;
612
613 return lttng_abi_create_channel(file, &chan_param,
614 PER_CPU_CHANNEL);
615 }
616 case LTTNG_KERNEL_CHANNEL:
617 {
618 if (copy_from_user(&chan_param,
619 (struct lttng_kernel_channel __user *) arg,
620 sizeof(struct lttng_kernel_channel)))
621 return -EFAULT;
622 return lttng_abi_create_channel(file, &chan_param,
623 PER_CPU_CHANNEL);
624 }
625 case LTTNG_KERNEL_OLD_SESSION_START:
626 case LTTNG_KERNEL_OLD_ENABLE:
627 case LTTNG_KERNEL_SESSION_START:
628 case LTTNG_KERNEL_ENABLE:
629 return lttng_session_enable(session);
630 case LTTNG_KERNEL_OLD_SESSION_STOP:
631 case LTTNG_KERNEL_OLD_DISABLE:
632 case LTTNG_KERNEL_SESSION_STOP:
633 case LTTNG_KERNEL_DISABLE:
634 return lttng_session_disable(session);
635 case LTTNG_KERNEL_OLD_METADATA:
636 {
637 if (copy_from_user(&old_chan_param,
638 (struct lttng_kernel_old_channel __user *) arg,
639 sizeof(struct lttng_kernel_old_channel)))
640 return -EFAULT;
641 chan_param.overwrite = old_chan_param.overwrite;
642 chan_param.subbuf_size = old_chan_param.subbuf_size;
643 chan_param.num_subbuf = old_chan_param.num_subbuf;
644 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
645 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
646 chan_param.output = old_chan_param.output;
647
648 return lttng_abi_create_channel(file, &chan_param,
649 METADATA_CHANNEL);
650 }
651 case LTTNG_KERNEL_METADATA:
652 {
653 if (copy_from_user(&chan_param,
654 (struct lttng_kernel_channel __user *) arg,
655 sizeof(struct lttng_kernel_channel)))
656 return -EFAULT;
657 return lttng_abi_create_channel(file, &chan_param,
658 METADATA_CHANNEL);
659 }
660 case LTTNG_KERNEL_SESSION_TRACK_PID:
661 return lttng_session_track_id(session, TRACKER_PID, (int) arg);
662 case LTTNG_KERNEL_SESSION_UNTRACK_PID:
663 return lttng_session_untrack_id(session, TRACKER_PID, (int) arg);
664 case LTTNG_KERNEL_SESSION_TRACK_ID:
665 {
666 struct lttng_kernel_tracker_args tracker;
667 enum tracker_type tracker_type;
668
669 if (copy_from_user(&tracker,
670 (struct lttng_kernel_tracker_args __user *) arg,
671 sizeof(struct lttng_kernel_tracker_args)))
672 return -EFAULT;
673 tracker_type = get_tracker_type(&tracker);
674 if (tracker_type == TRACKER_UNKNOWN)
675 return -EINVAL;
676 return lttng_session_track_id(session, tracker_type, tracker.id);
677 }
678 case LTTNG_KERNEL_SESSION_UNTRACK_ID:
679 {
680 struct lttng_kernel_tracker_args tracker;
681 enum tracker_type tracker_type;
682
683 if (copy_from_user(&tracker,
684 (struct lttng_kernel_tracker_args __user *) arg,
685 sizeof(struct lttng_kernel_tracker_args)))
686 return -EFAULT;
687 tracker_type = get_tracker_type(&tracker);
688 if (tracker_type == TRACKER_UNKNOWN)
689 return -EINVAL;
690 return lttng_session_untrack_id(session, tracker_type,
691 tracker.id);
692 }
693 case LTTNG_KERNEL_SESSION_LIST_TRACKER_PIDS:
694 return lttng_session_list_tracker_ids(session, TRACKER_PID);
695 case LTTNG_KERNEL_SESSION_LIST_TRACKER_IDS:
696 {
697 struct lttng_kernel_tracker_args tracker;
698 enum tracker_type tracker_type;
699
700 if (copy_from_user(&tracker,
701 (struct lttng_kernel_tracker_args __user *) arg,
702 sizeof(struct lttng_kernel_tracker_args)))
703 return -EFAULT;
704 tracker_type = get_tracker_type(&tracker);
705 if (tracker_type == TRACKER_UNKNOWN)
706 return -EINVAL;
707 return lttng_session_list_tracker_ids(session, tracker_type);
708 }
709 case LTTNG_KERNEL_SESSION_METADATA_REGEN:
710 return lttng_session_metadata_regenerate(session);
711 case LTTNG_KERNEL_SESSION_STATEDUMP:
712 return lttng_session_statedump(session);
713 case LTTNG_KERNEL_SESSION_SET_NAME:
714 {
715 struct lttng_kernel_session_name name;
716
717 if (copy_from_user(&name,
718 (struct lttng_kernel_session_name __user *) arg,
719 sizeof(struct lttng_kernel_session_name)))
720 return -EFAULT;
721 return lttng_abi_session_set_name(session, &name);
722 }
723 case LTTNG_KERNEL_SESSION_SET_CREATION_TIME:
724 {
725 struct lttng_kernel_session_creation_time time;
726
727 if (copy_from_user(&time,
728 (struct lttng_kernel_session_creation_time __user *) arg,
729 sizeof(struct lttng_kernel_session_creation_time)))
730 return -EFAULT;
731 return lttng_abi_session_set_creation_time(session, &time);
732 }
733 default:
734 return -ENOIOCTLCMD;
735 }
736 }
737
738 /*
739 * Called when the last file reference is dropped.
740 *
741 * Big fat note: channels and events are invariant for the whole session after
742 * their creation. So this session destruction also destroys all channel and
743 * event structures specific to this session (they are not destroyed when their
744 * individual file is released).
745 */
746 static
747 int lttng_session_release(struct inode *inode, struct file *file)
748 {
749 struct lttng_session *session = file->private_data;
750
751 if (session)
752 lttng_session_destroy(session);
753 return 0;
754 }
755
756 static const struct file_operations lttng_session_fops = {
757 .owner = THIS_MODULE,
758 .release = lttng_session_release,
759 .unlocked_ioctl = lttng_session_ioctl,
760 #ifdef CONFIG_COMPAT
761 .compat_ioctl = lttng_session_ioctl,
762 #endif
763 };
764
765 /**
766 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
767 * @filp: the file
768 * @wait: poll table
769 *
770 * Handles the poll operations for the metadata channels.
771 */
772 static
773 unsigned int lttng_metadata_ring_buffer_poll(struct file *filp,
774 poll_table *wait)
775 {
776 struct lttng_metadata_stream *stream = filp->private_data;
777 struct lib_ring_buffer *buf = stream->priv;
778 int finalized;
779 unsigned int mask = 0;
780
781 if (filp->f_mode & FMODE_READ) {
782 poll_wait_set_exclusive(wait);
783 poll_wait(filp, &stream->read_wait, wait);
784
785 finalized = stream->finalized;
786
787 /*
788 * lib_ring_buffer_is_finalized() contains a smp_rmb()
789 * ordering finalized load before offsets loads.
790 */
791 WARN_ON(atomic_long_read(&buf->active_readers) != 1);
792
793 if (finalized)
794 mask |= POLLHUP;
795
796 mutex_lock(&stream->metadata_cache->lock);
797 if (stream->metadata_cache->metadata_written >
798 stream->metadata_out)
799 mask |= POLLIN;
800 mutex_unlock(&stream->metadata_cache->lock);
801 }
802
803 return mask;
804 }
805
806 static
807 void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file *filp,
808 unsigned int cmd, unsigned long arg)
809 {
810 struct lttng_metadata_stream *stream = filp->private_data;
811
812 stream->metadata_out = stream->metadata_in;
813 }
814
815 /*
816 * Reset the counter of how much metadata has been consumed to 0. That way,
817 * the consumer receives the content of the metadata cache unchanged. This is
818 * different from the metadata_regenerate where the offset from epoch is
819 * resampled, here we want the exact same content as the last time the metadata
820 * was generated. This command is only possible if all the metadata written
821 * in the cache has been output to the metadata stream to avoid corrupting the
822 * metadata file.
823 *
824 * Return 0 on success, a negative value on error.
825 */
826 static
827 int lttng_metadata_cache_dump(struct lttng_metadata_stream *stream)
828 {
829 int ret;
830 struct lttng_metadata_cache *cache = stream->metadata_cache;
831
832 mutex_lock(&cache->lock);
833 if (stream->metadata_out != cache->metadata_written) {
834 ret = -EBUSY;
835 goto end;
836 }
837 stream->metadata_out = 0;
838 stream->metadata_in = 0;
839 wake_up_interruptible(&stream->read_wait);
840 ret = 0;
841
842 end:
843 mutex_unlock(&cache->lock);
844 return ret;
845 }
846
847 static
848 long lttng_metadata_ring_buffer_ioctl(struct file *filp,
849 unsigned int cmd, unsigned long arg)
850 {
851 int ret;
852 struct lttng_metadata_stream *stream = filp->private_data;
853 struct lib_ring_buffer *buf = stream->priv;
854 unsigned int rb_cmd;
855 bool coherent;
856
857 if (cmd == RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK)
858 rb_cmd = RING_BUFFER_GET_NEXT_SUBBUF;
859 else
860 rb_cmd = cmd;
861
862 switch (cmd) {
863 case RING_BUFFER_GET_NEXT_SUBBUF:
864 {
865 struct lttng_metadata_stream *stream = filp->private_data;
866 struct lib_ring_buffer *buf = stream->priv;
867 struct channel *chan = buf->backend.chan;
868
869 ret = lttng_metadata_output_channel(stream, chan, NULL);
870 if (ret > 0) {
871 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
872 ret = 0;
873 } else if (ret < 0)
874 goto err;
875 break;
876 }
877 case RING_BUFFER_GET_SUBBUF:
878 {
879 /*
880 * Random access is not allowed for metadata channel.
881 */
882 return -ENOSYS;
883 }
884 case RING_BUFFER_FLUSH_EMPTY: /* Fall-through. */
885 case RING_BUFFER_FLUSH:
886 {
887 struct lttng_metadata_stream *stream = filp->private_data;
888 struct lib_ring_buffer *buf = stream->priv;
889 struct channel *chan = buf->backend.chan;
890
891 /*
892 * Before doing the actual ring buffer flush, write up to one
893 * packet of metadata in the ring buffer.
894 */
895 ret = lttng_metadata_output_channel(stream, chan, NULL);
896 if (ret < 0)
897 goto err;
898 break;
899 }
900 case RING_BUFFER_GET_METADATA_VERSION:
901 {
902 struct lttng_metadata_stream *stream = filp->private_data;
903
904 return put_u64(stream->version, arg);
905 }
906 case RING_BUFFER_METADATA_CACHE_DUMP:
907 {
908 struct lttng_metadata_stream *stream = filp->private_data;
909
910 return lttng_metadata_cache_dump(stream);
911 }
912 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
913 {
914 struct lttng_metadata_stream *stream = filp->private_data;
915 struct lib_ring_buffer *buf = stream->priv;
916 struct channel *chan = buf->backend.chan;
917
918 ret = lttng_metadata_output_channel(stream, chan, &coherent);
919 if (ret > 0) {
920 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
921 ret = 0;
922 } else if (ret < 0) {
923 goto err;
924 }
925 break;
926 }
927 default:
928 break;
929 }
930 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
931
932 /* Performing lib ring buffer ioctl after our own. */
933 ret = lib_ring_buffer_ioctl(filp, rb_cmd, arg, buf);
934 if (ret < 0)
935 goto err;
936
937 switch (cmd) {
938 case RING_BUFFER_PUT_NEXT_SUBBUF:
939 {
940 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
941 cmd, arg);
942 break;
943 }
944 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
945 {
946 return put_u32(coherent, arg);
947 }
948 default:
949 break;
950 }
951 err:
952 return ret;
953 }
954
955 #ifdef CONFIG_COMPAT
956 static
957 long lttng_metadata_ring_buffer_compat_ioctl(struct file *filp,
958 unsigned int cmd, unsigned long arg)
959 {
960 int ret;
961 struct lttng_metadata_stream *stream = filp->private_data;
962 struct lib_ring_buffer *buf = stream->priv;
963 unsigned int rb_cmd;
964 bool coherent;
965
966 if (cmd == RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK)
967 rb_cmd = RING_BUFFER_GET_NEXT_SUBBUF;
968 else
969 rb_cmd = cmd;
970
971 switch (cmd) {
972 case RING_BUFFER_GET_NEXT_SUBBUF:
973 {
974 struct lttng_metadata_stream *stream = filp->private_data;
975 struct lib_ring_buffer *buf = stream->priv;
976 struct channel *chan = buf->backend.chan;
977
978 ret = lttng_metadata_output_channel(stream, chan, NULL);
979 if (ret > 0) {
980 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
981 ret = 0;
982 } else if (ret < 0)
983 goto err;
984 break;
985 }
986 case RING_BUFFER_GET_SUBBUF:
987 {
988 /*
989 * Random access is not allowed for metadata channel.
990 */
991 return -ENOSYS;
992 }
993 case RING_BUFFER_FLUSH_EMPTY: /* Fall-through. */
994 case RING_BUFFER_FLUSH:
995 {
996 struct lttng_metadata_stream *stream = filp->private_data;
997 struct lib_ring_buffer *buf = stream->priv;
998 struct channel *chan = buf->backend.chan;
999
1000 /*
1001 * Before doing the actual ring buffer flush, write up to one
1002 * packet of metadata in the ring buffer.
1003 */
1004 ret = lttng_metadata_output_channel(stream, chan, NULL);
1005 if (ret < 0)
1006 goto err;
1007 break;
1008 }
1009 case RING_BUFFER_GET_METADATA_VERSION:
1010 {
1011 struct lttng_metadata_stream *stream = filp->private_data;
1012
1013 return put_u64(stream->version, arg);
1014 }
1015 case RING_BUFFER_METADATA_CACHE_DUMP:
1016 {
1017 struct lttng_metadata_stream *stream = filp->private_data;
1018
1019 return lttng_metadata_cache_dump(stream);
1020 }
1021 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
1022 {
1023 struct lttng_metadata_stream *stream = filp->private_data;
1024 struct lib_ring_buffer *buf = stream->priv;
1025 struct channel *chan = buf->backend.chan;
1026
1027 ret = lttng_metadata_output_channel(stream, chan, &coherent);
1028 if (ret > 0) {
1029 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
1030 ret = 0;
1031 } else if (ret < 0) {
1032 goto err;
1033 }
1034 break;
1035 }
1036 default:
1037 break;
1038 }
1039 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
1040
1041 /* Performing lib ring buffer ioctl after our own. */
1042 ret = lib_ring_buffer_compat_ioctl(filp, rb_cmd, arg, buf);
1043 if (ret < 0)
1044 goto err;
1045
1046 switch (cmd) {
1047 case RING_BUFFER_PUT_NEXT_SUBBUF:
1048 {
1049 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
1050 cmd, arg);
1051 break;
1052 }
1053 case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK:
1054 {
1055 return put_u32(coherent, arg);
1056 }
1057 default:
1058 break;
1059 }
1060 err:
1061 return ret;
1062 }
1063 #endif
1064
1065 /*
1066 * This is not used by anonymous file descriptors. This code is left
1067 * there if we ever want to implement an inode with open() operation.
1068 */
1069 static
1070 int lttng_metadata_ring_buffer_open(struct inode *inode, struct file *file)
1071 {
1072 struct lttng_metadata_stream *stream = inode->i_private;
1073 struct lib_ring_buffer *buf = stream->priv;
1074
1075 file->private_data = buf;
1076 /*
1077 * Since life-time of metadata cache differs from that of
1078 * session, we need to keep our own reference on the transport.
1079 */
1080 if (!try_module_get(stream->transport->owner)) {
1081 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
1082 return -EBUSY;
1083 }
1084 return lib_ring_buffer_open(inode, file, buf);
1085 }
1086
1087 static
1088 int lttng_metadata_ring_buffer_release(struct inode *inode, struct file *file)
1089 {
1090 struct lttng_metadata_stream *stream = file->private_data;
1091 struct lib_ring_buffer *buf = stream->priv;
1092
1093 mutex_lock(&stream->metadata_cache->lock);
1094 list_del(&stream->list);
1095 mutex_unlock(&stream->metadata_cache->lock);
1096 kref_put(&stream->metadata_cache->refcount, metadata_cache_destroy);
1097 module_put(stream->transport->owner);
1098 kfree(stream);
1099 return lib_ring_buffer_release(inode, file, buf);
1100 }
1101
1102 static
1103 ssize_t lttng_metadata_ring_buffer_splice_read(struct file *in, loff_t *ppos,
1104 struct pipe_inode_info *pipe, size_t len,
1105 unsigned int flags)
1106 {
1107 struct lttng_metadata_stream *stream = in->private_data;
1108 struct lib_ring_buffer *buf = stream->priv;
1109
1110 return lib_ring_buffer_splice_read(in, ppos, pipe, len,
1111 flags, buf);
1112 }
1113
1114 static
1115 int lttng_metadata_ring_buffer_mmap(struct file *filp,
1116 struct vm_area_struct *vma)
1117 {
1118 struct lttng_metadata_stream *stream = filp->private_data;
1119 struct lib_ring_buffer *buf = stream->priv;
1120
1121 return lib_ring_buffer_mmap(filp, vma, buf);
1122 }
1123
1124 static
1125 const struct file_operations lttng_metadata_ring_buffer_file_operations = {
1126 .owner = THIS_MODULE,
1127 .open = lttng_metadata_ring_buffer_open,
1128 .release = lttng_metadata_ring_buffer_release,
1129 .poll = lttng_metadata_ring_buffer_poll,
1130 .splice_read = lttng_metadata_ring_buffer_splice_read,
1131 .mmap = lttng_metadata_ring_buffer_mmap,
1132 .unlocked_ioctl = lttng_metadata_ring_buffer_ioctl,
1133 .llseek = vfs_lib_ring_buffer_no_llseek,
1134 #ifdef CONFIG_COMPAT
1135 .compat_ioctl = lttng_metadata_ring_buffer_compat_ioctl,
1136 #endif
1137 };
1138
1139 static
1140 int lttng_abi_create_stream_fd(struct file *channel_file, void *stream_priv,
1141 const struct file_operations *fops)
1142 {
1143 int stream_fd, ret;
1144 struct file *stream_file;
1145
1146 stream_fd = lttng_get_unused_fd();
1147 if (stream_fd < 0) {
1148 ret = stream_fd;
1149 goto fd_error;
1150 }
1151 stream_file = anon_inode_getfile("[lttng_stream]", fops,
1152 stream_priv, O_RDWR);
1153 if (IS_ERR(stream_file)) {
1154 ret = PTR_ERR(stream_file);
1155 goto file_error;
1156 }
1157 /*
1158 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
1159 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
1160 * file descriptor, so we set FMODE_PREAD here.
1161 */
1162 stream_file->f_mode |= FMODE_PREAD;
1163 fd_install(stream_fd, stream_file);
1164 /*
1165 * The stream holds a reference to the channel within the generic ring
1166 * buffer library, so no need to hold a refcount on the channel and
1167 * session files here.
1168 */
1169 return stream_fd;
1170
1171 file_error:
1172 put_unused_fd(stream_fd);
1173 fd_error:
1174 return ret;
1175 }
1176
1177 static
1178 int lttng_abi_open_stream(struct file *channel_file)
1179 {
1180 struct lttng_channel *channel = channel_file->private_data;
1181 struct lib_ring_buffer *buf;
1182 int ret;
1183 void *stream_priv;
1184
1185 buf = channel->ops->buffer_read_open(channel->chan);
1186 if (!buf)
1187 return -ENOENT;
1188
1189 stream_priv = buf;
1190 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
1191 &lttng_stream_ring_buffer_file_operations);
1192 if (ret < 0)
1193 goto fd_error;
1194
1195 return ret;
1196
1197 fd_error:
1198 channel->ops->buffer_read_close(buf);
1199 return ret;
1200 }
1201
1202 static
1203 int lttng_abi_open_metadata_stream(struct file *channel_file)
1204 {
1205 struct lttng_channel *channel = channel_file->private_data;
1206 struct lttng_session *session = channel->session;
1207 struct lib_ring_buffer *buf;
1208 int ret;
1209 struct lttng_metadata_stream *metadata_stream;
1210 void *stream_priv;
1211
1212 buf = channel->ops->buffer_read_open(channel->chan);
1213 if (!buf)
1214 return -ENOENT;
1215
1216 metadata_stream = kzalloc(sizeof(struct lttng_metadata_stream),
1217 GFP_KERNEL);
1218 if (!metadata_stream) {
1219 ret = -ENOMEM;
1220 goto nomem;
1221 }
1222 metadata_stream->metadata_cache = session->metadata_cache;
1223 init_waitqueue_head(&metadata_stream->read_wait);
1224 metadata_stream->priv = buf;
1225 stream_priv = metadata_stream;
1226 metadata_stream->transport = channel->transport;
1227 /* Initial state is an empty metadata, considered as incoherent. */
1228 metadata_stream->coherent = false;
1229
1230 /*
1231 * Since life-time of metadata cache differs from that of
1232 * session, we need to keep our own reference on the transport.
1233 */
1234 if (!try_module_get(metadata_stream->transport->owner)) {
1235 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
1236 ret = -EINVAL;
1237 goto notransport;
1238 }
1239
1240 if (!lttng_kref_get(&session->metadata_cache->refcount)) {
1241 ret = -EOVERFLOW;
1242 goto kref_error;
1243 }
1244
1245 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
1246 &lttng_metadata_ring_buffer_file_operations);
1247 if (ret < 0)
1248 goto fd_error;
1249
1250 mutex_lock(&session->metadata_cache->lock);
1251 list_add(&metadata_stream->list,
1252 &session->metadata_cache->metadata_stream);
1253 mutex_unlock(&session->metadata_cache->lock);
1254 return ret;
1255
1256 fd_error:
1257 kref_put(&session->metadata_cache->refcount, metadata_cache_destroy);
1258 kref_error:
1259 module_put(metadata_stream->transport->owner);
1260 notransport:
1261 kfree(metadata_stream);
1262 nomem:
1263 channel->ops->buffer_read_close(buf);
1264 return ret;
1265 }
1266
1267 static
1268 int lttng_abi_validate_event_param(struct lttng_kernel_event *event_param)
1269 {
1270 /* Limit ABI to implemented features. */
1271 switch (event_param->instrumentation) {
1272 case LTTNG_KERNEL_SYSCALL:
1273 switch (event_param->u.syscall.entryexit) {
1274 case LTTNG_KERNEL_SYSCALL_ENTRYEXIT:
1275 break;
1276 default:
1277 return -EINVAL;
1278 }
1279 switch (event_param->u.syscall.abi) {
1280 case LTTNG_KERNEL_SYSCALL_ABI_ALL:
1281 break;
1282 default:
1283 return -EINVAL;
1284 }
1285 switch (event_param->u.syscall.match) {
1286 case LTTNG_SYSCALL_MATCH_NAME:
1287 break;
1288 default:
1289 return -EINVAL;
1290 }
1291 break;
1292
1293 case LTTNG_KERNEL_TRACEPOINT: /* Fallthrough */
1294 case LTTNG_KERNEL_KPROBE: /* Fallthrough */
1295 case LTTNG_KERNEL_KRETPROBE: /* Fallthrough */
1296 case LTTNG_KERNEL_NOOP: /* Fallthrough */
1297 case LTTNG_KERNEL_UPROBE:
1298 break;
1299
1300 case LTTNG_KERNEL_FUNCTION: /* Fallthrough */
1301 default:
1302 return -EINVAL;
1303 }
1304 return 0;
1305 }
1306
1307 static
1308 int lttng_abi_create_event(struct file *channel_file,
1309 struct lttng_kernel_event *event_param)
1310 {
1311 struct lttng_channel *channel = channel_file->private_data;
1312 int event_fd, ret;
1313 struct file *event_file;
1314 void *priv;
1315
1316 event_param->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
1317 switch (event_param->instrumentation) {
1318 case LTTNG_KERNEL_KRETPROBE:
1319 event_param->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
1320 break;
1321 case LTTNG_KERNEL_KPROBE:
1322 event_param->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
1323 break;
1324 case LTTNG_KERNEL_FUNCTION:
1325 WARN_ON_ONCE(1);
1326 /* Not implemented. */
1327 break;
1328 default:
1329 break;
1330 }
1331 event_fd = lttng_get_unused_fd();
1332 if (event_fd < 0) {
1333 ret = event_fd;
1334 goto fd_error;
1335 }
1336 event_file = anon_inode_getfile("[lttng_event]",
1337 &lttng_event_fops,
1338 NULL, O_RDWR);
1339 if (IS_ERR(event_file)) {
1340 ret = PTR_ERR(event_file);
1341 goto file_error;
1342 }
1343 /* The event holds a reference on the channel */
1344 if (!atomic_long_add_unless(&channel_file->f_count, 1, LONG_MAX)) {
1345 ret = -EOVERFLOW;
1346 goto refcount_error;
1347 }
1348 ret = lttng_abi_validate_event_param(event_param);
1349 if (ret)
1350 goto event_error;
1351 if (event_param->instrumentation == LTTNG_KERNEL_TRACEPOINT
1352 || event_param->instrumentation == LTTNG_KERNEL_SYSCALL) {
1353 struct lttng_enabler *enabler;
1354
1355 if (strutils_is_star_glob_pattern(event_param->name)) {
1356 /*
1357 * If the event name is a star globbing pattern,
1358 * we create the special star globbing enabler.
1359 */
1360 enabler = lttng_enabler_create(LTTNG_ENABLER_STAR_GLOB,
1361 event_param, channel);
1362 } else {
1363 enabler = lttng_enabler_create(LTTNG_ENABLER_NAME,
1364 event_param, channel);
1365 }
1366 priv = enabler;
1367 } else {
1368 struct lttng_event *event;
1369
1370 /*
1371 * We tolerate no failure path after event creation. It
1372 * will stay invariant for the rest of the session.
1373 */
1374 event = lttng_event_create(channel, event_param,
1375 NULL, NULL,
1376 event_param->instrumentation);
1377 WARN_ON_ONCE(!event);
1378 if (IS_ERR(event)) {
1379 ret = PTR_ERR(event);
1380 goto event_error;
1381 }
1382 priv = event;
1383 }
1384 event_file->private_data = priv;
1385 fd_install(event_fd, event_file);
1386 return event_fd;
1387
1388 event_error:
1389 atomic_long_dec(&channel_file->f_count);
1390 refcount_error:
1391 fput(event_file);
1392 file_error:
1393 put_unused_fd(event_fd);
1394 fd_error:
1395 return ret;
1396 }
1397
1398 /**
1399 * lttng_channel_ioctl - lttng syscall through ioctl
1400 *
1401 * @file: the file
1402 * @cmd: the command
1403 * @arg: command arg
1404 *
1405 * This ioctl implements lttng commands:
1406 * LTTNG_KERNEL_STREAM
1407 * Returns an event stream file descriptor or failure.
1408 * (typically, one event stream records events from one CPU)
1409 * LTTNG_KERNEL_EVENT
1410 * Returns an event file descriptor or failure.
1411 * LTTNG_KERNEL_CONTEXT
1412 * Prepend a context field to each event in the channel
1413 * LTTNG_KERNEL_ENABLE
1414 * Enable recording for events in this channel (weak enable)
1415 * LTTNG_KERNEL_DISABLE
1416 * Disable recording for events in this channel (strong disable)
1417 *
1418 * Channel and event file descriptors also hold a reference on the session.
1419 */
1420 static
1421 long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1422 {
1423 struct lttng_channel *channel = file->private_data;
1424
1425 switch (cmd) {
1426 case LTTNG_KERNEL_OLD_STREAM:
1427 case LTTNG_KERNEL_STREAM:
1428 return lttng_abi_open_stream(file);
1429 case LTTNG_KERNEL_OLD_EVENT:
1430 {
1431 struct lttng_kernel_event *uevent_param;
1432 struct lttng_kernel_old_event *old_uevent_param;
1433 int ret;
1434
1435 uevent_param = kmalloc(sizeof(struct lttng_kernel_event),
1436 GFP_KERNEL);
1437 if (!uevent_param) {
1438 ret = -ENOMEM;
1439 goto old_event_end;
1440 }
1441 old_uevent_param = kmalloc(
1442 sizeof(struct lttng_kernel_old_event),
1443 GFP_KERNEL);
1444 if (!old_uevent_param) {
1445 ret = -ENOMEM;
1446 goto old_event_error_free_param;
1447 }
1448 if (copy_from_user(old_uevent_param,
1449 (struct lttng_kernel_old_event __user *) arg,
1450 sizeof(struct lttng_kernel_old_event))) {
1451 ret = -EFAULT;
1452 goto old_event_error_free_old_param;
1453 }
1454
1455 memcpy(uevent_param->name, old_uevent_param->name,
1456 sizeof(uevent_param->name));
1457 uevent_param->instrumentation =
1458 old_uevent_param->instrumentation;
1459
1460 switch (old_uevent_param->instrumentation) {
1461 case LTTNG_KERNEL_KPROBE:
1462 uevent_param->u.kprobe.addr =
1463 old_uevent_param->u.kprobe.addr;
1464 uevent_param->u.kprobe.offset =
1465 old_uevent_param->u.kprobe.offset;
1466 memcpy(uevent_param->u.kprobe.symbol_name,
1467 old_uevent_param->u.kprobe.symbol_name,
1468 sizeof(uevent_param->u.kprobe.symbol_name));
1469 break;
1470 case LTTNG_KERNEL_KRETPROBE:
1471 uevent_param->u.kretprobe.addr =
1472 old_uevent_param->u.kretprobe.addr;
1473 uevent_param->u.kretprobe.offset =
1474 old_uevent_param->u.kretprobe.offset;
1475 memcpy(uevent_param->u.kretprobe.symbol_name,
1476 old_uevent_param->u.kretprobe.symbol_name,
1477 sizeof(uevent_param->u.kretprobe.symbol_name));
1478 break;
1479 case LTTNG_KERNEL_FUNCTION:
1480 WARN_ON_ONCE(1);
1481 /* Not implemented. */
1482 break;
1483 default:
1484 break;
1485 }
1486 ret = lttng_abi_create_event(file, uevent_param);
1487
1488 old_event_error_free_old_param:
1489 kfree(old_uevent_param);
1490 old_event_error_free_param:
1491 kfree(uevent_param);
1492 old_event_end:
1493 return ret;
1494 }
1495 case LTTNG_KERNEL_EVENT:
1496 {
1497 struct lttng_kernel_event uevent_param;
1498
1499 if (copy_from_user(&uevent_param,
1500 (struct lttng_kernel_event __user *) arg,
1501 sizeof(uevent_param)))
1502 return -EFAULT;
1503 return lttng_abi_create_event(file, &uevent_param);
1504 }
1505 case LTTNG_KERNEL_OLD_CONTEXT:
1506 {
1507 struct lttng_kernel_context *ucontext_param;
1508 struct lttng_kernel_old_context *old_ucontext_param;
1509 int ret;
1510
1511 ucontext_param = kmalloc(sizeof(struct lttng_kernel_context),
1512 GFP_KERNEL);
1513 if (!ucontext_param) {
1514 ret = -ENOMEM;
1515 goto old_ctx_end;
1516 }
1517 old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context),
1518 GFP_KERNEL);
1519 if (!old_ucontext_param) {
1520 ret = -ENOMEM;
1521 goto old_ctx_error_free_param;
1522 }
1523
1524 if (copy_from_user(old_ucontext_param,
1525 (struct lttng_kernel_old_context __user *) arg,
1526 sizeof(struct lttng_kernel_old_context))) {
1527 ret = -EFAULT;
1528 goto old_ctx_error_free_old_param;
1529 }
1530 ucontext_param->ctx = old_ucontext_param->ctx;
1531 memcpy(ucontext_param->padding, old_ucontext_param->padding,
1532 sizeof(ucontext_param->padding));
1533 /* only type that uses the union */
1534 if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) {
1535 ucontext_param->u.perf_counter.type =
1536 old_ucontext_param->u.perf_counter.type;
1537 ucontext_param->u.perf_counter.config =
1538 old_ucontext_param->u.perf_counter.config;
1539 memcpy(ucontext_param->u.perf_counter.name,
1540 old_ucontext_param->u.perf_counter.name,
1541 sizeof(ucontext_param->u.perf_counter.name));
1542 }
1543
1544 ret = lttng_abi_add_context(file,
1545 ucontext_param,
1546 &channel->ctx, channel->session);
1547
1548 old_ctx_error_free_old_param:
1549 kfree(old_ucontext_param);
1550 old_ctx_error_free_param:
1551 kfree(ucontext_param);
1552 old_ctx_end:
1553 return ret;
1554 }
1555 case LTTNG_KERNEL_CONTEXT:
1556 {
1557 struct lttng_kernel_context ucontext_param;
1558
1559 if (copy_from_user(&ucontext_param,
1560 (struct lttng_kernel_context __user *) arg,
1561 sizeof(ucontext_param)))
1562 return -EFAULT;
1563 return lttng_abi_add_context(file,
1564 &ucontext_param,
1565 &channel->ctx, channel->session);
1566 }
1567 case LTTNG_KERNEL_OLD_ENABLE:
1568 case LTTNG_KERNEL_ENABLE:
1569 return lttng_channel_enable(channel);
1570 case LTTNG_KERNEL_OLD_DISABLE:
1571 case LTTNG_KERNEL_DISABLE:
1572 return lttng_channel_disable(channel);
1573 case LTTNG_KERNEL_SYSCALL_MASK:
1574 return lttng_channel_syscall_mask(channel,
1575 (struct lttng_kernel_syscall_mask __user *) arg);
1576 default:
1577 return -ENOIOCTLCMD;
1578 }
1579 }
1580
1581 /**
1582 * lttng_metadata_ioctl - lttng syscall through ioctl
1583 *
1584 * @file: the file
1585 * @cmd: the command
1586 * @arg: command arg
1587 *
1588 * This ioctl implements lttng commands:
1589 * LTTNG_KERNEL_STREAM
1590 * Returns an event stream file descriptor or failure.
1591 *
1592 * Channel and event file descriptors also hold a reference on the session.
1593 */
1594 static
1595 long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1596 {
1597 switch (cmd) {
1598 case LTTNG_KERNEL_OLD_STREAM:
1599 case LTTNG_KERNEL_STREAM:
1600 return lttng_abi_open_metadata_stream(file);
1601 default:
1602 return -ENOIOCTLCMD;
1603 }
1604 }
1605
1606 /**
1607 * lttng_channel_poll - lttng stream addition/removal monitoring
1608 *
1609 * @file: the file
1610 * @wait: poll table
1611 */
1612 unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
1613 {
1614 struct lttng_channel *channel = file->private_data;
1615 unsigned int mask = 0;
1616
1617 if (file->f_mode & FMODE_READ) {
1618 poll_wait_set_exclusive(wait);
1619 poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan),
1620 wait);
1621
1622 if (channel->ops->is_disabled(channel->chan))
1623 return POLLERR;
1624 if (channel->ops->is_finalized(channel->chan))
1625 return POLLHUP;
1626 if (channel->ops->buffer_has_read_closed_stream(channel->chan))
1627 return POLLIN | POLLRDNORM;
1628 return 0;
1629 }
1630 return mask;
1631
1632 }
1633
1634 static
1635 int lttng_channel_release(struct inode *inode, struct file *file)
1636 {
1637 struct lttng_channel *channel = file->private_data;
1638
1639 if (channel)
1640 fput(channel->session->file);
1641 return 0;
1642 }
1643
1644 static
1645 int lttng_metadata_channel_release(struct inode *inode, struct file *file)
1646 {
1647 struct lttng_channel *channel = file->private_data;
1648
1649 if (channel) {
1650 fput(channel->session->file);
1651 lttng_metadata_channel_destroy(channel);
1652 }
1653
1654 return 0;
1655 }
1656
1657 static const struct file_operations lttng_channel_fops = {
1658 .owner = THIS_MODULE,
1659 .release = lttng_channel_release,
1660 .poll = lttng_channel_poll,
1661 .unlocked_ioctl = lttng_channel_ioctl,
1662 #ifdef CONFIG_COMPAT
1663 .compat_ioctl = lttng_channel_ioctl,
1664 #endif
1665 };
1666
1667 static const struct file_operations lttng_metadata_fops = {
1668 .owner = THIS_MODULE,
1669 .release = lttng_metadata_channel_release,
1670 .unlocked_ioctl = lttng_metadata_ioctl,
1671 #ifdef CONFIG_COMPAT
1672 .compat_ioctl = lttng_metadata_ioctl,
1673 #endif
1674 };
1675
1676 /**
1677 * lttng_event_ioctl - lttng syscall through ioctl
1678 *
1679 * @file: the file
1680 * @cmd: the command
1681 * @arg: command arg
1682 *
1683 * This ioctl implements lttng commands:
1684 * LTTNG_KERNEL_CONTEXT
1685 * Prepend a context field to each record of this event
1686 * LTTNG_KERNEL_ENABLE
1687 * Enable recording for this event (weak enable)
1688 * LTTNG_KERNEL_DISABLE
1689 * Disable recording for this event (strong disable)
1690 */
1691 static
1692 long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1693 {
1694 struct lttng_event *event;
1695 struct lttng_enabler *enabler;
1696 enum lttng_event_type *evtype = file->private_data;
1697
1698 switch (cmd) {
1699 case LTTNG_KERNEL_OLD_CONTEXT:
1700 {
1701 /* Not implemented */
1702 return -ENOSYS;
1703 }
1704 case LTTNG_KERNEL_CONTEXT:
1705 {
1706 /* Not implemented */
1707 return -ENOSYS;
1708 }
1709 case LTTNG_KERNEL_OLD_ENABLE:
1710 case LTTNG_KERNEL_ENABLE:
1711 switch (*evtype) {
1712 case LTTNG_TYPE_EVENT:
1713 event = file->private_data;
1714 return lttng_event_enable(event);
1715 case LTTNG_TYPE_ENABLER:
1716 enabler = file->private_data;
1717 return lttng_enabler_enable(enabler);
1718 default:
1719 WARN_ON_ONCE(1);
1720 return -ENOSYS;
1721 }
1722 case LTTNG_KERNEL_OLD_DISABLE:
1723 case LTTNG_KERNEL_DISABLE:
1724 switch (*evtype) {
1725 case LTTNG_TYPE_EVENT:
1726 event = file->private_data;
1727 return lttng_event_disable(event);
1728 case LTTNG_TYPE_ENABLER:
1729 enabler = file->private_data;
1730 return lttng_enabler_disable(enabler);
1731 default:
1732 WARN_ON_ONCE(1);
1733 return -ENOSYS;
1734 }
1735 case LTTNG_KERNEL_FILTER:
1736 switch (*evtype) {
1737 case LTTNG_TYPE_EVENT:
1738 return -EINVAL;
1739 case LTTNG_TYPE_ENABLER:
1740 {
1741 enabler = file->private_data;
1742 return lttng_enabler_attach_bytecode(enabler,
1743 (struct lttng_kernel_filter_bytecode __user *) arg);
1744 }
1745 default:
1746 WARN_ON_ONCE(1);
1747 return -ENOSYS;
1748 }
1749 case LTTNG_KERNEL_ADD_CALLSITE:
1750 switch (*evtype) {
1751 case LTTNG_TYPE_EVENT:
1752 event = file->private_data;
1753 return lttng_event_add_callsite(event,
1754 (struct lttng_kernel_event_callsite __user *) arg);
1755 case LTTNG_TYPE_ENABLER:
1756 return -EINVAL;
1757 default:
1758 WARN_ON_ONCE(1);
1759 return -ENOSYS;
1760 }
1761 default:
1762 return -ENOIOCTLCMD;
1763 }
1764 }
1765
1766 static
1767 int lttng_event_release(struct inode *inode, struct file *file)
1768 {
1769 struct lttng_event *event;
1770 struct lttng_enabler *enabler;
1771 enum lttng_event_type *evtype = file->private_data;
1772
1773 if (!evtype)
1774 return 0;
1775
1776 switch (*evtype) {
1777 case LTTNG_TYPE_EVENT:
1778 event = file->private_data;
1779 if (event)
1780 fput(event->chan->file);
1781 break;
1782 case LTTNG_TYPE_ENABLER:
1783 enabler = file->private_data;
1784 if (enabler)
1785 fput(enabler->chan->file);
1786 break;
1787 default:
1788 WARN_ON_ONCE(1);
1789 break;
1790 }
1791
1792 return 0;
1793 }
1794
1795 /* TODO: filter control ioctl */
1796 static const struct file_operations lttng_event_fops = {
1797 .owner = THIS_MODULE,
1798 .release = lttng_event_release,
1799 .unlocked_ioctl = lttng_event_ioctl,
1800 #ifdef CONFIG_COMPAT
1801 .compat_ioctl = lttng_event_ioctl,
1802 #endif
1803 };
1804
1805 static int put_u64(uint64_t val, unsigned long arg)
1806 {
1807 return put_user(val, (uint64_t __user *) arg);
1808 }
1809
1810 static int put_u32(uint32_t val, unsigned long arg)
1811 {
1812 return put_user(val, (uint32_t __user *) arg);
1813 }
1814
1815 static long lttng_stream_ring_buffer_ioctl(struct file *filp,
1816 unsigned int cmd, unsigned long arg)
1817 {
1818 struct lib_ring_buffer *buf = filp->private_data;
1819 struct channel *chan = buf->backend.chan;
1820 const struct lib_ring_buffer_config *config = &chan->backend.config;
1821 const struct lttng_channel_ops *ops = chan->backend.priv_ops;
1822 int ret;
1823
1824 if (atomic_read(&chan->record_disabled))
1825 return -EIO;
1826
1827 switch (cmd) {
1828 case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN:
1829 {
1830 uint64_t ts;
1831
1832 ret = ops->timestamp_begin(config, buf, &ts);
1833 if (ret < 0)
1834 goto error;
1835 return put_u64(ts, arg);
1836 }
1837 case LTTNG_RING_BUFFER_GET_TIMESTAMP_END:
1838 {
1839 uint64_t ts;
1840
1841 ret = ops->timestamp_end(config, buf, &ts);
1842 if (ret < 0)
1843 goto error;
1844 return put_u64(ts, arg);
1845 }
1846 case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED:
1847 {
1848 uint64_t ed;
1849
1850 ret = ops->events_discarded(config, buf, &ed);
1851 if (ret < 0)
1852 goto error;
1853 return put_u64(ed, arg);
1854 }
1855 case LTTNG_RING_BUFFER_GET_CONTENT_SIZE:
1856 {
1857 uint64_t cs;
1858
1859 ret = ops->content_size(config, buf, &cs);
1860 if (ret < 0)
1861 goto error;
1862 return put_u64(cs, arg);
1863 }
1864 case LTTNG_RING_BUFFER_GET_PACKET_SIZE:
1865 {
1866 uint64_t ps;
1867
1868 ret = ops->packet_size(config, buf, &ps);
1869 if (ret < 0)
1870 goto error;
1871 return put_u64(ps, arg);
1872 }
1873 case LTTNG_RING_BUFFER_GET_STREAM_ID:
1874 {
1875 uint64_t si;
1876
1877 ret = ops->stream_id(config, buf, &si);
1878 if (ret < 0)
1879 goto error;
1880 return put_u64(si, arg);
1881 }
1882 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP:
1883 {
1884 uint64_t ts;
1885
1886 ret = ops->current_timestamp(config, buf, &ts);
1887 if (ret < 0)
1888 goto error;
1889 return put_u64(ts, arg);
1890 }
1891 case LTTNG_RING_BUFFER_GET_SEQ_NUM:
1892 {
1893 uint64_t seq;
1894
1895 ret = ops->sequence_number(config, buf, &seq);
1896 if (ret < 0)
1897 goto error;
1898 return put_u64(seq, arg);
1899 }
1900 case LTTNG_RING_BUFFER_INSTANCE_ID:
1901 {
1902 uint64_t id;
1903
1904 ret = ops->instance_id(config, buf, &id);
1905 if (ret < 0)
1906 goto error;
1907 return put_u64(id, arg);
1908 }
1909 default:
1910 return lib_ring_buffer_file_operations.unlocked_ioctl(filp,
1911 cmd, arg);
1912 }
1913
1914 error:
1915 return -ENOSYS;
1916 }
1917
1918 #ifdef CONFIG_COMPAT
1919 static long lttng_stream_ring_buffer_compat_ioctl(struct file *filp,
1920 unsigned int cmd, unsigned long arg)
1921 {
1922 struct lib_ring_buffer *buf = filp->private_data;
1923 struct channel *chan = buf->backend.chan;
1924 const struct lib_ring_buffer_config *config = &chan->backend.config;
1925 const struct lttng_channel_ops *ops = chan->backend.priv_ops;
1926 int ret;
1927
1928 if (atomic_read(&chan->record_disabled))
1929 return -EIO;
1930
1931 switch (cmd) {
1932 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN:
1933 {
1934 uint64_t ts;
1935
1936 ret = ops->timestamp_begin(config, buf, &ts);
1937 if (ret < 0)
1938 goto error;
1939 return put_u64(ts, arg);
1940 }
1941 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END:
1942 {
1943 uint64_t ts;
1944
1945 ret = ops->timestamp_end(config, buf, &ts);
1946 if (ret < 0)
1947 goto error;
1948 return put_u64(ts, arg);
1949 }
1950 case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED:
1951 {
1952 uint64_t ed;
1953
1954 ret = ops->events_discarded(config, buf, &ed);
1955 if (ret < 0)
1956 goto error;
1957 return put_u64(ed, arg);
1958 }
1959 case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE:
1960 {
1961 uint64_t cs;
1962
1963 ret = ops->content_size(config, buf, &cs);
1964 if (ret < 0)
1965 goto error;
1966 return put_u64(cs, arg);
1967 }
1968 case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE:
1969 {
1970 uint64_t ps;
1971
1972 ret = ops->packet_size(config, buf, &ps);
1973 if (ret < 0)
1974 goto error;
1975 return put_u64(ps, arg);
1976 }
1977 case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID:
1978 {
1979 uint64_t si;
1980
1981 ret = ops->stream_id(config, buf, &si);
1982 if (ret < 0)
1983 goto error;
1984 return put_u64(si, arg);
1985 }
1986 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP:
1987 {
1988 uint64_t ts;
1989
1990 ret = ops->current_timestamp(config, buf, &ts);
1991 if (ret < 0)
1992 goto error;
1993 return put_u64(ts, arg);
1994 }
1995 case LTTNG_RING_BUFFER_COMPAT_GET_SEQ_NUM:
1996 {
1997 uint64_t seq;
1998
1999 ret = ops->sequence_number(config, buf, &seq);
2000 if (ret < 0)
2001 goto error;
2002 return put_u64(seq, arg);
2003 }
2004 case LTTNG_RING_BUFFER_COMPAT_INSTANCE_ID:
2005 {
2006 uint64_t id;
2007
2008 ret = ops->instance_id(config, buf, &id);
2009 if (ret < 0)
2010 goto error;
2011 return put_u64(id, arg);
2012 }
2013 default:
2014 return lib_ring_buffer_file_operations.compat_ioctl(filp,
2015 cmd, arg);
2016 }
2017
2018 error:
2019 return -ENOSYS;
2020 }
2021 #endif /* CONFIG_COMPAT */
2022
2023 static void lttng_stream_override_ring_buffer_fops(void)
2024 {
2025 lttng_stream_ring_buffer_file_operations.owner = THIS_MODULE;
2026 lttng_stream_ring_buffer_file_operations.open =
2027 lib_ring_buffer_file_operations.open;
2028 lttng_stream_ring_buffer_file_operations.release =
2029 lib_ring_buffer_file_operations.release;
2030 lttng_stream_ring_buffer_file_operations.poll =
2031 lib_ring_buffer_file_operations.poll;
2032 lttng_stream_ring_buffer_file_operations.splice_read =
2033 lib_ring_buffer_file_operations.splice_read;
2034 lttng_stream_ring_buffer_file_operations.mmap =
2035 lib_ring_buffer_file_operations.mmap;
2036 lttng_stream_ring_buffer_file_operations.unlocked_ioctl =
2037 lttng_stream_ring_buffer_ioctl;
2038 lttng_stream_ring_buffer_file_operations.llseek =
2039 lib_ring_buffer_file_operations.llseek;
2040 #ifdef CONFIG_COMPAT
2041 lttng_stream_ring_buffer_file_operations.compat_ioctl =
2042 lttng_stream_ring_buffer_compat_ioctl;
2043 #endif
2044 }
2045
2046 int __init lttng_abi_init(void)
2047 {
2048 int ret = 0;
2049
2050 wrapper_vmalloc_sync_mappings();
2051 lttng_clock_ref();
2052
2053 ret = lttng_tp_mempool_init();
2054 if (ret) {
2055 goto error;
2056 }
2057
2058 lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL,
2059 &lttng_proc_ops, NULL);
2060
2061 if (!lttng_proc_dentry) {
2062 printk(KERN_ERR "Error creating LTTng control file\n");
2063 ret = -ENOMEM;
2064 goto error;
2065 }
2066 lttng_stream_override_ring_buffer_fops();
2067 return 0;
2068
2069 error:
2070 lttng_tp_mempool_destroy();
2071 lttng_clock_unref();
2072 return ret;
2073 }
2074
2075 /* No __exit annotation because used by init error path too. */
2076 void lttng_abi_exit(void)
2077 {
2078 lttng_tp_mempool_destroy();
2079 lttng_clock_unref();
2080 if (lttng_proc_dentry)
2081 remove_proc_entry("lttng", NULL);
2082 }
This page took 0.09439 seconds and 4 git commands to generate.