| 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Infrastructure for profiling code inserted by 'gcc -pg'. |
| 4 | * |
| 5 | * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com> |
| 6 | * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com> |
| 7 | * |
| 8 | * Originally ported from the -rt patch by: |
| 9 | * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com> |
| 10 | * |
| 11 | * Based on code in the latency_tracer, that is: |
| 12 | * |
| 13 | * Copyright (C) 2004-2006 Ingo Molnar |
| 14 | * Copyright (C) 2004 Nadia Yvette Chambers |
| 15 | */ |
| 16 | |
| 17 | #include <linux/stop_machine.h> |
| 18 | #include <linux/clocksource.h> |
| 19 | #include <linux/sched/task.h> |
| 20 | #include <linux/kallsyms.h> |
| 21 | #include <linux/security.h> |
| 22 | #include <linux/seq_file.h> |
| 23 | #include <linux/tracefs.h> |
| 24 | #include <linux/hardirq.h> |
| 25 | #include <linux/kthread.h> |
| 26 | #include <linux/uaccess.h> |
| 27 | #include <linux/bsearch.h> |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/ftrace.h> |
| 30 | #include <linux/sysctl.h> |
| 31 | #include <linux/slab.h> |
| 32 | #include <linux/ctype.h> |
| 33 | #include <linux/sort.h> |
| 34 | #include <linux/list.h> |
| 35 | #include <linux/hash.h> |
| 36 | #include <linux/rcupdate.h> |
| 37 | #include <linux/kprobes.h> |
| 38 | |
| 39 | #include <trace/events/sched.h> |
| 40 | |
| 41 | #include <asm/sections.h> |
| 42 | #include <asm/setup.h> |
| 43 | |
| 44 | #include "ftrace_internal.h" |
| 45 | #include "trace_output.h" |
| 46 | #include "trace_stat.h" |
| 47 | |
| 48 | /* Flags that do not get reset */ |
| 49 | #define FTRACE_NOCLEAR_FLAGS (FTRACE_FL_DISABLED | FTRACE_FL_TOUCHED | \ |
| 50 | FTRACE_FL_MODIFIED) |
| 51 | |
| 52 | #define FTRACE_INVALID_FUNCTION "__ftrace_invalid_address__" |
| 53 | |
| 54 | #define FTRACE_WARN_ON(cond) \ |
| 55 | ({ \ |
| 56 | int ___r = cond; \ |
| 57 | if (WARN_ON(___r)) \ |
| 58 | ftrace_kill(); \ |
| 59 | ___r; \ |
| 60 | }) |
| 61 | |
| 62 | #define FTRACE_WARN_ON_ONCE(cond) \ |
| 63 | ({ \ |
| 64 | int ___r = cond; \ |
| 65 | if (WARN_ON_ONCE(___r)) \ |
| 66 | ftrace_kill(); \ |
| 67 | ___r; \ |
| 68 | }) |
| 69 | |
| 70 | /* hash bits for specific function selection */ |
| 71 | #define FTRACE_HASH_DEFAULT_BITS 10 |
| 72 | #define FTRACE_HASH_MAX_BITS 12 |
| 73 | |
| 74 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 75 | #define INIT_OPS_HASH(opsname) \ |
| 76 | .func_hash = &opsname.local_hash, \ |
| 77 | .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock), \ |
| 78 | .subop_list = LIST_HEAD_INIT(opsname.subop_list), |
| 79 | #else |
| 80 | #define INIT_OPS_HASH(opsname) |
| 81 | #endif |
| 82 | |
| 83 | enum { |
| 84 | FTRACE_MODIFY_ENABLE_FL = (1 << 0), |
| 85 | FTRACE_MODIFY_MAY_SLEEP_FL = (1 << 1), |
| 86 | }; |
| 87 | |
| 88 | struct ftrace_ops ftrace_list_end __read_mostly = { |
| 89 | .func = ftrace_stub, |
| 90 | .flags = FTRACE_OPS_FL_STUB, |
| 91 | INIT_OPS_HASH(ftrace_list_end) |
| 92 | }; |
| 93 | |
| 94 | /* ftrace_enabled is a method to turn ftrace on or off */ |
| 95 | int ftrace_enabled __read_mostly; |
| 96 | static int __maybe_unused last_ftrace_enabled; |
| 97 | |
| 98 | /* Current function tracing op */ |
| 99 | struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end; |
| 100 | /* What to set function_trace_op to */ |
| 101 | static struct ftrace_ops *set_function_trace_op; |
| 102 | |
| 103 | bool ftrace_pids_enabled(struct ftrace_ops *ops) |
| 104 | { |
| 105 | struct trace_array *tr; |
| 106 | |
| 107 | if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private) |
| 108 | return false; |
| 109 | |
| 110 | tr = ops->private; |
| 111 | |
| 112 | return tr->function_pids != NULL || tr->function_no_pids != NULL; |
| 113 | } |
| 114 | |
| 115 | static void ftrace_update_trampoline(struct ftrace_ops *ops); |
| 116 | |
| 117 | /* |
| 118 | * ftrace_disabled is set when an anomaly is discovered. |
| 119 | * ftrace_disabled is much stronger than ftrace_enabled. |
| 120 | */ |
| 121 | static int ftrace_disabled __read_mostly; |
| 122 | |
| 123 | DEFINE_MUTEX(ftrace_lock); |
| 124 | |
| 125 | struct ftrace_ops __rcu *ftrace_ops_list __read_mostly = (struct ftrace_ops __rcu *)&ftrace_list_end; |
| 126 | ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub; |
| 127 | struct ftrace_ops global_ops; |
| 128 | |
| 129 | /* Defined by vmlinux.lds.h see the comment above arch_ftrace_ops_list_func for details */ |
| 130 | void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip, |
| 131 | struct ftrace_ops *op, struct ftrace_regs *fregs); |
| 132 | |
| 133 | #ifdef CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS |
| 134 | /* |
| 135 | * Stub used to invoke the list ops without requiring a separate trampoline. |
| 136 | */ |
| 137 | const struct ftrace_ops ftrace_list_ops = { |
| 138 | .func = ftrace_ops_list_func, |
| 139 | .flags = FTRACE_OPS_FL_STUB, |
| 140 | }; |
| 141 | |
| 142 | static void ftrace_ops_nop_func(unsigned long ip, unsigned long parent_ip, |
| 143 | struct ftrace_ops *op, |
| 144 | struct ftrace_regs *fregs) |
| 145 | { |
| 146 | /* do nothing */ |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Stub used when a call site is disabled. May be called transiently by threads |
| 151 | * which have made it into ftrace_caller but haven't yet recovered the ops at |
| 152 | * the point the call site is disabled. |
| 153 | */ |
| 154 | const struct ftrace_ops ftrace_nop_ops = { |
| 155 | .func = ftrace_ops_nop_func, |
| 156 | .flags = FTRACE_OPS_FL_STUB, |
| 157 | }; |
| 158 | #endif |
| 159 | |
| 160 | static inline void ftrace_ops_init(struct ftrace_ops *ops) |
| 161 | { |
| 162 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 163 | if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) { |
| 164 | mutex_init(&ops->local_hash.regex_lock); |
| 165 | INIT_LIST_HEAD(list: &ops->subop_list); |
| 166 | ops->func_hash = &ops->local_hash; |
| 167 | ops->flags |= FTRACE_OPS_FL_INITIALIZED; |
| 168 | } |
| 169 | #endif |
| 170 | } |
| 171 | |
| 172 | /* Call this function for when a callback filters on set_ftrace_pid */ |
| 173 | static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip, |
| 174 | struct ftrace_ops *op, struct ftrace_regs *fregs) |
| 175 | { |
| 176 | struct trace_array *tr = op->private; |
| 177 | int pid; |
| 178 | |
| 179 | if (tr) { |
| 180 | pid = this_cpu_read(tr->array_buffer.data->ftrace_ignore_pid); |
| 181 | if (pid == FTRACE_PID_IGNORE) |
| 182 | return; |
| 183 | if (pid != FTRACE_PID_TRACE && |
| 184 | pid != current->pid) |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | op->saved_func(ip, parent_ip, op, fregs); |
| 189 | } |
| 190 | |
| 191 | void ftrace_sync_ipi(void *data) |
| 192 | { |
| 193 | /* Probably not needed, but do it anyway */ |
| 194 | smp_rmb(); |
| 195 | } |
| 196 | |
| 197 | static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops) |
| 198 | { |
| 199 | /* |
| 200 | * If this is a dynamic or RCU ops, or we force list func, |
| 201 | * then it needs to call the list anyway. |
| 202 | */ |
| 203 | if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_RCU) || |
| 204 | FTRACE_FORCE_LIST_FUNC) |
| 205 | return ftrace_ops_list_func; |
| 206 | |
| 207 | return ftrace_ops_get_func(ops); |
| 208 | } |
| 209 | |
| 210 | static void update_ftrace_function(void) |
| 211 | { |
| 212 | ftrace_func_t func; |
| 213 | |
| 214 | /* |
| 215 | * Prepare the ftrace_ops that the arch callback will use. |
| 216 | * If there's only one ftrace_ops registered, the ftrace_ops_list |
| 217 | * will point to the ops we want. |
| 218 | */ |
| 219 | set_function_trace_op = rcu_dereference_protected(ftrace_ops_list, |
| 220 | lockdep_is_held(&ftrace_lock)); |
| 221 | |
| 222 | /* If there's no ftrace_ops registered, just call the stub function */ |
| 223 | if (set_function_trace_op == &ftrace_list_end) { |
| 224 | func = ftrace_stub; |
| 225 | |
| 226 | /* |
| 227 | * If we are at the end of the list and this ops is |
| 228 | * recursion safe and not dynamic and the arch supports passing ops, |
| 229 | * then have the mcount trampoline call the function directly. |
| 230 | */ |
| 231 | } else if (rcu_dereference_protected(ftrace_ops_list->next, |
| 232 | lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) { |
| 233 | func = ftrace_ops_get_list_func(ops: ftrace_ops_list); |
| 234 | |
| 235 | } else { |
| 236 | /* Just use the default ftrace_ops */ |
| 237 | set_function_trace_op = &ftrace_list_end; |
| 238 | func = ftrace_ops_list_func; |
| 239 | } |
| 240 | |
| 241 | /* If there's no change, then do nothing more here */ |
| 242 | if (ftrace_trace_function == func) |
| 243 | return; |
| 244 | |
| 245 | /* |
| 246 | * If we are using the list function, it doesn't care |
| 247 | * about the function_trace_ops. |
| 248 | */ |
| 249 | if (func == ftrace_ops_list_func) { |
| 250 | ftrace_trace_function = func; |
| 251 | /* |
| 252 | * Don't even bother setting function_trace_ops, |
| 253 | * it would be racy to do so anyway. |
| 254 | */ |
| 255 | return; |
| 256 | } |
| 257 | |
| 258 | #ifndef CONFIG_DYNAMIC_FTRACE |
| 259 | /* |
| 260 | * For static tracing, we need to be a bit more careful. |
| 261 | * The function change takes affect immediately. Thus, |
| 262 | * we need to coordinate the setting of the function_trace_ops |
| 263 | * with the setting of the ftrace_trace_function. |
| 264 | * |
| 265 | * Set the function to the list ops, which will call the |
| 266 | * function we want, albeit indirectly, but it handles the |
| 267 | * ftrace_ops and doesn't depend on function_trace_op. |
| 268 | */ |
| 269 | ftrace_trace_function = ftrace_ops_list_func; |
| 270 | /* |
| 271 | * Make sure all CPUs see this. Yes this is slow, but static |
| 272 | * tracing is slow and nasty to have enabled. |
| 273 | */ |
| 274 | synchronize_rcu_tasks_rude(); |
| 275 | /* Now all cpus are using the list ops. */ |
| 276 | function_trace_op = set_function_trace_op; |
| 277 | /* Make sure the function_trace_op is visible on all CPUs */ |
| 278 | smp_wmb(); |
| 279 | /* Nasty way to force a rmb on all cpus */ |
| 280 | smp_call_function(ftrace_sync_ipi, NULL, 1); |
| 281 | /* OK, we are all set to update the ftrace_trace_function now! */ |
| 282 | #endif /* !CONFIG_DYNAMIC_FTRACE */ |
| 283 | |
| 284 | ftrace_trace_function = func; |
| 285 | } |
| 286 | |
| 287 | static void add_ftrace_ops(struct ftrace_ops __rcu **list, |
| 288 | struct ftrace_ops *ops) |
| 289 | { |
| 290 | rcu_assign_pointer(ops->next, *list); |
| 291 | |
| 292 | /* |
| 293 | * We are entering ops into the list but another |
| 294 | * CPU might be walking that list. We need to make sure |
| 295 | * the ops->next pointer is valid before another CPU sees |
| 296 | * the ops pointer included into the list. |
| 297 | */ |
| 298 | rcu_assign_pointer(*list, ops); |
| 299 | } |
| 300 | |
| 301 | static int remove_ftrace_ops(struct ftrace_ops __rcu **list, |
| 302 | struct ftrace_ops *ops) |
| 303 | { |
| 304 | struct ftrace_ops **p; |
| 305 | |
| 306 | /* |
| 307 | * If we are removing the last function, then simply point |
| 308 | * to the ftrace_stub. |
| 309 | */ |
| 310 | if (rcu_dereference_protected(*list, |
| 311 | lockdep_is_held(&ftrace_lock)) == ops && |
| 312 | rcu_dereference_protected(ops->next, |
| 313 | lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) { |
| 314 | rcu_assign_pointer(*list, &ftrace_list_end); |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | for (p = list; *p != &ftrace_list_end; p = &(*p)->next) |
| 319 | if (*p == ops) |
| 320 | break; |
| 321 | |
| 322 | if (*p != ops) |
| 323 | return -1; |
| 324 | |
| 325 | *p = (*p)->next; |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | static void ftrace_update_trampoline(struct ftrace_ops *ops); |
| 330 | |
| 331 | int __register_ftrace_function(struct ftrace_ops *ops) |
| 332 | { |
| 333 | if (ops->flags & FTRACE_OPS_FL_DELETED) |
| 334 | return -EINVAL; |
| 335 | |
| 336 | if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED)) |
| 337 | return -EBUSY; |
| 338 | |
| 339 | #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS |
| 340 | /* |
| 341 | * If the ftrace_ops specifies SAVE_REGS, then it only can be used |
| 342 | * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set. |
| 343 | * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant. |
| 344 | */ |
| 345 | if (ops->flags & FTRACE_OPS_FL_SAVE_REGS && |
| 346 | !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)) |
| 347 | return -EINVAL; |
| 348 | |
| 349 | if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED) |
| 350 | ops->flags |= FTRACE_OPS_FL_SAVE_REGS; |
| 351 | #endif |
| 352 | if (!ftrace_enabled && (ops->flags & FTRACE_OPS_FL_PERMANENT)) |
| 353 | return -EBUSY; |
| 354 | |
| 355 | if (!is_kernel_core_data(addr: (unsigned long)ops)) |
| 356 | ops->flags |= FTRACE_OPS_FL_DYNAMIC; |
| 357 | |
| 358 | add_ftrace_ops(list: &ftrace_ops_list, ops); |
| 359 | |
| 360 | /* Always save the function, and reset at unregistering */ |
| 361 | ops->saved_func = ops->func; |
| 362 | |
| 363 | if (ftrace_pids_enabled(ops)) |
| 364 | ops->func = ftrace_pid_func; |
| 365 | |
| 366 | ftrace_update_trampoline(ops); |
| 367 | |
| 368 | if (ftrace_enabled) |
| 369 | update_ftrace_function(); |
| 370 | |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | int __unregister_ftrace_function(struct ftrace_ops *ops) |
| 375 | { |
| 376 | int ret; |
| 377 | |
| 378 | if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED))) |
| 379 | return -EBUSY; |
| 380 | |
| 381 | ret = remove_ftrace_ops(list: &ftrace_ops_list, ops); |
| 382 | |
| 383 | if (ret < 0) |
| 384 | return ret; |
| 385 | |
| 386 | if (ftrace_enabled) |
| 387 | update_ftrace_function(); |
| 388 | |
| 389 | ops->func = ops->saved_func; |
| 390 | |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | static void ftrace_update_pid_func(void) |
| 395 | { |
| 396 | struct ftrace_ops *op; |
| 397 | |
| 398 | /* Only do something if we are tracing something */ |
| 399 | if (ftrace_trace_function == ftrace_stub) |
| 400 | return; |
| 401 | |
| 402 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 403 | if (op->flags & FTRACE_OPS_FL_PID) { |
| 404 | op->func = ftrace_pids_enabled(ops: op) ? |
| 405 | ftrace_pid_func : op->saved_func; |
| 406 | ftrace_update_trampoline(ops: op); |
| 407 | } |
| 408 | } while_for_each_ftrace_op(op); |
| 409 | |
| 410 | fgraph_update_pid_func(); |
| 411 | |
| 412 | update_ftrace_function(); |
| 413 | } |
| 414 | |
| 415 | #ifdef CONFIG_FUNCTION_PROFILER |
| 416 | struct ftrace_profile { |
| 417 | struct hlist_node node; |
| 418 | unsigned long ip; |
| 419 | unsigned long counter; |
| 420 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 421 | unsigned long long time; |
| 422 | unsigned long long time_squared; |
| 423 | #endif |
| 424 | }; |
| 425 | |
| 426 | struct ftrace_profile_page { |
| 427 | struct ftrace_profile_page *next; |
| 428 | unsigned long index; |
| 429 | struct ftrace_profile records[]; |
| 430 | }; |
| 431 | |
| 432 | struct ftrace_profile_stat { |
| 433 | atomic_t disabled; |
| 434 | struct hlist_head *hash; |
| 435 | struct ftrace_profile_page *pages; |
| 436 | struct ftrace_profile_page *start; |
| 437 | struct tracer_stat stat; |
| 438 | }; |
| 439 | |
| 440 | #define PROFILE_RECORDS_SIZE \ |
| 441 | (PAGE_SIZE - offsetof(struct ftrace_profile_page, records)) |
| 442 | |
| 443 | #define PROFILES_PER_PAGE \ |
| 444 | (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile)) |
| 445 | |
| 446 | static int ftrace_profile_enabled __read_mostly; |
| 447 | |
| 448 | /* ftrace_profile_lock - synchronize the enable and disable of the profiler */ |
| 449 | static DEFINE_MUTEX(ftrace_profile_lock); |
| 450 | |
| 451 | static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats); |
| 452 | |
| 453 | #define FTRACE_PROFILE_HASH_BITS 10 |
| 454 | #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS) |
| 455 | |
| 456 | static void * |
| 457 | function_stat_next(void *v, int idx) |
| 458 | { |
| 459 | struct ftrace_profile *rec = v; |
| 460 | struct ftrace_profile_page *pg; |
| 461 | |
| 462 | pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK); |
| 463 | |
| 464 | again: |
| 465 | if (idx != 0) |
| 466 | rec++; |
| 467 | |
| 468 | if ((void *)rec >= (void *)&pg->records[pg->index]) { |
| 469 | pg = pg->next; |
| 470 | if (!pg) |
| 471 | return NULL; |
| 472 | rec = &pg->records[0]; |
| 473 | if (!rec->counter) |
| 474 | goto again; |
| 475 | } |
| 476 | |
| 477 | return rec; |
| 478 | } |
| 479 | |
| 480 | static void *function_stat_start(struct tracer_stat *trace) |
| 481 | { |
| 482 | struct ftrace_profile_stat *stat = |
| 483 | container_of(trace, struct ftrace_profile_stat, stat); |
| 484 | |
| 485 | if (!stat || !stat->start) |
| 486 | return NULL; |
| 487 | |
| 488 | return function_stat_next(v: &stat->start->records[0], idx: 0); |
| 489 | } |
| 490 | |
| 491 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 492 | /* function graph compares on total time */ |
| 493 | static int function_stat_cmp(const void *p1, const void *p2) |
| 494 | { |
| 495 | const struct ftrace_profile *a = p1; |
| 496 | const struct ftrace_profile *b = p2; |
| 497 | |
| 498 | if (a->time < b->time) |
| 499 | return -1; |
| 500 | if (a->time > b->time) |
| 501 | return 1; |
| 502 | else |
| 503 | return 0; |
| 504 | } |
| 505 | #else |
| 506 | /* not function graph compares against hits */ |
| 507 | static int function_stat_cmp(const void *p1, const void *p2) |
| 508 | { |
| 509 | const struct ftrace_profile *a = p1; |
| 510 | const struct ftrace_profile *b = p2; |
| 511 | |
| 512 | if (a->counter < b->counter) |
| 513 | return -1; |
| 514 | if (a->counter > b->counter) |
| 515 | return 1; |
| 516 | else |
| 517 | return 0; |
| 518 | } |
| 519 | #endif |
| 520 | |
| 521 | static int (struct seq_file *m) |
| 522 | { |
| 523 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 524 | seq_puts(m, s: " Function " |
| 525 | "Hit Time Avg s^2\n" |
| 526 | " -------- " |
| 527 | "--- ---- --- ---\n" ); |
| 528 | #else |
| 529 | seq_puts(m, " Function Hit\n" |
| 530 | " -------- ---\n" ); |
| 531 | #endif |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | static int function_stat_show(struct seq_file *m, void *v) |
| 536 | { |
| 537 | struct trace_array *tr = trace_get_global_array(); |
| 538 | struct ftrace_profile *rec = v; |
| 539 | const char *refsymbol = NULL; |
| 540 | char str[KSYM_SYMBOL_LEN]; |
| 541 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 542 | static struct trace_seq s; |
| 543 | unsigned long long avg; |
| 544 | unsigned long long stddev; |
| 545 | unsigned long long stddev_denom; |
| 546 | #endif |
| 547 | guard(mutex)(T: &ftrace_profile_lock); |
| 548 | |
| 549 | /* we raced with function_profile_reset() */ |
| 550 | if (unlikely(rec->counter == 0)) |
| 551 | return -EBUSY; |
| 552 | |
| 553 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 554 | avg = div64_ul(rec->time, rec->counter); |
| 555 | if (tracing_thresh && (avg < tracing_thresh)) |
| 556 | return 0; |
| 557 | #endif |
| 558 | |
| 559 | if (tr->trace_flags & TRACE_ITER(PROF_TEXT_OFFSET)) { |
| 560 | unsigned long offset; |
| 561 | |
| 562 | if (core_kernel_text(addr: rec->ip)) { |
| 563 | refsymbol = "_text" ; |
| 564 | offset = rec->ip - (unsigned long)_text; |
| 565 | } else { |
| 566 | struct module *mod; |
| 567 | |
| 568 | guard(rcu)(); |
| 569 | mod = __module_text_address(addr: rec->ip); |
| 570 | if (mod) { |
| 571 | refsymbol = mod->name; |
| 572 | /* Calculate offset from module's text entry address. */ |
| 573 | offset = rec->ip - (unsigned long)mod->mem[MOD_TEXT].base; |
| 574 | } |
| 575 | } |
| 576 | if (refsymbol) |
| 577 | snprintf(buf: str, size: sizeof(str), fmt: " %s+%#lx" , refsymbol, offset); |
| 578 | } |
| 579 | if (!refsymbol) |
| 580 | kallsyms_lookup(addr: rec->ip, NULL, NULL, NULL, namebuf: str); |
| 581 | |
| 582 | seq_printf(m, fmt: " %-30.30s %10lu" , str, rec->counter); |
| 583 | |
| 584 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 585 | seq_puts(m, s: " " ); |
| 586 | |
| 587 | /* |
| 588 | * Variance formula: |
| 589 | * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2) |
| 590 | * Maybe Welford's method is better here? |
| 591 | * Divide only by 1000 for ns^2 -> us^2 conversion. |
| 592 | * trace_print_graph_duration will divide by 1000 again. |
| 593 | */ |
| 594 | stddev = 0; |
| 595 | stddev_denom = rec->counter * (rec->counter - 1) * 1000; |
| 596 | if (stddev_denom) { |
| 597 | stddev = rec->counter * rec->time_squared - |
| 598 | rec->time * rec->time; |
| 599 | stddev = div64_ul(stddev, stddev_denom); |
| 600 | } |
| 601 | |
| 602 | trace_seq_init(s: &s); |
| 603 | trace_print_graph_duration(duration: rec->time, s: &s); |
| 604 | trace_seq_puts(s: &s, str: " " ); |
| 605 | trace_print_graph_duration(duration: avg, s: &s); |
| 606 | trace_seq_puts(s: &s, str: " " ); |
| 607 | trace_print_graph_duration(duration: stddev, s: &s); |
| 608 | trace_print_seq(m, s: &s); |
| 609 | #endif |
| 610 | seq_putc(m, c: '\n'); |
| 611 | |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | static void ftrace_profile_reset(struct ftrace_profile_stat *stat) |
| 616 | { |
| 617 | struct ftrace_profile_page *pg; |
| 618 | |
| 619 | pg = stat->pages = stat->start; |
| 620 | |
| 621 | while (pg) { |
| 622 | memset(pg->records, 0, PROFILE_RECORDS_SIZE); |
| 623 | pg->index = 0; |
| 624 | pg = pg->next; |
| 625 | } |
| 626 | |
| 627 | memset(stat->hash, 0, |
| 628 | FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head)); |
| 629 | } |
| 630 | |
| 631 | static int ftrace_profile_pages_init(struct ftrace_profile_stat *stat) |
| 632 | { |
| 633 | struct ftrace_profile_page *pg; |
| 634 | int functions; |
| 635 | int pages; |
| 636 | int i; |
| 637 | |
| 638 | /* If we already allocated, do nothing */ |
| 639 | if (stat->pages) |
| 640 | return 0; |
| 641 | |
| 642 | stat->pages = (void *)get_zeroed_page(GFP_KERNEL); |
| 643 | if (!stat->pages) |
| 644 | return -ENOMEM; |
| 645 | |
| 646 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 647 | functions = ftrace_update_tot_cnt; |
| 648 | #else |
| 649 | /* |
| 650 | * We do not know the number of functions that exist because |
| 651 | * dynamic tracing is what counts them. With past experience |
| 652 | * we have around 20K functions. That should be more than enough. |
| 653 | * It is highly unlikely we will execute every function in |
| 654 | * the kernel. |
| 655 | */ |
| 656 | functions = 20000; |
| 657 | #endif |
| 658 | |
| 659 | pg = stat->start = stat->pages; |
| 660 | |
| 661 | pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE); |
| 662 | |
| 663 | for (i = 1; i < pages; i++) { |
| 664 | pg->next = (void *)get_zeroed_page(GFP_KERNEL); |
| 665 | if (!pg->next) |
| 666 | goto out_free; |
| 667 | pg = pg->next; |
| 668 | } |
| 669 | |
| 670 | return 0; |
| 671 | |
| 672 | out_free: |
| 673 | pg = stat->start; |
| 674 | while (pg) { |
| 675 | unsigned long tmp = (unsigned long)pg; |
| 676 | |
| 677 | pg = pg->next; |
| 678 | free_page(tmp); |
| 679 | } |
| 680 | |
| 681 | stat->pages = NULL; |
| 682 | stat->start = NULL; |
| 683 | |
| 684 | return -ENOMEM; |
| 685 | } |
| 686 | |
| 687 | static int ftrace_profile_init_cpu(int cpu) |
| 688 | { |
| 689 | struct ftrace_profile_stat *stat; |
| 690 | int size; |
| 691 | |
| 692 | stat = &per_cpu(ftrace_profile_stats, cpu); |
| 693 | |
| 694 | if (stat->hash) { |
| 695 | /* If the profile is already created, simply reset it */ |
| 696 | ftrace_profile_reset(stat); |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | /* |
| 701 | * We are profiling all functions, but usually only a few thousand |
| 702 | * functions are hit. We'll make a hash of 1024 items. |
| 703 | */ |
| 704 | size = FTRACE_PROFILE_HASH_SIZE; |
| 705 | |
| 706 | stat->hash = kcalloc(size, sizeof(struct hlist_head), GFP_KERNEL); |
| 707 | |
| 708 | if (!stat->hash) |
| 709 | return -ENOMEM; |
| 710 | |
| 711 | /* Preallocate the function profiling pages */ |
| 712 | if (ftrace_profile_pages_init(stat) < 0) { |
| 713 | kfree(objp: stat->hash); |
| 714 | stat->hash = NULL; |
| 715 | return -ENOMEM; |
| 716 | } |
| 717 | |
| 718 | return 0; |
| 719 | } |
| 720 | |
| 721 | static int ftrace_profile_init(void) |
| 722 | { |
| 723 | int cpu; |
| 724 | int ret = 0; |
| 725 | |
| 726 | for_each_possible_cpu(cpu) { |
| 727 | ret = ftrace_profile_init_cpu(cpu); |
| 728 | if (ret) |
| 729 | break; |
| 730 | } |
| 731 | |
| 732 | return ret; |
| 733 | } |
| 734 | |
| 735 | /* interrupts must be disabled */ |
| 736 | static struct ftrace_profile * |
| 737 | ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip) |
| 738 | { |
| 739 | struct ftrace_profile *rec; |
| 740 | struct hlist_head *hhd; |
| 741 | unsigned long key; |
| 742 | |
| 743 | key = hash_long(ip, FTRACE_PROFILE_HASH_BITS); |
| 744 | hhd = &stat->hash[key]; |
| 745 | |
| 746 | if (hlist_empty(h: hhd)) |
| 747 | return NULL; |
| 748 | |
| 749 | hlist_for_each_entry_rcu_notrace(rec, hhd, node) { |
| 750 | if (rec->ip == ip) |
| 751 | return rec; |
| 752 | } |
| 753 | |
| 754 | return NULL; |
| 755 | } |
| 756 | |
| 757 | static void ftrace_add_profile(struct ftrace_profile_stat *stat, |
| 758 | struct ftrace_profile *rec) |
| 759 | { |
| 760 | unsigned long key; |
| 761 | |
| 762 | key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS); |
| 763 | hlist_add_head_rcu(n: &rec->node, h: &stat->hash[key]); |
| 764 | } |
| 765 | |
| 766 | /* |
| 767 | * The memory is already allocated, this simply finds a new record to use. |
| 768 | */ |
| 769 | static struct ftrace_profile * |
| 770 | ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip) |
| 771 | { |
| 772 | struct ftrace_profile *rec = NULL; |
| 773 | |
| 774 | /* prevent recursion (from NMIs) */ |
| 775 | if (atomic_inc_return(v: &stat->disabled) != 1) |
| 776 | goto out; |
| 777 | |
| 778 | /* |
| 779 | * Try to find the function again since an NMI |
| 780 | * could have added it |
| 781 | */ |
| 782 | rec = ftrace_find_profiled_func(stat, ip); |
| 783 | if (rec) |
| 784 | goto out; |
| 785 | |
| 786 | if (stat->pages->index == PROFILES_PER_PAGE) { |
| 787 | if (!stat->pages->next) |
| 788 | goto out; |
| 789 | stat->pages = stat->pages->next; |
| 790 | } |
| 791 | |
| 792 | rec = &stat->pages->records[stat->pages->index++]; |
| 793 | rec->ip = ip; |
| 794 | ftrace_add_profile(stat, rec); |
| 795 | |
| 796 | out: |
| 797 | atomic_dec(v: &stat->disabled); |
| 798 | |
| 799 | return rec; |
| 800 | } |
| 801 | |
| 802 | static void |
| 803 | function_profile_call(unsigned long ip, unsigned long parent_ip, |
| 804 | struct ftrace_ops *ops, struct ftrace_regs *fregs) |
| 805 | { |
| 806 | struct ftrace_profile_stat *stat; |
| 807 | struct ftrace_profile *rec; |
| 808 | |
| 809 | if (!ftrace_profile_enabled) |
| 810 | return; |
| 811 | |
| 812 | guard(preempt_notrace)(); |
| 813 | |
| 814 | stat = this_cpu_ptr(&ftrace_profile_stats); |
| 815 | if (!stat->hash || !ftrace_profile_enabled) |
| 816 | return; |
| 817 | |
| 818 | rec = ftrace_find_profiled_func(stat, ip); |
| 819 | if (!rec) { |
| 820 | rec = ftrace_profile_alloc(stat, ip); |
| 821 | if (!rec) |
| 822 | return; |
| 823 | } |
| 824 | |
| 825 | rec->counter++; |
| 826 | } |
| 827 | |
| 828 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 829 | static bool fgraph_graph_time = true; |
| 830 | |
| 831 | void ftrace_graph_graph_time_control(bool enable) |
| 832 | { |
| 833 | fgraph_graph_time = enable; |
| 834 | } |
| 835 | |
| 836 | struct profile_fgraph_data { |
| 837 | unsigned long long calltime; |
| 838 | unsigned long long subtime; |
| 839 | unsigned long long sleeptime; |
| 840 | }; |
| 841 | |
| 842 | static int profile_graph_entry(struct ftrace_graph_ent *trace, |
| 843 | struct fgraph_ops *gops, |
| 844 | struct ftrace_regs *fregs) |
| 845 | { |
| 846 | struct profile_fgraph_data *profile_data; |
| 847 | |
| 848 | function_profile_call(ip: trace->func, parent_ip: 0, NULL, NULL); |
| 849 | |
| 850 | /* If function graph is shutting down, ret_stack can be NULL */ |
| 851 | if (!current->ret_stack) |
| 852 | return 0; |
| 853 | |
| 854 | profile_data = fgraph_reserve_data(idx: gops->idx, size_bytes: sizeof(*profile_data)); |
| 855 | if (!profile_data) |
| 856 | return 0; |
| 857 | |
| 858 | profile_data->subtime = 0; |
| 859 | profile_data->sleeptime = current->ftrace_sleeptime; |
| 860 | profile_data->calltime = trace_clock_local(); |
| 861 | |
| 862 | return 1; |
| 863 | } |
| 864 | |
| 865 | bool fprofile_no_sleep_time; |
| 866 | |
| 867 | static void profile_graph_return(struct ftrace_graph_ret *trace, |
| 868 | struct fgraph_ops *gops, |
| 869 | struct ftrace_regs *fregs) |
| 870 | { |
| 871 | struct profile_fgraph_data *profile_data; |
| 872 | struct ftrace_profile_stat *stat; |
| 873 | unsigned long long calltime; |
| 874 | unsigned long long rettime = trace_clock_local(); |
| 875 | struct ftrace_profile *rec; |
| 876 | int size; |
| 877 | |
| 878 | guard(preempt_notrace)(); |
| 879 | |
| 880 | stat = this_cpu_ptr(&ftrace_profile_stats); |
| 881 | if (!stat->hash || !ftrace_profile_enabled) |
| 882 | return; |
| 883 | |
| 884 | profile_data = fgraph_retrieve_data(idx: gops->idx, size_bytes: &size); |
| 885 | |
| 886 | /* If the calltime was zero'd ignore it */ |
| 887 | if (!profile_data || !profile_data->calltime) |
| 888 | return; |
| 889 | |
| 890 | calltime = rettime - profile_data->calltime; |
| 891 | |
| 892 | if (fprofile_no_sleep_time) { |
| 893 | if (current->ftrace_sleeptime) |
| 894 | calltime -= current->ftrace_sleeptime - profile_data->sleeptime; |
| 895 | } |
| 896 | |
| 897 | if (!fgraph_graph_time) { |
| 898 | struct profile_fgraph_data *parent_data; |
| 899 | |
| 900 | /* Append this call time to the parent time to subtract */ |
| 901 | parent_data = fgraph_retrieve_parent_data(idx: gops->idx, size_bytes: &size, depth: 1); |
| 902 | if (parent_data) |
| 903 | parent_data->subtime += calltime; |
| 904 | |
| 905 | if (profile_data->subtime && profile_data->subtime < calltime) |
| 906 | calltime -= profile_data->subtime; |
| 907 | else |
| 908 | calltime = 0; |
| 909 | } |
| 910 | |
| 911 | rec = ftrace_find_profiled_func(stat, ip: trace->func); |
| 912 | if (rec) { |
| 913 | rec->time += calltime; |
| 914 | rec->time_squared += calltime * calltime; |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | static struct fgraph_ops fprofiler_ops = { |
| 919 | .entryfunc = &profile_graph_entry, |
| 920 | .retfunc = &profile_graph_return, |
| 921 | }; |
| 922 | |
| 923 | static int register_ftrace_profiler(void) |
| 924 | { |
| 925 | ftrace_ops_set_global_filter(ops: &fprofiler_ops.ops); |
| 926 | return register_ftrace_graph(ops: &fprofiler_ops); |
| 927 | } |
| 928 | |
| 929 | static void unregister_ftrace_profiler(void) |
| 930 | { |
| 931 | unregister_ftrace_graph(ops: &fprofiler_ops); |
| 932 | } |
| 933 | #else |
| 934 | static struct ftrace_ops ftrace_profile_ops __read_mostly = { |
| 935 | .func = function_profile_call, |
| 936 | }; |
| 937 | |
| 938 | static int register_ftrace_profiler(void) |
| 939 | { |
| 940 | ftrace_ops_set_global_filter(&ftrace_profile_ops); |
| 941 | return register_ftrace_function(&ftrace_profile_ops); |
| 942 | } |
| 943 | |
| 944 | static void unregister_ftrace_profiler(void) |
| 945 | { |
| 946 | unregister_ftrace_function(&ftrace_profile_ops); |
| 947 | } |
| 948 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 949 | |
| 950 | static ssize_t |
| 951 | ftrace_profile_write(struct file *filp, const char __user *ubuf, |
| 952 | size_t cnt, loff_t *ppos) |
| 953 | { |
| 954 | unsigned long val; |
| 955 | int ret; |
| 956 | |
| 957 | ret = kstrtoul_from_user(s: ubuf, count: cnt, base: 10, res: &val); |
| 958 | if (ret) |
| 959 | return ret; |
| 960 | |
| 961 | val = !!val; |
| 962 | |
| 963 | guard(mutex)(T: &ftrace_profile_lock); |
| 964 | if (ftrace_profile_enabled ^ val) { |
| 965 | if (val) { |
| 966 | ret = ftrace_profile_init(); |
| 967 | if (ret < 0) |
| 968 | return ret; |
| 969 | |
| 970 | ret = register_ftrace_profiler(); |
| 971 | if (ret < 0) |
| 972 | return ret; |
| 973 | ftrace_profile_enabled = 1; |
| 974 | } else { |
| 975 | ftrace_profile_enabled = 0; |
| 976 | /* |
| 977 | * unregister_ftrace_profiler calls stop_machine |
| 978 | * so this acts like an synchronize_rcu. |
| 979 | */ |
| 980 | unregister_ftrace_profiler(); |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | *ppos += cnt; |
| 985 | |
| 986 | return cnt; |
| 987 | } |
| 988 | |
| 989 | static ssize_t |
| 990 | ftrace_profile_read(struct file *filp, char __user *ubuf, |
| 991 | size_t cnt, loff_t *ppos) |
| 992 | { |
| 993 | char buf[64]; /* big enough to hold a number */ |
| 994 | int r; |
| 995 | |
| 996 | r = sprintf(buf, fmt: "%u\n" , ftrace_profile_enabled); |
| 997 | return simple_read_from_buffer(to: ubuf, count: cnt, ppos, from: buf, available: r); |
| 998 | } |
| 999 | |
| 1000 | static const struct file_operations ftrace_profile_fops = { |
| 1001 | .open = tracing_open_generic, |
| 1002 | .read = ftrace_profile_read, |
| 1003 | .write = ftrace_profile_write, |
| 1004 | .llseek = default_llseek, |
| 1005 | }; |
| 1006 | |
| 1007 | /* used to initialize the real stat files */ |
| 1008 | static struct tracer_stat function_stats __initdata = { |
| 1009 | .name = "functions" , |
| 1010 | .stat_start = function_stat_start, |
| 1011 | .stat_next = function_stat_next, |
| 1012 | .stat_cmp = function_stat_cmp, |
| 1013 | .stat_headers = function_stat_headers, |
| 1014 | .stat_show = function_stat_show |
| 1015 | }; |
| 1016 | |
| 1017 | static __init void ftrace_profile_tracefs(struct dentry *d_tracer) |
| 1018 | { |
| 1019 | struct ftrace_profile_stat *stat; |
| 1020 | char *name; |
| 1021 | int ret; |
| 1022 | int cpu; |
| 1023 | |
| 1024 | for_each_possible_cpu(cpu) { |
| 1025 | stat = &per_cpu(ftrace_profile_stats, cpu); |
| 1026 | |
| 1027 | name = kasprintf(GFP_KERNEL, fmt: "function%d" , cpu); |
| 1028 | if (!name) { |
| 1029 | /* |
| 1030 | * The files created are permanent, if something happens |
| 1031 | * we still do not free memory. |
| 1032 | */ |
| 1033 | WARN(1, |
| 1034 | "Could not allocate stat file for cpu %d\n" , |
| 1035 | cpu); |
| 1036 | return; |
| 1037 | } |
| 1038 | stat->stat = function_stats; |
| 1039 | stat->stat.name = name; |
| 1040 | ret = register_stat_tracer(trace: &stat->stat); |
| 1041 | if (ret) { |
| 1042 | WARN(1, |
| 1043 | "Could not register function stat for cpu %d\n" , |
| 1044 | cpu); |
| 1045 | kfree(objp: name); |
| 1046 | return; |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | trace_create_file(name: "function_profile_enabled" , |
| 1051 | TRACE_MODE_WRITE, parent: d_tracer, NULL, |
| 1052 | fops: &ftrace_profile_fops); |
| 1053 | } |
| 1054 | |
| 1055 | #else /* CONFIG_FUNCTION_PROFILER */ |
| 1056 | static __init void ftrace_profile_tracefs(struct dentry *d_tracer) |
| 1057 | { |
| 1058 | } |
| 1059 | #endif /* CONFIG_FUNCTION_PROFILER */ |
| 1060 | |
| 1061 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 1062 | |
| 1063 | static struct ftrace_ops *removed_ops; |
| 1064 | |
| 1065 | /* |
| 1066 | * Set when doing a global update, like enabling all recs or disabling them. |
| 1067 | * It is not set when just updating a single ftrace_ops. |
| 1068 | */ |
| 1069 | static bool update_all_ops; |
| 1070 | |
| 1071 | struct ftrace_func_probe { |
| 1072 | struct ftrace_probe_ops *probe_ops; |
| 1073 | struct ftrace_ops ops; |
| 1074 | struct trace_array *tr; |
| 1075 | struct list_head list; |
| 1076 | void *data; |
| 1077 | int ref; |
| 1078 | }; |
| 1079 | |
| 1080 | /* |
| 1081 | * We make these constant because no one should touch them, |
| 1082 | * but they are used as the default "empty hash", to avoid allocating |
| 1083 | * it all the time. These are in a read only section such that if |
| 1084 | * anyone does try to modify it, it will cause an exception. |
| 1085 | */ |
| 1086 | static const struct hlist_head empty_buckets[1]; |
| 1087 | static const struct ftrace_hash empty_hash = { |
| 1088 | .buckets = (struct hlist_head *)empty_buckets, |
| 1089 | }; |
| 1090 | #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash) |
| 1091 | |
| 1092 | struct ftrace_ops global_ops = { |
| 1093 | .func = ftrace_stub, |
| 1094 | .local_hash.notrace_hash = EMPTY_HASH, |
| 1095 | .local_hash.filter_hash = EMPTY_HASH, |
| 1096 | INIT_OPS_HASH(global_ops) |
| 1097 | .flags = FTRACE_OPS_FL_INITIALIZED | |
| 1098 | FTRACE_OPS_FL_PID, |
| 1099 | }; |
| 1100 | |
| 1101 | /* |
| 1102 | * Used by the stack unwinder to know about dynamic ftrace trampolines. |
| 1103 | */ |
| 1104 | struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr) |
| 1105 | { |
| 1106 | struct ftrace_ops *op = NULL; |
| 1107 | |
| 1108 | /* |
| 1109 | * Some of the ops may be dynamically allocated, |
| 1110 | * they are freed after a synchronize_rcu(). |
| 1111 | */ |
| 1112 | preempt_disable_notrace(); |
| 1113 | |
| 1114 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 1115 | /* |
| 1116 | * This is to check for dynamically allocated trampolines. |
| 1117 | * Trampolines that are in kernel text will have |
| 1118 | * core_kernel_text() return true. |
| 1119 | */ |
| 1120 | if (op->trampoline && op->trampoline_size) |
| 1121 | if (addr >= op->trampoline && |
| 1122 | addr < op->trampoline + op->trampoline_size) { |
| 1123 | preempt_enable_notrace(); |
| 1124 | return op; |
| 1125 | } |
| 1126 | } while_for_each_ftrace_op(op); |
| 1127 | preempt_enable_notrace(); |
| 1128 | |
| 1129 | return NULL; |
| 1130 | } |
| 1131 | |
| 1132 | /* |
| 1133 | * This is used by __kernel_text_address() to return true if the |
| 1134 | * address is on a dynamically allocated trampoline that would |
| 1135 | * not return true for either core_kernel_text() or |
| 1136 | * is_module_text_address(). |
| 1137 | */ |
| 1138 | bool is_ftrace_trampoline(unsigned long addr) |
| 1139 | { |
| 1140 | return ftrace_ops_trampoline(addr) != NULL; |
| 1141 | } |
| 1142 | |
| 1143 | struct ftrace_page { |
| 1144 | struct ftrace_page *next; |
| 1145 | struct dyn_ftrace *records; |
| 1146 | int index; |
| 1147 | int order; |
| 1148 | }; |
| 1149 | |
| 1150 | #define ENTRY_SIZE sizeof(struct dyn_ftrace) |
| 1151 | |
| 1152 | static struct ftrace_page *ftrace_pages_start; |
| 1153 | static struct ftrace_page *ftrace_pages; |
| 1154 | |
| 1155 | static __always_inline unsigned long |
| 1156 | ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip) |
| 1157 | { |
| 1158 | if (hash->size_bits > 0) |
| 1159 | return hash_long(ip, hash->size_bits); |
| 1160 | |
| 1161 | return 0; |
| 1162 | } |
| 1163 | |
| 1164 | /* Only use this function if ftrace_hash_empty() has already been tested */ |
| 1165 | static __always_inline struct ftrace_func_entry * |
| 1166 | __ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip) |
| 1167 | { |
| 1168 | unsigned long key; |
| 1169 | struct ftrace_func_entry *entry; |
| 1170 | struct hlist_head *hhd; |
| 1171 | |
| 1172 | key = ftrace_hash_key(hash, ip); |
| 1173 | hhd = &hash->buckets[key]; |
| 1174 | |
| 1175 | hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) { |
| 1176 | if (entry->ip == ip) |
| 1177 | return entry; |
| 1178 | } |
| 1179 | return NULL; |
| 1180 | } |
| 1181 | |
| 1182 | /** |
| 1183 | * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash |
| 1184 | * @hash: The hash to look at |
| 1185 | * @ip: The instruction pointer to test |
| 1186 | * |
| 1187 | * Search a given @hash to see if a given instruction pointer (@ip) |
| 1188 | * exists in it. |
| 1189 | * |
| 1190 | * Returns: the entry that holds the @ip if found. NULL otherwise. |
| 1191 | */ |
| 1192 | struct ftrace_func_entry * |
| 1193 | ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip) |
| 1194 | { |
| 1195 | if (ftrace_hash_empty(hash)) |
| 1196 | return NULL; |
| 1197 | |
| 1198 | return __ftrace_lookup_ip(hash, ip); |
| 1199 | } |
| 1200 | |
| 1201 | static void __add_hash_entry(struct ftrace_hash *hash, |
| 1202 | struct ftrace_func_entry *entry) |
| 1203 | { |
| 1204 | struct hlist_head *hhd; |
| 1205 | unsigned long key; |
| 1206 | |
| 1207 | key = ftrace_hash_key(hash, ip: entry->ip); |
| 1208 | hhd = &hash->buckets[key]; |
| 1209 | hlist_add_head(n: &entry->hlist, h: hhd); |
| 1210 | hash->count++; |
| 1211 | } |
| 1212 | |
| 1213 | static struct ftrace_func_entry * |
| 1214 | add_hash_entry(struct ftrace_hash *hash, unsigned long ip) |
| 1215 | { |
| 1216 | struct ftrace_func_entry *entry; |
| 1217 | |
| 1218 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
| 1219 | if (!entry) |
| 1220 | return NULL; |
| 1221 | |
| 1222 | entry->ip = ip; |
| 1223 | __add_hash_entry(hash, entry); |
| 1224 | |
| 1225 | return entry; |
| 1226 | } |
| 1227 | |
| 1228 | static void |
| 1229 | free_hash_entry(struct ftrace_hash *hash, |
| 1230 | struct ftrace_func_entry *entry) |
| 1231 | { |
| 1232 | hlist_del(n: &entry->hlist); |
| 1233 | kfree(objp: entry); |
| 1234 | hash->count--; |
| 1235 | } |
| 1236 | |
| 1237 | static void |
| 1238 | remove_hash_entry(struct ftrace_hash *hash, |
| 1239 | struct ftrace_func_entry *entry) |
| 1240 | { |
| 1241 | hlist_del_rcu(n: &entry->hlist); |
| 1242 | hash->count--; |
| 1243 | } |
| 1244 | |
| 1245 | static void ftrace_hash_clear(struct ftrace_hash *hash) |
| 1246 | { |
| 1247 | struct hlist_head *hhd; |
| 1248 | struct hlist_node *tn; |
| 1249 | struct ftrace_func_entry *entry; |
| 1250 | int size = 1 << hash->size_bits; |
| 1251 | int i; |
| 1252 | |
| 1253 | if (!hash->count) |
| 1254 | return; |
| 1255 | |
| 1256 | for (i = 0; i < size; i++) { |
| 1257 | hhd = &hash->buckets[i]; |
| 1258 | hlist_for_each_entry_safe(entry, tn, hhd, hlist) |
| 1259 | free_hash_entry(hash, entry); |
| 1260 | } |
| 1261 | FTRACE_WARN_ON(hash->count); |
| 1262 | } |
| 1263 | |
| 1264 | static void free_ftrace_mod(struct ftrace_mod_load *ftrace_mod) |
| 1265 | { |
| 1266 | list_del(entry: &ftrace_mod->list); |
| 1267 | kfree(objp: ftrace_mod->module); |
| 1268 | kfree(objp: ftrace_mod->func); |
| 1269 | kfree(objp: ftrace_mod); |
| 1270 | } |
| 1271 | |
| 1272 | static void clear_ftrace_mod_list(struct list_head *head) |
| 1273 | { |
| 1274 | struct ftrace_mod_load *p, *n; |
| 1275 | |
| 1276 | /* stack tracer isn't supported yet */ |
| 1277 | if (!head) |
| 1278 | return; |
| 1279 | |
| 1280 | mutex_lock(&ftrace_lock); |
| 1281 | list_for_each_entry_safe(p, n, head, list) |
| 1282 | free_ftrace_mod(ftrace_mod: p); |
| 1283 | mutex_unlock(lock: &ftrace_lock); |
| 1284 | } |
| 1285 | |
| 1286 | static void free_ftrace_hash(struct ftrace_hash *hash) |
| 1287 | { |
| 1288 | if (!hash || hash == EMPTY_HASH) |
| 1289 | return; |
| 1290 | ftrace_hash_clear(hash); |
| 1291 | kfree(objp: hash->buckets); |
| 1292 | kfree(objp: hash); |
| 1293 | } |
| 1294 | |
| 1295 | static void __free_ftrace_hash_rcu(struct rcu_head *rcu) |
| 1296 | { |
| 1297 | struct ftrace_hash *hash; |
| 1298 | |
| 1299 | hash = container_of(rcu, struct ftrace_hash, rcu); |
| 1300 | free_ftrace_hash(hash); |
| 1301 | } |
| 1302 | |
| 1303 | static void free_ftrace_hash_rcu(struct ftrace_hash *hash) |
| 1304 | { |
| 1305 | if (!hash || hash == EMPTY_HASH) |
| 1306 | return; |
| 1307 | call_rcu(head: &hash->rcu, func: __free_ftrace_hash_rcu); |
| 1308 | } |
| 1309 | |
| 1310 | /** |
| 1311 | * ftrace_free_filter - remove all filters for an ftrace_ops |
| 1312 | * @ops: the ops to remove the filters from |
| 1313 | */ |
| 1314 | void ftrace_free_filter(struct ftrace_ops *ops) |
| 1315 | { |
| 1316 | ftrace_ops_init(ops); |
| 1317 | if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED)) |
| 1318 | return; |
| 1319 | free_ftrace_hash(hash: ops->func_hash->filter_hash); |
| 1320 | free_ftrace_hash(hash: ops->func_hash->notrace_hash); |
| 1321 | ops->func_hash->filter_hash = EMPTY_HASH; |
| 1322 | ops->func_hash->notrace_hash = EMPTY_HASH; |
| 1323 | } |
| 1324 | EXPORT_SYMBOL_GPL(ftrace_free_filter); |
| 1325 | |
| 1326 | static struct ftrace_hash *alloc_ftrace_hash(int size_bits) |
| 1327 | { |
| 1328 | struct ftrace_hash *hash; |
| 1329 | int size; |
| 1330 | |
| 1331 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); |
| 1332 | if (!hash) |
| 1333 | return NULL; |
| 1334 | |
| 1335 | size = 1 << size_bits; |
| 1336 | hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL); |
| 1337 | |
| 1338 | if (!hash->buckets) { |
| 1339 | kfree(objp: hash); |
| 1340 | return NULL; |
| 1341 | } |
| 1342 | |
| 1343 | hash->size_bits = size_bits; |
| 1344 | |
| 1345 | return hash; |
| 1346 | } |
| 1347 | |
| 1348 | /* Used to save filters on functions for modules not loaded yet */ |
| 1349 | static int ftrace_add_mod(struct trace_array *tr, |
| 1350 | const char *func, const char *module, |
| 1351 | int enable) |
| 1352 | { |
| 1353 | struct ftrace_mod_load *ftrace_mod; |
| 1354 | struct list_head *mod_head = enable ? &tr->mod_trace : &tr->mod_notrace; |
| 1355 | |
| 1356 | ftrace_mod = kzalloc(sizeof(*ftrace_mod), GFP_KERNEL); |
| 1357 | if (!ftrace_mod) |
| 1358 | return -ENOMEM; |
| 1359 | |
| 1360 | INIT_LIST_HEAD(list: &ftrace_mod->list); |
| 1361 | ftrace_mod->func = kstrdup(s: func, GFP_KERNEL); |
| 1362 | ftrace_mod->module = kstrdup(s: module, GFP_KERNEL); |
| 1363 | ftrace_mod->enable = enable; |
| 1364 | |
| 1365 | if (!ftrace_mod->func || !ftrace_mod->module) |
| 1366 | goto out_free; |
| 1367 | |
| 1368 | list_add(new: &ftrace_mod->list, head: mod_head); |
| 1369 | |
| 1370 | return 0; |
| 1371 | |
| 1372 | out_free: |
| 1373 | free_ftrace_mod(ftrace_mod); |
| 1374 | |
| 1375 | return -ENOMEM; |
| 1376 | } |
| 1377 | |
| 1378 | static struct ftrace_hash * |
| 1379 | alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash) |
| 1380 | { |
| 1381 | struct ftrace_func_entry *entry; |
| 1382 | struct ftrace_hash *new_hash; |
| 1383 | int size; |
| 1384 | int i; |
| 1385 | |
| 1386 | new_hash = alloc_ftrace_hash(size_bits); |
| 1387 | if (!new_hash) |
| 1388 | return NULL; |
| 1389 | |
| 1390 | if (hash) |
| 1391 | new_hash->flags = hash->flags; |
| 1392 | |
| 1393 | /* Empty hash? */ |
| 1394 | if (ftrace_hash_empty(hash)) |
| 1395 | return new_hash; |
| 1396 | |
| 1397 | size = 1 << hash->size_bits; |
| 1398 | for (i = 0; i < size; i++) { |
| 1399 | hlist_for_each_entry(entry, &hash->buckets[i], hlist) { |
| 1400 | if (add_hash_entry(hash: new_hash, ip: entry->ip) == NULL) |
| 1401 | goto free_hash; |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | FTRACE_WARN_ON(new_hash->count != hash->count); |
| 1406 | |
| 1407 | return new_hash; |
| 1408 | |
| 1409 | free_hash: |
| 1410 | free_ftrace_hash(hash: new_hash); |
| 1411 | return NULL; |
| 1412 | } |
| 1413 | |
| 1414 | static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops); |
| 1415 | static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops); |
| 1416 | |
| 1417 | static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops, |
| 1418 | struct ftrace_hash *new_hash); |
| 1419 | |
| 1420 | /* |
| 1421 | * Allocate a new hash and remove entries from @src and move them to the new hash. |
| 1422 | * On success, the @src hash will be empty and should be freed. |
| 1423 | */ |
| 1424 | static struct ftrace_hash *__move_hash(struct ftrace_hash *src, int size) |
| 1425 | { |
| 1426 | struct ftrace_func_entry *entry; |
| 1427 | struct ftrace_hash *new_hash; |
| 1428 | struct hlist_head *hhd; |
| 1429 | struct hlist_node *tn; |
| 1430 | int bits = 0; |
| 1431 | int i; |
| 1432 | |
| 1433 | /* |
| 1434 | * Use around half the size (max bit of it), but |
| 1435 | * a minimum of 2 is fine (as size of 0 or 1 both give 1 for bits). |
| 1436 | */ |
| 1437 | bits = fls(x: size / 2); |
| 1438 | |
| 1439 | /* Don't allocate too much */ |
| 1440 | if (bits > FTRACE_HASH_MAX_BITS) |
| 1441 | bits = FTRACE_HASH_MAX_BITS; |
| 1442 | |
| 1443 | new_hash = alloc_ftrace_hash(size_bits: bits); |
| 1444 | if (!new_hash) |
| 1445 | return NULL; |
| 1446 | |
| 1447 | new_hash->flags = src->flags; |
| 1448 | |
| 1449 | size = 1 << src->size_bits; |
| 1450 | for (i = 0; i < size; i++) { |
| 1451 | hhd = &src->buckets[i]; |
| 1452 | hlist_for_each_entry_safe(entry, tn, hhd, hlist) { |
| 1453 | remove_hash_entry(hash: src, entry); |
| 1454 | __add_hash_entry(hash: new_hash, entry); |
| 1455 | } |
| 1456 | } |
| 1457 | return new_hash; |
| 1458 | } |
| 1459 | |
| 1460 | /* Move the @src entries to a newly allocated hash */ |
| 1461 | static struct ftrace_hash * |
| 1462 | __ftrace_hash_move(struct ftrace_hash *src) |
| 1463 | { |
| 1464 | int size = src->count; |
| 1465 | |
| 1466 | /* |
| 1467 | * If the new source is empty, just return the empty_hash. |
| 1468 | */ |
| 1469 | if (ftrace_hash_empty(hash: src)) |
| 1470 | return EMPTY_HASH; |
| 1471 | |
| 1472 | return __move_hash(src, size); |
| 1473 | } |
| 1474 | |
| 1475 | /** |
| 1476 | * ftrace_hash_move - move a new hash to a filter and do updates |
| 1477 | * @ops: The ops with the hash that @dst points to |
| 1478 | * @enable: True if for the filter hash, false for the notrace hash |
| 1479 | * @dst: Points to the @ops hash that should be updated |
| 1480 | * @src: The hash to update @dst with |
| 1481 | * |
| 1482 | * This is called when an ftrace_ops hash is being updated and the |
| 1483 | * the kernel needs to reflect this. Note, this only updates the kernel |
| 1484 | * function callbacks if the @ops is enabled (not to be confused with |
| 1485 | * @enable above). If the @ops is enabled, its hash determines what |
| 1486 | * callbacks get called. This function gets called when the @ops hash |
| 1487 | * is updated and it requires new callbacks. |
| 1488 | * |
| 1489 | * On success the elements of @src is moved to @dst, and @dst is updated |
| 1490 | * properly, as well as the functions determined by the @ops hashes |
| 1491 | * are now calling the @ops callback function. |
| 1492 | * |
| 1493 | * Regardless of return type, @src should be freed with free_ftrace_hash(). |
| 1494 | */ |
| 1495 | static int |
| 1496 | ftrace_hash_move(struct ftrace_ops *ops, int enable, |
| 1497 | struct ftrace_hash **dst, struct ftrace_hash *src) |
| 1498 | { |
| 1499 | struct ftrace_hash *new_hash; |
| 1500 | int ret; |
| 1501 | |
| 1502 | /* Reject setting notrace hash on IPMODIFY ftrace_ops */ |
| 1503 | if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable) |
| 1504 | return -EINVAL; |
| 1505 | |
| 1506 | new_hash = __ftrace_hash_move(src); |
| 1507 | if (!new_hash) |
| 1508 | return -ENOMEM; |
| 1509 | |
| 1510 | /* Make sure this can be applied if it is IPMODIFY ftrace_ops */ |
| 1511 | if (enable) { |
| 1512 | /* IPMODIFY should be updated only when filter_hash updating */ |
| 1513 | ret = ftrace_hash_ipmodify_update(ops, new_hash); |
| 1514 | if (ret < 0) { |
| 1515 | free_ftrace_hash(hash: new_hash); |
| 1516 | return ret; |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | /* |
| 1521 | * Remove the current set, update the hash and add |
| 1522 | * them back. |
| 1523 | */ |
| 1524 | ftrace_hash_rec_disable_modify(ops); |
| 1525 | |
| 1526 | rcu_assign_pointer(*dst, new_hash); |
| 1527 | |
| 1528 | ftrace_hash_rec_enable_modify(ops); |
| 1529 | |
| 1530 | return 0; |
| 1531 | } |
| 1532 | |
| 1533 | static bool hash_contains_ip(unsigned long ip, |
| 1534 | struct ftrace_ops_hash *hash) |
| 1535 | { |
| 1536 | /* |
| 1537 | * The function record is a match if it exists in the filter |
| 1538 | * hash and not in the notrace hash. Note, an empty hash is |
| 1539 | * considered a match for the filter hash, but an empty |
| 1540 | * notrace hash is considered not in the notrace hash. |
| 1541 | */ |
| 1542 | return (ftrace_hash_empty(hash: hash->filter_hash) || |
| 1543 | __ftrace_lookup_ip(hash: hash->filter_hash, ip)) && |
| 1544 | (ftrace_hash_empty(hash: hash->notrace_hash) || |
| 1545 | !__ftrace_lookup_ip(hash: hash->notrace_hash, ip)); |
| 1546 | } |
| 1547 | |
| 1548 | /* |
| 1549 | * Test the hashes for this ops to see if we want to call |
| 1550 | * the ops->func or not. |
| 1551 | * |
| 1552 | * It's a match if the ip is in the ops->filter_hash or |
| 1553 | * the filter_hash does not exist or is empty, |
| 1554 | * AND |
| 1555 | * the ip is not in the ops->notrace_hash. |
| 1556 | * |
| 1557 | * This needs to be called with preemption disabled as |
| 1558 | * the hashes are freed with call_rcu(). |
| 1559 | */ |
| 1560 | int |
| 1561 | ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs) |
| 1562 | { |
| 1563 | struct ftrace_ops_hash hash; |
| 1564 | int ret; |
| 1565 | |
| 1566 | #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS |
| 1567 | /* |
| 1568 | * There's a small race when adding ops that the ftrace handler |
| 1569 | * that wants regs, may be called without them. We can not |
| 1570 | * allow that handler to be called if regs is NULL. |
| 1571 | */ |
| 1572 | if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS)) |
| 1573 | return 0; |
| 1574 | #endif |
| 1575 | |
| 1576 | rcu_assign_pointer(hash.filter_hash, ops->func_hash->filter_hash); |
| 1577 | rcu_assign_pointer(hash.notrace_hash, ops->func_hash->notrace_hash); |
| 1578 | |
| 1579 | if (hash_contains_ip(ip, hash: &hash)) |
| 1580 | ret = 1; |
| 1581 | else |
| 1582 | ret = 0; |
| 1583 | |
| 1584 | return ret; |
| 1585 | } |
| 1586 | |
| 1587 | /* |
| 1588 | * This is a double for. Do not use 'break' to break out of the loop, |
| 1589 | * you must use a goto. |
| 1590 | */ |
| 1591 | #define do_for_each_ftrace_rec(pg, rec) \ |
| 1592 | for (pg = ftrace_pages_start; pg; pg = pg->next) { \ |
| 1593 | int _____i; \ |
| 1594 | for (_____i = 0; _____i < pg->index; _____i++) { \ |
| 1595 | rec = &pg->records[_____i]; |
| 1596 | |
| 1597 | #define while_for_each_ftrace_rec() \ |
| 1598 | } \ |
| 1599 | } |
| 1600 | |
| 1601 | |
| 1602 | static int ftrace_cmp_recs(const void *a, const void *b) |
| 1603 | { |
| 1604 | const struct dyn_ftrace *key = a; |
| 1605 | const struct dyn_ftrace *rec = b; |
| 1606 | |
| 1607 | if (key->flags < rec->ip) |
| 1608 | return -1; |
| 1609 | if (key->ip >= rec->ip + MCOUNT_INSN_SIZE) |
| 1610 | return 1; |
| 1611 | return 0; |
| 1612 | } |
| 1613 | |
| 1614 | static struct dyn_ftrace *lookup_rec(unsigned long start, unsigned long end) |
| 1615 | { |
| 1616 | struct ftrace_page *pg; |
| 1617 | struct dyn_ftrace *rec = NULL; |
| 1618 | struct dyn_ftrace key; |
| 1619 | |
| 1620 | key.ip = start; |
| 1621 | key.flags = end; /* overload flags, as it is unsigned long */ |
| 1622 | |
| 1623 | for (pg = ftrace_pages_start; pg; pg = pg->next) { |
| 1624 | if (pg->index == 0 || |
| 1625 | end < pg->records[0].ip || |
| 1626 | start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE)) |
| 1627 | continue; |
| 1628 | rec = bsearch(key: &key, base: pg->records, num: pg->index, |
| 1629 | size: sizeof(struct dyn_ftrace), |
| 1630 | cmp: ftrace_cmp_recs); |
| 1631 | if (rec) |
| 1632 | break; |
| 1633 | } |
| 1634 | return rec; |
| 1635 | } |
| 1636 | |
| 1637 | /** |
| 1638 | * ftrace_location_range - return the first address of a traced location |
| 1639 | * if it touches the given ip range |
| 1640 | * @start: start of range to search. |
| 1641 | * @end: end of range to search (inclusive). @end points to the last byte |
| 1642 | * to check. |
| 1643 | * |
| 1644 | * Returns: rec->ip if the related ftrace location is a least partly within |
| 1645 | * the given address range. That is, the first address of the instruction |
| 1646 | * that is either a NOP or call to the function tracer. It checks the ftrace |
| 1647 | * internal tables to determine if the address belongs or not. |
| 1648 | */ |
| 1649 | unsigned long ftrace_location_range(unsigned long start, unsigned long end) |
| 1650 | { |
| 1651 | struct dyn_ftrace *rec; |
| 1652 | unsigned long ip = 0; |
| 1653 | |
| 1654 | rcu_read_lock(); |
| 1655 | rec = lookup_rec(start, end); |
| 1656 | if (rec) |
| 1657 | ip = rec->ip; |
| 1658 | rcu_read_unlock(); |
| 1659 | |
| 1660 | return ip; |
| 1661 | } |
| 1662 | |
| 1663 | /** |
| 1664 | * ftrace_location - return the ftrace location |
| 1665 | * @ip: the instruction pointer to check |
| 1666 | * |
| 1667 | * Returns: |
| 1668 | * * If @ip matches the ftrace location, return @ip. |
| 1669 | * * If @ip matches sym+0, return sym's ftrace location. |
| 1670 | * * Otherwise, return 0. |
| 1671 | */ |
| 1672 | unsigned long ftrace_location(unsigned long ip) |
| 1673 | { |
| 1674 | unsigned long loc; |
| 1675 | unsigned long offset; |
| 1676 | unsigned long size; |
| 1677 | |
| 1678 | loc = ftrace_location_range(start: ip, end: ip); |
| 1679 | if (!loc) { |
| 1680 | if (!kallsyms_lookup_size_offset(addr: ip, symbolsize: &size, offset: &offset)) |
| 1681 | return 0; |
| 1682 | |
| 1683 | /* map sym+0 to __fentry__ */ |
| 1684 | if (!offset) |
| 1685 | loc = ftrace_location_range(start: ip, end: ip + size - 1); |
| 1686 | } |
| 1687 | return loc; |
| 1688 | } |
| 1689 | |
| 1690 | /** |
| 1691 | * ftrace_text_reserved - return true if range contains an ftrace location |
| 1692 | * @start: start of range to search |
| 1693 | * @end: end of range to search (inclusive). @end points to the last byte to check. |
| 1694 | * |
| 1695 | * Returns: 1 if @start and @end contains a ftrace location. |
| 1696 | * That is, the instruction that is either a NOP or call to |
| 1697 | * the function tracer. It checks the ftrace internal tables to |
| 1698 | * determine if the address belongs or not. |
| 1699 | */ |
| 1700 | int ftrace_text_reserved(const void *start, const void *end) |
| 1701 | { |
| 1702 | unsigned long ret; |
| 1703 | |
| 1704 | ret = ftrace_location_range(start: (unsigned long)start, |
| 1705 | end: (unsigned long)end); |
| 1706 | |
| 1707 | return (int)!!ret; |
| 1708 | } |
| 1709 | |
| 1710 | /* Test if ops registered to this rec needs regs */ |
| 1711 | static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec) |
| 1712 | { |
| 1713 | struct ftrace_ops *ops; |
| 1714 | bool keep_regs = false; |
| 1715 | |
| 1716 | for (ops = ftrace_ops_list; |
| 1717 | ops != &ftrace_list_end; ops = ops->next) { |
| 1718 | /* pass rec in as regs to have non-NULL val */ |
| 1719 | if (ftrace_ops_test(ops, ip: rec->ip, regs: rec)) { |
| 1720 | if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) { |
| 1721 | keep_regs = true; |
| 1722 | break; |
| 1723 | } |
| 1724 | } |
| 1725 | } |
| 1726 | |
| 1727 | return keep_regs; |
| 1728 | } |
| 1729 | |
| 1730 | static struct ftrace_ops * |
| 1731 | ftrace_find_tramp_ops_any(struct dyn_ftrace *rec); |
| 1732 | static struct ftrace_ops * |
| 1733 | ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude); |
| 1734 | static struct ftrace_ops * |
| 1735 | ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops); |
| 1736 | |
| 1737 | static bool skip_record(struct dyn_ftrace *rec) |
| 1738 | { |
| 1739 | /* |
| 1740 | * At boot up, weak functions are set to disable. Function tracing |
| 1741 | * can be enabled before they are, and they still need to be disabled now. |
| 1742 | * If the record is disabled, still continue if it is marked as already |
| 1743 | * enabled (this is needed to keep the accounting working). |
| 1744 | */ |
| 1745 | return rec->flags & FTRACE_FL_DISABLED && |
| 1746 | !(rec->flags & FTRACE_FL_ENABLED); |
| 1747 | } |
| 1748 | |
| 1749 | /* |
| 1750 | * This is the main engine to the ftrace updates to the dyn_ftrace records. |
| 1751 | * |
| 1752 | * It will iterate through all the available ftrace functions |
| 1753 | * (the ones that ftrace can have callbacks to) and set the flags |
| 1754 | * in the associated dyn_ftrace records. |
| 1755 | * |
| 1756 | * @inc: If true, the functions associated to @ops are added to |
| 1757 | * the dyn_ftrace records, otherwise they are removed. |
| 1758 | */ |
| 1759 | static bool __ftrace_hash_rec_update(struct ftrace_ops *ops, |
| 1760 | bool inc) |
| 1761 | { |
| 1762 | struct ftrace_hash *hash; |
| 1763 | struct ftrace_hash *notrace_hash; |
| 1764 | struct ftrace_page *pg; |
| 1765 | struct dyn_ftrace *rec; |
| 1766 | bool update = false; |
| 1767 | int count = 0; |
| 1768 | int all = false; |
| 1769 | |
| 1770 | /* Only update if the ops has been registered */ |
| 1771 | if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) |
| 1772 | return false; |
| 1773 | |
| 1774 | /* |
| 1775 | * If the count is zero, we update all records. |
| 1776 | * Otherwise we just update the items in the hash. |
| 1777 | */ |
| 1778 | hash = ops->func_hash->filter_hash; |
| 1779 | notrace_hash = ops->func_hash->notrace_hash; |
| 1780 | if (ftrace_hash_empty(hash)) |
| 1781 | all = true; |
| 1782 | |
| 1783 | do_for_each_ftrace_rec(pg, rec) { |
| 1784 | int in_notrace_hash = 0; |
| 1785 | int in_hash = 0; |
| 1786 | int match = 0; |
| 1787 | |
| 1788 | if (skip_record(rec)) |
| 1789 | continue; |
| 1790 | |
| 1791 | if (all) { |
| 1792 | /* |
| 1793 | * Only the filter_hash affects all records. |
| 1794 | * Update if the record is not in the notrace hash. |
| 1795 | */ |
| 1796 | if (!notrace_hash || !ftrace_lookup_ip(hash: notrace_hash, ip: rec->ip)) |
| 1797 | match = 1; |
| 1798 | } else { |
| 1799 | in_hash = !!ftrace_lookup_ip(hash, ip: rec->ip); |
| 1800 | in_notrace_hash = !!ftrace_lookup_ip(hash: notrace_hash, ip: rec->ip); |
| 1801 | |
| 1802 | /* |
| 1803 | * We want to match all functions that are in the hash but |
| 1804 | * not in the other hash. |
| 1805 | */ |
| 1806 | if (in_hash && !in_notrace_hash) |
| 1807 | match = 1; |
| 1808 | } |
| 1809 | if (!match) |
| 1810 | continue; |
| 1811 | |
| 1812 | if (inc) { |
| 1813 | rec->flags++; |
| 1814 | if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX)) |
| 1815 | return false; |
| 1816 | |
| 1817 | if (ops->flags & FTRACE_OPS_FL_DIRECT) |
| 1818 | rec->flags |= FTRACE_FL_DIRECT; |
| 1819 | |
| 1820 | /* |
| 1821 | * If there's only a single callback registered to a |
| 1822 | * function, and the ops has a trampoline registered |
| 1823 | * for it, then we can call it directly. |
| 1824 | */ |
| 1825 | if (ftrace_rec_count(rec) == 1 && ops->trampoline) |
| 1826 | rec->flags |= FTRACE_FL_TRAMP; |
| 1827 | else |
| 1828 | /* |
| 1829 | * If we are adding another function callback |
| 1830 | * to this function, and the previous had a |
| 1831 | * custom trampoline in use, then we need to go |
| 1832 | * back to the default trampoline. |
| 1833 | */ |
| 1834 | rec->flags &= ~FTRACE_FL_TRAMP; |
| 1835 | |
| 1836 | /* |
| 1837 | * If any ops wants regs saved for this function |
| 1838 | * then all ops will get saved regs. |
| 1839 | */ |
| 1840 | if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) |
| 1841 | rec->flags |= FTRACE_FL_REGS; |
| 1842 | } else { |
| 1843 | if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0)) |
| 1844 | return false; |
| 1845 | rec->flags--; |
| 1846 | |
| 1847 | /* |
| 1848 | * Only the internal direct_ops should have the |
| 1849 | * DIRECT flag set. Thus, if it is removing a |
| 1850 | * function, then that function should no longer |
| 1851 | * be direct. |
| 1852 | */ |
| 1853 | if (ops->flags & FTRACE_OPS_FL_DIRECT) |
| 1854 | rec->flags &= ~FTRACE_FL_DIRECT; |
| 1855 | |
| 1856 | /* |
| 1857 | * If the rec had REGS enabled and the ops that is |
| 1858 | * being removed had REGS set, then see if there is |
| 1859 | * still any ops for this record that wants regs. |
| 1860 | * If not, we can stop recording them. |
| 1861 | */ |
| 1862 | if (ftrace_rec_count(rec) > 0 && |
| 1863 | rec->flags & FTRACE_FL_REGS && |
| 1864 | ops->flags & FTRACE_OPS_FL_SAVE_REGS) { |
| 1865 | if (!test_rec_ops_needs_regs(rec)) |
| 1866 | rec->flags &= ~FTRACE_FL_REGS; |
| 1867 | } |
| 1868 | |
| 1869 | /* |
| 1870 | * The TRAMP needs to be set only if rec count |
| 1871 | * is decremented to one, and the ops that is |
| 1872 | * left has a trampoline. As TRAMP can only be |
| 1873 | * enabled if there is only a single ops attached |
| 1874 | * to it. |
| 1875 | */ |
| 1876 | if (ftrace_rec_count(rec) == 1 && |
| 1877 | ftrace_find_tramp_ops_any_other(rec, op_exclude: ops)) |
| 1878 | rec->flags |= FTRACE_FL_TRAMP; |
| 1879 | else |
| 1880 | rec->flags &= ~FTRACE_FL_TRAMP; |
| 1881 | |
| 1882 | /* |
| 1883 | * flags will be cleared in ftrace_check_record() |
| 1884 | * if rec count is zero. |
| 1885 | */ |
| 1886 | } |
| 1887 | |
| 1888 | /* |
| 1889 | * If the rec has a single associated ops, and ops->func can be |
| 1890 | * called directly, allow the call site to call via the ops. |
| 1891 | */ |
| 1892 | if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS) && |
| 1893 | ftrace_rec_count(rec) == 1 && |
| 1894 | ftrace_ops_get_func(ops) == ops->func) |
| 1895 | rec->flags |= FTRACE_FL_CALL_OPS; |
| 1896 | else |
| 1897 | rec->flags &= ~FTRACE_FL_CALL_OPS; |
| 1898 | |
| 1899 | count++; |
| 1900 | |
| 1901 | /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */ |
| 1902 | update |= ftrace_test_record(rec, enable: true) != FTRACE_UPDATE_IGNORE; |
| 1903 | |
| 1904 | /* Shortcut, if we handled all records, we are done. */ |
| 1905 | if (!all && count == hash->count) |
| 1906 | return update; |
| 1907 | } while_for_each_ftrace_rec(); |
| 1908 | |
| 1909 | return update; |
| 1910 | } |
| 1911 | |
| 1912 | /* |
| 1913 | * This is called when an ops is removed from tracing. It will decrement |
| 1914 | * the counters of the dyn_ftrace records for all the functions that |
| 1915 | * the @ops attached to. |
| 1916 | */ |
| 1917 | static bool ftrace_hash_rec_disable(struct ftrace_ops *ops) |
| 1918 | { |
| 1919 | return __ftrace_hash_rec_update(ops, inc: false); |
| 1920 | } |
| 1921 | |
| 1922 | /* |
| 1923 | * This is called when an ops is added to tracing. It will increment |
| 1924 | * the counters of the dyn_ftrace records for all the functions that |
| 1925 | * the @ops attached to. |
| 1926 | */ |
| 1927 | static bool ftrace_hash_rec_enable(struct ftrace_ops *ops) |
| 1928 | { |
| 1929 | return __ftrace_hash_rec_update(ops, inc: true); |
| 1930 | } |
| 1931 | |
| 1932 | /* |
| 1933 | * This function will update what functions @ops traces when its filter |
| 1934 | * changes. |
| 1935 | * |
| 1936 | * The @inc states if the @ops callbacks are going to be added or removed. |
| 1937 | * When one of the @ops hashes are updated to a "new_hash" the dyn_ftrace |
| 1938 | * records are update via: |
| 1939 | * |
| 1940 | * ftrace_hash_rec_disable_modify(ops); |
| 1941 | * ops->hash = new_hash |
| 1942 | * ftrace_hash_rec_enable_modify(ops); |
| 1943 | * |
| 1944 | * Where the @ops is removed from all the records it is tracing using |
| 1945 | * its old hash. The @ops hash is updated to the new hash, and then |
| 1946 | * the @ops is added back to the records so that it is tracing all |
| 1947 | * the new functions. |
| 1948 | */ |
| 1949 | static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops, bool inc) |
| 1950 | { |
| 1951 | struct ftrace_ops *op; |
| 1952 | |
| 1953 | __ftrace_hash_rec_update(ops, inc); |
| 1954 | |
| 1955 | if (ops->func_hash != &global_ops.local_hash) |
| 1956 | return; |
| 1957 | |
| 1958 | /* |
| 1959 | * If the ops shares the global_ops hash, then we need to update |
| 1960 | * all ops that are enabled and use this hash. |
| 1961 | */ |
| 1962 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 1963 | /* Already done */ |
| 1964 | if (op == ops) |
| 1965 | continue; |
| 1966 | if (op->func_hash == &global_ops.local_hash) |
| 1967 | __ftrace_hash_rec_update(ops: op, inc); |
| 1968 | } while_for_each_ftrace_op(op); |
| 1969 | } |
| 1970 | |
| 1971 | static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops) |
| 1972 | { |
| 1973 | ftrace_hash_rec_update_modify(ops, inc: false); |
| 1974 | } |
| 1975 | |
| 1976 | static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops) |
| 1977 | { |
| 1978 | ftrace_hash_rec_update_modify(ops, inc: true); |
| 1979 | } |
| 1980 | |
| 1981 | /* |
| 1982 | * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK |
| 1983 | * or no-needed to update, -EBUSY if it detects a conflict of the flag |
| 1984 | * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs. |
| 1985 | * Note that old_hash and new_hash has below meanings |
| 1986 | * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected) |
| 1987 | * - If the hash is EMPTY_HASH, it hits nothing |
| 1988 | * - Anything else hits the recs which match the hash entries. |
| 1989 | * |
| 1990 | * DIRECT ops does not have IPMODIFY flag, but we still need to check it |
| 1991 | * against functions with FTRACE_FL_IPMODIFY. If there is any overlap, call |
| 1992 | * ops_func(SHARE_IPMODIFY_SELF) to make sure current ops can share with |
| 1993 | * IPMODIFY. If ops_func(SHARE_IPMODIFY_SELF) returns non-zero, propagate |
| 1994 | * the return value to the caller and eventually to the owner of the DIRECT |
| 1995 | * ops. |
| 1996 | */ |
| 1997 | static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops, |
| 1998 | struct ftrace_hash *old_hash, |
| 1999 | struct ftrace_hash *new_hash, |
| 2000 | bool update_target) |
| 2001 | { |
| 2002 | struct ftrace_page *pg; |
| 2003 | struct dyn_ftrace *rec, *end = NULL; |
| 2004 | int in_old, in_new; |
| 2005 | bool is_ipmodify, is_direct; |
| 2006 | |
| 2007 | /* Only update if the ops has been registered */ |
| 2008 | if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) |
| 2009 | return 0; |
| 2010 | |
| 2011 | is_ipmodify = ops->flags & FTRACE_OPS_FL_IPMODIFY; |
| 2012 | is_direct = ops->flags & FTRACE_OPS_FL_DIRECT; |
| 2013 | |
| 2014 | /* neither IPMODIFY nor DIRECT, skip */ |
| 2015 | if (!is_ipmodify && !is_direct) |
| 2016 | return 0; |
| 2017 | |
| 2018 | if (WARN_ON_ONCE(is_ipmodify && is_direct)) |
| 2019 | return 0; |
| 2020 | |
| 2021 | /* |
| 2022 | * Since the IPMODIFY and DIRECT are very address sensitive |
| 2023 | * actions, we do not allow ftrace_ops to set all functions to new |
| 2024 | * hash. |
| 2025 | */ |
| 2026 | if (!new_hash || !old_hash) |
| 2027 | return -EINVAL; |
| 2028 | |
| 2029 | /* Update rec->flags */ |
| 2030 | do_for_each_ftrace_rec(pg, rec) { |
| 2031 | |
| 2032 | if (rec->flags & FTRACE_FL_DISABLED) |
| 2033 | continue; |
| 2034 | |
| 2035 | /* |
| 2036 | * Unless we are updating the target of a direct function, |
| 2037 | * we only need to update differences of filter_hash |
| 2038 | */ |
| 2039 | in_old = !!ftrace_lookup_ip(hash: old_hash, ip: rec->ip); |
| 2040 | in_new = !!ftrace_lookup_ip(hash: new_hash, ip: rec->ip); |
| 2041 | if (!update_target && (in_old == in_new)) |
| 2042 | continue; |
| 2043 | |
| 2044 | if (in_new) { |
| 2045 | if (rec->flags & FTRACE_FL_IPMODIFY) { |
| 2046 | int ret; |
| 2047 | |
| 2048 | /* Cannot have two ipmodify on same rec */ |
| 2049 | if (is_ipmodify) |
| 2050 | goto rollback; |
| 2051 | |
| 2052 | /* |
| 2053 | * If this is called by __modify_ftrace_direct() |
| 2054 | * then it is only changing where the direct |
| 2055 | * pointer is jumping to, and the record already |
| 2056 | * points to a direct trampoline. If it isn't, |
| 2057 | * then it is a bug to update ipmodify on a direct |
| 2058 | * caller. |
| 2059 | */ |
| 2060 | FTRACE_WARN_ON(!update_target && |
| 2061 | (rec->flags & FTRACE_FL_DIRECT)); |
| 2062 | |
| 2063 | /* |
| 2064 | * Another ops with IPMODIFY is already |
| 2065 | * attached. We are now attaching a direct |
| 2066 | * ops. Run SHARE_IPMODIFY_SELF, to check |
| 2067 | * whether sharing is supported. |
| 2068 | */ |
| 2069 | if (!ops->ops_func) |
| 2070 | return -EBUSY; |
| 2071 | ret = ops->ops_func(ops, FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_SELF); |
| 2072 | if (ret) |
| 2073 | return ret; |
| 2074 | } else if (is_ipmodify) { |
| 2075 | rec->flags |= FTRACE_FL_IPMODIFY; |
| 2076 | } |
| 2077 | } else if (is_ipmodify) { |
| 2078 | rec->flags &= ~FTRACE_FL_IPMODIFY; |
| 2079 | } |
| 2080 | } while_for_each_ftrace_rec(); |
| 2081 | |
| 2082 | return 0; |
| 2083 | |
| 2084 | rollback: |
| 2085 | end = rec; |
| 2086 | |
| 2087 | /* Roll back what we did above */ |
| 2088 | do_for_each_ftrace_rec(pg, rec) { |
| 2089 | |
| 2090 | if (rec->flags & FTRACE_FL_DISABLED) |
| 2091 | continue; |
| 2092 | |
| 2093 | if (rec == end) |
| 2094 | return -EBUSY; |
| 2095 | |
| 2096 | in_old = !!ftrace_lookup_ip(hash: old_hash, ip: rec->ip); |
| 2097 | in_new = !!ftrace_lookup_ip(hash: new_hash, ip: rec->ip); |
| 2098 | if (in_old == in_new) |
| 2099 | continue; |
| 2100 | |
| 2101 | if (in_new) |
| 2102 | rec->flags &= ~FTRACE_FL_IPMODIFY; |
| 2103 | else |
| 2104 | rec->flags |= FTRACE_FL_IPMODIFY; |
| 2105 | } while_for_each_ftrace_rec(); |
| 2106 | |
| 2107 | return -EBUSY; |
| 2108 | } |
| 2109 | |
| 2110 | static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops) |
| 2111 | { |
| 2112 | struct ftrace_hash *hash = ops->func_hash->filter_hash; |
| 2113 | |
| 2114 | if (ftrace_hash_empty(hash)) |
| 2115 | hash = NULL; |
| 2116 | |
| 2117 | return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, new_hash: hash, update_target: false); |
| 2118 | } |
| 2119 | |
| 2120 | /* Disabling always succeeds */ |
| 2121 | static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops) |
| 2122 | { |
| 2123 | struct ftrace_hash *hash = ops->func_hash->filter_hash; |
| 2124 | |
| 2125 | if (ftrace_hash_empty(hash)) |
| 2126 | hash = NULL; |
| 2127 | |
| 2128 | __ftrace_hash_update_ipmodify(ops, old_hash: hash, EMPTY_HASH, update_target: false); |
| 2129 | } |
| 2130 | |
| 2131 | static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops, |
| 2132 | struct ftrace_hash *new_hash) |
| 2133 | { |
| 2134 | struct ftrace_hash *old_hash = ops->func_hash->filter_hash; |
| 2135 | |
| 2136 | if (ftrace_hash_empty(hash: old_hash)) |
| 2137 | old_hash = NULL; |
| 2138 | |
| 2139 | if (ftrace_hash_empty(hash: new_hash)) |
| 2140 | new_hash = NULL; |
| 2141 | |
| 2142 | return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash, update_target: false); |
| 2143 | } |
| 2144 | |
| 2145 | static void print_ip_ins(const char *fmt, const unsigned char *p) |
| 2146 | { |
| 2147 | char ins[MCOUNT_INSN_SIZE]; |
| 2148 | |
| 2149 | if (copy_from_kernel_nofault(dst: ins, src: p, MCOUNT_INSN_SIZE)) { |
| 2150 | printk(KERN_CONT "%s[FAULT] %px\n" , fmt, p); |
| 2151 | return; |
| 2152 | } |
| 2153 | |
| 2154 | printk(KERN_CONT "%s" , fmt); |
| 2155 | pr_cont("%*phC" , MCOUNT_INSN_SIZE, ins); |
| 2156 | } |
| 2157 | |
| 2158 | enum ftrace_bug_type ftrace_bug_type; |
| 2159 | const void *ftrace_expected; |
| 2160 | |
| 2161 | static void print_bug_type(void) |
| 2162 | { |
| 2163 | switch (ftrace_bug_type) { |
| 2164 | case FTRACE_BUG_UNKNOWN: |
| 2165 | break; |
| 2166 | case FTRACE_BUG_INIT: |
| 2167 | pr_info("Initializing ftrace call sites\n" ); |
| 2168 | break; |
| 2169 | case FTRACE_BUG_NOP: |
| 2170 | pr_info("Setting ftrace call site to NOP\n" ); |
| 2171 | break; |
| 2172 | case FTRACE_BUG_CALL: |
| 2173 | pr_info("Setting ftrace call site to call ftrace function\n" ); |
| 2174 | break; |
| 2175 | case FTRACE_BUG_UPDATE: |
| 2176 | pr_info("Updating ftrace call site to call a different ftrace function\n" ); |
| 2177 | break; |
| 2178 | } |
| 2179 | } |
| 2180 | |
| 2181 | /** |
| 2182 | * ftrace_bug - report and shutdown function tracer |
| 2183 | * @failed: The failed type (EFAULT, EINVAL, EPERM) |
| 2184 | * @rec: The record that failed |
| 2185 | * |
| 2186 | * The arch code that enables or disables the function tracing |
| 2187 | * can call ftrace_bug() when it has detected a problem in |
| 2188 | * modifying the code. @failed should be one of either: |
| 2189 | * EFAULT - if the problem happens on reading the @ip address |
| 2190 | * EINVAL - if what is read at @ip is not what was expected |
| 2191 | * EPERM - if the problem happens on writing to the @ip address |
| 2192 | */ |
| 2193 | void ftrace_bug(int failed, struct dyn_ftrace *rec) |
| 2194 | { |
| 2195 | unsigned long ip = rec ? rec->ip : 0; |
| 2196 | |
| 2197 | pr_info("------------[ ftrace bug ]------------\n" ); |
| 2198 | |
| 2199 | switch (failed) { |
| 2200 | case -EFAULT: |
| 2201 | pr_info("ftrace faulted on modifying " ); |
| 2202 | print_ip_sym(KERN_INFO, ip); |
| 2203 | break; |
| 2204 | case -EINVAL: |
| 2205 | pr_info("ftrace failed to modify " ); |
| 2206 | print_ip_sym(KERN_INFO, ip); |
| 2207 | print_ip_ins(fmt: " actual: " , p: (unsigned char *)ip); |
| 2208 | pr_cont("\n" ); |
| 2209 | if (ftrace_expected) { |
| 2210 | print_ip_ins(fmt: " expected: " , p: ftrace_expected); |
| 2211 | pr_cont("\n" ); |
| 2212 | } |
| 2213 | break; |
| 2214 | case -EPERM: |
| 2215 | pr_info("ftrace faulted on writing " ); |
| 2216 | print_ip_sym(KERN_INFO, ip); |
| 2217 | break; |
| 2218 | default: |
| 2219 | pr_info("ftrace faulted on unknown error " ); |
| 2220 | print_ip_sym(KERN_INFO, ip); |
| 2221 | } |
| 2222 | print_bug_type(); |
| 2223 | if (rec) { |
| 2224 | struct ftrace_ops *ops = NULL; |
| 2225 | |
| 2226 | pr_info("ftrace record flags: %lx\n" , rec->flags); |
| 2227 | pr_cont(" (%ld)%s%s" , ftrace_rec_count(rec), |
| 2228 | rec->flags & FTRACE_FL_REGS ? " R" : " " , |
| 2229 | rec->flags & FTRACE_FL_CALL_OPS ? " O" : " " ); |
| 2230 | if (rec->flags & FTRACE_FL_TRAMP_EN) { |
| 2231 | ops = ftrace_find_tramp_ops_any(rec); |
| 2232 | if (ops) { |
| 2233 | do { |
| 2234 | pr_cont("\ttramp: %pS (%pS)" , |
| 2235 | (void *)ops->trampoline, |
| 2236 | (void *)ops->func); |
| 2237 | ops = ftrace_find_tramp_ops_next(rec, ops); |
| 2238 | } while (ops); |
| 2239 | } else |
| 2240 | pr_cont("\ttramp: ERROR!" ); |
| 2241 | |
| 2242 | } |
| 2243 | ip = ftrace_get_addr_curr(rec); |
| 2244 | pr_cont("\n expected tramp: %lx\n" , ip); |
| 2245 | } |
| 2246 | |
| 2247 | FTRACE_WARN_ON_ONCE(1); |
| 2248 | } |
| 2249 | |
| 2250 | static int ftrace_check_record(struct dyn_ftrace *rec, bool enable, bool update) |
| 2251 | { |
| 2252 | unsigned long flag = 0UL; |
| 2253 | |
| 2254 | ftrace_bug_type = FTRACE_BUG_UNKNOWN; |
| 2255 | |
| 2256 | if (skip_record(rec)) |
| 2257 | return FTRACE_UPDATE_IGNORE; |
| 2258 | |
| 2259 | /* |
| 2260 | * If we are updating calls: |
| 2261 | * |
| 2262 | * If the record has a ref count, then we need to enable it |
| 2263 | * because someone is using it. |
| 2264 | * |
| 2265 | * Otherwise we make sure its disabled. |
| 2266 | * |
| 2267 | * If we are disabling calls, then disable all records that |
| 2268 | * are enabled. |
| 2269 | */ |
| 2270 | if (enable && ftrace_rec_count(rec)) |
| 2271 | flag = FTRACE_FL_ENABLED; |
| 2272 | |
| 2273 | /* |
| 2274 | * If enabling and the REGS flag does not match the REGS_EN, or |
| 2275 | * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore |
| 2276 | * this record. Set flags to fail the compare against ENABLED. |
| 2277 | * Same for direct calls. |
| 2278 | */ |
| 2279 | if (flag) { |
| 2280 | if (!(rec->flags & FTRACE_FL_REGS) != |
| 2281 | !(rec->flags & FTRACE_FL_REGS_EN)) |
| 2282 | flag |= FTRACE_FL_REGS; |
| 2283 | |
| 2284 | if (!(rec->flags & FTRACE_FL_TRAMP) != |
| 2285 | !(rec->flags & FTRACE_FL_TRAMP_EN)) |
| 2286 | flag |= FTRACE_FL_TRAMP; |
| 2287 | |
| 2288 | /* |
| 2289 | * Direct calls are special, as count matters. |
| 2290 | * We must test the record for direct, if the |
| 2291 | * DIRECT and DIRECT_EN do not match, but only |
| 2292 | * if the count is 1. That's because, if the |
| 2293 | * count is something other than one, we do not |
| 2294 | * want the direct enabled (it will be done via the |
| 2295 | * direct helper). But if DIRECT_EN is set, and |
| 2296 | * the count is not one, we need to clear it. |
| 2297 | * |
| 2298 | */ |
| 2299 | if (ftrace_rec_count(rec) == 1) { |
| 2300 | if (!(rec->flags & FTRACE_FL_DIRECT) != |
| 2301 | !(rec->flags & FTRACE_FL_DIRECT_EN)) |
| 2302 | flag |= FTRACE_FL_DIRECT; |
| 2303 | } else if (rec->flags & FTRACE_FL_DIRECT_EN) { |
| 2304 | flag |= FTRACE_FL_DIRECT; |
| 2305 | } |
| 2306 | |
| 2307 | /* |
| 2308 | * Ops calls are special, as count matters. |
| 2309 | * As with direct calls, they must only be enabled when count |
| 2310 | * is one, otherwise they'll be handled via the list ops. |
| 2311 | */ |
| 2312 | if (ftrace_rec_count(rec) == 1) { |
| 2313 | if (!(rec->flags & FTRACE_FL_CALL_OPS) != |
| 2314 | !(rec->flags & FTRACE_FL_CALL_OPS_EN)) |
| 2315 | flag |= FTRACE_FL_CALL_OPS; |
| 2316 | } else if (rec->flags & FTRACE_FL_CALL_OPS_EN) { |
| 2317 | flag |= FTRACE_FL_CALL_OPS; |
| 2318 | } |
| 2319 | } |
| 2320 | |
| 2321 | /* If the state of this record hasn't changed, then do nothing */ |
| 2322 | if ((rec->flags & FTRACE_FL_ENABLED) == flag) |
| 2323 | return FTRACE_UPDATE_IGNORE; |
| 2324 | |
| 2325 | if (flag) { |
| 2326 | /* Save off if rec is being enabled (for return value) */ |
| 2327 | flag ^= rec->flags & FTRACE_FL_ENABLED; |
| 2328 | |
| 2329 | if (update) { |
| 2330 | rec->flags |= FTRACE_FL_ENABLED | FTRACE_FL_TOUCHED; |
| 2331 | if (flag & FTRACE_FL_REGS) { |
| 2332 | if (rec->flags & FTRACE_FL_REGS) |
| 2333 | rec->flags |= FTRACE_FL_REGS_EN; |
| 2334 | else |
| 2335 | rec->flags &= ~FTRACE_FL_REGS_EN; |
| 2336 | } |
| 2337 | if (flag & FTRACE_FL_TRAMP) { |
| 2338 | if (rec->flags & FTRACE_FL_TRAMP) |
| 2339 | rec->flags |= FTRACE_FL_TRAMP_EN; |
| 2340 | else |
| 2341 | rec->flags &= ~FTRACE_FL_TRAMP_EN; |
| 2342 | } |
| 2343 | |
| 2344 | /* Keep track of anything that modifies the function */ |
| 2345 | if (rec->flags & (FTRACE_FL_DIRECT | FTRACE_FL_IPMODIFY)) |
| 2346 | rec->flags |= FTRACE_FL_MODIFIED; |
| 2347 | |
| 2348 | if (flag & FTRACE_FL_DIRECT) { |
| 2349 | /* |
| 2350 | * If there's only one user (direct_ops helper) |
| 2351 | * then we can call the direct function |
| 2352 | * directly (no ftrace trampoline). |
| 2353 | */ |
| 2354 | if (ftrace_rec_count(rec) == 1) { |
| 2355 | if (rec->flags & FTRACE_FL_DIRECT) |
| 2356 | rec->flags |= FTRACE_FL_DIRECT_EN; |
| 2357 | else |
| 2358 | rec->flags &= ~FTRACE_FL_DIRECT_EN; |
| 2359 | } else { |
| 2360 | /* |
| 2361 | * Can only call directly if there's |
| 2362 | * only one callback to the function. |
| 2363 | */ |
| 2364 | rec->flags &= ~FTRACE_FL_DIRECT_EN; |
| 2365 | } |
| 2366 | } |
| 2367 | |
| 2368 | if (flag & FTRACE_FL_CALL_OPS) { |
| 2369 | if (ftrace_rec_count(rec) == 1) { |
| 2370 | if (rec->flags & FTRACE_FL_CALL_OPS) |
| 2371 | rec->flags |= FTRACE_FL_CALL_OPS_EN; |
| 2372 | else |
| 2373 | rec->flags &= ~FTRACE_FL_CALL_OPS_EN; |
| 2374 | } else { |
| 2375 | /* |
| 2376 | * Can only call directly if there's |
| 2377 | * only one set of associated ops. |
| 2378 | */ |
| 2379 | rec->flags &= ~FTRACE_FL_CALL_OPS_EN; |
| 2380 | } |
| 2381 | } |
| 2382 | } |
| 2383 | |
| 2384 | /* |
| 2385 | * If this record is being updated from a nop, then |
| 2386 | * return UPDATE_MAKE_CALL. |
| 2387 | * Otherwise, |
| 2388 | * return UPDATE_MODIFY_CALL to tell the caller to convert |
| 2389 | * from the save regs, to a non-save regs function or |
| 2390 | * vice versa, or from a trampoline call. |
| 2391 | */ |
| 2392 | if (flag & FTRACE_FL_ENABLED) { |
| 2393 | ftrace_bug_type = FTRACE_BUG_CALL; |
| 2394 | return FTRACE_UPDATE_MAKE_CALL; |
| 2395 | } |
| 2396 | |
| 2397 | ftrace_bug_type = FTRACE_BUG_UPDATE; |
| 2398 | return FTRACE_UPDATE_MODIFY_CALL; |
| 2399 | } |
| 2400 | |
| 2401 | if (update) { |
| 2402 | /* If there's no more users, clear all flags */ |
| 2403 | if (!ftrace_rec_count(rec)) |
| 2404 | rec->flags &= FTRACE_NOCLEAR_FLAGS; |
| 2405 | else |
| 2406 | /* |
| 2407 | * Just disable the record, but keep the ops TRAMP |
| 2408 | * and REGS states. The _EN flags must be disabled though. |
| 2409 | */ |
| 2410 | rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN | |
| 2411 | FTRACE_FL_REGS_EN | FTRACE_FL_DIRECT_EN | |
| 2412 | FTRACE_FL_CALL_OPS_EN); |
| 2413 | } |
| 2414 | |
| 2415 | ftrace_bug_type = FTRACE_BUG_NOP; |
| 2416 | return FTRACE_UPDATE_MAKE_NOP; |
| 2417 | } |
| 2418 | |
| 2419 | /** |
| 2420 | * ftrace_update_record - set a record that now is tracing or not |
| 2421 | * @rec: the record to update |
| 2422 | * @enable: set to true if the record is tracing, false to force disable |
| 2423 | * |
| 2424 | * The records that represent all functions that can be traced need |
| 2425 | * to be updated when tracing has been enabled. |
| 2426 | */ |
| 2427 | int ftrace_update_record(struct dyn_ftrace *rec, bool enable) |
| 2428 | { |
| 2429 | return ftrace_check_record(rec, enable, update: true); |
| 2430 | } |
| 2431 | |
| 2432 | /** |
| 2433 | * ftrace_test_record - check if the record has been enabled or not |
| 2434 | * @rec: the record to test |
| 2435 | * @enable: set to true to check if enabled, false if it is disabled |
| 2436 | * |
| 2437 | * The arch code may need to test if a record is already set to |
| 2438 | * tracing to determine how to modify the function code that it |
| 2439 | * represents. |
| 2440 | */ |
| 2441 | int ftrace_test_record(struct dyn_ftrace *rec, bool enable) |
| 2442 | { |
| 2443 | return ftrace_check_record(rec, enable, update: false); |
| 2444 | } |
| 2445 | |
| 2446 | static struct ftrace_ops * |
| 2447 | ftrace_find_tramp_ops_any(struct dyn_ftrace *rec) |
| 2448 | { |
| 2449 | struct ftrace_ops *op; |
| 2450 | unsigned long ip = rec->ip; |
| 2451 | |
| 2452 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 2453 | |
| 2454 | if (!op->trampoline) |
| 2455 | continue; |
| 2456 | |
| 2457 | if (hash_contains_ip(ip, hash: op->func_hash)) |
| 2458 | return op; |
| 2459 | } while_for_each_ftrace_op(op); |
| 2460 | |
| 2461 | return NULL; |
| 2462 | } |
| 2463 | |
| 2464 | static struct ftrace_ops * |
| 2465 | ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude) |
| 2466 | { |
| 2467 | struct ftrace_ops *op; |
| 2468 | unsigned long ip = rec->ip; |
| 2469 | |
| 2470 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 2471 | |
| 2472 | if (op == op_exclude || !op->trampoline) |
| 2473 | continue; |
| 2474 | |
| 2475 | if (hash_contains_ip(ip, hash: op->func_hash)) |
| 2476 | return op; |
| 2477 | } while_for_each_ftrace_op(op); |
| 2478 | |
| 2479 | return NULL; |
| 2480 | } |
| 2481 | |
| 2482 | static struct ftrace_ops * |
| 2483 | ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, |
| 2484 | struct ftrace_ops *op) |
| 2485 | { |
| 2486 | unsigned long ip = rec->ip; |
| 2487 | |
| 2488 | while_for_each_ftrace_op(op) { |
| 2489 | |
| 2490 | if (!op->trampoline) |
| 2491 | continue; |
| 2492 | |
| 2493 | if (hash_contains_ip(ip, hash: op->func_hash)) |
| 2494 | return op; |
| 2495 | } |
| 2496 | |
| 2497 | return NULL; |
| 2498 | } |
| 2499 | |
| 2500 | static struct ftrace_ops * |
| 2501 | ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec) |
| 2502 | { |
| 2503 | struct ftrace_ops *op; |
| 2504 | unsigned long ip = rec->ip; |
| 2505 | |
| 2506 | /* |
| 2507 | * Need to check removed ops first. |
| 2508 | * If they are being removed, and this rec has a tramp, |
| 2509 | * and this rec is in the ops list, then it would be the |
| 2510 | * one with the tramp. |
| 2511 | */ |
| 2512 | if (removed_ops) { |
| 2513 | if (hash_contains_ip(ip, hash: &removed_ops->old_hash)) |
| 2514 | return removed_ops; |
| 2515 | } |
| 2516 | |
| 2517 | /* |
| 2518 | * Need to find the current trampoline for a rec. |
| 2519 | * Now, a trampoline is only attached to a rec if there |
| 2520 | * was a single 'ops' attached to it. But this can be called |
| 2521 | * when we are adding another op to the rec or removing the |
| 2522 | * current one. Thus, if the op is being added, we can |
| 2523 | * ignore it because it hasn't attached itself to the rec |
| 2524 | * yet. |
| 2525 | * |
| 2526 | * If an ops is being modified (hooking to different functions) |
| 2527 | * then we don't care about the new functions that are being |
| 2528 | * added, just the old ones (that are probably being removed). |
| 2529 | * |
| 2530 | * If we are adding an ops to a function that already is using |
| 2531 | * a trampoline, it needs to be removed (trampolines are only |
| 2532 | * for single ops connected), then an ops that is not being |
| 2533 | * modified also needs to be checked. |
| 2534 | */ |
| 2535 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 2536 | |
| 2537 | if (!op->trampoline) |
| 2538 | continue; |
| 2539 | |
| 2540 | /* |
| 2541 | * If the ops is being added, it hasn't gotten to |
| 2542 | * the point to be removed from this tree yet. |
| 2543 | */ |
| 2544 | if (op->flags & FTRACE_OPS_FL_ADDING) |
| 2545 | continue; |
| 2546 | |
| 2547 | |
| 2548 | /* |
| 2549 | * If the ops is being modified and is in the old |
| 2550 | * hash, then it is probably being removed from this |
| 2551 | * function. |
| 2552 | */ |
| 2553 | if ((op->flags & FTRACE_OPS_FL_MODIFYING) && |
| 2554 | hash_contains_ip(ip, hash: &op->old_hash)) |
| 2555 | return op; |
| 2556 | /* |
| 2557 | * If the ops is not being added or modified, and it's |
| 2558 | * in its normal filter hash, then this must be the one |
| 2559 | * we want! |
| 2560 | */ |
| 2561 | if (!(op->flags & FTRACE_OPS_FL_MODIFYING) && |
| 2562 | hash_contains_ip(ip, hash: op->func_hash)) |
| 2563 | return op; |
| 2564 | |
| 2565 | } while_for_each_ftrace_op(op); |
| 2566 | |
| 2567 | return NULL; |
| 2568 | } |
| 2569 | |
| 2570 | static struct ftrace_ops * |
| 2571 | ftrace_find_tramp_ops_new(struct dyn_ftrace *rec) |
| 2572 | { |
| 2573 | struct ftrace_ops *op; |
| 2574 | unsigned long ip = rec->ip; |
| 2575 | |
| 2576 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 2577 | /* pass rec in as regs to have non-NULL val */ |
| 2578 | if (hash_contains_ip(ip, hash: op->func_hash)) |
| 2579 | return op; |
| 2580 | } while_for_each_ftrace_op(op); |
| 2581 | |
| 2582 | return NULL; |
| 2583 | } |
| 2584 | |
| 2585 | struct ftrace_ops * |
| 2586 | ftrace_find_unique_ops(struct dyn_ftrace *rec) |
| 2587 | { |
| 2588 | struct ftrace_ops *op, *found = NULL; |
| 2589 | unsigned long ip = rec->ip; |
| 2590 | |
| 2591 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 2592 | |
| 2593 | if (hash_contains_ip(ip, hash: op->func_hash)) { |
| 2594 | if (found) |
| 2595 | return NULL; |
| 2596 | found = op; |
| 2597 | } |
| 2598 | |
| 2599 | } while_for_each_ftrace_op(op); |
| 2600 | |
| 2601 | return found; |
| 2602 | } |
| 2603 | |
| 2604 | #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS |
| 2605 | /* Protected by rcu_tasks for reading, and direct_mutex for writing */ |
| 2606 | static struct ftrace_hash __rcu *direct_functions = EMPTY_HASH; |
| 2607 | static DEFINE_MUTEX(direct_mutex); |
| 2608 | |
| 2609 | /* |
| 2610 | * Search the direct_functions hash to see if the given instruction pointer |
| 2611 | * has a direct caller attached to it. |
| 2612 | */ |
| 2613 | unsigned long ftrace_find_rec_direct(unsigned long ip) |
| 2614 | { |
| 2615 | struct ftrace_func_entry *entry; |
| 2616 | |
| 2617 | entry = __ftrace_lookup_ip(hash: direct_functions, ip); |
| 2618 | if (!entry) |
| 2619 | return 0; |
| 2620 | |
| 2621 | return entry->direct; |
| 2622 | } |
| 2623 | |
| 2624 | static void call_direct_funcs(unsigned long ip, unsigned long pip, |
| 2625 | struct ftrace_ops *ops, struct ftrace_regs *fregs) |
| 2626 | { |
| 2627 | unsigned long addr = READ_ONCE(ops->direct_call); |
| 2628 | |
| 2629 | if (!addr) |
| 2630 | return; |
| 2631 | |
| 2632 | arch_ftrace_set_direct_caller(fregs, addr); |
| 2633 | } |
| 2634 | #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */ |
| 2635 | |
| 2636 | /** |
| 2637 | * ftrace_get_addr_new - Get the call address to set to |
| 2638 | * @rec: The ftrace record descriptor |
| 2639 | * |
| 2640 | * If the record has the FTRACE_FL_REGS set, that means that it |
| 2641 | * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS |
| 2642 | * is not set, then it wants to convert to the normal callback. |
| 2643 | * |
| 2644 | * Returns: the address of the trampoline to set to |
| 2645 | */ |
| 2646 | unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec) |
| 2647 | { |
| 2648 | struct ftrace_ops *ops; |
| 2649 | unsigned long addr; |
| 2650 | |
| 2651 | if ((rec->flags & FTRACE_FL_DIRECT) && |
| 2652 | (ftrace_rec_count(rec) == 1)) { |
| 2653 | addr = ftrace_find_rec_direct(ip: rec->ip); |
| 2654 | if (addr) |
| 2655 | return addr; |
| 2656 | WARN_ON_ONCE(1); |
| 2657 | } |
| 2658 | |
| 2659 | /* Trampolines take precedence over regs */ |
| 2660 | if (rec->flags & FTRACE_FL_TRAMP) { |
| 2661 | ops = ftrace_find_tramp_ops_new(rec); |
| 2662 | if (FTRACE_WARN_ON(!ops || !ops->trampoline)) { |
| 2663 | pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n" , |
| 2664 | (void *)rec->ip, (void *)rec->ip, rec->flags); |
| 2665 | /* Ftrace is shutting down, return anything */ |
| 2666 | return (unsigned long)FTRACE_ADDR; |
| 2667 | } |
| 2668 | return ops->trampoline; |
| 2669 | } |
| 2670 | |
| 2671 | if (rec->flags & FTRACE_FL_REGS) |
| 2672 | return (unsigned long)FTRACE_REGS_ADDR; |
| 2673 | else |
| 2674 | return (unsigned long)FTRACE_ADDR; |
| 2675 | } |
| 2676 | |
| 2677 | /** |
| 2678 | * ftrace_get_addr_curr - Get the call address that is already there |
| 2679 | * @rec: The ftrace record descriptor |
| 2680 | * |
| 2681 | * The FTRACE_FL_REGS_EN is set when the record already points to |
| 2682 | * a function that saves all the regs. Basically the '_EN' version |
| 2683 | * represents the current state of the function. |
| 2684 | * |
| 2685 | * Returns: the address of the trampoline that is currently being called |
| 2686 | */ |
| 2687 | unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec) |
| 2688 | { |
| 2689 | struct ftrace_ops *ops; |
| 2690 | unsigned long addr; |
| 2691 | |
| 2692 | /* Direct calls take precedence over trampolines */ |
| 2693 | if (rec->flags & FTRACE_FL_DIRECT_EN) { |
| 2694 | addr = ftrace_find_rec_direct(ip: rec->ip); |
| 2695 | if (addr) |
| 2696 | return addr; |
| 2697 | WARN_ON_ONCE(1); |
| 2698 | } |
| 2699 | |
| 2700 | /* Trampolines take precedence over regs */ |
| 2701 | if (rec->flags & FTRACE_FL_TRAMP_EN) { |
| 2702 | ops = ftrace_find_tramp_ops_curr(rec); |
| 2703 | if (FTRACE_WARN_ON(!ops)) { |
| 2704 | pr_warn("Bad trampoline accounting at: %p (%pS)\n" , |
| 2705 | (void *)rec->ip, (void *)rec->ip); |
| 2706 | /* Ftrace is shutting down, return anything */ |
| 2707 | return (unsigned long)FTRACE_ADDR; |
| 2708 | } |
| 2709 | return ops->trampoline; |
| 2710 | } |
| 2711 | |
| 2712 | if (rec->flags & FTRACE_FL_REGS_EN) |
| 2713 | return (unsigned long)FTRACE_REGS_ADDR; |
| 2714 | else |
| 2715 | return (unsigned long)FTRACE_ADDR; |
| 2716 | } |
| 2717 | |
| 2718 | static int |
| 2719 | __ftrace_replace_code(struct dyn_ftrace *rec, bool enable) |
| 2720 | { |
| 2721 | unsigned long ftrace_old_addr; |
| 2722 | unsigned long ftrace_addr; |
| 2723 | int ret; |
| 2724 | |
| 2725 | ftrace_addr = ftrace_get_addr_new(rec); |
| 2726 | |
| 2727 | /* This needs to be done before we call ftrace_update_record */ |
| 2728 | ftrace_old_addr = ftrace_get_addr_curr(rec); |
| 2729 | |
| 2730 | ret = ftrace_update_record(rec, enable); |
| 2731 | |
| 2732 | ftrace_bug_type = FTRACE_BUG_UNKNOWN; |
| 2733 | |
| 2734 | switch (ret) { |
| 2735 | case FTRACE_UPDATE_IGNORE: |
| 2736 | return 0; |
| 2737 | |
| 2738 | case FTRACE_UPDATE_MAKE_CALL: |
| 2739 | ftrace_bug_type = FTRACE_BUG_CALL; |
| 2740 | return ftrace_make_call(rec, addr: ftrace_addr); |
| 2741 | |
| 2742 | case FTRACE_UPDATE_MAKE_NOP: |
| 2743 | ftrace_bug_type = FTRACE_BUG_NOP; |
| 2744 | return ftrace_make_nop(NULL, rec, addr: ftrace_old_addr); |
| 2745 | |
| 2746 | case FTRACE_UPDATE_MODIFY_CALL: |
| 2747 | ftrace_bug_type = FTRACE_BUG_UPDATE; |
| 2748 | return ftrace_modify_call(rec, old_addr: ftrace_old_addr, addr: ftrace_addr); |
| 2749 | } |
| 2750 | |
| 2751 | return -1; /* unknown ftrace bug */ |
| 2752 | } |
| 2753 | |
| 2754 | void __weak ftrace_replace_code(int mod_flags) |
| 2755 | { |
| 2756 | struct dyn_ftrace *rec; |
| 2757 | struct ftrace_page *pg; |
| 2758 | bool enable = mod_flags & FTRACE_MODIFY_ENABLE_FL; |
| 2759 | int schedulable = mod_flags & FTRACE_MODIFY_MAY_SLEEP_FL; |
| 2760 | int failed; |
| 2761 | |
| 2762 | if (unlikely(ftrace_disabled)) |
| 2763 | return; |
| 2764 | |
| 2765 | do_for_each_ftrace_rec(pg, rec) { |
| 2766 | |
| 2767 | if (skip_record(rec)) |
| 2768 | continue; |
| 2769 | |
| 2770 | failed = __ftrace_replace_code(rec, enable); |
| 2771 | if (failed) { |
| 2772 | ftrace_bug(failed, rec); |
| 2773 | /* Stop processing */ |
| 2774 | return; |
| 2775 | } |
| 2776 | if (schedulable) |
| 2777 | cond_resched(); |
| 2778 | } while_for_each_ftrace_rec(); |
| 2779 | } |
| 2780 | |
| 2781 | struct ftrace_rec_iter { |
| 2782 | struct ftrace_page *pg; |
| 2783 | int index; |
| 2784 | }; |
| 2785 | |
| 2786 | /** |
| 2787 | * ftrace_rec_iter_start - start up iterating over traced functions |
| 2788 | * |
| 2789 | * Returns: an iterator handle that is used to iterate over all |
| 2790 | * the records that represent address locations where functions |
| 2791 | * are traced. |
| 2792 | * |
| 2793 | * May return NULL if no records are available. |
| 2794 | */ |
| 2795 | struct ftrace_rec_iter *ftrace_rec_iter_start(void) |
| 2796 | { |
| 2797 | /* |
| 2798 | * We only use a single iterator. |
| 2799 | * Protected by the ftrace_lock mutex. |
| 2800 | */ |
| 2801 | static struct ftrace_rec_iter ftrace_rec_iter; |
| 2802 | struct ftrace_rec_iter *iter = &ftrace_rec_iter; |
| 2803 | |
| 2804 | iter->pg = ftrace_pages_start; |
| 2805 | iter->index = 0; |
| 2806 | |
| 2807 | /* Could have empty pages */ |
| 2808 | while (iter->pg && !iter->pg->index) |
| 2809 | iter->pg = iter->pg->next; |
| 2810 | |
| 2811 | if (!iter->pg) |
| 2812 | return NULL; |
| 2813 | |
| 2814 | return iter; |
| 2815 | } |
| 2816 | |
| 2817 | /** |
| 2818 | * ftrace_rec_iter_next - get the next record to process. |
| 2819 | * @iter: The handle to the iterator. |
| 2820 | * |
| 2821 | * Returns: the next iterator after the given iterator @iter. |
| 2822 | */ |
| 2823 | struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter) |
| 2824 | { |
| 2825 | iter->index++; |
| 2826 | |
| 2827 | if (iter->index >= iter->pg->index) { |
| 2828 | iter->pg = iter->pg->next; |
| 2829 | iter->index = 0; |
| 2830 | |
| 2831 | /* Could have empty pages */ |
| 2832 | while (iter->pg && !iter->pg->index) |
| 2833 | iter->pg = iter->pg->next; |
| 2834 | } |
| 2835 | |
| 2836 | if (!iter->pg) |
| 2837 | return NULL; |
| 2838 | |
| 2839 | return iter; |
| 2840 | } |
| 2841 | |
| 2842 | /** |
| 2843 | * ftrace_rec_iter_record - get the record at the iterator location |
| 2844 | * @iter: The current iterator location |
| 2845 | * |
| 2846 | * Returns: the record that the current @iter is at. |
| 2847 | */ |
| 2848 | struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter) |
| 2849 | { |
| 2850 | return &iter->pg->records[iter->index]; |
| 2851 | } |
| 2852 | |
| 2853 | static int |
| 2854 | ftrace_nop_initialize(struct module *mod, struct dyn_ftrace *rec) |
| 2855 | { |
| 2856 | int ret; |
| 2857 | |
| 2858 | if (unlikely(ftrace_disabled)) |
| 2859 | return 0; |
| 2860 | |
| 2861 | ret = ftrace_init_nop(mod, rec); |
| 2862 | if (ret) { |
| 2863 | ftrace_bug_type = FTRACE_BUG_INIT; |
| 2864 | ftrace_bug(failed: ret, rec); |
| 2865 | return 0; |
| 2866 | } |
| 2867 | return 1; |
| 2868 | } |
| 2869 | |
| 2870 | /* |
| 2871 | * archs can override this function if they must do something |
| 2872 | * before the modifying code is performed. |
| 2873 | */ |
| 2874 | void __weak ftrace_arch_code_modify_prepare(void) |
| 2875 | { |
| 2876 | } |
| 2877 | |
| 2878 | /* |
| 2879 | * archs can override this function if they must do something |
| 2880 | * after the modifying code is performed. |
| 2881 | */ |
| 2882 | void __weak ftrace_arch_code_modify_post_process(void) |
| 2883 | { |
| 2884 | } |
| 2885 | |
| 2886 | static int update_ftrace_func(ftrace_func_t func) |
| 2887 | { |
| 2888 | static ftrace_func_t save_func; |
| 2889 | |
| 2890 | /* Avoid updating if it hasn't changed */ |
| 2891 | if (func == save_func) |
| 2892 | return 0; |
| 2893 | |
| 2894 | save_func = func; |
| 2895 | |
| 2896 | return ftrace_update_ftrace_func(func); |
| 2897 | } |
| 2898 | |
| 2899 | void ftrace_modify_all_code(int command) |
| 2900 | { |
| 2901 | int update = command & FTRACE_UPDATE_TRACE_FUNC; |
| 2902 | int mod_flags = 0; |
| 2903 | int err = 0; |
| 2904 | |
| 2905 | if (command & FTRACE_MAY_SLEEP) |
| 2906 | mod_flags = FTRACE_MODIFY_MAY_SLEEP_FL; |
| 2907 | |
| 2908 | /* |
| 2909 | * If the ftrace_caller calls a ftrace_ops func directly, |
| 2910 | * we need to make sure that it only traces functions it |
| 2911 | * expects to trace. When doing the switch of functions, |
| 2912 | * we need to update to the ftrace_ops_list_func first |
| 2913 | * before the transition between old and new calls are set, |
| 2914 | * as the ftrace_ops_list_func will check the ops hashes |
| 2915 | * to make sure the ops are having the right functions |
| 2916 | * traced. |
| 2917 | */ |
| 2918 | if (update) { |
| 2919 | err = update_ftrace_func(func: ftrace_ops_list_func); |
| 2920 | if (FTRACE_WARN_ON(err)) |
| 2921 | return; |
| 2922 | } |
| 2923 | |
| 2924 | if (command & FTRACE_UPDATE_CALLS) |
| 2925 | ftrace_replace_code(mod_flags: mod_flags | FTRACE_MODIFY_ENABLE_FL); |
| 2926 | else if (command & FTRACE_DISABLE_CALLS) |
| 2927 | ftrace_replace_code(mod_flags); |
| 2928 | |
| 2929 | if (update && ftrace_trace_function != ftrace_ops_list_func) { |
| 2930 | function_trace_op = set_function_trace_op; |
| 2931 | smp_wmb(); |
| 2932 | /* If irqs are disabled, we are in stop machine */ |
| 2933 | if (!irqs_disabled()) |
| 2934 | smp_call_function(func: ftrace_sync_ipi, NULL, wait: 1); |
| 2935 | err = update_ftrace_func(func: ftrace_trace_function); |
| 2936 | if (FTRACE_WARN_ON(err)) |
| 2937 | return; |
| 2938 | } |
| 2939 | |
| 2940 | if (command & FTRACE_START_FUNC_RET) |
| 2941 | err = ftrace_enable_ftrace_graph_caller(); |
| 2942 | else if (command & FTRACE_STOP_FUNC_RET) |
| 2943 | err = ftrace_disable_ftrace_graph_caller(); |
| 2944 | FTRACE_WARN_ON(err); |
| 2945 | } |
| 2946 | |
| 2947 | static int __ftrace_modify_code(void *data) |
| 2948 | { |
| 2949 | int *command = data; |
| 2950 | |
| 2951 | ftrace_modify_all_code(command: *command); |
| 2952 | |
| 2953 | return 0; |
| 2954 | } |
| 2955 | |
| 2956 | /** |
| 2957 | * ftrace_run_stop_machine - go back to the stop machine method |
| 2958 | * @command: The command to tell ftrace what to do |
| 2959 | * |
| 2960 | * If an arch needs to fall back to the stop machine method, the |
| 2961 | * it can call this function. |
| 2962 | */ |
| 2963 | void ftrace_run_stop_machine(int command) |
| 2964 | { |
| 2965 | stop_machine(fn: __ftrace_modify_code, data: &command, NULL); |
| 2966 | } |
| 2967 | |
| 2968 | /** |
| 2969 | * arch_ftrace_update_code - modify the code to trace or not trace |
| 2970 | * @command: The command that needs to be done |
| 2971 | * |
| 2972 | * Archs can override this function if it does not need to |
| 2973 | * run stop_machine() to modify code. |
| 2974 | */ |
| 2975 | void __weak arch_ftrace_update_code(int command) |
| 2976 | { |
| 2977 | ftrace_run_stop_machine(command); |
| 2978 | } |
| 2979 | |
| 2980 | static void ftrace_run_update_code(int command) |
| 2981 | { |
| 2982 | ftrace_arch_code_modify_prepare(); |
| 2983 | |
| 2984 | /* |
| 2985 | * By default we use stop_machine() to modify the code. |
| 2986 | * But archs can do what ever they want as long as it |
| 2987 | * is safe. The stop_machine() is the safest, but also |
| 2988 | * produces the most overhead. |
| 2989 | */ |
| 2990 | arch_ftrace_update_code(command); |
| 2991 | |
| 2992 | ftrace_arch_code_modify_post_process(); |
| 2993 | } |
| 2994 | |
| 2995 | static void ftrace_run_modify_code(struct ftrace_ops *ops, int command, |
| 2996 | struct ftrace_ops_hash *old_hash) |
| 2997 | { |
| 2998 | ops->flags |= FTRACE_OPS_FL_MODIFYING; |
| 2999 | ops->old_hash.filter_hash = old_hash->filter_hash; |
| 3000 | ops->old_hash.notrace_hash = old_hash->notrace_hash; |
| 3001 | ftrace_run_update_code(command); |
| 3002 | ops->old_hash.filter_hash = NULL; |
| 3003 | ops->old_hash.notrace_hash = NULL; |
| 3004 | ops->flags &= ~FTRACE_OPS_FL_MODIFYING; |
| 3005 | } |
| 3006 | |
| 3007 | static ftrace_func_t saved_ftrace_func; |
| 3008 | static int ftrace_start_up; |
| 3009 | |
| 3010 | void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops) |
| 3011 | { |
| 3012 | } |
| 3013 | |
| 3014 | /* List of trace_ops that have allocated trampolines */ |
| 3015 | static LIST_HEAD(ftrace_ops_trampoline_list); |
| 3016 | |
| 3017 | static void ftrace_add_trampoline_to_kallsyms(struct ftrace_ops *ops) |
| 3018 | { |
| 3019 | lockdep_assert_held(&ftrace_lock); |
| 3020 | list_add_rcu(new: &ops->list, head: &ftrace_ops_trampoline_list); |
| 3021 | } |
| 3022 | |
| 3023 | static void ftrace_remove_trampoline_from_kallsyms(struct ftrace_ops *ops) |
| 3024 | { |
| 3025 | lockdep_assert_held(&ftrace_lock); |
| 3026 | list_del_rcu(entry: &ops->list); |
| 3027 | synchronize_rcu(); |
| 3028 | } |
| 3029 | |
| 3030 | /* |
| 3031 | * "__builtin__ftrace" is used as a module name in /proc/kallsyms for symbols |
| 3032 | * for pages allocated for ftrace purposes, even though "__builtin__ftrace" is |
| 3033 | * not a module. |
| 3034 | */ |
| 3035 | #define FTRACE_TRAMPOLINE_MOD "__builtin__ftrace" |
| 3036 | #define FTRACE_TRAMPOLINE_SYM "ftrace_trampoline" |
| 3037 | |
| 3038 | static void ftrace_trampoline_free(struct ftrace_ops *ops) |
| 3039 | { |
| 3040 | if (ops && (ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP) && |
| 3041 | ops->trampoline) { |
| 3042 | /* |
| 3043 | * Record the text poke event before the ksymbol unregister |
| 3044 | * event. |
| 3045 | */ |
| 3046 | perf_event_text_poke(addr: (void *)ops->trampoline, |
| 3047 | old_bytes: (void *)ops->trampoline, |
| 3048 | old_len: ops->trampoline_size, NULL, new_len: 0); |
| 3049 | perf_event_ksymbol(ksym_type: PERF_RECORD_KSYMBOL_TYPE_OOL, |
| 3050 | addr: ops->trampoline, len: ops->trampoline_size, |
| 3051 | unregister: true, FTRACE_TRAMPOLINE_SYM); |
| 3052 | /* Remove from kallsyms after the perf events */ |
| 3053 | ftrace_remove_trampoline_from_kallsyms(ops); |
| 3054 | } |
| 3055 | |
| 3056 | arch_ftrace_trampoline_free(ops); |
| 3057 | } |
| 3058 | |
| 3059 | static void ftrace_startup_enable(int command) |
| 3060 | { |
| 3061 | if (saved_ftrace_func != ftrace_trace_function) { |
| 3062 | saved_ftrace_func = ftrace_trace_function; |
| 3063 | command |= FTRACE_UPDATE_TRACE_FUNC; |
| 3064 | } |
| 3065 | |
| 3066 | if (!command || !ftrace_enabled) |
| 3067 | return; |
| 3068 | |
| 3069 | ftrace_run_update_code(command); |
| 3070 | } |
| 3071 | |
| 3072 | static void ftrace_startup_all(int command) |
| 3073 | { |
| 3074 | update_all_ops = true; |
| 3075 | ftrace_startup_enable(command); |
| 3076 | update_all_ops = false; |
| 3077 | } |
| 3078 | |
| 3079 | int ftrace_startup(struct ftrace_ops *ops, int command) |
| 3080 | { |
| 3081 | int ret; |
| 3082 | |
| 3083 | if (unlikely(ftrace_disabled)) |
| 3084 | return -ENODEV; |
| 3085 | |
| 3086 | ret = __register_ftrace_function(ops); |
| 3087 | if (ret) |
| 3088 | return ret; |
| 3089 | |
| 3090 | ftrace_start_up++; |
| 3091 | |
| 3092 | /* |
| 3093 | * Note that ftrace probes uses this to start up |
| 3094 | * and modify functions it will probe. But we still |
| 3095 | * set the ADDING flag for modification, as probes |
| 3096 | * do not have trampolines. If they add them in the |
| 3097 | * future, then the probes will need to distinguish |
| 3098 | * between adding and updating probes. |
| 3099 | */ |
| 3100 | ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING; |
| 3101 | |
| 3102 | ret = ftrace_hash_ipmodify_enable(ops); |
| 3103 | if (ret < 0) { |
| 3104 | /* Rollback registration process */ |
| 3105 | __unregister_ftrace_function(ops); |
| 3106 | ftrace_start_up--; |
| 3107 | ops->flags &= ~FTRACE_OPS_FL_ENABLED; |
| 3108 | if (ops->flags & FTRACE_OPS_FL_DYNAMIC) |
| 3109 | ftrace_trampoline_free(ops); |
| 3110 | return ret; |
| 3111 | } |
| 3112 | |
| 3113 | if (ftrace_hash_rec_enable(ops)) |
| 3114 | command |= FTRACE_UPDATE_CALLS; |
| 3115 | |
| 3116 | ftrace_startup_enable(command); |
| 3117 | |
| 3118 | /* |
| 3119 | * If ftrace is in an undefined state, we just remove ops from list |
| 3120 | * to prevent the NULL pointer, instead of totally rolling it back and |
| 3121 | * free trampoline, because those actions could cause further damage. |
| 3122 | */ |
| 3123 | if (unlikely(ftrace_disabled)) { |
| 3124 | __unregister_ftrace_function(ops); |
| 3125 | return -ENODEV; |
| 3126 | } |
| 3127 | |
| 3128 | ops->flags &= ~FTRACE_OPS_FL_ADDING; |
| 3129 | |
| 3130 | return 0; |
| 3131 | } |
| 3132 | |
| 3133 | int ftrace_shutdown(struct ftrace_ops *ops, int command) |
| 3134 | { |
| 3135 | int ret; |
| 3136 | |
| 3137 | if (unlikely(ftrace_disabled)) |
| 3138 | return -ENODEV; |
| 3139 | |
| 3140 | ret = __unregister_ftrace_function(ops); |
| 3141 | if (ret) |
| 3142 | return ret; |
| 3143 | |
| 3144 | ftrace_start_up--; |
| 3145 | /* |
| 3146 | * Just warn in case of unbalance, no need to kill ftrace, it's not |
| 3147 | * critical but the ftrace_call callers may be never nopped again after |
| 3148 | * further ftrace uses. |
| 3149 | */ |
| 3150 | WARN_ON_ONCE(ftrace_start_up < 0); |
| 3151 | |
| 3152 | /* Disabling ipmodify never fails */ |
| 3153 | ftrace_hash_ipmodify_disable(ops); |
| 3154 | |
| 3155 | if (ftrace_hash_rec_disable(ops)) |
| 3156 | command |= FTRACE_UPDATE_CALLS; |
| 3157 | |
| 3158 | ops->flags &= ~FTRACE_OPS_FL_ENABLED; |
| 3159 | |
| 3160 | if (saved_ftrace_func != ftrace_trace_function) { |
| 3161 | saved_ftrace_func = ftrace_trace_function; |
| 3162 | command |= FTRACE_UPDATE_TRACE_FUNC; |
| 3163 | } |
| 3164 | |
| 3165 | if (!command || !ftrace_enabled) |
| 3166 | goto out; |
| 3167 | |
| 3168 | /* |
| 3169 | * If the ops uses a trampoline, then it needs to be |
| 3170 | * tested first on update. |
| 3171 | */ |
| 3172 | ops->flags |= FTRACE_OPS_FL_REMOVING; |
| 3173 | removed_ops = ops; |
| 3174 | |
| 3175 | /* The trampoline logic checks the old hashes */ |
| 3176 | ops->old_hash.filter_hash = ops->func_hash->filter_hash; |
| 3177 | ops->old_hash.notrace_hash = ops->func_hash->notrace_hash; |
| 3178 | |
| 3179 | ftrace_run_update_code(command); |
| 3180 | |
| 3181 | /* |
| 3182 | * If there's no more ops registered with ftrace, run a |
| 3183 | * sanity check to make sure all rec flags are cleared. |
| 3184 | */ |
| 3185 | if (rcu_dereference_protected(ftrace_ops_list, |
| 3186 | lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) { |
| 3187 | struct ftrace_page *pg; |
| 3188 | struct dyn_ftrace *rec; |
| 3189 | |
| 3190 | do_for_each_ftrace_rec(pg, rec) { |
| 3191 | if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_NOCLEAR_FLAGS)) |
| 3192 | pr_warn(" %pS flags:%lx\n" , |
| 3193 | (void *)rec->ip, rec->flags); |
| 3194 | } while_for_each_ftrace_rec(); |
| 3195 | } |
| 3196 | |
| 3197 | ops->old_hash.filter_hash = NULL; |
| 3198 | ops->old_hash.notrace_hash = NULL; |
| 3199 | |
| 3200 | removed_ops = NULL; |
| 3201 | ops->flags &= ~FTRACE_OPS_FL_REMOVING; |
| 3202 | |
| 3203 | out: |
| 3204 | /* |
| 3205 | * Dynamic ops may be freed, we must make sure that all |
| 3206 | * callers are done before leaving this function. |
| 3207 | */ |
| 3208 | if (ops->flags & FTRACE_OPS_FL_DYNAMIC) { |
| 3209 | /* |
| 3210 | * We need to do a hard force of sched synchronization. |
| 3211 | * This is because we use preempt_disable() to do RCU, but |
| 3212 | * the function tracers can be called where RCU is not watching |
| 3213 | * (like before user_exit()). We can not rely on the RCU |
| 3214 | * infrastructure to do the synchronization, thus we must do it |
| 3215 | * ourselves. |
| 3216 | */ |
| 3217 | synchronize_rcu_tasks_rude(); |
| 3218 | |
| 3219 | /* |
| 3220 | * When the kernel is preemptive, tasks can be preempted |
| 3221 | * while on a ftrace trampoline. Just scheduling a task on |
| 3222 | * a CPU is not good enough to flush them. Calling |
| 3223 | * synchronize_rcu_tasks() will wait for those tasks to |
| 3224 | * execute and either schedule voluntarily or enter user space. |
| 3225 | */ |
| 3226 | synchronize_rcu_tasks(); |
| 3227 | |
| 3228 | ftrace_trampoline_free(ops); |
| 3229 | } |
| 3230 | |
| 3231 | return 0; |
| 3232 | } |
| 3233 | |
| 3234 | /* Simply make a copy of @src and return it */ |
| 3235 | static struct ftrace_hash *copy_hash(struct ftrace_hash *src) |
| 3236 | { |
| 3237 | if (ftrace_hash_empty(hash: src)) |
| 3238 | return EMPTY_HASH; |
| 3239 | |
| 3240 | return alloc_and_copy_ftrace_hash(size_bits: src->size_bits, hash: src); |
| 3241 | } |
| 3242 | |
| 3243 | /* |
| 3244 | * Append @new_hash entries to @hash: |
| 3245 | * |
| 3246 | * If @hash is the EMPTY_HASH then it traces all functions and nothing |
| 3247 | * needs to be done. |
| 3248 | * |
| 3249 | * If @new_hash is the EMPTY_HASH, then make *hash the EMPTY_HASH so |
| 3250 | * that it traces everything. |
| 3251 | * |
| 3252 | * Otherwise, go through all of @new_hash and add anything that @hash |
| 3253 | * doesn't already have, to @hash. |
| 3254 | * |
| 3255 | * The filter_hash updates uses just the append_hash() function |
| 3256 | * and the notrace_hash does not. |
| 3257 | */ |
| 3258 | static int append_hash(struct ftrace_hash **hash, struct ftrace_hash *new_hash, |
| 3259 | int size_bits) |
| 3260 | { |
| 3261 | struct ftrace_func_entry *entry; |
| 3262 | int size; |
| 3263 | int i; |
| 3264 | |
| 3265 | if (*hash) { |
| 3266 | /* An empty hash does everything */ |
| 3267 | if (ftrace_hash_empty(hash: *hash)) |
| 3268 | return 0; |
| 3269 | } else { |
| 3270 | *hash = alloc_ftrace_hash(size_bits); |
| 3271 | if (!*hash) |
| 3272 | return -ENOMEM; |
| 3273 | } |
| 3274 | |
| 3275 | /* If new_hash has everything make hash have everything */ |
| 3276 | if (ftrace_hash_empty(hash: new_hash)) { |
| 3277 | free_ftrace_hash(hash: *hash); |
| 3278 | *hash = EMPTY_HASH; |
| 3279 | return 0; |
| 3280 | } |
| 3281 | |
| 3282 | size = 1 << new_hash->size_bits; |
| 3283 | for (i = 0; i < size; i++) { |
| 3284 | hlist_for_each_entry(entry, &new_hash->buckets[i], hlist) { |
| 3285 | /* Only add if not already in hash */ |
| 3286 | if (!__ftrace_lookup_ip(hash: *hash, ip: entry->ip) && |
| 3287 | add_hash_entry(hash: *hash, ip: entry->ip) == NULL) |
| 3288 | return -ENOMEM; |
| 3289 | } |
| 3290 | } |
| 3291 | return 0; |
| 3292 | } |
| 3293 | |
| 3294 | /* |
| 3295 | * Remove functions from @hash that are in @notrace_hash |
| 3296 | */ |
| 3297 | static void remove_hash(struct ftrace_hash *hash, struct ftrace_hash *notrace_hash) |
| 3298 | { |
| 3299 | struct ftrace_func_entry *entry; |
| 3300 | struct hlist_node *tmp; |
| 3301 | int size; |
| 3302 | int i; |
| 3303 | |
| 3304 | /* If the notrace hash is empty, there's nothing to do */ |
| 3305 | if (ftrace_hash_empty(hash: notrace_hash)) |
| 3306 | return; |
| 3307 | |
| 3308 | size = 1 << hash->size_bits; |
| 3309 | for (i = 0; i < size; i++) { |
| 3310 | hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) { |
| 3311 | if (!__ftrace_lookup_ip(hash: notrace_hash, ip: entry->ip)) |
| 3312 | continue; |
| 3313 | remove_hash_entry(hash, entry); |
| 3314 | kfree(objp: entry); |
| 3315 | } |
| 3316 | } |
| 3317 | } |
| 3318 | |
| 3319 | /* |
| 3320 | * Add to @hash only those that are in both @new_hash1 and @new_hash2 |
| 3321 | * |
| 3322 | * The notrace_hash updates uses just the intersect_hash() function |
| 3323 | * and the filter_hash does not. |
| 3324 | */ |
| 3325 | static int intersect_hash(struct ftrace_hash **hash, struct ftrace_hash *new_hash1, |
| 3326 | struct ftrace_hash *new_hash2) |
| 3327 | { |
| 3328 | struct ftrace_func_entry *entry; |
| 3329 | int size; |
| 3330 | int i; |
| 3331 | |
| 3332 | /* |
| 3333 | * If new_hash1 or new_hash2 is the EMPTY_HASH then make the hash |
| 3334 | * empty as well as empty for notrace means none are notraced. |
| 3335 | */ |
| 3336 | if (ftrace_hash_empty(hash: new_hash1) || ftrace_hash_empty(hash: new_hash2)) { |
| 3337 | free_ftrace_hash(hash: *hash); |
| 3338 | *hash = EMPTY_HASH; |
| 3339 | return 0; |
| 3340 | } |
| 3341 | |
| 3342 | size = 1 << new_hash1->size_bits; |
| 3343 | for (i = 0; i < size; i++) { |
| 3344 | hlist_for_each_entry(entry, &new_hash1->buckets[i], hlist) { |
| 3345 | /* Only add if in both @new_hash1 and @new_hash2 */ |
| 3346 | if (__ftrace_lookup_ip(hash: new_hash2, ip: entry->ip) && |
| 3347 | add_hash_entry(hash: *hash, ip: entry->ip) == NULL) |
| 3348 | return -ENOMEM; |
| 3349 | } |
| 3350 | } |
| 3351 | /* If nothing intersects, make it the empty set */ |
| 3352 | if (ftrace_hash_empty(hash: *hash)) { |
| 3353 | free_ftrace_hash(hash: *hash); |
| 3354 | *hash = EMPTY_HASH; |
| 3355 | } |
| 3356 | return 0; |
| 3357 | } |
| 3358 | |
| 3359 | static bool ops_equal(struct ftrace_hash *A, struct ftrace_hash *B) |
| 3360 | { |
| 3361 | struct ftrace_func_entry *entry; |
| 3362 | int size; |
| 3363 | int i; |
| 3364 | |
| 3365 | if (ftrace_hash_empty(hash: A)) |
| 3366 | return ftrace_hash_empty(hash: B); |
| 3367 | |
| 3368 | if (ftrace_hash_empty(hash: B)) |
| 3369 | return ftrace_hash_empty(hash: A); |
| 3370 | |
| 3371 | if (A->count != B->count) |
| 3372 | return false; |
| 3373 | |
| 3374 | size = 1 << A->size_bits; |
| 3375 | for (i = 0; i < size; i++) { |
| 3376 | hlist_for_each_entry(entry, &A->buckets[i], hlist) { |
| 3377 | if (!__ftrace_lookup_ip(hash: B, ip: entry->ip)) |
| 3378 | return false; |
| 3379 | } |
| 3380 | } |
| 3381 | |
| 3382 | return true; |
| 3383 | } |
| 3384 | |
| 3385 | static void ftrace_ops_update_code(struct ftrace_ops *ops, |
| 3386 | struct ftrace_ops_hash *old_hash); |
| 3387 | |
| 3388 | static int __ftrace_hash_move_and_update_ops(struct ftrace_ops *ops, |
| 3389 | struct ftrace_hash **orig_hash, |
| 3390 | struct ftrace_hash *hash, |
| 3391 | int enable) |
| 3392 | { |
| 3393 | struct ftrace_ops_hash old_hash_ops; |
| 3394 | struct ftrace_hash *old_hash; |
| 3395 | int ret; |
| 3396 | |
| 3397 | old_hash = *orig_hash; |
| 3398 | old_hash_ops.filter_hash = ops->func_hash->filter_hash; |
| 3399 | old_hash_ops.notrace_hash = ops->func_hash->notrace_hash; |
| 3400 | ret = ftrace_hash_move(ops, enable, dst: orig_hash, src: hash); |
| 3401 | if (!ret) { |
| 3402 | ftrace_ops_update_code(ops, old_hash: &old_hash_ops); |
| 3403 | free_ftrace_hash_rcu(hash: old_hash); |
| 3404 | } |
| 3405 | return ret; |
| 3406 | } |
| 3407 | |
| 3408 | static int ftrace_update_ops(struct ftrace_ops *ops, struct ftrace_hash *filter_hash, |
| 3409 | struct ftrace_hash *notrace_hash) |
| 3410 | { |
| 3411 | int ret; |
| 3412 | |
| 3413 | if (!ops_equal(A: filter_hash, B: ops->func_hash->filter_hash)) { |
| 3414 | ret = __ftrace_hash_move_and_update_ops(ops, orig_hash: &ops->func_hash->filter_hash, |
| 3415 | hash: filter_hash, enable: 1); |
| 3416 | if (ret < 0) |
| 3417 | return ret; |
| 3418 | } |
| 3419 | |
| 3420 | if (!ops_equal(A: notrace_hash, B: ops->func_hash->notrace_hash)) { |
| 3421 | ret = __ftrace_hash_move_and_update_ops(ops, orig_hash: &ops->func_hash->notrace_hash, |
| 3422 | hash: notrace_hash, enable: 0); |
| 3423 | if (ret < 0) |
| 3424 | return ret; |
| 3425 | } |
| 3426 | |
| 3427 | return 0; |
| 3428 | } |
| 3429 | |
| 3430 | static int add_first_hash(struct ftrace_hash **filter_hash, struct ftrace_hash **notrace_hash, |
| 3431 | struct ftrace_ops_hash *func_hash) |
| 3432 | { |
| 3433 | /* If the filter hash is not empty, simply remove the nohash from it */ |
| 3434 | if (!ftrace_hash_empty(hash: func_hash->filter_hash)) { |
| 3435 | *filter_hash = copy_hash(src: func_hash->filter_hash); |
| 3436 | if (!*filter_hash) |
| 3437 | return -ENOMEM; |
| 3438 | remove_hash(hash: *filter_hash, notrace_hash: func_hash->notrace_hash); |
| 3439 | *notrace_hash = EMPTY_HASH; |
| 3440 | |
| 3441 | } else { |
| 3442 | *notrace_hash = copy_hash(src: func_hash->notrace_hash); |
| 3443 | if (!*notrace_hash) |
| 3444 | return -ENOMEM; |
| 3445 | *filter_hash = EMPTY_HASH; |
| 3446 | } |
| 3447 | return 0; |
| 3448 | } |
| 3449 | |
| 3450 | static int add_next_hash(struct ftrace_hash **filter_hash, struct ftrace_hash **notrace_hash, |
| 3451 | struct ftrace_ops_hash *ops_hash, struct ftrace_ops_hash *subops_hash) |
| 3452 | { |
| 3453 | int size_bits; |
| 3454 | int ret; |
| 3455 | |
| 3456 | /* If the subops trace all functions so must the main ops */ |
| 3457 | if (ftrace_hash_empty(hash: ops_hash->filter_hash) || |
| 3458 | ftrace_hash_empty(hash: subops_hash->filter_hash)) { |
| 3459 | *filter_hash = EMPTY_HASH; |
| 3460 | } else { |
| 3461 | /* |
| 3462 | * The main ops filter hash is not empty, so its |
| 3463 | * notrace_hash had better be, as the notrace hash |
| 3464 | * is only used for empty main filter hashes. |
| 3465 | */ |
| 3466 | WARN_ON_ONCE(!ftrace_hash_empty(ops_hash->notrace_hash)); |
| 3467 | |
| 3468 | size_bits = max(ops_hash->filter_hash->size_bits, |
| 3469 | subops_hash->filter_hash->size_bits); |
| 3470 | |
| 3471 | /* Copy the subops hash */ |
| 3472 | *filter_hash = alloc_and_copy_ftrace_hash(size_bits, hash: subops_hash->filter_hash); |
| 3473 | if (!*filter_hash) |
| 3474 | return -ENOMEM; |
| 3475 | /* Remove any notrace functions from the copy */ |
| 3476 | remove_hash(hash: *filter_hash, notrace_hash: subops_hash->notrace_hash); |
| 3477 | |
| 3478 | ret = append_hash(hash: filter_hash, new_hash: ops_hash->filter_hash, |
| 3479 | size_bits); |
| 3480 | if (ret < 0) { |
| 3481 | free_ftrace_hash(hash: *filter_hash); |
| 3482 | *filter_hash = EMPTY_HASH; |
| 3483 | return ret; |
| 3484 | } |
| 3485 | } |
| 3486 | |
| 3487 | /* |
| 3488 | * Only process notrace hashes if the main filter hash is empty |
| 3489 | * (tracing all functions), otherwise the filter hash will just |
| 3490 | * remove the notrace hash functions, and the notrace hash is |
| 3491 | * not needed. |
| 3492 | */ |
| 3493 | if (ftrace_hash_empty(hash: *filter_hash)) { |
| 3494 | /* |
| 3495 | * Intersect the notrace functions. That is, if two |
| 3496 | * subops are not tracing a set of functions, the |
| 3497 | * main ops will only not trace the functions that are |
| 3498 | * in both subops, but has to trace the functions that |
| 3499 | * are only notrace in one of the subops, for the other |
| 3500 | * subops to be able to trace them. |
| 3501 | */ |
| 3502 | size_bits = max(ops_hash->notrace_hash->size_bits, |
| 3503 | subops_hash->notrace_hash->size_bits); |
| 3504 | *notrace_hash = alloc_ftrace_hash(size_bits); |
| 3505 | if (!*notrace_hash) |
| 3506 | return -ENOMEM; |
| 3507 | |
| 3508 | ret = intersect_hash(hash: notrace_hash, new_hash1: ops_hash->notrace_hash, |
| 3509 | new_hash2: subops_hash->notrace_hash); |
| 3510 | if (ret < 0) { |
| 3511 | free_ftrace_hash(hash: *notrace_hash); |
| 3512 | *notrace_hash = EMPTY_HASH; |
| 3513 | return ret; |
| 3514 | } |
| 3515 | } |
| 3516 | return 0; |
| 3517 | } |
| 3518 | |
| 3519 | /** |
| 3520 | * ftrace_startup_subops - enable tracing for subops of an ops |
| 3521 | * @ops: Manager ops (used to pick all the functions of its subops) |
| 3522 | * @subops: A new ops to add to @ops |
| 3523 | * @command: Extra commands to use to enable tracing |
| 3524 | * |
| 3525 | * The @ops is a manager @ops that has the filter that includes all the functions |
| 3526 | * that its list of subops are tracing. Adding a new @subops will add the |
| 3527 | * functions of @subops to @ops. |
| 3528 | */ |
| 3529 | int ftrace_startup_subops(struct ftrace_ops *ops, struct ftrace_ops *subops, int command) |
| 3530 | { |
| 3531 | struct ftrace_hash *filter_hash = EMPTY_HASH; |
| 3532 | struct ftrace_hash *notrace_hash = EMPTY_HASH; |
| 3533 | struct ftrace_hash *save_filter_hash; |
| 3534 | struct ftrace_hash *save_notrace_hash; |
| 3535 | int ret; |
| 3536 | |
| 3537 | if (unlikely(ftrace_disabled)) |
| 3538 | return -ENODEV; |
| 3539 | |
| 3540 | ftrace_ops_init(ops); |
| 3541 | ftrace_ops_init(ops: subops); |
| 3542 | |
| 3543 | if (WARN_ON_ONCE(subops->flags & FTRACE_OPS_FL_ENABLED)) |
| 3544 | return -EBUSY; |
| 3545 | |
| 3546 | /* Make everything canonical (Just in case!) */ |
| 3547 | if (!ops->func_hash->filter_hash) |
| 3548 | ops->func_hash->filter_hash = EMPTY_HASH; |
| 3549 | if (!ops->func_hash->notrace_hash) |
| 3550 | ops->func_hash->notrace_hash = EMPTY_HASH; |
| 3551 | if (!subops->func_hash->filter_hash) |
| 3552 | subops->func_hash->filter_hash = EMPTY_HASH; |
| 3553 | if (!subops->func_hash->notrace_hash) |
| 3554 | subops->func_hash->notrace_hash = EMPTY_HASH; |
| 3555 | |
| 3556 | /* For the first subops to ops just enable it normally */ |
| 3557 | if (list_empty(head: &ops->subop_list)) { |
| 3558 | |
| 3559 | /* The ops was empty, should have empty hashes */ |
| 3560 | WARN_ON_ONCE(!ftrace_hash_empty(ops->func_hash->filter_hash)); |
| 3561 | WARN_ON_ONCE(!ftrace_hash_empty(ops->func_hash->notrace_hash)); |
| 3562 | |
| 3563 | ret = add_first_hash(filter_hash: &filter_hash, notrace_hash: ¬race_hash, func_hash: subops->func_hash); |
| 3564 | if (ret < 0) |
| 3565 | return ret; |
| 3566 | |
| 3567 | save_filter_hash = ops->func_hash->filter_hash; |
| 3568 | save_notrace_hash = ops->func_hash->notrace_hash; |
| 3569 | |
| 3570 | ops->func_hash->filter_hash = filter_hash; |
| 3571 | ops->func_hash->notrace_hash = notrace_hash; |
| 3572 | list_add(new: &subops->list, head: &ops->subop_list); |
| 3573 | ret = ftrace_startup(ops, command); |
| 3574 | if (ret < 0) { |
| 3575 | list_del(entry: &subops->list); |
| 3576 | ops->func_hash->filter_hash = save_filter_hash; |
| 3577 | ops->func_hash->notrace_hash = save_notrace_hash; |
| 3578 | free_ftrace_hash(hash: filter_hash); |
| 3579 | free_ftrace_hash(hash: notrace_hash); |
| 3580 | } else { |
| 3581 | free_ftrace_hash(hash: save_filter_hash); |
| 3582 | free_ftrace_hash(hash: save_notrace_hash); |
| 3583 | subops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_SUBOP; |
| 3584 | subops->managed = ops; |
| 3585 | } |
| 3586 | return ret; |
| 3587 | } |
| 3588 | |
| 3589 | /* |
| 3590 | * Here there's already something attached. Here are the rules: |
| 3591 | * If the new subops and main ops filter hashes are not empty: |
| 3592 | * o Make a copy of the subops filter hash |
| 3593 | * o Remove all functions in the nohash from it. |
| 3594 | * o Add in the main hash filter functions |
| 3595 | * o Remove any of these functions from the main notrace hash |
| 3596 | */ |
| 3597 | |
| 3598 | ret = add_next_hash(filter_hash: &filter_hash, notrace_hash: ¬race_hash, ops_hash: ops->func_hash, subops_hash: subops->func_hash); |
| 3599 | if (ret < 0) |
| 3600 | return ret; |
| 3601 | |
| 3602 | list_add(new: &subops->list, head: &ops->subop_list); |
| 3603 | |
| 3604 | ret = ftrace_update_ops(ops, filter_hash, notrace_hash); |
| 3605 | free_ftrace_hash(hash: filter_hash); |
| 3606 | free_ftrace_hash(hash: notrace_hash); |
| 3607 | if (ret < 0) { |
| 3608 | list_del(entry: &subops->list); |
| 3609 | } else { |
| 3610 | subops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_SUBOP; |
| 3611 | subops->managed = ops; |
| 3612 | } |
| 3613 | return ret; |
| 3614 | } |
| 3615 | |
| 3616 | static int rebuild_hashes(struct ftrace_hash **filter_hash, struct ftrace_hash **notrace_hash, |
| 3617 | struct ftrace_ops *ops) |
| 3618 | { |
| 3619 | struct ftrace_ops_hash temp_hash; |
| 3620 | struct ftrace_ops *subops; |
| 3621 | bool first = true; |
| 3622 | int ret; |
| 3623 | |
| 3624 | temp_hash.filter_hash = EMPTY_HASH; |
| 3625 | temp_hash.notrace_hash = EMPTY_HASH; |
| 3626 | |
| 3627 | list_for_each_entry(subops, &ops->subop_list, list) { |
| 3628 | *filter_hash = EMPTY_HASH; |
| 3629 | *notrace_hash = EMPTY_HASH; |
| 3630 | |
| 3631 | if (first) { |
| 3632 | ret = add_first_hash(filter_hash, notrace_hash, func_hash: subops->func_hash); |
| 3633 | if (ret < 0) |
| 3634 | return ret; |
| 3635 | first = false; |
| 3636 | } else { |
| 3637 | ret = add_next_hash(filter_hash, notrace_hash, |
| 3638 | ops_hash: &temp_hash, subops_hash: subops->func_hash); |
| 3639 | if (ret < 0) { |
| 3640 | free_ftrace_hash(hash: temp_hash.filter_hash); |
| 3641 | free_ftrace_hash(hash: temp_hash.notrace_hash); |
| 3642 | return ret; |
| 3643 | } |
| 3644 | } |
| 3645 | |
| 3646 | free_ftrace_hash(hash: temp_hash.filter_hash); |
| 3647 | free_ftrace_hash(hash: temp_hash.notrace_hash); |
| 3648 | |
| 3649 | temp_hash.filter_hash = *filter_hash; |
| 3650 | temp_hash.notrace_hash = *notrace_hash; |
| 3651 | } |
| 3652 | return 0; |
| 3653 | } |
| 3654 | |
| 3655 | /** |
| 3656 | * ftrace_shutdown_subops - Remove a subops from a manager ops |
| 3657 | * @ops: A manager ops to remove @subops from |
| 3658 | * @subops: The subops to remove from @ops |
| 3659 | * @command: Any extra command flags to add to modifying the text |
| 3660 | * |
| 3661 | * Removes the functions being traced by the @subops from @ops. Note, it |
| 3662 | * will not affect functions that are being traced by other subops that |
| 3663 | * still exist in @ops. |
| 3664 | * |
| 3665 | * If the last subops is removed from @ops, then @ops is shutdown normally. |
| 3666 | */ |
| 3667 | int ftrace_shutdown_subops(struct ftrace_ops *ops, struct ftrace_ops *subops, int command) |
| 3668 | { |
| 3669 | struct ftrace_hash *filter_hash = EMPTY_HASH; |
| 3670 | struct ftrace_hash *notrace_hash = EMPTY_HASH; |
| 3671 | int ret; |
| 3672 | |
| 3673 | if (unlikely(ftrace_disabled)) |
| 3674 | return -ENODEV; |
| 3675 | |
| 3676 | if (WARN_ON_ONCE(!(subops->flags & FTRACE_OPS_FL_ENABLED))) |
| 3677 | return -EINVAL; |
| 3678 | |
| 3679 | list_del(entry: &subops->list); |
| 3680 | |
| 3681 | if (list_empty(head: &ops->subop_list)) { |
| 3682 | /* Last one, just disable the current ops */ |
| 3683 | |
| 3684 | ret = ftrace_shutdown(ops, command); |
| 3685 | if (ret < 0) { |
| 3686 | list_add(new: &subops->list, head: &ops->subop_list); |
| 3687 | return ret; |
| 3688 | } |
| 3689 | |
| 3690 | subops->flags &= ~FTRACE_OPS_FL_ENABLED; |
| 3691 | |
| 3692 | free_ftrace_hash(hash: ops->func_hash->filter_hash); |
| 3693 | free_ftrace_hash(hash: ops->func_hash->notrace_hash); |
| 3694 | ops->func_hash->filter_hash = EMPTY_HASH; |
| 3695 | ops->func_hash->notrace_hash = EMPTY_HASH; |
| 3696 | subops->flags &= ~(FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_SUBOP); |
| 3697 | subops->managed = NULL; |
| 3698 | |
| 3699 | return 0; |
| 3700 | } |
| 3701 | |
| 3702 | /* Rebuild the hashes without subops */ |
| 3703 | ret = rebuild_hashes(filter_hash: &filter_hash, notrace_hash: ¬race_hash, ops); |
| 3704 | if (ret < 0) |
| 3705 | return ret; |
| 3706 | |
| 3707 | ret = ftrace_update_ops(ops, filter_hash, notrace_hash); |
| 3708 | if (ret < 0) { |
| 3709 | list_add(new: &subops->list, head: &ops->subop_list); |
| 3710 | } else { |
| 3711 | subops->flags &= ~(FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_SUBOP); |
| 3712 | subops->managed = NULL; |
| 3713 | } |
| 3714 | free_ftrace_hash(hash: filter_hash); |
| 3715 | free_ftrace_hash(hash: notrace_hash); |
| 3716 | return ret; |
| 3717 | } |
| 3718 | |
| 3719 | static int ftrace_hash_move_and_update_subops(struct ftrace_ops *subops, |
| 3720 | struct ftrace_hash **orig_subhash, |
| 3721 | struct ftrace_hash *hash) |
| 3722 | { |
| 3723 | struct ftrace_ops *ops = subops->managed; |
| 3724 | struct ftrace_hash *notrace_hash; |
| 3725 | struct ftrace_hash *filter_hash; |
| 3726 | struct ftrace_hash *save_hash; |
| 3727 | struct ftrace_hash *new_hash; |
| 3728 | int ret; |
| 3729 | |
| 3730 | /* Manager ops can not be subops (yet) */ |
| 3731 | if (WARN_ON_ONCE(!ops || ops->flags & FTRACE_OPS_FL_SUBOP)) |
| 3732 | return -EINVAL; |
| 3733 | |
| 3734 | /* Move the new hash over to the subops hash */ |
| 3735 | save_hash = *orig_subhash; |
| 3736 | *orig_subhash = __ftrace_hash_move(src: hash); |
| 3737 | if (!*orig_subhash) { |
| 3738 | *orig_subhash = save_hash; |
| 3739 | return -ENOMEM; |
| 3740 | } |
| 3741 | |
| 3742 | ret = rebuild_hashes(filter_hash: &filter_hash, notrace_hash: ¬race_hash, ops); |
| 3743 | if (!ret) { |
| 3744 | ret = ftrace_update_ops(ops, filter_hash, notrace_hash); |
| 3745 | free_ftrace_hash(hash: filter_hash); |
| 3746 | free_ftrace_hash(hash: notrace_hash); |
| 3747 | } |
| 3748 | |
| 3749 | if (ret) { |
| 3750 | /* Put back the original hash */ |
| 3751 | new_hash = *orig_subhash; |
| 3752 | *orig_subhash = save_hash; |
| 3753 | free_ftrace_hash_rcu(hash: new_hash); |
| 3754 | } else { |
| 3755 | free_ftrace_hash_rcu(hash: save_hash); |
| 3756 | } |
| 3757 | return ret; |
| 3758 | } |
| 3759 | |
| 3760 | |
| 3761 | u64 ftrace_update_time; |
| 3762 | u64 ftrace_total_mod_time; |
| 3763 | unsigned long ftrace_update_tot_cnt; |
| 3764 | unsigned long ftrace_number_of_pages; |
| 3765 | unsigned long ftrace_number_of_groups; |
| 3766 | |
| 3767 | static inline int ops_traces_mod(struct ftrace_ops *ops) |
| 3768 | { |
| 3769 | /* |
| 3770 | * Filter_hash being empty will default to trace module. |
| 3771 | * But notrace hash requires a test of individual module functions. |
| 3772 | */ |
| 3773 | return ftrace_hash_empty(hash: ops->func_hash->filter_hash) && |
| 3774 | ftrace_hash_empty(hash: ops->func_hash->notrace_hash); |
| 3775 | } |
| 3776 | |
| 3777 | static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs) |
| 3778 | { |
| 3779 | bool init_nop = ftrace_need_init_nop(); |
| 3780 | struct ftrace_page *pg; |
| 3781 | struct dyn_ftrace *p; |
| 3782 | u64 start, stop, update_time; |
| 3783 | unsigned long update_cnt = 0; |
| 3784 | unsigned long rec_flags = 0; |
| 3785 | int i; |
| 3786 | |
| 3787 | start = ftrace_now(raw_smp_processor_id()); |
| 3788 | |
| 3789 | /* |
| 3790 | * When a module is loaded, this function is called to convert |
| 3791 | * the calls to mcount in its text to nops, and also to create |
| 3792 | * an entry in the ftrace data. Now, if ftrace is activated |
| 3793 | * after this call, but before the module sets its text to |
| 3794 | * read-only, the modification of enabling ftrace can fail if |
| 3795 | * the read-only is done while ftrace is converting the calls. |
| 3796 | * To prevent this, the module's records are set as disabled |
| 3797 | * and will be enabled after the call to set the module's text |
| 3798 | * to read-only. |
| 3799 | */ |
| 3800 | if (mod) |
| 3801 | rec_flags |= FTRACE_FL_DISABLED; |
| 3802 | |
| 3803 | for (pg = new_pgs; pg; pg = pg->next) { |
| 3804 | |
| 3805 | for (i = 0; i < pg->index; i++) { |
| 3806 | |
| 3807 | /* If something went wrong, bail without enabling anything */ |
| 3808 | if (unlikely(ftrace_disabled)) |
| 3809 | return -1; |
| 3810 | |
| 3811 | p = &pg->records[i]; |
| 3812 | p->flags = rec_flags; |
| 3813 | |
| 3814 | /* |
| 3815 | * Do the initial record conversion from mcount jump |
| 3816 | * to the NOP instructions. |
| 3817 | */ |
| 3818 | if (init_nop && !ftrace_nop_initialize(mod, rec: p)) |
| 3819 | break; |
| 3820 | |
| 3821 | update_cnt++; |
| 3822 | } |
| 3823 | } |
| 3824 | |
| 3825 | stop = ftrace_now(raw_smp_processor_id()); |
| 3826 | update_time = stop - start; |
| 3827 | if (mod) |
| 3828 | ftrace_total_mod_time += update_time; |
| 3829 | else |
| 3830 | ftrace_update_time = update_time; |
| 3831 | ftrace_update_tot_cnt += update_cnt; |
| 3832 | |
| 3833 | return 0; |
| 3834 | } |
| 3835 | |
| 3836 | static int ftrace_allocate_records(struct ftrace_page *pg, int count, |
| 3837 | unsigned long *num_pages) |
| 3838 | { |
| 3839 | int order; |
| 3840 | int pages; |
| 3841 | int cnt; |
| 3842 | |
| 3843 | if (WARN_ON(!count)) |
| 3844 | return -EINVAL; |
| 3845 | |
| 3846 | /* We want to fill as much as possible, with no empty pages */ |
| 3847 | pages = DIV_ROUND_UP(count * ENTRY_SIZE, PAGE_SIZE); |
| 3848 | order = fls(x: pages) - 1; |
| 3849 | |
| 3850 | again: |
| 3851 | pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order); |
| 3852 | |
| 3853 | if (!pg->records) { |
| 3854 | /* if we can't allocate this size, try something smaller */ |
| 3855 | if (!order) |
| 3856 | return -ENOMEM; |
| 3857 | order--; |
| 3858 | goto again; |
| 3859 | } |
| 3860 | |
| 3861 | ftrace_number_of_pages += 1 << order; |
| 3862 | *num_pages += 1 << order; |
| 3863 | ftrace_number_of_groups++; |
| 3864 | |
| 3865 | cnt = (PAGE_SIZE << order) / ENTRY_SIZE; |
| 3866 | pg->order = order; |
| 3867 | |
| 3868 | if (cnt > count) |
| 3869 | cnt = count; |
| 3870 | |
| 3871 | return cnt; |
| 3872 | } |
| 3873 | |
| 3874 | static void ftrace_free_pages(struct ftrace_page *pages) |
| 3875 | { |
| 3876 | struct ftrace_page *pg = pages; |
| 3877 | |
| 3878 | while (pg) { |
| 3879 | if (pg->records) { |
| 3880 | free_pages(addr: (unsigned long)pg->records, order: pg->order); |
| 3881 | ftrace_number_of_pages -= 1 << pg->order; |
| 3882 | } |
| 3883 | pages = pg->next; |
| 3884 | kfree(objp: pg); |
| 3885 | pg = pages; |
| 3886 | ftrace_number_of_groups--; |
| 3887 | } |
| 3888 | } |
| 3889 | |
| 3890 | static struct ftrace_page * |
| 3891 | ftrace_allocate_pages(unsigned long num_to_init, unsigned long *num_pages) |
| 3892 | { |
| 3893 | struct ftrace_page *start_pg; |
| 3894 | struct ftrace_page *pg; |
| 3895 | int cnt; |
| 3896 | |
| 3897 | *num_pages = 0; |
| 3898 | |
| 3899 | if (!num_to_init) |
| 3900 | return NULL; |
| 3901 | |
| 3902 | start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL); |
| 3903 | if (!pg) |
| 3904 | return NULL; |
| 3905 | |
| 3906 | /* |
| 3907 | * Try to allocate as much as possible in one continues |
| 3908 | * location that fills in all of the space. We want to |
| 3909 | * waste as little space as possible. |
| 3910 | */ |
| 3911 | for (;;) { |
| 3912 | cnt = ftrace_allocate_records(pg, count: num_to_init, num_pages); |
| 3913 | if (cnt < 0) |
| 3914 | goto free_pages; |
| 3915 | |
| 3916 | num_to_init -= cnt; |
| 3917 | if (!num_to_init) |
| 3918 | break; |
| 3919 | |
| 3920 | pg->next = kzalloc(sizeof(*pg), GFP_KERNEL); |
| 3921 | if (!pg->next) |
| 3922 | goto free_pages; |
| 3923 | |
| 3924 | pg = pg->next; |
| 3925 | } |
| 3926 | |
| 3927 | return start_pg; |
| 3928 | |
| 3929 | free_pages: |
| 3930 | ftrace_free_pages(pages: start_pg); |
| 3931 | pr_info("ftrace: FAILED to allocate memory for functions\n" ); |
| 3932 | return NULL; |
| 3933 | } |
| 3934 | |
| 3935 | #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */ |
| 3936 | |
| 3937 | struct ftrace_iterator { |
| 3938 | loff_t pos; |
| 3939 | loff_t func_pos; |
| 3940 | loff_t mod_pos; |
| 3941 | struct ftrace_page *pg; |
| 3942 | struct dyn_ftrace *func; |
| 3943 | struct ftrace_func_probe *probe; |
| 3944 | struct ftrace_func_entry *probe_entry; |
| 3945 | struct trace_parser parser; |
| 3946 | struct ftrace_hash *hash; |
| 3947 | struct ftrace_ops *ops; |
| 3948 | struct trace_array *tr; |
| 3949 | struct list_head *mod_list; |
| 3950 | int pidx; |
| 3951 | int idx; |
| 3952 | unsigned flags; |
| 3953 | }; |
| 3954 | |
| 3955 | static void * |
| 3956 | t_probe_next(struct seq_file *m, loff_t *pos) |
| 3957 | { |
| 3958 | struct ftrace_iterator *iter = m->private; |
| 3959 | struct trace_array *tr = iter->ops->private; |
| 3960 | struct list_head *func_probes; |
| 3961 | struct ftrace_hash *hash; |
| 3962 | struct list_head *next; |
| 3963 | struct hlist_node *hnd = NULL; |
| 3964 | struct hlist_head *hhd; |
| 3965 | int size; |
| 3966 | |
| 3967 | (*pos)++; |
| 3968 | iter->pos = *pos; |
| 3969 | |
| 3970 | if (!tr) |
| 3971 | return NULL; |
| 3972 | |
| 3973 | func_probes = &tr->func_probes; |
| 3974 | if (list_empty(head: func_probes)) |
| 3975 | return NULL; |
| 3976 | |
| 3977 | if (!iter->probe) { |
| 3978 | next = func_probes->next; |
| 3979 | iter->probe = list_entry(next, struct ftrace_func_probe, list); |
| 3980 | } |
| 3981 | |
| 3982 | if (iter->probe_entry) |
| 3983 | hnd = &iter->probe_entry->hlist; |
| 3984 | |
| 3985 | hash = iter->probe->ops.func_hash->filter_hash; |
| 3986 | |
| 3987 | /* |
| 3988 | * A probe being registered may temporarily have an empty hash |
| 3989 | * and it's at the end of the func_probes list. |
| 3990 | */ |
| 3991 | if (!hash || hash == EMPTY_HASH) |
| 3992 | return NULL; |
| 3993 | |
| 3994 | size = 1 << hash->size_bits; |
| 3995 | |
| 3996 | retry: |
| 3997 | if (iter->pidx >= size) { |
| 3998 | if (iter->probe->list.next == func_probes) |
| 3999 | return NULL; |
| 4000 | next = iter->probe->list.next; |
| 4001 | iter->probe = list_entry(next, struct ftrace_func_probe, list); |
| 4002 | hash = iter->probe->ops.func_hash->filter_hash; |
| 4003 | size = 1 << hash->size_bits; |
| 4004 | iter->pidx = 0; |
| 4005 | } |
| 4006 | |
| 4007 | hhd = &hash->buckets[iter->pidx]; |
| 4008 | |
| 4009 | if (hlist_empty(h: hhd)) { |
| 4010 | iter->pidx++; |
| 4011 | hnd = NULL; |
| 4012 | goto retry; |
| 4013 | } |
| 4014 | |
| 4015 | if (!hnd) |
| 4016 | hnd = hhd->first; |
| 4017 | else { |
| 4018 | hnd = hnd->next; |
| 4019 | if (!hnd) { |
| 4020 | iter->pidx++; |
| 4021 | goto retry; |
| 4022 | } |
| 4023 | } |
| 4024 | |
| 4025 | if (WARN_ON_ONCE(!hnd)) |
| 4026 | return NULL; |
| 4027 | |
| 4028 | iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist); |
| 4029 | |
| 4030 | return iter; |
| 4031 | } |
| 4032 | |
| 4033 | static void *t_probe_start(struct seq_file *m, loff_t *pos) |
| 4034 | { |
| 4035 | struct ftrace_iterator *iter = m->private; |
| 4036 | void *p = NULL; |
| 4037 | loff_t l; |
| 4038 | |
| 4039 | if (!(iter->flags & FTRACE_ITER_DO_PROBES)) |
| 4040 | return NULL; |
| 4041 | |
| 4042 | if (iter->mod_pos > *pos) |
| 4043 | return NULL; |
| 4044 | |
| 4045 | iter->probe = NULL; |
| 4046 | iter->probe_entry = NULL; |
| 4047 | iter->pidx = 0; |
| 4048 | for (l = 0; l <= (*pos - iter->mod_pos); ) { |
| 4049 | p = t_probe_next(m, pos: &l); |
| 4050 | if (!p) |
| 4051 | break; |
| 4052 | } |
| 4053 | if (!p) |
| 4054 | return NULL; |
| 4055 | |
| 4056 | /* Only set this if we have an item */ |
| 4057 | iter->flags |= FTRACE_ITER_PROBE; |
| 4058 | |
| 4059 | return iter; |
| 4060 | } |
| 4061 | |
| 4062 | static int |
| 4063 | t_probe_show(struct seq_file *m, struct ftrace_iterator *iter) |
| 4064 | { |
| 4065 | struct ftrace_func_entry *probe_entry; |
| 4066 | struct ftrace_probe_ops *probe_ops; |
| 4067 | struct ftrace_func_probe *probe; |
| 4068 | |
| 4069 | probe = iter->probe; |
| 4070 | probe_entry = iter->probe_entry; |
| 4071 | |
| 4072 | if (WARN_ON_ONCE(!probe || !probe_entry)) |
| 4073 | return -EIO; |
| 4074 | |
| 4075 | probe_ops = probe->probe_ops; |
| 4076 | |
| 4077 | if (probe_ops->print) |
| 4078 | return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data); |
| 4079 | |
| 4080 | seq_printf(m, fmt: "%ps:%ps\n" , (void *)probe_entry->ip, |
| 4081 | (void *)probe_ops->func); |
| 4082 | |
| 4083 | return 0; |
| 4084 | } |
| 4085 | |
| 4086 | static void * |
| 4087 | t_mod_next(struct seq_file *m, loff_t *pos) |
| 4088 | { |
| 4089 | struct ftrace_iterator *iter = m->private; |
| 4090 | struct trace_array *tr = iter->tr; |
| 4091 | |
| 4092 | (*pos)++; |
| 4093 | iter->pos = *pos; |
| 4094 | |
| 4095 | iter->mod_list = iter->mod_list->next; |
| 4096 | |
| 4097 | if (iter->mod_list == &tr->mod_trace || |
| 4098 | iter->mod_list == &tr->mod_notrace) { |
| 4099 | iter->flags &= ~FTRACE_ITER_MOD; |
| 4100 | return NULL; |
| 4101 | } |
| 4102 | |
| 4103 | iter->mod_pos = *pos; |
| 4104 | |
| 4105 | return iter; |
| 4106 | } |
| 4107 | |
| 4108 | static void *t_mod_start(struct seq_file *m, loff_t *pos) |
| 4109 | { |
| 4110 | struct ftrace_iterator *iter = m->private; |
| 4111 | void *p = NULL; |
| 4112 | loff_t l; |
| 4113 | |
| 4114 | if (iter->func_pos > *pos) |
| 4115 | return NULL; |
| 4116 | |
| 4117 | iter->mod_pos = iter->func_pos; |
| 4118 | |
| 4119 | /* probes are only available if tr is set */ |
| 4120 | if (!iter->tr) |
| 4121 | return NULL; |
| 4122 | |
| 4123 | for (l = 0; l <= (*pos - iter->func_pos); ) { |
| 4124 | p = t_mod_next(m, pos: &l); |
| 4125 | if (!p) |
| 4126 | break; |
| 4127 | } |
| 4128 | if (!p) { |
| 4129 | iter->flags &= ~FTRACE_ITER_MOD; |
| 4130 | return t_probe_start(m, pos); |
| 4131 | } |
| 4132 | |
| 4133 | /* Only set this if we have an item */ |
| 4134 | iter->flags |= FTRACE_ITER_MOD; |
| 4135 | |
| 4136 | return iter; |
| 4137 | } |
| 4138 | |
| 4139 | static int |
| 4140 | t_mod_show(struct seq_file *m, struct ftrace_iterator *iter) |
| 4141 | { |
| 4142 | struct ftrace_mod_load *ftrace_mod; |
| 4143 | struct trace_array *tr = iter->tr; |
| 4144 | |
| 4145 | if (WARN_ON_ONCE(!iter->mod_list) || |
| 4146 | iter->mod_list == &tr->mod_trace || |
| 4147 | iter->mod_list == &tr->mod_notrace) |
| 4148 | return -EIO; |
| 4149 | |
| 4150 | ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list); |
| 4151 | |
| 4152 | if (ftrace_mod->func) |
| 4153 | seq_printf(m, fmt: "%s" , ftrace_mod->func); |
| 4154 | else |
| 4155 | seq_putc(m, c: '*'); |
| 4156 | |
| 4157 | seq_printf(m, fmt: ":mod:%s\n" , ftrace_mod->module); |
| 4158 | |
| 4159 | return 0; |
| 4160 | } |
| 4161 | |
| 4162 | static void * |
| 4163 | t_func_next(struct seq_file *m, loff_t *pos) |
| 4164 | { |
| 4165 | struct ftrace_iterator *iter = m->private; |
| 4166 | struct dyn_ftrace *rec = NULL; |
| 4167 | |
| 4168 | (*pos)++; |
| 4169 | |
| 4170 | retry: |
| 4171 | if (iter->idx >= iter->pg->index) { |
| 4172 | if (iter->pg->next) { |
| 4173 | iter->pg = iter->pg->next; |
| 4174 | iter->idx = 0; |
| 4175 | goto retry; |
| 4176 | } |
| 4177 | } else { |
| 4178 | rec = &iter->pg->records[iter->idx++]; |
| 4179 | if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) && |
| 4180 | !ftrace_lookup_ip(hash: iter->hash, ip: rec->ip)) || |
| 4181 | |
| 4182 | ((iter->flags & FTRACE_ITER_ENABLED) && |
| 4183 | !(rec->flags & FTRACE_FL_ENABLED)) || |
| 4184 | |
| 4185 | ((iter->flags & FTRACE_ITER_TOUCHED) && |
| 4186 | !(rec->flags & FTRACE_FL_TOUCHED))) { |
| 4187 | |
| 4188 | rec = NULL; |
| 4189 | goto retry; |
| 4190 | } |
| 4191 | } |
| 4192 | |
| 4193 | if (!rec) |
| 4194 | return NULL; |
| 4195 | |
| 4196 | iter->pos = iter->func_pos = *pos; |
| 4197 | iter->func = rec; |
| 4198 | |
| 4199 | return iter; |
| 4200 | } |
| 4201 | |
| 4202 | static void * |
| 4203 | t_next(struct seq_file *m, void *v, loff_t *pos) |
| 4204 | { |
| 4205 | struct ftrace_iterator *iter = m->private; |
| 4206 | loff_t l = *pos; /* t_probe_start() must use original pos */ |
| 4207 | void *ret; |
| 4208 | |
| 4209 | if (unlikely(ftrace_disabled)) |
| 4210 | return NULL; |
| 4211 | |
| 4212 | if (iter->flags & FTRACE_ITER_PROBE) |
| 4213 | return t_probe_next(m, pos); |
| 4214 | |
| 4215 | if (iter->flags & FTRACE_ITER_MOD) |
| 4216 | return t_mod_next(m, pos); |
| 4217 | |
| 4218 | if (iter->flags & FTRACE_ITER_PRINTALL) { |
| 4219 | /* next must increment pos, and t_probe_start does not */ |
| 4220 | (*pos)++; |
| 4221 | return t_mod_start(m, pos: &l); |
| 4222 | } |
| 4223 | |
| 4224 | ret = t_func_next(m, pos); |
| 4225 | |
| 4226 | if (!ret) |
| 4227 | return t_mod_start(m, pos: &l); |
| 4228 | |
| 4229 | return ret; |
| 4230 | } |
| 4231 | |
| 4232 | static void reset_iter_read(struct ftrace_iterator *iter) |
| 4233 | { |
| 4234 | iter->pos = 0; |
| 4235 | iter->func_pos = 0; |
| 4236 | iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD); |
| 4237 | } |
| 4238 | |
| 4239 | static void *t_start(struct seq_file *m, loff_t *pos) |
| 4240 | { |
| 4241 | struct ftrace_iterator *iter = m->private; |
| 4242 | void *p = NULL; |
| 4243 | loff_t l; |
| 4244 | |
| 4245 | mutex_lock(&ftrace_lock); |
| 4246 | |
| 4247 | if (unlikely(ftrace_disabled)) |
| 4248 | return NULL; |
| 4249 | |
| 4250 | /* |
| 4251 | * If an lseek was done, then reset and start from beginning. |
| 4252 | */ |
| 4253 | if (*pos < iter->pos) |
| 4254 | reset_iter_read(iter); |
| 4255 | |
| 4256 | /* |
| 4257 | * For set_ftrace_filter reading, if we have the filter |
| 4258 | * off, we can short cut and just print out that all |
| 4259 | * functions are enabled. |
| 4260 | */ |
| 4261 | if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) && |
| 4262 | ftrace_hash_empty(hash: iter->hash)) { |
| 4263 | iter->func_pos = 1; /* Account for the message */ |
| 4264 | if (*pos > 0) |
| 4265 | return t_mod_start(m, pos); |
| 4266 | iter->flags |= FTRACE_ITER_PRINTALL; |
| 4267 | /* reset in case of seek/pread */ |
| 4268 | iter->flags &= ~FTRACE_ITER_PROBE; |
| 4269 | return iter; |
| 4270 | } |
| 4271 | |
| 4272 | if (iter->flags & FTRACE_ITER_MOD) |
| 4273 | return t_mod_start(m, pos); |
| 4274 | |
| 4275 | /* |
| 4276 | * Unfortunately, we need to restart at ftrace_pages_start |
| 4277 | * every time we let go of the ftrace_mutex. This is because |
| 4278 | * those pointers can change without the lock. |
| 4279 | */ |
| 4280 | iter->pg = ftrace_pages_start; |
| 4281 | iter->idx = 0; |
| 4282 | for (l = 0; l <= *pos; ) { |
| 4283 | p = t_func_next(m, pos: &l); |
| 4284 | if (!p) |
| 4285 | break; |
| 4286 | } |
| 4287 | |
| 4288 | if (!p) |
| 4289 | return t_mod_start(m, pos); |
| 4290 | |
| 4291 | return iter; |
| 4292 | } |
| 4293 | |
| 4294 | static void t_stop(struct seq_file *m, void *p) |
| 4295 | { |
| 4296 | mutex_unlock(lock: &ftrace_lock); |
| 4297 | } |
| 4298 | |
| 4299 | void * __weak |
| 4300 | arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec) |
| 4301 | { |
| 4302 | return NULL; |
| 4303 | } |
| 4304 | |
| 4305 | static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops, |
| 4306 | struct dyn_ftrace *rec) |
| 4307 | { |
| 4308 | void *ptr; |
| 4309 | |
| 4310 | ptr = arch_ftrace_trampoline_func(ops, rec); |
| 4311 | if (ptr) |
| 4312 | seq_printf(m, fmt: " ->%pS" , ptr); |
| 4313 | } |
| 4314 | |
| 4315 | #ifdef FTRACE_MCOUNT_MAX_OFFSET |
| 4316 | /* |
| 4317 | * Weak functions can still have an mcount/fentry that is saved in |
| 4318 | * the __mcount_loc section. These can be detected by having a |
| 4319 | * symbol offset of greater than FTRACE_MCOUNT_MAX_OFFSET, as the |
| 4320 | * symbol found by kallsyms is not the function that the mcount/fentry |
| 4321 | * is part of. The offset is much greater in these cases. |
| 4322 | * |
| 4323 | * Test the record to make sure that the ip points to a valid kallsyms |
| 4324 | * and if not, mark it disabled. |
| 4325 | */ |
| 4326 | static int test_for_valid_rec(struct dyn_ftrace *rec) |
| 4327 | { |
| 4328 | char str[KSYM_SYMBOL_LEN]; |
| 4329 | unsigned long offset; |
| 4330 | const char *ret; |
| 4331 | |
| 4332 | ret = kallsyms_lookup(addr: rec->ip, NULL, offset: &offset, NULL, namebuf: str); |
| 4333 | |
| 4334 | /* Weak functions can cause invalid addresses */ |
| 4335 | if (!ret || offset > FTRACE_MCOUNT_MAX_OFFSET) { |
| 4336 | rec->flags |= FTRACE_FL_DISABLED; |
| 4337 | return 0; |
| 4338 | } |
| 4339 | return 1; |
| 4340 | } |
| 4341 | |
| 4342 | static struct workqueue_struct *ftrace_check_wq __initdata; |
| 4343 | static struct work_struct ftrace_check_work __initdata; |
| 4344 | |
| 4345 | /* |
| 4346 | * Scan all the mcount/fentry entries to make sure they are valid. |
| 4347 | */ |
| 4348 | static __init void ftrace_check_work_func(struct work_struct *work) |
| 4349 | { |
| 4350 | struct ftrace_page *pg; |
| 4351 | struct dyn_ftrace *rec; |
| 4352 | |
| 4353 | mutex_lock(&ftrace_lock); |
| 4354 | do_for_each_ftrace_rec(pg, rec) { |
| 4355 | test_for_valid_rec(rec); |
| 4356 | } while_for_each_ftrace_rec(); |
| 4357 | mutex_unlock(lock: &ftrace_lock); |
| 4358 | } |
| 4359 | |
| 4360 | static int __init ftrace_check_for_weak_functions(void) |
| 4361 | { |
| 4362 | INIT_WORK(&ftrace_check_work, ftrace_check_work_func); |
| 4363 | |
| 4364 | ftrace_check_wq = alloc_workqueue("ftrace_check_wq" , WQ_UNBOUND, 0); |
| 4365 | |
| 4366 | queue_work(wq: ftrace_check_wq, work: &ftrace_check_work); |
| 4367 | return 0; |
| 4368 | } |
| 4369 | |
| 4370 | static int __init ftrace_check_sync(void) |
| 4371 | { |
| 4372 | /* Make sure the ftrace_check updates are finished */ |
| 4373 | if (ftrace_check_wq) |
| 4374 | destroy_workqueue(wq: ftrace_check_wq); |
| 4375 | return 0; |
| 4376 | } |
| 4377 | |
| 4378 | late_initcall_sync(ftrace_check_sync); |
| 4379 | subsys_initcall(ftrace_check_for_weak_functions); |
| 4380 | |
| 4381 | static int print_rec(struct seq_file *m, unsigned long ip) |
| 4382 | { |
| 4383 | unsigned long offset; |
| 4384 | char str[KSYM_SYMBOL_LEN]; |
| 4385 | char *modname; |
| 4386 | const char *ret; |
| 4387 | |
| 4388 | ret = kallsyms_lookup(addr: ip, NULL, offset: &offset, modname: &modname, namebuf: str); |
| 4389 | /* Weak functions can cause invalid addresses */ |
| 4390 | if (!ret || offset > FTRACE_MCOUNT_MAX_OFFSET) { |
| 4391 | snprintf(buf: str, KSYM_SYMBOL_LEN, fmt: "%s_%ld" , |
| 4392 | FTRACE_INVALID_FUNCTION, offset); |
| 4393 | ret = NULL; |
| 4394 | } |
| 4395 | |
| 4396 | seq_puts(m, s: str); |
| 4397 | if (modname) |
| 4398 | seq_printf(m, fmt: " [%s]" , modname); |
| 4399 | return ret == NULL ? -1 : 0; |
| 4400 | } |
| 4401 | #else |
| 4402 | static inline int test_for_valid_rec(struct dyn_ftrace *rec) |
| 4403 | { |
| 4404 | return 1; |
| 4405 | } |
| 4406 | |
| 4407 | static inline int print_rec(struct seq_file *m, unsigned long ip) |
| 4408 | { |
| 4409 | seq_printf(m, "%ps" , (void *)ip); |
| 4410 | return 0; |
| 4411 | } |
| 4412 | #endif |
| 4413 | |
| 4414 | static void print_subops(struct seq_file *m, struct ftrace_ops *ops, struct dyn_ftrace *rec) |
| 4415 | { |
| 4416 | struct ftrace_ops *subops; |
| 4417 | bool first = true; |
| 4418 | |
| 4419 | list_for_each_entry(subops, &ops->subop_list, list) { |
| 4420 | if (!((subops->flags & FTRACE_OPS_FL_ENABLED) && |
| 4421 | hash_contains_ip(ip: rec->ip, hash: subops->func_hash))) |
| 4422 | continue; |
| 4423 | if (first) { |
| 4424 | seq_printf(m, fmt: "\tsubops:" ); |
| 4425 | first = false; |
| 4426 | } |
| 4427 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 4428 | if (subops->flags & FTRACE_OPS_FL_GRAPH) { |
| 4429 | struct fgraph_ops *gops; |
| 4430 | |
| 4431 | gops = container_of(subops, struct fgraph_ops, ops); |
| 4432 | seq_printf(m, fmt: " {ent:%pS ret:%pS}" , |
| 4433 | (void *)gops->entryfunc, |
| 4434 | (void *)gops->retfunc); |
| 4435 | continue; |
| 4436 | } |
| 4437 | #endif |
| 4438 | if (subops->trampoline) { |
| 4439 | seq_printf(m, fmt: " {%pS (%pS)}" , |
| 4440 | (void *)subops->trampoline, |
| 4441 | (void *)subops->func); |
| 4442 | add_trampoline_func(m, ops: subops, rec); |
| 4443 | } else { |
| 4444 | seq_printf(m, fmt: " {%pS}" , |
| 4445 | (void *)subops->func); |
| 4446 | } |
| 4447 | } |
| 4448 | } |
| 4449 | |
| 4450 | static int t_show(struct seq_file *m, void *v) |
| 4451 | { |
| 4452 | struct ftrace_iterator *iter = m->private; |
| 4453 | struct dyn_ftrace *rec; |
| 4454 | |
| 4455 | if (iter->flags & FTRACE_ITER_PROBE) |
| 4456 | return t_probe_show(m, iter); |
| 4457 | |
| 4458 | if (iter->flags & FTRACE_ITER_MOD) |
| 4459 | return t_mod_show(m, iter); |
| 4460 | |
| 4461 | if (iter->flags & FTRACE_ITER_PRINTALL) { |
| 4462 | if (iter->flags & FTRACE_ITER_NOTRACE) |
| 4463 | seq_puts(m, s: "#### no functions disabled ####\n" ); |
| 4464 | else |
| 4465 | seq_puts(m, s: "#### all functions enabled ####\n" ); |
| 4466 | return 0; |
| 4467 | } |
| 4468 | |
| 4469 | rec = iter->func; |
| 4470 | |
| 4471 | if (!rec) |
| 4472 | return 0; |
| 4473 | |
| 4474 | if (iter->flags & FTRACE_ITER_ADDRS) |
| 4475 | seq_printf(m, fmt: "%lx " , rec->ip); |
| 4476 | |
| 4477 | if (print_rec(m, ip: rec->ip)) { |
| 4478 | /* This should only happen when a rec is disabled */ |
| 4479 | WARN_ON_ONCE(!(rec->flags & FTRACE_FL_DISABLED)); |
| 4480 | seq_putc(m, c: '\n'); |
| 4481 | return 0; |
| 4482 | } |
| 4483 | |
| 4484 | if (iter->flags & (FTRACE_ITER_ENABLED | FTRACE_ITER_TOUCHED)) { |
| 4485 | struct ftrace_ops *ops; |
| 4486 | |
| 4487 | seq_printf(m, fmt: " (%ld)%s%s%s%s%s" , |
| 4488 | ftrace_rec_count(rec), |
| 4489 | rec->flags & FTRACE_FL_REGS ? " R" : " " , |
| 4490 | rec->flags & FTRACE_FL_IPMODIFY ? " I" : " " , |
| 4491 | rec->flags & FTRACE_FL_DIRECT ? " D" : " " , |
| 4492 | rec->flags & FTRACE_FL_CALL_OPS ? " O" : " " , |
| 4493 | rec->flags & FTRACE_FL_MODIFIED ? " M " : " " ); |
| 4494 | if (rec->flags & FTRACE_FL_TRAMP_EN) { |
| 4495 | ops = ftrace_find_tramp_ops_any(rec); |
| 4496 | if (ops) { |
| 4497 | do { |
| 4498 | seq_printf(m, fmt: "\ttramp: %pS (%pS)" , |
| 4499 | (void *)ops->trampoline, |
| 4500 | (void *)ops->func); |
| 4501 | add_trampoline_func(m, ops, rec); |
| 4502 | print_subops(m, ops, rec); |
| 4503 | ops = ftrace_find_tramp_ops_next(rec, op: ops); |
| 4504 | } while (ops); |
| 4505 | } else |
| 4506 | seq_puts(m, s: "\ttramp: ERROR!" ); |
| 4507 | } else { |
| 4508 | add_trampoline_func(m, NULL, rec); |
| 4509 | } |
| 4510 | if (rec->flags & FTRACE_FL_CALL_OPS_EN) { |
| 4511 | ops = ftrace_find_unique_ops(rec); |
| 4512 | if (ops) { |
| 4513 | seq_printf(m, fmt: "\tops: %pS (%pS)" , |
| 4514 | ops, ops->func); |
| 4515 | print_subops(m, ops, rec); |
| 4516 | } else { |
| 4517 | seq_puts(m, s: "\tops: ERROR!" ); |
| 4518 | } |
| 4519 | } |
| 4520 | if (rec->flags & FTRACE_FL_DIRECT) { |
| 4521 | unsigned long direct; |
| 4522 | |
| 4523 | direct = ftrace_find_rec_direct(ip: rec->ip); |
| 4524 | if (direct) { |
| 4525 | seq_printf(m, fmt: "\n\tdirect%s-->%pS" , |
| 4526 | ftrace_is_jmp(addr: direct) ? "(jmp)" : "" , |
| 4527 | (void *)ftrace_jmp_get(addr: direct)); |
| 4528 | } |
| 4529 | } |
| 4530 | } |
| 4531 | |
| 4532 | seq_putc(m, c: '\n'); |
| 4533 | |
| 4534 | return 0; |
| 4535 | } |
| 4536 | |
| 4537 | static const struct seq_operations show_ftrace_seq_ops = { |
| 4538 | .start = t_start, |
| 4539 | .next = t_next, |
| 4540 | .stop = t_stop, |
| 4541 | .show = t_show, |
| 4542 | }; |
| 4543 | |
| 4544 | static int |
| 4545 | ftrace_avail_open(struct inode *inode, struct file *file) |
| 4546 | { |
| 4547 | struct ftrace_iterator *iter; |
| 4548 | int ret; |
| 4549 | |
| 4550 | ret = security_locked_down(what: LOCKDOWN_TRACEFS); |
| 4551 | if (ret) |
| 4552 | return ret; |
| 4553 | |
| 4554 | if (unlikely(ftrace_disabled)) |
| 4555 | return -ENODEV; |
| 4556 | |
| 4557 | iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter)); |
| 4558 | if (!iter) |
| 4559 | return -ENOMEM; |
| 4560 | |
| 4561 | iter->pg = ftrace_pages_start; |
| 4562 | iter->ops = &global_ops; |
| 4563 | |
| 4564 | return 0; |
| 4565 | } |
| 4566 | |
| 4567 | static int |
| 4568 | ftrace_enabled_open(struct inode *inode, struct file *file) |
| 4569 | { |
| 4570 | struct ftrace_iterator *iter; |
| 4571 | |
| 4572 | /* |
| 4573 | * This shows us what functions are currently being |
| 4574 | * traced and by what. Not sure if we want lockdown |
| 4575 | * to hide such critical information for an admin. |
| 4576 | * Although, perhaps it can show information we don't |
| 4577 | * want people to see, but if something is tracing |
| 4578 | * something, we probably want to know about it. |
| 4579 | */ |
| 4580 | |
| 4581 | iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter)); |
| 4582 | if (!iter) |
| 4583 | return -ENOMEM; |
| 4584 | |
| 4585 | iter->pg = ftrace_pages_start; |
| 4586 | iter->flags = FTRACE_ITER_ENABLED; |
| 4587 | iter->ops = &global_ops; |
| 4588 | |
| 4589 | return 0; |
| 4590 | } |
| 4591 | |
| 4592 | static int |
| 4593 | ftrace_touched_open(struct inode *inode, struct file *file) |
| 4594 | { |
| 4595 | struct ftrace_iterator *iter; |
| 4596 | |
| 4597 | /* |
| 4598 | * This shows us what functions have ever been enabled |
| 4599 | * (traced, direct, patched, etc). Not sure if we want lockdown |
| 4600 | * to hide such critical information for an admin. |
| 4601 | * Although, perhaps it can show information we don't |
| 4602 | * want people to see, but if something had traced |
| 4603 | * something, we probably want to know about it. |
| 4604 | */ |
| 4605 | |
| 4606 | iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter)); |
| 4607 | if (!iter) |
| 4608 | return -ENOMEM; |
| 4609 | |
| 4610 | iter->pg = ftrace_pages_start; |
| 4611 | iter->flags = FTRACE_ITER_TOUCHED; |
| 4612 | iter->ops = &global_ops; |
| 4613 | |
| 4614 | return 0; |
| 4615 | } |
| 4616 | |
| 4617 | static int |
| 4618 | ftrace_avail_addrs_open(struct inode *inode, struct file *file) |
| 4619 | { |
| 4620 | struct ftrace_iterator *iter; |
| 4621 | int ret; |
| 4622 | |
| 4623 | ret = security_locked_down(what: LOCKDOWN_TRACEFS); |
| 4624 | if (ret) |
| 4625 | return ret; |
| 4626 | |
| 4627 | if (unlikely(ftrace_disabled)) |
| 4628 | return -ENODEV; |
| 4629 | |
| 4630 | iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter)); |
| 4631 | if (!iter) |
| 4632 | return -ENOMEM; |
| 4633 | |
| 4634 | iter->pg = ftrace_pages_start; |
| 4635 | iter->flags = FTRACE_ITER_ADDRS; |
| 4636 | iter->ops = &global_ops; |
| 4637 | |
| 4638 | return 0; |
| 4639 | } |
| 4640 | |
| 4641 | /** |
| 4642 | * ftrace_regex_open - initialize function tracer filter files |
| 4643 | * @ops: The ftrace_ops that hold the hash filters |
| 4644 | * @flag: The type of filter to process |
| 4645 | * @inode: The inode, usually passed in to your open routine |
| 4646 | * @file: The file, usually passed in to your open routine |
| 4647 | * |
| 4648 | * ftrace_regex_open() initializes the filter files for the |
| 4649 | * @ops. Depending on @flag it may process the filter hash or |
| 4650 | * the notrace hash of @ops. With this called from the open |
| 4651 | * routine, you can use ftrace_filter_write() for the write |
| 4652 | * routine if @flag has FTRACE_ITER_FILTER set, or |
| 4653 | * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set. |
| 4654 | * tracing_lseek() should be used as the lseek routine, and |
| 4655 | * release must call ftrace_regex_release(). |
| 4656 | * |
| 4657 | * Returns: 0 on success or a negative errno value on failure |
| 4658 | */ |
| 4659 | int |
| 4660 | ftrace_regex_open(struct ftrace_ops *ops, int flag, |
| 4661 | struct inode *inode, struct file *file) |
| 4662 | { |
| 4663 | struct ftrace_iterator *iter; |
| 4664 | struct ftrace_hash *hash; |
| 4665 | struct list_head *mod_head; |
| 4666 | struct trace_array *tr = ops->private; |
| 4667 | int ret = -ENOMEM; |
| 4668 | |
| 4669 | ftrace_ops_init(ops); |
| 4670 | |
| 4671 | if (unlikely(ftrace_disabled)) |
| 4672 | return -ENODEV; |
| 4673 | |
| 4674 | if (tracing_check_open_get_tr(tr)) |
| 4675 | return -ENODEV; |
| 4676 | |
| 4677 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
| 4678 | if (!iter) |
| 4679 | goto out; |
| 4680 | |
| 4681 | if (trace_parser_get_init(parser: &iter->parser, FTRACE_BUFF_MAX)) |
| 4682 | goto out; |
| 4683 | |
| 4684 | iter->ops = ops; |
| 4685 | iter->flags = flag; |
| 4686 | iter->tr = tr; |
| 4687 | |
| 4688 | mutex_lock(&ops->func_hash->regex_lock); |
| 4689 | |
| 4690 | if (flag & FTRACE_ITER_NOTRACE) { |
| 4691 | hash = ops->func_hash->notrace_hash; |
| 4692 | mod_head = tr ? &tr->mod_notrace : NULL; |
| 4693 | } else { |
| 4694 | hash = ops->func_hash->filter_hash; |
| 4695 | mod_head = tr ? &tr->mod_trace : NULL; |
| 4696 | } |
| 4697 | |
| 4698 | iter->mod_list = mod_head; |
| 4699 | |
| 4700 | if (file->f_mode & FMODE_WRITE) { |
| 4701 | const int size_bits = FTRACE_HASH_DEFAULT_BITS; |
| 4702 | |
| 4703 | if (file->f_flags & O_TRUNC) { |
| 4704 | iter->hash = alloc_ftrace_hash(size_bits); |
| 4705 | clear_ftrace_mod_list(head: mod_head); |
| 4706 | } else { |
| 4707 | iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash); |
| 4708 | } |
| 4709 | } else { |
| 4710 | if (hash) |
| 4711 | iter->hash = alloc_and_copy_ftrace_hash(size_bits: hash->size_bits, hash); |
| 4712 | else |
| 4713 | iter->hash = EMPTY_HASH; |
| 4714 | } |
| 4715 | |
| 4716 | if (!iter->hash) { |
| 4717 | trace_parser_put(parser: &iter->parser); |
| 4718 | goto out_unlock; |
| 4719 | } |
| 4720 | |
| 4721 | ret = 0; |
| 4722 | |
| 4723 | if (file->f_mode & FMODE_READ) { |
| 4724 | iter->pg = ftrace_pages_start; |
| 4725 | |
| 4726 | ret = seq_open(file, &show_ftrace_seq_ops); |
| 4727 | if (!ret) { |
| 4728 | struct seq_file *m = file->private_data; |
| 4729 | m->private = iter; |
| 4730 | } else { |
| 4731 | /* Failed */ |
| 4732 | free_ftrace_hash(hash: iter->hash); |
| 4733 | trace_parser_put(parser: &iter->parser); |
| 4734 | } |
| 4735 | } else |
| 4736 | file->private_data = iter; |
| 4737 | |
| 4738 | out_unlock: |
| 4739 | mutex_unlock(lock: &ops->func_hash->regex_lock); |
| 4740 | |
| 4741 | out: |
| 4742 | if (ret) { |
| 4743 | kfree(objp: iter); |
| 4744 | if (tr) |
| 4745 | trace_array_put(tr); |
| 4746 | } |
| 4747 | |
| 4748 | return ret; |
| 4749 | } |
| 4750 | |
| 4751 | static int |
| 4752 | ftrace_filter_open(struct inode *inode, struct file *file) |
| 4753 | { |
| 4754 | struct ftrace_ops *ops = inode->i_private; |
| 4755 | |
| 4756 | /* Checks for tracefs lockdown */ |
| 4757 | return ftrace_regex_open(ops, |
| 4758 | flag: FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES, |
| 4759 | inode, file); |
| 4760 | } |
| 4761 | |
| 4762 | static int |
| 4763 | ftrace_notrace_open(struct inode *inode, struct file *file) |
| 4764 | { |
| 4765 | struct ftrace_ops *ops = inode->i_private; |
| 4766 | |
| 4767 | /* Checks for tracefs lockdown */ |
| 4768 | return ftrace_regex_open(ops, flag: FTRACE_ITER_NOTRACE, |
| 4769 | inode, file); |
| 4770 | } |
| 4771 | |
| 4772 | /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */ |
| 4773 | struct ftrace_glob { |
| 4774 | char *search; |
| 4775 | unsigned len; |
| 4776 | int type; |
| 4777 | }; |
| 4778 | |
| 4779 | /* |
| 4780 | * If symbols in an architecture don't correspond exactly to the user-visible |
| 4781 | * name of what they represent, it is possible to define this function to |
| 4782 | * perform the necessary adjustments. |
| 4783 | */ |
| 4784 | char * __weak arch_ftrace_match_adjust(char *str, const char *search) |
| 4785 | { |
| 4786 | return str; |
| 4787 | } |
| 4788 | |
| 4789 | static int ftrace_match(char *str, struct ftrace_glob *g) |
| 4790 | { |
| 4791 | int matched = 0; |
| 4792 | int slen; |
| 4793 | |
| 4794 | str = arch_ftrace_match_adjust(str, search: g->search); |
| 4795 | |
| 4796 | switch (g->type) { |
| 4797 | case MATCH_FULL: |
| 4798 | if (strcmp(str, g->search) == 0) |
| 4799 | matched = 1; |
| 4800 | break; |
| 4801 | case MATCH_FRONT_ONLY: |
| 4802 | if (strncmp(str, g->search, g->len) == 0) |
| 4803 | matched = 1; |
| 4804 | break; |
| 4805 | case MATCH_MIDDLE_ONLY: |
| 4806 | if (strstr(str, g->search)) |
| 4807 | matched = 1; |
| 4808 | break; |
| 4809 | case MATCH_END_ONLY: |
| 4810 | slen = strlen(str); |
| 4811 | if (slen >= g->len && |
| 4812 | memcmp(p: str + slen - g->len, q: g->search, size: g->len) == 0) |
| 4813 | matched = 1; |
| 4814 | break; |
| 4815 | case MATCH_GLOB: |
| 4816 | if (glob_match(pat: g->search, str)) |
| 4817 | matched = 1; |
| 4818 | break; |
| 4819 | } |
| 4820 | |
| 4821 | return matched; |
| 4822 | } |
| 4823 | |
| 4824 | static int |
| 4825 | enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter) |
| 4826 | { |
| 4827 | struct ftrace_func_entry *entry; |
| 4828 | int ret = 0; |
| 4829 | |
| 4830 | entry = ftrace_lookup_ip(hash, ip: rec->ip); |
| 4831 | if (clear_filter) { |
| 4832 | /* Do nothing if it doesn't exist */ |
| 4833 | if (!entry) |
| 4834 | return 0; |
| 4835 | |
| 4836 | free_hash_entry(hash, entry); |
| 4837 | } else { |
| 4838 | /* Do nothing if it exists */ |
| 4839 | if (entry) |
| 4840 | return 0; |
| 4841 | if (add_hash_entry(hash, ip: rec->ip) == NULL) |
| 4842 | ret = -ENOMEM; |
| 4843 | } |
| 4844 | return ret; |
| 4845 | } |
| 4846 | |
| 4847 | static int |
| 4848 | add_rec_by_index(struct ftrace_hash *hash, struct ftrace_glob *func_g, |
| 4849 | int clear_filter) |
| 4850 | { |
| 4851 | long index; |
| 4852 | struct ftrace_page *pg; |
| 4853 | struct dyn_ftrace *rec; |
| 4854 | |
| 4855 | /* The index starts at 1 */ |
| 4856 | if (kstrtoul(s: func_g->search, base: 0, res: &index) || --index < 0) |
| 4857 | return 0; |
| 4858 | |
| 4859 | do_for_each_ftrace_rec(pg, rec) { |
| 4860 | if (pg->index <= index) { |
| 4861 | index -= pg->index; |
| 4862 | /* this is a double loop, break goes to the next page */ |
| 4863 | break; |
| 4864 | } |
| 4865 | rec = &pg->records[index]; |
| 4866 | enter_record(hash, rec, clear_filter); |
| 4867 | return 1; |
| 4868 | } while_for_each_ftrace_rec(); |
| 4869 | return 0; |
| 4870 | } |
| 4871 | |
| 4872 | #ifdef FTRACE_MCOUNT_MAX_OFFSET |
| 4873 | static int lookup_ip(unsigned long ip, char **modname, char *str) |
| 4874 | { |
| 4875 | unsigned long offset; |
| 4876 | |
| 4877 | kallsyms_lookup(addr: ip, NULL, offset: &offset, modname, namebuf: str); |
| 4878 | if (offset > FTRACE_MCOUNT_MAX_OFFSET) |
| 4879 | return -1; |
| 4880 | return 0; |
| 4881 | } |
| 4882 | #else |
| 4883 | static int lookup_ip(unsigned long ip, char **modname, char *str) |
| 4884 | { |
| 4885 | kallsyms_lookup(ip, NULL, NULL, modname, str); |
| 4886 | return 0; |
| 4887 | } |
| 4888 | #endif |
| 4889 | |
| 4890 | static int |
| 4891 | ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g, |
| 4892 | struct ftrace_glob *mod_g, int exclude_mod) |
| 4893 | { |
| 4894 | char str[KSYM_SYMBOL_LEN]; |
| 4895 | char *modname; |
| 4896 | |
| 4897 | if (lookup_ip(ip: rec->ip, modname: &modname, str)) { |
| 4898 | /* This should only happen when a rec is disabled */ |
| 4899 | WARN_ON_ONCE(system_state == SYSTEM_RUNNING && |
| 4900 | !(rec->flags & FTRACE_FL_DISABLED)); |
| 4901 | return 0; |
| 4902 | } |
| 4903 | |
| 4904 | if (mod_g) { |
| 4905 | int mod_matches = (modname) ? ftrace_match(str: modname, g: mod_g) : 0; |
| 4906 | |
| 4907 | /* blank module name to match all modules */ |
| 4908 | if (!mod_g->len) { |
| 4909 | /* blank module globbing: modname xor exclude_mod */ |
| 4910 | if (!exclude_mod != !modname) |
| 4911 | goto func_match; |
| 4912 | return 0; |
| 4913 | } |
| 4914 | |
| 4915 | /* |
| 4916 | * exclude_mod is set to trace everything but the given |
| 4917 | * module. If it is set and the module matches, then |
| 4918 | * return 0. If it is not set, and the module doesn't match |
| 4919 | * also return 0. Otherwise, check the function to see if |
| 4920 | * that matches. |
| 4921 | */ |
| 4922 | if (!mod_matches == !exclude_mod) |
| 4923 | return 0; |
| 4924 | func_match: |
| 4925 | /* blank search means to match all funcs in the mod */ |
| 4926 | if (!func_g->len) |
| 4927 | return 1; |
| 4928 | } |
| 4929 | |
| 4930 | return ftrace_match(str, g: func_g); |
| 4931 | } |
| 4932 | |
| 4933 | static int |
| 4934 | match_records(struct ftrace_hash *hash, char *func, int len, char *mod) |
| 4935 | { |
| 4936 | struct ftrace_page *pg; |
| 4937 | struct dyn_ftrace *rec; |
| 4938 | struct ftrace_glob func_g = { .type = MATCH_FULL }; |
| 4939 | struct ftrace_glob mod_g = { .type = MATCH_FULL }; |
| 4940 | struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL; |
| 4941 | int exclude_mod = 0; |
| 4942 | int found = 0; |
| 4943 | int ret; |
| 4944 | int clear_filter = 0; |
| 4945 | |
| 4946 | if (func) { |
| 4947 | func_g.type = filter_parse_regex(buff: func, len, search: &func_g.search, |
| 4948 | not: &clear_filter); |
| 4949 | func_g.len = strlen(func_g.search); |
| 4950 | } |
| 4951 | |
| 4952 | if (mod) { |
| 4953 | mod_g.type = filter_parse_regex(buff: mod, strlen(mod), |
| 4954 | search: &mod_g.search, not: &exclude_mod); |
| 4955 | mod_g.len = strlen(mod_g.search); |
| 4956 | } |
| 4957 | |
| 4958 | guard(mutex)(T: &ftrace_lock); |
| 4959 | |
| 4960 | if (unlikely(ftrace_disabled)) |
| 4961 | return 0; |
| 4962 | |
| 4963 | if (func_g.type == MATCH_INDEX) |
| 4964 | return add_rec_by_index(hash, func_g: &func_g, clear_filter); |
| 4965 | |
| 4966 | do_for_each_ftrace_rec(pg, rec) { |
| 4967 | |
| 4968 | if (rec->flags & FTRACE_FL_DISABLED) |
| 4969 | continue; |
| 4970 | |
| 4971 | if (ftrace_match_record(rec, func_g: &func_g, mod_g: mod_match, exclude_mod)) { |
| 4972 | ret = enter_record(hash, rec, clear_filter); |
| 4973 | if (ret < 0) |
| 4974 | return ret; |
| 4975 | found = 1; |
| 4976 | } |
| 4977 | cond_resched(); |
| 4978 | } while_for_each_ftrace_rec(); |
| 4979 | |
| 4980 | return found; |
| 4981 | } |
| 4982 | |
| 4983 | static int |
| 4984 | ftrace_match_records(struct ftrace_hash *hash, char *buff, int len) |
| 4985 | { |
| 4986 | return match_records(hash, func: buff, len, NULL); |
| 4987 | } |
| 4988 | |
| 4989 | static void ftrace_ops_update_code(struct ftrace_ops *ops, |
| 4990 | struct ftrace_ops_hash *old_hash) |
| 4991 | { |
| 4992 | struct ftrace_ops *op; |
| 4993 | |
| 4994 | if (!ftrace_enabled) |
| 4995 | return; |
| 4996 | |
| 4997 | if (ops->flags & FTRACE_OPS_FL_ENABLED) { |
| 4998 | ftrace_run_modify_code(ops, command: FTRACE_UPDATE_CALLS, old_hash); |
| 4999 | return; |
| 5000 | } |
| 5001 | |
| 5002 | /* |
| 5003 | * If this is the shared global_ops filter, then we need to |
| 5004 | * check if there is another ops that shares it, is enabled. |
| 5005 | * If so, we still need to run the modify code. |
| 5006 | */ |
| 5007 | if (ops->func_hash != &global_ops.local_hash) |
| 5008 | return; |
| 5009 | |
| 5010 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 5011 | if (op->func_hash == &global_ops.local_hash && |
| 5012 | op->flags & FTRACE_OPS_FL_ENABLED) { |
| 5013 | ftrace_run_modify_code(ops: op, command: FTRACE_UPDATE_CALLS, old_hash); |
| 5014 | /* Only need to do this once */ |
| 5015 | return; |
| 5016 | } |
| 5017 | } while_for_each_ftrace_op(op); |
| 5018 | } |
| 5019 | |
| 5020 | static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops, |
| 5021 | struct ftrace_hash **orig_hash, |
| 5022 | struct ftrace_hash *hash, |
| 5023 | int enable) |
| 5024 | { |
| 5025 | if (ops->flags & FTRACE_OPS_FL_SUBOP) |
| 5026 | return ftrace_hash_move_and_update_subops(subops: ops, orig_subhash: orig_hash, hash); |
| 5027 | |
| 5028 | /* |
| 5029 | * If this ops is not enabled, it could be sharing its filters |
| 5030 | * with a subop. If that's the case, update the subop instead of |
| 5031 | * this ops. Shared filters are only allowed to have one ops set |
| 5032 | * at a time, and if we update the ops that is not enabled, |
| 5033 | * it will not affect subops that share it. |
| 5034 | */ |
| 5035 | if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) { |
| 5036 | struct ftrace_ops *op; |
| 5037 | |
| 5038 | /* Check if any other manager subops maps to this hash */ |
| 5039 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 5040 | struct ftrace_ops *subops; |
| 5041 | |
| 5042 | list_for_each_entry(subops, &op->subop_list, list) { |
| 5043 | if ((subops->flags & FTRACE_OPS_FL_ENABLED) && |
| 5044 | subops->func_hash == ops->func_hash) { |
| 5045 | return ftrace_hash_move_and_update_subops(subops, orig_subhash: orig_hash, hash); |
| 5046 | } |
| 5047 | } |
| 5048 | } while_for_each_ftrace_op(op); |
| 5049 | } |
| 5050 | |
| 5051 | return __ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable); |
| 5052 | } |
| 5053 | |
| 5054 | static int cache_mod(struct trace_array *tr, |
| 5055 | const char *func, char *module, int enable) |
| 5056 | { |
| 5057 | struct ftrace_mod_load *ftrace_mod, *n; |
| 5058 | struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace; |
| 5059 | |
| 5060 | guard(mutex)(T: &ftrace_lock); |
| 5061 | |
| 5062 | /* We do not cache inverse filters */ |
| 5063 | if (func[0] == '!') { |
| 5064 | int ret = -EINVAL; |
| 5065 | |
| 5066 | func++; |
| 5067 | |
| 5068 | /* Look to remove this hash */ |
| 5069 | list_for_each_entry_safe(ftrace_mod, n, head, list) { |
| 5070 | if (strcmp(ftrace_mod->module, module) != 0) |
| 5071 | continue; |
| 5072 | |
| 5073 | /* no func matches all */ |
| 5074 | if (strcmp(func, "*" ) == 0 || |
| 5075 | (ftrace_mod->func && |
| 5076 | strcmp(ftrace_mod->func, func) == 0)) { |
| 5077 | ret = 0; |
| 5078 | free_ftrace_mod(ftrace_mod); |
| 5079 | continue; |
| 5080 | } |
| 5081 | } |
| 5082 | return ret; |
| 5083 | } |
| 5084 | |
| 5085 | /* We only care about modules that have not been loaded yet */ |
| 5086 | if (module_exists(module)) |
| 5087 | return -EINVAL; |
| 5088 | |
| 5089 | /* Save this string off, and execute it when the module is loaded */ |
| 5090 | return ftrace_add_mod(tr, func, module, enable); |
| 5091 | } |
| 5092 | |
| 5093 | #ifdef CONFIG_MODULES |
| 5094 | static void process_mod_list(struct list_head *head, struct ftrace_ops *ops, |
| 5095 | char *mod, bool enable) |
| 5096 | { |
| 5097 | struct ftrace_mod_load *ftrace_mod, *n; |
| 5098 | struct ftrace_hash **orig_hash, *new_hash; |
| 5099 | LIST_HEAD(process_mods); |
| 5100 | char *func; |
| 5101 | |
| 5102 | mutex_lock(&ops->func_hash->regex_lock); |
| 5103 | |
| 5104 | if (enable) |
| 5105 | orig_hash = &ops->func_hash->filter_hash; |
| 5106 | else |
| 5107 | orig_hash = &ops->func_hash->notrace_hash; |
| 5108 | |
| 5109 | new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, |
| 5110 | hash: *orig_hash); |
| 5111 | if (!new_hash) |
| 5112 | goto out; /* warn? */ |
| 5113 | |
| 5114 | mutex_lock(&ftrace_lock); |
| 5115 | |
| 5116 | list_for_each_entry_safe(ftrace_mod, n, head, list) { |
| 5117 | |
| 5118 | if (strcmp(ftrace_mod->module, mod) != 0) |
| 5119 | continue; |
| 5120 | |
| 5121 | if (ftrace_mod->func) |
| 5122 | func = kstrdup(s: ftrace_mod->func, GFP_KERNEL); |
| 5123 | else |
| 5124 | func = kstrdup(s: "*" , GFP_KERNEL); |
| 5125 | |
| 5126 | if (!func) /* warn? */ |
| 5127 | continue; |
| 5128 | |
| 5129 | list_move(list: &ftrace_mod->list, head: &process_mods); |
| 5130 | |
| 5131 | /* Use the newly allocated func, as it may be "*" */ |
| 5132 | kfree(objp: ftrace_mod->func); |
| 5133 | ftrace_mod->func = func; |
| 5134 | } |
| 5135 | |
| 5136 | mutex_unlock(lock: &ftrace_lock); |
| 5137 | |
| 5138 | list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) { |
| 5139 | |
| 5140 | func = ftrace_mod->func; |
| 5141 | |
| 5142 | /* Grabs ftrace_lock, which is why we have this extra step */ |
| 5143 | match_records(hash: new_hash, func, strlen(func), mod); |
| 5144 | free_ftrace_mod(ftrace_mod); |
| 5145 | } |
| 5146 | |
| 5147 | if (enable && list_empty(head)) |
| 5148 | new_hash->flags &= ~FTRACE_HASH_FL_MOD; |
| 5149 | |
| 5150 | mutex_lock(&ftrace_lock); |
| 5151 | |
| 5152 | ftrace_hash_move_and_update_ops(ops, orig_hash, |
| 5153 | hash: new_hash, enable); |
| 5154 | mutex_unlock(lock: &ftrace_lock); |
| 5155 | |
| 5156 | out: |
| 5157 | mutex_unlock(lock: &ops->func_hash->regex_lock); |
| 5158 | |
| 5159 | free_ftrace_hash(hash: new_hash); |
| 5160 | } |
| 5161 | |
| 5162 | static void process_cached_mods(const char *mod_name) |
| 5163 | { |
| 5164 | struct trace_array *tr; |
| 5165 | char *mod; |
| 5166 | |
| 5167 | mod = kstrdup(s: mod_name, GFP_KERNEL); |
| 5168 | if (!mod) |
| 5169 | return; |
| 5170 | |
| 5171 | mutex_lock(&trace_types_lock); |
| 5172 | list_for_each_entry(tr, &ftrace_trace_arrays, list) { |
| 5173 | if (!list_empty(head: &tr->mod_trace)) |
| 5174 | process_mod_list(head: &tr->mod_trace, ops: tr->ops, mod, enable: true); |
| 5175 | if (!list_empty(head: &tr->mod_notrace)) |
| 5176 | process_mod_list(head: &tr->mod_notrace, ops: tr->ops, mod, enable: false); |
| 5177 | } |
| 5178 | mutex_unlock(lock: &trace_types_lock); |
| 5179 | |
| 5180 | kfree(objp: mod); |
| 5181 | } |
| 5182 | #endif |
| 5183 | |
| 5184 | /* |
| 5185 | * We register the module command as a template to show others how |
| 5186 | * to register the a command as well. |
| 5187 | */ |
| 5188 | |
| 5189 | static int |
| 5190 | ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash, |
| 5191 | char *func_orig, char *cmd, char *module, int enable) |
| 5192 | { |
| 5193 | char *func; |
| 5194 | int ret; |
| 5195 | |
| 5196 | if (!tr) |
| 5197 | return -ENODEV; |
| 5198 | |
| 5199 | /* match_records() modifies func, and we need the original */ |
| 5200 | func = kstrdup(s: func_orig, GFP_KERNEL); |
| 5201 | if (!func) |
| 5202 | return -ENOMEM; |
| 5203 | |
| 5204 | /* |
| 5205 | * cmd == 'mod' because we only registered this func |
| 5206 | * for the 'mod' ftrace_func_command. |
| 5207 | * But if you register one func with multiple commands, |
| 5208 | * you can tell which command was used by the cmd |
| 5209 | * parameter. |
| 5210 | */ |
| 5211 | ret = match_records(hash, func, strlen(func), mod: module); |
| 5212 | kfree(objp: func); |
| 5213 | |
| 5214 | if (!ret) |
| 5215 | return cache_mod(tr, func: func_orig, module, enable); |
| 5216 | if (ret < 0) |
| 5217 | return ret; |
| 5218 | return 0; |
| 5219 | } |
| 5220 | |
| 5221 | static struct ftrace_func_command ftrace_mod_cmd = { |
| 5222 | .name = "mod" , |
| 5223 | .func = ftrace_mod_callback, |
| 5224 | }; |
| 5225 | |
| 5226 | static int __init ftrace_mod_cmd_init(void) |
| 5227 | { |
| 5228 | return register_ftrace_command(cmd: &ftrace_mod_cmd); |
| 5229 | } |
| 5230 | core_initcall(ftrace_mod_cmd_init); |
| 5231 | |
| 5232 | static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip, |
| 5233 | struct ftrace_ops *op, struct ftrace_regs *fregs) |
| 5234 | { |
| 5235 | struct ftrace_probe_ops *probe_ops; |
| 5236 | struct ftrace_func_probe *probe; |
| 5237 | |
| 5238 | probe = container_of(op, struct ftrace_func_probe, ops); |
| 5239 | probe_ops = probe->probe_ops; |
| 5240 | |
| 5241 | /* |
| 5242 | * Disable preemption for these calls to prevent a RCU grace |
| 5243 | * period. This syncs the hash iteration and freeing of items |
| 5244 | * on the hash. rcu_read_lock is too dangerous here. |
| 5245 | */ |
| 5246 | preempt_disable_notrace(); |
| 5247 | probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data); |
| 5248 | preempt_enable_notrace(); |
| 5249 | } |
| 5250 | |
| 5251 | struct ftrace_func_map { |
| 5252 | struct ftrace_func_entry entry; |
| 5253 | void *data; |
| 5254 | }; |
| 5255 | |
| 5256 | /* |
| 5257 | * Note, ftrace_func_mapper is freed by free_ftrace_hash(&mapper->hash). |
| 5258 | * The hash field must be the first field. |
| 5259 | */ |
| 5260 | struct ftrace_func_mapper { |
| 5261 | struct ftrace_hash hash; /* Must be first! */ |
| 5262 | }; |
| 5263 | |
| 5264 | /** |
| 5265 | * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper |
| 5266 | * |
| 5267 | * Returns: a ftrace_func_mapper descriptor that can be used to map ips to data. |
| 5268 | */ |
| 5269 | struct ftrace_func_mapper *allocate_ftrace_func_mapper(void) |
| 5270 | { |
| 5271 | struct ftrace_hash *hash; |
| 5272 | |
| 5273 | /* |
| 5274 | * The mapper is simply a ftrace_hash, but since the entries |
| 5275 | * in the hash are not ftrace_func_entry type, we define it |
| 5276 | * as a separate structure. |
| 5277 | */ |
| 5278 | hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS); |
| 5279 | return (struct ftrace_func_mapper *)hash; |
| 5280 | } |
| 5281 | |
| 5282 | /** |
| 5283 | * ftrace_func_mapper_find_ip - Find some data mapped to an ip |
| 5284 | * @mapper: The mapper that has the ip maps |
| 5285 | * @ip: the instruction pointer to find the data for |
| 5286 | * |
| 5287 | * Returns: the data mapped to @ip if found otherwise NULL. The return |
| 5288 | * is actually the address of the mapper data pointer. The address is |
| 5289 | * returned for use cases where the data is no bigger than a long, and |
| 5290 | * the user can use the data pointer as its data instead of having to |
| 5291 | * allocate more memory for the reference. |
| 5292 | */ |
| 5293 | void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper, |
| 5294 | unsigned long ip) |
| 5295 | { |
| 5296 | struct ftrace_func_entry *entry; |
| 5297 | struct ftrace_func_map *map; |
| 5298 | |
| 5299 | entry = ftrace_lookup_ip(hash: &mapper->hash, ip); |
| 5300 | if (!entry) |
| 5301 | return NULL; |
| 5302 | |
| 5303 | map = (struct ftrace_func_map *)entry; |
| 5304 | return &map->data; |
| 5305 | } |
| 5306 | |
| 5307 | /** |
| 5308 | * ftrace_func_mapper_add_ip - Map some data to an ip |
| 5309 | * @mapper: The mapper that has the ip maps |
| 5310 | * @ip: The instruction pointer address to map @data to |
| 5311 | * @data: The data to map to @ip |
| 5312 | * |
| 5313 | * Returns: 0 on success otherwise an error. |
| 5314 | */ |
| 5315 | int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper, |
| 5316 | unsigned long ip, void *data) |
| 5317 | { |
| 5318 | struct ftrace_func_entry *entry; |
| 5319 | struct ftrace_func_map *map; |
| 5320 | |
| 5321 | entry = ftrace_lookup_ip(hash: &mapper->hash, ip); |
| 5322 | if (entry) |
| 5323 | return -EBUSY; |
| 5324 | |
| 5325 | map = kmalloc(sizeof(*map), GFP_KERNEL); |
| 5326 | if (!map) |
| 5327 | return -ENOMEM; |
| 5328 | |
| 5329 | map->entry.ip = ip; |
| 5330 | map->data = data; |
| 5331 | |
| 5332 | __add_hash_entry(hash: &mapper->hash, entry: &map->entry); |
| 5333 | |
| 5334 | return 0; |
| 5335 | } |
| 5336 | |
| 5337 | /** |
| 5338 | * ftrace_func_mapper_remove_ip - Remove an ip from the mapping |
| 5339 | * @mapper: The mapper that has the ip maps |
| 5340 | * @ip: The instruction pointer address to remove the data from |
| 5341 | * |
| 5342 | * Returns: the data if it is found, otherwise NULL. |
| 5343 | * Note, if the data pointer is used as the data itself, (see |
| 5344 | * ftrace_func_mapper_find_ip(), then the return value may be meaningless, |
| 5345 | * if the data pointer was set to zero. |
| 5346 | */ |
| 5347 | void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper, |
| 5348 | unsigned long ip) |
| 5349 | { |
| 5350 | struct ftrace_func_entry *entry; |
| 5351 | struct ftrace_func_map *map; |
| 5352 | void *data; |
| 5353 | |
| 5354 | entry = ftrace_lookup_ip(hash: &mapper->hash, ip); |
| 5355 | if (!entry) |
| 5356 | return NULL; |
| 5357 | |
| 5358 | map = (struct ftrace_func_map *)entry; |
| 5359 | data = map->data; |
| 5360 | |
| 5361 | remove_hash_entry(hash: &mapper->hash, entry); |
| 5362 | kfree(objp: entry); |
| 5363 | |
| 5364 | return data; |
| 5365 | } |
| 5366 | |
| 5367 | /** |
| 5368 | * free_ftrace_func_mapper - free a mapping of ips and data |
| 5369 | * @mapper: The mapper that has the ip maps |
| 5370 | * @free_func: A function to be called on each data item. |
| 5371 | * |
| 5372 | * This is used to free the function mapper. The @free_func is optional |
| 5373 | * and can be used if the data needs to be freed as well. |
| 5374 | */ |
| 5375 | void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper, |
| 5376 | ftrace_mapper_func free_func) |
| 5377 | { |
| 5378 | struct ftrace_func_entry *entry; |
| 5379 | struct ftrace_func_map *map; |
| 5380 | struct hlist_head *hhd; |
| 5381 | int size, i; |
| 5382 | |
| 5383 | if (!mapper) |
| 5384 | return; |
| 5385 | |
| 5386 | if (free_func && mapper->hash.count) { |
| 5387 | size = 1 << mapper->hash.size_bits; |
| 5388 | for (i = 0; i < size; i++) { |
| 5389 | hhd = &mapper->hash.buckets[i]; |
| 5390 | hlist_for_each_entry(entry, hhd, hlist) { |
| 5391 | map = (struct ftrace_func_map *)entry; |
| 5392 | free_func(map); |
| 5393 | } |
| 5394 | } |
| 5395 | } |
| 5396 | /* This also frees the mapper itself */ |
| 5397 | free_ftrace_hash(hash: &mapper->hash); |
| 5398 | } |
| 5399 | |
| 5400 | static void release_probe(struct ftrace_func_probe *probe) |
| 5401 | { |
| 5402 | struct ftrace_probe_ops *probe_ops; |
| 5403 | |
| 5404 | guard(mutex)(T: &ftrace_lock); |
| 5405 | |
| 5406 | WARN_ON(probe->ref <= 0); |
| 5407 | |
| 5408 | /* Subtract the ref that was used to protect this instance */ |
| 5409 | probe->ref--; |
| 5410 | |
| 5411 | if (!probe->ref) { |
| 5412 | probe_ops = probe->probe_ops; |
| 5413 | /* |
| 5414 | * Sending zero as ip tells probe_ops to free |
| 5415 | * the probe->data itself |
| 5416 | */ |
| 5417 | if (probe_ops->free) |
| 5418 | probe_ops->free(probe_ops, probe->tr, 0, probe->data); |
| 5419 | list_del(entry: &probe->list); |
| 5420 | kfree(objp: probe); |
| 5421 | } |
| 5422 | } |
| 5423 | |
| 5424 | static void acquire_probe_locked(struct ftrace_func_probe *probe) |
| 5425 | { |
| 5426 | /* |
| 5427 | * Add one ref to keep it from being freed when releasing the |
| 5428 | * ftrace_lock mutex. |
| 5429 | */ |
| 5430 | probe->ref++; |
| 5431 | } |
| 5432 | |
| 5433 | int |
| 5434 | register_ftrace_function_probe(char *glob, struct trace_array *tr, |
| 5435 | struct ftrace_probe_ops *probe_ops, |
| 5436 | void *data) |
| 5437 | { |
| 5438 | struct ftrace_func_probe *probe = NULL, *iter; |
| 5439 | struct ftrace_func_entry *entry; |
| 5440 | struct ftrace_hash **orig_hash; |
| 5441 | struct ftrace_hash *old_hash; |
| 5442 | struct ftrace_hash *hash; |
| 5443 | int count = 0; |
| 5444 | int size; |
| 5445 | int ret; |
| 5446 | int i; |
| 5447 | |
| 5448 | if (WARN_ON(!tr)) |
| 5449 | return -EINVAL; |
| 5450 | |
| 5451 | /* We do not support '!' for function probes */ |
| 5452 | if (WARN_ON(glob[0] == '!')) |
| 5453 | return -EINVAL; |
| 5454 | |
| 5455 | |
| 5456 | mutex_lock(&ftrace_lock); |
| 5457 | /* Check if the probe_ops is already registered */ |
| 5458 | list_for_each_entry(iter, &tr->func_probes, list) { |
| 5459 | if (iter->probe_ops == probe_ops) { |
| 5460 | probe = iter; |
| 5461 | break; |
| 5462 | } |
| 5463 | } |
| 5464 | if (!probe) { |
| 5465 | probe = kzalloc(sizeof(*probe), GFP_KERNEL); |
| 5466 | if (!probe) { |
| 5467 | mutex_unlock(lock: &ftrace_lock); |
| 5468 | return -ENOMEM; |
| 5469 | } |
| 5470 | probe->probe_ops = probe_ops; |
| 5471 | probe->ops.func = function_trace_probe_call; |
| 5472 | probe->tr = tr; |
| 5473 | ftrace_ops_init(ops: &probe->ops); |
| 5474 | list_add(new: &probe->list, head: &tr->func_probes); |
| 5475 | } |
| 5476 | |
| 5477 | acquire_probe_locked(probe); |
| 5478 | |
| 5479 | mutex_unlock(lock: &ftrace_lock); |
| 5480 | |
| 5481 | /* |
| 5482 | * Note, there's a small window here that the func_hash->filter_hash |
| 5483 | * may be NULL or empty. Need to be careful when reading the loop. |
| 5484 | */ |
| 5485 | mutex_lock(&probe->ops.func_hash->regex_lock); |
| 5486 | |
| 5487 | orig_hash = &probe->ops.func_hash->filter_hash; |
| 5488 | old_hash = *orig_hash; |
| 5489 | hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash: old_hash); |
| 5490 | |
| 5491 | if (!hash) { |
| 5492 | ret = -ENOMEM; |
| 5493 | goto out; |
| 5494 | } |
| 5495 | |
| 5496 | ret = ftrace_match_records(hash, buff: glob, strlen(glob)); |
| 5497 | |
| 5498 | /* Nothing found? */ |
| 5499 | if (!ret) |
| 5500 | ret = -EINVAL; |
| 5501 | |
| 5502 | if (ret < 0) |
| 5503 | goto out; |
| 5504 | |
| 5505 | size = 1 << hash->size_bits; |
| 5506 | for (i = 0; i < size; i++) { |
| 5507 | hlist_for_each_entry(entry, &hash->buckets[i], hlist) { |
| 5508 | if (ftrace_lookup_ip(hash: old_hash, ip: entry->ip)) |
| 5509 | continue; |
| 5510 | /* |
| 5511 | * The caller might want to do something special |
| 5512 | * for each function we find. We call the callback |
| 5513 | * to give the caller an opportunity to do so. |
| 5514 | */ |
| 5515 | if (probe_ops->init) { |
| 5516 | ret = probe_ops->init(probe_ops, tr, |
| 5517 | entry->ip, data, |
| 5518 | &probe->data); |
| 5519 | if (ret < 0) { |
| 5520 | if (probe_ops->free && count) |
| 5521 | probe_ops->free(probe_ops, tr, |
| 5522 | 0, probe->data); |
| 5523 | probe->data = NULL; |
| 5524 | goto out; |
| 5525 | } |
| 5526 | } |
| 5527 | count++; |
| 5528 | } |
| 5529 | } |
| 5530 | |
| 5531 | mutex_lock(&ftrace_lock); |
| 5532 | |
| 5533 | if (!count) { |
| 5534 | /* Nothing was added? */ |
| 5535 | ret = -EINVAL; |
| 5536 | goto out_unlock; |
| 5537 | } |
| 5538 | |
| 5539 | ret = ftrace_hash_move_and_update_ops(ops: &probe->ops, orig_hash, |
| 5540 | hash, enable: 1); |
| 5541 | if (ret < 0) |
| 5542 | goto err_unlock; |
| 5543 | |
| 5544 | /* One ref for each new function traced */ |
| 5545 | probe->ref += count; |
| 5546 | |
| 5547 | if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED)) |
| 5548 | ret = ftrace_startup(ops: &probe->ops, command: 0); |
| 5549 | |
| 5550 | out_unlock: |
| 5551 | mutex_unlock(lock: &ftrace_lock); |
| 5552 | |
| 5553 | if (!ret) |
| 5554 | ret = count; |
| 5555 | out: |
| 5556 | mutex_unlock(lock: &probe->ops.func_hash->regex_lock); |
| 5557 | free_ftrace_hash(hash); |
| 5558 | |
| 5559 | release_probe(probe); |
| 5560 | |
| 5561 | return ret; |
| 5562 | |
| 5563 | err_unlock: |
| 5564 | if (!probe_ops->free || !count) |
| 5565 | goto out_unlock; |
| 5566 | |
| 5567 | /* Failed to do the move, need to call the free functions */ |
| 5568 | for (i = 0; i < size; i++) { |
| 5569 | hlist_for_each_entry(entry, &hash->buckets[i], hlist) { |
| 5570 | if (ftrace_lookup_ip(hash: old_hash, ip: entry->ip)) |
| 5571 | continue; |
| 5572 | probe_ops->free(probe_ops, tr, entry->ip, probe->data); |
| 5573 | } |
| 5574 | } |
| 5575 | goto out_unlock; |
| 5576 | } |
| 5577 | |
| 5578 | int |
| 5579 | unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr, |
| 5580 | struct ftrace_probe_ops *probe_ops) |
| 5581 | { |
| 5582 | struct ftrace_func_probe *probe = NULL, *iter; |
| 5583 | struct ftrace_ops_hash old_hash_ops; |
| 5584 | struct ftrace_func_entry *entry; |
| 5585 | struct ftrace_glob func_g; |
| 5586 | struct ftrace_hash **orig_hash; |
| 5587 | struct ftrace_hash *old_hash; |
| 5588 | struct ftrace_hash *hash = NULL; |
| 5589 | struct hlist_node *tmp; |
| 5590 | struct hlist_head hhd; |
| 5591 | char str[KSYM_SYMBOL_LEN]; |
| 5592 | int count = 0; |
| 5593 | int i, ret = -ENODEV; |
| 5594 | int size; |
| 5595 | |
| 5596 | if (!glob || !strlen(glob) || !strcmp(glob, "*" )) |
| 5597 | func_g.search = NULL; |
| 5598 | else { |
| 5599 | int not; |
| 5600 | |
| 5601 | func_g.type = filter_parse_regex(buff: glob, strlen(glob), |
| 5602 | search: &func_g.search, not: ¬); |
| 5603 | func_g.len = strlen(func_g.search); |
| 5604 | |
| 5605 | /* we do not support '!' for function probes */ |
| 5606 | if (WARN_ON(not)) |
| 5607 | return -EINVAL; |
| 5608 | } |
| 5609 | |
| 5610 | mutex_lock(&ftrace_lock); |
| 5611 | /* Check if the probe_ops is already registered */ |
| 5612 | list_for_each_entry(iter, &tr->func_probes, list) { |
| 5613 | if (iter->probe_ops == probe_ops) { |
| 5614 | probe = iter; |
| 5615 | break; |
| 5616 | } |
| 5617 | } |
| 5618 | if (!probe) |
| 5619 | goto err_unlock_ftrace; |
| 5620 | |
| 5621 | ret = -EINVAL; |
| 5622 | if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED)) |
| 5623 | goto err_unlock_ftrace; |
| 5624 | |
| 5625 | acquire_probe_locked(probe); |
| 5626 | |
| 5627 | mutex_unlock(lock: &ftrace_lock); |
| 5628 | |
| 5629 | mutex_lock(&probe->ops.func_hash->regex_lock); |
| 5630 | |
| 5631 | orig_hash = &probe->ops.func_hash->filter_hash; |
| 5632 | old_hash = *orig_hash; |
| 5633 | |
| 5634 | if (ftrace_hash_empty(hash: old_hash)) |
| 5635 | goto out_unlock; |
| 5636 | |
| 5637 | old_hash_ops.filter_hash = old_hash; |
| 5638 | /* Probes only have filters */ |
| 5639 | old_hash_ops.notrace_hash = NULL; |
| 5640 | |
| 5641 | ret = -ENOMEM; |
| 5642 | hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash: old_hash); |
| 5643 | if (!hash) |
| 5644 | goto out_unlock; |
| 5645 | |
| 5646 | INIT_HLIST_HEAD(&hhd); |
| 5647 | |
| 5648 | size = 1 << hash->size_bits; |
| 5649 | for (i = 0; i < size; i++) { |
| 5650 | hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) { |
| 5651 | |
| 5652 | if (func_g.search) { |
| 5653 | kallsyms_lookup(addr: entry->ip, NULL, NULL, |
| 5654 | NULL, namebuf: str); |
| 5655 | if (!ftrace_match(str, g: &func_g)) |
| 5656 | continue; |
| 5657 | } |
| 5658 | count++; |
| 5659 | remove_hash_entry(hash, entry); |
| 5660 | hlist_add_head(n: &entry->hlist, h: &hhd); |
| 5661 | } |
| 5662 | } |
| 5663 | |
| 5664 | /* Nothing found? */ |
| 5665 | if (!count) { |
| 5666 | ret = -EINVAL; |
| 5667 | goto out_unlock; |
| 5668 | } |
| 5669 | |
| 5670 | mutex_lock(&ftrace_lock); |
| 5671 | |
| 5672 | WARN_ON(probe->ref < count); |
| 5673 | |
| 5674 | probe->ref -= count; |
| 5675 | |
| 5676 | if (ftrace_hash_empty(hash)) |
| 5677 | ftrace_shutdown(ops: &probe->ops, command: 0); |
| 5678 | |
| 5679 | ret = ftrace_hash_move_and_update_ops(ops: &probe->ops, orig_hash, |
| 5680 | hash, enable: 1); |
| 5681 | |
| 5682 | /* still need to update the function call sites */ |
| 5683 | if (ftrace_enabled && !ftrace_hash_empty(hash)) |
| 5684 | ftrace_run_modify_code(ops: &probe->ops, command: FTRACE_UPDATE_CALLS, |
| 5685 | old_hash: &old_hash_ops); |
| 5686 | synchronize_rcu(); |
| 5687 | |
| 5688 | hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) { |
| 5689 | hlist_del(n: &entry->hlist); |
| 5690 | if (probe_ops->free) |
| 5691 | probe_ops->free(probe_ops, tr, entry->ip, probe->data); |
| 5692 | kfree(objp: entry); |
| 5693 | } |
| 5694 | mutex_unlock(lock: &ftrace_lock); |
| 5695 | |
| 5696 | out_unlock: |
| 5697 | mutex_unlock(lock: &probe->ops.func_hash->regex_lock); |
| 5698 | free_ftrace_hash(hash); |
| 5699 | |
| 5700 | release_probe(probe); |
| 5701 | |
| 5702 | return ret; |
| 5703 | |
| 5704 | err_unlock_ftrace: |
| 5705 | mutex_unlock(lock: &ftrace_lock); |
| 5706 | return ret; |
| 5707 | } |
| 5708 | |
| 5709 | void clear_ftrace_function_probes(struct trace_array *tr) |
| 5710 | { |
| 5711 | struct ftrace_func_probe *probe, *n; |
| 5712 | |
| 5713 | list_for_each_entry_safe(probe, n, &tr->func_probes, list) |
| 5714 | unregister_ftrace_function_probe_func(NULL, tr, probe_ops: probe->probe_ops); |
| 5715 | } |
| 5716 | |
| 5717 | static LIST_HEAD(ftrace_commands); |
| 5718 | static DEFINE_MUTEX(ftrace_cmd_mutex); |
| 5719 | |
| 5720 | /* |
| 5721 | * Currently we only register ftrace commands from __init, so mark this |
| 5722 | * __init too. |
| 5723 | */ |
| 5724 | __init int register_ftrace_command(struct ftrace_func_command *cmd) |
| 5725 | { |
| 5726 | struct ftrace_func_command *p; |
| 5727 | |
| 5728 | guard(mutex)(T: &ftrace_cmd_mutex); |
| 5729 | list_for_each_entry(p, &ftrace_commands, list) { |
| 5730 | if (strcmp(cmd->name, p->name) == 0) |
| 5731 | return -EBUSY; |
| 5732 | } |
| 5733 | list_add(new: &cmd->list, head: &ftrace_commands); |
| 5734 | |
| 5735 | return 0; |
| 5736 | } |
| 5737 | |
| 5738 | /* |
| 5739 | * Currently we only unregister ftrace commands from __init, so mark |
| 5740 | * this __init too. |
| 5741 | */ |
| 5742 | __init int unregister_ftrace_command(struct ftrace_func_command *cmd) |
| 5743 | { |
| 5744 | struct ftrace_func_command *p, *n; |
| 5745 | |
| 5746 | guard(mutex)(T: &ftrace_cmd_mutex); |
| 5747 | |
| 5748 | list_for_each_entry_safe(p, n, &ftrace_commands, list) { |
| 5749 | if (strcmp(cmd->name, p->name) == 0) { |
| 5750 | list_del_init(entry: &p->list); |
| 5751 | return 0; |
| 5752 | } |
| 5753 | } |
| 5754 | |
| 5755 | return -ENODEV; |
| 5756 | } |
| 5757 | |
| 5758 | static int ftrace_process_regex(struct ftrace_iterator *iter, |
| 5759 | char *buff, int len, int enable) |
| 5760 | { |
| 5761 | struct ftrace_hash *hash = iter->hash; |
| 5762 | struct trace_array *tr = iter->ops->private; |
| 5763 | char *func, *command, *next = buff; |
| 5764 | struct ftrace_func_command *p; |
| 5765 | int ret; |
| 5766 | |
| 5767 | func = strsep(&next, ":" ); |
| 5768 | |
| 5769 | if (!next) { |
| 5770 | ret = ftrace_match_records(hash, buff: func, len); |
| 5771 | if (!ret) |
| 5772 | ret = -EINVAL; |
| 5773 | if (ret < 0) |
| 5774 | return ret; |
| 5775 | return 0; |
| 5776 | } |
| 5777 | |
| 5778 | /* command found */ |
| 5779 | |
| 5780 | command = strsep(&next, ":" ); |
| 5781 | |
| 5782 | guard(mutex)(T: &ftrace_cmd_mutex); |
| 5783 | |
| 5784 | list_for_each_entry(p, &ftrace_commands, list) { |
| 5785 | if (strcmp(p->name, command) == 0) |
| 5786 | return p->func(tr, hash, func, command, next, enable); |
| 5787 | } |
| 5788 | |
| 5789 | return -EINVAL; |
| 5790 | } |
| 5791 | |
| 5792 | static ssize_t |
| 5793 | ftrace_regex_write(struct file *file, const char __user *ubuf, |
| 5794 | size_t cnt, loff_t *ppos, int enable) |
| 5795 | { |
| 5796 | struct ftrace_iterator *iter; |
| 5797 | struct trace_parser *parser; |
| 5798 | ssize_t ret, read; |
| 5799 | |
| 5800 | if (!cnt) |
| 5801 | return 0; |
| 5802 | |
| 5803 | if (file->f_mode & FMODE_READ) { |
| 5804 | struct seq_file *m = file->private_data; |
| 5805 | iter = m->private; |
| 5806 | } else |
| 5807 | iter = file->private_data; |
| 5808 | |
| 5809 | if (unlikely(ftrace_disabled)) |
| 5810 | return -ENODEV; |
| 5811 | |
| 5812 | /* iter->hash is a local copy, so we don't need regex_lock */ |
| 5813 | |
| 5814 | parser = &iter->parser; |
| 5815 | read = trace_get_user(parser, ubuf, cnt, ppos); |
| 5816 | |
| 5817 | if (read >= 0 && trace_parser_loaded(parser) && |
| 5818 | !trace_parser_cont(parser)) { |
| 5819 | ret = ftrace_process_regex(iter, buff: parser->buffer, |
| 5820 | len: parser->idx, enable); |
| 5821 | trace_parser_clear(parser); |
| 5822 | if (ret < 0) |
| 5823 | return ret; |
| 5824 | } |
| 5825 | |
| 5826 | return read; |
| 5827 | } |
| 5828 | |
| 5829 | ssize_t |
| 5830 | ftrace_filter_write(struct file *file, const char __user *ubuf, |
| 5831 | size_t cnt, loff_t *ppos) |
| 5832 | { |
| 5833 | return ftrace_regex_write(file, ubuf, cnt, ppos, enable: 1); |
| 5834 | } |
| 5835 | |
| 5836 | ssize_t |
| 5837 | ftrace_notrace_write(struct file *file, const char __user *ubuf, |
| 5838 | size_t cnt, loff_t *ppos) |
| 5839 | { |
| 5840 | return ftrace_regex_write(file, ubuf, cnt, ppos, enable: 0); |
| 5841 | } |
| 5842 | |
| 5843 | static int |
| 5844 | __ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove) |
| 5845 | { |
| 5846 | struct ftrace_func_entry *entry; |
| 5847 | |
| 5848 | ip = ftrace_location(ip); |
| 5849 | if (!ip) |
| 5850 | return -EINVAL; |
| 5851 | |
| 5852 | if (remove) { |
| 5853 | entry = ftrace_lookup_ip(hash, ip); |
| 5854 | if (!entry) |
| 5855 | return -ENOENT; |
| 5856 | free_hash_entry(hash, entry); |
| 5857 | return 0; |
| 5858 | } else if (__ftrace_lookup_ip(hash, ip) != NULL) { |
| 5859 | /* Already exists */ |
| 5860 | return 0; |
| 5861 | } |
| 5862 | |
| 5863 | entry = add_hash_entry(hash, ip); |
| 5864 | return entry ? 0 : -ENOMEM; |
| 5865 | } |
| 5866 | |
| 5867 | static int |
| 5868 | ftrace_match_addr(struct ftrace_hash *hash, unsigned long *ips, |
| 5869 | unsigned int cnt, int remove) |
| 5870 | { |
| 5871 | unsigned int i; |
| 5872 | int err; |
| 5873 | |
| 5874 | for (i = 0; i < cnt; i++) { |
| 5875 | err = __ftrace_match_addr(hash, ip: ips[i], remove); |
| 5876 | if (err) { |
| 5877 | /* |
| 5878 | * This expects the @hash is a temporary hash and if this |
| 5879 | * fails the caller must free the @hash. |
| 5880 | */ |
| 5881 | return err; |
| 5882 | } |
| 5883 | } |
| 5884 | return 0; |
| 5885 | } |
| 5886 | |
| 5887 | static int |
| 5888 | ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len, |
| 5889 | unsigned long *ips, unsigned int cnt, |
| 5890 | int remove, int reset, int enable, char *mod) |
| 5891 | { |
| 5892 | struct ftrace_hash **orig_hash; |
| 5893 | struct ftrace_hash *hash; |
| 5894 | int ret; |
| 5895 | |
| 5896 | if (unlikely(ftrace_disabled)) |
| 5897 | return -ENODEV; |
| 5898 | |
| 5899 | mutex_lock(&ops->func_hash->regex_lock); |
| 5900 | |
| 5901 | if (enable) |
| 5902 | orig_hash = &ops->func_hash->filter_hash; |
| 5903 | else |
| 5904 | orig_hash = &ops->func_hash->notrace_hash; |
| 5905 | |
| 5906 | if (reset) |
| 5907 | hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS); |
| 5908 | else |
| 5909 | hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash: *orig_hash); |
| 5910 | |
| 5911 | if (!hash) { |
| 5912 | ret = -ENOMEM; |
| 5913 | goto out_regex_unlock; |
| 5914 | } |
| 5915 | |
| 5916 | if (buf && !match_records(hash, func: buf, len, mod)) { |
| 5917 | /* If this was for a module and nothing was enabled, flag it */ |
| 5918 | if (mod) |
| 5919 | (*orig_hash)->flags |= FTRACE_HASH_FL_MOD; |
| 5920 | |
| 5921 | /* |
| 5922 | * Even if it is a mod, return error to let caller know |
| 5923 | * nothing was added |
| 5924 | */ |
| 5925 | ret = -EINVAL; |
| 5926 | goto out_regex_unlock; |
| 5927 | } |
| 5928 | if (ips) { |
| 5929 | ret = ftrace_match_addr(hash, ips, cnt, remove); |
| 5930 | if (ret < 0) |
| 5931 | goto out_regex_unlock; |
| 5932 | } |
| 5933 | |
| 5934 | mutex_lock(&ftrace_lock); |
| 5935 | ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable); |
| 5936 | mutex_unlock(lock: &ftrace_lock); |
| 5937 | |
| 5938 | out_regex_unlock: |
| 5939 | mutex_unlock(lock: &ops->func_hash->regex_lock); |
| 5940 | |
| 5941 | free_ftrace_hash(hash); |
| 5942 | return ret; |
| 5943 | } |
| 5944 | |
| 5945 | static int |
| 5946 | ftrace_set_addr(struct ftrace_ops *ops, unsigned long *ips, unsigned int cnt, |
| 5947 | int remove, int reset, int enable) |
| 5948 | { |
| 5949 | return ftrace_set_hash(ops, NULL, len: 0, ips, cnt, remove, reset, enable, NULL); |
| 5950 | } |
| 5951 | |
| 5952 | #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS |
| 5953 | |
| 5954 | static int register_ftrace_function_nolock(struct ftrace_ops *ops); |
| 5955 | |
| 5956 | /* |
| 5957 | * If there are multiple ftrace_ops, use SAVE_REGS by default, so that direct |
| 5958 | * call will be jumped from ftrace_regs_caller. Only if the architecture does |
| 5959 | * not support ftrace_regs_caller but direct_call, use SAVE_ARGS so that it |
| 5960 | * jumps from ftrace_caller for multiple ftrace_ops. |
| 5961 | */ |
| 5962 | #ifndef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_REGS |
| 5963 | #define MULTI_FLAGS (FTRACE_OPS_FL_DIRECT | FTRACE_OPS_FL_SAVE_ARGS) |
| 5964 | #else |
| 5965 | #define MULTI_FLAGS (FTRACE_OPS_FL_DIRECT | FTRACE_OPS_FL_SAVE_REGS) |
| 5966 | #endif |
| 5967 | |
| 5968 | static int check_direct_multi(struct ftrace_ops *ops) |
| 5969 | { |
| 5970 | if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) |
| 5971 | return -EINVAL; |
| 5972 | if ((ops->flags & MULTI_FLAGS) != MULTI_FLAGS) |
| 5973 | return -EINVAL; |
| 5974 | return 0; |
| 5975 | } |
| 5976 | |
| 5977 | static void remove_direct_functions_hash(struct ftrace_hash *hash, unsigned long addr) |
| 5978 | { |
| 5979 | struct ftrace_func_entry *entry, *del; |
| 5980 | int size, i; |
| 5981 | |
| 5982 | size = 1 << hash->size_bits; |
| 5983 | for (i = 0; i < size; i++) { |
| 5984 | hlist_for_each_entry(entry, &hash->buckets[i], hlist) { |
| 5985 | del = __ftrace_lookup_ip(hash: direct_functions, ip: entry->ip); |
| 5986 | if (del && ftrace_jmp_get(addr: del->direct) == |
| 5987 | ftrace_jmp_get(addr)) { |
| 5988 | remove_hash_entry(hash: direct_functions, entry: del); |
| 5989 | kfree(objp: del); |
| 5990 | } |
| 5991 | } |
| 5992 | } |
| 5993 | } |
| 5994 | |
| 5995 | static void register_ftrace_direct_cb(struct rcu_head *rhp) |
| 5996 | { |
| 5997 | struct ftrace_hash *fhp = container_of(rhp, struct ftrace_hash, rcu); |
| 5998 | |
| 5999 | free_ftrace_hash(hash: fhp); |
| 6000 | } |
| 6001 | |
| 6002 | static void reset_direct(struct ftrace_ops *ops, unsigned long addr) |
| 6003 | { |
| 6004 | struct ftrace_hash *hash = ops->func_hash->filter_hash; |
| 6005 | |
| 6006 | remove_direct_functions_hash(hash, addr); |
| 6007 | |
| 6008 | /* cleanup for possible another register call */ |
| 6009 | ops->func = NULL; |
| 6010 | ops->trampoline = 0; |
| 6011 | } |
| 6012 | |
| 6013 | /** |
| 6014 | * register_ftrace_direct - Call a custom trampoline directly |
| 6015 | * for multiple functions registered in @ops |
| 6016 | * @ops: The address of the struct ftrace_ops object |
| 6017 | * @addr: The address of the trampoline to call at @ops functions |
| 6018 | * |
| 6019 | * This is used to connect a direct calls to @addr from the nop locations |
| 6020 | * of the functions registered in @ops (with by ftrace_set_filter_ip |
| 6021 | * function). |
| 6022 | * |
| 6023 | * The location that it calls (@addr) must be able to handle a direct call, |
| 6024 | * and save the parameters of the function being traced, and restore them |
| 6025 | * (or inject new ones if needed), before returning. |
| 6026 | * |
| 6027 | * Returns: |
| 6028 | * 0 on success |
| 6029 | * -EINVAL - The @ops object was already registered with this call or |
| 6030 | * when there are no functions in @ops object. |
| 6031 | * -EBUSY - Another direct function is already attached (there can be only one) |
| 6032 | * -ENODEV - @ip does not point to a ftrace nop location (or not supported) |
| 6033 | * -ENOMEM - There was an allocation failure. |
| 6034 | */ |
| 6035 | int register_ftrace_direct(struct ftrace_ops *ops, unsigned long addr) |
| 6036 | { |
| 6037 | struct ftrace_hash *hash, *new_hash = NULL, *free_hash = NULL; |
| 6038 | struct ftrace_func_entry *entry, *new; |
| 6039 | int err = -EBUSY, size, i; |
| 6040 | |
| 6041 | if (ops->func || ops->trampoline) |
| 6042 | return -EINVAL; |
| 6043 | if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) |
| 6044 | return -EINVAL; |
| 6045 | if (ops->flags & FTRACE_OPS_FL_ENABLED) |
| 6046 | return -EINVAL; |
| 6047 | |
| 6048 | hash = ops->func_hash->filter_hash; |
| 6049 | if (ftrace_hash_empty(hash)) |
| 6050 | return -EINVAL; |
| 6051 | |
| 6052 | /* This is a "raw" address, and this should never happen. */ |
| 6053 | if (WARN_ON_ONCE(ftrace_is_jmp(addr))) |
| 6054 | return -EINVAL; |
| 6055 | |
| 6056 | mutex_lock(&direct_mutex); |
| 6057 | |
| 6058 | if (ops->flags & FTRACE_OPS_FL_JMP) |
| 6059 | addr = ftrace_jmp_set(addr); |
| 6060 | |
| 6061 | /* Make sure requested entries are not already registered.. */ |
| 6062 | size = 1 << hash->size_bits; |
| 6063 | for (i = 0; i < size; i++) { |
| 6064 | hlist_for_each_entry(entry, &hash->buckets[i], hlist) { |
| 6065 | if (ftrace_find_rec_direct(ip: entry->ip)) |
| 6066 | goto out_unlock; |
| 6067 | } |
| 6068 | } |
| 6069 | |
| 6070 | err = -ENOMEM; |
| 6071 | |
| 6072 | /* Make a copy hash to place the new and the old entries in */ |
| 6073 | size = hash->count + direct_functions->count; |
| 6074 | size = fls(x: size); |
| 6075 | if (size > FTRACE_HASH_MAX_BITS) |
| 6076 | size = FTRACE_HASH_MAX_BITS; |
| 6077 | new_hash = alloc_ftrace_hash(size_bits: size); |
| 6078 | if (!new_hash) |
| 6079 | goto out_unlock; |
| 6080 | |
| 6081 | /* Now copy over the existing direct entries */ |
| 6082 | size = 1 << direct_functions->size_bits; |
| 6083 | for (i = 0; i < size; i++) { |
| 6084 | hlist_for_each_entry(entry, &direct_functions->buckets[i], hlist) { |
| 6085 | new = add_hash_entry(hash: new_hash, ip: entry->ip); |
| 6086 | if (!new) |
| 6087 | goto out_unlock; |
| 6088 | new->direct = entry->direct; |
| 6089 | } |
| 6090 | } |
| 6091 | |
| 6092 | /* ... and add the new entries */ |
| 6093 | size = 1 << hash->size_bits; |
| 6094 | for (i = 0; i < size; i++) { |
| 6095 | hlist_for_each_entry(entry, &hash->buckets[i], hlist) { |
| 6096 | new = add_hash_entry(hash: new_hash, ip: entry->ip); |
| 6097 | if (!new) |
| 6098 | goto out_unlock; |
| 6099 | /* Update both the copy and the hash entry */ |
| 6100 | new->direct = addr; |
| 6101 | entry->direct = addr; |
| 6102 | } |
| 6103 | } |
| 6104 | |
| 6105 | free_hash = direct_functions; |
| 6106 | rcu_assign_pointer(direct_functions, new_hash); |
| 6107 | new_hash = NULL; |
| 6108 | |
| 6109 | ops->func = call_direct_funcs; |
| 6110 | ops->flags |= MULTI_FLAGS; |
| 6111 | ops->trampoline = FTRACE_REGS_ADDR; |
| 6112 | ops->direct_call = addr; |
| 6113 | |
| 6114 | err = register_ftrace_function_nolock(ops); |
| 6115 | if (err) |
| 6116 | reset_direct(ops, addr); |
| 6117 | |
| 6118 | out_unlock: |
| 6119 | mutex_unlock(lock: &direct_mutex); |
| 6120 | |
| 6121 | if (free_hash && free_hash != EMPTY_HASH) |
| 6122 | call_rcu_tasks(head: &free_hash->rcu, func: register_ftrace_direct_cb); |
| 6123 | |
| 6124 | if (new_hash) |
| 6125 | free_ftrace_hash(hash: new_hash); |
| 6126 | |
| 6127 | return err; |
| 6128 | } |
| 6129 | EXPORT_SYMBOL_GPL(register_ftrace_direct); |
| 6130 | |
| 6131 | /** |
| 6132 | * unregister_ftrace_direct - Remove calls to custom trampoline |
| 6133 | * previously registered by register_ftrace_direct for @ops object. |
| 6134 | * @ops: The address of the struct ftrace_ops object |
| 6135 | * @addr: The address of the direct function that is called by the @ops functions |
| 6136 | * @free_filters: Set to true to remove all filters for the ftrace_ops, false otherwise |
| 6137 | * |
| 6138 | * This is used to remove a direct calls to @addr from the nop locations |
| 6139 | * of the functions registered in @ops (with by ftrace_set_filter_ip |
| 6140 | * function). |
| 6141 | * |
| 6142 | * Returns: |
| 6143 | * 0 on success |
| 6144 | * -EINVAL - The @ops object was not properly registered. |
| 6145 | */ |
| 6146 | int unregister_ftrace_direct(struct ftrace_ops *ops, unsigned long addr, |
| 6147 | bool free_filters) |
| 6148 | { |
| 6149 | int err; |
| 6150 | |
| 6151 | if (check_direct_multi(ops)) |
| 6152 | return -EINVAL; |
| 6153 | if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) |
| 6154 | return -EINVAL; |
| 6155 | |
| 6156 | mutex_lock(&direct_mutex); |
| 6157 | err = unregister_ftrace_function(ops); |
| 6158 | reset_direct(ops, addr); |
| 6159 | mutex_unlock(lock: &direct_mutex); |
| 6160 | |
| 6161 | if (free_filters) |
| 6162 | ftrace_free_filter(ops); |
| 6163 | return err; |
| 6164 | } |
| 6165 | EXPORT_SYMBOL_GPL(unregister_ftrace_direct); |
| 6166 | |
| 6167 | static int |
| 6168 | __modify_ftrace_direct(struct ftrace_ops *ops, unsigned long addr) |
| 6169 | { |
| 6170 | struct ftrace_hash *hash = ops->func_hash->filter_hash; |
| 6171 | struct ftrace_func_entry *entry, *iter; |
| 6172 | static struct ftrace_ops tmp_ops = { |
| 6173 | .func = ftrace_stub, |
| 6174 | .flags = FTRACE_OPS_FL_STUB, |
| 6175 | }; |
| 6176 | int i, size; |
| 6177 | int err; |
| 6178 | |
| 6179 | lockdep_assert_held_once(&direct_mutex); |
| 6180 | |
| 6181 | /* This is a "raw" address, and this should never happen. */ |
| 6182 | if (WARN_ON_ONCE(ftrace_is_jmp(addr))) |
| 6183 | return -EINVAL; |
| 6184 | |
| 6185 | if (ops->flags & FTRACE_OPS_FL_JMP) |
| 6186 | addr = ftrace_jmp_set(addr); |
| 6187 | |
| 6188 | /* Enable the tmp_ops to have the same functions as the direct ops */ |
| 6189 | ftrace_ops_init(ops: &tmp_ops); |
| 6190 | tmp_ops.func_hash = ops->func_hash; |
| 6191 | tmp_ops.direct_call = addr; |
| 6192 | |
| 6193 | err = register_ftrace_function_nolock(ops: &tmp_ops); |
| 6194 | if (err) |
| 6195 | return err; |
| 6196 | |
| 6197 | /* |
| 6198 | * Call __ftrace_hash_update_ipmodify() here, so that we can call |
| 6199 | * ops->ops_func for the ops. This is needed because the above |
| 6200 | * register_ftrace_function_nolock() worked on tmp_ops. |
| 6201 | */ |
| 6202 | err = __ftrace_hash_update_ipmodify(ops, old_hash: hash, new_hash: hash, update_target: true); |
| 6203 | if (err) |
| 6204 | goto out; |
| 6205 | |
| 6206 | /* |
| 6207 | * Now the ftrace_ops_list_func() is called to do the direct callers. |
| 6208 | * We can safely change the direct functions attached to each entry. |
| 6209 | */ |
| 6210 | mutex_lock(&ftrace_lock); |
| 6211 | |
| 6212 | size = 1 << hash->size_bits; |
| 6213 | for (i = 0; i < size; i++) { |
| 6214 | hlist_for_each_entry(iter, &hash->buckets[i], hlist) { |
| 6215 | entry = __ftrace_lookup_ip(hash: direct_functions, ip: iter->ip); |
| 6216 | if (!entry) |
| 6217 | continue; |
| 6218 | entry->direct = addr; |
| 6219 | } |
| 6220 | } |
| 6221 | /* Prevent store tearing if a trampoline concurrently accesses the value */ |
| 6222 | WRITE_ONCE(ops->direct_call, addr); |
| 6223 | |
| 6224 | mutex_unlock(lock: &ftrace_lock); |
| 6225 | |
| 6226 | out: |
| 6227 | /* Removing the tmp_ops will add the updated direct callers to the functions */ |
| 6228 | unregister_ftrace_function(ops: &tmp_ops); |
| 6229 | |
| 6230 | return err; |
| 6231 | } |
| 6232 | |
| 6233 | /** |
| 6234 | * modify_ftrace_direct_nolock - Modify an existing direct 'multi' call |
| 6235 | * to call something else |
| 6236 | * @ops: The address of the struct ftrace_ops object |
| 6237 | * @addr: The address of the new trampoline to call at @ops functions |
| 6238 | * |
| 6239 | * This is used to unregister currently registered direct caller and |
| 6240 | * register new one @addr on functions registered in @ops object. |
| 6241 | * |
| 6242 | * Note there's window between ftrace_shutdown and ftrace_startup calls |
| 6243 | * where there will be no callbacks called. |
| 6244 | * |
| 6245 | * Caller should already have direct_mutex locked, so we don't lock |
| 6246 | * direct_mutex here. |
| 6247 | * |
| 6248 | * Returns: zero on success. Non zero on error, which includes: |
| 6249 | * -EINVAL - The @ops object was not properly registered. |
| 6250 | */ |
| 6251 | int modify_ftrace_direct_nolock(struct ftrace_ops *ops, unsigned long addr) |
| 6252 | { |
| 6253 | if (check_direct_multi(ops)) |
| 6254 | return -EINVAL; |
| 6255 | if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) |
| 6256 | return -EINVAL; |
| 6257 | |
| 6258 | return __modify_ftrace_direct(ops, addr); |
| 6259 | } |
| 6260 | EXPORT_SYMBOL_GPL(modify_ftrace_direct_nolock); |
| 6261 | |
| 6262 | /** |
| 6263 | * modify_ftrace_direct - Modify an existing direct 'multi' call |
| 6264 | * to call something else |
| 6265 | * @ops: The address of the struct ftrace_ops object |
| 6266 | * @addr: The address of the new trampoline to call at @ops functions |
| 6267 | * |
| 6268 | * This is used to unregister currently registered direct caller and |
| 6269 | * register new one @addr on functions registered in @ops object. |
| 6270 | * |
| 6271 | * Note there's window between ftrace_shutdown and ftrace_startup calls |
| 6272 | * where there will be no callbacks called. |
| 6273 | * |
| 6274 | * Returns: zero on success. Non zero on error, which includes: |
| 6275 | * -EINVAL - The @ops object was not properly registered. |
| 6276 | */ |
| 6277 | int modify_ftrace_direct(struct ftrace_ops *ops, unsigned long addr) |
| 6278 | { |
| 6279 | int err; |
| 6280 | |
| 6281 | if (check_direct_multi(ops)) |
| 6282 | return -EINVAL; |
| 6283 | if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) |
| 6284 | return -EINVAL; |
| 6285 | |
| 6286 | mutex_lock(&direct_mutex); |
| 6287 | err = __modify_ftrace_direct(ops, addr); |
| 6288 | mutex_unlock(lock: &direct_mutex); |
| 6289 | return err; |
| 6290 | } |
| 6291 | EXPORT_SYMBOL_GPL(modify_ftrace_direct); |
| 6292 | #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */ |
| 6293 | |
| 6294 | /** |
| 6295 | * ftrace_set_filter_ip - set a function to filter on in ftrace by address |
| 6296 | * @ops: the ops to set the filter with |
| 6297 | * @ip: the address to add to or remove from the filter. |
| 6298 | * @remove: non zero to remove the ip from the filter |
| 6299 | * @reset: non zero to reset all filters before applying this filter. |
| 6300 | * |
| 6301 | * Filters denote which functions should be enabled when tracing is enabled |
| 6302 | * If @ip is NULL, it fails to update filter. |
| 6303 | * |
| 6304 | * This can allocate memory which must be freed before @ops can be freed, |
| 6305 | * either by removing each filtered addr or by using |
| 6306 | * ftrace_free_filter(@ops). |
| 6307 | */ |
| 6308 | int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip, |
| 6309 | int remove, int reset) |
| 6310 | { |
| 6311 | ftrace_ops_init(ops); |
| 6312 | return ftrace_set_addr(ops, ips: &ip, cnt: 1, remove, reset, enable: 1); |
| 6313 | } |
| 6314 | EXPORT_SYMBOL_GPL(ftrace_set_filter_ip); |
| 6315 | |
| 6316 | /** |
| 6317 | * ftrace_set_filter_ips - set functions to filter on in ftrace by addresses |
| 6318 | * @ops: the ops to set the filter with |
| 6319 | * @ips: the array of addresses to add to or remove from the filter. |
| 6320 | * @cnt: the number of addresses in @ips |
| 6321 | * @remove: non zero to remove ips from the filter |
| 6322 | * @reset: non zero to reset all filters before applying this filter. |
| 6323 | * |
| 6324 | * Filters denote which functions should be enabled when tracing is enabled |
| 6325 | * If @ips array or any ip specified within is NULL , it fails to update filter. |
| 6326 | * |
| 6327 | * This can allocate memory which must be freed before @ops can be freed, |
| 6328 | * either by removing each filtered addr or by using |
| 6329 | * ftrace_free_filter(@ops). |
| 6330 | */ |
| 6331 | int ftrace_set_filter_ips(struct ftrace_ops *ops, unsigned long *ips, |
| 6332 | unsigned int cnt, int remove, int reset) |
| 6333 | { |
| 6334 | ftrace_ops_init(ops); |
| 6335 | return ftrace_set_addr(ops, ips, cnt, remove, reset, enable: 1); |
| 6336 | } |
| 6337 | EXPORT_SYMBOL_GPL(ftrace_set_filter_ips); |
| 6338 | |
| 6339 | /** |
| 6340 | * ftrace_ops_set_global_filter - setup ops to use global filters |
| 6341 | * @ops: the ops which will use the global filters |
| 6342 | * |
| 6343 | * ftrace users who need global function trace filtering should call this. |
| 6344 | * It can set the global filter only if ops were not initialized before. |
| 6345 | */ |
| 6346 | void ftrace_ops_set_global_filter(struct ftrace_ops *ops) |
| 6347 | { |
| 6348 | if (ops->flags & FTRACE_OPS_FL_INITIALIZED) |
| 6349 | return; |
| 6350 | |
| 6351 | ftrace_ops_init(ops); |
| 6352 | ops->func_hash = &global_ops.local_hash; |
| 6353 | } |
| 6354 | EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter); |
| 6355 | |
| 6356 | static int |
| 6357 | ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len, |
| 6358 | int reset, int enable) |
| 6359 | { |
| 6360 | char *mod = NULL, *func, *command, *next = buf; |
| 6361 | char *tmp __free(kfree) = NULL; |
| 6362 | struct trace_array *tr = ops->private; |
| 6363 | int ret; |
| 6364 | |
| 6365 | func = strsep(&next, ":" ); |
| 6366 | |
| 6367 | /* This can also handle :mod: parsing */ |
| 6368 | if (next) { |
| 6369 | if (!tr) |
| 6370 | return -EINVAL; |
| 6371 | |
| 6372 | command = strsep(&next, ":" ); |
| 6373 | if (strcmp(command, "mod" ) != 0) |
| 6374 | return -EINVAL; |
| 6375 | |
| 6376 | mod = next; |
| 6377 | len = command - func; |
| 6378 | /* Save the original func as ftrace_set_hash() can modify it */ |
| 6379 | tmp = kstrdup(s: func, GFP_KERNEL); |
| 6380 | } |
| 6381 | |
| 6382 | ret = ftrace_set_hash(ops, buf: func, len, NULL, cnt: 0, remove: 0, reset, enable, mod); |
| 6383 | |
| 6384 | if (tr && mod && ret < 0) { |
| 6385 | /* Did tmp fail to allocate? */ |
| 6386 | if (!tmp) |
| 6387 | return -ENOMEM; |
| 6388 | ret = cache_mod(tr, func: tmp, module: mod, enable); |
| 6389 | } |
| 6390 | |
| 6391 | return ret; |
| 6392 | } |
| 6393 | |
| 6394 | /** |
| 6395 | * ftrace_set_filter - set a function to filter on in ftrace |
| 6396 | * @ops: the ops to set the filter with |
| 6397 | * @buf: the string that holds the function filter text. |
| 6398 | * @len: the length of the string. |
| 6399 | * @reset: non-zero to reset all filters before applying this filter. |
| 6400 | * |
| 6401 | * Filters denote which functions should be enabled when tracing is enabled. |
| 6402 | * If @buf is NULL and reset is set, all functions will be enabled for tracing. |
| 6403 | * |
| 6404 | * This can allocate memory which must be freed before @ops can be freed, |
| 6405 | * either by removing each filtered addr or by using |
| 6406 | * ftrace_free_filter(@ops). |
| 6407 | */ |
| 6408 | int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf, |
| 6409 | int len, int reset) |
| 6410 | { |
| 6411 | ftrace_ops_init(ops); |
| 6412 | return ftrace_set_regex(ops, buf, len, reset, enable: 1); |
| 6413 | } |
| 6414 | EXPORT_SYMBOL_GPL(ftrace_set_filter); |
| 6415 | |
| 6416 | /** |
| 6417 | * ftrace_set_notrace - set a function to not trace in ftrace |
| 6418 | * @ops: the ops to set the notrace filter with |
| 6419 | * @buf: the string that holds the function notrace text. |
| 6420 | * @len: the length of the string. |
| 6421 | * @reset: non-zero to reset all filters before applying this filter. |
| 6422 | * |
| 6423 | * Notrace Filters denote which functions should not be enabled when tracing |
| 6424 | * is enabled. If @buf is NULL and reset is set, all functions will be enabled |
| 6425 | * for tracing. |
| 6426 | * |
| 6427 | * This can allocate memory which must be freed before @ops can be freed, |
| 6428 | * either by removing each filtered addr or by using |
| 6429 | * ftrace_free_filter(@ops). |
| 6430 | */ |
| 6431 | int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf, |
| 6432 | int len, int reset) |
| 6433 | { |
| 6434 | ftrace_ops_init(ops); |
| 6435 | return ftrace_set_regex(ops, buf, len, reset, enable: 0); |
| 6436 | } |
| 6437 | EXPORT_SYMBOL_GPL(ftrace_set_notrace); |
| 6438 | /** |
| 6439 | * ftrace_set_global_filter - set a function to filter on with global tracers |
| 6440 | * @buf: the string that holds the function filter text. |
| 6441 | * @len: the length of the string. |
| 6442 | * @reset: non-zero to reset all filters before applying this filter. |
| 6443 | * |
| 6444 | * Filters denote which functions should be enabled when tracing is enabled. |
| 6445 | * If @buf is NULL and reset is set, all functions will be enabled for tracing. |
| 6446 | */ |
| 6447 | void ftrace_set_global_filter(unsigned char *buf, int len, int reset) |
| 6448 | { |
| 6449 | ftrace_set_regex(ops: &global_ops, buf, len, reset, enable: 1); |
| 6450 | } |
| 6451 | EXPORT_SYMBOL_GPL(ftrace_set_global_filter); |
| 6452 | |
| 6453 | /** |
| 6454 | * ftrace_set_global_notrace - set a function to not trace with global tracers |
| 6455 | * @buf: the string that holds the function notrace text. |
| 6456 | * @len: the length of the string. |
| 6457 | * @reset: non-zero to reset all filters before applying this filter. |
| 6458 | * |
| 6459 | * Notrace Filters denote which functions should not be enabled when tracing |
| 6460 | * is enabled. If @buf is NULL and reset is set, all functions will be enabled |
| 6461 | * for tracing. |
| 6462 | */ |
| 6463 | void ftrace_set_global_notrace(unsigned char *buf, int len, int reset) |
| 6464 | { |
| 6465 | ftrace_set_regex(ops: &global_ops, buf, len, reset, enable: 0); |
| 6466 | } |
| 6467 | EXPORT_SYMBOL_GPL(ftrace_set_global_notrace); |
| 6468 | |
| 6469 | /* |
| 6470 | * command line interface to allow users to set filters on boot up. |
| 6471 | */ |
| 6472 | #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE |
| 6473 | static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata; |
| 6474 | static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata; |
| 6475 | |
| 6476 | /* Used by function selftest to not test if filter is set */ |
| 6477 | bool ftrace_filter_param __initdata; |
| 6478 | |
| 6479 | static int __init set_ftrace_notrace(char *str) |
| 6480 | { |
| 6481 | ftrace_filter_param = true; |
| 6482 | strscpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE); |
| 6483 | return 1; |
| 6484 | } |
| 6485 | __setup("ftrace_notrace=" , set_ftrace_notrace); |
| 6486 | |
| 6487 | static int __init set_ftrace_filter(char *str) |
| 6488 | { |
| 6489 | ftrace_filter_param = true; |
| 6490 | strscpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE); |
| 6491 | return 1; |
| 6492 | } |
| 6493 | __setup("ftrace_filter=" , set_ftrace_filter); |
| 6494 | |
| 6495 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 6496 | static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata; |
| 6497 | static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata; |
| 6498 | static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer); |
| 6499 | |
| 6500 | static int __init set_graph_function(char *str) |
| 6501 | { |
| 6502 | strscpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE); |
| 6503 | return 1; |
| 6504 | } |
| 6505 | __setup("ftrace_graph_filter=" , set_graph_function); |
| 6506 | |
| 6507 | static int __init set_graph_notrace_function(char *str) |
| 6508 | { |
| 6509 | strscpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE); |
| 6510 | return 1; |
| 6511 | } |
| 6512 | __setup("ftrace_graph_notrace=" , set_graph_notrace_function); |
| 6513 | |
| 6514 | static int __init set_graph_max_depth_function(char *str) |
| 6515 | { |
| 6516 | if (!str || kstrtouint(s: str, base: 0, res: &fgraph_max_depth)) |
| 6517 | return 0; |
| 6518 | return 1; |
| 6519 | } |
| 6520 | __setup("ftrace_graph_max_depth=" , set_graph_max_depth_function); |
| 6521 | |
| 6522 | static void __init set_ftrace_early_graph(char *buf, int enable) |
| 6523 | { |
| 6524 | int ret; |
| 6525 | char *func; |
| 6526 | struct ftrace_hash *hash; |
| 6527 | |
| 6528 | hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS); |
| 6529 | if (MEM_FAIL(!hash, "Failed to allocate hash\n" )) |
| 6530 | return; |
| 6531 | |
| 6532 | while (buf) { |
| 6533 | func = strsep(&buf, "," ); |
| 6534 | /* we allow only one expression at a time */ |
| 6535 | ret = ftrace_graph_set_hash(hash, buffer: func); |
| 6536 | if (ret) |
| 6537 | printk(KERN_DEBUG "ftrace: function %s not " |
| 6538 | "traceable\n" , func); |
| 6539 | } |
| 6540 | |
| 6541 | if (enable) |
| 6542 | ftrace_graph_hash = hash; |
| 6543 | else |
| 6544 | ftrace_graph_notrace_hash = hash; |
| 6545 | } |
| 6546 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 6547 | |
| 6548 | void __init |
| 6549 | ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable) |
| 6550 | { |
| 6551 | char *func; |
| 6552 | |
| 6553 | ftrace_ops_init(ops); |
| 6554 | |
| 6555 | /* The trace_array is needed for caching module function filters */ |
| 6556 | if (!ops->private) { |
| 6557 | struct trace_array *tr = trace_get_global_array(); |
| 6558 | |
| 6559 | ops->private = tr; |
| 6560 | ftrace_init_trace_array(tr); |
| 6561 | } |
| 6562 | |
| 6563 | while (buf) { |
| 6564 | func = strsep(&buf, "," ); |
| 6565 | ftrace_set_regex(ops, buf: func, strlen(func), reset: 0, enable); |
| 6566 | } |
| 6567 | } |
| 6568 | |
| 6569 | static void __init set_ftrace_early_filters(void) |
| 6570 | { |
| 6571 | if (ftrace_filter_buf[0]) |
| 6572 | ftrace_set_early_filter(ops: &global_ops, buf: ftrace_filter_buf, enable: 1); |
| 6573 | if (ftrace_notrace_buf[0]) |
| 6574 | ftrace_set_early_filter(ops: &global_ops, buf: ftrace_notrace_buf, enable: 0); |
| 6575 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 6576 | if (ftrace_graph_buf[0]) |
| 6577 | set_ftrace_early_graph(buf: ftrace_graph_buf, enable: 1); |
| 6578 | if (ftrace_graph_notrace_buf[0]) |
| 6579 | set_ftrace_early_graph(buf: ftrace_graph_notrace_buf, enable: 0); |
| 6580 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 6581 | } |
| 6582 | |
| 6583 | int ftrace_regex_release(struct inode *inode, struct file *file) |
| 6584 | { |
| 6585 | struct seq_file *m = (struct seq_file *)file->private_data; |
| 6586 | struct ftrace_iterator *iter; |
| 6587 | struct ftrace_hash **orig_hash; |
| 6588 | struct trace_parser *parser; |
| 6589 | int filter_hash; |
| 6590 | |
| 6591 | if (file->f_mode & FMODE_READ) { |
| 6592 | iter = m->private; |
| 6593 | seq_release(inode, file); |
| 6594 | } else |
| 6595 | iter = file->private_data; |
| 6596 | |
| 6597 | parser = &iter->parser; |
| 6598 | if (trace_parser_loaded(parser)) { |
| 6599 | int enable = !(iter->flags & FTRACE_ITER_NOTRACE); |
| 6600 | |
| 6601 | ftrace_process_regex(iter, buff: parser->buffer, |
| 6602 | len: parser->idx, enable); |
| 6603 | } |
| 6604 | |
| 6605 | trace_parser_put(parser); |
| 6606 | |
| 6607 | mutex_lock(&iter->ops->func_hash->regex_lock); |
| 6608 | |
| 6609 | if (file->f_mode & FMODE_WRITE) { |
| 6610 | filter_hash = !!(iter->flags & FTRACE_ITER_FILTER); |
| 6611 | |
| 6612 | if (filter_hash) { |
| 6613 | orig_hash = &iter->ops->func_hash->filter_hash; |
| 6614 | if (iter->tr) { |
| 6615 | if (list_empty(head: &iter->tr->mod_trace)) |
| 6616 | iter->hash->flags &= ~FTRACE_HASH_FL_MOD; |
| 6617 | else |
| 6618 | iter->hash->flags |= FTRACE_HASH_FL_MOD; |
| 6619 | } |
| 6620 | } else |
| 6621 | orig_hash = &iter->ops->func_hash->notrace_hash; |
| 6622 | |
| 6623 | mutex_lock(&ftrace_lock); |
| 6624 | ftrace_hash_move_and_update_ops(ops: iter->ops, orig_hash, |
| 6625 | hash: iter->hash, enable: filter_hash); |
| 6626 | mutex_unlock(lock: &ftrace_lock); |
| 6627 | } |
| 6628 | |
| 6629 | mutex_unlock(lock: &iter->ops->func_hash->regex_lock); |
| 6630 | free_ftrace_hash(hash: iter->hash); |
| 6631 | if (iter->tr) |
| 6632 | trace_array_put(tr: iter->tr); |
| 6633 | kfree(objp: iter); |
| 6634 | |
| 6635 | return 0; |
| 6636 | } |
| 6637 | |
| 6638 | static const struct file_operations ftrace_avail_fops = { |
| 6639 | .open = ftrace_avail_open, |
| 6640 | .read = seq_read, |
| 6641 | .llseek = seq_lseek, |
| 6642 | .release = seq_release_private, |
| 6643 | }; |
| 6644 | |
| 6645 | static const struct file_operations ftrace_enabled_fops = { |
| 6646 | .open = ftrace_enabled_open, |
| 6647 | .read = seq_read, |
| 6648 | .llseek = seq_lseek, |
| 6649 | .release = seq_release_private, |
| 6650 | }; |
| 6651 | |
| 6652 | static const struct file_operations ftrace_touched_fops = { |
| 6653 | .open = ftrace_touched_open, |
| 6654 | .read = seq_read, |
| 6655 | .llseek = seq_lseek, |
| 6656 | .release = seq_release_private, |
| 6657 | }; |
| 6658 | |
| 6659 | static const struct file_operations ftrace_avail_addrs_fops = { |
| 6660 | .open = ftrace_avail_addrs_open, |
| 6661 | .read = seq_read, |
| 6662 | .llseek = seq_lseek, |
| 6663 | .release = seq_release_private, |
| 6664 | }; |
| 6665 | |
| 6666 | static const struct file_operations ftrace_filter_fops = { |
| 6667 | .open = ftrace_filter_open, |
| 6668 | .read = seq_read, |
| 6669 | .write = ftrace_filter_write, |
| 6670 | .llseek = tracing_lseek, |
| 6671 | .release = ftrace_regex_release, |
| 6672 | }; |
| 6673 | |
| 6674 | static const struct file_operations ftrace_notrace_fops = { |
| 6675 | .open = ftrace_notrace_open, |
| 6676 | .read = seq_read, |
| 6677 | .write = ftrace_notrace_write, |
| 6678 | .llseek = tracing_lseek, |
| 6679 | .release = ftrace_regex_release, |
| 6680 | }; |
| 6681 | |
| 6682 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 6683 | |
| 6684 | static DEFINE_MUTEX(graph_lock); |
| 6685 | |
| 6686 | struct ftrace_hash __rcu *ftrace_graph_hash = EMPTY_HASH; |
| 6687 | struct ftrace_hash __rcu *ftrace_graph_notrace_hash = EMPTY_HASH; |
| 6688 | |
| 6689 | enum graph_filter_type { |
| 6690 | GRAPH_FILTER_NOTRACE = 0, |
| 6691 | GRAPH_FILTER_FUNCTION, |
| 6692 | }; |
| 6693 | |
| 6694 | #define FTRACE_GRAPH_EMPTY ((void *)1) |
| 6695 | |
| 6696 | struct ftrace_graph_data { |
| 6697 | struct ftrace_hash *hash; |
| 6698 | struct ftrace_func_entry *entry; |
| 6699 | int idx; /* for hash table iteration */ |
| 6700 | enum graph_filter_type type; |
| 6701 | struct ftrace_hash *new_hash; |
| 6702 | const struct seq_operations *seq_ops; |
| 6703 | struct trace_parser parser; |
| 6704 | }; |
| 6705 | |
| 6706 | static void * |
| 6707 | __g_next(struct seq_file *m, loff_t *pos) |
| 6708 | { |
| 6709 | struct ftrace_graph_data *fgd = m->private; |
| 6710 | struct ftrace_func_entry *entry = fgd->entry; |
| 6711 | struct hlist_head *head; |
| 6712 | int i, idx = fgd->idx; |
| 6713 | |
| 6714 | if (*pos >= fgd->hash->count) |
| 6715 | return NULL; |
| 6716 | |
| 6717 | if (entry) { |
| 6718 | hlist_for_each_entry_continue(entry, hlist) { |
| 6719 | fgd->entry = entry; |
| 6720 | return entry; |
| 6721 | } |
| 6722 | |
| 6723 | idx++; |
| 6724 | } |
| 6725 | |
| 6726 | for (i = idx; i < 1 << fgd->hash->size_bits; i++) { |
| 6727 | head = &fgd->hash->buckets[i]; |
| 6728 | hlist_for_each_entry(entry, head, hlist) { |
| 6729 | fgd->entry = entry; |
| 6730 | fgd->idx = i; |
| 6731 | return entry; |
| 6732 | } |
| 6733 | } |
| 6734 | return NULL; |
| 6735 | } |
| 6736 | |
| 6737 | static void * |
| 6738 | g_next(struct seq_file *m, void *v, loff_t *pos) |
| 6739 | { |
| 6740 | (*pos)++; |
| 6741 | return __g_next(m, pos); |
| 6742 | } |
| 6743 | |
| 6744 | static void *g_start(struct seq_file *m, loff_t *pos) |
| 6745 | { |
| 6746 | struct ftrace_graph_data *fgd = m->private; |
| 6747 | |
| 6748 | mutex_lock(&graph_lock); |
| 6749 | |
| 6750 | if (fgd->type == GRAPH_FILTER_FUNCTION) |
| 6751 | fgd->hash = rcu_dereference_protected(ftrace_graph_hash, |
| 6752 | lockdep_is_held(&graph_lock)); |
| 6753 | else |
| 6754 | fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash, |
| 6755 | lockdep_is_held(&graph_lock)); |
| 6756 | |
| 6757 | /* Nothing, tell g_show to print all functions are enabled */ |
| 6758 | if (ftrace_hash_empty(hash: fgd->hash) && !*pos) |
| 6759 | return FTRACE_GRAPH_EMPTY; |
| 6760 | |
| 6761 | fgd->idx = 0; |
| 6762 | fgd->entry = NULL; |
| 6763 | return __g_next(m, pos); |
| 6764 | } |
| 6765 | |
| 6766 | static void g_stop(struct seq_file *m, void *p) |
| 6767 | { |
| 6768 | mutex_unlock(lock: &graph_lock); |
| 6769 | } |
| 6770 | |
| 6771 | static int g_show(struct seq_file *m, void *v) |
| 6772 | { |
| 6773 | struct ftrace_func_entry *entry = v; |
| 6774 | |
| 6775 | if (!entry) |
| 6776 | return 0; |
| 6777 | |
| 6778 | if (entry == FTRACE_GRAPH_EMPTY) { |
| 6779 | struct ftrace_graph_data *fgd = m->private; |
| 6780 | |
| 6781 | if (fgd->type == GRAPH_FILTER_FUNCTION) |
| 6782 | seq_puts(m, s: "#### all functions enabled ####\n" ); |
| 6783 | else |
| 6784 | seq_puts(m, s: "#### no functions disabled ####\n" ); |
| 6785 | return 0; |
| 6786 | } |
| 6787 | |
| 6788 | seq_printf(m, fmt: "%ps\n" , (void *)entry->ip); |
| 6789 | |
| 6790 | return 0; |
| 6791 | } |
| 6792 | |
| 6793 | static const struct seq_operations ftrace_graph_seq_ops = { |
| 6794 | .start = g_start, |
| 6795 | .next = g_next, |
| 6796 | .stop = g_stop, |
| 6797 | .show = g_show, |
| 6798 | }; |
| 6799 | |
| 6800 | static int |
| 6801 | __ftrace_graph_open(struct inode *inode, struct file *file, |
| 6802 | struct ftrace_graph_data *fgd) |
| 6803 | { |
| 6804 | int ret; |
| 6805 | struct ftrace_hash *new_hash = NULL; |
| 6806 | |
| 6807 | ret = security_locked_down(what: LOCKDOWN_TRACEFS); |
| 6808 | if (ret) |
| 6809 | return ret; |
| 6810 | |
| 6811 | if (file->f_mode & FMODE_WRITE) { |
| 6812 | const int size_bits = FTRACE_HASH_DEFAULT_BITS; |
| 6813 | |
| 6814 | if (trace_parser_get_init(parser: &fgd->parser, FTRACE_BUFF_MAX)) |
| 6815 | return -ENOMEM; |
| 6816 | |
| 6817 | if (file->f_flags & O_TRUNC) |
| 6818 | new_hash = alloc_ftrace_hash(size_bits); |
| 6819 | else |
| 6820 | new_hash = alloc_and_copy_ftrace_hash(size_bits, |
| 6821 | hash: fgd->hash); |
| 6822 | if (!new_hash) { |
| 6823 | ret = -ENOMEM; |
| 6824 | goto out; |
| 6825 | } |
| 6826 | } |
| 6827 | |
| 6828 | if (file->f_mode & FMODE_READ) { |
| 6829 | ret = seq_open(file, &ftrace_graph_seq_ops); |
| 6830 | if (!ret) { |
| 6831 | struct seq_file *m = file->private_data; |
| 6832 | m->private = fgd; |
| 6833 | } else { |
| 6834 | /* Failed */ |
| 6835 | free_ftrace_hash(hash: new_hash); |
| 6836 | new_hash = NULL; |
| 6837 | } |
| 6838 | } else |
| 6839 | file->private_data = fgd; |
| 6840 | |
| 6841 | out: |
| 6842 | if (ret < 0 && file->f_mode & FMODE_WRITE) |
| 6843 | trace_parser_put(parser: &fgd->parser); |
| 6844 | |
| 6845 | fgd->new_hash = new_hash; |
| 6846 | |
| 6847 | /* |
| 6848 | * All uses of fgd->hash must be taken with the graph_lock |
| 6849 | * held. The graph_lock is going to be released, so force |
| 6850 | * fgd->hash to be reinitialized when it is taken again. |
| 6851 | */ |
| 6852 | fgd->hash = NULL; |
| 6853 | |
| 6854 | return ret; |
| 6855 | } |
| 6856 | |
| 6857 | static int |
| 6858 | ftrace_graph_open(struct inode *inode, struct file *file) |
| 6859 | { |
| 6860 | struct ftrace_graph_data *fgd; |
| 6861 | int ret; |
| 6862 | |
| 6863 | if (unlikely(ftrace_disabled)) |
| 6864 | return -ENODEV; |
| 6865 | |
| 6866 | fgd = kmalloc(sizeof(*fgd), GFP_KERNEL); |
| 6867 | if (fgd == NULL) |
| 6868 | return -ENOMEM; |
| 6869 | |
| 6870 | mutex_lock(&graph_lock); |
| 6871 | |
| 6872 | fgd->hash = rcu_dereference_protected(ftrace_graph_hash, |
| 6873 | lockdep_is_held(&graph_lock)); |
| 6874 | fgd->type = GRAPH_FILTER_FUNCTION; |
| 6875 | fgd->seq_ops = &ftrace_graph_seq_ops; |
| 6876 | |
| 6877 | ret = __ftrace_graph_open(inode, file, fgd); |
| 6878 | if (ret < 0) |
| 6879 | kfree(objp: fgd); |
| 6880 | |
| 6881 | mutex_unlock(lock: &graph_lock); |
| 6882 | return ret; |
| 6883 | } |
| 6884 | |
| 6885 | static int |
| 6886 | ftrace_graph_notrace_open(struct inode *inode, struct file *file) |
| 6887 | { |
| 6888 | struct ftrace_graph_data *fgd; |
| 6889 | int ret; |
| 6890 | |
| 6891 | if (unlikely(ftrace_disabled)) |
| 6892 | return -ENODEV; |
| 6893 | |
| 6894 | fgd = kmalloc(sizeof(*fgd), GFP_KERNEL); |
| 6895 | if (fgd == NULL) |
| 6896 | return -ENOMEM; |
| 6897 | |
| 6898 | mutex_lock(&graph_lock); |
| 6899 | |
| 6900 | fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash, |
| 6901 | lockdep_is_held(&graph_lock)); |
| 6902 | fgd->type = GRAPH_FILTER_NOTRACE; |
| 6903 | fgd->seq_ops = &ftrace_graph_seq_ops; |
| 6904 | |
| 6905 | ret = __ftrace_graph_open(inode, file, fgd); |
| 6906 | if (ret < 0) |
| 6907 | kfree(objp: fgd); |
| 6908 | |
| 6909 | mutex_unlock(lock: &graph_lock); |
| 6910 | return ret; |
| 6911 | } |
| 6912 | |
| 6913 | static int |
| 6914 | ftrace_graph_release(struct inode *inode, struct file *file) |
| 6915 | { |
| 6916 | struct ftrace_graph_data *fgd; |
| 6917 | struct ftrace_hash *old_hash, *new_hash; |
| 6918 | struct trace_parser *parser; |
| 6919 | int ret = 0; |
| 6920 | |
| 6921 | if (file->f_mode & FMODE_READ) { |
| 6922 | struct seq_file *m = file->private_data; |
| 6923 | |
| 6924 | fgd = m->private; |
| 6925 | seq_release(inode, file); |
| 6926 | } else { |
| 6927 | fgd = file->private_data; |
| 6928 | } |
| 6929 | |
| 6930 | |
| 6931 | if (file->f_mode & FMODE_WRITE) { |
| 6932 | |
| 6933 | parser = &fgd->parser; |
| 6934 | |
| 6935 | if (trace_parser_loaded((parser))) { |
| 6936 | ret = ftrace_graph_set_hash(hash: fgd->new_hash, |
| 6937 | buffer: parser->buffer); |
| 6938 | } |
| 6939 | |
| 6940 | trace_parser_put(parser); |
| 6941 | |
| 6942 | new_hash = __ftrace_hash_move(src: fgd->new_hash); |
| 6943 | if (!new_hash) { |
| 6944 | ret = -ENOMEM; |
| 6945 | goto out; |
| 6946 | } |
| 6947 | |
| 6948 | mutex_lock(&graph_lock); |
| 6949 | |
| 6950 | if (fgd->type == GRAPH_FILTER_FUNCTION) { |
| 6951 | old_hash = rcu_dereference_protected(ftrace_graph_hash, |
| 6952 | lockdep_is_held(&graph_lock)); |
| 6953 | rcu_assign_pointer(ftrace_graph_hash, new_hash); |
| 6954 | } else { |
| 6955 | old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash, |
| 6956 | lockdep_is_held(&graph_lock)); |
| 6957 | rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash); |
| 6958 | } |
| 6959 | |
| 6960 | mutex_unlock(lock: &graph_lock); |
| 6961 | |
| 6962 | /* |
| 6963 | * We need to do a hard force of sched synchronization. |
| 6964 | * This is because we use preempt_disable() to do RCU, but |
| 6965 | * the function tracers can be called where RCU is not watching |
| 6966 | * (like before user_exit()). We can not rely on the RCU |
| 6967 | * infrastructure to do the synchronization, thus we must do it |
| 6968 | * ourselves. |
| 6969 | */ |
| 6970 | if (old_hash != EMPTY_HASH) |
| 6971 | synchronize_rcu_tasks_rude(); |
| 6972 | |
| 6973 | free_ftrace_hash(hash: old_hash); |
| 6974 | } |
| 6975 | |
| 6976 | out: |
| 6977 | free_ftrace_hash(hash: fgd->new_hash); |
| 6978 | kfree(objp: fgd); |
| 6979 | |
| 6980 | return ret; |
| 6981 | } |
| 6982 | |
| 6983 | static int |
| 6984 | ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer) |
| 6985 | { |
| 6986 | struct ftrace_glob func_g; |
| 6987 | struct dyn_ftrace *rec; |
| 6988 | struct ftrace_page *pg; |
| 6989 | struct ftrace_func_entry *entry; |
| 6990 | int fail = 1; |
| 6991 | int not; |
| 6992 | |
| 6993 | /* decode regex */ |
| 6994 | func_g.type = filter_parse_regex(buff: buffer, strlen(buffer), |
| 6995 | search: &func_g.search, not: ¬); |
| 6996 | |
| 6997 | func_g.len = strlen(func_g.search); |
| 6998 | |
| 6999 | guard(mutex)(T: &ftrace_lock); |
| 7000 | |
| 7001 | if (unlikely(ftrace_disabled)) |
| 7002 | return -ENODEV; |
| 7003 | |
| 7004 | do_for_each_ftrace_rec(pg, rec) { |
| 7005 | |
| 7006 | if (rec->flags & FTRACE_FL_DISABLED) |
| 7007 | continue; |
| 7008 | |
| 7009 | if (ftrace_match_record(rec, func_g: &func_g, NULL, exclude_mod: 0)) { |
| 7010 | entry = ftrace_lookup_ip(hash, ip: rec->ip); |
| 7011 | |
| 7012 | if (!not) { |
| 7013 | fail = 0; |
| 7014 | |
| 7015 | if (entry) |
| 7016 | continue; |
| 7017 | if (add_hash_entry(hash, ip: rec->ip) == NULL) |
| 7018 | return 0; |
| 7019 | } else { |
| 7020 | if (entry) { |
| 7021 | free_hash_entry(hash, entry); |
| 7022 | fail = 0; |
| 7023 | } |
| 7024 | } |
| 7025 | } |
| 7026 | cond_resched(); |
| 7027 | } while_for_each_ftrace_rec(); |
| 7028 | |
| 7029 | return fail ? -EINVAL : 0; |
| 7030 | } |
| 7031 | |
| 7032 | static ssize_t |
| 7033 | ftrace_graph_write(struct file *file, const char __user *ubuf, |
| 7034 | size_t cnt, loff_t *ppos) |
| 7035 | { |
| 7036 | ssize_t read, ret = 0; |
| 7037 | struct ftrace_graph_data *fgd = file->private_data; |
| 7038 | struct trace_parser *parser; |
| 7039 | |
| 7040 | if (!cnt) |
| 7041 | return 0; |
| 7042 | |
| 7043 | /* Read mode uses seq functions */ |
| 7044 | if (file->f_mode & FMODE_READ) { |
| 7045 | struct seq_file *m = file->private_data; |
| 7046 | fgd = m->private; |
| 7047 | } |
| 7048 | |
| 7049 | parser = &fgd->parser; |
| 7050 | |
| 7051 | read = trace_get_user(parser, ubuf, cnt, ppos); |
| 7052 | |
| 7053 | if (read >= 0 && trace_parser_loaded(parser) && |
| 7054 | !trace_parser_cont(parser)) { |
| 7055 | |
| 7056 | ret = ftrace_graph_set_hash(hash: fgd->new_hash, |
| 7057 | buffer: parser->buffer); |
| 7058 | trace_parser_clear(parser); |
| 7059 | } |
| 7060 | |
| 7061 | if (!ret) |
| 7062 | ret = read; |
| 7063 | |
| 7064 | return ret; |
| 7065 | } |
| 7066 | |
| 7067 | static const struct file_operations ftrace_graph_fops = { |
| 7068 | .open = ftrace_graph_open, |
| 7069 | .read = seq_read, |
| 7070 | .write = ftrace_graph_write, |
| 7071 | .llseek = tracing_lseek, |
| 7072 | .release = ftrace_graph_release, |
| 7073 | }; |
| 7074 | |
| 7075 | static const struct file_operations ftrace_graph_notrace_fops = { |
| 7076 | .open = ftrace_graph_notrace_open, |
| 7077 | .read = seq_read, |
| 7078 | .write = ftrace_graph_write, |
| 7079 | .llseek = tracing_lseek, |
| 7080 | .release = ftrace_graph_release, |
| 7081 | }; |
| 7082 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 7083 | |
| 7084 | void ftrace_create_filter_files(struct ftrace_ops *ops, |
| 7085 | struct dentry *parent) |
| 7086 | { |
| 7087 | |
| 7088 | trace_create_file(name: "set_ftrace_filter" , TRACE_MODE_WRITE, parent, |
| 7089 | data: ops, fops: &ftrace_filter_fops); |
| 7090 | |
| 7091 | trace_create_file(name: "set_ftrace_notrace" , TRACE_MODE_WRITE, parent, |
| 7092 | data: ops, fops: &ftrace_notrace_fops); |
| 7093 | } |
| 7094 | |
| 7095 | /* |
| 7096 | * The name "destroy_filter_files" is really a misnomer. Although |
| 7097 | * in the future, it may actually delete the files, but this is |
| 7098 | * really intended to make sure the ops passed in are disabled |
| 7099 | * and that when this function returns, the caller is free to |
| 7100 | * free the ops. |
| 7101 | * |
| 7102 | * The "destroy" name is only to match the "create" name that this |
| 7103 | * should be paired with. |
| 7104 | */ |
| 7105 | void ftrace_destroy_filter_files(struct ftrace_ops *ops) |
| 7106 | { |
| 7107 | mutex_lock(&ftrace_lock); |
| 7108 | if (ops->flags & FTRACE_OPS_FL_ENABLED) |
| 7109 | ftrace_shutdown(ops, command: 0); |
| 7110 | ops->flags |= FTRACE_OPS_FL_DELETED; |
| 7111 | ftrace_free_filter(ops); |
| 7112 | mutex_unlock(lock: &ftrace_lock); |
| 7113 | } |
| 7114 | |
| 7115 | static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer) |
| 7116 | { |
| 7117 | |
| 7118 | trace_create_file(name: "available_filter_functions" , TRACE_MODE_READ, |
| 7119 | parent: d_tracer, NULL, fops: &ftrace_avail_fops); |
| 7120 | |
| 7121 | trace_create_file(name: "available_filter_functions_addrs" , TRACE_MODE_READ, |
| 7122 | parent: d_tracer, NULL, fops: &ftrace_avail_addrs_fops); |
| 7123 | |
| 7124 | trace_create_file(name: "enabled_functions" , TRACE_MODE_READ, |
| 7125 | parent: d_tracer, NULL, fops: &ftrace_enabled_fops); |
| 7126 | |
| 7127 | trace_create_file(name: "touched_functions" , TRACE_MODE_READ, |
| 7128 | parent: d_tracer, NULL, fops: &ftrace_touched_fops); |
| 7129 | |
| 7130 | ftrace_create_filter_files(ops: &global_ops, parent: d_tracer); |
| 7131 | |
| 7132 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 7133 | trace_create_file(name: "set_graph_function" , TRACE_MODE_WRITE, parent: d_tracer, |
| 7134 | NULL, |
| 7135 | fops: &ftrace_graph_fops); |
| 7136 | trace_create_file(name: "set_graph_notrace" , TRACE_MODE_WRITE, parent: d_tracer, |
| 7137 | NULL, |
| 7138 | fops: &ftrace_graph_notrace_fops); |
| 7139 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 7140 | |
| 7141 | return 0; |
| 7142 | } |
| 7143 | |
| 7144 | static int ftrace_cmp_ips(const void *a, const void *b) |
| 7145 | { |
| 7146 | const unsigned long *ipa = a; |
| 7147 | const unsigned long *ipb = b; |
| 7148 | |
| 7149 | if (*ipa > *ipb) |
| 7150 | return 1; |
| 7151 | if (*ipa < *ipb) |
| 7152 | return -1; |
| 7153 | return 0; |
| 7154 | } |
| 7155 | |
| 7156 | #ifdef CONFIG_FTRACE_SORT_STARTUP_TEST |
| 7157 | static void test_is_sorted(unsigned long *start, unsigned long count) |
| 7158 | { |
| 7159 | int i; |
| 7160 | |
| 7161 | for (i = 1; i < count; i++) { |
| 7162 | if (WARN(start[i - 1] > start[i], |
| 7163 | "[%d] %pS at %lx is not sorted with %pS at %lx\n" , i, |
| 7164 | (void *)start[i - 1], start[i - 1], |
| 7165 | (void *)start[i], start[i])) |
| 7166 | break; |
| 7167 | } |
| 7168 | if (i == count) |
| 7169 | pr_info("ftrace section at %px sorted properly\n" , start); |
| 7170 | } |
| 7171 | #else |
| 7172 | static void test_is_sorted(unsigned long *start, unsigned long count) |
| 7173 | { |
| 7174 | } |
| 7175 | #endif |
| 7176 | |
| 7177 | static int ftrace_process_locs(struct module *mod, |
| 7178 | unsigned long *start, |
| 7179 | unsigned long *end) |
| 7180 | { |
| 7181 | struct ftrace_page *pg_unuse = NULL; |
| 7182 | struct ftrace_page *start_pg; |
| 7183 | struct ftrace_page *pg; |
| 7184 | struct dyn_ftrace *rec; |
| 7185 | unsigned long skipped = 0; |
| 7186 | unsigned long count; |
| 7187 | unsigned long *p; |
| 7188 | unsigned long addr; |
| 7189 | unsigned long flags = 0; /* Shut up gcc */ |
| 7190 | unsigned long pages; |
| 7191 | int ret = -ENOMEM; |
| 7192 | |
| 7193 | count = end - start; |
| 7194 | |
| 7195 | if (!count) |
| 7196 | return 0; |
| 7197 | |
| 7198 | /* |
| 7199 | * Sorting mcount in vmlinux at build time depend on |
| 7200 | * CONFIG_BUILDTIME_MCOUNT_SORT, while mcount loc in |
| 7201 | * modules can not be sorted at build time. |
| 7202 | */ |
| 7203 | if (!IS_ENABLED(CONFIG_BUILDTIME_MCOUNT_SORT) || mod) { |
| 7204 | sort(base: start, num: count, size: sizeof(*start), |
| 7205 | cmp_func: ftrace_cmp_ips, NULL); |
| 7206 | } else { |
| 7207 | test_is_sorted(start, count); |
| 7208 | } |
| 7209 | |
| 7210 | start_pg = ftrace_allocate_pages(num_to_init: count, num_pages: &pages); |
| 7211 | if (!start_pg) |
| 7212 | return -ENOMEM; |
| 7213 | |
| 7214 | mutex_lock(&ftrace_lock); |
| 7215 | |
| 7216 | /* |
| 7217 | * Core and each module needs their own pages, as |
| 7218 | * modules will free them when they are removed. |
| 7219 | * Force a new page to be allocated for modules. |
| 7220 | */ |
| 7221 | if (!mod) { |
| 7222 | WARN_ON(ftrace_pages || ftrace_pages_start); |
| 7223 | /* First initialization */ |
| 7224 | ftrace_pages = ftrace_pages_start = start_pg; |
| 7225 | } else { |
| 7226 | if (!ftrace_pages) |
| 7227 | goto out; |
| 7228 | |
| 7229 | if (WARN_ON(ftrace_pages->next)) { |
| 7230 | /* Hmm, we have free pages? */ |
| 7231 | while (ftrace_pages->next) |
| 7232 | ftrace_pages = ftrace_pages->next; |
| 7233 | } |
| 7234 | |
| 7235 | ftrace_pages->next = start_pg; |
| 7236 | } |
| 7237 | |
| 7238 | p = start; |
| 7239 | pg = start_pg; |
| 7240 | while (p < end) { |
| 7241 | unsigned long end_offset; |
| 7242 | |
| 7243 | addr = *p++; |
| 7244 | |
| 7245 | /* |
| 7246 | * Some architecture linkers will pad between |
| 7247 | * the different mcount_loc sections of different |
| 7248 | * object files to satisfy alignments. |
| 7249 | * Skip any NULL pointers. |
| 7250 | */ |
| 7251 | if (!addr) { |
| 7252 | skipped++; |
| 7253 | continue; |
| 7254 | } |
| 7255 | |
| 7256 | /* |
| 7257 | * If this is core kernel, make sure the address is in core |
| 7258 | * or inittext, as weak functions get zeroed and KASLR can |
| 7259 | * move them to something other than zero. It just will not |
| 7260 | * move it to an area where kernel text is. |
| 7261 | */ |
| 7262 | if (!mod && !(is_kernel_text(addr) || is_kernel_inittext(addr))) { |
| 7263 | skipped++; |
| 7264 | continue; |
| 7265 | } |
| 7266 | |
| 7267 | addr = ftrace_call_adjust(addr); |
| 7268 | |
| 7269 | end_offset = (pg->index+1) * sizeof(pg->records[0]); |
| 7270 | if (end_offset > PAGE_SIZE << pg->order) { |
| 7271 | /* We should have allocated enough */ |
| 7272 | if (WARN_ON(!pg->next)) |
| 7273 | break; |
| 7274 | pg = pg->next; |
| 7275 | } |
| 7276 | |
| 7277 | rec = &pg->records[pg->index++]; |
| 7278 | rec->ip = addr; |
| 7279 | } |
| 7280 | |
| 7281 | if (pg->next) { |
| 7282 | pg_unuse = pg->next; |
| 7283 | pg->next = NULL; |
| 7284 | } |
| 7285 | |
| 7286 | /* Assign the last page to ftrace_pages */ |
| 7287 | ftrace_pages = pg; |
| 7288 | |
| 7289 | /* |
| 7290 | * We only need to disable interrupts on start up |
| 7291 | * because we are modifying code that an interrupt |
| 7292 | * may execute, and the modification is not atomic. |
| 7293 | * But for modules, nothing runs the code we modify |
| 7294 | * until we are finished with it, and there's no |
| 7295 | * reason to cause large interrupt latencies while we do it. |
| 7296 | */ |
| 7297 | if (!mod) |
| 7298 | local_irq_save(flags); |
| 7299 | ftrace_update_code(mod, new_pgs: start_pg); |
| 7300 | if (!mod) |
| 7301 | local_irq_restore(flags); |
| 7302 | ret = 0; |
| 7303 | out: |
| 7304 | mutex_unlock(lock: &ftrace_lock); |
| 7305 | |
| 7306 | /* We should have used all pages unless we skipped some */ |
| 7307 | if (pg_unuse) { |
| 7308 | unsigned long pg_remaining, remaining = 0; |
| 7309 | long skip; |
| 7310 | |
| 7311 | /* Count the number of entries unused and compare it to skipped. */ |
| 7312 | pg_remaining = (PAGE_SIZE << pg->order) / ENTRY_SIZE - pg->index; |
| 7313 | |
| 7314 | if (!WARN(skipped < pg_remaining, "Extra allocated pages for ftrace" )) { |
| 7315 | |
| 7316 | skip = skipped - pg_remaining; |
| 7317 | |
| 7318 | for (pg = pg_unuse; pg && skip > 0; pg = pg->next) { |
| 7319 | remaining += 1 << pg->order; |
| 7320 | skip -= (PAGE_SIZE << pg->order) / ENTRY_SIZE; |
| 7321 | } |
| 7322 | |
| 7323 | pages -= remaining; |
| 7324 | |
| 7325 | /* |
| 7326 | * Check to see if the number of pages remaining would |
| 7327 | * just fit the number of entries skipped. |
| 7328 | */ |
| 7329 | WARN(pg || skip > 0, "Extra allocated pages for ftrace: %lu with %lu skipped" , |
| 7330 | remaining, skipped); |
| 7331 | } |
| 7332 | /* Need to synchronize with ftrace_location_range() */ |
| 7333 | synchronize_rcu(); |
| 7334 | ftrace_free_pages(pages: pg_unuse); |
| 7335 | } |
| 7336 | |
| 7337 | if (!mod) { |
| 7338 | count -= skipped; |
| 7339 | pr_info("ftrace: allocating %ld entries in %ld pages\n" , |
| 7340 | count, pages); |
| 7341 | } |
| 7342 | |
| 7343 | return ret; |
| 7344 | } |
| 7345 | |
| 7346 | struct ftrace_mod_func { |
| 7347 | struct list_head list; |
| 7348 | char *name; |
| 7349 | unsigned long ip; |
| 7350 | unsigned int size; |
| 7351 | }; |
| 7352 | |
| 7353 | struct ftrace_mod_map { |
| 7354 | struct rcu_head rcu; |
| 7355 | struct list_head list; |
| 7356 | struct module *mod; |
| 7357 | unsigned long start_addr; |
| 7358 | unsigned long end_addr; |
| 7359 | struct list_head funcs; |
| 7360 | unsigned int num_funcs; |
| 7361 | }; |
| 7362 | |
| 7363 | static int ftrace_get_trampoline_kallsym(unsigned int symnum, |
| 7364 | unsigned long *value, char *type, |
| 7365 | char *name, char *module_name, |
| 7366 | int *exported) |
| 7367 | { |
| 7368 | struct ftrace_ops *op; |
| 7369 | |
| 7370 | list_for_each_entry_rcu(op, &ftrace_ops_trampoline_list, list) { |
| 7371 | if (!op->trampoline || symnum--) |
| 7372 | continue; |
| 7373 | *value = op->trampoline; |
| 7374 | *type = 't'; |
| 7375 | strscpy(name, FTRACE_TRAMPOLINE_SYM, KSYM_NAME_LEN); |
| 7376 | strscpy(module_name, FTRACE_TRAMPOLINE_MOD, MODULE_NAME_LEN); |
| 7377 | *exported = 0; |
| 7378 | return 0; |
| 7379 | } |
| 7380 | |
| 7381 | return -ERANGE; |
| 7382 | } |
| 7383 | |
| 7384 | #if defined(CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS) || defined(CONFIG_MODULES) |
| 7385 | /* |
| 7386 | * Check if the current ops references the given ip. |
| 7387 | * |
| 7388 | * If the ops traces all functions, then it was already accounted for. |
| 7389 | * If the ops does not trace the current record function, skip it. |
| 7390 | * If the ops ignores the function via notrace filter, skip it. |
| 7391 | */ |
| 7392 | static bool |
| 7393 | ops_references_ip(struct ftrace_ops *ops, unsigned long ip) |
| 7394 | { |
| 7395 | /* If ops isn't enabled, ignore it */ |
| 7396 | if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) |
| 7397 | return false; |
| 7398 | |
| 7399 | /* If ops traces all then it includes this function */ |
| 7400 | if (ops_traces_mod(ops)) |
| 7401 | return true; |
| 7402 | |
| 7403 | /* The function must be in the filter */ |
| 7404 | if (!ftrace_hash_empty(hash: ops->func_hash->filter_hash) && |
| 7405 | !__ftrace_lookup_ip(hash: ops->func_hash->filter_hash, ip)) |
| 7406 | return false; |
| 7407 | |
| 7408 | /* If in notrace hash, we ignore it too */ |
| 7409 | if (ftrace_lookup_ip(hash: ops->func_hash->notrace_hash, ip)) |
| 7410 | return false; |
| 7411 | |
| 7412 | return true; |
| 7413 | } |
| 7414 | #endif |
| 7415 | |
| 7416 | #ifdef CONFIG_MODULES |
| 7417 | |
| 7418 | #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next) |
| 7419 | |
| 7420 | static LIST_HEAD(ftrace_mod_maps); |
| 7421 | |
| 7422 | static int referenced_filters(struct dyn_ftrace *rec) |
| 7423 | { |
| 7424 | struct ftrace_ops *ops; |
| 7425 | int cnt = 0; |
| 7426 | |
| 7427 | for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) { |
| 7428 | if (ops_references_ip(ops, ip: rec->ip)) { |
| 7429 | if (WARN_ON_ONCE(ops->flags & FTRACE_OPS_FL_DIRECT)) |
| 7430 | continue; |
| 7431 | if (WARN_ON_ONCE(ops->flags & FTRACE_OPS_FL_IPMODIFY)) |
| 7432 | continue; |
| 7433 | cnt++; |
| 7434 | if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) |
| 7435 | rec->flags |= FTRACE_FL_REGS; |
| 7436 | if (cnt == 1 && ops->trampoline) |
| 7437 | rec->flags |= FTRACE_FL_TRAMP; |
| 7438 | else |
| 7439 | rec->flags &= ~FTRACE_FL_TRAMP; |
| 7440 | } |
| 7441 | } |
| 7442 | |
| 7443 | return cnt; |
| 7444 | } |
| 7445 | |
| 7446 | static void |
| 7447 | clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash) |
| 7448 | { |
| 7449 | struct ftrace_func_entry *entry; |
| 7450 | struct dyn_ftrace *rec; |
| 7451 | int i; |
| 7452 | |
| 7453 | if (ftrace_hash_empty(hash)) |
| 7454 | return; |
| 7455 | |
| 7456 | for (i = 0; i < pg->index; i++) { |
| 7457 | rec = &pg->records[i]; |
| 7458 | entry = __ftrace_lookup_ip(hash, ip: rec->ip); |
| 7459 | /* |
| 7460 | * Do not allow this rec to match again. |
| 7461 | * Yeah, it may waste some memory, but will be removed |
| 7462 | * if/when the hash is modified again. |
| 7463 | */ |
| 7464 | if (entry) |
| 7465 | entry->ip = 0; |
| 7466 | } |
| 7467 | } |
| 7468 | |
| 7469 | /* Clear any records from hashes */ |
| 7470 | static void clear_mod_from_hashes(struct ftrace_page *pg) |
| 7471 | { |
| 7472 | struct trace_array *tr; |
| 7473 | |
| 7474 | mutex_lock(&trace_types_lock); |
| 7475 | list_for_each_entry(tr, &ftrace_trace_arrays, list) { |
| 7476 | if (!tr->ops || !tr->ops->func_hash) |
| 7477 | continue; |
| 7478 | mutex_lock(&tr->ops->func_hash->regex_lock); |
| 7479 | clear_mod_from_hash(pg, hash: tr->ops->func_hash->filter_hash); |
| 7480 | clear_mod_from_hash(pg, hash: tr->ops->func_hash->notrace_hash); |
| 7481 | mutex_unlock(lock: &tr->ops->func_hash->regex_lock); |
| 7482 | } |
| 7483 | mutex_unlock(lock: &trace_types_lock); |
| 7484 | } |
| 7485 | |
| 7486 | static void ftrace_free_mod_map(struct rcu_head *rcu) |
| 7487 | { |
| 7488 | struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu); |
| 7489 | struct ftrace_mod_func *mod_func; |
| 7490 | struct ftrace_mod_func *n; |
| 7491 | |
| 7492 | /* All the contents of mod_map are now not visible to readers */ |
| 7493 | list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) { |
| 7494 | kfree(objp: mod_func->name); |
| 7495 | list_del(entry: &mod_func->list); |
| 7496 | kfree(objp: mod_func); |
| 7497 | } |
| 7498 | |
| 7499 | kfree(objp: mod_map); |
| 7500 | } |
| 7501 | |
| 7502 | void ftrace_release_mod(struct module *mod) |
| 7503 | { |
| 7504 | struct ftrace_mod_map *mod_map; |
| 7505 | struct ftrace_mod_map *n; |
| 7506 | struct dyn_ftrace *rec; |
| 7507 | struct ftrace_page **last_pg; |
| 7508 | struct ftrace_page *tmp_page = NULL; |
| 7509 | struct ftrace_page *pg; |
| 7510 | |
| 7511 | mutex_lock(&ftrace_lock); |
| 7512 | |
| 7513 | /* |
| 7514 | * To avoid the UAF problem after the module is unloaded, the |
| 7515 | * 'mod_map' resource needs to be released unconditionally. |
| 7516 | */ |
| 7517 | list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) { |
| 7518 | if (mod_map->mod == mod) { |
| 7519 | list_del_rcu(entry: &mod_map->list); |
| 7520 | call_rcu(head: &mod_map->rcu, func: ftrace_free_mod_map); |
| 7521 | break; |
| 7522 | } |
| 7523 | } |
| 7524 | |
| 7525 | if (ftrace_disabled) |
| 7526 | goto out_unlock; |
| 7527 | |
| 7528 | /* |
| 7529 | * Each module has its own ftrace_pages, remove |
| 7530 | * them from the list. |
| 7531 | */ |
| 7532 | last_pg = &ftrace_pages_start; |
| 7533 | for (pg = ftrace_pages_start; pg; pg = *last_pg) { |
| 7534 | rec = &pg->records[0]; |
| 7535 | if (within_module(addr: rec->ip, mod)) { |
| 7536 | /* |
| 7537 | * As core pages are first, the first |
| 7538 | * page should never be a module page. |
| 7539 | */ |
| 7540 | if (WARN_ON(pg == ftrace_pages_start)) |
| 7541 | goto out_unlock; |
| 7542 | |
| 7543 | /* Check if we are deleting the last page */ |
| 7544 | if (pg == ftrace_pages) |
| 7545 | ftrace_pages = next_to_ftrace_page(last_pg); |
| 7546 | |
| 7547 | ftrace_update_tot_cnt -= pg->index; |
| 7548 | *last_pg = pg->next; |
| 7549 | |
| 7550 | pg->next = tmp_page; |
| 7551 | tmp_page = pg; |
| 7552 | } else |
| 7553 | last_pg = &pg->next; |
| 7554 | } |
| 7555 | out_unlock: |
| 7556 | mutex_unlock(lock: &ftrace_lock); |
| 7557 | |
| 7558 | /* Need to synchronize with ftrace_location_range() */ |
| 7559 | if (tmp_page) |
| 7560 | synchronize_rcu(); |
| 7561 | for (pg = tmp_page; pg; pg = tmp_page) { |
| 7562 | |
| 7563 | /* Needs to be called outside of ftrace_lock */ |
| 7564 | clear_mod_from_hashes(pg); |
| 7565 | |
| 7566 | if (pg->records) { |
| 7567 | free_pages(addr: (unsigned long)pg->records, order: pg->order); |
| 7568 | ftrace_number_of_pages -= 1 << pg->order; |
| 7569 | } |
| 7570 | tmp_page = pg->next; |
| 7571 | kfree(objp: pg); |
| 7572 | ftrace_number_of_groups--; |
| 7573 | } |
| 7574 | } |
| 7575 | |
| 7576 | void ftrace_module_enable(struct module *mod) |
| 7577 | { |
| 7578 | struct dyn_ftrace *rec; |
| 7579 | struct ftrace_page *pg; |
| 7580 | |
| 7581 | mutex_lock(&ftrace_lock); |
| 7582 | |
| 7583 | if (ftrace_disabled) |
| 7584 | goto out_unlock; |
| 7585 | |
| 7586 | /* |
| 7587 | * If the tracing is enabled, go ahead and enable the record. |
| 7588 | * |
| 7589 | * The reason not to enable the record immediately is the |
| 7590 | * inherent check of ftrace_make_nop/ftrace_make_call for |
| 7591 | * correct previous instructions. Making first the NOP |
| 7592 | * conversion puts the module to the correct state, thus |
| 7593 | * passing the ftrace_make_call check. |
| 7594 | * |
| 7595 | * We also delay this to after the module code already set the |
| 7596 | * text to read-only, as we now need to set it back to read-write |
| 7597 | * so that we can modify the text. |
| 7598 | */ |
| 7599 | if (ftrace_start_up) |
| 7600 | ftrace_arch_code_modify_prepare(); |
| 7601 | |
| 7602 | do_for_each_ftrace_rec(pg, rec) { |
| 7603 | int cnt; |
| 7604 | /* |
| 7605 | * do_for_each_ftrace_rec() is a double loop. |
| 7606 | * module text shares the pg. If a record is |
| 7607 | * not part of this module, then skip this pg, |
| 7608 | * which the "break" will do. |
| 7609 | */ |
| 7610 | if (!within_module(addr: rec->ip, mod)) |
| 7611 | break; |
| 7612 | |
| 7613 | cond_resched(); |
| 7614 | |
| 7615 | /* Weak functions should still be ignored */ |
| 7616 | if (!test_for_valid_rec(rec)) { |
| 7617 | /* Clear all other flags. Should not be enabled anyway */ |
| 7618 | rec->flags = FTRACE_FL_DISABLED; |
| 7619 | continue; |
| 7620 | } |
| 7621 | |
| 7622 | cnt = 0; |
| 7623 | |
| 7624 | /* |
| 7625 | * When adding a module, we need to check if tracers are |
| 7626 | * currently enabled and if they are, and can trace this record, |
| 7627 | * we need to enable the module functions as well as update the |
| 7628 | * reference counts for those function records. |
| 7629 | */ |
| 7630 | if (ftrace_start_up) |
| 7631 | cnt += referenced_filters(rec); |
| 7632 | |
| 7633 | rec->flags &= ~FTRACE_FL_DISABLED; |
| 7634 | rec->flags += cnt; |
| 7635 | |
| 7636 | if (ftrace_start_up && cnt) { |
| 7637 | int failed = __ftrace_replace_code(rec, enable: 1); |
| 7638 | if (failed) { |
| 7639 | ftrace_bug(failed, rec); |
| 7640 | goto out_loop; |
| 7641 | } |
| 7642 | } |
| 7643 | |
| 7644 | } while_for_each_ftrace_rec(); |
| 7645 | |
| 7646 | out_loop: |
| 7647 | if (ftrace_start_up) |
| 7648 | ftrace_arch_code_modify_post_process(); |
| 7649 | |
| 7650 | out_unlock: |
| 7651 | mutex_unlock(lock: &ftrace_lock); |
| 7652 | |
| 7653 | process_cached_mods(mod_name: mod->name); |
| 7654 | } |
| 7655 | |
| 7656 | void ftrace_module_init(struct module *mod) |
| 7657 | { |
| 7658 | int ret; |
| 7659 | |
| 7660 | if (ftrace_disabled || !mod->num_ftrace_callsites) |
| 7661 | return; |
| 7662 | |
| 7663 | ret = ftrace_process_locs(mod, start: mod->ftrace_callsites, |
| 7664 | end: mod->ftrace_callsites + mod->num_ftrace_callsites); |
| 7665 | if (ret) |
| 7666 | pr_warn("ftrace: failed to allocate entries for module '%s' functions\n" , |
| 7667 | mod->name); |
| 7668 | } |
| 7669 | |
| 7670 | static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map, |
| 7671 | struct dyn_ftrace *rec) |
| 7672 | { |
| 7673 | struct ftrace_mod_func *mod_func; |
| 7674 | unsigned long symsize; |
| 7675 | unsigned long offset; |
| 7676 | char str[KSYM_SYMBOL_LEN]; |
| 7677 | char *modname; |
| 7678 | const char *ret; |
| 7679 | |
| 7680 | ret = kallsyms_lookup(addr: rec->ip, symbolsize: &symsize, offset: &offset, modname: &modname, namebuf: str); |
| 7681 | if (!ret) |
| 7682 | return; |
| 7683 | |
| 7684 | mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL); |
| 7685 | if (!mod_func) |
| 7686 | return; |
| 7687 | |
| 7688 | mod_func->name = kstrdup(s: str, GFP_KERNEL); |
| 7689 | if (!mod_func->name) { |
| 7690 | kfree(objp: mod_func); |
| 7691 | return; |
| 7692 | } |
| 7693 | |
| 7694 | mod_func->ip = rec->ip - offset; |
| 7695 | mod_func->size = symsize; |
| 7696 | |
| 7697 | mod_map->num_funcs++; |
| 7698 | |
| 7699 | list_add_rcu(new: &mod_func->list, head: &mod_map->funcs); |
| 7700 | } |
| 7701 | |
| 7702 | static struct ftrace_mod_map * |
| 7703 | allocate_ftrace_mod_map(struct module *mod, |
| 7704 | unsigned long start, unsigned long end) |
| 7705 | { |
| 7706 | struct ftrace_mod_map *mod_map; |
| 7707 | |
| 7708 | if (ftrace_disabled) |
| 7709 | return NULL; |
| 7710 | |
| 7711 | mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL); |
| 7712 | if (!mod_map) |
| 7713 | return NULL; |
| 7714 | |
| 7715 | mod_map->mod = mod; |
| 7716 | mod_map->start_addr = start; |
| 7717 | mod_map->end_addr = end; |
| 7718 | mod_map->num_funcs = 0; |
| 7719 | |
| 7720 | INIT_LIST_HEAD_RCU(list: &mod_map->funcs); |
| 7721 | |
| 7722 | list_add_rcu(new: &mod_map->list, head: &ftrace_mod_maps); |
| 7723 | |
| 7724 | return mod_map; |
| 7725 | } |
| 7726 | |
| 7727 | static int |
| 7728 | ftrace_func_address_lookup(struct ftrace_mod_map *mod_map, |
| 7729 | unsigned long addr, unsigned long *size, |
| 7730 | unsigned long *off, char *sym) |
| 7731 | { |
| 7732 | struct ftrace_mod_func *found_func = NULL; |
| 7733 | struct ftrace_mod_func *mod_func; |
| 7734 | |
| 7735 | list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) { |
| 7736 | if (addr >= mod_func->ip && |
| 7737 | addr < mod_func->ip + mod_func->size) { |
| 7738 | found_func = mod_func; |
| 7739 | break; |
| 7740 | } |
| 7741 | } |
| 7742 | |
| 7743 | if (found_func) { |
| 7744 | if (size) |
| 7745 | *size = found_func->size; |
| 7746 | if (off) |
| 7747 | *off = addr - found_func->ip; |
| 7748 | return strscpy(sym, found_func->name, KSYM_NAME_LEN); |
| 7749 | } |
| 7750 | |
| 7751 | return 0; |
| 7752 | } |
| 7753 | |
| 7754 | int |
| 7755 | ftrace_mod_address_lookup(unsigned long addr, unsigned long *size, |
| 7756 | unsigned long *off, char **modname, char *sym) |
| 7757 | { |
| 7758 | struct ftrace_mod_map *mod_map; |
| 7759 | int ret = 0; |
| 7760 | |
| 7761 | /* mod_map is freed via call_rcu() */ |
| 7762 | preempt_disable(); |
| 7763 | list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) { |
| 7764 | ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym); |
| 7765 | if (ret) { |
| 7766 | if (modname) |
| 7767 | *modname = mod_map->mod->name; |
| 7768 | break; |
| 7769 | } |
| 7770 | } |
| 7771 | preempt_enable(); |
| 7772 | |
| 7773 | return ret; |
| 7774 | } |
| 7775 | |
| 7776 | int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value, |
| 7777 | char *type, char *name, |
| 7778 | char *module_name, int *exported) |
| 7779 | { |
| 7780 | struct ftrace_mod_map *mod_map; |
| 7781 | struct ftrace_mod_func *mod_func; |
| 7782 | int ret; |
| 7783 | |
| 7784 | preempt_disable(); |
| 7785 | list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) { |
| 7786 | |
| 7787 | if (symnum >= mod_map->num_funcs) { |
| 7788 | symnum -= mod_map->num_funcs; |
| 7789 | continue; |
| 7790 | } |
| 7791 | |
| 7792 | list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) { |
| 7793 | if (symnum > 1) { |
| 7794 | symnum--; |
| 7795 | continue; |
| 7796 | } |
| 7797 | |
| 7798 | *value = mod_func->ip; |
| 7799 | *type = 'T'; |
| 7800 | strscpy(name, mod_func->name, KSYM_NAME_LEN); |
| 7801 | strscpy(module_name, mod_map->mod->name, MODULE_NAME_LEN); |
| 7802 | *exported = 1; |
| 7803 | preempt_enable(); |
| 7804 | return 0; |
| 7805 | } |
| 7806 | WARN_ON(1); |
| 7807 | break; |
| 7808 | } |
| 7809 | ret = ftrace_get_trampoline_kallsym(symnum, value, type, name, |
| 7810 | module_name, exported); |
| 7811 | preempt_enable(); |
| 7812 | return ret; |
| 7813 | } |
| 7814 | |
| 7815 | #else |
| 7816 | static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map, |
| 7817 | struct dyn_ftrace *rec) { } |
| 7818 | static inline struct ftrace_mod_map * |
| 7819 | allocate_ftrace_mod_map(struct module *mod, |
| 7820 | unsigned long start, unsigned long end) |
| 7821 | { |
| 7822 | return NULL; |
| 7823 | } |
| 7824 | int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value, |
| 7825 | char *type, char *name, char *module_name, |
| 7826 | int *exported) |
| 7827 | { |
| 7828 | int ret; |
| 7829 | |
| 7830 | preempt_disable(); |
| 7831 | ret = ftrace_get_trampoline_kallsym(symnum, value, type, name, |
| 7832 | module_name, exported); |
| 7833 | preempt_enable(); |
| 7834 | return ret; |
| 7835 | } |
| 7836 | #endif /* CONFIG_MODULES */ |
| 7837 | |
| 7838 | struct ftrace_init_func { |
| 7839 | struct list_head list; |
| 7840 | unsigned long ip; |
| 7841 | }; |
| 7842 | |
| 7843 | /* Clear any init ips from hashes */ |
| 7844 | static void |
| 7845 | clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash) |
| 7846 | { |
| 7847 | struct ftrace_func_entry *entry; |
| 7848 | |
| 7849 | entry = ftrace_lookup_ip(hash, ip: func->ip); |
| 7850 | /* |
| 7851 | * Do not allow this rec to match again. |
| 7852 | * Yeah, it may waste some memory, but will be removed |
| 7853 | * if/when the hash is modified again. |
| 7854 | */ |
| 7855 | if (entry) |
| 7856 | entry->ip = 0; |
| 7857 | } |
| 7858 | |
| 7859 | static void |
| 7860 | clear_func_from_hashes(struct ftrace_init_func *func) |
| 7861 | { |
| 7862 | struct trace_array *tr; |
| 7863 | |
| 7864 | mutex_lock(&trace_types_lock); |
| 7865 | list_for_each_entry(tr, &ftrace_trace_arrays, list) { |
| 7866 | if (!tr->ops || !tr->ops->func_hash) |
| 7867 | continue; |
| 7868 | mutex_lock(&tr->ops->func_hash->regex_lock); |
| 7869 | clear_func_from_hash(func, hash: tr->ops->func_hash->filter_hash); |
| 7870 | clear_func_from_hash(func, hash: tr->ops->func_hash->notrace_hash); |
| 7871 | mutex_unlock(lock: &tr->ops->func_hash->regex_lock); |
| 7872 | } |
| 7873 | mutex_unlock(lock: &trace_types_lock); |
| 7874 | } |
| 7875 | |
| 7876 | static void add_to_clear_hash_list(struct list_head *clear_list, |
| 7877 | struct dyn_ftrace *rec) |
| 7878 | { |
| 7879 | struct ftrace_init_func *func; |
| 7880 | |
| 7881 | func = kmalloc(sizeof(*func), GFP_KERNEL); |
| 7882 | if (!func) { |
| 7883 | MEM_FAIL(1, "alloc failure, ftrace filter could be stale\n" ); |
| 7884 | return; |
| 7885 | } |
| 7886 | |
| 7887 | func->ip = rec->ip; |
| 7888 | list_add(new: &func->list, head: clear_list); |
| 7889 | } |
| 7890 | |
| 7891 | void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr) |
| 7892 | { |
| 7893 | unsigned long start = (unsigned long)(start_ptr); |
| 7894 | unsigned long end = (unsigned long)(end_ptr); |
| 7895 | struct ftrace_page **last_pg = &ftrace_pages_start; |
| 7896 | struct ftrace_page *tmp_page = NULL; |
| 7897 | struct ftrace_page *pg; |
| 7898 | struct dyn_ftrace *rec; |
| 7899 | struct dyn_ftrace key; |
| 7900 | struct ftrace_mod_map *mod_map = NULL; |
| 7901 | struct ftrace_init_func *func, *func_next; |
| 7902 | LIST_HEAD(clear_hash); |
| 7903 | |
| 7904 | key.ip = start; |
| 7905 | key.flags = end; /* overload flags, as it is unsigned long */ |
| 7906 | |
| 7907 | mutex_lock(&ftrace_lock); |
| 7908 | |
| 7909 | /* |
| 7910 | * If we are freeing module init memory, then check if |
| 7911 | * any tracer is active. If so, we need to save a mapping of |
| 7912 | * the module functions being freed with the address. |
| 7913 | */ |
| 7914 | if (mod && ftrace_ops_list != &ftrace_list_end) |
| 7915 | mod_map = allocate_ftrace_mod_map(mod, start, end); |
| 7916 | |
| 7917 | for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) { |
| 7918 | if (end < pg->records[0].ip || |
| 7919 | start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE)) |
| 7920 | continue; |
| 7921 | again: |
| 7922 | rec = bsearch(key: &key, base: pg->records, num: pg->index, |
| 7923 | size: sizeof(struct dyn_ftrace), |
| 7924 | cmp: ftrace_cmp_recs); |
| 7925 | if (!rec) |
| 7926 | continue; |
| 7927 | |
| 7928 | /* rec will be cleared from hashes after ftrace_lock unlock */ |
| 7929 | add_to_clear_hash_list(clear_list: &clear_hash, rec); |
| 7930 | |
| 7931 | if (mod_map) |
| 7932 | save_ftrace_mod_rec(mod_map, rec); |
| 7933 | |
| 7934 | pg->index--; |
| 7935 | ftrace_update_tot_cnt--; |
| 7936 | if (!pg->index) { |
| 7937 | *last_pg = pg->next; |
| 7938 | pg->next = tmp_page; |
| 7939 | tmp_page = pg; |
| 7940 | pg = container_of(last_pg, struct ftrace_page, next); |
| 7941 | if (!(*last_pg)) |
| 7942 | ftrace_pages = pg; |
| 7943 | continue; |
| 7944 | } |
| 7945 | memmove(rec, rec + 1, |
| 7946 | (pg->index - (rec - pg->records)) * sizeof(*rec)); |
| 7947 | /* More than one function may be in this block */ |
| 7948 | goto again; |
| 7949 | } |
| 7950 | mutex_unlock(lock: &ftrace_lock); |
| 7951 | |
| 7952 | list_for_each_entry_safe(func, func_next, &clear_hash, list) { |
| 7953 | clear_func_from_hashes(func); |
| 7954 | kfree(objp: func); |
| 7955 | } |
| 7956 | /* Need to synchronize with ftrace_location_range() */ |
| 7957 | if (tmp_page) { |
| 7958 | synchronize_rcu(); |
| 7959 | ftrace_free_pages(pages: tmp_page); |
| 7960 | } |
| 7961 | } |
| 7962 | |
| 7963 | void __init ftrace_free_init_mem(void) |
| 7964 | { |
| 7965 | void *start = (void *)(&__init_begin); |
| 7966 | void *end = (void *)(&__init_end); |
| 7967 | |
| 7968 | ftrace_boot_snapshot(); |
| 7969 | |
| 7970 | ftrace_free_mem(NULL, start_ptr: start, end_ptr: end); |
| 7971 | } |
| 7972 | |
| 7973 | int __init __weak ftrace_dyn_arch_init(void) |
| 7974 | { |
| 7975 | return 0; |
| 7976 | } |
| 7977 | |
| 7978 | void __init ftrace_init(void) |
| 7979 | { |
| 7980 | extern unsigned long __start_mcount_loc[]; |
| 7981 | extern unsigned long __stop_mcount_loc[]; |
| 7982 | unsigned long count, flags; |
| 7983 | int ret; |
| 7984 | |
| 7985 | local_irq_save(flags); |
| 7986 | ret = ftrace_dyn_arch_init(); |
| 7987 | local_irq_restore(flags); |
| 7988 | if (ret) |
| 7989 | goto failed; |
| 7990 | |
| 7991 | count = __stop_mcount_loc - __start_mcount_loc; |
| 7992 | if (!count) { |
| 7993 | pr_info("ftrace: No functions to be traced?\n" ); |
| 7994 | goto failed; |
| 7995 | } |
| 7996 | |
| 7997 | ret = ftrace_process_locs(NULL, |
| 7998 | start: __start_mcount_loc, |
| 7999 | end: __stop_mcount_loc); |
| 8000 | if (ret) { |
| 8001 | pr_warn("ftrace: failed to allocate entries for functions\n" ); |
| 8002 | goto failed; |
| 8003 | } |
| 8004 | |
| 8005 | pr_info("ftrace: allocated %ld pages with %ld groups\n" , |
| 8006 | ftrace_number_of_pages, ftrace_number_of_groups); |
| 8007 | |
| 8008 | last_ftrace_enabled = ftrace_enabled = 1; |
| 8009 | |
| 8010 | set_ftrace_early_filters(); |
| 8011 | |
| 8012 | return; |
| 8013 | failed: |
| 8014 | ftrace_disabled = 1; |
| 8015 | } |
| 8016 | |
| 8017 | /* Do nothing if arch does not support this */ |
| 8018 | void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops) |
| 8019 | { |
| 8020 | } |
| 8021 | |
| 8022 | static void ftrace_update_trampoline(struct ftrace_ops *ops) |
| 8023 | { |
| 8024 | unsigned long trampoline = ops->trampoline; |
| 8025 | |
| 8026 | arch_ftrace_update_trampoline(ops); |
| 8027 | if (ops->trampoline && ops->trampoline != trampoline && |
| 8028 | (ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP)) { |
| 8029 | /* Add to kallsyms before the perf events */ |
| 8030 | ftrace_add_trampoline_to_kallsyms(ops); |
| 8031 | perf_event_ksymbol(ksym_type: PERF_RECORD_KSYMBOL_TYPE_OOL, |
| 8032 | addr: ops->trampoline, len: ops->trampoline_size, unregister: false, |
| 8033 | FTRACE_TRAMPOLINE_SYM); |
| 8034 | /* |
| 8035 | * Record the perf text poke event after the ksymbol register |
| 8036 | * event. |
| 8037 | */ |
| 8038 | perf_event_text_poke(addr: (void *)ops->trampoline, NULL, old_len: 0, |
| 8039 | new_bytes: (void *)ops->trampoline, |
| 8040 | new_len: ops->trampoline_size); |
| 8041 | } |
| 8042 | } |
| 8043 | |
| 8044 | void ftrace_init_trace_array(struct trace_array *tr) |
| 8045 | { |
| 8046 | if (tr->flags & TRACE_ARRAY_FL_MOD_INIT) |
| 8047 | return; |
| 8048 | |
| 8049 | INIT_LIST_HEAD(list: &tr->func_probes); |
| 8050 | INIT_LIST_HEAD(list: &tr->mod_trace); |
| 8051 | INIT_LIST_HEAD(list: &tr->mod_notrace); |
| 8052 | |
| 8053 | tr->flags |= TRACE_ARRAY_FL_MOD_INIT; |
| 8054 | } |
| 8055 | #else |
| 8056 | |
| 8057 | struct ftrace_ops global_ops = { |
| 8058 | .func = ftrace_stub, |
| 8059 | .flags = FTRACE_OPS_FL_INITIALIZED | |
| 8060 | FTRACE_OPS_FL_PID, |
| 8061 | }; |
| 8062 | |
| 8063 | static int __init ftrace_nodyn_init(void) |
| 8064 | { |
| 8065 | ftrace_enabled = 1; |
| 8066 | return 0; |
| 8067 | } |
| 8068 | core_initcall(ftrace_nodyn_init); |
| 8069 | |
| 8070 | static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; } |
| 8071 | static inline void ftrace_startup_all(int command) { } |
| 8072 | |
| 8073 | static void ftrace_update_trampoline(struct ftrace_ops *ops) |
| 8074 | { |
| 8075 | } |
| 8076 | |
| 8077 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
| 8078 | |
| 8079 | __init void ftrace_init_global_array_ops(struct trace_array *tr) |
| 8080 | { |
| 8081 | tr->ops = &global_ops; |
| 8082 | if (!global_ops.private) |
| 8083 | global_ops.private = tr; |
| 8084 | ftrace_init_trace_array(tr); |
| 8085 | init_array_fgraph_ops(tr, ops: tr->ops); |
| 8086 | } |
| 8087 | |
| 8088 | void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func) |
| 8089 | { |
| 8090 | /* If we filter on pids, update to use the pid function */ |
| 8091 | if (tr->flags & TRACE_ARRAY_FL_GLOBAL) { |
| 8092 | if (WARN_ON(tr->ops->func != ftrace_stub)) |
| 8093 | printk("ftrace ops had %pS for function\n" , |
| 8094 | tr->ops->func); |
| 8095 | } |
| 8096 | tr->ops->func = func; |
| 8097 | tr->ops->private = tr; |
| 8098 | } |
| 8099 | |
| 8100 | void ftrace_reset_array_ops(struct trace_array *tr) |
| 8101 | { |
| 8102 | tr->ops->func = ftrace_stub; |
| 8103 | } |
| 8104 | |
| 8105 | static nokprobe_inline void |
| 8106 | __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip, |
| 8107 | struct ftrace_ops *ignored, struct ftrace_regs *fregs) |
| 8108 | { |
| 8109 | struct pt_regs *regs = ftrace_get_regs(fregs); |
| 8110 | struct ftrace_ops *op; |
| 8111 | int bit; |
| 8112 | |
| 8113 | /* |
| 8114 | * The ftrace_test_and_set_recursion() will disable preemption, |
| 8115 | * which is required since some of the ops may be dynamically |
| 8116 | * allocated, they must be freed after a synchronize_rcu(). |
| 8117 | */ |
| 8118 | bit = trace_test_and_set_recursion(ip, pip: parent_ip, TRACE_LIST_START); |
| 8119 | if (bit < 0) |
| 8120 | return; |
| 8121 | |
| 8122 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 8123 | /* Stub functions don't need to be called nor tested */ |
| 8124 | if (op->flags & FTRACE_OPS_FL_STUB) |
| 8125 | continue; |
| 8126 | /* |
| 8127 | * Check the following for each ops before calling their func: |
| 8128 | * if RCU flag is set, then rcu_is_watching() must be true |
| 8129 | * Otherwise test if the ip matches the ops filter |
| 8130 | * |
| 8131 | * If any of the above fails then the op->func() is not executed. |
| 8132 | */ |
| 8133 | if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) && |
| 8134 | ftrace_ops_test(ops: op, ip, regs)) { |
| 8135 | if (FTRACE_WARN_ON(!op->func)) { |
| 8136 | pr_warn("op=%p %pS\n" , op, op); |
| 8137 | goto out; |
| 8138 | } |
| 8139 | op->func(ip, parent_ip, op, fregs); |
| 8140 | } |
| 8141 | } while_for_each_ftrace_op(op); |
| 8142 | out: |
| 8143 | trace_clear_recursion(bit); |
| 8144 | } |
| 8145 | |
| 8146 | /* |
| 8147 | * Some archs only support passing ip and parent_ip. Even though |
| 8148 | * the list function ignores the op parameter, we do not want any |
| 8149 | * C side effects, where a function is called without the caller |
| 8150 | * sending a third parameter. |
| 8151 | * Archs are to support both the regs and ftrace_ops at the same time. |
| 8152 | * If they support ftrace_ops, it is assumed they support regs. |
| 8153 | * If call backs want to use regs, they must either check for regs |
| 8154 | * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS. |
| 8155 | * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved. |
| 8156 | * An architecture can pass partial regs with ftrace_ops and still |
| 8157 | * set the ARCH_SUPPORTS_FTRACE_OPS. |
| 8158 | * |
| 8159 | * In vmlinux.lds.h, ftrace_ops_list_func() is defined to be |
| 8160 | * arch_ftrace_ops_list_func. |
| 8161 | */ |
| 8162 | #if ARCH_SUPPORTS_FTRACE_OPS |
| 8163 | void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip, |
| 8164 | struct ftrace_ops *op, struct ftrace_regs *fregs) |
| 8165 | { |
| 8166 | kmsan_unpoison_memory(address: fregs, ftrace_regs_size()); |
| 8167 | __ftrace_ops_list_func(ip, parent_ip, NULL, fregs); |
| 8168 | } |
| 8169 | #else |
| 8170 | void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip) |
| 8171 | { |
| 8172 | __ftrace_ops_list_func(ip, parent_ip, NULL, NULL); |
| 8173 | } |
| 8174 | #endif |
| 8175 | NOKPROBE_SYMBOL(arch_ftrace_ops_list_func); |
| 8176 | |
| 8177 | /* |
| 8178 | * If there's only one function registered but it does not support |
| 8179 | * recursion, needs RCU protection, then this function will be called |
| 8180 | * by the mcount trampoline. |
| 8181 | */ |
| 8182 | static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip, |
| 8183 | struct ftrace_ops *op, struct ftrace_regs *fregs) |
| 8184 | { |
| 8185 | int bit; |
| 8186 | |
| 8187 | bit = trace_test_and_set_recursion(ip, pip: parent_ip, TRACE_LIST_START); |
| 8188 | if (bit < 0) |
| 8189 | return; |
| 8190 | |
| 8191 | if (!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) |
| 8192 | op->func(ip, parent_ip, op, fregs); |
| 8193 | |
| 8194 | trace_clear_recursion(bit); |
| 8195 | } |
| 8196 | NOKPROBE_SYMBOL(ftrace_ops_assist_func); |
| 8197 | |
| 8198 | /** |
| 8199 | * ftrace_ops_get_func - get the function a trampoline should call |
| 8200 | * @ops: the ops to get the function for |
| 8201 | * |
| 8202 | * Normally the mcount trampoline will call the ops->func, but there |
| 8203 | * are times that it should not. For example, if the ops does not |
| 8204 | * have its own recursion protection, then it should call the |
| 8205 | * ftrace_ops_assist_func() instead. |
| 8206 | * |
| 8207 | * Returns: the function that the trampoline should call for @ops. |
| 8208 | */ |
| 8209 | ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops) |
| 8210 | { |
| 8211 | /* |
| 8212 | * If the function does not handle recursion or needs to be RCU safe, |
| 8213 | * then we need to call the assist handler. |
| 8214 | */ |
| 8215 | if (ops->flags & (FTRACE_OPS_FL_RECURSION | |
| 8216 | FTRACE_OPS_FL_RCU)) |
| 8217 | return ftrace_ops_assist_func; |
| 8218 | |
| 8219 | return ops->func; |
| 8220 | } |
| 8221 | |
| 8222 | static void |
| 8223 | ftrace_filter_pid_sched_switch_probe(void *data, bool preempt, |
| 8224 | struct task_struct *prev, |
| 8225 | struct task_struct *next, |
| 8226 | unsigned int prev_state) |
| 8227 | { |
| 8228 | struct trace_array *tr = data; |
| 8229 | struct trace_pid_list *pid_list; |
| 8230 | struct trace_pid_list *no_pid_list; |
| 8231 | |
| 8232 | pid_list = rcu_dereference_sched(tr->function_pids); |
| 8233 | no_pid_list = rcu_dereference_sched(tr->function_no_pids); |
| 8234 | |
| 8235 | if (trace_ignore_this_task(filtered_pids: pid_list, filtered_no_pids: no_pid_list, task: next)) |
| 8236 | this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid, |
| 8237 | FTRACE_PID_IGNORE); |
| 8238 | else |
| 8239 | this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid, |
| 8240 | next->pid); |
| 8241 | } |
| 8242 | |
| 8243 | static void |
| 8244 | ftrace_pid_follow_sched_process_fork(void *data, |
| 8245 | struct task_struct *self, |
| 8246 | struct task_struct *task) |
| 8247 | { |
| 8248 | struct trace_pid_list *pid_list; |
| 8249 | struct trace_array *tr = data; |
| 8250 | |
| 8251 | pid_list = rcu_dereference_sched(tr->function_pids); |
| 8252 | trace_filter_add_remove_task(pid_list, self, task); |
| 8253 | |
| 8254 | pid_list = rcu_dereference_sched(tr->function_no_pids); |
| 8255 | trace_filter_add_remove_task(pid_list, self, task); |
| 8256 | } |
| 8257 | |
| 8258 | static void |
| 8259 | ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task) |
| 8260 | { |
| 8261 | struct trace_pid_list *pid_list; |
| 8262 | struct trace_array *tr = data; |
| 8263 | |
| 8264 | pid_list = rcu_dereference_sched(tr->function_pids); |
| 8265 | trace_filter_add_remove_task(pid_list, NULL, task); |
| 8266 | |
| 8267 | pid_list = rcu_dereference_sched(tr->function_no_pids); |
| 8268 | trace_filter_add_remove_task(pid_list, NULL, task); |
| 8269 | } |
| 8270 | |
| 8271 | void ftrace_pid_follow_fork(struct trace_array *tr, bool enable) |
| 8272 | { |
| 8273 | if (enable) { |
| 8274 | register_trace_sched_process_fork(probe: ftrace_pid_follow_sched_process_fork, |
| 8275 | data: tr); |
| 8276 | register_trace_sched_process_free(probe: ftrace_pid_follow_sched_process_exit, |
| 8277 | data: tr); |
| 8278 | } else { |
| 8279 | unregister_trace_sched_process_fork(probe: ftrace_pid_follow_sched_process_fork, |
| 8280 | data: tr); |
| 8281 | unregister_trace_sched_process_free(probe: ftrace_pid_follow_sched_process_exit, |
| 8282 | data: tr); |
| 8283 | } |
| 8284 | } |
| 8285 | |
| 8286 | static void clear_ftrace_pids(struct trace_array *tr, int type) |
| 8287 | { |
| 8288 | struct trace_pid_list *pid_list; |
| 8289 | struct trace_pid_list *no_pid_list; |
| 8290 | int cpu; |
| 8291 | |
| 8292 | pid_list = rcu_dereference_protected(tr->function_pids, |
| 8293 | lockdep_is_held(&ftrace_lock)); |
| 8294 | no_pid_list = rcu_dereference_protected(tr->function_no_pids, |
| 8295 | lockdep_is_held(&ftrace_lock)); |
| 8296 | |
| 8297 | /* Make sure there's something to do */ |
| 8298 | if (!pid_type_enabled(type, pid_list, no_pid_list)) |
| 8299 | return; |
| 8300 | |
| 8301 | /* See if the pids still need to be checked after this */ |
| 8302 | if (!still_need_pid_events(type, pid_list, no_pid_list)) { |
| 8303 | unregister_trace_sched_switch(probe: ftrace_filter_pid_sched_switch_probe, data: tr); |
| 8304 | for_each_possible_cpu(cpu) |
| 8305 | per_cpu_ptr(tr->array_buffer.data, cpu)->ftrace_ignore_pid = FTRACE_PID_TRACE; |
| 8306 | } |
| 8307 | |
| 8308 | if (type & TRACE_PIDS) |
| 8309 | rcu_assign_pointer(tr->function_pids, NULL); |
| 8310 | |
| 8311 | if (type & TRACE_NO_PIDS) |
| 8312 | rcu_assign_pointer(tr->function_no_pids, NULL); |
| 8313 | |
| 8314 | /* Wait till all users are no longer using pid filtering */ |
| 8315 | synchronize_rcu(); |
| 8316 | |
| 8317 | if ((type & TRACE_PIDS) && pid_list) |
| 8318 | trace_pid_list_free(pid_list); |
| 8319 | |
| 8320 | if ((type & TRACE_NO_PIDS) && no_pid_list) |
| 8321 | trace_pid_list_free(pid_list: no_pid_list); |
| 8322 | } |
| 8323 | |
| 8324 | void ftrace_clear_pids(struct trace_array *tr) |
| 8325 | { |
| 8326 | mutex_lock(&ftrace_lock); |
| 8327 | |
| 8328 | clear_ftrace_pids(tr, type: TRACE_PIDS | TRACE_NO_PIDS); |
| 8329 | |
| 8330 | mutex_unlock(lock: &ftrace_lock); |
| 8331 | } |
| 8332 | |
| 8333 | static void ftrace_pid_reset(struct trace_array *tr, int type) |
| 8334 | { |
| 8335 | mutex_lock(&ftrace_lock); |
| 8336 | clear_ftrace_pids(tr, type); |
| 8337 | |
| 8338 | ftrace_update_pid_func(); |
| 8339 | ftrace_startup_all(command: 0); |
| 8340 | |
| 8341 | mutex_unlock(lock: &ftrace_lock); |
| 8342 | } |
| 8343 | |
| 8344 | /* Greater than any max PID */ |
| 8345 | #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1) |
| 8346 | |
| 8347 | static void *fpid_start(struct seq_file *m, loff_t *pos) |
| 8348 | __acquires(RCU) |
| 8349 | { |
| 8350 | struct trace_pid_list *pid_list; |
| 8351 | struct trace_array *tr = m->private; |
| 8352 | |
| 8353 | mutex_lock(&ftrace_lock); |
| 8354 | rcu_read_lock_sched(); |
| 8355 | |
| 8356 | pid_list = rcu_dereference_sched(tr->function_pids); |
| 8357 | |
| 8358 | if (!pid_list) |
| 8359 | return !(*pos) ? FTRACE_NO_PIDS : NULL; |
| 8360 | |
| 8361 | return trace_pid_start(pid_list, pos); |
| 8362 | } |
| 8363 | |
| 8364 | static void *fpid_next(struct seq_file *m, void *v, loff_t *pos) |
| 8365 | { |
| 8366 | struct trace_array *tr = m->private; |
| 8367 | struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids); |
| 8368 | |
| 8369 | if (v == FTRACE_NO_PIDS) { |
| 8370 | (*pos)++; |
| 8371 | return NULL; |
| 8372 | } |
| 8373 | return trace_pid_next(pid_list, v, pos); |
| 8374 | } |
| 8375 | |
| 8376 | static void fpid_stop(struct seq_file *m, void *p) |
| 8377 | __releases(RCU) |
| 8378 | { |
| 8379 | rcu_read_unlock_sched(); |
| 8380 | mutex_unlock(lock: &ftrace_lock); |
| 8381 | } |
| 8382 | |
| 8383 | static int fpid_show(struct seq_file *m, void *v) |
| 8384 | { |
| 8385 | if (v == FTRACE_NO_PIDS) { |
| 8386 | seq_puts(m, s: "no pid\n" ); |
| 8387 | return 0; |
| 8388 | } |
| 8389 | |
| 8390 | return trace_pid_show(m, v); |
| 8391 | } |
| 8392 | |
| 8393 | static const struct seq_operations ftrace_pid_sops = { |
| 8394 | .start = fpid_start, |
| 8395 | .next = fpid_next, |
| 8396 | .stop = fpid_stop, |
| 8397 | .show = fpid_show, |
| 8398 | }; |
| 8399 | |
| 8400 | static void *fnpid_start(struct seq_file *m, loff_t *pos) |
| 8401 | __acquires(RCU) |
| 8402 | { |
| 8403 | struct trace_pid_list *pid_list; |
| 8404 | struct trace_array *tr = m->private; |
| 8405 | |
| 8406 | mutex_lock(&ftrace_lock); |
| 8407 | rcu_read_lock_sched(); |
| 8408 | |
| 8409 | pid_list = rcu_dereference_sched(tr->function_no_pids); |
| 8410 | |
| 8411 | if (!pid_list) |
| 8412 | return !(*pos) ? FTRACE_NO_PIDS : NULL; |
| 8413 | |
| 8414 | return trace_pid_start(pid_list, pos); |
| 8415 | } |
| 8416 | |
| 8417 | static void *fnpid_next(struct seq_file *m, void *v, loff_t *pos) |
| 8418 | { |
| 8419 | struct trace_array *tr = m->private; |
| 8420 | struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_no_pids); |
| 8421 | |
| 8422 | if (v == FTRACE_NO_PIDS) { |
| 8423 | (*pos)++; |
| 8424 | return NULL; |
| 8425 | } |
| 8426 | return trace_pid_next(pid_list, v, pos); |
| 8427 | } |
| 8428 | |
| 8429 | static const struct seq_operations ftrace_no_pid_sops = { |
| 8430 | .start = fnpid_start, |
| 8431 | .next = fnpid_next, |
| 8432 | .stop = fpid_stop, |
| 8433 | .show = fpid_show, |
| 8434 | }; |
| 8435 | |
| 8436 | static int pid_open(struct inode *inode, struct file *file, int type) |
| 8437 | { |
| 8438 | const struct seq_operations *seq_ops; |
| 8439 | struct trace_array *tr = inode->i_private; |
| 8440 | struct seq_file *m; |
| 8441 | int ret = 0; |
| 8442 | |
| 8443 | ret = tracing_check_open_get_tr(tr); |
| 8444 | if (ret) |
| 8445 | return ret; |
| 8446 | |
| 8447 | if ((file->f_mode & FMODE_WRITE) && |
| 8448 | (file->f_flags & O_TRUNC)) |
| 8449 | ftrace_pid_reset(tr, type); |
| 8450 | |
| 8451 | switch (type) { |
| 8452 | case TRACE_PIDS: |
| 8453 | seq_ops = &ftrace_pid_sops; |
| 8454 | break; |
| 8455 | case TRACE_NO_PIDS: |
| 8456 | seq_ops = &ftrace_no_pid_sops; |
| 8457 | break; |
| 8458 | default: |
| 8459 | trace_array_put(tr); |
| 8460 | WARN_ON_ONCE(1); |
| 8461 | return -EINVAL; |
| 8462 | } |
| 8463 | |
| 8464 | ret = seq_open(file, seq_ops); |
| 8465 | if (ret < 0) { |
| 8466 | trace_array_put(tr); |
| 8467 | } else { |
| 8468 | m = file->private_data; |
| 8469 | /* copy tr over to seq ops */ |
| 8470 | m->private = tr; |
| 8471 | } |
| 8472 | |
| 8473 | return ret; |
| 8474 | } |
| 8475 | |
| 8476 | static int |
| 8477 | ftrace_pid_open(struct inode *inode, struct file *file) |
| 8478 | { |
| 8479 | return pid_open(inode, file, type: TRACE_PIDS); |
| 8480 | } |
| 8481 | |
| 8482 | static int |
| 8483 | ftrace_no_pid_open(struct inode *inode, struct file *file) |
| 8484 | { |
| 8485 | return pid_open(inode, file, type: TRACE_NO_PIDS); |
| 8486 | } |
| 8487 | |
| 8488 | static void ignore_task_cpu(void *data) |
| 8489 | { |
| 8490 | struct trace_array *tr = data; |
| 8491 | struct trace_pid_list *pid_list; |
| 8492 | struct trace_pid_list *no_pid_list; |
| 8493 | |
| 8494 | /* |
| 8495 | * This function is called by on_each_cpu() while the |
| 8496 | * event_mutex is held. |
| 8497 | */ |
| 8498 | pid_list = rcu_dereference_protected(tr->function_pids, |
| 8499 | mutex_is_locked(&ftrace_lock)); |
| 8500 | no_pid_list = rcu_dereference_protected(tr->function_no_pids, |
| 8501 | mutex_is_locked(&ftrace_lock)); |
| 8502 | |
| 8503 | if (trace_ignore_this_task(filtered_pids: pid_list, filtered_no_pids: no_pid_list, current)) |
| 8504 | this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid, |
| 8505 | FTRACE_PID_IGNORE); |
| 8506 | else |
| 8507 | this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid, |
| 8508 | current->pid); |
| 8509 | } |
| 8510 | |
| 8511 | static ssize_t |
| 8512 | pid_write(struct file *filp, const char __user *ubuf, |
| 8513 | size_t cnt, loff_t *ppos, int type) |
| 8514 | { |
| 8515 | struct seq_file *m = filp->private_data; |
| 8516 | struct trace_array *tr = m->private; |
| 8517 | struct trace_pid_list *filtered_pids; |
| 8518 | struct trace_pid_list *other_pids; |
| 8519 | struct trace_pid_list *pid_list; |
| 8520 | ssize_t ret; |
| 8521 | |
| 8522 | if (!cnt) |
| 8523 | return 0; |
| 8524 | |
| 8525 | guard(mutex)(T: &ftrace_lock); |
| 8526 | |
| 8527 | switch (type) { |
| 8528 | case TRACE_PIDS: |
| 8529 | filtered_pids = rcu_dereference_protected(tr->function_pids, |
| 8530 | lockdep_is_held(&ftrace_lock)); |
| 8531 | other_pids = rcu_dereference_protected(tr->function_no_pids, |
| 8532 | lockdep_is_held(&ftrace_lock)); |
| 8533 | break; |
| 8534 | case TRACE_NO_PIDS: |
| 8535 | filtered_pids = rcu_dereference_protected(tr->function_no_pids, |
| 8536 | lockdep_is_held(&ftrace_lock)); |
| 8537 | other_pids = rcu_dereference_protected(tr->function_pids, |
| 8538 | lockdep_is_held(&ftrace_lock)); |
| 8539 | break; |
| 8540 | default: |
| 8541 | WARN_ON_ONCE(1); |
| 8542 | return -EINVAL; |
| 8543 | } |
| 8544 | |
| 8545 | ret = trace_pid_write(filtered_pids, new_pid_list: &pid_list, ubuf, cnt); |
| 8546 | if (ret < 0) |
| 8547 | return ret; |
| 8548 | |
| 8549 | switch (type) { |
| 8550 | case TRACE_PIDS: |
| 8551 | rcu_assign_pointer(tr->function_pids, pid_list); |
| 8552 | break; |
| 8553 | case TRACE_NO_PIDS: |
| 8554 | rcu_assign_pointer(tr->function_no_pids, pid_list); |
| 8555 | break; |
| 8556 | } |
| 8557 | |
| 8558 | |
| 8559 | if (filtered_pids) { |
| 8560 | synchronize_rcu(); |
| 8561 | trace_pid_list_free(pid_list: filtered_pids); |
| 8562 | } else if (pid_list && !other_pids) { |
| 8563 | /* Register a probe to set whether to ignore the tracing of a task */ |
| 8564 | register_trace_sched_switch(probe: ftrace_filter_pid_sched_switch_probe, data: tr); |
| 8565 | } |
| 8566 | |
| 8567 | /* |
| 8568 | * Ignoring of pids is done at task switch. But we have to |
| 8569 | * check for those tasks that are currently running. |
| 8570 | * Always do this in case a pid was appended or removed. |
| 8571 | */ |
| 8572 | on_each_cpu(func: ignore_task_cpu, info: tr, wait: 1); |
| 8573 | |
| 8574 | ftrace_update_pid_func(); |
| 8575 | ftrace_startup_all(command: 0); |
| 8576 | |
| 8577 | *ppos += ret; |
| 8578 | |
| 8579 | return ret; |
| 8580 | } |
| 8581 | |
| 8582 | static ssize_t |
| 8583 | ftrace_pid_write(struct file *filp, const char __user *ubuf, |
| 8584 | size_t cnt, loff_t *ppos) |
| 8585 | { |
| 8586 | return pid_write(filp, ubuf, cnt, ppos, type: TRACE_PIDS); |
| 8587 | } |
| 8588 | |
| 8589 | static ssize_t |
| 8590 | ftrace_no_pid_write(struct file *filp, const char __user *ubuf, |
| 8591 | size_t cnt, loff_t *ppos) |
| 8592 | { |
| 8593 | return pid_write(filp, ubuf, cnt, ppos, type: TRACE_NO_PIDS); |
| 8594 | } |
| 8595 | |
| 8596 | static int |
| 8597 | ftrace_pid_release(struct inode *inode, struct file *file) |
| 8598 | { |
| 8599 | struct trace_array *tr = inode->i_private; |
| 8600 | |
| 8601 | trace_array_put(tr); |
| 8602 | |
| 8603 | return seq_release(inode, file); |
| 8604 | } |
| 8605 | |
| 8606 | static const struct file_operations ftrace_pid_fops = { |
| 8607 | .open = ftrace_pid_open, |
| 8608 | .write = ftrace_pid_write, |
| 8609 | .read = seq_read, |
| 8610 | .llseek = tracing_lseek, |
| 8611 | .release = ftrace_pid_release, |
| 8612 | }; |
| 8613 | |
| 8614 | static const struct file_operations ftrace_no_pid_fops = { |
| 8615 | .open = ftrace_no_pid_open, |
| 8616 | .write = ftrace_no_pid_write, |
| 8617 | .read = seq_read, |
| 8618 | .llseek = tracing_lseek, |
| 8619 | .release = ftrace_pid_release, |
| 8620 | }; |
| 8621 | |
| 8622 | void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer) |
| 8623 | { |
| 8624 | trace_create_file(name: "set_ftrace_pid" , TRACE_MODE_WRITE, parent: d_tracer, |
| 8625 | data: tr, fops: &ftrace_pid_fops); |
| 8626 | trace_create_file(name: "set_ftrace_notrace_pid" , TRACE_MODE_WRITE, |
| 8627 | parent: d_tracer, data: tr, fops: &ftrace_no_pid_fops); |
| 8628 | } |
| 8629 | |
| 8630 | void __init ftrace_init_tracefs_toplevel(struct trace_array *tr, |
| 8631 | struct dentry *d_tracer) |
| 8632 | { |
| 8633 | /* Only the top level directory has the dyn_tracefs and profile */ |
| 8634 | WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL)); |
| 8635 | |
| 8636 | ftrace_init_dyn_tracefs(d_tracer); |
| 8637 | ftrace_profile_tracefs(d_tracer); |
| 8638 | } |
| 8639 | |
| 8640 | /** |
| 8641 | * ftrace_kill - kill ftrace |
| 8642 | * |
| 8643 | * This function should be used by panic code. It stops ftrace |
| 8644 | * but in a not so nice way. If you need to simply kill ftrace |
| 8645 | * from a non-atomic section, use ftrace_kill. |
| 8646 | */ |
| 8647 | void ftrace_kill(void) |
| 8648 | { |
| 8649 | ftrace_disabled = 1; |
| 8650 | ftrace_enabled = 0; |
| 8651 | ftrace_trace_function = ftrace_stub; |
| 8652 | kprobe_ftrace_kill(); |
| 8653 | } |
| 8654 | |
| 8655 | /** |
| 8656 | * ftrace_is_dead - Test if ftrace is dead or not. |
| 8657 | * |
| 8658 | * Returns: 1 if ftrace is "dead", zero otherwise. |
| 8659 | */ |
| 8660 | int ftrace_is_dead(void) |
| 8661 | { |
| 8662 | return ftrace_disabled; |
| 8663 | } |
| 8664 | |
| 8665 | #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS |
| 8666 | /* |
| 8667 | * When registering ftrace_ops with IPMODIFY, it is necessary to make sure |
| 8668 | * it doesn't conflict with any direct ftrace_ops. If there is existing |
| 8669 | * direct ftrace_ops on a kernel function being patched, call |
| 8670 | * FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_PEER on it to enable sharing. |
| 8671 | * |
| 8672 | * @ops: ftrace_ops being registered. |
| 8673 | * |
| 8674 | * Returns: |
| 8675 | * 0 on success; |
| 8676 | * Negative on failure. |
| 8677 | */ |
| 8678 | static int prepare_direct_functions_for_ipmodify(struct ftrace_ops *ops) |
| 8679 | { |
| 8680 | struct ftrace_func_entry *entry; |
| 8681 | struct ftrace_hash *hash; |
| 8682 | struct ftrace_ops *op; |
| 8683 | int size, i, ret; |
| 8684 | |
| 8685 | lockdep_assert_held_once(&direct_mutex); |
| 8686 | |
| 8687 | if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY)) |
| 8688 | return 0; |
| 8689 | |
| 8690 | hash = ops->func_hash->filter_hash; |
| 8691 | size = 1 << hash->size_bits; |
| 8692 | for (i = 0; i < size; i++) { |
| 8693 | hlist_for_each_entry(entry, &hash->buckets[i], hlist) { |
| 8694 | unsigned long ip = entry->ip; |
| 8695 | bool found_op = false; |
| 8696 | |
| 8697 | mutex_lock(&ftrace_lock); |
| 8698 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 8699 | if (!(op->flags & FTRACE_OPS_FL_DIRECT)) |
| 8700 | continue; |
| 8701 | if (ops_references_ip(ops: op, ip)) { |
| 8702 | found_op = true; |
| 8703 | break; |
| 8704 | } |
| 8705 | } while_for_each_ftrace_op(op); |
| 8706 | mutex_unlock(lock: &ftrace_lock); |
| 8707 | |
| 8708 | if (found_op) { |
| 8709 | if (!op->ops_func) |
| 8710 | return -EBUSY; |
| 8711 | |
| 8712 | ret = op->ops_func(op, FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_PEER); |
| 8713 | if (ret) |
| 8714 | return ret; |
| 8715 | } |
| 8716 | } |
| 8717 | } |
| 8718 | |
| 8719 | return 0; |
| 8720 | } |
| 8721 | |
| 8722 | /* |
| 8723 | * Similar to prepare_direct_functions_for_ipmodify, clean up after ops |
| 8724 | * with IPMODIFY is unregistered. The cleanup is optional for most DIRECT |
| 8725 | * ops. |
| 8726 | */ |
| 8727 | static void cleanup_direct_functions_after_ipmodify(struct ftrace_ops *ops) |
| 8728 | { |
| 8729 | struct ftrace_func_entry *entry; |
| 8730 | struct ftrace_hash *hash; |
| 8731 | struct ftrace_ops *op; |
| 8732 | int size, i; |
| 8733 | |
| 8734 | if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY)) |
| 8735 | return; |
| 8736 | |
| 8737 | mutex_lock(&direct_mutex); |
| 8738 | |
| 8739 | hash = ops->func_hash->filter_hash; |
| 8740 | size = 1 << hash->size_bits; |
| 8741 | for (i = 0; i < size; i++) { |
| 8742 | hlist_for_each_entry(entry, &hash->buckets[i], hlist) { |
| 8743 | unsigned long ip = entry->ip; |
| 8744 | bool found_op = false; |
| 8745 | |
| 8746 | mutex_lock(&ftrace_lock); |
| 8747 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 8748 | if (!(op->flags & FTRACE_OPS_FL_DIRECT)) |
| 8749 | continue; |
| 8750 | if (ops_references_ip(ops: op, ip)) { |
| 8751 | found_op = true; |
| 8752 | break; |
| 8753 | } |
| 8754 | } while_for_each_ftrace_op(op); |
| 8755 | mutex_unlock(lock: &ftrace_lock); |
| 8756 | |
| 8757 | /* The cleanup is optional, ignore any errors */ |
| 8758 | if (found_op && op->ops_func) |
| 8759 | op->ops_func(op, FTRACE_OPS_CMD_DISABLE_SHARE_IPMODIFY_PEER); |
| 8760 | } |
| 8761 | } |
| 8762 | mutex_unlock(lock: &direct_mutex); |
| 8763 | } |
| 8764 | |
| 8765 | #define lock_direct_mutex() mutex_lock(&direct_mutex) |
| 8766 | #define unlock_direct_mutex() mutex_unlock(&direct_mutex) |
| 8767 | |
| 8768 | #else /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */ |
| 8769 | |
| 8770 | static int prepare_direct_functions_for_ipmodify(struct ftrace_ops *ops) |
| 8771 | { |
| 8772 | return 0; |
| 8773 | } |
| 8774 | |
| 8775 | static void cleanup_direct_functions_after_ipmodify(struct ftrace_ops *ops) |
| 8776 | { |
| 8777 | } |
| 8778 | |
| 8779 | #define lock_direct_mutex() do { } while (0) |
| 8780 | #define unlock_direct_mutex() do { } while (0) |
| 8781 | |
| 8782 | #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */ |
| 8783 | |
| 8784 | /* |
| 8785 | * Similar to register_ftrace_function, except we don't lock direct_mutex. |
| 8786 | */ |
| 8787 | static int register_ftrace_function_nolock(struct ftrace_ops *ops) |
| 8788 | { |
| 8789 | int ret; |
| 8790 | |
| 8791 | ftrace_ops_init(ops); |
| 8792 | |
| 8793 | mutex_lock(&ftrace_lock); |
| 8794 | |
| 8795 | ret = ftrace_startup(ops, command: 0); |
| 8796 | |
| 8797 | mutex_unlock(lock: &ftrace_lock); |
| 8798 | |
| 8799 | return ret; |
| 8800 | } |
| 8801 | |
| 8802 | /** |
| 8803 | * register_ftrace_function - register a function for profiling |
| 8804 | * @ops: ops structure that holds the function for profiling. |
| 8805 | * |
| 8806 | * Register a function to be called by all functions in the |
| 8807 | * kernel. |
| 8808 | * |
| 8809 | * Note: @ops->func and all the functions it calls must be labeled |
| 8810 | * with "notrace", otherwise it will go into a |
| 8811 | * recursive loop. |
| 8812 | */ |
| 8813 | int register_ftrace_function(struct ftrace_ops *ops) |
| 8814 | { |
| 8815 | int ret; |
| 8816 | |
| 8817 | lock_direct_mutex(); |
| 8818 | ret = prepare_direct_functions_for_ipmodify(ops); |
| 8819 | if (ret < 0) |
| 8820 | goto out_unlock; |
| 8821 | |
| 8822 | ret = register_ftrace_function_nolock(ops); |
| 8823 | |
| 8824 | out_unlock: |
| 8825 | unlock_direct_mutex(); |
| 8826 | return ret; |
| 8827 | } |
| 8828 | EXPORT_SYMBOL_GPL(register_ftrace_function); |
| 8829 | |
| 8830 | /** |
| 8831 | * unregister_ftrace_function - unregister a function for profiling. |
| 8832 | * @ops: ops structure that holds the function to unregister |
| 8833 | * |
| 8834 | * Unregister a function that was added to be called by ftrace profiling. |
| 8835 | */ |
| 8836 | int unregister_ftrace_function(struct ftrace_ops *ops) |
| 8837 | { |
| 8838 | int ret; |
| 8839 | |
| 8840 | mutex_lock(&ftrace_lock); |
| 8841 | ret = ftrace_shutdown(ops, command: 0); |
| 8842 | mutex_unlock(lock: &ftrace_lock); |
| 8843 | |
| 8844 | cleanup_direct_functions_after_ipmodify(ops); |
| 8845 | return ret; |
| 8846 | } |
| 8847 | EXPORT_SYMBOL_GPL(unregister_ftrace_function); |
| 8848 | |
| 8849 | static int symbols_cmp(const void *a, const void *b) |
| 8850 | { |
| 8851 | const char **str_a = (const char **) a; |
| 8852 | const char **str_b = (const char **) b; |
| 8853 | |
| 8854 | return strcmp(*str_a, *str_b); |
| 8855 | } |
| 8856 | |
| 8857 | struct kallsyms_data { |
| 8858 | unsigned long *addrs; |
| 8859 | const char **syms; |
| 8860 | size_t cnt; |
| 8861 | size_t found; |
| 8862 | }; |
| 8863 | |
| 8864 | /* This function gets called for all kernel and module symbols |
| 8865 | * and returns 1 in case we resolved all the requested symbols, |
| 8866 | * 0 otherwise. |
| 8867 | */ |
| 8868 | static int kallsyms_callback(void *data, const char *name, unsigned long addr) |
| 8869 | { |
| 8870 | struct kallsyms_data *args = data; |
| 8871 | const char **sym; |
| 8872 | int idx; |
| 8873 | |
| 8874 | sym = bsearch(key: &name, base: args->syms, num: args->cnt, size: sizeof(*args->syms), cmp: symbols_cmp); |
| 8875 | if (!sym) |
| 8876 | return 0; |
| 8877 | |
| 8878 | idx = sym - args->syms; |
| 8879 | if (args->addrs[idx]) |
| 8880 | return 0; |
| 8881 | |
| 8882 | if (!ftrace_location(ip: addr)) |
| 8883 | return 0; |
| 8884 | |
| 8885 | args->addrs[idx] = addr; |
| 8886 | args->found++; |
| 8887 | return args->found == args->cnt ? 1 : 0; |
| 8888 | } |
| 8889 | |
| 8890 | /** |
| 8891 | * ftrace_lookup_symbols - Lookup addresses for array of symbols |
| 8892 | * |
| 8893 | * @sorted_syms: array of symbols pointers symbols to resolve, |
| 8894 | * must be alphabetically sorted |
| 8895 | * @cnt: number of symbols/addresses in @syms/@addrs arrays |
| 8896 | * @addrs: array for storing resulting addresses |
| 8897 | * |
| 8898 | * This function looks up addresses for array of symbols provided in |
| 8899 | * @syms array (must be alphabetically sorted) and stores them in |
| 8900 | * @addrs array, which needs to be big enough to store at least @cnt |
| 8901 | * addresses. |
| 8902 | * |
| 8903 | * Returns: 0 if all provided symbols are found, -ESRCH otherwise. |
| 8904 | */ |
| 8905 | int ftrace_lookup_symbols(const char **sorted_syms, size_t cnt, unsigned long *addrs) |
| 8906 | { |
| 8907 | struct kallsyms_data args; |
| 8908 | int found_all; |
| 8909 | |
| 8910 | memset(addrs, 0, sizeof(*addrs) * cnt); |
| 8911 | args.addrs = addrs; |
| 8912 | args.syms = sorted_syms; |
| 8913 | args.cnt = cnt; |
| 8914 | args.found = 0; |
| 8915 | |
| 8916 | found_all = kallsyms_on_each_symbol(fn: kallsyms_callback, data: &args); |
| 8917 | if (found_all) |
| 8918 | return 0; |
| 8919 | found_all = module_kallsyms_on_each_symbol(NULL, fn: kallsyms_callback, data: &args); |
| 8920 | return found_all ? 0 : -ESRCH; |
| 8921 | } |
| 8922 | |
| 8923 | #ifdef CONFIG_SYSCTL |
| 8924 | |
| 8925 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 8926 | static void ftrace_startup_sysctl(void) |
| 8927 | { |
| 8928 | int command; |
| 8929 | |
| 8930 | if (unlikely(ftrace_disabled)) |
| 8931 | return; |
| 8932 | |
| 8933 | /* Force update next time */ |
| 8934 | saved_ftrace_func = NULL; |
| 8935 | /* ftrace_start_up is true if we want ftrace running */ |
| 8936 | if (ftrace_start_up) { |
| 8937 | command = FTRACE_UPDATE_CALLS; |
| 8938 | if (ftrace_graph_active) |
| 8939 | command |= FTRACE_START_FUNC_RET; |
| 8940 | ftrace_startup_enable(command); |
| 8941 | } |
| 8942 | } |
| 8943 | |
| 8944 | static void ftrace_shutdown_sysctl(void) |
| 8945 | { |
| 8946 | int command; |
| 8947 | |
| 8948 | if (unlikely(ftrace_disabled)) |
| 8949 | return; |
| 8950 | |
| 8951 | /* ftrace_start_up is true if ftrace is running */ |
| 8952 | if (ftrace_start_up) { |
| 8953 | command = FTRACE_DISABLE_CALLS; |
| 8954 | if (ftrace_graph_active) |
| 8955 | command |= FTRACE_STOP_FUNC_RET; |
| 8956 | ftrace_run_update_code(command); |
| 8957 | } |
| 8958 | } |
| 8959 | #else |
| 8960 | # define ftrace_startup_sysctl() do { } while (0) |
| 8961 | # define ftrace_shutdown_sysctl() do { } while (0) |
| 8962 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
| 8963 | |
| 8964 | static bool is_permanent_ops_registered(void) |
| 8965 | { |
| 8966 | struct ftrace_ops *op; |
| 8967 | |
| 8968 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 8969 | if (op->flags & FTRACE_OPS_FL_PERMANENT) |
| 8970 | return true; |
| 8971 | } while_for_each_ftrace_op(op); |
| 8972 | |
| 8973 | return false; |
| 8974 | } |
| 8975 | |
| 8976 | static int |
| 8977 | ftrace_enable_sysctl(const struct ctl_table *table, int write, |
| 8978 | void *buffer, size_t *lenp, loff_t *ppos) |
| 8979 | { |
| 8980 | int ret; |
| 8981 | |
| 8982 | guard(mutex)(T: &ftrace_lock); |
| 8983 | |
| 8984 | if (unlikely(ftrace_disabled)) |
| 8985 | return -ENODEV; |
| 8986 | |
| 8987 | ret = proc_dointvec(table, write, buffer, lenp, ppos); |
| 8988 | |
| 8989 | if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled)) |
| 8990 | return ret; |
| 8991 | |
| 8992 | if (ftrace_enabled) { |
| 8993 | |
| 8994 | /* we are starting ftrace again */ |
| 8995 | if (rcu_dereference_protected(ftrace_ops_list, |
| 8996 | lockdep_is_held(&ftrace_lock)) != &ftrace_list_end) |
| 8997 | update_ftrace_function(); |
| 8998 | |
| 8999 | ftrace_startup_sysctl(); |
| 9000 | |
| 9001 | } else { |
| 9002 | if (is_permanent_ops_registered()) { |
| 9003 | ftrace_enabled = true; |
| 9004 | return -EBUSY; |
| 9005 | } |
| 9006 | |
| 9007 | /* stopping ftrace calls (just send to ftrace_stub) */ |
| 9008 | ftrace_trace_function = ftrace_stub; |
| 9009 | |
| 9010 | ftrace_shutdown_sysctl(); |
| 9011 | } |
| 9012 | |
| 9013 | last_ftrace_enabled = !!ftrace_enabled; |
| 9014 | return 0; |
| 9015 | } |
| 9016 | |
| 9017 | static const struct ctl_table ftrace_sysctls[] = { |
| 9018 | { |
| 9019 | .procname = "ftrace_enabled" , |
| 9020 | .data = &ftrace_enabled, |
| 9021 | .maxlen = sizeof(int), |
| 9022 | .mode = 0644, |
| 9023 | .proc_handler = ftrace_enable_sysctl, |
| 9024 | }, |
| 9025 | }; |
| 9026 | |
| 9027 | static int __init ftrace_sysctl_init(void) |
| 9028 | { |
| 9029 | register_sysctl_init("kernel" , ftrace_sysctls); |
| 9030 | return 0; |
| 9031 | } |
| 9032 | late_initcall(ftrace_sysctl_init); |
| 9033 | #endif |
| 9034 | |