[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <20230414084353.36545-1-songrui.771@bytedance.com>
Date: Fri, 14 Apr 2023 16:43:53 +0800
From: "songrui.771" <songrui.771@...edance.com>
To: Andrii Nakryiko <andrii@...nel.org>,
Alexei Starovoitov <ast@...nel.org>,
Daniel Borkmann <daniel@...earbox.net>
Cc: bpf@...r.kernel.org, linux-kernel@...r.kernel.org,
"songrui.771" <songrui.771@...edance.com>
Subject: [PATCH v2] libbpf: correct the macro KERNEL_VERSION for old kernel
The introduced header file linux/version.h in libbpf_probes.c may have a wrong macro KERNEL_VERSION for calculating LINUX_VERSION_CODE in some old kernel (Debian9,10). Below is a version info example from Debian 10.
release: 4.19.0-22-amd64
version: #1 SMP Debian 4.19.260-1 (2022-09-29)
The macro KERNEL_VERSION is defined to (((a) << 16) + ((b) << 8)) + (c)), which a, b, and c stand for major, minor and patch version. So in example here, the major is 4, minor is 19, patch is 260, the LINUX_VERSION(4, 19, 260) which is 267268 should be matched to LINUX_VERSION_CODE. However, the KERNEL_VERSION_CODE in linux/version.h is defined to 267263.
I noticed that the macro KERNEL_VERSION in linux/version.h of some new kernel is defined to (((a) << 16) + ((b) << 8) + ((c) > 255 ? 255 : (c))). And KERNEL_VERSION(4, 19, 260) is equal to 267263 which is the right LINUX_VERSION_CODE.
The mismatched LINUX_VERSION_CODE which will cause failing to load kprobe BPF programs in the version check of BPF syscall.
The return value of get_kernel_version in libbpf_probes.c should be matched to LINUX_VERSION_CODE by correcting the macro KERNEL_VERSION.
Signed-off-by: songrui.771 <songrui.771@...edance.com>
---
Changes since v1:
- reintroduce header file linux/version.h
- define a new macro LIBBPF_KERNEL_VERSION to get kernel version rather than KERNEL_VERSION
---
tools/lib/bpf/libbpf_probes.c | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/tools/lib/bpf/libbpf_probes.c b/tools/lib/bpf/libbpf_probes.c
index 4f3bc968ff8e..5b22a880c7e7 100644
--- a/tools/lib/bpf/libbpf_probes.c
+++ b/tools/lib/bpf/libbpf_probes.c
@@ -18,6 +18,10 @@
#include "libbpf.h"
#include "libbpf_internal.h"
+#ifndef LIBBPF_KERNEL_VERSION
+#define LIBBPF_KERNEL_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + ((c) > 255 ? 255 : (c)))
+#endif
+
/* On Ubuntu LINUX_VERSION_CODE doesn't correspond to info.release,
* but Ubuntu provides /proc/version_signature file, as described at
* https://ubuntu.com/kernel, with an example contents below, which we
@@ -47,7 +51,7 @@ static __u32 get_ubuntu_kernel_version(void)
if (ret != 3)
return 0;
- return KERNEL_VERSION(major, minor, patch);
+ return LIBBPF_KERNEL_VERSION(major, minor, patch);
}
/* On Debian LINUX_VERSION_CODE doesn't correspond to info.release.
@@ -74,7 +78,7 @@ static __u32 get_debian_kernel_version(struct utsname *info)
if (sscanf(p, "Debian %u.%u.%u", &major, &minor, &patch) != 3)
return 0;
- return KERNEL_VERSION(major, minor, patch);
+ return LIBBPF_KERNEL_VERSION(major, minor, patch);
}
__u32 get_kernel_version(void)
@@ -97,7 +101,7 @@ __u32 get_kernel_version(void)
if (sscanf(info.release, "%u.%u.%u", &major, &minor, &patch) != 3)
return 0;
- return KERNEL_VERSION(major, minor, patch);
+ return LIBBPF_KERNEL_VERSION(major, minor, patch);
}
static int probe_prog_load(enum bpf_prog_type prog_type,
--
2.39.2 (Apple Git-143)
Powered by blists - more mailing lists