zram: check comp algorithm availability earlier
authorSergey Senozhatsky <sergey.senozhatsky@gmail.com>
Thu, 25 Jun 2015 22:00:32 +0000 (15:00 -0700)
committerDanny Wood <danwood76@gmail.com>
Tue, 14 Sep 2021 19:02:54 +0000 (20:02 +0100)
Improvement idea by Marcin Jabrzyk.

comp_algorithm_store() silently accepts any supplied algorithm name,
because zram performs algorithm availability check later, during the
device configuration phase in disksize_store() and emits the following
error:

  "zram: Cannot initialise %s compressing backend"

this error line is somewhat generic and, besides, can indicate a failed
attempt to allocate compression backend's working buffers.

add algorithm availability check to comp_algorithm_store():

  echo lzz > /sys/block/zram0/comp_algorithm
  -bash: echo: write error: Invalid argument

Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Reported-by: Marcin Jabrzyk <m.jabrzyk@samsung.com>
Acked-by: Minchan Kim <minchan@kernel.org>
Cc: Nitin Gupta <ngupta@vflare.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Change-Id: Iece40446b18e7e37fa4ced3459d40ddbb9984297

drivers/block/zram/zcomp.c
drivers/block/zram/zcomp.h
drivers/block/zram/zram_drv.c

index b299ac8ee85cd6b8f5999e795488dfe1ec2f6ac2..c53617752b93cc3976f764e19cc436b5cb66cdcf 100644 (file)
@@ -286,6 +286,11 @@ ssize_t zcomp_available_show(const char *comp, char *buf)
        return sz;
 }
 
+bool zcomp_available_algorithm(const char *comp)
+{
+       return find_backend(comp) != NULL;
+}
+
 bool zcomp_set_max_streams(struct zcomp *comp, int num_strm)
 {
        return comp->set_max_streams(comp, num_strm);
index c59d1fca72c064d9990faf34dc1c95f1f9a807d1..46e2b9f8f1f0e3ec684736305c4d8a1ab8de0c97 100644 (file)
@@ -51,6 +51,7 @@ struct zcomp {
 };
 
 ssize_t zcomp_available_show(const char *comp, char *buf);
+bool zcomp_available_algorithm(const char *comp);
 
 struct zcomp *zcomp_create(const char *comp, int max_strm);
 void zcomp_destroy(struct zcomp *comp);
index 87df9f3bcec8310e32d6ecd2f7e8b58259936719..28343e440f66dc72662a39bd0d8332bce3e8f9cf 100644 (file)
@@ -286,6 +286,9 @@ static ssize_t comp_algorithm_store(struct device *dev,
        if (sz > 0 && zram->compressor[sz - 1] == '\n')
                zram->compressor[sz - 1] = 0x00;
 
+       if (!zcomp_available_algorithm(zram->compressor))
+               len = -EINVAL;
+
        up_write(&zram->init_lock);
        return len;
 }