Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libbpf: Switch kernel version lookup to a more reliable method. #421

Closed
usamasaqib opened this issue Dec 14, 2021 · 2 comments
Closed

libbpf: Switch kernel version lookup to a more reliable method. #421

usamasaqib opened this issue Dec 14, 2021 · 2 comments
Labels
improvement Improvements to existing functionality

Comments

@usamasaqib
Copy link

usamasaqib commented Dec 14, 2021

On older kernel versions, the kernel checks whether the version number provided in the bpf_attr object, matches the LINUX_VERSION_CODE (https://elixir.bootlin.com/linux/v4.19/source/kernel/bpf/syscall.c#L1360). If not, an error code is returned and the loading fails.
I have an encountered issues where the kernel version number reported by the uname syscall does not match the LINUX_VERSION_CODE.
Following is the example output of uname syscall on system where the LINUX_VERSION_CODE is 4.19.37

sysname: Linux
nodename: pardus
release: 4.19.0-5-amd64
version: #1 SMP Debian 4.19.37-5+deb10u1 (2019-07-19)
machine: x86_64

Since libbpf uses the uname syscall to probe the kernel version, it fails to load BPF programs on such environments.

Bpftrace attempts to solve this issue by parsing the kernel version code from the PT_NOTE section of the vdso file mapped into the virtual memory of every program. This seems to contain the correct kernel version number matching LINUX_VERSION_CODE.
https://github.com/iovisor/bpftrace/blob/master/src/utils.cpp#L886
For architectures where this mapping is not present, it defaults to the uname syscall.

Maybe a similar approach can be replicated in libbpf.

@anakryiko
Copy link
Member

perf seems to be using a different approach, it's parsing /proc/version_signature, see https://github.com/torvalds/linux/blob/master/tools/perf/util/util.c#L347

I wonder which one is more reliable. I don't have an Ubuntu system to test something like this, can you please post the contents of /proc/version_signature here?

@usamasaqib
Copy link
Author

Output of uname -a is:

Linux usama 5.4.0-91-generic #102~18.04.1-Ubuntu SMP Thu Nov 11 14:46:36 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

Output of cat /proc/version_signature is:

Ubuntu 5.4.0-91.102~18.04.1-generic 5.4.151

Kernel version obtained from parsing the vdso section:

release: 5.4.151

@anakryiko anakryiko added the improvement Improvements to existing functionality label Dec 18, 2021
fengguang pushed a commit to 0day-ci/linux that referenced this issue Dec 22, 2021
Ubuntu reports incorrect kernel version through uname(), which on older
kernels leads to kprobe BPF programs failing to load due to the version
check mismatch.

Accommodate Ubuntu's quirks with LINUX_VERSION_CODE by using
Ubuntu-specific /proc/version_code to fetch major/minor/patch versions
to form LINUX_VERSION_CODE.

While at it, consolide libbpf's kernel version detection code between
libbpf.c and libbpf_probes.c.

  [0] Closes: libbpf/libbpf#421

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
kernel-patches-bot pushed a commit to kernel-patches/bpf that referenced this issue Dec 22, 2021
Ubuntu reports incorrect kernel version through uname(), which on older
kernels leads to kprobe BPF programs failing to load due to the version
check mismatch.

Accommodate Ubuntu's quirks with LINUX_VERSION_CODE by using
Ubuntu-specific /proc/version_code to fetch major/minor/patch versions
to form LINUX_VERSION_CODE.

While at it, consolide libbpf's kernel version detection code between
libbpf.c and libbpf_probes.c.

  [0] Closes: libbpf/libbpf#421

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
tsipa pushed a commit to tsipa/bpf-next that referenced this issue Dec 22, 2021
Ubuntu reports incorrect kernel version through uname(), which on older
kernels leads to kprobe BPF programs failing to load due to the version
check mismatch.

Accommodate Ubuntu's quirks with LINUX_VERSION_CODE by using
Ubuntu-specific /proc/version_code to fetch major/minor/patch versions
to form LINUX_VERSION_CODE.

While at it, consolide libbpf's kernel version detection code between
libbpf.c and libbpf_probes.c.

  [0] Closes: libbpf/libbpf#421

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
tsipa pushed a commit to tsipa/bpf-next that referenced this issue Dec 23, 2021
Ubuntu reports incorrect kernel version through uname(), which on older
kernels leads to kprobe BPF programs failing to load due to the version
check mismatch.

Accommodate Ubuntu's quirks with LINUX_VERSION_CODE by using
Ubuntu-specific /proc/version_code to fetch major/minor/patch versions
to form LINUX_VERSION_CODE.

While at it, consolide libbpf's kernel version detection code between
libbpf.c and libbpf_probes.c.

  [0] Closes: libbpf/libbpf#421

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
kernel-patches-bot pushed a commit to kernel-patches/bpf that referenced this issue Dec 23, 2021
Ubuntu reports incorrect kernel version through uname(), which on older
kernels leads to kprobe BPF programs failing to load due to the version
check mismatch.

Accommodate Ubuntu's quirks with LINUX_VERSION_CODE by using
Ubuntu-specific /proc/version_code to fetch major/minor/patch versions
to form LINUX_VERSION_CODE.

While at it, consolide libbpf's kernel version detection code between
libbpf.c and libbpf_probes.c.

  [0] Closes: libbpf/libbpf#421

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
kernel-patches-bot pushed a commit to kernel-patches/bpf that referenced this issue Dec 29, 2021
Ubuntu reports incorrect kernel version through uname(), which on older
kernels leads to kprobe BPF programs failing to load due to the version
check mismatch.

Accommodate Ubuntu's quirks with LINUX_VERSION_CODE by using
Ubuntu-specific /proc/version_code to fetch major/minor/patch versions
to form LINUX_VERSION_CODE.

While at it, consolide libbpf's kernel version detection code between
libbpf.c and libbpf_probes.c.

  [0] Closes: libbpf/libbpf#421

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
tsipa pushed a commit to tsipa/bpf-next that referenced this issue Dec 29, 2021
Ubuntu reports incorrect kernel version through uname(), which on older
kernels leads to kprobe BPF programs failing to load due to the version
check mismatch.

Accommodate Ubuntu's quirks with LINUX_VERSION_CODE by using
Ubuntu-specific /proc/version_code to fetch major/minor/patch versions
to form LINUX_VERSION_CODE.

While at it, consolide libbpf's kernel version detection code between
libbpf.c and libbpf_probes.c.

  [0] Closes: libbpf/libbpf#421

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
kernel-patches-bot pushed a commit to kernel-patches/bpf that referenced this issue Dec 29, 2021
Ubuntu reports incorrect kernel version through uname(), which on older
kernels leads to kprobe BPF programs failing to load due to the version
check mismatch.

Accommodate Ubuntu's quirks with LINUX_VERSION_CODE by using
Ubuntu-specific /proc/version_code to fetch major/minor/patch versions
to form LINUX_VERSION_CODE.

While at it, consolide libbpf's kernel version detection code between
libbpf.c and libbpf_probes.c.

  [0] Closes: libbpf/libbpf#421

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20211222231003.2334940-1-andrii@kernel.org
anakryiko added a commit to anakryiko/libbpf that referenced this issue Jan 6, 2022
Ubuntu reports incorrect kernel version through uname(), which on older
kernels leads to kprobe BPF programs failing to load due to the version
check mismatch.

Accommodate Ubuntu's quirks with LINUX_VERSION_CODE by using
Ubuntu-specific /proc/version_code to fetch major/minor/patch versions
to form LINUX_VERSION_CODE.

While at it, consolide libbpf's kernel version detection code between
libbpf.c and libbpf_probes.c.

  [0] Closes: libbpf#421

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20211222231003.2334940-1-andrii@kernel.org
bella485 pushed a commit to bella485/centos-stream-9 that referenced this issue May 1, 2024
Bugzilla: https://bugzilla.redhat.com/2069046

Upstream Status: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

commit 5b3d72987701d51bf31823b39db49d10970f5c2d
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Dec 22 15:10:03 2021 -0800

    libbpf: Improve LINUX_VERSION_CODE detection

    Ubuntu reports incorrect kernel version through uname(), which on older
    kernels leads to kprobe BPF programs failing to load due to the version
    check mismatch.

    Accommodate Ubuntu's quirks with LINUX_VERSION_CODE by using
    Ubuntu-specific /proc/version_code to fetch major/minor/patch versions
    to form LINUX_VERSION_CODE.

    While at it, consolide libbpf's kernel version detection code between
    libbpf.c and libbpf_probes.c.

      [0] Closes: libbpf/libbpf#421

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Acked-by: Yonghong Song <yhs@fb.com>
    Link: https://lore.kernel.org/bpf/20211222231003.2334940-1-andrii@kernel.org

Signed-off-by: Artem Savkov <asavkov@redhat.com>
bella485 pushed a commit to bella485/centos-stream-9 that referenced this issue May 1, 2024
Bugzilla: https://bugzilla.redhat.com/2069046

Upstream Status: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

commit 5b3d72987701d51bf31823b39db49d10970f5c2d
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Dec 22 15:10:03 2021 -0800

    libbpf: Improve LINUX_VERSION_CODE detection

    Ubuntu reports incorrect kernel version through uname(), which on older
    kernels leads to kprobe BPF programs failing to load due to the version
    check mismatch.

    Accommodate Ubuntu's quirks with LINUX_VERSION_CODE by using
    Ubuntu-specific /proc/version_code to fetch major/minor/patch versions
    to form LINUX_VERSION_CODE.

    While at it, consolide libbpf's kernel version detection code between
    libbpf.c and libbpf_probes.c.

      [0] Closes: libbpf/libbpf#421

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Acked-by: Yonghong Song <yhs@fb.com>
    Link: https://lore.kernel.org/bpf/20211222231003.2334940-1-andrii@kernel.org

Signed-off-by: Artem Savkov <asavkov@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement Improvements to existing functionality
Projects
None yet
Development

No branches or pull requests

2 participants