[PATCH] kernel: make zram honor available compressors when selecting the initial algo

Sven Roederer devel-sven at geroedel.de
Tue Nov 30 15:13:44 PST 2021


When initializing the ZRAM it has a hardcoded default of "lzo-rle" compressor. This
compressor is not available in OpenWrt as only "lzo" is provided. The current
behaviour make the PROCD_ZRAM_TMPFS feature failing, as procd-init is relying on
the kernels default-algo, which is not present. So setting tmpfs falls back to not
use ZRAM.
Patch the kernel to keep the current preference to "lzo-rle" but check that it is
present. If not, fall back to "lzo" or any other of the known compressors.

See also:
* http://lists.openwrt.org/pipermail/openwrt-devel/2020-September/031430.html
* 419f149e482641ddc520f80a7ab2038f7e2ebc8a

Signed-off-by: Sven Roederer <devel-sven at geroedel.de>
---
 .../pending-5.4/801-zram_default-algo.patch   | 109 ++++++++++++++++++
 1 file changed, 109 insertions(+)
 create mode 100644 target/linux/generic/pending-5.4/801-zram_default-algo.patch

diff --git a/target/linux/generic/pending-5.4/801-zram_default-algo.patch b/target/linux/generic/pending-5.4/801-zram_default-algo.patch
new file mode 100644
index 0000000000..332bf24fed
--- /dev/null
+++ b/target/linux/generic/pending-5.4/801-zram_default-algo.patch
@@ -0,0 +1,109 @@
+diff -ur linux-5.4.154.orig/drivers/block/zram/zcomp.c linux-5.4.154/drivers/block/zram/zcomp.c
+--- linux-5.4.154.orig/drivers/block/zram/zcomp.c	2021-10-17 10:42:35.000000000 +0200
++++ linux-5.4.154/drivers/block/zram/zcomp.c	2021-11-29 02:07:04.118702563 +0100
+@@ -14,24 +14,6 @@
+ 
+ #include "zcomp.h"
+ 
+-static const char * const backends[] = {
+-	"lzo",
+-	"lzo-rle",
+-#if IS_ENABLED(CONFIG_CRYPTO_LZ4)
+-	"lz4",
+-#endif
+-#if IS_ENABLED(CONFIG_CRYPTO_LZ4HC)
+-	"lz4hc",
+-#endif
+-#if IS_ENABLED(CONFIG_CRYPTO_842)
+-	"842",
+-#endif
+-#if IS_ENABLED(CONFIG_CRYPTO_ZSTD)
+-	"zstd",
+-#endif
+-	NULL
+-};
+-
+ static void zcomp_strm_free(struct zcomp_strm *zstrm)
+ {
+ 	if (!IS_ERR_OR_NULL(zstrm->tfm))
+diff -ur linux-5.4.154.orig/drivers/block/zram/zcomp.h linux-5.4.154/drivers/block/zram/zcomp.h
+--- linux-5.4.154.orig/drivers/block/zram/zcomp.h	2021-10-17 10:42:35.000000000 +0200
++++ linux-5.4.154/drivers/block/zram/zcomp.h	2021-11-29 02:05:57.777496700 +0100
+@@ -6,6 +6,24 @@
+ #ifndef _ZCOMP_H_
+ #define _ZCOMP_H_
+ 
++static const char * const backends[] = {
++        "lzo-rle",
++        "lzo",
++#if IS_ENABLED(CONFIG_CRYPTO_LZ4)
++        "lz4",
++#endif
++#if IS_ENABLED(CONFIG_CRYPTO_LZ4HC)
++        "lz4hc",
++#endif
++#if IS_ENABLED(CONFIG_CRYPTO_842)
++        "842",
++#endif
++#if IS_ENABLED(CONFIG_CRYPTO_ZSTD)
++        "zstd",
++#endif
++        NULL
++};
++
+ struct zcomp_strm {
+ 	/* compression/decompression buffer */
+ 	void *buffer;
+diff -ur linux-5.4.154.orig/drivers/block/zram/zram_drv.c linux-5.4.154/drivers/block/zram/zram_drv.c
+--- linux-5.4.154.orig/drivers/block/zram/zram_drv.c	2021-10-17 10:42:35.000000000 +0200
++++ linux-5.4.154/drivers/block/zram/zram_drv.c	2021-11-29 02:04:57.400399244 +0100
+@@ -41,7 +41,6 @@
+ static DEFINE_MUTEX(zram_index_mutex);
+ 
+ static int zram_major;
+-static const char *default_compressor = "lzo-rle";
+ 
+ /* Module params (documentation at end) */
+ static unsigned int num_devices = 1;
+@@ -1882,6 +1881,7 @@
+ 	struct zram *zram;
+ 	struct request_queue *queue;
+ 	int ret, device_id;
++	int comp_count = 0;
+ 
+ 	zram = kzalloc(sizeof(struct zram), GFP_KERNEL);
+ 	if (!zram)
+@@ -1915,6 +1915,24 @@
+ 		goto out_free_queue;
+ 	}
+ 
++	/* check default compressors and select 1st available */
++	for (; backends[comp_count]; comp_count++) {
++		if (crypto_has_alg(backends[comp_count], 0, 0)) {
++			pr_info("Using %s compressor as zram default\n",
++					backends[comp_count]);
++			strlcpy(zram->compressor, backends[comp_count],
++				sizeof(zram->compressor));
++		} else {
++			pr_warn("%s compressor not found. Checking next ...\n",
++					backends[comp_count]);
++		}
++	}
++	if (!zram->compressor) {
++		pr_err("Error finding a default compressor\n");
++		ret = -ENXIO;
++		goto out_free_queue;
++	}
++
+ 	zram->disk->major = zram_major;
+ 	zram->disk->first_minor = device_id;
+ 	zram->disk->fops = &zram_devops;
+@@ -1957,8 +1975,6 @@
+ 			(BDI_CAP_STABLE_WRITES | BDI_CAP_SYNCHRONOUS_IO);
+ 	device_add_disk(NULL, zram->disk, zram_disk_attr_groups);
+ 
+-	strlcpy(zram->compressor, default_compressor, sizeof(zram->compressor));
+-
+ 	zram_debugfs_register(zram);
+ 	pr_info("Added device: %s\n", zram->disk->disk_name);
+ 	return device_id;
-- 
2.30.2




More information about the openwrt-devel mailing list