Skip to content

Commit 7418f23

Browse files
sergey-senozhatskyNick Terrell
authored and
Nick Terrell
committed
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/[email protected] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Sergey Senozhatsky <[email protected]> Cc: Nick Terrell <[email protected]> Cc: Minchan Kim <[email protected]> Cc: Sergey Senozhatsky <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent f8745da commit 7418f23

File tree

3 files changed

+251
-0
lines changed

3 files changed

+251
-0
lines changed

contrib/linux-kernel/linux_zstd.h

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,30 @@ int zstd_min_clevel(void);
7777
*/
7878
int zstd_max_clevel(void);
7979

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+
80104
/* ====== Parameter Selection ====== */
81105

82106
/**
@@ -136,6 +160,18 @@ typedef ZSTD_parameters zstd_parameters;
136160
zstd_parameters zstd_get_params(int level,
137161
unsigned long long estimated_src_size);
138162

163+
/**
164+
* zstd_get_cparams() - returns zstd_compression_parameters for selected level
165+
* @level: The compression level
166+
* @estimated_src_size: The estimated source size to compress or 0
167+
* if unknown.
168+
* @dict_size: Dictionary size.
169+
*
170+
* Return: The selected zstd_compression_parameters.
171+
*/
172+
zstd_compression_parameters zstd_get_cparams(int level,
173+
unsigned long long estimated_src_size, size_t dict_size);
174+
139175
typedef ZSTD_CCtx zstd_cctx;
140176
typedef ZSTD_cParameter zstd_cparameter;
141177

@@ -205,6 +241,71 @@ zstd_cctx *zstd_init_cctx(void *workspace, size_t workspace_size);
205241
size_t zstd_compress_cctx(zstd_cctx *cctx, void *dst, size_t dst_capacity,
206242
const void *src, size_t src_size, const zstd_parameters *parameters);
207243

244+
/**
245+
* zstd_create_cctx_advanced() - Create compression context
246+
* @custom_mem: Custom allocator.
247+
*
248+
* Return: NULL on error, pointer to compression context otherwise.
249+
*/
250+
zstd_cctx *zstd_create_cctx_advanced(zstd_custom_mem custom_mem);
251+
252+
/**
253+
* zstd_free_cctx() - Free compression context
254+
* @cdict: Pointer to compression context.
255+
*
256+
* Return: Always 0.
257+
*/
258+
size_t zstd_free_cctx(zstd_cctx* cctx);
259+
260+
/**
261+
* struct zstd_cdict - Compression dictionary.
262+
* See zstd_lib.h.
263+
*/
264+
typedef ZSTD_CDict zstd_cdict;
265+
266+
/**
267+
* zstd_create_cdict_byreference() - Create compression dictionary
268+
* @dict: Pointer to dictionary buffer.
269+
* @dict_size: Size of the dictionary buffer.
270+
* @dict_load_method: Dictionary load method.
271+
* @dict_content_type: Dictionary content type.
272+
* @custom_mem: Memory allocator.
273+
*
274+
* Note, this uses @dict by reference (ZSTD_dlm_byRef), so it should be
275+
* free before zstd_cdict is destroyed.
276+
*
277+
* Return: NULL on error, pointer to compression dictionary
278+
* otherwise.
279+
*/
280+
zstd_cdict *zstd_create_cdict_byreference(const void *dict, size_t dict_size,
281+
zstd_compression_parameters cparams,
282+
zstd_custom_mem custom_mem);
283+
284+
/**
285+
* zstd_free_cdict() - Free compression dictionary
286+
* @cdict: Pointer to compression dictionary.
287+
*
288+
* Return: Always 0.
289+
*/
290+
size_t zstd_free_cdict(zstd_cdict* cdict);
291+
292+
/**
293+
* zstd_compress_using_cdict() - compress src into dst using a dictionary
294+
* @cctx: The context. Must have been initialized with zstd_init_cctx().
295+
* @dst: The buffer to compress src into.
296+
* @dst_capacity: The size of the destination buffer. May be any size, but
297+
* ZSTD_compressBound(srcSize) is guaranteed to be large enough.
298+
* @src: The data to compress.
299+
* @src_size: The size of the data to compress.
300+
* @cdict: The dictionary to be used.
301+
*
302+
* Return: The compressed size or an error, which can be checked using
303+
* zstd_is_error().
304+
*/
305+
size_t zstd_compress_using_cdict(zstd_cctx *cctx, void *dst,
306+
size_t dst_capacity, const void *src, size_t src_size,
307+
const zstd_cdict *cdict);
308+
208309
/* ====== Single-pass Decompression ====== */
209310

