Fix: metadata lttng channel refcount
[lttng-modules.git] / lttng-abi.c
1 /*
2 * lttng-abi.c
3 *
4 * LTTng ABI
5 *
6 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 *
23 * Mimic system calls for:
24 * - session creation, returns a file descriptor or failure.
25 * - channel creation, returns a file descriptor or failure.
26 * - Operates on a session file descriptor
27 * - Takes all channel options as parameters.
28 * - stream get, returns a file descriptor or failure.
29 * - Operates on a channel file descriptor.
30 * - stream notifier get, returns a file descriptor or failure.
31 * - Operates on a channel file descriptor.
32 * - event creation, returns a file descriptor or failure.
33 * - Operates on a channel file descriptor
34 * - Takes an event name as parameter
35 * - Takes an instrumentation source as parameter
36 * - e.g. tracepoints, dynamic_probes...
37 * - Takes instrumentation source specific arguments.
38 */
39
40 #include <linux/module.h>
41 #include <linux/proc_fs.h>
42 #include <linux/anon_inodes.h>
43 #include <linux/file.h>
44 #include <linux/uaccess.h>
45 #include <linux/slab.h>
46 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
47 #include "wrapper/ringbuffer/vfs.h"
48 #include "wrapper/ringbuffer/backend.h"
49 #include "wrapper/ringbuffer/frontend.h"
50 #include "wrapper/poll.h"
51 #include "lttng-abi.h"
52 #include "lttng-abi-old.h"
53 #include "lttng-events.h"
54 #include "lttng-tracer.h"
55
56 /*
57 * This is LTTng's own personal way to create a system call as an external
58 * module. We use ioctl() on /proc/lttng.
59 */
60
61 static struct proc_dir_entry *lttng_proc_dentry;
62 static const struct file_operations lttng_fops;
63 static const struct file_operations lttng_session_fops;
64 static const struct file_operations lttng_channel_fops;
65 static const struct file_operations lttng_metadata_fops;
66 static const struct file_operations lttng_event_fops;
67
68 /*
69 * Teardown management: opened file descriptors keep a refcount on the module,
70 * so it can only exit when all file descriptors are closed.
71 */
72
73 static
74 int lttng_abi_create_session(void)
75 {
76 struct lttng_session *session;
77 struct file *session_file;
78 int session_fd, ret;
79
80 session = lttng_session_create();
81 if (!session)
82 return -ENOMEM;
83 session_fd = get_unused_fd();
84 if (session_fd < 0) {
85 ret = session_fd;
86 goto fd_error;
87 }
88 session_file = anon_inode_getfile("[lttng_session]",
89 &lttng_session_fops,
90 session, O_RDWR);
91 if (IS_ERR(session_file)) {
92 ret = PTR_ERR(session_file);
93 goto file_error;
94 }
95 session->file = session_file;
96 fd_install(session_fd, session_file);
97 return session_fd;
98
99 file_error:
100 put_unused_fd(session_fd);
101 fd_error:
102 lttng_session_destroy(session);
103 return ret;
104 }
105
106 static
107 int lttng_abi_tracepoint_list(void)
108 {
109 struct file *tracepoint_list_file;
110 int file_fd, ret;
111
112 file_fd = get_unused_fd();
113 if (file_fd < 0) {
114 ret = file_fd;
115 goto fd_error;
116 }
117
118 tracepoint_list_file = anon_inode_getfile("[lttng_session]",
119 &lttng_tracepoint_list_fops,
120 NULL, O_RDWR);
121 if (IS_ERR(tracepoint_list_file)) {
122 ret = PTR_ERR(tracepoint_list_file);
123 goto file_error;
124 }
125 ret = lttng_tracepoint_list_fops.open(NULL, tracepoint_list_file);
126 if (ret < 0)
127 goto open_error;
128 fd_install(file_fd, tracepoint_list_file);
129 if (file_fd < 0) {
130 ret = file_fd;
131 goto fd_error;
132 }
133 return file_fd;
134
135 open_error:
136 fput(tracepoint_list_file);
137 file_error:
138 put_unused_fd(file_fd);
139 fd_error:
140 return ret;
141 }
142
143 static
144 void lttng_abi_tracer_version(struct lttng_kernel_tracer_version *v)
145 {
146 v->major = LTTNG_MODULES_MAJOR_VERSION;
147 v->minor = LTTNG_MODULES_MINOR_VERSION;
148 v->patchlevel = LTTNG_MODULES_PATCHLEVEL_VERSION;
149 }
150
151 static
152 long lttng_abi_add_context(struct file *file,
153 struct lttng_kernel_context *context_param,
154 struct lttng_ctx **ctx, struct lttng_session *session)
155 {
156
157 if (session->been_active)
158 return -EPERM;
159
160 switch (context_param->ctx) {
161 case LTTNG_KERNEL_CONTEXT_PID:
162 return lttng_add_pid_to_ctx(ctx);
163 case LTTNG_KERNEL_CONTEXT_PRIO:
164 return lttng_add_prio_to_ctx(ctx);
165 case LTTNG_KERNEL_CONTEXT_NICE:
166 return lttng_add_nice_to_ctx(ctx);
167 case LTTNG_KERNEL_CONTEXT_VPID:
168 return lttng_add_vpid_to_ctx(ctx);
169 case LTTNG_KERNEL_CONTEXT_TID:
170 return lttng_add_tid_to_ctx(ctx);
171 case LTTNG_KERNEL_CONTEXT_VTID:
172 return lttng_add_vtid_to_ctx(ctx);
173 case LTTNG_KERNEL_CONTEXT_PPID:
174 return lttng_add_ppid_to_ctx(ctx);
175 case LTTNG_KERNEL_CONTEXT_VPPID:
176 return lttng_add_vppid_to_ctx(ctx);
177 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER:
178 context_param->u.perf_counter.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
179 return lttng_add_perf_counter_to_ctx(context_param->u.perf_counter.type,
180 context_param->u.perf_counter.config,
181 context_param->u.perf_counter.name,
182 ctx);
183 case LTTNG_KERNEL_CONTEXT_PROCNAME:
184 return lttng_add_procname_to_ctx(ctx);
185 case LTTNG_KERNEL_CONTEXT_HOSTNAME:
186 return lttng_add_hostname_to_ctx(ctx);
187 default:
188 return -EINVAL;
189 }
190 }
191
192 /**
193 * lttng_ioctl - lttng syscall through ioctl
194 *
195 * @file: the file
196 * @cmd: the command
197 * @arg: command arg
198 *
199 * This ioctl implements lttng commands:
200 * LTTNG_KERNEL_SESSION
201 * Returns a LTTng trace session file descriptor
202 * LTTNG_KERNEL_TRACER_VERSION
203 * Returns the LTTng kernel tracer version
204 * LTTNG_KERNEL_TRACEPOINT_LIST
205 * Returns a file descriptor listing available tracepoints
206 * LTTNG_KERNEL_WAIT_QUIESCENT
207 * Returns after all previously running probes have completed
208 *
209 * The returned session will be deleted when its file descriptor is closed.
210 */
211 static
212 long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
213 {
214 switch (cmd) {
215 case LTTNG_KERNEL_OLD_SESSION:
216 case LTTNG_KERNEL_SESSION:
217 return lttng_abi_create_session();
218 case LTTNG_KERNEL_OLD_TRACER_VERSION:
219 {
220 struct lttng_kernel_tracer_version v;
221 struct lttng_kernel_old_tracer_version oldv;
222 struct lttng_kernel_old_tracer_version *uversion =
223 (struct lttng_kernel_old_tracer_version __user *) arg;
224
225 lttng_abi_tracer_version(&v);
226 oldv.major = v.major;
227 oldv.minor = v.minor;
228 oldv.patchlevel = v.patchlevel;
229
230 if (copy_to_user(uversion, &oldv, sizeof(oldv)))
231 return -EFAULT;
232 return 0;
233 }
234 case LTTNG_KERNEL_TRACER_VERSION:
235 {
236 struct lttng_kernel_tracer_version version;
237 struct lttng_kernel_tracer_version *uversion =
238 (struct lttng_kernel_tracer_version __user *) arg;
239
240 lttng_abi_tracer_version(&version);
241
242 if (copy_to_user(uversion, &version, sizeof(version)))
243 return -EFAULT;
244 return 0;
245 }
246 case LTTNG_KERNEL_OLD_TRACEPOINT_LIST:
247 case LTTNG_KERNEL_TRACEPOINT_LIST:
248 return lttng_abi_tracepoint_list();
249 case LTTNG_KERNEL_OLD_WAIT_QUIESCENT:
250 case LTTNG_KERNEL_WAIT_QUIESCENT:
251 synchronize_trace();
252 return 0;
253 case LTTNG_KERNEL_OLD_CALIBRATE:
254 {
255 struct lttng_kernel_old_calibrate __user *ucalibrate =
256 (struct lttng_kernel_old_calibrate __user *) arg;
257 struct lttng_kernel_old_calibrate old_calibrate;
258 struct lttng_kernel_calibrate calibrate;
259 int ret;
260
261 if (copy_from_user(&old_calibrate, ucalibrate, sizeof(old_calibrate)))
262 return -EFAULT;
263 calibrate.type = old_calibrate.type;
264 ret = lttng_calibrate(&calibrate);
265 if (copy_to_user(ucalibrate, &old_calibrate, sizeof(old_calibrate)))
266 return -EFAULT;
267 return ret;
268 }
269 case LTTNG_KERNEL_CALIBRATE:
270 {
271 struct lttng_kernel_calibrate __user *ucalibrate =
272 (struct lttng_kernel_calibrate __user *) arg;
273 struct lttng_kernel_calibrate calibrate;
274 int ret;
275
276 if (copy_from_user(&calibrate, ucalibrate, sizeof(calibrate)))
277 return -EFAULT;
278 ret = lttng_calibrate(&calibrate);
279 if (copy_to_user(ucalibrate, &calibrate, sizeof(calibrate)))
280 return -EFAULT;
281 return ret;
282 }
283 default:
284 return -ENOIOCTLCMD;
285 }
286 }
287
288 static const struct file_operations lttng_fops = {
289 .owner = THIS_MODULE,
290 .unlocked_ioctl = lttng_ioctl,
291 #ifdef CONFIG_COMPAT
292 .compat_ioctl = lttng_ioctl,
293 #endif
294 };
295
296 static
297 int lttng_abi_create_channel(struct file *session_file,
298 struct lttng_kernel_channel *chan_param,
299 enum channel_type channel_type)
300 {
301 struct lttng_session *session = session_file->private_data;
302 const struct file_operations *fops = NULL;
303 const char *transport_name;
304 struct lttng_channel *chan;
305 struct file *chan_file;
306 int chan_fd;
307 int ret = 0;
308
309 chan_fd = get_unused_fd();
310 if (chan_fd < 0) {
311 ret = chan_fd;
312 goto fd_error;
313 }
314 switch (channel_type) {
315 case PER_CPU_CHANNEL:
316 fops = &lttng_channel_fops;
317 break;
318 case METADATA_CHANNEL:
319 fops = &lttng_metadata_fops;
320 break;
321 }
322
323 chan_file = anon_inode_getfile("[lttng_channel]",
324 fops,
325 NULL, O_RDWR);
326 if (IS_ERR(chan_file)) {
327 ret = PTR_ERR(chan_file);
328 goto file_error;
329 }
330 switch (channel_type) {
331 case PER_CPU_CHANNEL:
332 if (chan_param->output == LTTNG_KERNEL_SPLICE) {
333 transport_name = chan_param->overwrite ?
334 "relay-overwrite" : "relay-discard";
335 } else if (chan_param->output == LTTNG_KERNEL_MMAP) {
336 transport_name = chan_param->overwrite ?
337 "relay-overwrite-mmap" : "relay-discard-mmap";
338 } else {
339 return -EINVAL;
340 }
341 break;
342 case METADATA_CHANNEL:
343 if (chan_param->output == LTTNG_KERNEL_SPLICE)
344 transport_name = "relay-metadata";
345 else if (chan_param->output == LTTNG_KERNEL_MMAP)
346 transport_name = "relay-metadata-mmap";
347 else
348 return -EINVAL;
349 break;
350 default:
351 transport_name = "<unknown>";
352 break;
353 }
354 /*
355 * We tolerate no failure path after channel creation. It will stay
356 * invariant for the rest of the session.
357 */
358 chan = lttng_channel_create(session, transport_name, NULL,
359 chan_param->subbuf_size,
360 chan_param->num_subbuf,
361 chan_param->switch_timer_interval,
362 chan_param->read_timer_interval,
363 channel_type);
364 if (!chan) {
365 ret = -EINVAL;
366 goto chan_error;
367 }
368 chan->file = chan_file;
369 chan_file->private_data = chan;
370 fd_install(chan_fd, chan_file);
371 atomic_long_inc(&session_file->f_count);
372
373 return chan_fd;
374
375 chan_error:
376 fput(chan_file);
377 file_error:
378 put_unused_fd(chan_fd);
379 fd_error:
380 return ret;
381 }
382
383 /**
384 * lttng_session_ioctl - lttng session fd ioctl
385 *
386 * @file: the file
387 * @cmd: the command
388 * @arg: command arg
389 *
390 * This ioctl implements lttng commands:
391 * LTTNG_KERNEL_CHANNEL
392 * Returns a LTTng channel file descriptor
393 * LTTNG_KERNEL_ENABLE
394 * Enables tracing for a session (weak enable)
395 * LTTNG_KERNEL_DISABLE
396 * Disables tracing for a session (strong disable)
397 * LTTNG_KERNEL_METADATA
398 * Returns a LTTng metadata file descriptor
399 *
400 * The returned channel will be deleted when its file descriptor is closed.
401 */
402 static
403 long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
404 {
405 struct lttng_session *session = file->private_data;
406
407 switch (cmd) {
408 case LTTNG_KERNEL_OLD_CHANNEL:
409 {
410 struct lttng_kernel_channel chan_param;
411 struct lttng_kernel_old_channel old_chan_param;
412
413 if (copy_from_user(&old_chan_param,
414 (struct lttng_kernel_old_channel __user *) arg,
415 sizeof(struct lttng_kernel_old_channel)))
416 return -EFAULT;
417 chan_param.overwrite = old_chan_param.overwrite;
418 chan_param.subbuf_size = old_chan_param.subbuf_size;
419 chan_param.num_subbuf = old_chan_param.num_subbuf;
420 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
421 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
422 chan_param.output = old_chan_param.output;
423
424 return lttng_abi_create_channel(file, &chan_param,
425 PER_CPU_CHANNEL);
426 }
427 case LTTNG_KERNEL_CHANNEL:
428 {
429 struct lttng_kernel_channel chan_param;
430
431 if (copy_from_user(&chan_param,
432 (struct lttng_kernel_channel __user *) arg,
433 sizeof(struct lttng_kernel_channel)))
434 return -EFAULT;
435 return lttng_abi_create_channel(file, &chan_param,
436 PER_CPU_CHANNEL);
437 }
438 case LTTNG_KERNEL_OLD_SESSION_START:
439 case LTTNG_KERNEL_OLD_ENABLE:
440 case LTTNG_KERNEL_SESSION_START:
441 case LTTNG_KERNEL_ENABLE:
442 return lttng_session_enable(session);
443 case LTTNG_KERNEL_OLD_SESSION_STOP:
444 case LTTNG_KERNEL_OLD_DISABLE:
445 case LTTNG_KERNEL_SESSION_STOP:
446 case LTTNG_KERNEL_DISABLE:
447 return lttng_session_disable(session);
448 case LTTNG_KERNEL_OLD_METADATA:
449 {
450 struct lttng_kernel_channel chan_param;
451 struct lttng_kernel_old_channel old_chan_param;
452
453 if (copy_from_user(&old_chan_param,
454 (struct lttng_kernel_old_channel __user *) arg,
455 sizeof(struct lttng_kernel_old_channel)))
456 return -EFAULT;
457 chan_param.overwrite = old_chan_param.overwrite;
458 chan_param.subbuf_size = old_chan_param.subbuf_size;
459 chan_param.num_subbuf = old_chan_param.num_subbuf;
460 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
461 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
462 chan_param.output = old_chan_param.output;
463
464 return lttng_abi_create_channel(file, &chan_param,
465 METADATA_CHANNEL);
466 }
467 case LTTNG_KERNEL_METADATA:
468 {
469 struct lttng_kernel_channel chan_param;
470
471 if (copy_from_user(&chan_param,
472 (struct lttng_kernel_channel __user *) arg,
473 sizeof(struct lttng_kernel_channel)))
474 return -EFAULT;
475 return lttng_abi_create_channel(file, &chan_param,
476 METADATA_CHANNEL);
477 }
478 default:
479 return -ENOIOCTLCMD;
480 }
481 }
482
483 /*
484 * Called when the last file reference is dropped.
485 *
486 * Big fat note: channels and events are invariant for the whole session after
487 * their creation. So this session destruction also destroys all channel and
488 * event structures specific to this session (they are not destroyed when their
489 * individual file is released).
490 */
491 static
492 int lttng_session_release(struct inode *inode, struct file *file)
493 {
494 struct lttng_session *session = file->private_data;
495
496 if (session)
497 lttng_session_destroy(session);
498 return 0;
499 }
500
501 static const struct file_operations lttng_session_fops = {
502 .owner = THIS_MODULE,
503 .release = lttng_session_release,
504 .unlocked_ioctl = lttng_session_ioctl,
505 #ifdef CONFIG_COMPAT
506 .compat_ioctl = lttng_session_ioctl,
507 #endif
508 };
509
510 /**
511 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
512 * @filp: the file
513 * @wait: poll table
514 *
515 * Handles the poll operations for the metadata channels.
516 */
517 static
518 unsigned int lttng_metadata_ring_buffer_poll(struct file *filp,
519 poll_table *wait)
520 {
521 struct lttng_metadata_stream *stream = filp->private_data;
522 struct lib_ring_buffer *buf = stream->priv;
523 int finalized;
524 unsigned int mask = 0;
525
526 if (filp->f_mode & FMODE_READ) {
527 poll_wait_set_exclusive(wait);
528 poll_wait(filp, &stream->read_wait, wait);
529
530 finalized = stream->finalized;
531
532 /*
533 * lib_ring_buffer_is_finalized() contains a smp_rmb()
534 * ordering finalized load before offsets loads.
535 */
536 WARN_ON(atomic_long_read(&buf->active_readers) != 1);
537
538 if (finalized)
539 mask |= POLLHUP;
540
541 if (stream->metadata_cache->metadata_written >
542 stream->metadata_out)
543 mask |= POLLIN;
544 }
545
546 return mask;
547 }
548
549 static
550 int lttng_metadata_ring_buffer_ioctl_get_next_subbuf(struct file *filp,
551 unsigned int cmd, unsigned long arg)
552 {
553 struct lttng_metadata_stream *stream = filp->private_data;
554 struct lib_ring_buffer *buf = stream->priv;
555 struct channel *chan = buf->backend.chan;
556 struct lttng_channel *lttng_chan = channel_get_private(chan);
557 int ret;
558
559 ret = lttng_metadata_output_channel(lttng_chan, stream);
560 if (ret > 0) {
561 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
562 ret = 0;
563 }
564 return ret;
565 }
566
567 static
568 void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file *filp,
569 unsigned int cmd, unsigned long arg)
570 {
571 struct lttng_metadata_stream *stream = filp->private_data;
572
573 stream->metadata_out = stream->metadata_in;
574 }
575
576 static
577 long lttng_metadata_ring_buffer_ioctl(struct file *filp,
578 unsigned int cmd, unsigned long arg)
579 {
580 int ret;
581 struct lttng_metadata_stream *stream = filp->private_data;
582 struct lib_ring_buffer *buf = stream->priv;
583
584 switch (cmd) {
585 case RING_BUFFER_GET_NEXT_SUBBUF:
586 {
587 ret = lttng_metadata_ring_buffer_ioctl_get_next_subbuf(filp,
588 cmd, arg);
589 if (ret < 0)
590 goto err;
591 break;
592 }
593 case RING_BUFFER_GET_SUBBUF:
594 {
595 /*
596 * Random access is not allowed for metadata channel.
597 */
598 return -ENOSYS;
599 }
600 default:
601 break;
602 }
603 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
604
605 /* Performing lib ring buffer ioctl after our own. */
606 ret = lib_ring_buffer_ioctl(filp, cmd, arg, buf);
607 if (ret < 0)
608 goto err;
609
610 switch (cmd) {
611 case RING_BUFFER_PUT_NEXT_SUBBUF:
612 {
613 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
614 cmd, arg);
615 break;
616 }
617 default:
618 break;
619 }
620 err:
621 return ret;
622 }
623
624 #ifdef CONFIG_COMPAT
625 static
626 long lttng_metadata_ring_buffer_compat_ioctl(struct file *filp,
627 unsigned int cmd, unsigned long arg)
628 {
629 int ret;
630 struct lttng_metadata_stream *stream = filp->private_data;
631 struct lib_ring_buffer *buf = stream->priv;
632
633 switch (cmd) {
634 case RING_BUFFER_GET_NEXT_SUBBUF:
635 {
636 ret = lttng_metadata_ring_buffer_ioctl_get_next_subbuf(filp,
637 cmd, arg);
638 if (ret < 0)
639 goto err;
640 break;
641 }
642 case RING_BUFFER_GET_SUBBUF:
643 {
644 /*
645 * Random access is not allowed for metadata channel.
646 */
647 return -ENOSYS;
648 }
649 default:
650 break;
651 }
652 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
653
654 /* Performing lib ring buffer ioctl after our own. */
655 ret = lib_ring_buffer_compat_ioctl(filp, cmd, arg, buf);
656 if (ret < 0)
657 goto err;
658
659 switch (cmd) {
660 case RING_BUFFER_PUT_NEXT_SUBBUF:
661 {
662 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
663 cmd, arg);
664 break;
665 }
666 default:
667 break;
668 }
669 err:
670 return ret;
671 }
672 #endif
673
674 static
675 int lttng_metadata_ring_buffer_open(struct inode *inode, struct file *file)
676 {
677 struct lttng_metadata_stream *stream = inode->i_private;
678 struct lib_ring_buffer *buf = stream->priv;
679
680 file->private_data = buf;
681 return lib_ring_buffer_open(inode, file, buf);
682 }
683
684 static
685 int lttng_metadata_ring_buffer_release(struct inode *inode, struct file *file)
686 {
687 struct lttng_metadata_stream *stream = file->private_data;
688 struct lib_ring_buffer *buf = stream->priv;
689 struct channel *chan = buf->backend.chan;
690 struct lttng_channel *lttng_chan = channel_get_private(chan);
691
692 kref_put(&stream->metadata_cache->refcount, metadata_cache_destroy);
693 fput(lttng_chan->file);
694
695 return lib_ring_buffer_release(inode, file, buf);
696 }
697
698 static
699 ssize_t lttng_metadata_ring_buffer_splice_read(struct file *in, loff_t *ppos,
700 struct pipe_inode_info *pipe, size_t len,
701 unsigned int flags)
702 {
703 struct lttng_metadata_stream *stream = in->private_data;
704 struct lib_ring_buffer *buf = stream->priv;
705
706 return lib_ring_buffer_splice_read(in, ppos, pipe, len,
707 flags, buf);
708 }
709
710 static
711 int lttng_metadata_ring_buffer_mmap(struct file *filp,
712 struct vm_area_struct *vma)
713 {
714 struct lttng_metadata_stream *stream = filp->private_data;
715 struct lib_ring_buffer *buf = stream->priv;
716
717 return lib_ring_buffer_mmap(filp, vma, buf);
718 }
719
720 static
721 const struct file_operations lttng_metadata_ring_buffer_file_operations = {
722 .owner = THIS_MODULE,
723 .open = lttng_metadata_ring_buffer_open,
724 .release = lttng_metadata_ring_buffer_release,
725 .poll = lttng_metadata_ring_buffer_poll,
726 .splice_read = lttng_metadata_ring_buffer_splice_read,
727 .mmap = lttng_metadata_ring_buffer_mmap,
728 .unlocked_ioctl = lttng_metadata_ring_buffer_ioctl,
729 .llseek = vfs_lib_ring_buffer_no_llseek,
730 #ifdef CONFIG_COMPAT
731 .compat_ioctl = lttng_metadata_ring_buffer_compat_ioctl,
732 #endif
733 };
734
735 static
736 int lttng_abi_create_stream_fd(struct file *channel_file, void *stream_priv,
737 const struct file_operations *fops)
738 {
739 int stream_fd, ret;
740 struct file *stream_file;
741
742 stream_fd = get_unused_fd();
743 if (stream_fd < 0) {
744 ret = stream_fd;
745 goto fd_error;
746 }
747 stream_file = anon_inode_getfile("[lttng_stream]", fops,
748 stream_priv, O_RDWR);
749 if (IS_ERR(stream_file)) {
750 ret = PTR_ERR(stream_file);
751 goto file_error;
752 }
753 /*
754 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
755 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
756 * file descriptor, so we set FMODE_PREAD here.
757 */
758 stream_file->f_mode |= FMODE_PREAD;
759 fd_install(stream_fd, stream_file);
760 /*
761 * The stream holds a reference to the channel within the generic ring
762 * buffer library, so no need to hold a refcount on the channel and
763 * session files here.
764 */
765 return stream_fd;
766
767 file_error:
768 put_unused_fd(stream_fd);
769 fd_error:
770 return ret;
771 }
772
773 static
774 int lttng_abi_open_stream(struct file *channel_file)
775 {
776 struct lttng_channel *channel = channel_file->private_data;
777 struct lib_ring_buffer *buf;
778 int ret;
779 void *stream_priv;
780
781 buf = channel->ops->buffer_read_open(channel->chan);
782 if (!buf)
783 return -ENOENT;
784
785 stream_priv = buf;
786 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
787 &lib_ring_buffer_file_operations);
788 if (ret < 0)
789 goto fd_error;
790
791 return ret;
792
793 fd_error:
794 channel->ops->buffer_read_close(buf);
795 return ret;
796 }
797
798 static
799 int lttng_abi_open_metadata_stream(struct file *channel_file)
800 {
801 struct lttng_channel *channel = channel_file->private_data;
802 struct lttng_session *session = channel->session;
803 struct lib_ring_buffer *buf;
804 int ret;
805 struct lttng_metadata_stream *metadata_stream;
806 void *stream_priv;
807
808 buf = channel->ops->buffer_read_open(channel->chan);
809 if (!buf)
810 return -ENOENT;
811
812 metadata_stream = kzalloc(sizeof(struct lttng_metadata_stream),
813 GFP_KERNEL);
814 if (!metadata_stream)
815 return -ENOMEM;
816 metadata_stream->metadata_cache = session->metadata_cache;
817 init_waitqueue_head(&metadata_stream->read_wait);
818 metadata_stream->priv = buf;
819 stream_priv = metadata_stream;
820 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
821 &lttng_metadata_ring_buffer_file_operations);
822 if (ret < 0)
823 goto fd_error;
824
825 atomic_long_inc(&channel_file->f_count);
826 kref_get(&session->metadata_cache->refcount);
827 list_add(&metadata_stream->list,
828 &session->metadata_cache->metadata_stream);
829 return ret;
830
831 fd_error:
832 channel->ops->buffer_read_close(buf);
833 return ret;
834 }
835
836 static
837 int lttng_abi_create_event(struct file *channel_file,
838 struct lttng_kernel_event *event_param)
839 {
840 struct lttng_channel *channel = channel_file->private_data;
841 struct lttng_event *event;
842 int event_fd, ret;
843 struct file *event_file;
844
845 event_param->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
846 switch (event_param->instrumentation) {
847 case LTTNG_KERNEL_KRETPROBE:
848 event_param->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
849 break;
850 case LTTNG_KERNEL_KPROBE:
851 event_param->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
852 break;
853 case LTTNG_KERNEL_FUNCTION:
854 event_param->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
855 break;
856 default:
857 break;
858 }
859 switch (event_param->instrumentation) {
860 default:
861 event_fd = get_unused_fd();
862 if (event_fd < 0) {
863 ret = event_fd;
864 goto fd_error;
865 }
866 event_file = anon_inode_getfile("[lttng_event]",
867 &lttng_event_fops,
868 NULL, O_RDWR);
869 if (IS_ERR(event_file)) {
870 ret = PTR_ERR(event_file);
871 goto file_error;
872 }
873 /*
874 * We tolerate no failure path after event creation. It
875 * will stay invariant for the rest of the session.
876 */
877 event = lttng_event_create(channel, event_param, NULL, NULL);
878 if (!event) {
879 ret = -EINVAL;
880 goto event_error;
881 }
882 event_file->private_data = event;
883 fd_install(event_fd, event_file);
884 /* The event holds a reference on the channel */
885 atomic_long_inc(&channel_file->f_count);
886 break;
887 case LTTNG_KERNEL_SYSCALL:
888 /*
889 * Only all-syscall tracing supported for now.
890 */
891 if (event_param->name[0] != '\0')
892 return -EINVAL;
893 ret = lttng_syscalls_register(channel, NULL);
894 if (ret)
895 goto fd_error;
896 event_fd = 0;
897 break;
898 }
899 return event_fd;
900
901 event_error:
902 fput(event_file);
903 file_error:
904 put_unused_fd(event_fd);
905 fd_error:
906 return ret;
907 }
908
909 /**
910 * lttng_channel_ioctl - lttng syscall through ioctl
911 *
912 * @file: the file
913 * @cmd: the command
914 * @arg: command arg
915 *
916 * This ioctl implements lttng commands:
917 * LTTNG_KERNEL_STREAM
918 * Returns an event stream file descriptor or failure.
919 * (typically, one event stream records events from one CPU)
920 * LTTNG_KERNEL_EVENT
921 * Returns an event file descriptor or failure.
922 * LTTNG_KERNEL_CONTEXT
923 * Prepend a context field to each event in the channel
924 * LTTNG_KERNEL_ENABLE
925 * Enable recording for events in this channel (weak enable)
926 * LTTNG_KERNEL_DISABLE
927 * Disable recording for events in this channel (strong disable)
928 *
929 * Channel and event file descriptors also hold a reference on the session.
930 */
931 static
932 long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
933 {
934 struct lttng_channel *channel = file->private_data;
935
936 switch (cmd) {
937 case LTTNG_KERNEL_OLD_STREAM:
938 case LTTNG_KERNEL_STREAM:
939 return lttng_abi_open_stream(file);
940 case LTTNG_KERNEL_OLD_EVENT:
941 {
942 struct lttng_kernel_event *uevent_param;
943 struct lttng_kernel_old_event *old_uevent_param;
944 int ret;
945
946 uevent_param = kmalloc(sizeof(struct lttng_kernel_event),
947 GFP_KERNEL);
948 if (!uevent_param) {
949 ret = -ENOMEM;
950 goto old_event_end;
951 }
952 old_uevent_param = kmalloc(
953 sizeof(struct lttng_kernel_old_event),
954 GFP_KERNEL);
955 if (!old_uevent_param) {
956 ret = -ENOMEM;
957 goto old_event_error_free_param;
958 }
959 if (copy_from_user(old_uevent_param,
960 (struct lttng_kernel_old_event __user *) arg,
961 sizeof(struct lttng_kernel_old_event))) {
962 ret = -EFAULT;
963 goto old_event_error_free_old_param;
964 }
965
966 memcpy(uevent_param->name, old_uevent_param->name,
967 sizeof(uevent_param->name));
968 uevent_param->instrumentation =
969 old_uevent_param->instrumentation;
970
971 switch (old_uevent_param->instrumentation) {
972 case LTTNG_KERNEL_KPROBE:
973 uevent_param->u.kprobe.addr =
974 old_uevent_param->u.kprobe.addr;
975 uevent_param->u.kprobe.offset =
976 old_uevent_param->u.kprobe.offset;
977 memcpy(uevent_param->u.kprobe.symbol_name,
978 old_uevent_param->u.kprobe.symbol_name,
979 sizeof(uevent_param->u.kprobe.symbol_name));
980 break;
981 case LTTNG_KERNEL_KRETPROBE:
982 uevent_param->u.kretprobe.addr =
983 old_uevent_param->u.kretprobe.addr;
984 uevent_param->u.kretprobe.offset =
985 old_uevent_param->u.kretprobe.offset;
986 memcpy(uevent_param->u.kretprobe.symbol_name,
987 old_uevent_param->u.kretprobe.symbol_name,
988 sizeof(uevent_param->u.kretprobe.symbol_name));
989 break;
990 case LTTNG_KERNEL_FUNCTION:
991 memcpy(uevent_param->u.ftrace.symbol_name,
992 old_uevent_param->u.ftrace.symbol_name,
993 sizeof(uevent_param->u.ftrace.symbol_name));
994 break;
995 default:
996 break;
997 }
998 ret = lttng_abi_create_event(file, uevent_param);
999
1000 old_event_error_free_old_param:
1001 kfree(old_uevent_param);
1002 old_event_error_free_param:
1003 kfree(uevent_param);
1004 old_event_end:
1005 return ret;
1006 }
1007 case LTTNG_KERNEL_EVENT:
1008 {
1009 struct lttng_kernel_event uevent_param;
1010
1011 if (copy_from_user(&uevent_param,
1012 (struct lttng_kernel_event __user *) arg,
1013 sizeof(uevent_param)))
1014 return -EFAULT;
1015 return lttng_abi_create_event(file, &uevent_param);
1016 }
1017 case LTTNG_KERNEL_OLD_CONTEXT:
1018 {
1019 struct lttng_kernel_context *ucontext_param;
1020 struct lttng_kernel_old_context *old_ucontext_param;
1021 int ret;
1022
1023 ucontext_param = kmalloc(sizeof(struct lttng_kernel_context),
1024 GFP_KERNEL);
1025 if (!ucontext_param) {
1026 ret = -ENOMEM;
1027 goto old_ctx_end;
1028 }
1029 old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context),
1030 GFP_KERNEL);
1031 if (!old_ucontext_param) {
1032 ret = -ENOMEM;
1033 goto old_ctx_error_free_param;
1034 }
1035
1036 if (copy_from_user(old_ucontext_param,
1037 (struct lttng_kernel_old_context __user *) arg,
1038 sizeof(struct lttng_kernel_old_context))) {
1039 ret = -EFAULT;
1040 goto old_ctx_error_free_old_param;
1041 }
1042 ucontext_param->ctx = old_ucontext_param->ctx;
1043 memcpy(ucontext_param->padding, old_ucontext_param->padding,
1044 sizeof(ucontext_param->padding));
1045 /* only type that uses the union */
1046 if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) {
1047 ucontext_param->u.perf_counter.type =
1048 old_ucontext_param->u.perf_counter.type;
1049 ucontext_param->u.perf_counter.config =
1050 old_ucontext_param->u.perf_counter.config;
1051 memcpy(ucontext_param->u.perf_counter.name,
1052 old_ucontext_param->u.perf_counter.name,
1053 sizeof(ucontext_param->u.perf_counter.name));
1054 }
1055
1056 ret = lttng_abi_add_context(file,
1057 ucontext_param,
1058 &channel->ctx, channel->session);
1059
1060 old_ctx_error_free_old_param:
1061 kfree(old_ucontext_param);
1062 old_ctx_error_free_param:
1063 kfree(ucontext_param);
1064 old_ctx_end:
1065 return ret;
1066 }
1067 case LTTNG_KERNEL_CONTEXT:
1068 {
1069 struct lttng_kernel_context ucontext_param;
1070
1071 if (copy_from_user(&ucontext_param,
1072 (struct lttng_kernel_context __user *) arg,
1073 sizeof(ucontext_param)))
1074 return -EFAULT;
1075 return lttng_abi_add_context(file,
1076 &ucontext_param,
1077 &channel->ctx, channel->session);
1078 }
1079 case LTTNG_KERNEL_OLD_ENABLE:
1080 case LTTNG_KERNEL_ENABLE:
1081 return lttng_channel_enable(channel);
1082 case LTTNG_KERNEL_OLD_DISABLE:
1083 case LTTNG_KERNEL_DISABLE:
1084 return lttng_channel_disable(channel);
1085 default:
1086 return -ENOIOCTLCMD;
1087 }
1088
1089 }
1090
1091 /**
1092 * lttng_metadata_ioctl - lttng syscall through ioctl
1093 *
1094 * @file: the file
1095 * @cmd: the command
1096 * @arg: command arg
1097 *
1098 * This ioctl implements lttng commands:
1099 * LTTNG_KERNEL_STREAM
1100 * Returns an event stream file descriptor or failure.
1101 *
1102 * Channel and event file descriptors also hold a reference on the session.
1103 */
1104 static
1105 long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1106 {
1107 switch (cmd) {
1108 case LTTNG_KERNEL_OLD_STREAM:
1109 case LTTNG_KERNEL_STREAM:
1110 return lttng_abi_open_metadata_stream(file);
1111 default:
1112 return -ENOIOCTLCMD;
1113 }
1114 }
1115
1116 /**
1117 * lttng_channel_poll - lttng stream addition/removal monitoring
1118 *
1119 * @file: the file
1120 * @wait: poll table
1121 */
1122 unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
1123 {
1124 struct lttng_channel *channel = file->private_data;
1125 unsigned int mask = 0;
1126
1127 if (file->f_mode & FMODE_READ) {
1128 poll_wait_set_exclusive(wait);
1129 poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan),
1130 wait);
1131
1132 if (channel->ops->is_disabled(channel->chan))
1133 return POLLERR;
1134 if (channel->ops->is_finalized(channel->chan))
1135 return POLLHUP;
1136 if (channel->ops->buffer_has_read_closed_stream(channel->chan))
1137 return POLLIN | POLLRDNORM;
1138 return 0;
1139 }
1140 return mask;
1141
1142 }
1143
1144 static
1145 int lttng_channel_release(struct inode *inode, struct file *file)
1146 {
1147 struct lttng_channel *channel = file->private_data;
1148
1149 if (channel)
1150 fput(channel->session->file);
1151 return 0;
1152 }
1153
1154 static
1155 int lttng_metadata_channel_release(struct inode *inode, struct file *file)
1156 {
1157 struct lttng_channel *channel = file->private_data;
1158
1159 if (channel) {
1160 lttng_metadata_channel_destroy(channel);
1161 fput(channel->session->file);
1162 }
1163
1164 return 0;
1165 }
1166
1167 static const struct file_operations lttng_channel_fops = {
1168 .owner = THIS_MODULE,
1169 .release = lttng_channel_release,
1170 .poll = lttng_channel_poll,
1171 .unlocked_ioctl = lttng_channel_ioctl,
1172 #ifdef CONFIG_COMPAT
1173 .compat_ioctl = lttng_channel_ioctl,
1174 #endif
1175 };
1176
1177 static const struct file_operations lttng_metadata_fops = {
1178 .owner = THIS_MODULE,
1179 .release = lttng_metadata_channel_release,
1180 .unlocked_ioctl = lttng_metadata_ioctl,
1181 #ifdef CONFIG_COMPAT
1182 .compat_ioctl = lttng_metadata_ioctl,
1183 #endif
1184 };
1185
1186 /**
1187 * lttng_event_ioctl - lttng syscall through ioctl
1188 *
1189 * @file: the file
1190 * @cmd: the command
1191 * @arg: command arg
1192 *
1193 * This ioctl implements lttng commands:
1194 * LTTNG_KERNEL_CONTEXT
1195 * Prepend a context field to each record of this event
1196 * LTTNG_KERNEL_ENABLE
1197 * Enable recording for this event (weak enable)
1198 * LTTNG_KERNEL_DISABLE
1199 * Disable recording for this event (strong disable)
1200 */
1201 static
1202 long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1203 {
1204 struct lttng_event *event = file->private_data;
1205
1206 switch (cmd) {
1207 case LTTNG_KERNEL_OLD_CONTEXT:
1208 {
1209 struct lttng_kernel_context *ucontext_param;
1210 struct lttng_kernel_old_context *old_ucontext_param;
1211 int ret;
1212
1213 ucontext_param = kmalloc(sizeof(struct lttng_kernel_context),
1214 GFP_KERNEL);
1215 if (!ucontext_param) {
1216 ret = -ENOMEM;
1217 goto old_ctx_end;
1218 }
1219 old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context),
1220 GFP_KERNEL);
1221 if (!old_ucontext_param) {
1222 ret = -ENOMEM;
1223 goto old_ctx_error_free_param;
1224 }
1225
1226 if (copy_from_user(old_ucontext_param,
1227 (struct lttng_kernel_old_context __user *) arg,
1228 sizeof(struct lttng_kernel_old_context))) {
1229 ret = -EFAULT;
1230 goto old_ctx_error_free_old_param;
1231 }
1232 ucontext_param->ctx = old_ucontext_param->ctx;
1233 memcpy(ucontext_param->padding, old_ucontext_param->padding,
1234 sizeof(ucontext_param->padding));
1235 /* only type that uses the union */
1236 if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) {
1237 ucontext_param->u.perf_counter.type =
1238 old_ucontext_param->u.perf_counter.type;
1239 ucontext_param->u.perf_counter.config =
1240 old_ucontext_param->u.perf_counter.config;
1241 memcpy(ucontext_param->u.perf_counter.name,
1242 old_ucontext_param->u.perf_counter.name,
1243 sizeof(ucontext_param->u.perf_counter.name));
1244 }
1245
1246 ret = lttng_abi_add_context(file,
1247 ucontext_param,
1248 &event->ctx, event->chan->session);
1249
1250 old_ctx_error_free_old_param:
1251 kfree(old_ucontext_param);
1252 old_ctx_error_free_param:
1253 kfree(ucontext_param);
1254 old_ctx_end:
1255 return ret;
1256 }
1257 case LTTNG_KERNEL_CONTEXT:
1258 {
1259 struct lttng_kernel_context ucontext_param;
1260
1261 if (copy_from_user(&ucontext_param,
1262 (struct lttng_kernel_context __user *) arg,
1263 sizeof(ucontext_param)))
1264 return -EFAULT;
1265 return lttng_abi_add_context(file,
1266 &ucontext_param,
1267 &event->ctx, event->chan->session);
1268 }
1269 case LTTNG_KERNEL_OLD_ENABLE:
1270 case LTTNG_KERNEL_ENABLE:
1271 return lttng_event_enable(event);
1272 case LTTNG_KERNEL_OLD_DISABLE:
1273 case LTTNG_KERNEL_DISABLE:
1274 return lttng_event_disable(event);
1275 default:
1276 return -ENOIOCTLCMD;
1277 }
1278 }
1279
1280 static
1281 int lttng_event_release(struct inode *inode, struct file *file)
1282 {
1283 struct lttng_event *event = file->private_data;
1284
1285 if (event)
1286 fput(event->chan->file);
1287 return 0;
1288 }
1289
1290 /* TODO: filter control ioctl */
1291 static const struct file_operations lttng_event_fops = {
1292 .owner = THIS_MODULE,
1293 .release = lttng_event_release,
1294 .unlocked_ioctl = lttng_event_ioctl,
1295 #ifdef CONFIG_COMPAT
1296 .compat_ioctl = lttng_event_ioctl,
1297 #endif
1298 };
1299
1300 int __init lttng_abi_init(void)
1301 {
1302 int ret = 0;
1303
1304 wrapper_vmalloc_sync_all();
1305 lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL,
1306 &lttng_fops, NULL);
1307
1308 if (!lttng_proc_dentry) {
1309 printk(KERN_ERR "Error creating LTTng control file\n");
1310 ret = -ENOMEM;
1311 goto error;
1312 }
1313 error:
1314 return ret;
1315 }
1316
1317 void __exit lttng_abi_exit(void)
1318 {
1319 if (lttng_proc_dentry)
1320 remove_proc_entry("lttng", NULL);
1321 }
This page took 0.057703 seconds and 5 git commands to generate.