libbpf: reset errno after probing kernel features
Subsystems (4)
PATCH 1/1 libbpf: reset errno after probing kernel features
The kernel feature probing results in 'errno' being set if the probing fails (as is often the case). This can stick around and leak to the caller, which can lead to confusion later. So let's make sure we always reset errno after calling a probe function.
--- tools/lib/bpf/libbpf.c | 2 ++ 1 file changed, 2 insertions(+)
tools/lib/bpf/libbpf.c +2 -0
@@ -4021,6 +4021,8 @@ static bool kernel_supports(enum kern_feature_id feat_id)pr_warn ("Detection of kernel %s support failed: %d\n" , feat->desc, ret);WRITE_ONCE (feat->res,FEAT_MISSING ); } +/* reset errno after probing to prevent leaking it to caller */ + errno =0 ; }return READ_ONCE (feat->res) ==FEAT_SUPPORTED ;
Signature · 1 line
2.29.2
Quoted 5 lines · Toke Høiland-Jørgensen
The kernel feature probing results in 'errno' being set if the probing fails (as is often the case). This can stick around and leak to the caller, which can lead to confusion later. So let's make sure we always reset errno after calling a probe function.
What specifically is the problem and what sort of confusion we are talking about here? You are not supposed to check errno, unless the function returned -1 or other error result.
In some cases, you have to reset errno manually just to avoid confusion (see how strtol() is used, as an example).
I.e., I don't see the problem here, any printf() technically can set errno to <0, we don't reset errno after each printf call though, right?
Quoted 23 lines
Fixes 47b6cb4d0add ("libbpf: Make kernel feature probing lazy")Signed-off-by Toke Høiland-Jørgensen <[email protected]>--- tools/lib/bpf/libbpf.c | 2 ++ 1 file changed, 2 insertions(+)tools/lib/bpf/libbpf.c +2
-1@@ -4021,6 +4021,8 @@ static bool kernel_supports(enum kern_feature_id feat_id)pr_warn ("Detection of kernel %s support failed: %d\n" , feat->desc, ret);WRITE_ONCE (feat->res,FEAT_MISSING ); } +/* reset errno after probing to prevent leaking it to caller */ + errno =0 ; }return READ_ONCE (feat->res) ==FEAT_SUPPORTED ; --2.29.2
Quoted 11 lines · Andrii Nakryiko
Quoted 5 lines · Toke Høiland-Jørgensen
The kernel feature probing results in 'errno' being set if the probing fails (as is often the case). This can stick around and leak to the caller, which can lead to confusion later. So let's make sure we always reset errno after calling a probe function.
What specifically is the problem and what sort of confusion we are talking about here? You are not supposed to check errno, unless the function returned -1 or other error result.
Quoted 6 lines
In some cases, you have to reset errno manually just to avoid confusion (see how
strtol()is used, as an example).I.e., I don't see the problem here, any
printf()technically can set errno to <0, we don't reset errno after each printf call though, right?
Well yeah, technically things work fine in the common case. But this errno thing sent me on quite the wild goose chase when trying to find the root cause of the pinning issue I also sent a patch for...
So since reseting errno doesn't hurt either I figured I'd save others ending up in similar trouble. If it's not to your taste feel free to just drop the patch :)
-Toke
Quoted 14 lines · Toke Høiland-Jørgensen
Quoted 11 lines · Andrii Nakryiko
Quoted 5 lines · Toke Høiland-Jørgensen
The kernel feature probing results in 'errno' being set if the probing fails (as is often the case). This can stick around and leak to the caller, which can lead to confusion later. So let's make sure we always reset errno after calling a probe function.
What specifically is the problem and what sort of confusion we are talking about here? You are not supposed to check errno, unless the function returned -1 or other error result.
Quoted 8 lines
Quoted 6 lines
In some cases, you have to reset errno manually just to avoid confusion (see how
strtol()is used, as an example).I.e., I don't see the problem here, any
printf()technically can set errno to <0, we don't reset errno after each printf call though, right?Well yeah, technically things work fine in the common case. But this
It works fine in all cases. Assuming "errno != 0 means last libc/syscall failed" is just wrong.
Quoted 6 lines
errno thing sent me on quite the wild goose chase when trying to find the root cause of the pinning issue I also sent a patch for...
So since reseting errno doesn't hurt either I figured I'd save others ending up in similar trouble. If it's not to your taste feel free to just drop the patch :)
Yep, let's just drop it, no need to create a bad precedent.
Quoted 3 lines
-Toke