|
7 | 7 | * |
8 | 8 | * Highly modified by Steven Rostedt (VMware). |
9 | 9 | */ |
| 10 | +#include <linux/suspend.h> |
10 | 11 | #include <linux/ftrace.h> |
| 12 | +#include <linux/slab.h> |
11 | 13 |
|
12 | | -#include "trace.h" |
| 14 | +#include <trace/events/sched.h> |
| 15 | + |
| 16 | +#include "ftrace_internal.h" |
| 17 | + |
| 18 | +#ifdef CONFIG_DYNAMIC_FTRACE |
| 19 | +#define ASSIGN_OPS_HASH(opsname, val) \ |
| 20 | + .func_hash = val, \ |
| 21 | + .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock), |
| 22 | +#else |
| 23 | +#define ASSIGN_OPS_HASH(opsname, val) |
| 24 | +#endif |
13 | 25 |
|
14 | 26 | static bool kill_ftrace_graph; |
| 27 | +int ftrace_graph_active; |
| 28 | + |
| 29 | +/* Both enabled by default (can be cleared by function_graph tracer flags */ |
| 30 | +static bool fgraph_sleep_time = true; |
15 | 31 |
|
16 | 32 | /** |
17 | 33 | * ftrace_graph_is_dead - returns true if ftrace_graph_stop() was called |
@@ -161,6 +177,31 @@ ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret, |
161 | 177 | barrier(); |
162 | 178 | } |
163 | 179 |
|
| 180 | +/* |
| 181 | + * Hibernation protection. |
| 182 | + * The state of the current task is too much unstable during |
| 183 | + * suspend/restore to disk. We want to protect against that. |
| 184 | + */ |
| 185 | +static int |
| 186 | +ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state, |
| 187 | + void *unused) |
| 188 | +{ |
| 189 | + switch (state) { |
| 190 | + case PM_HIBERNATION_PREPARE: |
| 191 | + pause_graph_tracing(); |
| 192 | + break; |
| 193 | + |
| 194 | + case PM_POST_HIBERNATION: |
| 195 | + unpause_graph_tracing(); |
| 196 | + break; |
| 197 | + } |
| 198 | + return NOTIFY_DONE; |
| 199 | +} |
| 200 | + |
| 201 | +static struct notifier_block ftrace_suspend_notifier = { |
| 202 | + .notifier_call = ftrace_suspend_notifier_call, |
| 203 | +}; |
| 204 | + |
164 | 205 | /* |
165 | 206 | * Send the trace to the ring-buffer. |
166 | 207 | * @return the original return address. |
@@ -190,3 +231,320 @@ unsigned long ftrace_return_to_handler(unsigned long frame_pointer) |
190 | 231 |
|
191 | 232 | return ret; |
192 | 233 | } |
| 234 | + |
| 235 | +static struct ftrace_ops graph_ops = { |
| 236 | + .func = ftrace_stub, |
| 237 | + .flags = FTRACE_OPS_FL_RECURSION_SAFE | |
| 238 | + FTRACE_OPS_FL_INITIALIZED | |
| 239 | + FTRACE_OPS_FL_PID | |
| 240 | + FTRACE_OPS_FL_STUB, |
| 241 | +#ifdef FTRACE_GRAPH_TRAMP_ADDR |
| 242 | + .trampoline = FTRACE_GRAPH_TRAMP_ADDR, |
| 243 | + /* trampoline_size is only needed for dynamically allocated tramps */ |
| 244 | +#endif |
| 245 | + ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash) |
| 246 | +}; |
| 247 | + |
| 248 | +void ftrace_graph_sleep_time_control(bool enable) |
| 249 | +{ |
| 250 | + fgraph_sleep_time = enable; |
| 251 | +} |
| 252 | + |
| 253 | +int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace) |
| 254 | +{ |
| 255 | + return 0; |
| 256 | +} |
| 257 | + |
| 258 | +/* The callbacks that hook a function */ |
| 259 | +trace_func_graph_ret_t ftrace_graph_return = |
| 260 | + (trace_func_graph_ret_t)ftrace_stub; |
| 261 | +trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub; |
| 262 | +static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub; |
| 263 | + |
| 264 | +/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */ |
| 265 | +static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list) |
| 266 | +{ |
| 267 | + int i; |
| 268 | + int ret = 0; |
| 269 | + int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE; |
| 270 | + struct task_struct *g, *t; |
| 271 | + |
| 272 | + for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) { |
| 273 | + ret_stack_list[i] = |
| 274 | + kmalloc_array(FTRACE_RETFUNC_DEPTH, |
| 275 | + sizeof(struct ftrace_ret_stack), |
| 276 | + GFP_KERNEL); |
| 277 | + if (!ret_stack_list[i]) { |
| 278 | + start = 0; |
| 279 | + end = i; |
| 280 | + ret = -ENOMEM; |
| 281 | + goto free; |
| 282 | + } |
| 283 | + } |
| 284 | + |
| 285 | + read_lock(&tasklist_lock); |
| 286 | + do_each_thread(g, t) { |
| 287 | + if (start == end) { |
| 288 | + ret = -EAGAIN; |
| 289 | + goto unlock; |
| 290 | + } |
| 291 | + |
| 292 | + if (t->ret_stack == NULL) { |
| 293 | + atomic_set(&t->tracing_graph_pause, 0); |
| 294 | + atomic_set(&t->trace_overrun, 0); |
| 295 | + t->curr_ret_stack = -1; |
| 296 | + t->curr_ret_depth = -1; |
| 297 | + /* Make sure the tasks see the -1 first: */ |
| 298 | + smp_wmb(); |
| 299 | + t->ret_stack = ret_stack_list[start++]; |
| 300 | + } |
| 301 | + } while_each_thread(g, t); |
| 302 | + |
| 303 | +unlock: |
| 304 | + read_unlock(&tasklist_lock); |
| 305 | +free: |
| 306 | + for (i = start; i < end; i++) |
| 307 | + kfree(ret_stack_list[i]); |
| 308 | + return ret; |
| 309 | +} |
| 310 | + |
| 311 | +static void |
| 312 | +ftrace_graph_probe_sched_switch(void *ignore, bool preempt, |
| 313 | + struct task_struct *prev, struct task_struct *next) |
| 314 | +{ |
| 315 | + unsigned long long timestamp; |
| 316 | + int index; |
| 317 | + |
| 318 | + /* |
| 319 | + * Does the user want to count the time a function was asleep. |
| 320 | + * If so, do not update the time stamps. |
| 321 | + */ |
| 322 | + if (fgraph_sleep_time) |
| 323 | + return; |
| 324 | + |
| 325 | + timestamp = trace_clock_local(); |
| 326 | + |
| 327 | + prev->ftrace_timestamp = timestamp; |
| 328 | + |
| 329 | + /* only process tasks that we timestamped */ |
| 330 | + if (!next->ftrace_timestamp) |
| 331 | + return; |
| 332 | + |
| 333 | + /* |
| 334 | + * Update all the counters in next to make up for the |
| 335 | + * time next was sleeping. |
| 336 | + */ |
| 337 | + timestamp -= next->ftrace_timestamp; |
| 338 | + |
| 339 | + for (index = next->curr_ret_stack; index >= 0; index--) |
| 340 | + next->ret_stack[index].calltime += timestamp; |
| 341 | +} |
| 342 | + |
| 343 | +static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace) |
| 344 | +{ |
| 345 | + if (!ftrace_ops_test(&global_ops, trace->func, NULL)) |
| 346 | + return 0; |
| 347 | + return __ftrace_graph_entry(trace); |
| 348 | +} |
| 349 | + |
| 350 | +/* |
| 351 | + * The function graph tracer should only trace the functions defined |
| 352 | + * by set_ftrace_filter and set_ftrace_notrace. If another function |
| 353 | + * tracer ops is registered, the graph tracer requires testing the |
| 354 | + * function against the global ops, and not just trace any function |
| 355 | + * that any ftrace_ops registered. |
| 356 | + */ |
| 357 | +void update_function_graph_func(void) |
| 358 | +{ |
| 359 | + struct ftrace_ops *op; |
| 360 | + bool do_test = false; |
| 361 | + |
| 362 | + /* |
| 363 | + * The graph and global ops share the same set of functions |
| 364 | + * to test. If any other ops is on the list, then |
| 365 | + * the graph tracing needs to test if its the function |
| 366 | + * it should call. |
| 367 | + */ |
| 368 | + do_for_each_ftrace_op(op, ftrace_ops_list) { |
| 369 | + if (op != &global_ops && op != &graph_ops && |
| 370 | + op != &ftrace_list_end) { |
| 371 | + do_test = true; |
| 372 | + /* in double loop, break out with goto */ |
| 373 | + goto out; |
| 374 | + } |
| 375 | + } while_for_each_ftrace_op(op); |
| 376 | + out: |
| 377 | + if (do_test) |
| 378 | + ftrace_graph_entry = ftrace_graph_entry_test; |
| 379 | + else |
| 380 | + ftrace_graph_entry = __ftrace_graph_entry; |
| 381 | +} |
| 382 | + |
| 383 | +static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack); |
| 384 | + |
| 385 | +static void |
| 386 | +graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack) |
| 387 | +{ |
| 388 | + atomic_set(&t->tracing_graph_pause, 0); |
| 389 | + atomic_set(&t->trace_overrun, 0); |
| 390 | + t->ftrace_timestamp = 0; |
| 391 | + /* make curr_ret_stack visible before we add the ret_stack */ |
| 392 | + smp_wmb(); |
| 393 | + t->ret_stack = ret_stack; |
| 394 | +} |
| 395 | + |
| 396 | +/* |
| 397 | + * Allocate a return stack for the idle task. May be the first |
| 398 | + * time through, or it may be done by CPU hotplug online. |
| 399 | + */ |
| 400 | +void ftrace_graph_init_idle_task(struct task_struct *t, int cpu) |
| 401 | +{ |
| 402 | + t->curr_ret_stack = -1; |
| 403 | + t->curr_ret_depth = -1; |
| 404 | + /* |
| 405 | + * The idle task has no parent, it either has its own |
| 406 | + * stack or no stack at all. |
| 407 | + */ |
| 408 | + if (t->ret_stack) |
| 409 | + WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu)); |
| 410 | + |
| 411 | + if (ftrace_graph_active) { |
| 412 | + struct ftrace_ret_stack *ret_stack; |
| 413 | + |
| 414 | + ret_stack = per_cpu(idle_ret_stack, cpu); |
| 415 | + if (!ret_stack) { |
| 416 | + ret_stack = |
| 417 | + kmalloc_array(FTRACE_RETFUNC_DEPTH, |
| 418 | + sizeof(struct ftrace_ret_stack), |
| 419 | + GFP_KERNEL); |
| 420 | + if (!ret_stack) |
| 421 | + return; |
| 422 | + per_cpu(idle_ret_stack, cpu) = ret_stack; |
| 423 | + } |
| 424 | + graph_init_task(t, ret_stack); |
| 425 | + } |
| 426 | +} |
| 427 | + |
| 428 | +/* Allocate a return stack for newly created task */ |
| 429 | +void ftrace_graph_init_task(struct task_struct *t) |
| 430 | +{ |
| 431 | + /* Make sure we do not use the parent ret_stack */ |
| 432 | + t->ret_stack = NULL; |
| 433 | + t->curr_ret_stack = -1; |
| 434 | + t->curr_ret_depth = -1; |
| 435 | + |
| 436 | + if (ftrace_graph_active) { |
| 437 | + struct ftrace_ret_stack *ret_stack; |
| 438 | + |
| 439 | + ret_stack = kmalloc_array(FTRACE_RETFUNC_DEPTH, |
| 440 | + sizeof(struct ftrace_ret_stack), |
| 441 | + GFP_KERNEL); |
| 442 | + if (!ret_stack) |
| 443 | + return; |
| 444 | + graph_init_task(t, ret_stack); |
| 445 | + } |
| 446 | +} |
| 447 | + |
| 448 | +void ftrace_graph_exit_task(struct task_struct *t) |
| 449 | +{ |
| 450 | + struct ftrace_ret_stack *ret_stack = t->ret_stack; |
| 451 | + |
| 452 | + t->ret_stack = NULL; |
| 453 | + /* NULL must become visible to IRQs before we free it: */ |
| 454 | + barrier(); |
| 455 | + |
| 456 | + kfree(ret_stack); |
| 457 | +} |
| 458 | + |
| 459 | +/* Allocate a return stack for each task */ |
| 460 | +static int start_graph_tracing(void) |
| 461 | +{ |
| 462 | + struct ftrace_ret_stack **ret_stack_list; |
| 463 | + int ret, cpu; |
| 464 | + |
| 465 | + ret_stack_list = kmalloc_array(FTRACE_RETSTACK_ALLOC_SIZE, |
| 466 | + sizeof(struct ftrace_ret_stack *), |
| 467 | + GFP_KERNEL); |
| 468 | + |
| 469 | + if (!ret_stack_list) |
| 470 | + return -ENOMEM; |
| 471 | + |
| 472 | + /* The cpu_boot init_task->ret_stack will never be freed */ |
| 473 | + for_each_online_cpu(cpu) { |
| 474 | + if (!idle_task(cpu)->ret_stack) |
| 475 | + ftrace_graph_init_idle_task(idle_task(cpu), cpu); |
| 476 | + } |
| 477 | + |
| 478 | + do { |
| 479 | + ret = alloc_retstack_tasklist(ret_stack_list); |
| 480 | + } while (ret == -EAGAIN); |
| 481 | + |
| 482 | + if (!ret) { |
| 483 | + ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL); |
| 484 | + if (ret) |
| 485 | + pr_info("ftrace_graph: Couldn't activate tracepoint" |
| 486 | + " probe to kernel_sched_switch\n"); |
| 487 | + } |
| 488 | + |
| 489 | + kfree(ret_stack_list); |
| 490 | + return ret; |
| 491 | +} |
| 492 | + |
| 493 | +int register_ftrace_graph(trace_func_graph_ret_t retfunc, |
| 494 | + trace_func_graph_ent_t entryfunc) |
| 495 | +{ |
| 496 | + int ret = 0; |
| 497 | + |
| 498 | + mutex_lock(&ftrace_lock); |
| 499 | + |
| 500 | + /* we currently allow only one tracer registered at a time */ |
| 501 | + if (ftrace_graph_active) { |
| 502 | + ret = -EBUSY; |
| 503 | + goto out; |
| 504 | + } |
| 505 | + |
| 506 | + register_pm_notifier(&ftrace_suspend_notifier); |
| 507 | + |
| 508 | + ftrace_graph_active++; |
| 509 | + ret = start_graph_tracing(); |
| 510 | + if (ret) { |
| 511 | + ftrace_graph_active--; |
| 512 | + goto out; |
| 513 | + } |
| 514 | + |
| 515 | + ftrace_graph_return = retfunc; |
| 516 | + |
| 517 | + /* |
| 518 | + * Update the indirect function to the entryfunc, and the |
| 519 | + * function that gets called to the entry_test first. Then |
| 520 | + * call the update fgraph entry function to determine if |
| 521 | + * the entryfunc should be called directly or not. |
| 522 | + */ |
| 523 | + __ftrace_graph_entry = entryfunc; |
| 524 | + ftrace_graph_entry = ftrace_graph_entry_test; |
| 525 | + update_function_graph_func(); |
| 526 | + |
| 527 | + ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET); |
| 528 | +out: |
| 529 | + mutex_unlock(&ftrace_lock); |
| 530 | + return ret; |
| 531 | +} |
| 532 | + |
| 533 | +void unregister_ftrace_graph(void) |
| 534 | +{ |
| 535 | + mutex_lock(&ftrace_lock); |
| 536 | + |
| 537 | + if (unlikely(!ftrace_graph_active)) |
| 538 | + goto out; |
| 539 | + |
| 540 | + ftrace_graph_active--; |
| 541 | + ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub; |
| 542 | + ftrace_graph_entry = ftrace_graph_entry_stub; |
| 543 | + __ftrace_graph_entry = ftrace_graph_entry_stub; |
| 544 | + ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET); |
| 545 | + unregister_pm_notifier(&ftrace_suspend_notifier); |
| 546 | + unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL); |
| 547 | + |
| 548 | + out: |
| 549 | + mutex_unlock(&ftrace_lock); |
| 550 | +} |
0 commit comments