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 v2 2/2] zram: keep the hot statistics per CPU
Date: Tue, 24 Nov 2026 05:36:54 +0000 [thread overview]
Message-ID: <sashiko-outbox-230559@kernel.org> (raw)
In-Reply-To: <cd96ecab2cfbb9a50a0b5141c09f9fda086780a0.1795412161.git.senozhatsky@chromium.org>
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] zram: missing co-author Signed-off-by trailer for
Co-developed-by
- [Medium] zram: unbounded retry loop in zram_stat_read() when slot is
freed before add
--
commit 1e6f41c95000ee48e7db6e0bb20c176ae2d9349b
Author: Sergey Senozhatsky <senozhatsky@chromium.org>
> zram: keep the hot statistics per CPU
>
> When several processes free swapped out memory at the same time, e.g.
> on munmap() or exit, all CPUs update tpages_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 local64_t 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%)
>
> [1] https://lore.kernel.org/20261017031542.3504947-1-hongfengchen@xiaomi.com
>
> Suggested-by: Hongfeng Chen <hongfengchen@xiaomi.com>
> Co-developed-by: Hongfeng Chen <hongfengchen@xiaomi.com>
> Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
[Severity: High]
Per Documentation/process/submitting-patches.rst, a Co-developed-by:
trailer must be immediately followed by a matching Signed-off-by:
trailer from that co-author (Hongfeng Chen <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();
> + }
> +}
[ ... ]
> @@ -2252,8 +2294,8 @@ static int write_same_filled_page(struct zram *zram, unsigned long fill,
> mark_slot_accessed(zram, index);
> slot_unlock(zram, index);
>
> - atomic64_inc(&zram->stats.same_pages);
> - atomic64_inc(&zram->stats.pages_stored);
> + zram_stat_add(zram, ZRAM_STAT_SAME_PAGES, 1);
> + zram_stat_add(zram, ZRAM_STAT_PAGES_STORED, 1);
>
> return 0;
> }
> @@ -2283,10 +2325,10 @@ static int write_incompressible_page(struct zram *zram, struct page *page,
> mark_slot_accessed(zram, index);
> slot_unlock(zram, index);
>
> - atomic64_add(PAGE_SIZE, &zram->stats.compr_data_size);
> - atomic64_inc(&zram->stats.huge_pages);
> - atomic64_inc(&zram->stats.huge_pages_since);
> - atomic64_inc(&zram->stats.pages_stored);
> + zram_stat_add(zram, ZRAM_STAT_COMPR_DATA_SIZE, PAGE_SIZE);
> + zram_stat_add(zram, ZRAM_STAT_HUGE_PAGES, 1);
> + zram_stat_add(zram, ZRAM_STAT_HUGE_PAGES_SINCE, 1);
> + zram_stat_add(zram, ZRAM_STAT_PAGES_STORED, 1);
>
> return 0;
> }
> @@ -2346,8 +2388,8 @@ static int zram_write_page(struct zram *zram, struct page *page,
> slot_unlock(zram, index);
>
> /* Update stats */
> - atomic64_inc(&zram->stats.pages_stored);
> - atomic64_add(comp_len, &zram->stats.compr_data_size);
> + zram_stat_add(zram, ZRAM_STAT_PAGES_STORED, 1);
> + zram_stat_add(zram, ZRAM_STAT_COMPR_DATA_SIZE, comp_len);
>
> return ret;
> }
[Severity: Medium]
Can zram_stat_read() spin in an unbounded loop when a slot is freed
before the writer calls zram_stat_add()?
write_same_filled_page(), write_incompressible_page() and
zram_write_page() publish the slot and call slot_unlock() before calling
zram_stat_add(), whereas slot_free() calls zram_stat_sub() while holding
slot_lock():
CPU0 CPU1
write_same_filled_page()
set_slot_flag(..., ZRAM_SAME)
slot_unlock(zram, index)
<preempted>
zram_slot_free_notify()
slot_trylock(zram, index)
slot_free(zram, index)
zram_stat_sub(...,
ZRAM_STAT_SAME_PAGES, 1)
slot_unlock(zram, index)
mm_stat_show()
down_read(&zram->dev_lock)
zram_stat_read(...,
ZRAM_STAT_SAME_PAGES)
/* sum is -1 across all CPUs */
for (;;) <-- spins until CPU0 runs
zram_stat_add(...,
ZRAM_STAT_SAME_PAGES, 1)
While the writer is preempted or throttled, the true sum across all CPUs
is negative, so mm_stat_show() spins in zram_stat_read() holding
zram->dev_lock; if a SCHED_FIFO task runs mm_stat_show() on the same CPU
as the preempted writer, cond_resched() never yields to the writer and
livelocks the CPU.
Updating the stats before slot_unlock() and clamping negative sums to 0
in zram_stat_read() (similar to percpu_counter_sum_positive()) would
avoid the unbounded loop.
--
Sashiko AI review · https://sashiko.dev/#/patchset/a74aca7a3d27333972782b29786c7b33861a56f1.1795412161.git.senozhatsky@chromium.org?part=2
prev parent reply other threads:[~2026-11-24 5:37 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 5:36 [PATCH v2 1/2] zsmalloc: free a zspage's pages in one batch Sergey Senozhatsky
2026-11-23 5:36 ` [PATCH v2 2/2] zram: keep the hot statistics per CPU Sergey Senozhatsky
2026-11-23 5:38 ` Sergey Senozhatsky
2026-11-24 5:36 ` sashiko-bot [this message]
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-230559@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox