Re: [PATCH] mm/page_alloc: Occasionally relinquish zone lock in batch freeing

From: Joshua Hahn
Date: Wed Aug 20 2025 - 09:20:46 EST


On Tue, 19 Aug 2025 14:44:21 -0700 Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> wrote:

> On Tue, 19 Aug 2025 08:18:45 -0700 Joshua Hahn <joshua.hahnjy@xxxxxxxxx> wrote:
>
> > > Pretty this isn't.
> > >
> > > Sigh, we do so much stuff here and in __free_one_page().
> > >
> > > What sort of guarantee do we have that the contending task will be able
> > > to get in and grab the spinlock in that tiny time window?
> >
> > Thank you for pointing this out -- I don't think there is any guarantee.
> > Kiryl suggested that I put a cond_resched() here, in order to guarantee that
> > the contending tasks will be able to grab the spinlock. I think that's a great
> > idea -- I'll make this change in v2.

Hello Andrew, thank you for your review!

> cond_resched() might help because it takes more CPU cycles and expands
> the window. A udelay() would of course do this more nicely.

I was wondering if we could rely on the spinlock implementation here in order
to allow some fairness in who grabs the lock. From what I have gathered, on
a lot of architectures, the default implementation for spin locks use a
queued spin lock (on x86 and arm64, among others, just by doing a quick
grep for "select ARCH_USE_QUEUED_SPINLOCKS"). This means that whoever was
waiting the longest for the spin lock will be able to grab it, guaranteeing
that this function doesn't immediately lock again.

With that said, I understand that the solution should be generic and work for
all architectures. I wonder if it would make sense to change the zone lock
into an explicit queued spin lock?

> But the contending task is already in state TASK_RUNNING so a
> cond_resched() won't have any effect on it?
>
> Also, callers hold pcp->lock, so cond_resched() cannot be called.

Ah yes, that makes sense.

> Sigh, I dunno, it's all very nasty. I have vague memories of there
> being a way of relinquishing a lock to some other task which is
> spinning on that lock. Or at least, a proposal. Or I dreamed it.
> peterz would be a good person to ask.

Cc-ing Peter, please let us know if you have any thoughts about all of this!

Thank you Andrew, I hope you have a great day!
Joshua