BPF List
 help / color / mirror / Atom feed
From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: "Anton Protopopov" <a.s.protopopov@gmail.com>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"Andrii Nakryiko" <andrii@kernel.org>,
	"Eduard Zingerman" <eddyz87@gmail.com>,
	"Kumar Kartikeya Dwivedi" <memxor@gmail.com>,
	"Jiri Olsa" <jolsa@kernel.org>,
	"Alan Maguire" <almagui@kernel.org>,
	"John Fastabend" <john.fastabend@gmail.com>,
	<bpf@vger.kernel.org>
Subject: Re: [PATCH v1 bpf-next 4/5] resolve_btfids: Exclude the code of kernel/bpf and of the JIT
Date: Thu, 26 Nov 2026 00:53:59 +0000	[thread overview]
Message-ID: <DN4QDHMFA99A.27B7GAJLQQ7DK@gmail.com> (raw)
In-Reply-To: <20261125212849.868523-5-a.s.protopopov@gmail.com>

On Wed, Nov 25, 2026 at 01:28 PM Anton Protopopov <a.s.protopopov@gmail.com> wrote:
> bpf_tramp_ftrace_ops_func() and bpf_arch_text_invalidate(). Read the
> symbols of kernel/bpf/built-in.a and arch/<arch>/net/built-in.a with
> the llvm-nm of llvm-config, which reads ELF objects and LTO bitcode,
> and do not tag these functions.

bot is correct.
Let's run ${NM} in gen-btf.sh and give its output to resolve_btfids ?
kbuild knows the paths and the arch. The tool doesn't have to.
${NM} reads what ${CC} writes. HAS_LTO_CLANG requires it to be llvm-nm.
link-vmlinux.sh runs it for kallsyms already.
llvm-config can be of another LLVM than the compiler. llvm-mc.sh says so.
image_func() finds a function by name. No need to copy and sort the names.
Then LLVM_NM, the nm mode of llvm-mc.sh, bpf_archives[], popen(), qsort()
and bsearch() go away.

It replaces patch 4.

diff --git a/scripts/gen-btf.sh b/scripts/gen-btf.sh
index b17f118..341885b 100755
--- a/scripts/gen-btf.sh
+++ b/scripts/gen-btf.sh
@@ -75,8 +75,17 @@ gen_btf_data()
 		--btf_encode_detached=${btf1}		\
 		"${ELF_FILE}"

+	# The code of BPF itself is not a target of fmod_ret
+	fmodret_deny=
+	if [ -z "${BTF_BASE}" ] && is_enabled CONFIG_BPF_FMODRET_SCAN; then
+		fmodret_deny="${ELF_FILE}.fmodret_deny"
+		${NM} --defined-only kernel/bpf/built-in.a	\
+			arch/${SRCARCH}/net/built-in.a > ${fmodret_deny}
+	fi
+
 	${RESOLVE_BTFIDS} ${RESOLVE_BTFIDS_FLAGS}	\
 		${BTF_BASE:+--btf_base ${BTF_BASE}}	\
+		${fmodret_deny:+--fmodret_deny ${fmodret_deny}}	\
 		${inline}				\
 		--btf ${btf1} "${ELF_FILE}"
 }
@@ -148,6 +157,7 @@ cleanup()
 	rm -f "${ELF_FILE}.BTF"
 	rm -f "${ELF_FILE}.BTF.inline"
 	rm -f "${ELF_FILE}.BTF.inline.link"
+	rm -f "${ELF_FILE}.fmodret_deny"
 	if [ "${BTFGEN_MODE}" = "module" ]; then
 		rm -f "${ELF_FILE}.BTF.base"
 		rm -f "${ELF_FILE}.BTF_ids"
diff --git a/tools/bpf/resolve_btfids/fmodret.c b/tools/bpf/resolve_btfids/fmodret.c
index e1afdb6..d2fa9b7 100644
--- a/tools/bpf/resolve_btfids/fmodret.c
+++ b/tools/bpf/resolve_btfids/fmodret.c
@@ -188,12 +188,41 @@ static void set_protos(struct scan *scan, const struct image *img, const struct
 	}
 }

