Skip to content

Commit 84112e3

Browse files
sergey-senozhatskyakpm00
authored andcommitted
zram: add zlib compression backend support
Add s/w zlib (inflate/deflate) compression. Link: https://lkml.kernel.org/r/20240902105656.1383858-11-senozhatsky@chromium.org Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org> Cc: Minchan Kim <minchan@kernel.org> Cc: Nick Terrell <terrelln@fb.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent dbf2763 commit 84112e3

File tree

5 files changed

+158
-0
lines changed

5 files changed

+158
-0
lines changed

drivers/block/zram/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ config ZRAM_BACKEND_ZSTD
3838
select ZSTD_COMPRESS
3939
select ZSTD_DECOMPRESS
4040

41+
config ZRAM_BACKEND_DEFLATE
42+
bool "deflate compression support"
43+
depends on ZRAM
44+
select ZLIB_DEFLATE
45+
select ZLIB_INFLATE
46+
4147
choice
4248
prompt "Default zram compressor"
4349
default ZRAM_DEF_COMP_LZORLE
@@ -63,6 +69,10 @@ config ZRAM_DEF_COMP_ZSTD
6369
bool "zstd"
6470
depends on ZRAM_BACKEND_ZSTD
6571

72+
config ZRAM_DEF_COMP_DEFLATE
73+
bool "deflate"
74+
depends on ZRAM_BACKEND_DEFLATE
75+
6676
endchoice
6777

6878
config ZRAM_DEF_COMP
@@ -72,6 +82,7 @@ config ZRAM_DEF_COMP
7282
default "lz4" if ZRAM_DEF_COMP_LZ4
7383
default "lz4hc" if ZRAM_DEF_COMP_LZ4HC
7484
default "zstd" if ZRAM_DEF_COMP_ZSTD
85+
default "deflate" if ZRAM_DEF_COMP_DEFLATE
7586
default "unset-value"
7687

7788
config ZRAM_WRITEBACK

drivers/block/zram/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ zram-$(CONFIG_ZRAM_BACKEND_LZO) += backend_lzorle.o backend_lzo.o
66
zram-$(CONFIG_ZRAM_BACKEND_LZ4) += backend_lz4.o
77
zram-$(CONFIG_ZRAM_BACKEND_LZ4HC) += backend_lz4hc.o
88
zram-$(CONFIG_ZRAM_BACKEND_ZSTD) += backend_zstd.o
9+
zram-$(CONFIG_ZRAM_BACKEND_DEFLATE) += backend_deflate.o
910

