:p
atchew
Login
From: Geliang Tang <tanggeliang@kylinos.cn> v3: - fix this build error: kernel/bpf/btf.c:7750:11: error: incomplete definition of type 'struct module' Reported-by: kernel test robot <lkp@intel.com> Closes: https://lore.kernel.org/oe-kbuild-all/202402040934.Fph0XeEo-lkp@intel.com/ v2: - add register_check_missing_btf helper as Jiri suggested. Geliang Tang (2): bpf, btf: Add register_check_missing_btf helper bpf, btf: Check btf for register_bpf_struct_ops kernel/bpf/btf.c | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) -- 2.40.1
From: Geliang Tang <tanggeliang@kylinos.cn> This patch extracts duplicate code on error path when btf_get_module_btf() returns NULL from the functions __register_btf_kfunc_id_set() and register_btf_id_dtor_kfuncs() into a new helper named register_check_missing_btf() to check CONFIG_DEBUG_INFO_BTF, CONFIG_DEBUG_INFO_BTF_MODULES and CONFIG_MODULE_ALLOW_BTF_MISMATCH in it. Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn> --- kernel/bpf/btf.c | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index XXXXXXX..XXXXXXX 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -XXX,XX +XXX,XX @@ #include <linux/bsearch.h> #include <linux/kobject.h> #include <linux/sysfs.h> +#include <linux/module.h> #include <net/netfilter/nf_bpf_link.h> @@ -XXX,XX +XXX,XX @@ static struct btf *btf_get_module_btf(const struct module *module) return btf; } +static int register_check_missing_btf(const struct module *module, const char *msg) +{ + if (!module && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { + pr_err("missing vmlinux BTF, cannot register %s\n", msg); + return -ENOENT; + } + if (module && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES)) { + if (IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH)) { + pr_warn("allow module %s BTF mismatch, skip register %s\n", + module->name, msg); + return 0; + } + pr_err("missing module %s BTF, cannot register %s\n", module->name, msg); + return -ENOENT; + } + return 0; +} + BPF_CALL_4(bpf_btf_find_by_name_kind, char *, name, int, name_sz, u32, kind, int, flags) { struct btf *btf = NULL; @@ -XXX,XX +XXX,XX @@ static int __register_btf_kfunc_id_set(enum btf_kfunc_hook hook, int ret, i; btf = btf_get_module_btf(kset->owner); - if (!btf) { - if (!kset->owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { - pr_err("missing vmlinux BTF, cannot register kfuncs\n"); - return -ENOENT; - } - if (kset->owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES)) - pr_warn("missing module BTF, cannot register kfuncs\n"); - return 0; - } + if (!btf) + return register_check_missing_btf(kset->owner, "kfuncs"); if (IS_ERR(btf)) return PTR_ERR(btf); @@ -XXX,XX +XXX,XX @@ int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_c int ret; btf = btf_get_module_btf(owner); - if (!btf) { - if (!owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { - pr_err("missing vmlinux BTF, cannot register dtor kfuncs\n"); - return -ENOENT; - } - if (owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES)) { - pr_err("missing module BTF, cannot register dtor kfuncs\n"); - return -ENOENT; - } - return 0; - } + if (!btf) + return register_check_missing_btf(owner, "dtor kfuncs"); if (IS_ERR(btf)) return PTR_ERR(btf); -- 2.40.1
From: Geliang Tang <tanggeliang@kylinos.cn> Similar to the handling in the functions __register_btf_kfunc_id_set() and register_btf_id_dtor_kfuncs(), this patch uses the newly added helper register_check_missing_btf() and IS_ERR() to check the return value of btf_get_module_btf(). Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn> --- kernel/bpf/btf.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index XXXXXXX..XXXXXXX 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -XXX,XX +XXX,XX @@ int __register_bpf_struct_ops(struct bpf_struct_ops *st_ops) btf = btf_get_module_btf(st_ops->owner); if (!btf) - return -EINVAL; + return register_check_missing_btf(st_ops->owner, "structs"); + if (IS_ERR(btf)) + return PTR_ERR(btf); log = kzalloc(sizeof(*log), GFP_KERNEL | __GFP_NOWARN); if (!log) { -- 2.40.1
From: Geliang Tang <tanggeliang@kylinos.cn> v4: - add a new patch to fix error checks for btf_get_module_btf. - rename the helper to check_btf_kconfigs. v3: - fix this build error: kernel/bpf/btf.c:7750:11: error: incomplete definition of type 'struct module' Reported-by: kernel test robot <lkp@intel.com> Closes: https://lore.kernel.org/oe-kbuild-all/202402040934.Fph0XeEo-lkp@intel.com/ v2: - add register_check_missing_btf helper as Jiri suggested. Geliang Tang (3): bpf, btf: Fix error checks for btf_get_module_btf bpf, btf: Add check_btf_kconfigs helper bpf, btf: Check btf for register_bpf_struct_ops kernel/bpf/btf.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) -- 2.40.1
From: Geliang Tang <tanggeliang@kylinos.cn> To let the modules loaded, commit 3de4d22cc9ac ("bpf, btf: Warn but return no error for NULL btf from __register_btf_kfunc_id_set()") changes the return value of __register_btf_kfunc_id_set() from -ENOENT to 0 when btf is NULL. A better way to do this is to enable CONFIG_MODULE_ALLOW_BTF_MISMATCH. An error code -ENOENT should indeed be returned when kernel module BTF mismatch detected except CONFIG_MODULE_ALLOW_BTF_MISMATCH is enabled in __register_btf_kfunc_id_set(). The same in register_btf_id_dtor_kfuncs(), give the modules a chance to be loaded when CONFIG_MODULE_ALLOW_BTF_MISMATCH is enabled. Fixes: 3de4d22cc9ac ("bpf, btf: Warn but return no error for NULL btf from __register_btf_kfunc_id_set()") Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn> --- kernel/bpf/btf.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index XXXXXXX..XXXXXXX 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -XXX,XX +XXX,XX @@ static int __register_btf_kfunc_id_set(enum btf_kfunc_hook hook, pr_err("missing vmlinux BTF, cannot register kfuncs\n"); return -ENOENT; } - if (kset->owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES)) + if (kset->owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES) && + !IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH)) { pr_warn("missing module BTF, cannot register kfuncs\n"); + return -ENOENT; + } return 0; } if (IS_ERR(btf)) @@ -XXX,XX +XXX,XX @@ int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_c pr_err("missing vmlinux BTF, cannot register dtor kfuncs\n"); return -ENOENT; } - if (owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES)) { + if (owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES) && + !IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH)) { pr_err("missing module BTF, cannot register dtor kfuncs\n"); return -ENOENT; } -- 2.40.1
From: Geliang Tang <tanggeliang@kylinos.cn> This patch extracts duplicate code on error path when btf_get_module_btf() returns NULL from the functions __register_btf_kfunc_id_set() and register_btf_id_dtor_kfuncs() into a new helper named check_btf_kconfigs() to check CONFIG_DEBUG_INFO_BTF, CONFIG_DEBUG_INFO_BTF_MODULES and CONFIG_MODULE_ALLOW_BTF_MISMATCH in it. Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn> --- kernel/bpf/btf.c | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index XXXXXXX..XXXXXXX 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -XXX,XX +XXX,XX @@ static struct btf *btf_get_module_btf(const struct module *module) return btf; } +static int check_btf_kconfigs(const struct module *module) +{ + if (!module && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { + pr_err("missing vmlinux BTF, cannot register kfuncs\n"); + return -ENOENT; + } + if (module && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES) && + !IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH)) { + pr_err("missing module BTF, cannot register kfuncs\n"); + return -ENOENT; + } + return 0; +} + BPF_CALL_4(bpf_btf_find_by_name_kind, char *, name, int, name_sz, u32, kind, int, flags) { struct btf *btf = NULL; @@ -XXX,XX +XXX,XX @@ static int __register_btf_kfunc_id_set(enum btf_kfunc_hook hook, int ret, i; btf = btf_get_module_btf(kset->owner); - if (!btf) { - if (!kset->owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { - pr_err("missing vmlinux BTF, cannot register kfuncs\n"); - return -ENOENT; - } - if (kset->owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES) && - !IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH)) { - pr_warn("missing module BTF, cannot register kfuncs\n"); - return -ENOENT; - } - return 0; - } + if (!btf) + return check_btf_kconfigs(kset->owner); if (IS_ERR(btf)) return PTR_ERR(btf); @@ -XXX,XX +XXX,XX @@ int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_c int ret; btf = btf_get_module_btf(owner); - if (!btf) { - if (!owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { - pr_err("missing vmlinux BTF, cannot register dtor kfuncs\n"); - return -ENOENT; - } - if (owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES) && - !IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH)) { - pr_err("missing module BTF, cannot register dtor kfuncs\n"); - return -ENOENT; - } - return 0; - } + if (!btf) + return check_btf_kconfigs(owner); if (IS_ERR(btf)) return PTR_ERR(btf); -- 2.40.1
From: Geliang Tang <tanggeliang@kylinos.cn> Similar to the handling in the functions __register_btf_kfunc_id_set() and register_btf_id_dtor_kfuncs(), this patch uses the newly added helper check_btf_kconfigs() and IS_ERR() to check the return value of btf_get_module_btf(). Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn> --- kernel/bpf/btf.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index XXXXXXX..XXXXXXX 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -XXX,XX +XXX,XX @@ int __register_bpf_struct_ops(struct bpf_struct_ops *st_ops) btf = btf_get_module_btf(st_ops->owner); if (!btf) - return -EINVAL; + return check_btf_kconfigs(st_ops->owner); + if (IS_ERR(btf)) + return PTR_ERR(btf); log = kzalloc(sizeof(*log), GFP_KERNEL | __GFP_NOWARN); if (!log) { -- 2.40.1