bpf path iterator
Subsystems (7)
Song Liu
reviewerv5 cover bpf path iterator
In security use cases, it is common to apply rules to VFS subtrees. However, filtering files in a subtree is not straightforward [1].
One solution to this problem is to start from a path and walk up the VFS tree (towards the root). Among in-tree LSMs, Landlock uses this solution.
BPF LSM solutions, such like Tetragon [2], also use similar approaches. However, due to lack of proper helper/kfunc support, BPF LSM solutions usually do the path walk with probe read, which is racy.
This patchset introduces a new helper path_walk_parent, which walks path to its VFS parent. The helper is used in Landlock.
A new BPF iterator, path iterator, is introduced to do the path walking. The BPF path iterator uses the new path_walk_parent help to walk the VFS tree.
Changes v4 => v5:
1. Minor changes to path_walk_parent per suggestions by Neil Brown and Tingmao Wang.
v4: https://lore.kernel.org/bpf/[email protected]/
Changes v3 => v4:
1. Rewrite path_walk_parent based on suggestion from Neil Brown.
2. When path_walk_parent() returns false, it call path_put on @path and then zeros @path.
v3: https://lore.kernel.org/bpf/[email protected]/
Changes v2 => v3:
1. Fix an issue with path_walk_parent.
2. Move bpf path iterator to fs/bpf_fs_kfuncs.c
3. Optimize bpf path iterator (less memory).
4. Add more selftests.
5. Add more comments.
v2: https://lore.kernel.org/bpf/[email protected]/
Changes v1 => v2:
1. Rename path_parent => path_walk_parent.
2. Remove path_connected check in path_walk_parent.
3. Fix is_access_to_paths_allowed().
4. Remove mode for path iterator, add a flag instead.
v1: https://lore.kernel.org/bpf/[email protected]/
[1] https://lpc.events/event/18/contributions/1940/
[2] https://github.com/cilium/tetragon/
Song Liu (5):
namei: Introduce new helper function path_walk_parent()
landlock: Use path_walk_parent()
bpf: Introduce path iterator
selftests/bpf: Add tests for bpf path iterator selftests/bpf: Path walk test
fs/bpf_fs_kfuncs.c | 72 +++++++++ fs/namei.c | 95 +++++++++--- include/linux/namei.h | 2 + kernel/bpf/verifier.c | 5 + security/landlock/fs.c | 30 +--- .../testing/selftests/bpf/bpf_experimental.h | 6 + .../selftests/bpf/prog_tests/path_iter.c | 111 ++++++++++++++ tools/testing/selftests/bpf/progs/path_iter.c | 145 ++++++++++++++++++ tools/testing/selftests/bpf/progs/path_walk.c | 59 +++++++ 9 files changed, 485 insertions(+), 40 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/path_iter.c create mode 100644 tools/testing/selftests/bpf/progs/path_iter.c create mode 100644 tools/testing/selftests/bpf/progs/path_walk.c
--
2.47.1
Song Liu
reviewerPATCH v5 1/5 namei: Introduce new helper function path_walk_parent()
This helper walks an input path to its parent. Logic are added to handle walking across mount tree.
This will be used by landlock, and BPF LSM.
--- fs/namei.c | 95 +++++++++++++++++++++++++++++++++++-------- include/linux/namei.h | 2 + 2 files changed, 79 insertions(+), 18 deletions(-)
fs/namei.c +77 -18
@@ -2048,36 +2048,95 @@ static struct dentry *follow_dotdot_rcu(struct nameidata *nd)return nd->path.dentry; } -static struct dentry *follow_dotdot (struct nameidata *nd) +/** +* __path_walk_parent - Find the parent of the given struct path +* @path - The struct path to start from +* @root - A struct path which serves as a boundary not to be crosses. +* - If @root is zero'ed, walk all the way to global root. +* @flags - Some LOOKUP_ flags. +* +* Find and return the dentry for the parent of the given path +* (mount/dentry). If the given path is the root of a mounted tree, it +* is first updated to the mount point on which that tree is mounted. +* +* If %LOOKUP_NO_XDEV is given, then *after* the path is updated to a new +* mount, the error EXDEV is returned. +* +* If no parent can be found, either because the tree is not mounted or +* because the @path matches the @root, then @path->dentry is returned +* unless @flags contains %LOOKUP_BENEATH, in which case -EXDEV is returned. +* +* Returns: either an ERR_PTR() or the chosen parent which will have had +* the refcount incremented. +*/ +static struct dentry *__path_walk_parent (struct path *path,const struct path *root,int flags) { -struct dentry *parent; - -if (path_equal (&nd->path, &nd->root)) +if (path_equal (path, root)) goto in_root; -if (unlikely (nd->path.dentry == nd->path.mnt->mnt_root)) { -struct path path; +if (unlikely (path->dentry == path->mnt->mnt_root)) { +struct path new_path; -if (!choose_mountpoint (real_mount (nd->path.mnt), - &nd->root, &path)) +if (!choose_mountpoint (real_mount (path->mnt), + root, &new_path)) goto in_root; -path_put (&nd->path); - nd->path = path; - nd->inode = path.dentry->d_inode; -if (unlikely (nd->flags &LOOKUP_NO_XDEV )) +path_put (path); + *path = new_path; +if (unlikely (flags &LOOKUP_NO_XDEV ))return ERR_PTR (-EXDEV ); }/* rare case of legitimate dget_parent()... */ - parent =dget_parent (nd->path.dentry); +return dget_parent (path->dentry); + +in_root: +if (unlikely (flags &LOOKUP_BENEATH )) +return ERR_PTR (-EXDEV ); +return dget (path->dentry); +} + +/** +* path_walk_parent - Walk to the parent of path +* @path: input and output path. +* @root: root of the path walk, do not go beyond this root. If @root is +* zero'ed, walk all the way to real root. +* +* Given a path, find the parent path. Replace @path with the parent path. +* If we were already at the real root or a disconnected root, @path is +* not changed. +* +* Returns: +* 0 - if @path is updated to its parent. +* <0 - if @path is already the root (real root or @root). +*/ +int path_walk_parent (struct path *path,const struct path *root) +{ +struct dentry *parent; + + parent =__path_walk_parent (path, root,LOOKUP_BENEATH ); + +if (IS_ERR (parent)) +return PTR_ERR (parent); + +if (parent == path->dentry) { +dput (parent); +return -ENOENT ; + } +dput (path->dentry); + path->dentry = parent; +return 0 ; +} + +static struct dentry *follow_dotdot (struct nameidata *nd) +{ +struct dentry *parent =__path_walk_parent (&nd->path, &nd->root, nd->flags); + +if (IS_ERR (parent)) +return parent;if (unlikely (!path_connected (nd->path.mnt, parent))) {dput (parent);return ERR_PTR (-ENOENT ); } + nd->inode = nd->path.dentry->d_inode;return parent; - -in_root: -if (unlikely (nd->flags &LOOKUP_BENEATH )) -return ERR_PTR (-EXDEV ); -return dget (nd->path.dentry); }static const char *handle_dots (struct nameidata *nd,int type)
include/linux/namei.h +2 -0
@@ -85,6 +85,8 @@ extern int follow_down_one(struct path *);extern int follow_down (struct path *path,unsigned int flags);extern int follow_up (struct path *); +int path_walk_parent (struct path *path,const struct path *root); +extern struct dentry *lock_rename (struct dentry *,struct dentry *);extern struct dentry *lock_rename_child (struct dentry *,struct dentry *);extern void unlock_rename (struct dentry *,struct dentry *);
Signature · 1 line
2.47.1
kernel test robot
build botRe: [PATCH v5 bpf-next 1/5] namei: Introduce new helper function path_walk_parent()
kernel test robot noticed the following build warnings:
Hi Song,
kernel test robot noticed the following build warnings:
[auto build test WARNING on bpf-next/master]
url: https://github.com/intel-lab-lkp/linux/commits/Song-Liu/namei-Introduce-new-helper-function-path_walk_parent/20250617-141322 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master patch link: https://lore.kernel.org/r/20250617061116.3681325-2-song%40kernel.org patch subject: [PATCH v5 bpf-next 1/5] namei: Introduce new helper function path_walk_parent() config: loongarch-randconfig-r072-20250618 (https://download.01.org/0day-ci/archive/20250618/[email protected]/config) compiler: loongarch64-linux-gcc (GCC) 12.4.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250618/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> Warning: fs/namei.c:2072 function parameter 'path' not described in '__path_walk_parent' >> Warning: fs/namei.c:2072 function parameter 'root' not described in '__path_walk_parent' >> Warning: fs/namei.c:2072 function parameter 'flags' not described in '__path_walk_parent'
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [PATCH v5 bpf-next 1/5] namei: Introduce new helper function path_walk_parent()
In reply to Song LiuQuoted 7 lines · Song Liu
This helper walks an input path to its parent. Logic are added to handle walking across mount tree.
This will be used by landlock, and BPF LSM.
Suggested-by Neil Brown <[email protected]>Signed-off-by Song Liu <[email protected]>
Looks good to me. Feel free to add:
One note below:
Quoted 12 lines
fs/namei.c +11
-1-static struct dentry *follow_dotdot (struct nameidata *nd) +/** +* __path_walk_parent - Find the parent of the given struct path +* @path - The struct path to start from +* @root - A struct path which serves as a boundary not to be crosses. +* - If @root is zero'ed, walk all the way to global root. +* @flags - Some LOOKUP_ flags. +* +* Find and return the dentry for the parent of the given path +* (mount/dentry). If the given path is the root of a mounted tree, it +* is first updated to the mount point on which that tree is mounted. +*
fs/namei.c static struct dentry *follow_dotdot_rcu(struct nameidata *nd) · line 2071
Quoted 10 lines
fs/namei.c +10
-0+* If %LOOKUP_NO_XDEV is given, then *after* the path is updated to a new +* mount, the error EXDEV is returned. +* +* If no parent can be found, either because the tree is not mounted or +* because the @path matches the @root, then @path->dentry is returned +* unless @flags contains %LOOKUP_BENEATH, in which case -EXDEV is returned. +* +* Returns: either an ERR_PTR() or the chosen parent which will have had +* the refcount incremented. +*/
The behavior with LOOKUP_NO_XDEV is kind of odd (not your fault) and interestingly I wasn't able to find a place that would depend on the path being updated in that case. So either I'm missing some subtle detail (quite possible) or we can clean that up in the future.
Honza
Quoted 104 lines
fs/namei.c +56
-17+static struct dentry *__path_walk_parent (struct path *path,const struct path *root,int flags) { -struct dentry *parent; - -if (path_equal (&nd->path, &nd->root)) +if (path_equal (path, root)) goto in_root; -if (unlikely (nd->path.dentry == nd->path.mnt->mnt_root)) { -struct path path; +if (unlikely (path->dentry == path->mnt->mnt_root)) { +struct path new_path; -if (!choose_mountpoint (real_mount (nd->path.mnt), - &nd->root, &path)) +if (!choose_mountpoint (real_mount (path->mnt), + root, &new_path)) goto in_root; -path_put (&nd->path); - nd->path = path; - nd->inode = path.dentry->d_inode; -if (unlikely (nd->flags &LOOKUP_NO_XDEV )) +path_put (path); + *path = new_path; +if (unlikely (flags &LOOKUP_NO_XDEV ))return ERR_PTR (-EXDEV ); }/* rare case of legitimate dget_parent()... */ - parent =dget_parent (nd->path.dentry); +return dget_parent (path->dentry); + +in_root: +if (unlikely (flags &LOOKUP_BENEATH )) +return ERR_PTR (-EXDEV ); +return dget (path->dentry); +} + +/** +* path_walk_parent - Walk to the parent of path +* @path: input and output path. +* @root: root of the path walk, do not go beyond this root. If @root is +* zero'ed, walk all the way to real root. +* +* Given a path, find the parent path. Replace @path with the parent path. +* If we were already at the real root or a disconnected root, @path is +* not changed. +* +* Returns: +* 0 - if @path is updated to its parent. +* <0 - if @path is already the root (real root or @root). +*/ +int path_walk_parent (struct path *path,const struct path *root) +{ +struct dentry *parent; + + parent =__path_walk_parent (path, root,LOOKUP_BENEATH ); + +if (IS_ERR (parent)) +return PTR_ERR (parent); + +if (parent == path->dentry) { +dput (parent); +return -ENOENT ; + } +dput (path->dentry); + path->dentry = parent; +return 0 ; +} + +static struct dentry *follow_dotdot (struct nameidata *nd) +{ +struct dentry *parent =__path_walk_parent (&nd->path, &nd->root, nd->flags); + +if (IS_ERR (parent)) +return parent;if (unlikely (!path_connected (nd->path.mnt, parent))) {dput (parent);return ERR_PTR (-ENOENT ); } + nd->inode = nd->path.dentry->d_inode;return parent; - -in_root: -if (unlikely (nd->flags &LOOKUP_BENEATH )) -return ERR_PTR (-EXDEV ); -return dget (nd->path.dentry); }static const char *handle_dots (struct nameidata *nd,int type)include/linux/namei.h +2
-0@@ -85,6 +85,8 @@ extern int follow_down_one(struct path *);extern int follow_down (struct path *path,unsigned int flags);extern int follow_up (struct path *); +int path_walk_parent (struct path *path,const struct path *root); +extern struct dentry *lock_rename (struct dentry *,struct dentry *);extern struct dentry *lock_rename_child (struct dentry *,struct dentry *);extern void unlock_rename (struct dentry *,struct dentry *);Signature · 1 line
2.47.1
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Song Liu
reviewerRe: [PATCH v5 bpf-next 1/5] namei: Introduce new helper function path_walk_parent()
Hi Jan,
Quoted 5 lines · Jan Kara
Quoted 3 lines · Song Liu
This helper walks an input path to its parent. Logic are added to handle walking across mount tree.
Quoted 8 lines
Quoted 4 lines
This will be used by landlock, and BPF LSM.
Suggested-by Neil Brown <[email protected]>Signed-off-by Song Liu <[email protected]>Looks good to me. Feel free to add:
Reviewed-by Jan Kara <[email protected]>
Thanks for the review!
[...]
Quoted 9 lines
Quoted 4 lines
+ *
+ * Returns: either anERR_PTR()or the chosen parent which will have had
+ * the refcount incremented.
+ */The behavior with
LOOKUP_NO_XDEVis kind of odd (not your fault) and interestingly I wasn't able to find a place that would depend on the path being updated in that case. So either I'm missing some subtle detail (quite possible) or we can clean that up in the future.
We have RESOLVE_NO_XDEV in uapi/linux/openat2.h, so I guess we cannot really remove it?
Thanks,
Song
[...]
Jan Kara
reviewerRe: [PATCH v5 bpf-next 1/5] namei: Introduce new helper function path_walk_parent()
Quoted 6 lines · Song Liu
Quoted 5 lines · Jan Kara
Quoted 4 lines
+ *
+ * Returns: either anERR_PTR()or the chosen parent which will have had
+ * the refcount incremented.
+ */
Quoted 7 lines
Quoted 4 lines
The behavior with
LOOKUP_NO_XDEVis kind of odd (not your fault) and interestingly I wasn't able to find a place that would depend on the path being updated in that case. So either I'm missing some subtle detail (quite possible) or we can clean that up in the future.We have
RESOLVE_NO_XDEVinuapi/linux/openat2.h, so I guess we cannot really remove it?
I didn't mean to remove the LOOKUP_NO_XDEV flag, I meant to not update the passed path if LOOKUP_NO_XDEV is set, we are crossing the mountpoint and thus returning -EXDEV. As far as I've checked once we return error, everybody just path_put()s the nd->path so its update is just pointless. But there are many (indirect) callers so I might have missed some case.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Yonghong Song
reviewerRe: [PATCH v5 bpf-next 1/5] namei: Introduce new helper function path_walk_parent()
In reply to Song LiuQuoted 61 lines · Song Liu
This helper walks an input path to its parent. Logic are added to handle walking across mount tree.
This will be used by landlock, and BPF LSM.
Suggested-by Neil Brown <[email protected]>Signed-off-by Song Liu <[email protected]>--- fs/namei.c | 95 +++++++++++++++++++++++++++++++++++-------- include/linux/namei.h | 2 + 2 files changed, 79 insertions(+), 18 deletions(-)fs/namei.c +27
-10@@ -2048,36 +2048,95 @@ static struct dentry *follow_dotdot_rcu(struct nameidata *nd)return nd->path.dentry; } -static struct dentry *follow_dotdot (struct nameidata *nd) +/** +* __path_walk_parent - Find the parent of the given struct path +* @path - The struct path to start from +* @root - A struct path which serves as a boundary not to be crosses. +* - If @root is zero'ed, walk all the way to global root. +* @flags - Some LOOKUP_ flags. +* +* Find and return the dentry for the parent of the given path +* (mount/dentry). If the given path is the root of a mounted tree, it +* is first updated to the mount point on which that tree is mounted. +* +* If %LOOKUP_NO_XDEV is given, then *after* the path is updated to a new +* mount, the error EXDEV is returned. +* +* If no parent can be found, either because the tree is not mounted or +* because the @path matches the @root, then @path->dentry is returned +* unless @flags contains %LOOKUP_BENEATH, in which case -EXDEV is returned. +* +* Returns: either an ERR_PTR() or the chosen parent which will have had +* the refcount incremented. +*/ +static struct dentry *__path_walk_parent (struct path *path,const struct path *root,int flags) { -struct dentry *parent; - -if (path_equal (&nd->path, &nd->root)) +if (path_equal (path, root)) goto in_root; -if (unlikely (nd->path.dentry == nd->path.mnt->mnt_root)) { -struct path path; +if (unlikely (path->dentry == path->mnt->mnt_root)) { +struct path new_path; -if (!choose_mountpoint (real_mount (nd->path.mnt), - &nd->root, &path)) +if (!choose_mountpoint (real_mount (path->mnt), + root, &new_path)) goto in_root; -path_put (&nd->path); - nd->path = path;
fs/namei.c static struct dentry *follow_dotdot_rcu(struct nameidata *nd) · line 2088
Quoted 10 lines
fs/namei.c +4
-3- nd->inode = path.dentry->d_inode; -if (unlikely (nd->flags &LOOKUP_NO_XDEV )) +path_put (path); + *path = new_path; +if (unlikely (flags &LOOKUP_NO_XDEV ))return ERR_PTR (-EXDEV ); }/* rare case of legitimate dget_parent()... */ - parent =dget_parent (nd->path.dentry); +return dget_parent (path->dentry);
I have some confusion with this patch when crossing mount boundary.
In d_path.c, we have
static int __prepend_path(const struct dentry *dentry, const struct mount *mnt,
const struct path *root, struct prepend_buffer *p)
{
while (dentry != root->dentry || &mnt->mnt != root->mnt) {
const struct dentry *parent = READ_ONCE(dentry->d_parent);
if (dentry == mnt->mnt.mnt_root) {
struct mount *m = READ_ONCE(mnt->mnt_parent);
struct mnt_namespace *mnt_ns;
if (likely(mnt != m)) {
dentry = READ_ONCE(mnt->mnt_mountpoint);
mnt = m;
continue;
}
/* Global root */
mnt_ns = READ_ONCE(mnt->mnt_ns);
/* open-coded is_mounted() to use local mnt_ns */
if (!IS_ERR_OR_NULL(mnt_ns) && !is_anon_ns(mnt_ns))
return 1; // absolute root
else
return 2; // detached or not attached yet
}
if (unlikely(dentry == parent))
/* Escaped? */
return 3;
prefetch(parent);
if (!prepend_name(p, &dentry->d_name))
break;
dentry = parent;
}
return 0;
}At the mount boundary and not at root mount, the code has
dentry = READ_ONCE(mnt->mnt_mountpoint); mnt = m; /* 'mnt' will be parent mount */ continue;
After that, we have
const struct dentry *parent = READ_ONCE(dentry->d_parent);
if (dentry == mnt->mnt.mnt_root) {
/* assume this is false */
}
...
prefetch(parent);
if (!prepend_name(p, &dentry->d_name))
break;
dentry = parent;So the prepend_name(p, &dentry->d_name) is actually from mnt->mnt_mountpoint.
In your above code, maybe we should return path->dentry in the below if statement?
if (unlikely(path->dentry == path->mnt->mnt_root)) {
struct path new_path;
if (!choose_mountpoint(real_mount(path->mnt),
root, &new_path))
goto in_root;
path_put(path);
*path = new_path;
if (unlikely(flags & LOOKUP_NO_XDEV))
return ERR_PTR(-EXDEV);
+ return path->dentry;
}
/* rare case of legitimate dget_parent()... */
return dget_parent(path->dentry);Also, could you add some selftests cross mount points? This will have more coverages with __path_walk_parent().
Quoted 39 lines
fs/namei.c +39
-0+ +in_root: +if (unlikely (flags &LOOKUP_BENEATH )) +return ERR_PTR (-EXDEV ); +return dget (path->dentry); +} + +/** +* path_walk_parent - Walk to the parent of path +* @path: input and output path. +* @root: root of the path walk, do not go beyond this root. If @root is +* zero'ed, walk all the way to real root. +* +* Given a path, find the parent path. Replace @path with the parent path. +* If we were already at the real root or a disconnected root, @path is +* not changed. +* +* Returns: +* 0 - if @path is updated to its parent. +* <0 - if @path is already the root (real root or @root). +*/ +int path_walk_parent (struct path *path,const struct path *root) +{ +struct dentry *parent; + + parent =__path_walk_parent (path, root,LOOKUP_BENEATH ); + +if (IS_ERR (parent)) +return PTR_ERR (parent); + +if (parent == path->dentry) { +dput (parent); +return -ENOENT ; + } +dput (path->dentry); + path->dentry = parent; +return 0 ; +} +
[...]
Re: [PATCH v5 bpf-next 1/5] namei: Introduce new helper function path_walk_parent()
Quoted 3 lines · Yonghong Song
[...]
Quoted 80 lines
Quoted 18 lines
fs/namei.c +6
-8+static struct dentry *__path_walk_parent(struct path *path, const struct path *root, int flags) { -struct dentry *parent; - -if (path_equal (&nd->path, &nd->root)) + if (path_equal(path, root)) goto in_root; -if (unlikely (nd->path.dentry == nd->path.mnt->mnt_root)) { -struct path path; + if (unlikely(path->dentry == path->mnt->mnt_root)) { + struct path new_path; - if (!choose_mountpoint(real_mount(nd->path.mnt), - &nd->root, &path)) + if (!choose_mountpoint(real_mount(path->mnt), + root, &new_path)) goto in_root; -path_put (&nd->path); - nd->path = path;fs/namei.c static struct dentry *follow_dotdot_rcu(struct nameidata *nd) · line 2088
Quoted 10 lines
fs/namei.c +4
-3- nd->inode = path.dentry->d_inode; -if (unlikely (nd->flags &LOOKUP_NO_XDEV )) +path_put (path); + *path = new_path; +if (unlikely (flags &LOOKUP_NO_XDEV ))return ERR_PTR (-EXDEV ); }/* rare case of legitimate dget_parent()... */ - parent =dget_parent (nd->path.dentry); +return dget_parent (path->dentry);I have some confusion with this patch when crossing mount boundary.
In
d_path.c, we havestatic int __prepend_path(const struct dentry *dentry, const struct mount *mnt,
const struct path *root, structprepend_buffer*p){ while (dentry != root->dentry || &mnt->mnt != root->mnt) { const struct dentry *parent = READ_ONCE(dentry->d_parent); if (dentry == mnt->mnt.mnt_root) { struct mount *m = READ_ONCE(mnt->mnt_parent); struct mnt_namespace *mnt_ns; if (likely(mnt != m)) { dentry = READ_ONCE(mnt->mnt_mountpoint); mnt = m; continue; } /* Global root */ mnt_ns = READ_ONCE(mnt->mnt_ns); /* open-coded is_mounted() to use local mnt_ns */ if (!IS_ERR_OR_NULL(mnt_ns) && !is_anon_ns(mnt_ns)) return 1; // absolute root else return 2; // detached or not attached yet } if (unlikely(dentry == parent)) /* Escaped? */ return 3; prefetch(parent); if (!prepend_name(p, &dentry->d_name)) break; dentry = parent; } return 0; }At the mount boundary and not at root mount, the code has dentry =
READ_ONCE(mnt->mnt_mountpoint);
mnt = m; /* 'mnt' will be parent mount */ continue;After that, we have
const struct dentry *parent =READ_ONCE(dentry->d_parent); if (dentry ==mnt->mnt.mnt_root) {
/* assume this is false */
}
...
Quoted 6 lines
prefetch(parent); if (!prepend_name(p, &dentry->d_name)) break; dentry = parent;So the
prepend_name(p, &dentry->d_name)is actually frommnt->mnt_mountpoint.
I am not quite following the question. In the code below:
if (dentry == mnt->mnt.mnt_root) {
struct mount *m = READ_ONCE(mnt->mnt_parent);
struct mnt_namespace *mnt_ns;
if (likely(mnt != m)) {
dentry = READ_ONCE(mnt->mnt_mountpoint);
mnt = m;
continue;/* We either continue, here */
}
/* Global root */
mnt_ns = READ_ONCE(mnt->mnt_ns);
/* open-coded is_mounted() to use local mnt_ns */
if (!IS_ERR_OR_NULL(mnt_ns) && !is_anon_ns(mnt_ns))
return 1; // absolute root
else
return 2; // detached or not attached yet
/* Or return here */
}So we will not hit prepend_name(). Does this answer the question?
Quoted 18 lines
In your above code, maybe we should return
path->dentryin the below if statement?if (unlikely(path->dentry == path->mnt->mnt_root)) { struct path new_path; if (!choose_mountpoint(real_mount(path->mnt), root, &new_path)) goto in_root; path_put(path); *path = new_path; if (unlikely(flags & LOOKUP_NO_XDEV)) return ERR_PTR(-EXDEV); + return path->dentry; } /* rare case of legitimate dget_parent()... */ return dget_parent(path->dentry);
Quoted 2 lines
Also, could you add some selftests cross mount points? This will have more coverages with
__path_walk_parent().
Yeah, I will try to add more tests in the next revision.
Thanks,
Song
Yonghong Song
reviewerRe: [PATCH v5 bpf-next 1/5] namei: Introduce new helper function path_walk_parent()
Quoted 111 lines · Song Liu
Quoted 2 lines · Yonghong Song
[...]
Quoted 79 lines
Quoted 18 lines
fs/namei.c +6
-8+static struct dentry *__path_walk_parent(struct path *path, const struct path *root, int flags) { -struct dentry *parent; - -if (path_equal (&nd->path, &nd->root)) + if (path_equal(path, root)) goto in_root; -if (unlikely (nd->path.dentry == nd->path.mnt->mnt_root)) { -struct path path; + if (unlikely(path->dentry == path->mnt->mnt_root)) { + struct path new_path; - if (!choose_mountpoint(real_mount(nd->path.mnt), - &nd->root, &path)) + if (!choose_mountpoint(real_mount(path->mnt), + root, &new_path)) goto in_root; -path_put (&nd->path); - nd->path = path;fs/namei.c static struct dentry *follow_dotdot_rcu(struct nameidata *nd) · line 2088
Quoted 10 lines
fs/namei.c +4
-3- nd->inode = path.dentry->d_inode; -if (unlikely (nd->flags &LOOKUP_NO_XDEV )) +path_put (path); + *path = new_path; +if (unlikely (flags &LOOKUP_NO_XDEV ))return ERR_PTR (-EXDEV ); }/* rare case of legitimate dget_parent()... */ - parent =dget_parent (nd->path.dentry); +return dget_parent (path->dentry);I have some confusion with this patch when crossing mount boundary.
In
d_path.c, we havestatic int __prepend_path(const struct dentry *dentry, const struct mount *mnt,
const struct path *root, structprepend_buffer*p){ while (dentry != root->dentry || &mnt->mnt != root->mnt) { const struct dentry *parent = READ_ONCE(dentry->d_parent); if (dentry == mnt->mnt.mnt_root) { struct mount *m = READ_ONCE(mnt->mnt_parent); struct mnt_namespace *mnt_ns; if (likely(mnt != m)) { dentry = READ_ONCE(mnt->mnt_mountpoint); mnt = m; continue; } /* Global root */ mnt_ns = READ_ONCE(mnt->mnt_ns); /* open-coded is_mounted() to use local mnt_ns */ if (!IS_ERR_OR_NULL(mnt_ns) && !is_anon_ns(mnt_ns)) return 1; // absolute root else return 2; // detached or not attached yet } if (unlikely(dentry == parent)) /* Escaped? */ return 3; prefetch(parent); if (!prepend_name(p, &dentry->d_name)) break; dentry = parent; } return 0; }At the mount boundary and not at root mount, the code has dentry =
READ_ONCE(mnt->mnt_mountpoint);
mnt = m; /* 'mnt' will be parent mount */ continue;After that, we have
const struct dentry *parent =READ_ONCE(dentry->d_parent); if (dentry ==mnt->mnt.mnt_root) {
/* assume this is false */
}
...Quoted 6 lines
prefetch(parent); if (!prepend_name(p, &dentry->d_name)) break; dentry = parent;So the
prepend_name(p, &dentry->d_name)is actually frommnt->mnt_mountpoint.I am not quite following the question. In the code below:
if (dentry == mnt->mnt.mnt_root) { struct mount *m = READ_ONCE(mnt->mnt_parent); struct mnt_namespace *mnt_ns; if (likely(mnt != m)) { dentry = READ_ONCE(mnt->mnt_mountpoint); mnt = m; continue;/* We either continue, here */
} /* Global root */ mnt_ns = READ_ONCE(mnt->mnt_ns); /* open-coded is_mounted() to use local mnt_ns */ if (!IS_ERR_OR_NULL(mnt_ns) && !is_anon_ns(mnt_ns)) return 1; // absolute root else return 2; // detached or not attached yet /* Or return here */ }
Quoted 22 lines
So we will not hit
prepend_name(). Does this answer the question?Quoted 19 lines
In your above code, maybe we should return
path->dentryin the below if statement?if (unlikely(path->dentry == path->mnt->mnt_root)) { struct path new_path; if (!choose_mountpoint(real_mount(path->mnt), root, &new_path)) goto in_root; path_put(path); *path = new_path; if (unlikely(flags & LOOKUP_NO_XDEV)) return ERR_PTR(-EXDEV); + return path->dentry; } /* rare case of legitimate dget_parent()... */ return dget_parent(path->dentry);Also, could you add some selftests cross mount points? This will have more coverages with
__path_walk_parent().
Looks like __path_walk_parent() works for the root of mounted fs. If this is the case, the implementation is correct. It could be good to add some comments to clarify.
Quoted 5 lines
Yeah, I will try to add more tests in the next revision.
Thanks,
Song
Song Liu
reviewerPATCH v5 2/5 landlock: Use path_walk_parent()
In reply to Song LiuUse path_walk_parent() to walk a path up to its parent.
No functional changes intended.
--- security/landlock/fs.c | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-)
security/landlock/fs.c +8 -22
@@ -837,8 +837,8 @@ static bool is_access_to_paths_allowed(* restriction. */ while (true) { -struct dentry *parent_dentry;const struct landlock_rule *rule; +struct path root = {};/* * If at least all accesses allowed on the destination are
@@ -895,34 +895,20 @@ static bool is_access_to_paths_allowed(/* Stops when a rule from each layer grants access. */ if (allowed_parent1 && allowed_parent2)break ; -jump_up: -if (walker_path.dentry == walker_path.mnt->mnt_root) { -if (follow_up (&walker_path)) { -/* Ignores hidden mount points. */ - goto jump_up; - }else { -/* -* Stops at the real root. Denies access -* because not all layers have granted access. -*/ -break ; - } - } -if (unlikely (IS_ROOT (walker_path.dentry))) { + +if (unlikely (IS_ROOT (walker_path.dentry)) && + (walker_path.mnt->mnt_flags &MNT_INTERNAL )) {/* * Stops at disconnected root directories. Only allows * access to internal filesystems (e.g. nsfs, which is * reachable through /proc/<pid>/ns/<namespace>). */ -if (walker_path.mnt->mnt_flags &MNT_INTERNAL ) { - allowed_parent1 = true; - allowed_parent2 = true; - } + allowed_parent1 = true; + allowed_parent2 = true;break ; } - parent_dentry =dget_parent (walker_path.dentry); -dput (walker_path.dentry); - walker_path.dentry = parent_dentry; +if (path_walk_parent (&walker_path, &root)) +break ; }path_put (&walker_path);
Signature · 1 line
2.47.1
Mickaël Salaün
maintainerRe: [PATCH v5 bpf-next 2/5] landlock: Use path_walk_parent()
Quoted 3 lines · Song Liu
Use
path_walk_parent()to walk a path up to its parent.No functional changes intended.
Using this helper actualy fixes the issue highlighted by Al. Even if it was reported after the first version of this patch series, the issue should be explained in the commit message and these tags should be added:
I like this new helper but we should have a clear plan to be able to call such helper in a RCU read-side critical section before we merge this series. We're still waiting for Christian.
I sent a patch to fix the handling of disconnected directories for Landlock, and it will need to be backported:
https://lore.kernel.org/all/[email protected]/
Unfortunately a rebase would be needed for the path_walk_parent patch, but I can take it in my tree if everyone is OK.
However, users of path_walk_parent() would still have to properly deal with such disconnected directories. The Landlock fix I sent takes a safe approach by handling disconnected directories such as only their mount point is actually taken into account for access control decision (see rationale in the patch series). I'm wondering if path_walk_parent() should not help its users avoid the same issue, or at least force them to make an explicit and informed choice.
Quoted 31 lines
Signed-off-by Song Liu <[email protected]>--- security/landlock/fs.c | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-)security/landlock/fs.c +1
-8@@ -837,8 +837,8 @@ static bool is_access_to_paths_allowed(* restriction. */ while (true) { -struct dentry *parent_dentry;const struct landlock_rule *rule; +struct path root = {};/* * If at least all accesses allowed on the destination are @@ -895,34 +895,20 @@ static bool is_access_to_paths_allowed(/* Stops when a rule from each layer grants access. */ if (allowed_parent1 && allowed_parent2)break ; -jump_up: -if (walker_path.dentry == walker_path.mnt->mnt_root) { -if (follow_up (&walker_path)) { -/* Ignores hidden mount points. */ - goto jump_up; - }else { -/*
security/landlock/fs.c static bool is_access_to_paths_allowed( · line 900
Quoted 10 lines
security/landlock/fs.c +3
-7-* Stops at the real root. Denies access -* because not all layers have granted access. -*/ -break ; - } - } -if (unlikely (IS_ROOT (walker_path.dentry))) { + +if (unlikely (IS_ROOT (walker_path.dentry)) && + (walker_path.mnt->mnt_flags &MNT_INTERNAL )) {
This would not fit well with the ongoing Landlock fix because !MNT_INTERNAL root directories should also be handled specifically, but only if they are not mount points.
Quoted 25 lines
security/landlock/fs.c +4
-7/* * Stops at disconnected root directories. Only allows * access to internal filesystems (e.g. nsfs, which is * reachable through /proc/<pid>/ns/<namespace>). */ -if (walker_path.mnt->mnt_flags &MNT_INTERNAL ) { - allowed_parent1 = true; - allowed_parent2 = true; - } + allowed_parent1 = true; + allowed_parent2 = true;break ; } - parent_dentry =dget_parent (walker_path.dentry); -dput (walker_path.dentry); - walker_path.dentry = parent_dentry; +if (path_walk_parent (&walker_path, &root)) +break ; }path_put (&walker_path);Signature · 1 line
2.47.1
Re: [PATCH v5 bpf-next 2/5] landlock: Use path_walk_parent()
Hi Mickaël,
Quoted 20 lines · Mickaël Salaün
Quoted 3 lines · Song Liu
Use
path_walk_parent()to walk a path up to its parent.No functional changes intended.
Using this helper actualy fixes the issue highlighted by Al. Even if it was reported after the first version of this patch series, the issue should be explained in the commit message and these tags should be added:
Reported-by Al Viro <[email protected]>Fixes cb2c7d1a1776 ("landlock: Support filesystem access-control")I like this new helper but we should have a clear plan to be able to call such helper in a RCU read-side critical section before we merge this series. We're still waiting for Christian.
Quoted 5 lines
I sent a patch to fix the handling of disconnected directories for Landlock, and it will need to be backported:
https://lore.kernel.org/all/[email protected]/
Unfortunately a rebase would be needed for thepath_walk_parentpatch, but I can take it in my tree if everyone is OK.
The fix above also touches VFS code (makes path_connected available out of namei.c. It probably should also go through VFS tree?
Maybe you can send 1/5 and 2/5 of this set (with necessary changes) and your fix together to VFS tree. Then, I will see how to route the BPF side patches.
Thanks,
Song
Mickaël Salaün
maintainerRe: [PATCH v5 bpf-next 2/5] landlock: Use path_walk_parent()
Quoted 28 lines · Song Liu
Hi Mickaël,
Quoted 20 lines · Mickaël Salaün
Quoted 3 lines · Song Liu
Use
path_walk_parent()to walk a path up to its parent.No functional changes intended.
Using this helper actualy fixes the issue highlighted by Al. Even if it was reported after the first version of this patch series, the issue should be explained in the commit message and these tags should be added:
Reported-by Al Viro <[email protected]>Fixes cb2c7d1a1776 ("landlock: Support filesystem access-control")I like this new helper but we should have a clear plan to be able to call such helper in a RCU read-side critical section before we merge this series. We're still waiting for Christian.
Quoted 5 lines
I sent a patch to fix the handling of disconnected directories for Landlock, and it will need to be backported:
https://lore.kernel.org/all/[email protected]/
Unfortunately a rebase would be needed for thepath_walk_parentpatch, but I can take it in my tree if everyone is OK.
Quoted 6 lines
The fix above also touches VFS code (makes
path_connectedavailable out ofnamei.c. It probably should also go through VFS tree?Maybe you can send 1/5 and 2/5 of this set (with necessary changes) and your fix together to VFS tree. Then, I will see how to route the BPF side patches.
That could work, but because it would be much more Landlock-specific code than VFS-specific code, and there will probably be a few versions of my fixes, I'd prefer to keep this into my tree if VFS folks are OK. BTW, my fixes already touch the VFS subsystem a bit.
However, as pointed out in my previous email, the disconnected directory case should be carefully considered for the path_walk_parent() users to avoid BPF LSM programs having the same issue I'm fixing for Landlock. The safe approaches I can think of to avoid this issue for BPF programs while making the interface efficient (by not calling path_connected() after each path_walk_parent() call) is to either have some kind of iterator as Tingmao proposed, or a callback function as Neil proposed. The callback approach looks simpler and more future-proof, but I guess you'll have to make it compatible with the eBPF runtime. I think the best approach would be to have a VFS API with a callback, and a BPF helper (leveraging this VFS API) with an iterator state.
I'm aware that this disconnected directory fix might delay your patch series, but the good news is that it's an opportunity for eBPF programs to not have the issue I'm fixing for Landlock.
Quoted 5 lines
Thanks,
Song
Re: [PATCH v5 bpf-next 2/5] landlock: Use path_walk_parent()
Quoted 45 lines · Mickaël Salaün
Quoted 28 lines · Song Liu
Hi Mickaël,
Quoted 20 lines · Mickaël Salaün
Quoted 3 lines · Song Liu
Use
path_walk_parent()to walk a path up to its parent.No functional changes intended.
Using this helper actualy fixes the issue highlighted by Al. Even if it was reported after the first version of this patch series, the issue should be explained in the commit message and these tags should be added:
Reported-by Al Viro <[email protected]>Fixes cb2c7d1a1776 ("landlock: Support filesystem access-control")I like this new helper but we should have a clear plan to be able to call such helper in a RCU read-side critical section before we merge this series. We're still waiting for Christian.
Quoted 5 lines
I sent a patch to fix the handling of disconnected directories for Landlock, and it will need to be backported:
https://lore.kernel.org/all/[email protected]/
Unfortunately a rebase would be needed for thepath_walk_parentpatch, but I can take it in my tree if everyone is OK.Quoted 6 lines
The fix above also touches VFS code (makes
path_connectedavailable out ofnamei.c. It probably should also go through VFS tree?Maybe you can send 1/5 and 2/5 of this set (with necessary changes) and your fix together to VFS tree. Then, I will see how to route the BPF side patches.
That could work, but because it would be much more Landlock-specific code than VFS-specific code, and there will probably be a few versions of my fixes, I'd prefer to keep this into my tree if VFS folks are OK. BTW, my fixes already touch the VFS subsystem a bit.
Quoted 11 lines
However, as pointed out in my previous email, the disconnected directory case should be carefully considered for the
path_walk_parent()users to avoid BPF LSM programs having the same issue I'm fixing for Landlock. The safe approaches I can think of to avoid this issue for BPF programs while making the interface efficient (by not callingpath_connected()after eachpath_walk_parent()call) is to either have some kind of iterator as Tingmao proposed, or a callback function as Neil proposed. The callback approach looks simpler and more future-proof, but I guess you'll have to make it compatible with the eBPF runtime. I think the best approach would be to have a VFS API with a callback, and a BPF helper (leveraging this VFS API) with an iterator state.
Since we are proposing an open-coded BPF iterator. Having a real callback, which is no longer an open coded iterator, requires more work. At the moment, it is easier to just add a path_connected call in bpf_iter_path_next().
Thanks,
Song
Christian Brauner
maintainerRe: [PATCH v5 bpf-next 2/5] landlock: Use path_walk_parent()
In reply to Mickaël SalaünQuoted 32 lines · Mickaël Salaün
Quoted 31 lines · Song Liu
Hi Mickaël,
Quoted 20 lines · Mickaël Salaün
Quoted 3 lines · Song Liu
Use
path_walk_parent()to walk a path up to its parent.No functional changes intended.
Using this helper actualy fixes the issue highlighted by Al. Even if it was reported after the first version of this patch series, the issue should be explained in the commit message and these tags should be added:
Reported-by Al Viro <[email protected]>Fixes cb2c7d1a1776 ("landlock: Support filesystem access-control")I like this new helper but we should have a clear plan to be able to call such helper in a RCU read-side critical section before we merge this series. We're still waiting for Christian.
Quoted 5 lines
I sent a patch to fix the handling of disconnected directories for Landlock, and it will need to be backported:
https://lore.kernel.org/all/[email protected]/
Unfortunately a rebase would be needed for thepath_walk_parentpatch, but I can take it in my tree if everyone is OK.The fix above also touches VFS code (makes
path_connectedavailable out ofnamei.c. It probably should also go through VFS tree?
Quoted 8 lines
Quoted 3 lines
Maybe you can send 1/5 and 2/5 of this set (with necessary changes) and your fix together to VFS tree. Then, I will see how to route the BPF side patches.
That could work, but because it would be much more Landlock-specific code than VFS-specific code, and there will probably be a few versions of my fixes, I'd prefer to keep this into my tree if VFS folks are OK. BTW, my fixes already touch the VFS subsystem a bit.
Under specific circumstances we will accept very minor changes to VFS code to go through selected other trees depending on the amount of trust between the respective trees. Afaict, your series just exports a function. I'll take a look at it.
Song Liu
reviewerPATCH v5 3/5 bpf: Introduce path iterator
In reply to Song LiuIntroduce a path iterator, which walks a struct path toward the root. This path iterator is based on path_walk_parent. A fixed zero'ed root is passed to path_walk_parent(). Therefore, unless the user terminates it earlier, the iterator will terminate at the real root.
--- fs/bpf_fs_kfuncs.c | 72 +++++++++++++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 5 +++ 2 files changed, 77 insertions(+)
fs/bpf_fs_kfuncs.c +72 -0
@@ -10,6 +10,7 @@#include <linux/fsnotify.h> #include <linux/file.h> #include <linux/mm.h> +#include <linux/namei.h> #include <linux/xattr.h> __bpf_kfunc_start_defs ();
@@ -324,6 +325,74 @@ __bpf_kfunc int bpf_remove_dentry_xattr(struct dentry *dentry, const char *name___bpf_kfunc_end_defs (); +/* open-coded path iterator */ +struct bpf_iter_path { +__u64 __opaque[2 ]; +}__aligned(8) ; + +struct bpf_iter_path_kern { +struct path path; +}__aligned(8) ; + +__bpf_kfunc_start_defs (); + +__bpf_kfunc int bpf_iter_path_new(struct bpf_iter_path *it, +struct path *start, +__u64 flags) +{ +struct bpf_iter_path_kern *kit = (void *)it; + +BUILD_BUG_ON (sizeof (*kit) >sizeof (*it)); + BUILD_BUG_ON(__alignof__(*kit) != __alignof__(*it)); + +if (flags) { +/* +* _destroy() is still called when _new() fails. Zero +* kit->path so that it be passed to path_put() safely. +* Note: path_put() is no-op for zero'ed path. +*/ +memset (&kit->path,0 ,sizeof (struct path )); +return -EINVAL ; + } + + kit->path = *start; +path_get (&kit->path); + +return 0 ; +} + +__bpf_kfunc struct path *bpf_iter_path_next(struct bpf_iter_path *it) +{ +struct bpf_iter_path_kern *kit = (void *)it; +struct path root = {}; + +/* +* "root" is zero'ed. Therefore, unless the loop is explicitly +* terminated, bpf_iter_path_next() will continue looping until +* we've reached the global root of the VFS. +* +* If a root of walk is needed, the user can check "path" against +* that root on each iteration. +*/ +if (path_walk_parent (&kit->path, &root)) +return NULL ; + +return &kit->path; +} + +__bpf_kfunc void bpf_iter_path_destroy(struct bpf_iter_path *it) +{ +struct bpf_iter_path_kern *kit = (void *)it; + +/* +* kit->path might be zero'ed, but this is OK because path_put() +* is no-op for zero'ed struct path +*/ +path_put (&kit->path); +} + +__bpf_kfunc_end_defs (); + BTF_KFUNCS_START(bpf_fs_kfunc_set_ids) BTF_ID_FLAGS(func, bpf_get_task_exe_file, KF_ACQUIRE | KF_TRUSTED_ARGS | KF_RET_NULL)
@@ -333,6 +402,9 @@ BTF_ID_FLAGS(func, bpf_get_dentry_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_get_file_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_set_dentry_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_remove_dentry_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_iter_path_new, KF_ITER_NEW | KF_TRUSTED_ARGS | KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_iter_path_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_iter_path_destroy, KF_ITER_DESTROY | KF_SLEEPABLE)BTF_KFUNCS_END (bpf_fs_kfunc_set_ids) static int bpf_fs_kfuncs_filter(const struct bpf_prog *prog, u32 kfunc_id)
kernel/bpf/verifier.c +5 -0
@@ -7101,6 +7101,10 @@ BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct socket) {struct sock *sk; }; +BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct path) { +struct dentry *dentry; +}; +static bool type_is_rcu (struct bpf_verifier_env *env,struct bpf_reg_state *reg,const char *field_name,u32 btf_id)
@@ -7141,6 +7145,7 @@ static bool type_is_trusted_or_null(struct bpf_verifier_env *env, const char *field_name, u32 btf_id) {BTF_TYPE_EMIT (BTF_TYPE_SAFE_TRUSTED_OR_NULL (struct socket )); +BTF_TYPE_EMIT (BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct path ));return btf_nested_type_is_trusted (&env->log, reg, field_name, btf_id,"__safe_trusted_or_null" );
Signature · 1 line
2.47.1
Song Liu
reviewerPATCH v5 4/5 selftests/bpf: Add tests for bpf path iterator
In reply to Song LiuAdd tests for bpf path iterator, including test cases similar to real workload (call bpf_path_d_path and bpf_get_dentry_xattr), and test cases where the verifier rejects invalid use of the iterator.
--- .../testing/selftests/bpf/bpf_experimental.h | 6 + .../selftests/bpf/prog_tests/path_iter.c | 12 ++ tools/testing/selftests/bpf/progs/path_iter.c | 145 ++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/path_iter.c create mode 100644 tools/testing/selftests/bpf/progs/path_iter.c
tools/testing/selftests/bpf/bpf_experimental.h +6 -0
@@ -596,4 +596,10 @@ extern int bpf_iter_dmabuf_new(struct bpf_iter_dmabuf *it) __weak __ksym;extern struct dma_buf *bpf_iter_dmabuf_next (struct bpf_iter_dmabuf *it)__weak __ksym; extern void bpf_iter_dmabuf_destroy(struct bpf_iter_dmabuf *it) __weak __ksym; +struct bpf_iter_path ; +extern int bpf_iter_path_new(struct bpf_iter_path *it, struct path *start, + __u64 flags) __weak __ksym; +extern struct path *bpf_iter_path_next (struct bpf_iter_path *it)__weak __ksym; +extern void bpf_iter_path_destroy(struct bpf_iter_path *it) __weak __ksym; +#endif
tools/testing/selftests/bpf/prog_tests/path_iter.c new file +12 -0
@@ -0,0 +1,12 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ + +#include <test_progs.h> +#include <bpf/libbpf.h> +#include <bpf/btf.h> +#include "path_iter.skel.h" + +void test_path_iter (void ) +{ +RUN_TESTS (path_iter); +}
tools/testing/selftests/bpf/progs/path_iter.c new file +145 -0
145 lines not on this page · raw message
Signature · 1 line
2.47.1
Song Liu
reviewerPATCH v5 5/5 selftests/bpf: Path walk test
In reply to Song LiuAdd an end-to-end test with path_iter on security hook file_open.
A test file is created in folder /tmp/test_progs_path_iter/folder. On file_open, walk file->f_path up to its parent and grand parent, and test bpf_get_dentry_xattr and bpf_path_d_path on the folders.
--- .../selftests/bpf/prog_tests/path_iter.c | 99 +++++++++++++++++++ tools/testing/selftests/bpf/progs/path_walk.c | 59 +++++++++++ 2 files changed, 158 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/path_walk.c
tools/testing/selftests/bpf/prog_tests/path_iter.c +99 -0
@@ -2,11 +2,110 @@/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ #include <test_progs.h> +#include <fcntl.h> #include <bpf/libbpf.h> #include <bpf/btf.h> +#include <sys/stat.h> +#include <sys/xattr.h> +#include "path_iter.skel.h" +#include "path_walk.skel.h" + +static const char grand_parent_path[] ="/tmp/test_progs_path_iter" ; +static const char parent_path[] ="/tmp/test_progs_path_iter/folder" ; +static const char file_path[] ="/tmp/test_progs_path_iter/folder/file" ; +static const char xattr_name[] ="user.bpf.selftests" ; +static const char xattr_value[] ="selftest_path_iter" ; + +static void cleanup_files (void ) +{ +remove (file_path); +rmdir (parent_path); +rmdir (grand_parent_path); +} + +static int setup_files_and_xattrs (void ) +{ +int ret =-1 ; + +/* create test folders */ +if (mkdir (grand_parent_path,0755 )) + goto error; +if (mkdir (parent_path,0755 )) + goto error; + +/* setxattr for test folders */ + ret =setxattr (grand_parent_path, xattr_name, + xattr_value,sizeof (xattr_value),0 ); +if (ret <0 ) { +/* return errno, so that we can handle EOPNOTSUPP in the caller */ + ret = errno; + goto error; + } + ret =setxattr (parent_path, xattr_name, + xattr_value,sizeof (xattr_value),0 ); +if (ret <0 ) { +/* return errno, so that we can handle EOPNOTSUPP in the caller */ + ret = errno; + goto error; + } + +return 0 ; +error: +cleanup_files (); +return ret; +} + +static void test_path_walk (void ) +{ +struct path_walk *skel =NULL ; +int file_fd; +int err; + + err =setup_files_and_xattrs (); +if (err ==EOPNOTSUPP ) { +printf ("%s:SKIP:local fs doesn't support xattr (%d)\n" +"To run this test, make sure /tmp filesystem supports xattr.\n" , + __func__, errno); +test__skip (); +return ; + } + +if (!ASSERT_OK (err,"setup_file" )) +return ; + + skel =path_walk__open_and_load (); +if (!ASSERT_OK_PTR (skel,"path_walk__open_and_load" )) + goto cleanup; + + skel->bss->monitored_pid =getpid (); +if (!ASSERT_OK (path_walk__attach (skel),"path_walk__attach" )) + goto cleanup; + + file_fd =open (file_path,O_CREAT ); +if (!ASSERT_OK_FD (file_fd,"open_file" )) + goto cleanup; +close (file_fd); + +ASSERT_OK (strncmp (skel->bss->parent_xattr_buf, xattr_value,strlen (xattr_value)), +"parent_xattr" ); +ASSERT_OK (strncmp (skel->bss->grand_parent_xattr_buf, xattr_value,strlen (xattr_value)), +"grand_parent_xattr" ); + +ASSERT_OK (strncmp (skel->bss->parent_path_buf, parent_path,strlen (parent_path)), +"parent_d_path" ); +ASSERT_OK (strncmp (skel->bss->grand_parent_path_buf, grand_parent_path, +strlen (grand_parent_path)), +"grand_parent_d_path" ); + +cleanup: +path_walk__destroy (skel); +cleanup_files (); +}void test_path_iter (void ) {RUN_TESTS (path_iter); +if (test__start_subtest ("path_walk_example" )) +test_path_walk (); }
tools/testing/selftests/bpf/progs/path_walk.c new file +59 -0
59 lines not on this page · raw message
Signature · 1 line
2.47.1
Song Liu
reviewerHi Christian, Mickaël, and folks,
Could you please share your comments on this version? Does this look sane?
Thanks,
Song
Quoted 7 lines · Song Liu
In security use cases, it is common to apply rules to VFS subtrees. However, filtering files in a subtree is not straightforward [1].
One solution to this problem is to start from a path and walk up the VFS tree (towards the root). Among in-tree LSMs, Landlock uses this solution.
[...]
Mickaël Salaün
maintainerQuoted 4 lines · Song Liu
Hi Christian, Mickaël, and folks,
Could you please share your comments on this version? Does this look sane?
This looks good to me but we need to know what is the acceptable next step to support RCU. If we can go with another _rcu helper, I'm good with the current approach, otherwise we need to figure out a way to leverage the current helper to make it compatible with callers being in a RCU read-side critical section while leveraging safe path walk (i.e. several calls to path_walk_parent).
Quoted 15 lines
Thanks,
SongQuoted 7 lines · Song Liu
In security use cases, it is common to apply rules to VFS subtrees. However, filtering files in a subtree is not straightforward [1].
One solution to this problem is to start from a path and walk up the VFS tree (towards the root). Among in-tree LSMs, Landlock uses this solution.
[...]
Quoted 6 lines · Mickaël Salaün
Quoted 4 lines · Song Liu
Hi Christian, Mickaël, and folks,
Could you please share your comments on this version? Does this look sane?
Quoted 6 lines
This looks good to me but we need to know what is the acceptable next step to support RCU. If we can go with another
_rcuhelper, I'm good with the current approach, otherwise we need to figure out a way to leverage the current helper to make it compatible with callers being in a RCU read-side critical section while leveraging safe path walk (i.e. several calls topath_walk_parent).
Can you spell out the minimum that you need?
My vague impression is that you want to search up from a given strut path, no further then some other given path, looking for a dentry that matches some rule. Is that correct?
In general, the original dentry could be moved away from under the dentry you find moments after the match is reported. What mechanisms do you have in place to ensure this doesn't happen, or that it doesn't matter?
Would it be sufficient to have an iterator which reported successive ancestors in turn, or reported that you need to restart because something changed? Would you need to know that a restart happened or would it be acceptable to transparently start again at the parent of the starting point?
Or do you really need a "one step at a time" interface?
Do you need more complex movements around the tree, or is just walking up sufficient?
If this has been discussed or documented elsewhere I'd be happy for you just to provide a reference, and I can come back with follow-up questions if needed.
Thanks,
NeilBrown
Quoted 17 lines
Quoted 16 lines
Thanks,
SongQuoted 7 lines · Song Liu
In security use cases, it is common to apply rules to VFS subtrees. However, filtering files in a subtree is not straightforward [1].
One solution to this problem is to start from a path and walk up the VFS tree (towards the root). Among in-tree LSMs, Landlock uses this solution.
[...]
Mickaël Salaün
maintainerQuoted 7 lines · NeilBrown
Quoted 6 lines · Mickaël Salaün
Quoted 4 lines · Song Liu
Hi Christian, Mickaël, and folks,
Could you please share your comments on this version? Does this look sane?
Quoted 8 lines
Quoted 6 lines
This looks good to me but we need to know what is the acceptable next step to support RCU. If we can go with another
_rcuhelper, I'm good with the current approach, otherwise we need to figure out a way to leverage the current helper to make it compatible with callers being in a RCU read-side critical section while leveraging safe path walk (i.e. several calls topath_walk_parent).Can you spell out the minimum that you need?
Sure. We'd like to call this new helper in a RCU
read-side critical section and leverage this capability to speed up path walk when there is no concurrent hierarchy modification. This use case is similar to handle_dots() with LOOKUP_RCU calling follow_dotdot_rcu().
The main issue with this approach is to keep some state of the path walk to know if the next call to "path_walk_parent_rcu()" would be valid (i.e. something like a very light version of nameidata, mainly sequence integers), and to get back to the non-RCU version otherwise.
Quoted 4 lines
My vague impression is that you want to search up from a given strut path, no further then some other given path, looking for a dentry that matches some rule. Is that correct?
Yes
Quoted 5 lines
In general, the original dentry could be moved away from under the dentry you find moments after the match is reported. What mechanisms do you have in place to ensure this doesn't happen, or that it doesn't matter?
In the case of Landlock, by default, a set of access rights are denied and can only be allowed by an element in the file hierarchy. The goal is to only allow access to files under a specific directory (or directly a specific file). That's why we only care of the file hierarchy at the time of access check. It's not an issue if the file/directory was moved or is being moved as long as we can walk its "current" hierarchy. Furthermore, a sandboxed process is restricted from doing arbitrary mounts (and renames/links are controlled with the LANDLOCK_ACCESS_FS_REFER right).
However, we need to get a valid "snapshot" of the set of dentries that (could) lead to the evaluated file/directory.
Quoted 6 lines
Would it be sufficient to have an iterator which reported successive ancestors in turn, or reported that you need to restart because something changed? Would you need to know that a restart happened or would it be acceptable to transparently start again at the parent of the starting point?
If the path walk is being invalidated, we need to reset the collected access right and start again the path walk to get all the access rights from a consistent/real file hierarchy.
Quoted 2 lines
Or do you really need a "one step at a time" interface?
We need to check each component of the path walk, so either we call an helper to get each of them and we do our check after that (we should be able to do that in RCU), or we provide a callback function which is called by the path walk helper.
Quoted 3 lines
Do you need more complex movements around the tree, or is just walking up sufficient?
Just walking up.
Quoted 4 lines
If this has been discussed or documented elsewhere I'd be happy for you just to provide a reference, and I can come back with follow-up questions if needed.
Tingmao initially described the goal here:
https://lore.kernel.org/all/[email protected]/
and she sent an RFC to illustrate that:
https://lore.kernel.org/all/[email protected]/
The discussion mainly raised two questions:
- Should we have one or two APIs?
- How to store the state of the walk without exposing VFS internals to the rest of the kernel?
Thanks
Quoted 24 lines
Thanks,
NeilBrownQuoted 17 lines
Quoted 16 lines
Thanks,
SongQuoted 7 lines · Song Liu
In security use cases, it is common to apply rules to VFS subtrees. However, filtering files in a subtree is not straightforward [1].
One solution to this problem is to start from a path and walk up the VFS tree (towards the root). Among in-tree LSMs, Landlock uses this solution.
[...]
Quoted 37 lines · Mickaël Salaün
Quoted 2 lines · NeilBrown
Can you spell out the minimum that you need?
Sure. We'd like to call this new helper in a RCU
read-side critical section and leverage this capability to speed up path walk when there is no concurrent hierarchy modification. This use case is similar tohandle_dots()withLOOKUP_RCUcallingfollow_dotdot_rcu().The main issue with this approach is to keep some state of the path walk to know if the next call to "
path_walk_parent_rcu()" would be valid (i.e. something like a very light version of nameidata, mainly sequence integers), and to get back to the non-RCU version otherwise.Quoted 4 lines
My vague impression is that you want to search up from a given strut path, no further then some other given path, looking for a dentry that matches some rule. Is that correct?
Yes
Quoted 5 lines
In general, the original dentry could be moved away from under the dentry you find moments after the match is reported. What mechanisms do you have in place to ensure this doesn't happen, or that it doesn't matter?
In the case of Landlock, by default, a set of access rights are denied and can only be allowed by an element in the file hierarchy. The goal is to only allow access to files under a specific directory (or directly a specific file). That's why we only care of the file hierarchy at the time of access check. It's not an issue if the file/directory was moved or is being moved as long as we can walk its "current" hierarchy. Furthermore, a sandboxed process is restricted from doing arbitrary mounts (and renames/links are controlled with the
LANDLOCK_ACCESS_FS_REFERright).
Quoted 2 lines
However, we need to get a valid "snapshot" of the set of dentries that (could) lead to the evaluated file/directory.
A "snapshot" is an interesting idea - though looking at the landlock code you one need inodes, not dentries.
I imagine an interface where you give it a starting path, a root, and and array of inode pointers, and it fills in the pointers with the path
- all under rcu so no references are needed.
But you would need some fallback if the array isn't big enough, so maybe that isn't a good idea.
Based on the comments by Al and Christian, I think the only viable approach is to pass a callback to some vfs function that does the walking.
vfs_walk_ancestors(struct path *path, struct path *root,
int (*walk_cb)(struct path *ancestor, void *data), void *data)
where walk_cb() returns a negative number if it wants to abort, and is given a NULL ancestor if vfs_walk_ancestors() needed to restart.
vfs_walk_ancestors() would initialise a "struct nameidata" and effectively call handle_dots(&nd, LAST_DOTDOT) repeatedly, calling
walk_cb(&nd.path, data)
each time.
How would you feel about that sort of interface?
NeilBrown
Song Liu
reviewerQuoted 50 lines · NeilBrown
Quoted 37 lines · Mickaël Salaün
Quoted 2 lines · NeilBrown
Can you spell out the minimum that you need?
Sure. We'd like to call this new helper in a RCU
read-side critical section and leverage this capability to speed up path walk when there is no concurrent hierarchy modification. This use case is similar tohandle_dots()withLOOKUP_RCUcallingfollow_dotdot_rcu().The main issue with this approach is to keep some state of the path walk to know if the next call to "
path_walk_parent_rcu()" would be valid (i.e. something like a very light version of nameidata, mainly sequence integers), and to get back to the non-RCU version otherwise.Quoted 4 lines
My vague impression is that you want to search up from a given strut path, no further then some other given path, looking for a dentry that matches some rule. Is that correct?
Yes
Quoted 5 lines
In general, the original dentry could be moved away from under the dentry you find moments after the match is reported. What mechanisms do you have in place to ensure this doesn't happen, or that it doesn't matter?
In the case of Landlock, by default, a set of access rights are denied and can only be allowed by an element in the file hierarchy. The goal is to only allow access to files under a specific directory (or directly a specific file). That's why we only care of the file hierarchy at the time of access check. It's not an issue if the file/directory was moved or is being moved as long as we can walk its "current" hierarchy. Furthermore, a sandboxed process is restricted from doing arbitrary mounts (and renames/links are controlled with the
LANDLOCK_ACCESS_FS_REFERright).Quoted 2 lines
However, we need to get a valid "snapshot" of the set of dentries that (could) lead to the evaluated file/directory.
A "snapshot" is an interesting idea - though looking at the landlock code you one need inodes, not dentries.
I imagine an interface where you give it a starting path, a root, and and array of inode pointers, and it fills in the pointers with the path
- all under rcu so no references are needed.
But you would need some fallback if the array isn't big enough, so maybe that isn't a good idea.
Quoted 7 lines
Based on the comments by Al and Christian, I think the only viable approach is to pass a callback to some vfs function that does the walking.
vfs_walk_ancestors(struct path *path, struct path *root,
int (*walk_cb)(struct path *ancestor, void *data), void *data)
I like this idea.
Maybe we want "struct path *ancestor" of walk_cb to be const. walk_cb should only change "data", so that we can undo all the changes when the rcu walk fails.
Thanks,
Song
Quoted 57 lines · NeilBrown
Quoted 37 lines · Mickaël Salaün
Quoted 2 lines · NeilBrown
Can you spell out the minimum that you need?
Sure. We'd like to call this new helper in a RCU
read-side critical section and leverage this capability to speed up path walk when there is no concurrent hierarchy modification. This use case is similar tohandle_dots()withLOOKUP_RCUcallingfollow_dotdot_rcu().The main issue with this approach is to keep some state of the path walk to know if the next call to "
path_walk_parent_rcu()" would be valid (i.e. something like a very light version of nameidata, mainly sequence integers), and to get back to the non-RCU version otherwise.Quoted 4 lines
My vague impression is that you want to search up from a given strut path, no further then some other given path, looking for a dentry that matches some rule. Is that correct?
Yes
Quoted 5 lines
In general, the original dentry could be moved away from under the dentry you find moments after the match is reported. What mechanisms do you have in place to ensure this doesn't happen, or that it doesn't matter?
In the case of Landlock, by default, a set of access rights are denied and can only be allowed by an element in the file hierarchy. The goal is to only allow access to files under a specific directory (or directly a specific file). That's why we only care of the file hierarchy at the time of access check. It's not an issue if the file/directory was moved or is being moved as long as we can walk its "current" hierarchy. Furthermore, a sandboxed process is restricted from doing arbitrary mounts (and renames/links are controlled with the
LANDLOCK_ACCESS_FS_REFERright).Quoted 2 lines
However, we need to get a valid "snapshot" of the set of dentries that (could) lead to the evaluated file/directory.
A "snapshot" is an interesting idea - though looking at the landlock code you one need inodes, not dentries.
I imagine an interface where you give it a starting path, a root, and and array of inode pointers, and it fills in the pointers with the path
- all under rcu so no references are needed.
But you would need some fallback if the array isn't big enough, so maybe that isn't a good idea.Based on the comments by Al and Christian, I think the only viable approach is to pass a callback to some vfs function that does the walking.
vfs_walk_ancestors(struct path *path, struct path *root,
int (*walk_cb)(struct path *ancestor, void *data), void *data)
Quoted 7 lines
where
walk_cb()returns a negative number if it wants to abort, and is given a NULL ancestor ifvfs_walk_ancestors()needed to restart.
vfs_walk_ancestors()would initialise a "struct nameidata" and effectively callhandle_dots(&nd, LAST_DOTDOT)repeatedly, callingwalk_cb(&nd.path, data)each time.
handle_dots semantically does more than dget_parent + choose_mountpoint tho (which is what Landlock currently does, and is also what Song's iterator will do). There is the step_into which will step into mountpoints (there is also code to handle symlinks, although I'm not sure if that's relevant for following ".."), and it will also return ENOENT if the path is disconnected.
Also I guess we might not need to have an entire nameidata? In theory it only needs to do what follow_dotdot_rcu does without the path_connected check. So it seems like given we have path and root as function argument, it would only need nd->{seq,m_seq}.
I might be wrong tho, but certainly the behaviour is different.
Quoted 2 lines
How would you feel about that sort of interface?
I can't speak for Mickaël, but a callback-based interface is less flexible (and _maybe_ less performant?). Also, probably we will want to fallback to a reference-taking walk if the walk fails (rather than, say, retry infinitely), and this should probably use Song's proposed iterator. I'm not sure if Song would be keen to rewrite this iterator patch series in callback style (to be clear, it doesn't necessarily seem like a good idea to me, and I'm not asking him to), which means that we will end up with the reference walk API being a "call this function repeatedly", and the rcu walk API taking a callback. I think it is still workable (after all, if Landlock wants to reuse the code in the callback it can just call the callback function itself when doing the reference walk), but it seems a bit "ugly" to me.
But this is just my opinion, and if there is a stronger desire to not expose any VFS seqcount integers then maybe we will just need to work with a callback.
Quick note in case anyone reading this has not seen it, a while ago I made a POC of a non-callback style API for rcu parent walk based on Song's series:
https://lore.kernel.org/all/[email protected]/#iZ31fs:namei.c
Quoted 3 lines
NeilBrown
Quoted 66 lines · Tingmao Wang
Quoted 57 lines · NeilBrown
Quoted 37 lines · Mickaël Salaün
Quoted 2 lines · NeilBrown
Can you spell out the minimum that you need?
Sure. We'd like to call this new helper in a RCU
read-side critical section and leverage this capability to speed up path walk when there is no concurrent hierarchy modification. This use case is similar tohandle_dots()withLOOKUP_RCUcallingfollow_dotdot_rcu().The main issue with this approach is to keep some state of the path walk to know if the next call to "
path_walk_parent_rcu()" would be valid (i.e. something like a very light version of nameidata, mainly sequence integers), and to get back to the non-RCU version otherwise.Quoted 4 lines
My vague impression is that you want to search up from a given strut path, no further then some other given path, looking for a dentry that matches some rule. Is that correct?
Yes
Quoted 5 lines
In general, the original dentry could be moved away from under the dentry you find moments after the match is reported. What mechanisms do you have in place to ensure this doesn't happen, or that it doesn't matter?
In the case of Landlock, by default, a set of access rights are denied and can only be allowed by an element in the file hierarchy. The goal is to only allow access to files under a specific directory (or directly a specific file). That's why we only care of the file hierarchy at the time of access check. It's not an issue if the file/directory was moved or is being moved as long as we can walk its "current" hierarchy. Furthermore, a sandboxed process is restricted from doing arbitrary mounts (and renames/links are controlled with the
LANDLOCK_ACCESS_FS_REFERright).Quoted 2 lines
However, we need to get a valid "snapshot" of the set of dentries that (could) lead to the evaluated file/directory.
A "snapshot" is an interesting idea - though looking at the landlock code you one need inodes, not dentries.
I imagine an interface where you give it a starting path, a root, and and array of inode pointers, and it fills in the pointers with the path
- all under rcu so no references are needed.
But you would need some fallback if the array isn't big enough, so maybe that isn't a good idea.Based on the comments by Al and Christian, I think the only viable approach is to pass a callback to some vfs function that does the walking.
vfs_walk_ancestors(struct path *path, struct path *root,
int (*walk_cb)(struct path *ancestor, void *data), void *data)Quoted 7 lines
where
walk_cb()returns a negative number if it wants to abort, and is given a NULL ancestor ifvfs_walk_ancestors()needed to restart.
vfs_walk_ancestors()would initialise a "struct nameidata" and effectively callhandle_dots(&nd, LAST_DOTDOT)repeatedly, callingwalk_cb(&nd.path, data)each time.
Quoted 6 lines
handle_dotssemantically does more thandget_parent+choose_mountpointtho (which is what Landlock currently does, and is also what Song's iterator will do). There is thestep_intowhich will step into mountpoints (there is also code to handle symlinks, although I'm not sure if that's relevant for following ".."), and it will also return ENOENT if the path is disconnected.
Is any of this a problem for you?
Quoted 5 lines
Also I guess we might not need to have an entire nameidata? In theory it only needs to do what
follow_dotdot_rcudoes without thepath_connectedcheck. So it seems like given we have path and root as function argument, it would only need nd->{seq,m_seq}.
Those are implementation details internal to namei.c. Certainly this function wouldn't use all of the fields in nameidata, but it doesn't hurt to have a few fields in a struct on the stack which don't get used. Keeping the code simple and uniform is much more important. Using nameidata would help achieve that.
Quoted 2 lines
I might be wrong tho, but certainly the behaviour is different.
Here we get back to the question of "precisely what behaviour do you need?".
"The same as what a previous patch did" is not a reasonable answer.
If, from userspace, you repeatedly did chdir("..") and then examined the current directory you would get exactly the sequence of directories provided by repeatedly calling handle_dots(..., LAST_DOTDOT). If there is some circumstance where that would be not acceptable for your use case, you need to explain (and we need to document) what differences you need and why use need it.
Quoted 4 lines
Quoted 3 lines
How would you feel about that sort of interface?
Quoted 12 lines
I can't speak for Mickaël, but a callback-based interface is less flexible (and _maybe_ less performant?). Also, probably we will want to fallback to a reference-taking walk if the walk fails (rather than, say, retry infinitely), and this should probably use Song's proposed iterator. I'm not sure if Song would be keen to rewrite this iterator patch series in callback style (to be clear, it doesn't necessarily seem like a good idea to me, and I'm not asking him to), which means that we will end up with the reference walk API being a "call this function repeatedly", and the rcu walk API taking a callback. I think it is still workable (after all, if Landlock wants to reuse the code in the callback it can just call the callback function itself when doing the reference walk), but it seems a bit "ugly" to me.
call-back can have a performance impact (less opportunity for compiler optimisation and CPU speculation), though less than taking spinlock and references. However Al and Christian have drawn a hard line against making seq numbers visible outside VFS code so I think it is the approach most likely to be accepted.
Certainly vfs_walk_ancestors() would fallback to ref-walk if rcu-walk resulted in -ECHILD - just like all other path walking code in namei.c. This would be largely transparent to the caller - the caller would only see that the callback received a NULL path indicating a restart. It wouldn't need to know why.
NeilBrown
Quoted 3 lines · NeilBrown
[...]
Quoted 20 lines
Quoted 13 lines
I can't speak for Mickaël, but a callback-based interface is less flexible (and _maybe_ less performant?). Also, probably we will want to fallback to a reference-taking walk if the walk fails (rather than, say, retry infinitely), and this should probably use Song's proposed iterator. I'm not sure if Song would be keen to rewrite this iterator patch series in callback style (to be clear, it doesn't necessarily seem like a good idea to me, and I'm not asking him to), which means that we will end up with the reference walk API being a "call this function repeatedly", and the rcu walk API taking a callback. I think it is still workable (after all, if Landlock wants to reuse the code in the callback it can just call the callback function itself when doing the reference walk), but it seems a bit "ugly" to me.
call-back can have a performance impact (less opportunity for compiler optimisation and CPU speculation), though less than taking spinlock and references. However Al and Christian have drawn a hard line against making seq numbers visible outside VFS code so I think it is the approach most likely to be accepted.
Quoted 5 lines
Certainly
vfs_walk_ancestors()would fallback to ref-walk if rcu-walk resulted in-ECHILD- just like all other path walking code innamei.c. This would be largely transparent to the caller - the caller would only see that the callback received a NULL path indicating a restart. It wouldn't need to know why.
I guess I misunderstood the proposal of vfs_walk_ancestors() initially, so some clarification:
I think vfs_walk_ancestors() is good for the rcu-walk, and some rcu-then-ref-walk. However, I don’t think it fits all use cases. A reliable step-by-step ref-walk, like this set, works well with BPF, and we want to keep it.
Can we ship this set as-is (or after fixing the comment reported by kernel test robot)? I really don’t think we need figure out all details about the rcu-walk here.
Once this is landed, we can try implementing the rcu-walk (vfs_walk_ancestors or some variation). If no one volunteers, I can give it a try.
Thanks,
Song
Mickaël Salaün
maintainerQuoted 26 lines · Song Liu
Quoted 3 lines · NeilBrown
[...]
Quoted 20 lines
Quoted 13 lines
I can't speak for Mickaël, but a callback-based interface is less flexible (and _maybe_ less performant?). Also, probably we will want to fallback to a reference-taking walk if the walk fails (rather than, say, retry infinitely), and this should probably use Song's proposed iterator. I'm not sure if Song would be keen to rewrite this iterator patch series in callback style (to be clear, it doesn't necessarily seem like a good idea to me, and I'm not asking him to), which means that we will end up with the reference walk API being a "call this function repeatedly", and the rcu walk API taking a callback. I think it is still workable (after all, if Landlock wants to reuse the code in the callback it can just call the callback function itself when doing the reference walk), but it seems a bit "ugly" to me.
call-back can have a performance impact (less opportunity for compiler optimisation and CPU speculation), though less than taking spinlock and references. However Al and Christian have drawn a hard line against making seq numbers visible outside VFS code so I think it is the approach most likely to be accepted.
Quoted 5 lines
Quoted 5 lines
Certainly
vfs_walk_ancestors()would fallback to ref-walk if rcu-walk resulted in-ECHILD- just like all other path walking code innamei.c. This would be largely transparent to the caller - the caller would only see that the callback received a NULL path indicating a restart. It wouldn't need to know why.
Given the constraints this looks good to me. Here is an updated API with two extra consts, an updated walk_cb() signature, and a new "flags" and without @root:
int vfs_walk_ancestors(struct path *path,
bool (*walk_cb)(const struct path *ancestor, void *data), void *data, int flags)
The walk continue while walk_cb() returns true. walk_cb() can then check if @ancestor is equal to a @root, or other properties. The walk_cb() return value (if not bool) should not be returned by vfs_walk_ancestors() because a walk stop doesn't mean an error.
@path would be updated with latest ancestor path (e.g. @root). @flags could contain LOOKUP_RCU or not, which enables us to have walk_cb() not-RCU compatible.
When passing LOOKUP_RCU, if the first call to vfs_walk_ancestors() failed with -ECHILD, the caller can restart the walk by calling vfs_walk_ancestors() again but without LOOKUP_RCU.
Quoted 8 lines
I guess I misunderstood the proposal of
vfs_walk_ancestors()initially, so some clarification:I think
vfs_walk_ancestors()is good for the rcu-walk, and some rcu-then-ref-walk. However, I don’t think it fits all use cases. A reliable step-by-step ref-walk, like this set, works well with BPF, and we want to keep it.
The above updated API should work for both use cases: if the caller wants to walk only one level, walk_cb() can just always return false (and potentially save that it was called) and then stop the walk the first time it is called. This makes it possible to write an eBPF helper with the same API as path_walk_parent(), while making the kernel API more flexible.
Quoted 8 lines
Can we ship this set as-is (or after fixing the comment reported by kernel test robot)? I really don’t think we need figure out all details about the rcu-walk here.
Once this is landed, we can try implementing the rcu-walk (
vfs_walk_ancestorsor some variation). If no one volunteers, I can give it a try.
My understanding is that Christian only wants one helper (that should handle both use cases). I think this updated API should be good enough for everyone. Most of your code should stay the same. What do you think?
Quoted 4 lines
Thanks,
Song
Quoted 3 lines · Mickaël Salaün
[...]
Quoted 13 lines
Given the constraints this looks good to me. Here is an updated API with two extra consts, an updated
walk_cb()signature, and a new "flags" and without @root:int vfs_walk_ancestors(struct path *path,
bool (*walk_cb)(const struct path *ancestor, void *data), void *data, int flags)The walk continue while
walk_cb()returns true.walk_cb()can then check if @ancestor is equal to a @root, or other properties. Thewalk_cb()return value (if not bool) should not be returned byvfs_walk_ancestors()because a walk stop doesn't mean an error.
Quoted 7 lines
@path would be updated with latest ancestor path (e.g. @root). @flags could contain
LOOKUP_RCUor not, which enables us to havewalk_cb()not-RCU compatible.When passing
LOOKUP_RCU, if the first call tovfs_walk_ancestors()failed with-ECHILD, the caller can restart the walk by callingvfs_walk_ancestors()again but withoutLOOKUP_RCU.
IIUC, Neil is against using LOOKUP_RCU as input, and VFS folks may share same the concerns.
Quoted 9 lines
Quoted 8 lines
I guess I misunderstood the proposal of
vfs_walk_ancestors()initially, so some clarification:I think
vfs_walk_ancestors()is good for the rcu-walk, and some rcu-then-ref-walk. However, I don’t think it fits all use cases. A reliable step-by-step ref-walk, like this set, works well with BPF, and we want to keep it.
Quoted 6 lines
The above updated API should work for both use cases: if the caller wants to walk only one level,
walk_cb()can just always return false (and potentially save that it was called) and then stop the walk the first time it is called. This makes it possible to write an eBPF helper with the same API aspath_walk_parent(), while making the kernel API more flexible.
I don’t think this will be the same. Current path_walk_parent() holds a reference on parent path, and returns control to the caller. However, when vfs_walk_ancestors() returns, it should not hold any extra reference, right? IOW, vfs_walk_ancestors may hold some reference between callbacks, but is expected to release these references before finally returning to the caller.
Quoted 4 lines
Can we ship this set as-is (or after fixing the comment reported
Quoted 3 lines
by kernel test robot)? I really don’t think we need figure out all details about the rcu-walk here.
Quoted 8 lines
Quoted 3 lines
Once this is landed, we can try implementing the rcu-walk (
vfs_walk_ancestorsor some variation). If no one volunteers, I can give it a try.My understanding is that Christian only wants one helper (that should handle both use cases). I think this updated API should be good enough for everyone. Most of your code should stay the same. What do you think?
Christian, could you please clarify this requirement?
Given different expectations in how the references are handled (see above), I don’t think we can fit all use cases in one API. However, if we find such an API in the future, which works for all cases, we can refactor BPF side code to use that instead. Therefore, even we have a “one API only” requirement, it is not necessary to delay this set for a RCU-walk API.
Thanks,
Song
Quoted 32 lines · Song Liu
Quoted 3 lines · NeilBrown
[...]
Quoted 20 lines
Quoted 13 lines
I can't speak for Mickaël, but a callback-based interface is less flexible (and _maybe_ less performant?). Also, probably we will want to fallback to a reference-taking walk if the walk fails (rather than, say, retry infinitely), and this should probably use Song's proposed iterator. I'm not sure if Song would be keen to rewrite this iterator patch series in callback style (to be clear, it doesn't necessarily seem like a good idea to me, and I'm not asking him to), which means that we will end up with the reference walk API being a "call this function repeatedly", and the rcu walk API taking a callback. I think it is still workable (after all, if Landlock wants to reuse the code in the callback it can just call the callback function itself when doing the reference walk), but it seems a bit "ugly" to me.
call-back can have a performance impact (less opportunity for compiler optimisation and CPU speculation), though less than taking spinlock and references. However Al and Christian have drawn a hard line against making seq numbers visible outside VFS code so I think it is the approach most likely to be accepted.
Quoted 5 lines
Certainly
vfs_walk_ancestors()would fallback to ref-walk if rcu-walk resulted in-ECHILD- just like all other path walking code innamei.c. This would be largely transparent to the caller - the caller would only see that the callback received a NULL path indicating a restart. It wouldn't need to know why.
Quoted 7 lines
I guess I misunderstood the proposal of
vfs_walk_ancestors()initially, so some clarification:I think
vfs_walk_ancestors()is good for the rcu-walk, and some rcu-then-ref-walk. However, I don’t think it fits all use cases. A reliable step-by-step ref-walk, like this set, works well with BPF, and we want to keep it.
The distinction between rcu-walk and ref-walk is an internal implementation detail. You as a caller shouldn't need to think about the difference. You just want to walk. Note that LOOKUP_RCU is documented in namei.h as "semi-internal". The only uses outside of core-VFS code is in individual filesystem's d_revalidate handler - they are checking if they are allowed to sleep or not. You should never expect to pass LOOKUP_RCU to an VFS API - no other code does.
It might be reasonable for you as a caller to have some control over whether the call can sleep or not. LOOKUP_CACHED is a bit like that. But for dotdot lookup the code will never sleep - so that is not relevant.
I strongly suggest you stop thinking about rcu-walk vs ref-walk. Think about the needs of your code. If you need a high-performance API, then ask for a high-performance API, don't assume what form it will take or what the internal implementation details will be.
I think you already have a clear answer that a step-by-step API will not be read-only on the dcache (i.e. it will adjust refcounts) and so will not be high performance. If you want high performance, you need to accept a different style of API.
NeilBrown
Quoted 3 lines · NeilBrown
[...]
Quoted 16 lines
Quoted 7 lines
I guess I misunderstood the proposal of
vfs_walk_ancestors()initially, so some clarification:I think
vfs_walk_ancestors()is good for the rcu-walk, and some rcu-then-ref-walk. However, I don’t think it fits all use cases. A reliable step-by-step ref-walk, like this set, works well with BPF, and we want to keep it.The distinction between rcu-walk and ref-walk is an internal implementation detail. You as a caller shouldn't need to think about the difference. You just want to walk. Note that
LOOKUP_RCUis documented innamei.has "semi-internal". The only uses outside of core-VFS code is in individual filesystem'sd_revalidatehandler - they are checking if they are allowed to sleep or not. You should never expect to passLOOKUP_RCUto an VFS API - no other code does.
Quoted 4 lines
It might be reasonable for you as a caller to have some control over whether the call can sleep or not.
LOOKUP_CACHEDis a bit like that. But for dotdot lookup the code will never sleep - so that is not relevant.
Unfortunately, the BPF use case is more complicated. In some cases, the callback function cannot be call in rcu critical sections. For example, the callback may need to read xatter. For these cases, we we cannot use RCU walk at all.
Quoted 4 lines
I strongly suggest you stop thinking about rcu-walk vs ref-walk. Think about the needs of your code. If you need a high-performance API, then ask for a high-performance API, don't assume what form it will take or what the internal implementation details will be.
At the moment, we need a ref-walk API on the BPF side. The RCU walk is a totally separate topic.
Quoted 4 lines
I think you already have a clear answer that a step-by-step API will not be read-only on the dcache (i.e. it will adjust refcounts) and so will not be high performance. If you want high performance, you need to accept a different style of API.
Agreed.
Thanks,
Song
Quoted 27 lines · Song Liu
Quoted 3 lines · NeilBrown
[...]
Quoted 16 lines
Quoted 7 lines
I guess I misunderstood the proposal of
vfs_walk_ancestors()initially, so some clarification:I think
vfs_walk_ancestors()is good for the rcu-walk, and some rcu-then-ref-walk. However, I don’t think it fits all use cases. A reliable step-by-step ref-walk, like this set, works well with BPF, and we want to keep it.The distinction between rcu-walk and ref-walk is an internal implementation detail. You as a caller shouldn't need to think about the difference. You just want to walk. Note that
LOOKUP_RCUis documented innamei.has "semi-internal". The only uses outside of core-VFS code is in individual filesystem'sd_revalidatehandler - they are checking if they are allowed to sleep or not. You should never expect to passLOOKUP_RCUto an VFS API - no other code does.Quoted 4 lines
It might be reasonable for you as a caller to have some control over whether the call can sleep or not.
LOOKUP_CACHEDis a bit like that. But for dotdot lookup the code will never sleep - so that is not relevant.
Quoted 4 lines
Unfortunately, the BPF use case is more complicated. In some cases, the callback function cannot be call in rcu critical sections. For example, the callback may need to read xatter. For these cases, we we cannot use RCU walk at all.
I really think you should stop using the terms RCU walk and ref-walk. I think they might be focusing your thinking in an unhelpful direction.
The key issue about reading xattrs is that it might need to sleep. Focusing on what might need to sleep and what will never need to sleep is a useful approach - the distinction is wide spread in the kernel and several function take a flag indicating if they are permitted to sleep, or if failure when sleeping would be required.
So your above observation is better described as
The vfs_walk_ancestors() API has an (implicit) requirement that the callback mustn't sleep. This is a problem for some use-cases where the call back might need to sleep - e.g. for accessing xattrs.
That is a good and useful observation. I can see three possibly responses:
1/ Add a vfs_walk_ancestors_maysleep() API for which the callback is
always allowed to sleep. I don't particularly like this approach.
2/ Use repeated calls to vfs_walk_parent() when the handling of each
ancestor might need to sleep. I see no problem with supporting both vfs_walk_ancestors() and vfs_walk_parent(). There is plenty of precedent for having different interfaces for different use cases.
3/ Extend vfs_walk_ancestors() to pass a "may sleep" flag to the callback.
If the callback finds that it needs to sleep but that "may sleep" isn't set, it returns some well known status, like -EWOULDBLOCK (or -ECHILD). It can expect to be called again but with "may sleep" set. This is my preferred approach. There is precedent with the d_revalidate callbacks which works like this.
I suspect that accessing xattrs might often be possible without sleeping. It is conceivable that we could add a "may sleep" argument to vfs_getxattr() so that it could still often be used without requiring vfs_walk_ancestors() to permit sleeping.
This would almost certainly require a clear demonstration that there was a performance cost in not having the option of non-sleeping vfs_getxattr().
Quoted 8 lines
Quoted 5 lines
I strongly suggest you stop thinking about rcu-walk vs ref-walk. Think about the needs of your code. If you need a high-performance API, then ask for a high-performance API, don't assume what form it will take or what the internal implementation details will be.
At the moment, we need a ref-walk API on the BPF side. The RCU walk is a totally separate topic.
Do you mean "we need step-by-step walking" or do you mean "we need to potentially sleep for each ancestor"? These are conceptually different requirements, but I cannot tell which you mean when you talk about "RCU walk".
Thanks,
NeilBrown
Quoted 12 lines
Quoted 5 lines
I think you already have a clear answer that a step-by-step API will not be read-only on the dcache (i.e. it will adjust refcounts) and so will not be high performance. If you want high performance, you need to accept a different style of API.
Agreed.
Thanks,
Song
Quoted 3 lines · NeilBrown
[...]
Quoted 23 lines
Quoted 4 lines
Unfortunately, the BPF use case is more complicated. In some cases, the callback function cannot be call in rcu critical sections. For example, the callback may need to read xatter. For these cases, we we cannot use RCU walk at all.
I really think you should stop using the terms RCU walk and ref-walk. I think they might be focusing your thinking in an unhelpful direction.
The key issue about reading xattrs is that it might need to sleep. Focusing on what might need to sleep and what will never need to sleep is a useful approach - the distinction is wide spread in the kernel and several function take a flag indicating if they are permitted to sleep, or if failure when sleeping would be required.
So your above observation is better described as
The
vfs_walk_ancestors()API has an (implicit) requirement that the callback mustn't sleep. This is a problem for some use-cases where the call back might need to sleep - e.g. for accessing xattrs.That is a good and useful observation. I can see three possibly responses:
Quoted 7 lines
1/ Add a
vfs_walk_ancestors_maysleep()API for which the callback is
always allowed to sleep. I don't particularly like this approach.2/ Use repeated calls to
vfs_walk_parent()when the handling of each
ancestor might need to sleep. I see no problem with supporting bothvfs_walk_ancestors()andvfs_walk_parent(). There is plenty of precedent for having different interfaces for different use cases.
I prefer option 2.
Quoted 14 lines
3/ Extend
vfs_walk_ancestors()to pass a "may sleep" flag to the callback.
If the callback finds that it needs to sleep but that "may sleep" isn't set, it returns some well known status, like-EWOULDBLOCK(or-ECHILD). It can expect to be called again but with "may sleep" set. This is my preferred approach. There is precedent with thed_revalidatecallbacks which works like this.
I suspect that accessing xattrs might often be possible without sleeping. It is conceivable that we could add a "may sleep" argument tovfs_getxattr()so that it could still often be used without requiringvfs_walk_ancestors()to permit sleeping.
This would almost certainly require a clear demonstration that there was a performance cost in not having the option of non-sleepingvfs_getxattr().
For built-in kernel code, I can see this works. However, I don’t see why it is necessary to introduce the extra complexity of -EWOULDBLOCK, and vfs_get_xattr_cannot_sleep, etc. A separate step-by-step walking API is much cleaner.
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
I strongly suggest you stop thinking about rcu-walk vs ref-walk. Think about the needs of your code. If you need a high-performance API, then ask for a high-performance API, don't assume what form it will take or what the internal implementation details will be.
Quoted 7 lines
Quoted 2 lines
At the moment, we need a ref-walk API on the BPF side. The RCU walk is a totally separate topic.
Do you mean "we need step-by-step walking" or do you mean "we need to potentially sleep for each ancestor"? These are conceptually different requirements, but I cannot tell which you mean when you talk about "RCU walk”.
To be extra clear, I mean we need "step-by-step and take-reference-on-each-step walking”, for existing use cases.
In the future, if it is possible to have a “do-not-take-reference, cannot-sleep, callback-based walking”. We may try to use that for some use cases. But that won’t replace step-by-step walking for all users.
Thanks,
Song
Christian Brauner
maintainerQuoted 28 lines · NeilBrown
Quoted 27 lines · Song Liu
Quoted 3 lines · NeilBrown
[...]
Quoted 20 lines
Quoted 7 lines
I guess I misunderstood the proposal of
vfs_walk_ancestors()initially, so some clarification:I think
vfs_walk_ancestors()is good for the rcu-walk, and some rcu-then-ref-walk. However, I don’t think it fits all use cases. A reliable step-by-step ref-walk, like this set, works well with BPF, and we want to keep it.The distinction between rcu-walk and ref-walk is an internal implementation detail. You as a caller shouldn't need to think about the difference. You just want to walk. Note that
LOOKUP_RCUis documented innamei.has "semi-internal". The only uses outside of core-VFS code is in individual filesystem'sd_revalidatehandler - they are checking if they are allowed to sleep or not. You should never expect to passLOOKUP_RCUto an VFS API - no other code does.It might be reasonable for you as a caller to have some control over whether the call can sleep or not.
LOOKUP_CACHEDis a bit like that. But for dotdot lookup the code will never sleep - so that is not relevant.
Quoted 7 lines
Quoted 4 lines
Unfortunately, the BPF use case is more complicated. In some cases, the callback function cannot be call in rcu critical sections. For example, the callback may need to read xatter. For these cases, we we cannot use RCU walk at all.
I really think you should stop using the terms RCU walk and ref-walk. I think they might be focusing your thinking in an unhelpful direction.
Thank you! I really appreciate you helping to shape this API and it aligns a lot with my thinking.
Quoted 12 lines
The key issue about reading xattrs is that it might need to sleep. Focusing on what might need to sleep and what will never need to sleep is a useful approach - the distinction is wide spread in the kernel and several function take a flag indicating if they are permitted to sleep, or if failure when sleeping would be required.
So your above observation is better described as
The
vfs_walk_ancestors()API has an (implicit) requirement that the callback mustn't sleep. This is a problem for some use-cases where the call back might need to sleep - e.g. for accessing xattrs.
Quoted 5 lines
That is a good and useful observation. I can see three possibly responses:
1/ Add a
vfs_walk_ancestors_maysleep()API for which the callback is
always allowed to sleep. I don't particularly like this approach.
Agreed.
Quoted 5 lines
2/ Use repeated calls to
vfs_walk_parent()when the handling of each
ancestor might need to sleep. I see no problem with supporting bothvfs_walk_ancestors()andvfs_walk_parent(). There is plenty of precedent for having different interfaces for different use cases.
Meh.
Quoted 2 lines
3/ Extend
vfs_walk_ancestors()to pass a "may sleep" flag to the callback.
I think that's fine.
Christian Brauner
maintainerQuoted 66 lines · Christian Brauner
Quoted 28 lines · NeilBrown
Quoted 27 lines · Song Liu
Quoted 3 lines · NeilBrown
[...]
Quoted 20 lines
Quoted 7 lines
I guess I misunderstood the proposal of
vfs_walk_ancestors()initially, so some clarification:I think
vfs_walk_ancestors()is good for the rcu-walk, and some rcu-then-ref-walk. However, I don’t think it fits all use cases. A reliable step-by-step ref-walk, like this set, works well with BPF, and we want to keep it.The distinction between rcu-walk and ref-walk is an internal implementation detail. You as a caller shouldn't need to think about the difference. You just want to walk. Note that
LOOKUP_RCUis documented innamei.has "semi-internal". The only uses outside of core-VFS code is in individual filesystem'sd_revalidatehandler - they are checking if they are allowed to sleep or not. You should never expect to passLOOKUP_RCUto an VFS API - no other code does.It might be reasonable for you as a caller to have some control over whether the call can sleep or not.
LOOKUP_CACHEDis a bit like that. But for dotdot lookup the code will never sleep - so that is not relevant.Quoted 7 lines
Quoted 4 lines
Unfortunately, the BPF use case is more complicated. In some cases, the callback function cannot be call in rcu critical sections. For example, the callback may need to read xatter. For these cases, we we cannot use RCU walk at all.
I really think you should stop using the terms RCU walk and ref-walk. I think they might be focusing your thinking in an unhelpful direction.
Thank you! I really appreciate you helping to shape this API and it aligns a lot with my thinking.
Quoted 12 lines
The key issue about reading xattrs is that it might need to sleep. Focusing on what might need to sleep and what will never need to sleep is a useful approach - the distinction is wide spread in the kernel and several function take a flag indicating if they are permitted to sleep, or if failure when sleeping would be required.
So your above observation is better described as
The
vfs_walk_ancestors()API has an (implicit) requirement that the callback mustn't sleep. This is a problem for some use-cases where the call back might need to sleep - e.g. for accessing xattrs.Quoted 5 lines
That is a good and useful observation. I can see three possibly responses:
1/ Add a
vfs_walk_ancestors_maysleep()API for which the callback is
always allowed to sleep. I don't particularly like this approach.Agreed.
Quoted 5 lines
2/ Use repeated calls to
vfs_walk_parent()when the handling of each
ancestor might need to sleep. I see no problem with supporting bothvfs_walk_ancestors()andvfs_walk_parent(). There is plenty of precedent for having different interfaces for different use cases.
Quoted 6 lines
Meh.
Quoted 2 lines
3/ Extend
vfs_walk_ancestors()to pass a "may sleep" flag to the callback.I think that's fine.
Ok, sorry for the delay but there's a lot of different things going on right now and this one isn't exactly an easy thing to solve.
I mentioned this before and so did Neil: the lookup implementation supports two modes sleeping and non-sleeping. That api is abstracted away as heavily as possible by the VFS so that non-core code will not be exposed to it other than in exceptional circumstances and doesn't have to care about it.
It is a conceptual dead-end to expose these two modes via separate APIs and leak this implementation detail into non-core code. It will not happen as far as I'm concerned.
I very much understand the urge to get the refcount step-by-step thing merged asap. Everyone wants their APIs merged fast. And if it's reasonable to move fast we will (see the kernfs xattr thing).
But here are two use-cases that ask for the same thing with different constraints that closely mirror our unified approach. Merging one quickly just to have something and then later bolting the other one on top, augmenting, or replacing, possible having to deprecate the old API is just objectively nuts. That's how we end up with a spaghetthi helper collection. We want as little helper fragmentation as possible.
We need a unified API that serves both use-cases. I dislike callback-based APIs generally but we have precedent in the VFS for this for cases where the internal state handling is delicate enough that it should not be exposed (see __iterate_supers() which does exactly work like Neil suggested down to the flag argument itself I added).
So I'm open to the callback solution.
(Note for really absurd perf requirements you could even make it work with static calls I'm pretty sure.)
Hi Christian,
Thanks for your comments!
Quoted 1 line · Christian Brauner
[...]
Quoted 34 lines
Quoted 3 lines
Quoted 1 line
3/ Extend
vfs_walk_ancestors()to pass a "may sleep" flag to the callback.I think that's fine.
Ok, sorry for the delay but there's a lot of different things going on right now and this one isn't exactly an easy thing to solve.
I mentioned this before and so did Neil: the lookup implementation supports two modes sleeping and non-sleeping. That api is abstracted away as heavily as possible by the VFS so that non-core code will not be exposed to it other than in exceptional circumstances and doesn't have to care about it.
It is a conceptual dead-end to expose these two modes via separate APIs and leak this implementation detail into non-core code. It will not happen as far as I'm concerned.
I very much understand the urge to get the refcount step-by-step thing merged asap. Everyone wants their APIs merged fast. And if it's reasonable to move fast we will (see the kernfs xattr thing).
But here are two use-cases that ask for the same thing with different constraints that closely mirror our unified approach. Merging one quickly just to have something and then later bolting the other one on top, augmenting, or replacing, possible having to deprecate the old API is just objectively nuts. That's how we end up with a spaghetthi helper collection. We want as little helper fragmentation as possible.
We need a unified API that serves both use-cases. I dislike callback-based APIs generally but we have precedent in the VFS for this for cases where the internal state handling is delicate enough that it should not be exposed (see
__iterate_supers()which does exactly work like Neil suggested down to the flag argument itself I added).
Quoted 4 lines
So I'm open to the callback solution.
(Note for really absurd perf requirements you could even make it work with static calls I'm pretty sure.)
I guess we will go with Mickaël’s idea:
Quoted 8 lines
int vfs_walk_ancestors(struct path *path,
bool (*walk_cb)(const struct path *ancestor, void *data), void *data, int flags)The walk continue while
walk_cb()returns true.walk_cb()can then check if @ancestor is equal to a @root, or other properties. Thewalk_cb()return value (if not bool) should not be returned byvfs_walk_ancestors()because a walk stop doesn't mean an error.
If necessary, we hide “root" inside @data. This is good.
Quoted 1 line
@path would be updated with latest ancestor path (e.g. @root).
Update @path to the last ancestor and hold proper references. I missed this part earlier. With this feature, vfs_walk_ancestors should work usable with open-codeed bpf path iterator.
I have a question about this behavior with RCU walk. IIUC, RCU walk does not hold reference to @ancestor when calling walk_cb(). If walk_cb() returns false, shall vfs_walk_ancestors() then grab a reference on @ancestor? This feels a bit weird to me. Maybe “updating @path to the last ancestor” should only apply to LOOKUP_RCU==false case?
Quoted 6 lines
@flags could contain
LOOKUP_RCUor not, which enables us to havewalk_cb()not-RCU compatible.When passing
LOOKUP_RCU, if the first call tovfs_walk_ancestors()failed with-ECHILD, the caller can restart the walk by callingvfs_walk_ancestors()again but withoutLOOKUP_RCU.
Given we want callers to handle -ECHILD and call vfs_walk_ancestors again without LOOKUP_RCU, I think we should keep @path not changed With LOOKUP_RCU==true, and only update it to the last ancestor when LOOKUP_RCU==false.
With this behavior, landlock code will be like:
/* Assume we hold reference on “path”.
* With LOOKUP_RCU, path will not change, we don’t need
* extra reference on “path”.
*/
err = vfs_walk_ancestors(path, ll_cb, data, LOOKUP_RCU); /*
* At this point, whether err is 0 or not, path is not
* changed.
*/
if (err == -ECHILD) {
struct path walk_path = *path;
/* reset any data changed by the walk */
reset_data(data);
/* get a reference on walk_path. */
path_get(&walk_path);
err = vfs_walk_ancestors(&walk_path, ll_cb, data, 0);
/* Now, walk_path might be updated */
/* Always release reference on walk_path */
path_put(&walk_path);
}BPF path iterator sode will look like:
static bool bpf_cb(const struct path *ancestor, void *data)
{
return false;
}struct path *bpf_iter_path_next(struct bpf_iter_path *it)
{
struct bpf_iter_path_kern *kit = (void *)it;
if (vfs_walk_ancestors(&kit->path, bpf_cb, NULL))
return NULL;
return &kit->path;
}Does this sound reasonable to every body?
Thanks,
Song
Mickaël Salaün
maintainerQuoted 61 lines · Song Liu
Hi Christian,
Thanks for your comments!
Quoted 1 line · Christian Brauner
[...]
Quoted 34 lines
Quoted 3 lines
Quoted 1 line
3/ Extend
vfs_walk_ancestors()to pass a "may sleep" flag to the callback.I think that's fine.
Ok, sorry for the delay but there's a lot of different things going on right now and this one isn't exactly an easy thing to solve.
I mentioned this before and so did Neil: the lookup implementation supports two modes sleeping and non-sleeping. That api is abstracted away as heavily as possible by the VFS so that non-core code will not be exposed to it other than in exceptional circumstances and doesn't have to care about it.
It is a conceptual dead-end to expose these two modes via separate APIs and leak this implementation detail into non-core code. It will not happen as far as I'm concerned.
I very much understand the urge to get the refcount step-by-step thing merged asap. Everyone wants their APIs merged fast. And if it's reasonable to move fast we will (see the kernfs xattr thing).
But here are two use-cases that ask for the same thing with different constraints that closely mirror our unified approach. Merging one quickly just to have something and then later bolting the other one on top, augmenting, or replacing, possible having to deprecate the old API is just objectively nuts. That's how we end up with a spaghetthi helper collection. We want as little helper fragmentation as possible.
We need a unified API that serves both use-cases. I dislike callback-based APIs generally but we have precedent in the VFS for this for cases where the internal state handling is delicate enough that it should not be exposed (see
__iterate_supers()which does exactly work like Neil suggested down to the flag argument itself I added).Quoted 4 lines
So I'm open to the callback solution.
(Note for really absurd perf requirements you could even make it work with static calls I'm pretty sure.)
I guess we will go with Mickaël’s idea:
Quoted 9 lines
int vfs_walk_ancestors(struct path *path,
bool (*walk_cb)(const struct path *ancestor, void *data),void *data, int flags)
The walk continue while
walk_cb()returns true.walk_cb()can then check if @ancestor is equal to a @root, or other properties. Thewalk_cb()return value (if not bool) should not be returned byvfs_walk_ancestors()because a walk stop doesn't mean an error.If necessary, we hide “root" inside @data. This is good.
Quoted 8 lines
Quoted 1 line
@path would be updated with latest ancestor path (e.g. @root).
Update @path to the last ancestor and hold proper references. I missed this part earlier. With this feature,
vfs_walk_ancestorsshould work usable with open-codeed bpf path iterator.I have a question about this behavior with RCU walk. IIUC, RCU walk does not hold reference to @ancestor when calling
walk_cb().
I think a reference to the mount should be held, but not necessarily to the dentry if we are still in the same mount as the original path.
Quoted 2 lines
If
walk_cb()returns false, shallvfs_walk_ancestors()then grab a reference on @ancestor? This feels a bit weird to me.
If walk_cb() checks for a root, it will return false when the path will match, and the caller would expect to get this root path, right?
In general, it's safer to always have the same behavior when holding or releasing a reference. I think the caller should then always call path_put() after vfs_walk_ancestors() whatever the return code is.
Quoted 11 lines
Maybe “updating @path to the last ancestor” should only apply to LOOKUP_RCU==false case?
Quoted 6 lines
@flags could contain
LOOKUP_RCUor not, which enables us to havewalk_cb()not-RCU compatible.When passing
LOOKUP_RCU, if the first call tovfs_walk_ancestors()failed with-ECHILD, the caller can restart the walk by callingvfs_walk_ancestors()again but withoutLOOKUP_RCU.
Quoted 4 lines
Given we want callers to handle
-ECHILDand callvfs_walk_ancestorsagain withoutLOOKUP_RCU, I think we should keep @path not changed With LOOKUP_RCU==true, and only update it to the last ancestor when LOOKUP_RCU==false.
As Neil said, we don't want to explicitly pass LOOKUP_RCU as a public flag. Instead, walk_cb() should never sleep (and then potentially be called under RCU by the vfs_walk_ancestors() implementation).
Quoted 31 lines
With this behavior, landlock code will be like:
/* Assume we hold reference on “path”.
* WithLOOKUP_RCU, path will not change, we don’t need
* extra reference on “path”.
*/
err =vfs_walk_ancestors(path, ll_cb, data, LOOKUP_RCU); /*
* At this point, whether err is 0 or not, path is not
* changed.
*/if (err == -ECHILD) { struct path walk_path = *path; /* reset any data changed by the walk */ reset_data(data); /* get a reference on walk_path. */ path_get(&walk_path); err = vfs_walk_ancestors(&walk_path, ll_cb, data, 0); /* Now, walk_path might be updated */ /* Always release reference on walk_path */ path_put(&walk_path); }
Quoted 6 lines
BPF path iterator sode will look like:
static bool
bpf_cb(const struct path *ancestor, void *data){ return false; }
Instead of this callback, we could just always return if walk_cb is NULL.
Quoted 16 lines
struct path *bpf_iter_path_next(struct
bpf_iter_path*it){ struct bpf_iter_path_kern *kit = (void *)it; if (vfs_walk_ancestors(&kit->path, bpf_cb, NULL)) return NULL; return &kit->path; }Does this sound reasonable to every body?
Thanks,
Song
Quoted 3 lines
On Jul 9, 2025, at 9:06 AM, Mickaël Salaün <[email protected]> wrote:\
[...]
Quoted 8 lines
Quoted 8 lines
If necessary, we hide “root" inside @data. This is good.
Quoted 1 line
@path would be updated with latest ancestor path (e.g. @root).
Update @path to the last ancestor and hold proper references. I missed this part earlier. With this feature,
vfs_walk_ancestorsshould work usable with open-codeed bpf path iterator.
Quoted 5 lines
Quoted 2 lines
I have a question about this behavior with RCU walk. IIUC, RCU walk does not hold reference to @ancestor when calling
walk_cb().I think a reference to the mount should be held, but not necessarily to the dentry if we are still in the same mount as the original path.
If we update @path and do path_put() after the walk, we have to hold reference to both the mnt and the dentry, no?
Quoted 6 lines
Quoted 3 lines
If
walk_cb()returns false, shallvfs_walk_ancestors()then grab a reference on @ancestor? This feels a bit weird to me.If
walk_cb()checks for a root, it will return false when the path will match, and the caller would expect to get this root path, right?
If the user want to walk to the global root, walk_cb() may not return false at all, IIUC. walk_cb() may also return false on other conditions.
Quoted 16 lines
In general, it's safer to always have the same behavior when holding or releasing a reference. I think the caller should then always call
path_put()aftervfs_walk_ancestors()whatever the return code is.Quoted 11 lines
Maybe “updating @path to the last ancestor” should only apply to LOOKUP_RCU==false case?
Quoted 6 lines
@flags could contain
LOOKUP_RCUor not, which enables us to havewalk_cb()not-RCU compatible.When passing
LOOKUP_RCU, if the first call tovfs_walk_ancestors()failed with-ECHILD, the caller can restart the walk by callingvfs_walk_ancestors()again but withoutLOOKUP_RCU.
Quoted 8 lines
Quoted 4 lines
Given we want callers to handle
-ECHILDand callvfs_walk_ancestorsagain withoutLOOKUP_RCU, I think we should keep @path not changed With LOOKUP_RCU==true, and only update it to the last ancestor when LOOKUP_RCU==false.As Neil said, we don't want to explicitly pass
LOOKUP_RCUas a public flag. Instead,walk_cb()should never sleep (and then potentially be called under RCU by thevfs_walk_ancestors()implementation).
How should the user handle -ECHILD without LOOKUP_RCU flag? Say the following code in landlocked:
/* Try RCU walk first */
err = vfs_walk_ancestors(path, ll_cb, data, LOOKUP_RCU);
if (err == -ECHILD) {
struct path walk_path = *path;
/* reset any data changed by the walk */
reset_data(data);
/* now do ref-walk */
err = vfs_walk_ancestors(&walk_path, ll_cb, data, 0);
}Or do you mean vfs_walk_ancestors will never return -ECHILD? Then we need vfs_walk_ancestors to call reset_data logic, right?
Thanks,
Song
Quoted 75 lines · Song Liu
Quoted 3 lines
On Jul 9, 2025, at 9:06 AM, Mickaël Salaün <[email protected]> wrote:\
[...]
Quoted 8 lines
Quoted 8 lines
If necessary, we hide “root" inside @data. This is good.
Quoted 1 line
@path would be updated with latest ancestor path (e.g. @root).
Update @path to the last ancestor and hold proper references. I missed this part earlier. With this feature,
vfs_walk_ancestorsshould work usable with open-codeed bpf path iterator.Quoted 5 lines
Quoted 2 lines
I have a question about this behavior with RCU walk. IIUC, RCU walk does not hold reference to @ancestor when calling
walk_cb().I think a reference to the mount should be held, but not necessarily to the dentry if we are still in the same mount as the original path.
If we update @path and do
path_put()after the walk, we have to hold reference to both the mnt and the dentry, no?Quoted 6 lines
Quoted 3 lines
If
walk_cb()returns false, shallvfs_walk_ancestors()then grab a reference on @ancestor? This feels a bit weird to me.If
walk_cb()checks for a root, it will return false when the path will match, and the caller would expect to get this root path, right?If the user want to walk to the global root,
walk_cb()may not return false at all, IIUC.walk_cb()may also return false on other conditions.Quoted 16 lines
In general, it's safer to always have the same behavior when holding or releasing a reference. I think the caller should then always call
path_put()aftervfs_walk_ancestors()whatever the return code is.Quoted 11 lines
Maybe “updating @path to the last ancestor” should only apply to LOOKUP_RCU==false case?
Quoted 6 lines
@flags could contain
LOOKUP_RCUor not, which enables us to havewalk_cb()not-RCU compatible.When passing
LOOKUP_RCU, if the first call tovfs_walk_ancestors()failed with-ECHILD, the caller can restart the walk by callingvfs_walk_ancestors()again but withoutLOOKUP_RCU.Quoted 8 lines
Quoted 4 lines
Given we want callers to handle
-ECHILDand callvfs_walk_ancestorsagain withoutLOOKUP_RCU, I think we should keep @path not changed With LOOKUP_RCU==true, and only update it to the last ancestor when LOOKUP_RCU==false.As Neil said, we don't want to explicitly pass
LOOKUP_RCUas a public flag. Instead,walk_cb()should never sleep (and then potentially be called under RCU by thevfs_walk_ancestors()implementation).How should the user handle
-ECHILDwithoutLOOKUP_RCUflag? Say the following code in landlocked:/* Try RCU walk first */
err =vfs_walk_ancestors(path, ll_cb, data, LOOKUP_RCU);if (err == -ECHILD) { struct path walk_path = *path; /* reset any data changed by the walk */ reset_data(data); /* now do ref-walk */ err = vfs_walk_ancestors(&walk_path, ll_cb, data, 0); }
Quoted 2 lines
Or do you mean
vfs_walk_ancestorswill never return-ECHILD? Then we needvfs_walk_ancestorsto callreset_datalogic, right?
It isn't clear to me that vfs_walk_ancestors() needs to return anything. All the communication happens through walk_cb()
walk_cb() is called with a path, the data, and a "may_sleep" flag. If it needs to sleep but may_sleep is not set, it returns "-ECHILD" which causes the walk to restart and use refcounts.
If it wants to stop, it returns 0.
If it wants to continue, it returns 1.
If it wants a reference to the path then it can use (new) vfs_legitimize_path() which might fail.
If it wants a reference to the path and may_sleep is true, it can use path_get() which won't fail.
When returning -ECHILD (either because of a need to sleep or because vfs_legitimize_path() fails), walk_cb() would reset_data().
NeilBrown
Quoted 3 lines · NeilBrown
[...]
Quoted 33 lines
Quoted 13 lines
How should the user handle
-ECHILDwithoutLOOKUP_RCUflag? Say the following code in landlocked:/* Try RCU walk first */
err =vfs_walk_ancestors(path, ll_cb, data, LOOKUP_RCU);if (err ==
-ECHILD) {
struct pathwalk_path= *path;/* reset any data changed by the walk */
reset_data(data);Quoted 6 lines
/* now do ref-walk */
err =vfs_walk_ancestors(&walk_path, ll_cb, data, 0); }Or do you mean
vfs_walk_ancestorswill never return-ECHILD? Then we needvfs_walk_ancestorsto callreset_datalogic, right?It isn't clear to me that
vfs_walk_ancestors()needs to return anything. All the communication happens throughwalk_cb()
walk_cb()is called with a path, the data, and a "may_sleep" flag. If it needs to sleep butmay_sleepis not set, it returns "-ECHILD" which causes the walk to restart and use refcounts.
If it wants to stop, it returns 0.
If it wants to continue, it returns 1.
If it wants a reference to the path then it can use (new)vfs_legitimize_path()which might fail.
If it wants a reference to the path andmay_sleepis true, it can usepath_get()which won't fail.
Quoted 2 lines
When returning
-ECHILD(either because of a need to sleep or becausevfs_legitimize_path()fails),walk_cb()wouldreset_data().
This might actually work.
My only concern is with vfs_legitimize_path. It is probably safer if we only allow taking references with may_sleep==true, so that path_get won’t fail. In this case, we will not need walk_cb() to call vfs_legitimize_path. If the user want a reference, the walk_cb will first return -ECHILD, and call path_get when may_sleep is true.
Does this make sense? Did I miss any cases?
Thanks,
Song
Quoted 40 lines · Song Liu
Quoted 3 lines · NeilBrown
[...]
Quoted 33 lines
Quoted 13 lines
How should the user handle
-ECHILDwithoutLOOKUP_RCUflag? Say the following code in landlocked:/* Try RCU walk first */
err =vfs_walk_ancestors(path, ll_cb, data, LOOKUP_RCU);if (err ==
-ECHILD) {
struct pathwalk_path= *path;/* reset any data changed by the walk */
reset_data(data);Quoted 6 lines
/* now do ref-walk */
err =vfs_walk_ancestors(&walk_path, ll_cb, data, 0); }Or do you mean
vfs_walk_ancestorswill never return-ECHILD? Then we needvfs_walk_ancestorsto callreset_datalogic, right?It isn't clear to me that
vfs_walk_ancestors()needs to return anything. All the communication happens throughwalk_cb()
walk_cb()is called with a path, the data, and a "may_sleep" flag. If it needs to sleep butmay_sleepis not set, it returns "-ECHILD" which causes the walk to restart and use refcounts.
If it wants to stop, it returns 0.
If it wants to continue, it returns 1.
If it wants a reference to the path then it can use (new)vfs_legitimize_path()which might fail.
If it wants a reference to the path andmay_sleepis true, it can usepath_get()which won't fail.Quoted 2 lines
When returning
-ECHILD(either because of a need to sleep or becausevfs_legitimize_path()fails),walk_cb()wouldreset_data().
Quoted 7 lines
This might actually work.
My only concern is with
vfs_legitimize_path. It is probably safer if we only allow taking references with may_sleep==true, so thatpath_getwon’t fail. In this case, we will not needwalk_cb()to callvfs_legitimize_path. If the user want a reference, thewalk_cbwill first return-ECHILD, and callpath_getwhenmay_sleepis true.
What is your concern with vfs_legitimize_path() ??
I've since realised that always restarting in response to -ECHILD isn't necessary and isn't how normal path-walk works. Restarting might be needed, but the first response to -ECHILD is to try legitimize_path(). If that succeeds, then it is safe to sleep.
So returning -ECHILD might just result in vfs_walk_ancestors() calling legitimize_path() and then calling walk_cb() again. Why not have walk_cb() do the vfs_legitimize_path() call (which will almost always succeed in practice).
NeilBrown
Quoted 7 lines
Does this make sense? Did I miss any cases?
Thanks,
Song
Quoted 55 lines · NeilBrown
Quoted 40 lines · Song Liu
Quoted 3 lines · NeilBrown
[...]
Quoted 33 lines
Quoted 13 lines
How should the user handle
-ECHILDwithoutLOOKUP_RCUflag? Say the following code in landlocked:/* Try RCU walk first */
err =vfs_walk_ancestors(path, ll_cb, data, LOOKUP_RCU);if (err ==
-ECHILD) {
struct pathwalk_path= *path;/* reset any data changed by the walk */
reset_data(data);Quoted 6 lines
/* now do ref-walk */
err =vfs_walk_ancestors(&walk_path, ll_cb, data, 0); }Or do you mean
vfs_walk_ancestorswill never return-ECHILD? Then we needvfs_walk_ancestorsto callreset_datalogic, right?It isn't clear to me that
vfs_walk_ancestors()needs to return anything. All the communication happens throughwalk_cb()
walk_cb()is called with a path, the data, and a "may_sleep" flag. If it needs to sleep butmay_sleepis not set, it returns "-ECHILD" which causes the walk to restart and use refcounts.
If it wants to stop, it returns 0.
If it wants to continue, it returns 1.
If it wants a reference to the path then it can use (new)vfs_legitimize_path()which might fail.
If it wants a reference to the path andmay_sleepis true, it can usepath_get()which won't fail.Quoted 2 lines
When returning
-ECHILD(either because of a need to sleep or becausevfs_legitimize_path()fails),walk_cb()wouldreset_data().Quoted 7 lines
This might actually work.
My only concern is with
vfs_legitimize_path. It is probably safer if we only allow taking references with may_sleep==true, so thatpath_getwon’t fail. In this case, we will not needwalk_cb()to callvfs_legitimize_path. If the user want a reference, thewalk_cbwill first return-ECHILD, and callpath_getwhenmay_sleepis true.What is your concern with
vfs_legitimize_path()??
Quoted 8 lines
I've since realised that always restarting in response to
-ECHILDisn't necessary and isn't how normal path-walk works. Restarting might be needed, but the first response to-ECHILDis to trylegitimize_path(). If that succeeds, then it is safe to sleep.
So returning-ECHILDmight just result invfs_walk_ancestors()callinglegitimize_path()and then callingwalk_cb()again. Why not havewalk_cb()do thevfs_legitimize_path()call (which will almost always succeed in practice).
After reading the emails and the code more, I think I misunderstood why we need to call vfs_legitimize_path(). The goal of “legitimize” is to get a reference on @path, so a reference-less walk may not need legitimize_path() at all. Do I get this right this time?
However, I still have some concern with legitimize_path: it requires m_seq and r_seq recorded at the beginning of the walk, do we want to pass those to walk_cb()? IIUC, one of the reason we prefer a callback based solution is that it doesn’t expose nameidata (or a subset of it). Letting walk_cb to call legitimize_path appears to defeat this benefit, no?
A separate question below.
I still have some question about how vfs_walk_ancestors() and the walk_cb() interact. Let’s look at the landlock use case: the user (landlock) just want to look at each ancestor, but doesn’t need to take any references. walk_cb() will check @path against @root, and return 0 when @path is the same as @root.
IIUC, in this case, we will record m_seq and r_seq at the beginning of vfs_walk_ancestors(), and check them against mount_lock and rename_lock at the end of the walk. (Maybe we also need to check them at some points before the end of the walk?) If either seq changed during the walk, we need to restart the walk, and take reference on each step. Did I get this right so far?
If the above is right, here are my questions about the reference-less walk above:
1. Which function (vfs_walk_ancestors or walk_cb) will check m_seq and r_seq? I think vfs_walk_ancestors should check them.
2. When either seq changes, which function will call reset_data? I think there are 3 options here:
2.a: vfs_walk_ancestors calls reset_data, which will be another
callback function the caller passes to vfs_walk_ancestors.
2.b: walk_cb will call reset_data(), but we need a mechanism to
tell walk_cb to do it, maybe a “restart” flag?
2.c: Caller of vfs_walk_ancestors will call reset_data(). In
this case, vfs_walk_ancestors will return -ECHILD to its caller. But I think this option is NACKed.
I think the right solution is to have vfs_walk_ancestors check m_seq and r_seq, and have walk_cb call reset_data. But this is Different to the proposal above.
Do my questions above make any sense? Or maybe I totally misunderstood something?
Thanks,
Song
Quoted 2 lines · Song Liu
[...]
Quoted 78 lines
Quoted 26 lines
Quoted 16 lines
Quoted 13 lines
It isn't clear to me that
vfs_walk_ancestors()needs to return anything. All the communication happens throughwalk_cb()
walk_cb()is called with a path, the data, and a "may_sleep" flag. If it needs to sleep butmay_sleepis not set, it returns "-ECHILD" which causes the walk to restart and use refcounts.
If it wants to stop, it returns 0.
If it wants to continue, it returns 1.
If it wants a reference to the path then it can use (new)vfs_legitimize_path()which might fail.
If it wants a reference to the path andmay_sleepis true, it can usepath_get()which won't fail.Quoted 2 lines
When returning
-ECHILD(either because of a need to sleep or becausevfs_legitimize_path()fails),walk_cb()wouldreset_data().Quoted 7 lines
This might actually work.
My only concern is with
vfs_legitimize_path. It is probably safer if we only allow taking references with may_sleep==true, so thatpath_getwon’t fail. In this case, we will not needwalk_cb()to callvfs_legitimize_path. If the user want a reference, thewalk_cbwill first return-ECHILD, and callpath_getwhenmay_sleepis true.What is your concern with
vfs_legitimize_path()??Quoted 8 lines
I've since realised that always restarting in response to
-ECHILDisn't necessary and isn't how normal path-walk works. Restarting might be needed, but the first response to-ECHILDis to trylegitimize_path(). If that succeeds, then it is safe to sleep.
So returning-ECHILDmight just result invfs_walk_ancestors()callinglegitimize_path()and then callingwalk_cb()again. Why not havewalk_cb()do thevfs_legitimize_path()call (which will almost always succeed in practice).After reading the emails and the code more, I think I misunderstood why we need to call
vfs_legitimize_path(). The goal of “legitimize” is to get a reference on @path, so a reference-less walk may not needlegitimize_path()at all. Do I get this right this time?However, I still have some concern with
legitimize_path: it requiresm_seqandr_seqrecorded at the beginning of the walk, do we want to pass those towalk_cb()? IIUC, one of the reason we prefer a callback based solution is that it doesn’t expose nameidata (or a subset of it). Lettingwalk_cbto calllegitimize_pathappears to defeat this benefit, no?A separate question below.
I still have some question about how
vfs_walk_ancestors()and thewalk_cb()interact. Let’s look at the landlock use case: the user (landlock) just want to look at each ancestor, but doesn’t need to take any references.walk_cb()will check @path against @root, and return 0 when @path is the same as @root.IIUC, in this case, we will record
m_seqandr_seqat the beginning ofvfs_walk_ancestors(), and check them againstmount_lockandrename_lockat the end of the walk. (Maybe we also need to check them at some points before the end of the walk?) If either seq changed during the walk, we need to restart the walk, and take reference on each step. Did I get this right so far?If the above is right, here are my questions about the reference-less walk above:
1. Which function (
vfs_walk_ancestorsorwalk_cb) will checkm_seqandr_seq? I thinkvfs_walk_ancestorsshould check them.
2. When either seq changes, which function will callreset_data? I think there are 3 options here:
2.a:vfs_walk_ancestorscallsreset_data, which will be another
callback function the caller passes tovfs_walk_ancestors.
2.b:walk_cbwill callreset_data(), but we need a mechanism to
tellwalk_cbto do it, maybe a “restart” flag?
2.c: Caller ofvfs_walk_ancestorswill callreset_data(). In
this case,vfs_walk_ancestorswill return-ECHILDto its caller. But I think this option is NACKed.
Quoted 6 lines
I think the right solution is to have
vfs_walk_ancestorscheckm_seqandr_seq, and havewalk_cbcallreset_data. But this is Different to the proposal above.Do my questions above make any sense? Or maybe I totally misunderstood something?
Hi Neil,
Did my questions/comments above make sense? I am hoping we can agree on some design soon.
Christian and Mickaël,
Could you please also share your thoughts on this?
Current requirements from BPF side is straightforward: we just need a mechanism to “walk up one level and hold reference”. So most of the requirement comes from LandLock side.
Thanks,
Song
Mickaël Salaün
maintainerQuoted 45 lines · Song Liu
Quoted 2 lines · Song Liu
[...]
Quoted 40 lines
Quoted 26 lines
Quoted 16 lines
Quoted 13 lines
It isn't clear to me that
vfs_walk_ancestors()needs to return anything. All the communication happens throughwalk_cb()
walk_cb()is called with a path, the data, and a "may_sleep" flag. If it needs to sleep butmay_sleepis not set, it returns "-ECHILD" which causes the walk to restart and use refcounts.
If it wants to stop, it returns 0.
If it wants to continue, it returns 1.
If it wants a reference to the path then it can use (new)vfs_legitimize_path()which might fail.
If it wants a reference to the path andmay_sleepis true, it can usepath_get()which won't fail.Quoted 2 lines
When returning
-ECHILD(either because of a need to sleep or becausevfs_legitimize_path()fails),walk_cb()wouldreset_data().Quoted 7 lines
This might actually work.
My only concern is with
vfs_legitimize_path. It is probably safer if we only allow taking references with may_sleep==true, so thatpath_getwon’t fail. In this case, we will not needwalk_cb()to callvfs_legitimize_path. If the user want a reference, thewalk_cbwill first return-ECHILD, and callpath_getwhenmay_sleepis true.What is your concern with
vfs_legitimize_path()??Quoted 8 lines
I've since realised that always restarting in response to
-ECHILDisn't necessary and isn't how normal path-walk works. Restarting might be needed, but the first response to-ECHILDis to trylegitimize_path(). If that succeeds, then it is safe to sleep.
So returning-ECHILDmight just result invfs_walk_ancestors()callinglegitimize_path()and then callingwalk_cb()again. Why not havewalk_cb()do thevfs_legitimize_path()call (which will almost always succeed in practice).After reading the emails and the code more, I think I misunderstood why we need to call
vfs_legitimize_path(). The goal of “legitimize” is to get a reference on @path, so a reference-less walk may not needlegitimize_path()at all. Do I get this right this time?
Quoted 6 lines
Quoted 6 lines
However, I still have some concern with
legitimize_path: it requiresm_seqandr_seqrecorded at the beginning of the walk, do we want to pass those towalk_cb()? IIUC, one of the reason we prefer a callback based solution is that it doesn’t expose nameidata (or a subset of it). Lettingwalk_cbto calllegitimize_pathappears to defeat this benefit, no?
Yes, walk_cb() should be very light and non-blocking/non-sleepable. If the caller cannot give these guarantees, then it can just pass NULL instead of a valid walk_cb(), and continue the walk (if needed) by calling the vfs_walk_ancentors() helper again, which would not benefit from the RCU optimization in this case.
Before this patch series land, handling of disconnected directories should be well defined, or at least let the caller deal with it. How do you plan to handle disconnected directories for the eBPF use case? See
https://lore.kernel.org/all/[email protected]/
Unfortunately, this issue is not solved for Landlock yet.
Quoted 10 lines
Quoted 10 lines
A separate question below.
I still have some question about how
vfs_walk_ancestors()and thewalk_cb()interact. Let’s look at the landlock use case: the user (landlock) just want to look at each ancestor, but doesn’t need to take any references.walk_cb()will check @path against @root, and return 0 when @path is the same as @root.
Quoted 6 lines
Quoted 6 lines
IIUC, in this case, we will record
m_seqandr_seqat the beginning ofvfs_walk_ancestors(), and check them againstmount_lockandrename_lockat the end of the walk. (Maybe we also need to check them at some points before the end of the walk?) If either seq changed during the walk, we need to restart the walk, and take reference on each step. Did I get this right so far?
I think so. You should get some inspiration from prepend_path().
Quoted 6 lines
Quoted 6 lines
If the above is right, here are my questions about the reference-less walk above:
1. Which function (
vfs_walk_ancestorsorwalk_cb) will checkm_seqandr_seq? I thinkvfs_walk_ancestorsshould check them.
Yes, walk_cb() should be as simple as possible: the simpler version should just return a constant.
Quoted 10 lines
Quoted 10 lines
2. When either seq changes, which function will call
reset_data? I think there are 3 options here:
2.a:vfs_walk_ancestorscallsreset_data, which will be another
callback function the caller passes tovfs_walk_ancestors.
2.b:walk_cbwill callreset_data(), but we need a mechanism to
tellwalk_cbto do it, maybe a “restart” flag?
2.c: Caller ofvfs_walk_ancestorswill callreset_data(). In
this case,vfs_walk_ancestorswill return-ECHILDto its caller. But I think this option is NACKed.
Quoted 3 lines
Quoted 3 lines
I think the right solution is to have
vfs_walk_ancestorscheckm_seqandr_seq, and havewalk_cbcallreset_data. But this is Different to the proposal above.
I'm not sure a reset_data() would be useful if walk_cb() never sleep.
If we really need such reset_data(), a fourth option would be for walk_cb() to return a specific value (an enum instead of a bool) to trigger the reset.
Quoted 9 lines
Quoted 3 lines
Do my questions above make any sense? Or maybe I totally misunderstood something?
Hi Neil,
Did my questions/comments above make sense? I am hoping we can agree on some design soon.
Quoted 7 lines
Christian and Mickaël,
Could you please also share your thoughts on this?
Current requirements from BPF side is straightforward: we just need a mechanism to “walk up one level and hold reference”. So most of the requirement comes from LandLock side.
Have you thought about how to handle disconnected directories?
Quoted 4 lines
Thanks,
Song
Quoted 3 lines · Mickaël Salaün
[...]
Quoted 11 lines
Quoted 11 lines
Quoted 3 lines
Do my questions above make any sense? Or maybe I totally misunderstood something?
Hi Neil,
Did my questions/comments above make sense? I am hoping we can agree on some design soon.
Christian and Mickaël,
Quoted 7 lines
Quoted 5 lines
Could you please also share your thoughts on this?
Current requirements from BPF side is straightforward: we just need a mechanism to “walk up one level and hold reference”. So most of the requirement comes from LandLock side.
Have you thought about how to handle disconnected directories?
In the case of open-coded path iterator, the iterator will return a special value for disconnected roots and disconnected directories. Then the BPF program need to handle them based on the policy.
Thanks,
Song
Quoted 58 lines · Song Liu
Hi Christian,
Thanks for your comments!
Quoted 1 line · Christian Brauner
[...]
Quoted 34 lines
Quoted 3 lines
Quoted 1 line
3/ Extend
vfs_walk_ancestors()to pass a "may sleep" flag to the callback.I think that's fine.
Ok, sorry for the delay but there's a lot of different things going on right now and this one isn't exactly an easy thing to solve.
I mentioned this before and so did Neil: the lookup implementation supports two modes sleeping and non-sleeping. That api is abstracted away as heavily as possible by the VFS so that non-core code will not be exposed to it other than in exceptional circumstances and doesn't have to care about it.
It is a conceptual dead-end to expose these two modes via separate APIs and leak this implementation detail into non-core code. It will not happen as far as I'm concerned.
I very much understand the urge to get the refcount step-by-step thing merged asap. Everyone wants their APIs merged fast. And if it's reasonable to move fast we will (see the kernfs xattr thing).
But here are two use-cases that ask for the same thing with different constraints that closely mirror our unified approach. Merging one quickly just to have something and then later bolting the other one on top, augmenting, or replacing, possible having to deprecate the old API is just objectively nuts. That's how we end up with a spaghetthi helper collection. We want as little helper fragmentation as possible.
We need a unified API that serves both use-cases. I dislike callback-based APIs generally but we have precedent in the VFS for this for cases where the internal state handling is delicate enough that it should not be exposed (see
__iterate_supers()which does exactly work like Neil suggested down to the flag argument itself I added).Quoted 4 lines
So I'm open to the callback solution.
(Note for really absurd perf requirements you could even make it work with static calls I'm pretty sure.)
I guess we will go with Mickaël’s idea:
Quoted 8 lines
int vfs_walk_ancestors(struct path *path,
bool (*walk_cb)(const struct path *ancestor, void *data), void *data, int flags)The walk continue while
walk_cb()returns true.walk_cb()can then check if @ancestor is equal to a @root, or other properties. Thewalk_cb()return value (if not bool) should not be returned byvfs_walk_ancestors()because a walk stop doesn't mean an error.
Quoted 7 lines
If necessary, we hide “root" inside @data. This is good.
Quoted 1 line
@path would be updated with latest ancestor path (e.g. @root).
Update @path to the last ancestor and hold proper references. I missed this part earlier. With this feature,
vfs_walk_ancestorsshould work usable with open-codeed bpf path iterator.
I don't think path should be updated. That adds complexity which might not be needed. The original (landlock) requirements were only to look at each ancestor, not to take a reference to any of them.
If the caller needs a reference to any of the ancestors I think that walk_cb() needs to take that reference and store it in data. Note that attempting to take the reference might fail. See legitimize_path() in fs/namei.c.
It isn't yet clear to me what would be a good API for requesting the reference.
One option would be for vfs_walk_ancestors() to pass another void* to walk_cb(), and it passed it on to vfs_legitimize_path() which extracts the seq numbers from there.
Another might be that the path passed to walk_cb is always nameidata.path, and so when that is passed to vfs_legitimize_path() path it can use container_of() to find the seq numbers.
If vfs_legitimize_path() fail, walk_cb() might want to ask for the walk to be restarted.
Quoted 16 lines
I have a question about this behavior with RCU walk. IIUC, RCU walk does not hold reference to @ancestor when calling
walk_cb(). Ifwalk_cb()returns false, shallvfs_walk_ancestors()then grab a reference on @ancestor? This feels a bit weird to me. Maybe “updating @path to the last ancestor” should only apply to LOOKUP_RCU==false case?Quoted 6 lines
@flags could contain
LOOKUP_RCUor not, which enables us to havewalk_cb()not-RCU compatible.When passing
LOOKUP_RCU, if the first call tovfs_walk_ancestors()failed with-ECHILD, the caller can restart the walk by callingvfs_walk_ancestors()again but withoutLOOKUP_RCU.
Quoted 4 lines
Given we want callers to handle
-ECHILDand callvfs_walk_ancestorsagain withoutLOOKUP_RCU, I think we should keep @path not changed With LOOKUP_RCU==true, and only update it to the last ancestor when LOOKUP_RCU==false.
No, we really don't want to pass a LOOKUP_RCU() flag to vfs_walk_ancestors().vfs_walk_ancestors() might choose to pass that flag to walk_cb().
NeilBrown
Quoted 67 lines · NeilBrown
Quoted 62 lines · Song Liu
Hi Christian,
Thanks for your comments!
Quoted 1 line · Christian Brauner
[...]
Quoted 34 lines
Quoted 3 lines
Quoted 1 line
3/ Extend
vfs_walk_ancestors()to pass a "may sleep" flag to the callback.I think that's fine.
Ok, sorry for the delay but there's a lot of different things going on right now and this one isn't exactly an easy thing to solve.
I mentioned this before and so did Neil: the lookup implementation supports two modes sleeping and non-sleeping. That api is abstracted away as heavily as possible by the VFS so that non-core code will not be exposed to it other than in exceptional circumstances and doesn't have to care about it.
It is a conceptual dead-end to expose these two modes via separate APIs and leak this implementation detail into non-core code. It will not happen as far as I'm concerned.
I very much understand the urge to get the refcount step-by-step thing merged asap. Everyone wants their APIs merged fast. And if it's reasonable to move fast we will (see the kernfs xattr thing).
But here are two use-cases that ask for the same thing with different constraints that closely mirror our unified approach. Merging one quickly just to have something and then later bolting the other one on top, augmenting, or replacing, possible having to deprecate the old API is just objectively nuts. That's how we end up with a spaghetthi helper collection. We want as little helper fragmentation as possible.
We need a unified API that serves both use-cases. I dislike callback-based APIs generally but we have precedent in the VFS for this for cases where the internal state handling is delicate enough that it should not be exposed (see
__iterate_supers()which does exactly work like Neil suggested down to the flag argument itself I added).Quoted 4 lines
So I'm open to the callback solution.
(Note for really absurd perf requirements you could even make it work with static calls I'm pretty sure.)
I guess we will go with Mickaël’s idea:
Quoted 8 lines
int vfs_walk_ancestors(struct path *path,
bool (*walk_cb)(const struct path *ancestor, void *data), void *data, int flags)The walk continue while
walk_cb()returns true.walk_cb()can then check if @ancestor is equal to a @root, or other properties. Thewalk_cb()return value (if not bool) should not be returned byvfs_walk_ancestors()because a walk stop doesn't mean an error.If necessary, we hide “root" inside @data. This is good.
Quoted 1 line
@path would be updated with latest ancestor path (e.g. @root).
Quoted 7 lines
Quoted 3 lines
Update @path to the last ancestor and hold proper references. I missed this part earlier. With this feature,
vfs_walk_ancestorsshould work usable with open-codeed bpf path iterator.I don't think path should be updated. That adds complexity which might not be needed. The original (landlock) requirements were only to look at each ancestor, not to take a reference to any of them.
I think this is the ideal case that landlock wants in the long term. But we may need to take references when the attempt fails. Also, current landlock code takes reference at each step.
Quoted 5 lines
If the caller needs a reference to any of the ancestors I think that
walk_cb()needs to take that reference and store it in data. Note that attempting to take the reference might fail. Seelegitimize_path()infs/namei.c.
Quoted 8 lines
It isn't yet clear to me what would be a good API for requesting the reference.
One option would be forvfs_walk_ancestors()to pass another void* towalk_cb(), and it passed it on tovfs_legitimize_path()which extracts the seq numbers from there.
Another might be that the path passed towalk_cbis always nameidata.path, and so when that is passed tovfs_legitimize_path()path it can usecontainer_of()to find the seq numbers.
Letting walk_cb() call vfs_legitimize_path() seems suboptimal to me. I think the original goal is to have vfs_walk_ancestors() to:
1. Try to walk the ancestors without taking any references;
2. Detect when the not-taking-reference walk is not reliable;
3. Maybe, retry the walk from beginning, but takes references on each step.
With walk_cb() calling vfs_legitimize_path(), we are moving #2 above to walk_cb(). I think this is not what we want?
Quoted 19 lines
If
vfs_legitimize_path()fail,walk_cb()might want to ask for the walk to be restarted.Quoted 16 lines
I have a question about this behavior with RCU walk. IIUC, RCU walk does not hold reference to @ancestor when calling
walk_cb(). Ifwalk_cb()returns false, shallvfs_walk_ancestors()then grab a reference on @ancestor? This feels a bit weird to me. Maybe “updating @path to the last ancestor” should only apply to LOOKUP_RCU==false case?Quoted 6 lines
@flags could contain
LOOKUP_RCUor not, which enables us to havewalk_cb()not-RCU compatible.When passing
LOOKUP_RCU, if the first call tovfs_walk_ancestors()failed with-ECHILD, the caller can restart the walk by callingvfs_walk_ancestors()again but withoutLOOKUP_RCU.
Quoted 8 lines
Quoted 4 lines
Given we want callers to handle
-ECHILDand callvfs_walk_ancestorsagain withoutLOOKUP_RCU, I think we should keep @path not changed With LOOKUP_RCU==true, and only update it to the last ancestor when LOOKUP_RCU==false.No, we really don't want to pass a
LOOKUP_RCU()flag tovfs_walk_ancestors().vfs_walk_ancestors()might choose to pass that flag towalk_cb().
In this case, we will need vfs_walk_ancestors to handle #3 above.
Thanks,
Song
Quoted 71 lines · Song Liu
Quoted 71 lines · NeilBrown
Quoted 65 lines · Song Liu
Hi Christian,
Thanks for your comments!
Quoted 1 line · Christian Brauner
[...]
Quoted 34 lines
Quoted 3 lines
Quoted 1 line
3/ Extend
vfs_walk_ancestors()to pass a "may sleep" flag to the callback.I think that's fine.
Ok, sorry for the delay but there's a lot of different things going on right now and this one isn't exactly an easy thing to solve.
I mentioned this before and so did Neil: the lookup implementation supports two modes sleeping and non-sleeping. That api is abstracted away as heavily as possible by the VFS so that non-core code will not be exposed to it other than in exceptional circumstances and doesn't have to care about it.
It is a conceptual dead-end to expose these two modes via separate APIs and leak this implementation detail into non-core code. It will not happen as far as I'm concerned.
I very much understand the urge to get the refcount step-by-step thing merged asap. Everyone wants their APIs merged fast. And if it's reasonable to move fast we will (see the kernfs xattr thing).
But here are two use-cases that ask for the same thing with different constraints that closely mirror our unified approach. Merging one quickly just to have something and then later bolting the other one on top, augmenting, or replacing, possible having to deprecate the old API is just objectively nuts. That's how we end up with a spaghetthi helper collection. We want as little helper fragmentation as possible.
We need a unified API that serves both use-cases. I dislike callback-based APIs generally but we have precedent in the VFS for this for cases where the internal state handling is delicate enough that it should not be exposed (see
__iterate_supers()which does exactly work like Neil suggested down to the flag argument itself I added).Quoted 4 lines
So I'm open to the callback solution.
(Note for really absurd perf requirements you could even make it work with static calls I'm pretty sure.)
I guess we will go with Mickaël’s idea:
Quoted 8 lines
int vfs_walk_ancestors(struct path *path,
bool (*walk_cb)(const struct path *ancestor, void *data), void *data, int flags)The walk continue while
walk_cb()returns true.walk_cb()can then check if @ancestor is equal to a @root, or other properties. Thewalk_cb()return value (if not bool) should not be returned byvfs_walk_ancestors()because a walk stop doesn't mean an error.If necessary, we hide “root" inside @data. This is good.
Quoted 1 line
@path would be updated with latest ancestor path (e.g. @root).
Update @path to the last ancestor and hold proper references. I missed this part earlier. With this feature,
vfs_walk_ancestorsshould work usable with open-codeed bpf path iterator.
Quoted 7 lines
Quoted 3 lines
I don't think path should be updated. That adds complexity which might not be needed. The original (landlock) requirements were only to look at each ancestor, not to take a reference to any of them.
I think this is the ideal case that landlock wants in the long term. But we may need to take references when the attempt fails. Also, current landlock code takes reference at each step.
Why may be need to?
Yes, current landlock code takes references, but I don't think that is because it needs references, only because the API requires it to take references.
Quoted 22 lines
Quoted 6 lines
If the caller needs a reference to any of the ancestors I think that
walk_cb()needs to take that reference and store it in data. Note that attempting to take the reference might fail. Seelegitimize_path()infs/namei.c.Quoted 8 lines
It isn't yet clear to me what would be a good API for requesting the reference.
One option would be forvfs_walk_ancestors()to pass another void* towalk_cb(), and it passed it on tovfs_legitimize_path()which extracts the seq numbers from there.
Another might be that the path passed towalk_cbis always nameidata.path, and so when that is passed tovfs_legitimize_path()path it can usecontainer_of()to find the seq numbers.Letting
walk_cb()callvfs_legitimize_path()seems suboptimal to me. I think the original goal is to havevfs_walk_ancestors()to:
1. Try to walk the ancestors without taking any references;
2. Detect when the not-taking-reference walk is not reliable;
3. Maybe, retry the walk from beginning, but takes references on each step.
Quoted 2 lines
With
walk_cb()callingvfs_legitimize_path(), we are moving #2 above towalk_cb(). I think this is not what we want?
I think you are looking at this the wrong way around. Focus on the needs for the caller, not on how you think it might be implemented.
If the caller needs a reference, there should be a way for it to get a reference. This is quite separate from the choices vfs_walk_ancestors() makes about how it is going to walk the list of dentries.
NeilBrown
Christian Brauner
maintainerQuoted 4 lines
Those are implementation details internal to
namei.c. Certainly this function wouldn't use all of the fields in nameidata, but it doesn't hurt to have a few fields in a struct on the stack which don't get used. Keeping the code simple and uniform is much more important. Using
Exactly.
Quoted 5 lines
Certainly
vfs_walk_ancestors()would fallback to ref-walk if rcu-walk resulted in-ECHILD- just like all other path walking code innamei.c. This would be largely transparent to the caller - the caller would only see that the callback received a NULL path indicating a restart. It wouldn't need to know why.
Yes, that's also what I mentioned in an earlier mail.
Song Liu
reviewerHi Christian,
Quoted 7 lines · Mickaël Salaün
Quoted 4 lines · Song Liu
Hi Christian, Mickaël, and folks,
Could you please share your comments on this version? Does this look sane?
Quoted 6 lines
This looks good to me but we need to know what is the acceptable next step to support RCU. If we can go with another
_rcuhelper, I'm good with the current approach, otherwise we need to figure out a way to leverage the current helper to make it compatible with callers being in a RCU read-side critical section while leveraging safe path walk (i.e. several calls topath_walk_parent).
Could you please share your suggestions on this topic? RCU protected path walk out of fs/ seems controversial in multiple ways. Do we have to let this set wait indefinitely for a solution of RCU protected path walk? I would like to highlight that this set doesn't add any persistent APIs. path_walk_parent() is not in the UAPI, nor exported. If a newer and better API is created, we can refactor bpf and landlock code and deprecate path_walk_parent().
Thanks,
Song