[PATCH v2] fs: avoid calls to legitimize_links() if possible

Mateusz Guzik posted 1 patch 1 month, 1 week ago
fs/namei.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
[PATCH v2] fs: avoid calls to legitimize_links() if possible
Posted by Mateusz Guzik 1 month, 1 week ago
The routine is always called towards the end of lookup.

According to bpftrace on my boxen and boxen of people I asked, the depth
count is almost always 0, thus the call can be avoided in the common case.

one-liner:
bpftrace -e 'kprobe:legitimize_links { @[((struct nameidata *)arg0)->depth] = count(); }'

sample results from few minutes of tracing:
@[1]: 59
@[0]: 147236

@[2]: 1
@[1]: 12087
@[0]: 5926235

And of course the venerable kernel build:
@[1]: 3563
@[0]: 6625425

Signed-off-by: Mateusz Guzik <mjguzik@gmail.com>
---

v2:
- drop 'noinline'
- spell out the check at call sites

verified no change in asm

 fs/namei.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/fs/namei.c b/fs/namei.c
index 2a112b2c0951..0de0344a2ab2 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -882,7 +882,7 @@ static bool try_to_unlazy(struct nameidata *nd)
 
 	BUG_ON(!(nd->flags & LOOKUP_RCU));
 
-	if (unlikely(!legitimize_links(nd)))
+	if (unlikely(nd->depth && !legitimize_links(nd)))
 		goto out1;
 	if (unlikely(!legitimize_path(nd, &nd->path, nd->seq)))
 		goto out;
@@ -917,7 +917,7 @@ static bool try_to_unlazy_next(struct nameidata *nd, struct dentry *dentry)
 	int res;
 	BUG_ON(!(nd->flags & LOOKUP_RCU));
 
-	if (unlikely(!legitimize_links(nd)))
+	if (unlikely(nd->depth && !legitimize_links(nd)))
 		goto out2;
 	res = __legitimize_mnt(nd->path.mnt, nd->m_seq);
 	if (unlikely(res)) {
-- 
2.48.1
Re: [PATCH v2] fs: avoid calls to legitimize_links() if possible
Posted by Christian Brauner 1 month, 1 week ago
On Mon, 10 Nov 2025 11:05:03 +0100, Mateusz Guzik wrote:
> The routine is always called towards the end of lookup.
> 
> According to bpftrace on my boxen and boxen of people I asked, the depth
> count is almost always 0, thus the call can be avoided in the common case.
> 
> one-liner:
> bpftrace -e 'kprobe:legitimize_links { @[((struct nameidata *)arg0)->depth] = count(); }'
> 
> [...]

Applied to the vfs-6.19.misc branch of the vfs/vfs.git tree.
Patches in the vfs-6.19.misc branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a
new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the
patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase,
trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs-6.19.misc

[1/1] fs: avoid calls to legitimize_links() if possible
      https://git.kernel.org/vfs/vfs/c/ab328bc1eb61
Re: [PATCH v2] fs: avoid calls to legitimize_links() if possible
Posted by Mateusz Guzik 1 month, 1 week ago
this patch is obsolete, I posted a v3 (and later v4) with more
predicts in the area:
https://lore.kernel.org/linux-fsdevel/20251110165901.1491476-1-mjguzik@gmail.com/

but that will require at least a v5 later

tl;dr please drop this patch

On Tue, Nov 11, 2025 at 10:47 AM Christian Brauner <brauner@kernel.org> wrote:
>
> On Mon, 10 Nov 2025 11:05:03 +0100, Mateusz Guzik wrote:
> > The routine is always called towards the end of lookup.
> >
> > According to bpftrace on my boxen and boxen of people I asked, the depth
> > count is almost always 0, thus the call can be avoided in the common case.
> >
> > one-liner:
> > bpftrace -e 'kprobe:legitimize_links { @[((struct nameidata *)arg0)->depth] = count(); }'
> >
> > [...]
>
> Applied to the vfs-6.19.misc branch of the vfs/vfs.git tree.
> Patches in the vfs-6.19.misc branch should appear in linux-next soon.
>
> Please report any outstanding bugs that were missed during review in a
> new review to the original patch series allowing us to drop it.
>
> It's encouraged to provide Acked-bys and Reviewed-bys even though the
> patch has now been applied. If possible patch trailers will be updated.
>
> Note that commit hashes shown below are subject to change due to rebase,
> trailer updates or similar. If in doubt, please check the listed branch.
>
> tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
> branch: vfs-6.19.misc
>
> [1/1] fs: avoid calls to legitimize_links() if possible
>       https://git.kernel.org/vfs/vfs/c/ab328bc1eb61
Re: [PATCH v2] fs: avoid calls to legitimize_links() if possible
Posted by Jan Kara 1 month, 1 week ago
On Mon 10-11-25 11:05:03, Mateusz Guzik wrote:
> The routine is always called towards the end of lookup.
> 
> According to bpftrace on my boxen and boxen of people I asked, the depth
> count is almost always 0, thus the call can be avoided in the common case.
> 
> one-liner:
> bpftrace -e 'kprobe:legitimize_links { @[((struct nameidata *)arg0)->depth] = count(); }'
> 
> sample results from few minutes of tracing:
> @[1]: 59
> @[0]: 147236
> 
> @[2]: 1
> @[1]: 12087
> @[0]: 5926235
> 
> And of course the venerable kernel build:
> @[1]: 3563
> @[0]: 6625425
> 
> Signed-off-by: Mateusz Guzik <mjguzik@gmail.com>

Looks good. Feel free to add:

Reviewed-by: Jan Kara <jack@suse.cz>

								Honza

> ---
> 
> v2:
> - drop 'noinline'
> - spell out the check at call sites
> 
> verified no change in asm
> 
>  fs/namei.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/fs/namei.c b/fs/namei.c
> index 2a112b2c0951..0de0344a2ab2 100644
> --- a/fs/namei.c
> +++ b/fs/namei.c
> @@ -882,7 +882,7 @@ static bool try_to_unlazy(struct nameidata *nd)
>  
>  	BUG_ON(!(nd->flags & LOOKUP_RCU));
>  
> -	if (unlikely(!legitimize_links(nd)))
> +	if (unlikely(nd->depth && !legitimize_links(nd)))
>  		goto out1;
>  	if (unlikely(!legitimize_path(nd, &nd->path, nd->seq)))
>  		goto out;
> @@ -917,7 +917,7 @@ static bool try_to_unlazy_next(struct nameidata *nd, struct dentry *dentry)
>  	int res;
>  	BUG_ON(!(nd->flags & LOOKUP_RCU));
>  
> -	if (unlikely(!legitimize_links(nd)))
> +	if (unlikely(nd->depth && !legitimize_links(nd)))
>  		goto out2;
>  	res = __legitimize_mnt(nd->path.mnt, nd->m_seq);
>  	if (unlikely(res)) {
> -- 
> 2.48.1
> 
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR