All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf] libbpf: reset errno after probing kernel features
@ 2020-11-30 15:41 Toke Høiland-Jørgensen
  2020-11-30 22:24 ` Andrii Nakryiko
  0 siblings, 1 reply; 4+ messages in thread
From: Toke Høiland-Jørgensen @ 2020-11-30 15:41 UTC (permalink / raw)
  To: daniel, ast, andrii; +Cc: Toke Høiland-Jørgensen, bpf, netdev

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.

Fixes: 47b6cb4d0add ("libbpf: Make kernel feature probing lazy")
Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
---
 tools/lib/bpf/libbpf.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 28baee7ba1ca..8d05132e1945 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf] libbpf: reset errno after probing kernel features
  2020-11-30 15:41 [PATCH bpf] libbpf: reset errno after probing kernel features Toke Høiland-Jørgensen
@ 2020-11-30 22:24 ` Andrii Nakryiko
  2020-11-30 22:40   ` Toke Høiland-Jørgensen
  0 siblings, 1 reply; 4+ messages in thread
From: Andrii Nakryiko @ 2020-11-30 22:24 UTC (permalink / raw)
  To: Toke Høiland-Jørgensen
  Cc: Daniel Borkmann, Alexei Starovoitov, Andrii Nakryiko, bpf, Networking

On Mon, Nov 30, 2020 at 7:42 AM Toke Høiland-Jørgensen <toke@redhat.com> wrote:
>
> 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?

>
> Fixes: 47b6cb4d0add ("libbpf: Make kernel feature probing lazy")
> Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
> ---
>  tools/lib/bpf/libbpf.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 28baee7ba1ca..8d05132e1945 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -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
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf] libbpf: reset errno after probing kernel features
  2020-11-30 22:24 ` Andrii Nakryiko
@ 2020-11-30 22:40   ` Toke Høiland-Jørgensen
  2020-12-01  0:18     ` Andrii Nakryiko
  0 siblings, 1 reply; 4+ messages in thread
From: Toke Høiland-Jørgensen @ 2020-11-30 22:40 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: Daniel Borkmann, Alexei Starovoitov, Andrii Nakryiko, bpf, Networking

Andrii Nakryiko <andrii.nakryiko@gmail.com> writes:

> On Mon, Nov 30, 2020 at 7:42 AM Toke Høiland-Jørgensen <toke@redhat.com> wrote:
>>
>> 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?

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf] libbpf: reset errno after probing kernel features
  2020-11-30 22:40   ` Toke Høiland-Jørgensen
@ 2020-12-01  0:18     ` Andrii Nakryiko
  0 siblings, 0 replies; 4+ messages in thread
From: Andrii Nakryiko @ 2020-12-01  0:18 UTC (permalink / raw)
  To: Toke Høiland-Jørgensen
  Cc: Daniel Borkmann, Alexei Starovoitov, Andrii Nakryiko, bpf, Networking

On Mon, Nov 30, 2020 at 2:41 PM Toke Høiland-Jørgensen <toke@redhat.com> wrote:
>
> Andrii Nakryiko <andrii.nakryiko@gmail.com> writes:
>
> > On Mon, Nov 30, 2020 at 7:42 AM Toke Høiland-Jørgensen <toke@redhat.com> wrote:
> >>
> >> 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?
>
> 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.

> 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.

>
> -Toke
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-12-01  0:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-30 15:41 [PATCH bpf] libbpf: reset errno after probing kernel features Toke Høiland-Jørgensen
2020-11-30 22:24 ` Andrii Nakryiko
2020-11-30 22:40   ` Toke Høiland-Jørgensen
2020-12-01  0:18     ` Andrii Nakryiko

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.