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

lib: zstd: export API needed for dictionary support

Patch series "zram: introduce custom comp backends API", v7.

This series introduces support for run-time compression algorithms tuning,
so users, for instance, can adjust compression/acceleration levels and
provide pre-trained compression/decompression dictionaries which certain
algorithms support.

At this point we stop supporting (old/deprecated) comp API. We may add
new acomp API support in the future, but before that zram needs to undergo
some major rework (we are not ready for async compression).

Some benchmarks for reference (look at column #2)

*** init zstd
/sys/block/zram0/mm_stat
1750659072 504622188 514355200 0 514355200 1 0 34204 34204

*** init zstd dict=/home/ss/zstd-dict-amd64
/sys/block/zram0/mm_stat
1750650880 465908890 475398144 0 475398144 1 0 34185 34185

*** init zstd level=8 dict=/home/ss/zstd-dict-amd64
/sys/block/zram0/mm_stat
1750654976 430803319 439873536 0 439873536 1 0 34185 34185

*** init lz4
/sys/block/zram0/mm_stat
1750646784 664266564 677060608 0 677060608 1 0 34288 34288

*** init lz4 dict=/home/ss/lz4-dict-amd64
/sys/block/zram0/mm_stat
1750650880 619990300 632102912 0 632102912 1 0 34278 34278

*** init lz4hc
/sys/block/zram0/mm_stat
1750630400 609023822 621232128 0 621232128 1 0 34288 34288

*** init lz4hc dict=/home/ss/lz4-dict-amd64
/sys/block/zram0/mm_stat
1750659072 505133172 515231744 0 515231744 1 0 34278 34278


Recompress
init zram zstd (prio=0), zstd level=5 (prio 1), zstd with dict (prio 2)

*** zstd
/sys/block/zram0/mm_stat
1750982656 504630584 514269184 0 514269184 1 0 34204 34204

*** idle recompress priority=1 (zstd level=5)
/sys/block/zram0/mm_stat
1750982656 488645601 525438976 0 514269184 1 0 34204 34204

*** idle recompress priority=2 (zstd dict)
/sys/block/zram0/mm_stat
1750982656 460869640 517914624 0 514269184 1 0 34185 34204


This patch (of 24):

We need to export a number of API functions that enable advanced zstd
usage - C/D dictionaries, dictionaries sharing between contexts, etc.

Link: https://lkml.kernel.org/r/20240902105656.1383858-1-senozhatsky@chromium.org
Link: https://lkml.kernel.org/r/20240902105656.1383858-2-senozhatsky@chromium.org
Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
Cc: Nick Terrell <terrelln@fb.com>
Cc: Minchan Kim <minchan@kernel.org>
Cc: Sergey Senozhatsky <senozhatsky@chromium.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Sergey Senozhatsky and committed by
Andrew Morton
4fc41879 6050df6d

+252
+167
include/linux/zstd.h
··· 77 77 */ 78 78 int zstd_max_clevel(void); 79 79 80 + /** 81 + * zstd_default_clevel() - default compression level 82 + * 83 + * Return: Default compression level. 84 + */ 85 + int zstd_default_clevel(void); 86 + 87 + /** 88 + * struct zstd_custom_mem - custom memory allocation 89 + */ 90 + typedef ZSTD_customMem zstd_custom_mem; 91 + 92 + /** 93 + * struct zstd_dict_load_method - Dictionary load method. 94 + * See zstd_lib.h. 95 + */ 96 + typedef ZSTD_dictLoadMethod_e zstd_dict_load_method; 97 + 98 + /** 99 + * struct zstd_dict_content_type - Dictionary context type. 100 + * See zstd_lib.h. 101 + */ 102 + typedef ZSTD_dictContentType_e zstd_dict_content_type; 103 + 80 104 /* ====== Parameter Selection ====== */ 81 105 82 106 /** ··· 160 136 zstd_parameters zstd_get_params(int level, 161 137 unsigned long long estimated_src_size); 162 138 139 + 140 + /** 141 + * zstd_get_cparams() - returns zstd_compression_parameters for selected level 142 + * @level: The compression level 143 + * @estimated_src_size: The estimated source size to compress or 0 144 + * if unknown. 145 + * @dict_size: Dictionary size. 146 + * 147 + * Return: The selected zstd_compression_parameters. 148 + */ 149 + zstd_compression_parameters zstd_get_cparams(int level, 150 + unsigned long long estimated_src_size, size_t dict_size); 151 + 163 152 /* ====== Single-pass Compression ====== */ 164 153 165 154 typedef ZSTD_CCtx zstd_cctx; ··· 217 180 size_t zstd_compress_cctx(zstd_cctx *cctx, void *dst, size_t dst_capacity, 218 181 const void *src, size_t src_size, const zstd_parameters *parameters); 219 182 183 + /** 184 + * zstd_create_cctx_advanced() - Create compression context 185 + * @custom_mem: Custom allocator. 186 + * 187 + * Return: NULL on error, pointer to compression context otherwise. 188 + */ 189 + zstd_cctx *zstd_create_cctx_advanced(zstd_custom_mem custom_mem); 190 + 191 + /** 192 + * zstd_free_cctx() - Free compression context 193 + * @cdict: Pointer to compression context. 194 + * 195 + * Return: Always 0. 196 + */ 197 + size_t zstd_free_cctx(zstd_cctx* cctx); 198 + 199 + /** 200 + * struct zstd_cdict - Compression dictionary. 201 + * See zstd_lib.h. 202 + */ 203 + typedef ZSTD_CDict zstd_cdict; 204 + 205 + /** 206 + * zstd_create_cdict_byreference() - Create compression dictionary 207 + * @dict: Pointer to dictionary buffer. 208 + * @dict_size: Size of the dictionary buffer. 209 + * @dict_load_method: Dictionary load method. 210 + * @dict_content_type: Dictionary content type. 211 + * @custom_mem: Memory allocator. 212 + * 213 + * Note, this uses @dict by reference (ZSTD_dlm_byRef), so it should be 214 + * free before zstd_cdict is destroyed. 215 + * 216 + * Return: NULL on error, pointer to compression dictionary 217 + * otherwise. 218 + */ 219 + zstd_cdict *zstd_create_cdict_byreference(const void *dict, size_t dict_size, 220 + zstd_compression_parameters cparams, 221 + zstd_custom_mem custom_mem); 222 + 223 + /** 224 + * zstd_free_cdict() - Free compression dictionary 225 + * @cdict: Pointer to compression dictionary. 226 + * 227 + * Return: Always 0. 228 + */ 229 + size_t zstd_free_cdict(zstd_cdict* cdict); 230 + 231 + /** 232 + * zstd_compress_using_cdict() - compress src into dst using a dictionary 233 + * @cctx: The context. Must have been initialized with zstd_init_cctx(). 234 + * @dst: The buffer to compress src into. 235 + * @dst_capacity: The size of the destination buffer. May be any size, but 236 + * ZSTD_compressBound(srcSize) is guaranteed to be large enough. 237 + * @src: The data to compress. 238 + * @src_size: The size of the data to compress. 239 + * @cdict: The dictionary to be used. 240 + * 241 + * Return: The compressed size or an error, which can be checked using 242 + * zstd_is_error(). 243 + */ 244 + size_t zstd_compress_using_cdict(zstd_cctx *cctx, void *dst, 245 + size_t dst_capacity, const void *src, size_t src_size, 246 + const zstd_cdict *cdict); 247 + 220 248 /* ====== Single-pass Decompression ====== */ 221 249 222 250 typedef ZSTD_DCtx zstd_dctx; ··· 321 219 */ 322 220 size_t zstd_decompress_dctx(zstd_dctx *dctx, void *dst, size_t dst_capacity, 323 221 const void *src, size_t src_size); 222 + 223 + /** 224 + * struct zstd_ddict - Decompression dictionary. 225 + * See zstd_lib.h. 226 + */ 227 + typedef ZSTD_DDict zstd_ddict; 228 + 229 + /** 230 + * zstd_create_ddict_byreference() - Create decompression dictionary 231 + * @dict: Pointer to dictionary buffer. 232 + * @dict_size: Size of the dictionary buffer. 233 + * @dict_load_method: Dictionary load method. 234 + * @dict_content_type: Dictionary content type. 235 + * @custom_mem: Memory allocator. 236 + * 237 + * Note, this uses @dict by reference (ZSTD_dlm_byRef), so it should be 238 + * free before zstd_ddict is destroyed. 239 + * 240 + * Return: NULL on error, pointer to decompression dictionary 241 + * otherwise. 242 + */ 243 + zstd_ddict *zstd_create_ddict_byreference(const void *dict, size_t dict_size, 244 + zstd_custom_mem custom_mem); 245 + /** 246 + * zstd_free_ddict() - Free decompression dictionary 247 + * @dict: Pointer to the dictionary. 248 + * 249 + * Return: Always 0. 250 + */ 251 + size_t zstd_free_ddict(zstd_ddict *ddict); 252 + 253 + /** 254 + * zstd_create_dctx_advanced() - Create decompression context 255 + * @custom_mem: Custom allocator. 256 + * 257 + * Return: NULL on error, pointer to decompression context otherwise. 258 + */ 259 + zstd_dctx *zstd_create_dctx_advanced(zstd_custom_mem custom_mem); 260 + 261 + /** 262 + * zstd_free_dctx() -- Free decompression context 263 + * @dctx: Pointer to decompression context. 264 + * Return: Always 0. 265 + */ 266 + size_t zstd_free_dctx(zstd_dctx *dctx); 267 + 268 + /** 269 + * zstd_decompress_using_ddict() - decompress src into dst using a dictionary 270 + * @dctx: The decompression context. 271 + * @dst: The buffer to decompress src into. 272 + * @dst_capacity: The size of the destination buffer. Must be at least as large 273 + * as the decompressed size. If the caller cannot upper bound the 274 + * decompressed size, then it's better to use the streaming API. 275 + * @src: The zstd compressed data to decompress. Multiple concatenated 276 + * frames and skippable frames are allowed. 277 + * @src_size: The exact size of the data to decompress. 278 + * @ddict: The dictionary to be used. 279 + * 280 + * Return: The decompressed size or an error, which can be checked using 281 + * zstd_is_error(). 282 + */ 283 + size_t zstd_decompress_using_ddict(zstd_dctx *dctx, 284 + void *dst, size_t dst_capacity, const void *src, size_t src_size, 285 + const zstd_ddict *ddict); 286 + 324 287 325 288 /* ====== Streaming Buffers ====== */ 326 289
+49
lib/zstd/zstd_compress_module.c
··· 66 66 } 67 67 EXPORT_SYMBOL(zstd_max_clevel); 68 68 69 + int zstd_default_clevel(void) 70 + { 71 + return ZSTD_defaultCLevel(); 72 + } 73 + EXPORT_SYMBOL(zstd_default_clevel); 74 + 69 75 size_t zstd_compress_bound(size_t src_size) 70 76 { 71 77 return ZSTD_compressBound(src_size); ··· 84 78 return ZSTD_getParams(level, estimated_src_size, 0); 85 79 } 86 80 EXPORT_SYMBOL(zstd_get_params); 81 + 82 + zstd_compression_parameters zstd_get_cparams(int level, 83 + unsigned long long estimated_src_size, size_t dict_size) 84 + { 85 + return ZSTD_getCParams(level, estimated_src_size, dict_size); 86 + } 87 + EXPORT_SYMBOL(zstd_get_cparams); 87 88 88 89 size_t zstd_cctx_workspace_bound(const zstd_compression_parameters *cparams) 89 90 { ··· 106 93 } 107 94 EXPORT_SYMBOL(zstd_init_cctx); 108 95 96 + zstd_cctx *zstd_create_cctx_advanced(zstd_custom_mem custom_mem) 97 + { 98 + return ZSTD_createCCtx_advanced(custom_mem); 99 + } 100 + EXPORT_SYMBOL(zstd_create_cctx_advanced); 101 + 102 + size_t zstd_free_cctx(zstd_cctx *cctx) 103 + { 104 + return ZSTD_freeCCtx(cctx); 105 + } 106 + EXPORT_SYMBOL(zstd_free_cctx); 107 + 108 + zstd_cdict *zstd_create_cdict_byreference(const void *dict, size_t dict_size, 109 + zstd_compression_parameters cparams, 110 + zstd_custom_mem custom_mem) 111 + { 112 + return ZSTD_createCDict_advanced(dict, dict_size, ZSTD_dlm_byRef, 113 + ZSTD_dct_auto, cparams, custom_mem); 114 + } 115 + EXPORT_SYMBOL(zstd_create_cdict_byreference); 116 + 117 + size_t zstd_free_cdict(zstd_cdict *cdict) 118 + { 119 + return ZSTD_freeCDict(cdict); 120 + } 121 + EXPORT_SYMBOL(zstd_free_cdict); 122 + 109 123 size_t zstd_compress_cctx(zstd_cctx *cctx, void *dst, size_t dst_capacity, 110 124 const void *src, size_t src_size, const zstd_parameters *parameters) 111 125 { ··· 140 100 return ZSTD_compress2(cctx, dst, dst_capacity, src, src_size); 141 101 } 142 102 EXPORT_SYMBOL(zstd_compress_cctx); 103 + 104 + size_t zstd_compress_using_cdict(zstd_cctx *cctx, void *dst, 105 + size_t dst_capacity, const void *src, size_t src_size, 106 + const ZSTD_CDict *cdict) 107 + { 108 + return ZSTD_compress_usingCDict(cctx, dst, dst_capacity, 109 + src, src_size, cdict); 110 + } 111 + EXPORT_SYMBOL(zstd_compress_using_cdict); 143 112 144 113 size_t zstd_cstream_workspace_bound(const zstd_compression_parameters *cparams) 145 114 {
+36
lib/zstd/zstd_decompress_module.c
··· 44 44 } 45 45 EXPORT_SYMBOL(zstd_dctx_workspace_bound); 46 46 47 + zstd_dctx *zstd_create_dctx_advanced(zstd_custom_mem custom_mem) 48 + { 49 + return ZSTD_createDCtx_advanced(custom_mem); 50 + } 51 + EXPORT_SYMBOL(zstd_create_dctx_advanced); 52 + 53 + size_t zstd_free_dctx(zstd_dctx *dctx) 54 + { 55 + return ZSTD_freeDCtx(dctx); 56 + } 57 + EXPORT_SYMBOL(zstd_free_dctx); 58 + 59 + zstd_ddict *zstd_create_ddict_byreference(const void *dict, size_t dict_size, 60 + zstd_custom_mem custom_mem) 61 + { 62 + return ZSTD_createDDict_advanced(dict, dict_size, ZSTD_dlm_byRef, 63 + ZSTD_dct_auto, custom_mem); 64 + 65 + } 66 + EXPORT_SYMBOL(zstd_create_ddict_byreference); 67 + 68 + size_t zstd_free_ddict(zstd_ddict *ddict) 69 + { 70 + return ZSTD_freeDDict(ddict); 71 + } 72 + EXPORT_SYMBOL(zstd_free_ddict); 73 + 47 74 zstd_dctx *zstd_init_dctx(void *workspace, size_t workspace_size) 48 75 { 49 76 if (workspace == NULL) ··· 85 58 return ZSTD_decompressDCtx(dctx, dst, dst_capacity, src, src_size); 86 59 } 87 60 EXPORT_SYMBOL(zstd_decompress_dctx); 61 + 62 + size_t zstd_decompress_using_ddict(zstd_dctx *dctx, 63 + void *dst, size_t dst_capacity, const void* src, size_t src_size, 64 + const zstd_ddict* ddict) 65 + { 66 + return ZSTD_decompress_usingDDict(dctx, dst, dst_capacity, src, 67 + src_size, ddict); 68 + } 69 + EXPORT_SYMBOL(zstd_decompress_using_ddict); 88 70 89 71 size_t zstd_dstream_workspace_bound(size_t max_window_size) 90 72 {