Skip to contentSkip to conversation

libbpf: add bpf object kern_version attribute setter

PATCH Superseded Rafael David Tinoco 2 participants 4 messages Mar 20, 2021 BPF

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libbpf: add bpf object kern_version attribute setter

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

---
 src/libbpf.c | 15 +++++++++++++++
 src/libbpf.h |  1 +
 2 files changed, 16 insertions(+)
src/libbpf.c +15 -0
index 0c4a386..7b52cd6 100644
@@ -8278,6 +8278,21 @@ 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, char *kern_version)
+{
+	__u32 major, minor, patch;
+
+	if (!kern_version) {
+		obj->kern_version = 0;
+		return 0;
+	}
+	if (sscanf(kern_version, "%u.%u.%u", &major, &minor, &patch) != 3)
+		return -1;
+	obj->kern_version = KERNEL_VERSION(major, minor, patch);
+
+	return 0;
+}
+
 int bpf_object__set_priv(struct bpf_object *obj, void *priv,
 			 bpf_object_clear_priv_t clear_priv)
 {
src/libbpf.h +1 -0
index 3c35eb4..3e14ae7 100644
@@ -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, char *kern_version);
 
 struct btf;
 LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj);
Signature · 1 line
2.27.0
#1 lore raw
Quoted 22 lines · Rafael David Tinoco

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

---
 src/libbpf.c | 15 +++++++++++++++
 src/libbpf.h |  1 +
 2 files changed, 16 insertions(+)
src/libbpf.c +0 -0
index 0c4a386..7b52cd6 100644
@@ -8278,6 +8278,21 @@ int bpf_object__btf_fd(const struct bpf_object *obj)
        return obj->btf ? btf__fd(obj->btf) : -1;
 }

src/libbpf.c int bpf_object__btf_fd(const struct bpf_object *obj) · line 8289

Quoted 10 lines
src/libbpf.c +9 -0
 
+int bpf_object__set_kversion(struct bpf_object *obj, char *kern_version)
+{
+       __u32 major, minor, patch;
+
+       if (!kern_version) {
+               obj->kern_version = 0;
+               return 0;
+       }
+       if (sscanf(kern_version, "%u.%u.%u", &major, &minor, &patch) != 3)

given SEC("version") expects int and bpf_object__kversion() returns int, I think it's appropriate for bpf_object__set_kversion() to accept just opaque int as well. Please also check that obj is not loaded and return error if it is. Thanks!

Quoted 24 lines
src/libbpf.c +6 -0
+               return -1;
+       obj->kern_version = KERNEL_VERSION(major, minor, patch);
+
+       return 0;
+}
+
 int bpf_object__set_priv(struct bpf_object *obj, void *priv,
                         bpf_object_clear_priv_t clear_priv)
 {
src/libbpf.h +1 -1
index 3c35eb4..3e14ae7 100644
@@ -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, char *kern_version);
 
 struct btf;
 LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj);
--

2.27.0

#2 lore raw
Quoted 26 lines · Andrii Nakryiko
Quoted 23 lines · Rafael David Tinoco

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

---
 src/libbpf.c | 15 +++++++++++++++
 src/libbpf.h |  1 +
 2 files changed, 16 insertions(+)
src/libbpf.c +0 -0
index 0c4a386..7b52cd6 100644
@@ -8278,6 +8278,21 @@ int bpf_object__btf_fd(const struct bpf_object *obj)
        return obj->btf ? btf__fd(obj->btf) : -1;
 }
 
Quoted 15 lines

src/libbpf.c int bpf_object__btf_fd(const struct bpf_object *obj) · line 8289

Quoted 9 lines
src/libbpf.c +9 -0
+int bpf_object__set_kversion(struct bpf_object *obj, char *kern_version)
+{
+       __u32 major, minor, patch;
+
+       if (!kern_version) {
+               obj->kern_version = 0;
+               return 0;
+       }
+       if (sscanf(kern_version, "%u.%u.%u", &major, &minor, &patch) != 3)

given SEC("version") expects int and bpf_object__kversion() returns int, I think it's appropriate for bpf_object__set_kversion() to accept just opaque int as well. Please also check that obj is not loaded and return error if it is. Thanks!

Oh, and please use [PATCH bpf-next] subject prefix to specify that this is destined to the bpf-next tree. You'll also add v2 in between PATCH and bpf-next for the next version, of course.

Quoted 24 lines
Quoted 24 lines
src/libbpf.c +6 -0
+               return -1;
+       obj->kern_version = KERNEL_VERSION(major, minor, patch);
+
+       return 0;
+}
+
 int bpf_object__set_priv(struct bpf_object *obj, void *priv,
                         bpf_object_clear_priv_t clear_priv)
 {
src/libbpf.h +1 -1
index 3c35eb4..3e14ae7 100644
@@ -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, char *kern_version);
 
 struct btf;
 LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj);
--

2.27.0

#3 lore raw
Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
src/libbpf.c +7 -0
+int bpf_object__set_kversion(struct bpf_object *obj, char *kern_version)
+{
+       __u32 major, minor, patch;
+
+       if (!kern_version) {
+               obj->kern_version = 0;
+               return 0;
Quoted 8 lines
Quoted 3 lines
(quoted diff) +2 -0
+       }
+       if (sscanf(kern_version, "%u.%u.%u", &major, &minor, &patch) !=  

3)

given SEC("version") expects int and bpf_object__kversion() returns int, I think it's appropriate for bpf_object__set_kversion() to accept just opaque int as well. Please also check that obj is not loaded and return error if it is. Thanks!

Quoted 3 lines

Oh, and please use [PATCH bpf-next] subject prefix to specify that this is destined to the bpf-next tree. You'll also add v2 in between PATCH and bpf-next for the next version, of course.

will do, and sorry for the initial burden, getting used to libbpf devel.