public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: patches@lists.linux.dev, stable@vger.kernel.org
Cc: "Ricardo B. Marlière" <rbm@suse.com>,
	"Andrii Nakryiko" <andrii@kernel.org>,
	"Sasha Levin" <sashal@kernel.org>,
	eddyz87@gmail.com, ast@kernel.org, daniel@iogearbox.net,
	bpf@vger.kernel.org
Subject: [PATCH AUTOSEL 6.17-5.4] selftests/bpf: Fix bpf_prog_detach2 usage in test_lirc_mode2
Date: Thu,  9 Oct 2025 11:55:14 -0400	[thread overview]
Message-ID: <20251009155752.773732-48-sashal@kernel.org> (raw)
In-Reply-To: <20251009155752.773732-1-sashal@kernel.org>

From: Ricardo B. Marlière <rbm@suse.com>

[ Upstream commit 98857d111c53954aa038fcbc4cf48873e4240f7c ]

Commit e9fc3ce99b34 ("libbpf: Streamline error reporting for high-level
APIs") redefined the way that bpf_prog_detach2() returns. Therefore, adapt
the usage in test_lirc_mode2_user.c.

Signed-off-by: Ricardo B. Marlière <rbm@suse.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20250828-selftests-bpf-v1-1-c7811cd8b98c@suse.com
Signed-off-by: Sasha Levin <sashal@kernel.org>
---

LLM Generated explanations, may be completely bogus:

## Analysis Complete

**Backport Status: YES**

### Extensive Technical Explanation

#### The Bug and Its Context

This commit fixes a **broken selftest** in `test_lirc_mode2_user.c` that
has been failing since Linux kernel v6.0 (June 2022) - a period of
approximately **3 years**.

#### Historical Timeline of the API Change

**Phase 1 - May 2021 (v5.14)**: Commit e9fc3ce99b34 ("libbpf: Streamline
error reporting for high-level APIs") introduced a new error handling
model for libbpf:

- **Legacy mode (default before v6.0)**: Integer-returning functions
  like `bpf_prog_detach2()` returned `-1` on error and set `errno`
- **Strict mode (LIBBPF_STRICT_DIRECT_ERRS)**: Functions return negative
  error codes directly (e.g., `-ENOENT` instead of `-1`)

The implementation in `libbpf_internal.h` showed:
```c
static inline int libbpf_err_errno(int ret)
{
    if (libbpf_mode & LIBBPF_STRICT_DIRECT_ERRS)
        return ret < 0 ? -errno : ret;  // New behavior
    return ret;  // Legacy: return -1
}
```

**Phase 2 - June 2022 (v6.0)**: Commit bd054102a8c7f ("libbpf: enforce
strict libbpf 1.0 behaviors") **made strict mode the mandatory default**
by removing the conditional logic:

```c
static inline int libbpf_err_errno(int ret)
{
    /* errno is already assumed to be set on error */
    return ret < 0 ? -errno : ret;  // Always strict mode now
}
```

This change is in all stable branches from **v6.0 onwards** (6.0.y,
6.1.y, 6.6.y, 6.12.y, 6.17.y, etc.).

#### The Actual Code Problem
(tools/testing/selftests/bpf/test_lirc_mode2_user.c:77)

**Before the fix** (broken since v6.0):
```c
ret = bpf_prog_detach2(progfd, lircfd, BPF_LIRC_MODE2);
if (ret != -1 || errno != ENOENT) {  // WRONG: expects ret == -1
    printf("bpf_prog_detach2 not attached should fail: %m\n");
    return 1;
}
```

**After the fix**:
```c
ret = bpf_prog_detach2(progfd, lircfd, BPF_LIRC_MODE2);
if (ret != -ENOENT) {  // CORRECT: expects ret == -ENOENT
    printf("bpf_prog_detach2 not attached should fail: %m\n");
    return 1;
}
```

#### Why The Test Was Broken

**Execution flow in v6.0+**:
1. `bpf_prog_detach2()` calls `sys_bpf(BPF_PROG_DETACH, ...)`
2. `sys_bpf()` → `syscall(__NR_bpf, ...)` returns `-1`, sets `errno =
   ENOENT`
3. `libbpf_err_errno(-1)` converts: `ret < 0 ? -errno : ret` → returns
   `-ENOENT` (value: -2)
4. Test checks `if (ret != -1 || errno != ENOENT)`:
   - `ret` is `-2` (not `-1`) ✗
   - Condition evaluates to `TRUE`
   - **Test incorrectly fails**

#### Why This Should Be Backported

1. **Fixes a Real Problem**: The test has been incorrectly failing for 3
   years on all v6.0+ kernels, potentially misleading developers who run
   BPF selftests

2. **Minimal Risk**: This is a **1-line change** in a selftest (not
   kernel code), changing only the expected return value check from `-1`
   to `-ENOENT`

3. **Meets Stable Criteria**:
   - ✅ Small (1 line changed)
   - ✅ Obviously correct (adapts test to match documented API behavior)
   - ✅ Fixes a genuine bug (broken test)
   - ✅ Already in mainline (v6.18)

4. **Selftest Policy**: My research shows selftests ARE regularly
   backported to stable kernels. Example commits in stable/linux-6.1.y:
   - `138749a8ff619 selftests/bpf: Fix a user_ringbuf failure with arm64
     64KB page size`
   - `5f3d693861c71 selftests/bpf: Mitigate sockmap_ktls
     disconnect_after_delete failure`

5. **Affects All Active Stable Branches**: Every stable kernel from v6.0
   onwards (including LTS 6.1, 6.6, and 6.12) has the broken test

#### Scope of Backport

This fix should be backported to **all stable kernels v6.0 and later**
that contain commit bd054102a8c7f (libbpf 1.0 enforcement). This
includes:
- linux-6.0.y
- linux-6.1.y (LTS)
- linux-6.6.y (LTS)
- linux-6.12.y (LTS)
- linux-6.13.y through linux-6.17.y

Kernels v5.19 and earlier do NOT need this fix because they still use
legacy mode where `bpf_prog_detach2()` returns `-1`.

 tools/testing/selftests/bpf/test_lirc_mode2_user.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/testing/selftests/bpf/test_lirc_mode2_user.c b/tools/testing/selftests/bpf/test_lirc_mode2_user.c
index 4694422aa76c3..88e4aeab21b7b 100644
--- a/tools/testing/selftests/bpf/test_lirc_mode2_user.c
+++ b/tools/testing/selftests/bpf/test_lirc_mode2_user.c
@@ -74,7 +74,7 @@ int main(int argc, char **argv)
 
 	/* Let's try detach it before it was ever attached */
 	ret = bpf_prog_detach2(progfd, lircfd, BPF_LIRC_MODE2);
-	if (ret != -1 || errno != ENOENT) {
+	if (ret != -ENOENT) {
 		printf("bpf_prog_detach2 not attached should fail: %m\n");
 		return 1;
 	}
-- 
2.51.0


  parent reply	other threads:[~2025-10-09 15:59 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20251009155752.773732-1-sashal@kernel.org>
2025-10-09 15:54 ` [PATCH AUTOSEL 6.17-5.4] bpf: Don't use %pK through printk Sasha Levin
2025-10-09 15:54 ` [PATCH AUTOSEL 6.17-6.1] bpftool: Fix -Wuninitialized-const-pointer warnings with clang >= 21 Sasha Levin
2025-10-09 15:54 ` [PATCH AUTOSEL 6.17-6.12] bpf: Use tnums for JEQ/JNE is_branch_taken logic Sasha Levin
2025-10-09 15:54 ` [PATCH AUTOSEL 6.17-6.16] selftests/bpf: Fix incorrect array size calculation Sasha Levin
2025-10-09 15:54 ` [PATCH AUTOSEL 6.17-6.12] selftests/bpf: Fix selftest verifier_arena_large failure Sasha Levin
2025-10-09 15:55 ` [PATCH AUTOSEL 6.17-6.1] bpf: Clear pfmemalloc flag when freeing all fragments Sasha Levin
2025-10-09 15:55 ` Sasha Levin [this message]
2025-10-09 15:55 ` [PATCH AUTOSEL 6.17-6.12] selftests/bpf: Fix flaky bpf_cookie selftest Sasha Levin
2025-10-09 15:55 ` [PATCH AUTOSEL 6.17] selftests: drv-net: Pull data before parsing headers Sasha Levin
2025-10-09 15:55 ` [PATCH AUTOSEL 6.17-6.12] libbpf: Fix USDT SIB argument handling causing unrecognized register error Sasha Levin
2025-10-09 15:55 ` [PATCH AUTOSEL 6.17-6.12] bpftool: Add CET-aware symbol matching for x86_64 architectures Sasha Levin
2025-10-09 15:55 ` [PATCH AUTOSEL 6.17-6.6] bpf: Do not limit bpf_cgroup_from_id to current's namespace Sasha Levin
2025-10-09 15:55 ` [PATCH AUTOSEL 6.17-6.1] selftests/bpf: Upon failures, exit with code 1 in test_xsk.sh Sasha Levin
2025-10-09 15:55 ` [PATCH AUTOSEL 6.17-6.16] selftests/bpf: Fix arena_spin_lock selftest failure Sasha Levin
2025-10-09 15:56 ` [PATCH AUTOSEL 6.17-6.6] riscv: bpf: Fix uninitialized symbol 'retval_off' Sasha Levin

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=20251009155752.773732-48-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=patches@lists.linux.dev \
    --cc=rbm@suse.com \
    --cc=stable@vger.kernel.org \
    /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