1011
obj-$(CONFIG_ZRAM) += zram.o
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
3+
#include <linux/kernel.h>
4+
#include <linux/slab.h>
5+
#include <linux/vmalloc.h>
6+
#include <linux/zlib.h>
7+
8+
#include "backend_deflate.h"
9+
10+
/* Use the same value as crypto API */
11+
#define DEFLATE_DEF_WINBITS 11
12+
#define DEFLATE_DEF_MEMLEVEL MAX_MEM_LEVEL
13+
14+
struct deflate_ctx {
15+
struct z_stream_s cctx;
16+
struct z_stream_s dctx;
17+
s32 level;
18+
};
19+
20+
static void deflate_destroy(void *ctx)
21+
{
22+
struct deflate_ctx *zctx = ctx;
23+
24+
if (zctx->cctx.workspace) {
25+
zlib_deflateEnd(&zctx->cctx);
26+
vfree(zctx->cctx.workspace);
27+
}
28+
if (zctx->dctx.workspace) {
29+
zlib_inflateEnd(&zctx->dctx);
30+
vfree(zctx->dctx.workspace);
31+
}
32+
kfree(zctx);
33+
}
34+
35+
static void *deflate_create(void)
36+
{
37+
struct deflate_ctx *ctx;
38+
size_t sz;
39+
int ret;
40+
41+
ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
42+
if (!ctx)
43+
return NULL;
44+
45+
/* @FIXME: using a hardcoded Z_DEFAULT_COMPRESSION for now */
46+
ctx->level = Z_DEFAULT_COMPRESSION;
47+
sz = zlib_deflate_workspacesize(-DEFLATE_DEF_WINBITS, MAX_MEM_LEVEL);
48+
ctx->cctx.workspace = vzalloc(sz);
49+
if (!ctx->cctx.workspace)
50+
goto error;
51+
52+
ret = zlib_deflateInit2(&ctx->cctx, ctx->level, Z_DEFLATED,
53+
-DEFLATE_DEF_WINBITS, DEFLATE_DEF_MEMLEVEL,
54+
Z_DEFAULT_STRATEGY);
55+
if (ret != Z_OK)
56+
goto error;
57+
58+
sz = zlib_inflate_workspacesize();
59+
ctx->dctx.workspace = vzalloc(sz);
60+
if (!ctx->dctx.workspace)
61+
goto error;
62+
63+
ret = zlib_inflateInit2(&ctx->dctx, -DEFLATE_DEF_WINBITS);
64+
if (ret != Z_OK)
65+
goto error;
66+
67+
return ctx;
68+
69+
error:
70+
deflate_destroy(ctx);
71+
return NULL;
72+
}
73+
74+
static int deflate_compress(void *ctx, const unsigned char *src,
75+
size_t src_len, unsigned char *dst,
76+
size_t *dst_len)
77+
{
78+
struct deflate_ctx *zctx = ctx;
79+
struct z_stream_s *deflate;
80+
int ret;
81+
82+
deflate = &zctx->cctx;
83+
ret = zlib_deflateReset(deflate);
84+
if (ret != Z_OK)
85+
return -EINVAL;
86+
87+
deflate->next_in = (u8 *)src;
88+
deflate->avail_in = src_len;
89+
deflate->next_out = (u8 *)dst;
90+
deflate->avail_out = *dst_len;
91+
92+
ret = zlib_deflate(deflate, Z_FINISH);
93+
if (ret != Z_STREAM_END)
94+
return -EINVAL;
95+
96+
*dst_len = deflate->total_out;
97+
return 0;
98+
}
99+
100+
static int deflate_decompress(void *ctx, const unsigned char *src,
101+
size_t src_len, unsigned char *dst,
102+
size_t dst_len)
103+
{
104+
struct deflate_ctx *zctx = ctx;
105+
struct z_stream_s *inflate;
106+
int ret;
107+
108+
inflate = &zctx->dctx;
109+
110+
ret = zlib_inflateReset(inflate);
111+
if (ret != Z_OK)
112+
return -EINVAL;
113+
114+
inflate->next_in = (u8 *)src;
115+
inflate->avail_in = src_len;
116+
inflate->next_out = (u8 *)dst;
117+
inflate->avail_out = dst_len;
118+
119+
ret = zlib_inflate(inflate, Z_SYNC_FLUSH);
120+
if (ret != Z_STREAM_END)
121+
return -EINVAL;
122+
123+
return 0;
124+
}
125+
126+
const struct zcomp_ops backend_deflate = {
127+
.compress = deflate_compress,
128+
.decompress = deflate_decompress,
129+
.create_ctx = deflate_create,
130+
.destroy_ctx = deflate_destroy,
131+
.name = "deflate",
132+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
3+
#ifndef __BACKEND_DEFLATE_H__
4+
#define __BACKEND_DEFLATE_H__
5+
6+
#include "zcomp.h"
7+
8+
extern const struct zcomp_ops backend_deflate;
9+
10+
#endif /* __BACKEND_DEFLATE_H__ */

drivers/block/zram/zcomp.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "backend_lz4.h"
1818
#include "backend_lz4hc.h"
1919
#include "backend_zstd.h"
20+
#include "backend_deflate.h"
2021

2122
static const struct zcomp_ops *backends[] = {
2223
#if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZO)
@@ -31,6 +32,9 @@ static const struct zcomp_ops *backends[] = {
3132
#endif
3233
#if IS_ENABLED(CONFIG_ZRAM_BACKEND_ZSTD)
3334
&backend_zstd,
35+
#endif
36+
#if IS_ENABLED(CONFIG_ZRAM_BACKEND_DEFLATE)
37+
&backend_deflate,
3438
#endif
3539
NULL
3640
};

0 commit comments

Comments
 (0)