Re: [PATCH v3 2/2] zram: keep the hot statistics per CPU
From: Barry Song
Date: Sun Dec 06 2026 - 01:20:01 EST
On Sun, Dec 6, 2026 at 1:34 PM Sergey Senozhatsky
<senozhatsky@xxxxxxxxxxxx> wrote:
>
> Hi,
>
> On (26/12/06 13:29), Hongfeng Chen wrote:
> > On Sun, Dec 06, 2026 at 12:58:53PM +0800, Barry Song wrote:
> > > On Sun, Dec 6, 2026 at 11:55 AM Sergey Senozhatsky
> > > <senozhatsky@xxxxxxxxxxxx> wrote:
> > > >
> > > > How about __local64_add() / __local64_sub()?
> > >
> > > Makes sense to me, thanks!
> > >
> > > > Is {WRITE, READ}_ONCE of s64 is going to work on 32-bit arch?
> > >
> > > I don't think so if there are cross-CPU reads. get_cpu_ptr()
> > > only disables preemption on the local CPU and doesn't prevent
> > > other CPUs from concurrently reading the counter. A remote
> > > CPU could still observe a torn 64-bit value on a 32-bit
> > > architecture.
> >
> > Agreed. How about the below? It reads the counters under
> > u64_stats_sync. On 32-bit this keeps the 64-bit reads consistent. On
> > 64-bit u64_stats_sync is empty.
> >
> > ---
> > diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
> > index cadce71c2d2a..820e693f0593 100644
> > --- a/drivers/block/zram/zram_drv.c
> > +++ b/drivers/block/zram/zram_drv.c
> > @@ -106,7 +106,9 @@ static void zram_stat_add(struct zram *zram, enum zram_pcpu_stat item,
> > {
> > struct zram_pcpu_stats *stats = get_cpu_ptr(zram->pcpu_stats);
> >
> > - local64_add(amount, &stats->stat[item]);
> > + u64_stats_update_begin(&stats->syncp);
> > + __local64_add(amount, &stats->stat[item]);
> > + u64_stats_update_end(&stats->syncp);
>
> I did a combo of syncp and s64 [1]
Nice, thanks!
That's exactly the approach Hongfeng proposed in his original RFC:
https://lore.kernel.org/all/20261017031542.3504947-1-hongfengchen@xxxxxxxxxx/
>
> [1] https://lore.kernel.org/linux-kernel/20261206052927.2499698-1-senozhatsky@xxxxxxxxxxxx