Re: [PATCH v2] zram: keep the hot statistics per CPU
From: Barry Song
Date: Sun Dec 06 2026 - 01:54:14 EST
On Sun, Dec 6, 2026 at 1:29 PM Sergey Senozhatsky
<senozhatsky@xxxxxxxxxxxx> wrote:
>
> When several processes free swapped out memory at the same time, e.g. on
> munmap() or exit, all CPUs update pages_stored, compr_data_size,
> same_pages, huge_pages, huge_pages_since and notify_free stats, so the
> cachelines bounce a lot [1]. Keep the six counters per CPU instead, as
> s64 + u64_stats_sync updated with preemption disabled.
>
> munmap() test:
> before after
> 4 x 256 MiB 18.69 ms 14.12 ms (-24%)
> 8 x 128 MiB 14.82 ms 10.09 ms (-32%)
>
> Link: https://patch.msgid.link/20261123054746.2579253-3-senozhatsky@xxxxxxxxxxxx
> Link: https://lore.kernel.org/20261017031542.3504947-1-hongfengchen@xxxxxxxxxx [1]
> Signed-off-by: Hongfeng Chen <hongfengchen@xxxxxxxxxx>
> Signed-off-by: Sergey Senozhatsky <senozhatsky@xxxxxxxxxxxx>
> Co-developed-by: Hongfeng Chen <hongfengchen@xxxxxxxxxx>
> ---
The patch looks good to me:
Reviewed-by: Barry Song <baohua@xxxxxxxxxx>
One thing, though: the original patch was part of a series, but
only one patch has been updated this time. I suspect this might
have confused Sashiko, as it reports "Failed To Apply".
Also, I'm not sure how Andrew will update the changelog, as he
usually says things about the entire series.
> drivers/block/zram/zram_drv.c | 116 ++++++++++++++++++++++++++--------
> drivers/block/zram/zram_drv.h | 27 ++++++--
> 2 files changed, 110 insertions(+), 33 deletions(-)
>
[...]
> @@ -1962,20 +2004,20 @@ static ssize_t mm_stat_show(struct device *dev, struct device_attribute *attr,
> zs_pool_stats(zram->mem_pool, &pool_stats);
> }
>
> - orig_size = atomic64_read(&zram->stats.pages_stored);
> + orig_size = zram_stat_read(zram, ZRAM_STAT_PAGES_STORED);
> max_used = atomic_long_read(&zram->stats.max_used_pages);
>
> ret = sysfs_emit(buf,
> "%8llu %8llu %8llu %8lu %8ld %8llu %8lu %8llu %8llu\n",
> orig_size << PAGE_SHIFT,
> - (u64)atomic64_read(&zram->stats.compr_data_size),
> + zram_stat_read(zram, ZRAM_STAT_COMPR_DATA_SIZE),
> mem_used << PAGE_SHIFT,
> zram->limit_pages << PAGE_SHIFT,
> max_used << PAGE_SHIFT,
> - (u64)atomic64_read(&zram->stats.same_pages),
> + zram_stat_read(zram, ZRAM_STAT_SAME_PAGES),
> atomic_long_read(&pool_stats.pages_compacted),
> - (u64)atomic64_read(&zram->stats.huge_pages),
> - (u64)atomic64_read(&zram->stats.huge_pages_since));
> + zram_stat_read(zram, ZRAM_STAT_HUGE_PAGES),
> + zram_stat_read(zram, ZRAM_STAT_HUGE_PAGES_SINCE));
>
I'm wondering if batching multiple counter updates and reads would
help further, along the lines of what Hongfeng originally proposed
(with the code adapted to Sergey's new implementation, of course):
+ stats = get_cpu_ptr(zram->pcpu_stats);
+ u64_stats_update_begin(&stats->syncp);
+ WRITE_ONCE(stats->pages_stored, stats->pages_stored + pages);
+ WRITE_ONCE(stats->compr_data_size, stats->compr_data_size + bytes);
+ WRITE_ONCE(stats->same_pages, stats->same_pages + same);
+ WRITE_ONCE(stats->huge_pages, stats->huge_pages + huge);
+ u64_stats_update_end(&stats->syncp);
+ put_cpu_ptr(zram->pcpu_stats);
and
+static void zram_pcpu_sum(struct zram *zram, struct zram_stats_sum *sum)
+{
+ ...
+
+ stats = per_cpu_ptr(zram->pcpu_stats, cpu);
+ do {
+ start = u64_stats_fetch_begin(&stats->syncp);
+ pages = READ_ONCE(stats->pages_stored);
+ bytes = READ_ONCE(stats->compr_data_size);
+ same = READ_ONCE(stats->same_pages);
+ huge = READ_ONCE(stats->huge_pages);
+ notify = READ_ONCE(stats->notify_free);
+ } while (u64_stats_fetch_retry(&stats->syncp, start));
+
+ ...
+ }
+}
Hongfeng, do you have any performance data on this? If batching does
help, I guess we could address it in a follow-up patch. If the numbers
show no meaningful improvement, there's probably no need to add
extra complexity.
Thanks,
Barry