Re: [PATCH v6 03/18] mm/memcontrol: avoid potential counter leak when reparenting local stats
From: KunWu Chan
Date: Mon Nov 23 2026 - 21:07:30 EST
On Tue, Nov 24, 2026 at 2:53 AM Kairui Song via B4 Relay
<devnull+kasong.tencent.com@xxxxxxxxxx> wrote:
>
> From: Kairui Song <kasong@xxxxxxxxxxx>
>
> 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.
>
> 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>
> ---
> mm/memcontrol.c | 15 +++++++++++----
> 1 file changed, 11 insertions(+), 4 deletions(-)
>
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index 091401cecf39..3f551e2e3206 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -562,11 +562,10 @@ unsigned long lruvec_page_state_monotonic(const struct lruvec *lruvec,
> return (unsigned long)READ_ONCE(pn->lruvec_stats->state[i]);
> }
>
> -unsigned long lruvec_page_state_local(const struct lruvec *lruvec,
> +static long __lruvec_page_state_local(const struct lruvec *lruvec,
> enum node_stat_item idx)
> {
> const struct mem_cgroup_per_node *pn;
> - long x;
> int i;
>
> if (mem_cgroup_disabled())
> @@ -577,7 +576,14 @@ unsigned long lruvec_page_state_local(const struct lruvec *lruvec,
> return 0;
>
> pn = container_of_const(lruvec, struct mem_cgroup_per_node, lruvec);
> - x = READ_ONCE(pn->lruvec_stats->state_local[i]);
> + return READ_ONCE(pn->lruvec_stats->state_local[i]);
> +}
> +
> +unsigned long lruvec_page_state_local(const struct lruvec *lruvec,
> + enum node_stat_item idx)
> +{
> + long x = __lruvec_page_state_local(lruvec, idx);
> +
> #ifdef CONFIG_SMP
> if (x < 0)
> x = 0;
> @@ -595,7 +601,8 @@ void reparent_memcg_lruvec_state_local(struct mem_cgroup *memcg,
>
> for_each_node(nid) {
> struct lruvec *lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(nid));
> - unsigned long value = lruvec_page_state_local(lruvec, idx);
> + /* Reparent the raw value: the clamp drops the child's debit. */
> + long value = __lruvec_page_state_local(lruvec, idx);
>
> __mod_memcg_lruvec_state(memcg, nid, idx, -value);
> __mod_memcg_lruvec_state(parent, nid, idx, value);
>
> --
> 2.56.1
>
>
Thanks for following up, Kairui. Reparenting the raw signed count
fixes the case I had in mind. I also checked the clear_refs and
workingset changes in v6. Both look good to me.
Reviewed-by: Kunwu Chan <kunwu.chan@xxxxxxxxx>