Skip to content

Commit

Permalink
bpf: Add bpf_verifier_vlog() and bpf_verifier_log_needed()
Browse files Browse the repository at this point in the history
The BTF (BPF Type Format) verifier needs to reuse the current
BPF verifier log.  Hence, it requires the following changes:

(1) Expose log_write() in verifier.c for other users.
    Its name is renamed to bpf_verifier_vlog().

(2) The BTF verifier also needs to check
'log->level && log->ubuf && !bpf_verifier_log_full(log);'
independently outside of the current log_write().  It is
because the BTF verifier will do one-check before
making multiple calls to btf_verifier_vlog to log
the details of a type.

Hence, this check is also re-factored to a new function
bpf_verifier_log_needed().  Since it is re-factored,
we can check it before va_start() in the current
bpf_verifier_log_write() and verbose().

Signed-off-by: Martin KaFai Lau <[email protected]>
Acked-by: Alexei Starovoitov <[email protected]>
Signed-off-by: Daniel Borkmann <[email protected]>
  • Loading branch information
iamkafai authored and borkmann committed Mar 26, 2018
1 parent b9193c1 commit 77d2e05
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
7 changes: 7 additions & 0 deletions include/linux/bpf_verifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ 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 @@ -192,6 +197,8 @@ struct bpf_verifier_env {
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
19 changes: 11 additions & 8 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_verifier_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

0 comments on commit 77d2e05

Please sign in to comment.