From nobody Mon Dec 1 22:03:44 2025 Received: from out-173.mta1.migadu.com (out-173.mta1.migadu.com [95.215.58.173]) (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 EC6992FF14F for ; Thu, 27 Nov 2025 18:53:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764269611; cv=none; b=bIQvfTs2HxkuwZ6Moa3CPGarjYnGbJzwanldN8h74jJmf877AW7DRX35dNmQvWd+e30AnpO3AYswonWQ4yiardjFtaRaSagkwltNWIhWRkn9XrqtWbbNHV4kE3Q7BR/uJASjTf2z76w/3AdaOq1oeuIph7xglwRxkHVPlu/ciII= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764269611; c=relaxed/simple; bh=UiX1KHXAYHBqfEnpkaLtaeiHRckaGHIX0/LdcZAokkg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RkJYxP52O5fxkhfQfgYmhgr/6gYrXgPIAxbkVgcw1H20ZvaYGzuTiNGnmKfcDoOliyhfiORvlHl/RgxD9ZE7v5B8++KYNofR/S/X0GgrXzOUM+NcV/uOTRjzIVmPfPpCGeS+Lt6XCFCMhNwRhVpdD9k0NQvumTiY6mwX5FXqTKM= 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=xBMutdIn; arc=none smtp.client-ip=95.215.58.173 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="xBMutdIn" 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=1764269606; 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=Nm+akLsLdjhLrTZLoo5eicKi1CYuvGgFHN9rYjxvECU=; b=xBMutdInCkFzD5kQQlokMRHPsBlZyrRxe+vUe69kzKjUqFQS6bF2zywpYkn5u72j+7DgUb +WarAEtZtG2E/T1wrm57+ibpjwfvcovb7Lm5XIuLNt03OpbUQet8havF6WlUmi9Wq0iSr1 /K4yHOQkqbH0HEOuS8cfYpnwmL2ED38= 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 , Alan Maguire , Donglin Peng Cc: bpf@vger.kernel.org, dwarves@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org Subject: [PATCH bpf-next v2 2/4] resolve_btfids: factor out load_btf() Date: Thu, 27 Nov 2025 10:52:40 -0800 Message-ID: <20251127185242.3954132-3-ihor.solodrai@linux.dev> In-Reply-To: <20251127185242.3954132-1-ihor.solodrai@linux.dev> References: <20251127185242.3954132-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 164f0c941f04..b4caae1170dd 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