[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <eb5315b4b05c881f82c91c071215f6edc3a4744f.1744169424.git.dxu@dxuuu.xyz>
Date: Tue, 8 Apr 2025 21:33:57 -0600
From: Daniel Xu <dxu@...uu.xyz>
To: andrii@...nel.org,
ast@...nel.org,
martin.lau@...ux.dev,
daniel@...earbox.net
Cc: eddyz87@...il.com,
song@...nel.org,
yonghong.song@...ux.dev,
john.fastabend@...il.com,
kpsingh@...nel.org,
sdf@...ichev.me,
haoluo@...gle.com,
jolsa@...nel.org,
bpf@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: [RFC bpf-next 02/13] bpf: Move BTF related globals out of verifier.c
Continuing with the effort to make verifier.c a leaf node, this commit
moves out bpf_verifier_lock, btf_vmlinux, and bpf_get_btf_vmlinux().
These can be owned by btf.c (perhaps rightly so).
Signed-off-by: Daniel Xu <dxu@...uu.xyz>
---
include/linux/btf.h | 4 ++++
kernel/bpf/btf.c | 21 +++++++++++++++++++--
kernel/bpf/verifier.c | 20 +++-----------------
3 files changed, 26 insertions(+), 19 deletions(-)
diff --git a/include/linux/btf.h b/include/linux/btf.h
index ebc0c0c9b944..c51032659c58 100644
--- a/include/linux/btf.h
+++ b/include/linux/btf.h
@@ -8,6 +8,7 @@
#include <linux/bpfptr.h>
#include <linux/bsearch.h>
#include <linux/btf_ids.h>
+#include <linux/mutex.h>
#include <uapi/linux/btf.h>
#include <uapi/linux/bpf.h>
@@ -141,6 +142,9 @@ struct btf_struct_metas {
extern const struct file_operations btf_fops;
+extern struct mutex btf_mutex;
+extern struct btf *btf_vmlinux;
+
const char *btf_get_name(const struct btf *btf);
void btf_get(struct btf *btf);
void btf_put(struct btf *btf);
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 16ba36f34dfa..5b38c90e1184 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -563,6 +563,18 @@ s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind)
return -ENOENT;
}
+struct btf *bpf_get_btf_vmlinux(void)
+{
+ if (!btf_vmlinux && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) {
+ mutex_lock(&btf_mutex);
+ if (!btf_vmlinux)
+ btf_vmlinux = btf_parse_vmlinux();
+ mutex_unlock(&btf_mutex);
+ }
+ return btf_vmlinux;
+}
+EXPORT_SYMBOL_GPL(bpf_get_btf_vmlinux);
+
s32 bpf_find_btf_id(const char *name, u32 kind, struct btf **btf_p)
{
struct btf *btf;
@@ -5857,7 +5869,12 @@ static struct btf *btf_parse(const union bpf_attr *attr, bpfptr_t uattr, u32 uat
extern char __start_BTF[];
extern char __stop_BTF[];
-extern struct btf *btf_vmlinux;
+
+DEFINE_MUTEX(btf_mutex);
+EXPORT_SYMBOL_GPL(btf_mutex);
+
+struct btf *btf_vmlinux;
+EXPORT_SYMBOL_GPL(btf_vmlinux);
#define BPF_MAP_TYPE(_id, _ops)
#define BPF_LINK_TYPE(_id, _name)
@@ -6252,7 +6269,7 @@ struct btf *btf_parse_vmlinux(void)
if (IS_ERR(btf))
goto err_out;
- /* btf_parse_vmlinux() runs under bpf_verifier_lock */
+ /* btf_parse_vmlinux() runs under btf_mutex */
bpf_ctx_convert.t = btf_type_by_id(btf, bpf_ctx_convert_btf_id[0]);
err = btf_alloc_id(btf);
if (err) {
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 4a60e2d7c10f..6ed302dab08b 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -345,14 +345,11 @@ struct bpf_kfunc_call_arg_meta {
u64 mem_size;
};
-struct btf *btf_vmlinux;
-
static const char *btf_type_name(const struct btf *btf, u32 id)
{
return btf_name_by_offset(btf, btf_type_by_id(btf, id)->name_off);
}
-static DEFINE_MUTEX(bpf_verifier_lock);
static DEFINE_MUTEX(bpf_percpu_ma_lock);
__printf(2, 3) static void verbose(void *private_data, const char *fmt, ...)
@@ -23518,17 +23515,6 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
return 0;
}
-struct btf *bpf_get_btf_vmlinux(void)
-{
- if (!btf_vmlinux && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) {
- mutex_lock(&bpf_verifier_lock);
- if (!btf_vmlinux)
- btf_vmlinux = btf_parse_vmlinux();
- mutex_unlock(&bpf_verifier_lock);
- }
- return btf_vmlinux;
-}
-
/*
* The add_fd_from_fd_array() is executed only if fd_array_cnt is non-zero. In
* this case expect that every file descriptor in the array is either a map or
@@ -23932,9 +23918,9 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3
bpf_get_btf_vmlinux();
- /* grab the mutex to protect few globals used by verifier */
+ /* grab the mutex to protect BTF globals used by verifier */
if (!is_priv)
- mutex_lock(&bpf_verifier_lock);
+ mutex_lock(&btf_mutex);
/* user could have requested verbose verifier output
* and supplied buffer to store the verification trace
@@ -24151,7 +24137,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3
module_put(env->attach_btf_mod);
err_unlock:
if (!is_priv)
- mutex_unlock(&bpf_verifier_lock);
+ mutex_unlock(&btf_mutex);
vfree(env->insn_aux_data);
kvfree(env->insn_hist);
err_free_env:
--
2.47.1
Powered by blists - more mailing lists