* [PATCH] fs: avoid calls to legitimize_links() if possible
@ 2025-11-09 18:54 Mateusz Guzik
2025-11-10 1:27 ` Al Viro
0 siblings, 1 reply; 2+ messages in thread
From: Mateusz Guzik @ 2025-11-09 18:54 UTC (permalink / raw)
To: brauner; +Cc: viro, jack, linux-kernel, linux-fsdevel, Mateusz Guzik
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>
---
fs/namei.c | 19 ++++++++++++++-----
1 file changed, 14 insertions(+), 5 deletions(-)
diff --git a/fs/namei.c b/fs/namei.c
index 2a112b2c0951..d89937c2c0b2 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -826,7 +826,7 @@ static inline bool legitimize_path(struct nameidata *nd,
return __legitimize_path(path, seq, nd->m_seq);
}
-static bool legitimize_links(struct nameidata *nd)
+static noinline bool legitimize_links(struct nameidata *nd)
{
int i;
if (unlikely(nd->flags & LOOKUP_CACHED)) {
@@ -845,6 +845,11 @@ static bool legitimize_links(struct nameidata *nd)
return true;
}
+static __always_inline bool need_legitimize_links(struct nameidata *nd)
+{
+ return nd->depth > 0;
+}
+
static bool legitimize_root(struct nameidata *nd)
{
/* Nothing to do if nd->root is zero or is managed by the VFS user. */
@@ -882,8 +887,10 @@ static bool try_to_unlazy(struct nameidata *nd)
BUG_ON(!(nd->flags & LOOKUP_RCU));
- if (unlikely(!legitimize_links(nd)))
- goto out1;
+ if (unlikely(need_legitimize_links(nd))) {
+ if (unlikely(!legitimize_links(nd)))
+ goto out1;
+ }
if (unlikely(!legitimize_path(nd, &nd->path, nd->seq)))
goto out;
if (unlikely(!legitimize_root(nd)))
@@ -917,8 +924,10 @@ 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)))
- goto out2;
+ if (unlikely(need_legitimize_links(nd))) {
+ if (unlikely(!legitimize_links(nd)))
+ goto out2;
+ }
res = __legitimize_mnt(nd->path.mnt, nd->m_seq);
if (unlikely(res)) {
if (res > 0)
--
2.48.1
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] fs: avoid calls to legitimize_links() if possible
2025-11-09 18:54 [PATCH] fs: avoid calls to legitimize_links() if possible Mateusz Guzik
@ 2025-11-10 1:27 ` Al Viro
0 siblings, 0 replies; 2+ messages in thread
From: Al Viro @ 2025-11-10 1:27 UTC (permalink / raw)
To: Mateusz Guzik; +Cc: brauner, jack, linux-kernel, linux-fsdevel
On Sun, Nov 09, 2025 at 07:54:09PM +0100, Mateusz Guzik wrote:
> @@ -882,8 +887,10 @@ static bool try_to_unlazy(struct nameidata *nd)
>
> BUG_ON(!(nd->flags & LOOKUP_RCU));
>
> - if (unlikely(!legitimize_links(nd)))
> - goto out1;
> + if (unlikely(need_legitimize_links(nd))) {
> + if (unlikely(!legitimize_links(nd)))
> + goto out1;
> + }
> if (unlikely(!legitimize_path(nd, &nd->path, nd->seq)))
> goto out;
> if (unlikely(!legitimize_root(nd)))
> @@ -917,8 +924,10 @@ 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)))
> - goto out2;
> + if (unlikely(need_legitimize_links(nd))) {
> + if (unlikely(!legitimize_links(nd)))
> + goto out2;
> + }
> res = __legitimize_mnt(nd->path.mnt, nd->m_seq);
> if (unlikely(res)) {
> if (res > 0)
Seeing that odds of extra callers showing up are pretty much nil,
I think your need_legitimize_links() is only obfuscating things.
Let's just make it
if (unlikely(nd->depth) && !legitimize_links(nd))
goto ...
and be done with that.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-11-10 1:27 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-09 18:54 [PATCH] fs: avoid calls to legitimize_links() if possible Mateusz Guzik
2025-11-10 1:27 ` Al Viro
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).