| 1 | /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only) |
| 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 <ringbuffer/vfs.h> |
| 35 | #include <ringbuffer/backend.h> |
| 36 | #include <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 <ringbuffer/frontend_types.h> |
| 47 | #include <ringbuffer/iterator.h> |
| 48 | |
| 49 | /* |
| 50 | * This is LTTng's own personal way to create a system call as an external |
| 51 | * module. We use ioctl() on /proc/lttng. |
| 52 | */ |
| 53 | |
| 54 | static struct proc_dir_entry *lttng_proc_dentry; |
| 55 | |
| 56 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0)) |
| 57 | static const struct proc_ops lttng_proc_ops; |
| 58 | #else |
| 59 | static const struct file_operations lttng_proc_ops; |
| 60 | #endif |
| 61 | |
| 62 | static const struct file_operations lttng_session_fops; |
| 63 | static const struct file_operations lttng_event_notifier_group_fops; |
| 64 | static const struct file_operations lttng_channel_fops; |
| 65 | static const struct file_operations lttng_metadata_fops; |
| 66 | static const struct file_operations lttng_event_fops; |
| 67 | static struct file_operations lttng_stream_ring_buffer_file_operations; |
| 68 | |
| 69 | static int put_u64(uint64_t val, unsigned long arg); |
| 70 | static int put_u32(uint32_t val, unsigned long arg); |
| 71 | |
| 72 | static int validate_zeroed_padding(char *p, size_t len) |
| 73 | { |
| 74 | size_t i; |
| 75 | |
| 76 | for (i = 0; i < len; i++) { |
| 77 | if (p[i]) |
| 78 | return -1; |
| 79 | } |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * Teardown management: opened file descriptors keep a refcount on the module, |
| 85 | * so it can only exit when all file descriptors are closed. |
| 86 | */ |
| 87 | |
| 88 | static |
| 89 | int lttng_abi_create_session(void) |
| 90 | { |
| 91 | struct lttng_session *session; |
| 92 | struct file *session_file; |
| 93 | int session_fd, ret; |
| 94 | |
| 95 | session = lttng_session_create(); |
| 96 | if (!session) |
| 97 | return -ENOMEM; |
| 98 | session_fd = lttng_get_unused_fd(); |
| 99 | if (session_fd < 0) { |
| 100 | ret = session_fd; |
| 101 | goto fd_error; |
| 102 | } |
| 103 | session_file = anon_inode_getfile("[lttng_session]", |
| 104 | <tng_session_fops, |
| 105 | session, O_RDWR); |
| 106 | if (IS_ERR(session_file)) { |
| 107 | ret = PTR_ERR(session_file); |
| 108 | goto file_error; |
| 109 | } |
| 110 | session->file = session_file; |
| 111 | fd_install(session_fd, session_file); |
| 112 | return session_fd; |
| 113 | |
| 114 | file_error: |
| 115 | put_unused_fd(session_fd); |
| 116 | fd_error: |
| 117 | lttng_session_destroy(session); |
| 118 | return ret; |
| 119 | } |
| 120 | |
| 121 | void event_notifier_send_notification_work_wakeup(struct irq_work *entry) |
| 122 | { |
| 123 | struct lttng_event_notifier_group *event_notifier_group = |
| 124 | container_of(entry, struct lttng_event_notifier_group, |
| 125 | wakeup_pending); |
| 126 | wake_up_interruptible(&event_notifier_group->read_wait); |
| 127 | } |
| 128 | |
| 129 | static |
| 130 | int lttng_abi_create_event_notifier_group(void) |
| 131 | { |
| 132 | struct lttng_event_notifier_group *event_notifier_group; |
| 133 | struct file *event_notifier_group_file; |
| 134 | int event_notifier_group_fd, ret; |
| 135 | |
| 136 | event_notifier_group = lttng_event_notifier_group_create(); |
| 137 | if (!event_notifier_group) |
| 138 | return -ENOMEM; |
| 139 | |
| 140 | event_notifier_group_fd = lttng_get_unused_fd(); |
| 141 | if (event_notifier_group_fd < 0) { |
| 142 | ret = event_notifier_group_fd; |
| 143 | goto fd_error; |
| 144 | } |
| 145 | event_notifier_group_file = anon_inode_getfile("[lttng_event_notifier_group]", |
| 146 | <tng_event_notifier_group_fops, |
| 147 | event_notifier_group, O_RDWR); |
| 148 | if (IS_ERR(event_notifier_group_file)) { |
| 149 | ret = PTR_ERR(event_notifier_group_file); |
| 150 | goto file_error; |
| 151 | } |
| 152 | |
| 153 | event_notifier_group->file = event_notifier_group_file; |
| 154 | init_waitqueue_head(&event_notifier_group->read_wait); |
| 155 | init_irq_work(&event_notifier_group->wakeup_pending, |
| 156 | event_notifier_send_notification_work_wakeup); |
| 157 | fd_install(event_notifier_group_fd, event_notifier_group_file); |
| 158 | return event_notifier_group_fd; |
| 159 | |
| 160 | file_error: |
| 161 | put_unused_fd(event_notifier_group_fd); |
| 162 | fd_error: |
| 163 | lttng_event_notifier_group_destroy(event_notifier_group); |
| 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | static |
| 168 | int lttng_abi_tracepoint_list(void) |
| 169 | { |
| 170 | struct file *tracepoint_list_file; |
| 171 | int file_fd, ret; |
| 172 | |
| 173 | file_fd = lttng_get_unused_fd(); |
| 174 | if (file_fd < 0) { |
| 175 | ret = file_fd; |
| 176 | goto fd_error; |
| 177 | } |
| 178 | |
| 179 | tracepoint_list_file = anon_inode_getfile("[lttng_tracepoint_list]", |
| 180 | <tng_tracepoint_list_fops, |
| 181 | NULL, O_RDWR); |
| 182 | if (IS_ERR(tracepoint_list_file)) { |
| 183 | ret = PTR_ERR(tracepoint_list_file); |
| 184 | goto file_error; |
| 185 | } |
| 186 | ret = lttng_tracepoint_list_fops.open(NULL, tracepoint_list_file); |
| 187 | if (ret < 0) |
| 188 | goto open_error; |
| 189 | fd_install(file_fd, tracepoint_list_file); |
| 190 | return file_fd; |
| 191 | |
| 192 | open_error: |
| 193 | fput(tracepoint_list_file); |
| 194 | file_error: |
| 195 | put_unused_fd(file_fd); |
| 196 | fd_error: |
| 197 | return ret; |
| 198 | } |
| 199 | |
| 200 | #ifndef CONFIG_HAVE_SYSCALL_TRACEPOINTS |
| 201 | static inline |
| 202 | int lttng_abi_syscall_list(void) |
| 203 | { |
| 204 | return -ENOSYS; |
| 205 | } |
| 206 | #else |
| 207 | static |
| 208 | int lttng_abi_syscall_list(void) |
| 209 | { |
| 210 | struct file *syscall_list_file; |
| 211 | int file_fd, ret; |
| 212 | |
| 213 | file_fd = lttng_get_unused_fd(); |
| 214 | if (file_fd < 0) { |
| 215 | ret = file_fd; |
| 216 | goto fd_error; |
| 217 | } |
| 218 | |
| 219 | syscall_list_file = anon_inode_getfile("[lttng_syscall_list]", |
| 220 | <tng_syscall_list_fops, |
| 221 | NULL, O_RDWR); |
| 222 | if (IS_ERR(syscall_list_file)) { |
| 223 | ret = PTR_ERR(syscall_list_file); |
| 224 | goto file_error; |
| 225 | } |
| 226 | ret = lttng_syscall_list_fops.open(NULL, syscall_list_file); |
| 227 | if (ret < 0) |
| 228 | goto open_error; |
| 229 | fd_install(file_fd, syscall_list_file); |
| 230 | return file_fd; |
| 231 | |
| 232 | open_error: |
| 233 | fput(syscall_list_file); |
| 234 | file_error: |
| 235 | put_unused_fd(file_fd); |
| 236 | fd_error: |
| 237 | return ret; |
| 238 | } |
| 239 | #endif |
| 240 | |
| 241 | static |
| 242 | void lttng_abi_tracer_version(struct lttng_kernel_tracer_version *v) |
| 243 | { |
| 244 | v->major = LTTNG_MODULES_MAJOR_VERSION; |
| 245 | v->minor = LTTNG_MODULES_MINOR_VERSION; |
| 246 | v->patchlevel = LTTNG_MODULES_PATCHLEVEL_VERSION; |
| 247 | } |
| 248 | |
| 249 | static |
| 250 | void lttng_abi_tracer_abi_version(struct lttng_kernel_tracer_abi_version *v) |
| 251 | { |
| 252 | v->major = LTTNG_MODULES_ABI_MAJOR_VERSION; |
| 253 | v->minor = LTTNG_MODULES_ABI_MINOR_VERSION; |
| 254 | } |
| 255 | |
| 256 | static |
| 257 | long lttng_abi_add_context(struct file *file, |
| 258 | struct lttng_kernel_context *context_param, |
| 259 | struct lttng_ctx **ctx, struct lttng_session *session) |
| 260 | { |
| 261 | |
| 262 | if (session->been_active) |
| 263 | return -EPERM; |
| 264 | |
| 265 | switch (context_param->ctx) { |
| 266 | case LTTNG_KERNEL_CONTEXT_PID: |
| 267 | return lttng_add_pid_to_ctx(ctx); |
| 268 | case LTTNG_KERNEL_CONTEXT_PRIO: |
| 269 | return lttng_add_prio_to_ctx(ctx); |
| 270 | case LTTNG_KERNEL_CONTEXT_NICE: |
| 271 | return lttng_add_nice_to_ctx(ctx); |
| 272 | case LTTNG_KERNEL_CONTEXT_VPID: |
| 273 | return lttng_add_vpid_to_ctx(ctx); |
| 274 | case LTTNG_KERNEL_CONTEXT_TID: |
| 275 | return lttng_add_tid_to_ctx(ctx); |
| 276 | case LTTNG_KERNEL_CONTEXT_VTID: |
| 277 | return lttng_add_vtid_to_ctx(ctx); |
| 278 | case LTTNG_KERNEL_CONTEXT_PPID: |
| 279 | return lttng_add_ppid_to_ctx(ctx); |
| 280 | case LTTNG_KERNEL_CONTEXT_VPPID: |
| 281 | return lttng_add_vppid_to_ctx(ctx); |
| 282 | case LTTNG_KERNEL_CONTEXT_PERF_COUNTER: |
| 283 | context_param->u.perf_counter.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 284 | return lttng_add_perf_counter_to_ctx(context_param->u.perf_counter.type, |
| 285 | context_param->u.perf_counter.config, |
| 286 | context_param->u.perf_counter.name, |
| 287 | ctx); |
| 288 | case LTTNG_KERNEL_CONTEXT_PROCNAME: |
| 289 | return lttng_add_procname_to_ctx(ctx); |
| 290 | case LTTNG_KERNEL_CONTEXT_HOSTNAME: |
| 291 | return lttng_add_hostname_to_ctx(ctx); |
| 292 | case LTTNG_KERNEL_CONTEXT_CPU_ID: |
| 293 | return lttng_add_cpu_id_to_ctx(ctx); |
| 294 | case LTTNG_KERNEL_CONTEXT_INTERRUPTIBLE: |
| 295 | return lttng_add_interruptible_to_ctx(ctx); |
| 296 | case LTTNG_KERNEL_CONTEXT_NEED_RESCHEDULE: |
| 297 | return lttng_add_need_reschedule_to_ctx(ctx); |
| 298 | case LTTNG_KERNEL_CONTEXT_PREEMPTIBLE: |
| 299 | return lttng_add_preemptible_to_ctx(ctx); |
| 300 | case LTTNG_KERNEL_CONTEXT_MIGRATABLE: |
| 301 | return lttng_add_migratable_to_ctx(ctx); |
| 302 | case LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL: |
| 303 | case LTTNG_KERNEL_CONTEXT_CALLSTACK_USER: |
| 304 | return lttng_add_callstack_to_ctx(ctx, context_param->ctx); |
| 305 | case LTTNG_KERNEL_CONTEXT_CGROUP_NS: |
| 306 | return lttng_add_cgroup_ns_to_ctx(ctx); |
| 307 | case LTTNG_KERNEL_CONTEXT_IPC_NS: |
| 308 | return lttng_add_ipc_ns_to_ctx(ctx); |
| 309 | case LTTNG_KERNEL_CONTEXT_MNT_NS: |
| 310 | return lttng_add_mnt_ns_to_ctx(ctx); |
| 311 | case LTTNG_KERNEL_CONTEXT_NET_NS: |
| 312 | return lttng_add_net_ns_to_ctx(ctx); |
| 313 | case LTTNG_KERNEL_CONTEXT_PID_NS: |
| 314 | return lttng_add_pid_ns_to_ctx(ctx); |
| 315 | case LTTNG_KERNEL_CONTEXT_USER_NS: |
| 316 | return lttng_add_user_ns_to_ctx(ctx); |
| 317 | case LTTNG_KERNEL_CONTEXT_UTS_NS: |
| 318 | return lttng_add_uts_ns_to_ctx(ctx); |
| 319 | case LTTNG_KERNEL_CONTEXT_UID: |
| 320 | return lttng_add_uid_to_ctx(ctx); |
| 321 | case LTTNG_KERNEL_CONTEXT_EUID: |
| 322 | return lttng_add_euid_to_ctx(ctx); |
| 323 | case LTTNG_KERNEL_CONTEXT_SUID: |
| 324 | return lttng_add_suid_to_ctx(ctx); |
| 325 | case LTTNG_KERNEL_CONTEXT_GID: |
| 326 | return lttng_add_gid_to_ctx(ctx); |
| 327 | case LTTNG_KERNEL_CONTEXT_EGID: |
| 328 | return lttng_add_egid_to_ctx(ctx); |
| 329 | case LTTNG_KERNEL_CONTEXT_SGID: |
| 330 | return lttng_add_sgid_to_ctx(ctx); |
| 331 | case LTTNG_KERNEL_CONTEXT_VUID: |
| 332 | return lttng_add_vuid_to_ctx(ctx); |
| 333 | case LTTNG_KERNEL_CONTEXT_VEUID: |
| 334 | return lttng_add_veuid_to_ctx(ctx); |
| 335 | case LTTNG_KERNEL_CONTEXT_VSUID: |
| 336 | return lttng_add_vsuid_to_ctx(ctx); |
| 337 | case LTTNG_KERNEL_CONTEXT_VGID: |
| 338 | return lttng_add_vgid_to_ctx(ctx); |
| 339 | case LTTNG_KERNEL_CONTEXT_VEGID: |
| 340 | return lttng_add_vegid_to_ctx(ctx); |
| 341 | case LTTNG_KERNEL_CONTEXT_VSGID: |
| 342 | return lttng_add_vsgid_to_ctx(ctx); |
| 343 | case LTTNG_KERNEL_CONTEXT_TIME_NS: |
| 344 | return lttng_add_time_ns_to_ctx(ctx); |
| 345 | default: |
| 346 | return -EINVAL; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * lttng_ioctl - lttng syscall through ioctl |
| 352 | * |
| 353 | * @file: the file |
| 354 | * @cmd: the command |
| 355 | * @arg: command arg |
| 356 | * |
| 357 | * This ioctl implements lttng commands: |
| 358 | * LTTNG_KERNEL_SESSION |
| 359 | * Returns a LTTng trace session file descriptor |
| 360 | * LTTNG_KERNEL_TRACER_VERSION |
| 361 | * Returns the LTTng kernel tracer version |
| 362 | * LTTNG_KERNEL_TRACEPOINT_LIST |
| 363 | * Returns a file descriptor listing available tracepoints |
| 364 | * LTTNG_KERNEL_WAIT_QUIESCENT |
| 365 | * Returns after all previously running probes have completed |
| 366 | * LTTNG_KERNEL_TRACER_ABI_VERSION |
| 367 | * Returns the LTTng kernel tracer ABI version |
| 368 | * LTTNG_KERNEL_EVENT_NOTIFIER_GROUP_CREATE |
| 369 | * Returns a LTTng event notifier group file descriptor |
| 370 | * |
| 371 | * The returned session will be deleted when its file descriptor is closed. |
| 372 | */ |
| 373 | static |
| 374 | long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 375 | { |
| 376 | switch (cmd) { |
| 377 | case LTTNG_KERNEL_OLD_SESSION: |
| 378 | case LTTNG_KERNEL_SESSION: |
| 379 | return lttng_abi_create_session(); |
| 380 | case LTTNG_KERNEL_EVENT_NOTIFIER_GROUP_CREATE: |
| 381 | return lttng_abi_create_event_notifier_group(); |
| 382 | case LTTNG_KERNEL_OLD_TRACER_VERSION: |
| 383 | { |
| 384 | struct lttng_kernel_tracer_version v; |
| 385 | struct lttng_kernel_old_tracer_version oldv; |
| 386 | struct lttng_kernel_old_tracer_version *uversion = |
| 387 | (struct lttng_kernel_old_tracer_version __user *) arg; |
| 388 | |
| 389 | lttng_abi_tracer_version(&v); |
| 390 | oldv.major = v.major; |
| 391 | oldv.minor = v.minor; |
| 392 | oldv.patchlevel = v.patchlevel; |
| 393 | |
| 394 | if (copy_to_user(uversion, &oldv, sizeof(oldv))) |
| 395 | return -EFAULT; |
| 396 | return 0; |
| 397 | } |
| 398 | case LTTNG_KERNEL_TRACER_VERSION: |
| 399 | { |
| 400 | struct lttng_kernel_tracer_version version; |
| 401 | struct lttng_kernel_tracer_version *uversion = |
| 402 | (struct lttng_kernel_tracer_version __user *) arg; |
| 403 | |
| 404 | lttng_abi_tracer_version(&version); |
| 405 | |
| 406 | if (copy_to_user(uversion, &version, sizeof(version))) |
| 407 | return -EFAULT; |
| 408 | return 0; |
| 409 | } |
| 410 | case LTTNG_KERNEL_TRACER_ABI_VERSION: |
| 411 | { |
| 412 | struct lttng_kernel_tracer_abi_version version; |
| 413 | struct lttng_kernel_tracer_abi_version *uversion = |
| 414 | (struct lttng_kernel_tracer_abi_version __user *) arg; |
| 415 | |
| 416 | lttng_abi_tracer_abi_version(&version); |
| 417 | |
| 418 | if (copy_to_user(uversion, &version, sizeof(version))) |
| 419 | return -EFAULT; |
| 420 | return 0; |
| 421 | } |
| 422 | case LTTNG_KERNEL_OLD_TRACEPOINT_LIST: |
| 423 | case LTTNG_KERNEL_TRACEPOINT_LIST: |
| 424 | return lttng_abi_tracepoint_list(); |
| 425 | case LTTNG_KERNEL_SYSCALL_LIST: |
| 426 | return lttng_abi_syscall_list(); |
| 427 | case LTTNG_KERNEL_OLD_WAIT_QUIESCENT: |
| 428 | case LTTNG_KERNEL_WAIT_QUIESCENT: |
| 429 | synchronize_trace(); |
| 430 | return 0; |
| 431 | case LTTNG_KERNEL_OLD_CALIBRATE: |
| 432 | { |
| 433 | struct lttng_kernel_old_calibrate __user *ucalibrate = |
| 434 | (struct lttng_kernel_old_calibrate __user *) arg; |
| 435 | struct lttng_kernel_old_calibrate old_calibrate; |
| 436 | struct lttng_kernel_calibrate calibrate; |
| 437 | int ret; |
| 438 | |
| 439 | if (copy_from_user(&old_calibrate, ucalibrate, sizeof(old_calibrate))) |
| 440 | return -EFAULT; |
| 441 | calibrate.type = old_calibrate.type; |
| 442 | ret = lttng_calibrate(&calibrate); |
| 443 | if (copy_to_user(ucalibrate, &old_calibrate, sizeof(old_calibrate))) |
| 444 | return -EFAULT; |
| 445 | return ret; |
| 446 | } |
| 447 | case LTTNG_KERNEL_CALIBRATE: |
| 448 | { |
| 449 | struct lttng_kernel_calibrate __user *ucalibrate = |
| 450 | (struct lttng_kernel_calibrate __user *) arg; |
| 451 | struct lttng_kernel_calibrate calibrate; |
| 452 | int ret; |
| 453 | |
| 454 | if (copy_from_user(&calibrate, ucalibrate, sizeof(calibrate))) |
| 455 | return -EFAULT; |
| 456 | ret = lttng_calibrate(&calibrate); |
| 457 | if (copy_to_user(ucalibrate, &calibrate, sizeof(calibrate))) |
| 458 | return -EFAULT; |
| 459 | return ret; |
| 460 | } |
| 461 | default: |
| 462 | return -ENOIOCTLCMD; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0)) |
| 467 | static const struct proc_ops lttng_proc_ops = { |
| 468 | .proc_ioctl = lttng_ioctl, |
| 469 | #ifdef CONFIG_COMPAT |
| 470 | .proc_compat_ioctl = lttng_ioctl, |
| 471 | #endif /* CONFIG_COMPAT */ |
| 472 | }; |
| 473 | #else |
| 474 | static const struct file_operations lttng_proc_ops = { |
| 475 | .owner = THIS_MODULE, |
| 476 | .unlocked_ioctl = lttng_ioctl, |
| 477 | #ifdef CONFIG_COMPAT |
| 478 | .compat_ioctl = lttng_ioctl, |
| 479 | #endif /* CONFIG_COMPAT */ |
| 480 | }; |
| 481 | #endif |
| 482 | |
| 483 | static |
| 484 | int lttng_abi_create_channel(struct file *session_file, |
| 485 | struct lttng_kernel_channel *chan_param, |
| 486 | enum channel_type channel_type) |
| 487 | { |
| 488 | struct lttng_session *session = session_file->private_data; |
| 489 | const struct file_operations *fops = NULL; |
| 490 | const char *transport_name; |
| 491 | struct lttng_channel *chan; |
| 492 | struct file *chan_file; |
| 493 | int chan_fd; |
| 494 | int ret = 0; |
| 495 | |
| 496 | chan_fd = lttng_get_unused_fd(); |
| 497 | if (chan_fd < 0) { |
| 498 | ret = chan_fd; |
| 499 | goto fd_error; |
| 500 | } |
| 501 | switch (channel_type) { |
| 502 | case PER_CPU_CHANNEL: |
| 503 | fops = <tng_channel_fops; |
| 504 | break; |
| 505 | case METADATA_CHANNEL: |
| 506 | fops = <tng_metadata_fops; |
| 507 | break; |
| 508 | } |
| 509 | |
| 510 | chan_file = anon_inode_getfile("[lttng_channel]", |
| 511 | fops, |
| 512 | NULL, O_RDWR); |
| 513 | if (IS_ERR(chan_file)) { |
| 514 | ret = PTR_ERR(chan_file); |
| 515 | goto file_error; |
| 516 | } |
| 517 | switch (channel_type) { |
| 518 | case PER_CPU_CHANNEL: |
| 519 | if (chan_param->output == LTTNG_KERNEL_SPLICE) { |
| 520 | transport_name = chan_param->overwrite ? |
| 521 | "relay-overwrite" : "relay-discard"; |
| 522 | } else if (chan_param->output == LTTNG_KERNEL_MMAP) { |
| 523 | transport_name = chan_param->overwrite ? |
| 524 | "relay-overwrite-mmap" : "relay-discard-mmap"; |
| 525 | } else { |
| 526 | return -EINVAL; |
| 527 | } |
| 528 | break; |
| 529 | case METADATA_CHANNEL: |
| 530 | if (chan_param->output == LTTNG_KERNEL_SPLICE) |
| 531 | transport_name = "relay-metadata"; |
| 532 | else if (chan_param->output == LTTNG_KERNEL_MMAP) |
| 533 | transport_name = "relay-metadata-mmap"; |
| 534 | else |
| 535 | return -EINVAL; |
| 536 | break; |
| 537 | default: |
| 538 | transport_name = "<unknown>"; |
| 539 | break; |
| 540 | } |
| 541 | if (!atomic_long_add_unless(&session_file->f_count, 1, LONG_MAX)) { |
| 542 | ret = -EOVERFLOW; |
| 543 | goto refcount_error; |
| 544 | } |
| 545 | /* |
| 546 | * We tolerate no failure path after channel creation. It will stay |
| 547 | * invariant for the rest of the session. |
| 548 | */ |
| 549 | chan = lttng_channel_create(session, transport_name, NULL, |
| 550 | chan_param->subbuf_size, |
| 551 | chan_param->num_subbuf, |
| 552 | chan_param->switch_timer_interval, |
| 553 | chan_param->read_timer_interval, |
| 554 | channel_type); |
| 555 | if (!chan) { |
| 556 | ret = -EINVAL; |
| 557 | goto chan_error; |
| 558 | } |
| 559 | chan->file = chan_file; |
| 560 | chan_file->private_data = chan; |
| 561 | fd_install(chan_fd, chan_file); |
| 562 | |
| 563 | return chan_fd; |
| 564 | |
| 565 | chan_error: |
| 566 | atomic_long_dec(&session_file->f_count); |
| 567 | refcount_error: |
| 568 | fput(chan_file); |
| 569 | file_error: |
| 570 | put_unused_fd(chan_fd); |
| 571 | fd_error: |
| 572 | return ret; |
| 573 | } |
| 574 | |
| 575 | static |
| 576 | int lttng_abi_session_set_name(struct lttng_session *session, |
| 577 | struct lttng_kernel_session_name *name) |
| 578 | { |
| 579 | size_t len; |
| 580 | |
| 581 | len = strnlen(name->name, LTTNG_KERNEL_SESSION_NAME_LEN); |
| 582 | |
| 583 | if (len == LTTNG_KERNEL_SESSION_NAME_LEN) { |
| 584 | /* Name is too long/malformed */ |
| 585 | return -EINVAL; |
| 586 | } |
| 587 | |
| 588 | strcpy(session->name, name->name); |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | static |
| 593 | int lttng_abi_session_set_creation_time(struct lttng_session *session, |
| 594 | struct lttng_kernel_session_creation_time *time) |
| 595 | { |
| 596 | size_t len; |
| 597 | |
| 598 | len = strnlen(time->iso8601, LTTNG_KERNEL_SESSION_CREATION_TIME_ISO8601_LEN); |
| 599 | |
| 600 | if (len == LTTNG_KERNEL_SESSION_CREATION_TIME_ISO8601_LEN) { |
| 601 | /* Time is too long/malformed */ |
| 602 | return -EINVAL; |
| 603 | } |
| 604 | |
| 605 | strcpy(session->creation_time, time->iso8601); |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | static |
| 610 | int lttng_counter_release(struct inode *inode, struct file *file) |
| 611 | { |
| 612 | struct lttng_counter *counter = file->private_data; |
| 613 | |
| 614 | if (counter) { |
| 615 | /* |
| 616 | * Do not destroy the counter itself. Wait of the owner |
| 617 | * (event_notifier group) to be destroyed. |
| 618 | */ |
| 619 | fput(counter->owner); |
| 620 | } |
| 621 | |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | static |
| 626 | long lttng_counter_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 627 | { |
| 628 | struct lttng_counter *counter = file->private_data; |
| 629 | size_t indexes[LTTNG_KERNEL_COUNTER_DIMENSION_MAX] = { 0 }; |
| 630 | int i; |
| 631 | |
| 632 | switch (cmd) { |
| 633 | case LTTNG_KERNEL_COUNTER_READ: |
| 634 | { |
| 635 | struct lttng_kernel_counter_read local_counter_read; |
| 636 | struct lttng_kernel_counter_read __user *ucounter_read = |
| 637 | (struct lttng_kernel_counter_read __user *) arg; |
| 638 | bool overflow, underflow; |
| 639 | int64_t value; |
| 640 | int32_t cpu; |
| 641 | int ret; |
| 642 | |
| 643 | if (copy_from_user(&local_counter_read, ucounter_read, |
| 644 | sizeof(local_counter_read))) |
| 645 | return -EFAULT; |
| 646 | if (validate_zeroed_padding(local_counter_read.padding, |
| 647 | sizeof(local_counter_read.padding))) |
| 648 | return -EINVAL; |
| 649 | |
| 650 | /* Cast all indexes into size_t. */ |
| 651 | for (i = 0; i < local_counter_read.index.number_dimensions; i++) |
| 652 | indexes[i] = (size_t) local_counter_read.index.dimension_indexes[i]; |
| 653 | cpu = local_counter_read.cpu; |
| 654 | |
| 655 | ret = lttng_kernel_counter_read(counter, indexes, cpu, &value, |
| 656 | &overflow, &underflow); |
| 657 | if (ret) |
| 658 | return ret; |
| 659 | local_counter_read.value.value = value; |
| 660 | local_counter_read.value.overflow = overflow; |
| 661 | local_counter_read.value.underflow = underflow; |
| 662 | |
| 663 | if (copy_to_user(&ucounter_read->value, &local_counter_read.value, |
| 664 | sizeof(local_counter_read.value))) |
| 665 | return -EFAULT; |
| 666 | |
| 667 | return 0; |
| 668 | } |
| 669 | case LTTNG_KERNEL_COUNTER_AGGREGATE: |
| 670 | { |
| 671 | struct lttng_kernel_counter_aggregate local_counter_aggregate; |
| 672 | struct lttng_kernel_counter_aggregate __user *ucounter_aggregate = |
| 673 | (struct lttng_kernel_counter_aggregate __user *) arg; |
| 674 | bool overflow, underflow; |
| 675 | int64_t value; |
| 676 | int ret; |
| 677 | |
| 678 | if (copy_from_user(&local_counter_aggregate, ucounter_aggregate, |
| 679 | sizeof(local_counter_aggregate))) |
| 680 | return -EFAULT; |
| 681 | if (validate_zeroed_padding(local_counter_aggregate.padding, |
| 682 | sizeof(local_counter_aggregate.padding))) |
| 683 | return -EINVAL; |
| 684 | |
| 685 | /* Cast all indexes into size_t. */ |
| 686 | for (i = 0; i < local_counter_aggregate.index.number_dimensions; i++) |
| 687 | indexes[i] = (size_t) local_counter_aggregate.index.dimension_indexes[i]; |
| 688 | |
| 689 | ret = lttng_kernel_counter_aggregate(counter, indexes, &value, |
| 690 | &overflow, &underflow); |
| 691 | if (ret) |
| 692 | return ret; |
| 693 | local_counter_aggregate.value.value = value; |
| 694 | local_counter_aggregate.value.overflow = overflow; |
| 695 | local_counter_aggregate.value.underflow = underflow; |
| 696 | |
| 697 | if (copy_to_user(&ucounter_aggregate->value, &local_counter_aggregate.value, |
| 698 | sizeof(local_counter_aggregate.value))) |
| 699 | return -EFAULT; |
| 700 | |
| 701 | return 0; |
| 702 | } |
| 703 | case LTTNG_KERNEL_COUNTER_CLEAR: |
| 704 | { |
| 705 | struct lttng_kernel_counter_clear local_counter_clear; |
| 706 | struct lttng_kernel_counter_clear __user *ucounter_clear = |
| 707 | (struct lttng_kernel_counter_clear __user *) arg; |
| 708 | |
| 709 | if (copy_from_user(&local_counter_clear, ucounter_clear, |
| 710 | sizeof(local_counter_clear))) |
| 711 | return -EFAULT; |
| 712 | if (validate_zeroed_padding(local_counter_clear.padding, |
| 713 | sizeof(local_counter_clear.padding))) |
| 714 | return -EINVAL; |
| 715 | |
| 716 | /* Cast all indexes into size_t. */ |
| 717 | for (i = 0; i < local_counter_clear.index.number_dimensions; i++) |
| 718 | indexes[i] = (size_t) local_counter_clear.index.dimension_indexes[i]; |
| 719 | |
| 720 | return lttng_kernel_counter_clear(counter, indexes); |
| 721 | } |
| 722 | default: |
| 723 | WARN_ON_ONCE(1); |
| 724 | return -ENOSYS; |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | static const struct file_operations lttng_counter_fops = { |
| 729 | .owner = THIS_MODULE, |
| 730 | .release = lttng_counter_release, |
| 731 | .unlocked_ioctl = lttng_counter_ioctl, |
| 732 | #ifdef CONFIG_COMPAT |
| 733 | .compat_ioctl = lttng_counter_ioctl, |
| 734 | #endif |
| 735 | }; |
| 736 | |
| 737 | |
| 738 | static |
| 739 | enum tracker_type get_tracker_type(struct lttng_kernel_tracker_args *tracker) |
| 740 | { |
| 741 | switch (tracker->type) { |
| 742 | case LTTNG_KERNEL_TRACKER_PID: |
| 743 | return TRACKER_PID; |
| 744 | case LTTNG_KERNEL_TRACKER_VPID: |
| 745 | return TRACKER_VPID; |
| 746 | case LTTNG_KERNEL_TRACKER_UID: |
| 747 | return TRACKER_UID; |
| 748 | case LTTNG_KERNEL_TRACKER_VUID: |
| 749 | return TRACKER_VUID; |
| 750 | case LTTNG_KERNEL_TRACKER_GID: |
| 751 | return TRACKER_GID; |
| 752 | case LTTNG_KERNEL_TRACKER_VGID: |
| 753 | return TRACKER_VGID; |
| 754 | default: |
| 755 | return TRACKER_UNKNOWN; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | /** |
| 760 | * lttng_session_ioctl - lttng session fd ioctl |
| 761 | * |
| 762 | * @file: the file |
| 763 | * @cmd: the command |
| 764 | * @arg: command arg |
| 765 | * |
| 766 | * This ioctl implements lttng commands: |
| 767 | * LTTNG_KERNEL_CHANNEL |
| 768 | * Returns a LTTng channel file descriptor |
| 769 | * LTTNG_KERNEL_ENABLE |
| 770 | * Enables tracing for a session (weak enable) |
| 771 | * LTTNG_KERNEL_DISABLE |
| 772 | * Disables tracing for a session (strong disable) |
| 773 | * LTTNG_KERNEL_METADATA |
| 774 | * Returns a LTTng metadata file descriptor |
| 775 | * LTTNG_KERNEL_SESSION_TRACK_PID |
| 776 | * Add PID to session PID tracker |
| 777 | * LTTNG_KERNEL_SESSION_UNTRACK_PID |
| 778 | * Remove PID from session PID tracker |
| 779 | * LTTNG_KERNEL_SESSION_TRACK_ID |
| 780 | * Add ID to tracker |
| 781 | * LTTNG_KERNEL_SESSION_UNTRACK_ID |
| 782 | * Remove ID from tracker |
| 783 | * |
| 784 | * The returned channel will be deleted when its file descriptor is closed. |
| 785 | */ |
| 786 | static |
| 787 | long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 788 | { |
| 789 | struct lttng_session *session = file->private_data; |
| 790 | struct lttng_kernel_channel chan_param; |
| 791 | struct lttng_kernel_old_channel old_chan_param; |
| 792 | |
| 793 | switch (cmd) { |
| 794 | case LTTNG_KERNEL_OLD_CHANNEL: |
| 795 | { |
| 796 | if (copy_from_user(&old_chan_param, |
| 797 | (struct lttng_kernel_old_channel __user *) arg, |
| 798 | sizeof(struct lttng_kernel_old_channel))) |
| 799 | return -EFAULT; |
| 800 | chan_param.overwrite = old_chan_param.overwrite; |
| 801 | chan_param.subbuf_size = old_chan_param.subbuf_size; |
| 802 | chan_param.num_subbuf = old_chan_param.num_subbuf; |
| 803 | chan_param.switch_timer_interval = old_chan_param.switch_timer_interval; |
| 804 | chan_param.read_timer_interval = old_chan_param.read_timer_interval; |
| 805 | chan_param.output = old_chan_param.output; |
| 806 | |
| 807 | return lttng_abi_create_channel(file, &chan_param, |
| 808 | PER_CPU_CHANNEL); |
| 809 | } |
| 810 | case LTTNG_KERNEL_CHANNEL: |
| 811 | { |
| 812 | if (copy_from_user(&chan_param, |
| 813 | (struct lttng_kernel_channel __user *) arg, |
| 814 | sizeof(struct lttng_kernel_channel))) |
| 815 | return -EFAULT; |
| 816 | return lttng_abi_create_channel(file, &chan_param, |
| 817 | PER_CPU_CHANNEL); |
| 818 | } |
| 819 | case LTTNG_KERNEL_OLD_SESSION_START: |
| 820 | case LTTNG_KERNEL_OLD_ENABLE: |
| 821 | case LTTNG_KERNEL_SESSION_START: |
| 822 | case LTTNG_KERNEL_ENABLE: |
| 823 | return lttng_session_enable(session); |
| 824 | case LTTNG_KERNEL_OLD_SESSION_STOP: |
| 825 | case LTTNG_KERNEL_OLD_DISABLE: |
| 826 | case LTTNG_KERNEL_SESSION_STOP: |
| 827 | case LTTNG_KERNEL_DISABLE: |
| 828 | return lttng_session_disable(session); |
| 829 | case LTTNG_KERNEL_OLD_METADATA: |
| 830 | { |
| 831 | if (copy_from_user(&old_chan_param, |
| 832 | (struct lttng_kernel_old_channel __user *) arg, |
| 833 | sizeof(struct lttng_kernel_old_channel))) |
| 834 | return -EFAULT; |
| 835 | chan_param.overwrite = old_chan_param.overwrite; |
| 836 | chan_param.subbuf_size = old_chan_param.subbuf_size; |
| 837 | chan_param.num_subbuf = old_chan_param.num_subbuf; |
| 838 | chan_param.switch_timer_interval = old_chan_param.switch_timer_interval; |
| 839 | chan_param.read_timer_interval = old_chan_param.read_timer_interval; |
| 840 | chan_param.output = old_chan_param.output; |
| 841 | |
| 842 | return lttng_abi_create_channel(file, &chan_param, |
| 843 | METADATA_CHANNEL); |
| 844 | } |
| 845 | case LTTNG_KERNEL_METADATA: |
| 846 | { |
| 847 | if (copy_from_user(&chan_param, |
| 848 | (struct lttng_kernel_channel __user *) arg, |
| 849 | sizeof(struct lttng_kernel_channel))) |
| 850 | return -EFAULT; |
| 851 | return lttng_abi_create_channel(file, &chan_param, |
| 852 | METADATA_CHANNEL); |
| 853 | } |
| 854 | case LTTNG_KERNEL_SESSION_TRACK_PID: |
| 855 | return lttng_session_track_id(session, TRACKER_PID, (int) arg); |
| 856 | case LTTNG_KERNEL_SESSION_UNTRACK_PID: |
| 857 | return lttng_session_untrack_id(session, TRACKER_PID, (int) arg); |
| 858 | case LTTNG_KERNEL_SESSION_TRACK_ID: |
| 859 | { |
| 860 | struct lttng_kernel_tracker_args tracker; |
| 861 | enum tracker_type tracker_type; |
| 862 | |
| 863 | if (copy_from_user(&tracker, |
| 864 | (struct lttng_kernel_tracker_args __user *) arg, |
| 865 | sizeof(struct lttng_kernel_tracker_args))) |
| 866 | return -EFAULT; |
| 867 | tracker_type = get_tracker_type(&tracker); |
| 868 | if (tracker_type == TRACKER_UNKNOWN) |
| 869 | return -EINVAL; |
| 870 | return lttng_session_track_id(session, tracker_type, tracker.id); |
| 871 | } |
| 872 | case LTTNG_KERNEL_SESSION_UNTRACK_ID: |
| 873 | { |
| 874 | struct lttng_kernel_tracker_args tracker; |
| 875 | enum tracker_type tracker_type; |
| 876 | |
| 877 | if (copy_from_user(&tracker, |
| 878 | (struct lttng_kernel_tracker_args __user *) arg, |
| 879 | sizeof(struct lttng_kernel_tracker_args))) |
| 880 | return -EFAULT; |
| 881 | tracker_type = get_tracker_type(&tracker); |
| 882 | if (tracker_type == TRACKER_UNKNOWN) |
| 883 | return -EINVAL; |
| 884 | return lttng_session_untrack_id(session, tracker_type, |
| 885 | tracker.id); |
| 886 | } |
| 887 | case LTTNG_KERNEL_SESSION_LIST_TRACKER_PIDS: |
| 888 | return lttng_session_list_tracker_ids(session, TRACKER_PID); |
| 889 | case LTTNG_KERNEL_SESSION_LIST_TRACKER_IDS: |
| 890 | { |
| 891 | struct lttng_kernel_tracker_args tracker; |
| 892 | enum tracker_type tracker_type; |
| 893 | |
| 894 | if (copy_from_user(&tracker, |
| 895 | (struct lttng_kernel_tracker_args __user *) arg, |
| 896 | sizeof(struct lttng_kernel_tracker_args))) |
| 897 | return -EFAULT; |
| 898 | tracker_type = get_tracker_type(&tracker); |
| 899 | if (tracker_type == TRACKER_UNKNOWN) |
| 900 | return -EINVAL; |
| 901 | return lttng_session_list_tracker_ids(session, tracker_type); |
| 902 | } |
| 903 | case LTTNG_KERNEL_SESSION_METADATA_REGEN: |
| 904 | return lttng_session_metadata_regenerate(session); |
| 905 | case LTTNG_KERNEL_SESSION_STATEDUMP: |
| 906 | return lttng_session_statedump(session); |
| 907 | case LTTNG_KERNEL_SESSION_SET_NAME: |
| 908 | { |
| 909 | struct lttng_kernel_session_name name; |
| 910 | |
| 911 | if (copy_from_user(&name, |
| 912 | (struct lttng_kernel_session_name __user *) arg, |
| 913 | sizeof(struct lttng_kernel_session_name))) |
| 914 | return -EFAULT; |
| 915 | return lttng_abi_session_set_name(session, &name); |
| 916 | } |
| 917 | case LTTNG_KERNEL_SESSION_SET_CREATION_TIME: |
| 918 | { |
| 919 | struct lttng_kernel_session_creation_time time; |
| 920 | |
| 921 | if (copy_from_user(&time, |
| 922 | (struct lttng_kernel_session_creation_time __user *) arg, |
| 923 | sizeof(struct lttng_kernel_session_creation_time))) |
| 924 | return -EFAULT; |
| 925 | return lttng_abi_session_set_creation_time(session, &time); |
| 926 | } |
| 927 | default: |
| 928 | return -ENOIOCTLCMD; |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | /* |
| 933 | * Called when the last file reference is dropped. |
| 934 | * |
| 935 | * Big fat note: channels and events are invariant for the whole session after |
| 936 | * their creation. So this session destruction also destroys all channel and |
| 937 | * event structures specific to this session (they are not destroyed when their |
| 938 | * individual file is released). |
| 939 | */ |
| 940 | static |
| 941 | int lttng_session_release(struct inode *inode, struct file *file) |
| 942 | { |
| 943 | struct lttng_session *session = file->private_data; |
| 944 | |
| 945 | if (session) |
| 946 | lttng_session_destroy(session); |
| 947 | return 0; |
| 948 | } |
| 949 | |
| 950 | static const struct file_operations lttng_session_fops = { |
| 951 | .owner = THIS_MODULE, |
| 952 | .release = lttng_session_release, |
| 953 | .unlocked_ioctl = lttng_session_ioctl, |
| 954 | #ifdef CONFIG_COMPAT |
| 955 | .compat_ioctl = lttng_session_ioctl, |
| 956 | #endif |
| 957 | }; |
| 958 | |
| 959 | /* |
| 960 | * When encountering empty buffer, flush current sub-buffer if non-empty |
| 961 | * and retry (if new data available to read after flush). |
| 962 | */ |
| 963 | static |
| 964 | ssize_t lttng_event_notifier_group_notif_read(struct file *filp, char __user *user_buf, |
| 965 | size_t count, loff_t *ppos) |
| 966 | { |
| 967 | struct lttng_event_notifier_group *event_notifier_group = filp->private_data; |
| 968 | struct channel *chan = event_notifier_group->chan; |
| 969 | struct lib_ring_buffer *buf = event_notifier_group->buf; |
| 970 | ssize_t read_count = 0, len; |
| 971 | size_t read_offset; |
| 972 | |
| 973 | might_sleep(); |
| 974 | if (!lttng_access_ok(VERIFY_WRITE, user_buf, count)) |
| 975 | return -EFAULT; |
| 976 | |
| 977 | /* Finish copy of previous record */ |
| 978 | if (*ppos != 0) { |
| 979 | if (read_count < count) { |
| 980 | len = chan->iter.len_left; |
| 981 | read_offset = *ppos; |
| 982 | goto skip_get_next; |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | while (read_count < count) { |
| 987 | size_t copy_len, space_left; |
| 988 | |
| 989 | len = lib_ring_buffer_get_next_record(chan, buf); |
| 990 | len_test: |
| 991 | if (len < 0) { |
| 992 | /* |
| 993 | * Check if buffer is finalized (end of file). |
| 994 | */ |
| 995 | if (len == -ENODATA) { |
| 996 | /* A 0 read_count will tell about end of file */ |
| 997 | goto nodata; |
| 998 | } |
| 999 | if (filp->f_flags & O_NONBLOCK) { |
| 1000 | if (!read_count) |
| 1001 | read_count = -EAGAIN; |
| 1002 | goto nodata; |
| 1003 | } else { |
| 1004 | int error; |
| 1005 | |
| 1006 | /* |
| 1007 | * No data available at the moment, return what |
| 1008 | * we got. |
| 1009 | */ |
| 1010 | if (read_count) |
| 1011 | goto nodata; |
| 1012 | |
| 1013 | /* |
| 1014 | * Wait for returned len to be >= 0 or -ENODATA. |
| 1015 | */ |
| 1016 | error = wait_event_interruptible( |
| 1017 | event_notifier_group->read_wait, |
| 1018 | ((len = lib_ring_buffer_get_next_record( |
| 1019 | chan, buf)), len != -EAGAIN)); |
| 1020 | CHAN_WARN_ON(chan, len == -EBUSY); |
| 1021 | if (error) { |
| 1022 | read_count = error; |
| 1023 | goto nodata; |
| 1024 | } |
| 1025 | CHAN_WARN_ON(chan, len < 0 && len != -ENODATA); |
| 1026 | goto len_test; |
| 1027 | } |
| 1028 | } |
| 1029 | read_offset = buf->iter.read_offset; |
| 1030 | skip_get_next: |
| 1031 | space_left = count - read_count; |
| 1032 | if (len <= space_left) { |
| 1033 | copy_len = len; |
| 1034 | chan->iter.len_left = 0; |
| 1035 | *ppos = 0; |
| 1036 | } else { |
| 1037 | copy_len = space_left; |
| 1038 | chan->iter.len_left = len - copy_len; |
| 1039 | *ppos = read_offset + copy_len; |
| 1040 | } |
| 1041 | if (__lib_ring_buffer_copy_to_user(&buf->backend, read_offset, |
| 1042 | &user_buf[read_count], |
| 1043 | copy_len)) { |
| 1044 | /* |
| 1045 | * Leave the len_left and ppos values at their current |
| 1046 | * state, as we currently have a valid event to read. |
| 1047 | */ |
| 1048 | return -EFAULT; |
| 1049 | } |
| 1050 | read_count += copy_len; |
| 1051 | } |
| 1052 | goto put_record; |
| 1053 | |
| 1054 | nodata: |
| 1055 | *ppos = 0; |
| 1056 | chan->iter.len_left = 0; |
| 1057 | |
| 1058 | put_record: |
| 1059 | lib_ring_buffer_put_current_record(buf); |
| 1060 | return read_count; |
| 1061 | } |
| 1062 | |
| 1063 | /* |
| 1064 | * If the ring buffer is non empty (even just a partial subbuffer), return that |
| 1065 | * there is data available. Perform a ring buffer flush if we encounter a |
| 1066 | * non-empty ring buffer which does not have any consumeable subbuffer available. |
| 1067 | */ |
| 1068 | static |
| 1069 | unsigned int lttng_event_notifier_group_notif_poll(struct file *filp, |
| 1070 | poll_table *wait) |
| 1071 | { |
| 1072 | unsigned int mask = 0; |
| 1073 | struct lttng_event_notifier_group *event_notifier_group = filp->private_data; |
| 1074 | struct channel *chan = event_notifier_group->chan; |
| 1075 | struct lib_ring_buffer *buf = event_notifier_group->buf; |
| 1076 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1077 | int finalized, disabled; |
| 1078 | unsigned long consumed, offset; |
| 1079 | size_t subbuffer_header_size = config->cb.subbuffer_header_size(); |
| 1080 | |
| 1081 | if (filp->f_mode & FMODE_READ) { |
| 1082 | poll_wait_set_exclusive(wait); |
| 1083 | poll_wait(filp, &event_notifier_group->read_wait, wait); |
| 1084 | |
| 1085 | finalized = lib_ring_buffer_is_finalized(config, buf); |
| 1086 | disabled = lib_ring_buffer_channel_is_disabled(chan); |
| 1087 | |
| 1088 | /* |
| 1089 | * lib_ring_buffer_is_finalized() contains a smp_rmb() ordering |
| 1090 | * finalized load before offsets loads. |
| 1091 | */ |
| 1092 | WARN_ON(atomic_long_read(&buf->active_readers) != 1); |
| 1093 | retry: |
| 1094 | if (disabled) |
| 1095 | return POLLERR; |
| 1096 | |
| 1097 | offset = lib_ring_buffer_get_offset(config, buf); |
| 1098 | consumed = lib_ring_buffer_get_consumed(config, buf); |
| 1099 | |
| 1100 | /* |
| 1101 | * If there is no buffer available to consume. |
| 1102 | */ |
| 1103 | if (subbuf_trunc(offset, chan) - subbuf_trunc(consumed, chan) == 0) { |
| 1104 | /* |
| 1105 | * If there is a non-empty subbuffer, flush and try again. |
| 1106 | */ |
| 1107 | if (subbuf_offset(offset, chan) > subbuffer_header_size) { |
| 1108 | lib_ring_buffer_switch_remote(buf); |
| 1109 | goto retry; |
| 1110 | } |
| 1111 | |
| 1112 | if (finalized) |
| 1113 | return POLLHUP; |
| 1114 | else { |
| 1115 | /* |
| 1116 | * The memory barriers |
| 1117 | * __wait_event()/wake_up_interruptible() take |
| 1118 | * care of "raw_spin_is_locked" memory ordering. |
| 1119 | */ |
| 1120 | if (raw_spin_is_locked(&buf->raw_tick_nohz_spinlock)) |
| 1121 | goto retry; |
| 1122 | else |
| 1123 | return 0; |
| 1124 | } |
| 1125 | } else { |
| 1126 | if (subbuf_trunc(offset, chan) - subbuf_trunc(consumed, chan) |
| 1127 | >= chan->backend.buf_size) |
| 1128 | return POLLPRI | POLLRDBAND; |
| 1129 | else |
| 1130 | return POLLIN | POLLRDNORM; |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | return mask; |
| 1135 | } |
| 1136 | |
| 1137 | /** |
| 1138 | * lttng_event_notifier_group_notif_open - event_notifier ring buffer open file operation |
| 1139 | * @inode: opened inode |
| 1140 | * @file: opened file |
| 1141 | * |
| 1142 | * Open implementation. Makes sure only one open instance of a buffer is |
| 1143 | * done at a given moment. |
| 1144 | */ |
| 1145 | static int lttng_event_notifier_group_notif_open(struct inode *inode, struct file *file) |
| 1146 | { |
| 1147 | struct lttng_event_notifier_group *event_notifier_group = inode->i_private; |
| 1148 | struct lib_ring_buffer *buf = event_notifier_group->buf; |
| 1149 | |
| 1150 | file->private_data = event_notifier_group; |
| 1151 | return lib_ring_buffer_open(inode, file, buf); |
| 1152 | } |
| 1153 | |
| 1154 | /** |
| 1155 | * lttng_event_notifier_group_notif_release - event_notifier ring buffer release file operation |
| 1156 | * @inode: opened inode |
| 1157 | * @file: opened file |
| 1158 | * |
| 1159 | * Release implementation. |
| 1160 | */ |
| 1161 | static int lttng_event_notifier_group_notif_release(struct inode *inode, struct file *file) |
| 1162 | { |
| 1163 | struct lttng_event_notifier_group *event_notifier_group = file->private_data; |
| 1164 | struct lib_ring_buffer *buf = event_notifier_group->buf; |
| 1165 | int ret; |
| 1166 | |
| 1167 | ret = lib_ring_buffer_release(inode, file, buf); |
| 1168 | if (ret) |
| 1169 | return ret; |
| 1170 | fput(event_notifier_group->file); |
| 1171 | return 0; |
| 1172 | } |
| 1173 | |
| 1174 | static const struct file_operations lttng_event_notifier_group_notif_fops = { |
| 1175 | .owner = THIS_MODULE, |
| 1176 | .open = lttng_event_notifier_group_notif_open, |
| 1177 | .release = lttng_event_notifier_group_notif_release, |
| 1178 | .read = lttng_event_notifier_group_notif_read, |
| 1179 | .poll = lttng_event_notifier_group_notif_poll, |
| 1180 | }; |
| 1181 | |
| 1182 | /** |
| 1183 | * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation |
| 1184 | * @filp: the file |
| 1185 | * @wait: poll table |
| 1186 | * |
| 1187 | * Handles the poll operations for the metadata channels. |
| 1188 | */ |
| 1189 | static |
| 1190 | unsigned int lttng_metadata_ring_buffer_poll(struct file *filp, |
| 1191 | poll_table *wait) |
| 1192 | { |
| 1193 | struct lttng_metadata_stream *stream = filp->private_data; |
| 1194 | struct lib_ring_buffer *buf = stream->priv; |
| 1195 | int finalized; |
| 1196 | unsigned int mask = 0; |
| 1197 | |
| 1198 | if (filp->f_mode & FMODE_READ) { |
| 1199 | poll_wait_set_exclusive(wait); |
| 1200 | poll_wait(filp, &stream->read_wait, wait); |
| 1201 | |
| 1202 | finalized = stream->finalized; |
| 1203 | |
| 1204 | /* |
| 1205 | * lib_ring_buffer_is_finalized() contains a smp_rmb() |
| 1206 | * ordering finalized load before offsets loads. |
| 1207 | */ |
| 1208 | WARN_ON(atomic_long_read(&buf->active_readers) != 1); |
| 1209 | |
| 1210 | if (finalized) |
| 1211 | mask |= POLLHUP; |
| 1212 | |
| 1213 | mutex_lock(&stream->metadata_cache->lock); |
| 1214 | if (stream->metadata_cache->metadata_written > |
| 1215 | stream->metadata_out) |
| 1216 | mask |= POLLIN; |
| 1217 | mutex_unlock(&stream->metadata_cache->lock); |
| 1218 | } |
| 1219 | |
| 1220 | return mask; |
| 1221 | } |
| 1222 | |
| 1223 | static |
| 1224 | void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file *filp, |
| 1225 | unsigned int cmd, unsigned long arg) |
| 1226 | { |
| 1227 | struct lttng_metadata_stream *stream = filp->private_data; |
| 1228 | |
| 1229 | stream->metadata_out = stream->metadata_in; |
| 1230 | } |
| 1231 | |
| 1232 | /* |
| 1233 | * Reset the counter of how much metadata has been consumed to 0. That way, |
| 1234 | * the consumer receives the content of the metadata cache unchanged. This is |
| 1235 | * different from the metadata_regenerate where the offset from epoch is |
| 1236 | * resampled, here we want the exact same content as the last time the metadata |
| 1237 | * was generated. This command is only possible if all the metadata written |
| 1238 | * in the cache has been output to the metadata stream to avoid corrupting the |
| 1239 | * metadata file. |
| 1240 | * |
| 1241 | * Return 0 on success, a negative value on error. |
| 1242 | */ |
| 1243 | static |
| 1244 | int lttng_metadata_cache_dump(struct lttng_metadata_stream *stream) |
| 1245 | { |
| 1246 | int ret; |
| 1247 | struct lttng_metadata_cache *cache = stream->metadata_cache; |
| 1248 | |
| 1249 | mutex_lock(&cache->lock); |
| 1250 | if (stream->metadata_out != cache->metadata_written) { |
| 1251 | ret = -EBUSY; |
| 1252 | goto end; |
| 1253 | } |
| 1254 | stream->metadata_out = 0; |
| 1255 | stream->metadata_in = 0; |
| 1256 | wake_up_interruptible(&stream->read_wait); |
| 1257 | ret = 0; |
| 1258 | |
| 1259 | end: |
| 1260 | mutex_unlock(&cache->lock); |
| 1261 | return ret; |
| 1262 | } |
| 1263 | |
| 1264 | static |
| 1265 | long lttng_metadata_ring_buffer_ioctl(struct file *filp, |
| 1266 | unsigned int cmd, unsigned long arg) |
| 1267 | { |
| 1268 | int ret; |
| 1269 | struct lttng_metadata_stream *stream = filp->private_data; |
| 1270 | struct lib_ring_buffer *buf = stream->priv; |
| 1271 | unsigned int rb_cmd; |
| 1272 | bool coherent; |
| 1273 | |
| 1274 | if (cmd == RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK) |
| 1275 | rb_cmd = RING_BUFFER_GET_NEXT_SUBBUF; |
| 1276 | else |
| 1277 | rb_cmd = cmd; |
| 1278 | |
| 1279 | switch (cmd) { |
| 1280 | case RING_BUFFER_GET_NEXT_SUBBUF: |
| 1281 | { |
| 1282 | struct lttng_metadata_stream *stream = filp->private_data; |
| 1283 | struct lib_ring_buffer *buf = stream->priv; |
| 1284 | struct channel *chan = buf->backend.chan; |
| 1285 | |
| 1286 | ret = lttng_metadata_output_channel(stream, chan, NULL); |
| 1287 | if (ret > 0) { |
| 1288 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE); |
| 1289 | ret = 0; |
| 1290 | } else if (ret < 0) |
| 1291 | goto err; |
| 1292 | break; |
| 1293 | } |
| 1294 | case RING_BUFFER_GET_SUBBUF: |
| 1295 | { |
| 1296 | /* |
| 1297 | * Random access is not allowed for metadata channel. |
| 1298 | */ |
| 1299 | return -ENOSYS; |
| 1300 | } |
| 1301 | case RING_BUFFER_FLUSH_EMPTY: /* Fall-through. */ |
| 1302 | case RING_BUFFER_FLUSH: |
| 1303 | { |
| 1304 | struct lttng_metadata_stream *stream = filp->private_data; |
| 1305 | struct lib_ring_buffer *buf = stream->priv; |
| 1306 | struct channel *chan = buf->backend.chan; |
| 1307 | |
| 1308 | /* |
| 1309 | * Before doing the actual ring buffer flush, write up to one |
| 1310 | * packet of metadata in the ring buffer. |
| 1311 | */ |
| 1312 | ret = lttng_metadata_output_channel(stream, chan, NULL); |
| 1313 | if (ret < 0) |
| 1314 | goto err; |
| 1315 | break; |
| 1316 | } |
| 1317 | case RING_BUFFER_GET_METADATA_VERSION: |
| 1318 | { |
| 1319 | struct lttng_metadata_stream *stream = filp->private_data; |
| 1320 | |
| 1321 | return put_u64(stream->version, arg); |
| 1322 | } |
| 1323 | case RING_BUFFER_METADATA_CACHE_DUMP: |
| 1324 | { |
| 1325 | struct lttng_metadata_stream *stream = filp->private_data; |
| 1326 | |
| 1327 | return lttng_metadata_cache_dump(stream); |
| 1328 | } |
| 1329 | case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK: |
| 1330 | { |
| 1331 | struct lttng_metadata_stream *stream = filp->private_data; |
| 1332 | struct lib_ring_buffer *buf = stream->priv; |
| 1333 | struct channel *chan = buf->backend.chan; |
| 1334 | |
| 1335 | ret = lttng_metadata_output_channel(stream, chan, &coherent); |
| 1336 | if (ret > 0) { |
| 1337 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE); |
| 1338 | ret = 0; |
| 1339 | } else if (ret < 0) { |
| 1340 | goto err; |
| 1341 | } |
| 1342 | break; |
| 1343 | } |
| 1344 | default: |
| 1345 | break; |
| 1346 | } |
| 1347 | /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */ |
| 1348 | |
| 1349 | /* Performing lib ring buffer ioctl after our own. */ |
| 1350 | ret = lib_ring_buffer_ioctl(filp, rb_cmd, arg, buf); |
| 1351 | if (ret < 0) |
| 1352 | goto err; |
| 1353 | |
| 1354 | switch (cmd) { |
| 1355 | case RING_BUFFER_PUT_NEXT_SUBBUF: |
| 1356 | { |
| 1357 | lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp, |
| 1358 | cmd, arg); |
| 1359 | break; |
| 1360 | } |
| 1361 | case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK: |
| 1362 | { |
| 1363 | return put_u32(coherent, arg); |
| 1364 | } |
| 1365 | default: |
| 1366 | break; |
| 1367 | } |
| 1368 | err: |
| 1369 | return ret; |
| 1370 | } |
| 1371 | |
| 1372 | #ifdef CONFIG_COMPAT |
| 1373 | static |
| 1374 | long lttng_metadata_ring_buffer_compat_ioctl(struct file *filp, |
| 1375 | unsigned int cmd, unsigned long arg) |
| 1376 | { |
| 1377 | int ret; |
| 1378 | struct lttng_metadata_stream *stream = filp->private_data; |
| 1379 | struct lib_ring_buffer *buf = stream->priv; |
| 1380 | unsigned int rb_cmd; |
| 1381 | bool coherent; |
| 1382 | |
| 1383 | if (cmd == RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK) |
| 1384 | rb_cmd = RING_BUFFER_GET_NEXT_SUBBUF; |
| 1385 | else |
| 1386 | rb_cmd = cmd; |
| 1387 | |
| 1388 | switch (cmd) { |
| 1389 | case RING_BUFFER_GET_NEXT_SUBBUF: |
| 1390 | { |
| 1391 | struct lttng_metadata_stream *stream = filp->private_data; |
| 1392 | struct lib_ring_buffer *buf = stream->priv; |
| 1393 | struct channel *chan = buf->backend.chan; |
| 1394 | |
| 1395 | ret = lttng_metadata_output_channel(stream, chan, NULL); |
| 1396 | if (ret > 0) { |
| 1397 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE); |
| 1398 | ret = 0; |
| 1399 | } else if (ret < 0) |
| 1400 | goto err; |
| 1401 | break; |
| 1402 | } |
| 1403 | case RING_BUFFER_GET_SUBBUF: |
| 1404 | { |
| 1405 | /* |
| 1406 | * Random access is not allowed for metadata channel. |
| 1407 | */ |
| 1408 | return -ENOSYS; |
| 1409 | } |
| 1410 | case RING_BUFFER_FLUSH_EMPTY: /* Fall-through. */ |
| 1411 | case RING_BUFFER_FLUSH: |
| 1412 | { |
| 1413 | struct lttng_metadata_stream *stream = filp->private_data; |
| 1414 | struct lib_ring_buffer *buf = stream->priv; |
| 1415 | struct channel *chan = buf->backend.chan; |
| 1416 | |
| 1417 | /* |
| 1418 | * Before doing the actual ring buffer flush, write up to one |
| 1419 | * packet of metadata in the ring buffer. |
| 1420 | */ |
| 1421 | ret = lttng_metadata_output_channel(stream, chan, NULL); |
| 1422 | if (ret < 0) |
| 1423 | goto err; |
| 1424 | break; |
| 1425 | } |
| 1426 | case RING_BUFFER_GET_METADATA_VERSION: |
| 1427 | { |
| 1428 | struct lttng_metadata_stream *stream = filp->private_data; |
| 1429 | |
| 1430 | return put_u64(stream->version, arg); |
| 1431 | } |
| 1432 | case RING_BUFFER_METADATA_CACHE_DUMP: |
| 1433 | { |
| 1434 | struct lttng_metadata_stream *stream = filp->private_data; |
| 1435 | |
| 1436 | return lttng_metadata_cache_dump(stream); |
| 1437 | } |
| 1438 | case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK: |
| 1439 | { |
| 1440 | struct lttng_metadata_stream *stream = filp->private_data; |
| 1441 | struct lib_ring_buffer *buf = stream->priv; |
| 1442 | struct channel *chan = buf->backend.chan; |
| 1443 | |
| 1444 | ret = lttng_metadata_output_channel(stream, chan, &coherent); |
| 1445 | if (ret > 0) { |
| 1446 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE); |
| 1447 | ret = 0; |
| 1448 | } else if (ret < 0) { |
| 1449 | goto err; |
| 1450 | } |
| 1451 | break; |
| 1452 | } |
| 1453 | default: |
| 1454 | break; |
| 1455 | } |
| 1456 | /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */ |
| 1457 | |
| 1458 | /* Performing lib ring buffer ioctl after our own. */ |
| 1459 | ret = lib_ring_buffer_compat_ioctl(filp, rb_cmd, arg, buf); |
| 1460 | if (ret < 0) |
| 1461 | goto err; |
| 1462 | |
| 1463 | switch (cmd) { |
| 1464 | case RING_BUFFER_PUT_NEXT_SUBBUF: |
| 1465 | { |
| 1466 | lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp, |
| 1467 | cmd, arg); |
| 1468 | break; |
| 1469 | } |
| 1470 | case RING_BUFFER_GET_NEXT_SUBBUF_METADATA_CHECK: |
| 1471 | { |
| 1472 | return put_u32(coherent, arg); |
| 1473 | } |
| 1474 | default: |
| 1475 | break; |
| 1476 | } |
| 1477 | err: |
| 1478 | return ret; |
| 1479 | } |
| 1480 | #endif |
| 1481 | |
| 1482 | /* |
| 1483 | * This is not used by anonymous file descriptors. This code is left |
| 1484 | * there if we ever want to implement an inode with open() operation. |
| 1485 | */ |
| 1486 | static |
| 1487 | int lttng_metadata_ring_buffer_open(struct inode *inode, struct file *file) |
| 1488 | { |
| 1489 | struct lttng_metadata_stream *stream = inode->i_private; |
| 1490 | struct lib_ring_buffer *buf = stream->priv; |
| 1491 | |
| 1492 | file->private_data = buf; |
| 1493 | /* |
| 1494 | * Since life-time of metadata cache differs from that of |
| 1495 | * session, we need to keep our own reference on the transport. |
| 1496 | */ |
| 1497 | if (!try_module_get(stream->transport->owner)) { |
| 1498 | printk(KERN_WARNING "LTTng: Can't lock transport module.\n"); |
| 1499 | return -EBUSY; |
| 1500 | } |
| 1501 | return lib_ring_buffer_open(inode, file, buf); |
| 1502 | } |
| 1503 | |
| 1504 | static |
| 1505 | int lttng_metadata_ring_buffer_release(struct inode *inode, struct file *file) |
| 1506 | { |
| 1507 | struct lttng_metadata_stream *stream = file->private_data; |
| 1508 | struct lib_ring_buffer *buf = stream->priv; |
| 1509 | |
| 1510 | mutex_lock(&stream->metadata_cache->lock); |
| 1511 | list_del(&stream->list); |
| 1512 | mutex_unlock(&stream->metadata_cache->lock); |
| 1513 | kref_put(&stream->metadata_cache->refcount, metadata_cache_destroy); |
| 1514 | module_put(stream->transport->owner); |
| 1515 | kfree(stream); |
| 1516 | return lib_ring_buffer_release(inode, file, buf); |
| 1517 | } |
| 1518 | |
| 1519 | static |
| 1520 | ssize_t lttng_metadata_ring_buffer_splice_read(struct file *in, loff_t *ppos, |
| 1521 | struct pipe_inode_info *pipe, size_t len, |
| 1522 | unsigned int flags) |
| 1523 | { |
| 1524 | struct lttng_metadata_stream *stream = in->private_data; |
| 1525 | struct lib_ring_buffer *buf = stream->priv; |
| 1526 | |
| 1527 | return lib_ring_buffer_splice_read(in, ppos, pipe, len, |
| 1528 | flags, buf); |
| 1529 | } |
| 1530 | |
| 1531 | static |
| 1532 | int lttng_metadata_ring_buffer_mmap(struct file *filp, |
| 1533 | struct vm_area_struct *vma) |
| 1534 | { |
| 1535 | struct lttng_metadata_stream *stream = filp->private_data; |
| 1536 | struct lib_ring_buffer *buf = stream->priv; |
| 1537 | |
| 1538 | return lib_ring_buffer_mmap(filp, vma, buf); |
| 1539 | } |
| 1540 | |
| 1541 | static |
| 1542 | const struct file_operations lttng_metadata_ring_buffer_file_operations = { |
| 1543 | .owner = THIS_MODULE, |
| 1544 | .open = lttng_metadata_ring_buffer_open, |
| 1545 | .release = lttng_metadata_ring_buffer_release, |
| 1546 | .poll = lttng_metadata_ring_buffer_poll, |
| 1547 | .splice_read = lttng_metadata_ring_buffer_splice_read, |
| 1548 | .mmap = lttng_metadata_ring_buffer_mmap, |
| 1549 | .unlocked_ioctl = lttng_metadata_ring_buffer_ioctl, |
| 1550 | .llseek = vfs_lib_ring_buffer_no_llseek, |
| 1551 | #ifdef CONFIG_COMPAT |
| 1552 | .compat_ioctl = lttng_metadata_ring_buffer_compat_ioctl, |
| 1553 | #endif |
| 1554 | }; |
| 1555 | |
| 1556 | static |
| 1557 | int lttng_abi_create_stream_fd(struct file *channel_file, void *stream_priv, |
| 1558 | const struct file_operations *fops, const char *name) |
| 1559 | { |
| 1560 | int stream_fd, ret; |
| 1561 | struct file *stream_file; |
| 1562 | |
| 1563 | stream_fd = lttng_get_unused_fd(); |
| 1564 | if (stream_fd < 0) { |
| 1565 | ret = stream_fd; |
| 1566 | goto fd_error; |
| 1567 | } |
| 1568 | stream_file = anon_inode_getfile(name, fops, stream_priv, O_RDWR); |
| 1569 | if (IS_ERR(stream_file)) { |
| 1570 | ret = PTR_ERR(stream_file); |
| 1571 | goto file_error; |
| 1572 | } |
| 1573 | /* |
| 1574 | * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor |
| 1575 | * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this |
| 1576 | * file descriptor, so we set FMODE_PREAD here. |
| 1577 | */ |
| 1578 | stream_file->f_mode |= FMODE_PREAD; |
| 1579 | fd_install(stream_fd, stream_file); |
| 1580 | /* |
| 1581 | * The stream holds a reference to the channel within the generic ring |
| 1582 | * buffer library, so no need to hold a refcount on the channel and |
| 1583 | * session files here. |
| 1584 | */ |
| 1585 | return stream_fd; |
| 1586 | |
| 1587 | file_error: |
| 1588 | put_unused_fd(stream_fd); |
| 1589 | fd_error: |
| 1590 | return ret; |
| 1591 | } |
| 1592 | |
| 1593 | static |
| 1594 | int lttng_abi_open_stream(struct file *channel_file) |
| 1595 | { |
| 1596 | struct lttng_channel *channel = channel_file->private_data; |
| 1597 | struct lib_ring_buffer *buf; |
| 1598 | int ret; |
| 1599 | void *stream_priv; |
| 1600 | |
| 1601 | buf = channel->ops->buffer_read_open(channel->chan); |
| 1602 | if (!buf) |
| 1603 | return -ENOENT; |
| 1604 | |
| 1605 | stream_priv = buf; |
| 1606 | ret = lttng_abi_create_stream_fd(channel_file, stream_priv, |
| 1607 | <tng_stream_ring_buffer_file_operations, |
| 1608 | "[lttng_stream]"); |
| 1609 | if (ret < 0) |
| 1610 | goto fd_error; |
| 1611 | |
| 1612 | return ret; |
| 1613 | |
| 1614 | fd_error: |
| 1615 | channel->ops->buffer_read_close(buf); |
| 1616 | return ret; |
| 1617 | } |
| 1618 | |
| 1619 | static |
| 1620 | int lttng_abi_open_metadata_stream(struct file *channel_file) |
| 1621 | { |
| 1622 | struct lttng_channel *channel = channel_file->private_data; |
| 1623 | struct lttng_session *session = channel->session; |
| 1624 | struct lib_ring_buffer *buf; |
| 1625 | int ret; |
| 1626 | struct lttng_metadata_stream *metadata_stream; |
| 1627 | void *stream_priv; |
| 1628 | |
| 1629 | buf = channel->ops->buffer_read_open(channel->chan); |
| 1630 | if (!buf) |
| 1631 | return -ENOENT; |
| 1632 | |
| 1633 | metadata_stream = kzalloc(sizeof(struct lttng_metadata_stream), |
| 1634 | GFP_KERNEL); |
| 1635 | if (!metadata_stream) { |
| 1636 | ret = -ENOMEM; |
| 1637 | goto nomem; |
| 1638 | } |
| 1639 | metadata_stream->metadata_cache = session->metadata_cache; |
| 1640 | init_waitqueue_head(&metadata_stream->read_wait); |
| 1641 | metadata_stream->priv = buf; |
| 1642 | stream_priv = metadata_stream; |
| 1643 | metadata_stream->transport = channel->transport; |
| 1644 | /* Initial state is an empty metadata, considered as incoherent. */ |
| 1645 | metadata_stream->coherent = false; |
| 1646 | |
| 1647 | /* |
| 1648 | * Since life-time of metadata cache differs from that of |
| 1649 | * session, we need to keep our own reference on the transport. |
| 1650 | */ |
| 1651 | if (!try_module_get(metadata_stream->transport->owner)) { |
| 1652 | printk(KERN_WARNING "LTTng: Can't lock transport module.\n"); |
| 1653 | ret = -EINVAL; |
| 1654 | goto notransport; |
| 1655 | } |
| 1656 | |
| 1657 | if (!lttng_kref_get(&session->metadata_cache->refcount)) { |
| 1658 | ret = -EOVERFLOW; |
| 1659 | goto kref_error; |
| 1660 | } |
| 1661 | |
| 1662 | ret = lttng_abi_create_stream_fd(channel_file, stream_priv, |
| 1663 | <tng_metadata_ring_buffer_file_operations, |
| 1664 | "[lttng_metadata_stream]"); |
| 1665 | if (ret < 0) |
| 1666 | goto fd_error; |
| 1667 | |
| 1668 | mutex_lock(&session->metadata_cache->lock); |
| 1669 | list_add(&metadata_stream->list, |
| 1670 | &session->metadata_cache->metadata_stream); |
| 1671 | mutex_unlock(&session->metadata_cache->lock); |
| 1672 | return ret; |
| 1673 | |
| 1674 | fd_error: |
| 1675 | kref_put(&session->metadata_cache->refcount, metadata_cache_destroy); |
| 1676 | kref_error: |
| 1677 | module_put(metadata_stream->transport->owner); |
| 1678 | notransport: |
| 1679 | kfree(metadata_stream); |
| 1680 | nomem: |
| 1681 | channel->ops->buffer_read_close(buf); |
| 1682 | return ret; |
| 1683 | } |
| 1684 | |
| 1685 | static |
| 1686 | int lttng_abi_open_event_notifier_group_stream(struct file *notif_file) |
| 1687 | { |
| 1688 | struct lttng_event_notifier_group *event_notifier_group = notif_file->private_data; |
| 1689 | struct channel *chan = event_notifier_group->chan; |
| 1690 | struct lib_ring_buffer *buf; |
| 1691 | int ret; |
| 1692 | void *stream_priv; |
| 1693 | |
| 1694 | buf = event_notifier_group->ops->buffer_read_open(chan); |
| 1695 | if (!buf) |
| 1696 | return -ENOENT; |
| 1697 | |
| 1698 | /* The event_notifier notification fd holds a reference on the event_notifier group */ |
| 1699 | if (!atomic_long_add_unless(¬if_file->f_count, 1, LONG_MAX)) { |
| 1700 | ret = -EOVERFLOW; |
| 1701 | goto refcount_error; |
| 1702 | } |
| 1703 | event_notifier_group->buf = buf; |
| 1704 | stream_priv = event_notifier_group; |
| 1705 | ret = lttng_abi_create_stream_fd(notif_file, stream_priv, |
| 1706 | <tng_event_notifier_group_notif_fops, |
| 1707 | "[lttng_event_notifier_stream]"); |
| 1708 | if (ret < 0) |
| 1709 | goto fd_error; |
| 1710 | |
| 1711 | return ret; |
| 1712 | |
| 1713 | fd_error: |
| 1714 | atomic_long_dec(¬if_file->f_count); |
| 1715 | refcount_error: |
| 1716 | event_notifier_group->ops->buffer_read_close(buf); |
| 1717 | return ret; |
| 1718 | } |
| 1719 | |
| 1720 | static |
| 1721 | int lttng_abi_validate_event_param(struct lttng_kernel_event *event_param) |
| 1722 | { |
| 1723 | /* Limit ABI to implemented features. */ |
| 1724 | switch (event_param->instrumentation) { |
| 1725 | case LTTNG_KERNEL_SYSCALL: |
| 1726 | switch (event_param->u.syscall.entryexit) { |
| 1727 | case LTTNG_KERNEL_SYSCALL_ENTRY: |
| 1728 | case LTTNG_KERNEL_SYSCALL_EXIT: |
| 1729 | case LTTNG_KERNEL_SYSCALL_ENTRYEXIT: |
| 1730 | break; |
| 1731 | default: |
| 1732 | return -EINVAL; |
| 1733 | } |
| 1734 | switch (event_param->u.syscall.abi) { |
| 1735 | case LTTNG_KERNEL_SYSCALL_ABI_ALL: |
| 1736 | break; |
| 1737 | default: |
| 1738 | return -EINVAL; |
| 1739 | } |
| 1740 | switch (event_param->u.syscall.match) { |
| 1741 | case LTTNG_KERNEL_SYSCALL_MATCH_NAME: |
| 1742 | break; |
| 1743 | default: |
| 1744 | return -EINVAL; |
| 1745 | } |
| 1746 | break; |
| 1747 | |
| 1748 | case LTTNG_KERNEL_TRACEPOINT: /* Fallthrough */ |
| 1749 | case LTTNG_KERNEL_KPROBE: /* Fallthrough */ |
| 1750 | case LTTNG_KERNEL_KRETPROBE: /* Fallthrough */ |
| 1751 | case LTTNG_KERNEL_NOOP: /* Fallthrough */ |
| 1752 | case LTTNG_KERNEL_UPROBE: |
| 1753 | break; |
| 1754 | |
| 1755 | case LTTNG_KERNEL_FUNCTION: /* Fallthrough */ |
| 1756 | default: |
| 1757 | return -EINVAL; |
| 1758 | } |
| 1759 | return 0; |
| 1760 | } |
| 1761 | |
| 1762 | static |
| 1763 | int lttng_abi_create_event(struct file *channel_file, |
| 1764 | struct lttng_kernel_event *event_param) |
| 1765 | { |
| 1766 | struct lttng_channel *channel = channel_file->private_data; |
| 1767 | int event_fd, ret; |
| 1768 | struct file *event_file; |
| 1769 | void *priv; |
| 1770 | |
| 1771 | event_param->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 1772 | switch (event_param->instrumentation) { |
| 1773 | case LTTNG_KERNEL_KRETPROBE: |
| 1774 | event_param->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 1775 | break; |
| 1776 | case LTTNG_KERNEL_KPROBE: |
| 1777 | event_param->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 1778 | break; |
| 1779 | case LTTNG_KERNEL_FUNCTION: |
| 1780 | WARN_ON_ONCE(1); |
| 1781 | /* Not implemented. */ |
| 1782 | break; |
| 1783 | default: |
| 1784 | break; |
| 1785 | } |
| 1786 | event_fd = lttng_get_unused_fd(); |
| 1787 | if (event_fd < 0) { |
| 1788 | ret = event_fd; |
| 1789 | goto fd_error; |
| 1790 | } |
| 1791 | event_file = anon_inode_getfile("[lttng_event]", |
| 1792 | <tng_event_fops, |
| 1793 | NULL, O_RDWR); |
| 1794 | if (IS_ERR(event_file)) { |
| 1795 | ret = PTR_ERR(event_file); |
| 1796 | goto file_error; |
| 1797 | } |
| 1798 | /* The event holds a reference on the channel */ |
| 1799 | if (!atomic_long_add_unless(&channel_file->f_count, 1, LONG_MAX)) { |
| 1800 | ret = -EOVERFLOW; |
| 1801 | goto refcount_error; |
| 1802 | } |
| 1803 | ret = lttng_abi_validate_event_param(event_param); |
| 1804 | if (ret) |
| 1805 | goto event_error; |
| 1806 | if (event_param->instrumentation == LTTNG_KERNEL_TRACEPOINT |
| 1807 | || event_param->instrumentation == LTTNG_KERNEL_SYSCALL) { |
| 1808 | struct lttng_event_enabler *event_enabler; |
| 1809 | |
| 1810 | if (strutils_is_star_glob_pattern(event_param->name)) { |
| 1811 | /* |
| 1812 | * If the event name is a star globbing pattern, |
| 1813 | * we create the special star globbing enabler. |
| 1814 | */ |
| 1815 | event_enabler = lttng_event_enabler_create(LTTNG_ENABLER_FORMAT_STAR_GLOB, |
| 1816 | event_param, channel); |
| 1817 | } else { |
| 1818 | event_enabler = lttng_event_enabler_create(LTTNG_ENABLER_FORMAT_NAME, |
| 1819 | event_param, channel); |
| 1820 | } |
| 1821 | priv = event_enabler; |
| 1822 | } else { |
| 1823 | struct lttng_event *event; |
| 1824 | |
| 1825 | /* |
| 1826 | * We tolerate no failure path after event creation. It |
| 1827 | * will stay invariant for the rest of the session. |
| 1828 | */ |
| 1829 | event = lttng_event_create(channel, event_param, |
| 1830 | NULL, NULL, |
| 1831 | event_param->instrumentation); |
| 1832 | WARN_ON_ONCE(!event); |
| 1833 | if (IS_ERR(event)) { |
| 1834 | ret = PTR_ERR(event); |
| 1835 | goto event_error; |
| 1836 | } |
| 1837 | priv = event; |
| 1838 | } |
| 1839 | event_file->private_data = priv; |
| 1840 | fd_install(event_fd, event_file); |
| 1841 | return event_fd; |
| 1842 | |
| 1843 | event_error: |
| 1844 | atomic_long_dec(&channel_file->f_count); |
| 1845 | refcount_error: |
| 1846 | fput(event_file); |
| 1847 | file_error: |
| 1848 | put_unused_fd(event_fd); |
| 1849 | fd_error: |
| 1850 | return ret; |
| 1851 | } |
| 1852 | |
| 1853 | static |
| 1854 | long lttng_event_notifier_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 1855 | { |
| 1856 | struct lttng_event_notifier *event_notifier; |
| 1857 | struct lttng_event_notifier_enabler *event_notifier_enabler; |
| 1858 | enum lttng_event_type *evtype = file->private_data; |
| 1859 | |
| 1860 | switch (cmd) { |
| 1861 | case LTTNG_KERNEL_ENABLE: |
| 1862 | switch (*evtype) { |
| 1863 | case LTTNG_TYPE_EVENT: |
| 1864 | event_notifier = file->private_data; |
| 1865 | return lttng_event_notifier_enable(event_notifier); |
| 1866 | case LTTNG_TYPE_ENABLER: |
| 1867 | event_notifier_enabler = file->private_data; |
| 1868 | return lttng_event_notifier_enabler_enable(event_notifier_enabler); |
| 1869 | default: |
| 1870 | WARN_ON_ONCE(1); |
| 1871 | return -ENOSYS; |
| 1872 | } |
| 1873 | case LTTNG_KERNEL_DISABLE: |
| 1874 | switch (*evtype) { |
| 1875 | case LTTNG_TYPE_EVENT: |
| 1876 | event_notifier = file->private_data; |
| 1877 | return lttng_event_notifier_disable(event_notifier); |
| 1878 | case LTTNG_TYPE_ENABLER: |
| 1879 | event_notifier_enabler = file->private_data; |
| 1880 | return lttng_event_notifier_enabler_disable(event_notifier_enabler); |
| 1881 | default: |
| 1882 | WARN_ON_ONCE(1); |
| 1883 | return -ENOSYS; |
| 1884 | } |
| 1885 | case LTTNG_KERNEL_FILTER: |
| 1886 | switch (*evtype) { |
| 1887 | case LTTNG_TYPE_EVENT: |
| 1888 | return -EINVAL; |
| 1889 | case LTTNG_TYPE_ENABLER: |
| 1890 | event_notifier_enabler = file->private_data; |
| 1891 | return lttng_event_notifier_enabler_attach_filter_bytecode( |
| 1892 | event_notifier_enabler, |
| 1893 | (struct lttng_kernel_filter_bytecode __user *) arg); |
| 1894 | default: |
| 1895 | WARN_ON_ONCE(1); |
| 1896 | return -ENOSYS; |
| 1897 | } |
| 1898 | |
| 1899 | case LTTNG_KERNEL_CAPTURE: |
| 1900 | switch (*evtype) { |
| 1901 | case LTTNG_TYPE_EVENT: |
| 1902 | return -EINVAL; |
| 1903 | case LTTNG_TYPE_ENABLER: |
| 1904 | event_notifier_enabler = file->private_data; |
| 1905 | return lttng_event_notifier_enabler_attach_capture_bytecode( |
| 1906 | event_notifier_enabler, |
| 1907 | (struct lttng_kernel_capture_bytecode __user *) arg); |
| 1908 | default: |
| 1909 | WARN_ON_ONCE(1); |
| 1910 | return -ENOSYS; |
| 1911 | } |
| 1912 | case LTTNG_KERNEL_ADD_CALLSITE: |
| 1913 | switch (*evtype) { |
| 1914 | case LTTNG_TYPE_EVENT: |
| 1915 | event_notifier = file->private_data; |
| 1916 | return lttng_event_notifier_add_callsite(event_notifier, |
| 1917 | (struct lttng_kernel_event_callsite __user *) arg); |
| 1918 | case LTTNG_TYPE_ENABLER: |
| 1919 | return -EINVAL; |
| 1920 | default: |
| 1921 | WARN_ON_ONCE(1); |
| 1922 | return -ENOSYS; |
| 1923 | } |
| 1924 | default: |
| 1925 | return -ENOIOCTLCMD; |
| 1926 | } |
| 1927 | } |
| 1928 | |
| 1929 | static |
| 1930 | int lttng_event_notifier_release(struct inode *inode, struct file *file) |
| 1931 | { |
| 1932 | struct lttng_event_notifier *event_notifier; |
| 1933 | struct lttng_event_notifier_enabler *event_notifier_enabler; |
| 1934 | enum lttng_event_type *evtype = file->private_data; |
| 1935 | |
| 1936 | if (!evtype) |
| 1937 | return 0; |
| 1938 | |
| 1939 | switch (*evtype) { |
| 1940 | case LTTNG_TYPE_EVENT: |
| 1941 | event_notifier = file->private_data; |
| 1942 | if (event_notifier) |
| 1943 | fput(event_notifier->group->file); |
| 1944 | break; |
| 1945 | case LTTNG_TYPE_ENABLER: |
| 1946 | event_notifier_enabler = file->private_data; |
| 1947 | if (event_notifier_enabler) |
| 1948 | fput(event_notifier_enabler->group->file); |
| 1949 | break; |
| 1950 | default: |
| 1951 | WARN_ON_ONCE(1); |
| 1952 | break; |
| 1953 | } |
| 1954 | |
| 1955 | return 0; |
| 1956 | } |
| 1957 | |
| 1958 | static const struct file_operations lttng_event_notifier_fops = { |
| 1959 | .owner = THIS_MODULE, |
| 1960 | .release = lttng_event_notifier_release, |
| 1961 | .unlocked_ioctl = lttng_event_notifier_ioctl, |
| 1962 | #ifdef CONFIG_COMPAT |
| 1963 | .compat_ioctl = lttng_event_notifier_ioctl, |
| 1964 | #endif |
| 1965 | }; |
| 1966 | |
| 1967 | static |
| 1968 | int lttng_abi_create_event_notifier(struct file *event_notifier_group_file, |
| 1969 | struct lttng_kernel_event_notifier *event_notifier_param) |
| 1970 | { |
| 1971 | struct lttng_event_notifier_group *event_notifier_group = |
| 1972 | event_notifier_group_file->private_data; |
| 1973 | int event_notifier_fd, ret; |
| 1974 | struct file *event_notifier_file; |
| 1975 | void *priv; |
| 1976 | |
| 1977 | switch (event_notifier_param->event.instrumentation) { |
| 1978 | case LTTNG_KERNEL_TRACEPOINT: |
| 1979 | case LTTNG_KERNEL_UPROBE: |
| 1980 | break; |
| 1981 | case LTTNG_KERNEL_KPROBE: |
| 1982 | event_notifier_param->event.u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 1983 | break; |
| 1984 | case LTTNG_KERNEL_SYSCALL: |
| 1985 | break; |
| 1986 | case LTTNG_KERNEL_KRETPROBE: |
| 1987 | /* Placing an event notifier on kretprobe is not supported. */ |
| 1988 | case LTTNG_KERNEL_FUNCTION: |
| 1989 | case LTTNG_KERNEL_NOOP: |
| 1990 | default: |
| 1991 | ret = -EINVAL; |
| 1992 | goto inval_instr; |
| 1993 | } |
| 1994 | |
| 1995 | event_notifier_param->event.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 1996 | |
| 1997 | event_notifier_fd = lttng_get_unused_fd(); |
| 1998 | if (event_notifier_fd < 0) { |
| 1999 | ret = event_notifier_fd; |
| 2000 | goto fd_error; |
| 2001 | } |
| 2002 | |
| 2003 | event_notifier_file = anon_inode_getfile("[lttng_event_notifier]", |
| 2004 | <tng_event_notifier_fops, |
| 2005 | NULL, O_RDWR); |
| 2006 | if (IS_ERR(event_notifier_file)) { |
| 2007 | ret = PTR_ERR(event_notifier_file); |
| 2008 | goto file_error; |
| 2009 | } |
| 2010 | |
| 2011 | /* The event notifier holds a reference on the event notifier group. */ |
| 2012 | if (!atomic_long_add_unless(&event_notifier_group_file->f_count, 1, LONG_MAX)) { |
| 2013 | ret = -EOVERFLOW; |
| 2014 | goto refcount_error; |
| 2015 | } |
| 2016 | |
| 2017 | if (event_notifier_param->event.instrumentation == LTTNG_KERNEL_TRACEPOINT |
| 2018 | || event_notifier_param->event.instrumentation == LTTNG_KERNEL_SYSCALL) { |
| 2019 | struct lttng_event_notifier_enabler *enabler; |
| 2020 | |
| 2021 | if (strutils_is_star_glob_pattern(event_notifier_param->event.name)) { |
| 2022 | /* |
| 2023 | * If the event name is a star globbing pattern, |
| 2024 | * we create the special star globbing enabler. |
| 2025 | */ |
| 2026 | enabler = lttng_event_notifier_enabler_create( |
| 2027 | event_notifier_group, |
| 2028 | LTTNG_ENABLER_FORMAT_STAR_GLOB, |
| 2029 | event_notifier_param); |
| 2030 | } else { |
| 2031 | enabler = lttng_event_notifier_enabler_create( |
| 2032 | event_notifier_group, |
| 2033 | LTTNG_ENABLER_FORMAT_NAME, |
| 2034 | event_notifier_param); |
| 2035 | } |
| 2036 | priv = enabler; |
| 2037 | } else { |
| 2038 | struct lttng_event_notifier *event_notifier; |
| 2039 | |
| 2040 | /* |
| 2041 | * We tolerate no failure path after event notifier creation. |
| 2042 | * It will stay invariant for the rest of the session. |
| 2043 | */ |
| 2044 | event_notifier = lttng_event_notifier_create(NULL, |
| 2045 | event_notifier_param->event.token, |
| 2046 | event_notifier_param->error_counter_index, |
| 2047 | event_notifier_group, |
| 2048 | event_notifier_param, NULL, |
| 2049 | event_notifier_param->event.instrumentation); |
| 2050 | WARN_ON_ONCE(!event_notifier); |
| 2051 | if (IS_ERR(event_notifier)) { |
| 2052 | ret = PTR_ERR(event_notifier); |
| 2053 | goto event_notifier_error; |
| 2054 | } |
| 2055 | priv = event_notifier; |
| 2056 | } |
| 2057 | event_notifier_file->private_data = priv; |
| 2058 | fd_install(event_notifier_fd, event_notifier_file); |
| 2059 | return event_notifier_fd; |
| 2060 | |
| 2061 | event_notifier_error: |
| 2062 | atomic_long_dec(&event_notifier_group_file->f_count); |
| 2063 | refcount_error: |
| 2064 | fput(event_notifier_file); |
| 2065 | file_error: |
| 2066 | put_unused_fd(event_notifier_fd); |
| 2067 | fd_error: |
| 2068 | inval_instr: |
| 2069 | return ret; |
| 2070 | } |
| 2071 | |
| 2072 | static |
| 2073 | long lttng_abi_event_notifier_group_create_error_counter( |
| 2074 | struct file *event_notifier_group_file, |
| 2075 | const struct lttng_kernel_counter_conf *error_counter_conf) |
| 2076 | { |
| 2077 | int counter_fd, ret; |
| 2078 | char *counter_transport_name; |
| 2079 | size_t counter_len; |
| 2080 | struct lttng_counter *counter = NULL; |
| 2081 | struct file *counter_file; |
| 2082 | struct lttng_event_notifier_group *event_notifier_group = |
| 2083 | (struct lttng_event_notifier_group *) event_notifier_group_file->private_data; |
| 2084 | |
| 2085 | if (error_counter_conf->arithmetic != LTTNG_KERNEL_COUNTER_ARITHMETIC_MODULAR) { |
| 2086 | printk(KERN_ERR "LTTng: event_notifier: Error counter of the wrong arithmetic type.\n"); |
| 2087 | return -EINVAL; |
| 2088 | } |
| 2089 | |
| 2090 | if (error_counter_conf->number_dimensions != 1) { |
| 2091 | printk(KERN_ERR "LTTng: event_notifier: Error counter has more than one dimension.\n"); |
| 2092 | return -EINVAL; |
| 2093 | } |
| 2094 | |
| 2095 | switch (error_counter_conf->bitness) { |
| 2096 | case LTTNG_KERNEL_COUNTER_BITNESS_64: |
| 2097 | counter_transport_name = "counter-per-cpu-64-modular"; |
| 2098 | break; |
| 2099 | case LTTNG_KERNEL_COUNTER_BITNESS_32: |
| 2100 | counter_transport_name = "counter-per-cpu-32-modular"; |
| 2101 | break; |
| 2102 | default: |
| 2103 | return -EINVAL; |
| 2104 | } |
| 2105 | |
| 2106 | /* |
| 2107 | * Lock sessions to provide mutual exclusion against concurrent |
| 2108 | * modification of event_notifier group, which would result in |
| 2109 | * overwriting the error counter if set concurrently. |
| 2110 | */ |
| 2111 | lttng_lock_sessions(); |
| 2112 | |
| 2113 | if (event_notifier_group->error_counter) { |
| 2114 | printk(KERN_ERR "Error counter already created in event_notifier group\n"); |
| 2115 | ret = -EBUSY; |
| 2116 | goto fd_error; |
| 2117 | } |
| 2118 | |
| 2119 | counter_fd = lttng_get_unused_fd(); |
| 2120 | if (counter_fd < 0) { |
| 2121 | ret = counter_fd; |
| 2122 | goto fd_error; |
| 2123 | } |
| 2124 | |
| 2125 | counter_file = anon_inode_getfile("[lttng_counter]", |
| 2126 | <tng_counter_fops, |
| 2127 | NULL, O_RDONLY); |
| 2128 | if (IS_ERR(counter_file)) { |
| 2129 | ret = PTR_ERR(counter_file); |
| 2130 | goto file_error; |
| 2131 | } |
| 2132 | |
| 2133 | counter_len = error_counter_conf->dimensions[0].size; |
| 2134 | |
| 2135 | if (!atomic_long_add_unless(&event_notifier_group_file->f_count, 1, LONG_MAX)) { |
| 2136 | ret = -EOVERFLOW; |
| 2137 | goto refcount_error; |
| 2138 | } |
| 2139 | |
| 2140 | counter = lttng_kernel_counter_create(counter_transport_name, |
| 2141 | 1, &counter_len); |
| 2142 | if (!counter) { |
| 2143 | ret = -EINVAL; |
| 2144 | goto counter_error; |
| 2145 | } |
| 2146 | |
| 2147 | event_notifier_group->error_counter_len = counter_len; |
| 2148 | /* |
| 2149 | * store-release to publish error counter matches load-acquire |
| 2150 | * in record_error. Ensures the counter is created and the |
| 2151 | * error_counter_len is set before they are used. |
| 2152 | */ |
| 2153 | smp_store_release(&event_notifier_group->error_counter, counter); |
| 2154 | |
| 2155 | counter->file = counter_file; |
| 2156 | counter->owner = event_notifier_group->file; |
| 2157 | counter_file->private_data = counter; |
| 2158 | /* Ownership transferred. */ |
| 2159 | counter = NULL; |
| 2160 | |
| 2161 | fd_install(counter_fd, counter_file); |
| 2162 | lttng_unlock_sessions(); |
| 2163 | |
| 2164 | return counter_fd; |
| 2165 | |
| 2166 | counter_error: |
| 2167 | atomic_long_dec(&event_notifier_group_file->f_count); |
| 2168 | refcount_error: |
| 2169 | fput(counter_file); |
| 2170 | file_error: |
| 2171 | put_unused_fd(counter_fd); |
| 2172 | fd_error: |
| 2173 | lttng_unlock_sessions(); |
| 2174 | return ret; |
| 2175 | } |
| 2176 | |
| 2177 | static |
| 2178 | long lttng_event_notifier_group_ioctl(struct file *file, unsigned int cmd, |
| 2179 | unsigned long arg) |
| 2180 | { |
| 2181 | switch (cmd) { |
| 2182 | case LTTNG_KERNEL_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD: |
| 2183 | { |
| 2184 | return lttng_abi_open_event_notifier_group_stream(file); |
| 2185 | } |
| 2186 | case LTTNG_KERNEL_EVENT_NOTIFIER_CREATE: |
| 2187 | { |
| 2188 | struct lttng_kernel_event_notifier uevent_notifier_param; |
| 2189 | |
| 2190 | if (copy_from_user(&uevent_notifier_param, |
| 2191 | (struct lttng_kernel_event_notifier __user *) arg, |
| 2192 | sizeof(uevent_notifier_param))) |
| 2193 | return -EFAULT; |
| 2194 | return lttng_abi_create_event_notifier(file, &uevent_notifier_param); |
| 2195 | } |
| 2196 | case LTTNG_KERNEL_COUNTER: |
| 2197 | { |
| 2198 | struct lttng_kernel_counter_conf uerror_counter_conf; |
| 2199 | |
| 2200 | if (copy_from_user(&uerror_counter_conf, |
| 2201 | (struct lttng_kernel_counter_conf __user *) arg, |
| 2202 | sizeof(uerror_counter_conf))) |
| 2203 | return -EFAULT; |
| 2204 | return lttng_abi_event_notifier_group_create_error_counter(file, |
| 2205 | &uerror_counter_conf); |
| 2206 | } |
| 2207 | default: |
| 2208 | return -ENOIOCTLCMD; |
| 2209 | } |
| 2210 | return 0; |
| 2211 | } |
| 2212 | |
| 2213 | static |
| 2214 | int lttng_event_notifier_group_release(struct inode *inode, struct file *file) |
| 2215 | { |
| 2216 | struct lttng_event_notifier_group *event_notifier_group = |
| 2217 | file->private_data; |
| 2218 | |
| 2219 | if (event_notifier_group) |
| 2220 | lttng_event_notifier_group_destroy(event_notifier_group); |
| 2221 | return 0; |
| 2222 | } |
| 2223 | |
| 2224 | static const struct file_operations lttng_event_notifier_group_fops = { |
| 2225 | .owner = THIS_MODULE, |
| 2226 | .release = lttng_event_notifier_group_release, |
| 2227 | .unlocked_ioctl = lttng_event_notifier_group_ioctl, |
| 2228 | #ifdef CONFIG_COMPAT |
| 2229 | .compat_ioctl = lttng_event_notifier_group_ioctl, |
| 2230 | #endif |
| 2231 | }; |
| 2232 | |
| 2233 | /** |
| 2234 | * lttng_channel_ioctl - lttng syscall through ioctl |
| 2235 | * |
| 2236 | * @file: the file |
| 2237 | * @cmd: the command |
| 2238 | * @arg: command arg |
| 2239 | * |
| 2240 | * This ioctl implements lttng commands: |
| 2241 | * LTTNG_KERNEL_STREAM |
| 2242 | * Returns an event stream file descriptor or failure. |
| 2243 | * (typically, one event stream records events from one CPU) |
| 2244 | * LTTNG_KERNEL_EVENT |
| 2245 | * Returns an event file descriptor or failure. |
| 2246 | * LTTNG_KERNEL_CONTEXT |
| 2247 | * Prepend a context field to each event in the channel |
| 2248 | * LTTNG_KERNEL_ENABLE |
| 2249 | * Enable recording for events in this channel (weak enable) |
| 2250 | * LTTNG_KERNEL_DISABLE |
| 2251 | * Disable recording for events in this channel (strong disable) |
| 2252 | * |
| 2253 | * Channel and event file descriptors also hold a reference on the session. |
| 2254 | */ |
| 2255 | static |
| 2256 | long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 2257 | { |
| 2258 | struct lttng_channel *channel = file->private_data; |
| 2259 | |
| 2260 | switch (cmd) { |
| 2261 | case LTTNG_KERNEL_OLD_STREAM: |
| 2262 | case LTTNG_KERNEL_STREAM: |
| 2263 | return lttng_abi_open_stream(file); |
| 2264 | case LTTNG_KERNEL_OLD_EVENT: |
| 2265 | { |
| 2266 | struct lttng_kernel_event *uevent_param; |
| 2267 | struct lttng_kernel_old_event *old_uevent_param; |
| 2268 | int ret; |
| 2269 | |
| 2270 | uevent_param = kmalloc(sizeof(struct lttng_kernel_event), |
| 2271 | GFP_KERNEL); |
| 2272 | if (!uevent_param) { |
| 2273 | ret = -ENOMEM; |
| 2274 | goto old_event_end; |
| 2275 | } |
| 2276 | old_uevent_param = kmalloc( |
| 2277 | sizeof(struct lttng_kernel_old_event), |
| 2278 | GFP_KERNEL); |
| 2279 | if (!old_uevent_param) { |
| 2280 | ret = -ENOMEM; |
| 2281 | goto old_event_error_free_param; |
| 2282 | } |
| 2283 | if (copy_from_user(old_uevent_param, |
| 2284 | (struct lttng_kernel_old_event __user *) arg, |
| 2285 | sizeof(struct lttng_kernel_old_event))) { |
| 2286 | ret = -EFAULT; |
| 2287 | goto old_event_error_free_old_param; |
| 2288 | } |
| 2289 | |
| 2290 | memcpy(uevent_param->name, old_uevent_param->name, |
| 2291 | sizeof(uevent_param->name)); |
| 2292 | uevent_param->instrumentation = |
| 2293 | old_uevent_param->instrumentation; |
| 2294 | |
| 2295 | switch (old_uevent_param->instrumentation) { |
| 2296 | case LTTNG_KERNEL_KPROBE: |
| 2297 | uevent_param->u.kprobe.addr = |
| 2298 | old_uevent_param->u.kprobe.addr; |
| 2299 | uevent_param->u.kprobe.offset = |
| 2300 | old_uevent_param->u.kprobe.offset; |
| 2301 | memcpy(uevent_param->u.kprobe.symbol_name, |
| 2302 | old_uevent_param->u.kprobe.symbol_name, |
| 2303 | sizeof(uevent_param->u.kprobe.symbol_name)); |
| 2304 | break; |
| 2305 | case LTTNG_KERNEL_KRETPROBE: |
| 2306 | uevent_param->u.kretprobe.addr = |
| 2307 | old_uevent_param->u.kretprobe.addr; |
| 2308 | uevent_param->u.kretprobe.offset = |
| 2309 | old_uevent_param->u.kretprobe.offset; |
| 2310 | memcpy(uevent_param->u.kretprobe.symbol_name, |
| 2311 | old_uevent_param->u.kretprobe.symbol_name, |
| 2312 | sizeof(uevent_param->u.kretprobe.symbol_name)); |
| 2313 | break; |
| 2314 | case LTTNG_KERNEL_FUNCTION: |
| 2315 | WARN_ON_ONCE(1); |
| 2316 | /* Not implemented. */ |
| 2317 | break; |
| 2318 | default: |
| 2319 | break; |
| 2320 | } |
| 2321 | ret = lttng_abi_create_event(file, uevent_param); |
| 2322 | |
| 2323 | old_event_error_free_old_param: |
| 2324 | kfree(old_uevent_param); |
| 2325 | old_event_error_free_param: |
| 2326 | kfree(uevent_param); |
| 2327 | old_event_end: |
| 2328 | return ret; |
| 2329 | } |
| 2330 | case LTTNG_KERNEL_EVENT: |
| 2331 | { |
| 2332 | struct lttng_kernel_event uevent_param; |
| 2333 | |
| 2334 | if (copy_from_user(&uevent_param, |
| 2335 | (struct lttng_kernel_event __user *) arg, |
| 2336 | sizeof(uevent_param))) |
| 2337 | return -EFAULT; |
| 2338 | return lttng_abi_create_event(file, &uevent_param); |
| 2339 | } |
| 2340 | case LTTNG_KERNEL_OLD_CONTEXT: |
| 2341 | { |
| 2342 | struct lttng_kernel_context *ucontext_param; |
| 2343 | struct lttng_kernel_old_context *old_ucontext_param; |
| 2344 | int ret; |
| 2345 | |
| 2346 | ucontext_param = kmalloc(sizeof(struct lttng_kernel_context), |
| 2347 | GFP_KERNEL); |
| 2348 | if (!ucontext_param) { |
| 2349 | ret = -ENOMEM; |
| 2350 | goto old_ctx_end; |
| 2351 | } |
| 2352 | old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context), |
| 2353 | GFP_KERNEL); |
| 2354 | if (!old_ucontext_param) { |
| 2355 | ret = -ENOMEM; |
| 2356 | goto old_ctx_error_free_param; |
| 2357 | } |
| 2358 | |
| 2359 | if (copy_from_user(old_ucontext_param, |
| 2360 | (struct lttng_kernel_old_context __user *) arg, |
| 2361 | sizeof(struct lttng_kernel_old_context))) { |
| 2362 | ret = -EFAULT; |
| 2363 | goto old_ctx_error_free_old_param; |
| 2364 | } |
| 2365 | ucontext_param->ctx = old_ucontext_param->ctx; |
| 2366 | memcpy(ucontext_param->padding, old_ucontext_param->padding, |
| 2367 | sizeof(ucontext_param->padding)); |
| 2368 | /* only type that uses the union */ |
| 2369 | if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) { |
| 2370 | ucontext_param->u.perf_counter.type = |
| 2371 | old_ucontext_param->u.perf_counter.type; |
| 2372 | ucontext_param->u.perf_counter.config = |
| 2373 | old_ucontext_param->u.perf_counter.config; |
| 2374 | memcpy(ucontext_param->u.perf_counter.name, |
| 2375 | old_ucontext_param->u.perf_counter.name, |
| 2376 | sizeof(ucontext_param->u.perf_counter.name)); |
| 2377 | } |
| 2378 | |
| 2379 | ret = lttng_abi_add_context(file, |
| 2380 | ucontext_param, |
| 2381 | &channel->ctx, channel->session); |
| 2382 | |
| 2383 | old_ctx_error_free_old_param: |
| 2384 | kfree(old_ucontext_param); |
| 2385 | old_ctx_error_free_param: |
| 2386 | kfree(ucontext_param); |
| 2387 | old_ctx_end: |
| 2388 | return ret; |
| 2389 | } |
| 2390 | case LTTNG_KERNEL_CONTEXT: |
| 2391 | { |
| 2392 | struct lttng_kernel_context ucontext_param; |
| 2393 | |
| 2394 | if (copy_from_user(&ucontext_param, |
| 2395 | (struct lttng_kernel_context __user *) arg, |
| 2396 | sizeof(ucontext_param))) |
| 2397 | return -EFAULT; |
| 2398 | return lttng_abi_add_context(file, |
| 2399 | &ucontext_param, |
| 2400 | &channel->ctx, channel->session); |
| 2401 | } |
| 2402 | case LTTNG_KERNEL_OLD_ENABLE: |
| 2403 | case LTTNG_KERNEL_ENABLE: |
| 2404 | return lttng_channel_enable(channel); |
| 2405 | case LTTNG_KERNEL_OLD_DISABLE: |
| 2406 | case LTTNG_KERNEL_DISABLE: |
| 2407 | return lttng_channel_disable(channel); |
| 2408 | case LTTNG_KERNEL_SYSCALL_MASK: |
| 2409 | return lttng_channel_syscall_mask(channel, |
| 2410 | (struct lttng_kernel_syscall_mask __user *) arg); |
| 2411 | default: |
| 2412 | return -ENOIOCTLCMD; |
| 2413 | } |
| 2414 | } |
| 2415 | |
| 2416 | /** |
| 2417 | * lttng_metadata_ioctl - lttng syscall through ioctl |
| 2418 | * |
| 2419 | * @file: the file |
| 2420 | * @cmd: the command |
| 2421 | * @arg: command arg |
| 2422 | * |
| 2423 | * This ioctl implements lttng commands: |
| 2424 | * LTTNG_KERNEL_STREAM |
| 2425 | * Returns an event stream file descriptor or failure. |
| 2426 | * |
| 2427 | * Channel and event file descriptors also hold a reference on the session. |
| 2428 | */ |
| 2429 | static |
| 2430 | long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 2431 | { |
| 2432 | switch (cmd) { |
| 2433 | case LTTNG_KERNEL_OLD_STREAM: |
| 2434 | case LTTNG_KERNEL_STREAM: |
| 2435 | return lttng_abi_open_metadata_stream(file); |
| 2436 | default: |
| 2437 | return -ENOIOCTLCMD; |
| 2438 | } |
| 2439 | } |
| 2440 | |
| 2441 | /** |
| 2442 | * lttng_channel_poll - lttng stream addition/removal monitoring |
| 2443 | * |
| 2444 | * @file: the file |
| 2445 | * @wait: poll table |
| 2446 | */ |
| 2447 | unsigned int lttng_channel_poll(struct file *file, poll_table *wait) |
| 2448 | { |
| 2449 | struct lttng_channel *channel = file->private_data; |
| 2450 | unsigned int mask = 0; |
| 2451 | |
| 2452 | if (file->f_mode & FMODE_READ) { |
| 2453 | poll_wait_set_exclusive(wait); |
| 2454 | poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan), |
| 2455 | wait); |
| 2456 | |
| 2457 | if (channel->ops->is_disabled(channel->chan)) |
| 2458 | return POLLERR; |
| 2459 | if (channel->ops->is_finalized(channel->chan)) |
| 2460 | return POLLHUP; |
| 2461 | if (channel->ops->buffer_has_read_closed_stream(channel->chan)) |
| 2462 | return POLLIN | POLLRDNORM; |
| 2463 | return 0; |
| 2464 | } |
| 2465 | return mask; |
| 2466 | |
| 2467 | } |
| 2468 | |
| 2469 | static |
| 2470 | int lttng_channel_release(struct inode *inode, struct file *file) |
| 2471 | { |
| 2472 | struct lttng_channel *channel = file->private_data; |
| 2473 | |
| 2474 | if (channel) |
| 2475 | fput(channel->session->file); |
| 2476 | return 0; |
| 2477 | } |
| 2478 | |
| 2479 | static |
| 2480 | int lttng_metadata_channel_release(struct inode *inode, struct file *file) |
| 2481 | { |
| 2482 | struct lttng_channel *channel = file->private_data; |
| 2483 | |
| 2484 | if (channel) { |
| 2485 | fput(channel->session->file); |
| 2486 | lttng_metadata_channel_destroy(channel); |
| 2487 | } |
| 2488 | |
| 2489 | return 0; |
| 2490 | } |
| 2491 | |
| 2492 | static const struct file_operations lttng_channel_fops = { |
| 2493 | .owner = THIS_MODULE, |
| 2494 | .release = lttng_channel_release, |
| 2495 | .poll = lttng_channel_poll, |
| 2496 | .unlocked_ioctl = lttng_channel_ioctl, |
| 2497 | #ifdef CONFIG_COMPAT |
| 2498 | .compat_ioctl = lttng_channel_ioctl, |
| 2499 | #endif |
| 2500 | }; |
| 2501 | |
| 2502 | static const struct file_operations lttng_metadata_fops = { |
| 2503 | .owner = THIS_MODULE, |
| 2504 | .release = lttng_metadata_channel_release, |
| 2505 | .unlocked_ioctl = lttng_metadata_ioctl, |
| 2506 | #ifdef CONFIG_COMPAT |
| 2507 | .compat_ioctl = lttng_metadata_ioctl, |
| 2508 | #endif |
| 2509 | }; |
| 2510 | |
| 2511 | /** |
| 2512 | * lttng_event_ioctl - lttng syscall through ioctl |
| 2513 | * |
| 2514 | * @file: the file |
| 2515 | * @cmd: the command |
| 2516 | * @arg: command arg |
| 2517 | * |
| 2518 | * This ioctl implements lttng commands: |
| 2519 | * LTTNG_KERNEL_CONTEXT |
| 2520 | * Prepend a context field to each record of this event |
| 2521 | * LTTNG_KERNEL_ENABLE |
| 2522 | * Enable recording for this event (weak enable) |
| 2523 | * LTTNG_KERNEL_DISABLE |
| 2524 | * Disable recording for this event (strong disable) |
| 2525 | */ |
| 2526 | static |
| 2527 | long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 2528 | { |
| 2529 | struct lttng_event *event; |
| 2530 | struct lttng_event_enabler *event_enabler; |
| 2531 | enum lttng_event_type *evtype = file->private_data; |
| 2532 | |
| 2533 | switch (cmd) { |
| 2534 | case LTTNG_KERNEL_OLD_CONTEXT: |
| 2535 | { |
| 2536 | /* Not implemented */ |
| 2537 | return -ENOSYS; |
| 2538 | } |
| 2539 | case LTTNG_KERNEL_CONTEXT: |
| 2540 | { |
| 2541 | /* Not implemented */ |
| 2542 | return -ENOSYS; |
| 2543 | } |
| 2544 | case LTTNG_KERNEL_OLD_ENABLE: |
| 2545 | case LTTNG_KERNEL_ENABLE: |
| 2546 | switch (*evtype) { |
| 2547 | case LTTNG_TYPE_EVENT: |
| 2548 | event = file->private_data; |
| 2549 | return lttng_event_enable(event); |
| 2550 | case LTTNG_TYPE_ENABLER: |
| 2551 | event_enabler = file->private_data; |
| 2552 | return lttng_event_enabler_enable(event_enabler); |
| 2553 | default: |
| 2554 | WARN_ON_ONCE(1); |
| 2555 | return -ENOSYS; |
| 2556 | } |
| 2557 | case LTTNG_KERNEL_OLD_DISABLE: |
| 2558 | case LTTNG_KERNEL_DISABLE: |
| 2559 | switch (*evtype) { |
| 2560 | case LTTNG_TYPE_EVENT: |
| 2561 | event = file->private_data; |
| 2562 | return lttng_event_disable(event); |
| 2563 | case LTTNG_TYPE_ENABLER: |
| 2564 | event_enabler = file->private_data; |
| 2565 | return lttng_event_enabler_disable(event_enabler); |
| 2566 | default: |
| 2567 | WARN_ON_ONCE(1); |
| 2568 | return -ENOSYS; |
| 2569 | } |
| 2570 | case LTTNG_KERNEL_FILTER: |
| 2571 | switch (*evtype) { |
| 2572 | case LTTNG_TYPE_EVENT: |
| 2573 | return -EINVAL; |
| 2574 | case LTTNG_TYPE_ENABLER: |
| 2575 | { |
| 2576 | event_enabler = file->private_data; |
| 2577 | return lttng_event_enabler_attach_filter_bytecode( |
| 2578 | event_enabler, |
| 2579 | (struct lttng_kernel_filter_bytecode __user *) arg); |
| 2580 | } |
| 2581 | default: |
| 2582 | WARN_ON_ONCE(1); |
| 2583 | return -ENOSYS; |
| 2584 | } |
| 2585 | case LTTNG_KERNEL_ADD_CALLSITE: |
| 2586 | switch (*evtype) { |
| 2587 | case LTTNG_TYPE_EVENT: |
| 2588 | event = file->private_data; |
| 2589 | return lttng_event_add_callsite(event, |
| 2590 | (struct lttng_kernel_event_callsite __user *) arg); |
| 2591 | case LTTNG_TYPE_ENABLER: |
| 2592 | return -EINVAL; |
| 2593 | default: |
| 2594 | WARN_ON_ONCE(1); |
| 2595 | return -ENOSYS; |
| 2596 | } |
| 2597 | default: |
| 2598 | return -ENOIOCTLCMD; |
| 2599 | } |
| 2600 | } |
| 2601 | |
| 2602 | static |
| 2603 | int lttng_event_release(struct inode *inode, struct file *file) |
| 2604 | { |
| 2605 | struct lttng_event *event; |
| 2606 | struct lttng_event_enabler *event_enabler; |
| 2607 | enum lttng_event_type *evtype = file->private_data; |
| 2608 | |
| 2609 | if (!evtype) |
| 2610 | return 0; |
| 2611 | |
| 2612 | switch (*evtype) { |
| 2613 | case LTTNG_TYPE_EVENT: |
| 2614 | event = file->private_data; |
| 2615 | if (event) |
| 2616 | fput(event->chan->file); |
| 2617 | break; |
| 2618 | case LTTNG_TYPE_ENABLER: |
| 2619 | event_enabler = file->private_data; |
| 2620 | if (event_enabler) |
| 2621 | fput(event_enabler->chan->file); |
| 2622 | break; |
| 2623 | default: |
| 2624 | WARN_ON_ONCE(1); |
| 2625 | break; |
| 2626 | } |
| 2627 | |
| 2628 | return 0; |
| 2629 | } |
| 2630 | |
| 2631 | /* TODO: filter control ioctl */ |
| 2632 | static const struct file_operations lttng_event_fops = { |
| 2633 | .owner = THIS_MODULE, |
| 2634 | .release = lttng_event_release, |
| 2635 | .unlocked_ioctl = lttng_event_ioctl, |
| 2636 | #ifdef CONFIG_COMPAT |
| 2637 | .compat_ioctl = lttng_event_ioctl, |
| 2638 | #endif |
| 2639 | }; |
| 2640 | |
| 2641 | static int put_u64(uint64_t val, unsigned long arg) |
| 2642 | { |
| 2643 | return put_user(val, (uint64_t __user *) arg); |
| 2644 | } |
| 2645 | |
| 2646 | static int put_u32(uint32_t val, unsigned long arg) |
| 2647 | { |
| 2648 | return put_user(val, (uint32_t __user *) arg); |
| 2649 | } |
| 2650 | |
| 2651 | static long lttng_stream_ring_buffer_ioctl(struct file *filp, |
| 2652 | unsigned int cmd, unsigned long arg) |
| 2653 | { |
| 2654 | struct lib_ring_buffer *buf = filp->private_data; |
| 2655 | struct channel *chan = buf->backend.chan; |
| 2656 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 2657 | const struct lttng_channel_ops *ops = chan->backend.priv_ops; |
| 2658 | int ret; |
| 2659 | |
| 2660 | if (atomic_read(&chan->record_disabled)) |
| 2661 | return -EIO; |
| 2662 | |
| 2663 | switch (cmd) { |
| 2664 | case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN: |
| 2665 | { |
| 2666 | uint64_t ts; |
| 2667 | |
| 2668 | ret = ops->timestamp_begin(config, buf, &ts); |
| 2669 | if (ret < 0) |
| 2670 | goto error; |
| 2671 | return put_u64(ts, arg); |
| 2672 | } |
| 2673 | case LTTNG_RING_BUFFER_GET_TIMESTAMP_END: |
| 2674 | { |
| 2675 | uint64_t ts; |
| 2676 | |
| 2677 | ret = ops->timestamp_end(config, buf, &ts); |
| 2678 | if (ret < 0) |
| 2679 | goto error; |
| 2680 | return put_u64(ts, arg); |
| 2681 | } |
| 2682 | case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED: |
| 2683 | { |
| 2684 | uint64_t ed; |
| 2685 | |
| 2686 | ret = ops->events_discarded(config, buf, &ed); |
| 2687 | if (ret < 0) |
| 2688 | goto error; |
| 2689 | return put_u64(ed, arg); |
| 2690 | } |
| 2691 | case LTTNG_RING_BUFFER_GET_CONTENT_SIZE: |
| 2692 | { |
| 2693 | uint64_t cs; |
| 2694 | |
| 2695 | ret = ops->content_size(config, buf, &cs); |
| 2696 | if (ret < 0) |
| 2697 | goto error; |
| 2698 | return put_u64(cs, arg); |
| 2699 | } |
| 2700 | case LTTNG_RING_BUFFER_GET_PACKET_SIZE: |
| 2701 | { |
| 2702 | uint64_t ps; |
| 2703 | |
| 2704 | ret = ops->packet_size(config, buf, &ps); |
| 2705 | if (ret < 0) |
| 2706 | goto error; |
| 2707 | return put_u64(ps, arg); |
| 2708 | } |
| 2709 | case LTTNG_RING_BUFFER_GET_STREAM_ID: |
| 2710 | { |
| 2711 | uint64_t si; |
| 2712 | |
| 2713 | ret = ops->stream_id(config, buf, &si); |
| 2714 | if (ret < 0) |
| 2715 | goto error; |
| 2716 | return put_u64(si, arg); |
| 2717 | } |
| 2718 | case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP: |
| 2719 | { |
| 2720 | uint64_t ts; |
| 2721 | |
| 2722 | ret = ops->current_timestamp(config, buf, &ts); |
| 2723 | if (ret < 0) |
| 2724 | goto error; |
| 2725 | return put_u64(ts, arg); |
| 2726 | } |
| 2727 | case LTTNG_RING_BUFFER_GET_SEQ_NUM: |
| 2728 | { |
| 2729 | uint64_t seq; |
| 2730 | |
| 2731 | ret = ops->sequence_number(config, buf, &seq); |
| 2732 | if (ret < 0) |
| 2733 | goto error; |
| 2734 | return put_u64(seq, arg); |
| 2735 | } |
| 2736 | case LTTNG_RING_BUFFER_INSTANCE_ID: |
| 2737 | { |
| 2738 | uint64_t id; |
| 2739 | |
| 2740 | ret = ops->instance_id(config, buf, &id); |
| 2741 | if (ret < 0) |
| 2742 | goto error; |
| 2743 | return put_u64(id, arg); |
| 2744 | } |
| 2745 | default: |
| 2746 | return lib_ring_buffer_file_operations.unlocked_ioctl(filp, |
| 2747 | cmd, arg); |
| 2748 | } |
| 2749 | |
| 2750 | error: |
| 2751 | return -ENOSYS; |
| 2752 | } |
| 2753 | |
| 2754 | #ifdef CONFIG_COMPAT |
| 2755 | static long lttng_stream_ring_buffer_compat_ioctl(struct file *filp, |
| 2756 | unsigned int cmd, unsigned long arg) |
| 2757 | { |
| 2758 | struct lib_ring_buffer *buf = filp->private_data; |
| 2759 | struct channel *chan = buf->backend.chan; |
| 2760 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 2761 | const struct lttng_channel_ops *ops = chan->backend.priv_ops; |
| 2762 | int ret; |
| 2763 | |
| 2764 | if (atomic_read(&chan->record_disabled)) |
| 2765 | return -EIO; |
| 2766 | |
| 2767 | switch (cmd) { |
| 2768 | case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN: |
| 2769 | { |
| 2770 | uint64_t ts; |
| 2771 | |
| 2772 | ret = ops->timestamp_begin(config, buf, &ts); |
| 2773 | if (ret < 0) |
| 2774 | goto error; |
| 2775 | return put_u64(ts, arg); |
| 2776 | } |
| 2777 | case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END: |
| 2778 | { |
| 2779 | uint64_t ts; |
| 2780 | |
| 2781 | ret = ops->timestamp_end(config, buf, &ts); |
| 2782 | if (ret < 0) |
| 2783 | goto error; |
| 2784 | return put_u64(ts, arg); |
| 2785 | } |
| 2786 | case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED: |
| 2787 | { |
| 2788 | uint64_t ed; |
| 2789 | |
| 2790 | ret = ops->events_discarded(config, buf, &ed); |
| 2791 | if (ret < 0) |
| 2792 | goto error; |
| 2793 | return put_u64(ed, arg); |
| 2794 | } |
| 2795 | case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE: |
| 2796 | { |
| 2797 | uint64_t cs; |
| 2798 | |
| 2799 | ret = ops->content_size(config, buf, &cs); |
| 2800 | if (ret < 0) |
| 2801 | goto error; |
| 2802 | return put_u64(cs, arg); |
| 2803 | } |
| 2804 | case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE: |
| 2805 | { |
| 2806 | uint64_t ps; |
| 2807 | |
| 2808 | ret = ops->packet_size(config, buf, &ps); |
| 2809 | if (ret < 0) |
| 2810 | goto error; |
| 2811 | return put_u64(ps, arg); |
| 2812 | } |
| 2813 | case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID: |
| 2814 | { |
| 2815 | uint64_t si; |
| 2816 | |
| 2817 | ret = ops->stream_id(config, buf, &si); |
| 2818 | if (ret < 0) |
| 2819 | goto error; |
| 2820 | return put_u64(si, arg); |
| 2821 | } |
| 2822 | case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP: |
| 2823 | { |
| 2824 | uint64_t ts; |
| 2825 | |
| 2826 | ret = ops->current_timestamp(config, buf, &ts); |
| 2827 | if (ret < 0) |
| 2828 | goto error; |
| 2829 | return put_u64(ts, arg); |
| 2830 | } |
| 2831 | case LTTNG_RING_BUFFER_COMPAT_GET_SEQ_NUM: |
| 2832 | { |
| 2833 | uint64_t seq; |
| 2834 | |
| 2835 | ret = ops->sequence_number(config, buf, &seq); |
| 2836 | if (ret < 0) |
| 2837 | goto error; |
| 2838 | return put_u64(seq, arg); |
| 2839 | } |
| 2840 | case LTTNG_RING_BUFFER_COMPAT_INSTANCE_ID: |
| 2841 | { |
| 2842 | uint64_t id; |
| 2843 | |
| 2844 | ret = ops->instance_id(config, buf, &id); |
| 2845 | if (ret < 0) |
| 2846 | goto error; |
| 2847 | return put_u64(id, arg); |
| 2848 | } |
| 2849 | default: |
| 2850 | return lib_ring_buffer_file_operations.compat_ioctl(filp, |
| 2851 | cmd, arg); |
| 2852 | } |
| 2853 | |
| 2854 | error: |
| 2855 | return -ENOSYS; |
| 2856 | } |
| 2857 | #endif /* CONFIG_COMPAT */ |
| 2858 | |
| 2859 | static void lttng_stream_override_ring_buffer_fops(void) |
| 2860 | { |
| 2861 | lttng_stream_ring_buffer_file_operations.owner = THIS_MODULE; |
| 2862 | lttng_stream_ring_buffer_file_operations.open = |
| 2863 | lib_ring_buffer_file_operations.open; |
| 2864 | lttng_stream_ring_buffer_file_operations.release = |
| 2865 | lib_ring_buffer_file_operations.release; |
| 2866 | lttng_stream_ring_buffer_file_operations.poll = |
| 2867 | lib_ring_buffer_file_operations.poll; |
| 2868 | lttng_stream_ring_buffer_file_operations.splice_read = |
| 2869 | lib_ring_buffer_file_operations.splice_read; |
| 2870 | lttng_stream_ring_buffer_file_operations.mmap = |
| 2871 | lib_ring_buffer_file_operations.mmap; |
| 2872 | lttng_stream_ring_buffer_file_operations.unlocked_ioctl = |
| 2873 | lttng_stream_ring_buffer_ioctl; |
| 2874 | lttng_stream_ring_buffer_file_operations.llseek = |
| 2875 | lib_ring_buffer_file_operations.llseek; |
| 2876 | #ifdef CONFIG_COMPAT |
| 2877 | lttng_stream_ring_buffer_file_operations.compat_ioctl = |
| 2878 | lttng_stream_ring_buffer_compat_ioctl; |
| 2879 | #endif |
| 2880 | } |
| 2881 | |
| 2882 | int __init lttng_abi_init(void) |
| 2883 | { |
| 2884 | int ret = 0; |
| 2885 | |
| 2886 | wrapper_vmalloc_sync_mappings(); |
| 2887 | lttng_clock_ref(); |
| 2888 | |
| 2889 | ret = lttng_tp_mempool_init(); |
| 2890 | if (ret) { |
| 2891 | goto error; |
| 2892 | } |
| 2893 | |
| 2894 | lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL, |
| 2895 | <tng_proc_ops, NULL); |
| 2896 | |
| 2897 | if (!lttng_proc_dentry) { |
| 2898 | printk(KERN_ERR "LTTng: Error creating control file\n"); |
| 2899 | ret = -ENOMEM; |
| 2900 | goto error; |
| 2901 | } |
| 2902 | lttng_stream_override_ring_buffer_fops(); |
| 2903 | return 0; |
| 2904 | |
| 2905 | error: |
| 2906 | lttng_tp_mempool_destroy(); |
| 2907 | lttng_clock_unref(); |
| 2908 | return ret; |
| 2909 | } |
| 2910 | |
| 2911 | /* No __exit annotation because used by init error path too. */ |
| 2912 | void lttng_abi_exit(void) |
| 2913 | { |
| 2914 | lttng_tp_mempool_destroy(); |
| 2915 | lttng_clock_unref(); |
| 2916 | if (lttng_proc_dentry) |
| 2917 | remove_proc_entry("lttng", NULL); |
| 2918 | } |