210311
typedef ZSTD_DCtx zstd_dctx;
@@ -245,6 +346,71 @@ zstd_dctx *zstd_init_dctx(void *workspace, size_t workspace_size);
245346
size_t zstd_decompress_dctx(zstd_dctx *dctx, void *dst, size_t dst_capacity,
246347
const void *src, size_t src_size);
247348

349+
/**
350+
* struct zstd_ddict - Decompression dictionary.
351+
* See zstd_lib.h.
352+
*/
353+
typedef ZSTD_DDict zstd_ddict;
354+
355+
/**
356+
* zstd_create_ddict_byreference() - Create decompression dictionary
357+
* @dict: Pointer to dictionary buffer.
358+
* @dict_size: Size of the dictionary buffer.
359+
* @dict_load_method: Dictionary load method.
360+
* @dict_content_type: Dictionary content type.
361+
* @custom_mem: Memory allocator.
362+
*
363+
* Note, this uses @dict by reference (ZSTD_dlm_byRef), so it should be
364+
* free before zstd_ddict is destroyed.
365+
*
366+
* Return: NULL on error, pointer to decompression dictionary
367+
* otherwise.
368+
*/
369+
zstd_ddict *zstd_create_ddict_byreference(const void *dict, size_t dict_size,
370+
zstd_custom_mem custom_mem);
371+
/**
372+
* zstd_free_ddict() - Free decompression dictionary
373+
* @dict: Pointer to the dictionary.
374+
*
375+
* Return: Always 0.
376+
*/
377+
size_t zstd_free_ddict(zstd_ddict *ddict);
378+
379+
/**
380+
* zstd_create_dctx_advanced() - Create decompression context
381+
* @custom_mem: Custom allocator.
382+
*
383+
* Return: NULL on error, pointer to decompression context otherwise.
384+
*/
385+
zstd_dctx *zstd_create_dctx_advanced(zstd_custom_mem custom_mem);
386+
387+
/**
388+
* zstd_free_dctx() -- Free decompression context
389+
* @dctx: Pointer to decompression context.
390+
* Return: Always 0.
391+
*/
392+
size_t zstd_free_dctx(zstd_dctx *dctx);
393+
394+
/**
395+
* zstd_decompress_using_ddict() - decompress src into dst using a dictionary
396+
* @dctx: The decompression context.
397+
* @dst: The buffer to decompress src into.
398+
* @dst_capacity: The size of the destination buffer. Must be at least as large
399+
* as the decompressed size. If the caller cannot upper bound the
400+
* decompressed size, then it's better to use the streaming API.
401+
* @src: The zstd compressed data to decompress. Multiple concatenated
402+
* frames and skippable frames are allowed.
403+
* @src_size: The exact size of the data to decompress.
404+
* @ddict: The dictionary to be used.
405+
*
406+
* Return: The decompressed size or an error, which can be checked using
407+
* zstd_is_error().
408+
*/
409+
size_t zstd_decompress_using_ddict(zstd_dctx *dctx,
410+
void *dst, size_t dst_capacity, const void *src, size_t src_size,
411+
const zstd_ddict *ddict);
412+
413+
248414
/* ====== Streaming Buffers ====== */
249415

