Re: [PATCH v7 1/5] VFS: don't count references through ->d_parent
From: Jan Kara
Date: Wed Nov 25 2026 - 05:36:15 EST
On Wed 25-11-26 11:03:38, 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. This can be done without
> using ino->count but simply by looking at ->d_children. So a new
> static-inline "dentry_busy()" is added which performs the
> check and is told how many unusual references to expect.
> As no locks are held, this test is racy. And comment is provided
> explaining why the race is of no consequence.
>
> Signed-off-by: NeilBrown <neil@xxxxxxxxxx>
Looks good to me although I'm not very confident autofs (but I don't see an
obvious issue there). Feel free to add:
Reviewed-by: Jan Kara <jack@xxxxxxx>
Just one nit below.
> @@ -3300,12 +3308,21 @@ static void __d_move(struct dentry *dentry, struct dentry *target,
>
> /* ... and switch them in the tree */
> dentry->d_parent = target->d_parent;
> +
> + /*
> + * Ensure ref count on parents reflect d_children being non-empty,
> + * which they almost certainly are. If either end up being empty,
> + * this is handled below after the moves.
> + */
I'd add here: "Note that old_parent->d_children can indeed be empty if e.g.
@dentry was IS_ROOT." Because otherwise one has to think too hard why the
condition is here :)
> + if (hlist_empty(&old_parent->d_children))
> + dget_dlock(old_parent);
> + if (dentry->d_parent != old_parent &&
> + hlist_empty(&dentry->d_parent->d_children))
> + dget_dlock(dentry->d_parent);
Honza
> +
--
Jan Kara <jack@xxxxxxxx>
SUSE Labs, CR