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

From: Johannes Weiner

Date: Wed Aug 12 2026 - 12:49:13 EST


On Fri, Aug 07, 2026 at 04:59:59PM +0800, Ridong wrote:
> From: Ridong Chen <chenridong@xxxxxxxxxx>
>
> 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/20260730115314.1069089-1-ridong.chen@xxxxxxxxx?part=1
> Fixes: c6f53ed8f213 ("mm, memcg: cg2 memory{.swap,}.peak write handlers")
> Assisted-by: Claude:claude-opus-4-8
> Signed-off-by: Ridong Chen <chenridong@xxxxxxxxxx>

Acked-by: Johannes Weiner <hannes@xxxxxxxxxxx>