From 6f1ceccf1443005248bec27bb24003124213a350 Mon Sep 17 00:00:00 2001 From: Motomu Utsumi Date: Wed, 19 Mar 2025 19:49:08 +0900 Subject: [PATCH] Skip enabling BTF on kernel older than 4.18 BPF_BTF_LOAD command was introduced in 4.18 Bug: 403381101 Test: boot on 4.9 kernel Change-Id: Ibf2070750334417bd1052486c1f59a5d3f7fc20c --- bpf/loader/NetBpfLoad.cpp | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/bpf/loader/NetBpfLoad.cpp b/bpf/loader/NetBpfLoad.cpp index f11b9a372b..d70a2c8e04 100644 --- a/bpf/loader/NetBpfLoad.cpp +++ b/bpf/loader/NetBpfLoad.cpp @@ -856,20 +856,24 @@ static int createMaps(const char* elfPath, ifstream& elfFile, vector& ret = getSectionSymNames(elfFile, "maps", mapNames); if (ret) return ret; - ret = readSectionByName(".BTF", elfFile, btfData); - if (ret) { - ALOGE("Failed to read .BTF section, ret:%d", ret); - return ret; - } - struct btf *btf = btf__new(btfData.data(), btfData.size()); - if (btf == NULL) { - ALOGE("btf__new failed, errno: %d", errno); - return -errno; - } - auto scopeGuard = base::make_scope_guard([btf] { btf__free(btf); }); + struct btf *btf = NULL; + auto scopeGuard = base::make_scope_guard([btf] { if (btf) btf__free(btf); }); + if (isAtLeastKernelVersion(4, 18, 0)) { + // On Linux Kernels older than 4.18 BPF_BTF_LOAD command doesn't exist. + ret = readSectionByName(".BTF", elfFile, btfData); + if (ret) { + ALOGE("Failed to read .BTF section, ret:%d", ret); + return ret; + } + struct btf *btf = btf__new(btfData.data(), btfData.size()); + if (btf == NULL) { + ALOGE("btf__new failed, errno: %d", errno); + return -errno; + } - ret = loadBtf(elfFile, btf); - if (ret) return ret; + ret = loadBtf(elfFile, btf); + if (ret) return ret; + } unsigned kvers = kernelVersion(); @@ -997,7 +1001,7 @@ static int createMaps(const char* elfPath, ifstream& elfFile, vector& if (isAtLeastKernelVersion(4, 15, 0)) strlcpy(req.map_name, mapNames[i].c_str(), sizeof(req.map_name)); - bool haveBtf = isBtfSupported(type); + bool haveBtf = btf && isBtfSupported(type); if (haveBtf) { uint32_t kTid, vTid; ret = getKeyValueTids(btf, mapNames[i].c_str(), md[i].key_size,