* [PATCH v3 0/2] zram: zsmalloc: speed up zspage free path @ 2026-11-23 5:47 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 0 siblings, 2 replies; 17+ messages in thread From: Sergey Senozhatsky @ 2026-11-23 5:47 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 This is a followup on deferred zs_free() discussion [1]. We cannot do deferred zs_free(), at least not on the zsmalloc side, due to the fact that object handle is now stored in-place on the client side (zswap/zram entry). There is still a possibility for a speed up tho. One is batching of zspage's pages free. The other one is moving zram stats into per-CPU area (as was suggested by Hongfeng Chen), which reduces cache lines bouncing in zram slot-free notification path. [1] https://lore.kernel.org/all/20261016120927.3191690-1-hongfengchen@xiaomi.com/ v2 -> v3: - clamp negative stats to 0 (Sashiko). Sergey Senozhatsky (2): zsmalloc: free a zspage's pages in one batch zram: keep the hot statistics per CPU drivers/block/zram/zram_drv.c | 99 ++++++++++++++++++++++++++--------- drivers/block/zram/zram_drv.h | 26 ++++++--- mm/zsmalloc.c | 52 +++++++++++++++--- 3 files changed, 139 insertions(+), 38 deletions(-) -- 2.56.1.750.gf70dc682cf-goog ^ permalink raw reply [flat|nested] 17+ messages in thread
* [PATCH v3 1/2] zsmalloc: free a zspage's pages in one batch 2026-11-23 5:47 [PATCH v3 0/2] zram: zsmalloc: speed up zspage free path Sergey Senozhatsky @ 2026-11-23 5:47 ` Sergey Senozhatsky 2026-11-24 0:11 ` + zsmalloc-free-a-zspages-pages-in-one-batch.patch added to mm-new branch Andrew Morton 2026-11-23 5:47 ` [PATCH v3 2/2] zram: keep the hot statistics per CPU Sergey Senozhatsky 1 sibling, 1 reply; 17+ messages in thread From: Sergey Senozhatsky @ 2026-11-23 5:47 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, hence the batch path lives in its own noinline function. munmap() test: next-20261119 v7.4-rc3 256 MiB -2.0% .. -4.1% -4.4% 1 GiB -2.8% .. -4.4% -3.8% [1] https://lore.kernel.org/20261109105957.2577351-1-chenhongfeng0212@gmail.com Reported-by: Hongfeng Chen <hongfengchen@xiaomi.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 d5f313754934..a1b6161fd237 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" @@ -837,6 +838,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: * @@ -855,21 +894,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] 17+ messages in thread
* + zsmalloc-free-a-zspages-pages-in-one-batch.patch added to mm-new branch 2026-11-23 5:47 ` [PATCH v3 1/2] zsmalloc: free a zspage's pages in one batch Sergey Senozhatsky @ 2026-11-24 0:11 ` Andrew Morton 0 siblings, 0 replies; 17+ messages in thread From: Andrew Morton @ 2026-11-24 0:11 UTC (permalink / raw) To: mm-commits, yosry, nphamcs, minchan, hongfengchen, chenhongfeng0212, bgeffon, senozhatsky, akpm The patch titled Subject: zsmalloc: free a zspage's pages in one batch has been added to the -mm mm-new branch. Its filename is zsmalloc-free-a-zspages-pages-in-one-batch.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/zsmalloc-free-a-zspages-pages-in-one-batch.patch This patch will later appear in the mm-new branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new. The mm-new branch of mm.git is not included in linux-next If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there most days ------------------------------------------------------ From: Sergey Senozhatsky <senozhatsky@chromium.org> Subject: zsmalloc: free a zspage's pages in one batch Date: Mon, 23 Nov 2026 14:47:36 +0900 Patch series "zram: zsmalloc: speed up zspage free path", v3. This is a followup on deferred zs_free() discussion [1]. We cannot do deferred zs_free(), at least not on the zsmalloc side, due to the fact that object handle is now stored in-place on the client side (zswap/zram entry). There is still a possibility for a speed up tho. One is batching of zspage's pages free. The other one is moving zram stats into per-CPU area (as was suggested by Hongfeng Chen), which reduces cache lines bouncing in zram slot-free notification path. This patch (of 2): __free_frozen_pages() and dec_zone_page_state() take about 22% of the profile [2]. 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, hence the batch path lives in its own noinline function. 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/20261123054746.2579253-1-senozhatsky@chromium.org Link: https://patch.msgid.link/20261123054746.2579253-2-senozhatsky@chromium.org Link: https://lore.kernel.org/all/20261016120927.3191690-1-hongfengchen@xiaomi.com/ [1] Link: https://lore.kernel.org/20261109105957.2577351-1-chenhongfeng0212@gmail.com [2] Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Reported-by: Hongfeng Chen <hongfengchen@xiaomi.com> Cc: Nhat Pham <nphamcs@gmail.com> Cc: Yosry Ahmed <yosry@kernel.org> Cc: Brian Geffon <bgeffon@google.com> Cc: Minchan Kim <minchan@kernel.org> Cc: Hongfeng Chen <chenhongfeng0212@gmail.com> mm/zsmalloc.c | 52 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 6 deletions(-) --- a/mm/zsmalloc.c~zsmalloc-free-a-zspages-pages-in-one-batch +++ a/mm/zsmalloc.c @@ -43,6 +43,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" @@ -844,6 +845,44 @@ unlock: } /* + * 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: * * __free_zspage_lockless(): bare core; walks zpdescs and returns pages @@ -861,21 +900,22 @@ unlock: */ 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); } _ Patches currently in -mm which might be from senozhatsky@chromium.org are zram-prefix-backends-printk-s.patch zram-remove-debugfs-entry-on-init-failure.patch zram-reset-writeback-state-in-zram_reset_device.patch zram-move-mark_slot_accessed-under-existing-slot-lock-sections.patch zram-remove-stale-warn_on-from-zram_read_page.patch zram-zstd-do-not-allocate-empty-c-d-dictionaries.patch zram-lz4hc-pre-initialize-dictionary-stream-in-setup_params.patch zram-lz4-use-lz4_decompress_safe_usingdict.patch zram-lz4hc-use-lz4_decompress_safe_usingdict.patch zram-zstd-do-not-use-kvzalloc-for-zstd-allocations.patch zram-split-zcomp-into-separate-r-w-streams.patch zram-introduce-shared-decompression-streams.patch zram-allocate-bounce-buffer-for-spanning-objects.patch zram-borrow-idle-decompression-streams-in-exclusive-mode.patch zram-use-a-singleton-compression-context-for-recompression.patch zram-drop-stale-comment-in-write_incompressible_page.patch zram-drop-get_slot_handle-and-set_slot_handle.patch zsmalloc-return-handle-from-obj_allocated.patch zsmalloc-switch-handle-type-to-struct-zs_handle.patch zram-check-mem_limit-before-allocating-zsmalloc-object.patch zram-allocate-zsmalloc-object-under-slot-lock.patch zswap-do-not-free-the-entry-if-its-pool-is-null.patch zsmalloc-encode-1-based-obj_idx-so-valid-obj-is-never-0.patch zsmalloc-embed-struct-zs_handle-into-zram-and-zswap-entries.patch zsmalloc-fold-zs_obj_write-into-zs_store.patch zsmalloc-populate-new-zspage-outside-class-lock-in-zs_store.patch zram-fix-quadratic-writeback-bitmap-search.patch zsmalloc-free-a-zspages-pages-in-one-batch.patch zram-keep-the-hot-statistics-per-cpu.patch zsmalloc-fix-spdx-license-identifier.patch zram-use-spdx-license-identifier.patch ^ permalink raw reply [flat|nested] 17+ messages in thread
* [PATCH v3 2/2] zram: keep the hot statistics per CPU 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 ` Sergey Senozhatsky 2026-11-24 0:11 ` + zram-keep-the-hot-statistics-per-cpu.patch added to mm-new branch Andrew Morton 2026-12-04 3:56 ` [PATCH v3 2/2] " Hongfeng Chen 1 sibling, 2 replies; 17+ messages in thread From: Sergey Senozhatsky @ 2026-11-23 5:47 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 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 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 Co-developed-by: Hongfeng Chen <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 | 99 ++++++++++++++++++++++++++--------- drivers/block/zram/zram_drv.h | 26 ++++++--- 2 files changed, 93 insertions(+), 32 deletions(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 54eb691f1409..96b135e3f727 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -95,6 +95,38 @@ 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 = 0; + int cpu; + + for_each_possible_cpu(cpu) { + stats = per_cpu_ptr(zram->pcpu_stats, cpu); + sum += local64_read(&stats->stat[item]); + } + + return sum < 0 ? 0 : sum; +} + static inline bool init_done(struct zram *zram) { return zram->disksize; @@ -944,8 +976,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 +1960,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 +1982,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 +2078,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 +2094,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 +2104,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 +2279,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 +2310,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 +2373,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 +2565,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 +2744,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 +2837,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 +2878,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 +2892,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 +3113,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 +3163,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 +3214,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] 17+ messages in thread
* + zram-keep-the-hot-statistics-per-cpu.patch added to mm-new branch 2026-11-23 5:47 ` [PATCH v3 2/2] zram: keep the hot statistics per CPU Sergey Senozhatsky @ 2026-11-24 0:11 ` Andrew Morton 2026-12-06 5:29 ` [PATCH v2] zram: keep the hot statistics per CPU Sergey Senozhatsky 2026-12-04 3:56 ` [PATCH v3 2/2] " Hongfeng Chen 1 sibling, 1 reply; 17+ messages in thread From: Andrew Morton @ 2026-11-24 0:11 UTC (permalink / raw) To: mm-commits, yosry, nphamcs, minchan, hongfengchen, chenhongfeng0212, bgeffon, senozhatsky, akpm The patch titled Subject: zram: keep the hot statistics per CPU has been added to the -mm mm-new branch. Its filename is zram-keep-the-hot-statistics-per-cpu.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/zram-keep-the-hot-statistics-per-cpu.patch This patch will later appear in the mm-new branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new. The mm-new branch of mm.git is not included in linux-next If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there most days ------------------------------------------------------ From: Sergey Senozhatsky <senozhatsky@chromium.org> Subject: zram: keep the hot statistics per CPU Date: Mon, 23 Nov 2026 14:47:37 +0900 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 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://patch.msgid.link/20261123054746.2579253-3-senozhatsky@chromium.org Link: https://lore.kernel.org/20261017031542.3504947-1-hongfengchen@xiaomi.com [1] Signed-off-by: Hongfeng Chen <hongfengchen@xiaomi.com> Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Co-developed-by: Hongfeng Chen <hongfengchen@xiaomi.com> Cc: Nhat Pham <nphamcs@gmail.com> Cc: Yosry Ahmed <yosry@kernel.org> Cc: Brian Geffon <bgeffon@google.com> Cc: Minchan Kim <minchan@kernel.org> Cc: Hongfeng Chen <chenhongfeng0212@gmail.com> drivers/block/zram/zram_drv.c | 99 +++++++++++++++++++++++--------- drivers/block/zram/zram_drv.h | 26 ++++++-- 2 files changed, 93 insertions(+), 32 deletions(-) --- a/drivers/block/zram/zram_drv.c~zram-keep-the-hot-statistics-per-cpu +++ a/drivers/block/zram/zram_drv.c @@ -101,6 +101,38 @@ static void slot_unlock(struct zram *zra 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 = 0; + int cpu; + + for_each_possible_cpu(cpu) { + stats = per_cpu_ptr(zram->pcpu_stats, cpu); + sum += local64_read(&stats->stat[item]); + } + + return sum < 0 ? 0 : sum; +} + static inline bool init_done(struct zram *zram) { return zram->disksize; @@ -950,8 +982,9 @@ static int zram_writeback_complete(struc 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); @@ -1933,7 +1966,7 @@ static ssize_t io_stat_show(struct devic "%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; } @@ -1955,20 +1988,20 @@ static ssize_t mm_stat_show(struct devic 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; } @@ -2051,7 +2084,7 @@ static void slot_free(struct zram *zram, * 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); } @@ -2067,7 +2100,7 @@ static void slot_free(struct zram *zram, */ 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; } @@ -2077,10 +2110,10 @@ static void slot_free(struct zram *zram, 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); } @@ -2252,8 +2285,8 @@ static int write_same_filled_page(struct 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 +2316,10 @@ static int write_incompressible_page(str 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 +2379,8 @@ static int zram_write_page(struct zram * 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; } @@ -2538,8 +2571,8 @@ static int recompress_slot(struct zram * 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; } @@ -2717,7 +2750,7 @@ static void zram_bio_discard(struct zram 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; } @@ -2810,7 +2843,7 @@ static void zram_slot_free_notify(struct 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; @@ -2851,6 +2884,8 @@ static void zram_destroy_comps(struct zr static void zram_reset_device(struct zram *zram) { + int cpu; + guard(rwsem_write)(&zram->dev_lock); zram->limit_pages = 0; @@ -2863,6 +2898,9 @@ static void zram_reset_device(struct zra 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; @@ -3081,9 +3119,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); @@ -3125,6 +3169,8 @@ out_cleanup_disk: 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; @@ -3174,6 +3220,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; } --- a/drivers/block/zram/zram_drv.h~zram-keep-the-hot-statistics-per-cpu +++ a/drivers/block/zram/zram_drv.h @@ -16,6 +16,7 @@ #define _ZRAM_DRV_H_ #include <asm/byteorder.h> +#include <asm/local64.h> #include <linux/rwsem.h> #include <linux/zsmalloc.h> @@ -94,15 +95,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 @@ -138,6 +151,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. _ Patches currently in -mm which might be from senozhatsky@chromium.org are zram-prefix-backends-printk-s.patch zram-remove-debugfs-entry-on-init-failure.patch zram-reset-writeback-state-in-zram_reset_device.patch zram-move-mark_slot_accessed-under-existing-slot-lock-sections.patch zram-remove-stale-warn_on-from-zram_read_page.patch zram-zstd-do-not-allocate-empty-c-d-dictionaries.patch zram-lz4hc-pre-initialize-dictionary-stream-in-setup_params.patch zram-lz4-use-lz4_decompress_safe_usingdict.patch zram-lz4hc-use-lz4_decompress_safe_usingdict.patch zram-zstd-do-not-use-kvzalloc-for-zstd-allocations.patch zram-split-zcomp-into-separate-r-w-streams.patch zram-introduce-shared-decompression-streams.patch zram-allocate-bounce-buffer-for-spanning-objects.patch zram-borrow-idle-decompression-streams-in-exclusive-mode.patch zram-use-a-singleton-compression-context-for-recompression.patch zram-drop-stale-comment-in-write_incompressible_page.patch zram-drop-get_slot_handle-and-set_slot_handle.patch zsmalloc-return-handle-from-obj_allocated.patch zsmalloc-switch-handle-type-to-struct-zs_handle.patch zram-check-mem_limit-before-allocating-zsmalloc-object.patch zram-allocate-zsmalloc-object-under-slot-lock.patch zswap-do-not-free-the-entry-if-its-pool-is-null.patch zsmalloc-encode-1-based-obj_idx-so-valid-obj-is-never-0.patch zsmalloc-embed-struct-zs_handle-into-zram-and-zswap-entries.patch zsmalloc-fold-zs_obj_write-into-zs_store.patch zsmalloc-populate-new-zspage-outside-class-lock-in-zs_store.patch zram-fix-quadratic-writeback-bitmap-search.patch zsmalloc-free-a-zspages-pages-in-one-batch.patch zram-keep-the-hot-statistics-per-cpu.patch zsmalloc-fix-spdx-license-identifier.patch zram-use-spdx-license-identifier.patch ^ permalink raw reply [flat|nested] 17+ messages in thread
* [PATCH v2] zram: keep the hot statistics per CPU 2026-11-24 0:11 ` + zram-keep-the-hot-statistics-per-cpu.patch added to mm-new branch Andrew Morton @ 2026-12-06 5:29 ` Sergey Senozhatsky 2026-12-06 5:35 ` Sergey Senozhatsky 0 siblings, 1 reply; 17+ messages in thread From: Sergey Senozhatsky @ 2026-12-06 5:29 UTC (permalink / raw) To: Andrew Morton Cc: Hongfeng Chen, Barry Song, 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 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@chromium.org Link: https://lore.kernel.org/20261017031542.3504947-1-hongfengchen@xiaomi.com [1] Signed-off-by: Hongfeng Chen <hongfengchen@xiaomi.com> Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org> Co-developed-by: Hongfeng Chen <hongfengchen@xiaomi.com> --- drivers/block/zram/zram_drv.c | 116 ++++++++++++++++++++++++++-------- drivers/block/zram/zram_drv.h | 27 ++++++-- 2 files changed, 110 insertions(+), 33 deletions(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 2ff75b4d3e42..0d585f01e7c2 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); + + u64_stats_update_begin(&stats->syncp); + WRITE_ONCE(stats->stat[item], stats->stat[item] + amount); + u64_stats_update_end(&stats->syncp); + 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); + + u64_stats_update_begin(&stats->syncp); + WRITE_ONCE(stats->stat[item], stats->stat[item] - amount); + 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; + unsigned int start; + s64 val, sum = 0; + int cpu; + + for_each_possible_cpu(cpu) { + stats = per_cpu_ptr(zram->pcpu_stats, cpu); + do { + start = u64_stats_fetch_begin(&stats->syncp); + val = READ_ONCE(stats->stat[item]); + } while (u64_stats_fetch_retry(&stats->syncp, start)); + sum += val; + } + + return sum < 0 ? 0 : sum; +} + static inline bool init_done(struct zram *zram) { return zram->disksize; @@ -942,8 +983,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); @@ -1940,7 +1982,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; } @@ -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)); return ret; } @@ -2059,7 +2101,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); } @@ -2075,7 +2117,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; } @@ -2085,10 +2127,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); } @@ -2260,8 +2302,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; } @@ -2291,10 +2333,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; } @@ -2354,8 +2396,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; } @@ -2546,8 +2588,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; } @@ -2726,7 +2768,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; } @@ -2819,7 +2861,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; @@ -2860,6 +2902,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; @@ -2872,6 +2916,12 @@ 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) { + struct zram_pcpu_stats *stats; + + stats = per_cpu_ptr(zram->pcpu_stats, cpu); + memset(stats->stat, 0, sizeof(stats->stat)); + } reset_bdev(zram); #ifdef CONFIG_ZRAM_WRITEBACK zram->wb_limit_enable = false; @@ -3084,15 +3134,24 @@ 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) return -ENOMEM; + zram->pcpu_stats = alloc_percpu(struct zram_pcpu_stats); + if (!zram->pcpu_stats) { + 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) - goto out_free_dev; + goto out_free_stats; device_id = ret; init_rwsem(&zram->dev_lock); @@ -3134,6 +3193,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; @@ -3183,6 +3244,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..bcf4280940a9 100644 --- a/drivers/block/zram/zram_drv.h +++ b/drivers/block/zram/zram_drv.h @@ -11,6 +11,7 @@ #include <asm/byteorder.h> #include <linux/rwsem.h> +#include <linux/u64_stats_sync.h> #include <linux/zsmalloc.h> #include "zcomp.h" @@ -88,15 +89,28 @@ 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 { + struct u64_stats_sync syncp; + s64 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 +146,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.987.g2ec0ab4260-goog ^ permalink raw reply related [flat|nested] 17+ messages in thread
* Re: [PATCH v2] zram: keep the hot statistics per CPU 2026-12-06 5:29 ` [PATCH v2] zram: keep the hot statistics per CPU Sergey Senozhatsky @ 2026-12-06 5:35 ` Sergey Senozhatsky 0 siblings, 0 replies; 17+ messages in thread From: Sergey Senozhatsky @ 2026-12-06 5:35 UTC (permalink / raw) To: Andrew Morton Cc: Hongfeng Chen, Barry Song, Minchan Kim, linux-kernel, linux-block, linux-mm, Sergey Senozhatsky On (26/12/06 14:29), Sergey Senozhatsky 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@chromium.org > Link: https://lore.kernel.org/20261017031542.3504947-1-hongfengchen@xiaomi.com [1] > Signed-off-by: Hongfeng Chen <hongfengchen@xiaomi.com> > Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org> > Co-developed-by: Hongfeng Chen <hongfengchen@xiaomi.com> Andrew, can you please replace 25-new.git/tree/patches/zram-keep-the-hot-statistics-per-cpu.patch with this patch? ^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH v3 2/2] zram: keep the hot statistics per CPU 2026-11-23 5:47 ` [PATCH v3 2/2] zram: keep the hot statistics per CPU Sergey Senozhatsky 2026-11-24 0:11 ` + zram-keep-the-hot-statistics-per-cpu.patch added to mm-new branch Andrew Morton @ 2026-12-04 3:56 ` Hongfeng Chen 2026-12-04 4:15 ` Barry Song 2026-12-06 3:55 ` Sergey Senozhatsky 1 sibling, 2 replies; 17+ messages in thread From: Hongfeng Chen @ 2026-12-04 3:56 UTC (permalink / raw) To: Sergey Senozhatsky Cc: Hongfeng Chen, Andrew Morton, Minchan Kim, Nhat Pham, Yosry Ahmed, Brian Geffon, Barry Song, Jens Axboe, linux-mm, linux-block, linux-kernel From: Hongfeng Chen <hongfengchen@xiaomi.com> On Mon, Nov 23, 2026 at 02:47:37PM +0900, Sergey Senozhatsky wrote: [..] > +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); > +} On x86 local64_add() is an add without the LOCK prefix, but on arm64, which has no asm/local.h, it is a full atomic64_add(), and this patch makes munmap() slower there. munmap() of 256MB of swapped out memory per process (ms), mm-new and mm-new with the diff below: mm-new patched speed-up RK3588 A76 1 proc 23.23 19.94 1.165x RK3588 A76 4 procs 40.00 37.86 1.057x RK3588 A55 1 proc 47.98 45.84 1.047x RK3588 A55 4 procs 95.97 94.73 1.013x i7-14700HX 1 proc 10.24 10.22 1.002x i7-14700HX 4 procs 30.26 29.89 1.012x Could you fold something like this in? --- diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index cadce71c2d2a..ca4d38cadf77 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -106,7 +106,7 @@ 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]); + WRITE_ONCE(stats->stat[item], stats->stat[item] + amount); put_cpu_ptr(zram->pcpu_stats); } @@ -115,7 +115,7 @@ 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]); + WRITE_ONCE(stats->stat[item], stats->stat[item] - amount); put_cpu_ptr(zram->pcpu_stats); } @@ -127,7 +127,7 @@ static u64 zram_stat_read(struct zram *zram, enum zram_pcpu_stat item) for_each_possible_cpu(cpu) { stats = per_cpu_ptr(zram->pcpu_stats, cpu); - sum += local64_read(&stats->stat[item]); + sum += READ_ONCE(stats->stat[item]); } return sum < 0 ? 0 : sum; diff --git a/drivers/block/zram/zram_drv.h b/drivers/block/zram/zram_drv.h index 1fbb00dc2c29..fe6ab482b280 100644 --- a/drivers/block/zram/zram_drv.h +++ b/drivers/block/zram/zram_drv.h @@ -16,7 +16,6 @@ #define _ZRAM_DRV_H_ #include <asm/byteorder.h> -#include <asm/local64.h> #include <linux/rwsem.h> #include <linux/zsmalloc.h> @@ -110,7 +109,7 @@ enum zram_pcpu_stat { }; struct zram_pcpu_stats { - local64_t stat[NR_ZRAM_PCPU_STATS]; + s64 stat[NR_ZRAM_PCPU_STATS]; }; struct zram_stats { ^ permalink raw reply related [flat|nested] 17+ messages in thread
* Re: [PATCH v3 2/2] zram: keep the hot statistics per CPU 2026-12-04 3:56 ` [PATCH v3 2/2] " Hongfeng Chen @ 2026-12-04 4:15 ` Barry Song 2026-12-06 3:44 ` Sergey Senozhatsky 2026-12-06 3:55 ` Sergey Senozhatsky 1 sibling, 1 reply; 17+ messages in thread From: Barry Song @ 2026-12-04 4:15 UTC (permalink / raw) To: Hongfeng Chen Cc: Sergey Senozhatsky, Hongfeng Chen, Andrew Morton, Minchan Kim, Nhat Pham, Yosry Ahmed, Brian Geffon, Jens Axboe, linux-mm, linux-block, linux-kernel On Fri, Dec 4, 2026 at 11:56 AM Hongfeng Chen <chenhongfeng0212@gmail.com> wrote: > > From: Hongfeng Chen <hongfengchen@xiaomi.com> > > On Mon, Nov 23, 2026 at 02:47:37PM +0900, Sergey Senozhatsky wrote: > [..] > > +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); > > +} > > On x86 local64_add() is an add without the LOCK prefix, but on arm64, > which has no asm/local.h, it is a full atomic64_add(), and this patch > makes munmap() slower there. Right. On x86, an RMW operation can be performed in a single instruction, which is atomic with respect to the local CPU. A LOCK prefix is needed to guarantee atomicity across CPUs for ordinary RMW instructions. On arm64, we have to rely on atomic instructions or disable preemption for CPU-local RMW operations. In your case, get_cpu_ptr() already disables preemption, so I think it's safe to switch to non-atomic operations on both x86 and arm64, assuming there are no concurrent accesses from interrupt contexts. Hi Sergey, Could we squash Hongfeng's change into your original patch? Thanks, Barry ^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH v3 2/2] zram: keep the hot statistics per CPU 2026-12-04 4:15 ` Barry Song @ 2026-12-06 3:44 ` Sergey Senozhatsky 0 siblings, 0 replies; 17+ messages in thread From: Sergey Senozhatsky @ 2026-12-06 3:44 UTC (permalink / raw) To: Barry Song Cc: Hongfeng Chen, Sergey Senozhatsky, Hongfeng Chen, Andrew Morton, Minchan Kim, Nhat Pham, Yosry Ahmed, Brian Geffon, Jens Axboe, linux-mm, linux-block, linux-kernel On (26/12/04 12:15), Barry Song wrote: > On Fri, Dec 4, 2026 at 11:56 AM Hongfeng Chen > <chenhongfeng0212@gmail.com> wrote: > > > > From: Hongfeng Chen <hongfengchen@xiaomi.com> > > > > On Mon, Nov 23, 2026 at 02:47:37PM +0900, Sergey Senozhatsky wrote: > > [..] > > > +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); > > > +} > > > > On x86 local64_add() is an add without the LOCK prefix, but on arm64, > > which has no asm/local.h, it is a full atomic64_add(), and this patch > > makes munmap() slower there. > > Right. On x86, an RMW operation can be performed in a single > instruction, which is atomic with respect to the local CPU. > A LOCK prefix is needed to guarantee atomicity across CPUs > for ordinary RMW instructions. > > On arm64, we have to rely on atomic instructions or disable > preemption for CPU-local RMW operations. > > In your case, get_cpu_ptr() already disables preemption, so I > think it's safe to switch to non-atomic operations on both x86 > and arm64, assuming there are no concurrent accesses from > interrupt contexts. > > Hi Sergey, > Could we squash Hongfeng's change into your original patch? Hi Hongfeng, Barry, Sure, let me work on this. Thank you! ^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH v3 2/2] zram: keep the hot statistics per CPU 2026-12-04 3:56 ` [PATCH v3 2/2] " Hongfeng Chen 2026-12-04 4:15 ` Barry Song @ 2026-12-06 3:55 ` Sergey Senozhatsky 2026-12-06 4:58 ` Barry Song 1 sibling, 1 reply; 17+ messages in thread From: Sergey Senozhatsky @ 2026-12-06 3:55 UTC (permalink / raw) To: Hongfeng Chen Cc: Sergey Senozhatsky, Hongfeng Chen, Andrew Morton, Minchan Kim, Nhat Pham, Yosry Ahmed, Brian Geffon, Barry Song, Jens Axboe, linux-mm, linux-block, linux-kernel On (26/12/04 11:56), Hongfeng Chen wrote: [..] > @@ -106,7 +106,7 @@ 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]); > + WRITE_ONCE(stats->stat[item], stats->stat[item] + amount); > put_cpu_ptr(zram->pcpu_stats); > } > > @@ -115,7 +115,7 @@ 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]); > + WRITE_ONCE(stats->stat[item], stats->stat[item] - amount); > put_cpu_ptr(zram->pcpu_stats); How about __local64_add() / __local64_sub()? > struct zram_pcpu_stats { > - local64_t stat[NR_ZRAM_PCPU_STATS]; > + s64 stat[NR_ZRAM_PCPU_STATS]; > }; Is {WRITE, READ}_ONCE of s64 is going to work on 32-bit arch? ^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH v3 2/2] zram: keep the hot statistics per CPU 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 0 siblings, 2 replies; 17+ messages in thread From: Barry Song @ 2026-12-06 4:58 UTC (permalink / raw) To: Sergey Senozhatsky Cc: Hongfeng Chen, Hongfeng Chen, Andrew Morton, Minchan Kim, Nhat Pham, Yosry Ahmed, Brian Geffon, Jens Axboe, linux-mm, linux-block, linux-kernel On Sun, Dec 6, 2026 at 11:55 AM Sergey Senozhatsky <senozhatsky@chromium.org> wrote: > > On (26/12/04 11:56), Hongfeng Chen wrote: > [..] > > @@ -106,7 +106,7 @@ 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]); > > + WRITE_ONCE(stats->stat[item], stats->stat[item] + amount); > > put_cpu_ptr(zram->pcpu_stats); > > } > > > > @@ -115,7 +115,7 @@ 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]); > > + WRITE_ONCE(stats->stat[item], stats->stat[item] - amount); > > put_cpu_ptr(zram->pcpu_stats); > > 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. Indeed, we do have cross-CPU reads: static u64 zram_stat_read(struct zram *zram, enum zram_pcpu_stat item) { struct zram_pcpu_stats *stats; s64 sum = 0; int cpu; for_each_possible_cpu(cpu) { stats = per_cpu_ptr(zram->pcpu_stats, cpu); sum += local64_read(&stats->stat[item]); } return sum < 0 ? 0 : sum; } Thanks, Barry ^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH v3 2/2] zram: keep the hot statistics per CPU 2026-12-06 4:58 ` Barry Song @ 2026-12-06 5:27 ` Sergey Senozhatsky 2026-12-06 5:29 ` Hongfeng Chen 1 sibling, 0 replies; 17+ messages in thread From: Sergey Senozhatsky @ 2026-12-06 5:27 UTC (permalink / raw) To: Barry Song Cc: Sergey Senozhatsky, Hongfeng Chen, Hongfeng Chen, Andrew Morton, Minchan Kim, Nhat Pham, Yosry Ahmed, Brian Geffon, Jens Axboe, linux-mm, linux-block, linux-kernel On (26/12/06 12:58), Barry Song wrote: > On Sun, Dec 6, 2026 at 11:55 AM Sergey Senozhatsky > <senozhatsky@chromium.org> wrote: > > > > On (26/12/04 11:56), Hongfeng Chen wrote: > > [..] > > > @@ -106,7 +106,7 @@ 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]); > > > + WRITE_ONCE(stats->stat[item], stats->stat[item] + amount); > > > put_cpu_ptr(zram->pcpu_stats); > > > } > > > > > > @@ -115,7 +115,7 @@ 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]); > > > + WRITE_ONCE(stats->stat[item], stats->stat[item] - amount); > > > put_cpu_ptr(zram->pcpu_stats); > > > > How about __local64_add() / __local64_sub()? > > Makes sense to me, thanks! It seems that on arm64 with LTO enabled __READ_ONCE emits ldapr/ldar + "memory". So I ended up doing s64, with u64_stats_sync, basically what Hongfeng had in RFC patch. ^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH v3 2/2] zram: keep the hot statistics per CPU 2026-12-06 4:58 ` Barry Song 2026-12-06 5:27 ` Sergey Senozhatsky @ 2026-12-06 5:29 ` Hongfeng Chen 2026-12-06 5:34 ` Sergey Senozhatsky 1 sibling, 1 reply; 17+ messages in thread From: Hongfeng Chen @ 2026-12-06 5:29 UTC (permalink / raw) To: Barry Song Cc: Hongfeng Chen, Sergey Senozhatsky, Andrew Morton, Minchan Kim, Nhat Pham, Yosry Ahmed, Brian Geffon, Jens Axboe, Hongfeng Chen, linux-mm, linux-block, linux-kernel 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 { ^ permalink raw reply related [flat|nested] 17+ messages in thread
* Re: [PATCH v3 2/2] zram: keep the hot statistics per CPU 2026-12-06 5:29 ` Hongfeng Chen @ 2026-12-06 5:34 ` Sergey Senozhatsky 2026-12-06 6:19 ` Barry Song 0 siblings, 1 reply; 17+ messages in thread From: Sergey Senozhatsky @ 2026-12-06 5:34 UTC (permalink / raw) To: Hongfeng Chen Cc: Barry Song, Hongfeng Chen, Sergey Senozhatsky, Andrew Morton, Minchan Kim, Nhat Pham, Yosry Ahmed, Brian Geffon, Jens Axboe, linux-mm, linux-block, linux-kernel Hi, On (26/12/06 13:29), Hongfeng Chen wrote: > 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); I did a combo of syncp and s64 [1] [1] https://lore.kernel.org/linux-kernel/20261206052927.2499698-1-senozhatsky@chromium.org ^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH v3 2/2] zram: keep the hot statistics per CPU 2026-12-06 5:34 ` Sergey Senozhatsky @ 2026-12-06 6:19 ` Barry Song 2026-12-06 6:22 ` Sergey Senozhatsky 0 siblings, 1 reply; 17+ messages in thread From: Barry Song @ 2026-12-06 6:19 UTC (permalink / raw) To: Sergey Senozhatsky Cc: Hongfeng Chen, Hongfeng Chen, Andrew Morton, Minchan Kim, Nhat Pham, Yosry Ahmed, Brian Geffon, Jens Axboe, linux-mm, linux-block, linux-kernel On Sun, Dec 6, 2026 at 1:34 PM Sergey Senozhatsky <senozhatsky@chromium.org> wrote: > > Hi, > > On (26/12/06 13:29), Hongfeng Chen wrote: > > 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); > > I did a combo of syncp and s64 [1] Nice, thanks! That's exactly the approach Hongfeng proposed in his original RFC: https://lore.kernel.org/all/20261017031542.3504947-1-hongfengchen@xiaomi.com/ > > [1] https://lore.kernel.org/linux-kernel/20261206052927.2499698-1-senozhatsky@chromium.org ^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH v3 2/2] zram: keep the hot statistics per CPU 2026-12-06 6:19 ` Barry Song @ 2026-12-06 6:22 ` Sergey Senozhatsky 0 siblings, 0 replies; 17+ messages in thread From: Sergey Senozhatsky @ 2026-12-06 6:22 UTC (permalink / raw) To: Barry Song Cc: Sergey Senozhatsky, Hongfeng Chen, Hongfeng Chen, Andrew Morton, Minchan Kim, Nhat Pham, Yosry Ahmed, Brian Geffon, Jens Axboe, linux-mm, linux-block, linux-kernel On (26/12/06 14:19), Barry Song wrote: > > > 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); > > > > I did a combo of syncp and s64 [1] > > Nice, thanks! > That's exactly the approach Hongfeng proposed in his original RFC: Yes, kudos to Hongfeng. ^ permalink raw reply [flat|nested] 17+ messages in thread
end of thread, other threads:[~2026-12-06 6:22 UTC | newest] Thread overview: 17+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 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-24 0:11 ` + zsmalloc-free-a-zspages-pages-in-one-batch.patch added to mm-new branch Andrew Morton 2026-11-23 5:47 ` [PATCH v3 2/2] zram: keep the hot statistics per CPU Sergey Senozhatsky 2026-11-24 0:11 ` + zram-keep-the-hot-statistics-per-cpu.patch added to mm-new branch Andrew Morton 2026-12-06 5:29 ` [PATCH v2] zram: keep the hot statistics per CPU Sergey Senozhatsky 2026-12-06 5:35 ` Sergey Senozhatsky 2026-12-04 3:56 ` [PATCH v3 2/2] " 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 2026-12-06 5:34 ` Sergey Senozhatsky 2026-12-06 6:19 ` Barry Song 2026-12-06 6:22 ` 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.