zstd: support external sequence producer API
PATCH 1/1 zstd: support external sequence producer API
Cherry-picks support for using zstd's external sequence producer API in the kernel. This unblocks the use of QuickAssist hardware acceleration for zstd in applications such as BTRFS.
Some context: the kernel uses ZSTD_initStaticCCtx() to create compression contexts. This function builds the compression context in a user-provided buffer which must be sized according to the compression parameters. zstd provides size estimation functions for this purpose, but until now they were not compatible with the external sequence producer API.
This cherry-pick fixes that incompatibility. More specifically, it pulls in zstd upstream PRs #3839 and #3854. PR #3854 includes a unit test (upstream only) which validates that the external sequence producer API works correctly in conjunction with ZSTD_initStaticCCtx().
To build this commit, I first cherry-picked the relevant upstream commits onto the upstream v1.5.5-kernel tag:
cd ~/repos/zstd
git checkout tags/v1.5.5-kernel
git cherry-pick -m 1 126ec2669c927b24acd38ea903a211c1b5416588 git cherry-pick c6cabf94417d84ebb5da62e05d8b8a9623763585
I then ran "make import" to copy the changes into my fork of Linux:
cd ~/repos/zstd/contrib/linux-kernel/
make import
--- include/linux/zstd_lib.h | 23 +++++-- lib/zstd/compress/zstd_compress.c | 77 ++++++++++++---------- lib/zstd/compress/zstd_compress_internal.h | 26 ++++---- 3 files changed, 72 insertions(+), 54 deletions(-)
include/linux/zstd_lib.h +18 -5
@@ -1632,9 +1632,6 @@ ZSTDLIB_API unsigned ZSTD_isSkippableFrame(const void* buffer, size_t size);* * Note : only single-threaded compression is supported. * ZSTD_estimateCCtxSize_usingCCtxParams() will return an error code if ZSTD_c_nbWorkers is >= 1. -* -* Note 2 : ZSTD_estimateCCtxSize* functions are not compatible with the Block-Level Sequence Producer API at this time. -* Size estimates assume that no external sequence producer is registered. */ ZSTDLIB_STATIC_API size_t ZSTD_estimateCCtxSize(int compressionLevel); ZSTDLIB_STATIC_API size_t ZSTD_estimateCCtxSize_usingCParams(ZSTD_compressionParameters cParams);
@@ -2736,7 +2733,7 @@ ZSTDLIB_STATIC_API size_t ZSTD_resetDStream(ZSTD_DStream* zds);#define ZSTD_SEQUENCE_PRODUCER_ERROR ((size_t)(-1)) -typedef size_t ZSTD_sequenceProducer_F ( +typedef size_t (*ZSTD_sequenceProducer_F) ( void* sequenceProducerState, ZSTD_Sequence* outSeqs, size_t outSeqsCapacity, const void* src, size_t srcSize,
@@ -2768,7 +2765,23 @@ ZSTDLIB_STATIC_API voidZSTD_registerSequenceProducer ( ZSTD_CCtx* cctx, void* sequenceProducerState, - ZSTD_sequenceProducer_F* sequenceProducer + ZSTD_sequenceProducer_F sequenceProducer +); + +/*! ZSTD_CCtxParams_registerSequenceProducer() : +* Same as ZSTD_registerSequenceProducer(), but operates on ZSTD_CCtx_params. +* This is used for accurate size estimation with ZSTD_estimateCCtxSize_usingCCtxParams(), +* which is needed when creating a ZSTD_CCtx with ZSTD_initStaticCCtx(). +* +* If you are using the external sequence producer API in a scenario where ZSTD_initStaticCCtx() +* is required, then this function is for you. Otherwise, you probably don't need it. +* +* See tests/zstreamtest.c for example usage. */ +ZSTDLIB_STATIC_API void +ZSTD_CCtxParams_registerSequenceProducer ( +ZSTD_CCtx_params * params, +void * sequenceProducerState, +ZSTD_sequenceProducer_F sequenceProducer );
lib/zstd/compress/zstd_compress.c +42 -35
173 lines not on this page · raw message
lib/zstd/compress/zstd_compress_internal.h +12 -14
@@ -355,10 +355,11 @@ struct ZSTD_CCtx_params_s {* if the external matchfinder returns an error code. */ int enableMatchFinderFallback; -/* Indicates whether an external matchfinder has been referenced. -* Users can't set this externally. -* It is set internally in ZSTD_registerSequenceProducer(). */ -int useSequenceProducer; +/* Parameters for the external sequence producer API. +* Users set these parameters through ZSTD_registerSequenceProducer(). +* It is not possible to set these parameters individually through the public API. */ +void * extSeqProdState; +ZSTD_sequenceProducer_F extSeqProdFunc;/* Adjust the max block size*/ size_t maxBlockSize;
@@ -396,14 +397,6 @@ typedef struct {ZSTD_entropyCTablesMetadata_t entropyMetadata; } ZSTD_blockSplitCtx; -/* Context for block-level external matchfinder API */ -typedef struct { -void * mState; -ZSTD_sequenceProducer_F * mFinder; -ZSTD_Sequence * seqBuffer; -size_t seqBufferCapacity; -}ZSTD_externalMatchCtx ; -struct ZSTD_CCtx_s {ZSTD_compressionStage_e stage;int cParamsChanged;/* == 1 if cParams(except wlog) or compression level are changed in requestedParams. Triggers transmission of new params to ZSTDMT (if available) then reset to 0. */
@@ -468,8 +461,9 @@ struct ZSTD_CCtx_s {/* Workspace for block splitter */ ZSTD_blockSplitCtx blockSplitCtx; -/* Workspace for external matchfinder */ -ZSTD_externalMatchCtx externalMatchCtx; +/* Buffer for output from external sequence producer */ +ZSTD_Sequence * extSeqBuf; +size_t extSeqBufCapacity; };typedef enum { ZSTD_dtlm_fast, ZSTD_dtlm_full }ZSTD_dictTableLoadMethod_e ;
@@ -1495,6 +1489,10 @@ ZSTD_copySequencesToSeqStoreNoBlockDelim(ZSTD_CCtx* cctx, ZSTD_sequencePosition* const ZSTD_Sequence* const inSeqs, size_t inSeqsSize, const void* src, size_t blockSize, ZSTD_paramSwitch_e externalRepSearch); +/* Returns 1 if an external sequence producer is registered, otherwise returns 0. */ +MEM_STATIC int ZSTD_hasExtSeqProd(const ZSTD_CCtx_params* params) { +return params->extSeqProdFunc !=NULL ; +}/* =============================================================== * Deprecated definitions that are still used internally to avoid
Signature · 1 line
2.39.2 (Apple Git-143)