Re: [PATCH v15 3/4] selftests/bpf: add iocost cost model test

From: bot+bpf-ci

Date: Thu Dec 03 2026 - 10:33:32 EST


> selftests/bpf: add iocost cost model test

TL;DR: In iocost_model.c, iocost_test_env() may leave test_progs' cwd
unreachable after unshare(), if fchdir() runs on success, possibly breaking
later tests like test_sleepable_tracepoints, which expects getcwd() to start
with '/'.

> diff --git a/tools/testing/selftests/bpf/prog_tests/iocost_model.c b/tools/testing/selftests/bpf/prog_tests/iocost_model.c
> new file mode 100644
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/iocost_model.c

[ ... ]

> +static bool iocost_test_env(void)
> +{
> + static bool ready;
> + struct statfs sf;
> + int orig_ns = -1, orig_cwd = -1, supported;
> + bool ok = false;

[ ... ]

> + if (!ready) {
> + orig_cwd = open(".", O_RDONLY | O_DIRECTORY);
> + orig_ns = open("/proc/self/ns/mnt", O_RDONLY);
> + if (unshare(CLONE_NEWNS) ||

[ ... ]

> +out:
> + if (!ok)
> + ready = false;
> + if (orig_ns >= 0) {
> + if (!ok)
> + setns(orig_ns, CLONE_NEWNS);
> + close(orig_ns);
> + }
> + if (orig_cwd >= 0)
> + fchdir(orig_cwd);
> + close(orig_cwd);
> + return ok;
> +}

On the success path the process stays in the new private mount namespace,
since setns(orig_ns) only runs when !ok. Yet fchdir(orig_cwd) runs
unconditionally, pointing the cwd back at the vfsmount of the old
namespace.

Can this leave the cwd unreachable from the root for the rest of the
test_progs process? If the old namespace was created by an earlier
test's setup_cgroup_environment() and nothing else uses it, it is freed
and the cwd mount is detached. Later unshare() calls would not move the
cwd either, because copy_mnt_ns() only moves a pwd that lies in the
namespace being copied.

getcwd() would then return "(unreachable)/..." and realpath() on relative
paths would fail with ENOENT. For example, in a non-parallel ./test_progs
run, test_sleepable_tracepoints runs after the iocost tests and its
sys_exit subtest checks:

ASSERT_EQ(skel->bss->copied_byte, '/', "copied_byte");

That first byte of the getcwd() buffer would be '(' instead.

Would it work to call fchdir(orig_cwd) only on the !ok path, right after
setns(orig_ns, CLONE_NEWNS), and to close orig_cwd only when it is >= 0?


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/47135984243