* [PATCH 1/2] zsmalloc: free a zspage's pages in one batch
@ 2026-11-23 3:10 Sergey Senozhatsky
2026-11-23 3:10 ` [PATCH 2/2] zram: keep the hot statistics per CPU Sergey Senozhatsky
2026-11-23 3:14 ` [PATCH 1/2] zsmalloc: free a zspage's pages in one batch Sergey Senozhatsky
0 siblings, 2 replies; 5+ messages in thread
From: Sergey Senozhatsky @ 2026-11-23 3:10 UTC (permalink / raw)
To: Andrew Morton
Cc: Nhat Pham, Yosry Ahmed, Hongfeng Chen, Brian Geffon, Minchan Kim,
linux-kernel, linux-block, linux-mm, Sergey Senozhatsky
__free_frozen_pages() and dec_zone_page_state() take about 22% of the
profile [1].
Collect the pages of a multi-page zspage in a folio_batch and free
them with folios_put(), which takes the pcp lock once per run of
same-zone pages, and update NR_ZSPAGES once per such run as well
A zspage never has more pages than a folio_batch holds.
Single-page zspages, incompressible or power of 2 size classes, use
the current inline path. There is nothing to batch there, and
folio_batch is not free. The batch lives in its own noinline
function, which also keeps.
munmap() test:
next-20261119 v7.4-rc3
256 MiB -2.0% .. -4.1% -4.4%
1 GiB -2.8% .. -4.4% -3.8%
Link: https://lore.kernel.org/20261109105957.2577351-1-chenhongfeng0212@gmail.com
Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
---
mm/zsmalloc.c | 52 +++++++++++++++++++++++++++++++++++++++++++++------
1 file changed, 46 insertions(+), 6 deletions(-)
diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c
index f571b3c9e9ca..9dcd1549c2fb 100644
--- a/mm/zsmalloc.c
+++ b/mm/zsmalloc.c
@@ -37,6 +37,7 @@
#include <linux/debugfs.h>
#include <linux/zsmalloc.h>
#include <linux/fs.h>
+#include <linux/folio_batch.h>
#include <linux/workqueue.h>
#include "zpdesc.h"
@@ -866,6 +867,44 @@ static int trylock_zspage(struct zspage *zspage)
return 0;
}
+/*
+ * Free the pages of a multi-page zspage in one go, see folios_put().
+ * Not inlined, so that the folio_batch is only set up (and, with
+ * CONFIG_INIT_STACK_ALL_ZERO, zero-filled) when there is something
+ * to batch.
+ */
+static noinline void free_page_chain(struct zpdesc *zpdesc)
+{
+ struct zone *zone = zpdesc_zone(zpdesc);
+ struct folio_batch fbatch;
+ int nr = 0;
+
+ static_assert(ZS_MAX_PAGES_PER_ZSPAGE <= FOLIO_BATCH_SIZE);
+
+ folio_batch_init(&fbatch);
+ do {
+ struct zpdesc *next = get_next_zpdesc(zpdesc);
+
+ VM_BUG_ON_PAGE(!zpdesc_is_locked(zpdesc), zpdesc_page(zpdesc));
+ reset_zpdesc(zpdesc);
+ zpdesc_unlock(zpdesc);
+
+ /* NR_ZSPAGES is per zone: one update per same-zone run */
+ if (unlikely(zpdesc_zone(zpdesc) != zone)) {
+ mod_zone_page_state(zone, NR_ZSPAGES, -nr);
+ zone = zpdesc_zone(zpdesc);
+ nr = 0;
+ }
+ nr++;
+
+ folio_batch_add(&fbatch, zpdesc_folio(zpdesc));
+ zpdesc = next;
+ } while (zpdesc);
+
+ mod_zone_page_state(zone, NR_ZSPAGES, -nr);
+ folios_put(&fbatch);
+}
+
/*
* Three free helpers, kept apart here:
*
@@ -884,21 +923,22 @@ static int trylock_zspage(struct zspage *zspage)
*/
static inline void __free_zspage_lockless(struct zspage *zspage)
{
- struct zpdesc *zpdesc, *next;
+ struct zpdesc *zpdesc;
VM_BUG_ON(get_zspage_inuse(zspage));
VM_BUG_ON(zspage->fullness != ZS_INUSE_RATIO_0);
- next = zpdesc = get_first_zpdesc(zspage);
- do {
+ zpdesc = get_first_zpdesc(zspage);
+ if (get_next_zpdesc(zpdesc)) {
+ free_page_chain(zpdesc);
+ } else {
+ /* Single-page zspage, e.g. a huge object: nothing to batch */
VM_BUG_ON_PAGE(!zpdesc_is_locked(zpdesc), zpdesc_page(zpdesc));
- next = get_next_zpdesc(zpdesc);
reset_zpdesc(zpdesc);
zpdesc_unlock(zpdesc);
zpdesc_dec_zone_page_state(zpdesc);
zpdesc_put(zpdesc);
- zpdesc = next;
- } while (zpdesc != NULL);
+ }
cache_free_zspage(zspage);
}
--
2.56.1.750.gf70dc682cf-goog
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 2/2] zram: keep the hot statistics per CPU
2026-11-23 3:10 [PATCH 1/2] zsmalloc: free a zspage's pages in one batch Sergey Senozhatsky
@ 2026-11-23 3:10 ` Sergey Senozhatsky
2026-11-23 3:55 ` sashiko-bot
2026-11-23 3:14 ` [PATCH 1/2] zsmalloc: free a zspage's pages in one batch Sergey Senozhatsky
1 sibling, 1 reply; 5+ messages in thread
From: Sergey Senozhatsky @ 2026-11-23 3:10 UTC (permalink / raw)
To: Andrew Morton
Cc: Nhat Pham, Yosry Ahmed, Hongfeng Chen, Brian Geffon, Minchan Kim,
linux-kernel, linux-block, linux-mm, Sergey Senozhatsky
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. 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%)
Link: https://lore.kernel.org/20261017031542.3504947-1-hongfengchen@xiaomi.com
Signed-off-by: Hongfeng Chen <hongfengchen@xiaomi.com>
Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
---
drivers/block/zram/zram_drv.c | 108 ++++++++++++++++++++++++++--------
drivers/block/zram/zram_drv.h | 26 ++++++--
2 files changed, 102 insertions(+), 32 deletions(-)
diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
index 54eb691f1409..951a38caeb93 100644
--- a/drivers/block/zram/zram_drv.c
+++ b/drivers/block/zram/zram_drv.c
@@ -95,6 +95,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();
+ }
+}
+
static inline bool init_done(struct zram *zram)
{
return zram->disksize;
@@ -944,8 +985,9 @@ static int zram_writeback_complete(struct zram *zram, struct zram_wb_req *req)
clear_slot_flag(zram, index, ZRAM_IDLE);
if (test_slot_flag(zram, index, ZRAM_HUGE))
- atomic64_dec(&zram->stats.huge_pages);
- atomic64_sub(get_slot_size(zram, index), &zram->stats.compr_data_size);
+ zram_stat_sub(zram, ZRAM_STAT_HUGE_PAGES, 1);
+ zram_stat_sub(zram, ZRAM_STAT_COMPR_DATA_SIZE,
+ get_slot_size(zram, index));
zs_free(zram->mem_pool, &zram->table[index].handle);
zram->table[index].blk_idx = req->blk_idx;
set_slot_flag(zram, index, ZRAM_WB);
@@ -1927,7 +1969,7 @@ static ssize_t io_stat_show(struct device *dev, struct device_attribute *attr,
"%8llu %8llu 0 %8llu\n",
(u64)atomic64_read(&zram->stats.failed_reads),
(u64)atomic64_read(&zram->stats.failed_writes),
- (u64)atomic64_read(&zram->stats.notify_free));
+ zram_stat_read(zram, ZRAM_STAT_NOTIFY_FREE));
return ret;
}
@@ -1949,20 +1991,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));
return ret;
}
@@ -2045,7 +2087,7 @@ static void slot_free(struct zram *zram, unsigned long index)
* ZRAM_HUGE flag for deferred decompression path.
*/
if (!test_slot_flag(zram, index, ZRAM_WB))
- atomic64_dec(&zram->stats.huge_pages);
+ zram_stat_sub(zram, ZRAM_STAT_HUGE_PAGES, 1);
clear_slot_flag(zram, index, ZRAM_HUGE);
}
@@ -2061,7 +2103,7 @@ static void slot_free(struct zram *zram, unsigned long index)
*/
if (test_slot_flag(zram, index, ZRAM_SAME)) {
clear_slot_flag(zram, index, ZRAM_SAME);
- atomic64_dec(&zram->stats.same_pages);
+ zram_stat_sub(zram, ZRAM_STAT_SAME_PAGES, 1);
goto out;
}
@@ -2071,10 +2113,10 @@ static void slot_free(struct zram *zram, unsigned long index)
zs_free(zram->mem_pool, handle);
- atomic64_sub(get_slot_size(zram, index),
- &zram->stats.compr_data_size);
+ zram_stat_sub(zram, ZRAM_STAT_COMPR_DATA_SIZE,
+ get_slot_size(zram, index));
out:
- atomic64_dec(&zram->stats.pages_stored);
+ zram_stat_sub(zram, ZRAM_STAT_PAGES_STORED, 1);
zram->table[index].element = 0;
set_slot_size(zram, index, 0);
}
@@ -2246,8 +2288,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;
}
@@ -2277,10 +2319,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;
}
@@ -2340,8 +2382,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;
}
@@ -2532,8 +2574,8 @@ static int recompress_slot(struct zram *zram, unsigned long index,
set_slot_size(zram, index, comp_len_new);
set_slot_comp_priority(zram, index, prio);
- atomic64_add(comp_len_new, &zram->stats.compr_data_size);
- atomic64_inc(&zram->stats.pages_stored);
+ zram_stat_add(zram, ZRAM_STAT_COMPR_DATA_SIZE, comp_len_new);
+ zram_stat_add(zram, ZRAM_STAT_PAGES_STORED, 1);
return 0;
}
@@ -2711,7 +2753,7 @@ static void zram_bio_discard(struct zram *zram, struct bio *bio)
slot_lock(zram, index);
slot_free(zram, index);
slot_unlock(zram, index);
- atomic64_inc(&zram->stats.notify_free);
+ zram_stat_add(zram, ZRAM_STAT_NOTIFY_FREE, 1);
index++;
n -= PAGE_SIZE;
}
@@ -2804,7 +2846,7 @@ static void zram_slot_free_notify(struct block_device *bdev,
zram = bdev->bd_disk->private_data;
- atomic64_inc(&zram->stats.notify_free);
+ zram_stat_add(zram, ZRAM_STAT_NOTIFY_FREE, 1);
if (!slot_trylock(zram, index)) {
atomic64_inc(&zram->stats.miss_free);
return;
@@ -2845,6 +2887,8 @@ static void zram_destroy_comps(struct zram *zram)
static void zram_reset_device(struct zram *zram)
{
+ int cpu;
+
guard(rwsem_write)(&zram->dev_lock);
zram->limit_pages = 0;
@@ -2857,6 +2901,9 @@ static void zram_reset_device(struct zram *zram)
zram->disksize = 0;
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));
reset_bdev(zram);
#ifdef CONFIG_ZRAM_WRITEBACK
zram->wb_limit_enable = false;
@@ -3075,9 +3122,15 @@ static int zram_add(void)
if (!zram)
return -ENOMEM;
+ zram->pcpu_stats = alloc_percpu(struct zram_pcpu_stats);
+ if (!zram->pcpu_stats) {
+ ret = -ENOMEM;
+ goto out_free_dev;
+ }
+
ret = idr_alloc(&zram_index_idr, zram, 0, 0, GFP_KERNEL);
if (ret < 0)
- goto out_free_dev;
+ goto out_free_stats;
device_id = ret;
init_rwsem(&zram->dev_lock);
@@ -3119,6 +3172,8 @@ static int zram_add(void)
put_disk(zram->disk);
out_free_idr:
idr_remove(&zram_index_idr, device_id);
+out_free_stats:
+ free_percpu(zram->pcpu_stats);
out_free_dev:
kfree(zram);
return ret;
@@ -3168,6 +3223,7 @@ static int zram_remove(struct zram *zram)
zram_reset_device(zram);
put_disk(zram->disk);
+ free_percpu(zram->pcpu_stats);
kfree(zram);
return 0;
}
diff --git a/drivers/block/zram/zram_drv.h b/drivers/block/zram/zram_drv.h
index 724f8ce6709b..a3fa6e3d007f 100644
--- a/drivers/block/zram/zram_drv.h
+++ b/drivers/block/zram/zram_drv.h
@@ -10,6 +10,7 @@
#define _ZRAM_DRV_H_
#include <asm/byteorder.h>
+#include <asm/local64.h>
#include <linux/rwsem.h>
#include <linux/zsmalloc.h>
@@ -88,15 +89,27 @@ struct zram_table_entry {
};
};
+/*
+ * Updated for every page that is stored or freed, so kept per CPU: with
+ * shared counters, concurrent swap frees bounce their cacheline.
+ */
+enum zram_pcpu_stat {
+ ZRAM_STAT_PAGES_STORED, /* no. of pages currently stored */
+ ZRAM_STAT_COMPR_DATA_SIZE, /* compressed size of pages stored */
+ ZRAM_STAT_SAME_PAGES, /* no. of same element filled pages */
+ ZRAM_STAT_HUGE_PAGES, /* no. of huge pages */
+ ZRAM_STAT_HUGE_PAGES_SINCE, /* no. of huge pages since set up */
+ ZRAM_STAT_NOTIFY_FREE, /* no. of slot free notifications */
+ NR_ZRAM_PCPU_STATS,
+};
+
+struct zram_pcpu_stats {
+ local64_t stat[NR_ZRAM_PCPU_STATS];
+};
+
struct zram_stats {
- atomic64_t compr_data_size; /* compressed size of pages stored */
atomic64_t failed_reads; /* can happen when memory is too low */
atomic64_t failed_writes; /* can happen when memory is too low */
- atomic64_t notify_free; /* no. of swap slot free notifications */
- atomic64_t same_pages; /* no. of same element filled pages */
- atomic64_t huge_pages; /* no. of huge pages */
- atomic64_t huge_pages_since; /* no. of huge pages since zram set up */
- atomic64_t pages_stored; /* no. of pages currently stored */
atomic_long_t max_used_pages; /* no. of maximum pages stored */
atomic64_t miss_free; /* no. of missed free */
#ifdef CONFIG_ZRAM_WRITEBACK
@@ -132,6 +145,7 @@ struct zram {
unsigned long limit_pages;
struct zram_stats stats;
+ struct zram_pcpu_stats __percpu *pcpu_stats;
/*
* This is the limit on amount of *uncompressed* worth of data
* we can store in a disk.
--
2.56.1.750.gf70dc682cf-goog
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 1/2] zsmalloc: free a zspage's pages in one batch
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:14 ` Sergey Senozhatsky
1 sibling, 0 replies; 5+ messages in thread
From: Sergey Senozhatsky @ 2026-11-23 3:14 UTC (permalink / raw)
To: Sergey Senozhatsky
Cc: Andrew Morton, Nhat Pham, Yosry Ahmed, Hongfeng Chen,
Brian Geffon, Minchan Kim, linux-kernel, linux-block, linux-mm
On (26/11/23 12:10), Sergey Senozhatsky wrote:
> [...] The batch lives in its own noinline
> function, which also keeps.
Oh, the "which also keeps" part is a lefover from a previous version of the
patch.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 2/2] zram: keep the hot statistics per CPU
2026-11-23 3:10 ` [PATCH 2/2] zram: keep the hot statistics per CPU Sergey Senozhatsky
@ 2026-11-23 3:55 ` sashiko-bot
2026-11-23 5:27 ` Sergey Senozhatsky
0 siblings, 1 reply; 5+ messages in thread
From: sashiko-bot @ 2026-11-23 3:55 UTC (permalink / raw)
To: Sergey Senozhatsky, Andrew Morton
Cc: Nhat Pham, Yosry Ahmed, Hongfeng Chen, Brian Geffon, Minchan Kim,
linux-mm
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 2/2] zram: keep the hot statistics per CPU
2026-11-23 3:55 ` sashiko-bot
@ 2026-11-23 5:27 ` Sergey Senozhatsky
0 siblings, 0 replies; 5+ messages in thread
From: Sergey Senozhatsky @ 2026-11-23 5:27 UTC (permalink / raw)
To: sashiko-reviews
Cc: Sergey Senozhatsky, Andrew Morton, Nhat Pham, Yosry Ahmed,
Hongfeng Chen, Brian Geffon, Minchan Kim, linux-mm
On (26/11/23 03:55), sashiko-bot@kernel.org wrote:
> 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.
I high prio mm_stats reader is an unlikely scnerio, but yes, okay.
> 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.
I disliked the clamping approach when I looked at it. But thinking
about it more, I think it is the right thing to do. It's perfectly
legal for zram to return 0 for those dynamic stats: no data stored,
no incompressible pages, no same-filled pages, no slot-free, etc.
So userspace should not be surprised to occasionally see zeroes for
those stats.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-23 5:27 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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
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.