Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

zram: add zstd compression backend support

Add s/w zstd compression.

Link: https://lkml.kernel.org/r/20240902105656.1383858-9-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>

authored by

Sergey Senozhatsky and committed by
Andrew Morton
73e7d81a c60a4ef5

+123
+11
drivers/block/zram/Kconfig
··· 32 32 select LZ4HC_COMPRESS 33 33 select LZ4_DECOMPRESS 34 34 35 + config ZRAM_BACKEND_ZSTD 36 + bool "zstd compression support" 37 + depends on ZRAM 38 + select ZSTD_COMPRESS 39 + select ZSTD_DECOMPRESS 40 + 35 41 choice 36 42 prompt "Default zram compressor" 37 43 default ZRAM_DEF_COMP_LZORLE ··· 59 53 bool "lz4hc" 60 54 depends on ZRAM_BACKEND_LZ4HC 61 55 56 + config ZRAM_DEF_COMP_ZSTD 57 + bool "zstd" 58 + depends on ZRAM_BACKEND_ZSTD 59 + 62 60 endchoice 63 61 64 62 config ZRAM_DEF_COMP ··· 71 61 default "lzo" if ZRAM_DEF_COMP_LZO 72 62 default "lz4" if ZRAM_DEF_COMP_LZ4 73 63 default "lz4hc" if ZRAM_DEF_COMP_LZ4HC 64 + default "zstd" if ZRAM_DEF_COMP_ZSTD 74 65 default "unset-value" 75 66 76 67 config ZRAM_WRITEBACK
+1
drivers/block/zram/Makefile
··· 5 5 zram-$(CONFIG_ZRAM_BACKEND_LZO) += backend_lzorle.o backend_lzo.o 6 6 zram-$(CONFIG_ZRAM_BACKEND_LZ4) += backend_lz4.o 7 7 zram-$(CONFIG_ZRAM_BACKEND_LZ4HC) += backend_lz4hc.o 8 + zram-$(CONFIG_ZRAM_BACKEND_ZSTD) += backend_zstd.o 8 9 9 10 obj-$(CONFIG_ZRAM) += zram.o
+97
drivers/block/zram/backend_zstd.c
··· 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/zstd.h> 7 + 8 + #include "backend_zstd.h" 9 + 10 + struct zstd_ctx { 11 + zstd_cctx *cctx; 12 + zstd_dctx *dctx; 13 + void *cctx_mem; 14 + void *dctx_mem; 15 + s32 level; 16 + }; 17 + 18 + static void zstd_destroy(void *ctx) 19 + { 20 + struct zstd_ctx *zctx = ctx; 21 + 22 + vfree(zctx->cctx_mem); 23 + vfree(zctx->dctx_mem); 24 + kfree(zctx); 25 + } 26 + 27 + static void *zstd_create(void) 28 + { 29 + zstd_parameters params; 30 + struct zstd_ctx *ctx; 31 + size_t sz; 32 + 33 + ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 34 + if (!ctx) 35 + return NULL; 36 + 37 + ctx->level = zstd_default_clevel(); 38 + params = zstd_get_params(ctx->level, 0); 39 + sz = zstd_cctx_workspace_bound(&params.cParams); 40 + ctx->cctx_mem = vzalloc(sz); 41 + if (!ctx->cctx_mem) 42 + goto error; 43 + 44 + ctx->cctx = zstd_init_cctx(ctx->cctx_mem, sz); 45 + if (!ctx->cctx) 46 + goto error; 47 + 48 + sz = zstd_dctx_workspace_bound(); 49 + ctx->dctx_mem = vzalloc(sz); 50 + if (!ctx->dctx_mem) 51 + goto error; 52 + 53 + ctx->dctx = zstd_init_dctx(ctx->dctx_mem, sz); 54 + if (!ctx->dctx) 55 + goto error; 56 + 57 + return ctx; 58 + 59 + error: 60 + zstd_destroy(ctx); 61 + return NULL; 62 + } 63 + 64 + static int zstd_compress(void *ctx, const unsigned char *src, size_t src_len, 65 + unsigned char *dst, size_t *dst_len) 66 + { 67 + struct zstd_ctx *zctx = ctx; 68 + const zstd_parameters params = zstd_get_params(zctx->level, 0); 69 + size_t ret; 70 + 71 + ret = zstd_compress_cctx(zctx->cctx, dst, *dst_len, 72 + src, src_len, &params); 73 + if (zstd_is_error(ret)) 74 + return -EINVAL; 75 + *dst_len = ret; 76 + return 0; 77 + } 78 + 79 + static int zstd_decompress(void *ctx, const unsigned char *src, size_t src_len, 80 + unsigned char *dst, size_t dst_len) 81 + { 82 + struct zstd_ctx *zctx = ctx; 83 + size_t ret; 84 + 85 + ret = zstd_decompress_dctx(zctx->dctx, dst, dst_len, src, src_len); 86 + if (zstd_is_error(ret)) 87 + return -EINVAL; 88 + return 0; 89 + } 90 + 91 + const struct zcomp_ops backend_zstd = { 92 + .compress = zstd_compress, 93 + .decompress = zstd_decompress, 94 + .create_ctx = zstd_create, 95 + .destroy_ctx = zstd_destroy, 96 + .name = "zstd", 97 + };
+10
drivers/block/zram/backend_zstd.h
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + 3 + #ifndef __BACKEND_ZSTD_H__ 4 + #define __BACKEND_ZSTD_H__ 5 + 6 + #include "zcomp.h" 7 + 8 + extern const struct zcomp_ops backend_zstd; 9 + 10 + #endif /* __BACKEND_ZSTD_H__ */
+4
drivers/block/zram/zcomp.c
··· 16 16 #include "backend_lzorle.h" 17 17 #include "backend_lz4.h" 18 18 #include "backend_lz4hc.h" 19 + #include "backend_zstd.h" 19 20 20 21 static const struct zcomp_ops *backends[] = { 21 22 #if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZO) ··· 28 27 #endif 29 28 #if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZ4HC) 30 29 &backend_lz4hc, 30 + #endif 31 + #if IS_ENABLED(CONFIG_ZRAM_BACKEND_ZSTD) 32 + &backend_zstd, 31 33 #endif 32 34 NULL 33 35 };