BPF List
 help / color / mirror / Atom feed
From: Anton Protopopov <a.s.protopopov@gmail.com>
To: Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	Alan Maguire <almagui@kernel.org>,
	John Fastabend <john.fastabend@gmail.com>,
	bpf@vger.kernel.org
Cc: Anton Protopopov <a.s.protopopov@gmail.com>
Subject: [PATCH v2 bpf-next 1/5] resolve_btfids: Do not count errors translated to NULL
Date: Mon, 30 Nov 2026 18:04:18 +0000	[thread overview]
Message-ID: <20261130180425.150703-2-a.s.protopopov@gmail.com> (raw)
In-Reply-To: <20261130180425.150703-1-a.s.protopopov@gmail.com>

A function may test a value with IS_ERR() and return it only if it
is not an error:

	return IS_ERR(prev) ? NULL : prev;

The scanner does not evaluate conditions and counts the errors which
the value may hold. For a function which returns a pointer the tag
would let a program return an error which the callers do not expect.
They test only for NULL and dereference the error pointer.
inet_frag_find() on x86-64 is an example:

	mov $-12, %rbp; ...; cmp $-0xfff, %rbp
	mov %rbp, %rax; cmovae %r14, %rax

Describe a compare of a register with an immediate on x86-64 and
arm64. A 64-bit compare with -MAX_ERRNO or -MAX_ERRNO - 1 is the test
of IS_ERR(). The errors which the register holds there do not count.

Losing an error is safe, but for -EINPROGRESS: with it -EBUSY is no
failure, see means_failure(). So count -EINPROGRESS if the function
loads it anywhere, whether it is left out or not.

Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
---
 tools/bpf/resolve_btfids/fmodret.c       | 18 +++++++++---------
 tools/bpf/resolve_btfids/fmodret_scan.c  |  5 ++++-
 tools/bpf/resolve_btfids/fmodret_scan.h  |  5 ++++-
 tools/bpf/resolve_btfids/text_flow.c     | 12 ++++++++++--
 tools/bpf/resolve_btfids/text_flow.h     |  3 +++
 tools/bpf/resolve_btfids/text_insn.h     |  1 +
 tools/bpf/resolve_btfids/text_mc.cpp     |  9 ++++++---
 tools/bpf/resolve_btfids/text_mc.h       |  4 ++++
 tools/bpf/resolve_btfids/text_mc_arm64.c |  6 ++++++
 tools/bpf/resolve_btfids/text_mc_x86.c   |  6 ++++++
 10 files changed, 53 insertions(+), 16 deletions(-)

diff --git a/tools/bpf/resolve_btfids/fmodret.c b/tools/bpf/resolve_btfids/fmodret.c
index b950d6b52554..34085d944718 100644
--- a/tools/bpf/resolve_btfids/fmodret.c
+++ b/tools/bpf/resolve_btfids/fmodret.c
@@ -140,9 +140,10 @@ static bool *find_bpf_funcs(const struct btf *btf)
  * wait, not that the function failed. Their numbers are those of the kernel, in
  * asm-generic/errno.h. The errno.h of the build host can have other numbers.
  *
- * ASYNC: the function returns -EINPROGRESS too. Then its -EBUSY is no failure
+ * ASYNC: the function may return -EINPROGRESS too. Then its -EBUSY is no failure
  * either. In the crypto API it says that the request is queued on the backlog:
