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,
423 1, INT_MAX) == INT_MAX) {
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,
1076 1, INT_MAX) == INT_MAX) {
1077 ret = -EOVERFLOW;
1078 goto refcount_error;
1079 }
1080 if (event_param->instrumentation == LTTNG_KERNEL_TRACEPOINT
1081 || event_param->instrumentation == LTTNG_KERNEL_SYSCALL) {
1082 struct lttng_enabler *enabler;
1083
1084 if (strutils_is_star_glob_pattern(event_param->name)) {
1085 /*
1086 * If the event name is a star globbing pattern,
1087 * we create the special star globbing enabler.
1088 */
1089 enabler = lttng_enabler_create(LTTNG_ENABLER_STAR_GLOB,
1090 event_param, channel);
1091 } else {
1092 enabler = lttng_enabler_create(LTTNG_ENABLER_NAME,
1093 event_param, channel);
1094 }
1095 priv = enabler;
1096 } else {
1097 struct lttng_event *event;
1098
1099 /*
1100 * We tolerate no failure path after event creation. It
1101 * will stay invariant for the rest of the session.
1102 */
1103 event = lttng_event_create(channel, event_param,
1104 NULL, NULL,
1105 event_param->instrumentation);
1106 WARN_ON_ONCE(!event);
1107 if (IS_ERR(event)) {
1108 ret = PTR_ERR(event);
1109 goto event_error;
1110 }
1111 priv = event;
1112 }
1113 event_file->private_data = priv;
1114 fd_install(event_fd, event_file);
1115 return event_fd;
1116
1117 event_error:
1118 atomic_long_dec(&channel_file->f_count);
1119 refcount_error:
1120 fput(event_file);
1121 file_error:
1122 put_unused_fd(event_fd);
1123 fd_error:
1124 return ret;
1125 }
1126
1127 /**
1128 * lttng_channel_ioctl - lttng syscall through ioctl
1129 *
1130 * @file: the file
1131 * @cmd: the command
1132 * @arg: command arg
1133 *
1134 * This ioctl implements lttng commands:
1135 * LTTNG_KERNEL_STREAM
1136 * Returns an event stream file descriptor or failure.
1137 * (typically, one event stream records events from one CPU)
1138 * LTTNG_KERNEL_EVENT
1139 * Returns an event file descriptor or failure.
1140 * LTTNG_KERNEL_CONTEXT
1141 * Prepend a context field to each event in the channel
1142 * LTTNG_KERNEL_ENABLE
1143 * Enable recording for events in this channel (weak enable)
1144 * LTTNG_KERNEL_DISABLE
1145 * Disable recording for events in this channel (strong disable)
1146 *
1147 * Channel and event file descriptors also hold a reference on the session.
1148 */
1149 static
1150 long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1151 {
1152 struct lttng_channel *channel = file->private_data;
1153
1154 switch (cmd) {
1155 case LTTNG_KERNEL_OLD_STREAM:
1156 case LTTNG_KERNEL_STREAM:
1157 return lttng_abi_open_stream(file);
1158 case LTTNG_KERNEL_OLD_EVENT:
1159 {
1160 struct lttng_kernel_event *uevent_param;
1161 struct lttng_kernel_old_event *old_uevent_param;
1162 int ret;
1163
1164 uevent_param = kmalloc(sizeof(struct lttng_kernel_event),
1165 GFP_KERNEL);
1166 if (!uevent_param) {
1167 ret = -ENOMEM;
1168 goto old_event_end;
1169 }
1170 old_uevent_param = kmalloc(
1171 sizeof(struct lttng_kernel_old_event),
1172 GFP_KERNEL);
1173 if (!old_uevent_param) {
1174 ret = -ENOMEM;
1175 goto old_event_error_free_param;
1176 }
1177 if (copy_from_user(old_uevent_param,
1178 (struct lttng_kernel_old_event __user *) arg,
1179 sizeof(struct lttng_kernel_old_event))) {
1180 ret = -EFAULT;
1181 goto old_event_error_free_old_param;
1182 }
1183
1184 memcpy(uevent_param->name, old_uevent_param->name,
1185 sizeof(uevent_param->name));
1186 uevent_param->instrumentation =
1187 old_uevent_param->instrumentation;
1188
1189 switch (old_uevent_param->instrumentation) {
1190 case LTTNG_KERNEL_KPROBE:
1191 uevent_param->u.kprobe.addr =
1192 old_uevent_param->u.kprobe.addr;
1193 uevent_param->u.kprobe.offset =
1194 old_uevent_param->u.kprobe.offset;
1195 memcpy(uevent_param->u.kprobe.symbol_name,
1196 old_uevent_param->u.kprobe.symbol_name,
1197 sizeof(uevent_param->u.kprobe.symbol_name));
1198 break;
1199 case LTTNG_KERNEL_KRETPROBE:
1200 uevent_param->u.kretprobe.addr =
1201 old_uevent_param->u.kretprobe.addr;
1202 uevent_param->u.kretprobe.offset =
1203 old_uevent_param->u.kretprobe.offset;
1204 memcpy(uevent_param->u.kretprobe.symbol_name,
1205 old_uevent_param->u.kretprobe.symbol_name,
1206 sizeof(uevent_param->u.kretprobe.symbol_name));
1207 break;
1208 case LTTNG_KERNEL_FUNCTION:
1209 memcpy(uevent_param->u.ftrace.symbol_name,
1210 old_uevent_param->u.ftrace.symbol_name,
1211 sizeof(uevent_param->u.ftrace.symbol_name));
1212 break;
1213 default:
1214 break;
1215 }
1216 ret = lttng_abi_create_event(file, uevent_param);
1217
1218 old_event_error_free_old_param:
1219 kfree(old_uevent_param);
1220 old_event_error_free_param:
1221 kfree(uevent_param);
1222 old_event_end:
1223 return ret;
1224 }
1225 case LTTNG_KERNEL_EVENT:
1226 {
1227 struct lttng_kernel_event uevent_param;
1228
1229 if (copy_from_user(&uevent_param,
1230 (struct lttng_kernel_event __user *) arg,
1231 sizeof(uevent_param)))
1232 return -EFAULT;
1233 return lttng_abi_create_event(file, &uevent_param);
1234 }
1235 case LTTNG_KERNEL_OLD_CONTEXT:
1236 {
1237 struct lttng_kernel_context *ucontext_param;
1238 struct lttng_kernel_old_context *old_ucontext_param;
1239 int ret;
1240
1241 ucontext_param = kmalloc(sizeof(struct lttng_kernel_context),
1242 GFP_KERNEL);
1243 if (!ucontext_param) {
1244 ret = -ENOMEM;
1245 goto old_ctx_end;
1246 }
1247 old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context),
1248 GFP_KERNEL);
1249 if (!old_ucontext_param) {
1250 ret = -ENOMEM;
1251 goto old_ctx_error_free_param;
1252 }
1253
1254 if (copy_from_user(old_ucontext_param,
1255 (struct lttng_kernel_old_context __user *) arg,
1256 sizeof(struct lttng_kernel_old_context))) {
1257 ret = -EFAULT;
1258 goto old_ctx_error_free_old_param;
1259 }
1260 ucontext_param->ctx = old_ucontext_param->ctx;
1261 memcpy(ucontext_param->padding, old_ucontext_param->padding,
1262 sizeof(ucontext_param->padding));
1263 /* only type that uses the union */
1264 if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) {
1265 ucontext_param->u.perf_counter.type =
1266 old_ucontext_param->u.perf_counter.type;
1267 ucontext_param->u.perf_counter.config =
1268 old_ucontext_param->u.perf_counter.config;
1269 memcpy(ucontext_param->u.perf_counter.name,
1270 old_ucontext_param->u.perf_counter.name,
1271 sizeof(ucontext_param->u.perf_counter.name));
1272 }
1273
1274 ret = lttng_abi_add_context(file,
1275 ucontext_param,
1276 &channel->ctx, channel->session);
1277
1278 old_ctx_error_free_old_param:
1279 kfree(old_ucontext_param);
1280 old_ctx_error_free_param:
1281 kfree(ucontext_param);
1282 old_ctx_end:
1283 return ret;
1284 }
1285 case LTTNG_KERNEL_CONTEXT:
1286 {
1287 struct lttng_kernel_context ucontext_param;
1288
1289 if (copy_from_user(&ucontext_param,
1290 (struct lttng_kernel_context __user *) arg,
1291 sizeof(ucontext_param)))
1292 return -EFAULT;
1293 return lttng_abi_add_context(file,
1294 &ucontext_param,
1295 &channel->ctx, channel->session);
1296 }
1297 case LTTNG_KERNEL_OLD_ENABLE:
1298 case LTTNG_KERNEL_ENABLE:
1299 return lttng_channel_enable(channel);
1300 case LTTNG_KERNEL_OLD_DISABLE:
1301 case LTTNG_KERNEL_DISABLE:
1302 return lttng_channel_disable(channel);
1303 case LTTNG_KERNEL_SYSCALL_MASK:
1304 return lttng_channel_syscall_mask(channel,
1305 (struct lttng_kernel_syscall_mask __user *) arg);
1306 default:
1307 return -ENOIOCTLCMD;
1308 }
1309
1310 }
1311
1312 /**
1313 * lttng_metadata_ioctl - lttng syscall through ioctl
1314 *
1315 * @file: the file
1316 * @cmd: the command
1317 * @arg: command arg
1318 *
1319 * This ioctl implements lttng commands:
1320 * LTTNG_KERNEL_STREAM
1321 * Returns an event stream file descriptor or failure.
1322 *
1323 * Channel and event file descriptors also hold a reference on the session.
1324 */
1325 static
1326 long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1327 {
1328 switch (cmd) {
1329 case LTTNG_KERNEL_OLD_STREAM:
1330 case LTTNG_KERNEL_STREAM:
1331 return lttng_abi_open_metadata_stream(file);
1332 default:
1333 return -ENOIOCTLCMD;
1334 }
1335 }
1336
1337 /**
1338 * lttng_channel_poll - lttng stream addition/removal monitoring
1339 *
1340 * @file: the file
1341 * @wait: poll table
1342 */
1343 unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
1344 {
1345 struct lttng_channel *channel = file->private_data;
1346 unsigned int mask = 0;
1347
1348 if (file->f_mode & FMODE_READ) {
1349 poll_wait_set_exclusive(wait);
1350 poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan),
1351 wait);
1352
1353 if (channel->ops->is_disabled(channel->chan))
1354 return POLLERR;
1355 if (channel->ops->is_finalized(channel->chan))
1356 return POLLHUP;
1357 if (channel->ops->buffer_has_read_closed_stream(channel->chan))
1358 return POLLIN | POLLRDNORM;
1359 return 0;
1360 }
1361 return mask;
1362
1363 }
1364
1365 static
1366 int lttng_channel_release(struct inode *inode, struct file *file)
1367 {
1368 struct lttng_channel *channel = file->private_data;
1369
1370 if (channel)
1371 fput(channel->session->file);
1372 return 0;
1373 }
1374
1375 static
1376 int lttng_metadata_channel_release(struct inode *inode, struct file *file)
1377 {
1378 struct lttng_channel *channel = file->private_data;
1379
1380 if (channel) {
1381 fput(channel->session->file);
1382 lttng_metadata_channel_destroy(channel);
1383 }
1384
1385 return 0;
1386 }
1387
1388 static const struct file_operations lttng_channel_fops = {
1389 .owner = THIS_MODULE,
1390 .release = lttng_channel_release,
1391 .poll = lttng_channel_poll,
1392 .unlocked_ioctl = lttng_channel_ioctl,
1393 #ifdef CONFIG_COMPAT
1394 .compat_ioctl = lttng_channel_ioctl,
1395 #endif
1396 };
1397
1398 static const struct file_operations lttng_metadata_fops = {
1399 .owner = THIS_MODULE,
1400 .release = lttng_metadata_channel_release,
1401 .unlocked_ioctl = lttng_metadata_ioctl,
1402 #ifdef CONFIG_COMPAT
1403 .compat_ioctl = lttng_metadata_ioctl,
1404 #endif
1405 };
1406
1407 /**
1408 * lttng_event_ioctl - lttng syscall through ioctl
1409 *
1410 * @file: the file
1411 * @cmd: the command
1412 * @arg: command arg
1413 *
1414 * This ioctl implements lttng commands:
1415 * LTTNG_KERNEL_CONTEXT
1416 * Prepend a context field to each record of this event
1417 * LTTNG_KERNEL_ENABLE
1418 * Enable recording for this event (weak enable)
1419 * LTTNG_KERNEL_DISABLE
1420 * Disable recording for this event (strong disable)
1421 */
1422 static
1423 long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1424 {
1425 struct lttng_event *event;
1426 struct lttng_enabler *enabler;
1427 enum lttng_event_type *evtype = file->private_data;
1428
1429 switch (cmd) {
1430 case LTTNG_KERNEL_OLD_CONTEXT:
1431 {
1432 /* Not implemented */
1433 return -ENOSYS;
1434 }
1435 case LTTNG_KERNEL_CONTEXT:
1436 {
1437 /* Not implemented */
1438 return -ENOSYS;
1439 }
1440 case LTTNG_KERNEL_OLD_ENABLE:
1441 case LTTNG_KERNEL_ENABLE:
1442 switch (*evtype) {
1443 case LTTNG_TYPE_EVENT:
1444 event = file->private_data;
1445 return lttng_event_enable(event);
1446 case LTTNG_TYPE_ENABLER:
1447 enabler = file->private_data;
1448 return lttng_enabler_enable(enabler);
1449 default:
1450 WARN_ON_ONCE(1);
1451 return -ENOSYS;
1452 }
1453 case LTTNG_KERNEL_OLD_DISABLE:
1454 case LTTNG_KERNEL_DISABLE:
1455 switch (*evtype) {
1456 case LTTNG_TYPE_EVENT:
1457 event = file->private_data;
1458 return lttng_event_disable(event);
1459 case LTTNG_TYPE_ENABLER:
1460 enabler = file->private_data;
1461 return lttng_enabler_disable(enabler);
1462 default:
1463 WARN_ON_ONCE(1);
1464 return -ENOSYS;
1465 }
1466 case LTTNG_KERNEL_FILTER:
1467 switch (*evtype) {
1468 case LTTNG_TYPE_EVENT:
1469 return -EINVAL;
1470 case LTTNG_TYPE_ENABLER:
1471 {
1472 enabler = file->private_data;
1473 return lttng_enabler_attach_bytecode(enabler,
1474 (struct lttng_kernel_filter_bytecode __user *) arg);
1475 }
1476
1477 }
1478 default:
1479 return -ENOIOCTLCMD;
1480 }
1481 }
1482
1483 static
1484 int lttng_event_release(struct inode *inode, struct file *file)
1485 {
1486 struct lttng_event *event;
1487 struct lttng_enabler *enabler;
1488 enum lttng_event_type *evtype = file->private_data;
1489
1490 if (!evtype)
1491 return 0;
1492
1493 switch (*evtype) {
1494 case LTTNG_TYPE_EVENT:
1495 event = file->private_data;
1496 if (event)
1497 fput(event->chan->file);
1498 break;
1499 case LTTNG_TYPE_ENABLER:
1500 enabler = file->private_data;
1501 if (enabler)
1502 fput(enabler->chan->file);
1503 break;
1504 default:
1505 WARN_ON_ONCE(1);
1506 break;
1507 }
1508
1509 return 0;
1510 }
1511
1512 /* TODO: filter control ioctl */
1513 static const struct file_operations lttng_event_fops = {
1514 .owner = THIS_MODULE,
1515 .release = lttng_event_release,
1516 .unlocked_ioctl = lttng_event_ioctl,
1517 #ifdef CONFIG_COMPAT
1518 .compat_ioctl = lttng_event_ioctl,
1519 #endif
1520 };
1521
1522 static int put_u64(uint64_t val, unsigned long arg)
1523 {
1524 return put_user(val, (uint64_t __user *) arg);
1525 }
1526
1527 static long lttng_stream_ring_buffer_ioctl(struct file *filp,
1528 unsigned int cmd, unsigned long arg)
1529 {
1530 struct lib_ring_buffer *buf = filp->private_data;
1531 struct channel *chan = buf->backend.chan;
1532 const struct lib_ring_buffer_config *config = &chan->backend.config;
1533 const struct lttng_channel_ops *ops = chan->backend.priv_ops;
1534 int ret;
1535
1536 if (atomic_read(&chan->record_disabled))
1537 return -EIO;
1538
1539 switch (cmd) {
1540 case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN:
1541 {
1542 uint64_t ts;
1543
1544 ret = ops->timestamp_begin(config, buf, &ts);
1545 if (ret < 0)
1546 goto error;
1547 return put_u64(ts, arg);
1548 }
1549 case LTTNG_RING_BUFFER_GET_TIMESTAMP_END:
1550 {
1551 uint64_t ts;
1552
1553 ret = ops->timestamp_end(config, buf, &ts);
1554 if (ret < 0)
1555 goto error;
1556 return put_u64(ts, arg);
1557 }
1558 case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED:
1559 {
1560 uint64_t ed;
1561
1562 ret = ops->events_discarded(config, buf, &ed);
1563 if (ret < 0)
1564 goto error;
1565 return put_u64(ed, arg);
1566 }
1567 case LTTNG_RING_BUFFER_GET_CONTENT_SIZE:
1568 {
1569 uint64_t cs;
1570
1571 ret = ops->content_size(config, buf, &cs);
1572 if (ret < 0)
1573 goto error;
1574 return put_u64(cs, arg);
1575 }
1576 case LTTNG_RING_BUFFER_GET_PACKET_SIZE:
1577 {
1578 uint64_t ps;
1579
1580 ret = ops->packet_size(config, buf, &ps);
1581 if (ret < 0)
1582 goto error;
1583 return put_u64(ps, arg);
1584 }
1585 case LTTNG_RING_BUFFER_GET_STREAM_ID:
1586 {
1587 uint64_t si;
1588
1589 ret = ops->stream_id(config, buf, &si);
1590 if (ret < 0)
1591 goto error;
1592 return put_u64(si, arg);
1593 }
1594 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP:
1595 {
1596 uint64_t ts;
1597
1598 ret = ops->current_timestamp(config, buf, &ts);
1599 if (ret < 0)
1600 goto error;
1601 return put_u64(ts, arg);
1602 }
1603 case LTTNG_RING_BUFFER_GET_SEQ_NUM:
1604 {
1605 uint64_t seq;
1606
1607 ret = ops->sequence_number(config, buf, &seq);
1608 if (ret < 0)
1609 goto error;
1610 return put_u64(seq, arg);
1611 }
1612 case LTTNG_RING_BUFFER_INSTANCE_ID:
1613 {
1614 uint64_t id;
1615
1616 ret = ops->instance_id(config, buf, &id);
1617 if (ret < 0)
1618 goto error;
1619 return put_u64(id, arg);
1620 }
1621 default:
1622 return lib_ring_buffer_file_operations.unlocked_ioctl(filp,
1623 cmd, arg);
1624 }
1625
1626 error:
1627 return -ENOSYS;
1628 }
1629
1630 #ifdef CONFIG_COMPAT
1631 static long lttng_stream_ring_buffer_compat_ioctl(struct file *filp,
1632 unsigned int cmd, unsigned long arg)
1633 {
1634 struct lib_ring_buffer *buf = filp->private_data;
1635 struct channel *chan = buf->backend.chan;
1636 const struct lib_ring_buffer_config *config = &chan->backend.config;
1637 const struct lttng_channel_ops *ops = chan->backend.priv_ops;
1638 int ret;
1639
1640 if (atomic_read(&chan->record_disabled))
1641 return -EIO;
1642
1643 switch (cmd) {
1644 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN:
1645 {
1646 uint64_t ts;
1647
1648 ret = ops->timestamp_begin(config, buf, &ts);
1649 if (ret < 0)
1650 goto error;
1651 return put_u64(ts, arg);
1652 }
1653 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END:
1654 {
1655 uint64_t ts;
1656
1657 ret = ops->timestamp_end(config, buf, &ts);
1658 if (ret < 0)
1659 goto error;
1660 return put_u64(ts, arg);
1661 }
1662 case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED:
1663 {
1664 uint64_t ed;
1665
1666 ret = ops->events_discarded(config, buf, &ed);
1667 if (ret < 0)
1668 goto error;
1669 return put_u64(ed, arg);
1670 }
1671 case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE:
1672 {
1673 uint64_t cs;
1674
1675 ret = ops->content_size(config, buf, &cs);
1676 if (ret < 0)
1677 goto error;
1678 return put_u64(cs, arg);
1679 }
1680 case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE:
1681 {
1682 uint64_t ps;
1683
1684 ret = ops->packet_size(config, buf, &ps);
1685 if (ret < 0)
1686 goto error;
1687 return put_u64(ps, arg);
1688 }
1689 case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID:
1690 {
1691 uint64_t si;
1692
1693 ret = ops->stream_id(config, buf, &si);
1694 if (ret < 0)
1695 goto error;
1696 return put_u64(si, arg);
1697 }
1698 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP:
1699 {
1700 uint64_t ts;
1701
1702 ret = ops->current_timestamp(config, buf, &ts);
1703 if (ret < 0)
1704 goto error;
1705 return put_u64(ts, arg);
1706 }
1707 case LTTNG_RING_BUFFER_COMPAT_GET_SEQ_NUM:
1708 {
1709 uint64_t seq;
1710
1711 ret = ops->sequence_number(config, buf, &seq);
1712 if (ret < 0)
1713 goto error;
1714 return put_u64(seq, arg);
1715 }
1716 case LTTNG_RING_BUFFER_COMPAT_INSTANCE_ID:
1717 {
1718 uint64_t id;
1719
1720 ret = ops->instance_id(config, buf, &id);
1721 if (ret < 0)
1722 goto error;
1723 return put_u64(id, arg);
1724 }
1725 default:
1726 return lib_ring_buffer_file_operations.compat_ioctl(filp,
1727 cmd, arg);
1728 }
1729
1730 error:
1731 return -ENOSYS;
1732 }
1733 #endif /* CONFIG_COMPAT */
1734
1735 static void lttng_stream_override_ring_buffer_fops(void)
1736 {
1737 lttng_stream_ring_buffer_file_operations.owner = THIS_MODULE;
1738 lttng_stream_ring_buffer_file_operations.open =
1739 lib_ring_buffer_file_operations.open;
1740 lttng_stream_ring_buffer_file_operations.release =
1741 lib_ring_buffer_file_operations.release;
1742 lttng_stream_ring_buffer_file_operations.poll =
1743 lib_ring_buffer_file_operations.poll;
1744 lttng_stream_ring_buffer_file_operations.splice_read =
1745 lib_ring_buffer_file_operations.splice_read;
1746 lttng_stream_ring_buffer_file_operations.mmap =
1747 lib_ring_buffer_file_operations.mmap;
1748 lttng_stream_ring_buffer_file_operations.unlocked_ioctl =
1749 lttng_stream_ring_buffer_ioctl;
1750 lttng_stream_ring_buffer_file_operations.llseek =
1751 lib_ring_buffer_file_operations.llseek;
1752 #ifdef CONFIG_COMPAT
1753 lttng_stream_ring_buffer_file_operations.compat_ioctl =
1754 lttng_stream_ring_buffer_compat_ioctl;
1755 #endif
1756 }
1757
1758 int __init lttng_abi_init(void)
1759 {
1760 int ret = 0;
1761
1762 wrapper_vmalloc_sync_all();
1763 lttng_clock_ref();
1764
1765 ret = lttng_tp_mempool_init();
1766 if (ret) {
1767 goto error;
1768 }
1769
1770 lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL,
1771 &lttng_fops, NULL);
1772
1773 if (!lttng_proc_dentry) {
1774 printk(KERN_ERR "Error creating LTTng control file\n");
1775 ret = -ENOMEM;
1776 goto error;
1777 }
1778 lttng_stream_override_ring_buffer_fops();
1779 return 0;
1780
1781 error:
1782 lttng_tp_mempool_destroy();
1783 lttng_clock_unref();
1784 return ret;
1785 }
1786
1787 /* No __exit annotation because used by init error path too. */
1788 void lttng_abi_exit(void)
1789 {
1790 lttng_tp_mempool_destroy();
1791 lttng_clock_unref();
1792 if (lttng_proc_dentry)
1793 remove_proc_entry("lttng", NULL);
1794 }
This page took 0.102913 seconds and 4 git commands to generate.