Re: [PATCH v5 1/5] VFS: don't count references through ->d_parent

From: NeilBrown

Date: Fri Nov 20 2026 - 16:06:02 EST


On Fri, 20 Nov 2026, Amir Goldstein wrote:
> On Thu, Nov 19, 2026 at 4:00 AM NeilBrown <neilb@xxxxxxxxxxx> wrote:
> >
> > From: NeilBrown <neil@xxxxxxxxxx>
> >
> > The number of negative child dentries that a given dentry can have is
> > effectively unbounded, so counting the references through ->d_parent can
> > overflow ->d_lockref.count.
> >
> > We only use this count so that we can know when there are no remaining
> > references including from children. However most children appear on the
> > ->d_children list and the few remaining children (inactive
> > DCACHE_DENTRY_CURSORs) only exist in a context were a separate reference
> > is held on the parent.
> >
> > So if we count whether d_children is nonempty (i.e. dget() when it
> > becomes non-empty, dput() when it comes empty) and require children not
> > on ->d_children to have a reference on the parent some other way (which
> > they already do), then ->d_lockref.count will not overflow and we can
> > still know exactly when all references, including through ->d_parent,
> > are gone.
> >
> > __d_move() allows that dentries might not be on any d_children list as
> > is the case for IS_ROOT() dentries. Rather than track when this is the
> > case we dget() either parent if they have an empty d_children before
> > children are moved around, then dput() if they turn out to be empty
> > afterwards. This will increment dentry if it IS_ROOT(), and then
> > decrement it afterwards, and will decrement the refcount on old_parent
> > if its ->d_children becomes empty.
> >
> > To help with this, dput_dlock() is added which warns rather than
> > decrementing to zero (which should never happen).
> >
> > When a DCACHE_CURSOR dentry is added to d_children, it is only ever
> > added "before" or "behind" an existing child, so it will never be the
> > first child added, and so never needs to adjust the refcount on the
> > parent. If it remains on the list until dput() it could be the last
> > child to be removed, in which case normal handling applies in
> > dentry_unlist() (where it has been moved from dentry_kill(). When a
> > cursor is deliberately removed from the d_children list the refcount
> > must be decremented if that leaves the list empty.
> >
> > hlist_move_behind() and hlist_move_before() are added to simplify moving
> > a cursor within the d_children list. This results in some duplicate code
> > as they share a common prefix with d_detach_cursor() (hlist_del)
> > which is currently factored out. We can expect the compiler to optimise
> > this away and don't need to clutter the C code with it.
> >
> > autofs compares d_count() with the number of known children of a dentry
> > to detect if there are any concurrent references to a dentry. These
> > comparisons must be changed to reflect that we now only count "one" for
> > any children rather than "one" for each child.
> >
> > Signed-off-by: NeilBrown <neil@xxxxxxxxxx>
> > ---
> > fs/autofs/expire.c | 25 ++++++++++++++++---------
> > fs/dcache.c | 40 +++++++++++++++++++++++++++++++++-------
> > fs/internal.h | 10 ++++++++++
> > fs/libfs.c | 14 +++++++-------
> > include/linux/dcache.h | 17 ++++++++++++++++-
> > include/linux/list.h | 30 ++++++++++++++++++++++++++++++
> > 6 files changed, 112 insertions(+), 24 deletions(-)
> >
> > diff --git a/fs/autofs/expire.c b/fs/autofs/expire.c
> > index 5c2d459e1e48..cd276bce28c9 100644
> > --- a/fs/autofs/expire.c
> > +++ b/fs/autofs/expire.c
> > @@ -208,15 +208,18 @@ static int autofs_tree_busy(struct vfsmount *mnt,
> > }
> > } else {
> > struct autofs_info *ino = autofs_dentry_ino(p);
> > - unsigned int ino_count = READ_ONCE(ino->count);
> > + unsigned int use_count;
> > +
> > + /* allow for 1 if there are any children */
> > + use_count = READ_ONCE(ino->count) > 0;
> >
> > /* allow for dget above and top is already dgot */
> > if (p == top)
> > - ino_count += 2;
> > + use_count += 2;
> > else
> > - ino_count++;
> > + use_count++;
> >
> > - if (d_count(p) > ino_count) {
> > + if (d_count(p) > use_count) {
> > top_ino->last_used = jiffies;
> > dput(p);
> > return 1;
> > @@ -321,7 +324,7 @@ static struct dentry *should_expire(struct dentry *dentry,
> > unsigned int how)
> > {
> > struct autofs_info *ino = autofs_dentry_ino(dentry);
> > - unsigned int ino_count;
> > + unsigned int use_count;
> >
> > /* No point expiring a pending mount */
> > if (ino->flags & AUTOFS_INF_PENDING)
> > @@ -375,9 +378,11 @@ static struct dentry *should_expire(struct dentry *dentry,
> > if (!(how & AUTOFS_EXP_LEAVES)) {
> > /* Not a forced expire? */
> > if (!(how & AUTOFS_EXP_FORCED)) {
> > + /* dentry has any children? */
> > + use_count = READ_ONCE(ino->count) > 0;
> > /* ref-walk currently on this dentry? */
> > - ino_count = READ_ONCE(ino->count) + 1;
> > - if (d_count(dentry) > ino_count)
> > + use_count += 1;
> > + if (d_count(dentry) > use_count)
> > return NULL;
> > }
> >
> > @@ -392,9 +397,11 @@ static struct dentry *should_expire(struct dentry *dentry,
> >
> > /* Not a forced expire? */
> > if (!(how & AUTOFS_EXP_FORCED)) {
> > + /* dentry has any children? */
> > + use_count = READ_ONCE(ino->count) > 0;
> > /* ref-walk currently on this dentry? */
> > - ino_count = READ_ONCE(ino->count) + 1;
> > - if (d_count(dentry) > ino_count)
> > + use_count += 1;
> > + if (d_count(dentry) > use_count)
> > return NULL;
> > }
> >
>
> AI review claims that ino->count starts at 1 and (ino->count > 0) is
> always true,
> so use_count should be:
>
> use_count = READ_ONCE(ino->count) > 1;

Yes, ino->count does start at 1. So it is the number of children plus
1.
autofs_empty() is true when "->count < 2" which confirms that.
So I would need
use_count = (READ_ONCE(ino->count) > 1) + 1;
to match how ino->count was being used before.
Or maybe
use_count = !autofs_empty(ino) + 1;
??

I guess the "+1" could be seen as the reference added by
d_make_persistent().
Then we need to account for various dget()s, hence the +1 or +2 etc.

I'll fix that up - thanks.

NeilBrown


>
> It sounds right, but I did not look closely.
>
> Thanks,
> Amir.
>