Re: [PATCH v5 1/5] VFS: don't count references through ->d_parent
From: Jan Kara
Date: Fri Nov 20 2026 - 04:51:26 EST
On Fri 20-11-26 10:29:12, Jan Kara wrote:
> On Thu 19-11-26 13:57:01, NeilBrown 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>
>
> Besides the objection about ino->count starting at 1 (which seems correct
> AFAICS in autofs_new_ino()) this looks good to me.
Oh, and Sashiko found a conversion of d_duplicate() to the new world order
was forgotten.
Honza
--
Jan Kara <jack@xxxxxxxx>
SUSE Labs, CR