[PATCH bpf-next v5 2/3] bpf, btf: Add check_btf_kconfigs helper

Geliang Tang posted 3 patches 7 months ago
[PATCH bpf-next v5 2/3] bpf, btf: Add check_btf_kconfigs helper
Posted by Geliang Tang 7 months ago
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 and CONFIG_DEBUG_INFO_BTF_MODULES in it.

Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
---
 kernel/bpf/btf.c | 33 +++++++++++++++------------------
 1 file changed, 15 insertions(+), 18 deletions(-)

diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 16eb937eca46..e318df7f0071 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -7738,6 +7738,17 @@ 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))
+		pr_warn("missing module BTF, cannot register kfuncs\n");
+	return 0;
+}
+
 BPF_CALL_4(bpf_btf_find_by_name_kind, char *, name, int, name_sz, u32, kind, int, flags)
 {
 	struct btf *btf = NULL;
@@ -8098,15 +8109,8 @@ 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 check_btf_kconfigs(kset->owner);
 	if (IS_ERR(btf))
 		return PTR_ERR(btf);
 
@@ -8214,15 +8218,8 @@ 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_warn("missing module BTF, cannot register dtor kfuncs\n");
-		return 0;
-	}
+	if (!btf)
+		return check_btf_kconfigs(owner);
 	if (IS_ERR(btf))
 		return PTR_ERR(btf);
 
-- 
2.40.1
Re: [PATCH bpf-next v5 2/3] bpf, btf: Add check_btf_kconfigs helper
Posted by Jiri Olsa 7 months ago
On Thu, Feb 08, 2024 at 02:24:22PM +0800, Geliang Tang wrote:
> 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 and CONFIG_DEBUG_INFO_BTF_MODULES in it.
> 
> Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
> ---
>  kernel/bpf/btf.c | 33 +++++++++++++++------------------
>  1 file changed, 15 insertions(+), 18 deletions(-)
> 
> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> index 16eb937eca46..e318df7f0071 100644
> --- a/kernel/bpf/btf.c
> +++ b/kernel/bpf/btf.c
> @@ -7738,6 +7738,17 @@ 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))
> +		pr_warn("missing module BTF, cannot register kfuncs\n");
> +	return 0;
> +}
> +
>  BPF_CALL_4(bpf_btf_find_by_name_kind, char *, name, int, name_sz, u32, kind, int, flags)
>  {
>  	struct btf *btf = NULL;
> @@ -8098,15 +8109,8 @@ 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 check_btf_kconfigs(kset->owner);
>  	if (IS_ERR(btf))
>  		return PTR_ERR(btf);
>  
> @@ -8214,15 +8218,8 @@ 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_warn("missing module BTF, cannot register dtor kfuncs\n");

nit, we do lose the 'dtor' from the message but I think it's ok,
for the patchset:

Acked-by: Jiri Olsa <jolsa@kernel.org>

jirka

> -		return 0;
> -	}
> +	if (!btf)
> +		return check_btf_kconfigs(owner);
>  	if (IS_ERR(btf))
>  		return PTR_ERR(btf);
>  
> -- 
> 2.40.1
>
Re: [PATCH bpf-next v5 2/3] bpf, btf: Add check_btf_kconfigs helper
Posted by Martin KaFai Lau 7 months ago
On 2/8/24 2:07 AM, Jiri Olsa wrote:
>> +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))
>> +		pr_warn("missing module BTF, cannot register kfuncs\n");
>> +	return 0;
>> +}
>> +
>>   BPF_CALL_4(bpf_btf_find_by_name_kind, char *, name, int, name_sz, u32, kind, int, flags)
>>   {
>>   	struct btf *btf = NULL;
>> @@ -8098,15 +8109,8 @@ 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 check_btf_kconfigs(kset->owner);
>>   	if (IS_ERR(btf))
>>   		return PTR_ERR(btf);
>>   
>> @@ -8214,15 +8218,8 @@ 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_warn("missing module BTF, cannot register dtor kfuncs\n");
> nit, we do lose the 'dtor' from the message but I think it's ok,
> for the patchset:

I added "const char *feature" argument to check_btf_kconfigs(), so it is like 
check_btf_kconfigs(, "kfunc"), check_btf_kconfigs(, "dtor kfunc"), and 
check_btf_kconfigs(, "struct_ops"). Applied. Thanks.