Commit f9d532fc authored by Alexei Starovoitov's avatar Alexei Starovoitov

Merge branch 'bpf: use 32bit safe version of u64_stats'

Eric Dumazet says:

====================

From: Eric Dumazet <edumazet@google.com>

Two first patches fix bugs added in 5.1 and 5.5

Third patch replaces the u64 fields in struct bpf_prog_stats
with u64_stats_t ones to avoid possible sampling errors,
in case of load/store stearing.
====================
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parents 689624f0 61a0abae
...@@ -553,9 +553,9 @@ struct bpf_binary_header { ...@@ -553,9 +553,9 @@ struct bpf_binary_header {
}; };
struct bpf_prog_stats { struct bpf_prog_stats {
u64 cnt; u64_stats_t cnt;
u64 nsecs; u64_stats_t nsecs;
u64 misses; u64_stats_t misses;
struct u64_stats_sync syncp; struct u64_stats_sync syncp;
} __aligned(2 * sizeof(u64)); } __aligned(2 * sizeof(u64));
...@@ -612,13 +612,14 @@ static __always_inline u32 __bpf_prog_run(const struct bpf_prog *prog, ...@@ -612,13 +612,14 @@ static __always_inline u32 __bpf_prog_run(const struct bpf_prog *prog,
if (static_branch_unlikely(&bpf_stats_enabled_key)) { if (static_branch_unlikely(&bpf_stats_enabled_key)) {
struct bpf_prog_stats *stats; struct bpf_prog_stats *stats;
u64 start = sched_clock(); u64 start = sched_clock();
unsigned long flags;
ret = dfunc(ctx, prog->insnsi, prog->bpf_func); ret = dfunc(ctx, prog->insnsi, prog->bpf_func);
stats = this_cpu_ptr(prog->stats); stats = this_cpu_ptr(prog->stats);
u64_stats_update_begin(&stats->syncp); flags = u64_stats_update_begin_irqsave(&stats->syncp);
stats->cnt++; u64_stats_inc(&stats->cnt);
stats->nsecs += sched_clock() - start; u64_stats_add(&stats->nsecs, sched_clock() - start);
u64_stats_update_end(&stats->syncp); u64_stats_update_end_irqrestore(&stats->syncp, flags);
} else { } else {
ret = dfunc(ctx, prog->insnsi, prog->bpf_func); ret = dfunc(ctx, prog->insnsi, prog->bpf_func);
} }
......
...@@ -1804,8 +1804,14 @@ static int bpf_prog_release(struct inode *inode, struct file *filp) ...@@ -1804,8 +1804,14 @@ static int bpf_prog_release(struct inode *inode, struct file *filp)
return 0; return 0;
} }
struct bpf_prog_kstats {
u64 nsecs;
u64 cnt;
u64 misses;
};
static void bpf_prog_get_stats(const struct bpf_prog *prog, static void bpf_prog_get_stats(const struct bpf_prog *prog,
struct bpf_prog_stats *stats) struct bpf_prog_kstats *stats)
{ {
u64 nsecs = 0, cnt = 0, misses = 0; u64 nsecs = 0, cnt = 0, misses = 0;
int cpu; int cpu;
...@@ -1818,9 +1824,9 @@ static void bpf_prog_get_stats(const struct bpf_prog *prog, ...@@ -1818,9 +1824,9 @@ static void bpf_prog_get_stats(const struct bpf_prog *prog,
st = per_cpu_ptr(prog->stats, cpu); st = per_cpu_ptr(prog->stats, cpu);
do { do {
start = u64_stats_fetch_begin_irq(&st->syncp); start = u64_stats_fetch_begin_irq(&st->syncp);
tnsecs = st->nsecs; tnsecs = u64_stats_read(&st->nsecs);
tcnt = st->cnt; tcnt = u64_stats_read(&st->cnt);
tmisses = st->misses; tmisses = u64_stats_read(&st->misses);
} while (u64_stats_fetch_retry_irq(&st->syncp, start)); } while (u64_stats_fetch_retry_irq(&st->syncp, start));
nsecs += tnsecs; nsecs += tnsecs;
cnt += tcnt; cnt += tcnt;
...@@ -1836,7 +1842,7 @@ static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp) ...@@ -1836,7 +1842,7 @@ static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
{ {
const struct bpf_prog *prog = filp->private_data; const struct bpf_prog *prog = filp->private_data;
char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
struct bpf_prog_stats stats; struct bpf_prog_kstats stats;
bpf_prog_get_stats(prog, &stats); bpf_prog_get_stats(prog, &stats);
bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
...@@ -3577,7 +3583,7 @@ static int bpf_prog_get_info_by_fd(struct file *file, ...@@ -3577,7 +3583,7 @@ static int bpf_prog_get_info_by_fd(struct file *file,
struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info); struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
struct bpf_prog_info info; struct bpf_prog_info info;
u32 info_len = attr->info.info_len; u32 info_len = attr->info.info_len;
struct bpf_prog_stats stats; struct bpf_prog_kstats stats;
char __user *uinsns; char __user *uinsns;
u32 ulen; u32 ulen;
int err; int err;
......
...@@ -545,7 +545,7 @@ static void notrace inc_misses_counter(struct bpf_prog *prog) ...@@ -545,7 +545,7 @@ static void notrace inc_misses_counter(struct bpf_prog *prog)
stats = this_cpu_ptr(prog->stats); stats = this_cpu_ptr(prog->stats);
u64_stats_update_begin(&stats->syncp); u64_stats_update_begin(&stats->syncp);
stats->misses++; u64_stats_inc(&stats->misses);
u64_stats_update_end(&stats->syncp); u64_stats_update_end(&stats->syncp);
} }
...@@ -586,11 +586,13 @@ static void notrace update_prog_stats(struct bpf_prog *prog, ...@@ -586,11 +586,13 @@ static void notrace update_prog_stats(struct bpf_prog *prog,
* Hence check that 'start' is valid. * Hence check that 'start' is valid.
*/ */
start > NO_START_TIME) { start > NO_START_TIME) {
unsigned long flags;
stats = this_cpu_ptr(prog->stats); stats = this_cpu_ptr(prog->stats);
u64_stats_update_begin(&stats->syncp); flags = u64_stats_update_begin_irqsave(&stats->syncp);
stats->cnt++; u64_stats_inc(&stats->cnt);
stats->nsecs += sched_clock() - start; u64_stats_add(&stats->nsecs, sched_clock() - start);
u64_stats_update_end(&stats->syncp); u64_stats_update_end_irqrestore(&stats->syncp, flags);
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment