Skip to contentSkip to conversation

overlayfs fixes for 7.3

GIT PULL Merged Amir Goldstein 4 participants 10 messages Oct 31 – Nov 6

Subsystems (2)
lore
Share & tools
lore

Amir Goldstein

author maintainer
#0 lore raw

Hi Linus,

Please pull overlayfs fixes for 7.3.

- Was in linux-next overnight

- Gone through the usual overlayfs test routines

- Includes one change in vfs code, factor out helper try_take_dentry_name_snapshot() with RVB from Christian

- Merges cleanly with master branch of the moment

Note that commit 8457ca071faba ("selftests/ovl: fix detached layer mountinfo expectations") was already merged via Christain's vfs.fixes PR from earlier today is duplicate and also on my branch.

Standard maintainers race and miscommunication. No merge conflict. Let me know if you want me to drop this patch from my branch.

Thanks,
Amir.

----------------------------------------------------------------
The following changes since commit 214897acc8c2e5d6b63189d23ccaed6fa2af45a9:

Revert "namei: don't reveal overmounted entries in refwalk" (2026-10-16 10:50:57 +0200)

are available in the Git repository at:

git://git.kernel.org/pub/scm/linux/kernel/git/overlayfs/vfs.git ovl-fixes-7.3

for you to fetch changes up to 1d27da7de3f74405b1de85ad26c5ea90f826114b:

ovl: check the casefold flag of directories only (2026-10-29 13:40:31 +0100)

----------------------------------------------------------------
overlayfs fixes for 7.3

- Fixes for bugs found by Chris Mason's AI review, several of them reachable by unprivileged users: an info leak, use-after-free and NULL deref bugs, an RCU walk stall with remote layers and data loss with nfs_export

- Fix copy up of files with an empty security.capability xattr, which any user could set to block copy up

- Fix a regression from v6.16 with the override_creds mount option

- Two casefold fixes

----------------------------------------------------------------
Amir Goldstein (6):
ovl: fortify ovl_copy_xattr() against underlying layer modifications
ovl: add assertions in dir cache code
vfs: add try_take_dentry_name_snapshot()
ovl: skip copy up of empty security.capability selftests/ovl: fix detached layer mountinfo expectations
ovl: fix mount setup failing when override_creds drops CAP_SYS_ADMIN

Chris Mason (13):
ovl: do not copy up an xattr value that was never read
ovl: set OVL_WHITEOUTS before publishing the upper dir
ovl: check that the parents of volatile/dirty are dirs
ovl: take a snapshot of the name for copy up
ovl: do not take a name snapshot in RCU walk
ovl: use the pinned upper dir in ovl_fix_origin()
ovl: hold a reference on the parent in readdir
ovl: do not copy up a dentry that was removed
ovl: fix a torn path in ovl_get_redirect()
ovl: read the nlink xattr of every indexed non-dir
ovl: always encode the origin fh when index is enabled
ovl: set the revalidate flags after d_instantiate()
ovl: check the casefold flag of directories only

Hungyu Lin (1):
ovl: reserve space for the casefolded name terminator

 fs/dcache.c                                        |  29 +++++-
 fs/overlayfs/copy_up.c                             | 110 +++++++++++++--------
 fs/overlayfs/dir.c                                 |  22 ++++-
 fs/overlayfs/inode.c                               |  16 ++-
 fs/overlayfs/namei.c                               |  11 ++-
 fs/overlayfs/overlayfs.h                           |   8 +-
 fs/overlayfs/readdir.c                             |  65 +++++++++---
 fs/overlayfs/super.c                               |  39 ++++++--
 include/linux/dcache.h                             |   1 +
 .../filesystems/overlayfs/set_layers_via_fds.c     |  18 ++--
 10 files changed, 233 insertions(+), 86 deletions(-)
#1 lore raw
Quoted 3 lines · Amir Goldstein

Amir Goldstein (6):
vfs: add try_take_dentry_name_snapshot()

I've pulled this, but I detest this particular commit. It just "smells" wrong to me.

I don't doubt that it fixes a real bug in overlayfs, but I think the fix is entriely wrong. It is wrong in two ways:

- it incorrectly fails when the issue is a race with a rename (this
is harmless for overlayfs and will just result in -ECHILD)

- it adds a bogus new "try" helper that I don't believe should be
needed or exist

I think the proper fix would be to just say that the !atomic_inc_not_zero() can indeed fail, and we should never retry based on that.

But if we should *always* retry based on dentry->d_seq.

And once you retry correctly based on dentry->d_seq, now the "we didn't get a name reference" is reliable: if it happened despite d_seq being stable, it's because we really don't have a name any more, and so we should return failure.

