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

zram: add dictionary support to zstd backend

This adds support for pre-trained zstd dictionaries [1] Dictionary is
setup in params once (per-comp) and loaded to Cctx and Dctx by reference,
so we don't allocate extra memory.

TEST
====

*** zstd
/sys/block/zram0/mm_stat
1750654976 504565092 514203648 0 514203648 1 0 34204 34204

*** zstd dict=/etc/zstd-dict-amd64
/sys/block/zram0/mm_stat
1750638592 465851259 475373568 0 475373568 1 0 34185 34185

*** zstd level=8 dict=/etc/zstd-dict-amd64
/sys/block/zram0/mm_stat
1750642688 430765171 439955456 0 439955456 1 0 34185 34185

[1] https://github.com/facebook/zstd/blob/dev/programs/zstd.1.md#dictionary-builder

Link: https://lkml.kernel.org/r/20240902105656.1383858-23-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
6a559ecd 1e673c8c

+121 -23
+121 -23
drivers/block/zram/backend_zstd.c
··· 15 15 }; 16 16 17 17 struct zstd_params { 18 + zstd_custom_mem custom_mem; 19 + zstd_cdict *cdict; 20 + zstd_ddict *ddict; 18 21 zstd_parameters cprm; 19 22 }; 20 23 24 + /* 25 + * For C/D dictionaries we need to provide zstd with zstd_custom_mem, 26 + * which zstd uses internally to allocate/free memory when needed. 27 + * 28 + * This means that allocator.customAlloc() can be called from zcomp_compress() 29 + * under local-lock (per-CPU compression stream), in which case we must use 30 + * GFP_ATOMIC. 31 + * 32 + * Another complication here is that we can be configured as a swap device. 33 + */ 34 + static void *zstd_custom_alloc(void *opaque, size_t size) 35 + { 36 + if (!preemptible()) 37 + return kvzalloc(size, GFP_ATOMIC); 38 + 39 + return kvzalloc(size, __GFP_KSWAPD_RECLAIM | __GFP_NOWARN); 40 + } 41 + 42 + static void zstd_custom_free(void *opaque, void *address) 43 + { 44 + kvfree(address); 45 + } 46 + 21 47 static void zstd_release_params(struct zcomp_params *params) 22 48 { 23 - kfree(params->drv_data); 49 + struct zstd_params *zp = params->drv_data; 50 + 51 + params->drv_data = NULL; 52 + if (!zp) 53 + return; 54 + 55 + zstd_free_cdict(zp->cdict); 56 + zstd_free_ddict(zp->ddict); 57 + kfree(zp); 24 58 } 25 59 26 60 static int zstd_setup_params(struct zcomp_params *params) 27 61 { 62 + zstd_compression_parameters prm; 28 63 struct zstd_params *zp; 29 64 30 65 zp = kzalloc(sizeof(*zp), GFP_KERNEL); 31 66 if (!zp) 32 67 return -ENOMEM; 33 68 69 + params->drv_data = zp; 34 70 if (params->level == ZCOMP_PARAM_NO_LEVEL) 35 71 params->level = zstd_default_clevel(); 36 72 37 73 zp->cprm = zstd_get_params(params->level, PAGE_SIZE); 38 - params->drv_data = zp; 74 + 75 + zp->custom_mem.customAlloc = zstd_custom_alloc; 76 + zp->custom_mem.customFree = zstd_custom_free; 77 + 78 + prm = zstd_get_cparams(params->level, PAGE_SIZE, 79 + params->dict_sz); 80 + 81 + zp->cdict = zstd_create_cdict_byreference(params->dict, 82 + params->dict_sz, 83 + prm, 84 + zp->custom_mem); 85 + if (!zp->cdict) 86 + goto error; 87 + 88 + zp->ddict = zstd_create_ddict_byreference(params->dict, 89 + params->dict_sz, 90 + zp->custom_mem); 91 + if (!zp->ddict) 92 + goto error; 39 93 40 94 return 0; 95 + 96 + error: 97 + zstd_release_params(params); 98 + return -EINVAL; 41 99 } 42 100 43 101 static void zstd_destroy(struct zcomp_ctx *ctx) ··· 105 47 if (!zctx) 106 48 return; 107 49 108 - vfree(zctx->cctx_mem); 109 - vfree(zctx->dctx_mem); 50 + /* 51 + * If ->cctx_mem and ->dctx_mem were allocated then we didn't use 52 + * C/D dictionary and ->cctx / ->dctx were "embedded" into these 53 + * buffers. 54 + * 55 + * If otherwise then we need to explicitly release ->cctx / ->dctx. 56 + */ 57 + if (zctx->cctx_mem) 58 + vfree(zctx->cctx_mem); 59 + else 60 + zstd_free_cctx(zctx->cctx); 61 + 62 + if (zctx->dctx_mem) 63 + vfree(zctx->dctx_mem); 64 + else 65 + zstd_free_dctx(zctx->dctx); 66 + 110 67 kfree(zctx); 111 68 } 112 69 ··· 136 63 return -ENOMEM; 137 64 138 65 ctx->context = zctx; 139 - prm = zstd_get_params(params->level, PAGE_SIZE); 140 - sz = zstd_cctx_workspace_bound(&prm.cParams); 141 - zctx->cctx_mem = vzalloc(sz); 142 - if (!zctx->cctx_mem) 143 - goto error; 66 + if (params->dict_sz == 0) { 67 + prm = zstd_get_params(params->level, PAGE_SIZE); 68 + sz = zstd_cctx_workspace_bound(&prm.cParams); 69 + zctx->cctx_mem = vzalloc(sz); 70 + if (!zctx->cctx_mem) 71 + goto error; 144 72 145 - zctx->cctx = zstd_init_cctx(zctx->cctx_mem, sz); 146 - if (!zctx->cctx) 147 - goto error; 73 + zctx->cctx = zstd_init_cctx(zctx->cctx_mem, sz); 74 + if (!zctx->cctx) 75 + goto error; 148 76 149 - sz = zstd_dctx_workspace_bound(); 150 - zctx->dctx_mem = vzalloc(sz); 151 - if (!zctx->dctx_mem) 152 - goto error; 77 + sz = zstd_dctx_workspace_bound(); 78 + zctx->dctx_mem = vzalloc(sz); 79 + if (!zctx->dctx_mem) 80 + goto error; 153 81 154 - zctx->dctx = zstd_init_dctx(zctx->dctx_mem, sz); 155 - if (!zctx->dctx) 156 - goto error; 82 + zctx->dctx = zstd_init_dctx(zctx->dctx_mem, sz); 83 + if (!zctx->dctx) 84 + goto error; 85 + } else { 86 + struct zstd_params *zp = params->drv_data; 87 + 88 + zctx->cctx = zstd_create_cctx_advanced(zp->custom_mem); 89 + if (!zctx->cctx) 90 + goto error; 91 + 92 + zctx->dctx = zstd_create_dctx_advanced(zp->custom_mem); 93 + if (!zctx->dctx) 94 + goto error; 95 + } 157 96 158 97 return 0; 159 98 160 99 error: 100 + zstd_release_params(params); 161 101 zstd_destroy(ctx); 162 102 return -EINVAL; 163 103 } ··· 182 96 struct zstd_ctx *zctx = ctx->context; 183 97 size_t ret; 184 98 185 - ret = zstd_compress_cctx(zctx->cctx, req->dst, req->dst_len, 186 - req->src, req->src_len, &zp->cprm); 99 + if (params->dict_sz == 0) 100 + ret = zstd_compress_cctx(zctx->cctx, req->dst, req->dst_len, 101 + req->src, req->src_len, &zp->cprm); 102 + else 103 + ret = zstd_compress_using_cdict(zctx->cctx, req->dst, 104 + req->dst_len, req->src, 105 + req->src_len, 106 + zp->cdict); 187 107 if (zstd_is_error(ret)) 188 108 return -EINVAL; 189 109 req->dst_len = ret; ··· 199 107 static int zstd_decompress(struct zcomp_params *params, struct zcomp_ctx *ctx, 200 108 struct zcomp_req *req) 201 109 { 110 + struct zstd_params *zp = params->drv_data; 202 111 struct zstd_ctx *zctx = ctx->context; 203 112 size_t ret; 204 113 205 - ret = zstd_decompress_dctx(zctx->dctx, req->dst, req->dst_len, 206 - req->src, req->src_len); 114 + if (params->dict_sz == 0) 115 + ret = zstd_decompress_dctx(zctx->dctx, req->dst, req->dst_len, 116 + req->src, req->src_len); 117 + else 118 + ret = zstd_decompress_using_ddict(zctx->dctx, req->dst, 119 + req->dst_len, req->src, 120 + req->src_len, zp->ddict); 207 121 if (zstd_is_error(ret)) 208 122 return -EINVAL; 209 123 return 0;