mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: NeilBrown <neilb@ownmail.net>
To: linux-kernel@vger.kernel.org
Subject: [PATCH 1/9] VFS: introduce d_is_negative_acquire()
Date: Thu,  3 Dec 2026 11:07:56 +1100	[thread overview]
Message-ID: <20261203000848.2986775-2-neilb@ownmail.net> (raw)
In-Reply-To: <20261203000848.2986775-1-neilb@ownmail.net>

From: NeilBrown <neil@brown.name>

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@brown.name>
---
 fs/namei.c             |  8 ++++----
 fs/overlayfs/namei.c   |  2 +-
 include/linux/dcache.h | 34 ++++++++++++++++++++++++++++++++++
 3 files changed, 39 insertions(+), 5 deletions(-)

diff --git a/fs/namei.c b/fs/namei.c
index c79eecffe4ac..5bf77dca0c53 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


  reply	other threads:[~2026-12-03  0:09 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03  0:07 [PATCH 0/9 v3] vfs: improvements for atomic_open and vfs_lookup_open NeilBrown
2026-12-03  0:07 ` NeilBrown [this message]
2026-12-03  0:07 ` [PATCH 2/9] vfs: revise and expand documentation for atomic_open NeilBrown
2026-12-03  0:07 ` [PATCH 3/9] vfs: only set op.mode when O_CREAT in vfs_lookup_open() NeilBrown
2026-12-03  0:07 ` [PATCH 4/9] vfs: don't call ->atomic_open with hashed negative in non-O_CREAT case NeilBrown
2026-12-03  0:08 ` [PATCH 5/9] vfs: add some allowed open flags to vfs_lookup_open() NeilBrown
2026-12-03  0:08 ` [PATCH 6/9] vfs: O_NONBLOCK|O_CREAT open shouldn't wait for directory delegation NeilBrown
2026-12-03  0:08 ` [PATCH 7/9] vfs: don't return -ENODEV from vfs_lookup_open() NeilBrown
2026-12-03  0:08 ` [PATCH 8/9] vfs: change vfs_lookup_open() to use do_open(), not vfs_open() NeilBrown
2026-12-03  0:08 ` [PATCH 9/9] vfs: add MAY_SERVE_FILE and FMODE_SERVE_FILE NeilBrown

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203000848.2986775-2-neilb@ownmail.net \
    --to=neilb@ownmail.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=neil@brown.name \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®