[RFC PATCH v5 0/4] zram/zsmalloc: multi-page compression for large folios
From: Nanzhe Zhao
Date: Fri Dec 04 2026 - 21:20:04 EST
When large folios are compressed at a larger granularity, we observe
a notable reduction in CPU usage and a significant improvement in
compression ratios.
mTHP's ability to be swapped out without splitting and swapped back in
as a whole allows compression and decompression at larger granularities.
This patchset enhances zsmalloc and zram by adding support for dividing
large folios into multi-page blocks, typically configured with a
2-order granularity. Without this patchset, a large folio is always
divided into nr_pages 4KiB blocks.
The granularity can be set using the ZSMALLOC_MULTI_PAGES_ORDER
setting, where the default of 2 allows all anonymous THP to benefit.
Examples include:
* A 16KiB large folio will be compressed and stored as a single 16KiB
block.
* A 64KiB large folio will be compressed and stored as four 16KiB
blocks.
We captured anonymous pages from four Android applications on a Pixel 6.
To evaluate throughput, we allocate a buffer via mmap(MAP_PRIVATE |
MAP_ANONYMOUS), populate it with the captured pages, and invoke
madvise(MADV_PAGEOUT) to swap the buffer out to zram, measuring the
pageout duration. We then sequentially touch every 4 KiB page to fault
them back in from zram, measuring the swapin duration.
For example, testing the 143 MiB WeChat corpus (times in seconds;
w/o: without the patch, w/: with the patch):
Algorithm pageout w/o (s) pageout w/ swapin w/o (s) swapin w/
lz4 0.247 0.210 (-14.8%) 0.137 0.113 (-17.6%)
lzo-rle 0.240 0.207 (-13.7%) 0.149 0.123 (-17.5%)
zstd 0.567 0.452 (-20.4%) 0.258 0.192 (-25.6%)
In v4 [2], we discussed and tested the two design issues left from v3:
The first issue is internal fragmentation in zsmalloc. Multi-page
compression increases the class gap between adjacent size classes,
and unused memory in partially filled zspages grows by ~1.8 to 2.4 MiB
across our tested apps. However, because the object count drops by
~74%, combined with better compression ratios, total memory
consumption actually decreases. For a synthetic extreme case
constructed to maximize class padding, memory overhead can increase
by up to 5.0% under the default chain size.
The second issue is partial read. When order-2 swapin allocation fails
under memory pressure, falling back to order-0 requires decompressing
the entire 16 KiB block for a single 4 KiB fault. Allowing direct
reclaim (__GFP_RECLAIM) for order-2 swapin eliminated over 99.9% of
partial decompressions (from 235k down to 218 in our stress test),
speeding up swapin by 8.3% without observable stall regression.
In v5, we would like to further discuss the compatibility of multi-page
compression with zram recompression and backing-device writeback.
Currently, zram recompression and backing-device writeback assume that
every slot corresponds to an independent PAGE_SIZE object. In v5, we
temporarily enforce mutual exclusion via Kconfig. We would like to
discuss the preferred architectural direction for full compatibility.
Regarding architecture support, on MIPS with D-cache aliasing,
flush_dcache_page() only flushes a single page. However, MIPS does not
support THP_SWAP, so multi-page swap-in cannot occur there. Non-swap
multi-page block I/O is left out of scope for now.
Changes since v4:
* Rebased onto akpm/mm-new (7.4-rc4).
* Patch 1 (zsmalloc):
- Clamped the last base size class to PAGE_SIZE and offset multi-page
classes by 1 to prevent class index collisions on 16K/64K kernels.
- Converted obj_write() cross-page copying to kmap_local_zpdesc().
- Updated set_first_obj_offset() assertion to check ZS_MAX_ALLOC_SIZE.
- Added 'depends on 64BIT' to avoid handle index overflow on 32-bit LPAE.
* Patch 2 (zram):
- Adopted the aligned head slot lock model: all slot operations (read,
write, discard, notify) serialize on the head slot lock of the
multi-page slots group.
- slot_free() frees the multi-page object through the head slot, and also
frees any single-page objects left behind on tail slots by failed
zram_slot_free_notify trylocks to prevent leaks.
- Fixed partial write RMW concurrency: recheck the multi-page flags under
the head slot lock; if the multi-page slot was concurrently freed,
return -EAGAIN to let the caller retry through the single-page path.
- Corrected zram_bio_discard() to compute slot-relative offsets and
re-lock the single-page slot if the multi-page slot was concurrently freed.
Note that this series only supports swap-generated discard, which is
issued in full swap-cluster units under CLUSTER_FLAG_DISCARD. Direct
block-layer discard is currently not supported.
- Enforced Kconfig mutual exclusion with ZRAM_WRITEBACK and ZRAM_MULTI_COMP.
* Patch 4 (swap):
- Added __GFP_NORETRY to order-2 swapin direct reclaim to prevent premature
OOM invocations on allocation failure.
[1] v3: https://lore.kernel.org/all/20241121222521.83458-1-21cnbao@xxxxxxxxx/
[2] v4: https://lore.kernel.org/all/20261115024455.1465726-1-zhaonanzhe@xxxxxxxxxx/
Barry Song (1):
zram: backend_zstd: use multi-page size for zstd parameter setup
Nanzhe Zhao (1):
mm: swap: allow direct reclaim for synchronous multi-page swapin
Tangquan Zheng (2):
mm: zsmalloc: support objects compressed based on multiple pages
zram: support compression at the granularity of multi-pages
Documentation/ABI/testing/sysfs-block-zram | 13 +
Documentation/admin-guide/blockdev/zram.rst | 3 +
Documentation/mm/zsmalloc.rst | 51 +-
drivers/block/zram/Kconfig | 10 +
drivers/block/zram/backend_zstd.c | 4 +-
drivers/block/zram/zcomp.c | 36 +-
drivers/block/zram/zcomp.h | 18 +-
drivers/block/zram/zram_drv.c | 705 +++++++++++++++++---
drivers/block/zram/zram_drv.h | 43 ++
include/linux/zsmalloc.h | 10 +-
mm/Kconfig | 41 +-
mm/swap_state.c | 7 +
mm/zsmalloc.c | 259 ++++---
13 files changed, 1009 insertions(+), 191 deletions(-)
--
2.43.0