bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 bpf-next] libbpf: add bpf object kern_version attribute setter
@ 2021-03-20 20:28 Rafael David Tinoco
  2021-03-22 17:06 ` Andrii Nakryiko
  0 siblings, 1 reply; 4+ messages in thread
From: Rafael David Tinoco @ 2021-03-20 20:28 UTC (permalink / raw)
  To: bpf, rafaeldtinoco; +Cc: andrii.nakryiko, daniel

Unfortunately some distros don't have their kernel version defined
accurately in <linux/version.h> due to different long term support
reasons.

It is important to have a way to override the bpf kern_version
attribute during runtime: some old kernels might still check for
kern_version attribute during bpf_prog_load().

Signed-off-by: Rafael David Tinoco <rafaeldtinoco@ubuntu.com>
---
 src/libbpf.c | 10 ++++++++++
 src/libbpf.h |  1 +
 2 files changed, 11 insertions(+)

diff --git a/src/libbpf.c b/src/libbpf.c
index 2f351d3..3b1c79f 100644
--- a/src/libbpf.c
+++ b/src/libbpf.c
@@ -8278,6 +8278,16 @@ int bpf_object__btf_fd(const struct bpf_object *obj)
 	return obj->btf ? btf__fd(obj->btf) : -1;
 }
 
+int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version)
+{
+	if (obj->loaded)
+		return -1;
+
+	obj->kern_version = kern_version;
+
+	return 0;
+}
+
 int bpf_object__set_priv(struct bpf_object *obj, void *priv,
 			 bpf_object_clear_priv_t clear_priv)
 {
diff --git a/src/libbpf.h b/src/libbpf.h
index 3c35eb4..f73ec5b 100644
--- a/src/libbpf.h
+++ b/src/libbpf.h
@@ -143,6 +143,7 @@ LIBBPF_API int bpf_object__unload(struct bpf_object *obj);
 
 LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj);
 LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj);
+LIBBPF_API int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version);
 
 struct btf;
 LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj);
-- 
2.27.0


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

* Re: [PATCH v2 bpf-next] libbpf: add bpf object kern_version attribute setter
  2021-03-20 20:28 [PATCH v2 bpf-next] libbpf: add bpf object kern_version attribute setter Rafael David Tinoco
@ 2021-03-22 17:06 ` Andrii Nakryiko
  2021-03-22 19:36   ` Rafael David Tinoco
  0 siblings, 1 reply; 4+ messages in thread
From: Andrii Nakryiko @ 2021-03-22 17:06 UTC (permalink / raw)
  To: Rafael David Tinoco; +Cc: bpf, Daniel Borkmann

On Sat, Mar 20, 2021 at 1:28 PM Rafael David Tinoco
<rafaeldtinoco@ubuntu.com> wrote:
>
> Unfortunately some distros don't have their kernel version defined
> accurately in <linux/version.h> due to different long term support
> reasons.
>
> It is important to have a way to override the bpf kern_version
> attribute during runtime: some old kernels might still check for
> kern_version attribute during bpf_prog_load().
>
> Signed-off-by: Rafael David Tinoco <rafaeldtinoco@ubuntu.com>
> ---

Ok, getting close, but probably another iteration is needed.

>  src/libbpf.c | 10 ++++++++++
>  src/libbpf.h |  1 +
>  2 files changed, 11 insertions(+)
>
> diff --git a/src/libbpf.c b/src/libbpf.c
> index 2f351d3..3b1c79f 100644
> --- a/src/libbpf.c
> +++ b/src/libbpf.c
> @@ -8278,6 +8278,16 @@ int bpf_object__btf_fd(const struct bpf_object *obj)
>         return obj->btf ? btf__fd(obj->btf) : -1;
>  }
>
> +int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version)
> +{
> +       if (obj->loaded)
> +               return -1;

other functions return -EINVAL in such cases. -1 is actually -EPERM,
which is very misleading, please update


> +
> +       obj->kern_version = kern_version;
> +
> +       return 0;
> +}
> +
>  int bpf_object__set_priv(struct bpf_object *obj, void *priv,
>                          bpf_object_clear_priv_t clear_priv)
>  {
> diff --git a/src/libbpf.h b/src/libbpf.h
> index 3c35eb4..f73ec5b 100644
> --- a/src/libbpf.h
> +++ b/src/libbpf.h
> @@ -143,6 +143,7 @@ LIBBPF_API int bpf_object__unload(struct bpf_object *obj);
>
>  LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj);
>  LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj);
> +LIBBPF_API int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version);

have you run libbpf's Makefile? It should have complained about
bpf_object__set_kversion symbol mismatches/etc. That means that this
API needs to be listed in libbpf.map file, please add it there (to
latest version, 0.4, and also preserve alphabetical order). Thanks.

>
>  struct btf;
>  LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj);
> --
> 2.27.0
>

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

* Re: [PATCH v2 bpf-next] libbpf: add bpf object kern_version attribute setter
  2021-03-22 17:06 ` Andrii Nakryiko
