Skip to content

Commit c11f34e

Browse files
image-dragonAlexei Starovoitov
authored andcommitted
bpf: Make update_prog_stats() always_inline
The function update_prog_stats() will be called in the bpf trampoline. In most cases, it will be optimized by the compiler by making it inline. However, we can't rely on the compiler all the time, and just make it __always_inline to reduce the possible overhead. Signed-off-by: Menglong Dong <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 99fe8af commit c11f34e

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

kernel/bpf/trampoline.c

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -911,27 +911,32 @@ static u64 notrace __bpf_prog_enter_recur(struct bpf_prog *prog, struct bpf_tram
911911
return bpf_prog_start_time();
912912
}
913913

914-
static void notrace update_prog_stats(struct bpf_prog *prog,
915-
u64 start)
914+
static void notrace __update_prog_stats(struct bpf_prog *prog, u64 start)
916915
{
917916
struct bpf_prog_stats *stats;
917+
unsigned long flags;
918+
u64 duration;
918919

919-
if (static_branch_unlikely(&bpf_stats_enabled_key) &&
920-
/* static_key could be enabled in __bpf_prog_enter*
921-
* and disabled in __bpf_prog_exit*.
922-
* And vice versa.
923-
* Hence check that 'start' is valid.
924-
*/
925-
start > NO_START_TIME) {
926-
u64 duration = sched_clock() - start;
927-
unsigned long flags;
928-
929-
stats = this_cpu_ptr(prog->stats);
930-
flags = u64_stats_update_begin_irqsave(&stats->syncp);
931-
u64_stats_inc(&stats->cnt);
932-
u64_stats_add(&stats->nsecs, duration);
933-
u64_stats_update_end_irqrestore(&stats->syncp, flags);
934-
}
920+
/*
921+
* static_key could be enabled in __bpf_prog_enter* and disabled in
922+
* __bpf_prog_exit*. And vice versa. Check that 'start' is valid.
923+
*/
924+
if (start <= NO_START_TIME)
925+
return;
926+
927+
duration = sched_clock() - start;
928+
stats = this_cpu_ptr(prog->stats);
929+
flags = u64_stats_update_begin_irqsave(&stats->syncp);
930+
u64_stats_inc(&stats->cnt);
931+
u64_stats_add(&stats->nsecs, duration);
932+
u64_stats_update_end_irqrestore(&stats->syncp, flags);
933+
}
934+
935+
static __always_inline void notrace update_prog_stats(struct bpf_prog *prog,
936+
u64 start)
937+
{
938+
if (static_branch_unlikely(&bpf_stats_enabled_key))
939+
__update_prog_stats(prog, start);
935940
}
936941

937942
static void notrace __bpf_prog_exit_recur(struct bpf_prog *prog, u64 start,

0 commit comments

Comments
 (0)