From nobody Tue Dec 2 00:00:38 2025 Received: from out-181.mta0.migadu.com (out-181.mta0.migadu.com [91.218.175.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F2D662F7443 for ; Wed, 26 Nov 2025 01:29:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764120580; cv=none; b=BY3gkVFaFWJZxMrpZwbMQHgwfaJjo+wpxGcD/91YED1humrKl1eHwCsh/yDtEDPYP2ul44PLl6wUSTVkQ1tpRyWyXxmak664dEiB4NEB8GofwQ4QenexVMuYL2l4vrtwqhiAMxiYMKnFsSZlw8URlMn+efTe0QhSpU/uwn2UMf4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764120580; c=relaxed/simple; bh=aXv/pih24yggz6hes7FfJ4CF5Nhjv1blxuz2JItVklU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OJyYEnsGtjenLE+aGdu7MBVkjLEMNHIbbUIPkXauM66cD2cNqazjP3wGDTImjGSCT9O2GFy9cDpfCDuBToEanidChwkJgBwx6SfCp+mNNFUbuFzgpanDz4US3+aMI5771zkwHgGosDwQ6mh3LklQYCbs4IbObC38HSMHJJsLiUA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=SrTIFUVB; arc=none smtp.client-ip=91.218.175.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="SrTIFUVB" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1764120577; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ncfgnkJo0FOQMsiCWv0mDQ10nQdwXB48HeO+ohRsxaQ=; b=SrTIFUVBrpFAmd98XMWC17KblJ3e7muWCuc+Or/BXUEkR6JHurnYCxGgznBKd6k1Y1goFG eSINmtcq8EG3lfVrbQ9T6efABDn33zVhzp/7cKiKHRKQfULWuLxvoPCZPk6/qQ2mimxhDw cA7fnESy8kLMq7BHs5cmoEeU2T3095k= From: Ihor Solodrai To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Nathan Chancellor , Nicolas Schier , Nick Desaulniers , Bill Wendling , Justin Stitt Cc: bpf@vger.kernel.org, dwarves@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org, Alan Maguire , Donglin Peng Subject: [PATCH bpf-next v1 2/4] resolve_btfids: factor out load_btf() Date: Tue, 25 Nov 2025 17:26:54 -0800 Message-ID: <20251126012656.3546071-3-ihor.solodrai@linux.dev> In-Reply-To: <20251126012656.3546071-1-ihor.solodrai@linux.dev> References: <20251126012656.3546071-1-ihor.solodrai@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Increase the lifetime of parsed BTF in resolve_btfids by factoring load_btf() routine out of symbols_resolve() and storing the base_btf and btf pointers in the struct object. Signed-off-by: Ihor Solodrai --- tools/bpf/resolve_btfids/main.c | 47 ++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/tools/bpf/resolve_btfids/main.c b/tools/bpf/resolve_btfids/mai= n.c index d2762487ce5f..b7b44e72e765 100644 --- a/tools/bpf/resolve_btfids/main.c +++ b/tools/bpf/resolve_btfids/main.c @@ -116,6 +116,9 @@ struct object { const char *btf_path; const char *base_btf_path; =20 + struct btf *btf; + struct btf *base_btf; + struct { int fd; Elf *elf; @@ -529,16 +532,10 @@ static int symbols_collect(struct object *obj) return 0; } =20 -static int symbols_resolve(struct object *obj) +static int load_btf(struct object *obj) { - int nr_typedefs =3D obj->nr_typedefs; - int nr_structs =3D obj->nr_structs; - int nr_unions =3D obj->nr_unions; - int nr_funcs =3D obj->nr_funcs; - struct btf *base_btf =3D NULL; - int err, type_id; - struct btf *btf; - __u32 nr_types; + struct btf *base_btf =3D NULL, *btf =3D NULL; + int err; =20 if (obj->base_btf_path) { base_btf =3D btf__parse(obj->base_btf_path, NULL); @@ -546,7 +543,7 @@ static int symbols_resolve(struct object *obj) if (err) { pr_err("FAILED: load base BTF from %s: %s\n", obj->base_btf_path, strerror(-err)); - return -1; + goto out_err; } } =20 @@ -555,9 +552,30 @@ static int symbols_resolve(struct object *obj) if (err) { pr_err("FAILED: load BTF from %s: %s\n", obj->btf_path ?: obj->path, strerror(-err)); - goto out; + goto out_err; } =20 + obj->base_btf =3D base_btf; + obj->btf =3D btf; + + return 0; + +out_err: + btf__free(base_btf); + btf__free(btf); + return err; +} + +static int symbols_resolve(struct object *obj) +{ + int nr_typedefs =3D obj->nr_typedefs; + int nr_structs =3D obj->nr_structs; + int nr_unions =3D obj->nr_unions; + int nr_funcs =3D obj->nr_funcs; + struct btf *btf =3D obj->btf; + int err, type_id; + __u32 nr_types; + err =3D -1; nr_types =3D btf__type_cnt(btf); =20 @@ -615,8 +633,6 @@ static int symbols_resolve(struct object *obj) =20 err =3D 0; out: - btf__free(base_btf); - btf__free(btf); return err; } =20 @@ -824,6 +840,9 @@ int main(int argc, const char **argv) if (symbols_collect(&obj)) goto out; =20 + if (load_btf(&obj)) + goto out; + if (symbols_resolve(&obj)) goto out; =20 @@ -833,6 +852,8 @@ int main(int argc, const char **argv) if (!(fatal_warnings && warnings)) err =3D 0; out: + btf__free(obj.base_btf); + btf__free(obj.btf); if (obj.efile.elf) { elf_end(obj.efile.elf); close(obj.efile.fd); --=20 2.52.0