Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * event tracer |
| 4 | * |
| 5 | * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com> |
| 6 | * |
| 7 | * - Added format output of fields of the trace point. |
| 8 | * This was based off of work by Tom Zanussi <tzanussi@gmail.com>. |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | #define pr_fmt(fmt) fmt |
| 13 | |
| 14 | #include <linux/workqueue.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 15 | #include <linux/security.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 16 | #include <linux/spinlock.h> |
| 17 | #include <linux/kthread.h> |
| 18 | #include <linux/tracefs.h> |
| 19 | #include <linux/uaccess.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/ctype.h> |
| 22 | #include <linux/sort.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/delay.h> |
| 25 | |
| 26 | #include <trace/events/sched.h> |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 27 | #include <trace/syscall.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 28 | |
| 29 | #include <asm/setup.h> |
| 30 | |
| 31 | #include "trace_output.h" |
| 32 | |
| 33 | #undef TRACE_SYSTEM |
| 34 | #define TRACE_SYSTEM "TRACE_SYSTEM" |
| 35 | |
| 36 | DEFINE_MUTEX(event_mutex); |
| 37 | |
| 38 | LIST_HEAD(ftrace_events); |
| 39 | static LIST_HEAD(ftrace_generic_fields); |
| 40 | static LIST_HEAD(ftrace_common_fields); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 41 | static bool eventdir_initialized; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 42 | |
| 43 | #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO) |
| 44 | |
| 45 | static struct kmem_cache *field_cachep; |
| 46 | static struct kmem_cache *file_cachep; |
| 47 | |
| 48 | static inline int system_refcount(struct event_subsystem *system) |
| 49 | { |
| 50 | return system->ref_count; |
| 51 | } |
| 52 | |
| 53 | static int system_refcount_inc(struct event_subsystem *system) |
| 54 | { |
| 55 | return system->ref_count++; |
| 56 | } |
| 57 | |
| 58 | static int system_refcount_dec(struct event_subsystem *system) |
| 59 | { |
| 60 | return --system->ref_count; |
| 61 | } |
| 62 | |
| 63 | /* Double loops, do not use break, only goto's work */ |
| 64 | #define do_for_each_event_file(tr, file) \ |
| 65 | list_for_each_entry(tr, &ftrace_trace_arrays, list) { \ |
| 66 | list_for_each_entry(file, &tr->events, list) |
| 67 | |
| 68 | #define do_for_each_event_file_safe(tr, file) \ |
| 69 | list_for_each_entry(tr, &ftrace_trace_arrays, list) { \ |
| 70 | struct trace_event_file *___n; \ |
| 71 | list_for_each_entry_safe(file, ___n, &tr->events, list) |
| 72 | |
| 73 | #define while_for_each_event_file() \ |
| 74 | } |
| 75 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 76 | static struct ftrace_event_field * |
| 77 | __find_event_field(struct list_head *head, char *name) |
| 78 | { |
| 79 | struct ftrace_event_field *field; |
| 80 | |
| 81 | list_for_each_entry(field, head, link) { |
| 82 | if (!strcmp(field->name, name)) |
| 83 | return field; |
| 84 | } |
| 85 | |
| 86 | return NULL; |
| 87 | } |
| 88 | |
| 89 | struct ftrace_event_field * |
| 90 | trace_find_event_field(struct trace_event_call *call, char *name) |
| 91 | { |
| 92 | struct ftrace_event_field *field; |
| 93 | struct list_head *head; |
| 94 | |
| 95 | head = trace_get_fields(call); |
| 96 | field = __find_event_field(head, name); |
| 97 | if (field) |
| 98 | return field; |
| 99 | |
| 100 | field = __find_event_field(&ftrace_generic_fields, name); |
| 101 | if (field) |
| 102 | return field; |
| 103 | |
| 104 | return __find_event_field(&ftrace_common_fields, name); |
| 105 | } |
| 106 | |
| 107 | static int __trace_define_field(struct list_head *head, const char *type, |
| 108 | const char *name, int offset, int size, |
| 109 | int is_signed, int filter_type) |
| 110 | { |
| 111 | struct ftrace_event_field *field; |
| 112 | |
| 113 | field = kmem_cache_alloc(field_cachep, GFP_TRACE); |
| 114 | if (!field) |
| 115 | return -ENOMEM; |
| 116 | |
| 117 | field->name = name; |
| 118 | field->type = type; |
| 119 | |
| 120 | if (filter_type == FILTER_OTHER) |
| 121 | field->filter_type = filter_assign_type(type); |
| 122 | else |
| 123 | field->filter_type = filter_type; |
| 124 | |
| 125 | field->offset = offset; |
| 126 | field->size = size; |
| 127 | field->is_signed = is_signed; |
| 128 | |
| 129 | list_add(&field->link, head); |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | int trace_define_field(struct trace_event_call *call, const char *type, |
| 135 | const char *name, int offset, int size, int is_signed, |
| 136 | int filter_type) |
| 137 | { |
| 138 | struct list_head *head; |
| 139 | |
| 140 | if (WARN_ON(!call->class)) |
| 141 | return 0; |
| 142 | |
| 143 | head = trace_get_fields(call); |
| 144 | return __trace_define_field(head, type, name, offset, size, |
| 145 | is_signed, filter_type); |
| 146 | } |
| 147 | EXPORT_SYMBOL_GPL(trace_define_field); |
| 148 | |
| 149 | #define __generic_field(type, item, filter_type) \ |
| 150 | ret = __trace_define_field(&ftrace_generic_fields, #type, \ |
| 151 | #item, 0, 0, is_signed_type(type), \ |
| 152 | filter_type); \ |
| 153 | if (ret) \ |
| 154 | return ret; |
| 155 | |
| 156 | #define __common_field(type, item) \ |
| 157 | ret = __trace_define_field(&ftrace_common_fields, #type, \ |
| 158 | "common_" #item, \ |
| 159 | offsetof(typeof(ent), item), \ |
| 160 | sizeof(ent.item), \ |
| 161 | is_signed_type(type), FILTER_OTHER); \ |
| 162 | if (ret) \ |
| 163 | return ret; |
| 164 | |
| 165 | static int trace_define_generic_fields(void) |
| 166 | { |
| 167 | int ret; |
| 168 | |
| 169 | __generic_field(int, CPU, FILTER_CPU); |
| 170 | __generic_field(int, cpu, FILTER_CPU); |
| 171 | __generic_field(char *, COMM, FILTER_COMM); |
| 172 | __generic_field(char *, comm, FILTER_COMM); |
| 173 | |
| 174 | return ret; |
| 175 | } |
| 176 | |
| 177 | static int trace_define_common_fields(void) |
| 178 | { |
| 179 | int ret; |
| 180 | struct trace_entry ent; |
| 181 | |
| 182 | __common_field(unsigned short, type); |
| 183 | __common_field(unsigned char, flags); |
| 184 | __common_field(unsigned char, preempt_count); |
| 185 | __common_field(int, pid); |
| 186 | |
| 187 | return ret; |
| 188 | } |
| 189 | |
| 190 | static void trace_destroy_fields(struct trace_event_call *call) |
| 191 | { |
| 192 | struct ftrace_event_field *field, *next; |
| 193 | struct list_head *head; |
| 194 | |
| 195 | head = trace_get_fields(call); |
| 196 | list_for_each_entry_safe(field, next, head, link) { |
| 197 | list_del(&field->link); |
| 198 | kmem_cache_free(field_cachep, field); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * run-time version of trace_event_get_offsets_<call>() that returns the last |
| 204 | * accessible offset of trace fields excluding __dynamic_array bytes |
| 205 | */ |
| 206 | int trace_event_get_offsets(struct trace_event_call *call) |
| 207 | { |
| 208 | struct ftrace_event_field *tail; |
| 209 | struct list_head *head; |
| 210 | |
| 211 | head = trace_get_fields(call); |
| 212 | /* |
| 213 | * head->next points to the last field with the largest offset, |
| 214 | * since it was added last by trace_define_field() |
| 215 | */ |
| 216 | tail = list_first_entry(head, struct ftrace_event_field, link); |
| 217 | return tail->offset + tail->size; |
| 218 | } |
| 219 | |
| 220 | int trace_event_raw_init(struct trace_event_call *call) |
| 221 | { |
| 222 | int id; |
| 223 | |
| 224 | id = register_trace_event(&call->event); |
| 225 | if (!id) |
| 226 | return -ENODEV; |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | EXPORT_SYMBOL_GPL(trace_event_raw_init); |
| 231 | |
| 232 | bool trace_event_ignore_this_pid(struct trace_event_file *trace_file) |
| 233 | { |
| 234 | struct trace_array *tr = trace_file->tr; |
| 235 | struct trace_array_cpu *data; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 236 | struct trace_pid_list *no_pid_list; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 237 | struct trace_pid_list *pid_list; |
| 238 | |
| 239 | pid_list = rcu_dereference_raw(tr->filtered_pids); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 240 | no_pid_list = rcu_dereference_raw(tr->filtered_no_pids); |
| 241 | |
| 242 | if (!pid_list && !no_pid_list) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 243 | return false; |
| 244 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 245 | data = this_cpu_ptr(tr->array_buffer.data); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 246 | |
| 247 | return data->ignore_pid; |
| 248 | } |
| 249 | EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid); |
| 250 | |
| 251 | void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer, |
| 252 | struct trace_event_file *trace_file, |
| 253 | unsigned long len) |
| 254 | { |
| 255 | struct trace_event_call *event_call = trace_file->event_call; |
| 256 | |
| 257 | if ((trace_file->flags & EVENT_FILE_FL_PID_FILTER) && |
| 258 | trace_event_ignore_this_pid(trace_file)) |
| 259 | return NULL; |
| 260 | |
| 261 | local_save_flags(fbuffer->flags); |
| 262 | fbuffer->pc = preempt_count(); |
| 263 | /* |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 264 | * If CONFIG_PREEMPTION is enabled, then the tracepoint itself disables |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 265 | * preemption (adding one to the preempt_count). Since we are |
| 266 | * interested in the preempt_count at the time the tracepoint was |
| 267 | * hit, we need to subtract one to offset the increment. |
| 268 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 269 | if (IS_ENABLED(CONFIG_PREEMPTION)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 270 | fbuffer->pc--; |
| 271 | fbuffer->trace_file = trace_file; |
| 272 | |
| 273 | fbuffer->event = |
| 274 | trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file, |
| 275 | event_call->event.type, len, |
| 276 | fbuffer->flags, fbuffer->pc); |
| 277 | if (!fbuffer->event) |
| 278 | return NULL; |
| 279 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 280 | fbuffer->regs = NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 281 | fbuffer->entry = ring_buffer_event_data(fbuffer->event); |
| 282 | return fbuffer->entry; |
| 283 | } |
| 284 | EXPORT_SYMBOL_GPL(trace_event_buffer_reserve); |
| 285 | |
| 286 | int trace_event_reg(struct trace_event_call *call, |
| 287 | enum trace_reg type, void *data) |
| 288 | { |
| 289 | struct trace_event_file *file = data; |
| 290 | |
| 291 | WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT)); |
| 292 | switch (type) { |
| 293 | case TRACE_REG_REGISTER: |
| 294 | return tracepoint_probe_register(call->tp, |
| 295 | call->class->probe, |
| 296 | file); |
| 297 | case TRACE_REG_UNREGISTER: |
| 298 | tracepoint_probe_unregister(call->tp, |
| 299 | call->class->probe, |
| 300 | file); |
| 301 | return 0; |
| 302 | |
| 303 | #ifdef CONFIG_PERF_EVENTS |
| 304 | case TRACE_REG_PERF_REGISTER: |
| 305 | return tracepoint_probe_register(call->tp, |
| 306 | call->class->perf_probe, |
| 307 | call); |
| 308 | case TRACE_REG_PERF_UNREGISTER: |
| 309 | tracepoint_probe_unregister(call->tp, |
| 310 | call->class->perf_probe, |
| 311 | call); |
| 312 | return 0; |
| 313 | case TRACE_REG_PERF_OPEN: |
| 314 | case TRACE_REG_PERF_CLOSE: |
| 315 | case TRACE_REG_PERF_ADD: |
| 316 | case TRACE_REG_PERF_DEL: |
| 317 | return 0; |
| 318 | #endif |
| 319 | } |
| 320 | return 0; |
| 321 | } |
| 322 | EXPORT_SYMBOL_GPL(trace_event_reg); |
| 323 | |
| 324 | void trace_event_enable_cmd_record(bool enable) |
| 325 | { |
| 326 | struct trace_event_file *file; |
| 327 | struct trace_array *tr; |
| 328 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 329 | lockdep_assert_held(&event_mutex); |
| 330 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 331 | do_for_each_event_file(tr, file) { |
| 332 | |
| 333 | if (!(file->flags & EVENT_FILE_FL_ENABLED)) |
| 334 | continue; |
| 335 | |
| 336 | if (enable) { |
| 337 | tracing_start_cmdline_record(); |
| 338 | set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags); |
| 339 | } else { |
| 340 | tracing_stop_cmdline_record(); |
| 341 | clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags); |
| 342 | } |
| 343 | } while_for_each_event_file(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | void trace_event_enable_tgid_record(bool enable) |
| 347 | { |
| 348 | struct trace_event_file *file; |
| 349 | struct trace_array *tr; |
| 350 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 351 | lockdep_assert_held(&event_mutex); |
| 352 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 353 | do_for_each_event_file(tr, file) { |
| 354 | if (!(file->flags & EVENT_FILE_FL_ENABLED)) |
| 355 | continue; |
| 356 | |
| 357 | if (enable) { |
| 358 | tracing_start_tgid_record(); |
| 359 | set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags); |
| 360 | } else { |
| 361 | tracing_stop_tgid_record(); |
| 362 | clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, |
| 363 | &file->flags); |
| 364 | } |
| 365 | } while_for_each_event_file(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | static int __ftrace_event_enable_disable(struct trace_event_file *file, |
| 369 | int enable, int soft_disable) |
| 370 | { |
| 371 | struct trace_event_call *call = file->event_call; |
| 372 | struct trace_array *tr = file->tr; |
| 373 | unsigned long file_flags = file->flags; |
| 374 | int ret = 0; |
| 375 | int disable; |
| 376 | |
| 377 | switch (enable) { |
| 378 | case 0: |
| 379 | /* |
| 380 | * When soft_disable is set and enable is cleared, the sm_ref |
| 381 | * reference counter is decremented. If it reaches 0, we want |
| 382 | * to clear the SOFT_DISABLED flag but leave the event in the |
| 383 | * state that it was. That is, if the event was enabled and |
| 384 | * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED |
| 385 | * is set we do not want the event to be enabled before we |
| 386 | * clear the bit. |
| 387 | * |
| 388 | * When soft_disable is not set but the SOFT_MODE flag is, |
| 389 | * we do nothing. Do not disable the tracepoint, otherwise |
| 390 | * "soft enable"s (clearing the SOFT_DISABLED bit) wont work. |
| 391 | */ |
| 392 | if (soft_disable) { |
| 393 | if (atomic_dec_return(&file->sm_ref) > 0) |
| 394 | break; |
| 395 | disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED; |
| 396 | clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags); |
| 397 | } else |
| 398 | disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE); |
| 399 | |
| 400 | if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) { |
| 401 | clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags); |
| 402 | if (file->flags & EVENT_FILE_FL_RECORDED_CMD) { |
| 403 | tracing_stop_cmdline_record(); |
| 404 | clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags); |
| 405 | } |
| 406 | |
| 407 | if (file->flags & EVENT_FILE_FL_RECORDED_TGID) { |
| 408 | tracing_stop_tgid_record(); |
| 409 | clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags); |
| 410 | } |
| 411 | |
| 412 | call->class->reg(call, TRACE_REG_UNREGISTER, file); |
| 413 | } |
| 414 | /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */ |
| 415 | if (file->flags & EVENT_FILE_FL_SOFT_MODE) |
| 416 | set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags); |
| 417 | else |
| 418 | clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags); |
| 419 | break; |
| 420 | case 1: |
| 421 | /* |
| 422 | * When soft_disable is set and enable is set, we want to |
| 423 | * register the tracepoint for the event, but leave the event |
| 424 | * as is. That means, if the event was already enabled, we do |
| 425 | * nothing (but set SOFT_MODE). If the event is disabled, we |
| 426 | * set SOFT_DISABLED before enabling the event tracepoint, so |
| 427 | * it still seems to be disabled. |
| 428 | */ |
| 429 | if (!soft_disable) |
| 430 | clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags); |
| 431 | else { |
| 432 | if (atomic_inc_return(&file->sm_ref) > 1) |
| 433 | break; |
| 434 | set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags); |
| 435 | } |
| 436 | |
| 437 | if (!(file->flags & EVENT_FILE_FL_ENABLED)) { |
| 438 | bool cmd = false, tgid = false; |
| 439 | |
| 440 | /* Keep the event disabled, when going to SOFT_MODE. */ |
| 441 | if (soft_disable) |
| 442 | set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags); |
| 443 | |
| 444 | if (tr->trace_flags & TRACE_ITER_RECORD_CMD) { |
| 445 | cmd = true; |
| 446 | tracing_start_cmdline_record(); |
| 447 | set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags); |
| 448 | } |
| 449 | |
| 450 | if (tr->trace_flags & TRACE_ITER_RECORD_TGID) { |
| 451 | tgid = true; |
| 452 | tracing_start_tgid_record(); |
| 453 | set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags); |
| 454 | } |
| 455 | |
| 456 | ret = call->class->reg(call, TRACE_REG_REGISTER, file); |
| 457 | if (ret) { |
| 458 | if (cmd) |
| 459 | tracing_stop_cmdline_record(); |
| 460 | if (tgid) |
| 461 | tracing_stop_tgid_record(); |
| 462 | pr_info("event trace: Could not enable event " |
| 463 | "%s\n", trace_event_name(call)); |
| 464 | break; |
| 465 | } |
| 466 | set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags); |
| 467 | |
| 468 | /* WAS_ENABLED gets set but never cleared. */ |
| 469 | set_bit(EVENT_FILE_FL_WAS_ENABLED_BIT, &file->flags); |
| 470 | } |
| 471 | break; |
| 472 | } |
| 473 | |
| 474 | /* Enable or disable use of trace_buffered_event */ |
| 475 | if ((file_flags & EVENT_FILE_FL_SOFT_DISABLED) != |
| 476 | (file->flags & EVENT_FILE_FL_SOFT_DISABLED)) { |
| 477 | if (file->flags & EVENT_FILE_FL_SOFT_DISABLED) |
| 478 | trace_buffered_event_enable(); |
| 479 | else |
| 480 | trace_buffered_event_disable(); |
| 481 | } |
| 482 | |
| 483 | return ret; |
| 484 | } |
| 485 | |
| 486 | int trace_event_enable_disable(struct trace_event_file *file, |
| 487 | int enable, int soft_disable) |
| 488 | { |
| 489 | return __ftrace_event_enable_disable(file, enable, soft_disable); |
| 490 | } |
| 491 | |
| 492 | static int ftrace_event_enable_disable(struct trace_event_file *file, |
| 493 | int enable) |
| 494 | { |
| 495 | return __ftrace_event_enable_disable(file, enable, 0); |
| 496 | } |
| 497 | |
| 498 | static void ftrace_clear_events(struct trace_array *tr) |
| 499 | { |
| 500 | struct trace_event_file *file; |
| 501 | |
| 502 | mutex_lock(&event_mutex); |
| 503 | list_for_each_entry(file, &tr->events, list) { |
| 504 | ftrace_event_enable_disable(file, 0); |
| 505 | } |
| 506 | mutex_unlock(&event_mutex); |
| 507 | } |
| 508 | |
| 509 | static void |
| 510 | event_filter_pid_sched_process_exit(void *data, struct task_struct *task) |
| 511 | { |
| 512 | struct trace_pid_list *pid_list; |
| 513 | struct trace_array *tr = data; |
| 514 | |
| 515 | pid_list = rcu_dereference_raw(tr->filtered_pids); |
| 516 | trace_filter_add_remove_task(pid_list, NULL, task); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 517 | |
| 518 | pid_list = rcu_dereference_raw(tr->filtered_no_pids); |
| 519 | trace_filter_add_remove_task(pid_list, NULL, task); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | static void |
| 523 | event_filter_pid_sched_process_fork(void *data, |
| 524 | struct task_struct *self, |
| 525 | struct task_struct *task) |
| 526 | { |
| 527 | struct trace_pid_list *pid_list; |
| 528 | struct trace_array *tr = data; |
| 529 | |
| 530 | pid_list = rcu_dereference_sched(tr->filtered_pids); |
| 531 | trace_filter_add_remove_task(pid_list, self, task); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 532 | |
| 533 | pid_list = rcu_dereference_sched(tr->filtered_no_pids); |
| 534 | trace_filter_add_remove_task(pid_list, self, task); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | void trace_event_follow_fork(struct trace_array *tr, bool enable) |
| 538 | { |
| 539 | if (enable) { |
| 540 | register_trace_prio_sched_process_fork(event_filter_pid_sched_process_fork, |
| 541 | tr, INT_MIN); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 542 | register_trace_prio_sched_process_free(event_filter_pid_sched_process_exit, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 543 | tr, INT_MAX); |
| 544 | } else { |
| 545 | unregister_trace_sched_process_fork(event_filter_pid_sched_process_fork, |
| 546 | tr); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 547 | unregister_trace_sched_process_free(event_filter_pid_sched_process_exit, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 548 | tr); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | static void |
| 553 | event_filter_pid_sched_switch_probe_pre(void *data, bool preempt, |
| 554 | struct task_struct *prev, struct task_struct *next) |
| 555 | { |
| 556 | struct trace_array *tr = data; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 557 | struct trace_pid_list *no_pid_list; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 558 | struct trace_pid_list *pid_list; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 559 | bool ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 560 | |
| 561 | pid_list = rcu_dereference_sched(tr->filtered_pids); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 562 | no_pid_list = rcu_dereference_sched(tr->filtered_no_pids); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 563 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 564 | /* |
| 565 | * Sched switch is funny, as we only want to ignore it |
| 566 | * in the notrace case if both prev and next should be ignored. |
| 567 | */ |
| 568 | ret = trace_ignore_this_task(NULL, no_pid_list, prev) && |
| 569 | trace_ignore_this_task(NULL, no_pid_list, next); |
| 570 | |
| 571 | this_cpu_write(tr->array_buffer.data->ignore_pid, ret || |
| 572 | (trace_ignore_this_task(pid_list, NULL, prev) && |
| 573 | trace_ignore_this_task(pid_list, NULL, next))); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | static void |
| 577 | event_filter_pid_sched_switch_probe_post(void *data, bool preempt, |
| 578 | struct task_struct *prev, struct task_struct *next) |
| 579 | { |
| 580 | struct trace_array *tr = data; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 581 | struct trace_pid_list *no_pid_list; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 582 | struct trace_pid_list *pid_list; |
| 583 | |
| 584 | pid_list = rcu_dereference_sched(tr->filtered_pids); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 585 | no_pid_list = rcu_dereference_sched(tr->filtered_no_pids); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 586 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 587 | this_cpu_write(tr->array_buffer.data->ignore_pid, |
| 588 | trace_ignore_this_task(pid_list, no_pid_list, next)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | static void |
| 592 | event_filter_pid_sched_wakeup_probe_pre(void *data, struct task_struct *task) |
| 593 | { |
| 594 | struct trace_array *tr = data; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 595 | struct trace_pid_list *no_pid_list; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 596 | struct trace_pid_list *pid_list; |
| 597 | |
| 598 | /* Nothing to do if we are already tracing */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 599 | if (!this_cpu_read(tr->array_buffer.data->ignore_pid)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 600 | return; |
| 601 | |
| 602 | pid_list = rcu_dereference_sched(tr->filtered_pids); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 603 | no_pid_list = rcu_dereference_sched(tr->filtered_no_pids); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 604 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 605 | this_cpu_write(tr->array_buffer.data->ignore_pid, |
| 606 | trace_ignore_this_task(pid_list, no_pid_list, task)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | static void |
| 610 | event_filter_pid_sched_wakeup_probe_post(void *data, struct task_struct *task) |
| 611 | { |
| 612 | struct trace_array *tr = data; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 613 | struct trace_pid_list *no_pid_list; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 614 | struct trace_pid_list *pid_list; |
| 615 | |
| 616 | /* Nothing to do if we are not tracing */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 617 | if (this_cpu_read(tr->array_buffer.data->ignore_pid)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 618 | return; |
| 619 | |
| 620 | pid_list = rcu_dereference_sched(tr->filtered_pids); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 621 | no_pid_list = rcu_dereference_sched(tr->filtered_no_pids); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 622 | |
| 623 | /* Set tracing if current is enabled */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 624 | this_cpu_write(tr->array_buffer.data->ignore_pid, |
| 625 | trace_ignore_this_task(pid_list, no_pid_list, current)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 628 | static void unregister_pid_events(struct trace_array *tr) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 629 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 630 | unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre, tr); |
| 631 | unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post, tr); |
| 632 | |
| 633 | unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, tr); |
| 634 | unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, tr); |
| 635 | |
| 636 | unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre, tr); |
| 637 | unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post, tr); |
| 638 | |
| 639 | unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_pre, tr); |
| 640 | unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_post, tr); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 641 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 642 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 643 | static void __ftrace_clear_event_pids(struct trace_array *tr, int type) |
| 644 | { |
| 645 | struct trace_pid_list *pid_list; |
| 646 | struct trace_pid_list *no_pid_list; |
| 647 | struct trace_event_file *file; |
| 648 | int cpu; |
| 649 | |
| 650 | pid_list = rcu_dereference_protected(tr->filtered_pids, |
| 651 | lockdep_is_held(&event_mutex)); |
| 652 | no_pid_list = rcu_dereference_protected(tr->filtered_no_pids, |
| 653 | lockdep_is_held(&event_mutex)); |
| 654 | |
| 655 | /* Make sure there's something to do */ |
| 656 | if (!pid_type_enabled(type, pid_list, no_pid_list)) |
| 657 | return; |
| 658 | |
| 659 | if (!still_need_pid_events(type, pid_list, no_pid_list)) { |
| 660 | unregister_pid_events(tr); |
| 661 | |
| 662 | list_for_each_entry(file, &tr->events, list) { |
| 663 | clear_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags); |
| 664 | } |
| 665 | |
| 666 | for_each_possible_cpu(cpu) |
| 667 | per_cpu_ptr(tr->array_buffer.data, cpu)->ignore_pid = false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 670 | if (type & TRACE_PIDS) |
| 671 | rcu_assign_pointer(tr->filtered_pids, NULL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 672 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 673 | if (type & TRACE_NO_PIDS) |
| 674 | rcu_assign_pointer(tr->filtered_no_pids, NULL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 675 | |
| 676 | /* Wait till all users are no longer using pid filtering */ |
| 677 | tracepoint_synchronize_unregister(); |
| 678 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 679 | if ((type & TRACE_PIDS) && pid_list) |
| 680 | trace_free_pid_list(pid_list); |
| 681 | |
| 682 | if ((type & TRACE_NO_PIDS) && no_pid_list) |
| 683 | trace_free_pid_list(no_pid_list); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 686 | static void ftrace_clear_event_pids(struct trace_array *tr, int type) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 687 | { |
| 688 | mutex_lock(&event_mutex); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 689 | __ftrace_clear_event_pids(tr, type); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 690 | mutex_unlock(&event_mutex); |
| 691 | } |
| 692 | |
| 693 | static void __put_system(struct event_subsystem *system) |
| 694 | { |
| 695 | struct event_filter *filter = system->filter; |
| 696 | |
| 697 | WARN_ON_ONCE(system_refcount(system) == 0); |
| 698 | if (system_refcount_dec(system)) |
| 699 | return; |
| 700 | |
| 701 | list_del(&system->list); |
| 702 | |
| 703 | if (filter) { |
| 704 | kfree(filter->filter_string); |
| 705 | kfree(filter); |
| 706 | } |
| 707 | kfree_const(system->name); |
| 708 | kfree(system); |
| 709 | } |
| 710 | |
| 711 | static void __get_system(struct event_subsystem *system) |
| 712 | { |
| 713 | WARN_ON_ONCE(system_refcount(system) == 0); |
| 714 | system_refcount_inc(system); |
| 715 | } |
| 716 | |
| 717 | static void __get_system_dir(struct trace_subsystem_dir *dir) |
| 718 | { |
| 719 | WARN_ON_ONCE(dir->ref_count == 0); |
| 720 | dir->ref_count++; |
| 721 | __get_system(dir->subsystem); |
| 722 | } |
| 723 | |
| 724 | static void __put_system_dir(struct trace_subsystem_dir *dir) |
| 725 | { |
| 726 | WARN_ON_ONCE(dir->ref_count == 0); |
| 727 | /* If the subsystem is about to be freed, the dir must be too */ |
| 728 | WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1); |
| 729 | |
| 730 | __put_system(dir->subsystem); |
| 731 | if (!--dir->ref_count) |
| 732 | kfree(dir); |
| 733 | } |
| 734 | |
| 735 | static void put_system(struct trace_subsystem_dir *dir) |
| 736 | { |
| 737 | mutex_lock(&event_mutex); |
| 738 | __put_system_dir(dir); |
| 739 | mutex_unlock(&event_mutex); |
| 740 | } |
| 741 | |
| 742 | static void remove_subsystem(struct trace_subsystem_dir *dir) |
| 743 | { |
| 744 | if (!dir) |
| 745 | return; |
| 746 | |
| 747 | if (!--dir->nr_events) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 748 | tracefs_remove(dir->entry); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 749 | list_del(&dir->list); |
| 750 | __put_system_dir(dir); |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | static void remove_event_file_dir(struct trace_event_file *file) |
| 755 | { |
| 756 | struct dentry *dir = file->dir; |
| 757 | struct dentry *child; |
| 758 | |
| 759 | if (dir) { |
| 760 | spin_lock(&dir->d_lock); /* probably unneeded */ |
| 761 | list_for_each_entry(child, &dir->d_subdirs, d_child) { |
| 762 | if (d_really_is_positive(child)) /* probably unneeded */ |
| 763 | d_inode(child)->i_private = NULL; |
| 764 | } |
| 765 | spin_unlock(&dir->d_lock); |
| 766 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 767 | tracefs_remove(dir); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | list_del(&file->list); |
| 771 | remove_subsystem(file->system); |
| 772 | free_event_filter(file->filter); |
| 773 | kmem_cache_free(file_cachep, file); |
| 774 | } |
| 775 | |
| 776 | /* |
| 777 | * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events. |
| 778 | */ |
| 779 | static int |
| 780 | __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match, |
| 781 | const char *sub, const char *event, int set) |
| 782 | { |
| 783 | struct trace_event_file *file; |
| 784 | struct trace_event_call *call; |
| 785 | const char *name; |
| 786 | int ret = -EINVAL; |
| 787 | int eret = 0; |
| 788 | |
| 789 | list_for_each_entry(file, &tr->events, list) { |
| 790 | |
| 791 | call = file->event_call; |
| 792 | name = trace_event_name(call); |
| 793 | |
| 794 | if (!name || !call->class || !call->class->reg) |
| 795 | continue; |
| 796 | |
| 797 | if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) |
| 798 | continue; |
| 799 | |
| 800 | if (match && |
| 801 | strcmp(match, name) != 0 && |
| 802 | strcmp(match, call->class->system) != 0) |
| 803 | continue; |
| 804 | |
| 805 | if (sub && strcmp(sub, call->class->system) != 0) |
| 806 | continue; |
| 807 | |
| 808 | if (event && strcmp(event, name) != 0) |
| 809 | continue; |
| 810 | |
| 811 | ret = ftrace_event_enable_disable(file, set); |
| 812 | |
| 813 | /* |
| 814 | * Save the first error and return that. Some events |
| 815 | * may still have been enabled, but let the user |
| 816 | * know that something went wrong. |
| 817 | */ |
| 818 | if (ret && !eret) |
| 819 | eret = ret; |
| 820 | |
| 821 | ret = eret; |
| 822 | } |
| 823 | |
| 824 | return ret; |
| 825 | } |
| 826 | |
| 827 | static int __ftrace_set_clr_event(struct trace_array *tr, const char *match, |
| 828 | const char *sub, const char *event, int set) |
| 829 | { |
| 830 | int ret; |
| 831 | |
| 832 | mutex_lock(&event_mutex); |
| 833 | ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set); |
| 834 | mutex_unlock(&event_mutex); |
| 835 | |
| 836 | return ret; |
| 837 | } |
| 838 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 839 | int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 840 | { |
| 841 | char *event = NULL, *sub = NULL, *match; |
| 842 | int ret; |
| 843 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 844 | if (!tr) |
| 845 | return -ENOENT; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 846 | /* |
| 847 | * The buf format can be <subsystem>:<event-name> |
| 848 | * *:<event-name> means any event by that name. |
| 849 | * :<event-name> is the same. |
| 850 | * |
| 851 | * <subsystem>:* means all events in that subsystem |
| 852 | * <subsystem>: means the same. |
| 853 | * |
| 854 | * <name> (no ':') means all events in a subsystem with |
| 855 | * the name <name> or any event that matches <name> |
| 856 | */ |
| 857 | |
| 858 | match = strsep(&buf, ":"); |
| 859 | if (buf) { |
| 860 | sub = match; |
| 861 | event = buf; |
| 862 | match = NULL; |
| 863 | |
| 864 | if (!strlen(sub) || strcmp(sub, "*") == 0) |
| 865 | sub = NULL; |
| 866 | if (!strlen(event) || strcmp(event, "*") == 0) |
| 867 | event = NULL; |
| 868 | } |
| 869 | |
| 870 | ret = __ftrace_set_clr_event(tr, match, sub, event, set); |
| 871 | |
| 872 | /* Put back the colon to allow this to be called again */ |
| 873 | if (buf) |
| 874 | *(buf - 1) = ':'; |
| 875 | |
| 876 | return ret; |
| 877 | } |
| 878 | |
| 879 | /** |
| 880 | * trace_set_clr_event - enable or disable an event |
| 881 | * @system: system name to match (NULL for any system) |
| 882 | * @event: event name to match (NULL for all events, within system) |
| 883 | * @set: 1 to enable, 0 to disable |
| 884 | * |
| 885 | * This is a way for other parts of the kernel to enable or disable |
| 886 | * event recording. |
| 887 | * |
| 888 | * Returns 0 on success, -EINVAL if the parameters do not match any |
| 889 | * registered events. |
| 890 | */ |
| 891 | int trace_set_clr_event(const char *system, const char *event, int set) |
| 892 | { |
| 893 | struct trace_array *tr = top_trace_array(); |
| 894 | |
| 895 | if (!tr) |
| 896 | return -ENODEV; |
| 897 | |
| 898 | return __ftrace_set_clr_event(tr, NULL, system, event, set); |
| 899 | } |
| 900 | EXPORT_SYMBOL_GPL(trace_set_clr_event); |
| 901 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 902 | /** |
| 903 | * trace_array_set_clr_event - enable or disable an event for a trace array. |
| 904 | * @tr: concerned trace array. |
| 905 | * @system: system name to match (NULL for any system) |
| 906 | * @event: event name to match (NULL for all events, within system) |
| 907 | * @enable: true to enable, false to disable |
| 908 | * |
| 909 | * This is a way for other parts of the kernel to enable or disable |
| 910 | * event recording. |
| 911 | * |
| 912 | * Returns 0 on success, -EINVAL if the parameters do not match any |
| 913 | * registered events. |
| 914 | */ |
| 915 | int trace_array_set_clr_event(struct trace_array *tr, const char *system, |
| 916 | const char *event, bool enable) |
| 917 | { |
| 918 | int set; |
| 919 | |
| 920 | if (!tr) |
| 921 | return -ENOENT; |
| 922 | |
| 923 | set = (enable == true) ? 1 : 0; |
| 924 | return __ftrace_set_clr_event(tr, NULL, system, event, set); |
| 925 | } |
| 926 | EXPORT_SYMBOL_GPL(trace_array_set_clr_event); |
| 927 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 928 | /* 128 should be much more than enough */ |
| 929 | #define EVENT_BUF_SIZE 127 |
| 930 | |
| 931 | static ssize_t |
| 932 | ftrace_event_write(struct file *file, const char __user *ubuf, |
| 933 | size_t cnt, loff_t *ppos) |
| 934 | { |
| 935 | struct trace_parser parser; |
| 936 | struct seq_file *m = file->private_data; |
| 937 | struct trace_array *tr = m->private; |
| 938 | ssize_t read, ret; |
| 939 | |
| 940 | if (!cnt) |
| 941 | return 0; |
| 942 | |
| 943 | ret = tracing_update_buffers(); |
| 944 | if (ret < 0) |
| 945 | return ret; |
| 946 | |
| 947 | if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1)) |
| 948 | return -ENOMEM; |
| 949 | |
| 950 | read = trace_get_user(&parser, ubuf, cnt, ppos); |
| 951 | |
| 952 | if (read >= 0 && trace_parser_loaded((&parser))) { |
| 953 | int set = 1; |
| 954 | |
| 955 | if (*parser.buffer == '!') |
| 956 | set = 0; |
| 957 | |
| 958 | ret = ftrace_set_clr_event(tr, parser.buffer + !set, set); |
| 959 | if (ret) |
| 960 | goto out_put; |
| 961 | } |
| 962 | |
| 963 | ret = read; |
| 964 | |
| 965 | out_put: |
| 966 | trace_parser_put(&parser); |
| 967 | |
| 968 | return ret; |
| 969 | } |
| 970 | |
| 971 | static void * |
| 972 | t_next(struct seq_file *m, void *v, loff_t *pos) |
| 973 | { |
| 974 | struct trace_event_file *file = v; |
| 975 | struct trace_event_call *call; |
| 976 | struct trace_array *tr = m->private; |
| 977 | |
| 978 | (*pos)++; |
| 979 | |
| 980 | list_for_each_entry_continue(file, &tr->events, list) { |
| 981 | call = file->event_call; |
| 982 | /* |
| 983 | * The ftrace subsystem is for showing formats only. |
| 984 | * They can not be enabled or disabled via the event files. |
| 985 | */ |
| 986 | if (call->class && call->class->reg && |
| 987 | !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)) |
| 988 | return file; |
| 989 | } |
| 990 | |
| 991 | return NULL; |
| 992 | } |
| 993 | |
| 994 | static void *t_start(struct seq_file *m, loff_t *pos) |
| 995 | { |
| 996 | struct trace_event_file *file; |
| 997 | struct trace_array *tr = m->private; |
| 998 | loff_t l; |
| 999 | |
| 1000 | mutex_lock(&event_mutex); |
| 1001 | |
| 1002 | file = list_entry(&tr->events, struct trace_event_file, list); |
| 1003 | for (l = 0; l <= *pos; ) { |
| 1004 | file = t_next(m, file, &l); |
| 1005 | if (!file) |
| 1006 | break; |
| 1007 | } |
| 1008 | return file; |
| 1009 | } |
| 1010 | |
| 1011 | static void * |
| 1012 | s_next(struct seq_file *m, void *v, loff_t *pos) |
| 1013 | { |
| 1014 | struct trace_event_file *file = v; |
| 1015 | struct trace_array *tr = m->private; |
| 1016 | |
| 1017 | (*pos)++; |
| 1018 | |
| 1019 | list_for_each_entry_continue(file, &tr->events, list) { |
| 1020 | if (file->flags & EVENT_FILE_FL_ENABLED) |
| 1021 | return file; |
| 1022 | } |
| 1023 | |
| 1024 | return NULL; |
| 1025 | } |
| 1026 | |
| 1027 | static void *s_start(struct seq_file *m, loff_t *pos) |
| 1028 | { |
| 1029 | struct trace_event_file *file; |
| 1030 | struct trace_array *tr = m->private; |
| 1031 | loff_t l; |
| 1032 | |
| 1033 | mutex_lock(&event_mutex); |
| 1034 | |
| 1035 | file = list_entry(&tr->events, struct trace_event_file, list); |
| 1036 | for (l = 0; l <= *pos; ) { |
| 1037 | file = s_next(m, file, &l); |
| 1038 | if (!file) |
| 1039 | break; |
| 1040 | } |
| 1041 | return file; |
| 1042 | } |
| 1043 | |
| 1044 | static int t_show(struct seq_file *m, void *v) |
| 1045 | { |
| 1046 | struct trace_event_file *file = v; |
| 1047 | struct trace_event_call *call = file->event_call; |
| 1048 | |
| 1049 | if (strcmp(call->class->system, TRACE_SYSTEM) != 0) |
| 1050 | seq_printf(m, "%s:", call->class->system); |
| 1051 | seq_printf(m, "%s\n", trace_event_name(call)); |
| 1052 | |
| 1053 | return 0; |
| 1054 | } |
| 1055 | |
| 1056 | static void t_stop(struct seq_file *m, void *p) |
| 1057 | { |
| 1058 | mutex_unlock(&event_mutex); |
| 1059 | } |
| 1060 | |
| 1061 | static void * |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1062 | __next(struct seq_file *m, void *v, loff_t *pos, int type) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1063 | { |
| 1064 | struct trace_array *tr = m->private; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1065 | struct trace_pid_list *pid_list; |
| 1066 | |
| 1067 | if (type == TRACE_PIDS) |
| 1068 | pid_list = rcu_dereference_sched(tr->filtered_pids); |
| 1069 | else |
| 1070 | pid_list = rcu_dereference_sched(tr->filtered_no_pids); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1071 | |
| 1072 | return trace_pid_next(pid_list, v, pos); |
| 1073 | } |
| 1074 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1075 | static void * |
| 1076 | p_next(struct seq_file *m, void *v, loff_t *pos) |
| 1077 | { |
| 1078 | return __next(m, v, pos, TRACE_PIDS); |
| 1079 | } |
| 1080 | |
| 1081 | static void * |
| 1082 | np_next(struct seq_file *m, void *v, loff_t *pos) |
| 1083 | { |
| 1084 | return __next(m, v, pos, TRACE_NO_PIDS); |
| 1085 | } |
| 1086 | |
| 1087 | static void *__start(struct seq_file *m, loff_t *pos, int type) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1088 | __acquires(RCU) |
| 1089 | { |
| 1090 | struct trace_pid_list *pid_list; |
| 1091 | struct trace_array *tr = m->private; |
| 1092 | |
| 1093 | /* |
| 1094 | * Grab the mutex, to keep calls to p_next() having the same |
| 1095 | * tr->filtered_pids as p_start() has. |
| 1096 | * If we just passed the tr->filtered_pids around, then RCU would |
| 1097 | * have been enough, but doing that makes things more complex. |
| 1098 | */ |
| 1099 | mutex_lock(&event_mutex); |
| 1100 | rcu_read_lock_sched(); |
| 1101 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1102 | if (type == TRACE_PIDS) |
| 1103 | pid_list = rcu_dereference_sched(tr->filtered_pids); |
| 1104 | else |
| 1105 | pid_list = rcu_dereference_sched(tr->filtered_no_pids); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1106 | |
| 1107 | if (!pid_list) |
| 1108 | return NULL; |
| 1109 | |
| 1110 | return trace_pid_start(pid_list, pos); |
| 1111 | } |
| 1112 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1113 | static void *p_start(struct seq_file *m, loff_t *pos) |
| 1114 | __acquires(RCU) |
| 1115 | { |
| 1116 | return __start(m, pos, TRACE_PIDS); |
| 1117 | } |
| 1118 | |
| 1119 | static void *np_start(struct seq_file *m, loff_t *pos) |
| 1120 | __acquires(RCU) |
| 1121 | { |
| 1122 | return __start(m, pos, TRACE_NO_PIDS); |
| 1123 | } |
| 1124 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1125 | static void p_stop(struct seq_file *m, void *p) |
| 1126 | __releases(RCU) |
| 1127 | { |
| 1128 | rcu_read_unlock_sched(); |
| 1129 | mutex_unlock(&event_mutex); |
| 1130 | } |
| 1131 | |
| 1132 | static ssize_t |
| 1133 | event_enable_read(struct file *filp, char __user *ubuf, size_t cnt, |
| 1134 | loff_t *ppos) |
| 1135 | { |
| 1136 | struct trace_event_file *file; |
| 1137 | unsigned long flags; |
| 1138 | char buf[4] = "0"; |
| 1139 | |
| 1140 | mutex_lock(&event_mutex); |
| 1141 | file = event_file_data(filp); |
| 1142 | if (likely(file)) |
| 1143 | flags = file->flags; |
| 1144 | mutex_unlock(&event_mutex); |
| 1145 | |
| 1146 | if (!file) |
| 1147 | return -ENODEV; |
| 1148 | |
| 1149 | if (flags & EVENT_FILE_FL_ENABLED && |
| 1150 | !(flags & EVENT_FILE_FL_SOFT_DISABLED)) |
| 1151 | strcpy(buf, "1"); |
| 1152 | |
| 1153 | if (flags & EVENT_FILE_FL_SOFT_DISABLED || |
| 1154 | flags & EVENT_FILE_FL_SOFT_MODE) |
| 1155 | strcat(buf, "*"); |
| 1156 | |
| 1157 | strcat(buf, "\n"); |
| 1158 | |
| 1159 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf)); |
| 1160 | } |
| 1161 | |
| 1162 | static ssize_t |
| 1163 | event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt, |
| 1164 | loff_t *ppos) |
| 1165 | { |
| 1166 | struct trace_event_file *file; |
| 1167 | unsigned long val; |
| 1168 | int ret; |
| 1169 | |
| 1170 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
| 1171 | if (ret) |
| 1172 | return ret; |
| 1173 | |
| 1174 | ret = tracing_update_buffers(); |
| 1175 | if (ret < 0) |
| 1176 | return ret; |
| 1177 | |
| 1178 | switch (val) { |
| 1179 | case 0: |
| 1180 | case 1: |
| 1181 | ret = -ENODEV; |
| 1182 | mutex_lock(&event_mutex); |
| 1183 | file = event_file_data(filp); |
| 1184 | if (likely(file)) |
| 1185 | ret = ftrace_event_enable_disable(file, val); |
| 1186 | mutex_unlock(&event_mutex); |
| 1187 | break; |
| 1188 | |
| 1189 | default: |
| 1190 | return -EINVAL; |
| 1191 | } |
| 1192 | |
| 1193 | *ppos += cnt; |
| 1194 | |
| 1195 | return ret ? ret : cnt; |
| 1196 | } |
| 1197 | |
| 1198 | static ssize_t |
| 1199 | system_enable_read(struct file *filp, char __user *ubuf, size_t cnt, |
| 1200 | loff_t *ppos) |
| 1201 | { |
| 1202 | const char set_to_char[4] = { '?', '0', '1', 'X' }; |
| 1203 | struct trace_subsystem_dir *dir = filp->private_data; |
| 1204 | struct event_subsystem *system = dir->subsystem; |
| 1205 | struct trace_event_call *call; |
| 1206 | struct trace_event_file *file; |
| 1207 | struct trace_array *tr = dir->tr; |
| 1208 | char buf[2]; |
| 1209 | int set = 0; |
| 1210 | int ret; |
| 1211 | |
| 1212 | mutex_lock(&event_mutex); |
| 1213 | list_for_each_entry(file, &tr->events, list) { |
| 1214 | call = file->event_call; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1215 | if ((call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) || |
| 1216 | !trace_event_name(call) || !call->class || !call->class->reg) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1217 | continue; |
| 1218 | |
| 1219 | if (system && strcmp(call->class->system, system->name) != 0) |
| 1220 | continue; |
| 1221 | |
| 1222 | /* |
| 1223 | * We need to find out if all the events are set |
| 1224 | * or if all events or cleared, or if we have |
| 1225 | * a mixture. |
| 1226 | */ |
| 1227 | set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED)); |
| 1228 | |
| 1229 | /* |
| 1230 | * If we have a mixture, no need to look further. |
| 1231 | */ |
| 1232 | if (set == 3) |
| 1233 | break; |
| 1234 | } |
| 1235 | mutex_unlock(&event_mutex); |
| 1236 | |
| 1237 | buf[0] = set_to_char[set]; |
| 1238 | buf[1] = '\n'; |
| 1239 | |
| 1240 | ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2); |
| 1241 | |
| 1242 | return ret; |
| 1243 | } |
| 1244 | |
| 1245 | static ssize_t |
| 1246 | system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt, |
| 1247 | loff_t *ppos) |
| 1248 | { |
| 1249 | struct trace_subsystem_dir *dir = filp->private_data; |
| 1250 | struct event_subsystem *system = dir->subsystem; |
| 1251 | const char *name = NULL; |
| 1252 | unsigned long val; |
| 1253 | ssize_t ret; |
| 1254 | |
| 1255 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
| 1256 | if (ret) |
| 1257 | return ret; |
| 1258 | |
| 1259 | ret = tracing_update_buffers(); |
| 1260 | if (ret < 0) |
| 1261 | return ret; |
| 1262 | |
| 1263 | if (val != 0 && val != 1) |
| 1264 | return -EINVAL; |
| 1265 | |
| 1266 | /* |
| 1267 | * Opening of "enable" adds a ref count to system, |
| 1268 | * so the name is safe to use. |
| 1269 | */ |
| 1270 | if (system) |
| 1271 | name = system->name; |
| 1272 | |
| 1273 | ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val); |
| 1274 | if (ret) |
| 1275 | goto out; |
| 1276 | |
| 1277 | ret = cnt; |
| 1278 | |
| 1279 | out: |
| 1280 | *ppos += cnt; |
| 1281 | |
| 1282 | return ret; |
| 1283 | } |
| 1284 | |
| 1285 | enum { |
| 1286 | FORMAT_HEADER = 1, |
| 1287 | FORMAT_FIELD_SEPERATOR = 2, |
| 1288 | FORMAT_PRINTFMT = 3, |
| 1289 | }; |
| 1290 | |
| 1291 | static void *f_next(struct seq_file *m, void *v, loff_t *pos) |
| 1292 | { |
| 1293 | struct trace_event_call *call = event_file_data(m->private); |
| 1294 | struct list_head *common_head = &ftrace_common_fields; |
| 1295 | struct list_head *head = trace_get_fields(call); |
| 1296 | struct list_head *node = v; |
| 1297 | |
| 1298 | (*pos)++; |
| 1299 | |
| 1300 | switch ((unsigned long)v) { |
| 1301 | case FORMAT_HEADER: |
| 1302 | node = common_head; |
| 1303 | break; |
| 1304 | |
| 1305 | case FORMAT_FIELD_SEPERATOR: |
| 1306 | node = head; |
| 1307 | break; |
| 1308 | |
| 1309 | case FORMAT_PRINTFMT: |
| 1310 | /* all done */ |
| 1311 | return NULL; |
| 1312 | } |
| 1313 | |
| 1314 | node = node->prev; |
| 1315 | if (node == common_head) |
| 1316 | return (void *)FORMAT_FIELD_SEPERATOR; |
| 1317 | else if (node == head) |
| 1318 | return (void *)FORMAT_PRINTFMT; |
| 1319 | else |
| 1320 | return node; |
| 1321 | } |
| 1322 | |
| 1323 | static int f_show(struct seq_file *m, void *v) |
| 1324 | { |
| 1325 | struct trace_event_call *call = event_file_data(m->private); |
| 1326 | struct ftrace_event_field *field; |
| 1327 | const char *array_descriptor; |
| 1328 | |
| 1329 | switch ((unsigned long)v) { |
| 1330 | case FORMAT_HEADER: |
| 1331 | seq_printf(m, "name: %s\n", trace_event_name(call)); |
| 1332 | seq_printf(m, "ID: %d\n", call->event.type); |
| 1333 | seq_puts(m, "format:\n"); |
| 1334 | return 0; |
| 1335 | |
| 1336 | case FORMAT_FIELD_SEPERATOR: |
| 1337 | seq_putc(m, '\n'); |
| 1338 | return 0; |
| 1339 | |
| 1340 | case FORMAT_PRINTFMT: |
| 1341 | seq_printf(m, "\nprint fmt: %s\n", |
| 1342 | call->print_fmt); |
| 1343 | return 0; |
| 1344 | } |
| 1345 | |
| 1346 | field = list_entry(v, struct ftrace_event_field, link); |
| 1347 | /* |
| 1348 | * Smartly shows the array type(except dynamic array). |
| 1349 | * Normal: |
| 1350 | * field:TYPE VAR |
| 1351 | * If TYPE := TYPE[LEN], it is shown: |
| 1352 | * field:TYPE VAR[LEN] |
| 1353 | */ |
| 1354 | array_descriptor = strchr(field->type, '['); |
| 1355 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1356 | if (str_has_prefix(field->type, "__data_loc")) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1357 | array_descriptor = NULL; |
| 1358 | |
| 1359 | if (!array_descriptor) |
| 1360 | seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n", |
| 1361 | field->type, field->name, field->offset, |
| 1362 | field->size, !!field->is_signed); |
| 1363 | else |
| 1364 | seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n", |
| 1365 | (int)(array_descriptor - field->type), |
| 1366 | field->type, field->name, |
| 1367 | array_descriptor, field->offset, |
| 1368 | field->size, !!field->is_signed); |
| 1369 | |
| 1370 | return 0; |
| 1371 | } |
| 1372 | |
| 1373 | static void *f_start(struct seq_file *m, loff_t *pos) |
| 1374 | { |
| 1375 | void *p = (void *)FORMAT_HEADER; |
| 1376 | loff_t l = 0; |
| 1377 | |
| 1378 | /* ->stop() is called even if ->start() fails */ |
| 1379 | mutex_lock(&event_mutex); |
| 1380 | if (!event_file_data(m->private)) |
| 1381 | return ERR_PTR(-ENODEV); |
| 1382 | |
| 1383 | while (l < *pos && p) |
| 1384 | p = f_next(m, p, &l); |
| 1385 | |
| 1386 | return p; |
| 1387 | } |
| 1388 | |
| 1389 | static void f_stop(struct seq_file *m, void *p) |
| 1390 | { |
| 1391 | mutex_unlock(&event_mutex); |
| 1392 | } |
| 1393 | |
| 1394 | static const struct seq_operations trace_format_seq_ops = { |
| 1395 | .start = f_start, |
| 1396 | .next = f_next, |
| 1397 | .stop = f_stop, |
| 1398 | .show = f_show, |
| 1399 | }; |
| 1400 | |
| 1401 | static int trace_format_open(struct inode *inode, struct file *file) |
| 1402 | { |
| 1403 | struct seq_file *m; |
| 1404 | int ret; |
| 1405 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1406 | /* Do we want to hide event format files on tracefs lockdown? */ |
| 1407 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1408 | ret = seq_open(file, &trace_format_seq_ops); |
| 1409 | if (ret < 0) |
| 1410 | return ret; |
| 1411 | |
| 1412 | m = file->private_data; |
| 1413 | m->private = file; |
| 1414 | |
| 1415 | return 0; |
| 1416 | } |
| 1417 | |
| 1418 | static ssize_t |
| 1419 | event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos) |
| 1420 | { |
| 1421 | int id = (long)event_file_data(filp); |
| 1422 | char buf[32]; |
| 1423 | int len; |
| 1424 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1425 | if (unlikely(!id)) |
| 1426 | return -ENODEV; |
| 1427 | |
| 1428 | len = sprintf(buf, "%d\n", id); |
| 1429 | |
| 1430 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, len); |
| 1431 | } |
| 1432 | |
| 1433 | static ssize_t |
| 1434 | event_filter_read(struct file *filp, char __user *ubuf, size_t cnt, |
| 1435 | loff_t *ppos) |
| 1436 | { |
| 1437 | struct trace_event_file *file; |
| 1438 | struct trace_seq *s; |
| 1439 | int r = -ENODEV; |
| 1440 | |
| 1441 | if (*ppos) |
| 1442 | return 0; |
| 1443 | |
| 1444 | s = kmalloc(sizeof(*s), GFP_KERNEL); |
| 1445 | |
| 1446 | if (!s) |
| 1447 | return -ENOMEM; |
| 1448 | |
| 1449 | trace_seq_init(s); |
| 1450 | |
| 1451 | mutex_lock(&event_mutex); |
| 1452 | file = event_file_data(filp); |
| 1453 | if (file) |
| 1454 | print_event_filter(file, s); |
| 1455 | mutex_unlock(&event_mutex); |
| 1456 | |
| 1457 | if (file) |
| 1458 | r = simple_read_from_buffer(ubuf, cnt, ppos, |
| 1459 | s->buffer, trace_seq_used(s)); |
| 1460 | |
| 1461 | kfree(s); |
| 1462 | |
| 1463 | return r; |
| 1464 | } |
| 1465 | |
| 1466 | static ssize_t |
| 1467 | event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt, |
| 1468 | loff_t *ppos) |
| 1469 | { |
| 1470 | struct trace_event_file *file; |
| 1471 | char *buf; |
| 1472 | int err = -ENODEV; |
| 1473 | |
| 1474 | if (cnt >= PAGE_SIZE) |
| 1475 | return -EINVAL; |
| 1476 | |
| 1477 | buf = memdup_user_nul(ubuf, cnt); |
| 1478 | if (IS_ERR(buf)) |
| 1479 | return PTR_ERR(buf); |
| 1480 | |
| 1481 | mutex_lock(&event_mutex); |
| 1482 | file = event_file_data(filp); |
| 1483 | if (file) |
| 1484 | err = apply_event_filter(file, buf); |
| 1485 | mutex_unlock(&event_mutex); |
| 1486 | |
| 1487 | kfree(buf); |
| 1488 | if (err < 0) |
| 1489 | return err; |
| 1490 | |
| 1491 | *ppos += cnt; |
| 1492 | |
| 1493 | return cnt; |
| 1494 | } |
| 1495 | |
| 1496 | static LIST_HEAD(event_subsystems); |
| 1497 | |
| 1498 | static int subsystem_open(struct inode *inode, struct file *filp) |
| 1499 | { |
| 1500 | struct event_subsystem *system = NULL; |
| 1501 | struct trace_subsystem_dir *dir = NULL; /* Initialize for gcc */ |
| 1502 | struct trace_array *tr; |
| 1503 | int ret; |
| 1504 | |
| 1505 | if (tracing_is_disabled()) |
| 1506 | return -ENODEV; |
| 1507 | |
| 1508 | /* Make sure the system still exists */ |
| 1509 | mutex_lock(&event_mutex); |
| 1510 | mutex_lock(&trace_types_lock); |
| 1511 | list_for_each_entry(tr, &ftrace_trace_arrays, list) { |
| 1512 | list_for_each_entry(dir, &tr->systems, list) { |
| 1513 | if (dir == inode->i_private) { |
| 1514 | /* Don't open systems with no events */ |
| 1515 | if (dir->nr_events) { |
| 1516 | __get_system_dir(dir); |
| 1517 | system = dir->subsystem; |
| 1518 | } |
| 1519 | goto exit_loop; |
| 1520 | } |
| 1521 | } |
| 1522 | } |
| 1523 | exit_loop: |
| 1524 | mutex_unlock(&trace_types_lock); |
| 1525 | mutex_unlock(&event_mutex); |
| 1526 | |
| 1527 | if (!system) |
| 1528 | return -ENODEV; |
| 1529 | |
| 1530 | /* Some versions of gcc think dir can be uninitialized here */ |
| 1531 | WARN_ON(!dir); |
| 1532 | |
| 1533 | /* Still need to increment the ref count of the system */ |
| 1534 | if (trace_array_get(tr) < 0) { |
| 1535 | put_system(dir); |
| 1536 | return -ENODEV; |
| 1537 | } |
| 1538 | |
| 1539 | ret = tracing_open_generic(inode, filp); |
| 1540 | if (ret < 0) { |
| 1541 | trace_array_put(tr); |
| 1542 | put_system(dir); |
| 1543 | } |
| 1544 | |
| 1545 | return ret; |
| 1546 | } |
| 1547 | |
| 1548 | static int system_tr_open(struct inode *inode, struct file *filp) |
| 1549 | { |
| 1550 | struct trace_subsystem_dir *dir; |
| 1551 | struct trace_array *tr = inode->i_private; |
| 1552 | int ret; |
| 1553 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1554 | /* Make a temporary dir that has no system but points to tr */ |
| 1555 | dir = kzalloc(sizeof(*dir), GFP_KERNEL); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1556 | if (!dir) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1557 | return -ENOMEM; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1558 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1559 | ret = tracing_open_generic_tr(inode, filp); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1560 | if (ret < 0) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1561 | kfree(dir); |
| 1562 | return ret; |
| 1563 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1564 | dir->tr = tr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1565 | filp->private_data = dir; |
| 1566 | |
| 1567 | return 0; |
| 1568 | } |
| 1569 | |
| 1570 | static int subsystem_release(struct inode *inode, struct file *file) |
| 1571 | { |
| 1572 | struct trace_subsystem_dir *dir = file->private_data; |
| 1573 | |
| 1574 | trace_array_put(dir->tr); |
| 1575 | |
| 1576 | /* |
| 1577 | * If dir->subsystem is NULL, then this is a temporary |
| 1578 | * descriptor that was made for a trace_array to enable |
| 1579 | * all subsystems. |
| 1580 | */ |
| 1581 | if (dir->subsystem) |
| 1582 | put_system(dir); |
| 1583 | else |
| 1584 | kfree(dir); |
| 1585 | |
| 1586 | return 0; |
| 1587 | } |
| 1588 | |
| 1589 | static ssize_t |
| 1590 | subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt, |
| 1591 | loff_t *ppos) |
| 1592 | { |
| 1593 | struct trace_subsystem_dir *dir = filp->private_data; |
| 1594 | struct event_subsystem *system = dir->subsystem; |
| 1595 | struct trace_seq *s; |
| 1596 | int r; |
| 1597 | |
| 1598 | if (*ppos) |
| 1599 | return 0; |
| 1600 | |
| 1601 | s = kmalloc(sizeof(*s), GFP_KERNEL); |
| 1602 | if (!s) |
| 1603 | return -ENOMEM; |
| 1604 | |
| 1605 | trace_seq_init(s); |
| 1606 | |
| 1607 | print_subsystem_event_filter(system, s); |
| 1608 | r = simple_read_from_buffer(ubuf, cnt, ppos, |
| 1609 | s->buffer, trace_seq_used(s)); |
| 1610 | |
| 1611 | kfree(s); |
| 1612 | |
| 1613 | return r; |
| 1614 | } |
| 1615 | |
| 1616 | static ssize_t |
| 1617 | subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt, |
| 1618 | loff_t *ppos) |
| 1619 | { |
| 1620 | struct trace_subsystem_dir *dir = filp->private_data; |
| 1621 | char *buf; |
| 1622 | int err; |
| 1623 | |
| 1624 | if (cnt >= PAGE_SIZE) |
| 1625 | return -EINVAL; |
| 1626 | |
| 1627 | buf = memdup_user_nul(ubuf, cnt); |
| 1628 | if (IS_ERR(buf)) |
| 1629 | return PTR_ERR(buf); |
| 1630 | |
| 1631 | err = apply_subsystem_event_filter(dir, buf); |
| 1632 | kfree(buf); |
| 1633 | if (err < 0) |
| 1634 | return err; |
| 1635 | |
| 1636 | *ppos += cnt; |
| 1637 | |
| 1638 | return cnt; |
| 1639 | } |
| 1640 | |
| 1641 | static ssize_t |
| 1642 | show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos) |
| 1643 | { |
| 1644 | int (*func)(struct trace_seq *s) = filp->private_data; |
| 1645 | struct trace_seq *s; |
| 1646 | int r; |
| 1647 | |
| 1648 | if (*ppos) |
| 1649 | return 0; |
| 1650 | |
| 1651 | s = kmalloc(sizeof(*s), GFP_KERNEL); |
| 1652 | if (!s) |
| 1653 | return -ENOMEM; |
| 1654 | |
| 1655 | trace_seq_init(s); |
| 1656 | |
| 1657 | func(s); |
| 1658 | r = simple_read_from_buffer(ubuf, cnt, ppos, |
| 1659 | s->buffer, trace_seq_used(s)); |
| 1660 | |
| 1661 | kfree(s); |
| 1662 | |
| 1663 | return r; |
| 1664 | } |
| 1665 | |
| 1666 | static void ignore_task_cpu(void *data) |
| 1667 | { |
| 1668 | struct trace_array *tr = data; |
| 1669 | struct trace_pid_list *pid_list; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1670 | struct trace_pid_list *no_pid_list; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1671 | |
| 1672 | /* |
| 1673 | * This function is called by on_each_cpu() while the |
| 1674 | * event_mutex is held. |
| 1675 | */ |
| 1676 | pid_list = rcu_dereference_protected(tr->filtered_pids, |
| 1677 | mutex_is_locked(&event_mutex)); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1678 | no_pid_list = rcu_dereference_protected(tr->filtered_no_pids, |
| 1679 | mutex_is_locked(&event_mutex)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1680 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1681 | this_cpu_write(tr->array_buffer.data->ignore_pid, |
| 1682 | trace_ignore_this_task(pid_list, no_pid_list, current)); |
| 1683 | } |
| 1684 | |
| 1685 | static void register_pid_events(struct trace_array *tr) |
| 1686 | { |
| 1687 | /* |
| 1688 | * Register a probe that is called before all other probes |
| 1689 | * to set ignore_pid if next or prev do not match. |
| 1690 | * Register a probe this is called after all other probes |
| 1691 | * to only keep ignore_pid set if next pid matches. |
| 1692 | */ |
| 1693 | register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre, |
| 1694 | tr, INT_MAX); |
| 1695 | register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post, |
| 1696 | tr, 0); |
| 1697 | |
| 1698 | register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, |
| 1699 | tr, INT_MAX); |
| 1700 | register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, |
| 1701 | tr, 0); |
| 1702 | |
| 1703 | register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre, |
| 1704 | tr, INT_MAX); |
| 1705 | register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post, |
| 1706 | tr, 0); |
| 1707 | |
| 1708 | register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_pre, |
| 1709 | tr, INT_MAX); |
| 1710 | register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_post, |
| 1711 | tr, 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
| 1714 | static ssize_t |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1715 | event_pid_write(struct file *filp, const char __user *ubuf, |
| 1716 | size_t cnt, loff_t *ppos, int type) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1717 | { |
| 1718 | struct seq_file *m = filp->private_data; |
| 1719 | struct trace_array *tr = m->private; |
| 1720 | struct trace_pid_list *filtered_pids = NULL; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1721 | struct trace_pid_list *other_pids = NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1722 | struct trace_pid_list *pid_list; |
| 1723 | struct trace_event_file *file; |
| 1724 | ssize_t ret; |
| 1725 | |
| 1726 | if (!cnt) |
| 1727 | return 0; |
| 1728 | |
| 1729 | ret = tracing_update_buffers(); |
| 1730 | if (ret < 0) |
| 1731 | return ret; |
| 1732 | |
| 1733 | mutex_lock(&event_mutex); |
| 1734 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1735 | if (type == TRACE_PIDS) { |
| 1736 | filtered_pids = rcu_dereference_protected(tr->filtered_pids, |
| 1737 | lockdep_is_held(&event_mutex)); |
| 1738 | other_pids = rcu_dereference_protected(tr->filtered_no_pids, |
| 1739 | lockdep_is_held(&event_mutex)); |
| 1740 | } else { |
| 1741 | filtered_pids = rcu_dereference_protected(tr->filtered_no_pids, |
| 1742 | lockdep_is_held(&event_mutex)); |
| 1743 | other_pids = rcu_dereference_protected(tr->filtered_pids, |
| 1744 | lockdep_is_held(&event_mutex)); |
| 1745 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1746 | |
| 1747 | ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt); |
| 1748 | if (ret < 0) |
| 1749 | goto out; |
| 1750 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1751 | if (type == TRACE_PIDS) |
| 1752 | rcu_assign_pointer(tr->filtered_pids, pid_list); |
| 1753 | else |
| 1754 | rcu_assign_pointer(tr->filtered_no_pids, pid_list); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1755 | |
| 1756 | list_for_each_entry(file, &tr->events, list) { |
| 1757 | set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags); |
| 1758 | } |
| 1759 | |
| 1760 | if (filtered_pids) { |
| 1761 | tracepoint_synchronize_unregister(); |
| 1762 | trace_free_pid_list(filtered_pids); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1763 | } else if (pid_list && !other_pids) { |
| 1764 | register_pid_events(tr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1765 | } |
| 1766 | |
| 1767 | /* |
| 1768 | * Ignoring of pids is done at task switch. But we have to |
| 1769 | * check for those tasks that are currently running. |
| 1770 | * Always do this in case a pid was appended or removed. |
| 1771 | */ |
| 1772 | on_each_cpu(ignore_task_cpu, tr, 1); |
| 1773 | |
| 1774 | out: |
| 1775 | mutex_unlock(&event_mutex); |
| 1776 | |
| 1777 | if (ret > 0) |
| 1778 | *ppos += ret; |
| 1779 | |
| 1780 | return ret; |
| 1781 | } |
| 1782 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1783 | static ssize_t |
| 1784 | ftrace_event_pid_write(struct file *filp, const char __user *ubuf, |
| 1785 | size_t cnt, loff_t *ppos) |
| 1786 | { |
| 1787 | return event_pid_write(filp, ubuf, cnt, ppos, TRACE_PIDS); |
| 1788 | } |
| 1789 | |
| 1790 | static ssize_t |
| 1791 | ftrace_event_npid_write(struct file *filp, const char __user *ubuf, |
| 1792 | size_t cnt, loff_t *ppos) |
| 1793 | { |
| 1794 | return event_pid_write(filp, ubuf, cnt, ppos, TRACE_NO_PIDS); |
| 1795 | } |
| 1796 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1797 | static int ftrace_event_avail_open(struct inode *inode, struct file *file); |
| 1798 | static int ftrace_event_set_open(struct inode *inode, struct file *file); |
| 1799 | static int ftrace_event_set_pid_open(struct inode *inode, struct file *file); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1800 | static int ftrace_event_set_npid_open(struct inode *inode, struct file *file); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1801 | static int ftrace_event_release(struct inode *inode, struct file *file); |
| 1802 | |
| 1803 | static const struct seq_operations show_event_seq_ops = { |
| 1804 | .start = t_start, |
| 1805 | .next = t_next, |
| 1806 | .show = t_show, |
| 1807 | .stop = t_stop, |
| 1808 | }; |
| 1809 | |
| 1810 | static const struct seq_operations show_set_event_seq_ops = { |
| 1811 | .start = s_start, |
| 1812 | .next = s_next, |
| 1813 | .show = t_show, |
| 1814 | .stop = t_stop, |
| 1815 | }; |
| 1816 | |
| 1817 | static const struct seq_operations show_set_pid_seq_ops = { |
| 1818 | .start = p_start, |
| 1819 | .next = p_next, |
| 1820 | .show = trace_pid_show, |
| 1821 | .stop = p_stop, |
| 1822 | }; |
| 1823 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1824 | static const struct seq_operations show_set_no_pid_seq_ops = { |
| 1825 | .start = np_start, |
| 1826 | .next = np_next, |
| 1827 | .show = trace_pid_show, |
| 1828 | .stop = p_stop, |
| 1829 | }; |
| 1830 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1831 | static const struct file_operations ftrace_avail_fops = { |
| 1832 | .open = ftrace_event_avail_open, |
| 1833 | .read = seq_read, |
| 1834 | .llseek = seq_lseek, |
| 1835 | .release = seq_release, |
| 1836 | }; |
| 1837 | |
| 1838 | static const struct file_operations ftrace_set_event_fops = { |
| 1839 | .open = ftrace_event_set_open, |
| 1840 | .read = seq_read, |
| 1841 | .write = ftrace_event_write, |
| 1842 | .llseek = seq_lseek, |
| 1843 | .release = ftrace_event_release, |
| 1844 | }; |
| 1845 | |
| 1846 | static const struct file_operations ftrace_set_event_pid_fops = { |
| 1847 | .open = ftrace_event_set_pid_open, |
| 1848 | .read = seq_read, |
| 1849 | .write = ftrace_event_pid_write, |
| 1850 | .llseek = seq_lseek, |
| 1851 | .release = ftrace_event_release, |
| 1852 | }; |
| 1853 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1854 | static const struct file_operations ftrace_set_event_notrace_pid_fops = { |
| 1855 | .open = ftrace_event_set_npid_open, |
| 1856 | .read = seq_read, |
| 1857 | .write = ftrace_event_npid_write, |
| 1858 | .llseek = seq_lseek, |
| 1859 | .release = ftrace_event_release, |
| 1860 | }; |
| 1861 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1862 | static const struct file_operations ftrace_enable_fops = { |
| 1863 | .open = tracing_open_generic, |
| 1864 | .read = event_enable_read, |
| 1865 | .write = event_enable_write, |
| 1866 | .llseek = default_llseek, |
| 1867 | }; |
| 1868 | |
| 1869 | static const struct file_operations ftrace_event_format_fops = { |
| 1870 | .open = trace_format_open, |
| 1871 | .read = seq_read, |
| 1872 | .llseek = seq_lseek, |
| 1873 | .release = seq_release, |
| 1874 | }; |
| 1875 | |
| 1876 | static const struct file_operations ftrace_event_id_fops = { |
| 1877 | .read = event_id_read, |
| 1878 | .llseek = default_llseek, |
| 1879 | }; |
| 1880 | |
| 1881 | static const struct file_operations ftrace_event_filter_fops = { |
| 1882 | .open = tracing_open_generic, |
| 1883 | .read = event_filter_read, |
| 1884 | .write = event_filter_write, |
| 1885 | .llseek = default_llseek, |
| 1886 | }; |
| 1887 | |
| 1888 | static const struct file_operations ftrace_subsystem_filter_fops = { |
| 1889 | .open = subsystem_open, |
| 1890 | .read = subsystem_filter_read, |
| 1891 | .write = subsystem_filter_write, |
| 1892 | .llseek = default_llseek, |
| 1893 | .release = subsystem_release, |
| 1894 | }; |
| 1895 | |
| 1896 | static const struct file_operations ftrace_system_enable_fops = { |
| 1897 | .open = subsystem_open, |
| 1898 | .read = system_enable_read, |
| 1899 | .write = system_enable_write, |
| 1900 | .llseek = default_llseek, |
| 1901 | .release = subsystem_release, |
| 1902 | }; |
| 1903 | |
| 1904 | static const struct file_operations ftrace_tr_enable_fops = { |
| 1905 | .open = system_tr_open, |
| 1906 | .read = system_enable_read, |
| 1907 | .write = system_enable_write, |
| 1908 | .llseek = default_llseek, |
| 1909 | .release = subsystem_release, |
| 1910 | }; |
| 1911 | |
| 1912 | static const struct file_operations ftrace_show_header_fops = { |
| 1913 | .open = tracing_open_generic, |
| 1914 | .read = show_header, |
| 1915 | .llseek = default_llseek, |
| 1916 | }; |
| 1917 | |
| 1918 | static int |
| 1919 | ftrace_event_open(struct inode *inode, struct file *file, |
| 1920 | const struct seq_operations *seq_ops) |
| 1921 | { |
| 1922 | struct seq_file *m; |
| 1923 | int ret; |
| 1924 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1925 | ret = security_locked_down(LOCKDOWN_TRACEFS); |
| 1926 | if (ret) |
| 1927 | return ret; |
| 1928 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1929 | ret = seq_open(file, seq_ops); |
| 1930 | if (ret < 0) |
| 1931 | return ret; |
| 1932 | m = file->private_data; |
| 1933 | /* copy tr over to seq ops */ |
| 1934 | m->private = inode->i_private; |
| 1935 | |
| 1936 | return ret; |
| 1937 | } |
| 1938 | |
| 1939 | static int ftrace_event_release(struct inode *inode, struct file *file) |
| 1940 | { |
| 1941 | struct trace_array *tr = inode->i_private; |
| 1942 | |
| 1943 | trace_array_put(tr); |
| 1944 | |
| 1945 | return seq_release(inode, file); |
| 1946 | } |
| 1947 | |
| 1948 | static int |
| 1949 | ftrace_event_avail_open(struct inode *inode, struct file *file) |
| 1950 | { |
| 1951 | const struct seq_operations *seq_ops = &show_event_seq_ops; |
| 1952 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1953 | /* Checks for tracefs lockdown */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1954 | return ftrace_event_open(inode, file, seq_ops); |
| 1955 | } |
| 1956 | |
| 1957 | static int |
| 1958 | ftrace_event_set_open(struct inode *inode, struct file *file) |
| 1959 | { |
| 1960 | const struct seq_operations *seq_ops = &show_set_event_seq_ops; |
| 1961 | struct trace_array *tr = inode->i_private; |
| 1962 | int ret; |
| 1963 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1964 | ret = tracing_check_open_get_tr(tr); |
| 1965 | if (ret) |
| 1966 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1967 | |
| 1968 | if ((file->f_mode & FMODE_WRITE) && |
| 1969 | (file->f_flags & O_TRUNC)) |
| 1970 | ftrace_clear_events(tr); |
| 1971 | |
| 1972 | ret = ftrace_event_open(inode, file, seq_ops); |
| 1973 | if (ret < 0) |
| 1974 | trace_array_put(tr); |
| 1975 | return ret; |
| 1976 | } |
| 1977 | |
| 1978 | static int |
| 1979 | ftrace_event_set_pid_open(struct inode *inode, struct file *file) |
| 1980 | { |
| 1981 | const struct seq_operations *seq_ops = &show_set_pid_seq_ops; |
| 1982 | struct trace_array *tr = inode->i_private; |
| 1983 | int ret; |
| 1984 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1985 | ret = tracing_check_open_get_tr(tr); |
| 1986 | if (ret) |
| 1987 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1988 | |
| 1989 | if ((file->f_mode & FMODE_WRITE) && |
| 1990 | (file->f_flags & O_TRUNC)) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1991 | ftrace_clear_event_pids(tr, TRACE_PIDS); |
| 1992 | |
| 1993 | ret = ftrace_event_open(inode, file, seq_ops); |
| 1994 | if (ret < 0) |
| 1995 | trace_array_put(tr); |
| 1996 | return ret; |
| 1997 | } |
| 1998 | |
| 1999 | static int |
| 2000 | ftrace_event_set_npid_open(struct inode *inode, struct file *file) |
| 2001 | { |
| 2002 | const struct seq_operations *seq_ops = &show_set_no_pid_seq_ops; |
| 2003 | struct trace_array *tr = inode->i_private; |
| 2004 | int ret; |
| 2005 | |
| 2006 | ret = tracing_check_open_get_tr(tr); |
| 2007 | if (ret) |
| 2008 | return ret; |
| 2009 | |
| 2010 | if ((file->f_mode & FMODE_WRITE) && |
| 2011 | (file->f_flags & O_TRUNC)) |
| 2012 | ftrace_clear_event_pids(tr, TRACE_NO_PIDS); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2013 | |
| 2014 | ret = ftrace_event_open(inode, file, seq_ops); |
| 2015 | if (ret < 0) |
| 2016 | trace_array_put(tr); |
| 2017 | return ret; |
| 2018 | } |
| 2019 | |
| 2020 | static struct event_subsystem * |
| 2021 | create_new_subsystem(const char *name) |
| 2022 | { |
| 2023 | struct event_subsystem *system; |
| 2024 | |
| 2025 | /* need to create new entry */ |
| 2026 | system = kmalloc(sizeof(*system), GFP_KERNEL); |
| 2027 | if (!system) |
| 2028 | return NULL; |
| 2029 | |
| 2030 | system->ref_count = 1; |
| 2031 | |
| 2032 | /* Only allocate if dynamic (kprobes and modules) */ |
| 2033 | system->name = kstrdup_const(name, GFP_KERNEL); |
| 2034 | if (!system->name) |
| 2035 | goto out_free; |
| 2036 | |
| 2037 | system->filter = NULL; |
| 2038 | |
| 2039 | system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL); |
| 2040 | if (!system->filter) |
| 2041 | goto out_free; |
| 2042 | |
| 2043 | list_add(&system->list, &event_subsystems); |
| 2044 | |
| 2045 | return system; |
| 2046 | |
| 2047 | out_free: |
| 2048 | kfree_const(system->name); |
| 2049 | kfree(system); |
| 2050 | return NULL; |
| 2051 | } |
| 2052 | |
| 2053 | static struct dentry * |
| 2054 | event_subsystem_dir(struct trace_array *tr, const char *name, |
| 2055 | struct trace_event_file *file, struct dentry *parent) |
| 2056 | { |
| 2057 | struct trace_subsystem_dir *dir; |
| 2058 | struct event_subsystem *system; |
| 2059 | struct dentry *entry; |
| 2060 | |
| 2061 | /* First see if we did not already create this dir */ |
| 2062 | list_for_each_entry(dir, &tr->systems, list) { |
| 2063 | system = dir->subsystem; |
| 2064 | if (strcmp(system->name, name) == 0) { |
| 2065 | dir->nr_events++; |
| 2066 | file->system = dir; |
| 2067 | return dir->entry; |
| 2068 | } |
| 2069 | } |
| 2070 | |
| 2071 | /* Now see if the system itself exists. */ |
| 2072 | list_for_each_entry(system, &event_subsystems, list) { |
| 2073 | if (strcmp(system->name, name) == 0) |
| 2074 | break; |
| 2075 | } |
| 2076 | /* Reset system variable when not found */ |
| 2077 | if (&system->list == &event_subsystems) |
| 2078 | system = NULL; |
| 2079 | |
| 2080 | dir = kmalloc(sizeof(*dir), GFP_KERNEL); |
| 2081 | if (!dir) |
| 2082 | goto out_fail; |
| 2083 | |
| 2084 | if (!system) { |
| 2085 | system = create_new_subsystem(name); |
| 2086 | if (!system) |
| 2087 | goto out_free; |
| 2088 | } else |
| 2089 | __get_system(system); |
| 2090 | |
| 2091 | dir->entry = tracefs_create_dir(name, parent); |
| 2092 | if (!dir->entry) { |
| 2093 | pr_warn("Failed to create system directory %s\n", name); |
| 2094 | __put_system(system); |
| 2095 | goto out_free; |
| 2096 | } |
| 2097 | |
| 2098 | dir->tr = tr; |
| 2099 | dir->ref_count = 1; |
| 2100 | dir->nr_events = 1; |
| 2101 | dir->subsystem = system; |
| 2102 | file->system = dir; |
| 2103 | |
| 2104 | entry = tracefs_create_file("filter", 0644, dir->entry, dir, |
| 2105 | &ftrace_subsystem_filter_fops); |
| 2106 | if (!entry) { |
| 2107 | kfree(system->filter); |
| 2108 | system->filter = NULL; |
| 2109 | pr_warn("Could not create tracefs '%s/filter' entry\n", name); |
| 2110 | } |
| 2111 | |
| 2112 | trace_create_file("enable", 0644, dir->entry, dir, |
| 2113 | &ftrace_system_enable_fops); |
| 2114 | |
| 2115 | list_add(&dir->list, &tr->systems); |
| 2116 | |
| 2117 | return dir->entry; |
| 2118 | |
| 2119 | out_free: |
| 2120 | kfree(dir); |
| 2121 | out_fail: |
| 2122 | /* Only print this message if failed on memory allocation */ |
| 2123 | if (!dir || !system) |
| 2124 | pr_warn("No memory to create event subsystem %s\n", name); |
| 2125 | return NULL; |
| 2126 | } |
| 2127 | |
| 2128 | static int |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2129 | event_define_fields(struct trace_event_call *call) |
| 2130 | { |
| 2131 | struct list_head *head; |
| 2132 | int ret = 0; |
| 2133 | |
| 2134 | /* |
| 2135 | * Other events may have the same class. Only update |
| 2136 | * the fields if they are not already defined. |
| 2137 | */ |
| 2138 | head = trace_get_fields(call); |
| 2139 | if (list_empty(head)) { |
| 2140 | struct trace_event_fields *field = call->class->fields_array; |
| 2141 | unsigned int offset = sizeof(struct trace_entry); |
| 2142 | |
| 2143 | for (; field->type; field++) { |
| 2144 | if (field->type == TRACE_FUNCTION_TYPE) { |
| 2145 | field->define_fields(call); |
| 2146 | break; |
| 2147 | } |
| 2148 | |
| 2149 | offset = ALIGN(offset, field->align); |
| 2150 | ret = trace_define_field(call, field->type, field->name, |
| 2151 | offset, field->size, |
| 2152 | field->is_signed, field->filter_type); |
| 2153 | if (WARN_ON_ONCE(ret)) { |
| 2154 | pr_err("error code is %d\n", ret); |
| 2155 | break; |
| 2156 | } |
| 2157 | |
| 2158 | offset += field->size; |
| 2159 | } |
| 2160 | } |
| 2161 | |
| 2162 | return ret; |
| 2163 | } |
| 2164 | |
| 2165 | static int |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2166 | event_create_dir(struct dentry *parent, struct trace_event_file *file) |
| 2167 | { |
| 2168 | struct trace_event_call *call = file->event_call; |
| 2169 | struct trace_array *tr = file->tr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2170 | struct dentry *d_events; |
| 2171 | const char *name; |
| 2172 | int ret; |
| 2173 | |
| 2174 | /* |
| 2175 | * If the trace point header did not define TRACE_SYSTEM |
| 2176 | * then the system would be called "TRACE_SYSTEM". |
| 2177 | */ |
| 2178 | if (strcmp(call->class->system, TRACE_SYSTEM) != 0) { |
| 2179 | d_events = event_subsystem_dir(tr, call->class->system, file, parent); |
| 2180 | if (!d_events) |
| 2181 | return -ENOMEM; |
| 2182 | } else |
| 2183 | d_events = parent; |
| 2184 | |
| 2185 | name = trace_event_name(call); |
| 2186 | file->dir = tracefs_create_dir(name, d_events); |
| 2187 | if (!file->dir) { |
| 2188 | pr_warn("Could not create tracefs '%s' directory\n", name); |
| 2189 | return -1; |
| 2190 | } |
| 2191 | |
| 2192 | if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)) |
| 2193 | trace_create_file("enable", 0644, file->dir, file, |
| 2194 | &ftrace_enable_fops); |
| 2195 | |
| 2196 | #ifdef CONFIG_PERF_EVENTS |
| 2197 | if (call->event.type && call->class->reg) |
| 2198 | trace_create_file("id", 0444, file->dir, |
| 2199 | (void *)(long)call->event.type, |
| 2200 | &ftrace_event_id_fops); |
| 2201 | #endif |
| 2202 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2203 | ret = event_define_fields(call); |
| 2204 | if (ret < 0) { |
| 2205 | pr_warn("Could not initialize trace point events/%s\n", name); |
| 2206 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2207 | } |
| 2208 | |
| 2209 | /* |
| 2210 | * Only event directories that can be enabled should have |
| 2211 | * triggers or filters. |
| 2212 | */ |
| 2213 | if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)) { |
| 2214 | trace_create_file("filter", 0644, file->dir, file, |
| 2215 | &ftrace_event_filter_fops); |
| 2216 | |
| 2217 | trace_create_file("trigger", 0644, file->dir, file, |
| 2218 | &event_trigger_fops); |
| 2219 | } |
| 2220 | |
| 2221 | #ifdef CONFIG_HIST_TRIGGERS |
| 2222 | trace_create_file("hist", 0444, file->dir, file, |
| 2223 | &event_hist_fops); |
| 2224 | #endif |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2225 | #ifdef CONFIG_HIST_TRIGGERS_DEBUG |
| 2226 | trace_create_file("hist_debug", 0444, file->dir, file, |
| 2227 | &event_hist_debug_fops); |
| 2228 | #endif |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2229 | trace_create_file("format", 0444, file->dir, call, |
| 2230 | &ftrace_event_format_fops); |
| 2231 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2232 | #ifdef CONFIG_TRACE_EVENT_INJECT |
| 2233 | if (call->event.type && call->class->reg) |
| 2234 | trace_create_file("inject", 0200, file->dir, file, |
| 2235 | &event_inject_fops); |
| 2236 | #endif |
| 2237 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2238 | return 0; |
| 2239 | } |
| 2240 | |
| 2241 | static void remove_event_from_tracers(struct trace_event_call *call) |
| 2242 | { |
| 2243 | struct trace_event_file *file; |
| 2244 | struct trace_array *tr; |
| 2245 | |
| 2246 | do_for_each_event_file_safe(tr, file) { |
| 2247 | if (file->event_call != call) |
| 2248 | continue; |
| 2249 | |
| 2250 | remove_event_file_dir(file); |
| 2251 | /* |
| 2252 | * The do_for_each_event_file_safe() is |
| 2253 | * a double loop. After finding the call for this |
| 2254 | * trace_array, we use break to jump to the next |
| 2255 | * trace_array. |
| 2256 | */ |
| 2257 | break; |
| 2258 | } while_for_each_event_file(); |
| 2259 | } |
| 2260 | |
| 2261 | static void event_remove(struct trace_event_call *call) |
| 2262 | { |
| 2263 | struct trace_array *tr; |
| 2264 | struct trace_event_file *file; |
| 2265 | |
| 2266 | do_for_each_event_file(tr, file) { |
| 2267 | if (file->event_call != call) |
| 2268 | continue; |
| 2269 | |
| 2270 | if (file->flags & EVENT_FILE_FL_WAS_ENABLED) |
| 2271 | tr->clear_trace = true; |
| 2272 | |
| 2273 | ftrace_event_enable_disable(file, 0); |
| 2274 | /* |
| 2275 | * The do_for_each_event_file() is |
| 2276 | * a double loop. After finding the call for this |
| 2277 | * trace_array, we use break to jump to the next |
| 2278 | * trace_array. |
| 2279 | */ |
| 2280 | break; |
| 2281 | } while_for_each_event_file(); |
| 2282 | |
| 2283 | if (call->event.funcs) |
| 2284 | __unregister_trace_event(&call->event); |
| 2285 | remove_event_from_tracers(call); |
| 2286 | list_del(&call->list); |
| 2287 | } |
| 2288 | |
| 2289 | static int event_init(struct trace_event_call *call) |
| 2290 | { |
| 2291 | int ret = 0; |
| 2292 | const char *name; |
| 2293 | |
| 2294 | name = trace_event_name(call); |
| 2295 | if (WARN_ON(!name)) |
| 2296 | return -EINVAL; |
| 2297 | |
| 2298 | if (call->class->raw_init) { |
| 2299 | ret = call->class->raw_init(call); |
| 2300 | if (ret < 0 && ret != -ENOSYS) |
| 2301 | pr_warn("Could not initialize trace events/%s\n", name); |
| 2302 | } |
| 2303 | |
| 2304 | return ret; |
| 2305 | } |
| 2306 | |
| 2307 | static int |
| 2308 | __register_event(struct trace_event_call *call, struct module *mod) |
| 2309 | { |
| 2310 | int ret; |
| 2311 | |
| 2312 | ret = event_init(call); |
| 2313 | if (ret < 0) |
| 2314 | return ret; |
| 2315 | |
| 2316 | list_add(&call->list, &ftrace_events); |
| 2317 | call->mod = mod; |
| 2318 | |
| 2319 | return 0; |
| 2320 | } |
| 2321 | |
| 2322 | static char *eval_replace(char *ptr, struct trace_eval_map *map, int len) |
| 2323 | { |
| 2324 | int rlen; |
| 2325 | int elen; |
| 2326 | |
| 2327 | /* Find the length of the eval value as a string */ |
| 2328 | elen = snprintf(ptr, 0, "%ld", map->eval_value); |
| 2329 | /* Make sure there's enough room to replace the string with the value */ |
| 2330 | if (len < elen) |
| 2331 | return NULL; |
| 2332 | |
| 2333 | snprintf(ptr, elen + 1, "%ld", map->eval_value); |
| 2334 | |
| 2335 | /* Get the rest of the string of ptr */ |
| 2336 | rlen = strlen(ptr + len); |
| 2337 | memmove(ptr + elen, ptr + len, rlen); |
| 2338 | /* Make sure we end the new string */ |
| 2339 | ptr[elen + rlen] = 0; |
| 2340 | |
| 2341 | return ptr + elen; |
| 2342 | } |
| 2343 | |
| 2344 | static void update_event_printk(struct trace_event_call *call, |
| 2345 | struct trace_eval_map *map) |
| 2346 | { |
| 2347 | char *ptr; |
| 2348 | int quote = 0; |
| 2349 | int len = strlen(map->eval_string); |
| 2350 | |
| 2351 | for (ptr = call->print_fmt; *ptr; ptr++) { |
| 2352 | if (*ptr == '\\') { |
| 2353 | ptr++; |
| 2354 | /* paranoid */ |
| 2355 | if (!*ptr) |
| 2356 | break; |
| 2357 | continue; |
| 2358 | } |
| 2359 | if (*ptr == '"') { |
| 2360 | quote ^= 1; |
| 2361 | continue; |
| 2362 | } |
| 2363 | if (quote) |
| 2364 | continue; |
| 2365 | if (isdigit(*ptr)) { |
| 2366 | /* skip numbers */ |
| 2367 | do { |
| 2368 | ptr++; |
| 2369 | /* Check for alpha chars like ULL */ |
| 2370 | } while (isalnum(*ptr)); |
| 2371 | if (!*ptr) |
| 2372 | break; |
| 2373 | /* |
| 2374 | * A number must have some kind of delimiter after |
| 2375 | * it, and we can ignore that too. |
| 2376 | */ |
| 2377 | continue; |
| 2378 | } |
| 2379 | if (isalpha(*ptr) || *ptr == '_') { |
| 2380 | if (strncmp(map->eval_string, ptr, len) == 0 && |
| 2381 | !isalnum(ptr[len]) && ptr[len] != '_') { |
| 2382 | ptr = eval_replace(ptr, map, len); |
| 2383 | /* enum/sizeof string smaller than value */ |
| 2384 | if (WARN_ON_ONCE(!ptr)) |
| 2385 | return; |
| 2386 | /* |
| 2387 | * No need to decrement here, as eval_replace() |
| 2388 | * returns the pointer to the character passed |
| 2389 | * the eval, and two evals can not be placed |
| 2390 | * back to back without something in between. |
| 2391 | * We can skip that something in between. |
| 2392 | */ |
| 2393 | continue; |
| 2394 | } |
| 2395 | skip_more: |
| 2396 | do { |
| 2397 | ptr++; |
| 2398 | } while (isalnum(*ptr) || *ptr == '_'); |
| 2399 | if (!*ptr) |
| 2400 | break; |
| 2401 | /* |
| 2402 | * If what comes after this variable is a '.' or |
| 2403 | * '->' then we can continue to ignore that string. |
| 2404 | */ |
| 2405 | if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) { |
| 2406 | ptr += *ptr == '.' ? 1 : 2; |
| 2407 | if (!*ptr) |
| 2408 | break; |
| 2409 | goto skip_more; |
| 2410 | } |
| 2411 | /* |
| 2412 | * Once again, we can skip the delimiter that came |
| 2413 | * after the string. |
| 2414 | */ |
| 2415 | continue; |
| 2416 | } |
| 2417 | } |
| 2418 | } |
| 2419 | |
| 2420 | void trace_event_eval_update(struct trace_eval_map **map, int len) |
| 2421 | { |
| 2422 | struct trace_event_call *call, *p; |
| 2423 | const char *last_system = NULL; |
| 2424 | bool first = false; |
| 2425 | int last_i; |
| 2426 | int i; |
| 2427 | |
| 2428 | down_write(&trace_event_sem); |
| 2429 | list_for_each_entry_safe(call, p, &ftrace_events, list) { |
| 2430 | /* events are usually grouped together with systems */ |
| 2431 | if (!last_system || call->class->system != last_system) { |
| 2432 | first = true; |
| 2433 | last_i = 0; |
| 2434 | last_system = call->class->system; |
| 2435 | } |
| 2436 | |
| 2437 | /* |
| 2438 | * Since calls are grouped by systems, the likelyhood that the |
| 2439 | * next call in the iteration belongs to the same system as the |
| 2440 | * previous call is high. As an optimization, we skip seaching |
| 2441 | * for a map[] that matches the call's system if the last call |
| 2442 | * was from the same system. That's what last_i is for. If the |
| 2443 | * call has the same system as the previous call, then last_i |
| 2444 | * will be the index of the first map[] that has a matching |
| 2445 | * system. |
| 2446 | */ |
| 2447 | for (i = last_i; i < len; i++) { |
| 2448 | if (call->class->system == map[i]->system) { |
| 2449 | /* Save the first system if need be */ |
| 2450 | if (first) { |
| 2451 | last_i = i; |
| 2452 | first = false; |
| 2453 | } |
| 2454 | update_event_printk(call, map[i]); |
| 2455 | } |
| 2456 | } |
| 2457 | } |
| 2458 | up_write(&trace_event_sem); |
| 2459 | } |
| 2460 | |
| 2461 | static struct trace_event_file * |
| 2462 | trace_create_new_event(struct trace_event_call *call, |
| 2463 | struct trace_array *tr) |
| 2464 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2465 | struct trace_pid_list *no_pid_list; |
| 2466 | struct trace_pid_list *pid_list; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2467 | struct trace_event_file *file; |
| 2468 | |
| 2469 | file = kmem_cache_alloc(file_cachep, GFP_TRACE); |
| 2470 | if (!file) |
| 2471 | return NULL; |
| 2472 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2473 | pid_list = rcu_dereference_protected(tr->filtered_pids, |
| 2474 | lockdep_is_held(&event_mutex)); |
| 2475 | no_pid_list = rcu_dereference_protected(tr->filtered_no_pids, |
| 2476 | lockdep_is_held(&event_mutex)); |
| 2477 | |
| 2478 | if (pid_list || no_pid_list) |
| 2479 | file->flags |= EVENT_FILE_FL_PID_FILTER; |
| 2480 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2481 | file->event_call = call; |
| 2482 | file->tr = tr; |
| 2483 | atomic_set(&file->sm_ref, 0); |
| 2484 | atomic_set(&file->tm_ref, 0); |
| 2485 | INIT_LIST_HEAD(&file->triggers); |
| 2486 | list_add(&file->list, &tr->events); |
| 2487 | |
| 2488 | return file; |
| 2489 | } |
| 2490 | |
| 2491 | /* Add an event to a trace directory */ |
| 2492 | static int |
| 2493 | __trace_add_new_event(struct trace_event_call *call, struct trace_array *tr) |
| 2494 | { |
| 2495 | struct trace_event_file *file; |
| 2496 | |
| 2497 | file = trace_create_new_event(call, tr); |
| 2498 | if (!file) |
| 2499 | return -ENOMEM; |
| 2500 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2501 | if (eventdir_initialized) |
| 2502 | return event_create_dir(tr->event_dir, file); |
| 2503 | else |
| 2504 | return event_define_fields(call); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2505 | } |
| 2506 | |
| 2507 | /* |
| 2508 | * Just create a decriptor for early init. A descriptor is required |
| 2509 | * for enabling events at boot. We want to enable events before |
| 2510 | * the filesystem is initialized. |
| 2511 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2512 | static int |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2513 | __trace_early_add_new_event(struct trace_event_call *call, |
| 2514 | struct trace_array *tr) |
| 2515 | { |
| 2516 | struct trace_event_file *file; |
| 2517 | |
| 2518 | file = trace_create_new_event(call, tr); |
| 2519 | if (!file) |
| 2520 | return -ENOMEM; |
| 2521 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2522 | return event_define_fields(call); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2523 | } |
| 2524 | |
| 2525 | struct ftrace_module_file_ops; |
| 2526 | static void __add_event_to_tracers(struct trace_event_call *call); |
| 2527 | |
| 2528 | /* Add an additional event_call dynamically */ |
| 2529 | int trace_add_event_call(struct trace_event_call *call) |
| 2530 | { |
| 2531 | int ret; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2532 | lockdep_assert_held(&event_mutex); |
| 2533 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2534 | mutex_lock(&trace_types_lock); |
| 2535 | |
| 2536 | ret = __register_event(call, NULL); |
| 2537 | if (ret >= 0) |
| 2538 | __add_event_to_tracers(call); |
| 2539 | |
| 2540 | mutex_unlock(&trace_types_lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2541 | return ret; |
| 2542 | } |
| 2543 | |
| 2544 | /* |
| 2545 | * Must be called under locking of trace_types_lock, event_mutex and |
| 2546 | * trace_event_sem. |
| 2547 | */ |
| 2548 | static void __trace_remove_event_call(struct trace_event_call *call) |
| 2549 | { |
| 2550 | event_remove(call); |
| 2551 | trace_destroy_fields(call); |
| 2552 | free_event_filter(call->filter); |
| 2553 | call->filter = NULL; |
| 2554 | } |
| 2555 | |
| 2556 | static int probe_remove_event_call(struct trace_event_call *call) |
| 2557 | { |
| 2558 | struct trace_array *tr; |
| 2559 | struct trace_event_file *file; |
| 2560 | |
| 2561 | #ifdef CONFIG_PERF_EVENTS |
| 2562 | if (call->perf_refcount) |
| 2563 | return -EBUSY; |
| 2564 | #endif |
| 2565 | do_for_each_event_file(tr, file) { |
| 2566 | if (file->event_call != call) |
| 2567 | continue; |
| 2568 | /* |
| 2569 | * We can't rely on ftrace_event_enable_disable(enable => 0) |
| 2570 | * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress |
| 2571 | * TRACE_REG_UNREGISTER. |
| 2572 | */ |
| 2573 | if (file->flags & EVENT_FILE_FL_ENABLED) |
| 2574 | return -EBUSY; |
| 2575 | /* |
| 2576 | * The do_for_each_event_file_safe() is |
| 2577 | * a double loop. After finding the call for this |
| 2578 | * trace_array, we use break to jump to the next |
| 2579 | * trace_array. |
| 2580 | */ |
| 2581 | break; |
| 2582 | } while_for_each_event_file(); |
| 2583 | |
| 2584 | __trace_remove_event_call(call); |
| 2585 | |
| 2586 | return 0; |
| 2587 | } |
| 2588 | |
| 2589 | /* Remove an event_call */ |
| 2590 | int trace_remove_event_call(struct trace_event_call *call) |
| 2591 | { |
| 2592 | int ret; |
| 2593 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2594 | lockdep_assert_held(&event_mutex); |
| 2595 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2596 | mutex_lock(&trace_types_lock); |
| 2597 | down_write(&trace_event_sem); |
| 2598 | ret = probe_remove_event_call(call); |
| 2599 | up_write(&trace_event_sem); |
| 2600 | mutex_unlock(&trace_types_lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2601 | |
| 2602 | return ret; |
| 2603 | } |
| 2604 | |
| 2605 | #define for_each_event(event, start, end) \ |
| 2606 | for (event = start; \ |
| 2607 | (unsigned long)event < (unsigned long)end; \ |
| 2608 | event++) |
| 2609 | |
| 2610 | #ifdef CONFIG_MODULES |
| 2611 | |
| 2612 | static void trace_module_add_events(struct module *mod) |
| 2613 | { |
| 2614 | struct trace_event_call **call, **start, **end; |
| 2615 | |
| 2616 | if (!mod->num_trace_events) |
| 2617 | return; |
| 2618 | |
| 2619 | /* Don't add infrastructure for mods without tracepoints */ |
| 2620 | if (trace_module_has_bad_taint(mod)) { |
| 2621 | pr_err("%s: module has bad taint, not creating trace events\n", |
| 2622 | mod->name); |
| 2623 | return; |
| 2624 | } |
| 2625 | |
| 2626 | start = mod->trace_events; |
| 2627 | end = mod->trace_events + mod->num_trace_events; |
| 2628 | |
| 2629 | for_each_event(call, start, end) { |
| 2630 | __register_event(*call, mod); |
| 2631 | __add_event_to_tracers(*call); |
| 2632 | } |
| 2633 | } |
| 2634 | |
| 2635 | static void trace_module_remove_events(struct module *mod) |
| 2636 | { |
| 2637 | struct trace_event_call *call, *p; |
| 2638 | |
| 2639 | down_write(&trace_event_sem); |
| 2640 | list_for_each_entry_safe(call, p, &ftrace_events, list) { |
| 2641 | if (call->mod == mod) |
| 2642 | __trace_remove_event_call(call); |
| 2643 | } |
| 2644 | up_write(&trace_event_sem); |
| 2645 | |
| 2646 | /* |
| 2647 | * It is safest to reset the ring buffer if the module being unloaded |
| 2648 | * registered any events that were used. The only worry is if |
| 2649 | * a new module gets loaded, and takes on the same id as the events |
| 2650 | * of this module. When printing out the buffer, traced events left |
| 2651 | * over from this module may be passed to the new module events and |
| 2652 | * unexpected results may occur. |
| 2653 | */ |
| 2654 | tracing_reset_all_online_cpus(); |
| 2655 | } |
| 2656 | |
| 2657 | static int trace_module_notify(struct notifier_block *self, |
| 2658 | unsigned long val, void *data) |
| 2659 | { |
| 2660 | struct module *mod = data; |
| 2661 | |
| 2662 | mutex_lock(&event_mutex); |
| 2663 | mutex_lock(&trace_types_lock); |
| 2664 | switch (val) { |
| 2665 | case MODULE_STATE_COMING: |
| 2666 | trace_module_add_events(mod); |
| 2667 | break; |
| 2668 | case MODULE_STATE_GOING: |
| 2669 | trace_module_remove_events(mod); |
| 2670 | break; |
| 2671 | } |
| 2672 | mutex_unlock(&trace_types_lock); |
| 2673 | mutex_unlock(&event_mutex); |
| 2674 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2675 | return NOTIFY_OK; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2676 | } |
| 2677 | |
| 2678 | static struct notifier_block trace_module_nb = { |
| 2679 | .notifier_call = trace_module_notify, |
| 2680 | .priority = 1, /* higher than trace.c module notify */ |
| 2681 | }; |
| 2682 | #endif /* CONFIG_MODULES */ |
| 2683 | |
| 2684 | /* Create a new event directory structure for a trace directory. */ |
| 2685 | static void |
| 2686 | __trace_add_event_dirs(struct trace_array *tr) |
| 2687 | { |
| 2688 | struct trace_event_call *call; |
| 2689 | int ret; |
| 2690 | |
| 2691 | list_for_each_entry(call, &ftrace_events, list) { |
| 2692 | ret = __trace_add_new_event(call, tr); |
| 2693 | if (ret < 0) |
| 2694 | pr_warn("Could not create directory for event %s\n", |
| 2695 | trace_event_name(call)); |
| 2696 | } |
| 2697 | } |
| 2698 | |
| 2699 | /* Returns any file that matches the system and event */ |
| 2700 | struct trace_event_file * |
| 2701 | __find_event_file(struct trace_array *tr, const char *system, const char *event) |
| 2702 | { |
| 2703 | struct trace_event_file *file; |
| 2704 | struct trace_event_call *call; |
| 2705 | const char *name; |
| 2706 | |
| 2707 | list_for_each_entry(file, &tr->events, list) { |
| 2708 | |
| 2709 | call = file->event_call; |
| 2710 | name = trace_event_name(call); |
| 2711 | |
| 2712 | if (!name || !call->class) |
| 2713 | continue; |
| 2714 | |
| 2715 | if (strcmp(event, name) == 0 && |
| 2716 | strcmp(system, call->class->system) == 0) |
| 2717 | return file; |
| 2718 | } |
| 2719 | return NULL; |
| 2720 | } |
| 2721 | |
| 2722 | /* Returns valid trace event files that match system and event */ |
| 2723 | struct trace_event_file * |
| 2724 | find_event_file(struct trace_array *tr, const char *system, const char *event) |
| 2725 | { |
| 2726 | struct trace_event_file *file; |
| 2727 | |
| 2728 | file = __find_event_file(tr, system, event); |
| 2729 | if (!file || !file->event_call->class->reg || |
| 2730 | file->event_call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) |
| 2731 | return NULL; |
| 2732 | |
| 2733 | return file; |
| 2734 | } |
| 2735 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2736 | /** |
| 2737 | * trace_get_event_file - Find and return a trace event file |
| 2738 | * @instance: The name of the trace instance containing the event |
| 2739 | * @system: The name of the system containing the event |
| 2740 | * @event: The name of the event |
| 2741 | * |
| 2742 | * Return a trace event file given the trace instance name, trace |
| 2743 | * system, and trace event name. If the instance name is NULL, it |
| 2744 | * refers to the top-level trace array. |
| 2745 | * |
| 2746 | * This function will look it up and return it if found, after calling |
| 2747 | * trace_array_get() to prevent the instance from going away, and |
| 2748 | * increment the event's module refcount to prevent it from being |
| 2749 | * removed. |
| 2750 | * |
| 2751 | * To release the file, call trace_put_event_file(), which will call |
| 2752 | * trace_array_put() and decrement the event's module refcount. |
| 2753 | * |
| 2754 | * Return: The trace event on success, ERR_PTR otherwise. |
| 2755 | */ |
| 2756 | struct trace_event_file *trace_get_event_file(const char *instance, |
| 2757 | const char *system, |
| 2758 | const char *event) |
| 2759 | { |
| 2760 | struct trace_array *tr = top_trace_array(); |
| 2761 | struct trace_event_file *file = NULL; |
| 2762 | int ret = -EINVAL; |
| 2763 | |
| 2764 | if (instance) { |
| 2765 | tr = trace_array_find_get(instance); |
| 2766 | if (!tr) |
| 2767 | return ERR_PTR(-ENOENT); |
| 2768 | } else { |
| 2769 | ret = trace_array_get(tr); |
| 2770 | if (ret) |
| 2771 | return ERR_PTR(ret); |
| 2772 | } |
| 2773 | |
| 2774 | mutex_lock(&event_mutex); |
| 2775 | |
| 2776 | file = find_event_file(tr, system, event); |
| 2777 | if (!file) { |
| 2778 | trace_array_put(tr); |
| 2779 | ret = -EINVAL; |
| 2780 | goto out; |
| 2781 | } |
| 2782 | |
| 2783 | /* Don't let event modules unload while in use */ |
| 2784 | ret = try_module_get(file->event_call->mod); |
| 2785 | if (!ret) { |
| 2786 | trace_array_put(tr); |
| 2787 | ret = -EBUSY; |
| 2788 | goto out; |
| 2789 | } |
| 2790 | |
| 2791 | ret = 0; |
| 2792 | out: |
| 2793 | mutex_unlock(&event_mutex); |
| 2794 | |
| 2795 | if (ret) |
| 2796 | file = ERR_PTR(ret); |
| 2797 | |
| 2798 | return file; |
| 2799 | } |
| 2800 | EXPORT_SYMBOL_GPL(trace_get_event_file); |
| 2801 | |
| 2802 | /** |
| 2803 | * trace_put_event_file - Release a file from trace_get_event_file() |
| 2804 | * @file: The trace event file |
| 2805 | * |
| 2806 | * If a file was retrieved using trace_get_event_file(), this should |
| 2807 | * be called when it's no longer needed. It will cancel the previous |
| 2808 | * trace_array_get() called by that function, and decrement the |
| 2809 | * event's module refcount. |
| 2810 | */ |
| 2811 | void trace_put_event_file(struct trace_event_file *file) |
| 2812 | { |
| 2813 | mutex_lock(&event_mutex); |
| 2814 | module_put(file->event_call->mod); |
| 2815 | mutex_unlock(&event_mutex); |
| 2816 | |
| 2817 | trace_array_put(file->tr); |
| 2818 | } |
| 2819 | EXPORT_SYMBOL_GPL(trace_put_event_file); |
| 2820 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2821 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 2822 | |
| 2823 | /* Avoid typos */ |
| 2824 | #define ENABLE_EVENT_STR "enable_event" |
| 2825 | #define DISABLE_EVENT_STR "disable_event" |
| 2826 | |
| 2827 | struct event_probe_data { |
| 2828 | struct trace_event_file *file; |
| 2829 | unsigned long count; |
| 2830 | int ref; |
| 2831 | bool enable; |
| 2832 | }; |
| 2833 | |
| 2834 | static void update_event_probe(struct event_probe_data *data) |
| 2835 | { |
| 2836 | if (data->enable) |
| 2837 | clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags); |
| 2838 | else |
| 2839 | set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags); |
| 2840 | } |
| 2841 | |
| 2842 | static void |
| 2843 | event_enable_probe(unsigned long ip, unsigned long parent_ip, |
| 2844 | struct trace_array *tr, struct ftrace_probe_ops *ops, |
| 2845 | void *data) |
| 2846 | { |
| 2847 | struct ftrace_func_mapper *mapper = data; |
| 2848 | struct event_probe_data *edata; |
| 2849 | void **pdata; |
| 2850 | |
| 2851 | pdata = ftrace_func_mapper_find_ip(mapper, ip); |
| 2852 | if (!pdata || !*pdata) |
| 2853 | return; |
| 2854 | |
| 2855 | edata = *pdata; |
| 2856 | update_event_probe(edata); |
| 2857 | } |
| 2858 | |
| 2859 | static void |
| 2860 | event_enable_count_probe(unsigned long ip, unsigned long parent_ip, |
| 2861 | struct trace_array *tr, struct ftrace_probe_ops *ops, |
| 2862 | void *data) |
| 2863 | { |
| 2864 | struct ftrace_func_mapper *mapper = data; |
| 2865 | struct event_probe_data *edata; |
| 2866 | void **pdata; |
| 2867 | |
| 2868 | pdata = ftrace_func_mapper_find_ip(mapper, ip); |
| 2869 | if (!pdata || !*pdata) |
| 2870 | return; |
| 2871 | |
| 2872 | edata = *pdata; |
| 2873 | |
| 2874 | if (!edata->count) |
| 2875 | return; |
| 2876 | |
| 2877 | /* Skip if the event is in a state we want to switch to */ |
| 2878 | if (edata->enable == !(edata->file->flags & EVENT_FILE_FL_SOFT_DISABLED)) |
| 2879 | return; |
| 2880 | |
| 2881 | if (edata->count != -1) |
| 2882 | (edata->count)--; |
| 2883 | |
| 2884 | update_event_probe(edata); |
| 2885 | } |
| 2886 | |
| 2887 | static int |
| 2888 | event_enable_print(struct seq_file *m, unsigned long ip, |
| 2889 | struct ftrace_probe_ops *ops, void *data) |
| 2890 | { |
| 2891 | struct ftrace_func_mapper *mapper = data; |
| 2892 | struct event_probe_data *edata; |
| 2893 | void **pdata; |
| 2894 | |
| 2895 | pdata = ftrace_func_mapper_find_ip(mapper, ip); |
| 2896 | |
| 2897 | if (WARN_ON_ONCE(!pdata || !*pdata)) |
| 2898 | return 0; |
| 2899 | |
| 2900 | edata = *pdata; |
| 2901 | |
| 2902 | seq_printf(m, "%ps:", (void *)ip); |
| 2903 | |
| 2904 | seq_printf(m, "%s:%s:%s", |
| 2905 | edata->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR, |
| 2906 | edata->file->event_call->class->system, |
| 2907 | trace_event_name(edata->file->event_call)); |
| 2908 | |
| 2909 | if (edata->count == -1) |
| 2910 | seq_puts(m, ":unlimited\n"); |
| 2911 | else |
| 2912 | seq_printf(m, ":count=%ld\n", edata->count); |
| 2913 | |
| 2914 | return 0; |
| 2915 | } |
| 2916 | |
| 2917 | static int |
| 2918 | event_enable_init(struct ftrace_probe_ops *ops, struct trace_array *tr, |
| 2919 | unsigned long ip, void *init_data, void **data) |
| 2920 | { |
| 2921 | struct ftrace_func_mapper *mapper = *data; |
| 2922 | struct event_probe_data *edata = init_data; |
| 2923 | int ret; |
| 2924 | |
| 2925 | if (!mapper) { |
| 2926 | mapper = allocate_ftrace_func_mapper(); |
| 2927 | if (!mapper) |
| 2928 | return -ENODEV; |
| 2929 | *data = mapper; |
| 2930 | } |
| 2931 | |
| 2932 | ret = ftrace_func_mapper_add_ip(mapper, ip, edata); |
| 2933 | if (ret < 0) |
| 2934 | return ret; |
| 2935 | |
| 2936 | edata->ref++; |
| 2937 | |
| 2938 | return 0; |
| 2939 | } |
| 2940 | |
| 2941 | static int free_probe_data(void *data) |
| 2942 | { |
| 2943 | struct event_probe_data *edata = data; |
| 2944 | |
| 2945 | edata->ref--; |
| 2946 | if (!edata->ref) { |
| 2947 | /* Remove the SOFT_MODE flag */ |
| 2948 | __ftrace_event_enable_disable(edata->file, 0, 1); |
| 2949 | module_put(edata->file->event_call->mod); |
| 2950 | kfree(edata); |
| 2951 | } |
| 2952 | return 0; |
| 2953 | } |
| 2954 | |
| 2955 | static void |
| 2956 | event_enable_free(struct ftrace_probe_ops *ops, struct trace_array *tr, |
| 2957 | unsigned long ip, void *data) |
| 2958 | { |
| 2959 | struct ftrace_func_mapper *mapper = data; |
| 2960 | struct event_probe_data *edata; |
| 2961 | |
| 2962 | if (!ip) { |
| 2963 | if (!mapper) |
| 2964 | return; |
| 2965 | free_ftrace_func_mapper(mapper, free_probe_data); |
| 2966 | return; |
| 2967 | } |
| 2968 | |
| 2969 | edata = ftrace_func_mapper_remove_ip(mapper, ip); |
| 2970 | |
| 2971 | if (WARN_ON_ONCE(!edata)) |
| 2972 | return; |
| 2973 | |
| 2974 | if (WARN_ON_ONCE(edata->ref <= 0)) |
| 2975 | return; |
| 2976 | |
| 2977 | free_probe_data(edata); |
| 2978 | } |
| 2979 | |
| 2980 | static struct ftrace_probe_ops event_enable_probe_ops = { |
| 2981 | .func = event_enable_probe, |
| 2982 | .print = event_enable_print, |
| 2983 | .init = event_enable_init, |
| 2984 | .free = event_enable_free, |
| 2985 | }; |
| 2986 | |
| 2987 | static struct ftrace_probe_ops event_enable_count_probe_ops = { |
| 2988 | .func = event_enable_count_probe, |
| 2989 | .print = event_enable_print, |
| 2990 | .init = event_enable_init, |
| 2991 | .free = event_enable_free, |
| 2992 | }; |
| 2993 | |
| 2994 | static struct ftrace_probe_ops event_disable_probe_ops = { |
| 2995 | .func = event_enable_probe, |
| 2996 | .print = event_enable_print, |
| 2997 | .init = event_enable_init, |
| 2998 | .free = event_enable_free, |
| 2999 | }; |
| 3000 | |
| 3001 | static struct ftrace_probe_ops event_disable_count_probe_ops = { |
| 3002 | .func = event_enable_count_probe, |
| 3003 | .print = event_enable_print, |
| 3004 | .init = event_enable_init, |
| 3005 | .free = event_enable_free, |
| 3006 | }; |
| 3007 | |
| 3008 | static int |
| 3009 | event_enable_func(struct trace_array *tr, struct ftrace_hash *hash, |
| 3010 | char *glob, char *cmd, char *param, int enabled) |
| 3011 | { |
| 3012 | struct trace_event_file *file; |
| 3013 | struct ftrace_probe_ops *ops; |
| 3014 | struct event_probe_data *data; |
| 3015 | const char *system; |
| 3016 | const char *event; |
| 3017 | char *number; |
| 3018 | bool enable; |
| 3019 | int ret; |
| 3020 | |
| 3021 | if (!tr) |
| 3022 | return -ENODEV; |
| 3023 | |
| 3024 | /* hash funcs only work with set_ftrace_filter */ |
| 3025 | if (!enabled || !param) |
| 3026 | return -EINVAL; |
| 3027 | |
| 3028 | system = strsep(¶m, ":"); |
| 3029 | if (!param) |
| 3030 | return -EINVAL; |
| 3031 | |
| 3032 | event = strsep(¶m, ":"); |
| 3033 | |
| 3034 | mutex_lock(&event_mutex); |
| 3035 | |
| 3036 | ret = -EINVAL; |
| 3037 | file = find_event_file(tr, system, event); |
| 3038 | if (!file) |
| 3039 | goto out; |
| 3040 | |
| 3041 | enable = strcmp(cmd, ENABLE_EVENT_STR) == 0; |
| 3042 | |
| 3043 | if (enable) |
| 3044 | ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops; |
| 3045 | else |
| 3046 | ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops; |
| 3047 | |
| 3048 | if (glob[0] == '!') { |
| 3049 | ret = unregister_ftrace_function_probe_func(glob+1, tr, ops); |
| 3050 | goto out; |
| 3051 | } |
| 3052 | |
| 3053 | ret = -ENOMEM; |
| 3054 | |
| 3055 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 3056 | if (!data) |
| 3057 | goto out; |
| 3058 | |
| 3059 | data->enable = enable; |
| 3060 | data->count = -1; |
| 3061 | data->file = file; |
| 3062 | |
| 3063 | if (!param) |
| 3064 | goto out_reg; |
| 3065 | |
| 3066 | number = strsep(¶m, ":"); |
| 3067 | |
| 3068 | ret = -EINVAL; |
| 3069 | if (!strlen(number)) |
| 3070 | goto out_free; |
| 3071 | |
| 3072 | /* |
| 3073 | * We use the callback data field (which is a pointer) |
| 3074 | * as our counter. |
| 3075 | */ |
| 3076 | ret = kstrtoul(number, 0, &data->count); |
| 3077 | if (ret) |
| 3078 | goto out_free; |
| 3079 | |
| 3080 | out_reg: |
| 3081 | /* Don't let event modules unload while probe registered */ |
| 3082 | ret = try_module_get(file->event_call->mod); |
| 3083 | if (!ret) { |
| 3084 | ret = -EBUSY; |
| 3085 | goto out_free; |
| 3086 | } |
| 3087 | |
| 3088 | ret = __ftrace_event_enable_disable(file, 1, 1); |
| 3089 | if (ret < 0) |
| 3090 | goto out_put; |
| 3091 | |
| 3092 | ret = register_ftrace_function_probe(glob, tr, ops, data); |
| 3093 | /* |
| 3094 | * The above returns on success the # of functions enabled, |
| 3095 | * but if it didn't find any functions it returns zero. |
| 3096 | * Consider no functions a failure too. |
| 3097 | */ |
| 3098 | if (!ret) { |
| 3099 | ret = -ENOENT; |
| 3100 | goto out_disable; |
| 3101 | } else if (ret < 0) |
| 3102 | goto out_disable; |
| 3103 | /* Just return zero, not the number of enabled functions */ |
| 3104 | ret = 0; |
| 3105 | out: |
| 3106 | mutex_unlock(&event_mutex); |
| 3107 | return ret; |
| 3108 | |
| 3109 | out_disable: |
| 3110 | __ftrace_event_enable_disable(file, 0, 1); |
| 3111 | out_put: |
| 3112 | module_put(file->event_call->mod); |
| 3113 | out_free: |
| 3114 | kfree(data); |
| 3115 | goto out; |
| 3116 | } |
| 3117 | |
| 3118 | static struct ftrace_func_command event_enable_cmd = { |
| 3119 | .name = ENABLE_EVENT_STR, |
| 3120 | .func = event_enable_func, |
| 3121 | }; |
| 3122 | |
| 3123 | static struct ftrace_func_command event_disable_cmd = { |
| 3124 | .name = DISABLE_EVENT_STR, |
| 3125 | .func = event_enable_func, |
| 3126 | }; |
| 3127 | |
| 3128 | static __init int register_event_cmds(void) |
| 3129 | { |
| 3130 | int ret; |
| 3131 | |
| 3132 | ret = register_ftrace_command(&event_enable_cmd); |
| 3133 | if (WARN_ON(ret < 0)) |
| 3134 | return ret; |
| 3135 | ret = register_ftrace_command(&event_disable_cmd); |
| 3136 | if (WARN_ON(ret < 0)) |
| 3137 | unregister_ftrace_command(&event_enable_cmd); |
| 3138 | return ret; |
| 3139 | } |
| 3140 | #else |
| 3141 | static inline int register_event_cmds(void) { return 0; } |
| 3142 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
| 3143 | |
| 3144 | /* |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3145 | * The top level array and trace arrays created by boot-time tracing |
| 3146 | * have already had its trace_event_file descriptors created in order |
| 3147 | * to allow for early events to be recorded. |
| 3148 | * This function is called after the tracefs has been initialized, |
| 3149 | * and we now have to create the files associated to the events. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3150 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3151 | static void __trace_early_add_event_dirs(struct trace_array *tr) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3152 | { |
| 3153 | struct trace_event_file *file; |
| 3154 | int ret; |
| 3155 | |
| 3156 | |
| 3157 | list_for_each_entry(file, &tr->events, list) { |
| 3158 | ret = event_create_dir(tr->event_dir, file); |
| 3159 | if (ret < 0) |
| 3160 | pr_warn("Could not create directory for event %s\n", |
| 3161 | trace_event_name(file->event_call)); |
| 3162 | } |
| 3163 | } |
| 3164 | |
| 3165 | /* |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3166 | * For early boot up, the top trace array and the trace arrays created |
| 3167 | * by boot-time tracing require to have a list of events that can be |
| 3168 | * enabled. This must be done before the filesystem is set up in order |
| 3169 | * to allow events to be traced early. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3170 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3171 | void __trace_early_add_events(struct trace_array *tr) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3172 | { |
| 3173 | struct trace_event_call *call; |
| 3174 | int ret; |
| 3175 | |
| 3176 | list_for_each_entry(call, &ftrace_events, list) { |
| 3177 | /* Early boot up should not have any modules loaded */ |
| 3178 | if (WARN_ON_ONCE(call->mod)) |
| 3179 | continue; |
| 3180 | |
| 3181 | ret = __trace_early_add_new_event(call, tr); |
| 3182 | if (ret < 0) |
| 3183 | pr_warn("Could not create early event %s\n", |
| 3184 | trace_event_name(call)); |
| 3185 | } |
| 3186 | } |
| 3187 | |
| 3188 | /* Remove the event directory structure for a trace directory. */ |
| 3189 | static void |
| 3190 | __trace_remove_event_dirs(struct trace_array *tr) |
| 3191 | { |
| 3192 | struct trace_event_file *file, *next; |
| 3193 | |
| 3194 | list_for_each_entry_safe(file, next, &tr->events, list) |
| 3195 | remove_event_file_dir(file); |
| 3196 | } |
| 3197 | |
| 3198 | static void __add_event_to_tracers(struct trace_event_call *call) |
| 3199 | { |
| 3200 | struct trace_array *tr; |
| 3201 | |
| 3202 | list_for_each_entry(tr, &ftrace_trace_arrays, list) |
| 3203 | __trace_add_new_event(call, tr); |
| 3204 | } |
| 3205 | |
| 3206 | extern struct trace_event_call *__start_ftrace_events[]; |
| 3207 | extern struct trace_event_call *__stop_ftrace_events[]; |
| 3208 | |
| 3209 | static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata; |
| 3210 | |
| 3211 | static __init int setup_trace_event(char *str) |
| 3212 | { |
| 3213 | strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE); |
| 3214 | ring_buffer_expanded = true; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3215 | disable_tracing_selftest("running event tracing"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3216 | |
| 3217 | return 1; |
| 3218 | } |
| 3219 | __setup("trace_event=", setup_trace_event); |
| 3220 | |
| 3221 | /* Expects to have event_mutex held when called */ |
| 3222 | static int |
| 3223 | create_event_toplevel_files(struct dentry *parent, struct trace_array *tr) |
| 3224 | { |
| 3225 | struct dentry *d_events; |
| 3226 | struct dentry *entry; |
| 3227 | |
| 3228 | entry = tracefs_create_file("set_event", 0644, parent, |
| 3229 | tr, &ftrace_set_event_fops); |
| 3230 | if (!entry) { |
| 3231 | pr_warn("Could not create tracefs 'set_event' entry\n"); |
| 3232 | return -ENOMEM; |
| 3233 | } |
| 3234 | |
| 3235 | d_events = tracefs_create_dir("events", parent); |
| 3236 | if (!d_events) { |
| 3237 | pr_warn("Could not create tracefs 'events' directory\n"); |
| 3238 | return -ENOMEM; |
| 3239 | } |
| 3240 | |
| 3241 | entry = trace_create_file("enable", 0644, d_events, |
| 3242 | tr, &ftrace_tr_enable_fops); |
| 3243 | if (!entry) { |
| 3244 | pr_warn("Could not create tracefs 'enable' entry\n"); |
| 3245 | return -ENOMEM; |
| 3246 | } |
| 3247 | |
| 3248 | /* There are not as crucial, just warn if they are not created */ |
| 3249 | |
| 3250 | entry = tracefs_create_file("set_event_pid", 0644, parent, |
| 3251 | tr, &ftrace_set_event_pid_fops); |
| 3252 | if (!entry) |
| 3253 | pr_warn("Could not create tracefs 'set_event_pid' entry\n"); |
| 3254 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3255 | entry = tracefs_create_file("set_event_notrace_pid", 0644, parent, |
| 3256 | tr, &ftrace_set_event_notrace_pid_fops); |
| 3257 | if (!entry) |
| 3258 | pr_warn("Could not create tracefs 'set_event_notrace_pid' entry\n"); |
| 3259 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3260 | /* ring buffer internal formats */ |
| 3261 | entry = trace_create_file("header_page", 0444, d_events, |
| 3262 | ring_buffer_print_page_header, |
| 3263 | &ftrace_show_header_fops); |
| 3264 | if (!entry) |
| 3265 | pr_warn("Could not create tracefs 'header_page' entry\n"); |
| 3266 | |
| 3267 | entry = trace_create_file("header_event", 0444, d_events, |
| 3268 | ring_buffer_print_entry_header, |
| 3269 | &ftrace_show_header_fops); |
| 3270 | if (!entry) |
| 3271 | pr_warn("Could not create tracefs 'header_event' entry\n"); |
| 3272 | |
| 3273 | tr->event_dir = d_events; |
| 3274 | |
| 3275 | return 0; |
| 3276 | } |
| 3277 | |
| 3278 | /** |
| 3279 | * event_trace_add_tracer - add a instance of a trace_array to events |
| 3280 | * @parent: The parent dentry to place the files/directories for events in |
| 3281 | * @tr: The trace array associated with these events |
| 3282 | * |
| 3283 | * When a new instance is created, it needs to set up its events |
| 3284 | * directory, as well as other files associated with events. It also |
| 3285 | * creates the event hierachry in the @parent/events directory. |
| 3286 | * |
| 3287 | * Returns 0 on success. |
| 3288 | * |
| 3289 | * Must be called with event_mutex held. |
| 3290 | */ |
| 3291 | int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr) |
| 3292 | { |
| 3293 | int ret; |
| 3294 | |
| 3295 | lockdep_assert_held(&event_mutex); |
| 3296 | |
| 3297 | ret = create_event_toplevel_files(parent, tr); |
| 3298 | if (ret) |
| 3299 | goto out; |
| 3300 | |
| 3301 | down_write(&trace_event_sem); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3302 | /* If tr already has the event list, it is initialized in early boot. */ |
| 3303 | if (unlikely(!list_empty(&tr->events))) |
| 3304 | __trace_early_add_event_dirs(tr); |
| 3305 | else |
| 3306 | __trace_add_event_dirs(tr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3307 | up_write(&trace_event_sem); |
| 3308 | |
| 3309 | out: |
| 3310 | return ret; |
| 3311 | } |
| 3312 | |
| 3313 | /* |
| 3314 | * The top trace array already had its file descriptors created. |
| 3315 | * Now the files themselves need to be created. |
| 3316 | */ |
| 3317 | static __init int |
| 3318 | early_event_add_tracer(struct dentry *parent, struct trace_array *tr) |
| 3319 | { |
| 3320 | int ret; |
| 3321 | |
| 3322 | mutex_lock(&event_mutex); |
| 3323 | |
| 3324 | ret = create_event_toplevel_files(parent, tr); |
| 3325 | if (ret) |
| 3326 | goto out_unlock; |
| 3327 | |
| 3328 | down_write(&trace_event_sem); |
| 3329 | __trace_early_add_event_dirs(tr); |
| 3330 | up_write(&trace_event_sem); |
| 3331 | |
| 3332 | out_unlock: |
| 3333 | mutex_unlock(&event_mutex); |
| 3334 | |
| 3335 | return ret; |
| 3336 | } |
| 3337 | |
| 3338 | /* Must be called with event_mutex held */ |
| 3339 | int event_trace_del_tracer(struct trace_array *tr) |
| 3340 | { |
| 3341 | lockdep_assert_held(&event_mutex); |
| 3342 | |
| 3343 | /* Disable any event triggers and associated soft-disabled events */ |
| 3344 | clear_event_triggers(tr); |
| 3345 | |
| 3346 | /* Clear the pid list */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3347 | __ftrace_clear_event_pids(tr, TRACE_PIDS | TRACE_NO_PIDS); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3348 | |
| 3349 | /* Disable any running events */ |
| 3350 | __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0); |
| 3351 | |
| 3352 | /* Make sure no more events are being executed */ |
| 3353 | tracepoint_synchronize_unregister(); |
| 3354 | |
| 3355 | down_write(&trace_event_sem); |
| 3356 | __trace_remove_event_dirs(tr); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3357 | tracefs_remove(tr->event_dir); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3358 | up_write(&trace_event_sem); |
| 3359 | |
| 3360 | tr->event_dir = NULL; |
| 3361 | |
| 3362 | return 0; |
| 3363 | } |
| 3364 | |
| 3365 | static __init int event_trace_memsetup(void) |
| 3366 | { |
| 3367 | field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC); |
| 3368 | file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC); |
| 3369 | return 0; |
| 3370 | } |
| 3371 | |
| 3372 | static __init void |
| 3373 | early_enable_events(struct trace_array *tr, bool disable_first) |
| 3374 | { |
| 3375 | char *buf = bootup_event_buf; |
| 3376 | char *token; |
| 3377 | int ret; |
| 3378 | |
| 3379 | while (true) { |
| 3380 | token = strsep(&buf, ","); |
| 3381 | |
| 3382 | if (!token) |
| 3383 | break; |
| 3384 | |
| 3385 | if (*token) { |
| 3386 | /* Restarting syscalls requires that we stop them first */ |
| 3387 | if (disable_first) |
| 3388 | ftrace_set_clr_event(tr, token, 0); |
| 3389 | |
| 3390 | ret = ftrace_set_clr_event(tr, token, 1); |
| 3391 | if (ret) |
| 3392 | pr_warn("Failed to enable trace event: %s\n", token); |
| 3393 | } |
| 3394 | |
| 3395 | /* Put back the comma to allow this to be called again */ |
| 3396 | if (buf) |
| 3397 | *(buf - 1) = ','; |
| 3398 | } |
| 3399 | } |
| 3400 | |
| 3401 | static __init int event_trace_enable(void) |
| 3402 | { |
| 3403 | struct trace_array *tr = top_trace_array(); |
| 3404 | struct trace_event_call **iter, *call; |
| 3405 | int ret; |
| 3406 | |
| 3407 | if (!tr) |
| 3408 | return -ENODEV; |
| 3409 | |
| 3410 | for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) { |
| 3411 | |
| 3412 | call = *iter; |
| 3413 | ret = event_init(call); |
| 3414 | if (!ret) |
| 3415 | list_add(&call->list, &ftrace_events); |
| 3416 | } |
| 3417 | |
| 3418 | /* |
| 3419 | * We need the top trace array to have a working set of trace |
| 3420 | * points at early init, before the debug files and directories |
| 3421 | * are created. Create the file entries now, and attach them |
| 3422 | * to the actual file dentries later. |
| 3423 | */ |
| 3424 | __trace_early_add_events(tr); |
| 3425 | |
| 3426 | early_enable_events(tr, false); |
| 3427 | |
| 3428 | trace_printk_start_comm(); |
| 3429 | |
| 3430 | register_event_cmds(); |
| 3431 | |
| 3432 | register_trigger_cmds(); |
| 3433 | |
| 3434 | return 0; |
| 3435 | } |
| 3436 | |
| 3437 | /* |
| 3438 | * event_trace_enable() is called from trace_event_init() first to |
| 3439 | * initialize events and perhaps start any events that are on the |
| 3440 | * command line. Unfortunately, there are some events that will not |
| 3441 | * start this early, like the system call tracepoints that need |
| 3442 | * to set the TIF_SYSCALL_TRACEPOINT flag of pid 1. But event_trace_enable() |
| 3443 | * is called before pid 1 starts, and this flag is never set, making |
| 3444 | * the syscall tracepoint never get reached, but the event is enabled |
| 3445 | * regardless (and not doing anything). |
| 3446 | */ |
| 3447 | static __init int event_trace_enable_again(void) |
| 3448 | { |
| 3449 | struct trace_array *tr; |
| 3450 | |
| 3451 | tr = top_trace_array(); |
| 3452 | if (!tr) |
| 3453 | return -ENODEV; |
| 3454 | |
| 3455 | early_enable_events(tr, true); |
| 3456 | |
| 3457 | return 0; |
| 3458 | } |
| 3459 | |
| 3460 | early_initcall(event_trace_enable_again); |
| 3461 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3462 | /* Init fields which doesn't related to the tracefs */ |
| 3463 | static __init int event_trace_init_fields(void) |
| 3464 | { |
| 3465 | if (trace_define_generic_fields()) |
| 3466 | pr_warn("tracing: Failed to allocated generic fields"); |
| 3467 | |
| 3468 | if (trace_define_common_fields()) |
| 3469 | pr_warn("tracing: Failed to allocate common fields"); |
| 3470 | |
| 3471 | return 0; |
| 3472 | } |
| 3473 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3474 | __init int event_trace_init(void) |
| 3475 | { |
| 3476 | struct trace_array *tr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3477 | struct dentry *entry; |
| 3478 | int ret; |
| 3479 | |
| 3480 | tr = top_trace_array(); |
| 3481 | if (!tr) |
| 3482 | return -ENODEV; |
| 3483 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3484 | entry = tracefs_create_file("available_events", 0444, NULL, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3485 | tr, &ftrace_avail_fops); |
| 3486 | if (!entry) |
| 3487 | pr_warn("Could not create tracefs 'available_events' entry\n"); |
| 3488 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3489 | ret = early_event_add_tracer(NULL, tr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3490 | if (ret) |
| 3491 | return ret; |
| 3492 | |
| 3493 | #ifdef CONFIG_MODULES |
| 3494 | ret = register_module_notifier(&trace_module_nb); |
| 3495 | if (ret) |
| 3496 | pr_warn("Failed to register trace events module notifier\n"); |
| 3497 | #endif |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3498 | |
| 3499 | eventdir_initialized = true; |
| 3500 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3501 | return 0; |
| 3502 | } |
| 3503 | |
| 3504 | void __init trace_event_init(void) |
| 3505 | { |
| 3506 | event_trace_memsetup(); |
| 3507 | init_ftrace_syscalls(); |
| 3508 | event_trace_enable(); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3509 | event_trace_init_fields(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3510 | } |
| 3511 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3512 | #ifdef CONFIG_EVENT_TRACE_STARTUP_TEST |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3513 | |
| 3514 | static DEFINE_SPINLOCK(test_spinlock); |
| 3515 | static DEFINE_SPINLOCK(test_spinlock_irq); |
| 3516 | static DEFINE_MUTEX(test_mutex); |
| 3517 | |
| 3518 | static __init void test_work(struct work_struct *dummy) |
| 3519 | { |
| 3520 | spin_lock(&test_spinlock); |
| 3521 | spin_lock_irq(&test_spinlock_irq); |
| 3522 | udelay(1); |
| 3523 | spin_unlock_irq(&test_spinlock_irq); |
| 3524 | spin_unlock(&test_spinlock); |
| 3525 | |
| 3526 | mutex_lock(&test_mutex); |
| 3527 | msleep(1); |
| 3528 | mutex_unlock(&test_mutex); |
| 3529 | } |
| 3530 | |
| 3531 | static __init int event_test_thread(void *unused) |
| 3532 | { |
| 3533 | void *test_malloc; |
| 3534 | |
| 3535 | test_malloc = kmalloc(1234, GFP_KERNEL); |
| 3536 | if (!test_malloc) |
| 3537 | pr_info("failed to kmalloc\n"); |
| 3538 | |
| 3539 | schedule_on_each_cpu(test_work); |
| 3540 | |
| 3541 | kfree(test_malloc); |
| 3542 | |
| 3543 | set_current_state(TASK_INTERRUPTIBLE); |
| 3544 | while (!kthread_should_stop()) { |
| 3545 | schedule(); |
| 3546 | set_current_state(TASK_INTERRUPTIBLE); |
| 3547 | } |
| 3548 | __set_current_state(TASK_RUNNING); |
| 3549 | |
| 3550 | return 0; |
| 3551 | } |
| 3552 | |
| 3553 | /* |
| 3554 | * Do various things that may trigger events. |
| 3555 | */ |
| 3556 | static __init void event_test_stuff(void) |
| 3557 | { |
| 3558 | struct task_struct *test_thread; |
| 3559 | |
| 3560 | test_thread = kthread_run(event_test_thread, NULL, "test-events"); |
| 3561 | msleep(1); |
| 3562 | kthread_stop(test_thread); |
| 3563 | } |
| 3564 | |
| 3565 | /* |
| 3566 | * For every trace event defined, we will test each trace point separately, |
| 3567 | * and then by groups, and finally all trace points. |
| 3568 | */ |
| 3569 | static __init void event_trace_self_tests(void) |
| 3570 | { |
| 3571 | struct trace_subsystem_dir *dir; |
| 3572 | struct trace_event_file *file; |
| 3573 | struct trace_event_call *call; |
| 3574 | struct event_subsystem *system; |
| 3575 | struct trace_array *tr; |
| 3576 | int ret; |
| 3577 | |
| 3578 | tr = top_trace_array(); |
| 3579 | if (!tr) |
| 3580 | return; |
| 3581 | |
| 3582 | pr_info("Running tests on trace events:\n"); |
| 3583 | |
| 3584 | list_for_each_entry(file, &tr->events, list) { |
| 3585 | |
| 3586 | call = file->event_call; |
| 3587 | |
| 3588 | /* Only test those that have a probe */ |
| 3589 | if (!call->class || !call->class->probe) |
| 3590 | continue; |
| 3591 | |
| 3592 | /* |
| 3593 | * Testing syscall events here is pretty useless, but |
| 3594 | * we still do it if configured. But this is time consuming. |
| 3595 | * What we really need is a user thread to perform the |
| 3596 | * syscalls as we test. |
| 3597 | */ |
| 3598 | #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS |
| 3599 | if (call->class->system && |
| 3600 | strcmp(call->class->system, "syscalls") == 0) |
| 3601 | continue; |
| 3602 | #endif |
| 3603 | |
| 3604 | pr_info("Testing event %s: ", trace_event_name(call)); |
| 3605 | |
| 3606 | /* |
| 3607 | * If an event is already enabled, someone is using |
| 3608 | * it and the self test should not be on. |
| 3609 | */ |
| 3610 | if (file->flags & EVENT_FILE_FL_ENABLED) { |
| 3611 | pr_warn("Enabled event during self test!\n"); |
| 3612 | WARN_ON_ONCE(1); |
| 3613 | continue; |
| 3614 | } |
| 3615 | |
| 3616 | ftrace_event_enable_disable(file, 1); |
| 3617 | event_test_stuff(); |
| 3618 | ftrace_event_enable_disable(file, 0); |
| 3619 | |
| 3620 | pr_cont("OK\n"); |
| 3621 | } |
| 3622 | |
| 3623 | /* Now test at the sub system level */ |
| 3624 | |
| 3625 | pr_info("Running tests on trace event systems:\n"); |
| 3626 | |
| 3627 | list_for_each_entry(dir, &tr->systems, list) { |
| 3628 | |
| 3629 | system = dir->subsystem; |
| 3630 | |
| 3631 | /* the ftrace system is special, skip it */ |
| 3632 | if (strcmp(system->name, "ftrace") == 0) |
| 3633 | continue; |
| 3634 | |
| 3635 | pr_info("Testing event system %s: ", system->name); |
| 3636 | |
| 3637 | ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1); |
| 3638 | if (WARN_ON_ONCE(ret)) { |
| 3639 | pr_warn("error enabling system %s\n", |
| 3640 | system->name); |
| 3641 | continue; |
| 3642 | } |
| 3643 | |
| 3644 | event_test_stuff(); |
| 3645 | |
| 3646 | ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0); |
| 3647 | if (WARN_ON_ONCE(ret)) { |
| 3648 | pr_warn("error disabling system %s\n", |
| 3649 | system->name); |
| 3650 | continue; |
| 3651 | } |
| 3652 | |
| 3653 | pr_cont("OK\n"); |
| 3654 | } |
| 3655 | |
| 3656 | /* Test with all events enabled */ |
| 3657 | |
| 3658 | pr_info("Running tests on all trace events:\n"); |
| 3659 | pr_info("Testing all events: "); |
| 3660 | |
| 3661 | ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1); |
| 3662 | if (WARN_ON_ONCE(ret)) { |
| 3663 | pr_warn("error enabling all events\n"); |
| 3664 | return; |
| 3665 | } |
| 3666 | |
| 3667 | event_test_stuff(); |
| 3668 | |
| 3669 | /* reset sysname */ |
| 3670 | ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0); |
| 3671 | if (WARN_ON_ONCE(ret)) { |
| 3672 | pr_warn("error disabling all events\n"); |
| 3673 | return; |
| 3674 | } |
| 3675 | |
| 3676 | pr_cont("OK\n"); |
| 3677 | } |
| 3678 | |
| 3679 | #ifdef CONFIG_FUNCTION_TRACER |
| 3680 | |
| 3681 | static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable); |
| 3682 | |
| 3683 | static struct trace_event_file event_trace_file __initdata; |
| 3684 | |
| 3685 | static void __init |
| 3686 | function_test_events_call(unsigned long ip, unsigned long parent_ip, |
| 3687 | struct ftrace_ops *op, struct pt_regs *pt_regs) |
| 3688 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3689 | struct trace_buffer *buffer; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3690 | struct ring_buffer_event *event; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3691 | struct ftrace_entry *entry; |
| 3692 | unsigned long flags; |
| 3693 | long disabled; |
| 3694 | int cpu; |
| 3695 | int pc; |
| 3696 | |
| 3697 | pc = preempt_count(); |
| 3698 | preempt_disable_notrace(); |
| 3699 | cpu = raw_smp_processor_id(); |
| 3700 | disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu)); |
| 3701 | |
| 3702 | if (disabled != 1) |
| 3703 | goto out; |
| 3704 | |
| 3705 | local_save_flags(flags); |
| 3706 | |
| 3707 | event = trace_event_buffer_lock_reserve(&buffer, &event_trace_file, |
| 3708 | TRACE_FN, sizeof(*entry), |
| 3709 | flags, pc); |
| 3710 | if (!event) |
| 3711 | goto out; |
| 3712 | entry = ring_buffer_event_data(event); |
| 3713 | entry->ip = ip; |
| 3714 | entry->parent_ip = parent_ip; |
| 3715 | |
| 3716 | event_trigger_unlock_commit(&event_trace_file, buffer, event, |
| 3717 | entry, flags, pc); |
| 3718 | out: |
| 3719 | atomic_dec(&per_cpu(ftrace_test_event_disable, cpu)); |
| 3720 | preempt_enable_notrace(); |
| 3721 | } |
| 3722 | |
| 3723 | static struct ftrace_ops trace_ops __initdata = |
| 3724 | { |
| 3725 | .func = function_test_events_call, |
| 3726 | .flags = FTRACE_OPS_FL_RECURSION_SAFE, |
| 3727 | }; |
| 3728 | |
| 3729 | static __init void event_trace_self_test_with_function(void) |
| 3730 | { |
| 3731 | int ret; |
| 3732 | |
| 3733 | event_trace_file.tr = top_trace_array(); |
| 3734 | if (WARN_ON(!event_trace_file.tr)) |
| 3735 | return; |
| 3736 | |
| 3737 | ret = register_ftrace_function(&trace_ops); |
| 3738 | if (WARN_ON(ret < 0)) { |
| 3739 | pr_info("Failed to enable function tracer for event tests\n"); |
| 3740 | return; |
| 3741 | } |
| 3742 | pr_info("Running tests again, along with the function tracer\n"); |
| 3743 | event_trace_self_tests(); |
| 3744 | unregister_ftrace_function(&trace_ops); |
| 3745 | } |
| 3746 | #else |
| 3747 | static __init void event_trace_self_test_with_function(void) |
| 3748 | { |
| 3749 | } |
| 3750 | #endif |
| 3751 | |
| 3752 | static __init int event_trace_self_tests_init(void) |
| 3753 | { |
| 3754 | if (!tracing_selftest_disabled) { |
| 3755 | event_trace_self_tests(); |
| 3756 | event_trace_self_test_with_function(); |
| 3757 | } |
| 3758 | |
| 3759 | return 0; |
| 3760 | } |
| 3761 | |
| 3762 | late_initcall(event_trace_self_tests_init); |
| 3763 | |
| 3764 | #endif |