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