Skip to content

Commit d090326

Browse files
kkdwivediAlexei Starovoitov
authored andcommitted
bpf: Ensure RCU lock is held around bpf_prog_ksym_find
Add a warning to ensure RCU lock is held around tree lookup, and then fix one of the invocations in bpf_stack_walker. The program has an active stack frame and won't disappear. Use the opportunity to remove unneeded invocation of is_bpf_text_address. Fixes: f18b03f ("bpf: Implement BPF exceptions") Reviewed-by: Emil Tsalapatis <[email protected]> Signed-off-by: Kumar Kartikeya Dwivedi <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 0e521ef commit d090326

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

kernel/bpf/core.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,10 @@ bool is_bpf_text_address(unsigned long addr)
782782

783783
struct bpf_prog *bpf_prog_ksym_find(unsigned long addr)
784784
{
785-
struct bpf_ksym *ksym = bpf_ksym_find(addr);
785+
struct bpf_ksym *ksym;
786+
787+
WARN_ON_ONCE(!rcu_read_lock_held());
788+
ksym = bpf_ksym_find(addr);
786789

787790
return ksym && ksym->prog ?
788791
container_of(ksym, struct bpf_prog_aux, ksym)->prog :

kernel/bpf/helpers.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2981,9 +2981,16 @@ static bool bpf_stack_walker(void *cookie, u64 ip, u64 sp, u64 bp)
29812981
struct bpf_throw_ctx *ctx = cookie;
29822982
struct bpf_prog *prog;
29832983

2984-
if (!is_bpf_text_address(ip))
2985-
return !ctx->cnt;
2984+
/*
2985+
* The RCU read lock is held to safely traverse the latch tree, but we
2986+
* don't need its protection when accessing the prog, since it has an
2987+
* active stack frame on the current stack trace, and won't disappear.
2988+
*/
2989+
rcu_read_lock();
29862990
prog = bpf_prog_ksym_find(ip);
2991+
rcu_read_unlock();
2992+
if (!prog)
2993+
return !ctx->cnt;
29872994
ctx->cnt++;
29882995
if (bpf_is_subprog(prog))
29892996
return true;

0 commit comments

Comments
 (0)