From b2a76fa63f19036fbc9b3a705fbfa6358992ae22 Mon Sep 17 00:00:00 2001 From: Hengqi Chen Date: Wed, 9 Jun 2021 13:42:54 +0800 Subject: [PATCH] libbpf-tools: optimize fentry_exists helper The previous implementation checks fentry support either in vmlinux or module BTF. So we need two calls to fentry_exists to verify that whether a symbol exists. This commit updates this behavior to use the module name provided as a hint, and fallback to vmlinux if module BTF is not available. Signed-off-by: Hengqi Chen --- libbpf-tools/fsdist.c | 3 +-- libbpf-tools/fsslower.c | 3 +-- libbpf-tools/trace_helpers.c | 9 +++++---- libbpf-tools/trace_helpers.h | 4 ++-- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/libbpf-tools/fsdist.c b/libbpf-tools/fsdist.c index c8b6e248..304158b8 100644 --- a/libbpf-tools/fsdist.c +++ b/libbpf-tools/fsdist.c @@ -230,8 +230,7 @@ static bool check_fentry() for (i = 0; i < MAX_OP; i++) { fn_name = fs_configs[fs_type].op_funcs[i]; module = fs_configs[fs_type].fs; - if (fn_name && !fentry_exists(fn_name, NULL) - && !fentry_exists(fn_name, module)) { + if (fn_name && !fentry_exists(fn_name, module)) { support_fentry = false; break; } diff --git a/libbpf-tools/fsslower.c b/libbpf-tools/fsslower.c index b21cd7f0..6ffefefc 100644 --- a/libbpf-tools/fsslower.c +++ b/libbpf-tools/fsslower.c @@ -202,8 +202,7 @@ static bool check_fentry() for (i = 0; i < MAX_OP; i++) { fn_name = fs_configs[fs_type].op_funcs[i]; module = fs_configs[fs_type].fs; - if (fn_name && !fentry_exists(fn_name, NULL) - && !fentry_exists(fn_name, module)) { + if (fn_name && !fentry_exists(fn_name, module)) { support_fentry = false; break; } diff --git a/libbpf-tools/trace_helpers.c b/libbpf-tools/trace_helpers.c index 04a7a0b9..0e9b0192 100644 --- a/libbpf-tools/trace_helpers.c +++ b/libbpf-tools/trace_helpers.c @@ -1013,16 +1013,18 @@ bool fentry_exists(const char *name, const char *mod) sysfs_vmlinux, strerror(-libbpf_get_error(base))); goto err_out; } - if (mod) { + if (mod && module_btf_exists(mod)) { snprintf(sysfs_mod, sizeof(sysfs_mod), "/sys/kernel/btf/%s", mod); btf = btf__parse_split(sysfs_mod, base); if (libbpf_get_error(btf)) { fprintf(stderr, "failed to load BTF from %s: %s\n", sysfs_mod, strerror(-libbpf_get_error(btf))); - goto err_out; + btf = base; + base = NULL; } } else { btf = base; + base = NULL; } id = btf__find_by_name_kind(btf, "bpf_attach_type", BTF_KIND_ENUM); @@ -1044,8 +1046,7 @@ bool fentry_exists(const char *name, const char *mod) } err_out: - if (mod) - btf__free(btf); + btf__free(btf); btf__free(base); return id > 0; } diff --git a/libbpf-tools/trace_helpers.h b/libbpf-tools/trace_helpers.h index 019380af..61cbe433 100644 --- a/libbpf-tools/trace_helpers.h +++ b/libbpf-tools/trace_helpers.h @@ -75,8 +75,8 @@ bool is_kernel_module(const char *name); * * *name* is the name of a kernel function to be attached to, which can be * from vmlinux or a kernel module. - * *mod* is the name of a kernel module, if NULL, it means *name* - * belongs to vmlinux. + * *mod* is a hint that indicates the *name* may reside in module BTF, + * if NULL, it means *name* belongs to vmlinux. */ bool fentry_exists(const char *name, const char *mod); -- 2.34.1