Re: [PATCH v5 4/5] Add d_for_each_positive_child family of iterators

From: NeilBrown

Date: Thu Nov 19 2026 - 18:29:28 EST


On Fri, 20 Nov 2026, Amir Goldstein wrote:
> On Thu, Nov 19, 2026 at 4:00 AM NeilBrown <neilb@xxxxxxxxxxx> wrote:

> > +
> > +/**
> > + * d_next_sibling_sched - return next sibling, but schedule() first
> > + * @child: current child
> > + *
> > + * This behaves like d_next_sibling(), but drops the parent d_lock
> > + * and calls cond_resched() to avoid any soft-lockup. A cursor
> > + * is inserted as place holder.
> > + *
> > + * Caller must hold the parents d_lock, and must hold a counted
> > + * reference on the parent.
> > + *
> > + * Returns: the next sibling, or %NULL if there is none.
> > + */
> > +struct dentry *d_next_sibling_sched(struct dentry *child)
> > +__must_hold(&child->d_parent->d_lock)
> > +{
> > + struct dentry cursor;
> > +
> > + /* always skip cursors to ensure we make some real progress */
> > + while (child && (child->d_flags & DCACHE_DENTRY_CURSOR))
> > + child = d_next_sibling(child);
> > + if (!child)
> > + return child;
>
> Why is this skip loop here and not after the resched?
>
> As this is written, if the caller passed @child which is another cursor,
> now child the next real dentry after another cursor and hlist_add_behind()
> below will insert our cursor after the real dentry.

You are right of course - that is the wrong way to address the problem.

I think moving the test into d_next_positive(), like with this
incremental patch, will work better.

Thanks!
NeilBrown


diff --git a/fs/dcache.c b/fs/dcache.c
index bacb218400a8..46aca619a9a3 100644
--- a/fs/dcache.c
+++ b/fs/dcache.c
@@ -2095,6 +2095,9 @@ static void d_init_cursor(struct dentry *dentry, struct dentry *parent)
* Caller must hold the parents d_lock, and must hold a counted
* reference on the parent.
*
+ * @child should not be a cursor, else two threads could potentially
+ * live-lock, each only stepping over each others cursors.
+ *
* Returns: the next sibling, or %NULL if there is none.
*/
struct dentry *d_next_sibling_sched(struct dentry *child)
@@ -2102,12 +2105,6 @@ __must_hold(&child->d_parent->d_lock)
{
struct dentry cursor;

- /* always skip cursors to ensure we make some real progress */
- while (child && (child->d_flags & DCACHE_DENTRY_CURSOR))
- child = d_next_sibling(child);
- if (!child)
- return child;
-
d_init_cursor(&cursor, child->d_parent);
hlist_add_behind(&cursor.d_sib, &child->d_sib);
spin_unlock(&cursor.d_parent->d_lock);
diff --git a/include/linux/dcache.h b/include/linux/dcache.h
index 7150ecd81d72..c384b0f8743e 100644
--- a/include/linux/dcache.h
+++ b/include/linux/dcache.h
@@ -697,7 +697,10 @@ struct dentry *d_next_sibling_sched(struct dentry *child);
static inline struct dentry *d_next_positive(struct dentry *child)
{
do {
- if (need_resched())
+ /* Don't schedule on cursors to ensure we make some real
+ * progress between scheduling
+ */
+ if (need_resched() && !(child->d_flags & DCACHE_DENTRY_CURSOR))
child = d_next_sibling_sched(child);
else
child = d_next_sibling(child);