250416
/**

contrib/linux-kernel/zstd_compress_module.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ int zstd_max_clevel(void)
6767
}
6868
EXPORT_SYMBOL(zstd_max_clevel);
6969

70+
int zstd_default_clevel(void)
71+
{
72+
return ZSTD_defaultCLevel();
73+
}
74+
EXPORT_SYMBOL(zstd_default_clevel);
75+
7076
size_t zstd_compress_bound(size_t src_size)
7177
{
7278
return ZSTD_compressBound(src_size);
@@ -80,6 +86,13 @@ zstd_parameters zstd_get_params(int level,
8086
}
8187
EXPORT_SYMBOL(zstd_get_params);
8288

89+
zstd_compression_parameters zstd_get_cparams(int level,
90+
unsigned long long estimated_src_size, size_t dict_size)
91+
{
92+
return ZSTD_getCParams(level, estimated_src_size, dict_size);
93+
}
94+
EXPORT_SYMBOL(zstd_get_cparams);
95+
8396
size_t zstd_cctx_set_param(zstd_cctx *cctx, ZSTD_cParameter param, int value)
8497
{
8598
return ZSTD_CCtx_setParameter(cctx, param, value);
@@ -146,6 +159,33 @@ zstd_cctx *zstd_init_cctx(void *workspace, size_t workspace_size)
146159
}
147160
EXPORT_SYMBOL(zstd_init_cctx);
148161

162+
zstd_cctx *zstd_create_cctx_advanced(zstd_custom_mem custom_mem)
163+
{
164+
return ZSTD_createCCtx_advanced(custom_mem);
165+
}
166+
EXPORT_SYMBOL(zstd_create_cctx_advanced);
167+
168+
size_t zstd_free_cctx(zstd_cctx *cctx)
169+
{
170+
return ZSTD_freeCCtx(cctx);
171+
}
172+
EXPORT_SYMBOL(zstd_free_cctx);
173+
174+
zstd_cdict *zstd_create_cdict_byreference(const void *dict, size_t dict_size,
175+
zstd_compression_parameters cparams,
176+
zstd_custom_mem custom_mem)
177+
{
178+
return ZSTD_createCDict_advanced(dict, dict_size, ZSTD_dlm_byRef,
179+
ZSTD_dct_auto, cparams, custom_mem);
180+
}
181+
EXPORT_SYMBOL(zstd_create_cdict_byreference);
182+
183+
size_t zstd_free_cdict(zstd_cdict *cdict)
184+
{
185+
return ZSTD_freeCDict(cdict);
186+
}
187+
EXPORT_SYMBOL(zstd_free_cdict);
188+
149189
size_t zstd_compress_cctx(zstd_cctx *cctx, void *dst, size_t dst_capacity,
150190
const void *src, size_t src_size, const zstd_parameters *parameters)
151191
{
@@ -154,6 +194,15 @@ size_t zstd_compress_cctx(zstd_cctx *cctx, void *dst, size_t dst_capacity,
154194
}
155195
EXPORT_SYMBOL(zstd_compress_cctx);
156196

197+
size_t zstd_compress_using_cdict(zstd_cctx *cctx, void *dst,
198+
size_t dst_capacity, const void *src, size_t src_size,
199+
const ZSTD_CDict *cdict)
200+
{
201+
return ZSTD_compress_usingCDict(cctx, dst, dst_capacity,
202+
src, src_size, cdict);
203+
}
204+
EXPORT_SYMBOL(zstd_compress_using_cdict);
205+
157206
size_t zstd_cstream_workspace_bound(const zstd_compression_parameters *cparams)
158207
{
159208
return ZSTD_estimateCStreamSize_usingCParams(*cparams);

contrib/linux-kernel/zstd_decompress_module.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,33 @@ size_t zstd_dctx_workspace_bound(void)
4444
}
4545
EXPORT_SYMBOL(zstd_dctx_workspace_bound);
4646

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+
4774
zstd_dctx *zstd_init_dctx(void *workspace, size_t workspace_size)
4875
{
4976
if (workspace == NULL)
@@ -59,6 +86,15 @@ size_t zstd_decompress_dctx(zstd_dctx *dctx, void *dst, size_t dst_capacity,
5986
}
6087
EXPORT_SYMBOL(zstd_decompress_dctx);
6188

89+
size_t zstd_decompress_using_ddict(zstd_dctx *dctx,
90+
void *dst, size_t dst_capacity, const void* src, size_t src_size,
91+
const zstd_ddict* ddict)
92+
{
93+
return ZSTD_decompress_usingDDict(dctx, dst, dst_capacity, src,
94+
src_size, ddict);
95+
}
96+
EXPORT_SYMBOL(zstd_decompress_using_ddict);
97+
6298
size_t zstd_dstream_workspace_bound(size_t max_window_size)
6399
{
64100
return ZSTD_estimateDStreamSize(max_window_size);

0 commit comments

Comments
 (0)