Skip to content

Commit

Permalink
bpf: Use this_cpu_{inc_return|dec} for prog->active
Browse files Browse the repository at this point in the history
[ Upstream commit c89e843 ]

Both __this_cpu_inc_return() and __this_cpu_dec() are not preemption
safe and now migrate_disable() doesn't disable preemption, so the update
of prog-active is not atomic and in theory under fully preemptible kernel
recurisve prevention may do not work.

Fixing by using the preemption-safe and IRQ-safe variants.

Fixes: ca06f55 ("bpf: Add per-program recursion prevention mechanism")
Signed-off-by: Hou Tao <[email protected]>
Acked-by: Alexei Starovoitov <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Martin KaFai Lau <[email protected]>
Signed-off-by: Sasha Levin <[email protected]>
  • Loading branch information
Hou Tao authored and gregkh committed Oct 24, 2022
1 parent ecbbef3 commit dd5273e
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions kernel/bpf/trampoline.c
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ u64 notrace __bpf_prog_enter(struct bpf_prog *prog, struct bpf_tramp_run_ctx *ru

run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx);

if (unlikely(__this_cpu_inc_return(*(prog->active)) != 1)) {
if (unlikely(this_cpu_inc_return(*(prog->active)) != 1)) {
inc_misses_counter(prog);
return 0;
}
Expand Down Expand Up @@ -620,7 +620,7 @@ void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start, struct bpf_tramp_
bpf_reset_run_ctx(run_ctx->saved_run_ctx);

update_prog_stats(prog, start);
__this_cpu_dec(*(prog->active));
this_cpu_dec(*(prog->active));
migrate_enable();
rcu_read_unlock();
}
Expand All @@ -631,7 +631,7 @@ u64 notrace __bpf_prog_enter_sleepable(struct bpf_prog *prog, struct bpf_tramp_r
migrate_disable();
might_fault();

if (unlikely(__this_cpu_inc_return(*(prog->active)) != 1)) {
if (unlikely(this_cpu_inc_return(*(prog->active)) != 1)) {
inc_misses_counter(prog);
return 0;
}
Expand All @@ -647,7 +647,7 @@ void notrace __bpf_prog_exit_sleepable(struct bpf_prog *prog, u64 start,
bpf_reset_run_ctx(run_ctx->saved_run_ctx);

update_prog_stats(prog, start);
__this_cpu_dec(*(prog->active));
this_cpu_dec(*(prog->active));
migrate_enable();
rcu_read_unlock_trace();
}
Expand Down

0 comments on commit dd5273e

Please sign in to comment.