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