BPF List
 help / color / mirror / Atom feed
From: Ihor Solodrai <ihor.solodrai@linux.dev>
To: Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	Quentin Monnet <qmo@kernel.org>
Cc: bpf@vger.kernel.org, Alan Maguire <alan.maguire@oracle.com>,
	Cupertino Miranda <cupertino.miranda@oracle.com>,
	David Faust <david.faust@oss.qualcomm.com>,
	Ian Rogers <irogers@google.com>,
	"Jose E . Marchesi" <jose.marchesi@oracle.com>,
	Vineet Gupta <vineet.gupta@linux.dev>,
	Yonghong Song <yonghong.song@linux.dev>
Subject: [PATCH bpf-next v4 02/17] libbpf: Look up BTF strings without making BTF modifiable
Date: Thu,  5 Nov 2026 10:58:30 -0700	[thread overview]
Message-ID: <20261105175845.30357-3-ihor.solodrai@linux.dev> (raw)
In-Reply-To: <20261105175845.30357-1-ihor.solodrai@linux.dev>

btf__find_str() needs a hash index of the string section, and gets one
by making BTF modifiable. That copies types and strings out of the raw
data, and fails with -EOPNOTSUPP on BTF with unknown extension bytes.

btf__add_str() and split btf__dedup() look up strings in the base BTF
first. So adding a string to split BTF, or deduping it, makes the base
modifiable as a side effect, and fails if the base comes from a newer
kernel with new header fields.

Build the string index without making BTF modifiable, and reuse it if
BTF becomes modifiable later. The index can now exist on read-only
BTF, so btf__free() frees it unconditionally.

Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev>
---
 tools/lib/bpf/btf.c | 45 +++++++++++++++++++++++++++------------------
 1 file changed, 27 insertions(+), 18 deletions(-)

diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index e1ff97098ee5..5786232c1fea 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -153,9 +153,10 @@ struct btf {
 	 */
 	int start_str_off;
 
-	/* only one of strs_data or strs_set can be non-NULL, depending on
-	 * whether BTF is in a modifiable state (strs_set is used) or not
-	 * (strs_data points inside raw_data)
+	/*
+	 * strs_data points inside raw_data until BTF becomes modifiable,
+	 * then strings live in strs_set. Before that, strs_set may exist
+	 * as a lookup index for btf__find_str().
 	 */
 	void *strs_data;
 	/* a set of unique strings */
@@ -1252,15 +1253,15 @@ void btf__free(struct btf *btf)
 
 	if (btf_is_modifiable(btf)) {
 		/* if BTF was modified after loading, it will have a split
-		 * in-memory representation for types, strings and layout
-		 * sections, so we need to free all of them individually. It
-		 * might still have a cached contiguous raw data present,
-		 * which will be unconditionally freed below.
+		 * in-memory representation for types and layout sections,
+		 * so we need to free both individually. It might still have
+		 * a cached contiguous raw data present, which will be
+		 * unconditionally freed below.
 		 */
 		free(btf->types_data);
-		strset__free(btf->strs_set);
 		free(btf->layout);
 	}
+	strset__free(btf->strs_set);
 	btf_free_raw_data(btf);
 	free(btf->raw_data_swapped);
 	free(btf->type_offs);
@@ -2043,6 +2044,21 @@ static void btf_invalidate_raw_data(struct btf *btf)
 	btf->named_start_id = 0;
 }
 
+static int btf_ensure_strs_set(struct btf *btf)
+{
+	struct strset *set;
+
+	if (btf->strs_set)
+		return 0;
+
+	set = strset__new(BTF_MAX_STR_OFFSET, btf->strs_data, btf->hdr.str_len);
+	if (IS_ERR(set))
+		return PTR_ERR(set);
+
+	btf->strs_set = set;
+	return 0;
+}
+
 /* Ensure BTF is ready to be modified (by splitting into a three memory
  * regions for types, strings and layout. Also invalidate cached
  * raw_data, if any.
@@ -2050,7 +2066,6 @@ static void btf_invalidate_raw_data(struct btf *btf)
 static int btf_ensure_modifiable(struct btf *btf)
 {
 	void *types, *layout = NULL;
-	struct strset *set = NULL;
 	int err = -ENOMEM;
 
 	if (btf_is_modifiable(btf)) {
@@ -2078,18 +2093,14 @@ static int btf_ensure_modifiable(struct btf *btf)
 		       btf->hdr.layout_len);
 	}
 
-	/* build lookup index for all strings */
-	set = strset__new(BTF_MAX_STR_OFFSET, btf->strs_data, btf->hdr.str_len);
-	if (IS_ERR(set)) {
-		err = PTR_ERR(set);
+	err = btf_ensure_strs_set(btf);
+	if (err)
 		goto err_out;
-	}
 
 	/* only when everything was successful, update internal state */
 	btf->types_data = types;
 	btf->types_data_cap = btf->hdr.type_len;
 	btf->strs_data = NULL;
