Skip to contentSkip to conversation

dcache: touch up predicts in __d_lookup_rcu()

PATCH Applied Mateusz Guzik 3 participants 3 messages Nov 27, 2025 – Nov 28, 2025 VFS

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 dcache: touch up predicts in __d_lookup_rcu()

Rationale is that if the parent dentry is the same and the length is the same, then you have to be unlucky for the name to not match.

At the same time the dentry was literally just found on the hash, so you have to be even more unlucky to determine it is unhashed.

While here add commentary while d_unhashed() is necessary. It was
already removed once and brought back in:
2e321806b681b192 ("Revert "vfs: remove unnecessary d_unhashed() check from __d_lookup_rcu"")

---

- move and precit on d_unhashed as well
- add commentary on it

this obsoletes https://lore.kernel.org/linux-fsdevel/[email protected]/T/#u

 fs/dcache.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
fs/dcache.c +12 -3
index 23d1752c29e6..dc2fff4811d1 100644
@@ -2342,11 +2342,20 @@ struct dentry *__d_lookup_rcu(const struct dentry *parent,
 		seq = raw_seqcount_begin(&dentry->d_seq);
 		if (dentry->d_parent != parent)
 			continue;
-		if (d_unhashed(dentry))
-			continue;
 		if (dentry->d_name.hash_len != hashlen)
 			continue;
-		if (dentry_cmp(dentry, str, hashlen_len(hashlen)) != 0)
+		if (unlikely(dentry_cmp(dentry, str, hashlen_len(hashlen)) != 0))
+			continue;
+		/*
+		 * Check for the dentry being unhashed.
+		 *
+		 * As tempting as it is, we *can't* skip it because of a race window
+		 * between us finding the dentry before it gets unhashed and loading
+		 * the sequence counter after unhashing is finished.
+		 *
+		 * We can at least predict on it.
+		 */
+		if (unlikely(d_unhashed(dentry)))
 			continue;
 		*seqp = seq;
 		return dentry;
Signature · 1 line
2.34.1
reviewer Reviewed-by on v2 1/1
#1 lore raw
Quoted 11 lines · Mateusz Guzik

Rationale is that if the parent dentry is the same and the length is the same, then you have to be unlucky for the name to not match.

At the same time the dentry was literally just found on the hash, so you have to be even more unlucky to determine it is unhashed.

While here add commentary while d_unhashed() is necessary. It was
already removed once and brought back in:
2e321806b681b192 ("Revert "vfs: remove unnecessary d_unhashed() check from __d_lookup_rcu"")

Looks good. Feel free to add:

Honza

Quoted 41 lines

---

- move and precit on d_unhashed as well
- add commentary on it

this obsoletes https://lore.kernel.org/linux-fsdevel/[email protected]/T/#u

 fs/dcache.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
fs/dcache.c +12 -3
index 23d1752c29e6..dc2fff4811d1 100644
@@ -2342,11 +2342,20 @@ struct dentry *__d_lookup_rcu(const struct dentry *parent,
 		seq = raw_seqcount_begin(&dentry->d_seq);
 		if (dentry->d_parent != parent)
 			continue;
-		if (d_unhashed(dentry))
-			continue;
 		if (dentry->d_name.hash_len != hashlen)
 			continue;
-		if (dentry_cmp(dentry, str, hashlen_len(hashlen)) != 0)
+		if (unlikely(dentry_cmp(dentry, str, hashlen_len(hashlen)) != 0))
+			continue;
+		/*
+		 * Check for the dentry being unhashed.
+		 *
+		 * As tempting as it is, we *can't* skip it because of a race window
+		 * between us finding the dentry before it gets unhashed and loading
+		 * the sequence counter after unhashing is finished.
+		 *
+		 * We can at least predict on it.
+		 */
+		if (unlikely(d_unhashed(dentry)))
 			continue;
 		*seqp = seq;
 		return dentry;
Signature · 1 line
2.34.1
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer Applied
#2 lore raw
In reply to Mateusz Guzik
Quoted 11 lines · Mateusz Guzik

Rationale is that if the parent dentry is the same and the length is the same, then you have to be unlucky for the name to not match.

At the same time the dentry was literally just found on the hash, so you have to be even more unlucky to determine it is unhashed.

While here add commentary while d_unhashed() is necessary. It was
already removed once and brought back in:
2e321806b681b192 ("Revert "vfs: remove unnecessary d_unhashed() check from __d_lookup_rcu"")

[...]

Applied to the vfs-6.19.inode branch of the vfs/vfs.git tree. Patches in the vfs-6.19.inode 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.inode

[1/1] dcache: touch up predicts in __d_lookup_rcu()

      https://git.kernel.org/vfs/vfs/c/ca0d620b0afa