[PATCH vfs v1] bpf: Allow bpf_d_path() from filp_close_sync()

From: Ihor Solodrai

Date: Thu Sep 24 2026 - 16:46:55 EST


The d_path selftest closes its descriptors with close_range() so that
its fentry program on filp_close() runs. The vfs commit
46ace7e4dc4b ("fs: make close_range() synchronous") switched
close_range() to filp_close_sync(), so the program still attaches but
is never called:

test_d_path_basic:FAIL:close trampoline for filp_close was not called

With that series, close(), close_range() and the closes on exec and
exit all go through filp_close_sync(). Few paths still reach
filp_close(), dup2() being one of them, so a program on filp_close()
no longer sees most file closes.

Allow bpf_d_path() from filp_close_sync(). It takes the same arguments
as filp_close() and the program runs before the file is flushed and
its last reference is dropped, so file->f_path is still valid. Keep
filp_close() in the allowlist for existing programs.

Attach the selftest program to filp_close_sync() and keep triggering
it with close_range(), since filp_close_sync() may be inlined into
close().

Assisted-by: LLM
Signed-off-by: Ihor Solodrai <ihor.solodrai@xxxxxxxxx>

---

The issue was found by BPF CI when running on linux-next:
https://github.com/kernel-patches/bpf/actions/runs/35923662147/job/107398237366
---
kernel/trace/bpf_trace.c | 1 +
tools/testing/selftests/bpf/prog_tests/d_path.c | 7 ++++---
tools/testing/selftests/bpf/progs/test_d_path.c | 2 +-
3 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index 29260951aa87..09a27810ca27 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -972,6 +972,7 @@ BTF_ID(func, vfs_fallocate)
BTF_ID(func, dentry_open)
BTF_ID(func, vfs_getattr)
BTF_ID(func, filp_close)
+BTF_ID(func, filp_close_sync)
BTF_SET_END(btf_allowlist_d_path)

static bool bpf_d_path_allowed(const struct bpf_prog *prog)
diff --git a/tools/testing/selftests/bpf/prog_tests/d_path.c b/tools/testing/selftests/bpf/prog_tests/d_path.c
index 1a2a2f1abf03..7665e1d28a64 100644
--- a/tools/testing/selftests/bpf/prog_tests/d_path.c
+++ b/tools/testing/selftests/bpf/prog_tests/d_path.c
@@ -109,8 +109,9 @@ static int trigger_fstat_events(pid_t pid)
fstat(indicatorfd, &fileStat);

out_close:
- /* sys_close no longer triggers filp_close, but we can
- * call sys_close_range instead which still does
+ /*
+ * filp_close_sync() may be inlined into close(2), so use
+ * close_range(2), which calls it directly.
*/
syscall_close(pipefd[0]);
syscall_close(pipefd[1]);
@@ -165,7 +166,7 @@ static void test_d_path_basic(void)

if (CHECK(!bss->called_close,
"close",
- "trampoline for filp_close was not called\n"))
+ "trampoline for filp_close_sync was not called\n"))
goto cleanup;

for (int i = 0; i < MAX_FILES; i++) {
diff --git a/tools/testing/selftests/bpf/progs/test_d_path.c b/tools/testing/selftests/bpf/progs/test_d_path.c
index 561b2f861808..edb5494a47f7 100644
--- a/tools/testing/selftests/bpf/progs/test_d_path.c
+++ b/tools/testing/selftests/bpf/progs/test_d_path.c
@@ -41,7 +41,7 @@ int BPF_PROG(prog_stat, struct path *path, struct kstat *stat,
return 0;
}

-SEC("fentry/filp_close")
+SEC("fentry/filp_close_sync")
int BPF_PROG(prog_close, struct file *file, void *id)
{
pid_t pid = bpf_get_current_pid_tgid() >> 32;
--
2.55.0