[PATCH v3 13/18] zram: lz4hc: compress levels above 9 as level 9

From: Michal Wilczynski

Date: Wed Dec 02 2026 - 12:35:43 EST


With the vendored LZ4, lz4hc levels 10 and up select upstream's optimal
parser, as they do in userspace. It suits data compressed once and read
many times; on zram's page-sized input it costs far more than it gains.
Writing 4K pages under KVM, against the fork's same levels:

binary kernel source
level 10 -74% speed, +0.06% -51% speed, -0.21% size
level 12 -78% speed, -0.07% -51% speed, -0.26% size

The fork's levels 10-16 only searched deeper than level 9, and on 4K
pages compressed within 0.05% of it. Compress them as level 9 again,
and log it, so that existing settings do not drop to a quarter or half
of their speed. This applies to recompression algorithms too: their
pages are the same 4K. f2fs, which compresses clusters of 16K and more,
keeps upstream's levels. Document it in zram.rst.

Signed-off-by: Michal Wilczynski <m.wilczynski@xxxxxxxxxxx>
---
Documentation/admin-guide/blockdev/zram.rst | 4 ++++
drivers/block/zram/backend_lz4hc.c | 10 ++++++++++
2 files changed, 14 insertions(+)

diff --git a/Documentation/admin-guide/blockdev/zram.rst b/Documentation/admin-guide/blockdev/zram.rst
index db256c53b1274d0bd429caedd307fd4a24f034b7..43f13ce803881bfd3f7bd997afe4d5e5d2877206 100644
--- a/Documentation/admin-guide/blockdev/zram.rst
+++ b/Documentation/admin-guide/blockdev/zram.rst
@@ -151,6 +151,10 @@ better the compression ratio, it even can take negatives values for some
algorithms), for other algorithms `level` is acceleration level (the higher
the value the lower the compression ratio).

+lz4hc compresses levels above 9 as level 9, and logs it: its higher levels
+select LZ4's optimal parser, which on page-sized input is several times
+slower for well under 1% smaller output.
+
Parameters are handed over to the compression algorithm when the device is
initialised, hence invalid parameters (or parameters that the selected
algorithm does not support) are reported by the `disksize` write, and not
diff --git a/drivers/block/zram/backend_lz4hc.c b/drivers/block/zram/backend_lz4hc.c
index 0d86bc4091c1fa167168d14f61fec253165d009c..6809fbe9b654cd51b7f8da031be1cb070ead47fc 100644
--- a/drivers/block/zram/backend_lz4hc.c
+++ b/drivers/block/zram/backend_lz4hc.c
@@ -36,6 +36,16 @@ static int lz4hc_setup_params(struct zcomp_params *params)
*/
pr_err("invalid compression level %d\n", params->level);
return -EINVAL;
+ } else if (params->level >= LZ4HC_OPT_MIN_CLEVEL) {
+ /*
+ * These levels select LZ4's optimal parser, which on a page
+ * compresses 2-5 times slower than level 9 for well under
+ * 1% smaller output. It suits data compressed once and
+ * read many times, not swap.
+ */
+ pr_info("level %d compresses as level %d\n", params->level,
+ LZ4HC_OPT_MIN_CLEVEL - 1);
+ params->level = LZ4HC_OPT_MIN_CLEVEL - 1;
}

if (!params->dict || !params->dict_sz)

--
2.34.1