| 1 | /* |
| 2 | * lttng-abi.c |
| 3 | * |
| 4 | * LTTng ABI |
| 5 | * |
| 6 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 7 | * |
| 8 | * This library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public |
| 10 | * License as published by the Free Software Foundation; only |
| 11 | * version 2.1 of the License. |
| 12 | * |
| 13 | * This library is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * Lesser General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public |
| 19 | * License along with this library; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | * |
| 22 | * |
| 23 | * Mimic system calls for: |
| 24 | * - session creation, returns a file descriptor or failure. |
| 25 | * - channel creation, returns a file descriptor or failure. |
| 26 | * - Operates on a session file descriptor |
| 27 | * - Takes all channel options as parameters. |
| 28 | * - stream get, returns a file descriptor or failure. |
| 29 | * - Operates on a channel file descriptor. |
| 30 | * - stream notifier get, returns a file descriptor or failure. |
| 31 | * - Operates on a channel file descriptor. |
| 32 | * - event creation, returns a file descriptor or failure. |
| 33 | * - Operates on a channel file descriptor |
| 34 | * - Takes an event name as parameter |
| 35 | * - Takes an instrumentation source as parameter |
| 36 | * - e.g. tracepoints, dynamic_probes... |
| 37 | * - Takes instrumentation source specific arguments. |
| 38 | */ |
| 39 | |
| 40 | #include <linux/module.h> |
| 41 | #include <linux/proc_fs.h> |
| 42 | #include <linux/anon_inodes.h> |
| 43 | #include <linux/file.h> |
| 44 | #include <linux/uaccess.h> |
| 45 | #include <linux/slab.h> |
| 46 | #include <linux/err.h> |
| 47 | #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */ |
| 48 | #include "wrapper/ringbuffer/vfs.h" |
| 49 | #include "wrapper/ringbuffer/backend.h" |
| 50 | #include "wrapper/ringbuffer/frontend.h" |
| 51 | #include "wrapper/poll.h" |
| 52 | #include "lttng-abi.h" |
| 53 | #include "lttng-abi-old.h" |
| 54 | #include "lttng-events.h" |
| 55 | #include "lttng-tracer.h" |
| 56 | #include "lib/ringbuffer/frontend_types.h" |
| 57 | |
| 58 | /* |
| 59 | * This is LTTng's own personal way to create a system call as an external |
| 60 | * module. We use ioctl() on /proc/lttng. |
| 61 | */ |
| 62 | |
| 63 | static struct proc_dir_entry *lttng_proc_dentry; |
| 64 | static const struct file_operations lttng_fops; |
| 65 | static const struct file_operations lttng_session_fops; |
| 66 | static const struct file_operations lttng_channel_fops; |
| 67 | static const struct file_operations lttng_metadata_fops; |
| 68 | static const struct file_operations lttng_event_fops; |
| 69 | static struct file_operations lttng_stream_ring_buffer_file_operations; |
| 70 | |
| 71 | /* |
| 72 | * Teardown management: opened file descriptors keep a refcount on the module, |
| 73 | * so it can only exit when all file descriptors are closed. |
| 74 | */ |
| 75 | |
| 76 | static |
| 77 | int lttng_abi_create_session(void) |
| 78 | { |
| 79 | struct lttng_session *session; |
| 80 | struct file *session_file; |
| 81 | int session_fd, ret; |
| 82 | |
| 83 | session = lttng_session_create(); |
| 84 | if (!session) |
| 85 | return -ENOMEM; |
| 86 | session_fd = get_unused_fd(); |
| 87 | if (session_fd < 0) { |
| 88 | ret = session_fd; |
| 89 | goto fd_error; |
| 90 | } |
| 91 | session_file = anon_inode_getfile("[lttng_session]", |
| 92 | <tng_session_fops, |
| 93 | session, O_RDWR); |
| 94 | if (IS_ERR(session_file)) { |
| 95 | ret = PTR_ERR(session_file); |
| 96 | goto file_error; |
| 97 | } |
| 98 | session->file = session_file; |
| 99 | fd_install(session_fd, session_file); |
| 100 | return session_fd; |
| 101 | |
| 102 | file_error: |
| 103 | put_unused_fd(session_fd); |
| 104 | fd_error: |
| 105 | lttng_session_destroy(session); |
| 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | static |
| 110 | int lttng_abi_tracepoint_list(void) |
| 111 | { |
| 112 | struct file *tracepoint_list_file; |
| 113 | int file_fd, ret; |
| 114 | |
| 115 | file_fd = get_unused_fd(); |
| 116 | if (file_fd < 0) { |
| 117 | ret = file_fd; |
| 118 | goto fd_error; |
| 119 | } |
| 120 | |
| 121 | tracepoint_list_file = anon_inode_getfile("[lttng_tracepoint_list]", |
| 122 | <tng_tracepoint_list_fops, |
| 123 | NULL, O_RDWR); |
| 124 | if (IS_ERR(tracepoint_list_file)) { |
| 125 | ret = PTR_ERR(tracepoint_list_file); |
| 126 | goto file_error; |
| 127 | } |
| 128 | ret = lttng_tracepoint_list_fops.open(NULL, tracepoint_list_file); |
| 129 | if (ret < 0) |
| 130 | goto open_error; |
| 131 | fd_install(file_fd, tracepoint_list_file); |
| 132 | if (file_fd < 0) { |
| 133 | ret = file_fd; |
| 134 | goto fd_error; |
| 135 | } |
| 136 | return file_fd; |
| 137 | |
| 138 | open_error: |
| 139 | fput(tracepoint_list_file); |
| 140 | file_error: |
| 141 | put_unused_fd(file_fd); |
| 142 | fd_error: |
| 143 | return ret; |
| 144 | } |
| 145 | |
| 146 | static |
| 147 | int lttng_abi_syscall_list(void) |
| 148 | { |
| 149 | struct file *syscall_list_file; |
| 150 | int file_fd, ret; |
| 151 | |
| 152 | file_fd = get_unused_fd(); |
| 153 | if (file_fd < 0) { |
| 154 | ret = file_fd; |
| 155 | goto fd_error; |
| 156 | } |
| 157 | |
| 158 | syscall_list_file = anon_inode_getfile("[lttng_syscall_list]", |
| 159 | <tng_syscall_list_fops, |
| 160 | NULL, O_RDWR); |
| 161 | if (IS_ERR(syscall_list_file)) { |
| 162 | ret = PTR_ERR(syscall_list_file); |
| 163 | goto file_error; |
| 164 | } |
| 165 | ret = lttng_syscall_list_fops.open(NULL, syscall_list_file); |
| 166 | if (ret < 0) |
| 167 | goto open_error; |
| 168 | fd_install(file_fd, syscall_list_file); |
| 169 | if (file_fd < 0) { |
| 170 | ret = file_fd; |
| 171 | goto fd_error; |
| 172 | } |
| 173 | return file_fd; |
| 174 | |
| 175 | open_error: |
| 176 | fput(syscall_list_file); |
| 177 | file_error: |
| 178 | put_unused_fd(file_fd); |
| 179 | fd_error: |
| 180 | return ret; |
| 181 | } |
| 182 | |
| 183 | static |
| 184 | void lttng_abi_tracer_version(struct lttng_kernel_tracer_version *v) |
| 185 | { |
| 186 | v->major = LTTNG_MODULES_MAJOR_VERSION; |
| 187 | v->minor = LTTNG_MODULES_MINOR_VERSION; |
| 188 | v->patchlevel = LTTNG_MODULES_PATCHLEVEL_VERSION; |
| 189 | } |
| 190 | |
| 191 | static |
| 192 | long lttng_abi_add_context(struct file *file, |
| 193 | struct lttng_kernel_context *context_param, |
| 194 | struct lttng_ctx **ctx, struct lttng_session *session) |
| 195 | { |
| 196 | |
| 197 | if (session->been_active) |
| 198 | return -EPERM; |
| 199 | |
| 200 | switch (context_param->ctx) { |
| 201 | case LTTNG_KERNEL_CONTEXT_PID: |
| 202 | return lttng_add_pid_to_ctx(ctx); |
| 203 | case LTTNG_KERNEL_CONTEXT_PRIO: |
| 204 | return lttng_add_prio_to_ctx(ctx); |
| 205 | case LTTNG_KERNEL_CONTEXT_NICE: |
| 206 | return lttng_add_nice_to_ctx(ctx); |
| 207 | case LTTNG_KERNEL_CONTEXT_VPID: |
| 208 | return lttng_add_vpid_to_ctx(ctx); |
| 209 | case LTTNG_KERNEL_CONTEXT_TID: |
| 210 | return lttng_add_tid_to_ctx(ctx); |
| 211 | case LTTNG_KERNEL_CONTEXT_VTID: |
| 212 | return lttng_add_vtid_to_ctx(ctx); |
| 213 | case LTTNG_KERNEL_CONTEXT_PPID: |
| 214 | return lttng_add_ppid_to_ctx(ctx); |
| 215 | case LTTNG_KERNEL_CONTEXT_VPPID: |
| 216 | return lttng_add_vppid_to_ctx(ctx); |
| 217 | case LTTNG_KERNEL_CONTEXT_PERF_COUNTER: |
| 218 | context_param->u.perf_counter.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 219 | return lttng_add_perf_counter_to_ctx(context_param->u.perf_counter.type, |
| 220 | context_param->u.perf_counter.config, |
| 221 | context_param->u.perf_counter.name, |
| 222 | ctx); |
| 223 | case LTTNG_KERNEL_CONTEXT_PROCNAME: |
| 224 | return lttng_add_procname_to_ctx(ctx); |
| 225 | case LTTNG_KERNEL_CONTEXT_HOSTNAME: |
| 226 | return lttng_add_hostname_to_ctx(ctx); |
| 227 | default: |
| 228 | return -EINVAL; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * lttng_ioctl - lttng syscall through ioctl |
| 234 | * |
| 235 | * @file: the file |
| 236 | * @cmd: the command |
| 237 | * @arg: command arg |
| 238 | * |
| 239 | * This ioctl implements lttng commands: |
| 240 | * LTTNG_KERNEL_SESSION |
| 241 | * Returns a LTTng trace session file descriptor |
| 242 | * LTTNG_KERNEL_TRACER_VERSION |
| 243 | * Returns the LTTng kernel tracer version |
| 244 | * LTTNG_KERNEL_TRACEPOINT_LIST |
| 245 | * Returns a file descriptor listing available tracepoints |
| 246 | * LTTNG_KERNEL_WAIT_QUIESCENT |
| 247 | * Returns after all previously running probes have completed |
| 248 | * |
| 249 | * The returned session will be deleted when its file descriptor is closed. |
| 250 | */ |
| 251 | static |
| 252 | long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 253 | { |
| 254 | switch (cmd) { |
| 255 | case LTTNG_KERNEL_OLD_SESSION: |
| 256 | case LTTNG_KERNEL_SESSION: |
| 257 | return lttng_abi_create_session(); |
| 258 | case LTTNG_KERNEL_OLD_TRACER_VERSION: |
| 259 | { |
| 260 | struct lttng_kernel_tracer_version v; |
| 261 | struct lttng_kernel_old_tracer_version oldv; |
| 262 | struct lttng_kernel_old_tracer_version *uversion = |
| 263 | (struct lttng_kernel_old_tracer_version __user *) arg; |
| 264 | |
| 265 | lttng_abi_tracer_version(&v); |
| 266 | oldv.major = v.major; |
| 267 | oldv.minor = v.minor; |
| 268 | oldv.patchlevel = v.patchlevel; |
| 269 | |
| 270 | if (copy_to_user(uversion, &oldv, sizeof(oldv))) |
| 271 | return -EFAULT; |
| 272 | return 0; |
| 273 | } |
| 274 | case LTTNG_KERNEL_TRACER_VERSION: |
| 275 | { |
| 276 | struct lttng_kernel_tracer_version version; |
| 277 | struct lttng_kernel_tracer_version *uversion = |
| 278 | (struct lttng_kernel_tracer_version __user *) arg; |
| 279 | |
| 280 | lttng_abi_tracer_version(&version); |
| 281 | |
| 282 | if (copy_to_user(uversion, &version, sizeof(version))) |
| 283 | return -EFAULT; |
| 284 | return 0; |
| 285 | } |
| 286 | case LTTNG_KERNEL_OLD_TRACEPOINT_LIST: |
| 287 | case LTTNG_KERNEL_TRACEPOINT_LIST: |
| 288 | return lttng_abi_tracepoint_list(); |
| 289 | case LTTNG_KERNEL_SYSCALL_LIST: |
| 290 | return lttng_abi_syscall_list(); |
| 291 | case LTTNG_KERNEL_OLD_WAIT_QUIESCENT: |
| 292 | case LTTNG_KERNEL_WAIT_QUIESCENT: |
| 293 | synchronize_trace(); |
| 294 | return 0; |
| 295 | case LTTNG_KERNEL_OLD_CALIBRATE: |
| 296 | { |
| 297 | struct lttng_kernel_old_calibrate __user *ucalibrate = |
| 298 | (struct lttng_kernel_old_calibrate __user *) arg; |
| 299 | struct lttng_kernel_old_calibrate old_calibrate; |
| 300 | struct lttng_kernel_calibrate calibrate; |
| 301 | int ret; |
| 302 | |
| 303 | if (copy_from_user(&old_calibrate, ucalibrate, sizeof(old_calibrate))) |
| 304 | return -EFAULT; |
| 305 | calibrate.type = old_calibrate.type; |
| 306 | ret = lttng_calibrate(&calibrate); |
| 307 | if (copy_to_user(ucalibrate, &old_calibrate, sizeof(old_calibrate))) |
| 308 | return -EFAULT; |
| 309 | return ret; |
| 310 | } |
| 311 | case LTTNG_KERNEL_CALIBRATE: |
| 312 | { |
| 313 | struct lttng_kernel_calibrate __user *ucalibrate = |
| 314 | (struct lttng_kernel_calibrate __user *) arg; |
| 315 | struct lttng_kernel_calibrate calibrate; |
| 316 | int ret; |
| 317 | |
| 318 | if (copy_from_user(&calibrate, ucalibrate, sizeof(calibrate))) |
| 319 | return -EFAULT; |
| 320 | ret = lttng_calibrate(&calibrate); |
| 321 | if (copy_to_user(ucalibrate, &calibrate, sizeof(calibrate))) |
| 322 | return -EFAULT; |
| 323 | return ret; |
| 324 | } |
| 325 | default: |
| 326 | return -ENOIOCTLCMD; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | static const struct file_operations lttng_fops = { |
| 331 | .owner = THIS_MODULE, |
| 332 | .unlocked_ioctl = lttng_ioctl, |
| 333 | #ifdef CONFIG_COMPAT |
| 334 | .compat_ioctl = lttng_ioctl, |
| 335 | #endif |
| 336 | }; |
| 337 | |
| 338 | static |
| 339 | int lttng_abi_create_channel(struct file *session_file, |
| 340 | struct lttng_kernel_channel *chan_param, |
| 341 | enum channel_type channel_type) |
| 342 | { |
| 343 | struct lttng_session *session = session_file->private_data; |
| 344 | const struct file_operations *fops = NULL; |
| 345 | const char *transport_name; |
| 346 | struct lttng_channel *chan; |
| 347 | struct file *chan_file; |
| 348 | int chan_fd; |
| 349 | int ret = 0; |
| 350 | |
| 351 | chan_fd = get_unused_fd(); |
| 352 | if (chan_fd < 0) { |
| 353 | ret = chan_fd; |
| 354 | goto fd_error; |
| 355 | } |
| 356 | switch (channel_type) { |
| 357 | case PER_CPU_CHANNEL: |
| 358 | fops = <tng_channel_fops; |
| 359 | break; |
| 360 | case METADATA_CHANNEL: |
| 361 | fops = <tng_metadata_fops; |
| 362 | break; |
| 363 | } |
| 364 | |
| 365 | chan_file = anon_inode_getfile("[lttng_channel]", |
| 366 | fops, |
| 367 | NULL, O_RDWR); |
| 368 | if (IS_ERR(chan_file)) { |
| 369 | ret = PTR_ERR(chan_file); |
| 370 | goto file_error; |
| 371 | } |
| 372 | switch (channel_type) { |
| 373 | case PER_CPU_CHANNEL: |
| 374 | if (chan_param->output == LTTNG_KERNEL_SPLICE) { |
| 375 | transport_name = chan_param->overwrite ? |
| 376 | "relay-overwrite" : "relay-discard"; |
| 377 | } else if (chan_param->output == LTTNG_KERNEL_MMAP) { |
| 378 | transport_name = chan_param->overwrite ? |
| 379 | "relay-overwrite-mmap" : "relay-discard-mmap"; |
| 380 | } else { |
| 381 | return -EINVAL; |
| 382 | } |
| 383 | break; |
| 384 | case METADATA_CHANNEL: |
| 385 | if (chan_param->output == LTTNG_KERNEL_SPLICE) |
| 386 | transport_name = "relay-metadata"; |
| 387 | else if (chan_param->output == LTTNG_KERNEL_MMAP) |
| 388 | transport_name = "relay-metadata-mmap"; |
| 389 | else |
| 390 | return -EINVAL; |
| 391 | break; |
| 392 | default: |
| 393 | transport_name = "<unknown>"; |
| 394 | break; |
| 395 | } |
| 396 | /* |
| 397 | * We tolerate no failure path after channel creation. It will stay |
| 398 | * invariant for the rest of the session. |
| 399 | */ |
| 400 | chan = lttng_channel_create(session, transport_name, NULL, |
| 401 | chan_param->subbuf_size, |
| 402 | chan_param->num_subbuf, |
| 403 | chan_param->switch_timer_interval, |
| 404 | chan_param->read_timer_interval, |
| 405 | channel_type); |
| 406 | if (!chan) { |
| 407 | ret = -EINVAL; |
| 408 | goto chan_error; |
| 409 | } |
| 410 | chan->file = chan_file; |
| 411 | chan_file->private_data = chan; |
| 412 | fd_install(chan_fd, chan_file); |
| 413 | atomic_long_inc(&session_file->f_count); |
| 414 | |
| 415 | return chan_fd; |
| 416 | |
| 417 | chan_error: |
| 418 | fput(chan_file); |
| 419 | file_error: |
| 420 | put_unused_fd(chan_fd); |
| 421 | fd_error: |
| 422 | return ret; |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * lttng_session_ioctl - lttng session fd ioctl |
| 427 | * |
| 428 | * @file: the file |
| 429 | * @cmd: the command |
| 430 | * @arg: command arg |
| 431 | * |
| 432 | * This ioctl implements lttng commands: |
| 433 | * LTTNG_KERNEL_CHANNEL |
| 434 | * Returns a LTTng channel file descriptor |
| 435 | * LTTNG_KERNEL_ENABLE |
| 436 | * Enables tracing for a session (weak enable) |
| 437 | * LTTNG_KERNEL_DISABLE |
| 438 | * Disables tracing for a session (strong disable) |
| 439 | * LTTNG_KERNEL_METADATA |
| 440 | * Returns a LTTng metadata file descriptor |
| 441 | * |
| 442 | * The returned channel will be deleted when its file descriptor is closed. |
| 443 | */ |
| 444 | static |
| 445 | long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 446 | { |
| 447 | struct lttng_session *session = file->private_data; |
| 448 | |
| 449 | switch (cmd) { |
| 450 | case LTTNG_KERNEL_OLD_CHANNEL: |
| 451 | { |
| 452 | struct lttng_kernel_channel chan_param; |
| 453 | struct lttng_kernel_old_channel old_chan_param; |
| 454 | |
| 455 | if (copy_from_user(&old_chan_param, |
| 456 | (struct lttng_kernel_old_channel __user *) arg, |
| 457 | sizeof(struct lttng_kernel_old_channel))) |
| 458 | return -EFAULT; |
| 459 | chan_param.overwrite = old_chan_param.overwrite; |
| 460 | chan_param.subbuf_size = old_chan_param.subbuf_size; |
| 461 | chan_param.num_subbuf = old_chan_param.num_subbuf; |
| 462 | chan_param.switch_timer_interval = old_chan_param.switch_timer_interval; |
| 463 | chan_param.read_timer_interval = old_chan_param.read_timer_interval; |
| 464 | chan_param.output = old_chan_param.output; |
| 465 | |
| 466 | return lttng_abi_create_channel(file, &chan_param, |
| 467 | PER_CPU_CHANNEL); |
| 468 | } |
| 469 | case LTTNG_KERNEL_CHANNEL: |
| 470 | { |
| 471 | struct lttng_kernel_channel chan_param; |
| 472 | |
| 473 | if (copy_from_user(&chan_param, |
| 474 | (struct lttng_kernel_channel __user *) arg, |
| 475 | sizeof(struct lttng_kernel_channel))) |
| 476 | return -EFAULT; |
| 477 | return lttng_abi_create_channel(file, &chan_param, |
| 478 | PER_CPU_CHANNEL); |
| 479 | } |
| 480 | case LTTNG_KERNEL_OLD_SESSION_START: |
| 481 | case LTTNG_KERNEL_OLD_ENABLE: |
| 482 | case LTTNG_KERNEL_SESSION_START: |
| 483 | case LTTNG_KERNEL_ENABLE: |
| 484 | return lttng_session_enable(session); |
| 485 | case LTTNG_KERNEL_OLD_SESSION_STOP: |
| 486 | case LTTNG_KERNEL_OLD_DISABLE: |
| 487 | case LTTNG_KERNEL_SESSION_STOP: |
| 488 | case LTTNG_KERNEL_DISABLE: |
| 489 | return lttng_session_disable(session); |
| 490 | case LTTNG_KERNEL_OLD_METADATA: |
| 491 | { |
| 492 | struct lttng_kernel_channel chan_param; |
| 493 | struct lttng_kernel_old_channel old_chan_param; |
| 494 | |
| 495 | if (copy_from_user(&old_chan_param, |
| 496 | (struct lttng_kernel_old_channel __user *) arg, |
| 497 | sizeof(struct lttng_kernel_old_channel))) |
| 498 | return -EFAULT; |
| 499 | chan_param.overwrite = old_chan_param.overwrite; |
| 500 | chan_param.subbuf_size = old_chan_param.subbuf_size; |
| 501 | chan_param.num_subbuf = old_chan_param.num_subbuf; |
| 502 | chan_param.switch_timer_interval = old_chan_param.switch_timer_interval; |
| 503 | chan_param.read_timer_interval = old_chan_param.read_timer_interval; |
| 504 | chan_param.output = old_chan_param.output; |
| 505 | |
| 506 | return lttng_abi_create_channel(file, &chan_param, |
| 507 | METADATA_CHANNEL); |
| 508 | } |
| 509 | case LTTNG_KERNEL_METADATA: |
| 510 | { |
| 511 | struct lttng_kernel_channel chan_param; |
| 512 | |
| 513 | if (copy_from_user(&chan_param, |
| 514 | (struct lttng_kernel_channel __user *) arg, |
| 515 | sizeof(struct lttng_kernel_channel))) |
| 516 | return -EFAULT; |
| 517 | return lttng_abi_create_channel(file, &chan_param, |
| 518 | METADATA_CHANNEL); |
| 519 | } |
| 520 | default: |
| 521 | return -ENOIOCTLCMD; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | /* |
| 526 | * Called when the last file reference is dropped. |
| 527 | * |
| 528 | * Big fat note: channels and events are invariant for the whole session after |
| 529 | * their creation. So this session destruction also destroys all channel and |
| 530 | * event structures specific to this session (they are not destroyed when their |
| 531 | * individual file is released). |
| 532 | */ |
| 533 | static |
| 534 | int lttng_session_release(struct inode *inode, struct file *file) |
| 535 | { |
| 536 | struct lttng_session *session = file->private_data; |
| 537 | |
| 538 | if (session) |
| 539 | lttng_session_destroy(session); |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | static const struct file_operations lttng_session_fops = { |
| 544 | .owner = THIS_MODULE, |
| 545 | .release = lttng_session_release, |
| 546 | .unlocked_ioctl = lttng_session_ioctl, |
| 547 | #ifdef CONFIG_COMPAT |
| 548 | .compat_ioctl = lttng_session_ioctl, |
| 549 | #endif |
| 550 | }; |
| 551 | |
| 552 | /** |
| 553 | * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation |
| 554 | * @filp: the file |
| 555 | * @wait: poll table |
| 556 | * |
| 557 | * Handles the poll operations for the metadata channels. |
| 558 | */ |
| 559 | static |
| 560 | unsigned int lttng_metadata_ring_buffer_poll(struct file *filp, |
| 561 | poll_table *wait) |
| 562 | { |
| 563 | struct lttng_metadata_stream *stream = filp->private_data; |
| 564 | struct lib_ring_buffer *buf = stream->priv; |
| 565 | int finalized; |
| 566 | unsigned int mask = 0; |
| 567 | |
| 568 | if (filp->f_mode & FMODE_READ) { |
| 569 | poll_wait_set_exclusive(wait); |
| 570 | poll_wait(filp, &stream->read_wait, wait); |
| 571 | |
| 572 | finalized = stream->finalized; |
| 573 | |
| 574 | /* |
| 575 | * lib_ring_buffer_is_finalized() contains a smp_rmb() |
| 576 | * ordering finalized load before offsets loads. |
| 577 | */ |
| 578 | WARN_ON(atomic_long_read(&buf->active_readers) != 1); |
| 579 | |
| 580 | if (finalized) |
| 581 | mask |= POLLHUP; |
| 582 | |
| 583 | if (stream->metadata_cache->metadata_written > |
| 584 | stream->metadata_out) |
| 585 | mask |= POLLIN; |
| 586 | } |
| 587 | |
| 588 | return mask; |
| 589 | } |
| 590 | |
| 591 | static |
| 592 | void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file *filp, |
| 593 | unsigned int cmd, unsigned long arg) |
| 594 | { |
| 595 | struct lttng_metadata_stream *stream = filp->private_data; |
| 596 | |
| 597 | stream->metadata_out = stream->metadata_in; |
| 598 | } |
| 599 | |
| 600 | static |
| 601 | long lttng_metadata_ring_buffer_ioctl(struct file *filp, |
| 602 | unsigned int cmd, unsigned long arg) |
| 603 | { |
| 604 | int ret; |
| 605 | struct lttng_metadata_stream *stream = filp->private_data; |
| 606 | struct lib_ring_buffer *buf = stream->priv; |
| 607 | |
| 608 | switch (cmd) { |
| 609 | case RING_BUFFER_GET_NEXT_SUBBUF: |
| 610 | { |
| 611 | struct lttng_metadata_stream *stream = filp->private_data; |
| 612 | struct lib_ring_buffer *buf = stream->priv; |
| 613 | struct channel *chan = buf->backend.chan; |
| 614 | |
| 615 | ret = lttng_metadata_output_channel(stream, chan); |
| 616 | if (ret > 0) { |
| 617 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE); |
| 618 | ret = 0; |
| 619 | } else if (ret < 0) |
| 620 | goto err; |
| 621 | break; |
| 622 | } |
| 623 | case RING_BUFFER_GET_SUBBUF: |
| 624 | { |
| 625 | /* |
| 626 | * Random access is not allowed for metadata channel. |
| 627 | */ |
| 628 | return -ENOSYS; |
| 629 | } |
| 630 | case RING_BUFFER_FLUSH: |
| 631 | { |
| 632 | struct lttng_metadata_stream *stream = filp->private_data; |
| 633 | struct lib_ring_buffer *buf = stream->priv; |
| 634 | struct channel *chan = buf->backend.chan; |
| 635 | |
| 636 | /* |
| 637 | * Before doing the actual ring buffer flush, write up to one |
| 638 | * packet of metadata in the ring buffer. |
| 639 | */ |
| 640 | ret = lttng_metadata_output_channel(stream, chan); |
| 641 | if (ret < 0) |
| 642 | goto err; |
| 643 | break; |
| 644 | } |
| 645 | default: |
| 646 | break; |
| 647 | } |
| 648 | /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */ |
| 649 | |
| 650 | /* Performing lib ring buffer ioctl after our own. */ |
| 651 | ret = lib_ring_buffer_ioctl(filp, cmd, arg, buf); |
| 652 | if (ret < 0) |
| 653 | goto err; |
| 654 | |
| 655 | switch (cmd) { |
| 656 | case RING_BUFFER_PUT_NEXT_SUBBUF: |
| 657 | { |
| 658 | lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp, |
| 659 | cmd, arg); |
| 660 | break; |
| 661 | } |
| 662 | default: |
| 663 | break; |
| 664 | } |
| 665 | err: |
| 666 | return ret; |
| 667 | } |
| 668 | |
| 669 | #ifdef CONFIG_COMPAT |
| 670 | static |
| 671 | long lttng_metadata_ring_buffer_compat_ioctl(struct file *filp, |
| 672 | unsigned int cmd, unsigned long arg) |
| 673 | { |
| 674 | int ret; |
| 675 | struct lttng_metadata_stream *stream = filp->private_data; |
| 676 | struct lib_ring_buffer *buf = stream->priv; |
| 677 | |
| 678 | switch (cmd) { |
| 679 | case RING_BUFFER_GET_NEXT_SUBBUF: |
| 680 | { |
| 681 | struct lttng_metadata_stream *stream = filp->private_data; |
| 682 | struct lib_ring_buffer *buf = stream->priv; |
| 683 | struct channel *chan = buf->backend.chan; |
| 684 | |
| 685 | ret = lttng_metadata_output_channel(stream, chan); |
| 686 | if (ret > 0) { |
| 687 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE); |
| 688 | ret = 0; |
| 689 | } else if (ret < 0) |
| 690 | goto err; |
| 691 | break; |
| 692 | } |
| 693 | case RING_BUFFER_GET_SUBBUF: |
| 694 | { |
| 695 | /* |
| 696 | * Random access is not allowed for metadata channel. |
| 697 | */ |
| 698 | return -ENOSYS; |
| 699 | } |
| 700 | default: |
| 701 | break; |
| 702 | } |
| 703 | /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */ |
| 704 | |
| 705 | /* Performing lib ring buffer ioctl after our own. */ |
| 706 | ret = lib_ring_buffer_compat_ioctl(filp, cmd, arg, buf); |
| 707 | if (ret < 0) |
| 708 | goto err; |
| 709 | |
| 710 | switch (cmd) { |
| 711 | case RING_BUFFER_PUT_NEXT_SUBBUF: |
| 712 | { |
| 713 | lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp, |
| 714 | cmd, arg); |
| 715 | break; |
| 716 | } |
| 717 | default: |
| 718 | break; |
| 719 | } |
| 720 | err: |
| 721 | return ret; |
| 722 | } |
| 723 | #endif |
| 724 | |
| 725 | /* |
| 726 | * This is not used by anonymous file descriptors. This code is left |
| 727 | * there if we ever want to implement an inode with open() operation. |
| 728 | */ |
| 729 | static |
| 730 | int lttng_metadata_ring_buffer_open(struct inode *inode, struct file *file) |
| 731 | { |
| 732 | struct lttng_metadata_stream *stream = inode->i_private; |
| 733 | struct lib_ring_buffer *buf = stream->priv; |
| 734 | |
| 735 | file->private_data = buf; |
| 736 | /* |
| 737 | * Since life-time of metadata cache differs from that of |
| 738 | * session, we need to keep our own reference on the transport. |
| 739 | */ |
| 740 | if (!try_module_get(stream->transport->owner)) { |
| 741 | printk(KERN_WARNING "LTT : Can't lock transport module.\n"); |
| 742 | return -EBUSY; |
| 743 | } |
| 744 | return lib_ring_buffer_open(inode, file, buf); |
| 745 | } |
| 746 | |
| 747 | static |
| 748 | int lttng_metadata_ring_buffer_release(struct inode *inode, struct file *file) |
| 749 | { |
| 750 | struct lttng_metadata_stream *stream = file->private_data; |
| 751 | struct lib_ring_buffer *buf = stream->priv; |
| 752 | |
| 753 | kref_put(&stream->metadata_cache->refcount, metadata_cache_destroy); |
| 754 | module_put(stream->transport->owner); |
| 755 | return lib_ring_buffer_release(inode, file, buf); |
| 756 | } |
| 757 | |
| 758 | static |
| 759 | ssize_t lttng_metadata_ring_buffer_splice_read(struct file *in, loff_t *ppos, |
| 760 | struct pipe_inode_info *pipe, size_t len, |
| 761 | unsigned int flags) |
| 762 | { |
| 763 | struct lttng_metadata_stream *stream = in->private_data; |
| 764 | struct lib_ring_buffer *buf = stream->priv; |
| 765 | |
| 766 | return lib_ring_buffer_splice_read(in, ppos, pipe, len, |
| 767 | flags, buf); |
| 768 | } |
| 769 | |
| 770 | static |
| 771 | int lttng_metadata_ring_buffer_mmap(struct file *filp, |
| 772 | struct vm_area_struct *vma) |
| 773 | { |
| 774 | struct lttng_metadata_stream *stream = filp->private_data; |
| 775 | struct lib_ring_buffer *buf = stream->priv; |
| 776 | |
| 777 | return lib_ring_buffer_mmap(filp, vma, buf); |
| 778 | } |
| 779 | |
| 780 | static |
| 781 | const struct file_operations lttng_metadata_ring_buffer_file_operations = { |
| 782 | .owner = THIS_MODULE, |
| 783 | .open = lttng_metadata_ring_buffer_open, |
| 784 | .release = lttng_metadata_ring_buffer_release, |
| 785 | .poll = lttng_metadata_ring_buffer_poll, |
| 786 | .splice_read = lttng_metadata_ring_buffer_splice_read, |
| 787 | .mmap = lttng_metadata_ring_buffer_mmap, |
| 788 | .unlocked_ioctl = lttng_metadata_ring_buffer_ioctl, |
| 789 | .llseek = vfs_lib_ring_buffer_no_llseek, |
| 790 | #ifdef CONFIG_COMPAT |
| 791 | .compat_ioctl = lttng_metadata_ring_buffer_compat_ioctl, |
| 792 | #endif |
| 793 | }; |
| 794 | |
| 795 | static |
| 796 | int lttng_abi_create_stream_fd(struct file *channel_file, void *stream_priv, |
| 797 | const struct file_operations *fops) |
| 798 | { |
| 799 | int stream_fd, ret; |
| 800 | struct file *stream_file; |
| 801 | |
| 802 | stream_fd = get_unused_fd(); |
| 803 | if (stream_fd < 0) { |
| 804 | ret = stream_fd; |
| 805 | goto fd_error; |
| 806 | } |
| 807 | stream_file = anon_inode_getfile("[lttng_stream]", fops, |
| 808 | stream_priv, O_RDWR); |
| 809 | if (IS_ERR(stream_file)) { |
| 810 | ret = PTR_ERR(stream_file); |
| 811 | goto file_error; |
| 812 | } |
| 813 | /* |
| 814 | * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor |
| 815 | * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this |
| 816 | * file descriptor, so we set FMODE_PREAD here. |
| 817 | */ |
| 818 | stream_file->f_mode |= FMODE_PREAD; |
| 819 | fd_install(stream_fd, stream_file); |
| 820 | /* |
| 821 | * The stream holds a reference to the channel within the generic ring |
| 822 | * buffer library, so no need to hold a refcount on the channel and |
| 823 | * session files here. |
| 824 | */ |
| 825 | return stream_fd; |
| 826 | |
| 827 | file_error: |
| 828 | put_unused_fd(stream_fd); |
| 829 | fd_error: |
| 830 | return ret; |
| 831 | } |
| 832 | |
| 833 | static |
| 834 | int lttng_abi_open_stream(struct file *channel_file) |
| 835 | { |
| 836 | struct lttng_channel *channel = channel_file->private_data; |
| 837 | struct lib_ring_buffer *buf; |
| 838 | int ret; |
| 839 | void *stream_priv; |
| 840 | |
| 841 | buf = channel->ops->buffer_read_open(channel->chan); |
| 842 | if (!buf) |
| 843 | return -ENOENT; |
| 844 | |
| 845 | stream_priv = buf; |
| 846 | ret = lttng_abi_create_stream_fd(channel_file, stream_priv, |
| 847 | <tng_stream_ring_buffer_file_operations); |
| 848 | if (ret < 0) |
| 849 | goto fd_error; |
| 850 | |
| 851 | return ret; |
| 852 | |
| 853 | fd_error: |
| 854 | channel->ops->buffer_read_close(buf); |
| 855 | return ret; |
| 856 | } |
| 857 | |
| 858 | static |
| 859 | int lttng_abi_open_metadata_stream(struct file *channel_file) |
| 860 | { |
| 861 | struct lttng_channel *channel = channel_file->private_data; |
| 862 | struct lttng_session *session = channel->session; |
| 863 | struct lib_ring_buffer *buf; |
| 864 | int ret; |
| 865 | struct lttng_metadata_stream *metadata_stream; |
| 866 | void *stream_priv; |
| 867 | |
| 868 | buf = channel->ops->buffer_read_open(channel->chan); |
| 869 | if (!buf) |
| 870 | return -ENOENT; |
| 871 | |
| 872 | metadata_stream = kzalloc(sizeof(struct lttng_metadata_stream), |
| 873 | GFP_KERNEL); |
| 874 | if (!metadata_stream) { |
| 875 | ret = -ENOMEM; |
| 876 | goto nomem; |
| 877 | } |
| 878 | metadata_stream->metadata_cache = session->metadata_cache; |
| 879 | init_waitqueue_head(&metadata_stream->read_wait); |
| 880 | metadata_stream->priv = buf; |
| 881 | stream_priv = metadata_stream; |
| 882 | metadata_stream->transport = channel->transport; |
| 883 | mutex_init(&metadata_stream->lock); |
| 884 | |
| 885 | /* |
| 886 | * Since life-time of metadata cache differs from that of |
| 887 | * session, we need to keep our own reference on the transport. |
| 888 | */ |
| 889 | if (!try_module_get(metadata_stream->transport->owner)) { |
| 890 | printk(KERN_WARNING "LTT : Can't lock transport module.\n"); |
| 891 | ret = -EINVAL; |
| 892 | goto notransport; |
| 893 | } |
| 894 | |
| 895 | ret = lttng_abi_create_stream_fd(channel_file, stream_priv, |
| 896 | <tng_metadata_ring_buffer_file_operations); |
| 897 | if (ret < 0) |
| 898 | goto fd_error; |
| 899 | |
| 900 | kref_get(&session->metadata_cache->refcount); |
| 901 | list_add(&metadata_stream->list, |
| 902 | &session->metadata_cache->metadata_stream); |
| 903 | return ret; |
| 904 | |
| 905 | fd_error: |
| 906 | module_put(metadata_stream->transport->owner); |
| 907 | notransport: |
| 908 | kfree(metadata_stream); |
| 909 | nomem: |
| 910 | channel->ops->buffer_read_close(buf); |
| 911 | return ret; |
| 912 | } |
| 913 | |
| 914 | static |
| 915 | int lttng_abi_create_event(struct file *channel_file, |
| 916 | struct lttng_kernel_event *event_param) |
| 917 | { |
| 918 | struct lttng_channel *channel = channel_file->private_data; |
| 919 | struct lttng_event *event; |
| 920 | int event_fd, ret; |
| 921 | struct file *event_file; |
| 922 | |
| 923 | event_param->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 924 | switch (event_param->instrumentation) { |
| 925 | case LTTNG_KERNEL_KRETPROBE: |
| 926 | event_param->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 927 | break; |
| 928 | case LTTNG_KERNEL_KPROBE: |
| 929 | event_param->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 930 | break; |
| 931 | case LTTNG_KERNEL_FUNCTION: |
| 932 | event_param->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 933 | break; |
| 934 | default: |
| 935 | break; |
| 936 | } |
| 937 | switch (event_param->instrumentation) { |
| 938 | default: |
| 939 | event_fd = get_unused_fd(); |
| 940 | if (event_fd < 0) { |
| 941 | ret = event_fd; |
| 942 | goto fd_error; |
| 943 | } |
| 944 | event_file = anon_inode_getfile("[lttng_event]", |
| 945 | <tng_event_fops, |
| 946 | NULL, O_RDWR); |
| 947 | if (IS_ERR(event_file)) { |
| 948 | ret = PTR_ERR(event_file); |
| 949 | goto file_error; |
| 950 | } |
| 951 | /* |
| 952 | * We tolerate no failure path after event creation. It |
| 953 | * will stay invariant for the rest of the session. |
| 954 | */ |
| 955 | event = lttng_event_create(channel, event_param, NULL, NULL); |
| 956 | WARN_ON_ONCE(!event); |
| 957 | if (IS_ERR(event)) { |
| 958 | ret = PTR_ERR(event); |
| 959 | goto event_error; |
| 960 | } |
| 961 | event_file->private_data = event; |
| 962 | fd_install(event_fd, event_file); |
| 963 | /* The event holds a reference on the channel */ |
| 964 | atomic_long_inc(&channel_file->f_count); |
| 965 | break; |
| 966 | case LTTNG_KERNEL_SYSCALL: |
| 967 | ret = lttng_syscalls_register(channel, NULL); |
| 968 | if (ret) |
| 969 | goto fd_error; |
| 970 | event_fd = 0; |
| 971 | if (event_param->u.syscall.enable) { |
| 972 | ret = lttng_syscall_filter_enable(channel, |
| 973 | event_param->name[0] == '\0' ? |
| 974 | NULL : event_param->name); |
| 975 | if (ret) |
| 976 | goto fd_error; |
| 977 | |
| 978 | } else { |
| 979 | ret = lttng_syscall_filter_disable(channel, |
| 980 | event_param->name[0] == '\0' ? |
| 981 | NULL : event_param->name); |
| 982 | if (ret) |
| 983 | goto fd_error; |
| 984 | } |
| 985 | break; |
| 986 | } |
| 987 | return event_fd; |
| 988 | |
| 989 | event_error: |
| 990 | fput(event_file); |
| 991 | file_error: |
| 992 | put_unused_fd(event_fd); |
| 993 | fd_error: |
| 994 | return ret; |
| 995 | } |
| 996 | |
| 997 | /** |
| 998 | * lttng_channel_ioctl - lttng syscall through ioctl |
| 999 | * |
| 1000 | * @file: the file |
| 1001 | * @cmd: the command |
| 1002 | * @arg: command arg |
| 1003 | * |
| 1004 | * This ioctl implements lttng commands: |
| 1005 | * LTTNG_KERNEL_STREAM |
| 1006 | * Returns an event stream file descriptor or failure. |
| 1007 | * (typically, one event stream records events from one CPU) |
| 1008 | * LTTNG_KERNEL_EVENT |
| 1009 | * Returns an event file descriptor or failure. |
| 1010 | * LTTNG_KERNEL_CONTEXT |
| 1011 | * Prepend a context field to each event in the channel |
| 1012 | * LTTNG_KERNEL_ENABLE |
| 1013 | * Enable recording for events in this channel (weak enable) |
| 1014 | * LTTNG_KERNEL_DISABLE |
| 1015 | * Disable recording for events in this channel (strong disable) |
| 1016 | * |
| 1017 | * Channel and event file descriptors also hold a reference on the session. |
| 1018 | */ |
| 1019 | static |
| 1020 | long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 1021 | { |
| 1022 | struct lttng_channel *channel = file->private_data; |
| 1023 | |
| 1024 | switch (cmd) { |
| 1025 | case LTTNG_KERNEL_OLD_STREAM: |
| 1026 | case LTTNG_KERNEL_STREAM: |
| 1027 | return lttng_abi_open_stream(file); |
| 1028 | case LTTNG_KERNEL_OLD_EVENT: |
| 1029 | { |
| 1030 | struct lttng_kernel_event *uevent_param; |
| 1031 | struct lttng_kernel_old_event *old_uevent_param; |
| 1032 | int ret; |
| 1033 | |
| 1034 | uevent_param = kmalloc(sizeof(struct lttng_kernel_event), |
| 1035 | GFP_KERNEL); |
| 1036 | if (!uevent_param) { |
| 1037 | ret = -ENOMEM; |
| 1038 | goto old_event_end; |
| 1039 | } |
| 1040 | old_uevent_param = kmalloc( |
| 1041 | sizeof(struct lttng_kernel_old_event), |
| 1042 | GFP_KERNEL); |
| 1043 | if (!old_uevent_param) { |
| 1044 | ret = -ENOMEM; |
| 1045 | goto old_event_error_free_param; |
| 1046 | } |
| 1047 | if (copy_from_user(old_uevent_param, |
| 1048 | (struct lttng_kernel_old_event __user *) arg, |
| 1049 | sizeof(struct lttng_kernel_old_event))) { |
| 1050 | ret = -EFAULT; |
| 1051 | goto old_event_error_free_old_param; |
| 1052 | } |
| 1053 | |
| 1054 | memcpy(uevent_param->name, old_uevent_param->name, |
| 1055 | sizeof(uevent_param->name)); |
| 1056 | uevent_param->instrumentation = |
| 1057 | old_uevent_param->instrumentation; |
| 1058 | |
| 1059 | switch (old_uevent_param->instrumentation) { |
| 1060 | case LTTNG_KERNEL_KPROBE: |
| 1061 | uevent_param->u.kprobe.addr = |
| 1062 | old_uevent_param->u.kprobe.addr; |
| 1063 | uevent_param->u.kprobe.offset = |
| 1064 | old_uevent_param->u.kprobe.offset; |
| 1065 | memcpy(uevent_param->u.kprobe.symbol_name, |
| 1066 | old_uevent_param->u.kprobe.symbol_name, |
| 1067 | sizeof(uevent_param->u.kprobe.symbol_name)); |
| 1068 | break; |
| 1069 | case LTTNG_KERNEL_KRETPROBE: |
| 1070 | uevent_param->u.kretprobe.addr = |
| 1071 | old_uevent_param->u.kretprobe.addr; |
| 1072 | uevent_param->u.kretprobe.offset = |
| 1073 | old_uevent_param->u.kretprobe.offset; |
| 1074 | memcpy(uevent_param->u.kretprobe.symbol_name, |
| 1075 | old_uevent_param->u.kretprobe.symbol_name, |
| 1076 | sizeof(uevent_param->u.kretprobe.symbol_name)); |
| 1077 | break; |
| 1078 | case LTTNG_KERNEL_FUNCTION: |
| 1079 | memcpy(uevent_param->u.ftrace.symbol_name, |
| 1080 | old_uevent_param->u.ftrace.symbol_name, |
| 1081 | sizeof(uevent_param->u.ftrace.symbol_name)); |
| 1082 | break; |
| 1083 | default: |
| 1084 | break; |
| 1085 | } |
| 1086 | ret = lttng_abi_create_event(file, uevent_param); |
| 1087 | |
| 1088 | old_event_error_free_old_param: |
| 1089 | kfree(old_uevent_param); |
| 1090 | old_event_error_free_param: |
| 1091 | kfree(uevent_param); |
| 1092 | old_event_end: |
| 1093 | return ret; |
| 1094 | } |
| 1095 | case LTTNG_KERNEL_EVENT: |
| 1096 | { |
| 1097 | struct lttng_kernel_event uevent_param; |
| 1098 | |
| 1099 | if (copy_from_user(&uevent_param, |
| 1100 | (struct lttng_kernel_event __user *) arg, |
| 1101 | sizeof(uevent_param))) |
| 1102 | return -EFAULT; |
| 1103 | return lttng_abi_create_event(file, &uevent_param); |
| 1104 | } |
| 1105 | case LTTNG_KERNEL_OLD_CONTEXT: |
| 1106 | { |
| 1107 | struct lttng_kernel_context *ucontext_param; |
| 1108 | struct lttng_kernel_old_context *old_ucontext_param; |
| 1109 | int ret; |
| 1110 | |
| 1111 | ucontext_param = kmalloc(sizeof(struct lttng_kernel_context), |
| 1112 | GFP_KERNEL); |
| 1113 | if (!ucontext_param) { |
| 1114 | ret = -ENOMEM; |
| 1115 | goto old_ctx_end; |
| 1116 | } |
| 1117 | old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context), |
| 1118 | GFP_KERNEL); |
| 1119 | if (!old_ucontext_param) { |
| 1120 | ret = -ENOMEM; |
| 1121 | goto old_ctx_error_free_param; |
| 1122 | } |
| 1123 | |
| 1124 | if (copy_from_user(old_ucontext_param, |
| 1125 | (struct lttng_kernel_old_context __user *) arg, |
| 1126 | sizeof(struct lttng_kernel_old_context))) { |
| 1127 | ret = -EFAULT; |
| 1128 | goto old_ctx_error_free_old_param; |
| 1129 | } |
| 1130 | ucontext_param->ctx = old_ucontext_param->ctx; |
| 1131 | memcpy(ucontext_param->padding, old_ucontext_param->padding, |
| 1132 | sizeof(ucontext_param->padding)); |
| 1133 | /* only type that uses the union */ |
| 1134 | if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) { |
| 1135 | ucontext_param->u.perf_counter.type = |
| 1136 | old_ucontext_param->u.perf_counter.type; |
| 1137 | ucontext_param->u.perf_counter.config = |
| 1138 | old_ucontext_param->u.perf_counter.config; |
| 1139 | memcpy(ucontext_param->u.perf_counter.name, |
| 1140 | old_ucontext_param->u.perf_counter.name, |
| 1141 | sizeof(ucontext_param->u.perf_counter.name)); |
| 1142 | } |
| 1143 | |
| 1144 | ret = lttng_abi_add_context(file, |
| 1145 | ucontext_param, |
| 1146 | &channel->ctx, channel->session); |
| 1147 | |
| 1148 | old_ctx_error_free_old_param: |
| 1149 | kfree(old_ucontext_param); |
| 1150 | old_ctx_error_free_param: |
| 1151 | kfree(ucontext_param); |
| 1152 | old_ctx_end: |
| 1153 | return ret; |
| 1154 | } |
| 1155 | case LTTNG_KERNEL_CONTEXT: |
| 1156 | { |
| 1157 | struct lttng_kernel_context ucontext_param; |
| 1158 | |
| 1159 | if (copy_from_user(&ucontext_param, |
| 1160 | (struct lttng_kernel_context __user *) arg, |
| 1161 | sizeof(ucontext_param))) |
| 1162 | return -EFAULT; |
| 1163 | return lttng_abi_add_context(file, |
| 1164 | &ucontext_param, |
| 1165 | &channel->ctx, channel->session); |
| 1166 | } |
| 1167 | case LTTNG_KERNEL_OLD_ENABLE: |
| 1168 | case LTTNG_KERNEL_ENABLE: |
| 1169 | return lttng_channel_enable(channel); |
| 1170 | case LTTNG_KERNEL_OLD_DISABLE: |
| 1171 | case LTTNG_KERNEL_DISABLE: |
| 1172 | return lttng_channel_disable(channel); |
| 1173 | default: |
| 1174 | return -ENOIOCTLCMD; |
| 1175 | } |
| 1176 | |
| 1177 | } |
| 1178 | |
| 1179 | /** |
| 1180 | * lttng_metadata_ioctl - lttng syscall through ioctl |
| 1181 | * |
| 1182 | * @file: the file |
| 1183 | * @cmd: the command |
| 1184 | * @arg: command arg |
| 1185 | * |
| 1186 | * This ioctl implements lttng commands: |
| 1187 | * LTTNG_KERNEL_STREAM |
| 1188 | * Returns an event stream file descriptor or failure. |
| 1189 | * |
| 1190 | * Channel and event file descriptors also hold a reference on the session. |
| 1191 | */ |
| 1192 | static |
| 1193 | long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 1194 | { |
| 1195 | switch (cmd) { |
| 1196 | case LTTNG_KERNEL_OLD_STREAM: |
| 1197 | case LTTNG_KERNEL_STREAM: |
| 1198 | return lttng_abi_open_metadata_stream(file); |
| 1199 | default: |
| 1200 | return -ENOIOCTLCMD; |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | /** |
| 1205 | * lttng_channel_poll - lttng stream addition/removal monitoring |
| 1206 | * |
| 1207 | * @file: the file |
| 1208 | * @wait: poll table |
| 1209 | */ |
| 1210 | unsigned int lttng_channel_poll(struct file *file, poll_table *wait) |
| 1211 | { |
| 1212 | struct lttng_channel *channel = file->private_data; |
| 1213 | unsigned int mask = 0; |
| 1214 | |
| 1215 | if (file->f_mode & FMODE_READ) { |
| 1216 | poll_wait_set_exclusive(wait); |
| 1217 | poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan), |
| 1218 | wait); |
| 1219 | |
| 1220 | if (channel->ops->is_disabled(channel->chan)) |
| 1221 | return POLLERR; |
| 1222 | if (channel->ops->is_finalized(channel->chan)) |
| 1223 | return POLLHUP; |
| 1224 | if (channel->ops->buffer_has_read_closed_stream(channel->chan)) |
| 1225 | return POLLIN | POLLRDNORM; |
| 1226 | return 0; |
| 1227 | } |
| 1228 | return mask; |
| 1229 | |
| 1230 | } |
| 1231 | |
| 1232 | static |
| 1233 | int lttng_channel_release(struct inode *inode, struct file *file) |
| 1234 | { |
| 1235 | struct lttng_channel *channel = file->private_data; |
| 1236 | |
| 1237 | if (channel) |
| 1238 | fput(channel->session->file); |
| 1239 | return 0; |
| 1240 | } |
| 1241 | |
| 1242 | static |
| 1243 | int lttng_metadata_channel_release(struct inode *inode, struct file *file) |
| 1244 | { |
| 1245 | struct lttng_channel *channel = file->private_data; |
| 1246 | |
| 1247 | if (channel) { |
| 1248 | lttng_metadata_channel_destroy(channel); |
| 1249 | fput(channel->session->file); |
| 1250 | } |
| 1251 | |
| 1252 | return 0; |
| 1253 | } |
| 1254 | |
| 1255 | static const struct file_operations lttng_channel_fops = { |
| 1256 | .owner = THIS_MODULE, |
| 1257 | .release = lttng_channel_release, |
| 1258 | .poll = lttng_channel_poll, |
| 1259 | .unlocked_ioctl = lttng_channel_ioctl, |
| 1260 | #ifdef CONFIG_COMPAT |
| 1261 | .compat_ioctl = lttng_channel_ioctl, |
| 1262 | #endif |
| 1263 | }; |
| 1264 | |
| 1265 | static const struct file_operations lttng_metadata_fops = { |
| 1266 | .owner = THIS_MODULE, |
| 1267 | .release = lttng_metadata_channel_release, |
| 1268 | .unlocked_ioctl = lttng_metadata_ioctl, |
| 1269 | #ifdef CONFIG_COMPAT |
| 1270 | .compat_ioctl = lttng_metadata_ioctl, |
| 1271 | #endif |
| 1272 | }; |
| 1273 | |
| 1274 | /** |
| 1275 | * lttng_event_ioctl - lttng syscall through ioctl |
| 1276 | * |
| 1277 | * @file: the file |
| 1278 | * @cmd: the command |
| 1279 | * @arg: command arg |
| 1280 | * |
| 1281 | * This ioctl implements lttng commands: |
| 1282 | * LTTNG_KERNEL_CONTEXT |
| 1283 | * Prepend a context field to each record of this event |
| 1284 | * LTTNG_KERNEL_ENABLE |
| 1285 | * Enable recording for this event (weak enable) |
| 1286 | * LTTNG_KERNEL_DISABLE |
| 1287 | * Disable recording for this event (strong disable) |
| 1288 | */ |
| 1289 | static |
| 1290 | long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 1291 | { |
| 1292 | struct lttng_event *event = file->private_data; |
| 1293 | |
| 1294 | switch (cmd) { |
| 1295 | case LTTNG_KERNEL_OLD_CONTEXT: |
| 1296 | { |
| 1297 | struct lttng_kernel_context *ucontext_param; |
| 1298 | struct lttng_kernel_old_context *old_ucontext_param; |
| 1299 | int ret; |
| 1300 | |
| 1301 | ucontext_param = kmalloc(sizeof(struct lttng_kernel_context), |
| 1302 | GFP_KERNEL); |
| 1303 | if (!ucontext_param) { |
| 1304 | ret = -ENOMEM; |
| 1305 | goto old_ctx_end; |
| 1306 | } |
| 1307 | old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context), |
| 1308 | GFP_KERNEL); |
| 1309 | if (!old_ucontext_param) { |
| 1310 | ret = -ENOMEM; |
| 1311 | goto old_ctx_error_free_param; |
| 1312 | } |
| 1313 | |
| 1314 | if (copy_from_user(old_ucontext_param, |
| 1315 | (struct lttng_kernel_old_context __user *) arg, |
| 1316 | sizeof(struct lttng_kernel_old_context))) { |
| 1317 | ret = -EFAULT; |
| 1318 | goto old_ctx_error_free_old_param; |
| 1319 | } |
| 1320 | ucontext_param->ctx = old_ucontext_param->ctx; |
| 1321 | memcpy(ucontext_param->padding, old_ucontext_param->padding, |
| 1322 | sizeof(ucontext_param->padding)); |
| 1323 | /* only type that uses the union */ |
| 1324 | if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) { |
| 1325 | ucontext_param->u.perf_counter.type = |
| 1326 | old_ucontext_param->u.perf_counter.type; |
| 1327 | ucontext_param->u.perf_counter.config = |
| 1328 | old_ucontext_param->u.perf_counter.config; |
| 1329 | memcpy(ucontext_param->u.perf_counter.name, |
| 1330 | old_ucontext_param->u.perf_counter.name, |
| 1331 | sizeof(ucontext_param->u.perf_counter.name)); |
| 1332 | } |
| 1333 | |
| 1334 | ret = lttng_abi_add_context(file, |
| 1335 | ucontext_param, |
| 1336 | &event->ctx, event->chan->session); |
| 1337 | |
| 1338 | old_ctx_error_free_old_param: |
| 1339 | kfree(old_ucontext_param); |
| 1340 | old_ctx_error_free_param: |
| 1341 | kfree(ucontext_param); |
| 1342 | old_ctx_end: |
| 1343 | return ret; |
| 1344 | } |
| 1345 | case LTTNG_KERNEL_CONTEXT: |
| 1346 | { |
| 1347 | struct lttng_kernel_context ucontext_param; |
| 1348 | |
| 1349 | if (copy_from_user(&ucontext_param, |
| 1350 | (struct lttng_kernel_context __user *) arg, |
| 1351 | sizeof(ucontext_param))) |
| 1352 | return -EFAULT; |
| 1353 | return lttng_abi_add_context(file, |
| 1354 | &ucontext_param, |
| 1355 | &event->ctx, event->chan->session); |
| 1356 | } |
| 1357 | case LTTNG_KERNEL_OLD_ENABLE: |
| 1358 | case LTTNG_KERNEL_ENABLE: |
| 1359 | return lttng_event_enable(event); |
| 1360 | case LTTNG_KERNEL_OLD_DISABLE: |
| 1361 | case LTTNG_KERNEL_DISABLE: |
| 1362 | return lttng_event_disable(event); |
| 1363 | default: |
| 1364 | return -ENOIOCTLCMD; |
| 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | static |
| 1369 | int lttng_event_release(struct inode *inode, struct file *file) |
| 1370 | { |
| 1371 | struct lttng_event *event = file->private_data; |
| 1372 | |
| 1373 | if (event) |
| 1374 | fput(event->chan->file); |
| 1375 | return 0; |
| 1376 | } |
| 1377 | |
| 1378 | /* TODO: filter control ioctl */ |
| 1379 | static const struct file_operations lttng_event_fops = { |
| 1380 | .owner = THIS_MODULE, |
| 1381 | .release = lttng_event_release, |
| 1382 | .unlocked_ioctl = lttng_event_ioctl, |
| 1383 | #ifdef CONFIG_COMPAT |
| 1384 | .compat_ioctl = lttng_event_ioctl, |
| 1385 | #endif |
| 1386 | }; |
| 1387 | |
| 1388 | static int put_u64(uint64_t val, unsigned long arg) |
| 1389 | { |
| 1390 | return put_user(val, (uint64_t __user *) arg); |
| 1391 | } |
| 1392 | |
| 1393 | static long lttng_stream_ring_buffer_ioctl(struct file *filp, |
| 1394 | unsigned int cmd, unsigned long arg) |
| 1395 | { |
| 1396 | struct lib_ring_buffer *buf = filp->private_data; |
| 1397 | struct channel *chan = buf->backend.chan; |
| 1398 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1399 | const struct lttng_channel_ops *ops = chan->backend.priv_ops; |
| 1400 | int ret; |
| 1401 | |
| 1402 | if (atomic_read(&chan->record_disabled)) |
| 1403 | return -EIO; |
| 1404 | |
| 1405 | switch (cmd) { |
| 1406 | case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN: |
| 1407 | { |
| 1408 | uint64_t ts; |
| 1409 | |
| 1410 | ret = ops->timestamp_begin(config, buf, &ts); |
| 1411 | if (ret < 0) |
| 1412 | goto error; |
| 1413 | return put_u64(ts, arg); |
| 1414 | } |
| 1415 | case LTTNG_RING_BUFFER_GET_TIMESTAMP_END: |
| 1416 | { |
| 1417 | uint64_t ts; |
| 1418 | |
| 1419 | ret = ops->timestamp_end(config, buf, &ts); |
| 1420 | if (ret < 0) |
| 1421 | goto error; |
| 1422 | return put_u64(ts, arg); |
| 1423 | } |
| 1424 | case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED: |
| 1425 | { |
| 1426 | uint64_t ed; |
| 1427 | |
| 1428 | ret = ops->events_discarded(config, buf, &ed); |
| 1429 | if (ret < 0) |
| 1430 | goto error; |
| 1431 | return put_u64(ed, arg); |
| 1432 | } |
| 1433 | case LTTNG_RING_BUFFER_GET_CONTENT_SIZE: |
| 1434 | { |
| 1435 | uint64_t cs; |
| 1436 | |
| 1437 | ret = ops->content_size(config, buf, &cs); |
| 1438 | if (ret < 0) |
| 1439 | goto error; |
| 1440 | return put_u64(cs, arg); |
| 1441 | } |
| 1442 | case LTTNG_RING_BUFFER_GET_PACKET_SIZE: |
| 1443 | { |
| 1444 | uint64_t ps; |
| 1445 | |
| 1446 | ret = ops->packet_size(config, buf, &ps); |
| 1447 | if (ret < 0) |
| 1448 | goto error; |
| 1449 | return put_u64(ps, arg); |
| 1450 | } |
| 1451 | case LTTNG_RING_BUFFER_GET_STREAM_ID: |
| 1452 | { |
| 1453 | uint64_t si; |
| 1454 | |
| 1455 | ret = ops->stream_id(config, buf, &si); |
| 1456 | if (ret < 0) |
| 1457 | goto error; |
| 1458 | return put_u64(si, arg); |
| 1459 | } |
| 1460 | case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP: |
| 1461 | { |
| 1462 | uint64_t ts; |
| 1463 | |
| 1464 | ret = ops->current_timestamp(config, buf, &ts); |
| 1465 | if (ret < 0) |
| 1466 | goto error; |
| 1467 | return put_u64(ts, arg); |
| 1468 | } |
| 1469 | default: |
| 1470 | return lib_ring_buffer_file_operations.unlocked_ioctl(filp, |
| 1471 | cmd, arg); |
| 1472 | } |
| 1473 | |
| 1474 | error: |
| 1475 | return -ENOSYS; |
| 1476 | } |
| 1477 | |
| 1478 | #ifdef CONFIG_COMPAT |
| 1479 | static long lttng_stream_ring_buffer_compat_ioctl(struct file *filp, |
| 1480 | unsigned int cmd, unsigned long arg) |
| 1481 | { |
| 1482 | struct lib_ring_buffer *buf = filp->private_data; |
| 1483 | struct channel *chan = buf->backend.chan; |
| 1484 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1485 | const struct lttng_channel_ops *ops = chan->backend.priv_ops; |
| 1486 | int ret; |
| 1487 | |
| 1488 | if (atomic_read(&chan->record_disabled)) |
| 1489 | return -EIO; |
| 1490 | |
| 1491 | switch (cmd) { |
| 1492 | case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN: |
| 1493 | { |
| 1494 | uint64_t ts; |
| 1495 | |
| 1496 | ret = ops->timestamp_begin(config, buf, &ts); |
| 1497 | if (ret < 0) |
| 1498 | goto error; |
| 1499 | return put_u64(ts, arg); |
| 1500 | } |
| 1501 | case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END: |
| 1502 | { |
| 1503 | uint64_t ts; |
| 1504 | |
| 1505 | ret = ops->timestamp_end(config, buf, &ts); |
| 1506 | if (ret < 0) |
| 1507 | goto error; |
| 1508 | return put_u64(ts, arg); |
| 1509 | } |
| 1510 | case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED: |
| 1511 | { |
| 1512 | uint64_t ed; |
| 1513 | |
| 1514 | ret = ops->events_discarded(config, buf, &ed); |
| 1515 | if (ret < 0) |
| 1516 | goto error; |
| 1517 | return put_u64(ed, arg); |
| 1518 | } |
| 1519 | case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE: |
| 1520 | { |
| 1521 | uint64_t cs; |
| 1522 | |
| 1523 | ret = ops->content_size(config, buf, &cs); |
| 1524 | if (ret < 0) |
| 1525 | goto error; |
| 1526 | return put_u64(cs, arg); |
| 1527 | } |
| 1528 | case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE: |
| 1529 | { |
| 1530 | uint64_t ps; |
| 1531 | |
| 1532 | ret = ops->packet_size(config, buf, &ps); |
| 1533 | if (ret < 0) |
| 1534 | goto error; |
| 1535 | return put_u64(ps, arg); |
| 1536 | } |
| 1537 | case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID: |
| 1538 | { |
| 1539 | uint64_t si; |
| 1540 | |
| 1541 | ret = ops->stream_id(config, buf, &si); |
| 1542 | if (ret < 0) |
| 1543 | goto error; |
| 1544 | return put_u64(si, arg); |
| 1545 | } |
| 1546 | case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP: |
| 1547 | { |
| 1548 | uint64_t ts; |
| 1549 | |
| 1550 | ret = ops->current_timestamp(config, buf, &ts); |
| 1551 | if (ret < 0) |
| 1552 | goto error; |
| 1553 | return put_u64(ts, arg); |
| 1554 | } |
| 1555 | default: |
| 1556 | return lib_ring_buffer_file_operations.compat_ioctl(filp, |
| 1557 | cmd, arg); |
| 1558 | } |
| 1559 | |
| 1560 | error: |
| 1561 | return -ENOSYS; |
| 1562 | } |
| 1563 | #endif /* CONFIG_COMPAT */ |
| 1564 | |
| 1565 | static void lttng_stream_override_ring_buffer_fops(void) |
| 1566 | { |
| 1567 | lttng_stream_ring_buffer_file_operations.owner = THIS_MODULE; |
| 1568 | lttng_stream_ring_buffer_file_operations.open = |
| 1569 | lib_ring_buffer_file_operations.open; |
| 1570 | lttng_stream_ring_buffer_file_operations.release = |
| 1571 | lib_ring_buffer_file_operations.release; |
| 1572 | lttng_stream_ring_buffer_file_operations.poll = |
| 1573 | lib_ring_buffer_file_operations.poll; |
| 1574 | lttng_stream_ring_buffer_file_operations.splice_read = |
| 1575 | lib_ring_buffer_file_operations.splice_read; |
| 1576 | lttng_stream_ring_buffer_file_operations.mmap = |
| 1577 | lib_ring_buffer_file_operations.mmap; |
| 1578 | lttng_stream_ring_buffer_file_operations.unlocked_ioctl = |
| 1579 | lttng_stream_ring_buffer_ioctl; |
| 1580 | lttng_stream_ring_buffer_file_operations.llseek = |
| 1581 | lib_ring_buffer_file_operations.llseek; |
| 1582 | #ifdef CONFIG_COMPAT |
| 1583 | lttng_stream_ring_buffer_file_operations.compat_ioctl = |
| 1584 | lttng_stream_ring_buffer_compat_ioctl; |
| 1585 | #endif |
| 1586 | } |
| 1587 | |
| 1588 | int __init lttng_abi_init(void) |
| 1589 | { |
| 1590 | int ret = 0; |
| 1591 | |
| 1592 | wrapper_vmalloc_sync_all(); |
| 1593 | lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL, |
| 1594 | <tng_fops, NULL); |
| 1595 | |
| 1596 | if (!lttng_proc_dentry) { |
| 1597 | printk(KERN_ERR "Error creating LTTng control file\n"); |
| 1598 | ret = -ENOMEM; |
| 1599 | goto error; |
| 1600 | } |
| 1601 | lttng_stream_override_ring_buffer_fops(); |
| 1602 | |
| 1603 | error: |
| 1604 | return ret; |
| 1605 | } |
| 1606 | |
| 1607 | /* No __exit annotation because used by init error path too. */ |
| 1608 | void lttng_abi_exit(void) |
| 1609 | { |
| 1610 | if (lttng_proc_dentry) |
| 1611 | remove_proc_entry("lttng", NULL); |
| 1612 | } |