[<prev] [next>] [day] [month] [year] [list]
Message-Id: <20240724152521.20546-1-me@manjusaka.me>
Date: Wed, 24 Jul 2024 15:25:20 +0000
From: Manjusaka <me@...jusaka.me>
To: Alexei Starovoitov <ast@...nel.org>,
Daniel Borkmann <daniel@...earbox.net>,
John Fastabend <john.fastabend@...il.com>,
Andrii Nakryiko <andrii@...nel.org>,
Martin KaFai Lau <martin.lau@...ux.dev>,
Eduard Zingerman <eddyz87@...il.com>,
Song Liu <song@...nel.org>,
Yonghong Song <yonghong.song@...ux.dev>,
KP Singh <kpsingh@...nel.org>,
Stanislav Fomichev <sdf@...ichev.me>,
Hao Luo <haoluo@...gle.com>,
Jiri Olsa <jolsa@...nel.org>
Cc: Manjusaka <me@...jusaka.me>,
Leon Hwang <hffilwlqm@...il.com>,
bpf@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: [PATCH] bpf: Add bpf_check_attach_target_with_kernel_log method to output failure logs to kernel
When attaching a freplace hook, failures can occur,
but currently, no output is provided to help developers diagnose the root cause.
This commit adds a new method, bpf_check_attach_target_with_kernel_log,
which outputs the verifier log to the kernel.
Developers can then use dmesg to obtain more detailed information about the failure.
For an example of eBPF code,
Link: https://github.com/Asphaltt/learn-by-example/blob/main/ebpf/freplace/main.go
Co-developed-by: Leon Hwang <hffilwlqm@...il.com>
Signed-off-by: Leon Hwang <hffilwlqm@...il.com>
Signed-off-by: Manjusaka <me@...jusaka.me>
---
include/linux/bpf_verifier.h | 7 +++++++
kernel/bpf/syscall.c | 3 ++-
kernel/bpf/trampoline.c | 3 ++-
kernel/bpf/verifier.c | 19 +++++++++++++++++++
4 files changed, 30 insertions(+), 2 deletions(-)
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 6503c85b10a3..0ba119665410 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -830,11 +830,18 @@ static inline void bpf_trampoline_unpack_key(u64 key, u32 *obj_id, u32 *btf_id)
*btf_id = key & 0x7FFFFFFF;
}
+int bpf_check_attach_target_with_kernel_log(const struct bpf_prog *prog,
+ const struct bpf_prog *tgt_prog,
+ u32 btf_id,
+ struct bpf_attach_target_info *tgt_info);
+
int bpf_check_attach_target(struct bpf_verifier_log *log,
const struct bpf_prog *prog,
const struct bpf_prog *tgt_prog,
u32 btf_id,
struct bpf_attach_target_info *tgt_info);
+
+
void bpf_free_kfunc_btf_tab(struct bpf_kfunc_btf_tab *tab);
int mark_chain_precision(struct bpf_verifier_env *env, int regno);
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 0719192a3482..d6ae9e8c40b2 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -3464,8 +3464,9 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog,
*/
struct bpf_attach_target_info tgt_info = {};
- err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id,
+ err = bpf_check_attach_target_with_kernel_log(prog, tgt_prog, btf_id,
&tgt_info);
+
if (err)
goto out_unlock;
diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c
index f8302a5ca400..22dcc058e0d6 100644
--- a/kernel/bpf/trampoline.c
+++ b/kernel/bpf/trampoline.c
@@ -699,9 +699,10 @@ int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,
u64 key;
int err;
- err = bpf_check_attach_target(NULL, prog, NULL,
+ err = bpf_check_attach_target_with_kernel_log(prog, NULL,
prog->aux->attach_btf_id,
&tgt_info);
+
if (err)
return err;
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 4cb5441ad75f..1d5dbbcac1bd 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -21145,6 +21145,25 @@ static int check_non_sleepable_error_inject(u32 btf_id)
return btf_id_set_contains(&btf_non_sleepable_error_inject, btf_id);
}
+int bpf_check_attach_target_with_kernel_log(const struct bpf_prog *prog,
+ const struct bpf_prog *tgt_prog,
+ u32 btf_id,
+ struct bpf_attach_target_info *tgt_info)
+{
+ struct bpf_verifier_log *log;
+ int err;
+
+ log = kzalloc(sizeof(*log), GFP_KERNEL | __GFP_NOWARN);
+ if (!log) {
+ err = -ENOMEM;
+ return err;
+ }
+ log->level = BPF_LOG_KERNEL;
+ err = bpf_check_attach_target(log, prog, tgt_prog, btf_id, tgt_info);
+ kfree(log);
+ return err;
+}
+
int bpf_check_attach_target(struct bpf_verifier_log *log,
const struct bpf_prog *prog,
const struct bpf_prog *tgt_prog,
--
2.34.1
Powered by blists - more mailing lists