Put another way: I think we should have just one "take_dentry_name_snapshot()" function, and it should return a boolean, and look somethign like this:

bool take_dentry_name_snapshot(struct name_snapshot *name, struct
dentry *dentry)

  {
        unsigned seq;
        const unsigned char *s;

        rcu_read_lock();
  retry:
        seq = read_seqcount_begin(&dentry->d_seq);
        s = READ_ONCE(dentry->d_name.name);
        name->name.hash_len = dentry->d_name.hash_len;
        name->name.name = name->inline_name.string;
        if (likely(s == dentry->d_shortname.string)) {
                name->inline_name = dentry->d_shortname;
                if (read_seqcount_retry(&dentry->d_seq, seq))
                        goto retry;
        } else {
                struct external_name *p;
                p = container_of(s, struct external_name, name[0]);

                // Try to get a valid reference
                if (unlikely(!atomic_inc_not_zero(&p->count))) {
                        if (read_seqcount_retry(&dentry->d_seq, seq))
                                goto retry;
                        rcu_read_unlock();
                        return false;
                }

                // Ok, we have a name reference. Make sure it is still valid
                if (read_seqcount_retry(&dentry->d_seq, seq)) {
                        release_dentry_name_snapshot(name);
                        goto retry;
                }

                name->name.name = s;
        }
        rcu_read_unlock();
        return true;

}
EXPORT_SYMBOL(take_dentry_name_snapshot);

(Note that in this version, if it fails it will just reutn the shortname in name->name.name, but it should never fail for a dentry that you hold a reference to afaik).

Attached it an ENTIRELY UNTESTED patch that does all the other changes too to make overlayfs use this version.

Again: ENTIRELY UNTESTED. Maybe I'm missing something and I'm just wrong on this. But I really don't like that nasty try_take_dentry_name_snapshot() thing. It really looks wrong to me, both conceptually and in being actively buggy and racing with renames.

Hmm? Comments?

Linus

 fs/dcache.c            | 38 +++++++++++++-------------------------
 fs/overlayfs/super.c   |  9 +++------
 include/linux/dcache.h |  3 +--
 3 files changed, 17 insertions(+), 33 deletions(-)
fs/dcache.c +13 -25
index 409f841e058e..fe037a876d76 100644
@@ -359,8 +359,7 @@ static inline int dname_external(const struct dentry *dentry)
 	return dentry->d_name.name != dentry->d_shortname.string;
 }
 
