| 1 | /* SPDX-License-Identifier: MIT |
| 2 | * |
| 3 | * lttng-filter.c |
| 4 | * |
| 5 | * LTTng modules filter code. |
| 6 | * |
| 7 | * Copyright (C) 2010-2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 8 | */ |
| 9 | |
| 10 | #include <linux/list.h> |
| 11 | #include <linux/slab.h> |
| 12 | |
| 13 | #include <lttng-filter.h> |
| 14 | |
| 15 | static const char *opnames[] = { |
| 16 | [ FILTER_OP_UNKNOWN ] = "UNKNOWN", |
| 17 | |
| 18 | [ FILTER_OP_RETURN ] = "RETURN", |
| 19 | |
| 20 | /* binary */ |
| 21 | [ FILTER_OP_MUL ] = "MUL", |
| 22 | [ FILTER_OP_DIV ] = "DIV", |
| 23 | [ FILTER_OP_MOD ] = "MOD", |
| 24 | [ FILTER_OP_PLUS ] = "PLUS", |
| 25 | [ FILTER_OP_MINUS ] = "MINUS", |
| 26 | [ FILTER_OP_BIT_RSHIFT ] = "BIT_RSHIFT", |
| 27 | [ FILTER_OP_BIT_LSHIFT ] = "BIT_LSHIFT", |
| 28 | [ FILTER_OP_BIT_AND ] = "BIT_AND", |
| 29 | [ FILTER_OP_BIT_OR ] = "BIT_OR", |
| 30 | [ FILTER_OP_BIT_XOR ] = "BIT_XOR", |
| 31 | |
| 32 | /* binary comparators */ |
| 33 | [ FILTER_OP_EQ ] = "EQ", |
| 34 | [ FILTER_OP_NE ] = "NE", |
| 35 | [ FILTER_OP_GT ] = "GT", |
| 36 | [ FILTER_OP_LT ] = "LT", |
| 37 | [ FILTER_OP_GE ] = "GE", |
| 38 | [ FILTER_OP_LE ] = "LE", |
| 39 | |
| 40 | /* string binary comparators */ |
| 41 | [ FILTER_OP_EQ_STRING ] = "EQ_STRING", |
| 42 | [ FILTER_OP_NE_STRING ] = "NE_STRING", |
| 43 | [ FILTER_OP_GT_STRING ] = "GT_STRING", |
| 44 | [ FILTER_OP_LT_STRING ] = "LT_STRING", |
| 45 | [ FILTER_OP_GE_STRING ] = "GE_STRING", |
| 46 | [ FILTER_OP_LE_STRING ] = "LE_STRING", |
| 47 | |
| 48 | /* s64 binary comparators */ |
| 49 | [ FILTER_OP_EQ_S64 ] = "EQ_S64", |
| 50 | [ FILTER_OP_NE_S64 ] = "NE_S64", |
| 51 | [ FILTER_OP_GT_S64 ] = "GT_S64", |
| 52 | [ FILTER_OP_LT_S64 ] = "LT_S64", |
| 53 | [ FILTER_OP_GE_S64 ] = "GE_S64", |
| 54 | [ FILTER_OP_LE_S64 ] = "LE_S64", |
| 55 | |
| 56 | /* double binary comparators */ |
| 57 | [ FILTER_OP_EQ_DOUBLE ] = "EQ_DOUBLE", |
| 58 | [ FILTER_OP_NE_DOUBLE ] = "NE_DOUBLE", |
| 59 | [ FILTER_OP_GT_DOUBLE ] = "GT_DOUBLE", |
| 60 | [ FILTER_OP_LT_DOUBLE ] = "LT_DOUBLE", |
| 61 | [ FILTER_OP_GE_DOUBLE ] = "GE_DOUBLE", |
| 62 | [ FILTER_OP_LE_DOUBLE ] = "LE_DOUBLE", |
| 63 | |
| 64 | /* Mixed S64-double binary comparators */ |
| 65 | [ FILTER_OP_EQ_DOUBLE_S64 ] = "EQ_DOUBLE_S64", |
| 66 | [ FILTER_OP_NE_DOUBLE_S64 ] = "NE_DOUBLE_S64", |
| 67 | [ FILTER_OP_GT_DOUBLE_S64 ] = "GT_DOUBLE_S64", |
| 68 | [ FILTER_OP_LT_DOUBLE_S64 ] = "LT_DOUBLE_S64", |
| 69 | [ FILTER_OP_GE_DOUBLE_S64 ] = "GE_DOUBLE_S64", |
| 70 | [ FILTER_OP_LE_DOUBLE_S64 ] = "LE_DOUBLE_S64", |
| 71 | |
| 72 | [ FILTER_OP_EQ_S64_DOUBLE ] = "EQ_S64_DOUBLE", |
| 73 | [ FILTER_OP_NE_S64_DOUBLE ] = "NE_S64_DOUBLE", |
| 74 | [ FILTER_OP_GT_S64_DOUBLE ] = "GT_S64_DOUBLE", |
| 75 | [ FILTER_OP_LT_S64_DOUBLE ] = "LT_S64_DOUBLE", |
| 76 | [ FILTER_OP_GE_S64_DOUBLE ] = "GE_S64_DOUBLE", |
| 77 | [ FILTER_OP_LE_S64_DOUBLE ] = "LE_S64_DOUBLE", |
| 78 | |
| 79 | /* unary */ |
| 80 | [ FILTER_OP_UNARY_PLUS ] = "UNARY_PLUS", |
| 81 | [ FILTER_OP_UNARY_MINUS ] = "UNARY_MINUS", |
| 82 | [ FILTER_OP_UNARY_NOT ] = "UNARY_NOT", |
| 83 | [ FILTER_OP_UNARY_PLUS_S64 ] = "UNARY_PLUS_S64", |
| 84 | [ FILTER_OP_UNARY_MINUS_S64 ] = "UNARY_MINUS_S64", |
| 85 | [ FILTER_OP_UNARY_NOT_S64 ] = "UNARY_NOT_S64", |
| 86 | [ FILTER_OP_UNARY_PLUS_DOUBLE ] = "UNARY_PLUS_DOUBLE", |
| 87 | [ FILTER_OP_UNARY_MINUS_DOUBLE ] = "UNARY_MINUS_DOUBLE", |
| 88 | [ FILTER_OP_UNARY_NOT_DOUBLE ] = "UNARY_NOT_DOUBLE", |
| 89 | |
| 90 | /* logical */ |
| 91 | [ FILTER_OP_AND ] = "AND", |
| 92 | [ FILTER_OP_OR ] = "OR", |
| 93 | |
| 94 | /* load field ref */ |
| 95 | [ FILTER_OP_LOAD_FIELD_REF ] = "LOAD_FIELD_REF", |
| 96 | [ FILTER_OP_LOAD_FIELD_REF_STRING ] = "LOAD_FIELD_REF_STRING", |
| 97 | [ FILTER_OP_LOAD_FIELD_REF_SEQUENCE ] = "LOAD_FIELD_REF_SEQUENCE", |
| 98 | [ FILTER_OP_LOAD_FIELD_REF_S64 ] = "LOAD_FIELD_REF_S64", |
| 99 | [ FILTER_OP_LOAD_FIELD_REF_DOUBLE ] = "LOAD_FIELD_REF_DOUBLE", |
| 100 | |
| 101 | /* load from immediate operand */ |
| 102 | [ FILTER_OP_LOAD_STRING ] = "LOAD_STRING", |
| 103 | [ FILTER_OP_LOAD_S64 ] = "LOAD_S64", |
| 104 | [ FILTER_OP_LOAD_DOUBLE ] = "LOAD_DOUBLE", |
| 105 | |
| 106 | /* cast */ |
| 107 | [ FILTER_OP_CAST_TO_S64 ] = "CAST_TO_S64", |
| 108 | [ FILTER_OP_CAST_DOUBLE_TO_S64 ] = "CAST_DOUBLE_TO_S64", |
| 109 | [ FILTER_OP_CAST_NOP ] = "CAST_NOP", |
| 110 | |
| 111 | /* get context ref */ |
| 112 | [ FILTER_OP_GET_CONTEXT_REF ] = "GET_CONTEXT_REF", |
| 113 | [ FILTER_OP_GET_CONTEXT_REF_STRING ] = "GET_CONTEXT_REF_STRING", |
| 114 | [ FILTER_OP_GET_CONTEXT_REF_S64 ] = "GET_CONTEXT_REF_S64", |
| 115 | [ FILTER_OP_GET_CONTEXT_REF_DOUBLE ] = "GET_CONTEXT_REF_DOUBLE", |
| 116 | |
| 117 | /* load userspace field ref */ |
| 118 | [ FILTER_OP_LOAD_FIELD_REF_USER_STRING ] = "LOAD_FIELD_REF_USER_STRING", |
| 119 | [ FILTER_OP_LOAD_FIELD_REF_USER_SEQUENCE ] = "LOAD_FIELD_REF_USER_SEQUENCE", |
| 120 | |
| 121 | /* |
| 122 | * load immediate star globbing pattern (literal string) |
| 123 | * from immediate. |
| 124 | */ |
| 125 | [ FILTER_OP_LOAD_STAR_GLOB_STRING ] = "LOAD_STAR_GLOB_STRING", |
| 126 | |
| 127 | /* globbing pattern binary operator: apply to */ |
| 128 | [ FILTER_OP_EQ_STAR_GLOB_STRING ] = "EQ_STAR_GLOB_STRING", |
| 129 | [ FILTER_OP_NE_STAR_GLOB_STRING ] = "NE_STAR_GLOB_STRING", |
| 130 | |
| 131 | /* |
| 132 | * Instructions for recursive traversal through composed types. |
| 133 | */ |
| 134 | [ FILTER_OP_GET_CONTEXT_ROOT ] = "GET_CONTEXT_ROOT", |
| 135 | [ FILTER_OP_GET_APP_CONTEXT_ROOT ] = "GET_APP_CONTEXT_ROOT", |
| 136 | [ FILTER_OP_GET_PAYLOAD_ROOT ] = "GET_PAYLOAD_ROOT", |
| 137 | |
| 138 | [ FILTER_OP_GET_SYMBOL ] = "GET_SYMBOL", |
| 139 | [ FILTER_OP_GET_SYMBOL_FIELD ] = "GET_SYMBOL_FIELD", |
| 140 | [ FILTER_OP_GET_INDEX_U16 ] = "GET_INDEX_U16", |
| 141 | [ FILTER_OP_GET_INDEX_U64 ] = "GET_INDEX_U64", |
| 142 | |
| 143 | [ FILTER_OP_LOAD_FIELD ] = "LOAD_FIELD", |
| 144 | [ FILTER_OP_LOAD_FIELD_S8 ] = "LOAD_FIELD_S8", |
| 145 | [ FILTER_OP_LOAD_FIELD_S16 ] = "LOAD_FIELD_S16", |
| 146 | [ FILTER_OP_LOAD_FIELD_S32 ] = "LOAD_FIELD_S32", |
| 147 | [ FILTER_OP_LOAD_FIELD_S64 ] = "LOAD_FIELD_S64", |
| 148 | [ FILTER_OP_LOAD_FIELD_U8 ] = "LOAD_FIELD_U8", |
| 149 | [ FILTER_OP_LOAD_FIELD_U16 ] = "LOAD_FIELD_U16", |
| 150 | [ FILTER_OP_LOAD_FIELD_U32 ] = "LOAD_FIELD_U32", |
| 151 | [ FILTER_OP_LOAD_FIELD_U64 ] = "LOAD_FIELD_U64", |
| 152 | [ FILTER_OP_LOAD_FIELD_STRING ] = "LOAD_FIELD_STRING", |
| 153 | [ FILTER_OP_LOAD_FIELD_SEQUENCE ] = "LOAD_FIELD_SEQUENCE", |
| 154 | [ FILTER_OP_LOAD_FIELD_DOUBLE ] = "LOAD_FIELD_DOUBLE", |
| 155 | |
| 156 | [ FILTER_OP_UNARY_BIT_NOT ] = "UNARY_BIT_NOT", |
| 157 | |
| 158 | [ FILTER_OP_RETURN_S64 ] = "RETURN_S64", |
| 159 | }; |
| 160 | |
| 161 | const char *lttng_filter_print_op(enum filter_op op) |
| 162 | { |
| 163 | if (op >= NR_FILTER_OPS) |
| 164 | return "UNKNOWN"; |
| 165 | else |
| 166 | return opnames[op]; |
| 167 | } |
| 168 | |
| 169 | static |
| 170 | int apply_field_reloc(struct lttng_event *event, |
| 171 | struct bytecode_runtime *runtime, |
| 172 | uint32_t runtime_len, |
| 173 | uint32_t reloc_offset, |
| 174 | const char *field_name, |
| 175 | enum filter_op filter_op) |
| 176 | { |
| 177 | const struct lttng_event_desc *desc; |
| 178 | const struct lttng_event_field *fields, *field = NULL; |
| 179 | unsigned int nr_fields, i; |
| 180 | struct load_op *op; |
| 181 | uint32_t field_offset = 0; |
| 182 | |
| 183 | dbg_printk("Apply field reloc: %u %s\n", reloc_offset, field_name); |
| 184 | |
| 185 | /* Lookup event by name */ |
| 186 | desc = event->desc; |
| 187 | if (!desc) |
| 188 | return -EINVAL; |
| 189 | fields = desc->fields; |
| 190 | if (!fields) |
| 191 | return -EINVAL; |
| 192 | nr_fields = desc->nr_fields; |
| 193 | for (i = 0; i < nr_fields; i++) { |
| 194 | if (!strcmp(fields[i].name, field_name)) { |
| 195 | field = &fields[i]; |
| 196 | break; |
| 197 | } |
| 198 | /* compute field offset */ |
| 199 | switch (fields[i].type.atype) { |
| 200 | case atype_integer: |
| 201 | case atype_enum: |
| 202 | field_offset += sizeof(int64_t); |
| 203 | break; |
| 204 | case atype_array: |
| 205 | case atype_sequence: |
| 206 | case atype_array_bitfield: |
| 207 | case atype_sequence_bitfield: |
| 208 | field_offset += sizeof(unsigned long); |
| 209 | field_offset += sizeof(void *); |
| 210 | break; |
| 211 | case atype_string: |
| 212 | field_offset += sizeof(void *); |
| 213 | break; |
| 214 | case atype_struct: /* Unsupported. */ |
| 215 | case atype_array_compound: /* Unsupported. */ |
| 216 | case atype_sequence_compound: /* Unsupported. */ |
| 217 | case atype_variant: /* Unsupported. */ |
| 218 | default: |
| 219 | return -EINVAL; |
| 220 | } |
| 221 | } |
| 222 | if (!field) |
| 223 | return -EINVAL; |
| 224 | |
| 225 | /* Check if field offset is too large for 16-bit offset */ |
| 226 | if (field_offset > LTTNG_KERNEL_FILTER_BYTECODE_MAX_LEN - 1) |
| 227 | return -EINVAL; |
| 228 | |
| 229 | /* set type */ |
| 230 | op = (struct load_op *) &runtime->code[reloc_offset]; |
| 231 | |
| 232 | switch (filter_op) { |
| 233 | case FILTER_OP_LOAD_FIELD_REF: |
| 234 | { |
| 235 | struct field_ref *field_ref; |
| 236 | |
| 237 | field_ref = (struct field_ref *) op->data; |
| 238 | switch (field->type.atype) { |
| 239 | case atype_integer: |
| 240 | case atype_enum: |
| 241 | op->op = FILTER_OP_LOAD_FIELD_REF_S64; |
| 242 | break; |
| 243 | case atype_array: |
| 244 | { |
| 245 | const struct lttng_basic_type *elem_type = &field->type.u.array.elem_type; |
| 246 | |
| 247 | if (elem_type->atype != atype_integer || elem_type->u.basic.integer.encoding == lttng_encode_none) |
| 248 | return -EINVAL; |
| 249 | if (elem_type->u.basic.integer.user) |
| 250 | op->op = FILTER_OP_LOAD_FIELD_REF_USER_SEQUENCE; |
| 251 | else |
| 252 | op->op = FILTER_OP_LOAD_FIELD_REF_SEQUENCE; |
| 253 | break; |
| 254 | } |
| 255 | case atype_sequence: |
| 256 | { |
| 257 | const struct lttng_basic_type *elem_type = &field->type.u.sequence.elem_type; |
| 258 | |
| 259 | if (elem_type->atype != atype_integer || elem_type->u.basic.integer.encoding == lttng_encode_none) |
| 260 | return -EINVAL; |
| 261 | if (elem_type->u.basic.integer.user) |
| 262 | op->op = FILTER_OP_LOAD_FIELD_REF_USER_SEQUENCE; |
| 263 | else |
| 264 | op->op = FILTER_OP_LOAD_FIELD_REF_SEQUENCE; |
| 265 | break; |
| 266 | } |
| 267 | case atype_string: |
| 268 | if (field->type.u.basic.string.user) |
| 269 | op->op = FILTER_OP_LOAD_FIELD_REF_USER_STRING; |
| 270 | else |
| 271 | op->op = FILTER_OP_LOAD_FIELD_REF_STRING; |
| 272 | break; |
| 273 | case atype_struct: /* Unsupported. */ |
| 274 | case atype_array_compound: /* Unsupported. */ |
| 275 | case atype_sequence_compound: /* Unsupported. */ |
| 276 | case atype_variant: /* Unsupported. */ |
| 277 | case atype_array_bitfield: /* Unsupported. */ |
| 278 | case atype_sequence_bitfield: /* Unsupported. */ |
| 279 | default: |
| 280 | return -EINVAL; |
| 281 | } |
| 282 | /* set offset */ |
| 283 | field_ref->offset = (uint16_t) field_offset; |
| 284 | break; |
| 285 | } |
| 286 | default: |
| 287 | return -EINVAL; |
| 288 | } |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | static |
| 293 | int apply_context_reloc(struct lttng_event *event, |
| 294 | struct bytecode_runtime *runtime, |
| 295 | uint32_t runtime_len, |
| 296 | uint32_t reloc_offset, |
| 297 | const char *context_name, |
| 298 | enum filter_op filter_op) |
| 299 | { |
| 300 | struct load_op *op; |
| 301 | struct lttng_ctx_field *ctx_field; |
| 302 | int idx; |
| 303 | |
| 304 | dbg_printk("Apply context reloc: %u %s\n", reloc_offset, context_name); |
| 305 | |
| 306 | /* Get context index */ |
| 307 | idx = lttng_get_context_index(lttng_static_ctx, context_name); |
| 308 | if (idx < 0) |
| 309 | return -ENOENT; |
| 310 | |
| 311 | /* Check if idx is too large for 16-bit offset */ |
| 312 | if (idx > LTTNG_KERNEL_FILTER_BYTECODE_MAX_LEN - 1) |
| 313 | return -EINVAL; |
| 314 | |
| 315 | /* Get context return type */ |
| 316 | ctx_field = <tng_static_ctx->fields[idx]; |
| 317 | op = (struct load_op *) &runtime->code[reloc_offset]; |
| 318 | |
| 319 | switch (filter_op) { |
| 320 | case FILTER_OP_GET_CONTEXT_REF: |
| 321 | { |
| 322 | struct field_ref *field_ref; |
| 323 | |
| 324 | field_ref = (struct field_ref *) op->data; |
| 325 | switch (ctx_field->event_field.type.atype) { |
| 326 | case atype_integer: |
| 327 | case atype_enum: |
| 328 | op->op = FILTER_OP_GET_CONTEXT_REF_S64; |
| 329 | break; |
| 330 | /* Sequence and array supported as string */ |
| 331 | case atype_array: |
| 332 | { |
| 333 | const struct lttng_basic_type *elem_type = &ctx_field->event_field.type.u.array.elem_type; |
| 334 | |
| 335 | if (elem_type->atype != atype_integer || elem_type->u.basic.integer.encoding == lttng_encode_none) |
| 336 | return -EINVAL; |
| 337 | BUG_ON(elem_type->u.basic.integer.user); |
| 338 | op->op = FILTER_OP_GET_CONTEXT_REF_STRING; |
| 339 | break; |
| 340 | } |
| 341 | case atype_sequence: |
| 342 | { |
| 343 | const struct lttng_basic_type *elem_type = &ctx_field->event_field.type.u.sequence.elem_type; |
| 344 | |
| 345 | if (elem_type->atype != atype_integer || elem_type->u.basic.integer.encoding == lttng_encode_none) |
| 346 | return -EINVAL; |
| 347 | BUG_ON(elem_type->u.basic.integer.user); |
| 348 | op->op = FILTER_OP_GET_CONTEXT_REF_STRING; |
| 349 | break; |
| 350 | } |
| 351 | case atype_string: |
| 352 | BUG_ON(ctx_field->event_field.type.u.basic.string.user); |
| 353 | op->op = FILTER_OP_GET_CONTEXT_REF_STRING; |
| 354 | break; |
| 355 | case atype_struct: /* Unsupported. */ |
| 356 | case atype_array_compound: /* Unsupported. */ |
| 357 | case atype_sequence_compound: /* Unsupported. */ |
| 358 | case atype_variant: /* Unsupported. */ |
| 359 | case atype_array_bitfield: /* Unsupported. */ |
| 360 | case atype_sequence_bitfield: /* Unsupported. */ |
| 361 | default: |
| 362 | return -EINVAL; |
| 363 | } |
| 364 | /* set offset to context index within channel contexts */ |
| 365 | field_ref->offset = (uint16_t) idx; |
| 366 | break; |
| 367 | } |
| 368 | default: |
| 369 | return -EINVAL; |
| 370 | } |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | static |
| 375 | int apply_reloc(struct lttng_event *event, |
| 376 | struct bytecode_runtime *runtime, |
| 377 | uint32_t runtime_len, |
| 378 | uint32_t reloc_offset, |
| 379 | const char *name) |
| 380 | { |
| 381 | struct load_op *op; |
| 382 | |
| 383 | dbg_printk("Apply reloc: %u %s\n", reloc_offset, name); |
| 384 | |
| 385 | /* Ensure that the reloc is within the code */ |
| 386 | if (runtime_len - reloc_offset < sizeof(uint16_t)) |
| 387 | return -EINVAL; |
| 388 | |
| 389 | op = (struct load_op *) &runtime->code[reloc_offset]; |
| 390 | switch (op->op) { |
| 391 | case FILTER_OP_LOAD_FIELD_REF: |
| 392 | return apply_field_reloc(event, runtime, runtime_len, |
| 393 | reloc_offset, name, op->op); |
| 394 | case FILTER_OP_GET_CONTEXT_REF: |
| 395 | return apply_context_reloc(event, runtime, runtime_len, |
| 396 | reloc_offset, name, op->op); |
| 397 | case FILTER_OP_GET_SYMBOL: |
| 398 | case FILTER_OP_GET_SYMBOL_FIELD: |
| 399 | /* |
| 400 | * Will be handled by load specialize phase or |
| 401 | * dynamically by interpreter. |
| 402 | */ |
| 403 | return 0; |
| 404 | default: |
| 405 | printk(KERN_WARNING "Unknown reloc op type %u\n", op->op); |
| 406 | return -EINVAL; |
| 407 | } |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | static |
| 412 | int bytecode_is_linked(struct lttng_filter_bytecode_node *filter_bytecode, |
| 413 | struct lttng_event *event) |
| 414 | { |
| 415 | struct lttng_bytecode_runtime *bc_runtime; |
| 416 | |
| 417 | list_for_each_entry(bc_runtime, |
| 418 | &event->bytecode_runtime_head, node) { |
| 419 | if (bc_runtime->bc == filter_bytecode) |
| 420 | return 1; |
| 421 | } |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | /* |
| 426 | * Take a bytecode with reloc table and link it to an event to create a |
| 427 | * bytecode runtime. |
| 428 | */ |
| 429 | static |
| 430 | int _lttng_filter_event_link_bytecode(struct lttng_event *event, |
| 431 | struct lttng_filter_bytecode_node *filter_bytecode, |
| 432 | struct list_head *insert_loc) |
| 433 | { |
| 434 | int ret, offset, next_offset; |
| 435 | struct bytecode_runtime *runtime = NULL; |
| 436 | size_t runtime_alloc_len; |
| 437 | |
| 438 | if (!filter_bytecode) |
| 439 | return 0; |
| 440 | /* Bytecode already linked */ |
| 441 | if (bytecode_is_linked(filter_bytecode, event)) |
| 442 | return 0; |
| 443 | |
| 444 | dbg_printk("Linking...\n"); |
| 445 | |
| 446 | /* We don't need the reloc table in the runtime */ |
| 447 | runtime_alloc_len = sizeof(*runtime) + filter_bytecode->bc.reloc_offset; |
| 448 | runtime = kzalloc(runtime_alloc_len, GFP_KERNEL); |
| 449 | if (!runtime) { |
| 450 | ret = -ENOMEM; |
| 451 | goto alloc_error; |
| 452 | } |
| 453 | runtime->p.bc = filter_bytecode; |
| 454 | runtime->p.event = event; |
| 455 | runtime->len = filter_bytecode->bc.reloc_offset; |
| 456 | /* copy original bytecode */ |
| 457 | memcpy(runtime->code, filter_bytecode->bc.data, runtime->len); |
| 458 | /* Validate bytecode load instructions before relocs. */ |
| 459 | ret = lttng_filter_validate_bytecode_load(runtime); |
| 460 | if (ret) { |
| 461 | goto link_error; |
| 462 | } |
| 463 | /* |
| 464 | * apply relocs. Those are a uint16_t (offset in bytecode) |
| 465 | * followed by a string (field name). |
| 466 | */ |
| 467 | for (offset = filter_bytecode->bc.reloc_offset; |
| 468 | offset < filter_bytecode->bc.len; |
| 469 | offset = next_offset) { |
| 470 | uint16_t reloc_offset = |
| 471 | *(uint16_t *) &filter_bytecode->bc.data[offset]; |
| 472 | const char *name = |
| 473 | (const char *) &filter_bytecode->bc.data[offset + sizeof(uint16_t)]; |
| 474 | |
| 475 | ret = apply_reloc(event, runtime, runtime->len, reloc_offset, name); |
| 476 | if (ret) { |
| 477 | goto link_error; |
| 478 | } |
| 479 | next_offset = offset + sizeof(uint16_t) + strlen(name) + 1; |
| 480 | } |
| 481 | /* Validate bytecode */ |
| 482 | ret = lttng_filter_validate_bytecode(runtime); |
| 483 | if (ret) { |
| 484 | goto link_error; |
| 485 | } |
| 486 | /* Specialize bytecode */ |
| 487 | ret = lttng_filter_specialize_bytecode(event, runtime); |
| 488 | if (ret) { |
| 489 | goto link_error; |
| 490 | } |
| 491 | runtime->p.filter = lttng_filter_interpret_bytecode; |
| 492 | runtime->p.link_failed = 0; |
| 493 | list_add_rcu(&runtime->p.node, insert_loc); |
| 494 | dbg_printk("Linking successful.\n"); |
| 495 | return 0; |
| 496 | |
| 497 | link_error: |
| 498 | runtime->p.filter = lttng_filter_false; |
| 499 | runtime->p.link_failed = 1; |
| 500 | list_add_rcu(&runtime->p.node, insert_loc); |
| 501 | alloc_error: |
| 502 | dbg_printk("Linking failed.\n"); |
| 503 | return ret; |
| 504 | } |
| 505 | |
| 506 | void lttng_filter_sync_state(struct lttng_bytecode_runtime *runtime) |
| 507 | { |
| 508 | struct lttng_filter_bytecode_node *bc = runtime->bc; |
| 509 | |
| 510 | if (!bc->enabler->enabled || runtime->link_failed) |
| 511 | runtime->filter = lttng_filter_false; |
| 512 | else |
| 513 | runtime->filter = lttng_filter_interpret_bytecode; |
| 514 | } |
| 515 | |
| 516 | /* |
| 517 | * Link bytecode for all enablers referenced by an event. |
| 518 | */ |
| 519 | void lttng_enabler_event_link_bytecode(struct lttng_event *event, |
| 520 | struct lttng_enabler *enabler) |
| 521 | { |
| 522 | struct lttng_filter_bytecode_node *bc; |
| 523 | struct lttng_bytecode_runtime *runtime; |
| 524 | |
| 525 | /* Can only be called for events with desc attached */ |
| 526 | WARN_ON_ONCE(!event->desc); |
| 527 | |
| 528 | /* Link each bytecode. */ |
| 529 | list_for_each_entry(bc, &enabler->filter_bytecode_head, node) { |
| 530 | int found = 0, ret; |
| 531 | struct list_head *insert_loc; |
| 532 | |
| 533 | list_for_each_entry(runtime, |
| 534 | &event->bytecode_runtime_head, node) { |
| 535 | if (runtime->bc == bc) { |
| 536 | found = 1; |
| 537 | break; |
| 538 | } |
| 539 | } |
| 540 | /* Skip bytecode already linked */ |
| 541 | if (found) |
| 542 | continue; |
| 543 | |
| 544 | /* |
| 545 | * Insert at specified priority (seqnum) in increasing |
| 546 | * order. |
| 547 | */ |
| 548 | list_for_each_entry_reverse(runtime, |
| 549 | &event->bytecode_runtime_head, node) { |
| 550 | if (runtime->bc->bc.seqnum < bc->bc.seqnum) { |
| 551 | /* insert here */ |
| 552 | insert_loc = &runtime->node; |
| 553 | goto add_within; |
| 554 | } |
| 555 | } |
| 556 | /* Add to head to list */ |
| 557 | insert_loc = &event->bytecode_runtime_head; |
| 558 | add_within: |
| 559 | dbg_printk("linking bytecode\n"); |
| 560 | ret = _lttng_filter_event_link_bytecode(event, bc, |
| 561 | insert_loc); |
| 562 | if (ret) { |
| 563 | dbg_printk("[lttng filter] warning: cannot link event bytecode\n"); |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | /* |
| 569 | * We own the filter_bytecode if we return success. |
| 570 | */ |
| 571 | int lttng_filter_enabler_attach_bytecode(struct lttng_enabler *enabler, |
| 572 | struct lttng_filter_bytecode_node *filter_bytecode) |
| 573 | { |
| 574 | list_add(&filter_bytecode->node, &enabler->filter_bytecode_head); |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | void lttng_free_enabler_filter_bytecode(struct lttng_enabler *enabler) |
| 579 | { |
| 580 | struct lttng_filter_bytecode_node *filter_bytecode, *tmp; |
| 581 | |
| 582 | list_for_each_entry_safe(filter_bytecode, tmp, |
| 583 | &enabler->filter_bytecode_head, node) { |
| 584 | kfree(filter_bytecode); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | void lttng_free_event_filter_runtime(struct lttng_event *event) |
| 589 | { |
| 590 | struct bytecode_runtime *runtime, *tmp; |
| 591 | |
| 592 | list_for_each_entry_safe(runtime, tmp, |
| 593 | &event->bytecode_runtime_head, p.node) { |
| 594 | kfree(runtime->data); |
| 595 | kfree(runtime); |
| 596 | } |
| 597 | } |