Re: [PATCH v3] mm/mm_init: Don't cond_resched() in deferred_init_memmap_chunk() if called from deferred_grow_zone()

Mike Rapoport
2026-01-23T08:46:51Z

On Thu, Jan 22, 2026 at 11:29:20AM -0800, Andrew Morton wrote:
> On Thu, 22 Jan 2026 13:43:43 -0500 Waiman Long <longman@redhat.com> wrote:
>
> > Commit 3acb913c9d5b ("mm/mm_init: use deferred_init_memmap_chunk()
> > in deferred_grow_zone()") made deferred_grow_zone() call
> > deferred_init_memmap_chunk() within a pgdat_resize_lock() critical
> > section with irqs disabled.
> >
> > It did check for irqs_disabled() in
> > deferred_init_memmap_chunk() to avoid calling cond_resched(). For a
> > PREEMPT_RT kernel build, however, spin_lock_irqsave() does not disable
> > interrupt but rcu_read_lock() is called. This leads to the following
> > bug report.
> >
> > BUG: sleeping function called from invalid context at mm/mm_init.c:2091
> > in_atomic(): 0, irqs_disabled(): 0, non_block: 0, pid: 1, name: swapper/0
> > preempt_count: 0, expected: 0
> >
> > @@ -2085,10 +2085,10 @@ deferred_init_memmap_chunk(unsigned long start_pfn, unsigned long end_pfn,
> >
> > spfn = chunk_end;
> >
> > - if (irqs_disabled())
> > - touch_nmi_watchdog();
> > - else
> > + if (can_resched)
> > cond_resched();
> > + else
> > + touch_nmi_watchdog();
> > }
> > }
>
> Disables the cond_resched() in some situations. Can this reintroduce
> the watchdog warnings which that cond_resched() was intended to
> prevent?
>
> The cond_resched() was added by <dig, dig> da97f2d56bbd ("mm: call
> cond_resched() from deferred_init_memmap()").
>
> Pasha's 2020 patch replaced touch_nmi_watchdog() with cond_resched() to
> prevent RCU stall warnings. So I think the answer to my question is
> yes, going back to touch_nmi_watchdog() could reintroduce those RCU
> warnings.

Before 3acb913c9d5b ("mm/mm_init: use deferred_init_memmap_chunk() we had
touch_nmi_watchdog() in deferred_grow_zone() and cond_resched() in the
deferred_init_memmap()->deferred_init_memmap_chunk() that ran in a thread context.

I thought irqs_disabled() would be enough to differentiate these cases
because deferred_grow_zone() takes a spinlock, but I missed that with RT
spinlock also sleeps.

Using a boolean essentially restores the behaviour we had before the
refactoring.

--
Sincerely yours,
Mike.