Re: [PATCH v4 1/2] memcg: acquire peaks_lock when reading memory.peak

Muchun Song <[email protected]>
Newsgroups gmane.linux.kernel.cgroups,gmane.linux.kernel.mm,gmane.linux.kernel
Message-ID <[email protected]>

> On Aug 14, 2026, at 11:30, Ridong Chen <[email protected]> wrote:
> 
> From: Ridong Chen <[email protected]>
> 
> Sashiko reported that a reader can transiently observe a lower peak
> within a race window [1]. peak_show() returns
> max(local_watermark, ofp->value), but peak_write() updates those two
> under peaks_lock while the reader takes no lock. The interleaving is:
> 
>  writer (reset on fd A)                 reader (fd B)
>  ----------------------                 -------------
>  usage = page_counter_read(pc)
>  WRITE_ONCE(local_watermark, usage)
>  // watermark lowered to usage
>                                         lw  = READ_ONCE(local_watermark)
>                                         // sees the lowered usage
>                                         val = READ_ONCE(ofp->value)
>                                         // B's value not updated yet
>                                         return max(lw, val)
>                                         // both low -> low peak
>  WRITE_ONCE(peer_ctx->value, usage)
>  // B updated, but too late
> 
> Fix it by acquiring peaks_lock when reading the peak, so the reader sees
> a consistent snapshot of local_watermark and the per-fd values. The same
> race applies to memory.swap.peak, which shares peaks_lock and the
> peak_write() path, so take the lock there as well.
> 
> [1] https://sashiko.dev/#/patchset/[email protected]?part=1
> Fixes: c6f53ed8f213 ("mm, memcg: cg2 memory{.swap,}.peak write handlers")
> Assisted-by: Claude:claude-opus-4-8
> Signed-off-by: Ridong Chen <[email protected]>
> Acked-by: Johannes Weiner <[email protected]>
> Acked-by: Shakeel Butt <[email protected]>

Reviewed-by: Muchun Song <[email protected]>

Thanks
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.