Re: [PATCH v3 4/5] mm/mglru: avoid redundant rotations of young memcgs

From: Barry Song

Date: Mon Nov 23 2026 - 08:27:45 EST


On Mon, Nov 23, 2026 at 9:13 PM Max Kellermann <max.kellermann@xxxxxxxxx> wrote:
>
> On Mon, Nov 23, 2026 at 3:01 AM Barry Song <baohua@xxxxxxxxxx> wrote:
> > I know it was my suggestion to avoid unnecessary calls to
> > get_random_u32_below() unless we actually need them.
> >
> > You then pointed out that this would move get_random_u32_below()
> > inside the spinlock, which I had overlooked. Sorry about that.
> >
> > Have you tested whether calling get_random_u32_below() under the
> > spinlock increases lock contention?
>
> Yes, indeed it makes a big difference.
>
> My initial version which always called get_random_u32_below() at the
> beginning of lru_gen_rotate_memcg(), prior to locking
> pgdat->memcg_lru.lock (but some of these "bin" values would never be
> used with this patch):
>
> &pgdat->memcg_lru.lock: 103487653 103501074
> 0.06 301.90 195317628.84 1.89
> 113777275 116290184 0.04 339.54 30052760.20
> 0.26
>
> Completed: 681 (success: 1, EAGAIN: 680, errors: 0)
> Elapsed: 61.23 s; requests/s: 11.12
> Latency: mean 713.05 ms; max 7470.24 ms
>
> With the get_random_u32_below() call moved inside the lock, however
> only called when it's really needed:
>
> &pgdat->memcg_lru.lock: 99442644 99458122
> 0.05 342.95 243043664.06 2.44
> 102985677 104642320 0.05 231.58 34095676.89
> 0.33
>
> Completed: 548 (success: 0, EAGAIN: 548, errors: 0)
> Elapsed: 60.66 s; requests/s: 9.03
> Latency: mean 880.18 ms; max 3847.24 ms
>
> I did a few runs, and while there was a lot of noise, but the
> difference is real and reproducible. Surprises me, I wouldn't have
> thought this would be measurable.
>
> I guess I'll move the call back out of the lock section?
>

Thanks for testing, Max!

Yes, I agree. Let's move it back outside the lock, even if that
means doing some redundant work. The cost of increased lock
contention is much higher.

Thanks,
Barry