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