From: Justin Suess <utilityemal77@gmail.com>
To: "Christian Brauner" <brauner@kernel.org>,
"Alexander Viro" <viro@zeniv.linux.org.uk>,
"Jan Kara" <jack@suse.cz>, NeilBrown <neil@brown.name>,
"Mickaël Salaün" <mic@digikod.net>,
"Alexei Starovoitov" <ast@kernel.org>,
"Daniel Borkmann" <daniel@iogearbox.net>,
"Andrii Nakryiko" <andrii@kernel.org>,
"Song Liu" <song@kernel.org>
Cc: linux-fsdevel@vger.kernel.org, bpf@vger.kernel.org,
linux-security-module@vger.kernel.org,
linux-kernel@vger.kernel.org, "Günther Noack" <gnoack@google.com>,
"Paul Moore" <paul@paul-moore.com>,
"James Morris" <jmorris@namei.org>,
"Serge E . Hallyn" <serge@hallyn.com>,
"Martin KaFai Lau" <martin.lau@linux.dev>,
"Eduard Zingerman" <eddyz87@gmail.com>,
"Yonghong Song" <yonghong.song@linux.dev>,
"John Fastabend" <john.fastabend@gmail.com>,
"Kumar Kartikeya Dwivedi" <memxor@gmail.com>,
"Jiri Olsa" <jolsa@kernel.org>,
"Jeff Layton" <jlayton@kernel.org>,
"Amir Goldstein" <amir73il@gmail.com>,
"Mateusz Guzik" <mjguzik@gmail.com>,
"Shuah Khan" <shuah@kernel.org>, "Tingmao Wang" <m@maowtm.org>,
"Justin Suess" <utilityemal77@gmail.com>
Subject: [PATCH bpf-next v1 02/11] namei: add vfs_walk_ancestors()
Date: Tue, 13 Oct 2026 12:47:51 -0400 [thread overview]
Message-ID: <20261013164800.2906577-3-utilityemal77@gmail.com> (raw)
In-Reply-To: <20261013164800.2906577-1-utilityemal77@gmail.com>
Add a callback-based walk over a path and its ancestors, built on
__path_walk_parent(): the kernel owns the loop and the callback must
not sleep, so an rcu-walk engine can be added later without changing
the API.
Internally the walk is a stepwise engine - vfs_walk_start(),
vfs_walk_next(), vfs_walk_end(), static to namei.c for now, with the
walk state declared in fs/internal.h - and vfs_walk_ancestors() is a
loop over it. The position's flags live in the walk state, where the
next step needs them anyway: stepping off a disconnected position
resumes at the root of its mount. A later patch exposes the engine so
an iterating consumer can drive it, with every step and invariant
staying in namei.c.
Disconnected root dentries are reported to the callback instead of
terminating the walk. A disconnected mountpoint landed on by a mount
crossing - the one position this walk visits that a
dget_parent()/follow_up() loop never does - is additionally flagged
VFS_WALK_POS_MOUNTPOINT, so security callers can reproduce their
pre-conversion evaluation sequence exactly.
Suggested-by: NeilBrown <neil@brown.name>
Suggested-by: Christian Brauner <brauner@kernel.org>
Signed-off-by: Justin Suess <utilityemal77@gmail.com>
---
fs/internal.h | 10 +++
fs/namei.c | 140 ++++++++++++++++++++++++++++++++++++++++++
include/linux/namei.h | 14 +++++
3 files changed, 164 insertions(+)
diff --git a/fs/internal.h b/fs/internal.h
index c658c8a5ebd5..bfe62c9bdfc9 100644
--- a/fs/internal.h
+++ b/fs/internal.h
@@ -71,6 +71,16 @@ struct dentry *start_dirop(struct dentry *parent, struct qstr *name,
unsigned int lookup_flags);
int lookup_noperm_common(struct qstr *qname, struct dentry *base);
+/*
+ * The stepwise engine under vfs_walk_ancestors(); fs-internal so iterating
+ * consumers (BPF) can drive it, with the walk invariants staying in namei.c.
+ */
+struct vfs_ancestor_walk {
+ struct path pos;
+ unsigned int pos_flags; /* VFS_WALK_POS_* describing pos */
+ unsigned int flags; /* VFS_WALK_* */
+};
+
void __init filename_init(void);
/*
diff --git a/fs/namei.c b/fs/namei.c
index 808fb4bed7c4..6d5b4b214334 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -2229,6 +2229,146 @@ static struct dentry *__path_walk_parent(struct path *path, const struct path *r
return dget(path->dentry);
}
+static unsigned int vfs_walk_pos_flags(const struct vfsmount *mnt,
+ const struct dentry *dentry)
+{
+ if (unlikely(IS_ROOT(dentry) && dentry != mnt->mnt_root))
+ return VFS_WALK_POS_DISCONNECTED;
+ return 0;
+}
+
+/* No boundary: ancestor walks go up to the real root. */
+static const struct path vfs_walk_no_root;
+
+/* Internal walk flag: the starting position has been yielded. */
+#define VFS_WALK_STARTED BIT(31)
+
+/**
+ * vfs_walk_start - begin a stepwise ancestor walk
+ * @aw: walk state, valid until vfs_walk_end()
+ * @path: position to walk up from; never modified
+ * @flags: %VFS_WALK_* flags; none defined yet, pass 0
+ */
+static void vfs_walk_start(struct vfs_ancestor_walk *aw,
+ const struct path *path, unsigned int flags)
+{
+ if (WARN_ON_ONCE(flags))
+ flags = 0;
+ aw->pos = *path;
+ aw->flags = flags;
+ aw->pos_flags = 0;
+ path_get(&aw->pos);
+}
+
+static int vfs_walk_step_ref(struct vfs_ancestor_walk *aw)
+{
+ struct dentry *parent;
+
+ if (unlikely(aw->pos_flags & VFS_WALK_POS_DISCONNECTED)) {
+ /* Resume at the root of the disconnected position's mount. */
+ dput(aw->pos.dentry);
+ aw->pos.dentry = dget(aw->pos.mnt->mnt_root);
+ aw->pos_flags = 0;
+ return 0;
+ }
+ parent = __path_walk_parent(&aw->pos, &vfs_walk_no_root,
+ LOOKUP_BENEATH);
+ if (IS_ERR(parent))
+ /* The real root. */
+ return 1;
+ /*
+ * A mount crossing can step onto a disconnected root, whose parent
+ * is itself: only then is the mountpoint itself yielded, flagged.
+ */
+ aw->pos_flags = parent == aw->pos.dentry ?
+ VFS_WALK_POS_DISCONNECTED | VFS_WALK_POS_MOUNTPOINT :
+ vfs_walk_pos_flags(aw->pos.mnt, parent);
+ dput(aw->pos.dentry);
+ aw->pos.dentry = parent;
+ return 0;
+}
+
+/**
+ * vfs_walk_next - yield the walk's next position in @aw->pos
+ * @aw: the walk
+ *
+ * The first call yields the starting position itself. @aw->pos_flags
+ * carries the position's %VFS_WALK_POS_* flags, with the semantics
+ * described at vfs_walk_ancestors().
+ *
+ * Returns: 0 with @aw->pos valid, 1 once the walk has passed the real
+ * root.
+ */
+static int vfs_walk_next(struct vfs_ancestor_walk *aw)
+{
+ if (aw->flags & VFS_WALK_STARTED) {
+ int err = vfs_walk_step_ref(aw);
+
+ if (err)
+ return err;
+ } else {
+ aw->flags |= VFS_WALK_STARTED;
+ aw->pos_flags |= vfs_walk_pos_flags(aw->pos.mnt, aw->pos.dentry);
+ }
+ return 0;
+}
+
+/**
+ * vfs_walk_end - finish a stepwise ancestor walk
+ * @aw: the walk; its positions, including @aw->pos, become invalid
+ */
+static void vfs_walk_end(struct vfs_ancestor_walk *aw)
+{
+ path_put(&aw->pos);
+}
+
+/**
+ * vfs_walk_ancestors - invoke a callback on a path and each of its ancestors
+ * @path: path to walk up from; the caller's path is never modified
+ * @cb: callback invoked on @path, then on each ancestor up to the real
+ * root, crossing mount boundaries. @cb must not sleep and returns
+ * %VFS_WALK_CONTINUE, %VFS_WALK_STOP or a negative errno to abort the
+ * walk. @ancestor is only valid during the invocation; @cb must take
+ * its own references to keep a position.
+ * A position whose dentry is a disconnected root is flagged with
+ * %VFS_WALK_POS_DISCONNECTED (plus %VFS_WALK_POS_MOUNTPOINT when it
+ * is a mountpoint a mount crossing landed on rather than a parent);
+ * if @cb continues over it, the walk resumes at the root of that
+ * position's mount.
+ * @data: opaque argument passed to @cb
+ * @flags: %VFS_WALK_* flags; none defined yet, pass 0
+ *
+ * Returns: 0 once the real root was reached, 1 if @cb stopped the walk, or
+ * the negative errno @cb aborted with.
+ */
+int vfs_walk_ancestors(const struct path *path,
+ int (*cb)(const struct path *ancestor,
+ unsigned int pos_flags, void *data),
+ void *data, unsigned int flags)
+{
+ struct vfs_ancestor_walk aw;
+ int ret;
+
+ vfs_walk_start(&aw, path, flags);
+ for (;;) {
+ ret = vfs_walk_next(&aw);
+ if (ret) {
+ if (ret == 1)
+ ret = 0;
+ break;
+ }
+ ret = cb(&aw.pos, aw.pos_flags, data);
+ if (ret < 0)
+ break;
+ if (ret == VFS_WALK_STOP) {
+ ret = 1;
+ break;
+ }
+ }
+ vfs_walk_end(&aw);
+ return ret;
+}
+
static struct dentry *follow_dotdot(struct nameidata *nd)
{
struct dentry *parent;
diff --git a/include/linux/namei.h b/include/linux/namei.h
index 86d657b24fc6..3e198de7a0d3 100644
--- a/include/linux/namei.h
+++ b/include/linux/namei.h
@@ -162,6 +162,20 @@ extern int follow_down_one(struct path *);
extern int follow_down(struct path *path, unsigned int flags);
extern int follow_up(struct path *);
+/* per-position flags passed to the vfs_walk_ancestors() callback */
+#define VFS_WALK_POS_DISCONNECTED BIT(0)
+/* the position is a mountpoint landed on by a mount crossing */
+#define VFS_WALK_POS_MOUNTPOINT BIT(1)
+
+/* vfs_walk_ancestors() callback verdicts; negative values abort the walk */
+#define VFS_WALK_STOP 0
+#define VFS_WALK_CONTINUE 1
+
+int vfs_walk_ancestors(const struct path *path,
+ int (*cb)(const struct path *ancestor,
+ unsigned int pos_flags, void *data),
+ void *data, unsigned int flags);
+
int start_renaming(struct renamedata *rd, int lookup_flags,
struct qstr *old_last, struct qstr *new_last);
int start_renaming_dentry(struct renamedata *rd, int lookup_flags,
--
2.55.0
next prev parent reply other threads:[~2026-10-13 16:48 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-13 16:47 [PATCH bpf-next v1 00/11] fs: unified VFS ancestor walk for Landlock and BPF Justin Suess
2026-10-13 16:47 ` [PATCH bpf-next v1 01/11] namei: introduce __path_walk_parent() Justin Suess
2026-10-13 16:54 ` sashiko-bot
2026-10-13 16:47 ` Justin Suess [this message]
2026-10-13 17:00 ` [PATCH bpf-next v1 02/11] namei: add vfs_walk_ancestors() sashiko-bot
2026-10-13 16:47 ` [PATCH bpf-next v1 03/11] landlock: convert ancestor walk to vfs_walk_ancestors() Justin Suess
2026-10-13 16:55 ` sashiko-bot
2026-10-13 16:47 ` [PATCH bpf-next v1 04/11] namei: expose the stepwise ancestor walk to fs/ Justin Suess
2026-10-13 16:54 ` sashiko-bot
2026-10-13 16:47 ` [PATCH bpf-next v1 05/11] bpf: add a path ancestor iterator Justin Suess
2026-10-13 17:11 ` sashiko-bot
2026-10-13 18:00 ` Justin Suess
2026-10-13 16:47 ` [PATCH bpf-next v1 06/11] selftests/bpf: exercise the " Justin Suess
2026-10-13 16:56 ` sashiko-bot
2026-10-13 19:50 ` Justin Suess
2026-10-13 16:47 ` [PATCH bpf-next v1 07/11] fs: add mnt_undo_legitimize() Justin Suess
2026-10-13 16:59 ` sashiko-bot
2026-10-13 16:47 ` [PATCH bpf-next v1 08/11] namei: add an rcu-walk mode to the ancestor walk Justin Suess
2026-10-13 17:01 ` sashiko-bot
2026-10-13 17:56 ` Justin Suess
2026-10-13 16:47 ` [PATCH bpf-next v1 09/11] bpf: support "__uninit" iterator arguments in generic kfuncs Justin Suess
2026-10-13 17:04 ` sashiko-bot
2026-10-13 16:47 ` [PATCH bpf-next v1 10/11] bpf: add a lockless path ancestor iterator Justin Suess
2026-10-13 17:03 ` sashiko-bot
2026-10-13 16:48 ` [PATCH bpf-next v1 11/11] selftests/bpf: exercise the " Justin Suess
2026-10-13 16:57 ` sashiko-bot
2026-10-13 17:50 ` bot+bpf-ci
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=20261013164800.2906577-3-utilityemal77@gmail.com \
--to=utilityemal77@gmail.com \
--cc=amir73il@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=brauner@kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=gnoack@google.com \
--cc=jack@suse.cz \
--cc=jlayton@kernel.org \
--cc=jmorris@namei.org \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=m@maowtm.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=mic@digikod.net \
--cc=mjguzik@gmail.com \
--cc=neil@brown.name \
--cc=paul@paul-moore.com \
--cc=serge@hallyn.com \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=viro@zeniv.linux.org.uk \
--cc=yonghong.song@linux.dev \
/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