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