-static bool __take_dentry_name_snapshot(struct name_snapshot *name,
-					struct dentry *dentry, bool retry)
+bool take_dentry_name_snapshot(struct name_snapshot *name, struct dentry *dentry)
 {
 	unsigned seq;
 	const unsigned char *s;
@@ -373,44 +372,33 @@ static bool __take_dentry_name_snapshot(struct name_snapshot *name,
 	name->name.name = name->inline_name.string;
 	if (likely(s == dentry->d_shortname.string)) {
 		name->inline_name = dentry->d_shortname;
+		if (read_seqcount_retry(&dentry->d_seq, seq))
+			goto retry;
 	} else {
 		struct external_name *p;
 		p = container_of(s, struct external_name, name[0]);
-		// get a valid reference
+
+		// Try to get a valid reference
 		if (unlikely(!atomic_inc_not_zero(&p->count))) {
-			if (retry)
+			if (read_seqcount_retry(&dentry->d_seq, seq))
 				goto retry;
 			rcu_read_unlock();
 			return false;
 		}
+
+		// Ok, we have a name reference. Make sure it is still valid
+		if (read_seqcount_retry(&dentry->d_seq, seq)) {
+			release_dentry_name_snapshot(name);
+			goto retry;
+		}
+
 		name->name.name = s;
 	}
-	if (read_seqcount_retry(&dentry->d_seq, seq)) {
-		release_dentry_name_snapshot(name);
-		goto retry;
-	}
 	rcu_read_unlock();
 	return true;
 }
-
-void take_dentry_name_snapshot(struct name_snapshot *name, struct dentry *dentry)
-{
-	__take_dentry_name_snapshot(name, dentry, true);
-}
 EXPORT_SYMBOL(take_dentry_name_snapshot);
 
-/*
- * For a caller that holds no reference on @dentry, e.g. in RCU walk.
- * The external name of a dead dentry has a zero refcount forever, so
- * take_dentry_name_snapshot() would retry without end.  Fail instead.
- */
-bool try_take_dentry_name_snapshot(struct name_snapshot *name,
-				   struct dentry *dentry)
-{
-	return __take_dentry_name_snapshot(name, dentry, false);
-}
-EXPORT_SYMBOL(try_take_dentry_name_snapshot);
-
 void release_dentry_name_snapshot(struct name_snapshot *name)
 {
 	if (unlikely(name->name.name != name->inline_name.string)) {
fs/overlayfs/super.c +3 -6
index 08695a99f4f9..2e160b7dcfe7 100644
@@ -100,17 +100,14 @@ static int ovl_revalidate_real(struct dentry *d, unsigned int flags, bool weak)
 			dir = d_inode_rcu(parent);
 			if (!dir)
 				return -ECHILD;
-			/*
-			 * Nothing pins d in RCU walk: eviction of the overlay
-			 * inode may drop the last reference at any time.
-			 */
-			if (!try_take_dentry_name_snapshot(&n, d))
-				return -ECHILD;
 		} else {
 			parent = dget_parent(d);
 			dir = d_inode(parent);
 			take_dentry_name_snapshot(&n, d);
 		}
+		// In RCU walk the dentry name may go away at any time
+		if (!take_dentry_name_snapshot(&n, d))
+			return -ECHILD;
 		ret = d->d_op->d_revalidate(dir, &n.name, d, flags);
 		release_dentry_name_snapshot(&n);
 		if (!(flags & LOOKUP_RCU))
include/linux/dcache.h +1 -2
index 84b0cfe4ed0e..aaa4ba7a1299 100644
@@ -664,8 +664,7 @@ struct name_snapshot {
 	struct qstr name;
 	union shortname_store inline_name;
 };
-void take_dentry_name_snapshot(struct name_snapshot *, struct dentry *);
-bool try_take_dentry_name_snapshot(struct name_snapshot *, struct dentry *);
+bool take_dentry_name_snapshot(struct name_snapshot *, struct dentry *);
 void release_dentry_name_snapshot(struct name_snapshot *);
 
 static inline struct dentry *d_first_child(const struct dentry *dentry)
author maintainer
#2 lore raw

Re: Return failure from take_dentry_name_snapshot() (Was: [GIT PULL] overlayfs fixes for 7.3)

Quoted 81 lines · Linus Torvalds
Quoted 3 lines · Amir Goldstein

Amir Goldstein (6):
vfs: add try_take_dentry_name_snapshot()

I've pulled this, but I detest this particular commit. It just "smells" wrong to me.

I don't doubt that it fixes a real bug in overlayfs, but I think the fix is entriely wrong. It is wrong in two ways:

- it incorrectly fails when the issue is a race with a rename (this
is harmless for overlayfs and will just result in -ECHILD)

- it adds a bogus new "try" helper that I don't believe should be
needed or exist

I think the proper fix would be to just say that the !atomic_inc_not_zero() can indeed fail, and we should never retry based on that.

But if we should *always* retry based on dentry->d_seq.

And once you retry correctly based on dentry->d_seq, now the "we didn't get a name reference" is reliable: if it happened despite d_seq being stable, it's because we really don't have a name any more, and so we should return failure.

Put another way: I think we should have just one "take_dentry_name_snapshot()" function, and it should return a boolean, and look somethign like this:

bool take_dentry_name_snapshot(struct name_snapshot *name, struct
dentry *dentry)

  {
        unsigned seq;
        const unsigned char *s;

        rcu_read_lock();
  retry:
        seq = read_seqcount_begin(&dentry->d_seq);
        s = READ_ONCE(dentry->d_name.name);
        name->name.hash_len = dentry->d_name.hash_len;
        name->name.name = name->inline_name.string;
        if (likely(s == dentry->d_shortname.string)) {
                name->inline_name = dentry->d_shortname;
                if (read_seqcount_retry(&dentry->d_seq, seq))
                        goto retry;
        } else {
                struct external_name *p;
                p = container_of(s, struct external_name, name[0]);

                // Try to get a valid reference
                if (unlikely(!atomic_inc_not_zero(&p->count))) {
                        if (read_seqcount_retry(&dentry->d_seq, seq))
                                goto retry;
                        rcu_read_unlock();
                        return false;
                }

                // Ok, we have a name reference. Make sure it is still valid
                if (read_seqcount_retry(&dentry->d_seq, seq)) {
                        release_dentry_name_snapshot(name);
                        goto retry;
                }

                name->name.name = s;
        }
        rcu_read_unlock();
        return true;

}
EXPORT_SYMBOL(take_dentry_name_snapshot);

(Note that in this version, if it fails it will just reutn the shortname in name->name.name, but it should never fail for a dentry that you hold a reference to afaik).

Attached it an ENTIRELY UNTESTED patch that does all the other changes too to make overlayfs use this version.

Quoted 6 lines

Again: ENTIRELY UNTESTED. Maybe I'm missing something and I'm just wrong on this. But I really don't like that nasty try_take_dentry_name_snapshot() thing. It really looks wrong to me, both conceptually and in being actively buggy and racing with renames.

Hmm? Comments?

I like this. Small bug in ovl code.
Forgot to remove old take_dentry_name_snapshot().

Attached proper patch.
It's sanity tested but I did not try Chris' reproducer with added delays.

Feel free to apply and/or wait for ACK from Al/Christian, or I can include it in my next ovl-fixes PR.

Thanks,
Amir.

From 6b027085c470a6641783a30f827cd047a7f7a072 Mon Sep 17 00:00:00 2001
From: Amir Goldstein <[email protected]>
Date: Sun, 1 Nov 2026 09:47:19 +0100
Subject: [PATCH] vfs: take_dentry_name_snapshot() may fail

This patch was posted by Linus as a better alternative to my try_take_dentry_name_snapshot() for fixing the overlayfs issue.

Quoting Linus:
I've pulled this, but I detest this particular commit. It just "smells" wrong to me.

I don't doubt that it fixes a real bug in overlayfs, but I think the fix is entriely wrong. It is wrong in two ways:

- it incorrectly fails when the issue is a race with a rename (this
is harmless for overlayfs and will just result in -ECHILD)

- it adds a bogus new "try" helper that I don't believe should be
needed or exist

I think the proper fix would be to just say that the !atomic_inc_not_zero() can indeed fail, and we should never retry based on that.

But if we should *always* retry based on dentry->d_seq.

And once you retry correctly based on dentry->d_seq, now the "we didn't get a name reference" is reliable: if it happened despite d_seq being stable, it's because we really don't have a name any more, and so we should return failure.

Put another way: I think we should have just one "take_dentry_name_snapshot()" function, and it should return a boolean.

---
 fs/dcache.c            | 38 +++++++++++++-------------------------
 fs/overlayfs/super.c   | 10 +++-------
 include/linux/dcache.h |  3 +--
 3 files changed, 17 insertions(+), 34 deletions(-)
fs/dcache.c +13 -25
index 409f841e058e4..fe037a876d767 100644
@@ -359,8 +359,7 @@ static inline int dname_external(const struct dentry *dentry)
 	return dentry->d_name.name != dentry->d_shortname.string;
 }
 
-static bool __take_dentry_name_snapshot(struct name_snapshot *name,
-					struct dentry *dentry, bool retry)
+bool take_dentry_name_snapshot(struct name_snapshot *name, struct dentry *dentry)
 {
 	unsigned seq;
 	const unsigned char *s;
@@ -373,44 +372,33 @@ static bool __take_dentry_name_snapshot(struct name_snapshot *name,
 	name->name.name = name->inline_name.string;
 	if (likely(s == dentry->d_shortname.string)) {
 		name->inline_name = dentry->d_shortname;
+		if (read_seqcount_retry(&dentry->d_seq, seq))
+			goto retry;
 	} else {
 		struct external_name *p;
 		p = container_of(s, struct external_name, name[0]);
-		// get a valid reference
+
+		// Try to get a valid reference
 		if (unlikely(!atomic_inc_not_zero(&p->count))) {
-			if (retry)
+			if (read_seqcount_retry(&dentry->d_seq, seq))
 				goto retry;
 			rcu_read_unlock();
 			return false;
 		}
+
+		// Ok, we have a name reference. Make sure it is still valid
+		if (read_seqcount_retry(&dentry->d_seq, seq)) {
+			release_dentry_name_snapshot(name);
+			goto retry;
+		}
+
 		name->name.name = s;
 	}
-	if (read_seqcount_retry(&dentry->d_seq, seq)) {
-		release_dentry_name_snapshot(name);
-		goto retry;
-	}
 	rcu_read_unlock();
 	return true;
 }
-
-void take_dentry_name_snapshot(struct name_snapshot *name, struct dentry *dentry)
-{
-	__take_dentry_name_snapshot(name, dentry, true);
-}
 EXPORT_SYMBOL(take_dentry_name_snapshot);
 
-/*
- * For a caller that holds no reference on @dentry, e.g. in RCU walk.
- * The external name of a dead dentry has a zero refcount forever, so
- * take_dentry_name_snapshot() would retry without end.  Fail instead.
- */
-bool try_take_dentry_name_snapshot(struct name_snapshot *name,
-				   struct dentry *dentry)
-{
-	return __take_dentry_name_snapshot(name, dentry, false);
-}
-EXPORT_SYMBOL(try_take_dentry_name_snapshot);
-
 void release_dentry_name_snapshot(struct name_snapshot *name)
 {
 	if (unlikely(name->name.name != name->inline_name.string)) {
fs/overlayfs/super.c +3 -7
index 08695a99f4f93..d9174eaccce2d 100644
@@ -100,17 +100,13 @@ static int ovl_revalidate_real(struct dentry *d, unsigned int flags, bool weak)
 			dir = d_inode_rcu(parent);
 			if (!dir)
 				return -ECHILD;
-			/*
-			 * Nothing pins d in RCU walk: eviction of the overlay
-			 * inode may drop the last reference at any time.
-			 */
-			if (!try_take_dentry_name_snapshot(&n, d))
-				return -ECHILD;
 		} else {
 			parent = dget_parent(d);
 			dir = d_inode(parent);
-			take_dentry_name_snapshot(&n, d);
 		}
+		/* In RCU walk the dentry name may go away at any time */
+		if (!take_dentry_name_snapshot(&n, d))
+			return -ECHILD;
 		ret = d->d_op->d_revalidate(dir, &n.name, d, flags);
 		release_dentry_name_snapshot(&n);
 		if (!(flags & LOOKUP_RCU))
include/linux/dcache.h +1 -2
index 84b0cfe4ed0e7..aaa4ba7a12991 100644
@@ -664,8 +664,7 @@ struct name_snapshot {
 	struct qstr name;
 	union shortname_store inline_name;
 };
-void take_dentry_name_snapshot(struct name_snapshot *, struct dentry *);
-bool try_take_dentry_name_snapshot(struct name_snapshot *, struct dentry *);
+bool take_dentry_name_snapshot(struct name_snapshot *, struct dentry *);
 void release_dentry_name_snapshot(struct name_snapshot *);
 
 static inline struct dentry *d_first_child(const struct dentry *dentry)
Signature · 1 line
2.55.0
#3 lore raw

Re: Return failure from take_dentry_name_snapshot() (Was: [GIT PULL] overlayfs fixes for 7.3)

Quoted 2 lines · Amir Goldstein

Feel free to apply and/or wait for ACK from Al/Christian,

I'd like to see an ack from Al / Christian, but

Quoted 1 line

or I can include it in my next ovl-fixes PR.

I think I'll also rewrite that function to use guards. I didn't do it originally because I just wanted to do minimal surgery, but when I look at that patch I go "it could be done more prettily with our modern infrastructure".

Let me look at it in my copious spare time.

Linus

#4 lore raw

Re: Return failure from take_dentry_name_snapshot() (Was: [GIT PULL] overlayfs fixes for 7.3)

Quoted 3 lines · Linus Torvalds

I'd like to see an ack from Al / Christian, but I think I'll also rewrite that function to use guards.

I didn't get any acks, but I obviously did do the guard rewrite (and then some: I got a bit OCD about it).

I've committed the end result.

Linus

author maintainer
#5 lore raw

Re: Return failure from take_dentry_name_snapshot() (Was: [GIT PULL] overlayfs fixes for 7.3)

Quoted 11 lines · Linus Torvalds
Quoted 3 lines · Linus Torvalds

I'd like to see an ack from Al / Christian, but I think I'll also rewrite that function to use guards.

I didn't get any acks, but I obviously did do the guard rewrite (and then some: I got a bit OCD about it).

I've committed the end result.

Thanks for cleaning it up.
Amir.

maintainer Reviewed-by
#6 lore raw

Re: Return failure from take_dentry_name_snapshot() (Was: [GIT PULL] overlayfs fixes for 7.3)

In reply to Linus Torvalds
Quoted 8 lines · Linus Torvalds
Quoted 3 lines · Linus Torvalds

I'd like to see an ack from Al / Christian, but I think I'll also rewrite that function to use guards.

I didn't get any acks, but I obviously did do the guard rewrite (and then some: I got a bit OCD about it).

VFS is just another acronym for OCD.

Quoted 1 line

I've committed the end result.

Thanks. Sorry, I didn't get to this earlier because of reasons... Well, you know as you were on receiving end of it. In any case,

retroactively.

#7 lore raw

Re: Return failure from take_dentry_name_snapshot() (Was: [GIT PULL] overlayfs fixes for 7.3)

In reply to Amir Goldstein
Quoted 5 lines · Amir Goldstein
Quoted 2 lines · Linus Torvalds
Quoted 7 lines
Quoted 7 lines
                // Ok, we have a name reference. Make sure it is still valid
                if (read_seqcount_retry(&dentry->d_seq, seq)) {
                        release_dentry_name_snapshot(name);
                        goto retry;
                }

                name->name.name = s;

This was wrong: it sets 'name->name.name' too late:
release_dentry_name_snapshot() already needs it set, because it then compares that name pointer to the inline pointer to see if it was an external name or not.

The fix is to just have a separate "release_external_name()" helper, since that's what we actually want here.

Quoted 1 line

Attached proper patch.

Your proper patch ended up having my bug in it, so it will leak the external name in the (admittedly very unlikely) case of take_dentry_name_snapshot having gotten an external name reference but then raced with a rename.

I think this attached patch is correct.

Linus

 fs/dcache.c            | 77 ++++++++++++++++++++++----------------------------
 fs/overlayfs/super.c   | 10 ++-----
 include/linux/dcache.h |  3 +-
 3 files changed, 37 insertions(+), 53 deletions(-)
fs/dcache.c +33 -44
index 409f841e058e..79ce1d0e52c2 100644
@@ -335,6 +335,12 @@ struct external_name {
 	unsigned char name[] __aligned(sizeof(unsigned long));
 };
 
+static inline void release_external_name(struct external_name *p)
+{
+	if (unlikely(atomic_dec_and_test(&p->count)))
+		kfree_rcu(p, head);
+}
+
 static inline struct external_name *external_name(struct dentry *dentry)
 {
 	return container_of(dentry->d_name.name, struct external_name, name[0]);
@@ -359,65 +365,48 @@ static inline int dname_external(const struct dentry *dentry)
 	return dentry->d_name.name != dentry->d_shortname.string;
 }
 
-static bool __take_dentry_name_snapshot(struct name_snapshot *name,
-					struct dentry *dentry, bool retry)
+bool take_dentry_name_snapshot(struct name_snapshot *name, struct dentry *dentry)
 {
-	unsigned seq;
-	const unsigned char *s;
-
-	rcu_read_lock();
-retry:
-	seq = read_seqcount_begin(&dentry->d_seq);
-	s = READ_ONCE(dentry->d_name.name);
-	name->name.hash_len = dentry->d_name.hash_len;
-	name->name.name = name->inline_name.string;
-	if (likely(s == dentry->d_shortname.string)) {
-		name->inline_name = dentry->d_shortname;
-	} else {
+	guard(rcu)();
+	for (;;) {
+		unsigned seq = read_seqcount_begin(&dentry->d_seq);
+		const unsigned char *s = READ_ONCE(dentry->d_name.name);
 		struct external_name *p;
+
+		name->name.hash_len = dentry->d_name.hash_len;
+		name->name.name = name->inline_name.string;
+		if (likely(s == dentry->d_shortname.string)) {
+			name->inline_name = dentry->d_shortname;
+			if (read_seqcount_retry(&dentry->d_seq, seq))
+				continue;
+			return true;
+		}
+
+		// Try to get a valid external reference
 		p = container_of(s, struct external_name, name[0]);
-		// get a valid reference
 		if (unlikely(!atomic_inc_not_zero(&p->count))) {
-			if (retry)
-				goto retry;
-			rcu_read_unlock();
+			if (read_seqcount_retry(&dentry->d_seq, seq))
+				continue;
 			return false;
 		}
-		name->name.name = s;
-	}
-	if (read_seqcount_retry(&dentry->d_seq, seq)) {
-		release_dentry_name_snapshot(name);
-		goto retry;
-	}
-	rcu_read_unlock();
-	return true;
-}
 
-void take_dentry_name_snapshot(struct name_snapshot *name, struct dentry *dentry)
-{
-	__take_dentry_name_snapshot(name, dentry, true);
+		// Ok, we have a name reference. Make sure it is still valid
+		name->name.name = s;
+		if (!read_seqcount_retry(&dentry->d_seq, seq))
+			return true;
+
+		release_external_name(p);
+		continue;
+	}
 }
 EXPORT_SYMBOL(take_dentry_name_snapshot);
 
-/*
- * For a caller that holds no reference on @dentry, e.g. in RCU walk.
- * The external name of a dead dentry has a zero refcount forever, so
- * take_dentry_name_snapshot() would retry without end.  Fail instead.
- */
-bool try_take_dentry_name_snapshot(struct name_snapshot *name,
-				   struct dentry *dentry)
-{
-	return __take_dentry_name_snapshot(name, dentry, false);
-}
-EXPORT_SYMBOL(try_take_dentry_name_snapshot);
-
 void release_dentry_name_snapshot(struct name_snapshot *name)
 {
 	if (unlikely(name->name.name != name->inline_name.string)) {
 		struct external_name *p;
 		p = container_of(name->name.name, struct external_name, name[0]);
-		if (unlikely(atomic_dec_and_test(&p->count)))
-			kfree_rcu(p, head);
+		release_external_name(p);
 	}
 }
 EXPORT_SYMBOL(release_dentry_name_snapshot);
fs/overlayfs/super.c +3 -7
index 08695a99f4f9..d9174eaccce2 100644
@@ -100,17 +100,13 @@ static int ovl_revalidate_real(struct dentry *d, unsigned int flags, bool weak)
 			dir = d_inode_rcu(parent);
 			if (!dir)
 				return -ECHILD;
-			/*
-			 * Nothing pins d in RCU walk: eviction of the overlay
-			 * inode may drop the last reference at any time.
-			 */
-			if (!try_take_dentry_name_snapshot(&n, d))
-				return -ECHILD;
 		} else {
 			parent = dget_parent(d);
 			dir = d_inode(parent);
-			take_dentry_name_snapshot(&n, d);
 		}
+		/* In RCU walk the dentry name may go away at any time */
+		if (!take_dentry_name_snapshot(&n, d))
+			return -ECHILD;
 		ret = d->d_op->d_revalidate(dir, &n.name, d, flags);
 		release_dentry_name_snapshot(&n);
 		if (!(flags & LOOKUP_RCU))
include/linux/dcache.h +1 -2
index 84b0cfe4ed0e..aaa4ba7a1299 100644
@@ -664,8 +664,7 @@ struct name_snapshot {
 	struct qstr name;
 	union shortname_store inline_name;
 };
-void take_dentry_name_snapshot(struct name_snapshot *, struct dentry *);
-bool try_take_dentry_name_snapshot(struct name_snapshot *, struct dentry *);
+bool take_dentry_name_snapshot(struct name_snapshot *, struct dentry *);
 void release_dentry_name_snapshot(struct name_snapshot *);
 
 static inline struct dentry *d_first_child(const struct dentry *dentry)
#8 lore raw

Re: Return failure from take_dentry_name_snapshot() (Was: [GIT PULL] overlayfs fixes for 7.3)

Quoted 2 lines · Linus Torvalds

I think this attached patch is correct.

Bah. I think it *was* correct, but I hated the deep indentations and wanted to add a few comments about the failure case.

So I rewrote it with a helper function, with he outer function doing both the RCU read-side protection and the sequence number test:

bool take_dentry_name_snapshot(struct name_snapshot *name, struct
dentry *dentry)

  {
        guard(rcu)();
        for (;;) {
                unsigned seq = read_seqcount_begin(&dentry->d_seq);
                bool retval = try_name_snapshot(name, dentry);
                if (!read_seqcount_retry(&dentry->d_seq, seq))
                        return retval;
                release_dentry_name_snapshot(name);
        }

}

which makes it fairly straightforward (with the main subtlety just being that try_name_snapshot() needs to set things up so that release_dentry_name_snapshot() is ok to call even on failure, but that was effectively true before too).

I have now looked at this code so much that I have semantic satiation and it has lost all meaning.

Time to stop.

Linus

 fs/dcache.c            | 66 +++++++++++++++++++++-----------------------------
 fs/overlayfs/super.c   | 10 +++-----
 include/linux/dcache.h |  3 +--
 3 files changed, 32 insertions(+), 47 deletions(-)
fs/dcache.c +28 -38
index 409f841e058e..b2fa5f0a68c4 100644
@@ -359,58 +359,48 @@ static inline int dname_external(const struct dentry *dentry)
 	return dentry->d_name.name != dentry->d_shortname.string;
 }
 
-static bool __take_dentry_name_snapshot(struct name_snapshot *name,
-					struct dentry *dentry, bool retry)
+static bool try_name_snapshot(struct name_snapshot *name, struct dentry *dentry)
 {
-	unsigned seq;
-	const unsigned char *s;
+	const unsigned char *s = READ_ONCE(dentry->d_name.name);
+	struct external_name *p;
 
-	rcu_read_lock();
-retry:
-	seq = read_seqcount_begin(&dentry->d_seq);
-	s = READ_ONCE(dentry->d_name.name);
 	name->name.hash_len = dentry->d_name.hash_len;
 	name->name.name = name->inline_name.string;
 	if (likely(s == dentry->d_shortname.string)) {
 		name->inline_name = dentry->d_shortname;
-	} else {
-		struct external_name *p;
-		p = container_of(s, struct external_name, name[0]);
-		// get a valid reference
-		if (unlikely(!atomic_inc_not_zero(&p->count))) {
-			if (retry)
-				goto retry;
-			rcu_read_unlock();
-			return false;
-		}
-		name->name.name = s;
+		return true;
 	}
-	if (read_seqcount_retry(&dentry->d_seq, seq)) {
-		release_dentry_name_snapshot(name);
-		goto retry;
-	}
-	rcu_read_unlock();
+
+	// Try to get a valid external reference
+	//
+	// In case we fail to get a ref, 'name.name' will
+	// point to the inline_name, which leaves the name
+	// invalid. But release_dentry_name_snapshot() can
+	// be called regardless.
+	p = container_of(s, struct external_name, name[0]);
+	if (unlikely(!atomic_inc_not_zero(&p->count)))
+		return false;
+
+	// We got a ref and the name is now needs to be
+	// free'd regardless of whether the sequence count
+	// matches or not
+	name->name.name = s;
 	return true;
 }
 
-void take_dentry_name_snapshot(struct name_snapshot *name, struct dentry *dentry)
+bool take_dentry_name_snapshot(struct name_snapshot *name, struct dentry *dentry)
 {
-	__take_dentry_name_snapshot(name, dentry, true);
+	guard(rcu)();
+	for (;;) {
+		unsigned seq = read_seqcount_begin(&dentry->d_seq);
+		bool retval = try_name_snapshot(name, dentry);
+		if (!read_seqcount_retry(&dentry->d_seq, seq))
+			return retval;
+		release_dentry_name_snapshot(name);
+	}
 }
 EXPORT_SYMBOL(take_dentry_name_snapshot);
 
-/*
- * For a caller that holds no reference on @dentry, e.g. in RCU walk.
- * The external name of a dead dentry has a zero refcount forever, so
- * take_dentry_name_snapshot() would retry without end.  Fail instead.
- */
-bool try_take_dentry_name_snapshot(struct name_snapshot *name,
-				   struct dentry *dentry)
-{
-	return __take_dentry_name_snapshot(name, dentry, false);
-}
-EXPORT_SYMBOL(try_take_dentry_name_snapshot);
-
 void release_dentry_name_snapshot(struct name_snapshot *name)
 {
 	if (unlikely(name->name.name != name->inline_name.string)) {
fs/overlayfs/super.c +3 -7
index 08695a99f4f9..d9174eaccce2 100644
@@ -100,17 +100,13 @@ static int ovl_revalidate_real(struct dentry *d, unsigned int flags, bool weak)
 			dir = d_inode_rcu(parent);
 			if (!dir)
 				return -ECHILD;
-			/*
-			 * Nothing pins d in RCU walk: eviction of the overlay
-			 * inode may drop the last reference at any time.
-			 */
-			if (!try_take_dentry_name_snapshot(&n, d))
-				return -ECHILD;
 		} else {
 			parent = dget_parent(d);
 			dir = d_inode(parent);
-			take_dentry_name_snapshot(&n, d);
 		}
+		/* In RCU walk the dentry name may go away at any time */
+		if (!take_dentry_name_snapshot(&n, d))
+			return -ECHILD;
 		ret = d->d_op->d_revalidate(dir, &n.name, d, flags);
 		release_dentry_name_snapshot(&n);
 		if (!(flags & LOOKUP_RCU))
include/linux/dcache.h +1 -2
index 84b0cfe4ed0e..aaa4ba7a1299 100644
@@ -664,8 +664,7 @@ struct name_snapshot {
 	struct qstr name;
 	union shortname_store inline_name;
 };
-void take_dentry_name_snapshot(struct name_snapshot *, struct dentry *);
-bool try_take_dentry_name_snapshot(struct name_snapshot *, struct dentry *);
+bool take_dentry_name_snapshot(struct name_snapshot *, struct dentry *);
 void release_dentry_name_snapshot(struct name_snapshot *);
 
 static inline struct dentry *d_first_child(const struct dentry *dentry)
merge bot Merged
#9 lore raw
In reply to Amir Goldstein
The pull request you sent on Sat, 31 Oct 2026 10:57:16 +0100:

The pull request you sent on Sat, 31 Oct 2026 10:57:16 +0100:

Quoted 1 line

git://git.kernel.org/pub/scm/linux/kernel/git/overlayfs/vfs.git ovl-fixes-7.3

has been merged into torvalds/linux.git:
https://git.kernel.org/torvalds/c/ca62fc8c6b1841039aee6de1ef03c9863751ec04

Thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/prtracker.html