[PATCH v3 1/9] vfs: introduce d_is_negative_acquire()

From: NeilBrown

Date: Wed Dec 02 2026 - 22:51:42 EST


From: NeilBrown <neil@xxxxxxxxxx>

As discussed in
Commit: 2fa6b1e01a9b ("fs/namei.c: fix missing barriers when checking positivity")
d_is_negative() is only safe when the dentry is locked against change,
either by d_lock or parent's i_rwsem. When not locked an acquire
barrier is needed.

A recent change introduced a new use of d_is_negative() is a non-locked
situation.

To make it easier to get this sort of thing right, introduce
d_is_negative_acquire(), document when it should be used and when
d_is_negative() should be avoided, and use d_is_negative_acquire() where
appropriate.

Fixes: 98b6cf102b35 ("namei: test for a negative dentry the way traverse_mounts() does")
Signed-off-by: NeilBrown <neil@xxxxxxxxxx>
---
fs/dcache.c | 1 +
fs/namei.c | 8 ++++----
fs/overlayfs/namei.c | 2 +-
include/linux/dcache.h | 34 ++++++++++++++++++++++++++++++++++
4 files changed, 40 insertions(+), 5 deletions(-)

diff --git a/fs/dcache.c b/fs/dcache.c
index a438a7de43f0..af67582a0a7d 100644
--- a/fs/dcache.c
+++ b/fs/dcache.c
@@ -427,6 +427,7 @@ static inline void __d_set_inode_and_type(struct dentry *dentry,
flags = READ_ONCE(dentry->d_flags);
flags &= ~DCACHE_ENTRY_TYPE;
flags |= type_flags;
+ /* release matches acquire in d_is_negative_acquire() */
smp_store_release(&dentry->d_flags, flags);
}

diff --git a/fs/namei.c b/fs/namei.c
index c79eecffe4ac..3dc2cf170aa2 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -3329,7 +3329,7 @@ struct dentry *lookup_one_positive_killable(const struct mnt_idmap *idmap,
ret = lookup_dcache(name, base, 0);
if (!ret)
ret = lookup_slow_killable(name, base, 0);
- if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) {
+ if (!IS_ERR(ret) && d_is_negative_acquire(ret)) {
dput(ret);
ret = ERR_PTR(-ENOENT);
}
@@ -3364,7 +3364,7 @@ struct dentry *lookup_one_positive_unlocked(const struct mnt_idmap *idmap,
{
struct dentry *ret = lookup_one_unlocked(idmap, name, base);

- if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) {
+ if (!IS_ERR(ret) && d_is_negative_acquire(ret)) {
dput(ret);
ret = ERR_PTR(-ENOENT);
}
@@ -3424,7 +3424,7 @@ struct dentry *lookup_noperm_positive_unlocked(struct qstr *name,
struct dentry *ret;

ret = lookup_noperm_unlocked(name, base);
- if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) {
+ if (!IS_ERR(ret) && d_is_negative_acquire(ret)) {
dput(ret);
ret = ERR_PTR(-ENOENT);
}
@@ -4830,7 +4830,7 @@ static struct dentry *lookup_fast_for_open(struct nameidata *nd, int open_flag)
return dentry;

if (open_flag & O_CREAT) {
- if (d_is_negative(dentry)) {
+ if (d_is_negative_acquire(dentry)) {
if (!(nd->flags & LOOKUP_RCU))
dput(dentry);
dentry = NULL;
diff --git a/fs/overlayfs/namei.c b/fs/overlayfs/namei.c
index ddf60254888a..8fa8c168dd50 100644
--- a/fs/overlayfs/namei.c
+++ b/fs/overlayfs/namei.c
@@ -216,7 +216,7 @@ static struct dentry *ovl_lookup_positive_unlocked(struct ovl_lookup_data *d,
struct dentry *ret = lookup_one_unlocked(mnt_idmap(d->layer->mnt),
&QSTR_LEN(name, len), base);

- if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) {
+ if (!IS_ERR(ret) && d_is_negative_acquire(ret)) {
if (drop_negative && ret->d_lockref.count == 1) {
spin_lock(&ret->d_lock);
/* Recheck condition under lock */
diff --git a/include/linux/dcache.h b/include/linux/dcache.h
index 1d03b3c6e081..7c03a862f675 100644
--- a/include/linux/dcache.h
+++ b/include/linux/dcache.h
@@ -498,6 +498,16 @@ static inline bool d_is_file(const struct dentry *dentry)
return d_is_reg(dentry) || d_is_special(dentry);
}

+/**
+ * d_is_negative - test if a dentry is negative
+ * @dentry: the dentry to test
+ *
+ * This should only be used if the dentry is locked against
+ * change, either by ->d_lock or by parent's ->i_rwsem.
+ * Outside this context use d_really_is_negative() if the sole
+ * purpose is to determine if it is safe to dereference the inode,
+ * or use d_is_negative_acquire() if the context is more general.
+ */
static inline bool d_is_negative(const struct dentry *dentry)
{
// TODO: check d_is_whiteout(dentry) also.
@@ -509,6 +519,30 @@ static inline bool d_flags_negative(unsigned flags)
return (flags & DCACHE_ENTRY_TYPE) == DCACHE_MISS_TYPE;
}

+/**
+ * d_is_negative_acquire - test if a dentry is negative, with barrier
+ * @dentry: the dentry to test
+ *
+ * This should be used if the dentry is NOT locked against
+ * change, either by ->d_lock or by parent's ->i_rwsem.
+ * In a locked context it is preferred to use d_is_negative().
+ */
+static inline bool d_is_negative_acquire(const struct dentry *dentry)
+{
+ /* acquire matches release in __d_set_inode_and_type() */
+ return d_flags_negative(smp_load_acquire(&dentry->d_flags));
+}
+
+/**
+ * d_is_positive - test if a dentry is positive
+ * @dentry: the dentry to test
+ *
+ * This should only be used if the dentry is locked against
+ * change, either by ->d_lock or by parent's ->i_rwsem.
+ * Outside this context use d_really_is_positive() if the sole
+ * purpose is to determine if it is safe to dereference the inode,
+ * or use !d_is_negative_acquire() if the context is more general.
+ */
static inline bool d_is_positive(const struct dentry *dentry)
{
return !d_is_negative(dentry);

base-commit: 84fb6f8a1ba91eee3ffe6aa519686fce851a0e42
--
2.50.0.107.gf914562f5916.dirty