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