Skip to contentSkip to conversation

remove unnecessary null check in kernel_supports()

PATCH Applied Eduard Zingerman 3 participants 3 messages Jan 31, 2024 – Feb 1, 2024

Subsystems (2)
lore
Share & tools
lore

Eduard Zingerman

author maintainer
#0 lore raw

PATCH 1/1 remove unnecessary null check in kernel_supports()

After recent changes, Coverity complained about inconsistent null checks in kernel_supports() function:

    kernel_supports(const struct bpf_object *obj, ...)
    ...
    // var_compare_op: Comparing obj to null implies that obj might be null
    if (obj && obj->gen_loader)
        return true;

    // var_deref_op: Dereferencing null pointer obj
    if (obj->token_fd)
        return feat_supported(obj->feat_cache, feat_id);
    ...

- The original null check was introduced by commit [0], which introduced a call kernel_supports(NULL, ...) in function bump_rlimit_memlock();
- This call was refactored to use feat_supported(NULL, ...) in commit [1].

Looking at all places where kernel_supports() is called:
- there is either obj->... access before the call;
- or obj comes from prog->obj expression, where prog comes from enumeration of programs in obj;
- or obj comes from prog->obj, where prog is a parameter to one of the API functions:
- bpf_program__attach_kprobe_opts;
- bpf_program__attach_kprobe;
- bpf_program__attach_ksyscall.

Assuming correct API usage, it appears that obj can never be null when passed to kernel_supports(). Silence the Coverity warning by removing redundant null check.

[0] e542f2c4cd16 ("libbpf: Auto-bump RLIMIT_MEMLOCK if kernel needs it for BPF")
[1] d6dd1d49367a ("libbpf: Further decouple feature checking logic from bpf_object")

---
 tools/lib/bpf/libbpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +1 -1
index db65ea59a05a..f6953d7faff1 100644
@@ -4661,7 +4661,7 @@ bpf_object__probe_loading(struct bpf_object *obj)
 
 bool kernel_supports(const struct bpf_object *obj, enum kern_feature_id feat_id)
 {
-	if (obj && obj->gen_loader)
+	if (obj->gen_loader)
 		/* To generate loader program assume the latest kernel
 		 * to avoid doing extra prog_load, map_create syscalls.
 		 */
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 30 lines · Eduard Zingerman

After recent changes, Coverity complained about inconsistent null checks in kernel_supports() function:

     kernel_supports(const struct bpf_object *obj, ...)
     ...
     // var_compare_op: Comparing obj to null implies that obj might be null
     if (obj && obj->gen_loader)
         return true;

     // var_deref_op: Dereferencing null pointer obj
     if (obj->token_fd)
         return feat_supported(obj->feat_cache, feat_id);
     ...

- The original null check was introduced by commit [0], which introduced a call kernel_supports(NULL, ...) in function bump_rlimit_memlock();
- This call was refactored to use feat_supported(NULL, ...) in commit [1].

Looking at all places where kernel_supports() is called:
- there is either obj->... access before the call;
- or obj comes from prog->obj expression, where prog comes from enumeration of programs in obj;
- or obj comes from prog->obj, where prog is a parameter to one of the API functions:
- bpf_program__attach_kprobe_opts;
- bpf_program__attach_kprobe;
- bpf_program__attach_ksyscall.

Quoted 8 lines

Assuming correct API usage, it appears that obj can never be null when passed to kernel_supports(). Silence the Coverity warning by removing redundant null check.

[0] e542f2c4cd16 ("libbpf: Auto-bump RLIMIT_MEMLOCK if kernel needs it for BPF")
[1] d6dd1d49367a ("libbpf: Further decouple feature checking logic from bpf_object")

lgtm, also on the latter the struct bpf_program is only defined in libbpf.c, and you can only use libbpf.h APIs to retrieve a pointer of this type from the obj.

#2 lore raw
In reply to Eduard Zingerman
This patch was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Quoted 10 lines · On Wed, 31 Jan 2024 23:26:15 +0200 you wrote:

After recent changes, Coverity complained about inconsistent null checks in kernel_supports() function:

    kernel_supports(const struct bpf_object *obj, ...)
    ...
    // var_compare_op: Comparing obj to null implies that obj might be null
    if (obj && obj->gen_loader)
        return true;

[...]

Here is the summary with links:
- [bpf-next,libbpf] remove unnecessary null check in kernel_supports()

    https://git.kernel.org/bpf/bpf-next/c/8263b3382d8c

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html