Re: [PATCH] memcg: decouple memcg_hotplug_cpu_dead from stock_lock

From: Shakeel Butt
Date: Fri Apr 11 2025 - 13:56:56 EST


(my migadu/linux.dev stopped working and I have to send through gmail,
sorry for any formatting issue)

On Fri, Apr 11, 2025 at 04:06:46PM +0200, Sebastian Andrzej Siewior wrote:
>
> On 2025-04-11 10:55:31 [+0200], Vlastimil Babka wrote:
>
> > @@ -1964,10 +1964,10 @@ static int memcg_hotplug_cpu_dead(unsigned int cpu)
>
> >
>
> > stock = &per_cpu(memcg_stock, cpu);
>
> >
>
> > - /* drain_obj_stock requires stock_lock */
>
> > - local_lock_irqsave(&memcg_stock.stock_lock, flags);
>
> > - drain_obj_stock(stock);
>
> > - local_unlock_irqrestore(&memcg_stock.stock_lock, flags);
>
> > + local_irq_save(flag);
>
> I think for RT this is not great?
>

This is not a performance critical function, so I would not worry about
that.

>
> At least in theory, probably it's not
>
> actually used together with cpu hotplug? As it relies on memcg_stats_lock()
>
> I think no irq save/enable is necessary there. local_lock_irqsave wasn't
>
> actually a irq disable on RT. I don't know if there's a handy wrapper for this.
>
> No seeing the whole context but memcg_hotplug_cpu_dead() should be
>
> invoked the control CPU while "cpu" is already gone. So the local_lock
>
> should be acquired and the target CPU needs no locks since it is
>
> offline. local_irq_save() will break things.
>

I don't see how local_irq_save() will break anything. We are working on
a stock of a dead remote cpu. We actually don't even need to disable irq
or need local cpu's local_lock. It is actually the calls to
__mod_memcg_lruvec_state() and __mod_memcg_state() in
__drain_obj_stock() which need irq-disabled on non-RT kernels and for
RT-kernels they already have preempt_disable_nested().

Disabling irq even on RT seems excessive but this is not a performance
critical code, so I don't see an issue unless there is
local_lock_irqsave() alternative which does not disables irqs on RT
kernels.