-int fmodret_tag(Elf *elf, struct btf *btf, int verbose)
+/*
+ * Mark the functions of IMG which are in PATH, the output of nm. The line of a
+ * symbol is "<address> <type> <name>". The other lines have fewer words.
+ */
+static bool *read_denied(const struct image *img, const char *path)
+{
+	bool *denied = calloc(img->nr_funcs, sizeof(*denied));
+	FILE *f = fopen(path, "r");
+	char line[1024], name[1024];
+
+	if (!denied || !f) {
+		fprintf(stderr, "FAILED: --fmodret_deny: cannot read %s\n", path);
+		free(denied);
+		denied = NULL;
+	}
+	while (denied && fgets(line, sizeof(line), f)) {
+		const struct image_func *func = NULL;
+
+		if (sscanf(line, "%*s %*s %1023s", name) == 1)
+			func = image_func(img, name);
+		if (func)
+			denied[func - img->funcs] = true;
+	}
+	if (f)
+		fclose(f);
+	return denied;
+}
+
+int fmodret_tag(Elf *elf, struct btf *btf, const char *deny_path, int verbose)
 {
 	size_t nr_funcs = 0, nr_scanned = 0, nr_undecodable = 0, nr_tags = 0;
 	const struct btf *base = btf__base_btf(btf);
 	__u32 id, nr_types = btf__type_cnt(btf);
 	struct scan *scan = NULL;
+	bool *denied = NULL;
 	struct image img;
 	char counts[200];
 	int err;
@@ -206,6 +235,11 @@ int fmodret_tag(Elf *elf, struct btf *btf, int verbose)
 	if (!scan)
 		goto out;
 	set_protos(scan, &img, btf);
+	if (deny_path) {
+		denied = read_denied(&img, deny_path);
+		if (!denied)
+			goto out;
+	}

 	for (id = base ? btf__type_cnt(base) : 1, err = 0; id < nr_types && err >= 0; id++) {
 		const struct btf_type *t = btf__type_by_id(btf, id);
@@ -225,6 +259,8 @@ int fmodret_tag(Elf *elf, struct btf *btf, int verbose)
 		func = image_func(&img, name);
 		if (!func || !strcmp(func->section, ".init.text"))
 			continue;
+		if (denied && denied[func - img.funcs])
+			continue;
 		nr_scanned++;
 		err = scan_func(scan, func, &values);
 		if (err == -EINVAL) {
@@ -257,6 +293,7 @@ int fmodret_tag(Elf *elf, struct btf *btf, int verbose)
 		printf("  FMODRET %s\n", counts);
 	}
 out:
+	free(denied);
 	scan_close(scan);
 	image_close(&img);
 	return err < 0 ? err : 0;
diff --git a/tools/bpf/resolve_btfids/fmodret.h b/tools/bpf/resolve_btfids/fmodret.h
index d61a9de..177b116 100644
--- a/tools/bpf/resolve_btfids/fmodret.h
+++ b/tools/bpf/resolve_btfids/fmodret.h
@@ -6,7 +6,7 @@

 struct btf;

-/* Tag the functions of ELF which return errors in BTF */
-int fmodret_tag(Elf *elf, struct btf *btf, int verbose);
+/* Tag the functions of ELF which return errors in BTF, but not those in DENY_PATH */
+int fmodret_tag(Elf *elf, struct btf *btf, const char *deny_path, int verbose);

 #endif
diff --git a/tools/bpf/resolve_btfids/main.c b/tools/bpf/resolve_btfids/main.c
index 0ac8952..b277a65 100644
--- a/tools/bpf/resolve_btfids/main.c
+++ b/tools/bpf/resolve_btfids/main.c
@@ -2100,6 +2100,7 @@ int main(int argc, const char **argv)
 		.sets     = RB_ROOT,
 	};
 	const char *btfids_path = NULL;
+	const char *fmodret_deny = NULL;
 	struct btf_links btf_links = {};
 	bool fatal_warnings = false;
 	bool resolve_btfids = true;
@@ -2121,6 +2122,8 @@ int main(int argc, const char **argv)
 			    "extract location BTF into a .BTF.inline file"),
 		OPT_BOOLEAN(0, "fmodret_scan", &fmodret_scan,
 			    "tag the functions which return errors as fmod_ret targets"),
+		OPT_STRING(0, "fmodret_deny", &fmodret_deny, "file",
+			   "output of nm with the functions which get no tag"),
 		OPT_CALLBACK(0, "btf_link", &btf_links, "section:module:btf-file",
 			     "patch a BTF link with --patch_btfids", parse_btf_link),
 		OPT_STRING(0, "patch_btfids", &btfids_path, "file",
@@ -2182,7 +2185,7 @@ int main(int argc, const char **argv)
 	if (btf2btf(&obj))
 		goto out;

-	if (fmodret_scan && fmodret_tag(obj.efile.elf, obj.btf, verbose))
+	if (fmodret_scan && fmodret_tag(obj.efile.elf, obj.btf, fmodret_deny, verbose))
 		goto out;

 	if (finalize_btf(&obj))

pw-bot: cr

  parent reply	other threads:[~2026-11-26  0:54 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 21:28 [PATCH v1 bpf-next 0/5] Do not tag selected functions as fmodret targets Anton Protopopov
2026-11-25 21:28 ` [PATCH v1 bpf-next 1/5] resolve_btfids: Do not tag functions which work on BPF objects Anton Protopopov
2026-11-25 21:25   ` sashiko-bot
2026-11-26 10:50     ` Anton Protopopov
2026-11-25 22:14   ` bot+bpf-ci
2026-11-26 10:49     ` Anton Protopopov
2026-11-25 21:28 ` [PATCH v1 bpf-next 2/5] resolve_btfids: Do not tag kfuncs and helpers Anton Protopopov
2026-11-25 21:28 ` [PATCH v1 bpf-next 3/5] resolve_btfids: Exclude a set of functions by name Anton Protopopov
2026-11-25 21:57   ` bot+bpf-ci
2026-11-26  0:55   ` Alexei Starovoitov
2026-11-26  0:56   ` Alexei Starovoitov
2026-11-26 11:29     ` Anton Protopopov
2026-11-25 21:28 ` [PATCH v1 bpf-next 4/5] resolve_btfids: Exclude the code of kernel/bpf and of the JIT Anton Protopopov
2026-11-25 22:15   ` bot+bpf-ci
2026-11-26  0:53   ` Alexei Starovoitov [this message]
2026-11-26 13:18     ` Anton Protopopov
2026-11-25 21:28 ` [PATCH v1 bpf-next 5/5] selftests/bpf: Test functions which must have no fmod_ret tag Anton Protopopov

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=DN4QDHMFA99A.27B7GAJLQQ7DK@gmail.com \
    --to=alexei.starovoitov@gmail.com \
    --cc=a.s.protopopov@gmail.com \
    --cc=almagui@kernel.org \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=memxor@gmail.com \
    /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