Depending on type of BPF programs served by BPF trampoline it can call original function. In such case the trampoline will skip one stack frame while returning. That will confuse function_graph tracer and will cause crashes with bad RIP. Teach graph tracer to skip functions that have BPF trampoline attached.
Unified · Split
| @@ -1042,20 +1042,6 @@ void prepare_ftrace_return(unsigned long self_addr, unsigned long *parent, · 1041 unmodified lines | ||
| 1042 | 1042 | if (unlikely(atomic_read(¤t->tracing_graph_pause))) |
| 1043 | 1043 | return; |
| 1044 | 1044 | |
| 1045 | − /* | |
| 1046 | − * If the return location is actually pointing directly to | |
| 1047 | − * the start of a direct trampoline (if we trace the trampoline | |
| 1048 | − * it will still be offset by MCOUNT_INSN_SIZE), then the | |
| 1049 | − * return address is actually off by one word, and we | |
| 1050 | − * need to adjust for that. | |
| 1051 | − */ | |
| 1052 | − if (ftrace_direct_func_count) { | |
| 1053 | − if (ftrace_find_direct_func(self_addr + MCOUNT_INSN_SIZE)) { | |
| 1054 | − self_addr = *parent; | |
| 1055 | − parent++; | |
| 1056 | − } | |
| 1057 | − } | |
| 1058 | − | |
| 1059 | 1045 | /* |
| 1060 | 1046 | * Protect against fault, even if it shouldn't |
| 1061 | 1047 | * happen. This tool is too much intrusive to |
| @@ -264,6 +264,7 @@ int ftrace_modify_direct_caller(struct ftrace_func_entry *entry, · 263 unmodified lines | ||
| 264 | 264 | struct dyn_ftrace *rec, |
| 265 | 265 | unsigned long old_addr, |
| 266 | 266 | unsigned long new_addr); |
| 267 | +unsigned long ftrace_find_rec_direct(unsigned long ip); | |
| 267 | 268 | #else |
| 268 | 269 | # define ftrace_direct_func_count 0 |
| 269 | 270 | static inline int register_ftrace_direct(unsigned long ip, unsigned long addr) |
| @@ -290,6 +291,10 @@ static inline int ftrace_modify_direct_caller(struct ftrace_func_entry *entry, · 20 unmodified lines | ||
| 290 | 291 | { |
| 291 | 292 | return -ENODEV; |
| 292 | 293 | } |
| 294 | +static inline unsigned long ftrace_find_rec_direct(unsigned long ip) | |
| 295 | +{ | |
| 296 | + return 0; | |
| 297 | +} | |
| 293 | 298 | #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */ |
| 294 | 299 | |
| 295 | 300 | #ifndef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS |
| @@ -101,6 +101,15 @@ int function_graph_enter(unsigned long ret, unsigned long func, · 100 unmodified lines | ||
| 101 | 101 | { |
| 102 | 102 | struct ftrace_graph_ent trace; |
| 103 | 103 | |
| 104 | + /* | |
| 105 | + * Skip graph tracing if the return location is served by direct trampoline, | |
| 106 | + * since call sequence and return addresses is unpredicatable anymore. | |
| 107 | + * Ex: BPF trampoline may call original function and may skip frame | |
| 108 | + * depending on type of BPF programs attached. | |
| 109 | + */ | |
| 110 | + if (ftrace_direct_func_count && | |
| 111 | + ftrace_find_rec_direct(ret - MCOUNT_INSN_SIZE)) | |
| 112 | + return -EBUSY; | |
| 104 | 113 | trace.func = func; |
| 105 | 114 | trace.depth = ++current->curr_ret_depth; |
| 106 | 115 | |
| @@ -2364,7 +2364,7 @@ int ftrace_direct_func_count; · 2363 unmodified lines | ||
| 2364 | 2364 | * Search the direct_functions hash to see if the given instruction pointer |
| 2365 | 2365 | * has a direct caller attached to it. |
| 2366 | 2366 | */ |
| 2367 | −static unsigned long find_rec_direct(unsigned long ip) | |
| 2367 | +unsigned long ftrace_find_rec_direct(unsigned long ip) | |
| 2368 | 2368 | { |
| 2369 | 2369 | struct ftrace_func_entry *entry; |
| 2370 | 2370 | |
| @@ -2380,7 +2380,7 @@ static void call_direct_funcs(unsigned long ip, unsigned long pip, · 9 unmodified lines | ||
| 2380 | 2380 | { |
| 2381 | 2381 | unsigned long addr; |
| 2382 | 2382 | |
| 2383 | − addr = find_rec_direct(ip); | |
| 2383 | + addr = ftrace_find_rec_direct(ip); | |
| 2384 | 2384 | if (!addr) |
| 2385 | 2385 | return; |
| 2386 | 2386 | |
| @@ -2393,11 +2393,6 @@ struct ftrace_ops direct_ops = { · 6 unmodified lines | ||
| 2393 | 2393 | | FTRACE_OPS_FL_DIRECT | FTRACE_OPS_FL_SAVE_REGS |
| 2394 | 2394 | | FTRACE_OPS_FL_PERMANENT, |
| 2395 | 2395 | }; |
| 2396 | −#else | |
| 2397 | −static inline unsigned long find_rec_direct(unsigned long ip) | |
| 2398 | −{ | |
| 2399 | − return 0; | |
| 2400 | −} | |
| 2401 | 2396 | #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */ |
| 2402 | 2397 | |
| 2403 | 2398 | /** |
| @@ -2417,7 +2412,7 @@ unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec) · 13 unmodified lines | ||
| 2417 | 2412 | |
| 2418 | 2413 | if ((rec->flags & FTRACE_FL_DIRECT) && |
| 2419 | 2414 | (ftrace_rec_count(rec) == 1)) { |
| 2420 | − addr = find_rec_direct(rec->ip); | |
| 2415 | + addr = ftrace_find_rec_direct(rec->ip); | |
| 2421 | 2416 | if (addr) |
| 2422 | 2417 | return addr; |
| 2423 | 2418 | WARN_ON_ONCE(1); |
| @@ -2458,7 +2453,7 @@ unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec) · 34 unmodified lines | ||
| 2458 | 2453 | |
| 2459 | 2454 | /* Direct calls take precedence over trampolines */ |
| 2460 | 2455 | if (rec->flags & FTRACE_FL_DIRECT_EN) { |
| 2461 | − addr = find_rec_direct(rec->ip); | |
| 2456 | + addr = ftrace_find_rec_direct(rec->ip); | |
| 2462 | 2457 | if (addr) |
| 2463 | 2458 | return addr; |
| 2464 | 2459 | WARN_ON_ONCE(1); |
| @@ -3604,7 +3599,7 @@ static int t_show(struct seq_file *m, void *v) · 1139 unmodified lines | ||
| 3604 | 3599 | if (rec->flags & FTRACE_FL_DIRECT) { |
| 3605 | 3600 | unsigned long direct; |
| 3606 | 3601 | |
| 3607 | − direct = find_rec_direct(rec->ip); | |
| 3602 | + direct = ftrace_find_rec_direct(rec->ip); | |
| 3608 | 3603 | if (direct) |
| 3609 | 3604 | seq_printf(m, "\n\tdirect-->%pS", (void *)direct); |
| 3610 | 3605 | } |
| @@ -5008,7 +5003,7 @@ int register_ftrace_direct(unsigned long ip, unsigned long addr) · 1397 unmodified lines | ||
| 5008 | 5003 | mutex_lock(&direct_mutex); |
| 5009 | 5004 | |
| 5010 | 5005 | /* See if there's a direct function at @ip already */ |
| 5011 | − if (find_rec_direct(ip)) | |
| 5006 | + if (ftrace_find_rec_direct(ip)) | |
| 5012 | 5007 | goto out_unlock; |
| 5013 | 5008 | |
| 5014 | 5009 | ret = -ENODEV; |
| @@ -5027,7 +5022,7 @@ int register_ftrace_direct(unsigned long ip, unsigned long addr) · 12 unmodified lines | ||
| 5027 | 5022 | if (ip != rec->ip) { |
| 5028 | 5023 | ip = rec->ip; |
| 5029 | 5024 | /* Need to check this ip for a direct. */ |
| 5030 | − if (find_rec_direct(ip)) | |
| 5025 | + if (ftrace_find_rec_direct(ip)) | |
| 5031 | 5026 | goto out_unlock; |
| 5032 | 5027 | } |
| 5033 | 5028 | |