@ 2021-03-22 19:36   ` Rafael David Tinoco
  2021-03-22 19:46     ` Andrii Nakryiko
  0 siblings, 1 reply; 4+ messages in thread
From: Rafael David Tinoco @ 2021-03-22 19:36 UTC (permalink / raw)
  To: Andrii Nakryiko; +Cc: bpf, Daniel Borkmann


>> +LIBBPF_API int bpf_object__set_kversion(struct bpf_object *obj, __u32  
>> kern_version);
>
> have you run libbpf's Makefile? It should have complained about
> bpf_object__set_kversion symbol mismatches/etc. That means that this
> API needs to be listed in libbpf.map file, please add it there (to
> latest version, 0.4, and also preserve alphabetical order). Thanks.

Alright, sending a v3 with changes. I had only static builds on
my side and it didn’t run assigned linker version-script. Will
include in my tests before further submissions.




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

* Re: [PATCH v2 bpf-next] libbpf: add bpf object kern_version attribute setter
  2021-03-22 19:36   ` Rafael David Tinoco
@ 2021-03-22 19:46     ` Andrii Nakryiko
  0 siblings, 0 replies; 4+ messages in thread
From: Andrii Nakryiko @ 2021-03-22 19:46 UTC (permalink / raw)
  To: Rafael David Tinoco; +Cc: bpf, Daniel Borkmann

On Mon, Mar 22, 2021 at 12:36 PM Rafael David Tinoco
<rafaeldtinoco@ubuntu.com> wrote:
>
>
> >> +LIBBPF_API int bpf_object__set_kversion(struct bpf_object *obj, __u32
> >> kern_version);
> >
> > have you run libbpf's Makefile? It should have complained about
> > bpf_object__set_kversion symbol mismatches/etc. That means that this
> > API needs to be listed in libbpf.map file, please add it there (to
> > latest version, 0.4, and also preserve alphabetical order). Thanks.
>
> Alright, sending a v3 with changes. I had only static builds on
> my side and it didn’t run assigned linker version-script. Will
> include in my tests before further submissions.
>
>

Oh, I just noticed that you based your patch on top of Github
repository. libbpf sources actually live and are developed against
bpf-next kernel tree. Github repository is periodically synced from
kernel trees with a special script. Please do the development against
libbpf sources in the kernel tree (in tools/lib/bpf). You should also
try running selftests from tools/testing/selftests/bpf, especially
`sudo ./test_progs`. You'll need very recent Clang built from sources
to build and run everything. But at least you won't have to spend
efforts setting up your VM for testing, see vmtest.sh script in
selftests, added recently by KP Singh. It will build latest kernel and
will spin up qemu VM to run tests.

It's a bit of an upfront setup, but if you are intending to keep
contributing to libbpf and kernel, it's worth it :)

Also we have CI that would automatically test submitted patch sets
(see [0]). See also [1] for the build for your v2.

  [0] https://github.com/kernel-patches/bpf/pulls
  [1] https://travis-ci.com/github/kernel-patches/bpf/builds/220716720

>

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

end of thread, other threads:[~2021-03-22 19:47 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-20 20:28 [PATCH v2 bpf-next] libbpf: add bpf object kern_version attribute setter Rafael David Tinoco
2021-03-22 17:06 ` Andrii Nakryiko
2021-03-22 19:36   ` Rafael David Tinoco
2021-03-22 19:46     ` Andrii Nakryiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).