[PATCH v2] fs: avoid calls to legitimize_links() if possible
From: Mateusz Guzik
Date: Mon Nov 10 2025 - 05:10:54 EST
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@xxxxxxxxx>
---
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