Skip to content

Commit

Permalink
Merge branch 'bpf-verifier-log-btf-prep'
Browse files Browse the repository at this point in the history
Martin KaFai Lau says:

====================
This patch set has some changes and clean-up works for
the bpf_verifier_log.  They are the prep works for the
BTF (BPF Type Format).
====================

Signed-off-by: Daniel Borkmann <[email protected]>
  • Loading branch information
borkmann committed Mar 26, 2018
2 parents 639a53d + 77d2e05 commit 2abb5fa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
13 changes: 10 additions & 3 deletions include/linux/bpf_verifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,19 +153,24 @@ struct bpf_insn_aux_data {

#define BPF_VERIFIER_TMP_LOG_SIZE 1024

struct bpf_verifer_log {
struct bpf_verifier_log {
u32 level;
char kbuf[BPF_VERIFIER_TMP_LOG_SIZE];
char __user *ubuf;
u32 len_used;
u32 len_total;
};

static inline bool bpf_verifier_log_full(const struct bpf_verifer_log *log)
static inline bool bpf_verifier_log_full(const struct bpf_verifier_log *log)
{
return log->len_used >= log->len_total - 1;
}

static inline bool bpf_verifier_log_needed(const struct bpf_verifier_log *log)
{
return log->level && log->ubuf && !bpf_verifier_log_full(log);
}

#define BPF_MAX_SUBPROGS 256

/* single container for all structs
Expand All @@ -185,13 +190,15 @@ struct bpf_verifier_env {
bool allow_ptr_leaks;
bool seen_direct_write;
struct bpf_insn_aux_data *insn_aux_data; /* array of per-insn state */
struct bpf_verifer_log log;
struct bpf_verifier_log log;
u32 subprog_starts[BPF_MAX_SUBPROGS];
/* computes the stack depth of each bpf function */
u16 subprog_stack_depth[BPF_MAX_SUBPROGS + 1];
u32 subprog_cnt;
};

void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt,
va_list args);
__printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env,
const char *fmt, ...);

Expand Down
21 changes: 12 additions & 9 deletions kernel/bpf/verifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,11 @@ struct bpf_call_arg_meta {

static DEFINE_MUTEX(bpf_verifier_lock);

static void log_write(struct bpf_verifier_env *env, const char *fmt,
va_list args)
void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt,
va_list args)
{
struct bpf_verifer_log *log = &env->log;
unsigned int n;

if (!log->level || !log->ubuf || bpf_verifier_log_full(log))
return;

n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args);

WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1,
Expand All @@ -200,18 +196,25 @@ __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env,
{
va_list args;

if (!bpf_verifier_log_needed(&env->log))
return;

va_start(args, fmt);
log_write(env, fmt, args);
bpf_verifier_vlog(&env->log, fmt, args);
va_end(args);
}
EXPORT_SYMBOL_GPL(bpf_verifier_log_write);

__printf(2, 3) static void verbose(void *private_data, const char *fmt, ...)
{
struct bpf_verifier_env *env = private_data;
va_list args;

if (!bpf_verifier_log_needed(&env->log))
return;

va_start(args, fmt);
log_write(private_data, fmt, args);
bpf_verifier_vlog(&env->log, fmt, args);
va_end(args);
}

Expand Down Expand Up @@ -5611,7 +5614,7 @@ static void free_states(struct bpf_verifier_env *env)
int bpf_check(struct bpf_prog **prog, union bpf_attr *attr)
{
struct bpf_verifier_env *env;
struct bpf_verifer_log *log;
struct bpf_verifier_log *log;
int ret = -EINVAL;

/* no program is valid */
Expand Down

0 comments on commit 2abb5fa

Please sign in to comment.