Re: [PATCH v3 2/2] zram: keep the hot statistics per CPU

From: Barry Song

Date: Sat Dec 05 2026 - 23:59:08 EST


On Sun, Dec 6, 2026 at 11:55 AM Sergey Senozhatsky
<senozhatsky@xxxxxxxxxxxx> wrote:
>
> On (26/12/04 11:56), Hongfeng Chen wrote:
> [..]
> > @@ -106,7 +106,7 @@ 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]);
> > + WRITE_ONCE(stats->stat[item], stats->stat[item] + amount);
> > put_cpu_ptr(zram->pcpu_stats);
> > }
> >
> > @@ -115,7 +115,7 @@ static void zram_stat_sub(struct zram *zram, enum zram_pcpu_stat item,
> > {
> > struct zram_pcpu_stats *stats = get_cpu_ptr(zram->pcpu_stats);
> >
> > - local64_sub(amount, &stats->stat[item]);
> > + WRITE_ONCE(stats->stat[item], stats->stat[item] - amount);
> > put_cpu_ptr(zram->pcpu_stats);
>
> 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.

Indeed, we do have cross-CPU reads:

static u64 zram_stat_read(struct zram *zram, enum zram_pcpu_stat item)
{
struct zram_pcpu_stats *stats;
s64 sum = 0;
int cpu;

for_each_possible_cpu(cpu) {
stats = per_cpu_ptr(zram->pcpu_stats, cpu);
sum += local64_read(&stats->stat[item]);
}

return sum < 0 ? 0 : sum;
}

Thanks,
Barry