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