From: Hongfeng Chen <chenhongfeng0212@gmail.com>
To: Barry Song <baohua@kernel.org>
Cc: Hongfeng Chen <hongfengchen@xiaomi.com>,
Sergey Senozhatsky <senozhatsky@chromium.org>,
Andrew Morton <akpm@linux-foundation.org>,
Minchan Kim <minchan@kernel.org>, Nhat Pham <nphamcs@gmail.com>,
Yosry Ahmed <yosry@kernel.org>, Brian Geffon <bgeffon@google.com>,
Jens Axboe <axboe@kernel.dk>,
Hongfeng Chen <chenhongfeng0212@gmail.com>,
linux-mm@kvack.org, linux-block@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3 2/2] zram: keep the hot statistics per CPU
Date: Sun, 6 Dec 2026 13:29:25 +0800 [thread overview]
Message-ID: <20261206052928.1523132-1-chenhongfeng0212@gmail.com> (raw)
In-Reply-To: <CAGsJ_4xHMgozbKKShorVBjeh2G+iTjOW1eQskkH2QuvsaCS6-g@mail.gmail.com>
From: Hongfeng Chen <hongfengchen@xiaomi.com>
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@chromium.org> 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);
put_cpu_ptr(zram->pcpu_stats);
}
@@ -115,19 +117,26 @@ 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]);
+ u64_stats_update_begin(&stats->syncp);
+ __local64_sub(amount, &stats->stat[item]);
+ u64_stats_update_end(&stats->syncp);
put_cpu_ptr(zram->pcpu_stats);
}
static u64 zram_stat_read(struct zram *zram, enum zram_pcpu_stat item)
{
struct zram_pcpu_stats *stats;
- s64 sum = 0;
+ unsigned int start;
+ s64 sum = 0, v;
int cpu;
for_each_possible_cpu(cpu) {
stats = per_cpu_ptr(zram->pcpu_stats, cpu);
- sum += local64_read(&stats->stat[item]);
+ do {
+ start = u64_stats_fetch_begin(&stats->syncp);
+ v = local64_read(&stats->stat[item]);
+ } while (u64_stats_fetch_retry(&stats->syncp, start));
+ sum += v;
}
return sum < 0 ? 0 : sum;
@@ -2914,8 +2923,8 @@ static void zram_reset_device(struct zram *zram)
zram_destroy_comps(zram);
memset(&zram->stats, 0, sizeof(zram->stats));
for_each_possible_cpu(cpu)
- memset(per_cpu_ptr(zram->pcpu_stats, cpu), 0,
- sizeof(struct zram_pcpu_stats));
+ memset(per_cpu_ptr(zram->pcpu_stats, cpu)->stat, 0,
+ sizeof_field(struct zram_pcpu_stats, stat));
reset_bdev(zram);
#ifdef CONFIG_ZRAM_WRITEBACK
zram->wb_limit_enable = false;
@@ -3128,7 +3137,7 @@ static int zram_add(void)
BLK_FEAT_SYNCHRONOUS,
};
struct zram *zram;
- int ret, device_id;
+ int ret, device_id, cpu;
zram = kzalloc_obj(struct zram);
if (!zram)
@@ -3139,6 +3148,8 @@ static int zram_add(void)
ret = -ENOMEM;
goto out_free_dev;
}
+ for_each_possible_cpu(cpu)
+ u64_stats_init(&per_cpu_ptr(zram->pcpu_stats, cpu)->syncp);
ret = idr_alloc(&zram_index_idr, zram, 0, 0, GFP_KERNEL);
if (ret < 0)
diff --git a/drivers/block/zram/zram_drv.h b/drivers/block/zram/zram_drv.h
index 1fbb00dc2c29..311cea397f6b 100644
--- a/drivers/block/zram/zram_drv.h
+++ b/drivers/block/zram/zram_drv.h
@@ -18,6 +18,7 @@
#include <asm/byteorder.h>
#include <asm/local64.h>
#include <linux/rwsem.h>
+#include <linux/u64_stats_sync.h>
#include <linux/zsmalloc.h>
#include "zcomp.h"
@@ -111,6 +112,7 @@ enum zram_pcpu_stat {
struct zram_pcpu_stats {
local64_t stat[NR_ZRAM_PCPU_STATS];
+ struct u64_stats_sync syncp;
};
struct zram_stats {
next prev parent reply other threads:[~2026-12-06 5:29 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 5:47 [PATCH v3 0/2] zram: zsmalloc: speed up zspage free path Sergey Senozhatsky
2026-11-23 5:47 ` [PATCH v3 1/2] zsmalloc: free a zspage's pages in one batch Sergey Senozhatsky
2026-11-23 5:47 ` [PATCH v3 2/2] zram: keep the hot statistics per CPU Sergey Senozhatsky
2026-12-04 3:56 ` Hongfeng Chen
2026-12-04 4:15 ` Barry Song
2026-12-06 3:44 ` Sergey Senozhatsky
2026-12-06 3:55 ` Sergey Senozhatsky
2026-12-06 4:58 ` Barry Song
2026-12-06 5:27 ` Sergey Senozhatsky
2026-12-06 5:29 ` Hongfeng Chen [this message]
2026-12-06 5:34 ` Sergey Senozhatsky
2026-12-06 6:19 ` Barry Song
2026-12-06 6:22 ` Sergey Senozhatsky
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261206052928.1523132-1-chenhongfeng0212@gmail.com \
--to=chenhongfeng0212@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=axboe@kernel.dk \
--cc=baohua@kernel.org \
--cc=bgeffon@google.com \
--cc=hongfengchen@xiaomi.com \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=minchan@kernel.org \
--cc=nphamcs@gmail.com \
--cc=senozhatsky@chromium.org \
--cc=yosry@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®