From: sashiko-bot@kernel.org
To: "Sergey Senozhatsky" <senozhatsky@chromium.org>,
"Andrew Morton" <akpm@linux-foundation.org>
Cc: "Nhat Pham" <nphamcs@gmail.com>, "Yosry Ahmed" <yosry@kernel.org>,
"Hongfeng Chen" <hongfengchen@xiaomi.com>,
"Brian Geffon" <bgeffon@google.com>,
"Minchan Kim" <minchan@kernel.org>,
linux-mm@kvack.org
Subject: Re: [PATCH 2/2] zram: keep the hot statistics per CPU
Date: Mon, 23 Nov 2026 03:55:38 +0000 [thread overview]
Message-ID: <sashiko-outbox-228621@kernel.org> (raw)
In-Reply-To: <8f0ec29c0e4230d10db088d29f79bba17c3ee6f0.1795403342.git.senozhatsky@chromium.org>
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] zram: unbounded retry loop in zram_stat_read() when stat sum
is negative
--
commit 2681914a77de11b7b626f04e6fc339b3a441683c
Author: Sergey Senozhatsky <senozhatsky@chromium.org>
zram: keep the hot statistics per CPU
This commit moves six hot zram statistics counters from shared atomic64_t
fields to per-CPU local64_t counters to reduce cacheline bouncing during
concurrent page stores and frees.
Link: https://lore.kernel.org/20261017031542.3504947-1-hongfengchen@xiaomi.com
> diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
> index 4f479d9051426..df7fbb844b888 100644
> --- a/drivers/block/zram/zram_drv.c
> +++ b/drivers/block/zram/zram_drv.c
> @@ -101,6 +101,47 @@ static void slot_unlock(struct zram *zram, unsigned long index)
> clear_and_wake_up_bit(ZRAM_ENTRY_LOCK_BIT, lock);
> }
>
> +static void zram_stat_add(struct zram *zram, enum zram_pcpu_stat item,
> + s64 amount)
> +{
> + struct zram_pcpu_stats *stats = get_cpu_ptr(zram->pcpu_stats);
> +
> + local64_add(amount, &stats->stat[item]);
> + put_cpu_ptr(zram->pcpu_stats);
> +}
> +
> +static void zram_stat_sub(struct zram *zram, enum zram_pcpu_stat item,
> + s64 amount)
> +{
> + struct zram_pcpu_stats *stats = get_cpu_ptr(zram->pcpu_stats);
> +
> + local64_sub(amount, &stats->stat[item]);
> + 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;
> + int cpu;
> +
> + /*
> + * Other CPUs keep updating while the values are added up: a page
> + * stored on a CPU already summed and freed on one not yet summed
> + * can make the sum negative. Sum them up again then.
> + */
> + for (;;) {
> + sum = 0;
> + for_each_possible_cpu(cpu) {
> + stats = per_cpu_ptr(zram->pcpu_stats, cpu);
> + sum += local64_read(&stats->stat[item]);
> + }
> + if (sum >= 0)
> + return sum;
> + cond_resched();
> + }
> +}
[Severity: Medium]
Can zram_stat_read() spin indefinitely (or livelock under a real-time
reader) when a writer is preempted between slot_unlock() and
zram_stat_add()?
zram_write_page(), write_same_filled_page(), and
write_incompressible_page() populate the slot and call slot_unlock()
before calling zram_stat_add(), whereas slot_free() calls zram_stat_sub()
under slot_lock():
CPU0 CPU1
zram_write_page()
slot_lock(zram, index)
zs_store(...)
set_slot_size(zram, index, len)
slot_unlock(zram, index)
<preempted>
zram_slot_free_notify()
slot_trylock(zram, index)
slot_free(zram, index)
zram_stat_sub(..., 1)
slot_unlock(zram, index)
mm_stat_show()
zram_stat_read()
sum < 0 <-- loops until CPU0 runs
While the writer on CPU0 remains preempted, the true sum across all CPUs
is negative, not just a transient artifact of racing with the summation
loop. Any reader in mm_stat_show() will busy-loop across all possible
CPUs with zram->dev_lock held until the writer runs, and a higher-priority
SCHED_FIFO reader on the same CPU as the preempted writer will never yield
via cond_resched(), livelocking the CPU.
Clamping negative sums to 0 in a single pass (like
percpu_counter_sum_positive()) and moving zram_stat_add() before
slot_unlock() would avoid the unbounded loop.
--
Sashiko AI review · https://sashiko.dev/#/patchset/5666c1c8c04998784c81fc8fe751e35aa40af578.1795403342.git.senozhatsky@chromium.org?part=2
next prev parent reply other threads:[~2026-11-23 3:55 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 3:10 [PATCH 1/2] zsmalloc: free a zspage's pages in one batch Sergey Senozhatsky
2026-11-23 3:10 ` [PATCH 2/2] zram: keep the hot statistics per CPU Sergey Senozhatsky
2026-11-23 3:55 ` sashiko-bot [this message]
2026-11-23 5:27 ` Sergey Senozhatsky
2026-11-23 3:14 ` [PATCH 1/2] zsmalloc: free a zspage's pages in one batch 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=sashiko-outbox-228621@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=bgeffon@google.com \
--cc=hongfengchen@xiaomi.com \
--cc=linux-mm@kvack.org \
--cc=minchan@kernel.org \
--cc=nphamcs@gmail.com \
--cc=sashiko-reviews@lists.linux.dev \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.