- * crypto_wait_req() waits after both.
+ * crypto_wait_req() waits after both. scan_func() may leave out an -EINPROGRESS
+ * which the function returns, so any one which it loads counts.
  */
 static bool means_failure(int error, bool async)
 {
@@ -158,20 +159,18 @@ static int cmp_error(const void *va, const void *vb)
 
 /*
  * Write the tag for a function which returns SIZE bytes, and in them one of the
- * NR VALUES of the return value register. In 8 bytes an error is sign extended.
+ * NR VALUES of the return value register. For ASYNC see means_failure(). In 8
+ * bytes an error is sign extended.
  * Return false if there is nothing to tag.
  *
  * The machine code cannot tell the -EPERM of a permission check from the -1 of a
  * comparator or of "no such index": a function whose only error is -1 has none.
  */
-static bool format_tag(char *tag, const int64_t *values, size_t nr, __u32 size)
+static bool format_tag(char *tag, const int64_t *values, size_t nr, __u32 size, bool async)
 {
-	bool async = false;
 	size_t i, n = 0;
 	int errors[nr];
 
-	for (i = 0; i < nr; i++)
-		async |= (int)values[i] == -115;
 	for (i = 0; i < nr; i++) {
 		int error = values[i];
 
@@ -332,6 +331,7 @@ int fmodret_tag(Elf *elf, struct btf *btf, const char *deny_path, int verbose)
 		const struct btf_type *t = btf__type_by_id(btf, id);
 		const struct image_func *func;
 		const int64_t *values;
+		bool async;
 		const char *name;
 		__u32 size;
 
@@ -360,7 +360,7 @@ int fmodret_tag(Elf *elf, struct btf *btf, const char *deny_path, int verbose)
 		if (denied && denied[func - img.funcs])
 			continue;
 		nr_scanned++;
-		err = scan_func(scan, func, &values);
+		err = scan_func(scan, func, &values, &async);
 		if (err == -EINVAL) {
 			nr_undecodable++;
 			err = 0;
@@ -368,7 +368,7 @@ int fmodret_tag(Elf *elf, struct btf *btf, const char *deny_path, int verbose)
 		if (err > 0) {
 			char tag[sizeof(TAG) + 6 * err];
 
-			if (!format_tag(tag, values, err, size))
+			if (!format_tag(tag, values, err, size, async))
 				continue;
 			if (verbose > 1)
 				fprintf(stderr, "fmodret: %s %s\n", name, tag + strlen(TAG));
diff --git a/tools/bpf/resolve_btfids/fmodret_scan.c b/tools/bpf/resolve_btfids/fmodret_scan.c
index 3753aa1e50c3..4dd0b8947812 100644
--- a/tools/bpf/resolve_btfids/fmodret_scan.c
+++ b/tools/bpf/resolve_btfids/fmodret_scan.c
@@ -201,7 +201,7 @@ static int add_value(struct scan *s, int64_t v)
 	return 0;
 }
 
-int scan_func(struct scan *s, const struct image_func *f, const int64_t **values)
+int scan_func(struct scan *s, const struct image_func *f, const int64_t **values, bool *async)
 {
 	const struct text_flow *flow = &s->flow;
 	int err = decode_func(s, f);
@@ -217,6 +217,7 @@ int scan_func(struct scan *s, const struct image_func *f, const int64_t **values
 	text_flow_solve(&s->flow);
 
 	s->nr_values = 0;
+	*async = false;
 	for (i = 0; i < flow->nr_insns && !err; i++) {
 		const struct text_insn *insn = &flow->insns[i];
 		uint64_t imm[2] = { insn->imm, insn->imm2 };
@@ -225,6 +226,8 @@ int scan_func(struct scan *s, const struct image_func *f, const int64_t **values
 
 		if (insn->kind != TEXT_MOV_IMM && insn->kind != TEXT_SELECT_IMM)
 			continue;
+		/* -EINPROGRESS of the kernel, not of the build host */
+		*async |= (int32_t)imm[0] == -115 || (int32_t)imm[1] == -115;
 		if (flow->reach[i].path == TEXT_EFFECT_PATH)
 			continue;
 		out = text_flow_out(flow, i);
diff --git a/tools/bpf/resolve_btfids/fmodret_scan.h b/tools/bpf/resolve_btfids/fmodret_scan.h
index f390ab7f5956..f3ae6e9cc5f3 100644
--- a/tools/bpf/resolve_btfids/fmodret_scan.h
+++ b/tools/bpf/resolve_btfids/fmodret_scan.h
@@ -24,8 +24,11 @@ void scan_set_proto(struct scan *scan, const struct image_func *func, unsigned i
  * Leave out a constant if each path from the entry to its return has an effect
  * on an object which FUNC gets a pointer to, see text_flow.h.
  * A function which ftrace cannot patch has none: no program can attach to it.
+ * Set ASYNC if any instruction of FUNC loads -EINPROGRESS, whether it is left
+ * out or not.
  * Return how many there are, or -EINVAL if the code cannot be decoded.
  */
-int scan_func(struct scan *scan, const struct image_func *func, const int64_t **values);
+int scan_func(struct scan *scan, const struct image_func *func, const int64_t **values,
+	      bool *async);
 
 #endif
diff --git a/tools/bpf/resolve_btfids/text_flow.c b/tools/bpf/resolve_btfids/text_flow.c
index d7d3f9d1c6c1..44e772e2deb2 100644
--- a/tools/bpf/resolve_btfids/text_flow.c
+++ b/tools/bpf/resolve_btfids/text_flow.c
@@ -5,6 +5,7 @@
 
 #include <linux/bits.h>
 #include <linux/compiler.h>
+#include <linux/err.h>
 
 #include "text_flow.h"
 
@@ -56,7 +57,8 @@ void text_flow_fold(struct text_flow *flow)
 			defs = TEXT_ALL_REGS;
 		else if (insn->kind == TEXT_CALL)
 			defs |= flow->arch->caller_saved;
-		else if (insn->kind != TEXT_OTHER && insn->kind != TEXT_COND_BRANCH)
+		else if (insn->kind != TEXT_OTHER && insn->kind != TEXT_CMP_IMM &&
+			 insn->kind != TEXT_COND_BRANCH)
 			defs |= BIT(insn->dst);
 		for (r = 0; defs && r < TEXT_MAX_REGS; r++)
 			if (defs & BIT(r))
@@ -156,6 +158,12 @@ static struct text_facts transfer(const struct text_flow *flow, u32 i)
 		if (insn->kind == TEXT_SELECT)
 			copy(&in, &out, insn->dst, insn->src2, cast);
 		break;
+	case TEXT_CMP_IMM:
+		/* IS_ERR(src) ? NULL : src. Count no error which the function tests itself */
+		if (!insn->is32 &&
+		    (insn->imm == -(u64)MAX_ERRNO || insn->imm == -(u64)MAX_ERRNO - 1))
+			kill(&in, BIT(insn->src));
+		break;
 	}
 	return in;
 }
@@ -170,7 +178,7 @@ static u32 transfer_based(const struct text_flow *flow, const struct text_insn *
 		return based;
 	if (insn->kind == TEXT_CALL)
 		return based & ~(defs | flow->arch->caller_saved);
-	if (insn->kind != TEXT_OTHER && insn->kind < TEXT_BRANCH)
+	if (insn->kind != TEXT_OTHER && insn->kind != TEXT_CMP_IMM && insn->kind < TEXT_BRANCH)
 		defs |= BIT(insn->dst);
 	if (insn->kind == TEXT_MOV_IMM || insn->load || !(insn->uses & based))
 		return based & ~defs;
diff --git a/tools/bpf/resolve_btfids/text_flow.h b/tools/bpf/resolve_btfids/text_flow.h
index 56cd13c277f3..beff2bc45538 100644
--- a/tools/bpf/resolve_btfids/text_flow.h
+++ b/tools/bpf/resolve_btfids/text_flow.h
@@ -14,6 +14,9 @@
  * join is the union. Branches are not evaluated, calls write what the ABI lets
  * them write, memory is not tracked.
  *
+ * Only one condition counts: a register which is compared with -MAX_ERRNO, as
+ * by IS_ERR(), leaves the set. The function returns no error which it tests.
+ *
  * A copy may convert the value, and only the last conversion counts. So there
  * are three sets.
  */
diff --git a/tools/bpf/resolve_btfids/text_insn.h b/tools/bpf/resolve_btfids/text_insn.h
index e498245a5b73..561cf152a7a2 100644
--- a/tools/bpf/resolve_btfids/text_insn.h
+++ b/tools/bpf/resolve_btfids/text_insn.h
@@ -23,6 +23,7 @@ enum text_insn_kind {
 	TEXT_SEXT32,		/* dst = (s32)src */
 	TEXT_SELECT,		/* dst = src or src2 */
 	TEXT_SELECT_IMM,	/* dst = src or imm */
+	TEXT_CMP_IMM,		/* the flags = src compared with imm, dst is not written */
 	TEXT_BRANCH,		/* to imm, 0 if it is computed */
 	TEXT_COND_BRANCH,	/* to imm or to the next instruction */
 	TEXT_CALL,		/* of imm, 0 if it is computed */
diff --git a/tools/bpf/resolve_btfids/text_mc.cpp b/tools/bpf/resolve_btfids/text_mc.cpp
index 69668f8b12d7..ffb3b6d54b2e 100644
--- a/tools/bpf/resolve_btfids/text_mc.cpp
+++ b/tools/bpf/resolve_btfids/text_mc.cpp
@@ -250,8 +250,8 @@ bool holds(const MCInst &inst, const mc_predicate &p)
 /* Describe INST as PATTERN says. Return false, and leave TI alone, if it does not match */
 bool match(const MCInst &inst, const Opcode &o, const mc_pattern &pattern, text_insn *ti)
 {
-	const Reg &dst = reg_of(inst, o, pattern.dst, true);
 	u8 kind = pattern.kind, src = 0, src2 = 0;
+	const Reg &dst = reg_of(inst, o, pattern.dst, kind != TEXT_CMP_IMM);
 	bool is32 = dst.bits == 32;
 	u64 imm = pattern.value;
 
@@ -271,6 +271,8 @@ bool match(const MCInst &inst, const Opcode &o, const mc_pattern &pattern, text_
 			imm <<= inst.getOperand(pattern.shift).getImm();
 		if (pattern.invert)
 			imm = ~imm;
+		if (pattern.negate)
+			imm = -imm;
 		if (is32)
 			imm = (u32)imm;
 	}
@@ -288,7 +290,7 @@ bool match(const MCInst &inst, const Opcode &o, const mc_pattern &pattern, text_
 	}
 	ti->kind = kind;
 	ti->is32 = is32;
-	ti->dst = dst.nr;
+	ti->dst = kind == TEXT_CMP_IMM ? 0 : dst.nr;
 	ti->src = src;
 	ti->src2 = src2;
 	ti->op = pattern.op;
@@ -370,7 +372,8 @@ int decode(const void *code, unsigned int len, u64 addr, text_insn *ti)
 	ti->load = d.MII->get(inst.getOpcode()).mayLoad();
 	for (const mc_pattern *pattern : o.patterns) {
 		if (match(inst, o, *pattern, ti)) {
-			ti->defs &= ~(1U << ti->dst);
+			if (ti->kind != TEXT_CMP_IMM)
+				ti->defs &= ~(1U << ti->dst);
 			return 0;
 		}
 	}
diff --git a/tools/bpf/resolve_btfids/text_mc.h b/tools/bpf/resolve_btfids/text_mc.h
index 574d4a94afa6..23478eb7d8b2 100644
--- a/tools/bpf/resolve_btfids/text_mc.h
+++ b/tools/bpf/resolve_btfids/text_mc.h
@@ -55,6 +55,9 @@ struct mc_predicate {
  * from another one in some release, while the pattern of the old one stays
  * for the releases before. The tests show that one of the two matches.
  *
+ * A pattern of kind TEXT_CMP_IMM writes no register: DST is the register which
+ * gives the width of the compare.
+ *
  * A pattern of kind TEXT_RETURN names a return to the caller. isReturn() holds
  * for a return from an exception as well, which does not hand the return value
  * register to a caller. Without such a pattern it is an opcode like any other,
@@ -71,6 +74,7 @@ struct mc_pattern {
 	u8 shift;		/* the operand which says how far the immediate is shifted left */
 	u8 op;			/* of TEXT_ALU_IMM */
 	bool invert;		/* the immediate */
+	bool negate;		/* the immediate */
 	bool or_zero;		/* TEXT_MOV_IMM: the immediate or 0 */
 	bool fixed;		/* the immediate is VALUE, not an operand */
 	s64 value;
diff --git a/tools/bpf/resolve_btfids/text_mc_arm64.c b/tools/bpf/resolve_btfids/text_mc_arm64.c
index 9cc41900fa4a..24e66a330852 100644
--- a/tools/bpf/resolve_btfids/text_mc_arm64.c
+++ b/tools/bpf/resolve_btfids/text_mc_arm64.c
@@ -22,6 +22,9 @@ static const struct mc_pattern arm64_patterns[] = {
 	{ "CSINCWr",	 TEXT_SELECT, .src = 1, .src2 = 1 },
 	/* and rd, rn, rm, asr #31: rn or 0 */
 	{ "ANDWrs",	 TEXT_SELECT, .src = 1, .src2 = 1, .predicates = { IMM(3, 2 << 6 | 31) } },
+	/* cmn of IS_ERR() */
+	{ "ADDSXri",	 TEXT_CMP_IMM, .dst = 1, .src = 1, .imm = 2, .shift = 3, .negate = true,
+			 .predicates = { ZERO_REG(0) } },
 	/* HINT: nop, bti, yield, wfe ... */
 	{ "HINT", TEXT_OTHER }, { "AUTIASP", TEXT_OTHER }, { "PRFMui", TEXT_OTHER },
 	{ "HINT", TEXT_OTHER, .nop = true, .predicates = { IMM(0, 0) } },	/* nop */
@@ -59,6 +62,9 @@ static const struct mc_test arm64_tests[] = {
 	T("00 05 89 1a", TEXT_SELECT, .is32 = true, SRC(8)),		/* csinc w0, w8, w9, eq */
 	T("00 7d 89 0a", TEXT_SELECT, .is32 = true, SRC(8)),		/* and w0, w8, w9, asr #31 */
 	T("00 01 09 0a", TEXT_OTHER, .defs = BIT(0)),			/* and w0, w8, w9 */
+	T("7f fe 3f b1", TEXT_CMP_IMM, SRC(19), IMMS(-4095, -4095)),	/* cmn x19, #4095 */
+	T("1f 04 40 b1", TEXT_CMP_IMM, IMMS(-4096, -4096)),		/* cmn x0, #1, lsl #12 */
+	T("60 fe 3f b1", TEXT_OTHER, .defs = BIT(0)),			/* adds x0, x19, #4095 */
 	T("1f 20 03 d5", TEXT_OTHER, .nop = true),			/* nop */
 	T("5f 24 03 d5", TEXT_OTHER),					/* bti c */
 	T("bf 23 03 d5", TEXT_OTHER, .defs = BIT(30)),			/* autiasp */
diff --git a/tools/bpf/resolve_btfids/text_mc_x86.c b/tools/bpf/resolve_btfids/text_mc_x86.c
index 4f60a8f3263c..d0ced79c2e27 100644
--- a/tools/bpf/resolve_btfids/text_mc_x86.c
+++ b/tools/bpf/resolve_btfids/text_mc_x86.c
@@ -32,6 +32,9 @@ static const struct mc_pattern x86_patterns[] = {
 	{ "SUB32ri8",		TEXT_ALU_IMM, .src = 1, .imm = 2, .op = TEXT_SUB },
 	{ "SUB64ri8",		TEXT_ALU_IMM, .src = 1, .imm = 2, .op = TEXT_SUB },
 	{ "NOT32r",		TEXT_ALU_IMM, .src = 1, .op = TEXT_XOR, VALUE(-1) },
+	/* The compares of IS_ERR() */
+	{ "CMP64ri32",		TEXT_CMP_IMM, .imm = 1 },
+	{ "CMP64i32",		TEXT_CMP_IMM, .dst = I, .src = I },
 	{ "LOCK_PREFIX",	TEXT_OTHER },
 	{ "NOOPL",		TEXT_OTHER, .nop = true },
 	/* A rep prefix is not a part of the MCInst */
@@ -70,6 +73,9 @@ static const struct mc_test x86_tests[] = {
 	T("83 eb 0e",		  TEXT_ALU_IMM, .is32 = true, .dst = RBX, SRC(RBX), .op = TEXT_SUB, IMMS(14, 14)), /* sub $14, %ebx */
 	T("48 83 eb 0e",	  TEXT_ALU_IMM, .dst = RBX, SRC(RBX), .op = TEXT_SUB, IMMS(14, 14)),	/* sub $14, %rbx */
 	T("f7 d0",		  TEXT_ALU_IMM, .is32 = true, .op = TEXT_XOR, IMMS(0xffffffff, 0xffffffff)), /* not %eax */
+	T("48 81 fd 01 f0 ff ff", TEXT_CMP_IMM, SRC(RBP), IMMS(-4095, -4095)),	/* cmp $-4095, %rbp */
+	T("48 3d 00 f0 ff ff",	  TEXT_CMP_IMM, IMMS(-4096, -4096)),		/* cmp $-4096, %rax */
+	T("3d 01 f0 ff ff",	  TEXT_OTHER),					/* cmp $-4095, %eax */
 	T("f0",			  TEXT_OTHER),					/* lock */
 	T("0f 1f 44 00 00",	  TEXT_OTHER, .nop = true),			/* nopl 0x0(%rax,%rax,1) */
 	T("f3 48 ab",		  TEXT_OTHER, .defs = BIT(RCX) | BIT(RDI)),	/* rep stosq */
-- 
2.43.0


  reply	other threads:[~2026-11-30 17:52 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 18:04 [PATCH v2 bpf-next 0/5] Tag fmod_ret functions that return ERR_PTR() Anton Protopopov
2026-11-30 18:04 ` Anton Protopopov [this message]
2026-11-30 18:06   ` [PATCH v2 bpf-next 1/5] resolve_btfids: Do not count errors translated to NULL sashiko-bot
2026-11-30 20:02     ` Anton Protopopov
2026-11-30 18:04 ` [PATCH v2 bpf-next 2/5] resolve_btfids: Tag the functions that return ERR_PTR() Anton Protopopov
2026-11-30 18:04 ` [PATCH v2 bpf-next 3/5] resolve_btfids: Do not tag seq_file operations Anton Protopopov
2026-11-30 18:04 ` [PATCH v2 bpf-next 4/5] resolve_btfids: Do not tag functions whose callers break on a skip Anton Protopopov
2026-11-30 18:02   ` sashiko-bot
2026-11-30 20:10     ` Anton Protopopov
2026-11-30 18:04 ` [PATCH v2 bpf-next 5/5] selftests/bpf: Test fmod_ret on functions that return a pointer 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=20261130180425.150703-2-a.s.protopopov@gmail.com \
    --to=a.s.protopopov@gmail.com \
    --cc=almagui@kernel.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=john.fastabend@gmail.com \
    --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