-	btf->strs_set = set;
 	if (layout)
 		btf->layout = layout;
 	/* if BTF was created from scratch, all strings are guaranteed to be
@@ -2108,7 +2119,6 @@ static int btf_ensure_modifiable(struct btf *btf)
 	return 0;
 
 err_out:
-	strset__free(set);
 	free(types);
 	free(layout);
 	return err;
@@ -2131,8 +2141,7 @@ int btf__find_str(struct btf *btf, const char *s)
 			return off;
 	}
 
-	/* BTF needs to be in a modifiable state to build string lookup index */
-	err = btf_ensure_modifiable(btf);
+	err = btf_ensure_strs_set(btf);
 	if (err)
 		return libbpf_err(err);
 
-- 
2.56.0


  parent reply	other threads:[~2026-11-05 17:59 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-05 17:58 [PATCH bpf-next v4 00/17] bpftool: Per-record CO-RE attributes in the C dump Ihor Solodrai
2026-11-05 17:58 ` [PATCH bpf-next v4 01/17] Revert "libbpf: Render the arena type tag with its address space" Ihor Solodrai
2026-11-05 17:58 ` Ihor Solodrai [this message]
2026-11-05 18:41   ` [PATCH bpf-next v4 02/17] libbpf: Look up BTF strings without making BTF modifiable bot+bpf-ci
2026-11-05 17:58 ` [PATCH bpf-next v4 03/17] bpftool: Dump C through a split BTF Ihor Solodrai
2026-11-05 17:58 ` [PATCH bpf-next v4 04/17] selftests/bpf: Build BTF fixtures in btf_to_tmpfile() Ihor Solodrai
2026-11-05 17:58 ` [PATCH bpf-next v4 05/17] selftests/bpf: Cover unknown BTF header-extension bytes in bpftool C dumps Ihor Solodrai
2026-11-05 17:58 ` [PATCH bpf-next v4 06/17] libbpf: Let decl tags override how btf_dump renders type tags Ihor Solodrai
2026-11-06  0:24   ` Andrii Nakryiko
2026-11-05 17:58 ` [PATCH bpf-next v4 07/17] libbpf, bpftool, resolve_btfids: Dump kflagged BTF tags as is Ihor Solodrai
2026-11-05 18:16   ` sashiko-bot
2026-11-06  0:25   ` Andrii Nakryiko
2026-11-05 17:58 ` [PATCH bpf-next v4 08/17] selftests/bpf: Cover type tag rendering overrides in btf_dump Ihor Solodrai
2026-11-05 17:58 ` [PATCH bpf-next v4 09/17] selftests/bpf: Cover legacy arena type tags in bpftool C dumps Ihor Solodrai
2026-11-05 17:58 ` [PATCH bpf-next v4 10/17] bpftool: Apply __bpf_relocatable per record on BTF dump Ihor Solodrai
2026-11-05 18:41   ` bot+bpf-ci
2026-11-05 17:58 ` [PATCH bpf-next v4 11/17] bpftool: Skip __bpf_relocatable on verifier-rewritten context types Ihor Solodrai
2026-11-05 17:58 ` [PATCH bpf-next v4 12/17] bpftool: Emit __bpf_static_offset " Ihor Solodrai
2026-11-05 17:58 ` [PATCH bpf-next v4 13/17] selftests/bpf: Cover record attributes in bpftool BTF dumps Ihor Solodrai
2026-11-05 17:58 ` [PATCH bpf-next v4 14/17] selftests/bpf: Cover context and socket accesses through vmlinux.h Ihor Solodrai
2026-11-05 17:58 ` [PATCH bpf-next v4 15/17] bpftool: Document __bpf_relocatable and __bpf_static_offset Ihor Solodrai
2026-11-05 17:58 ` [PATCH bpf-next v4 16/17] bpftool: Render the arena type tag as __bpf_arena Ihor Solodrai
2026-11-05 17:58 ` [PATCH bpf-next v4 17/17] selftests/bpf: Cover the arena type tag in bpftool C dumps Ihor Solodrai
2026-11-06  0:30 ` [PATCH bpf-next v4 00/17] bpftool: Per-record CO-RE attributes in the C dump patchwork-bot+netdevbpf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261105175845.30357-3-ihor.solodrai@linux.dev \
    --to=ihor.solodrai@linux.dev \
    --cc=alan.maguire@oracle.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=cupertino.miranda@oracle.com \
    --cc=daniel@iogearbox.net \
    --cc=david.faust@oss.qualcomm.com \
    --cc=eddyz87@gmail.com \
    --cc=irogers@google.com \
    --cc=jose.marchesi@oracle.com \
    --cc=memxor@gmail.com \
    --cc=qmo@kernel.org \
    --cc=vineet.gupta@linux.dev \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox