From 62b1c8836735463e48cd5aed4d4e5f35208bf9ac Mon Sep 17 00:00:00 2001 From: Motomu Utsumi Date: Fri, 21 Mar 2025 15:13:00 +0900 Subject: [PATCH 1/2] Fix an issue caused by variable re-declaration in inner scope Since btf is declared in inner scope, btf is null in outer scope and BTF was always disabled. This CL also makes NetBpfLoad buildable with -Wshadow flag Test: adb log | grep NetBpfLoad Change-Id: I511f21a3620ccfdadd797786e087d0d142c816ed --- bpf/loader/NetBpfLoad.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bpf/loader/NetBpfLoad.cpp b/bpf/loader/NetBpfLoad.cpp index 4c47f83bbf..dc0a548878 100644 --- a/bpf/loader/NetBpfLoad.cpp +++ b/bpf/loader/NetBpfLoad.cpp @@ -556,9 +556,9 @@ static int readCodeSections(ifstream& elfFile, vector& cs) { vector csSymNames; ret = getSectionSymNames(elfFile, oldName, csSymNames, STT_FUNC); if (ret || !csSymNames.size()) return ret; - for (size_t i = 0; i < progDefNames.size(); ++i) { - if (!progDefNames[i].compare(csSymNames[0] + "_def")) { - cs_temp.prog_def = pd[i]; + for (size_t j = 0; j < progDefNames.size(); ++j) { + if (!progDefNames[j].compare(csSymNames[0] + "_def")) { + cs_temp.prog_def = pd[j]; break; } } @@ -865,7 +865,7 @@ static int createMaps(const char* elfPath, ifstream& elfFile, vector& ALOGE("Failed to read .BTF section, ret:%d", ret); return ret; } - struct btf *btf = btf__new(btfData.data(), btfData.size()); + btf = btf__new(btfData.data(), btfData.size()); if (btf == NULL) { ALOGE("btf__new failed, errno: %d", errno); return -errno; From 861f11c9db15c0f5001110ac87fb594002f61b01 Mon Sep 17 00:00:00 2001 From: Motomu Utsumi Date: Fri, 21 Mar 2025 15:29:00 +0900 Subject: [PATCH 2/2] Skip enabling BTF on kernel older than 5.10 BTF_KIND_VAR and BTF_KIND_DATASEC were introduced in 5.2. It should be possible to enable BTF on 4.18 or above since libbpf can load BTF. Also, current code should work on 5.4 but this is untested. For now, this CL enables BTF on 5.10 or above. Test: boot on 4.9, 4.19, 5.10 Test: adb logcat | grep NetBpfLoad Change-Id: I3da4970464afcf9a76b1f2645ed79c44750bcd6a --- bpf/loader/NetBpfLoad.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bpf/loader/NetBpfLoad.cpp b/bpf/loader/NetBpfLoad.cpp index dc0a548878..63de1a6f53 100644 --- a/bpf/loader/NetBpfLoad.cpp +++ b/bpf/loader/NetBpfLoad.cpp @@ -858,8 +858,10 @@ static int createMaps(const char* elfPath, ifstream& elfFile, vector& struct btf *btf = NULL; auto scopeGuard = base::make_scope_guard([btf] { if (btf) btf__free(btf); }); - if (isAtLeastKernelVersion(4, 18, 0)) { + if (isAtLeastKernelVersion(5, 10, 0)) { + // Untested on Linux Kernel 5.4, but likely compatible. // On Linux Kernels older than 4.18 BPF_BTF_LOAD command doesn't exist. + // On Linux Kernels older than 5.2 BTF_KIND_VAR and BTF_KIND_DATASEC don't exist. ret = readSectionByName(".BTF", elfFile, btfData); if (ret) { ALOGE("Failed to read .BTF section, ret:%d", ret);