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