Re: [PATCH v6 03/18] mm/memcontrol: avoid potential counter leak when reparenting local stats

From: SJ Park

Date: Tue Nov 24 2026 - 20:41:53 EST


On Tue, 24 Nov 2026 02:52:55 +0800 Kairui Song <kasong@xxxxxxxxxxx> wrote:

> lruvec_page_state_local() clamps negative values to zero on SMP. That
> is right for readers reporting page counts, but can be wrong for
> reparenting once MGLRU updates the counters without the lruvec lock.
>
> Reparenting runs after the forced stats flush, so a negative is a real
> balance that has to move to the parent, not be discarded. We will
> introduce lockless MGLRU promotion which may cause following problem:
>
> 1. A refs CAS on a folio on the child's lruvec moves it from
> inactive to active, and its active/inactive counter update follows
> as a separate step.
>
> 2. A concurrent isolation of the folio reads the folio as active,
> debits the child's NR_ACTIVE_* by delta before CSS_DYING is visible
> to it.
>
> 3. The child is killed, and the two __update_lru_size() calls from
> step 1 land on the parent: NR_INACTIVE_* -delta, then NR_ACTIVE_*
> +delta.
>
> The child is left with NR_ACTIVE_* -delta while the parent holds the
> matching +delta, and reparenting the child's raw value is what cancels
> the parent's credit. However, if the child happens to hold few enough
> folios for that counter to be negative, lruvec_page_state_local() clamps
> the child's negative to zero, which drops the debit instead, so the
> parent keeps NR_ACTIVE_* overcounted by delta, and its active + inactive
> total overcounted as well, permanently: the child's local counters are
> gone once reparenting is done.
>
> So use a raw signed helper and reparent with that. Ordinary readers
> still keep the clamp.

Makes sense to me.

>
> Reported-by: Kunwu Chan <kunwu.chan@xxxxxxxxx>
> Closes: https://lore.kernel.org/linux-mm/CAN_Opa-+nb0vxQ+HsENCG2fEEMZoxG9Sy_DBC4CLkWjNLzDWKw@xxxxxxxxxxxxxx/
> Signed-off-by: Kairui Song <kasong@xxxxxxxxxxx>

Reviewed-by: SJ Park <sj@xxxxxxxxxx>


Thanks,
SJ

[...]