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