Update LZ4 compressor module
Subsystems (5)
cover Update LZ4 compressor module
This patchset is for updating the LZ4 compression module to a version based on LZ4 v1.7.2 allowing to use the fast compression algorithm aka LZ4 fast which provides an "acceleration" parameter as a tradeoff between compression ratio and compression speed.
We will use LZ4 fast in order to support compression in lustre. LZ4 fast empowers us to do client-side as well as server-side compression/decompression while being able to provide appropriate parameters to enable users to tune lustre's behaviour to obtain the best performance/compression/etc. on their behalf (adapative compression).
Also, it will be useful for other users of LZ4 compression,
as with LZ4 fast it is possible to enable applications to use fast and/or high compression depending of the usecase. E.g. a developer can use very high compression (low acceleration) for sending data over a network with limited rate of transmission or he trades the compression ratio for higher compression speed.
LZ4 homepage: http://www.lz4.org/
LZ4 source repository: https://github.com/lz4/lz4
Source version: 1.7.2
Compression in lustre: http://wiki.lustre.org/Enhanced_Adaptive_Compression_in_Lustre
Benchmark (taken from [1], Core i5-4300U @1.9GHz):
----------------|--------------|----------------|---------- Compressor | Compression | Decompression | Ratio ----------------|--------------|----------------|---------- memcpy | 4200 MB/s | 4200 MB/s | 1.000 LZ4 fast 50 | 1080 MB/s | 2650 MB/s | 1.375 LZ4 fast 17 | 680 MB/s | 2220 MB/s | 1.607 LZ4 fast 5 | 475 MB/s | 1920 MB/s | 1.886 LZ4 default | 385 MB/s | 1850 MB/s | 2.101
[1] http://fastcompression.blogspot.de/2015/04/sampling-or-faster-lz4.html
[PATCH 1/3] crypto: Change lz4 modules to work with new lz4 [PATCH 2/3] fs/pstore: fs/squashfs: Change lz4 compressor functions [PATCH 3/3] lib: Update LZ4 compressor module based on LZ4 v1.7.2.
PATCH 1/3 crypto: Change lz4 modules to work with new lz4 compressor module version
This patch updates the crypto modules using LZ4 compression to work with the new LZ4 kernel module version.
--- crypto/lz4.c | 93 +++++++-------------------------------------------------- crypto/lz4hc.c | 94 +++++++--------------------------------------------------- 2 files changed, 22 insertions(+), 165 deletions(-)
crypto/lz4.c +11 -82
@@ -23,53 +23,36 @@#include <linux/crypto.h> #include <linux/vmalloc.h> #include <linux/lz4.h> -#include <crypto/internal/scompress.h> struct lz4_ctx {void *lz4_comp_mem; }; -static void *lz4_alloc_ctx (struct crypto_scomp *tfm) -{ -void *ctx; - - ctx =vmalloc (LZ4_MEM_COMPRESS ); -if (!ctx) -return ERR_PTR (-ENOMEM ); - -return ctx; -} -static int lz4_init (struct crypto_tfm *tfm) {struct lz4_ctx *ctx =crypto_tfm_ctx (tfm); - ctx->lz4_comp_mem =lz4_alloc_ctx (NULL ); -if (IS_ERR (ctx->lz4_comp_mem)) + ctx->lz4_comp_mem =vmalloc (LZ4_MEM_COMPRESS ); +if (!ctx->lz4_comp_mem)return -ENOMEM ;return 0 ; } -static void lz4_free_ctx (struct crypto_scomp *tfm,void *ctx) -{ -vfree (ctx); -} -static void lz4_exit (struct crypto_tfm *tfm) {struct lz4_ctx *ctx =crypto_tfm_ctx (tfm); - -lz4_free_ctx (NULL , ctx->lz4_comp_mem); +vfree (ctx->lz4_comp_mem); } -static int __lz4_compress_crypto (const u8 *src,unsigned int slen, -u8 *dst,unsigned int *dlen,void *ctx) +static int lz4_compress_crypto (struct crypto_tfm *tfm,const u8 *src, +unsigned int slen,u8 *dst,unsigned int *dlen) { +struct lz4_ctx *ctx =crypto_tfm_ctx (tfm);size_t tmp_len = *dlen;int err; - err =lz4_compress (src, slen, dst, &tmp_len, ctx); + err =LZ4_compress_default (src, dst, slen, tmp_len, ctx->lz4_comp_mem);if (err <0 )return -EINVAL ;
@@ -78,29 +61,14 @@ static int __lz4_compress_crypto(const u8 *src, unsigned int slen,return 0 ; } -static int lz4_scompress (struct crypto_scomp *tfm,const u8 *src, -unsigned int slen,u8 *dst,unsigned int *dlen, -void *ctx) -{ -return __lz4_compress_crypto (src, slen, dst, dlen, ctx); -} - -static int lz4_compress_crypto (struct crypto_tfm *tfm,const u8 *src, -unsigned int slen,u8 *dst,unsigned int *dlen) -{ -struct lz4_ctx *ctx =crypto_tfm_ctx (tfm); - -return __lz4_compress_crypto (src, slen, dst, dlen, ctx->lz4_comp_mem); -} - -static int __lz4_decompress_crypto (const u8 *src,unsigned int slen, -u8 *dst,unsigned int *dlen,void *ctx) +static int lz4_decompress_crypto (struct crypto_tfm *tfm,const u8 *src, +unsigned int slen,u8 *dst,unsigned int *dlen) {int err;size_t tmp_len = *dlen;size_t __slen = slen; - err =lz4_decompress_unknownoutputsize (src, __slen, dst, &tmp_len); + err =LZ4_decompress_safe (src, dst, __slen, tmp_len);if (err <0 )return -EINVAL ;
@@ -108,20 +76,6 @@ static int __lz4_decompress_crypto(const u8 *src, unsigned int slen,return err; } -static int lz4_sdecompress (struct crypto_scomp *tfm,const u8 *src, -unsigned int slen,u8 *dst,unsigned int *dlen, -void *ctx) -{ -return __lz4_decompress_crypto (src, slen, dst, dlen,NULL ); -} - -static int lz4_decompress_crypto (struct crypto_tfm *tfm,const u8 *src, -unsigned int slen,u8 *dst, -unsigned int *dlen) -{ -return __lz4_decompress_crypto (src, slen, dst, dlen,NULL ); -} -static struct crypto_alg alg_lz4 = { .cra_name ="lz4" , .cra_flags =CRYPTO_ALG_TYPE_COMPRESS ,
@@ -135,39 +89,14 @@ static struct crypto_alg alg_lz4 = { .coa_decompress = lz4_decompress_crypto } } }; -static struct scomp_alg scomp = { - .alloc_ctx = lz4_alloc_ctx, - .free_ctx = lz4_free_ctx, - .compress = lz4_scompress, - .decompress = lz4_sdecompress, - .base = { - .cra_name ="lz4" , - .cra_driver_name ="lz4-scomp" , - .cra_module =THIS_MODULE , - } -}; -static int __init lz4_mod_init (void ) { -int ret; - - ret =crypto_register_alg (&alg_lz4); -if (ret) -return ret; - - ret =crypto_register_scomp (&scomp); -if (ret) { -crypto_unregister_alg (&alg_lz4); -return ret; - } - -return ret; +return crypto_register_alg (&alg_lz4); } static void __exit lz4_mod_fini(void) {crypto_unregister_alg (&alg_lz4); -crypto_unregister_scomp (&scomp); }module_init (lz4_mod_init);
crypto/lz4hc.c +11 -83
154 lines not on this page · raw message
Signature · 1 line
2.1.4
kbuild test robot
build botRe: [PATCH 1/3] crypto: Change lz4 modules to work with new lz4 compressor module version
url: https://github.com/0day-ci/linux/commits/Sven-Schmidt/Update-LZ4-compressor-module/20161221-130109 config: i386-randconfig-x003-201651 (attached as .config) compiler: gcc-6 (Debian 6.2.0-3)…
Hi Sven,
[auto build test ERROR on linus/master]
[also build test ERROR on next-20161221]
[cannot apply to v4.9]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Sven-Schmidt/Update-LZ4-compressor-module/20161221-130109
config: i386-randconfig-x003-201651 (attached as .config)
compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901 reproduce:
# save the attached .config to linux build tree
make ARCH=i386 Note: the linux-review/Sven-Schmidt/Update-LZ4-compressor-module/20161221-130109 HEAD 08652114d6d8867d2e8b3545b5e9f8a2fe11a5d7 builds fine.
It only hurts bisectibility.
All errors (new ones prefixed by >>):
crypto/lz4.c: In function 'lz4_compress_crypto':
>> crypto/lz4.c:55:8: error: implicit declaration of function 'LZ4_compress_default' [-Werror=implicit-function-declaration]
err = LZ4_compress_default(src, dst, slen, tmp_len, ctx->lz4_comp_mem);
^~~~~~~~~~~~~~~~~~~~
crypto/lz4.c: In function 'lz4_decompress_crypto':
>> crypto/lz4.c:71:8: error: implicit declaration of function 'LZ4_decompress_safe' [-Werror=implicit-function-declaration]
err = LZ4_decompress_safe(src, dst, __slen, tmp_len);
^~~~~~~~~~~~~~~~~~~
cc1: some warnings being treated as errors
--
crypto/lz4hc.c: In function 'lz4hc_compress_crypto':
>> crypto/lz4hc.c:55:8: error: implicit declaration of function 'LZ4_compress_HC' [-Werror=implicit-function-declaration]
err = LZ4_compress_HC(src, dst, slen, tmp_len, LZ4HC_DEFAULT_CLEVEL, ctx->lz4hc_comp_mem);
^~~~~~~~~~~~~~~
>> crypto/lz4hc.c:55:49: error: 'LZ4HC_DEFAULT_CLEVEL' undeclared (first use in this function)
err = LZ4_compress_HC(src, dst, slen, tmp_len, LZ4HC_DEFAULT_CLEVEL, ctx->lz4hc_comp_mem);
^~~~~~~~~~~~~~~~~~~~
crypto/lz4hc.c:55:49: note: each undeclared identifier is reported only once for each function it appears in
crypto/lz4hc.c: In function 'lz4hc_decompress_crypto':
>> crypto/lz4hc.c:71:8: error: implicit declaration of function 'LZ4_decompress_safe' [-Werror=implicit-function-declaration]
err = LZ4_decompress_safe(src, dst, __slen, (int)tmp_len);
^~~~~~~~~~~~~~~~~~~cc1: some warnings being treated as errors
vim +/LZ4_compress_default +55 crypto/lz4.c
49 unsigned int slen, u8 *dst, unsigned int *dlen)
50 {
51 struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
52 size_t tmp_len = *dlen;
53 int err;
54
> 55 err = LZ4_compress_default(src, dst, slen, tmp_len, ctx->lz4_comp_mem);
56
57 if (err < 0)
58 return -EINVAL;
59
60 *dlen = tmp_len;
61 return 0;
62 }
63
64 static int lz4_decompress_crypto(struct crypto_tfm *tfm, const u8 *src,
65 unsigned int slen, u8 *dst, unsigned int *dlen)
66 {
67 int err;
68 size_t tmp_len = *dlen;
69 size_t __slen = slen;
70
> 71 err = LZ4_decompress_safe(src, dst, __slen, tmp_len);
72 if (err < 0)
73 return -EINVAL;
74 ---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Re: [PATCH 1/3] crypto: Change lz4 modules to work with new lz4 compressor module version
In reply to Sven SchmidtCc Herbert and linux-crypto on this
https://marc.info/?l=linux-kernel&m=148226084823926
Quoted 335 lines · Sven Schmidt
This patch updates the crypto modules using LZ4 compression to work with the new LZ4 kernel module version.
Signed-off-by Sven Schmidt <[email protected]>--- crypto/lz4.c | 93 +++++++-------------------------------------------------- crypto/lz4hc.c | 94 +++++++--------------------------------------------------- 2 files changed, 22 insertions(+), 165 deletions(-)crypto/lz4.c +11
-82@@ -23,53 +23,36 @@#include <linux/crypto.h> #include <linux/vmalloc.h> #include <linux/lz4.h> -#include <crypto/internal/scompress.h> struct lz4_ctx {void *lz4_comp_mem; }; -static void *lz4_alloc_ctx (struct crypto_scomp *tfm) -{ -void *ctx; - - ctx =vmalloc (LZ4_MEM_COMPRESS ); -if (!ctx) -return ERR_PTR (-ENOMEM ); - -return ctx; -} -static int lz4_init (struct crypto_tfm *tfm) {struct lz4_ctx *ctx =crypto_tfm_ctx (tfm); - ctx->lz4_comp_mem =lz4_alloc_ctx (NULL ); -if (IS_ERR (ctx->lz4_comp_mem)) + ctx->lz4_comp_mem =vmalloc (LZ4_MEM_COMPRESS ); +if (!ctx->lz4_comp_mem)return -ENOMEM ;return 0 ; } -static void lz4_free_ctx (struct crypto_scomp *tfm,void *ctx) -{ -vfree (ctx); -} -static void lz4_exit (struct crypto_tfm *tfm) {struct lz4_ctx *ctx =crypto_tfm_ctx (tfm); - -lz4_free_ctx (NULL , ctx->lz4_comp_mem); +vfree (ctx->lz4_comp_mem); } -static int __lz4_compress_crypto (const u8 *src,unsigned int slen, -u8 *dst,unsigned int *dlen,void *ctx) +static int lz4_compress_crypto (struct crypto_tfm *tfm,const u8 *src, +unsigned int slen,u8 *dst,unsigned int *dlen) { +struct lz4_ctx *ctx =crypto_tfm_ctx (tfm);size_t tmp_len = *dlen;int err; - err =lz4_compress (src, slen, dst, &tmp_len, ctx); + err =LZ4_compress_default (src, dst, slen, tmp_len, ctx->lz4_comp_mem);if (err <0 )return -EINVAL ;@@ -78,29 +61,14 @@ static int __lz4_compress_crypto(const u8 *src, unsigned int slen,return 0 ; } -static int lz4_scompress (struct crypto_scomp *tfm,const u8 *src, -unsigned int slen,u8 *dst,unsigned int *dlen, -void *ctx) -{ -return __lz4_compress_crypto (src, slen, dst, dlen, ctx); -} - -static int lz4_compress_crypto (struct crypto_tfm *tfm,const u8 *src, -unsigned int slen,u8 *dst,unsigned int *dlen) -{ -struct lz4_ctx *ctx =crypto_tfm_ctx (tfm); - -return __lz4_compress_crypto (src, slen, dst, dlen, ctx->lz4_comp_mem); -} - -static int __lz4_decompress_crypto (const u8 *src,unsigned int slen, -u8 *dst,unsigned int *dlen,void *ctx) +static int lz4_decompress_crypto (struct crypto_tfm *tfm,const u8 *src, +unsigned int slen,u8 *dst,unsigned int *dlen) {int err;size_t tmp_len = *dlen;size_t __slen = slen; - err =lz4_decompress_unknownoutputsize (src, __slen, dst, &tmp_len); + err =LZ4_decompress_safe (src, dst, __slen, tmp_len);if (err <0 )return -EINVAL ;@@ -108,20 +76,6 @@ static int __lz4_decompress_crypto(const u8 *src, unsigned int slen,return err; } -static int lz4_sdecompress (struct crypto_scomp *tfm,const u8 *src, -unsigned int slen,u8 *dst,unsigned int *dlen, -void *ctx) -{ -return __lz4_decompress_crypto (src, slen, dst, dlen,NULL ); -} - -static int lz4_decompress_crypto (struct crypto_tfm *tfm,const u8 *src, -unsigned int slen,u8 *dst, -unsigned int *dlen) -{ -return __lz4_decompress_crypto (src, slen, dst, dlen,NULL ); -} -static struct crypto_alg alg_lz4 = { .cra_name ="lz4" , .cra_flags =CRYPTO_ALG_TYPE_COMPRESS ,@@ -135,39 +89,14 @@ static struct crypto_alg alg_lz4 = { .coa_decompress = lz4_decompress_crypto } } }; -static struct scomp_alg scomp = { - .alloc_ctx = lz4_alloc_ctx, - .free_ctx = lz4_free_ctx, - .compress = lz4_scompress, - .decompress = lz4_sdecompress, - .base = { - .cra_name ="lz4" , - .cra_driver_name ="lz4-scomp" , - .cra_module =THIS_MODULE , - } -}; -static int __init lz4_mod_init (void ) { -int ret; - - ret =crypto_register_alg (&alg_lz4); -if (ret) -return ret; - - ret =crypto_register_scomp (&scomp); -if (ret) { -crypto_unregister_alg (&alg_lz4); -return ret; - } - -return ret; +return crypto_register_alg (&alg_lz4); } static void __exit lz4_mod_fini(void) {crypto_unregister_alg (&alg_lz4); -crypto_unregister_scomp (&scomp); }module_init (lz4_mod_init);crypto/lz4hc.c +11
-83@@ -22,53 +22,37 @@#include <linux/crypto.h> #include <linux/vmalloc.h> #include <linux/lz4.h> -#include <crypto/internal/scompress.h> struct lz4hc_ctx {void *lz4hc_comp_mem; }; -static void *lz4hc_alloc_ctx (struct crypto_scomp *tfm) -{ -void *ctx; - - ctx =vmalloc (LZ4HC_MEM_COMPRESS ); -if (!ctx) -return ERR_PTR (-ENOMEM ); - -return ctx; -} -static int lz4hc_init (struct crypto_tfm *tfm) {struct lz4hc_ctx *ctx =crypto_tfm_ctx (tfm); - ctx->lz4hc_comp_mem =lz4hc_alloc_ctx (NULL ); -if (IS_ERR (ctx->lz4hc_comp_mem)) + ctx->lz4hc_comp_mem =vmalloc (LZ4HC_MEM_COMPRESS ); +if (!ctx->lz4hc_comp_mem)return -ENOMEM ;return 0 ; } -static void lz4hc_free_ctx (struct crypto_scomp *tfm,void *ctx) -{ -vfree (ctx); -} -static void lz4hc_exit (struct crypto_tfm *tfm) {struct lz4hc_ctx *ctx =crypto_tfm_ctx (tfm); -lz4hc_free_ctx (NULL , ctx->lz4hc_comp_mem); +vfree (ctx->lz4hc_comp_mem); } -static int __lz4hc_compress_crypto (const u8 *src,unsigned int slen, -u8 *dst,unsigned int *dlen,void *ctx) +static int lz4hc_compress_crypto (struct crypto_tfm *tfm,const u8 *src, +unsigned int slen,u8 *dst,unsigned int *dlen) { +struct lz4hc_ctx *ctx =crypto_tfm_ctx (tfm);size_t tmp_len = *dlen;int err; - err =lz4hc_compress (src, slen, dst, &tmp_len, ctx); + err =LZ4_compress_HC (src, dst, slen, tmp_len,LZ4HC_DEFAULT_CLEVEL , ctx->lz4hc_comp_mem);if (err <0 )return -EINVAL ;@@ -77,31 +61,14 @@ static int __lz4hc_compress_crypto(const u8 *src, unsigned int slen,return 0 ; } -static int lz4hc_scompress (struct crypto_scomp *tfm,const u8 *src, -unsigned int slen,u8 *dst,unsigned int *dlen, -void *ctx) -{ -return __lz4hc_compress_crypto (src, slen, dst, dlen, ctx); -} - -static int lz4hc_compress_crypto (struct crypto_tfm *tfm,const u8 *src, -unsigned int slen,u8 *dst, -unsigned int *dlen) -{ -struct lz4hc_ctx *ctx =crypto_tfm_ctx (tfm); - -return __lz4hc_compress_crypto (src, slen, dst, dlen, - ctx->lz4hc_comp_mem); -} - -static int __lz4hc_decompress_crypto (const u8 *src,unsigned int slen, -u8 *dst,unsigned int *dlen,void *ctx) +static int lz4hc_decompress_crypto (struct crypto_tfm *tfm,const u8 *src, +unsigned int slen,u8 *dst,unsigned int *dlen) {int err;size_t tmp_len = *dlen;size_t __slen = slen; - err =lz4_decompress_unknownoutputsize (src, __slen, dst, &tmp_len); + err =LZ4_decompress_safe (src, dst, __slen, (int )tmp_len);if (err <0 )return -EINVAL ;@@ -109,20 +76,6 @@ static int __lz4hc_decompress_crypto(const u8 *src, unsigned int slen,return err; } -static int lz4hc_sdecompress (struct crypto_scomp *tfm,const u8 *src, -unsigned int slen,u8 *dst,unsigned int *dlen, -void *ctx) -{ -return __lz4hc_decompress_crypto (src, slen, dst, dlen,NULL ); -} - -static int lz4hc_decompress_crypto (struct crypto_tfm *tfm,const u8 *src, -unsigned int slen,u8 *dst, -unsigned int *dlen) -{ -return __lz4hc_decompress_crypto (src, slen, dst, dlen,NULL ); -} -static struct crypto_alg alg_lz4hc = { .cra_name ="lz4hc" , .cra_flags =CRYPTO_ALG_TYPE_COMPRESS ,@@ -136,39 +89,14 @@ static struct crypto_alg alg_lz4hc = { .coa_decompress = lz4hc_decompress_crypto } } }; -static struct scomp_alg scomp = { - .alloc_ctx = lz4hc_alloc_ctx, - .free_ctx = lz4hc_free_ctx, - .compress = lz4hc_scompress, - .decompress = lz4hc_sdecompress, - .base = { - .cra_name ="lz4hc" , - .cra_driver_name ="lz4hc-scomp" , - .cra_module =THIS_MODULE , - } -}; -static int __init lz4hc_mod_init (void ) { -int ret; - - ret =crypto_register_alg (&alg_lz4hc); -if (ret) -return ret; - - ret =crypto_register_scomp (&scomp); -if (ret) { -crypto_unregister_alg (&alg_lz4hc); -return ret; - } - -return ret; +return crypto_register_alg (&alg_lz4hc); } static void __exit lz4hc_mod_fini(void) {crypto_unregister_alg (&alg_lz4hc); -crypto_unregister_scomp (&scomp); }module_init (lz4hc_mod_init);Signature · 1 line
2.1.4
PATCH 2/3 fs/pstore: fs/squashfs: Change lz4 compressor functions to work with new version
In reply to Sven SchmidtThis patch updates the fs/pstore and fs/squashfs to work with the new LZ4 kernel module version.
--- fs/pstore/platform.c | 11 +++++------ fs/squashfs/lz4_wrapper.c | 3 +-- 2 files changed, 6 insertions(+), 8 deletions(-)
fs/pstore/platform.c +5 -6
@@ -342,7 +342,7 @@ static int compress_lz4(const void *in, void *out, size_t inlen, size_t outlen) {int ret; - ret =lz4_compress (in, inlen, out, &outlen, workspace); + ret =LZ4_compress_default (in, out, inlen, outlen, workspace);if (ret) {pr_err ("lz4_compress error, ret = %d!\n" , ret);return -EIO ;
@@ -355,7 +355,7 @@ static int decompress_lz4(void *in, void *out, size_t inlen, size_t outlen) {int ret; - ret =lz4_decompress_unknownoutputsize (in, inlen, out, &outlen); + ret =LZ4_decompress_safe (in, out, inlen, outlen);if (ret) {pr_err ("lz4_decompress error, ret = %d!\n" , ret);return -EIO ;
@@ -366,7 +366,7 @@ static int decompress_lz4(void *in, void *out, size_t inlen, size_t outlen)static void allocate_lz4 (void ) { - big_oops_buf_sz =lz4_compressbound (psinfo->bufsize); + big_oops_buf_sz =LZ4_compressBound (psinfo->bufsize); big_oops_buf =kmalloc (big_oops_buf_sz,GFP_KERNEL );if (big_oops_buf) { workspace =kmalloc (LZ4_MEM_COMPRESS ,GFP_KERNEL );
@@ -493,7 +493,6 @@ static void pstore_dump(struct kmsg_dumper *dumper,if (!is_locked) {pr_err ("pstore dump routine blocked in %s path, may corrupt error record\n" ,in_nmi () ?"NMI" : why); -return ; } }else {spin_lock_irqsave (&psinfo->buf_lock, flags);
@@ -585,8 +584,8 @@ static void pstore_console_write(struct console *con, const char *s, unsigned c) }else {spin_lock_irqsave (&psinfo->buf_lock, flags); } - psinfo->write_buf (PSTORE_TYPE_CONSOLE ,0 , &id,0 , - s,0 , c, psinfo); +memcpy (psinfo->buf, s, c); + psinfo->write (PSTORE_TYPE_CONSOLE ,0 , &id,0 ,0 ,0 , c, psinfo);spin_unlock_irqrestore (&psinfo->buf_lock, flags); s += c; c = e - s;
fs/squashfs/lz4_wrapper.c +1 -2
@@ -108,8 +108,7 @@ static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm,put_bh (bh[i]); } - res =lz4_decompress_unknownoutputsize (stream->input, length, - stream->output, &dest_len); + res =LZ4_decompress_safe (stream->input, stream->output, length, (int )dest_len);if (res)return -EIO ;
Signature · 1 line
2.1.4
kbuild test robot
build botRe: [PATCH 2/3] fs/pstore: fs/squashfs: Change lz4 compressor functions to work with new version
url: https://github.com/0day-ci/linux/commits/Sven-Schmidt/Update-LZ4-compressor-module/20161221-130109 config: x86_64-randconfig-x004-201651 (attached as .config) compiler: gcc-6 (Debian 6.2.0-3)…
Hi Sven,
[auto build test ERROR on linus/master]
[also build test ERROR on next-20161221]
[cannot apply to v4.9]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Sven-Schmidt/Update-LZ4-compressor-module/20161221-130109
config: x86_64-randconfig-x004-201651 (attached as .config)
compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901 reproduce:
# save the attached .config to linux build tree
make ARCH=x86_64 Note: the linux-review/Sven-Schmidt/Update-LZ4-compressor-module/20161221-130109 HEAD 08652114d6d8867d2e8b3545b5e9f8a2fe11a5d7 builds fine.
It only hurts bisectibility.
All errors (new ones prefixed by >>):
fs/squashfs/lz4_wrapper.c: In function 'lz4_uncompress':
>> fs/squashfs/lz4_wrapper.c:111:8: error: implicit declaration of function 'LZ4_decompress_safe' [-Werror=implicit-function-declaration]
res = LZ4_decompress_safe(stream->input, stream->output, length, (int)dest_len);
^~~~~~~~~~~~~~~~~~~cc1: some warnings being treated as errors
vim +/LZ4_decompress_safe +111 fs/squashfs/lz4_wrapper.c
105 buff += avail;
106 bytes -= avail;
107 offset = 0;
108 put_bh(bh[i]);
109 }
110
> 111 res = LZ4_decompress_safe(stream->input, stream->output, length, (int)dest_len);
112 if (res)
113 return -EIO;
114
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
kbuild test robot
build botRe: [PATCH 2/3] fs/pstore: fs/squashfs: Change lz4 compressor functions to work with new version
In reply to Sven Schmidturl: https://github.com/0day-ci/linux/commits/Sven-Schmidt/Update-LZ4-compressor-module/20161221-130109 config: x86_64-randconfig-x006-201651 (attached as .config) compiler: gcc-6 (Debian 6.2.0-3)…
Hi Sven,
[auto build test ERROR on linus/master]
[also build test ERROR on next-20161221]
[cannot apply to v4.9]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Sven-Schmidt/Update-LZ4-compressor-module/20161221-130109
config: x86_64-randconfig-x006-201651 (attached as .config)
compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901 reproduce:
# save the attached .config to linux build tree
make ARCH=x86_64 Note: the linux-review/Sven-Schmidt/Update-LZ4-compressor-module/20161221-130109 HEAD 08652114d6d8867d2e8b3545b5e9f8a2fe11a5d7 builds fine.
It only hurts bisectibility.
All errors (new ones prefixed by >>):
fs/pstore/platform.c: In function 'compress_lz4':
>> fs/pstore/platform.c:345:8: error: implicit declaration of function 'LZ4_compress_default' [-Werror=implicit-function-declaration]
ret = LZ4_compress_default(in, out, inlen, outlen, workspace);
^~~~~~~~~~~~~~~~~~~~
fs/pstore/platform.c: In function 'decompress_lz4':
>> fs/pstore/platform.c:358:8: error: implicit declaration of function 'LZ4_decompress_safe' [-Werror=implicit-function-declaration]
ret = LZ4_decompress_safe(in, out, inlen, outlen);
^~~~~~~~~~~~~~~~~~~
fs/pstore/platform.c: In function 'allocate_lz4':
>> fs/pstore/platform.c:369:20: error: implicit declaration of function 'LZ4_compressBound' [-Werror=implicit-function-declaration]
big_oops_buf_sz = LZ4_compressBound(psinfo->bufsize);
^~~~~~~~~~~~~~~~~cc1: some warnings being treated as errors
vim +/LZ4_compress_default +345 fs/pstore/platform.c
339
340 #ifdef CONFIG_PSTORE_LZ4_COMPRESS
341 static int compress_lz4(const void *in, void *out, size_t inlen, size_t outlen)
342 {
343 int ret;
344
> 345 ret = LZ4_compress_default(in, out, inlen, outlen, workspace);
346 if (ret) {
347 pr_err("lz4_compress error, ret = %d!\n", ret);
348 return -EIO;
349 }
350
351 return outlen;
352 }
353
354 static int decompress_lz4(void *in, void *out, size_t inlen, size_t outlen)
355 {
356 int ret;
357
> 358 ret = LZ4_decompress_safe(in, out, inlen, outlen);
359 if (ret) {
360 pr_err("lz4_decompress error, ret = %d!\n", ret);
361 return -EIO;
362 }
363
364 return outlen;
365 }
366
367 static void allocate_lz4(void)
368 {
> 369 big_oops_buf_sz = LZ4_compressBound(psinfo->bufsize); 370 big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
371 if (big_oops_buf) {
372 workspace = kmalloc(LZ4_MEM_COMPRESS, GFP_KERNEL);
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Re: [PATCH 2/3] fs/pstore: fs/squashfs: Change lz4 compressor functions to work with new version
In reply to Sven SchmidtQuoted 10 lines · Sven Schmidt
fs/pstore/platform.c +1
-1@@ -366,7 +366,7 @@ static int decompress_lz4(void *in, void *out, size_t inlen, size_t outlen)static void allocate_lz4 (void ) { - big_oops_buf_sz =lz4_compressbound (psinfo->bufsize); + big_oops_buf_sz =LZ4_compressBound (psinfo->bufsize); big_oops_buf =kmalloc (big_oops_buf_sz,GFP_KERNEL );if (big_oops_buf) { workspace =kmalloc (LZ4_MEM_COMPRESS ,GFP_KERNEL );
fs/pstore/platform.c static void pstore_dump(struct kmsg_dumper *dumper, · line 496
Quoted 4 lines
fs/pstore/platform.c +0
-1@@ -493,7 +493,6 @@ static void pstore_dump(struct kmsg_dumper *dumper,if (!is_locked) {pr_err ("pstore dump routine blocked in %s path, may corrupt error record\n" ,in_nmi () ?"NMI" : why); -return ;
why did you remove this "failed to lock buf in critical path" return?
fs/pstore/platform.c static void pstore_console_write(struct console *con, const char *s, unsigned c) · line 588
Quoted 11 lines
fs/pstore/platform.c +2
-2} }else {spin_lock_irqsave (&psinfo->buf_lock, flags);@@ -585,8 +584,8 @@ static void pstore_console_write(struct console *con, const char *s, unsigned c) }else {spin_lock_irqsave (&psinfo->buf_lock, flags); } - psinfo->write_buf (PSTORE_TYPE_CONSOLE ,0 , &id,0 , - s,0 , c, psinfo); +memcpy (psinfo->buf, s, c); + psinfo->write (PSTORE_TYPE_CONSOLE ,0 , &id,0 ,0 ,0 , c, psinfo);
why extra memcpy()?
-ss
Quoted 16 lines
fs/pstore/platform.c +0
-0spin_unlock_irqrestore (&psinfo->buf_lock, flags); s += c; c = e - s;fs/squashfs/lz4_wrapper.c +1
-2@@ -108,8 +108,7 @@ static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm,put_bh (bh[i]); } - res =lz4_decompress_unknownoutputsize (stream->input, length, - stream->output, &dest_len); + res =LZ4_decompress_safe (stream->input, stream->output, length, (int )dest_len);if (res)return -EIO ;
Re: [PATCH 2/3] fs/pstore: fs/squashfs: Change lz4 compressor functions to work with new version
Quoted 46 lines · Sergey Senozhatsky
Quoted 10 lines · Sven Schmidt
fs/pstore/platform.c +1
-1@@ -366,7 +366,7 @@ static int decompress_lz4(void *in, void *out, size_t inlen, size_t outlen)static void allocate_lz4 (void ) { - big_oops_buf_sz =lz4_compressbound (psinfo->bufsize); + big_oops_buf_sz =LZ4_compressBound (psinfo->bufsize); big_oops_buf =kmalloc (big_oops_buf_sz,GFP_KERNEL );if (big_oops_buf) { workspace =kmalloc (LZ4_MEM_COMPRESS ,GFP_KERNEL );fs/pstore/platform.c static void pstore_dump(struct kmsg_dumper *dumper, · line 496
Quoted 4 lines
fs/pstore/platform.c +0
-1@@ -493,7 +493,6 @@ static void pstore_dump(struct kmsg_dumper *dumper,if (!is_locked) {pr_err ("pstore dump routine blocked in %s path, may corrupt error record\n" ,in_nmi () ?"NMI" : why); -return ;why did you remove this "failed to lock buf in critical path" return?
fs/pstore/platform.c static void pstore_console_write(struct console *con, const char *s, unsigned c) · line 588
Quoted 11 lines
fs/pstore/platform.c +2
-2} }else {spin_lock_irqsave (&psinfo->buf_lock, flags);@@ -585,8 +584,8 @@ static void pstore_console_write(struct console *con, const char *s, unsigned c) }else {spin_lock_irqsave (&psinfo->buf_lock, flags); } - psinfo->write_buf (PSTORE_TYPE_CONSOLE ,0 , &id,0 , - s,0 , c, psinfo); +memcpy (psinfo->buf, s, c); + psinfo->write (PSTORE_TYPE_CONSOLE ,0 , &id,0 ,0 ,0 , c, psinfo);why extra
memcpy()?-ssQuoted 11 lines
fs/pstore/platform.c +0
-0spin_unlock_irqrestore (&psinfo->buf_lock, flags); s += c; c = e - s;fs/squashfs/lz4_wrapper.c +0
-0@@ -108,8 +108,7 @@ static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm,put_bh (bh[i]); }
Quoted 6 lines
fs/squashfs/lz4_wrapper.c static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm, · line 113
Quoted 5 lines
fs/squashfs/lz4_wrapper.c +1
-2- res =lz4_decompress_unknownoutputsize (stream->input, length, - stream->output, &dest_len); + res =LZ4_decompress_safe (stream->input, stream->output, length, (int )dest_len);if (res)return -EIO ;
Hey Sergey and thanks for your time. I'm afraid I messed up the diffstat on this. Actually I just replaced the function calls and corrected the use of return values. I will send an updated patchset later since there also have been errors reported from the buildbots.
Sven
Re: [PATCH 2/3] fs/pstore: fs/squashfs: Change lz4 compressor functions to work with new version
Quoted 4 lines · Sven Schmidt
Hey Sergey and thanks for your time. I'm afraid I messed up the diffstat on this. Actually I just replaced the function calls and corrected the use of return values. I will send an updated patchset later since there also have been errors reported from the buildbots.
and please use scripts/get_maintainer.pl so maintainers are in Cc list.
-ss
Re: [PATCH 2/3] fs/pstore: fs/squashfs: Change lz4 compressor functions to work with new version
In reply to Sven SchmidtCc Anton, Colin, Kees, Tony on this
https://marc.info/?l=linux-kernel&m=148226086323932
Quoted 77 lines · Sven Schmidt
This patch updates the fs/pstore and fs/squashfs to work with the new LZ4 kernel module version.
Signed-off-by Sven Schmidt <[email protected]>--- fs/pstore/platform.c | 11 +++++------ fs/squashfs/lz4_wrapper.c | 3 +-- 2 files changed, 6 insertions(+), 8 deletions(-)fs/pstore/platform.c +5
-6@@ -342,7 +342,7 @@ static int compress_lz4(const void *in, void *out, size_t inlen, size_t outlen) {int ret; - ret =lz4_compress (in, inlen, out, &outlen, workspace); + ret =LZ4_compress_default (in, out, inlen, outlen, workspace);if (ret) {pr_err ("lz4_compress error, ret = %d!\n" , ret);return -EIO ;@@ -355,7 +355,7 @@ static int decompress_lz4(void *in, void *out, size_t inlen, size_t outlen) {int ret; - ret =lz4_decompress_unknownoutputsize (in, inlen, out, &outlen); + ret =LZ4_decompress_safe (in, out, inlen, outlen);if (ret) {pr_err ("lz4_decompress error, ret = %d!\n" , ret);return -EIO ;@@ -366,7 +366,7 @@ static int decompress_lz4(void *in, void *out, size_t inlen, size_t outlen)static void allocate_lz4 (void ) { - big_oops_buf_sz =lz4_compressbound (psinfo->bufsize); + big_oops_buf_sz =LZ4_compressBound (psinfo->bufsize); big_oops_buf =kmalloc (big_oops_buf_sz,GFP_KERNEL );if (big_oops_buf) { workspace =kmalloc (LZ4_MEM_COMPRESS ,GFP_KERNEL );@@ -493,7 +493,6 @@ static void pstore_dump(struct kmsg_dumper *dumper,if (!is_locked) {pr_err ("pstore dump routine blocked in %s path, may corrupt error record\n" ,in_nmi () ?"NMI" : why); -return ; } }else {spin_lock_irqsave (&psinfo->buf_lock, flags);@@ -585,8 +584,8 @@ static void pstore_console_write(struct console *con, const char *s, unsigned c) }else {spin_lock_irqsave (&psinfo->buf_lock, flags); } - psinfo->write_buf (PSTORE_TYPE_CONSOLE ,0 , &id,0 , - s,0 , c, psinfo); +memcpy (psinfo->buf, s, c); + psinfo->write (PSTORE_TYPE_CONSOLE ,0 , &id,0 ,0 ,0 , c, psinfo);spin_unlock_irqrestore (&psinfo->buf_lock, flags); s += c; c = e - s;fs/squashfs/lz4_wrapper.c +1
-2@@ -108,8 +108,7 @@ static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm,put_bh (bh[i]); } - res =lz4_decompress_unknownoutputsize (stream->input, length, - stream->output, &dest_len); + res =LZ4_decompress_safe (stream->input, stream->output, length, (int )dest_len);if (res)return -EIO ;Signature · 1 line
2.1.4
PATCH 3/3 lib: Update LZ4 compressor module based on LZ4 v1.7.2.
In reply to Sven SchmidtThis patch updates LZ4 kernel module to LZ4 v1.7.2 by Yann Collet. The kernel module is inspired by the previous work by Chanho Min. The updated LZ4 module will not break existing code since there were alias methods added to ensure backwards compatibility.
API changes:
New method LZ4_compress_fast which differs from the variant available in kernel by the new acceleration parameter, allowing to trade compression ratio for more speedup.
LZ4_decompress_fast is the respective decompression method, allowing to decompress data compressed with any LZ4 compression method including LZ4HC.
Also the useful functions LZ4_decompress_safe_partial LZ4_compress_destsize were added. The latter reverses the logic by trying to compress as much data as possible from source to dest while the former aims to decompress partial blocks of data.
The methods lz4_compress and lz4_decompress_unknownoutputsize are now known as LZ4_compress_default respectivley LZ4_decompress_safe. The old methods are still available for offering backwards compatibility.
--- include/linux/lz4.h | 267 +++++++++--- lib/decompress_unlz4.c | 11 +- lib/lz4/lz4_compress.c | 1024 +++++++++++++++++++++++++------------------ lib/lz4/lz4_decompress.c | 627 +++++++++++++-------------- lib/lz4/lz4defs.h | 359 +++++++++------ lib/lz4/lz4hc_compress.c | 1083 +++++++++++++++++++++++++--------------------- 6 files changed, 1941 insertions(+), 1430 deletions(-)
include/linux/lz4.h +199 -68
@@ -1,87 +1,218 @@#ifndef __LZ4_H__#define __LZ4_H__ +/* * LZ4 Kernel Interface * -* Copyright (C) 2013, LG Electronics, Kyungsik Lee <[email protected]> +* Copyright (C) 2016, Sven Schmidt <[email protected]> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. */ + +#include <linux/types.h> + +/*-************************************************************************ +* Version +**************************************************************************/ +#define LZ4_VERSION_MAJOR 1/* for breaking interface changes */ +#define LZ4_VERSION_MINOR 7/* for new (non-breaking) interface capabilities */ +#define LZ4_VERSION_RELEASE 2/* for tweaks, bug-fixes, or development */ + +#define LZ4_VERSION_NUMBER (LZ4_VERSION_MAJOR *100*100 + LZ4_VERSION_MINOR *100 + LZ4_VERSION_RELEASE) + +/*-************************************************************************ +* Constants +**************************************************************************/ #define LZ4_MEM_COMPRESS (16384)#define LZ4HC_MEM_COMPRESS (262144 + (2 * sizeof(unsigned char *))) -/* -* lz4_compressbound() -* Provides the maximum size that LZ4 may output in a "worst case" scenario -* (input data not compressible) -*/ -static inline size_t lz4_compressbound (size_t isize) -{ -return isize + (isize /255 ) +16 ; +#define LZ4_MAX_INPUT_SIZE 0x7E000000/* 2 113 929 216 bytes */ +#define LZ4_COMPRESSBOUND (isize) ((unsigned)(isize) > (unsigned)LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize)/255) + 16) + +#define LZ4HC_MIN_CLEVEL 3 +#define LZ4HC_DEFAULT_CLEVEL 9 +#define LZ4HC_MAX_CLEVEL 16 + +/*-************************************************************************ +* Compression Functions +**************************************************************************/ + +/*!LZ4_compressbound() : +Provides the maximum size that LZ4 may output in a "worst case" scenario +(input data not compressible) +*/ +static inline size_t LZ4_compressBound (size_t isize) { +return LZ4_COMPRESSBOUND (isize); } -/* -* lz4_compress() -* src : source address of the original data -* src_len : size of the original data -* dst : output buffer address of the compressed data -* This requires 'dst' of size LZ4_COMPRESSBOUND. -* dst_len : is the output size, which is returned after compress done -* workmem : address of the working memory. -* This requires 'workmem' of size LZ4_MEM_COMPRESS. -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer and workmem must be already allocated with -* the defined size. -*/ -int lz4_compress (const unsigned char *src,size_t src_len, -unsigned char *dst,size_t *dst_len,void *wrkmem); - -/* -* lz4hc_compress() -* src : source address of the original data -* src_len : size of the original data -* dst : output buffer address of the compressed data -* This requires 'dst' of size LZ4_COMPRESSBOUND. -* dst_len : is the output size, which is returned after compress done -* workmem : address of the working memory. -* This requires 'workmem' of size LZ4HC_MEM_COMPRESS. -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer and workmem must be already allocated with -* the defined size. -*/ -int lz4hc_compress (const unsigned char *src,size_t src_len, -unsigned char *dst,size_t *dst_len,void *wrkmem); +/*! lz4_compressbound() : +For backward compabitiliby +*/ +static inline size_t lz4_compressbound (size_t isize) { +return lz4_compressbound (isize); +} -/* -* lz4_decompress() -* src : source address of the compressed data -* src_len : is the input size, whcih is returned after decompress done -* dest : output buffer address of the decompressed data -* actual_dest_len: is the size of uncompressed data, supposing it's known -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer must be already allocated. -* slightly faster than lz4_decompress_unknownoutputsize() -*/ -int lz4_decompress (const unsigned char *src,size_t *src_len, -unsigned char *dest,size_t actual_dest_len); +/*! LZ4_compress_default() : +Compresses 'sourceSize' bytes from buffer 'source' +into already allocated 'dest' buffer of size 'maxDestSize'. +Compression is guaranteed to succeed if 'maxDestSize' >= LZ4_compressBound(sourceSize). +It also runs faster, so it's a recommended setting. +If the function cannot compress 'source' into a more limited 'dest' budget, +compression stops *immediately*, and the function result is zero. +As a consequence, 'dest' content is not valid. +This function never writes outside 'dest' buffer, nor read outside 'source' buffer. +sourceSize : Max supported value is LZ4_MAX_INPUT_VALUE +maxDestSize : full or partial size of buffer 'dest' (which must be already allocated) +workmem : address of the working memory. This requires 'workmem' of size LZ4_MEM_COMPRESS. +return : the number of bytes written into buffer 'dest' (necessarily <= maxOutputSize) +or 0 if compression fails */ +int LZ4_compress_default (const char * source,char * dest,int inputSize,int maxOutputSize,void * wrkmem); -/* -* lz4_decompress_unknownoutputsize() -* src : source address of the compressed data -* src_len : is the input size, therefore the compressed size -* dest : output buffer address of the decompressed data -* dest_len: is the max size of the destination buffer, which is -* returned with actual size of decompressed data after -* decompress done -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer must be already allocated. +/*! +LZ4_compress_fast() : +Same as LZ4_compress_default(), but allows to select an "acceleration" factor. +The larger the acceleration value, the faster the algorithm, but also the lesser the compression. +It's a trade-off. It can be fine tuned, with each successive value providing roughly +~3% to speed. +An acceleration value of "1" is the same as regular LZ4_compress_default() +Values <= 0 will be replaced by ACCELERATION_DEFAULT, which is 1. +*/ +int LZ4_compress_fast (const char * source,char * dest,int inputSize,int maxOutputSize,void * wrkmem,int acceleration); + +/*! +LZ4_compress_destSize() : +Reverse the logic, by compressing as much data as possible from 'source' buffer +into already allocated buffer 'dest' of size 'targetDestSize'. +This function either compresses the entire 'source' content into 'dest' if it's large enough, +or fill 'dest' buffer completely with as much data as possible from 'source'. +*sourceSizePtr : will be modified to indicate how many bytes where read from 'source' to fill 'dest'. +New value is necessarily <= old value. +workmem : address of the working memory. This requires 'workmem' of size LZ4_MEM_COMPRESS. +return : Nb bytes written into 'dest' (necessarily <= targetDestSize) +or 0 if compression fails +*/ +int LZ4_compress_destSize (const char * source,char * dest,int * sourceSizePtr,int targetDestSize,void * wrkmem); + +/*! +* lz4_compress() : +For backwards compabitiliby +src : source address of the original data +src_len : size of the original data +dst : output buffer address of the compressed data +This requires 'dst' of size LZ4_COMPRESSBOUND. +dst_len : is the output size, which is returned after compress done +workmem : address of the working memory. +This requires 'workmem' of size LZ4_MEM_COMPRESS. +return : Success if return 0 +Error if return (< 0) +note : Destination buffer and workmem must be already allocated with +the defined size. */ -int lz4_decompress_unknownoutputsize (const unsigned char *src,size_t src_len, -unsigned char *dest,size_t *dest_len); +int lz4_compress (const unsigned char *src,int src_len,unsigned char *dst,int dst_len,void *wrkmem); + +/*-************************************************************************ +* Decompression Functions +**************************************************************************/ + +/*! +LZ4_decompress_fast() : +originalSize : is the original and therefore uncompressed size +return : the number of bytes read from the source buffer (in other words, the compressed size) +If the source stream is detected malformed, the function will stop decoding and return a negative result. +Destination buffer must be already allocated. Its size must be a minimum of 'originalSize' bytes. +note : This function fully respect memory boundaries for properly formed compressed data. +It is a bit faster than LZ4_decompress_safe(). +However, it does not provide any protection against intentionally modified data stream (malicious input). +Use this function in trusted environment only (data to decode comes from a trusted source). +*/ +int LZ4_decompress_fast (const char * source,char * dest,int originalSize); + +/*! +LZ4_decompress_safe() : +compressedSize : is the precise full size of the compressed block. +maxDecompressedSize : is the size of destination buffer, which must be already allocated. +return : the number of bytes decompressed into destination buffer (necessarily <= maxDecompressedSize) +If destination buffer is not large enough, decoding will stop and output an error code (<0). +If the source stream is detected malformed, the function will stop decoding and return a negative result. +This function is protected against buffer overflow exploits, including malicious data packets. +It never writes outside output buffer, nor reads outside input buffer. +*/ +int LZ4_decompress_safe (const char * source,char * dest,int compressedSize,int maxDecompressedSize); + +/*! +LZ4_decompress_safe_partial() : +This function decompress a compressed block of size 'compressedSize' at position 'source' +into destination buffer 'dest' of size 'maxDecompressedSize'. +The function tries to stop decompressing operation as soon as 'targetOutputSize' has been reached, +reducing decompression time. +return : the number of bytes decoded in the destination buffer (necessarily <= maxDecompressedSize) +Note : this number can be < 'targetOutputSize' should the compressed block to decode be smaller. +Always control how many bytes were decoded. +If the source stream is detected malformed, the function will stop decoding and return a negative result. +This function never writes outside of output buffer, and never reads outside of input buffer. It is therefore protected against malicious data packets +*/ +int LZ4_decompress_safe_partial (const char * source,char * dest,int compressedSize,int targetOutputSize,int maxDecompressedSize); + + +/*! +lz4_decompress_unknownoutputsize() : +For backwards compabitiliby +src : source address of the compressed data +src_len : is the input size, therefore the compressed size +dest : output buffer address of the decompressed data +dest_len: is the max size of the destination buffer, which is +returned with actual size of decompressed data after +decompress done +return : Success if return 0 +Error if return (< 0) +note : Destination buffer must be already allocated. +*/ +int lz4_decompress_unknownoutputsize (const unsigned char *src,size_t src_len,unsigned char *dest,size_t *dest_len); + +/*! +lz4_decompress() : +For backwards compabitiliby +src : source address of the compressed data +src_len : is the input size, whcih is returned after decompress done +dest : output buffer address of the decompressed data +actual_dest_len: is the size of uncompressed data, supposing it's known +return : Success if return 0 +Error if return (< 0) +note : Destination buffer must be already allocated. +slightly faster than lz4_decompress_unknownoutputsize() +*/ +int lz4_decompress (const unsigned char *src,size_t *src_len,unsigned char *dest,size_t actual_dest_len); + +/*-************************************************************************ +* LZ4 HC Compression +**************************************************************************/ + +/*! LZ4_compress_HC() : +Compress data from `src` into `dst`, using the more powerful but slower "HC" algorithm. dst` must be already allocated. +wrkmem : address of the working memory. This requires 'wrkmem' of size LZ4HC_MEM_COMPRESS. +Compression is guaranteed to succeed if `dstCapacity >= LZ4_compressBound(srcSize)` +Max supported `srcSize` value is LZ4_MAX_INPUT_SIZE +compressionLevel` : Recommended values are between 4 and 9, although any value between 1 and LZ4HC_MAX_CLEVEL will work. +Values >LZ4HC_MAX_CLEVEL behave the same as 16. +@return : the number of bytes written into 'dst' or 0 if compression fails. +*/ +int LZ4_compress_HC (const char * src,char * dst,int srcSize,int dstCapacity,int compressionLevel,void * wrkmem); + +/*! lz4hc_compress() +For backwards compabitiliby +src : source address of the original data +src_len : size of the original data +dst : output buffer address of the compressed data +This requires 'dst' of size LZ4_COMPRESSBOUND. +dst_len : is the output size, which is returned after compress done +workmem : address of the working memory. +This requires 'workmem' of size LZ4HC_MEM_COMPRESS. +return : Success if return 0 +Error if return (< 0) +note : Destination buffer and workmem must be already allocated with +the defined size. +*/ +int lz4hc_compress (const unsigned char *src,size_t src_len,unsigned char *dst,size_t *dst_len,void *wrkmem); +#endif
lib/decompress_unlz4.c +6 -5
@@ -72,7 +72,7 @@ STATIC inline int INIT unlz4(u8 *input, long in_len,error ("NULL input pointer and missing fill function" ); goto exit_1; }else { - inp =large_malloc (lz4_compressbound (uncomp_chunksize)); + inp =large_malloc (LZ4_compressBound (uncomp_chunksize));if (!inp) {error ("Could not allocate input buffer" ); goto exit_1;
@@ -136,7 +136,7 @@ STATIC inline int INIT unlz4(u8 *input, long in_len, inp +=4 ; size -=4 ; }else { -if (chunksize >lz4_compressbound (uncomp_chunksize)) { +if (chunksize >LZ4_compressBound (uncomp_chunksize)) {error ("chunk length is longer than allocated" ); goto exit_2; }
@@ -152,11 +152,12 @@ STATIC inline int INIT unlz4(u8 *input, long in_len, out_len -= dest_len; }else dest_len = out_len; - ret =lz4_decompress (inp, &chunksize, outp, dest_len); + + ret =LZ4_decompress_fast (inp, outp, chunksize);#else dest_len = uncomp_chunksize; - ret =lz4_decompress_unknownoutputsize (inp, chunksize, outp, - &dest_len); + + ret =LZ4_decompress_safe (inp, outp, chunksize, dest_len);#endif if (ret <0 ) {error ("Decoding failed" );
lib/lz4/lz4_compress.c +610 -414
1,053 lines not on this page · raw message
lib/lz4/lz4_decompress.c +305 -322
648 lines not on this page · raw message
lib/lz4/lz4defs.h +237 -122
394 lines not on this page · raw message
lib/lz4/lz4hc_compress.c +584 -499
588 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.1.4
Part of the diff is not shown here. View the raw message.
Re: [PATCH 3/3] lib: Update LZ4 compressor module based on LZ4 v1.7.2.
Quoted 4 lines · Sven Schmidt
This patch updates LZ4 kernel module to LZ4 v1.7.2 by Yann Collet. The kernel module is inspired by the previous work by Chanho Min. The updated LZ4 module will not break existing code since there were alias methods added to ensure backwards compatibility.
[]
include/linux/lz4.h
Quoted 1 line
include/linux/lz4.h +0
-0
[]
include/linux/lz4.h line 7
Quoted 9 lines
include/linux/lz4.h +2
-1@@ -1,87 +1,218 @@#ifndef __LZ4_H__#define __LZ4_H__ +/* * LZ4 Kernel Interface * -* Copyright (C) 2013, LG Electronics, Kyungsik Lee <[email protected]> +* Copyright (C) 2016, Sven Schmidt <[email protected]>
Deleting copyright notices is poor form and shouldn't be done.
I didn't look at the rest
Re: [PATCH 3/3] lib: Update LZ4 compressor module based on LZ4 v1.7.2.
Quoted 22 lines · Joe Perches
Quoted 4 lines · Sven Schmidt
This patch updates LZ4 kernel module to LZ4 v1.7.2 by Yann Collet. The kernel module is inspired by the previous work by Chanho Min. The updated LZ4 module will not break existing code since there were alias methods added to ensure backwards compatibility.
[]
include/linux/lz4.h
Quoted 1 line
include/linux/lz4.h +0
-0[]
include/linux/lz4.h line 7
Quoted 9 lines
include/linux/lz4.h +2
-1@@ -1,87 +1,218 @@#ifndef __LZ4_H__#define __LZ4_H__ +/* * LZ4 Kernel Interface * -* Copyright (C) 2013, LG Electronics, Kyungsik Lee <[email protected]> +* Copyright (C) 2016, Sven Schmidt <[email protected]> Deleting copyright notices is poor form and shouldn't be done.
Quoted 2 lines
I didn't look at the rest
Thanks Joe for your time. If you would take a look at the rest, you may notice that I changed almost the whole file. That's why I also changed the copyright note. If you think I should keep the original note, I will do that.
What I did change:
- I included additional information stating that the file is based on Yann Collets original header file (especially in matters of the function comments)
Sven
Re: [PATCH 3/3] lib: Update LZ4 compressor module based on LZ4 v1.7.2.
Quoted 23 lines · Sven Schmidt
Quoted 22 lines · Joe Perches
Quoted 4 lines · Sven Schmidt
This patch updates LZ4 kernel module to LZ4 v1.7.2 by Yann Collet. The kernel module is inspired by the previous work by Chanho Min. The updated LZ4 module will not break existing code since there were alias methods added to ensure backwards compatibility.
[]
include/linux/lz4.h
Quoted 1 line
include/linux/lz4.h +0
-0[]
include/linux/lz4.h line 7
Quoted 9 lines
include/linux/lz4.h +2
-1@@ -1,87 +1,218 @@#ifndef __LZ4_H__#define __LZ4_H__ +/* * LZ4 Kernel Interface * -* Copyright (C) 2013, LG Electronics, Kyungsik Lee <[email protected]> +* Copyright (C) 2016, Sven Schmidt <[email protected]> Deleting copyright notices is poor form and shouldn't be done.
Quoted 6 lines
Quoted 2 lines
I didn't look at the rest
Thanks Joe for your time. If you would take a look at the rest, you may notice that I changed almost the whole file. That's why I also changed the copyright note. If you think I should keep the original note, I will do that.
You have to, please consult a lawyer if you have questions about copyright issues. Or better yet, there's a free presentation/course online about copyright on the linuxfoundation.org website somewhere that should answer all of your questions here. I recommend taking a few minutes and going through that.
thanks,
greg k-h
Re: [PATCH 3/3] lib: Update LZ4 compressor module based on LZ4 v1.7.2.
Quoted 37 lines · Greg KH
Quoted 23 lines · Sven Schmidt
Quoted 22 lines · Joe Perches
Quoted 4 lines · Sven Schmidt
This patch updates LZ4 kernel module to LZ4 v1.7.2 by Yann Collet. The kernel module is inspired by the previous work by Chanho Min. The updated LZ4 module will not break existing code since there were alias methods added to ensure backwards compatibility.
[]
include/linux/lz4.h
Quoted 1 line
include/linux/lz4.h +0
-0[]
include/linux/lz4.h line 7
Quoted 9 lines
include/linux/lz4.h +2
-1@@ -1,87 +1,218 @@#ifndef __LZ4_H__#define __LZ4_H__ +/* * LZ4 Kernel Interface * -* Copyright (C) 2013, LG Electronics, Kyungsik Lee <[email protected]> +* Copyright (C) 2016, Sven Schmidt <[email protected]> Deleting copyright notices is poor form and shouldn't be done.
Quoted 6 lines
Quoted 2 lines
I didn't look at the rest
Thanks Joe for your time. If you would take a look at the rest, you may notice that I changed almost the whole file. That's why I also changed the copyright note. If you think I should keep the original note, I will do that.
You have to, please consult a lawyer if you have questions about copyright issues. Or better yet, there's a free presentation/course online about copyright on the linuxfoundation.org website somewhere that should answer all of your questions here. I recommend taking a few minutes and going through that.
Quoted 4 lines
thanks,
greg k-h
Will do, thanks Greg!
kbuild test robot
build botRe: [PATCH 3/3] lib: Update LZ4 compressor module based on LZ4 v1.7.2.
In reply to Sven Schmidturl: https://github.com/0day-ci/linux/commits/Sven-Schmidt/Update-LZ4-compressor-module/20161221-130109 config: x86_64-randconfig-r0-12211341 (attached as .config) compiler: gcc-6 (Debian 6.2.0-3)…
Hi Sven,
[auto build test ERROR on linus/master]
[also build test ERROR on next-20161221]
[cannot apply to v4.9]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Sven-Schmidt/Update-LZ4-compressor-module/20161221-130109
config: x86_64-randconfig-r0-12211341 (attached as .config)
compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901 reproduce:
# save the attached .config to linux build tree
make ARCH=x86_64 All errors (new ones prefixed by >>):
In file included from /usr/lib/gcc/x86_64-linux-gnu/6/include/stdint.h:11:0,
from arch/x86/boot/compressed/../../../../lib/lz4/lz4defs.h:60,
from arch/x86/boot/compressed/../../../../lib/lz4/lz4_decompress.c:36,
from arch/x86/boot/compressed/../../../../lib/decompress_unlz4.c:13,
from arch/x86/boot/compressed/misc.c:75:
>> /usr/lib/gcc/x86_64-linux-gnu/6/include/stdint-gcc.h:43:24: error: conflicting types for 'int64_t'
typedef __INT64_TYPE__ int64_t;
^~~~~~~
In file included from include/uapi/linux/screen_info.h:4:0,
from include/linux/screen_info.h:4,
from arch/x86/boot/compressed/misc.h:15,
from arch/x86/boot/compressed/misc.c:14:
include/linux/types.h:113:17: note: previous declaration of 'int64_t' was here
typedef __s64 int64_t;
^~~~~~~
In file included from /usr/lib/gcc/x86_64-linux-gnu/6/include/stdint.h:11:0,
from arch/x86/boot/compressed/../../../../lib/lz4/lz4defs.h:60,
from arch/x86/boot/compressed/../../../../lib/lz4/lz4_decompress.c:36,
from arch/x86/boot/compressed/../../../../lib/decompress_unlz4.c:13,
from arch/x86/boot/compressed/misc.c:75:
>> /usr/lib/gcc/x86_64-linux-gnu/6/include/stdint-gcc.h:55:25: error: conflicting types for 'uint64_t'
typedef __UINT64_TYPE__ uint64_t;
^~~~~~~~
In file included from include/uapi/linux/screen_info.h:4:0,
from include/linux/screen_info.h:4,
from arch/x86/boot/compressed/misc.h:15,
from arch/x86/boot/compressed/misc.c:14:
include/linux/types.h:111:17: note: previous declaration of 'uint64_t' was here
typedef __u64 uint64_t;
^~~~~~~~---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Quoted 5 lines · Sven Schmidt
This patchset is for updating the LZ4 compression module to a version based on LZ4 v1.7.2 allowing to use the fast compression algorithm aka LZ4 fast which provides an "acceleration" parameter as a tradeoff between compression ratio and compression speed.
But why do this?
Quoted 5 lines
We will use LZ4 fast in order to support compression in lustre. LZ4 fast empowers us to do client-side as well as server-side compression/decompression while being able to provide appropriate parameters to enable users to tune lustre's behaviour to obtain the best performance/compression/etc. on their behalf (adapative compression).
We don't care about lustre, especially as it is not merged into the main portion of the kernel tree :)
Seriously, work on fixing up the known issues in lustre before adding additional features, I've only been saying this for a few _years_ now...
Quoted 6 lines
Also, it will be useful for other users of LZ4 compression,
as with LZ4 fast it is possible to enable applications to use fast and/or high compression depending of the usecase. E.g. a developer can use very high compression (low acceleration) for sending data over a network with limited rate of transmission or he trades the compression ratio for higher compression speed.
This whole patch series is broken, always test-build your code, there's nothing we could do with these patches even if we wanted to :(
greg k-h
Quoted 15 lines · Greg KH
Quoted 5 lines · Sven Schmidt
This patchset is for updating the LZ4 compression module to a version based on LZ4 v1.7.2 allowing to use the fast compression algorithm aka LZ4 fast which provides an "acceleration" parameter as a tradeoff between compression ratio and compression speed.
But why do this?
Quoted 5 lines
We will use LZ4 fast in order to support compression in lustre. LZ4 fast empowers us to do client-side as well as server-side compression/decompression while being able to provide appropriate parameters to enable users to tune lustre's behaviour to obtain the best performance/compression/etc. on their behalf (adapative compression).
Quoted 6 lines
We don't care about lustre, especially as it is not merged into the main portion of the kernel tree :)
Seriously, work on fixing up the known issues in lustre before adding additional features, I've only been saying this for a few _years_ now...
Hey Greg and thanks for your time. Actually, we're not the guys behind lustre, hence I'd leave fixing the bugs in lustre to them ;)
I'm working with the research group for scientific computing on the University of Hamburg on the German Climate Computing Centre. The research group is working with high performance storage systems etc. In this case, we investigate data reduction techniques in behalf of the increasing gap between computational speed, network speed and storage capacity. That's why we're ultimately aiming for compression support in lustre.
Initial studies have shown that an adequate compression ratio for scientific data can be archieved using LZ4. Since the currently available version of LZ4 in the kernel is about three years old, we would love if you accept our work on getting a more current version into the kernel.
Quoted 7 lines
Quoted 6 lines
Also, it will be useful for other users of LZ4 compression,
as with LZ4 fast it is possible to enable applications to use fast and/or high compression depending of the usecase. E.g. a developer can use very high compression (low acceleration) for sending data over a network with limited rate of transmission or he trades the compression ratio for higher compression speed.
Quoted 5 lines
This whole patch series is broken, always test-build your code, there's nothing we could do with these patches even if we wanted to :(
greg k-h
I'm sorry, this is my first patchset, so please be kind :( Already got rid of the issues on my local machine and reviewed the output from the buildbots. Will send an updated patchset later!
Thanks
Sven
Quoted 33 lines · Sven Schmidt
Quoted 15 lines · Greg KH
Quoted 5 lines · Sven Schmidt
This patchset is for updating the LZ4 compression module to a version based on LZ4 v1.7.2 allowing to use the fast compression algorithm aka LZ4 fast which provides an "acceleration" parameter as a tradeoff between compression ratio and compression speed.
But why do this?
Quoted 5 lines
We will use LZ4 fast in order to support compression in lustre. LZ4 fast empowers us to do client-side as well as server-side compression/decompression while being able to provide appropriate parameters to enable users to tune lustre's behaviour to obtain the best performance/compression/etc. on their behalf (adapative compression).
Quoted 6 lines
We don't care about lustre, especially as it is not merged into the main portion of the kernel tree :)
Seriously, work on fixing up the known issues in lustre before adding additional features, I've only been saying this for a few _years_ now...
Hey Greg and thanks for your time. Actually, we're not the guys behind lustre, hence I'd leave fixing the bugs in lustre to them ;)
I'm working with the research group for scientific computing on the University of Hamburg on the German Climate Computing Centre. The research group is working with high performance storage systems etc. In this case, we investigate data reduction techniques in behalf of the increasing gap between computational speed, network speed and storage capacity. That's why we're ultimately aiming for compression support in lustre.
Quoted 4 lines
Initial studies have shown that an adequate compression ratio for scientific data can be archieved using LZ4. Since the currently available version of LZ4 in the kernel is about three years old, we would love if you accept our work on getting a more current version into the kernel.
But the LZ4 code isn't really used much in the kernel today, so if you change it, what is really going to benefit? And if you want to archive things, you aren't using the in-kernel lz4 code, right?
If you want to add support for compression for lustre, great, I suggest working with those developers, but note, I can't take any new features for lustre until that code is out of drivers/staging/.
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Also, it will be useful for other users of LZ4 compression,
as with LZ4 fast it is possible to enable applications to use fast and/or high compression depending of the usecase. E.g. a developer can use very high compression (low acceleration) for sending data over a network with limited rate of transmission or he trades the compression ratio for higher compression speed.
Quoted 7 lines
Quoted 5 lines
This whole patch series is broken, always test-build your code, there's nothing we could do with these patches even if we wanted to :(
greg k-h
I'm sorry, this is my first patchset, so please be kind :(
Not a problem, just always test-build your patches, in series, so that we don't get grumpy for obvious problems :)
thanks,
greg k-h
v2 cover Update LZ4 compressor module
In reply to Sven SchmidtThis patchset is for updating the LZ4 compression module to a version based on LZ4 v1.7.2 allowing to use the fast compression algorithm aka LZ4 fast which provides an "acceleration" parameter as a tradeoff between high compression ratio and high compression speed.
We want to use LZ4 fast in order to support compression in lustre and (mostly, based on that) investigate data reduction techniques in behalf of storage systems.
Also, it will be useful for other users of LZ4 compression, as with LZ4 fast it is possible to enable applications to use fast and/or high compression depending on the usecase.
For instance, ZRAM is offering a LZ4 backend and could benefit from an updated LZ4 in the kernel.
LZ4 homepage: http://www.lz4.org/
LZ4 source repository: https://github.com/lz4/lz4
Source version: 1.7.2
Benchmark (taken from [1], Core i5-4300U @1.9GHz):
----------------|--------------|----------------|---------- Compressor | Compression | Decompression | Ratio ----------------|--------------|----------------|---------- memcpy | 4200 MB/s | 4200 MB/s | 1.000 LZ4 fast 50 | 1080 MB/s | 2650 MB/s | 1.375 LZ4 fast 17 | 680 MB/s | 2220 MB/s | 1.607 LZ4 fast 5 | 475 MB/s | 1920 MB/s | 1.886 LZ4 default | 385 MB/s | 1850 MB/s | 2.101
[1] http://fastcompression.blogspot.de/2015/04/sampling-or-faster-lz4.html
[PATCHv2 1/4] lib: Update LZ4 compressor module based on LZ4 v1.7.2
[PATCHv2 2/4] lib: Update decompress_unlz4 wrapper to work with new LZ4 module [PATCHv2 3/4] crypto: Change lz4 modules to work with new LZ4 module
[PATCHv2 4/4] fs/pstore: fs/squashfs: Change LZ4 compressor functions to work with new LZ4 module
v2:
- Changed order of the patches since in the initial patchset the lz4.h was in the last patch but was referenced by the other ones
- Split lib/decompress_unlz4.c in an own patch
- Fixed errors reported by the buildbot
- Further refactorings
- Added more appropriate copyright note to include/linux/lz4.h
PATCH v2 1/4 lib: Update LZ4 compressor module based on LZ4 v1.7.2.
This patch updates LZ4 kernel module to LZ4 v1.7.2 by Yann Collet. The kernel module is inspired by the previous work by Chanho Min. The updated LZ4 module will not break existing code since there were alias methods added to ensure backwards compatibility.
API changes:
New method LZ4_compress_fast which differs from the variant available in kernel by the new acceleration parameter, allowing to trade compression ratio for more speedup.
LZ4_decompress_fast is the respective decompression method, allowing to decompress data compressed with any LZ4 compression method including LZ4HC.
Also the useful functions LZ4_decompress_safe_partial LZ4_compress_destsize were added. The latter reverses the logic by trying to compress as much data as possible from source to dest while the former aims to decompress partial blocks of data.
The methods lz4_compress and lz4_decompress_unknownoutputsize are now known as LZ4_compress_default respectivley LZ4_decompress_safe. The old methods are still available for offering backwards compatibility.
--- include/linux/lz4.h | 307 ++++++++++---- lib/lz4/lz4_compress.c | 1024 +++++++++++++++++++++++++++------------------ lib/lz4/lz4_decompress.c | 631 ++++++++++++++-------------- lib/lz4/lz4defs.h | 378 +++++++++++------ lib/lz4/lz4hc_compress.c | 1045 ++++++++++++++++++++++++---------------------- 5 files changed, 1938 insertions(+), 1447 deletions(-)
include/linux/lz4.h +229 -78
@@ -1,87 +1,238 @@ +/* LZ4 Kernel Interface + +Copyright (C) 2013, LG Electronics, Kyungsik Lee <[email protected]> +Copyright (C) 2016, Sven Schmidt <[email protected]> + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License version 2 as +published by the Free Software Foundation. + +This file is based on the original header file for LZ4 - Fast LZ compression algorithm. + +LZ4 - Fast LZ compression algorithm +Copyright (C) 2011-2016, Yann Collet. +BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: +* Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. +* Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +You can contact the author at : +- LZ4 homepage : http://www.lz4.org +- LZ4 source repository : https://github.com/lz4/lz4 +*/ +#ifndef __LZ4_H__#define __LZ4_H__ -/* -* LZ4 Kernel Interface -* -* Copyright (C) 2013, LG Electronics, Kyungsik Lee <[email protected]> -* -* This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License version 2 as -* published by the Free Software Foundation. -*/ + +#include <linux/types.h> + +/*-************************************************************************ +* Constants +**************************************************************************/ #define LZ4_MEM_COMPRESS (16384)#define LZ4HC_MEM_COMPRESS (262144 + (2 * sizeof(unsigned char *))) -/* -* lz4_compressbound() -* Provides the maximum size that LZ4 may output in a "worst case" scenario -* (input data not compressible) -*/ -static inline size_t lz4_compressbound (size_t isize) -{ -return isize + (isize /255 ) +16 ; +#define LZ4_MAX_INPUT_SIZE 0x7E000000/* 2 113 929 216 bytes */ +#define LZ4_COMPRESSBOUND (isize) ((unsigned)(isize) > (unsigned)LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize)/255) + 16) + +#define LZ4HC_MIN_CLEVEL 3 +#define LZ4HC_DEFAULT_CLEVEL 9 +#define LZ4HC_MAX_CLEVEL 16 + +/*-************************************************************************ +* Compression Functions +**************************************************************************/ + +/*!LZ4_compressbound() : +Provides the maximum size that LZ4 may output in a "worst case" scenario +(input data not compressible) +*/ +static inline int LZ4_compressBound (int isize) { +return LZ4_COMPRESSBOUND (isize); } -/* -* lz4_compress() -* src : source address of the original data -* src_len : size of the original data -* dst : output buffer address of the compressed data -* This requires 'dst' of size LZ4_COMPRESSBOUND. -* dst_len : is the output size, which is returned after compress done -* workmem : address of the working memory. -* This requires 'workmem' of size LZ4_MEM_COMPRESS. -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer and workmem must be already allocated with -* the defined size. -*/ -int lz4_compress (const unsigned char *src,size_t src_len, -unsigned char *dst,size_t *dst_len,void *wrkmem); - -/* -* lz4hc_compress() -* src : source address of the original data -* src_len : size of the original data -* dst : output buffer address of the compressed data -* This requires 'dst' of size LZ4_COMPRESSBOUND. -* dst_len : is the output size, which is returned after compress done -* workmem : address of the working memory. -* This requires 'workmem' of size LZ4HC_MEM_COMPRESS. -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer and workmem must be already allocated with -* the defined size. -*/ -int lz4hc_compress (const unsigned char *src,size_t src_len, -unsigned char *dst,size_t *dst_len,void *wrkmem); - -/* -* lz4_decompress() -* src : source address of the compressed data -* src_len : is the input size, whcih is returned after decompress done -* dest : output buffer address of the decompressed data -* actual_dest_len: is the size of uncompressed data, supposing it's known -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer must be already allocated. -* slightly faster than lz4_decompress_unknownoutputsize() -*/ -int lz4_decompress (const unsigned char *src,size_t *src_len, -unsigned char *dest,size_t actual_dest_len); - -/* -* lz4_decompress_unknownoutputsize() -* src : source address of the compressed data -* src_len : is the input size, therefore the compressed size -* dest : output buffer address of the decompressed data -* dest_len: is the max size of the destination buffer, which is -* returned with actual size of decompressed data after -* decompress done -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer must be already allocated. +/*! lz4_compressbound() : +For backward compabitiliby +*/ +static inline size_t lz4_compressbound (size_t isize) { +return (int )LZ4_COMPRESSBOUND (isize); +} + +/*! LZ4_compress_default() : +Compresses 'sourceSize' bytes from buffer 'source' +into already allocated 'dest' buffer of size 'maxDestSize'. +Compression is guaranteed to succeed if 'maxDestSize' >= LZ4_compressBound(sourceSize). +It also runs faster, so it's a recommended setting. +If the function cannot compress 'source' into a more limited 'dest' budget, +compression stops *immediately*, and the function result is zero. +As a consequence, 'dest' content is not valid. +This function never writes outside 'dest' buffer, nor read outside 'source' buffer. +sourceSize : Max supported value is LZ4_MAX_INPUT_VALUE +maxDestSize : full or partial size of buffer 'dest' (which must be already allocated) +workmem : address of the working memory. This requires 'workmem' of size LZ4_MEM_COMPRESS. +return : the number of bytes written into buffer 'dest' (necessarily <= maxOutputSize) +or 0 if compression fails */ +int LZ4_compress_default (const char * source,char * dest,int inputSize,int maxOutputSize,void * wrkmem); + +/*! +LZ4_compress_fast() : +Same as LZ4_compress_default(), but allows to select an "acceleration" factor. +The larger the acceleration value, the faster the algorithm, but also the lesser the compression. +It's a trade-off. It can be fine tuned, with each successive value providing roughly +~3% to speed. +An acceleration value of "1" is the same as regular LZ4_compress_default() +Values <= 0 will be replaced by ACCELERATION_DEFAULT, which is 1. +*/ +int LZ4_compress_fast (const char * source,char * dest,int inputSize,int maxOutputSize,void * wrkmem,int acceleration); + +/*! +LZ4_compress_destSize() : +Reverse the logic, by compressing as much data as possible from 'source' buffer +into already allocated buffer 'dest' of size 'targetDestSize'. +This function either compresses the entire 'source' content into 'dest' if it's large enough, +or fill 'dest' buffer completely with as much data as possible from 'source'. +*sourceSizePtr : will be modified to indicate how many bytes where read from 'source' to fill 'dest'. +New value is necessarily <= old value. +workmem : address of the working memory. This requires 'workmem' of size LZ4_MEM_COMPRESS. +return : Nb bytes written into 'dest' (necessarily <= targetDestSize) +or 0 if compression fails +*/ +int LZ4_compress_destSize (const char * source,char * dest,int * sourceSizePtr,int targetDestSize,void * wrkmem); + +/*! +* lz4_compress() : +For backwards compabitiliby +src : source address of the original data +src_len : size of the original data +dst : output buffer address of the compressed data +This requires 'dst' of size LZ4_COMPRESSBOUND. +dst_len : is the output size, which is returned after compress done +workmem : address of the working memory. +This requires 'workmem' of size LZ4_MEM_COMPRESS. +return : Success if return 0 +Error if return (< 0) +note : Destination buffer and workmem must be already allocated with +the defined size. */ -int lz4_decompress_unknownoutputsize (const unsigned char *src,size_t src_len, -unsigned char *dest,size_t *dest_len); +int lz4_compress (const unsigned char *src,size_t src_len,unsigned char *dst,size_t *dst_len,void *wrkmem); + +/*-************************************************************************ +* Decompression Functions +**************************************************************************/ + +/*! +LZ4_decompress_fast() : +originalSize : is the original and therefore uncompressed size +return : the number of bytes read from the source buffer (in other words, the compressed size) +If the source stream is detected malformed, the function will stop decoding and return a negative result. +Destination buffer must be already allocated. Its size must be a minimum of 'originalSize' bytes. +note : This function fully respect memory boundaries for properly formed compressed data. +It is a bit faster than LZ4_decompress_safe(). +However, it does not provide any protection against intentionally modified data stream (malicious input). +Use this function in trusted environment only (data to decode comes from a trusted source). +*/ +int LZ4_decompress_fast (const char * source,char * dest,int originalSize); + +/*! +LZ4_decompress_safe() : +compressedSize : is the precise full size of the compressed block. +maxDecompressedSize : is the size of destination buffer, which must be already allocated. +return : the number of bytes decompressed into destination buffer (necessarily <= maxDecompressedSize) +If destination buffer is not large enough, decoding will stop and output an error code (<0). +If the source stream is detected malformed, the function will stop decoding and return a negative result. +This function is protected against buffer overflow exploits, including malicious data packets. +It never writes outside output buffer, nor reads outside input buffer. +*/ +int LZ4_decompress_safe (const char * source,char * dest,int compressedSize,int maxDecompressedSize); + +/*! +LZ4_decompress_safe_partial() : +This function decompress a compressed block of size 'compressedSize' at position 'source' +into destination buffer 'dest' of size 'maxDecompressedSize'. +The function tries to stop decompressing operation as soon as 'targetOutputSize' has been reached, +reducing decompression time. +return : the number of bytes decoded in the destination buffer (necessarily <= maxDecompressedSize) +Note : this number can be < 'targetOutputSize' should the compressed block to decode be smaller. +Always control how many bytes were decoded. +If the source stream is detected malformed, the function will stop decoding and return a negative result. +This function never writes outside of output buffer, and never reads outside of input buffer. It is therefore protected against malicious data packets +*/ +int LZ4_decompress_safe_partial (const char * source,char * dest,int compressedSize,int targetOutputSize,int maxDecompressedSize); + + +/*! +lz4_decompress_unknownoutputsize() : +For backwards compabitiliby +src : source address of the compressed data +src_len : is the input size, therefore the compressed size +dest : output buffer address of the decompressed data +dest_len: is the max size of the destination buffer, which is +returned with actual size of decompressed data after +decompress done +return : Success if return 0 +Error if return (< 0) +note : Destination buffer must be already allocated. +*/ +int lz4_decompress_unknownoutputsize (const unsigned char *src,size_t src_len,unsigned char *dest,size_t *dest_len); + +/*! +lz4_decompress() : +For backwards compabitiliby +src : source address of the compressed data +src_len : is the input size, which is returned after decompress done +dest : output buffer address of the decompressed data +actual_dest_len: is the size of uncompressed data, supposing it's known +return : Success if return 0 +Error if return (< 0) +note : Destination buffer must be already allocated. +slightly faster than lz4_decompress_unknownoutputsize() +*/ +int lz4_decompress (const unsigned char *src,size_t *src_len,unsigned char *dest,size_t actual_dest_len); + +/*-************************************************************************ +* LZ4 HC Compression +**************************************************************************/ + +/*! LZ4_compress_HC() : +Compress data from `src` into `dst`, using the more powerful but slower "HC" algorithm. dst` must be already allocated. +wrkmem : address of the working memory. This requires 'wrkmem' of size LZ4HC_MEM_COMPRESS. +Compression is guaranteed to succeed if `dstCapacity >= LZ4_compressBound(srcSize)` +Max supported `srcSize` value is LZ4_MAX_INPUT_SIZE +compressionLevel` : Recommended values are between 4 and 9, although any value between 1 and LZ4HC_MAX_CLEVEL will work. +Values >LZ4HC_MAX_CLEVEL behave the same as 16. +@return : the number of bytes written into 'dst' or 0 if compression fails. +*/ +int LZ4_compress_HC (const char * src,char * dst,int srcSize,int dstCapacity,int compressionLevel,void * wrkmem); + +/*! lz4hc_compress() +For backwards compabitiliby +src : source address of the original data +src_len : size of the original data +dst : output buffer address of the compressed data +This requires 'dst' of size LZ4_COMPRESSBOUND. +dst_len : is the output size, which is returned after compress done +workmem : address of the working memory. +This requires 'workmem' of size LZ4HC_MEM_COMPRESS. +return : Success if return 0 +Error if return (< 0) +note : Destination buffer and workmem must be already allocated with +the defined size. +*/ +int lz4hc_compress (const unsigned char *src,size_t src_len,unsigned char *dst,size_t *dst_len,void *wrkmem); +#endif
lib/lz4/lz4_compress.c +610 -414
1,053 lines not on this page · raw message
lib/lz4/lz4_decompress.c +309 -322
652 lines not on this page · raw message
lib/lz4/lz4defs.h +249 -129
406 lines not on this page · raw message
lib/lz4/lz4hc_compress.c +541 -504
573 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.1.4
Part of the diff is not shown here. View the raw message.
Re: [PATCH v2 1/4] lib: Update LZ4 compressor module based on LZ4 v1.7.2.
include/linux/lz4.h line 65
Quoted 4 lines · Sven Schmidt
include/linux/lz4.h +4
-0+/*!LZ4_compressbound() : +Provides the maximum size that LZ4 may output in a "worst case" scenario +(input data not compressible) +*/
Odd coding style, please use kerneldoc format if you are going to have comments like this.
include/linux/lz4.h line 67
Quoted 2 lines
include/linux/lz4.h +2
-0+static inline int LZ4_compressBound (int isize) { +return LZ4_COMPRESSBOUND (isize);
And follow the proper kernel coding style rules, putting your patches through scripts/checkpatch.pl should help you out here.
thanks,
greg k-h
Re: [PATCH v2 1/4] lib: Update LZ4 compressor module based on LZ4 v1.7.2.
Quoted 24 lines · Greg KH
Quoted 12 lines · Sven Schmidt
This patch updates the unlz4 wrapper to work with the new LZ4 kernel module version.
Signed-off-by Sven Schmidt <[email protected]>--- lib/decompress_unlz4.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)lib/decompress_unlz4.c +0
-0lib/decompress_unlz4.c STATIC inline int INIT unlz4(u8 *input, long in_len, · line 75
Quoted 5 lines
lib/decompress_unlz4.c +1
-1@@ -72,7 +72,7 @@ STATIC inline int INIT unlz4(u8 *input, long in_len,error ("NULL input pointer and missing fill function" ); goto exit_1; }else { - inp =large_malloc (lz4_compressbound (uncomp_chunksize)); + inp =large_malloc (LZ4_compressBound (uncomp_chunksize));Having functions differ by different cases of the characters is ripe for abuse and confusion. Please never do that, especially as these "new" functions you created don't follow the correct kernel coding style rules :(
Quoted 3 lines
thanks,
greg k-h
Hi Greg,
you're right about that. I think I put a little too much effort in keeping old function names here. I will get rid of that.
I also want to quote your previous E-Mail here:
Quoted 2 lines
And follow the proper kernel coding style rules, putting your patches through
scripts/checkpatch.plshould help you out here.
Sorry, I didn't know about that particular script. I already put Patches 2, 3 and 4 through checkpatch.pl; patch 1 is a little more to rework but I will, of course, do that.
Thanks,
Sven
Re: [PATCH v2 1/4] lib: Update LZ4 compressor module based on LZ4 v1.7.2.
Quoted 7 lines · Sven Schmidt
Quoted 2 lines · Greg KH
And follow the proper kernel coding style rules, putting your patches through
scripts/checkpatch.plshould help you out here.Sorry, I didn't know about that particular script. I already put Patches 2, 3 and 4 through checkpatch.pl; patch 1 is a little more to rework but I will, of course, do that.
You do know about Documentation/SubmittingPatches, right? If not, take a look at that as well :)
thanks,
greg k-h
Re: [PATCH v2 1/4] lib: Update LZ4 compressor module based on LZ4 v1.7.2.
In reply to Sven SchmidtQuoted 4 lines · Sven Schmidt
This patch updates LZ4 kernel module to LZ4 v1.7.2 by Yann Collet. The kernel module is inspired by the previous work by Chanho Min. The updated LZ4 module will not break existing code since there were alias methods added to ensure backwards compatibility.
Meta-comment. Does this update include all of the security fixes that we have made over the past few years to the lz4 code? I don't want to be adding back insecure functions that will cause us problems.
Specifically look at the changes I made in 2014 in this directory for an example of what I am talking about here.
thanks,
greg k-h
Re: [PATCH v2 1/4] lib: Update LZ4 compressor module based on LZ4 v1.7.2.
Quoted 10 lines · Greg KH
Quoted 4 lines · Sven Schmidt
This patch updates LZ4 kernel module to LZ4 v1.7.2 by Yann Collet. The kernel module is inspired by the previous work by Chanho Min. The updated LZ4 module will not break existing code since there were alias methods added to ensure backwards compatibility.
Meta-comment. Does this update include all of the security fixes that we have made over the past few years to the lz4 code? I don't want to be adding back insecure functions that will cause us problems.
Quoted 6 lines
Specifically look at the changes I made in 2014 in this directory for an example of what I am talking about here.
thanks,
greg k-h
Also, this series must be tested on big endian, to make sure the last fixes we made don't regress.
Thanks,
Rui
Re: [PATCH v2 1/4] lib: Update LZ4 compressor module based on LZ4 v1.7.2.
In reply to Greg KHQuoted 6 lines · Greg KH
Quoted 4 lines · Sven Schmidt
This patch updates LZ4 kernel module to LZ4 v1.7.2 by Yann Collet. The kernel module is inspired by the previous work by Chanho Min. The updated LZ4 module will not break existing code since there were alias methods added to ensure backwards compatibility.
Quoted 7 lines
Meta-comment. Does this update include all of the security fixes that we have made over the past few years to the lz4 code? I don't want to be adding back insecure functions that will cause us problems.
Specifically look at the changes I made in 2014 in this directory for an example of what I am talking about here.
Hi Greg,
it doesn't. I didn't have that in mind until now.
But I had a look at the related commits after I received your E-Mail and will review what I have to change (and, obviously, do the changes :)).
Thanks,
Sven
Re: [PATCH v2 1/4] lib: Update LZ4 compressor module based on LZ4 v1.7.2.
Quoted 11 lines · Sven Schmidt
Quoted 10 lines · Greg KH
Quoted 4 lines · Sven Schmidt
This patch updates LZ4 kernel module to LZ4 v1.7.2 by Yann Collet. The kernel module is inspired by the previous work by Chanho Min. The updated LZ4 module will not break existing code since there were alias methods added to ensure backwards compatibility.
Meta-comment. Does this update include all of the security fixes that we have made over the past few years to the lz4 code? I don't want to be adding back insecure functions that will cause us problems.
Quoted 7 lines
Quoted 3 lines
Specifically look at the changes I made in 2014 in this directory for an example of what I am talking about here.
Hi Greg,
it doesn't. I didn't have that in mind until now.
Ick, those changes never got made "upstream"? Not good, but makes sense as we couldn't really find an "upstream" when we made them :(
As you took this code from somewhere, you might want to also push your changes for these issues there as well, so that others don't run into them in the future.
thanks,
greg k-h
Re: [PATCH v2 1/4] lib: Update LZ4 compressor module based on LZ4 v1.7.2.
Quoted 16 lines · Greg KH
Quoted 15 lines · Sven Schmidt
Quoted 13 lines · Greg KH
Quoted 4 lines · Sven Schmidt
This patch updates LZ4 kernel module to LZ4 v1.7.2 by Yann Collet. The kernel module is inspired by the previous work by Chanho Min. The updated LZ4 module will not break existing code since there were alias methods added to ensure backwards compatibility.
Meta-comment. Does this update include all of the security fixes that we have made over the past few years to the lz4 code? I don't want to be adding back insecure functions that will cause us problems.
Specifically look at the changes I made in 2014 in this directory for an example of what I am talking about here.
Quoted 6 lines
Quoted 3 lines
Hi Greg,
it doesn't. I didn't have that in mind until now.
Ick, those changes never got made "upstream"? Not good, but makes sense as we couldn't really find an "upstream" when we made them :(
I *seem* to remember that some of these changes were specific to our implementation, and were discovered during a review after we worked on the the LZO implementation bugs, though I could be wrong.
If this is the case, it is one more reason for being extra careful.
Quoted 3 lines
As you took this code from somewhere, you might want to also push your changes for these issues there as well, so that others don't run into them in the future.
Agreed!
Willy
PATCH v2 2/4 lib/decompress_unlz4: Change module to work with new LZ4 module version
In reply to Sven SchmidtThis patch updates the unlz4 wrapper to work with the new LZ4 kernel module version.
--- lib/decompress_unlz4.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
lib/decompress_unlz4.c +8 -5
@@ -72,7 +72,7 @@ STATIC inline int INIT unlz4(u8 *input, long in_len,error ("NULL input pointer and missing fill function" ); goto exit_1; }else { - inp =large_malloc (lz4_compressbound (uncomp_chunksize)); + inp =large_malloc (LZ4_compressBound (uncomp_chunksize));if (!inp) {error ("Could not allocate input buffer" ); goto exit_1;
@@ -136,7 +136,7 @@ STATIC inline int INIT unlz4(u8 *input, long in_len, inp +=4 ; size -=4 ; }else { -if (chunksize >lz4_compressbound (uncomp_chunksize)) { +if (chunksize >LZ4_compressBound (uncomp_chunksize)) {error ("chunk length is longer than allocated" ); goto exit_2; }
@@ -152,11 +152,14 @@ STATIC inline int INIT unlz4(u8 *input, long in_len, out_len -= dest_len; }else dest_len = out_len; - ret =lz4_decompress (inp, &chunksize, outp, dest_len); + + ret =LZ4_decompress_fast (inp, outp, dest_len); + chunksize = ret;#else dest_len = uncomp_chunksize; - ret =lz4_decompress_unknownoutputsize (inp, chunksize, outp, - &dest_len); + + ret =LZ4_decompress_safe (inp, outp, chunksize, dest_len); + dest_len = ret;#endif if (ret <0 ) {error ("Decoding failed" );
Signature · 1 line
2.1.4
Re: [PATCH v2 2/4] lib/decompress_unlz4: Change module to work with new LZ4 module version
Quoted 12 lines · Sven Schmidt
This patch updates the unlz4 wrapper to work with the new LZ4 kernel module version.
Signed-off-by Sven Schmidt <[email protected]>--- lib/decompress_unlz4.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)lib/decompress_unlz4.c +0
-0
lib/decompress_unlz4.c STATIC inline int INIT unlz4(u8 *input, long in_len, · line 75
Quoted 5 lines
lib/decompress_unlz4.c +1
-1@@ -72,7 +72,7 @@ STATIC inline int INIT unlz4(u8 *input, long in_len,error ("NULL input pointer and missing fill function" ); goto exit_1; }else { - inp =large_malloc (lz4_compressbound (uncomp_chunksize)); + inp =large_malloc (LZ4_compressBound (uncomp_chunksize));
Having functions differ by different cases of the characters is ripe for abuse and confusion. Please never do that, especially as these "new" functions you created don't follow the correct kernel coding style rules :(
thanks,
greg k-h
PATCH v2 3/4 crypto: Change LZ4 modules to work with new LZ4 module version
In reply to Sven SchmidtThis patch updates the crypto modules using LZ4 compression to work with the new LZ4 module version.
--- crypto/lz4.c | 27 ++++++++++++++------------- crypto/lz4hc.c | 25 +++++++++++++------------ 2 files changed, 27 insertions(+), 25 deletions(-)
crypto/lz4.c +14 -13
@@ -66,15 +66,16 @@ static void lz4_exit(struct crypto_tfm *tfm)static int __lz4_compress_crypto (const u8 *src,unsigned int slen,u8 *dst,unsigned int *dlen,void *ctx) { -size_t tmp_len = *dlen; -int err; +int out_len; - err =lz4_compress (src, slen, dst, &tmp_len, ctx); + out_len =LZ4_compress_default (src, dst, slen, (int )((size_t )dlen), ctx); -if (err <0 ) -return -EINVAL ; +if (out_len ==0 ) { +// out_len is 0 means an error occured +return -EINVAL ; + } - *dlen = tmp_len; + *dlen = out_len;return 0 ; }
@@ -96,16 +97,16 @@ static int lz4_compress_crypto(struct crypto_tfm *tfm, const u8 *src,static int __lz4_decompress_crypto (const u8 *src,unsigned int slen,u8 *dst,unsigned int *dlen,void *ctx) { -int err; -size_t tmp_len = *dlen; -size_t __slen = slen; +int out_len; - err =lz4_decompress_unknownoutputsize (src, __slen, dst, &tmp_len); -if (err <0 ) + out_len =LZ4_decompress_safe (src, dst, slen, (int )((size_t )dlen)); +if (out_len <0 ) { +// out_len of less than 0 means an error occured return -EINVAL ; + } - *dlen = tmp_len; -return err; + *dlen = out_len; +return out_len; } static int lz4_sdecompress(struct crypto_scomp *tfm, const u8 *src,
crypto/lz4hc.c +13 -12
@@ -65,15 +65,16 @@ static void lz4hc_exit(struct crypto_tfm *tfm)static int __lz4hc_compress_crypto (const u8 *src,unsigned int slen,u8 *dst,unsigned int *dlen,void *ctx) { -size_t tmp_len = *dlen; -int err; +int out_len; - err =lz4hc_compress (src, slen, dst, &tmp_len, ctx); + out_len =LZ4_compress_HC (src, dst, slen, (int )((size_t )dlen),LZ4HC_DEFAULT_CLEVEL , ctx); -if (err <0 ) +if (out_len ==0 ) { +// out_len of 0 -> error return -EINVAL ; + } - *dlen = tmp_len; + *dlen = out_len;return 0 ; }
@@ -97,16 +98,16 @@ static int lz4hc_compress_crypto(struct crypto_tfm *tfm, const u8 *src,static int __lz4hc_decompress_crypto (const u8 *src,unsigned int slen,u8 *dst,unsigned int *dlen,void *ctx) { -int err; -size_t tmp_len = *dlen; -size_t __slen = slen; +int out_len; - err =lz4_decompress_unknownoutputsize (src, __slen, dst, &tmp_len); -if (err <0 ) + out_len =LZ4_decompress_safe (src, dst, slen, (int )((size_t )dlen)); +if (out_len <0 ) { +// out_len of less than 0 means an error occured return -EINVAL ; + } - *dlen = tmp_len; -return err; + *dlen = out_len; +return out_len; } static int lz4hc_sdecompress(struct crypto_scomp *tfm, const u8 *src,
Signature · 1 line
2.1.4
PATCH v2 4/4 fs/pstore: fs/squashfs: Change usage of LZ4 to comply with new LZ4 module version
In reply to Sven SchmidtThis patch updates fs/pstore and fs/squashfs to use the updated functions from the new LZ4 module.
--- fs/pstore/platform.c | 14 ++++++++------ fs/squashfs/lz4_wrapper.c | 12 ++++++------ 2 files changed, 14 insertions(+), 12 deletions(-)
fs/pstore/platform.c +8 -6
@@ -342,31 +342,33 @@ static int compress_lz4(const void *in, void *out, size_t inlen, size_t outlen) {int ret; - ret =lz4_compress (in, inlen, out, &outlen, workspace); + ret =LZ4_compress_default (in, out, inlen, outlen, workspace);if (ret) { +// ret is 0 means an error occured pr_err ("lz4_compress error, ret = %d!\n" , ret);return -EIO ; } -return outlen; +return ret; }static int decompress_lz4 (void *in,void *out,size_t inlen,size_t outlen) {int ret; - ret =lz4_decompress_unknownoutputsize (in, inlen, out, &outlen); -if (ret) { + ret =LZ4_decompress_safe (in, out, inlen, outlen); +if (ret <0 ) { +// return value is < 0 in case of error pr_err ("lz4_decompress error, ret = %d!\n" , ret);return -EIO ; } -return outlen; +return ret; }static void allocate_lz4 (void ) { - big_oops_buf_sz =lz4_compressbound (psinfo->bufsize); + big_oops_buf_sz =LZ4_compressBound (psinfo->bufsize); big_oops_buf =kmalloc (big_oops_buf_sz,GFP_KERNEL );if (big_oops_buf) { workspace =kmalloc (LZ4_MEM_COMPRESS ,GFP_KERNEL );
fs/squashfs/lz4_wrapper.c +6 -6
@@ -97,7 +97,6 @@ static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm,struct squashfs_lz4 *stream = strm;void *buff = stream->input, *data;int avail, i, bytes = length, res; -size_t dest_len = output->length;for (i =0 ; i < b; i++) { avail =min (bytes, msblk->devblksize - offset);
@@ -108,12 +107,13 @@ static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm,put_bh (bh[i]); } - res =lz4_decompress_unknownoutputsize (stream->input, length, - stream->output, &dest_len); -if (res) + res =LZ4_decompress_safe (stream->input, stream->output, length, output->length); +if (res <0 ) { +// res of less than 0 means an error occured return -EIO ; + } - bytes = dest_len; + bytes = res; data =squashfs_first_page (output); buff = stream->output;while (data) {
@@ -128,7 +128,7 @@ static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm, }squashfs_finish_page (output); -return dest_len; +return res; }const struct squashfs_decompressor squashfs_lz4_comp_ops = {
Signature · 1 line
2.1.4
Re: [PATCH v2 4/4] fs/pstore: fs/squashfs: Change usage of LZ4 to comply with new LZ4 module version
Quoted 14 lines · Sven Schmidt
This patch updates fs/pstore and fs/squashfs to use the updated functions from the new LZ4 module.
Signed-off-by Sven Schmidt <[email protected]>--- fs/pstore/platform.c | 14 ++++++++------ fs/squashfs/lz4_wrapper.c | 12 ++++++------ 2 files changed, 14 insertions(+), 12 deletions(-)fs/pstore/platform.c +0
-0
fs/pstore/platform.c static int compress_lz4(const void *in, void *out, size_t inlen, size_t outlen) · line 347
Quoted 7 lines
fs/pstore/platform.c +2
-1@@ -342,31 +342,33 @@ static int compress_lz4(const void *in, void *out, size_t inlen, size_t outlen) {int ret; - ret =lz4_compress (in, inlen, out, &outlen, workspace); + ret =LZ4_compress_default (in, out, inlen, outlen, workspace);if (ret) { +// ret is 0 means an error occured
If that's true, then shouldn't the "if" logic be changed? Also, here and in all following comments are C++ style instead of kernel C-style. This should be "/* ret == 0 means an error occured */", though really, that should be obvious from the code and the comment isn't really needed.
fs/pstore/platform.c static int compress_lz4(const void *in, void *out, size_t inlen, size_t outlen) · line 348
Quoted 1 line
fs/pstore/platform.c +0
-0pr_err ("lz4_compress error, ret = %d!\n" , ret);
If it's always going to be zero here, is there a better place to get details on why it failed?
Quoted 71 lines
fs/pstore/platform.c +6
-5return -EIO ; } -return outlen; +return ret; }static int decompress_lz4 (void *in,void *out,size_t inlen,size_t outlen) {int ret; - ret =lz4_decompress_unknownoutputsize (in, inlen, out, &outlen); -if (ret) { + ret =LZ4_decompress_safe (in, out, inlen, outlen); +if (ret <0 ) { +// return value is < 0 in case of error pr_err ("lz4_decompress error, ret = %d!\n" , ret);return -EIO ; } -return outlen; +return ret; }static void allocate_lz4 (void ) { - big_oops_buf_sz =lz4_compressbound (psinfo->bufsize); + big_oops_buf_sz =LZ4_compressBound (psinfo->bufsize); big_oops_buf =kmalloc (big_oops_buf_sz,GFP_KERNEL );if (big_oops_buf) { workspace =kmalloc (LZ4_MEM_COMPRESS ,GFP_KERNEL );fs/squashfs/lz4_wrapper.c +6
-7@@ -97,7 +97,6 @@ static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm,struct squashfs_lz4 *stream = strm;void *buff = stream->input, *data;int avail, i, bytes = length, res; -size_t dest_len = output->length;for (i =0 ; i < b; i++) { avail =min (bytes, msblk->devblksize - offset);@@ -108,12 +107,13 @@ static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm,put_bh (bh[i]); } - res =lz4_decompress_unknownoutputsize (stream->input, length, - stream->output, &dest_len); -if (res) + res =LZ4_decompress_safe (stream->input, stream->output, length, output->length); +if (res <0 ) { +// res of less than 0 means an error occured return -EIO ; + } - bytes = dest_len; + bytes = res; data =squashfs_first_page (output); buff = stream->output;while (data) {@@ -128,7 +128,7 @@ static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm, }squashfs_finish_page (output); -return dest_len; +return res; }const struct squashfs_decompressor squashfs_lz4_comp_ops = { --
Quoted 2 lines
2.1.4
-Kees
Signature · 2 lines
Kees Cook Nexus Security
Re: [PATCH v2 4/4] fs/pstore: fs/squashfs: Change usage of LZ4 to comply with new LZ4 module version
Hi Kees,
Quoted 19 lines · Kees Cook
Quoted 17 lines · Sven Schmidt
This patch updates fs/pstore and fs/squashfs to use the updated functions from the new LZ4 module.
Signed-off-by Sven Schmidt <[email protected]>--- fs/pstore/platform.c | 14 ++++++++------ fs/squashfs/lz4_wrapper.c | 12 ++++++------ 2 files changed, 14 insertions(+), 12 deletions(-)fs/pstore/platform.c +0
-0@@ -342,31 +342,33 @@ static int compress_lz4(const void *in, void *out, size_t inlen, size_t outlen) {int ret;
Quoted 10 lines
fs/pstore/platform.c static int compress_lz4(const void *in, void *out, size_t inlen, size_t outlen) · line 347
Quoted 4 lines
fs/pstore/platform.c +2
-1- ret =lz4_compress (in, inlen, out, &outlen, workspace); + ret =LZ4_compress_default (in, out, inlen, outlen, workspace);if (ret) { +// ret is 0 means an error occured If that's true, then shouldn't the "if" logic be changed? Also, here and in all following comments are C++ style instead of kernel C-style. This should be "/* ret == 0 means an error occured */", though really, that should be obvious from the code and the comment isn't really needed.
indeed, it should. I fixed that one.
Quoted 3 lines
fs/pstore/platform.c static int compress_lz4(const void *in, void *out, size_t inlen, size_t outlen) · line 348
Quoted 1 line
fs/pstore/platform.c +0
-0pr_err ("lz4_compress error, ret = %d!\n" , ret);If it's always going to be zero here, is there a better place to get details on why it failed?
It is always going to be zero. Honestly, after looking at the current LZ4 in kernel again I don't get why there actually was a need to print the return value since lz4_compress will (as far as I see) always return -1 in case of an error while the new lz4_compress_fast/default will return 0 in such case. Maybe I should just stick with the error?
Thanks,
Sven
v3 cover Update LZ4 compressor module
In reply to Sven SchmidtThis patchset is for updating the LZ4 compression module to a version based on LZ4 v1.7.3 allowing to use the fast compression algorithm aka LZ4 fast which provides an "acceleration" parameter as a tradeoff between high compression ratio and high compression speed.
We want to use LZ4 fast in order to support compression in lustre and (mostly, based on that) investigate data reduction techniques in behalf of storage systems.
Also, it will be useful for other users of LZ4 compression, as with LZ4 fast it is possible to enable applications to use fast and/or high compression depending on the usecase.
For instance, ZRAM is offering a LZ4 backend and could benefit from an updated LZ4 in the kernel.
LZ4 homepage: http://www.lz4.org/
LZ4 source repository: https://github.com/lz4/lz4
Source version: 1.7.3
Benchmark (taken from [1], Core i5-4300U @1.9GHz):
----------------|--------------|----------------|---------- Compressor | Compression | Decompression | Ratio ----------------|--------------|----------------|---------- memcpy | 4200 MB/s | 4200 MB/s | 1.000 LZ4 fast 50 | 1080 MB/s | 2650 MB/s | 1.375 LZ4 fast 17 | 680 MB/s | 2220 MB/s | 1.607 LZ4 fast 5 | 475 MB/s | 1920 MB/s | 1.886 LZ4 default | 385 MB/s | 1850 MB/s | 2.101
[1] http://fastcompression.blogspot.de/2015/04/sampling-or-faster-lz4.html
[PATCHv2 1/4] lib: Update LZ4 compressor module based on LZ4 v1.7.2
[PATCHv2 2/4] lib: Update decompress_unlz4 wrapper to work with new LZ4 module [PATCHv2 3/4] crypto: Change lz4 modules to work with new LZ4 module
[PATCHv2 4/4] fs/pstore: fs/squashfs: Change LZ4 compressor functions to work with new LZ4 module
v2:
- Changed order of the patches since in the initial patchset the lz4.h was in the last patch but was referenced by the other ones
- Split lib/decompress_unlz4.c in an own patch
- Fixed errors reported by the buildbot
- Further refactorings
- Added more appropriate copyright note to include/linux/lz4.h
v3:
- Adjusted the code to satisfy kernel coding style (checkpatch.pl)
- Made sure the changes to LZ4 in Kernel (overflow checks etc.) are included in the new module (they are)
- Removed the second LZ4_compressBound function with related name but different return type
- Corrected version number (was LZ4 1.7.3)
PATCH v3 1/4 lib: Update LZ4 compressor module
This patch updates LZ4 kernel module to LZ4 v1.7.3 by Yann Collet. The kernel module is inspired by the previous work by Chanho Min. The updated LZ4 module will not break existing code since there were alias methods added to ensure backwards compatibility.
API changes:
New method LZ4_compress_fast which differs from the variant available in kernel by the new acceleration parameter,
allowing to trade compression ratio for more compression speed and vice versa.
LZ4_decompress_fast is the respective decompression method, featuring a very fast decoder (multiple GB/s per core), able to reach RAM speed in multi-core systems. The decompressor allows to decompress data compressed with LZ4 fast as well as the LZ4 HC (high compression) algorithm.
Also the useful functions LZ4_decompress_safe_partial LZ4_compress_destsize were added. The latter reverses the logic by trying to compress as much data as possible from source to dest while the former aims to decompress partial blocks of data.
A bunch of streaming functions were also added which allow compressig/decompressing data in multiple steps (so called "streaming mode").
The methods lz4_compress and lz4_decompress_unknownoutputsize are now known as LZ4_compress_default respectivley LZ4_decompress_safe. The old methods are still available for providing backwards compatibility.
--- include/linux/lz4.h | 581 +++++++++++++++++++++--- lib/lz4/lz4_compress.c | 1103 ++++++++++++++++++++++++++++++++-------------- lib/lz4/lz4_decompress.c | 694 ++++++++++++++++++----------- lib/lz4/lz4defs.h | 320 ++++++++------ lib/lz4/lz4hc_compress.c | 855 ++++++++++++++++++++++------------- 5 files changed, 2477 insertions(+), 1076 deletions(-)
include/linux/lz4.h +520 -61
@@ -1,87 +1,546 @@ -#ifndef __LZ4_H__ -#define __LZ4_H__ -/* -* LZ4 Kernel Interface +/* LZ4 Kernel Interface * * Copyright (C) 2013, LG Electronics, Kyungsik Lee <[email protected]> +* Copyright (C) 2016, Sven Schmidt <[email protected]> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. +* +* This file is based on the original header file +* for LZ4 - Fast LZ compression algorithm. +* +* LZ4 - Fast LZ compression algorithm +* Copyright (C) 2011-2016, Yann Collet. +* BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are +* met: +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above +* copyright notice, this list of conditions and the following disclaimer +* in the documentation and/or other materials provided with the +* distribution. +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* You can contact the author at : +* - LZ4 homepage : http://www.lz4.org +* - LZ4 source repository : https://github.com/lz4/lz4 +*/ + +#ifndef __LZ4_H__ +#define __LZ4_H__ + +#include <linux/types.h> +#include <linux/string.h> /* memset, memcpy */ + +/*-************************************************************************ +* CONSTANTS +**************************************************************************/ +/* +* LZ4_MEMORY_USAGE : +* Memory usage formula : N->2^N Bytes +* (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.) +* Increasing memory usage improves compression ratio +* Reduced memory usage can improve speed, due to cache effect +* Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache +*/ +#define LZ4_MEMORY_USAGE 10 + +#define LZ4_MAX_INPUT_SIZE 0x7E000000/* 2 113 929 216 bytes */ +#define LZ4_COMPRESSBOUND (isize) (\ + (unsigned int)(isize) > (unsigned int)LZ4_MAX_INPUT_SIZE \ + ? 0 \ + : (isize) + ((isize)/255) + 16) + +#define LZ4_ACCELERATION_DEFAULT 1 +#define LZ4_HASHLOG (LZ4_MEMORY_USAGE-2) +#define LZ4_HASHTABLESIZE (1 << LZ4_MEMORY_USAGE) +#define LZ4_HASH_SIZE_U32 (1 << LZ4_HASHLOG) + +#define LZ4HC_MIN_CLEVEL 3 +#define LZ4HC_DEFAULT_CLEVEL 9 +#define LZ4HC_MAX_CLEVEL 16 + +#define LZ4HC_DICTIONARY_LOGSIZE 16 +#define LZ4HC_MAXD (1<<LZ4HC_DICTIONARY_LOGSIZE) +#define LZ4HC_MAXD_MASK (LZ4HC_MAXD - 1) +#define LZ4HC_HASH_LOG (LZ4HC_DICTIONARY_LOGSIZE-1) +#define LZ4HC_HASHTABLESIZE (1 << LZ4HC_HASH_LOG) +#define LZ4HC_HASH_MASK (LZ4HC_HASHTABLESIZE - 1) + +/*-************************************************************************ +* STREAMING CONSTANTS AND STRUCTURES +**************************************************************************/ +#define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE-3)) + 4) +#define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U64 * sizeof(unsigned long long)) + +#define LZ4_STREAMHCSIZE 262192 +#define LZ4_STREAMHCSIZE_SIZET (262192 / sizeof(size_t)) + +#define LZ4_STREAMDECODESIZE_U64 4 +#define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * \ + sizeof(unsigned long long)) + +/* +* LZ4_stream_t : +* information structure to track an LZ4 stream. +*/ +typedef struct { +uint32_t hashTable[LZ4_HASH_SIZE_U32 ]; +uint32_t currentOffset; +uint32_t initCheck; +const uint8_t *dictionary; +uint8_t *bufferStart; +uint32_t dictSize; +}LZ4_stream_t_internal ; +typedef union { +unsigned long long table[LZ4_STREAMSIZE_U64 ]; +LZ4_stream_t_internal internal_donotuse; +}LZ4_stream_t ; + +/* +* LZ4_streamHC_t : +* information structure to track an LZ4HC stream. +*/ +typedef struct { +unsigned int hashTable[LZ4HC_HASHTABLESIZE ]; +unsigned short chainTable[LZ4HC_MAXD ]; +/* next block to continue on current prefix */ +const unsigned char *end; +/* All index relative to this position */ +const unsigned char *base; +/* alternate base for extDict */ +const unsigned char *dictBase; +/* below that point, need extDict */ +unsigned int dictLimit; +/* below that point, no more dict */ +unsigned int lowLimit; +/* index from which to continue dict update */ +unsigned int nextToUpdate; +unsigned int compressionLevel; +}LZ4HC_CCtx_internal ; +typedef union { +size_t table[LZ4_STREAMHCSIZE_SIZET ]; +LZ4HC_CCtx_internal internal_donotuse; +}LZ4_streamHC_t ; + +/* +* LZ4_streamDecode_t : +* information structure to track an LZ4 stream during decompression. +* init this structure using LZ4_setStreamDecode +* (or memset()) before first use */ -#define LZ4_MEM_COMPRESS (16384) -#define LZ4HC_MEM_COMPRESS (262144 + (2 * sizeof(unsigned char *))) +typedef struct { +const uint8_t *externalDict; +size_t extDictSize; +const uint8_t *prefixEnd; +size_t prefixSize; +}LZ4_streamDecode_t_internal ; +typedef union { +unsigned long long table[LZ4_STREAMDECODESIZE_U64 ]; +LZ4_streamDecode_t_internal internal_donotuse; +}LZ4_streamDecode_t ; + +/*-************************************************************************ +* SIZE OF STATE +**************************************************************************/ +#define LZ4_MEM_COMPRESS LZ4_STREAMSIZE +#define LZ4HC_MEM_COMPRESS LZ4_STREAMHCSIZE + +/*-************************************************************************ +* Compression Functions +**************************************************************************/ /* -* lz4_compressbound() -* Provides the maximum size that LZ4 may output in a "worst case" scenario -* (input data not compressible) +* LZ4_compressBound() : +* Provides the maximum size that LZ4 may output in a "worst case" scenario +* (input data not compressible) */ -static inline size_t lz4_compressbound (size_t isize) +static inline int LZ4_compressBound (size_t isize) { -return isize + (isize /255 ) +16 ; +return (int )LZ4_COMPRESSBOUND (isize); }/* +* LZ4_compress_default() +* Compresses 'sourceSize' bytes from buffer 'source' +* into already allocated 'dest' buffer of size 'maxOutputSize'. +* Compression is guaranteed to succeed if +* 'maxOutputSize' >= LZ4_compressBound(inputSize). +* It also runs faster, so it's a recommended setting. +* If the function cannot compress 'source' +* into a more limited 'dest' budget, +* compression stops *immediately*, +* and the function result is zero. +* As a consequence, 'dest' content is not valid. +* +* source : source address of the original data +* dest : output buffer address +* of the compressed data +* inputSize : Max supported value is +* LZ4_MAX_INPUT_SIZE +* maxOutputSize: full or partial size of buffer 'dest' +* (which must be already allocated) +* workmem : address of the working memory. +* This requires 'workmem' of size LZ4_MEM_COMPRESS. +* return : the number of bytes written into buffer 'dest' +* (necessarily <= maxOutputSize) or 0 if compression fails +*/ +int LZ4_compress_default (const char *source,char *dest,int inputSize, +int maxOutputSize,void *wrkmem); + +/* +* LZ4_compress_fast() : +* Same as LZ4_compress_default(), +* but allows to select an "acceleration" factor. +* The larger the acceleration value, +* the faster the algorithm, +* but also the lesser the compression. +* It's a trade-off. It can be fine tuned, +* with each successive value +* providing roughly +~3% to speed. +* An acceleration value of "1" +* is the same as regular LZ4_compress_default() +* Values <= 0 will be replaced by +* LZ4_ACCELERATION_DEFAULT, which is 1. +*/ +int LZ4_compress_fast (const char *source,char *dest,int inputSize, +int maxOutputSize,void *wrkmem,int acceleration); + +/* +* LZ4_compress_destSize() +* Reverse the logic, by compressing as much data as possible +* from 'source' buffer into already allocated buffer 'dest' +* of size 'targetDestSize'. +* This function either compresses the entire 'source' content into 'dest' +* if it's large enough, or fill 'dest' buffer completely with as much data as +* ossible from 'source'. +* +* source : source address of the original data +* dest : output buffer address of the compressed data +* inputSize : Max supported value is LZ4_MAX_INPUT_SIZE +* *sourceSizePtr: will be modified to indicate how many bytes where read +* from 'source' to fill 'dest'. +* New value is necessarily <= old value. +* workmem : address of the working memory. +* This requires 'workmem' of size LZ4_MEM_COMPRESS. +* return : Nb bytes written into 'dest' (necessarily <= targetDestSize) +or 0 if compression fails +*/ +int LZ4_compress_destSize (const char *source,char *dest,int *sourceSizePtr, +int targetDestSize,void *wrkmem); + +/* * lz4_compress() -* src : source address of the original data -* src_len : size of the original data -* dst : output buffer address of the compressed data -* This requires 'dst' of size LZ4_COMPRESSBOUND. -* dst_len : is the output size, which is returned after compress done -* workmem : address of the working memory. -* This requires 'workmem' of size LZ4_MEM_COMPRESS. -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer and workmem must be already allocated with +* src : source address of the original data +* src_len: size of the original data +* dst : output buffer address of the compressed data +* This requires 'dst' of size LZ4_COMPRESSBOUND. +* dst_len: is the output size, which is returned after compress done +* workmem: address of the working memory. +* This requires 'workmem' of size LZ4_MEM_COMPRESS. +* return : Success if return 0 +* Error if return (< 0) +* note : Destination buffer and workmem must be already allocated with * the defined size. */ -int lz4_compress (const unsigned char *src,size_t src_len, -unsigned char *dst,size_t *dst_len,void *wrkmem); - -/* -* lz4hc_compress() -* src : source address of the original data -* src_len : size of the original data -* dst : output buffer address of the compressed data -* This requires 'dst' of size LZ4_COMPRESSBOUND. -* dst_len : is the output size, which is returned after compress done -* workmem : address of the working memory. -* This requires 'workmem' of size LZ4HC_MEM_COMPRESS. -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer and workmem must be already allocated with -* the defined size. -*/ -int lz4hc_compress (const unsigned char *src,size_t src_len, -unsigned char *dst,size_t *dst_len,void *wrkmem); - -/* -* lz4_decompress() -* src : source address of the compressed data -* src_len : is the input size, whcih is returned after decompress done -* dest : output buffer address of the decompressed data -* actual_dest_len: is the size of uncompressed data, supposing it's known -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer must be already allocated. -* slightly faster than lz4_decompress_unknownoutputsize() +int lz4_compress (const unsigned char *src,size_t src_len,unsigned char *dst, +size_t *dst_len,void *wrkmem); + +/*-************************************************************************ +* Decompression Functions +**************************************************************************/ + +/* +* LZ4_decompress_fast() +* source : source address of the compressed data +* dst : output buffer address of the decompressed data +* originalSize: is the original and therefore uncompressed size +* return : the number of bytes read from the source buffer +* (in other words, the compressed size) +* If the source stream is detected malformed, the function will +* stop decoding and return a negative result. +* Destination buffer must be already allocated. +* Its size must be a minimum of 'originalSize' bytes. +* note : This function fully respect memory boundaries +* for properly formed compressed data. +* It is a bit faster than LZ4_decompress_safe(). +* However, it does not provide any protection against intentionally +* modified data stream (malicious input). +* Use this function in trusted environment only +* (data to decode comes from a trusted source). */ -int lz4_decompress (const unsigned char *src,size_t *src_len, -unsigned char *dest,size_t actual_dest_len); +int LZ4_decompress_fast (const char *source,char *dest,int originalSize); + +/* +* LZ4_decompress_safe() +* source : source address of the compressed data +* dest : output buffer address of the decompressed data +* compressedSize : is the precise full size of the compressed block. +* maxDecompressedSize: is the size of destination buffer, +* which must be already allocated. +* return : the number of bytes decompressed into destination buffer +* (necessarily <= maxDecompressedSize) +* If destination buffer is not large enough, decoding will stop +* and output an error code (<0). +* If the source stream is detected malformed, the function will +* stop decoding and return a negative result. +* This function is protected against buffer overflow exploits, +* including malicious data packets. It never writes outside +* output buffer, nor reads outside input buffer. +*/ +int LZ4_decompress_safe (const char *source,char *dest,int compressedSize, +int maxDecompressedSize); + +/* +* LZ4_decompress_safe_partial() : +* This function decompress a compressed block of size 'compressedSize' +* at position 'source' into destination buffer 'dest' +* of size 'maxDecompressedSize'. +* The function tries to stop decompressing operation as soon as +* 'targetOutputSize' has been reached, reducing decompression time. +* +* source : source address of the compressed data +* dest : output buffer address of the decompressed data +* compressedSize : is the precise full size of the compressed block. +* targetOutputSize : the decompression operation will try +* to stop as soon as 'targetOutputSize' +* has been reached +* maxDecompressedSize: is the size of destination buffer, +* which must be already allocated. +* return : the number of bytes decoded in the destination buffer +* (necessarily <= maxDecompressedSize) +* Note : this number can be < 'targetOutputSize' should the compressed +* block to decode be smaller. Always control how many bytes +* were decoded. If the source stream is detected malformed, +* the function will stop decoding and return a negative result. +* This function never writes outside of output buffer, +* and never reads outside of input buffer. +* It is therefore protected against malicious data packets +*/ +int LZ4_decompress_safe_partial (const char *source,char *dest, +int compressedSize,int targetOutputSize,int maxDecompressedSize); +/* -* lz4_decompress_unknownoutputsize() +* lz4_decompress_unknownoutputsize() : * src : source address of the compressed data * src_len : is the input size, therefore the compressed size -* dest : output buffer address of the decompressed data +* dest : output buffer address of the decompressed data * dest_len: is the max size of the destination buffer, which is -* returned with actual size of decompressed data after -* decompress done -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer must be already allocated. +* returned with actual size of decompressed data after +* decompress done +* return: Success if return 0 +* Error if return (< 0) +* note: Destination buffer must be already allocated. */ int lz4_decompress_unknownoutputsize (const unsigned char *src,size_t src_len, -unsigned char *dest,size_t *dest_len); +unsigned char *dest,size_t *dest_len); + +/* +* lz4_decompress() : +* src : source address of the compressed data +* src_len : is the input size, +* which is returned after decompress done +* dest : output buffer address of the decompressed data +* actual_dest_len: is the size of uncompressed data, supposing it's known +* return: Success if return 0 +* Error if return (< 0) +* note : Destination buffer must be already allocated. +* slightly faster than lz4_decompress_unknownoutputsize() +*/ +int lz4_decompress (const unsigned char *src,size_t *src_len, +unsigned char *dest,size_t actual_dest_len); + +/*-************************************************************************ +* LZ4 HC Compression +**************************************************************************/ + +/*! LZ4_compress_HC() : +* Compress data from `src` into `dst`, using the more powerful +* but slower "HC" algorithm. dst` must be already allocated. +* Compression is guaranteed to succeed if +* `dstCapacity >= LZ4_compressBound(srcSize) +* Max supported `srcSize` value is LZ4_MAX_INPUT_SIZE +* +* src : source address of the original data +* dst : output buffer address of the compressed data +* srcSize : Max supported value is LZ4_MAX_INPUT_SIZE +* dstCapacity : full or partial size of buffer 'dst' +* (which must be already allocated) +* compressionLevel: Recommended values are between 4 and 9, although any +* value between 1 and LZ4HC_MAX_CLEVEL will work. +* Values >LZ4HC_MAX_CLEVEL behave the same as 16. +* wrkmem : address of the working memory. +* This requires 'wrkmem' of size LZ4HC_MEM_COMPRESS. +* return : the number of bytes written into 'dst' +* or 0 if compression fails. +*/ +int LZ4_compress_HC (const char *src,char *dst,int srcSize,int dstCapacity, +int compressionLevel,void *wrkmem); + +/* +* lz4hc_compress() +* src : source address of the original data +* src_len: size of the original data +* dst : output buffer address of the compressed data +* This requires 'dst' of size LZ4_COMPRESSBOUND. +* dst_len: is the output size, which is returned after compress done +* workmem: address of the working memory. +* This requires 'workmem' of size LZ4HC_MEM_COMPRESS. +* return : Success if return 0 +* Error if return (< 0) +* note : Destination buffer and workmem must be already allocated with +* the defined size. +*/ +int lz4hc_compress (const unsigned char *src,size_t src_len,unsigned char *dst, +size_t *dst_len,void *wrkmem); + +/* +* These functions compress data in successive blocks of any size, +* using previous blocks as dictionary. One key assumption is that previous +* blocks (up to 64 KB) remain read-accessible while +* compressing next blocks. +* There is an exception for ring buffers, which can be smaller than 64 KB. +* Ring buffers scenario is automatically detected and handled by +* LZ4_compress_HC_continue(). +* Before starting compression, state must be properly initialized, +* using LZ4_resetStreamHC(). +* A first "fictional block" can then be designated as initial dictionary, +* using LZ4_loadDictHC() (Optional). +* Then, use LZ4_compress_HC_continue() to compress each successive block. +* Previous memory blocks (including initial dictionary when present) must +* remain accessible and unmodified during compression. +* 'dst' buffer should be sized to handle worst case scenarios, using +* LZ4_compressBound(), to ensure operation success. +* If, for any reason, previous data blocks can't be preserved unmodified +* in memory during next compression block, +* you must save it to a safer memory space, using LZ4_saveDictHC(). +* Return value of LZ4_saveDictHC() is the size of dictionary +* effectively saved into 'safeBuffer'. +*/ +void LZ4_resetStreamHC (LZ4_streamHC_t *streamHCPtr,int compressionLevel); +int LZ4_loadDictHC (LZ4_streamHC_t *streamHCPtr,const char *dictionary, +int dictSize); +int LZ4_compress_HC_continue (LZ4_streamHC_t *streamHCPtr,const char *src, +char *dst,int srcSize,int maxDstSize); +int LZ4_saveDictHC (LZ4_streamHC_t *streamHCPtr,char *safeBuffer, +int maxDictSize); + +/*-********************************************* +* Streaming Compression Functions +***********************************************/ +/* +* LZ4_resetStream() : +* An LZ4_stream_t structure can be allocated once +* and re-used multiple times. +* Use this function to init an allocated `LZ4_stream_t` structure +* and start a new compression. +*/ +void LZ4_resetStream (LZ4_stream_t *LZ4_stream); + +/* +* LZ4_loadDict() : +* Use this function to load a static dictionary into LZ4_stream. +* Any previous data will be forgotten, only 'dictionary' +* will remain in memory. +* Loading a size of 0 is allowed. +* Return : dictionary size, in bytes (necessarily <= 64 KB) +*/ +int LZ4_loadDict (LZ4_stream_t *streamPtr,const char *dictionary, +int dictSize); + +/* +* LZ4_compress_fast_continue() : +* Compress buffer content 'src', +* using data from previously compressed blocks +* as dictionary to improve compression ratio. +* Important : Previous data blocks are assumed to still +* be present and unmodified ! +* 'dst' buffer must be already allocated. +* If maxDstSize >= LZ4_compressBound(srcSize), +* compression is guaranteed to succeed, and runs faster. +* If not, and if compressed data cannot fit into 'dst' buffer size, +* compression stops, and function returns a zero. +*/ +int LZ4_compress_fast_continue (LZ4_stream_t *streamPtr,const char *src, +char *dst,int srcSize,int maxDstSize,int acceleration); + +/* +* LZ4_saveDict() : +* If previously compressed data block is not guaranteed +* to remain available at its memory location, +* save it into a safer place (char *safeBuffer). +* Note : you don't need to call LZ4_loadDict() afterwards, +* dictionary is immediately usable, you can therefore call +* LZ4_compress_fast_continue(). +* Return : saved dictionary size in bytes (necessarily <= dictSize), +* or 0 if error. +*/ +int LZ4_saveDict (LZ4_stream_t *streamPtr,char *safeBuffer,int dictSize); + +/* +* LZ4_setStreamDecode() : +* Use this function to instruct where to find the dictionary. +* Setting a size of 0 is allowed (same effect as reset). +* @return : 1 if OK, 0 if error +*/ +int LZ4_setStreamDecode (LZ4_streamDecode_t *LZ4_streamDecode, +const char *dictionary,int dictSize); + +/* +* LZ4_decompress_*_continue() : +* These decoding functions allow decompression of multiple blocks +* in "streaming" mode. +* Previously decoded blocks *must* remain available at the memory position +* where they were decoded (up to 64 KB) +* In the case of a ring buffers, decoding buffer must be either : +* - Exactly same size as encoding buffer, with same update rule +* (block boundaries at same positions) In which case, +* the decoding & encoding ring buffer can have any size, +* including very small ones ( < 64 KB). +* - Larger than encoding buffer, by a minimum of maxBlockSize more bytes. +* maxBlockSize is implementation dependent. +* It's the maximum size you intend to compress into a single block. +* In which case, encoding and decoding buffers do not need +* to be synchronized, and encoding ring buffer can have any size, +* including small ones ( < 64 KB). +* - _At least_ 64 KB + 8 bytes + maxBlockSize. +* In which case, encoding and decoding buffers do not need to be +* synchronized, and encoding ring buffer can have any size, +* including larger than decoding buffer. W +* Whenever these conditions are not possible, save the last 64KB of decoded +* data into a safe buffer, and indicate where it is saved +* using LZ4_setStreamDecode() +*/ +int LZ4_decompress_safe_continue (LZ4_streamDecode_t *LZ4_streamDecode, +const char *source,char *dest,int compressedSize, +int maxDecompressedSize); +int LZ4_decompress_fast_continue (LZ4_streamDecode_t *LZ4_streamDecode, +const char *source,char *dest,int originalSize); + +/* +* LZ4_decompress_*_usingDict() : +* These decoding functions work the same as +* a combination of LZ4_setStreamDecode() followed by +* LZ4_decompress_*_continue() +* They are stand-alone, and don't need an LZ4_streamDecode_t structure. +*/ +int LZ4_decompress_safe_usingDict (const char *source,char *dest, +int compressedSize,int maxDecompressedSize,const char *dictStart, +int dictSize); +int LZ4_decompress_fast_usingDict (const char *source,char *dest, +int originalSize,const char *dictStart,int dictSize); +#endif
lib/lz4/lz4_compress.c +778 -325
1,214 lines not on this page · raw message
lib/lz4/lz4_decompress.c +440 -254
778 lines not on this page · raw message
lib/lz4/lz4defs.h +191 -129
348 lines not on this page · raw message
lib/lz4/lz4hc_compress.c +548 -307
53 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.1.4
Part of the diff is not shown here. View the raw message.
kbuild test robot
build botRe: [PATCH 1/4] lib: Update LZ4 compressor module
url: https://github.com/0day-ci/linux/commits/Sven-Schmidt/Update-LZ4-compressor-module/20170121-231418 config: i386-randconfig-r0-201703 (attached as .config) compiler: gcc-5 (Debian 5.4.1-2) 5.4.1…
Hi Sven,
[auto build test ERROR on linus/master]
[also build test ERROR on v4.10-rc4 next-20170120]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Sven-Schmidt/Update-LZ4-compressor-module/20170121-231418
config: i386-randconfig-r0-201703 (attached as .config)
compiler: gcc-5 (Debian 5.4.1-2) 5.4.1 20160904 reproduce:
# save the attached .config to linux build tree
make ARCH=i386 Note: the linux-review/Sven-Schmidt/Update-LZ4-compressor-module/20170121-231418 HEAD 0472409e2a1c442b51502961aa6d83b866218953 builds fine.
It only hurts bisectibility.
All errors (new ones prefixed by >>):
fs/pstore/platform.c: In function 'allocate_lz4':
>> fs/pstore/platform.c:369:20: error: implicit declaration of function 'lz4_compressbound' [-Werror=implicit-function-declaration]
big_oops_buf_sz = lz4_compressbound(psinfo->bufsize);
^cc1: some warnings being treated as errors
vim +/lz4_compressbound +369 fs/pstore/platform.c
8cfc8ddc Geliang Tang 2016-02-18 363
8cfc8ddc Geliang Tang 2016-02-18 364 return outlen;
8cfc8ddc Geliang Tang 2016-02-18 365 }
8cfc8ddc Geliang Tang 2016-02-18 366
8cfc8ddc Geliang Tang 2016-02-18 367 static void allocate_lz4(void)
8cfc8ddc Geliang Tang 2016-02-18 368 {
8cfc8ddc Geliang Tang 2016-02-18 @369 big_oops_buf_sz = lz4_compressbound(psinfo->bufsize);
8cfc8ddc Geliang Tang 2016-02-18 370 big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
8cfc8ddc Geliang Tang 2016-02-18 371 if (big_oops_buf) {
8cfc8ddc Geliang Tang 2016-02-18 372 workspace = kmalloc(LZ4_MEM_COMPRESS, GFP_KERNEL);:::::: The code at line 369 was first introduced by commit
:::::: 8cfc8ddc99df9509a46043b14af81f5c6a223eab pstore: add lzo/lz4 compression support
:::::: TO: Geliang Tang <[email protected]>
:::::: CC: Kees Cook <[email protected]>
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
kbuild test robot
build botRe: [PATCH 1/4] lib: Update LZ4 compressor module
In reply to Sven Schmidturl: https://github.com/0day-ci/linux/commits/Sven-Schmidt/Update-LZ4-compressor-module/20170121-231418 config: x86_64-lkp (attached as .config) compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901…
Hi Sven,
[auto build test ERROR on linus/master]
[also build test ERROR on v4.10-rc4 next-20170120]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Sven-Schmidt/Update-LZ4-compressor-module/20170121-231418
config: x86_64-lkp (attached as .config)
compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901
reproduce:
# save the attached .config to linux build tree
make ARCH=x86_64 Note: the linux-review/Sven-Schmidt/Update-LZ4-compressor-module/20170121-231418 HEAD 0472409e2a1c442b51502961aa6d83b866218953 builds fine.
It only hurts bisectibility.
All errors (new ones prefixed by >>):
In file included from lib/decompress_unlz4.c:19:0:
lib/decompress_unlz4.c: In function 'unlz4':
>> lib/decompress_unlz4.c:75:22: error: implicit declaration of function 'lz4_compressbound' [-Werror=implicit-function-declaration]
inp = large_malloc(lz4_compressbound(uncomp_chunksize));
^
include/linux/decompress/mm.h:83:33: note: in definition of macro 'large_malloc'
#define large_malloc(a) vmalloc(a)
^cc1: some warnings being treated as errors
vim +/lz4_compressbound +75 lib/decompress_unlz4.c
e76e1fdf Kyungsik Lee 2013-07-08 13 #include "lz4/lz4_decompress.c"
e76e1fdf Kyungsik Lee 2013-07-08 14 #else
e76e1fdf Kyungsik Lee 2013-07-08 15 #include <linux/decompress/unlz4.h>
e76e1fdf Kyungsik Lee 2013-07-08 16 #endif
e76e1fdf Kyungsik Lee 2013-07-08 17 #include <linux/types.h>
e76e1fdf Kyungsik Lee 2013-07-08 18 #include <linux/lz4.h>
e76e1fdf Kyungsik Lee 2013-07-08 @19 #include <linux/decompress/mm.h>
e76e1fdf Kyungsik Lee 2013-07-08 20 #include <linux/compiler.h>
e76e1fdf Kyungsik Lee 2013-07-08 21
e76e1fdf Kyungsik Lee 2013-07-08 22 #include <asm/unaligned.h>
e76e1fdf Kyungsik Lee 2013-07-08 23
e76e1fdf Kyungsik Lee 2013-07-08 24 /*
e76e1fdf Kyungsik Lee 2013-07-08 25 * Note: Uncompressed chunk size is used in the compressor side
e76e1fdf Kyungsik Lee 2013-07-08 26 * (userspace side for compression).
e76e1fdf Kyungsik Lee 2013-07-08 27 * It is hardcoded because there is not proper way to extract it
e76e1fdf Kyungsik Lee 2013-07-08 28 * from the binary stream which is generated by the preliminary
e76e1fdf Kyungsik Lee 2013-07-08 29 * version of LZ4 tool so far.
e76e1fdf Kyungsik Lee 2013-07-08 30 */
e76e1fdf Kyungsik Lee 2013-07-08 31 #define LZ4_DEFAULT_UNCOMPRESSED_CHUNK_SIZE (8 << 20)
e76e1fdf Kyungsik Lee 2013-07-08 32 #define ARCHIVE_MAGICNUMBER 0x184C2102
e76e1fdf Kyungsik Lee 2013-07-08 33
d97b07c5 Yinghai Lu 2014-08-08 34 STATIC inline int INIT unlz4(u8 *input, long in_len,
d97b07c5 Yinghai Lu 2014-08-08 35 long (*fill)(void *, unsigned long),
d97b07c5 Yinghai Lu 2014-08-08 36 long (*flush)(void *, unsigned long),
d97b07c5 Yinghai Lu 2014-08-08 37 u8 *output, long *posp,
e76e1fdf Kyungsik Lee 2013-07-08 38 void (*error) (char *x))
e76e1fdf Kyungsik Lee 2013-07-08 39 {
e76e1fdf Kyungsik Lee 2013-07-08 40 int ret = -1;
e76e1fdf Kyungsik Lee 2013-07-08 41 size_t chunksize = 0;
e76e1fdf Kyungsik Lee 2013-07-08 42 size_t uncomp_chunksize = LZ4_DEFAULT_UNCOMPRESSED_CHUNK_SIZE;
e76e1fdf Kyungsik Lee 2013-07-08 43 u8 *inp;
e76e1fdf Kyungsik Lee 2013-07-08 44 u8 *inp_start;
e76e1fdf Kyungsik Lee 2013-07-08 45 u8 *outp;
d97b07c5 Yinghai Lu 2014-08-08 46 long size = in_len;
e76e1fdf Kyungsik Lee 2013-07-08 47 #ifdef PREBOOT
e76e1fdf Kyungsik Lee 2013-07-08 48 size_t out_len = get_unaligned_le32(input + in_len);
e76e1fdf Kyungsik Lee 2013-07-08 49 #endif
e76e1fdf Kyungsik Lee 2013-07-08 50 size_t dest_len;
e76e1fdf Kyungsik Lee 2013-07-08 51
e76e1fdf Kyungsik Lee 2013-07-08 52
e76e1fdf Kyungsik Lee 2013-07-08 53 if (output) {
e76e1fdf Kyungsik Lee 2013-07-08 54 outp = output;
e76e1fdf Kyungsik Lee 2013-07-08 55 } else if (!flush) {
e76e1fdf Kyungsik Lee 2013-07-08 56 error("NULL output pointer and no flush function provided");
e76e1fdf Kyungsik Lee 2013-07-08 57 goto exit_0;
e76e1fdf Kyungsik Lee 2013-07-08 58 } else {
e76e1fdf Kyungsik Lee 2013-07-08 59 outp = large_malloc(uncomp_chunksize);
e76e1fdf Kyungsik Lee 2013-07-08 60 if (!outp) {
e76e1fdf Kyungsik Lee 2013-07-08 61 error("Could not allocate output buffer");
e76e1fdf Kyungsik Lee 2013-07-08 62 goto exit_0;
e76e1fdf Kyungsik Lee 2013-07-08 63 }
e76e1fdf Kyungsik Lee 2013-07-08 64 }
e76e1fdf Kyungsik Lee 2013-07-08 65
e76e1fdf Kyungsik Lee 2013-07-08 66 if (input && fill) {
e76e1fdf Kyungsik Lee 2013-07-08 67 error("Both input pointer and fill function provided,");
e76e1fdf Kyungsik Lee 2013-07-08 68 goto exit_1;
e76e1fdf Kyungsik Lee 2013-07-08 69 } else if (input) {
e76e1fdf Kyungsik Lee 2013-07-08 70 inp = input;
e76e1fdf Kyungsik Lee 2013-07-08 71 } else if (!fill) {
e76e1fdf Kyungsik Lee 2013-07-08 72 error("NULL input pointer and missing fill function");
e76e1fdf Kyungsik Lee 2013-07-08 73 goto exit_1;
e76e1fdf Kyungsik Lee 2013-07-08 74 } else {
e76e1fdf Kyungsik Lee 2013-07-08 @75 inp = large_malloc(lz4_compressbound(uncomp_chunksize));
e76e1fdf Kyungsik Lee 2013-07-08 76 if (!inp) {
e76e1fdf Kyungsik Lee 2013-07-08 77 error("Could not allocate input buffer");
e76e1fdf Kyungsik Lee 2013-07-08 78 goto exit_1;:::::: The code at line 75 was first introduced by commit
:::::: e76e1fdfa8f8dc1ea6699923cf5d92b5bee9c936 lib: add support for LZ4-compressed kernel
:::::: TO: Kyungsik Lee <[email protected]> :::::: CC: Linus Torvalds <[email protected]>
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
kbuild test robot
build botRe: [PATCH 1/4] lib: Update LZ4 compressor module
In reply to Sven Schmidturl: https://github.com/0day-ci/linux/commits/Sven-Schmidt/Update-LZ4-compressor-module/20170121-231418 reproduce:
Hi Sven,
[auto build test WARNING on linus/master]
[also build test WARNING on v4.10-rc4 next-20170120]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Sven-Schmidt/Update-LZ4-compressor-module/20170121-231418
reproduce:
# apt-get install sparse
make ARCH=x86_64 allmodconfig
make C=1 CF=-D__CHECK_ENDIAN__sparse warnings: (new ones prefixed by >>)
include/linux/compiler.h:253:8: sparse: attribute 'no_sanitize_address': unknown attribute
>> lib/lz4/lz4_decompress.c:521:21: sparse: incompatible types for operation (>)
lib/lz4/lz4_decompress.c:521:21: left side has type unsigned long [usertype] *src_len
lib/lz4/lz4_decompress.c:521:21: right side has type int
vim +521 lib/lz4/lz4_decompress.c
505 return -1;
506 }
507 EXPORT_SYMBOL(lz4_decompress_unknownoutputsize);
508
509 int lz4_decompress(const unsigned char *src, size_t *src_len,
510 unsigned char *dest, size_t actual_dest_len) {
511 *src_len = LZ4_decompress_fast(src, dest, (int)actual_dest_len);
512
513 /*
514 * Prior lz4_decompress will return
515 * 0 for success and a negative result for error
516 * new LZ4_decompress_fast returns
517 * - the length of data read on success
518 * - and also a negative result on error
519 * meaning when result > 0, we just return 0 here
520 */
> 521 if (src_len > 0)
522 return 0;
523 else
524 return -1;
525 }
526 EXPORT_SYMBOL(lz4_decompress);
527
528 MODULE_LICENSE("Dual BSD/GPL");
529 MODULE_DESCRIPTION("LZ4 decompressor");
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Re: [PATCH 1/4] lib: Update LZ4 compressor module
In reply to Sven SchmidtQuoted 26 lines · Sven Schmidt
This patch updates LZ4 kernel module to LZ4 v1.7.3 by Yann Collet. The kernel module is inspired by the previous work by Chanho Min. The updated LZ4 module will not break existing code since there were alias methods added to ensure backwards compatibility.
API changes:
New method
LZ4_compress_fastwhich differs from the variant available in kernel by the new acceleration parameter,
allowing to trade compression ratio for more compression speed and vice versa.
LZ4_decompress_fastis the respective decompression method, featuring a very fast decoder (multiple GB/s per core), able to reach RAM speed in multi-core systems. The decompressor allows to decompress data compressed with LZ4 fast as well as the LZ4 HC (high compression) algorithm.Also the useful functions
LZ4_decompress_safe_partialLZ4_compress_destsizewere added. The latter reverses the logic by trying to compress as much data as possible from source to dest while the former aims to decompress partial blocks of data.A bunch of streaming functions were also added which allow compressig/decompressing data in multiple steps (so called "streaming mode").
Quoted 5 lines
The methods
lz4_compressandlz4_decompress_unknownoutputsizeare now known asLZ4_compress_defaultrespectivleyLZ4_decompress_safe. The old methods are still available for providing backwards compatibility.Signed-off-by Sven Schmidt <[email protected]>
Please fix up so that it doesn't break the build as reported by the kbuild tool...
thanks,
greg k-h
PATCH v3 2/4 lib/decompress_unlz4: Change module to work with new LZ4 module version
In reply to Sven SchmidtThis patch updates the unlz4 wrapper to work with the updated LZ4 kernel module version.
--- lib/decompress_unlz4.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
lib/decompress_unlz4.c +8 -5
@@ -72,7 +72,7 @@ STATIC inline int INIT unlz4(u8 *input, long in_len,error ("NULL input pointer and missing fill function" ); goto exit_1; }else { - inp =large_malloc (lz4_compressbound (uncomp_chunksize)); + inp =large_malloc (LZ4_compressBound (uncomp_chunksize));if (!inp) {error ("Could not allocate input buffer" ); goto exit_1;
@@ -136,7 +136,7 @@ STATIC inline int INIT unlz4(u8 *input, long in_len, inp +=4 ; size -=4 ; }else { -if (chunksize >lz4_compressbound (uncomp_chunksize)) { +if (chunksize >LZ4_compressBound (uncomp_chunksize)) {error ("chunk length is longer than allocated" ); goto exit_2; }
@@ -152,11 +152,14 @@ STATIC inline int INIT unlz4(u8 *input, long in_len, out_len -= dest_len; }else dest_len = out_len; - ret =lz4_decompress (inp, &chunksize, outp, dest_len); + + ret =LZ4_decompress_fast (inp, outp, dest_len); + chunksize = ret;#else dest_len = uncomp_chunksize; - ret =lz4_decompress_unknownoutputsize (inp, chunksize, outp, - &dest_len); + + ret =LZ4_decompress_safe (inp, outp, chunksize, dest_len); + dest_len = ret;#endif if (ret <0 ) {error ("Decoding failed" );
Signature · 1 line
2.1.4
PATCH v3 3/4 crypto: Change LZ4 modules to work with new LZ4 module version
In reply to Sven SchmidtThis patch updates the crypto modules using LZ4 compression to work with the new LZ4 module version.
--- crypto/lz4.c | 21 ++++++++------------- crypto/lz4hc.c | 21 ++++++++------------- 2 files changed, 16 insertions(+), 26 deletions(-)
crypto/lz4.c +8 -13
@@ -66,15 +66,13 @@ static void lz4_exit(struct crypto_tfm *tfm)static int __lz4_compress_crypto (const u8 *src,unsigned int slen,u8 *dst,unsigned int *dlen,void *ctx) { -size_t tmp_len = *dlen; -int err; +int out_len =LZ4_compress_default (src, dst, + slen, (int )((size_t )dlen), ctx); - err =lz4_compress (src, slen, dst, &tmp_len, ctx); - -if (err <0 ) +if (!out_len)return -EINVAL ; - *dlen = tmp_len; + *dlen = out_len;return 0 ; }
@@ -96,16 +94,13 @@ static int lz4_compress_crypto(struct crypto_tfm *tfm, const u8 *src,static int __lz4_decompress_crypto (const u8 *src,unsigned int slen,u8 *dst,unsigned int *dlen,void *ctx) { -int err; -size_t tmp_len = *dlen; -size_t __slen = slen; +int out_len =LZ4_decompress_safe (src, dst, slen, (int )((size_t )dlen)); - err =lz4_decompress_unknownoutputsize (src, __slen, dst, &tmp_len); -if (err <0 ) +if (out_len <0 )return -EINVAL ; - *dlen = tmp_len; -return err; + *dlen = out_len; +return out_len; } static int lz4_sdecompress(struct crypto_scomp *tfm, const u8 *src,
crypto/lz4hc.c +8 -13
@@ -65,15 +65,13 @@ static void lz4hc_exit(struct crypto_tfm *tfm)static int __lz4hc_compress_crypto (const u8 *src,unsigned int slen,u8 *dst,unsigned int *dlen,void *ctx) { -size_t tmp_len = *dlen; -int err; +int out_len =LZ4_compress_HC (src, dst, slen, + (int )((size_t )dlen),LZ4HC_DEFAULT_CLEVEL , ctx); - err =lz4hc_compress (src, slen, dst, &tmp_len, ctx); - -if (err <0 ) +if (out_len ==0 )return -EINVAL ; - *dlen = tmp_len; + *dlen = out_len;return 0 ; }
@@ -97,16 +95,13 @@ static int lz4hc_compress_crypto(struct crypto_tfm *tfm, const u8 *src,static int __lz4hc_decompress_crypto (const u8 *src,unsigned int slen,u8 *dst,unsigned int *dlen,void *ctx) { -int err; -size_t tmp_len = *dlen; -size_t __slen = slen; +int out_len =LZ4_decompress_safe (src, dst, slen, (int )((size_t )dlen)); - err =lz4_decompress_unknownoutputsize (src, __slen, dst, &tmp_len); -if (err <0 ) +if (out_len <0 )return -EINVAL ; - *dlen = tmp_len; -return err; + *dlen = out_len; +return out_len; } static int lz4hc_sdecompress(struct crypto_scomp *tfm, const u8 *src,
Signature · 1 line
2.1.4
PATCH v3 4/4 fs/pstore: fs/squashfs: Change usage of LZ4 to work with new LZ4 version
In reply to Sven SchmidtThis patch updates fs/pstore and fs/squashfs to use the updated functions from the new LZ4 module.
--- fs/pstore/platform.c | 14 +++++++------- fs/squashfs/lz4_wrapper.c | 12 ++++++------ 2 files changed, 13 insertions(+), 13 deletions(-)
fs/pstore/platform.c +7 -7
@@ -342,31 +342,31 @@ static int compress_lz4(const void *in, void *out, size_t inlen, size_t outlen) {int ret; - ret =lz4_compress (in, inlen, out, &outlen, workspace); -if (ret) { + ret =LZ4_compress_default (in, out, inlen, outlen, workspace); +if (!ret) {pr_err ("lz4_compress error, ret = %d!\n" , ret);return -EIO ; } -return outlen; +return ret; }static int decompress_lz4 (void *in,void *out,size_t inlen,size_t outlen) {int ret; - ret =lz4_decompress_unknownoutputsize (in, inlen, out, &outlen); -if (ret) { + ret =LZ4_decompress_safe (in, out, inlen, outlen); +if (ret <0 ) {pr_err ("lz4_decompress error, ret = %d!\n" , ret);return -EIO ; } -return outlen; +return ret; }static void allocate_lz4 (void ) { - big_oops_buf_sz =lz4_compressbound (psinfo->bufsize); + big_oops_buf_sz =LZ4_compressBound (psinfo->bufsize); big_oops_buf =kmalloc (big_oops_buf_sz,GFP_KERNEL );if (big_oops_buf) { workspace =kmalloc (LZ4_MEM_COMPRESS ,GFP_KERNEL );
fs/squashfs/lz4_wrapper.c +6 -6
@@ -97,7 +97,6 @@ static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm,struct squashfs_lz4 *stream = strm;void *buff = stream->input, *data;int avail, i, bytes = length, res; -size_t dest_len = output->length;for (i =0 ; i < b; i++) { avail =min (bytes, msblk->devblksize - offset);
@@ -108,12 +107,13 @@ static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm,put_bh (bh[i]); } - res =lz4_decompress_unknownoutputsize (stream->input, length, - stream->output, &dest_len); -if (res) + res =LZ4_decompress_safe (stream->input, stream->output, + length, output->length); + +if (res <0 )return -EIO ; - bytes = dest_len; + bytes = res; data =squashfs_first_page (output); buff = stream->output;while (data) {
@@ -128,7 +128,7 @@ static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm, }squashfs_finish_page (output); -return dest_len; +return res; }const struct squashfs_decompressor squashfs_lz4_comp_ops = {
Signature · 1 line
2.1.4
v4 cover Update LZ4 compressor module
In reply to Sven SchmidtThis patchset is for updating the LZ4 compression module to a version based on LZ4 v1.7.3 allowing to use the fast compression algorithm aka LZ4 fast which provides an "acceleration" parameter as a tradeoff between high compression ratio and high compression speed.
We want to use LZ4 fast in order to support compression in lustre and (mostly, based on that) investigate data reduction techniques in behalf of storage systems.
Also, it will be useful for other users of LZ4 compression, as with LZ4 fast it is possible to enable applications to use fast and/or high compression depending on the usecase.
For instance, ZRAM is offering a LZ4 backend and could benefit from an updated LZ4 in the kernel.
LZ4 homepage: http://www.lz4.org/
LZ4 source repository: https://github.com/lz4/lz4
Source version: 1.7.3
Benchmark (taken from [1], Core i5-4300U @1.9GHz):
----------------|--------------|----------------|---------- Compressor | Compression | Decompression | Ratio ----------------|--------------|----------------|---------- memcpy | 4200 MB/s | 4200 MB/s | 1.000 LZ4 fast 50 | 1080 MB/s | 2650 MB/s | 1.375 LZ4 fast 17 | 680 MB/s | 2220 MB/s | 1.607 LZ4 fast 5 | 475 MB/s | 1920 MB/s | 1.886 LZ4 default | 385 MB/s | 1850 MB/s | 2.101
[1] http://fastcompression.blogspot.de/2015/04/sampling-or-faster-lz4.html
fs/pstore: fs/squashfs: Change usage of LZ4 to work with new LZ4 version
[PATCH 1/4] lib: Update LZ4 compressor module
[PATCH 2/4] lib/decompress_unlz4: Change module to work with new LZ4 module version [PATCH 3/4] crypto: Change LZ4 modules to work with new LZ4 module version [PATCH 4/4] fs/pstore: fs/squashfs: Change usage of LZ4 to work with new LZ4 version
v2:
- Changed order of the patches since in the initial patchset the lz4.h was in the last patch but was referenced by the other ones
- Split lib/decompress_unlz4.c in an own patch
- Fixed errors reported by the buildbot
- Further refactorings
- Added more appropriate copyright note to include/linux/lz4.h
v3:
- Adjusted the code to satisfy kernel coding style (checkpatch.pl)
- Made sure the changes to LZ4 in Kernel (overflow checks etc.) are included in the new module (they are)
- Removed the second LZ4_compressBound function with related name but different return type
- Corrected version number (was LZ4 1.7.3)
- Added missing LZ4 streaming functions
v4:
- Fixed kbuild errors
- Re-added lz4_compressbound as alias for LZ4_compressBound to ensure backwards compatibility
- Wrapped LZ4_hash5 with check for LZ4_ARCH64 since it is only used there and triggers an unused function warning when false
PATCH v4 1/4 lib: Update LZ4 compressor module
This patch updates LZ4 kernel module to LZ4 v1.7.3 by Yann Collet. The kernel module is inspired by the previous work by Chanho Min. The updated LZ4 module will not break existing code since there were alias methods added to ensure backwards compatibility.
API changes:
New method LZ4_compress_fast which differs from the variant available in kernel by the new acceleration parameter,
allowing to trade compression ratio for more compression speed and vice versa.
LZ4_decompress_fast is the respective decompression method, featuring a very fast decoder (multiple GB/s per core), able to reach RAM speed in multi-core systems. The decompressor allows to decompress data compressed with LZ4 fast as well as the LZ4 HC (high compression) algorithm.
Also the useful functions LZ4_decompress_safe_partial LZ4_compress_destsize were added. The latter reverses the logic by trying to compress as much data as possible from source to dest while the former aims to decompress partial blocks of data.
A bunch of streaming functions were also added which allow compressig/decompressing data in multiple steps (so called "streaming mode").
The methods lz4_compress and lz4_decompress_unknownoutputsize are now known as LZ4_compress_default respectivley LZ4_decompress_safe. The old methods are still available for providing backwards compatibility.
--- include/linux/lz4.h | 589 ++++++++++++++++++++++--- lib/lz4/lz4_compress.c | 1103 ++++++++++++++++++++++++++++++++-------------- lib/lz4/lz4_decompress.c | 694 ++++++++++++++++++----------- lib/lz4/lz4defs.h | 320 ++++++++------ lib/lz4/lz4hc_compress.c | 855 ++++++++++++++++++++++------------- 5 files changed, 2486 insertions(+), 1075 deletions(-)
include/linux/lz4.h +528 -61
@@ -1,87 +1,554 @@ -#ifndef __LZ4_H__ -#define __LZ4_H__ -/* -* LZ4 Kernel Interface +/* LZ4 Kernel Interface * * Copyright (C) 2013, LG Electronics, Kyungsik Lee <[email protected]> +* Copyright (C) 2016, Sven Schmidt <[email protected]> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. +* +* This file is based on the original header file +* for LZ4 - Fast LZ compression algorithm. +* +* LZ4 - Fast LZ compression algorithm +* Copyright (C) 2011-2016, Yann Collet. +* BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are +* met: +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above +* copyright notice, this list of conditions and the following disclaimer +* in the documentation and/or other materials provided with the +* distribution. +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* You can contact the author at : +* - LZ4 homepage : http://www.lz4.org +* - LZ4 source repository : https://github.com/lz4/lz4 +*/ + +#ifndef __LZ4_H__ +#define __LZ4_H__ + +#include <linux/types.h> +#include <linux/string.h> /* memset, memcpy */ + +/*-************************************************************************ +* CONSTANTS +**************************************************************************/ +/* +* LZ4_MEMORY_USAGE : +* Memory usage formula : N->2^N Bytes +* (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.) +* Increasing memory usage improves compression ratio +* Reduced memory usage can improve speed, due to cache effect +* Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache +*/ +#define LZ4_MEMORY_USAGE 10 + +#define LZ4_MAX_INPUT_SIZE 0x7E000000/* 2 113 929 216 bytes */ +#define LZ4_COMPRESSBOUND (isize) (\ + (unsigned int)(isize) > (unsigned int)LZ4_MAX_INPUT_SIZE \ + ? 0 \ + : (isize) + ((isize)/255) + 16) + +#define LZ4_ACCELERATION_DEFAULT 1 +#define LZ4_HASHLOG (LZ4_MEMORY_USAGE-2) +#define LZ4_HASHTABLESIZE (1 << LZ4_MEMORY_USAGE) +#define LZ4_HASH_SIZE_U32 (1 << LZ4_HASHLOG) + +#define LZ4HC_MIN_CLEVEL 3 +#define LZ4HC_DEFAULT_CLEVEL 9 +#define LZ4HC_MAX_CLEVEL 16 + +#define LZ4HC_DICTIONARY_LOGSIZE 16 +#define LZ4HC_MAXD (1<<LZ4HC_DICTIONARY_LOGSIZE) +#define LZ4HC_MAXD_MASK (LZ4HC_MAXD - 1) +#define LZ4HC_HASH_LOG (LZ4HC_DICTIONARY_LOGSIZE-1) +#define LZ4HC_HASHTABLESIZE (1 << LZ4HC_HASH_LOG) +#define LZ4HC_HASH_MASK (LZ4HC_HASHTABLESIZE - 1) + +/*-************************************************************************ +* STREAMING CONSTANTS AND STRUCTURES +**************************************************************************/ +#define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE-3)) + 4) +#define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U64 * sizeof(unsigned long long)) + +#define LZ4_STREAMHCSIZE 262192 +#define LZ4_STREAMHCSIZE_SIZET (262192 / sizeof(size_t)) + +#define LZ4_STREAMDECODESIZE_U64 4 +#define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * \ + sizeof(unsigned long long)) + +/* +* LZ4_stream_t : +* information structure to track an LZ4 stream. +*/ +typedef struct { +uint32_t hashTable[LZ4_HASH_SIZE_U32 ]; +uint32_t currentOffset; +uint32_t initCheck; +const uint8_t *dictionary; +uint8_t *bufferStart; +uint32_t dictSize; +}LZ4_stream_t_internal ; +typedef union { +unsigned long long table[LZ4_STREAMSIZE_U64 ]; +LZ4_stream_t_internal internal_donotuse; +}LZ4_stream_t ; + +/* +* LZ4_streamHC_t : +* information structure to track an LZ4HC stream. */ -#define LZ4_MEM_COMPRESS (16384) -#define LZ4HC_MEM_COMPRESS (262144 + (2 * sizeof(unsigned char *))) +typedef struct { +unsigned int hashTable[LZ4HC_HASHTABLESIZE ]; +unsigned short chainTable[LZ4HC_MAXD ]; +/* next block to continue on current prefix */ +const unsigned char *end; +/* All index relative to this position */ +const unsigned char *base; +/* alternate base for extDict */ +const unsigned char *dictBase; +/* below that point, need extDict */ +unsigned int dictLimit; +/* below that point, no more dict */ +unsigned int lowLimit; +/* index from which to continue dict update */ +unsigned int nextToUpdate; +unsigned int compressionLevel; +}LZ4HC_CCtx_internal ; +typedef union { +size_t table[LZ4_STREAMHCSIZE_SIZET ]; +LZ4HC_CCtx_internal internal_donotuse; +}LZ4_streamHC_t ;/* -* lz4_compressbound() -* Provides the maximum size that LZ4 may output in a "worst case" scenario -* (input data not compressible) +* LZ4_streamDecode_t : +* information structure to track an LZ4 stream during decompression. +* init this structure using LZ4_setStreamDecode +* (or memset()) before first use */ -static inline size_t lz4_compressbound (size_t isize) +typedef struct { +const uint8_t *externalDict; +size_t extDictSize; +const uint8_t *prefixEnd; +size_t prefixSize; +}LZ4_streamDecode_t_internal ; +typedef union { +unsigned long long table[LZ4_STREAMDECODESIZE_U64 ]; +LZ4_streamDecode_t_internal internal_donotuse; +}LZ4_streamDecode_t ; + +/*-************************************************************************ +* SIZE OF STATE +**************************************************************************/ +#define LZ4_MEM_COMPRESS LZ4_STREAMSIZE +#define LZ4HC_MEM_COMPRESS LZ4_STREAMHCSIZE + +/*-************************************************************************ +* Compression Functions +**************************************************************************/ + +/* +* LZ4_compressBound() : +* Provides the maximum size that LZ4 may output in a "worst case" scenario +* (input data not compressible) +*/ +static inline int LZ4_compressBound (size_t isize) +{ +return LZ4_COMPRESSBOUND (isize); +} + +/* +* For backward compatibility +*/ +static inline int lz4_compressbound (size_t isize) { -return isize + (isize /255 ) +16 ; +return LZ4_COMPRESSBOUND (isize); }/* +* LZ4_compress_default() +* Compresses 'sourceSize' bytes from buffer 'source' +* into already allocated 'dest' buffer of size 'maxOutputSize'. +* Compression is guaranteed to succeed if +* 'maxOutputSize' >= LZ4_compressBound(inputSize). +* It also runs faster, so it's a recommended setting. +* If the function cannot compress 'source' +* into a more limited 'dest' budget, +* compression stops *immediately*, +* and the function result is zero. +* As a consequence, 'dest' content is not valid. +* +* source : source address of the original data +* dest : output buffer address +* of the compressed data +* inputSize : Max supported value is +* LZ4_MAX_INPUT_SIZE +* maxOutputSize: full or partial size of buffer 'dest' +* (which must be already allocated) +* workmem : address of the working memory. +* This requires 'workmem' of size LZ4_MEM_COMPRESS. +* return : the number of bytes written into buffer 'dest' +* (necessarily <= maxOutputSize) or 0 if compression fails +*/ +int LZ4_compress_default (const char *source,char *dest,int inputSize, +int maxOutputSize,void *wrkmem); + +/* +* LZ4_compress_fast() : +* Same as LZ4_compress_default(), +* but allows to select an "acceleration" factor. +* The larger the acceleration value, +* the faster the algorithm, +* but also the lesser the compression. +* It's a trade-off. It can be fine tuned, +* with each successive value +* providing roughly +~3% to speed. +* An acceleration value of "1" +* is the same as regular LZ4_compress_default() +* Values <= 0 will be replaced by +* LZ4_ACCELERATION_DEFAULT, which is 1. +*/ +int LZ4_compress_fast (const char *source,char *dest,int inputSize, +int maxOutputSize,void *wrkmem,int acceleration); + +/* +* LZ4_compress_destSize() +* Reverse the logic, by compressing as much data as possible +* from 'source' buffer into already allocated buffer 'dest' +* of size 'targetDestSize'. +* This function either compresses the entire 'source' content into 'dest' +* if it's large enough, or fill 'dest' buffer completely with as much data as +* ossible from 'source'. +* +* source : source address of the original data +* dest : output buffer address of the compressed data +* inputSize : Max supported value is LZ4_MAX_INPUT_SIZE +* *sourceSizePtr: will be modified to indicate how many bytes where read +* from 'source' to fill 'dest'. +* New value is necessarily <= old value. +* workmem : address of the working memory. +* This requires 'workmem' of size LZ4_MEM_COMPRESS. +* return : Nb bytes written into 'dest' (necessarily <= targetDestSize) +or 0 if compression fails +*/ +int LZ4_compress_destSize (const char *source,char *dest,int *sourceSizePtr, +int targetDestSize,void *wrkmem); + +/* * lz4_compress() -* src : source address of the original data -* src_len : size of the original data -* dst : output buffer address of the compressed data -* This requires 'dst' of size LZ4_COMPRESSBOUND. -* dst_len : is the output size, which is returned after compress done -* workmem : address of the working memory. -* This requires 'workmem' of size LZ4_MEM_COMPRESS. -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer and workmem must be already allocated with +* src : source address of the original data +* src_len: size of the original data +* dst : output buffer address of the compressed data +* This requires 'dst' of size LZ4_COMPRESSBOUND. +* dst_len: is the output size, which is returned after compress done +* workmem: address of the working memory. +* This requires 'workmem' of size LZ4_MEM_COMPRESS. +* return : Success if return 0 +* Error if return (< 0) +* note : Destination buffer and workmem must be already allocated with * the defined size. */ -int lz4_compress (const unsigned char *src,size_t src_len, -unsigned char *dst,size_t *dst_len,void *wrkmem); - -/* -* lz4hc_compress() -* src : source address of the original data -* src_len : size of the original data -* dst : output buffer address of the compressed data -* This requires 'dst' of size LZ4_COMPRESSBOUND. -* dst_len : is the output size, which is returned after compress done -* workmem : address of the working memory. -* This requires 'workmem' of size LZ4HC_MEM_COMPRESS. -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer and workmem must be already allocated with -* the defined size. -*/ -int lz4hc_compress (const unsigned char *src,size_t src_len, -unsigned char *dst,size_t *dst_len,void *wrkmem); - -/* -* lz4_decompress() -* src : source address of the compressed data -* src_len : is the input size, whcih is returned after decompress done -* dest : output buffer address of the decompressed data -* actual_dest_len: is the size of uncompressed data, supposing it's known -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer must be already allocated. -* slightly faster than lz4_decompress_unknownoutputsize() +int lz4_compress (const unsigned char *src,size_t src_len,unsigned char *dst, +size_t *dst_len,void *wrkmem); + +/*-************************************************************************ +* Decompression Functions +**************************************************************************/ + +/* +* LZ4_decompress_fast() +* source : source address of the compressed data +* dst : output buffer address of the decompressed data +* originalSize: is the original and therefore uncompressed size +* return : the number of bytes read from the source buffer +* (in other words, the compressed size) +* If the source stream is detected malformed, the function will +* stop decoding and return a negative result. +* Destination buffer must be already allocated. +* Its size must be a minimum of 'originalSize' bytes. +* note : This function fully respect memory boundaries +* for properly formed compressed data. +* It is a bit faster than LZ4_decompress_safe(). +* However, it does not provide any protection against intentionally +* modified data stream (malicious input). +* Use this function in trusted environment only +* (data to decode comes from a trusted source). */ -int lz4_decompress (const unsigned char *src,size_t *src_len, -unsigned char *dest,size_t actual_dest_len); +int LZ4_decompress_fast (const char *source,char *dest,int originalSize); + +/* +* LZ4_decompress_safe() +* source : source address of the compressed data +* dest : output buffer address of the decompressed data +* compressedSize : is the precise full size of the compressed block. +* maxDecompressedSize: is the size of destination buffer, +* which must be already allocated. +* return : the number of bytes decompressed into destination buffer +* (necessarily <= maxDecompressedSize) +* If destination buffer is not large enough, decoding will stop +* and output an error code (<0). +* If the source stream is detected malformed, the function will +* stop decoding and return a negative result. +* This function is protected against buffer overflow exploits, +* including malicious data packets. It never writes outside +* output buffer, nor reads outside input buffer. +*/ +int LZ4_decompress_safe (const char *source,char *dest,int compressedSize, +int maxDecompressedSize);/* -* lz4_decompress_unknownoutputsize() +* LZ4_decompress_safe_partial() : +* This function decompress a compressed block of size 'compressedSize' +* at position 'source' into destination buffer 'dest' +* of size 'maxDecompressedSize'. +* The function tries to stop decompressing operation as soon as +* 'targetOutputSize' has been reached, reducing decompression time. +* +* source : source address of the compressed data +* dest : output buffer address of the decompressed data +* compressedSize : is the precise full size of the compressed block. +* targetOutputSize : the decompression operation will try +* to stop as soon as 'targetOutputSize' +* has been reached +* maxDecompressedSize: is the size of destination buffer, +* which must be already allocated. +* return : the number of bytes decoded in the destination buffer +* (necessarily <= maxDecompressedSize) +* Note : this number can be < 'targetOutputSize' should the compressed +* block to decode be smaller. Always control how many bytes +* were decoded. If the source stream is detected malformed, +* the function will stop decoding and return a negative result. +* This function never writes outside of output buffer, +* and never reads outside of input buffer. +* It is therefore protected against malicious data packets +*/ +int LZ4_decompress_safe_partial (const char *source,char *dest, +int compressedSize,int targetOutputSize,int maxDecompressedSize); + + +/* +* lz4_decompress_unknownoutputsize() : * src : source address of the compressed data * src_len : is the input size, therefore the compressed size -* dest : output buffer address of the decompressed data +* dest : output buffer address of the decompressed data * dest_len: is the max size of the destination buffer, which is -* returned with actual size of decompressed data after -* decompress done -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer must be already allocated. +* returned with actual size of decompressed data after +* decompress done +* return: Success if return 0 +* Error if return (< 0) +* note: Destination buffer must be already allocated. */ int lz4_decompress_unknownoutputsize (const unsigned char *src,size_t src_len, -unsigned char *dest,size_t *dest_len); +unsigned char *dest,size_t *dest_len); + +/* +* lz4_decompress() : +* src : source address of the compressed data +* src_len : is the input size, +* which is returned after decompress done +* dest : output buffer address of the decompressed data +* actual_dest_len: is the size of uncompressed data, supposing it's known +* return: Success if return 0 +* Error if return (< 0) +* note : Destination buffer must be already allocated. +* slightly faster than lz4_decompress_unknownoutputsize() +*/ +int lz4_decompress (const unsigned char *src,size_t *src_len, +unsigned char *dest,size_t actual_dest_len); + +/*-************************************************************************ +* LZ4 HC Compression +**************************************************************************/ + +/*! LZ4_compress_HC() : +* Compress data from `src` into `dst`, using the more powerful +* but slower "HC" algorithm. dst` must be already allocated. +* Compression is guaranteed to succeed if +* `dstCapacity >= LZ4_compressBound(srcSize) +* Max supported `srcSize` value is LZ4_MAX_INPUT_SIZE +* +* src : source address of the original data +* dst : output buffer address of the compressed data +* srcSize : Max supported value is LZ4_MAX_INPUT_SIZE +* dstCapacity : full or partial size of buffer 'dst' +* (which must be already allocated) +* compressionLevel: Recommended values are between 4 and 9, although any +* value between 1 and LZ4HC_MAX_CLEVEL will work. +* Values >LZ4HC_MAX_CLEVEL behave the same as 16. +* wrkmem : address of the working memory. +* This requires 'wrkmem' of size LZ4HC_MEM_COMPRESS. +* return : the number of bytes written into 'dst' +* or 0 if compression fails. +*/ +int LZ4_compress_HC (const char *src,char *dst,int srcSize,int dstCapacity, +int compressionLevel,void *wrkmem); + +/* +* lz4hc_compress() +* src : source address of the original data +* src_len: size of the original data +* dst : output buffer address of the compressed data +* This requires 'dst' of size LZ4_COMPRESSBOUND. +* dst_len: is the output size, which is returned after compress done +* workmem: address of the working memory. +* This requires 'workmem' of size LZ4HC_MEM_COMPRESS. +* return : Success if return 0 +* Error if return (< 0) +* note : Destination buffer and workmem must be already allocated with +* the defined size. +*/ +int lz4hc_compress (const unsigned char *src,size_t src_len,unsigned char *dst, +size_t *dst_len,void *wrkmem); + +/* +* These functions compress data in successive blocks of any size, +* using previous blocks as dictionary. One key assumption is that previous +* blocks (up to 64 KB) remain read-accessible while +* compressing next blocks. +* There is an exception for ring buffers, which can be smaller than 64 KB. +* Ring buffers scenario is automatically detected and handled by +* LZ4_compress_HC_continue(). +* Before starting compression, state must be properly initialized, +* using LZ4_resetStreamHC(). +* A first "fictional block" can then be designated as initial dictionary, +* using LZ4_loadDictHC() (Optional). +* Then, use LZ4_compress_HC_continue() to compress each successive block. +* Previous memory blocks (including initial dictionary when present) must +* remain accessible and unmodified during compression. +* 'dst' buffer should be sized to handle worst case scenarios, using +* LZ4_compressBound(), to ensure operation success. +* If, for any reason, previous data blocks can't be preserved unmodified +* in memory during next compression block, +* you must save it to a safer memory space, using LZ4_saveDictHC(). +* Return value of LZ4_saveDictHC() is the size of dictionary +* effectively saved into 'safeBuffer'. +*/ +void LZ4_resetStreamHC (LZ4_streamHC_t *streamHCPtr,int compressionLevel); +int LZ4_loadDictHC (LZ4_streamHC_t *streamHCPtr,const char *dictionary, +int dictSize); +int LZ4_compress_HC_continue (LZ4_streamHC_t *streamHCPtr,const char *src, +char *dst,int srcSize,int maxDstSize); +int LZ4_saveDictHC (LZ4_streamHC_t *streamHCPtr,char *safeBuffer, +int maxDictSize); + +/*-********************************************* +* Streaming Compression Functions +***********************************************/ +/* +* LZ4_resetStream() : +* An LZ4_stream_t structure can be allocated once +* and re-used multiple times. +* Use this function to init an allocated `LZ4_stream_t` structure +* and start a new compression. +*/ +void LZ4_resetStream (LZ4_stream_t *LZ4_stream); + +/* +* LZ4_loadDict() : +* Use this function to load a static dictionary into LZ4_stream. +* Any previous data will be forgotten, only 'dictionary' +* will remain in memory. +* Loading a size of 0 is allowed. +* Return : dictionary size, in bytes (necessarily <= 64 KB) +*/ +int LZ4_loadDict (LZ4_stream_t *streamPtr,const char *dictionary, +int dictSize); + +/* +* LZ4_compress_fast_continue() : +* Compress buffer content 'src', +* using data from previously compressed blocks +* as dictionary to improve compression ratio. +* Important : Previous data blocks are assumed to still +* be present and unmodified ! +* 'dst' buffer must be already allocated. +* If maxDstSize >= LZ4_compressBound(srcSize), +* compression is guaranteed to succeed, and runs faster. +* If not, and if compressed data cannot fit into 'dst' buffer size, +* compression stops, and function returns a zero. +*/ +int LZ4_compress_fast_continue (LZ4_stream_t *streamPtr,const char *src, +char *dst,int srcSize,int maxDstSize,int acceleration); + +/* +* LZ4_saveDict() : +* If previously compressed data block is not guaranteed +* to remain available at its memory location, +* save it into a safer place (char *safeBuffer). +* Note : you don't need to call LZ4_loadDict() afterwards, +* dictionary is immediately usable, you can therefore call +* LZ4_compress_fast_continue(). +* Return : saved dictionary size in bytes (necessarily <= dictSize), +* or 0 if error. +*/ +int LZ4_saveDict (LZ4_stream_t *streamPtr,char *safeBuffer,int dictSize); + +/* +* LZ4_setStreamDecode() : +* Use this function to instruct where to find the dictionary. +* Setting a size of 0 is allowed (same effect as reset). +* @return : 1 if OK, 0 if error +*/ +int LZ4_setStreamDecode (LZ4_streamDecode_t *LZ4_streamDecode, +const char *dictionary,int dictSize); + +/* +* LZ4_decompress_*_continue() : +* These decoding functions allow decompression of multiple blocks +* in "streaming" mode. +* Previously decoded blocks *must* remain available at the memory position +* where they were decoded (up to 64 KB) +* In the case of a ring buffers, decoding buffer must be either : +* - Exactly same size as encoding buffer, with same update rule +* (block boundaries at same positions) In which case, +* the decoding & encoding ring buffer can have any size, +* including very small ones ( < 64 KB). +* - Larger than encoding buffer, by a minimum of maxBlockSize more bytes. +* maxBlockSize is implementation dependent. +* It's the maximum size you intend to compress into a single block. +* In which case, encoding and decoding buffers do not need +* to be synchronized, and encoding ring buffer can have any size, +* including small ones ( < 64 KB). +* - _At least_ 64 KB + 8 bytes + maxBlockSize. +* In which case, encoding and decoding buffers do not need to be +* synchronized, and encoding ring buffer can have any size, +* including larger than decoding buffer. W +* Whenever these conditions are not possible, save the last 64KB of decoded +* data into a safe buffer, and indicate where it is saved +* using LZ4_setStreamDecode() +*/ +int LZ4_decompress_safe_continue (LZ4_streamDecode_t *LZ4_streamDecode, +const char *source,char *dest,int compressedSize, +int maxDecompressedSize); +int LZ4_decompress_fast_continue (LZ4_streamDecode_t *LZ4_streamDecode, +const char *source,char *dest,int originalSize); + +/* +* LZ4_decompress_*_usingDict() : +* These decoding functions work the same as +* a combination of LZ4_setStreamDecode() followed by +* LZ4_decompress_*_continue() +* They are stand-alone, and don't need an LZ4_streamDecode_t structure. +*/ +int LZ4_decompress_safe_usingDict (const char *source,char *dest, +int compressedSize,int maxDecompressedSize,const char *dictStart, +int dictSize); +int LZ4_decompress_fast_usingDict (const char *source,char *dest, +int originalSize,const char *dictStart,int dictSize); +#endif
lib/lz4/lz4_compress.c +779 -324
1,215 lines not on this page · raw message
lib/lz4/lz4_decompress.c +440 -254
778 lines not on this page · raw message
lib/lz4/lz4defs.h +191 -129
348 lines not on this page · raw message
lib/lz4/lz4hc_compress.c +548 -307
44 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.1.4
Part of the diff is not shown here. View the raw message.
Andrew Morton
maintainerRe: [PATCH v4 1/4] lib: Update LZ4 compressor module
Quoted 32 lines · On Sun, 22 Jan 2017 20:35:14 +0100 Sven Schmidt…
This patch updates LZ4 kernel module to LZ4 v1.7.3 by Yann Collet. The kernel module is inspired by the previous work by Chanho Min. The updated LZ4 module will not break existing code since there were alias methods added to ensure backwards compatibility.
API changes:
New method
LZ4_compress_fastwhich differs from the variant available in kernel by the new acceleration parameter,
allowing to trade compression ratio for more compression speed and vice versa.
LZ4_decompress_fastis the respective decompression method, featuring a very fast decoder (multiple GB/s per core), able to reach RAM speed in multi-core systems. The decompressor allows to decompress data compressed with LZ4 fast as well as the LZ4 HC (high compression) algorithm.Also the useful functions
LZ4_decompress_safe_partialLZ4_compress_destsizewere added. The latter reverses the logic by trying to compress as much data as possible from source to dest while the former aims to decompress partial blocks of data.A bunch of streaming functions were also added which allow compressig/decompressing data in multiple steps (so called "streaming mode").
The methods
lz4_compressandlz4_decompress_unknownoutputsizeare now known asLZ4_compress_defaultrespectivleyLZ4_decompress_safe. The old methods are still available for providing backwards compatibility....
include/linux/lz4.h line 179
Quoted 5 lines
include/linux/lz4.h +4
-0+/* +* For backward compatibility +*/ +static inline int lz4_compressbound (size_t isize) {
Do we actually need the back-compat wrappers? After your other three patches we have no callers, correct? If so, we should go ahead and eliminate such back-compatibility interfaces.
Quoted 3 lines
include/linux/lz4.h +1
-1-return isize + (isize /255 ) +16 ; +return LZ4_COMPRESSBOUND (isize); }
Re: [PATCH v4 1/4] lib: Update LZ4 compressor module
Quoted 45 lines · Andrew Morton
Quoted 32 lines · On Sun, 22 Jan 2017 20:35:14 +0100 Sven Schmidt…
This patch updates LZ4 kernel module to LZ4 v1.7.3 by Yann Collet. The kernel module is inspired by the previous work by Chanho Min. The updated LZ4 module will not break existing code since there were alias methods added to ensure backwards compatibility.
API changes:
New method
LZ4_compress_fastwhich differs from the variant available in kernel by the new acceleration parameter,
allowing to trade compression ratio for more compression speed and vice versa.
LZ4_decompress_fastis the respective decompression method, featuring a very fast decoder (multiple GB/s per core), able to reach RAM speed in multi-core systems. The decompressor allows to decompress data compressed with LZ4 fast as well as the LZ4 HC (high compression) algorithm.Also the useful functions
LZ4_decompress_safe_partialLZ4_compress_destsizewere added. The latter reverses the logic by trying to compress as much data as possible from source to dest while the former aims to decompress partial blocks of data.A bunch of streaming functions were also added which allow compressig/decompressing data in multiple steps (so called "streaming mode").
The methods
lz4_compressandlz4_decompress_unknownoutputsizeare now known asLZ4_compress_defaultrespectivleyLZ4_decompress_safe. The old methods are still available for providing backwards compatibility....
include/linux/lz4.h line 179
Quoted 5 lines
include/linux/lz4.h +4
-0+/* +* For backward compatibility +*/ +static inline int lz4_compressbound (size_t isize) {Do we actually need the back-compat wrappers? After your other three patches we have no callers, correct? If so, we should go ahead and eliminate such back-compatibility interfaces.
Quoted 4 lines
include/linux/lz4.h line 181
Quoted 3 lines
include/linux/lz4.h +1
-1-return isize + (isize /255 ) +16 ; +return LZ4_COMPRESSBOUND (isize); }
Hey Andrew,
you're absolutely right. Since I changed the callers to use the new functions, we do not need these wrappers anymore.
I will add a patch removing them and send an update.
Thanks,
Sven
PATCH v4 2/4 lib/decompress_unlz4: Change module to work with new LZ4 module version
In reply to Sven SchmidtThis patch updates the unlz4 wrapper to work with the updated LZ4 kernel module version.
--- lib/decompress_unlz4.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
lib/decompress_unlz4.c +8 -5
@@ -72,7 +72,7 @@ STATIC inline int INIT unlz4(u8 *input, long in_len,error ("NULL input pointer and missing fill function" ); goto exit_1; }else { - inp =large_malloc (lz4_compressbound (uncomp_chunksize)); + inp =large_malloc (LZ4_compressBound (uncomp_chunksize));if (!inp) {error ("Could not allocate input buffer" ); goto exit_1;
@@ -136,7 +136,7 @@ STATIC inline int INIT unlz4(u8 *input, long in_len, inp +=4 ; size -=4 ; }else { -if (chunksize >lz4_compressbound (uncomp_chunksize)) { +if (chunksize >LZ4_compressBound (uncomp_chunksize)) {error ("chunk length is longer than allocated" ); goto exit_2; }
@@ -152,11 +152,14 @@ STATIC inline int INIT unlz4(u8 *input, long in_len, out_len -= dest_len; }else dest_len = out_len; - ret =lz4_decompress (inp, &chunksize, outp, dest_len); + + ret =LZ4_decompress_fast (inp, outp, dest_len); + chunksize = ret;#else dest_len = uncomp_chunksize; - ret =lz4_decompress_unknownoutputsize (inp, chunksize, outp, - &dest_len); + + ret =LZ4_decompress_safe (inp, outp, chunksize, dest_len); + dest_len = ret;#endif if (ret <0 ) {error ("Decoding failed" );
Signature · 1 line
2.1.4
PATCH v4 3/4 crypto: Change LZ4 modules to work with new LZ4 module version
In reply to Sven SchmidtThis patch updates the crypto modules using LZ4 compression to work with the new LZ4 module version.
--- crypto/lz4.c | 21 ++++++++------------- crypto/lz4hc.c | 21 ++++++++------------- 2 files changed, 16 insertions(+), 26 deletions(-)
crypto/lz4.c +8 -13
@@ -66,15 +66,13 @@ static void lz4_exit(struct crypto_tfm *tfm)static int __lz4_compress_crypto (const u8 *src,unsigned int slen,u8 *dst,unsigned int *dlen,void *ctx) { -size_t tmp_len = *dlen; -int err; +int out_len =LZ4_compress_default (src, dst, + slen, (int )((size_t )dlen), ctx); - err =lz4_compress (src, slen, dst, &tmp_len, ctx); - -if (err <0 ) +if (!out_len)return -EINVAL ; - *dlen = tmp_len; + *dlen = out_len;return 0 ; }
@@ -96,16 +94,13 @@ static int lz4_compress_crypto(struct crypto_tfm *tfm, const u8 *src,static int __lz4_decompress_crypto (const u8 *src,unsigned int slen,u8 *dst,unsigned int *dlen,void *ctx) { -int err; -size_t tmp_len = *dlen; -size_t __slen = slen; +int out_len =LZ4_decompress_safe (src, dst, slen, (int )((size_t )dlen)); - err =lz4_decompress_unknownoutputsize (src, __slen, dst, &tmp_len); -if (err <0 ) +if (out_len <0 )return -EINVAL ; - *dlen = tmp_len; -return err; + *dlen = out_len; +return out_len; } static int lz4_sdecompress(struct crypto_scomp *tfm, const u8 *src,
crypto/lz4hc.c +8 -13
@@ -65,15 +65,13 @@ static void lz4hc_exit(struct crypto_tfm *tfm)static int __lz4hc_compress_crypto (const u8 *src,unsigned int slen,u8 *dst,unsigned int *dlen,void *ctx) { -size_t tmp_len = *dlen; -int err; +int out_len =LZ4_compress_HC (src, dst, slen, + (int )((size_t )dlen),LZ4HC_DEFAULT_CLEVEL , ctx); - err =lz4hc_compress (src, slen, dst, &tmp_len, ctx); - -if (err <0 ) +if (out_len ==0 )return -EINVAL ; - *dlen = tmp_len; + *dlen = out_len;return 0 ; }
@@ -97,16 +95,13 @@ static int lz4hc_compress_crypto(struct crypto_tfm *tfm, const u8 *src,static int __lz4hc_decompress_crypto (const u8 *src,unsigned int slen,u8 *dst,unsigned int *dlen,void *ctx) { -int err; -size_t tmp_len = *dlen; -size_t __slen = slen; +int out_len =LZ4_decompress_safe (src, dst, slen, (int )((size_t )dlen)); - err =lz4_decompress_unknownoutputsize (src, __slen, dst, &tmp_len); -if (err <0 ) +if (out_len <0 )return -EINVAL ; - *dlen = tmp_len; -return err; + *dlen = out_len; +return out_len; } static int lz4hc_sdecompress(struct crypto_scomp *tfm, const u8 *src,
Signature · 1 line
2.1.4
PATCH v4 4/4 fs/pstore: fs/squashfs: Change usage of LZ4 to work with new LZ4 version
In reply to Sven SchmidtThis patch updates fs/pstore and fs/squashfs to use the updated functions from the new LZ4 module.
--- fs/pstore/platform.c | 14 +++++++------- fs/squashfs/lz4_wrapper.c | 12 ++++++------ 2 files changed, 13 insertions(+), 13 deletions(-)
fs/pstore/platform.c +7 -7
@@ -342,31 +342,31 @@ static int compress_lz4(const void *in, void *out, size_t inlen, size_t outlen) {int ret; - ret =lz4_compress (in, inlen, out, &outlen, workspace); -if (ret) { + ret =LZ4_compress_default (in, out, inlen, outlen, workspace); +if (!ret) {pr_err ("lz4_compress error, ret = %d!\n" , ret);return -EIO ; } -return outlen; +return ret; }static int decompress_lz4 (void *in,void *out,size_t inlen,size_t outlen) {int ret; - ret =lz4_decompress_unknownoutputsize (in, inlen, out, &outlen); -if (ret) { + ret =LZ4_decompress_safe (in, out, inlen, outlen); +if (ret <0 ) {pr_err ("lz4_decompress error, ret = %d!\n" , ret);return -EIO ; } -return outlen; +return ret; }static void allocate_lz4 (void ) { - big_oops_buf_sz =lz4_compressbound (psinfo->bufsize); + big_oops_buf_sz =LZ4_compressBound (psinfo->bufsize); big_oops_buf =kmalloc (big_oops_buf_sz,GFP_KERNEL );if (big_oops_buf) { workspace =kmalloc (LZ4_MEM_COMPRESS ,GFP_KERNEL );
fs/squashfs/lz4_wrapper.c +6 -6
@@ -97,7 +97,6 @@ static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm,struct squashfs_lz4 *stream = strm;void *buff = stream->input, *data;int avail, i, bytes = length, res; -size_t dest_len = output->length;for (i =0 ; i < b; i++) { avail =min (bytes, msblk->devblksize - offset);
@@ -108,12 +107,13 @@ static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm,put_bh (bh[i]); } - res =lz4_decompress_unknownoutputsize (stream->input, length, - stream->output, &dest_len); -if (res) + res =LZ4_decompress_safe (stream->input, stream->output, + length, output->length); + +if (res <0 )return -EIO ; - bytes = dest_len; + bytes = res; data =squashfs_first_page (output); buff = stream->output;while (data) {
@@ -128,7 +128,7 @@ static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm, }squashfs_finish_page (output); -return dest_len; +return res; }const struct squashfs_decompressor squashfs_lz4_comp_ops = {
Signature · 1 line
2.1.4
v5 cover Update LZ4 compressor module
In reply to Sven SchmidtThis patchset is for updating the LZ4 compression module to a version based on LZ4 v1.7.3 allowing to use the fast compression algorithm aka LZ4 fast which provides an "acceleration" parameter as a tradeoff between high compression ratio and high compression speed.
We want to use LZ4 fast in order to support compression in lustre and (mostly, based on that) investigate data reduction techniques in behalf of storage systems.
Also, it will be useful for other users of LZ4 compression, as with LZ4 fast it is possible to enable applications to use fast and/or high compression depending on the usecase.
For instance, ZRAM is offering a LZ4 backend and could benefit from an updated LZ4 in the kernel.
LZ4 homepage: http://www.lz4.org/
LZ4 source repository: https://github.com/lz4/lz4
Source version: 1.7.3
Benchmark (taken from [1], Core i5-4300U @1.9GHz):
----------------|--------------|----------------|---------- Compressor | Compression | Decompression | Ratio ----------------|--------------|----------------|---------- memcpy | 4200 MB/s | 4200 MB/s | 1.000 LZ4 fast 50 | 1080 MB/s | 2650 MB/s | 1.375 LZ4 fast 17 | 680 MB/s | 2220 MB/s | 1.607 LZ4 fast 5 | 475 MB/s | 1920 MB/s | 1.886 LZ4 default | 385 MB/s | 1850 MB/s | 2.101
[1] http://fastcompression.blogspot.de/2015/04/sampling-or-faster-lz4.html
fs/pstore: fs/squashfs: Change usage of LZ4 to work with new LZ4 version
[PATCH 1/5] lib: Update LZ4 compressor module
[PATCH 2/5] lib/decompress_unlz4: Change module to work with new LZ4 module version [PATCH 3/5] crypto: Change LZ4 modules to work with new LZ4 module version [PATCH 4/5] fs/pstore: fs/squashfs: Change usage of LZ4 to work with new LZ4 version [PATCH 5/5] lib/lz4: Remove back-compat wrappers
v2:
- Changed order of the patches since in the initial patchset the lz4.h was in the last patch but was referenced by the other ones
- Split lib/decompress_unlz4.c in an own patch
- Fixed errors reported by the buildbot
- Further refactorings
- Added more appropriate copyright note to include/linux/lz4.h
v3:
- Adjusted the code to satisfy kernel coding style (checkpatch.pl)
- Made sure the changes to LZ4 in Kernel (overflow checks etc.) are included in the new module (they are)
- Removed the second LZ4_compressBound function with related name but different return type
- Corrected version number (was LZ4 1.7.3)
- Added missing LZ4 streaming functions
v4:
- Fixed kbuild errors
- Re-added lz4_compressbound as alias for LZ4_compressBound to ensure backwards compatibility
- Wrapped LZ4_hash5 with check for LZ4_ARCH64 since it is only used there and triggers an unused function warning when false
v5:
- Added a fifth patch to remove the back-compat wrappers introduced to ensure bisectibility between the patches (the functions are no longer needed since there's no callers left)
PATCH v5 1/5 lib: Update LZ4 compressor module
This patch updates LZ4 kernel module to LZ4 v1.7.3 by Yann Collet. The kernel module is inspired by the previous work by Chanho Min. The updated LZ4 module will not break existing code since the patchset contains appropriate changes.
API changes:
New method LZ4_compress_fast which differs from the variant available in kernel by the new acceleration parameter,
allowing to trade compression ratio for more compression speed and vice versa.
LZ4_decompress_fast is the respective decompression method, featuring a very fast decoder (multiple GB/s per core), able to reach RAM speed in multi-core systems. The decompressor allows to decompress data compressed with LZ4 fast as well as the LZ4 HC (high compression) algorithm.
Also the useful functions LZ4_decompress_safe_partial LZ4_compress_destsize were added. The latter reverses the logic by trying to compress as much data as possible from source to dest while the former aims to decompress partial blocks of data.
A bunch of streaming functions were also added which allow compressig/decompressing data in multiple steps (so called "streaming mode").
The methods lz4_compress and lz4_decompress_unknownoutputsize are now known as LZ4_compress_default respectivley LZ4_decompress_safe. The old methods will be removed since there's no callers left in the code.
--- include/linux/lz4.h | 589 ++++++++++++++++++++++--- lib/lz4/lz4_compress.c | 1103 ++++++++++++++++++++++++++++++++-------------- lib/lz4/lz4_decompress.c | 694 ++++++++++++++++++----------- lib/lz4/lz4defs.h | 320 ++++++++------ lib/lz4/lz4hc_compress.c | 855 ++++++++++++++++++++++------------- 5 files changed, 2486 insertions(+), 1075 deletions(-)
include/linux/lz4.h +528 -61
@@ -1,87 +1,554 @@ -#ifndef __LZ4_H__ -#define __LZ4_H__ -/* -* LZ4 Kernel Interface +/* LZ4 Kernel Interface * * Copyright (C) 2013, LG Electronics, Kyungsik Lee <[email protected]> +* Copyright (C) 2016, Sven Schmidt <[email protected]> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. +* +* This file is based on the original header file +* for LZ4 - Fast LZ compression algorithm. +* +* LZ4 - Fast LZ compression algorithm +* Copyright (C) 2011-2016, Yann Collet. +* BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are +* met: +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above +* copyright notice, this list of conditions and the following disclaimer +* in the documentation and/or other materials provided with the +* distribution. +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* You can contact the author at : +* - LZ4 homepage : http://www.lz4.org +* - LZ4 source repository : https://github.com/lz4/lz4 +*/ + +#ifndef __LZ4_H__ +#define __LZ4_H__ + +#include <linux/types.h> +#include <linux/string.h> /* memset, memcpy */ + +/*-************************************************************************ +* CONSTANTS +**************************************************************************/ +/* +* LZ4_MEMORY_USAGE : +* Memory usage formula : N->2^N Bytes +* (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.) +* Increasing memory usage improves compression ratio +* Reduced memory usage can improve speed, due to cache effect +* Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache +*/ +#define LZ4_MEMORY_USAGE 10 + +#define LZ4_MAX_INPUT_SIZE 0x7E000000/* 2 113 929 216 bytes */ +#define LZ4_COMPRESSBOUND (isize) (\ + (unsigned int)(isize) > (unsigned int)LZ4_MAX_INPUT_SIZE \ + ? 0 \ + : (isize) + ((isize)/255) + 16) + +#define LZ4_ACCELERATION_DEFAULT 1 +#define LZ4_HASHLOG (LZ4_MEMORY_USAGE-2) +#define LZ4_HASHTABLESIZE (1 << LZ4_MEMORY_USAGE) +#define LZ4_HASH_SIZE_U32 (1 << LZ4_HASHLOG) + +#define LZ4HC_MIN_CLEVEL 3 +#define LZ4HC_DEFAULT_CLEVEL 9 +#define LZ4HC_MAX_CLEVEL 16 + +#define LZ4HC_DICTIONARY_LOGSIZE 16 +#define LZ4HC_MAXD (1<<LZ4HC_DICTIONARY_LOGSIZE) +#define LZ4HC_MAXD_MASK (LZ4HC_MAXD - 1) +#define LZ4HC_HASH_LOG (LZ4HC_DICTIONARY_LOGSIZE-1) +#define LZ4HC_HASHTABLESIZE (1 << LZ4HC_HASH_LOG) +#define LZ4HC_HASH_MASK (LZ4HC_HASHTABLESIZE - 1) + +/*-************************************************************************ +* STREAMING CONSTANTS AND STRUCTURES +**************************************************************************/ +#define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE-3)) + 4) +#define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U64 * sizeof(unsigned long long)) + +#define LZ4_STREAMHCSIZE 262192 +#define LZ4_STREAMHCSIZE_SIZET (262192 / sizeof(size_t)) + +#define LZ4_STREAMDECODESIZE_U64 4 +#define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * \ + sizeof(unsigned long long)) + +/* +* LZ4_stream_t : +* information structure to track an LZ4 stream. +*/ +typedef struct { +uint32_t hashTable[LZ4_HASH_SIZE_U32 ]; +uint32_t currentOffset; +uint32_t initCheck; +const uint8_t *dictionary; +uint8_t *bufferStart; +uint32_t dictSize; +}LZ4_stream_t_internal ; +typedef union { +unsigned long long table[LZ4_STREAMSIZE_U64 ]; +LZ4_stream_t_internal internal_donotuse; +}LZ4_stream_t ; + +/* +* LZ4_streamHC_t : +* information structure to track an LZ4HC stream. */ -#define LZ4_MEM_COMPRESS (16384) -#define LZ4HC_MEM_COMPRESS (262144 + (2 * sizeof(unsigned char *))) +typedef struct { +unsigned int hashTable[LZ4HC_HASHTABLESIZE ]; +unsigned short chainTable[LZ4HC_MAXD ]; +/* next block to continue on current prefix */ +const unsigned char *end; +/* All index relative to this position */ +const unsigned char *base; +/* alternate base for extDict */ +const unsigned char *dictBase; +/* below that point, need extDict */ +unsigned int dictLimit; +/* below that point, no more dict */ +unsigned int lowLimit; +/* index from which to continue dict update */ +unsigned int nextToUpdate; +unsigned int compressionLevel; +}LZ4HC_CCtx_internal ; +typedef union { +size_t table[LZ4_STREAMHCSIZE_SIZET ]; +LZ4HC_CCtx_internal internal_donotuse; +}LZ4_streamHC_t ;/* -* lz4_compressbound() -* Provides the maximum size that LZ4 may output in a "worst case" scenario -* (input data not compressible) +* LZ4_streamDecode_t : +* information structure to track an LZ4 stream during decompression. +* init this structure using LZ4_setStreamDecode +* (or memset()) before first use */ -static inline size_t lz4_compressbound (size_t isize) +typedef struct { +const uint8_t *externalDict; +size_t extDictSize; +const uint8_t *prefixEnd; +size_t prefixSize; +}LZ4_streamDecode_t_internal ; +typedef union { +unsigned long long table[LZ4_STREAMDECODESIZE_U64 ]; +LZ4_streamDecode_t_internal internal_donotuse; +}LZ4_streamDecode_t ; + +/*-************************************************************************ +* SIZE OF STATE +**************************************************************************/ +#define LZ4_MEM_COMPRESS LZ4_STREAMSIZE +#define LZ4HC_MEM_COMPRESS LZ4_STREAMHCSIZE + +/*-************************************************************************ +* Compression Functions +**************************************************************************/ + +/* +* LZ4_compressBound() : +* Provides the maximum size that LZ4 may output in a "worst case" scenario +* (input data not compressible) +*/ +static inline int LZ4_compressBound (size_t isize) +{ +return LZ4_COMPRESSBOUND (isize); +} + +/* +* For backward compatibility +*/ +static inline int lz4_compressbound (size_t isize) { -return isize + (isize /255 ) +16 ; +return LZ4_COMPRESSBOUND (isize); }/* +* LZ4_compress_default() +* Compresses 'sourceSize' bytes from buffer 'source' +* into already allocated 'dest' buffer of size 'maxOutputSize'. +* Compression is guaranteed to succeed if +* 'maxOutputSize' >= LZ4_compressBound(inputSize). +* It also runs faster, so it's a recommended setting. +* If the function cannot compress 'source' +* into a more limited 'dest' budget, +* compression stops *immediately*, +* and the function result is zero. +* As a consequence, 'dest' content is not valid. +* +* source : source address of the original data +* dest : output buffer address +* of the compressed data +* inputSize : Max supported value is +* LZ4_MAX_INPUT_SIZE +* maxOutputSize: full or partial size of buffer 'dest' +* (which must be already allocated) +* workmem : address of the working memory. +* This requires 'workmem' of size LZ4_MEM_COMPRESS. +* return : the number of bytes written into buffer 'dest' +* (necessarily <= maxOutputSize) or 0 if compression fails +*/ +int LZ4_compress_default (const char *source,char *dest,int inputSize, +int maxOutputSize,void *wrkmem); + +/* +* LZ4_compress_fast() : +* Same as LZ4_compress_default(), +* but allows to select an "acceleration" factor. +* The larger the acceleration value, +* the faster the algorithm, +* but also the lesser the compression. +* It's a trade-off. It can be fine tuned, +* with each successive value +* providing roughly +~3% to speed. +* An acceleration value of "1" +* is the same as regular LZ4_compress_default() +* Values <= 0 will be replaced by +* LZ4_ACCELERATION_DEFAULT, which is 1. +*/ +int LZ4_compress_fast (const char *source,char *dest,int inputSize, +int maxOutputSize,void *wrkmem,int acceleration); + +/* +* LZ4_compress_destSize() +* Reverse the logic, by compressing as much data as possible +* from 'source' buffer into already allocated buffer 'dest' +* of size 'targetDestSize'. +* This function either compresses the entire 'source' content into 'dest' +* if it's large enough, or fill 'dest' buffer completely with as much data as +* ossible from 'source'. +* +* source : source address of the original data +* dest : output buffer address of the compressed data +* inputSize : Max supported value is LZ4_MAX_INPUT_SIZE +* *sourceSizePtr: will be modified to indicate how many bytes where read +* from 'source' to fill 'dest'. +* New value is necessarily <= old value. +* workmem : address of the working memory. +* This requires 'workmem' of size LZ4_MEM_COMPRESS. +* return : Nb bytes written into 'dest' (necessarily <= targetDestSize) +or 0 if compression fails +*/ +int LZ4_compress_destSize (const char *source,char *dest,int *sourceSizePtr, +int targetDestSize,void *wrkmem); + +/* * lz4_compress() -* src : source address of the original data -* src_len : size of the original data -* dst : output buffer address of the compressed data -* This requires 'dst' of size LZ4_COMPRESSBOUND. -* dst_len : is the output size, which is returned after compress done -* workmem : address of the working memory. -* This requires 'workmem' of size LZ4_MEM_COMPRESS. -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer and workmem must be already allocated with +* src : source address of the original data +* src_len: size of the original data +* dst : output buffer address of the compressed data +* This requires 'dst' of size LZ4_COMPRESSBOUND. +* dst_len: is the output size, which is returned after compress done +* workmem: address of the working memory. +* This requires 'workmem' of size LZ4_MEM_COMPRESS. +* return : Success if return 0 +* Error if return (< 0) +* note : Destination buffer and workmem must be already allocated with * the defined size. */ -int lz4_compress (const unsigned char *src,size_t src_len, -unsigned char *dst,size_t *dst_len,void *wrkmem); - -/* -* lz4hc_compress() -* src : source address of the original data -* src_len : size of the original data -* dst : output buffer address of the compressed data -* This requires 'dst' of size LZ4_COMPRESSBOUND. -* dst_len : is the output size, which is returned after compress done -* workmem : address of the working memory. -* This requires 'workmem' of size LZ4HC_MEM_COMPRESS. -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer and workmem must be already allocated with -* the defined size. -*/ -int lz4hc_compress (const unsigned char *src,size_t src_len, -unsigned char *dst,size_t *dst_len,void *wrkmem); - -/* -* lz4_decompress() -* src : source address of the compressed data -* src_len : is the input size, whcih is returned after decompress done -* dest : output buffer address of the decompressed data -* actual_dest_len: is the size of uncompressed data, supposing it's known -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer must be already allocated. -* slightly faster than lz4_decompress_unknownoutputsize() +int lz4_compress (const unsigned char *src,size_t src_len,unsigned char *dst, +size_t *dst_len,void *wrkmem); + +/*-************************************************************************ +* Decompression Functions +**************************************************************************/ + +/* +* LZ4_decompress_fast() +* source : source address of the compressed data +* dst : output buffer address of the decompressed data +* originalSize: is the original and therefore uncompressed size +* return : the number of bytes read from the source buffer +* (in other words, the compressed size) +* If the source stream is detected malformed, the function will +* stop decoding and return a negative result. +* Destination buffer must be already allocated. +* Its size must be a minimum of 'originalSize' bytes. +* note : This function fully respect memory boundaries +* for properly formed compressed data. +* It is a bit faster than LZ4_decompress_safe(). +* However, it does not provide any protection against intentionally +* modified data stream (malicious input). +* Use this function in trusted environment only +* (data to decode comes from a trusted source). */ -int lz4_decompress (const unsigned char *src,size_t *src_len, -unsigned char *dest,size_t actual_dest_len); +int LZ4_decompress_fast (const char *source,char *dest,int originalSize); + +/* +* LZ4_decompress_safe() +* source : source address of the compressed data +* dest : output buffer address of the decompressed data +* compressedSize : is the precise full size of the compressed block. +* maxDecompressedSize: is the size of destination buffer, +* which must be already allocated. +* return : the number of bytes decompressed into destination buffer +* (necessarily <= maxDecompressedSize) +* If destination buffer is not large enough, decoding will stop +* and output an error code (<0). +* If the source stream is detected malformed, the function will +* stop decoding and return a negative result. +* This function is protected against buffer overflow exploits, +* including malicious data packets. It never writes outside +* output buffer, nor reads outside input buffer. +*/ +int LZ4_decompress_safe (const char *source,char *dest,int compressedSize, +int maxDecompressedSize);/* -* lz4_decompress_unknownoutputsize() +* LZ4_decompress_safe_partial() : +* This function decompress a compressed block of size 'compressedSize' +* at position 'source' into destination buffer 'dest' +* of size 'maxDecompressedSize'. +* The function tries to stop decompressing operation as soon as +* 'targetOutputSize' has been reached, reducing decompression time. +* +* source : source address of the compressed data +* dest : output buffer address of the decompressed data +* compressedSize : is the precise full size of the compressed block. +* targetOutputSize : the decompression operation will try +* to stop as soon as 'targetOutputSize' +* has been reached +* maxDecompressedSize: is the size of destination buffer, +* which must be already allocated. +* return : the number of bytes decoded in the destination buffer +* (necessarily <= maxDecompressedSize) +* Note : this number can be < 'targetOutputSize' should the compressed +* block to decode be smaller. Always control how many bytes +* were decoded. If the source stream is detected malformed, +* the function will stop decoding and return a negative result. +* This function never writes outside of output buffer, +* and never reads outside of input buffer. +* It is therefore protected against malicious data packets +*/ +int LZ4_decompress_safe_partial (const char *source,char *dest, +int compressedSize,int targetOutputSize,int maxDecompressedSize); + + +/* +* lz4_decompress_unknownoutputsize() : * src : source address of the compressed data * src_len : is the input size, therefore the compressed size -* dest : output buffer address of the decompressed data +* dest : output buffer address of the decompressed data * dest_len: is the max size of the destination buffer, which is -* returned with actual size of decompressed data after -* decompress done -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer must be already allocated. +* returned with actual size of decompressed data after +* decompress done +* return: Success if return 0 +* Error if return (< 0) +* note: Destination buffer must be already allocated. */ int lz4_decompress_unknownoutputsize (const unsigned char *src,size_t src_len, -unsigned char *dest,size_t *dest_len); +unsigned char *dest,size_t *dest_len); + +/* +* lz4_decompress() : +* src : source address of the compressed data +* src_len : is the input size, +* which is returned after decompress done +* dest : output buffer address of the decompressed data +* actual_dest_len: is the size of uncompressed data, supposing it's known +* return: Success if return 0 +* Error if return (< 0) +* note : Destination buffer must be already allocated. +* slightly faster than lz4_decompress_unknownoutputsize() +*/ +int lz4_decompress (const unsigned char *src,size_t *src_len, +unsigned char *dest,size_t actual_dest_len); + +/*-************************************************************************ +* LZ4 HC Compression +**************************************************************************/ + +/*! LZ4_compress_HC() : +* Compress data from `src` into `dst`, using the more powerful +* but slower "HC" algorithm. dst` must be already allocated. +* Compression is guaranteed to succeed if +* `dstCapacity >= LZ4_compressBound(srcSize) +* Max supported `srcSize` value is LZ4_MAX_INPUT_SIZE +* +* src : source address of the original data +* dst : output buffer address of the compressed data +* srcSize : Max supported value is LZ4_MAX_INPUT_SIZE +* dstCapacity : full or partial size of buffer 'dst' +* (which must be already allocated) +* compressionLevel: Recommended values are between 4 and 9, although any +* value between 1 and LZ4HC_MAX_CLEVEL will work. +* Values >LZ4HC_MAX_CLEVEL behave the same as 16. +* wrkmem : address of the working memory. +* This requires 'wrkmem' of size LZ4HC_MEM_COMPRESS. +* return : the number of bytes written into 'dst' +* or 0 if compression fails. +*/ +int LZ4_compress_HC (const char *src,char *dst,int srcSize,int dstCapacity, +int compressionLevel,void *wrkmem); + +/* +* lz4hc_compress() +* src : source address of the original data +* src_len: size of the original data +* dst : output buffer address of the compressed data +* This requires 'dst' of size LZ4_COMPRESSBOUND. +* dst_len: is the output size, which is returned after compress done +* workmem: address of the working memory. +* This requires 'workmem' of size LZ4HC_MEM_COMPRESS. +* return : Success if return 0 +* Error if return (< 0) +* note : Destination buffer and workmem must be already allocated with +* the defined size. +*/ +int lz4hc_compress (const unsigned char *src,size_t src_len,unsigned char *dst, +size_t *dst_len,void *wrkmem); + +/* +* These functions compress data in successive blocks of any size, +* using previous blocks as dictionary. One key assumption is that previous +* blocks (up to 64 KB) remain read-accessible while +* compressing next blocks. +* There is an exception for ring buffers, which can be smaller than 64 KB. +* Ring buffers scenario is automatically detected and handled by +* LZ4_compress_HC_continue(). +* Before starting compression, state must be properly initialized, +* using LZ4_resetStreamHC(). +* A first "fictional block" can then be designated as initial dictionary, +* using LZ4_loadDictHC() (Optional). +* Then, use LZ4_compress_HC_continue() to compress each successive block. +* Previous memory blocks (including initial dictionary when present) must +* remain accessible and unmodified during compression. +* 'dst' buffer should be sized to handle worst case scenarios, using +* LZ4_compressBound(), to ensure operation success. +* If, for any reason, previous data blocks can't be preserved unmodified +* in memory during next compression block, +* you must save it to a safer memory space, using LZ4_saveDictHC(). +* Return value of LZ4_saveDictHC() is the size of dictionary +* effectively saved into 'safeBuffer'. +*/ +void LZ4_resetStreamHC (LZ4_streamHC_t *streamHCPtr,int compressionLevel); +int LZ4_loadDictHC (LZ4_streamHC_t *streamHCPtr,const char *dictionary, +int dictSize); +int LZ4_compress_HC_continue (LZ4_streamHC_t *streamHCPtr,const char *src, +char *dst,int srcSize,int maxDstSize); +int LZ4_saveDictHC (LZ4_streamHC_t *streamHCPtr,char *safeBuffer, +int maxDictSize); + +/*-********************************************* +* Streaming Compression Functions +***********************************************/ +/* +* LZ4_resetStream() : +* An LZ4_stream_t structure can be allocated once +* and re-used multiple times. +* Use this function to init an allocated `LZ4_stream_t` structure +* and start a new compression. +*/ +void LZ4_resetStream (LZ4_stream_t *LZ4_stream); + +/* +* LZ4_loadDict() : +* Use this function to load a static dictionary into LZ4_stream. +* Any previous data will be forgotten, only 'dictionary' +* will remain in memory. +* Loading a size of 0 is allowed. +* Return : dictionary size, in bytes (necessarily <= 64 KB) +*/ +int LZ4_loadDict (LZ4_stream_t *streamPtr,const char *dictionary, +int dictSize); + +/* +* LZ4_compress_fast_continue() : +* Compress buffer content 'src', +* using data from previously compressed blocks +* as dictionary to improve compression ratio. +* Important : Previous data blocks are assumed to still +* be present and unmodified ! +* 'dst' buffer must be already allocated. +* If maxDstSize >= LZ4_compressBound(srcSize), +* compression is guaranteed to succeed, and runs faster. +* If not, and if compressed data cannot fit into 'dst' buffer size, +* compression stops, and function returns a zero. +*/ +int LZ4_compress_fast_continue (LZ4_stream_t *streamPtr,const char *src, +char *dst,int srcSize,int maxDstSize,int acceleration); + +/* +* LZ4_saveDict() : +* If previously compressed data block is not guaranteed +* to remain available at its memory location, +* save it into a safer place (char *safeBuffer). +* Note : you don't need to call LZ4_loadDict() afterwards, +* dictionary is immediately usable, you can therefore call +* LZ4_compress_fast_continue(). +* Return : saved dictionary size in bytes (necessarily <= dictSize), +* or 0 if error. +*/ +int LZ4_saveDict (LZ4_stream_t *streamPtr,char *safeBuffer,int dictSize); + +/* +* LZ4_setStreamDecode() : +* Use this function to instruct where to find the dictionary. +* Setting a size of 0 is allowed (same effect as reset). +* @return : 1 if OK, 0 if error +*/ +int LZ4_setStreamDecode (LZ4_streamDecode_t *LZ4_streamDecode, +const char *dictionary,int dictSize); + +/* +* LZ4_decompress_*_continue() : +* These decoding functions allow decompression of multiple blocks +* in "streaming" mode. +* Previously decoded blocks *must* remain available at the memory position +* where they were decoded (up to 64 KB) +* In the case of a ring buffers, decoding buffer must be either : +* - Exactly same size as encoding buffer, with same update rule +* (block boundaries at same positions) In which case, +* the decoding & encoding ring buffer can have any size, +* including very small ones ( < 64 KB). +* - Larger than encoding buffer, by a minimum of maxBlockSize more bytes. +* maxBlockSize is implementation dependent. +* It's the maximum size you intend to compress into a single block. +* In which case, encoding and decoding buffers do not need +* to be synchronized, and encoding ring buffer can have any size, +* including small ones ( < 64 KB). +* - _At least_ 64 KB + 8 bytes + maxBlockSize. +* In which case, encoding and decoding buffers do not need to be +* synchronized, and encoding ring buffer can have any size, +* including larger than decoding buffer. W +* Whenever these conditions are not possible, save the last 64KB of decoded +* data into a safe buffer, and indicate where it is saved +* using LZ4_setStreamDecode() +*/ +int LZ4_decompress_safe_continue (LZ4_streamDecode_t *LZ4_streamDecode, +const char *source,char *dest,int compressedSize, +int maxDecompressedSize); +int LZ4_decompress_fast_continue (LZ4_streamDecode_t *LZ4_streamDecode, +const char *source,char *dest,int originalSize); + +/* +* LZ4_decompress_*_usingDict() : +* These decoding functions work the same as +* a combination of LZ4_setStreamDecode() followed by +* LZ4_decompress_*_continue() +* They are stand-alone, and don't need an LZ4_streamDecode_t structure. +*/ +int LZ4_decompress_safe_usingDict (const char *source,char *dest, +int compressedSize,int maxDecompressedSize,const char *dictStart, +int dictSize); +int LZ4_decompress_fast_usingDict (const char *source,char *dest, +int originalSize,const char *dictStart,int dictSize); +#endif
lib/lz4/lz4_compress.c +779 -324
1,215 lines not on this page · raw message
lib/lz4/lz4_decompress.c +440 -254
778 lines not on this page · raw message
lib/lz4/lz4defs.h +191 -129
348 lines not on this page · raw message
lib/lz4/lz4hc_compress.c +548 -307
44 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.1.4
Part of the diff is not shown here. View the raw message.
PATCH v5 2/5 lib/decompress_unlz4: Change module to work with new LZ4 module version
In reply to Sven SchmidtThis patch updates the unlz4 wrapper to work with the updated LZ4 kernel module version.
--- lib/decompress_unlz4.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
lib/decompress_unlz4.c +8 -5
@@ -72,7 +72,7 @@ STATIC inline int INIT unlz4(u8 *input, long in_len,error ("NULL input pointer and missing fill function" ); goto exit_1; }else { - inp =large_malloc (lz4_compressbound (uncomp_chunksize)); + inp =large_malloc (LZ4_compressBound (uncomp_chunksize));if (!inp) {error ("Could not allocate input buffer" ); goto exit_1;
@@ -136,7 +136,7 @@ STATIC inline int INIT unlz4(u8 *input, long in_len, inp +=4 ; size -=4 ; }else { -if (chunksize >lz4_compressbound (uncomp_chunksize)) { +if (chunksize >LZ4_compressBound (uncomp_chunksize)) {error ("chunk length is longer than allocated" ); goto exit_2; }
@@ -152,11 +152,14 @@ STATIC inline int INIT unlz4(u8 *input, long in_len, out_len -= dest_len; }else dest_len = out_len; - ret =lz4_decompress (inp, &chunksize, outp, dest_len); + + ret =LZ4_decompress_fast (inp, outp, dest_len); + chunksize = ret;#else dest_len = uncomp_chunksize; - ret =lz4_decompress_unknownoutputsize (inp, chunksize, outp, - &dest_len); + + ret =LZ4_decompress_safe (inp, outp, chunksize, dest_len); + dest_len = ret;#endif if (ret <0 ) {error ("Decoding failed" );
Signature · 1 line
2.1.4
PATCH v5 3/5 crypto: Change LZ4 modules to work with new LZ4 module version
In reply to Sven SchmidtThis patch updates the crypto modules using LZ4 compression to work with the new LZ4 module version.
--- crypto/lz4.c | 21 ++++++++------------- crypto/lz4hc.c | 21 ++++++++------------- 2 files changed, 16 insertions(+), 26 deletions(-)
crypto/lz4.c +8 -13
@@ -66,15 +66,13 @@ static void lz4_exit(struct crypto_tfm *tfm)static int __lz4_compress_crypto (const u8 *src,unsigned int slen,u8 *dst,unsigned int *dlen,void *ctx) { -size_t tmp_len = *dlen; -int err; +int out_len =LZ4_compress_default (src, dst, + slen, (int )((size_t )dlen), ctx); - err =lz4_compress (src, slen, dst, &tmp_len, ctx); - -if (err <0 ) +if (!out_len)return -EINVAL ; - *dlen = tmp_len; + *dlen = out_len;return 0 ; }
@@ -96,16 +94,13 @@ static int lz4_compress_crypto(struct crypto_tfm *tfm, const u8 *src,static int __lz4_decompress_crypto (const u8 *src,unsigned int slen,u8 *dst,unsigned int *dlen,void *ctx) { -int err; -size_t tmp_len = *dlen; -size_t __slen = slen; +int out_len =LZ4_decompress_safe (src, dst, slen, (int )((size_t )dlen)); - err =lz4_decompress_unknownoutputsize (src, __slen, dst, &tmp_len); -if (err <0 ) +if (out_len <0 )return -EINVAL ; - *dlen = tmp_len; -return err; + *dlen = out_len; +return out_len; } static int lz4_sdecompress(struct crypto_scomp *tfm, const u8 *src,
crypto/lz4hc.c +8 -13
@@ -65,15 +65,13 @@ static void lz4hc_exit(struct crypto_tfm *tfm)static int __lz4hc_compress_crypto (const u8 *src,unsigned int slen,u8 *dst,unsigned int *dlen,void *ctx) { -size_t tmp_len = *dlen; -int err; +int out_len =LZ4_compress_HC (src, dst, slen, + (int )((size_t )dlen),LZ4HC_DEFAULT_CLEVEL , ctx); - err =lz4hc_compress (src, slen, dst, &tmp_len, ctx); - -if (err <0 ) +if (out_len ==0 )return -EINVAL ; - *dlen = tmp_len; + *dlen = out_len;return 0 ; }
@@ -97,16 +95,13 @@ static int lz4hc_compress_crypto(struct crypto_tfm *tfm, const u8 *src,static int __lz4hc_decompress_crypto (const u8 *src,unsigned int slen,u8 *dst,unsigned int *dlen,void *ctx) { -int err; -size_t tmp_len = *dlen; -size_t __slen = slen; +int out_len =LZ4_decompress_safe (src, dst, slen, (int )((size_t )dlen)); - err =lz4_decompress_unknownoutputsize (src, __slen, dst, &tmp_len); -if (err <0 ) +if (out_len <0 )return -EINVAL ; - *dlen = tmp_len; -return err; + *dlen = out_len; +return out_len; } static int lz4hc_sdecompress(struct crypto_scomp *tfm, const u8 *src,
Signature · 1 line
2.1.4
PATCH v5 4/5 fs/pstore: fs/squashfs: Change usage of LZ4 to work with new LZ4 version
In reply to Sven SchmidtThis patch updates fs/pstore and fs/squashfs to use the updated functions from the new LZ4 module.
--- fs/pstore/platform.c | 14 +++++++------- fs/squashfs/lz4_wrapper.c | 12 ++++++------ 2 files changed, 13 insertions(+), 13 deletions(-)
fs/pstore/platform.c +7 -7
@@ -342,31 +342,31 @@ static int compress_lz4(const void *in, void *out, size_t inlen, size_t outlen) {int ret; - ret =lz4_compress (in, inlen, out, &outlen, workspace); -if (ret) { + ret =LZ4_compress_default (in, out, inlen, outlen, workspace); +if (!ret) {pr_err ("lz4_compress error, ret = %d!\n" , ret);return -EIO ; } -return outlen; +return ret; }static int decompress_lz4 (void *in,void *out,size_t inlen,size_t outlen) {int ret; - ret =lz4_decompress_unknownoutputsize (in, inlen, out, &outlen); -if (ret) { + ret =LZ4_decompress_safe (in, out, inlen, outlen); +if (ret <0 ) {pr_err ("lz4_decompress error, ret = %d!\n" , ret);return -EIO ; } -return outlen; +return ret; }static void allocate_lz4 (void ) { - big_oops_buf_sz =lz4_compressbound (psinfo->bufsize); + big_oops_buf_sz =LZ4_compressBound (psinfo->bufsize); big_oops_buf =kmalloc (big_oops_buf_sz,GFP_KERNEL );if (big_oops_buf) { workspace =kmalloc (LZ4_MEM_COMPRESS ,GFP_KERNEL );
fs/squashfs/lz4_wrapper.c +6 -6
@@ -97,7 +97,6 @@ static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm,struct squashfs_lz4 *stream = strm;void *buff = stream->input, *data;int avail, i, bytes = length, res; -size_t dest_len = output->length;for (i =0 ; i < b; i++) { avail =min (bytes, msblk->devblksize - offset);
@@ -108,12 +107,13 @@ static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm,put_bh (bh[i]); } - res =lz4_decompress_unknownoutputsize (stream->input, length, - stream->output, &dest_len); -if (res) + res =LZ4_decompress_safe (stream->input, stream->output, + length, output->length); + +if (res <0 )return -EIO ; - bytes = dest_len; + bytes = res; data =squashfs_first_page (output); buff = stream->output;while (data) {
@@ -128,7 +128,7 @@ static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm, }squashfs_finish_page (output); -return dest_len; +return res; }const struct squashfs_decompressor squashfs_lz4_comp_ops = {
Signature · 1 line
2.1.4
PATCH v5 5/5 lib/lz4: Remove back-compat wrappers
In reply to Sven SchmidtThis patch removes the functions introduced as wrappers for providing backwards compatibility to the prior LZ4 version.
They're not needed anymore since there's no callers left.
--- include/linux/lz4.h | 73 ------------------------------------------------ lib/lz4/lz4_compress.c | 22 --------------- lib/lz4/lz4_decompress.c | 42 ---------------------------- lib/lz4/lz4hc_compress.c | 23 --------------- 4 files changed, 160 deletions(-)
include/linux/lz4.h +0 -73
@@ -173,14 +173,6 @@ static inline int LZ4_compressBound(size_t isize) }/* -* For backward compatibility -*/ -static inline int lz4_compressbound (size_t isize) -{ -return LZ4_COMPRESSBOUND (isize); -} - -/* * LZ4_compress_default() * Compresses 'sourceSize' bytes from buffer 'source' * into already allocated 'dest' buffer of size 'maxOutputSize'.
@@ -249,23 +241,6 @@ int LZ4_compress_fast(const char *source, char *dest, int inputSize,int LZ4_compress_destSize (const char *source,char *dest,int *sourceSizePtr,int targetDestSize,void *wrkmem); -/* -* lz4_compress() -* src : source address of the original data -* src_len: size of the original data -* dst : output buffer address of the compressed data -* This requires 'dst' of size LZ4_COMPRESSBOUND. -* dst_len: is the output size, which is returned after compress done -* workmem: address of the working memory. -* This requires 'workmem' of size LZ4_MEM_COMPRESS. -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer and workmem must be already allocated with -* the defined size. -*/ -int lz4_compress (const unsigned char *src,size_t src_len,unsigned char *dst, -size_t *dst_len,void *wrkmem); -/*-************************************************************************ * Decompression Functions **************************************************************************/
@@ -340,37 +315,6 @@ int LZ4_decompress_safe(const char *source, char *dest, int compressedSize,int LZ4_decompress_safe_partial (const char *source,char *dest,int compressedSize,int targetOutputSize,int maxDecompressedSize); - -/* -* lz4_decompress_unknownoutputsize() : -* src : source address of the compressed data -* src_len : is the input size, therefore the compressed size -* dest : output buffer address of the decompressed data -* dest_len: is the max size of the destination buffer, which is -* returned with actual size of decompressed data after -* decompress done -* return: Success if return 0 -* Error if return (< 0) -* note: Destination buffer must be already allocated. -*/ -int lz4_decompress_unknownoutputsize (const unsigned char *src,size_t src_len, -unsigned char *dest,size_t *dest_len); - -/* -* lz4_decompress() : -* src : source address of the compressed data -* src_len : is the input size, -* which is returned after decompress done -* dest : output buffer address of the decompressed data -* actual_dest_len: is the size of uncompressed data, supposing it's known -* return: Success if return 0 -* Error if return (< 0) -* note : Destination buffer must be already allocated. -* slightly faster than lz4_decompress_unknownoutputsize() -*/ -int lz4_decompress (const unsigned char *src,size_t *src_len, -unsigned char *dest,size_t actual_dest_len); -/*-************************************************************************ * LZ4 HC Compression **************************************************************************/
@@ -399,23 +343,6 @@ int LZ4_compress_HC(const char *src, char *dst, int srcSize, int dstCapacity, int compressionLevel, void *wrkmem);/* -* lz4hc_compress() -* src : source address of the original data -* src_len: size of the original data -* dst : output buffer address of the compressed data -* This requires 'dst' of size LZ4_COMPRESSBOUND. -* dst_len: is the output size, which is returned after compress done -* workmem: address of the working memory. -* This requires 'workmem' of size LZ4HC_MEM_COMPRESS. -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer and workmem must be already allocated with -* the defined size. -*/ -int lz4hc_compress (const unsigned char *src,size_t src_len,unsigned char *dst, -size_t *dst_len,void *wrkmem); - -/* * These functions compress data in successive blocks of any size, * using previous blocks as dictionary. One key assumption is that previous * blocks (up to 64 KB) remain read-accessible while
lib/lz4/lz4_compress.c +0 -22
@@ -872,27 +872,5 @@ int LZ4_compress_fast_continue(LZ4_stream_t *LZ4_stream, const char *source, }EXPORT_SYMBOL (LZ4_compress_fast_continue); -/*-****************************** -* For backwards compatibility -********************************/ -int lz4_compress (const unsigned char *src,size_t src_len,unsigned char *dst, -size_t *dst_len,void *wrkmem) { - *dst_len =LZ4_compress_default (src, dst, (int )src_len, - (int )((size_t )dst_len), wrkmem); - -/* -* Prior lz4_compress will return -1 in case of error -* and 0 on success -* while new LZ4_compress_fast/default -* returns 0 in case of error -* and the output length on success -*/ -if (!dst_len) -return -1 ; -else -return 0 ; -} -EXPORT_SYMBOL (lz4_compress); -MODULE_LICENSE ("Dual BSD/GPL" );MODULE_DESCRIPTION ("LZ4 compressor" );
lib/lz4/lz4_decompress.c +0 -42
47 lines not on this page · raw message
lib/lz4/lz4hc_compress.c +0 -23
@@ -603,29 +603,6 @@ int LZ4_compress_HC(const char *src, char *dst, int srcSize, }EXPORT_SYMBOL (LZ4_compress_HC); -/*-****************************** -* For backwards compatibility -********************************/ -int lz4hc_compress (const unsigned char *src,size_t src_len, -unsigned char *dst,size_t *dst_len,void *wrkmem) -{ - *dst_len =LZ4_compress_HC (src, dst, (int )src_len, - (int )((size_t )dst_len),LZ4HC_DEFAULT_CLEVEL , wrkmem); - -/* -* Prior lz4hc_compress will return -1 in case of error -* and 0 on success -* while new LZ4_compress_HC -* returns 0 in case of error -* and the output length on success -*/ -if (!dst_len) -return -1 ; -else -return 0 ; -} -EXPORT_SYMBOL (lz4hc_compress); -/************************************** * Streaming Functions **************************************/
Signature · 1 line
2.1.4
Quoted 10 lines · Sven Schmidt
This patchset is for updating the LZ4 compression module to a version based on LZ4 v1.7.3 allowing to use the fast compression algorithm aka LZ4 fast which provides an "acceleration" parameter as a tradeoff between high compression ratio and high compression speed.
We want to use LZ4 fast in order to support compression in lustre and (mostly, based on that) investigate data reduction techniques in behalf of storage systems.
Quoted 6 lines
Also, it will be useful for other users of LZ4 compression, as with LZ4 fast it is possible to enable applications to use fast and/or high compression depending on the usecase.
For instance, ZRAM is offering a LZ4 backend and could benefit from an updated LZ4 in the kernel.
Hi Sven,
[For some reason I didn't receive patch 1/5 and had to get it from patchwork... I'm not sure why. I'm subscribed to linux-crypto but not linux-kernel.]
The proposed patch defines LZ4_MEMORY_USAGE to 10 which means that LZ4 compression will use a hash table of only 1024 bytes, containing only 256 entries, to find matches. This differs from upstream LZ4 1.7.3, which uses LZ4_MEMORY_USAGE of 14, as well as the previous LZ4 included in the Linux kernel, both of which specify the hash table size to be 16384 bytes, containing 4096 entries.
Given that varying the hash table size is a trade-off between memory usage, speed, and compression ratio, is this an intentional difference and has it been benchmarked?
Also, in lz4defs.h:
Quoted 5 lines
#if
defined(__x86_64__)
typedef U64reg_t; /* 64-bits in x32 mode */
#else
typedefsize_treg_t; /* 32-bits in x32 mode */
#endif
Are you sure this really needed over just always using size_t?
Quoted 5 lines
#if
LZ4_ARCH64
#ifdef__BIG_ENDIAN__
#defineLZ4_NBCOMMONBYTES(val)(__builtin_clzll(val)>> 3) #else
#defineLZ4_NBCOMMONBYTES(val)(__builtin_clzll(val)>> 3)
Quoted 8 lines
#endif
#else
#ifdef__BIG_ENDIAN__
#defineLZ4_NBCOMMONBYTES(val)(__builtin_clz(val)>> 3) #else
#defineLZ4_NBCOMMONBYTES(val)(__builtin_ctz(val)>> 3) #endif
#endif
LZ4_NBCOMMONBYTES() is defined incorrectly for 64-bit little endian; it should be using __builtin_ctzll().
Nit: can you also clean up the weird indentation (e.g. double tabs) in lz4defs.h?
Thanks,
Eric
Quoted 11 lines · Eric Biggers
Quoted 10 lines · Sven Schmidt
This patchset is for updating the LZ4 compression module to a version based on LZ4 v1.7.3 allowing to use the fast compression algorithm aka LZ4 fast which provides an "acceleration" parameter as a tradeoff between high compression ratio and high compression speed.
We want to use LZ4 fast in order to support compression in lustre and (mostly, based on that) investigate data reduction techniques in behalf of storage systems.
Quoted 7 lines
Quoted 6 lines
Also, it will be useful for other users of LZ4 compression, as with LZ4 fast it is possible to enable applications to use fast and/or high compression depending on the usecase.
For instance, ZRAM is offering a LZ4 backend and could benefit from an updated LZ4 in the kernel.
Hey Eric,
Quoted 4 lines
Hi Sven,
[For some reason I didn't receive patch 1/5 and had to get it from patchwork... I'm not sure why. I'm subscribed to linux-crypto but not linux-kernel.]
that's weird. I just experienced the first patch takes a little longer to get delivered because of its size. Please let me know if the problem occurs again.
Quoted 11 lines
The proposed patch defines
LZ4_MEMORY_USAGEto 10 which means that LZ4 compression will use a hash table of only 1024 bytes, containing only 256 entries, to find matches. This differs from upstream LZ4 1.7.3, which usesLZ4_MEMORY_USAGEof 14, as well as the previous LZ4 included in the Linux kernel, both of which specify the hash table size to be 16384 bytes, containing 4096 entries.Given that varying the hash table size is a trade-off between memory usage, speed, and compression ratio, is this an intentional difference and has it been benchmarked?
I believe I had some troubles with LZ4_MEMORY_USAGE of 14. But I may be wrong. I will test that again and eventually adapt that value.
Quoted 10 lines
Also, in
lz4defs.h:Quoted 5 lines
#if
defined(__x86_64__)
typedef U64reg_t; /* 64-bits in x32 mode */
#else
typedefsize_treg_t; /* 32-bits in x32 mode */
#endifAre you sure this really needed over just always using
size_t?
No, actually there's just one use of that value and the upstream version uses size_t instead of reg_t in that particular place. So I will replace it with size_t.
Quoted 14 lines
Quoted 5 lines
#if
LZ4_ARCH64
#ifdef__BIG_ENDIAN__
#defineLZ4_NBCOMMONBYTES(val)(__builtin_clzll(val)>> 3) #else
#defineLZ4_NBCOMMONBYTES(val)(__builtin_clzll(val)>> 3)Quoted 8 lines
#endif
#else
#ifdef__BIG_ENDIAN__
#defineLZ4_NBCOMMONBYTES(val)(__builtin_clz(val)>> 3) #else
#defineLZ4_NBCOMMONBYTES(val)(__builtin_ctz(val)>> 3) #endif
#endif
Quoted 3 lines
LZ4_NBCOMMONBYTES()is defined incorrectly for 64-bit little endian; it should be using__builtin_ctzll().
Indeed! Using the same values in if and else does not make sense at all. Thank you for pointing that one out. I will fix it.
Quoted 7 lines
Nit: can you also clean up the weird indentation (e.g. double tabs) in
lz4defs.h?Thanks,
Eric
I'm wondering why checkpatch does not point out this kind of styling problem? I did fix that in the other files but I think I missed lz4defs.h. Will fix the indentation.
Thanks,
Sven
v6 cover Update LZ4 compressor module
In reply to Sven SchmidtThis patchset is for updating the LZ4 compression module to a version based on LZ4 v1.7.3 allowing to use the fast compression algorithm aka LZ4 fast which provides an "acceleration" parameter as a tradeoff between high compression ratio and high compression speed.
We want to use LZ4 fast in order to support compression in lustre and (mostly, based on that) investigate data reduction techniques in behalf of storage systems.
Also, it will be useful for other users of LZ4 compression, as with LZ4 fast it is possible to enable applications to use fast and/or high compression depending on the usecase.
For instance, ZRAM is offering a LZ4 backend and could benefit from an updated LZ4 in the kernel.
LZ4 homepage: http://www.lz4.org/
LZ4 source repository: https://github.com/lz4/lz4
Source version: 1.7.3
Benchmark (taken from [1], Core i5-4300U @1.9GHz):
----------------|--------------|----------------|---------- Compressor | Compression | Decompression | Ratio ----------------|--------------|----------------|---------- memcpy | 4200 MB/s | 4200 MB/s | 1.000 LZ4 fast 50 | 1080 MB/s | 2650 MB/s | 1.375 LZ4 fast 17 | 680 MB/s | 2220 MB/s | 1.607 LZ4 fast 5 | 475 MB/s | 1920 MB/s | 1.886 LZ4 default | 385 MB/s | 1850 MB/s | 2.101
[1] http://fastcompression.blogspot.de/2015/04/sampling-or-faster-lz4.html
fs/pstore: fs/squashfs: Change usage of LZ4 to work with new LZ4 version
[PATCH 1/5] lib: Update LZ4 compressor module
[PATCH 2/5] lib/decompress_unlz4: Change module to work with new LZ4 module version [PATCH 3/5] crypto: Change LZ4 modules to work with new LZ4 module version [PATCH 4/5] fs/pstore: fs/squashfs: Change usage of LZ4 to work with new LZ4 version [PATCH 5/5] lib/lz4: Remove back-compat wrappers
v2:
- Changed order of the patches since in the initial patchset the lz4.h was in the last patch but was referenced by the other ones
- Split lib/decompress_unlz4.c in an own patch
- Fixed errors reported by the buildbot
- Further refactorings
- Added more appropriate copyright note to include/linux/lz4.h
v3:
- Adjusted the code to satisfy kernel coding style (checkpatch.pl)
- Made sure the changes to LZ4 in Kernel (overflow checks etc.) are included in the new module (they are)
- Removed the second LZ4_compressBound function with related name but different return type
- Corrected version number (was LZ4 1.7.3)
- Added missing LZ4 streaming functions
v4:
- Fixed kbuild errors
- Re-added lz4_compressbound as alias for LZ4_compressBound to ensure backwards compatibility
- Wrapped LZ4_hash5 with check for LZ4_ARCH64 since it is only used there and triggers an unused function warning when false
v5:
- Added a fifth patch to remove the back-compat wrappers introduced to ensure bisectibility between the patches (the functions are no longer needed since there's no callers left)
v6:
- Fixed LZ4_NBCOMMONBYTES() for 64-bit little endian
- Reset LZ4_MEMORY_USAGE to 14 (which is the value used in upstream LZ4 as well as the previous kernel module)
- Fixed that weird double-indentation in lz4defs.h and lz4.h
- Adjusted general styling issues in lz4defs.h (e.g. lines consisting of more than one instruction)
- Removed the architecture-dependent typedef to reg_t since upstream LZ4 is just using size_t and that works fine
- Changed error messages in pstore/platform.c:
* LZ4_compress_default always returns 0 in case of an error (no need to print the return value)
* LZ4_decompress_safe returns a negative error message (return value _does_ matter)
PATCH v6 1/5 lib: Update LZ4 compressor module
This patch updates LZ4 kernel module to LZ4 v1.7.3 by Yann Collet. The kernel module is inspired by the previous work by Chanho Min. The updated LZ4 module will not break existing code since the patchset contains appropriate changes.
API changes:
New method LZ4_compress_fast which differs from the variant available in kernel by the new acceleration parameter,
allowing to trade compression ratio for more compression speed and vice versa.
LZ4_decompress_fast is the respective decompression method, featuring a very fast decoder (multiple GB/s per core), able to reach RAM speed in multi-core systems.
The decompressor allows to decompress data compressed with LZ4 fast as well as the LZ4 HC (high compression) algorithm.
Also the useful functions LZ4_decompress_safe_partial LZ4_compress_destsize were added. The latter reverses the logic by trying to compress as much data as possible from source to dest while the former aims to decompress partial blocks of data.
A bunch of streaming functions were also added which allow compressig/decompressing data in multiple steps (so called "streaming mode").
The methods lz4_compress and lz4_decompress_unknownoutputsize are now known as LZ4_compress_default respectivley LZ4_decompress_safe. The old methods will be removed since there's no callers left in the code.
--- include/linux/lz4.h | 589 ++++++++++++++++++++++--- lib/lz4/lz4_compress.c | 1103 ++++++++++++++++++++++++++++++++-------------- lib/lz4/lz4_decompress.c | 696 ++++++++++++++++++----------- lib/lz4/lz4defs.h | 334 ++++++++------ lib/lz4/lz4hc_compress.c | 855 ++++++++++++++++++++++------------- 5 files changed, 2500 insertions(+), 1077 deletions(-)
include/linux/lz4.h +528 -61
@@ -1,87 +1,554 @@ -#ifndef __LZ4_H__ -#define __LZ4_H__ -/* -* LZ4 Kernel Interface +/* LZ4 Kernel Interface * * Copyright (C) 2013, LG Electronics, Kyungsik Lee <[email protected]> +* Copyright (C) 2016, Sven Schmidt <[email protected]> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. +* +* This file is based on the original header file +* for LZ4 - Fast LZ compression algorithm. +* +* LZ4 - Fast LZ compression algorithm +* Copyright (C) 2011-2016, Yann Collet. +* BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are +* met: +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above +* copyright notice, this list of conditions and the following disclaimer +* in the documentation and/or other materials provided with the +* distribution. +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* You can contact the author at : +* - LZ4 homepage : http://www.lz4.org +* - LZ4 source repository : https://github.com/lz4/lz4 +*/ + +#ifndef __LZ4_H__ +#define __LZ4_H__ + +#include <linux/types.h> +#include <linux/string.h> /* memset, memcpy */ + +/*-************************************************************************ +* CONSTANTS +**************************************************************************/ +/* +* LZ4_MEMORY_USAGE : +* Memory usage formula : N->2^N Bytes +* (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.) +* Increasing memory usage improves compression ratio +* Reduced memory usage can improve speed, due to cache effect +* Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache +*/ +#define LZ4_MEMORY_USAGE 14 + +#define LZ4_MAX_INPUT_SIZE 0x7E000000/* 2 113 929 216 bytes */ +#define LZ4_COMPRESSBOUND (isize) (\ + (unsigned int)(isize) > (unsigned int)LZ4_MAX_INPUT_SIZE \ + ? 0 \ + : (isize) + ((isize)/255) + 16) + +#define LZ4_ACCELERATION_DEFAULT 1 +#define LZ4_HASHLOG (LZ4_MEMORY_USAGE-2) +#define LZ4_HASHTABLESIZE (1 << LZ4_MEMORY_USAGE) +#define LZ4_HASH_SIZE_U32 (1 << LZ4_HASHLOG) + +#define LZ4HC_MIN_CLEVEL 3 +#define LZ4HC_DEFAULT_CLEVEL 9 +#define LZ4HC_MAX_CLEVEL 16 + +#define LZ4HC_DICTIONARY_LOGSIZE 16 +#define LZ4HC_MAXD (1<<LZ4HC_DICTIONARY_LOGSIZE) +#define LZ4HC_MAXD_MASK (LZ4HC_MAXD - 1) +#define LZ4HC_HASH_LOG (LZ4HC_DICTIONARY_LOGSIZE-1) +#define LZ4HC_HASHTABLESIZE (1 << LZ4HC_HASH_LOG) +#define LZ4HC_HASH_MASK (LZ4HC_HASHTABLESIZE - 1) + +/*-************************************************************************ +* STREAMING CONSTANTS AND STRUCTURES +**************************************************************************/ +#define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE-3)) + 4) +#define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U64 * sizeof(unsigned long long)) + +#define LZ4_STREAMHCSIZE 262192 +#define LZ4_STREAMHCSIZE_SIZET (262192 / sizeof(size_t)) + +#define LZ4_STREAMDECODESIZE_U64 4 +#define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * \ + sizeof(unsigned long long)) + +/* +* LZ4_stream_t : +* information structure to track an LZ4 stream. +*/ +typedef struct { +uint32_t hashTable[LZ4_HASH_SIZE_U32 ]; +uint32_t currentOffset; +uint32_t initCheck; +const uint8_t *dictionary; +uint8_t *bufferStart; +uint32_t dictSize; +}LZ4_stream_t_internal ; +typedef union { +unsigned long long table[LZ4_STREAMSIZE_U64 ]; +LZ4_stream_t_internal internal_donotuse; +}LZ4_stream_t ; + +/* +* LZ4_streamHC_t : +* information structure to track an LZ4HC stream. */ -#define LZ4_MEM_COMPRESS (16384) -#define LZ4HC_MEM_COMPRESS (262144 + (2 * sizeof(unsigned char *))) +typedef struct { +unsigned int hashTable[LZ4HC_HASHTABLESIZE ]; +unsigned short chainTable[LZ4HC_MAXD ]; +/* next block to continue on current prefix */ +const unsigned char *end; +/* All index relative to this position */ +const unsigned char *base; +/* alternate base for extDict */ +const unsigned char *dictBase; +/* below that point, need extDict */ +unsigned int dictLimit; +/* below that point, no more dict */ +unsigned int lowLimit; +/* index from which to continue dict update */ +unsigned int nextToUpdate; +unsigned int compressionLevel; +}LZ4HC_CCtx_internal ; +typedef union { +size_t table[LZ4_STREAMHCSIZE_SIZET ]; +LZ4HC_CCtx_internal internal_donotuse; +}LZ4_streamHC_t ;/* -* lz4_compressbound() -* Provides the maximum size that LZ4 may output in a "worst case" scenario -* (input data not compressible) +* LZ4_streamDecode_t : +* information structure to track an LZ4 stream during decompression. +* init this structure using LZ4_setStreamDecode +* (or memset()) before first use */ -static inline size_t lz4_compressbound (size_t isize) +typedef struct { +const uint8_t *externalDict; +size_t extDictSize; +const uint8_t *prefixEnd; +size_t prefixSize; +}LZ4_streamDecode_t_internal ; +typedef union { +unsigned long long table[LZ4_STREAMDECODESIZE_U64 ]; +LZ4_streamDecode_t_internal internal_donotuse; +}LZ4_streamDecode_t ; + +/*-************************************************************************ +* SIZE OF STATE +**************************************************************************/ +#define LZ4_MEM_COMPRESS LZ4_STREAMSIZE +#define LZ4HC_MEM_COMPRESS LZ4_STREAMHCSIZE + +/*-************************************************************************ +* Compression Functions +**************************************************************************/ + +/* +* LZ4_compressBound() : +* Provides the maximum size that LZ4 may output in a "worst case" scenario +* (input data not compressible) +*/ +static inline int LZ4_compressBound (size_t isize) +{ +return LZ4_COMPRESSBOUND (isize); +} + +/* +* For backward compatibility +*/ +static inline int lz4_compressbound (size_t isize) { -return isize + (isize /255 ) +16 ; +return LZ4_COMPRESSBOUND (isize); }/* +* LZ4_compress_default() +* Compresses 'sourceSize' bytes from buffer 'source' +* into already allocated 'dest' buffer of size 'maxOutputSize'. +* Compression is guaranteed to succeed if +* 'maxOutputSize' >= LZ4_compressBound(inputSize). +* It also runs faster, so it's a recommended setting. +* If the function cannot compress 'source' +* into a more limited 'dest' budget, +* compression stops *immediately*, +* and the function result is zero. +* As a consequence, 'dest' content is not valid. +* +* source : source address of the original data +* dest : output buffer address +* of the compressed data +* inputSize : Max supported value is +* LZ4_MAX_INPUT_SIZE +* maxOutputSize: full or partial size of buffer 'dest' +* (which must be already allocated) +* workmem : address of the working memory. +* This requires 'workmem' of size LZ4_MEM_COMPRESS. +* return : the number of bytes written into buffer 'dest' +* (necessarily <= maxOutputSize) or 0 if compression fails +*/ +int LZ4_compress_default (const char *source,char *dest,int inputSize, +int maxOutputSize,void *wrkmem); + +/* +* LZ4_compress_fast() : +* Same as LZ4_compress_default(), +* but allows to select an "acceleration" factor. +* The larger the acceleration value, +* the faster the algorithm, +* but also the lesser the compression. +* It's a trade-off. It can be fine tuned, +* with each successive value +* providing roughly +~3% to speed. +* An acceleration value of "1" +* is the same as regular LZ4_compress_default() +* Values <= 0 will be replaced by +* LZ4_ACCELERATION_DEFAULT, which is 1. +*/ +int LZ4_compress_fast (const char *source,char *dest,int inputSize, +int maxOutputSize,void *wrkmem,int acceleration); + +/* +* LZ4_compress_destSize() +* Reverse the logic, by compressing as much data as possible +* from 'source' buffer into already allocated buffer 'dest' +* of size 'targetDestSize'. +* This function either compresses the entire 'source' content into 'dest' +* if it's large enough, or fill 'dest' buffer completely with as much data as +* ossible from 'source'. +* +* source : source address of the original data +* dest : output buffer address of the compressed data +* inputSize : Max supported value is LZ4_MAX_INPUT_SIZE +* *sourceSizePtr: will be modified to indicate how many bytes where read +* from 'source' to fill 'dest'. +* New value is necessarily <= old value. +* workmem : address of the working memory. +* This requires 'workmem' of size LZ4_MEM_COMPRESS. +* return : Nb bytes written into 'dest' (necessarily <= targetDestSize) +or 0 if compression fails +*/ +int LZ4_compress_destSize (const char *source,char *dest,int *sourceSizePtr, +int targetDestSize,void *wrkmem); + +/* * lz4_compress() -* src : source address of the original data -* src_len : size of the original data -* dst : output buffer address of the compressed data -* This requires 'dst' of size LZ4_COMPRESSBOUND. -* dst_len : is the output size, which is returned after compress done -* workmem : address of the working memory. -* This requires 'workmem' of size LZ4_MEM_COMPRESS. -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer and workmem must be already allocated with +* src : source address of the original data +* src_len: size of the original data +* dst : output buffer address of the compressed data +* This requires 'dst' of size LZ4_COMPRESSBOUND. +* dst_len: is the output size, which is returned after compress done +* workmem: address of the working memory. +* This requires 'workmem' of size LZ4_MEM_COMPRESS. +* return : Success if return 0 +* Error if return (< 0) +* note : Destination buffer and workmem must be already allocated with * the defined size. */ -int lz4_compress (const unsigned char *src,size_t src_len, -unsigned char *dst,size_t *dst_len,void *wrkmem); - -/* -* lz4hc_compress() -* src : source address of the original data -* src_len : size of the original data -* dst : output buffer address of the compressed data -* This requires 'dst' of size LZ4_COMPRESSBOUND. -* dst_len : is the output size, which is returned after compress done -* workmem : address of the working memory. -* This requires 'workmem' of size LZ4HC_MEM_COMPRESS. -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer and workmem must be already allocated with -* the defined size. -*/ -int lz4hc_compress (const unsigned char *src,size_t src_len, -unsigned char *dst,size_t *dst_len,void *wrkmem); - -/* -* lz4_decompress() -* src : source address of the compressed data -* src_len : is the input size, whcih is returned after decompress done -* dest : output buffer address of the decompressed data -* actual_dest_len: is the size of uncompressed data, supposing it's known -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer must be already allocated. -* slightly faster than lz4_decompress_unknownoutputsize() +int lz4_compress (const unsigned char *src,size_t src_len,unsigned char *dst, +size_t *dst_len,void *wrkmem); + +/*-************************************************************************ +* Decompression Functions +**************************************************************************/ + +/* +* LZ4_decompress_fast() +* source : source address of the compressed data +* dst : output buffer address of the decompressed data +* originalSize: is the original and therefore uncompressed size +* return : the number of bytes read from the source buffer +* (in other words, the compressed size) +* If the source stream is detected malformed, the function will +* stop decoding and return a negative result. +* Destination buffer must be already allocated. +* Its size must be a minimum of 'originalSize' bytes. +* note : This function fully respect memory boundaries +* for properly formed compressed data. +* It is a bit faster than LZ4_decompress_safe(). +* However, it does not provide any protection against intentionally +* modified data stream (malicious input). +* Use this function in trusted environment only +* (data to decode comes from a trusted source). */ -int lz4_decompress (const unsigned char *src,size_t *src_len, -unsigned char *dest,size_t actual_dest_len); +int LZ4_decompress_fast (const char *source,char *dest,int originalSize); + +/* +* LZ4_decompress_safe() +* source : source address of the compressed data +* dest : output buffer address of the decompressed data +* compressedSize : is the precise full size of the compressed block. +* maxDecompressedSize: is the size of destination buffer, +* which must be already allocated. +* return : the number of bytes decompressed into destination buffer +* (necessarily <= maxDecompressedSize) +* If destination buffer is not large enough, decoding will stop +* and output an error code (<0). +* If the source stream is detected malformed, the function will +* stop decoding and return a negative result. +* This function is protected against buffer overflow exploits, +* including malicious data packets. It never writes outside +* output buffer, nor reads outside input buffer. +*/ +int LZ4_decompress_safe (const char *source,char *dest,int compressedSize, +int maxDecompressedSize);/* -* lz4_decompress_unknownoutputsize() +* LZ4_decompress_safe_partial() : +* This function decompress a compressed block of size 'compressedSize' +* at position 'source' into destination buffer 'dest' +* of size 'maxDecompressedSize'. +* The function tries to stop decompressing operation as soon as +* 'targetOutputSize' has been reached, reducing decompression time. +* +* source : source address of the compressed data +* dest : output buffer address of the decompressed data +* compressedSize : is the precise full size of the compressed block. +* targetOutputSize : the decompression operation will try +* to stop as soon as 'targetOutputSize' +* has been reached +* maxDecompressedSize: is the size of destination buffer, +* which must be already allocated. +* return : the number of bytes decoded in the destination buffer +* (necessarily <= maxDecompressedSize) +* Note : this number can be < 'targetOutputSize' should the compressed +* block to decode be smaller. Always control how many bytes +* were decoded. If the source stream is detected malformed, +* the function will stop decoding and return a negative result. +* This function never writes outside of output buffer, +* and never reads outside of input buffer. +* It is therefore protected against malicious data packets +*/ +int LZ4_decompress_safe_partial (const char *source,char *dest, +int compressedSize,int targetOutputSize,int maxDecompressedSize); + + +/* +* lz4_decompress_unknownoutputsize() : * src : source address of the compressed data * src_len : is the input size, therefore the compressed size -* dest : output buffer address of the decompressed data +* dest : output buffer address of the decompressed data * dest_len: is the max size of the destination buffer, which is -* returned with actual size of decompressed data after -* decompress done -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer must be already allocated. +* returned with actual size of decompressed data after +* decompress done +* return: Success if return 0 +* Error if return (< 0) +* note: Destination buffer must be already allocated. */ int lz4_decompress_unknownoutputsize (const unsigned char *src,size_t src_len, -unsigned char *dest,size_t *dest_len); +unsigned char *dest,size_t *dest_len); + +/* +* lz4_decompress() : +* src : source address of the compressed data +* src_len : is the input size, +* which is returned after decompress done +* dest : output buffer address of the decompressed data +* actual_dest_len: is the size of uncompressed data, supposing it's known +* return: Success if return 0 +* Error if return (< 0) +* note : Destination buffer must be already allocated. +* slightly faster than lz4_decompress_unknownoutputsize() +*/ +int lz4_decompress (const unsigned char *src,size_t *src_len, +unsigned char *dest,size_t actual_dest_len); + +/*-************************************************************************ +* LZ4 HC Compression +**************************************************************************/ + +/*! LZ4_compress_HC() : +* Compress data from `src` into `dst`, using the more powerful +* but slower "HC" algorithm. dst` must be already allocated. +* Compression is guaranteed to succeed if +* `dstCapacity >= LZ4_compressBound(srcSize) +* Max supported `srcSize` value is LZ4_MAX_INPUT_SIZE +* +* src : source address of the original data +* dst : output buffer address of the compressed data +* srcSize : Max supported value is LZ4_MAX_INPUT_SIZE +* dstCapacity : full or partial size of buffer 'dst' +* (which must be already allocated) +* compressionLevel: Recommended values are between 4 and 9, although any +* value between 1 and LZ4HC_MAX_CLEVEL will work. +* Values >LZ4HC_MAX_CLEVEL behave the same as 16. +* wrkmem : address of the working memory. +* This requires 'wrkmem' of size LZ4HC_MEM_COMPRESS. +* return : the number of bytes written into 'dst' +* or 0 if compression fails. +*/ +int LZ4_compress_HC (const char *src,char *dst,int srcSize,int dstCapacity, +int compressionLevel,void *wrkmem); + +/* +* lz4hc_compress() +* src : source address of the original data +* src_len: size of the original data +* dst : output buffer address of the compressed data +* This requires 'dst' of size LZ4_COMPRESSBOUND. +* dst_len: is the output size, which is returned after compress done +* workmem: address of the working memory. +* This requires 'workmem' of size LZ4HC_MEM_COMPRESS. +* return : Success if return 0 +* Error if return (< 0) +* note : Destination buffer and workmem must be already allocated with +* the defined size. +*/ +int lz4hc_compress (const unsigned char *src,size_t src_len,unsigned char *dst, +size_t *dst_len,void *wrkmem); + +/* +* These functions compress data in successive blocks of any size, +* using previous blocks as dictionary. One key assumption is that previous +* blocks (up to 64 KB) remain read-accessible while +* compressing next blocks. +* There is an exception for ring buffers, which can be smaller than 64 KB. +* Ring buffers scenario is automatically detected and handled by +* LZ4_compress_HC_continue(). +* Before starting compression, state must be properly initialized, +* using LZ4_resetStreamHC(). +* A first "fictional block" can then be designated as initial dictionary, +* using LZ4_loadDictHC() (Optional). +* Then, use LZ4_compress_HC_continue() to compress each successive block. +* Previous memory blocks (including initial dictionary when present) must +* remain accessible and unmodified during compression. +* 'dst' buffer should be sized to handle worst case scenarios, using +* LZ4_compressBound(), to ensure operation success. +* If, for any reason, previous data blocks can't be preserved unmodified +* in memory during next compression block, +* you must save it to a safer memory space, using LZ4_saveDictHC(). +* Return value of LZ4_saveDictHC() is the size of dictionary +* effectively saved into 'safeBuffer'. +*/ +void LZ4_resetStreamHC (LZ4_streamHC_t *streamHCPtr,int compressionLevel); +int LZ4_loadDictHC (LZ4_streamHC_t *streamHCPtr,const char *dictionary, +int dictSize); +int LZ4_compress_HC_continue (LZ4_streamHC_t *streamHCPtr,const char *src, +char *dst,int srcSize,int maxDstSize); +int LZ4_saveDictHC (LZ4_streamHC_t *streamHCPtr,char *safeBuffer, +int maxDictSize); + +/*-********************************************* +* Streaming Compression Functions +***********************************************/ +/* +* LZ4_resetStream() : +* An LZ4_stream_t structure can be allocated once +* and re-used multiple times. +* Use this function to init an allocated `LZ4_stream_t` structure +* and start a new compression. +*/ +void LZ4_resetStream (LZ4_stream_t *LZ4_stream); + +/* +* LZ4_loadDict() : +* Use this function to load a static dictionary into LZ4_stream. +* Any previous data will be forgotten, only 'dictionary' +* will remain in memory. +* Loading a size of 0 is allowed. +* Return : dictionary size, in bytes (necessarily <= 64 KB) +*/ +int LZ4_loadDict (LZ4_stream_t *streamPtr,const char *dictionary, +int dictSize); + +/* +* LZ4_compress_fast_continue() : +* Compress buffer content 'src', +* using data from previously compressed blocks +* as dictionary to improve compression ratio. +* Important : Previous data blocks are assumed to still +* be present and unmodified ! +* 'dst' buffer must be already allocated. +* If maxDstSize >= LZ4_compressBound(srcSize), +* compression is guaranteed to succeed, and runs faster. +* If not, and if compressed data cannot fit into 'dst' buffer size, +* compression stops, and function returns a zero. +*/ +int LZ4_compress_fast_continue (LZ4_stream_t *streamPtr,const char *src, +char *dst,int srcSize,int maxDstSize,int acceleration); + +/* +* LZ4_saveDict() : +* If previously compressed data block is not guaranteed +* to remain available at its memory location, +* save it into a safer place (char *safeBuffer). +* Note : you don't need to call LZ4_loadDict() afterwards, +* dictionary is immediately usable, you can therefore call +* LZ4_compress_fast_continue(). +* Return : saved dictionary size in bytes (necessarily <= dictSize), +* or 0 if error. +*/ +int LZ4_saveDict (LZ4_stream_t *streamPtr,char *safeBuffer,int dictSize); + +/* +* LZ4_setStreamDecode() : +* Use this function to instruct where to find the dictionary. +* Setting a size of 0 is allowed (same effect as reset). +* @return : 1 if OK, 0 if error +*/ +int LZ4_setStreamDecode (LZ4_streamDecode_t *LZ4_streamDecode, +const char *dictionary,int dictSize); + +/* +* LZ4_decompress_*_continue() : +* These decoding functions allow decompression of multiple blocks +* in "streaming" mode. +* Previously decoded blocks *must* remain available at the memory position +* where they were decoded (up to 64 KB) +* In the case of a ring buffers, decoding buffer must be either : +* - Exactly same size as encoding buffer, with same update rule +* (block boundaries at same positions) In which case, +* the decoding & encoding ring buffer can have any size, +* including very small ones ( < 64 KB). +* - Larger than encoding buffer, by a minimum of maxBlockSize more bytes. +* maxBlockSize is implementation dependent. +* It's the maximum size you intend to compress into a single block. +* In which case, encoding and decoding buffers do not need +* to be synchronized, and encoding ring buffer can have any size, +* including small ones ( < 64 KB). +* - _At least_ 64 KB + 8 bytes + maxBlockSize. +* In which case, encoding and decoding buffers do not need to be +* synchronized, and encoding ring buffer can have any size, +* including larger than decoding buffer. W +* Whenever these conditions are not possible, save the last 64KB of decoded +* data into a safe buffer, and indicate where it is saved +* using LZ4_setStreamDecode() +*/ +int LZ4_decompress_safe_continue (LZ4_streamDecode_t *LZ4_streamDecode, +const char *source,char *dest,int compressedSize, +int maxDecompressedSize); +int LZ4_decompress_fast_continue (LZ4_streamDecode_t *LZ4_streamDecode, +const char *source,char *dest,int originalSize); + +/* +* LZ4_decompress_*_usingDict() : +* These decoding functions work the same as +* a combination of LZ4_setStreamDecode() followed by +* LZ4_decompress_*_continue() +* They are stand-alone, and don't need an LZ4_streamDecode_t structure. +*/ +int LZ4_decompress_safe_usingDict (const char *source,char *dest, +int compressedSize,int maxDecompressedSize,const char *dictStart, +int dictSize); +int LZ4_decompress_fast_usingDict (const char *source,char *dest, +int originalSize,const char *dictStart,int dictSize); +#endif
lib/lz4/lz4_compress.c +779 -324
1,215 lines not on this page · raw message
lib/lz4/lz4_decompress.c +442 -254
780 lines not on this page · raw message
lib/lz4/lz4defs.h +203 -131
360 lines not on this page · raw message
lib/lz4/lz4hc_compress.c +548 -307
30 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.1.4
Part of the diff is not shown here. View the raw message.
Re: [PATCH v6 1/5] lib: Update LZ4 compressor module
On Fri, 27 Jan 2017 23:02:00 +0100
Sven Schmidt <[email protected]> wrote:
I have one quick question...
Quoted 25 lines
/*
+ *LZ4_compress_default()
+ * Compresses 'sourceSize' bytes from buffer 'source'
+ * into already allocated 'dest' buffer of size 'maxOutputSize'.
+ * Compression is guaranteed to succeed if
+ * 'maxOutputSize' >=LZ4_compressBound(inputSize).
+ * It also runs faster, so it's a recommended setting.
+ * If the function cannot compress 'source'
+ * into a more limited 'dest' budget,
+ * compression stops *immediately*,
+ * and the function result is zero.
+ * As a consequence, 'dest' content is not valid.
+ *
+ * source : source address of the original data
+ * dest : output buffer address
+ * of the compressed data
+ * inputSize : Max supported value is
+ *LZ4_MAX_INPUT_SIZE
+ * maxOutputSize: full or partial size of buffer 'dest'
+ * (which must be already allocated)
+ * workmem : address of the working memory.
+ * This requires 'workmem' of sizeLZ4_MEM_COMPRESS.
+ * return : the number of bytes written into buffer 'dest'
+ * (necessarily <= maxOutputSize) or 0 if compression fails
+ */
include/linux/lz4.h line 209
Quoted 2 lines
include/linux/lz4.h +2
-0+int LZ4_compress_default (const char *source,char *dest,int inputSize, +int maxOutputSize,void *wrkmem);
Is there any chance you could format these as kerneldoc comments? You're not too far from it now, and that would allow the LZ4 interface to be pulled into the documentation.
Thanks,
jon
Re: [PATCH v6 1/5] lib: Update LZ4 compressor module
Quoted 37 lines · Jonathan Corbet
On Fri, 27 Jan 2017 23:02:00 +0100
Sven Schmidt <[email protected]> wrote:I have one quick question...
Quoted 25 lines
/*
+ *LZ4_compress_default()
+ * Compresses 'sourceSize' bytes from buffer 'source'
+ * into already allocated 'dest' buffer of size 'maxOutputSize'.
+ * Compression is guaranteed to succeed if
+ * 'maxOutputSize' >=LZ4_compressBound(inputSize).
+ * It also runs faster, so it's a recommended setting.
+ * If the function cannot compress 'source'
+ * into a more limited 'dest' budget,
+ * compression stops *immediately*,
+ * and the function result is zero.
+ * As a consequence, 'dest' content is not valid.
+ *
+ * source : source address of the original data
+ * dest : output buffer address
+ * of the compressed data
+ * inputSize : Max supported value is
+ *LZ4_MAX_INPUT_SIZE
+ * maxOutputSize: full or partial size of buffer 'dest'
+ * (which must be already allocated)
+ * workmem : address of the working memory.
+ * This requires 'workmem' of sizeLZ4_MEM_COMPRESS.
+ * return : the number of bytes written into buffer 'dest'
+ * (necessarily <= maxOutputSize) or 0 if compression fails
+ */include/linux/lz4.h line 209
Quoted 2 lines
include/linux/lz4.h +2
-0+int LZ4_compress_default (const char *source,char *dest,int inputSize, +int maxOutputSize,void *wrkmem);Is there any chance you could format these as kerneldoc comments? You're not too far from it now, and that would allow the LZ4 interface to be pulled into the documentation.
Quoted 3 lines
Thanks,
jon
Hi Jon,
of course, that makes sense. I already checked the documentation and you're right, I'm not that far from it. Will do the necessary changes.
Thanks,
Sven
PATCH v6 2/5 lib/decompress_unlz4: Change module to work with new LZ4 module version
In reply to Sven SchmidtThis patch updates the unlz4 wrapper to work with the updated LZ4 kernel module version.
--- lib/decompress_unlz4.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
lib/decompress_unlz4.c +8 -5
@@ -72,7 +72,7 @@ STATIC inline int INIT unlz4(u8 *input, long in_len,error ("NULL input pointer and missing fill function" ); goto exit_1; }else { - inp =large_malloc (lz4_compressbound (uncomp_chunksize)); + inp =large_malloc (LZ4_compressBound (uncomp_chunksize));if (!inp) {error ("Could not allocate input buffer" ); goto exit_1;
@@ -136,7 +136,7 @@ STATIC inline int INIT unlz4(u8 *input, long in_len, inp +=4 ; size -=4 ; }else { -if (chunksize >lz4_compressbound (uncomp_chunksize)) { +if (chunksize >LZ4_compressBound (uncomp_chunksize)) {error ("chunk length is longer than allocated" ); goto exit_2; }
@@ -152,11 +152,14 @@ STATIC inline int INIT unlz4(u8 *input, long in_len, out_len -= dest_len; }else dest_len = out_len; - ret =lz4_decompress (inp, &chunksize, outp, dest_len); + + ret =LZ4_decompress_fast (inp, outp, dest_len); + chunksize = ret;#else dest_len = uncomp_chunksize; - ret =lz4_decompress_unknownoutputsize (inp, chunksize, outp, - &dest_len); + + ret =LZ4_decompress_safe (inp, outp, chunksize, dest_len); + dest_len = ret;#endif if (ret <0 ) {error ("Decoding failed" );
Signature · 1 line
2.1.4
PATCH v6 3/5 crypto: Change LZ4 modules to work with new LZ4 module version
In reply to Sven SchmidtThis patch updates the crypto modules using LZ4 compression to work with the new LZ4 module version.
--- crypto/lz4.c | 21 ++++++++------------- crypto/lz4hc.c | 21 ++++++++------------- 2 files changed, 16 insertions(+), 26 deletions(-)
crypto/lz4.c +8 -13
@@ -66,15 +66,13 @@ static void lz4_exit(struct crypto_tfm *tfm)static int __lz4_compress_crypto (const u8 *src,unsigned int slen,u8 *dst,unsigned int *dlen,void *ctx) { -size_t tmp_len = *dlen; -int err; +int out_len =LZ4_compress_default (src, dst, + slen, (int )((size_t )dlen), ctx); - err =lz4_compress (src, slen, dst, &tmp_len, ctx); - -if (err <0 ) +if (!out_len)return -EINVAL ; - *dlen = tmp_len; + *dlen = out_len;return 0 ; }
@@ -96,16 +94,13 @@ static int lz4_compress_crypto(struct crypto_tfm *tfm, const u8 *src,static int __lz4_decompress_crypto (const u8 *src,unsigned int slen,u8 *dst,unsigned int *dlen,void *ctx) { -int err; -size_t tmp_len = *dlen; -size_t __slen = slen; +int out_len =LZ4_decompress_safe (src, dst, slen, (int )((size_t )dlen)); - err =lz4_decompress_unknownoutputsize (src, __slen, dst, &tmp_len); -if (err <0 ) +if (out_len <0 )return -EINVAL ; - *dlen = tmp_len; -return err; + *dlen = out_len; +return out_len; } static int lz4_sdecompress(struct crypto_scomp *tfm, const u8 *src,
crypto/lz4hc.c +8 -13
@@ -65,15 +65,13 @@ static void lz4hc_exit(struct crypto_tfm *tfm)static int __lz4hc_compress_crypto (const u8 *src,unsigned int slen,u8 *dst,unsigned int *dlen,void *ctx) { -size_t tmp_len = *dlen; -int err; +int out_len =LZ4_compress_HC (src, dst, slen, + (int )((size_t )dlen),LZ4HC_DEFAULT_CLEVEL , ctx); - err =lz4hc_compress (src, slen, dst, &tmp_len, ctx); - -if (err <0 ) +if (out_len ==0 )return -EINVAL ; - *dlen = tmp_len; + *dlen = out_len;return 0 ; }
@@ -97,16 +95,13 @@ static int lz4hc_compress_crypto(struct crypto_tfm *tfm, const u8 *src,static int __lz4hc_decompress_crypto (const u8 *src,unsigned int slen,u8 *dst,unsigned int *dlen,void *ctx) { -int err; -size_t tmp_len = *dlen; -size_t __slen = slen; +int out_len =LZ4_decompress_safe (src, dst, slen, (int )((size_t )dlen)); - err =lz4_decompress_unknownoutputsize (src, __slen, dst, &tmp_len); -if (err <0 ) +if (out_len <0 )return -EINVAL ; - *dlen = tmp_len; -return err; + *dlen = out_len; +return out_len; } static int lz4hc_sdecompress(struct crypto_scomp *tfm, const u8 *src,
Signature · 1 line
2.1.4
PATCH v6 4/5 fs/pstore: fs/squashfs: Change usage of LZ4 to work with new LZ4 version
In reply to Sven SchmidtThis patch updates fs/pstore and fs/squashfs to use the updated functions from the new LZ4 module.
--- fs/pstore/platform.c | 22 +++++++++++++--------- fs/squashfs/lz4_wrapper.c | 12 ++++++------ 2 files changed, 19 insertions(+), 15 deletions(-)
fs/pstore/platform.c +13 -9
@@ -342,31 +342,35 @@ static int compress_lz4(const void *in, void *out, size_t inlen, size_t outlen) {int ret; - ret =lz4_compress (in, inlen, out, &outlen, workspace); -if (ret) { -pr_err ("lz4_compress error, ret = %d!\n" , ret); + ret =LZ4_compress_default (in, out, inlen, outlen, workspace); +if (!ret) { +pr_err ("LZ4_compress_default error; compression failed!\n" );return -EIO ; } -return outlen; +return ret; }static int decompress_lz4 (void *in,void *out,size_t inlen,size_t outlen) {int ret; - ret =lz4_decompress_unknownoutputsize (in, inlen, out, &outlen); -if (ret) { -pr_err ("lz4_decompress error, ret = %d!\n" , ret); + ret =LZ4_decompress_safe (in, out, inlen, outlen); +if (ret <0 ) { +/* +* LZ4_decompress_safe will return an error code +* (< 0) if decompression failed +*/ +pr_err ("LZ4_decompress_safe error, ret = %d!\n" , ret);return -EIO ; } -return outlen; +return ret; }static void allocate_lz4 (void ) { - big_oops_buf_sz =lz4_compressbound (psinfo->bufsize); + big_oops_buf_sz =LZ4_compressBound (psinfo->bufsize); big_oops_buf =kmalloc (big_oops_buf_sz,GFP_KERNEL );if (big_oops_buf) { workspace =kmalloc (LZ4_MEM_COMPRESS ,GFP_KERNEL );
fs/squashfs/lz4_wrapper.c +6 -6
@@ -97,7 +97,6 @@ static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm,struct squashfs_lz4 *stream = strm;void *buff = stream->input, *data;int avail, i, bytes = length, res; -size_t dest_len = output->length;for (i =0 ; i < b; i++) { avail =min (bytes, msblk->devblksize - offset);
@@ -108,12 +107,13 @@ static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm,put_bh (bh[i]); } - res =lz4_decompress_unknownoutputsize (stream->input, length, - stream->output, &dest_len); -if (res) + res =LZ4_decompress_safe (stream->input, stream->output, + length, output->length); + +if (res <0 )return -EIO ; - bytes = dest_len; + bytes = res; data =squashfs_first_page (output); buff = stream->output;while (data) {
@@ -128,7 +128,7 @@ static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm, }squashfs_finish_page (output); -return dest_len; +return res; }const struct squashfs_decompressor squashfs_lz4_comp_ops = {
Signature · 1 line
2.1.4
PATCH v6 5/5 lib/lz4: Remove back-compat wrappers
In reply to Sven SchmidtThis patch removes the functions introduced as wrappers for providing backwards compatibility to the prior LZ4 version.
They're not needed anymore since there's no callers left.
--- include/linux/lz4.h | 73 ------------------------------------------------ lib/lz4/lz4_compress.c | 22 --------------- lib/lz4/lz4_decompress.c | 42 ---------------------------- lib/lz4/lz4hc_compress.c | 23 --------------- 4 files changed, 160 deletions(-)
include/linux/lz4.h +0 -73
@@ -173,14 +173,6 @@ static inline int LZ4_compressBound(size_t isize) }/* -* For backward compatibility -*/ -static inline int lz4_compressbound (size_t isize) -{ -return LZ4_COMPRESSBOUND (isize); -} - -/* * LZ4_compress_default() * Compresses 'sourceSize' bytes from buffer 'source' * into already allocated 'dest' buffer of size 'maxOutputSize'.
@@ -249,23 +241,6 @@ int LZ4_compress_fast(const char *source, char *dest, int inputSize,int LZ4_compress_destSize (const char *source,char *dest,int *sourceSizePtr,int targetDestSize,void *wrkmem); -/* -* lz4_compress() -* src : source address of the original data -* src_len: size of the original data -* dst : output buffer address of the compressed data -* This requires 'dst' of size LZ4_COMPRESSBOUND. -* dst_len: is the output size, which is returned after compress done -* workmem: address of the working memory. -* This requires 'workmem' of size LZ4_MEM_COMPRESS. -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer and workmem must be already allocated with -* the defined size. -*/ -int lz4_compress (const unsigned char *src,size_t src_len,unsigned char *dst, -size_t *dst_len,void *wrkmem); -/*-************************************************************************ * Decompression Functions **************************************************************************/
@@ -340,37 +315,6 @@ int LZ4_decompress_safe(const char *source, char *dest, int compressedSize,int LZ4_decompress_safe_partial (const char *source,char *dest,int compressedSize,int targetOutputSize,int maxDecompressedSize); - -/* -* lz4_decompress_unknownoutputsize() : -* src : source address of the compressed data -* src_len : is the input size, therefore the compressed size -* dest : output buffer address of the decompressed data -* dest_len: is the max size of the destination buffer, which is -* returned with actual size of decompressed data after -* decompress done -* return: Success if return 0 -* Error if return (< 0) -* note: Destination buffer must be already allocated. -*/ -int lz4_decompress_unknownoutputsize (const unsigned char *src,size_t src_len, -unsigned char *dest,size_t *dest_len); - -/* -* lz4_decompress() : -* src : source address of the compressed data -* src_len : is the input size, -* which is returned after decompress done -* dest : output buffer address of the decompressed data -* actual_dest_len: is the size of uncompressed data, supposing it's known -* return: Success if return 0 -* Error if return (< 0) -* note : Destination buffer must be already allocated. -* slightly faster than lz4_decompress_unknownoutputsize() -*/ -int lz4_decompress (const unsigned char *src,size_t *src_len, -unsigned char *dest,size_t actual_dest_len); -/*-************************************************************************ * LZ4 HC Compression **************************************************************************/
@@ -399,23 +343,6 @@ int LZ4_compress_HC(const char *src, char *dst, int srcSize, int dstCapacity, int compressionLevel, void *wrkmem);/* -* lz4hc_compress() -* src : source address of the original data -* src_len: size of the original data -* dst : output buffer address of the compressed data -* This requires 'dst' of size LZ4_COMPRESSBOUND. -* dst_len: is the output size, which is returned after compress done -* workmem: address of the working memory. -* This requires 'workmem' of size LZ4HC_MEM_COMPRESS. -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer and workmem must be already allocated with -* the defined size. -*/ -int lz4hc_compress (const unsigned char *src,size_t src_len,unsigned char *dst, -size_t *dst_len,void *wrkmem); - -/* * These functions compress data in successive blocks of any size, * using previous blocks as dictionary. One key assumption is that previous * blocks (up to 64 KB) remain read-accessible while
lib/lz4/lz4_compress.c +0 -22
@@ -872,27 +872,5 @@ int LZ4_compress_fast_continue(LZ4_stream_t *LZ4_stream, const char *source, }EXPORT_SYMBOL (LZ4_compress_fast_continue); -/*-****************************** -* For backwards compatibility -********************************/ -int lz4_compress (const unsigned char *src,size_t src_len,unsigned char *dst, -size_t *dst_len,void *wrkmem) { - *dst_len =LZ4_compress_default (src, dst, (int )src_len, - (int )((size_t )dst_len), wrkmem); - -/* -* Prior lz4_compress will return -1 in case of error -* and 0 on success -* while new LZ4_compress_fast/default -* returns 0 in case of error -* and the output length on success -*/ -if (!dst_len) -return -1 ; -else -return 0 ; -} -EXPORT_SYMBOL (lz4_compress); -MODULE_LICENSE ("Dual BSD/GPL" );MODULE_DESCRIPTION ("LZ4 compressor" );
lib/lz4/lz4_decompress.c +0 -42
47 lines not on this page · raw message
lib/lz4/lz4hc_compress.c +0 -23
@@ -753,28 +753,5 @@ int LZ4_saveDictHC( }EXPORT_SYMBOL (LZ4_saveDictHC); -/*-****************************** -* For backwards compatibility -********************************/ -int lz4hc_compress (const unsigned char *src,size_t src_len, -unsigned char *dst,size_t *dst_len,void *wrkmem) -{ - *dst_len =LZ4_compress_HC (src, dst, (int )src_len, - (int )((size_t )dst_len),LZ4HC_DEFAULT_CLEVEL , wrkmem); - -/* -* Prior lz4hc_compress will return -1 in case of error -* and 0 on success -* while new LZ4_compress_HC -* returns 0 in case of error -* and the output length on success -*/ -if (!dst_len) -return -1 ; -else -return 0 ; -} -EXPORT_SYMBOL (lz4hc_compress); -MODULE_LICENSE ("Dual BSD/GPL" );MODULE_DESCRIPTION ("LZ4 HC compressor" );
Signature · 1 line
2.1.4
v7 cover Update LZ4 compressor module
In reply to Sven SchmidtThis patchset is for updating the LZ4 compression module to a version based on LZ4 v1.7.3 allowing to use the fast compression algorithm aka LZ4 fast which provides an "acceleration" parameter as a tradeoff between high compression ratio and high compression speed.
We want to use LZ4 fast in order to support compression in lustre and (mostly, based on that) investigate data reduction techniques in behalf of storage systems.
Also, it will be useful for other users of LZ4 compression, as with LZ4 fast it is possible to enable applications to use fast and/or high compression depending on the usecase.
For instance, ZRAM is offering a LZ4 backend and could benefit from an updated LZ4 in the kernel.
LZ4 homepage: http://www.lz4.org/
LZ4 source repository: https://github.com/lz4/lz4
Source version: 1.7.3
Benchmark (taken from [1], Core i5-4300U @1.9GHz):
----------------|--------------|----------------|---------- Compressor | Compression | Decompression | Ratio ----------------|--------------|----------------|---------- memcpy | 4200 MB/s | 4200 MB/s | 1.000 LZ4 fast 50 | 1080 MB/s | 2650 MB/s | 1.375 LZ4 fast 17 | 680 MB/s | 2220 MB/s | 1.607 LZ4 fast 5 | 475 MB/s | 1920 MB/s | 1.886 LZ4 default | 385 MB/s | 1850 MB/s | 2.101
[1] http://fastcompression.blogspot.de/2015/04/sampling-or-faster-lz4.html
[PATCH 1/5] lib: Update LZ4 compressor module
[PATCH 2/5] lib/decompress_unlz4: Change module to work with new LZ4 module version [PATCH 3/5] crypto: Change LZ4 modules to work with new LZ4 module version [PATCH 4/5] fs/pstore: fs/squashfs: Change usage of LZ4 to work with new LZ4 version [PATCH 5/5] lib/lz4: Remove back-compat wrappers
Changes:
v7:
- Fixed errors reported by the Smatch tool
- Changed function documentation comments in lz4.h to match kernel-doc style
- Fixed a misbehaviour of LZ4HC caused by the wrong level of indentation concerning two for loops introduced after I refactored the code style using checkpatch.pl (upstream LZ4 put dozens of stuff in just one line, gnah)
- Updated the crypto tests for LZ4 since they did fail for the new code and hence zram did fail to allocate memory for LZ4
v6:
- Fixed LZ4_NBCOMMONBYTES() for 64-bit little endian
- Reset LZ4_MEMORY_USAGE to 14 (which is the value used in upstream LZ4 as well as the previous kernel module)
- Fixed that weird double-indentation in lz4defs.h and lz4.h
- Adjusted general styling issues in lz4defs.h (e.g. lines consisting of more than one instruction)
- Removed the architecture-dependent typedef to reg_t since upstream LZ4 is just using size_t and that works fine
- Changed error messages in pstore/platform.c:
* LZ4_compress_default always returns 0 in case of an error (no need to print the return value)
* LZ4_decompress_safe returns a negative error message (return value _does_ matter)
v5:
- Added a fifth patch to remove the back-compat wrappers introduced to ensure bisectibility between the patches (the functions are no longer needed since there's no callers left)
v4:
- Fixed kbuild errors
- Re-added lz4_compressbound as alias for LZ4_compressBound to ensure backwards compatibility
- Wrapped LZ4_hash5 with check for LZ4_ARCH64 since it is only used there and triggers an unused function warning when false
v3:
- Adjusted the code to satisfy kernel coding style (checkpatch.pl)
- Made sure the changes to LZ4 in Kernel (overflow checks etc.) are included in the new module (they are)
- Removed the second LZ4_compressBound function with related name but different return type
- Corrected version number (was LZ4 1.7.3)
- Added missing LZ4 streaming functions
v2:
- Changed order of the patches since in the initial patchset the lz4.h was in the last patch but was referenced by the other ones
- Split lib/decompress_unlz4.c in an own patch
- Fixed errors reported by the buildbot
- Further refactorings
- Added more appropriate copyright note to include/linux/lz4.h
PATCH v7 1/5 lib: Update LZ4 compressor module
This patch updates LZ4 kernel module to LZ4 v1.7.3 by Yann Collet. The kernel module is inspired by the previous work by Chanho Min. The updated LZ4 module will not break existing code since the patchset contains appropriate changes.
API changes:
New method LZ4_compress_fast which differs from the variant available in kernel by the new acceleration parameter,
allowing to trade compression ratio for more compression speed and vice versa.
LZ4_decompress_fast is the respective decompression method, featuring a very fast decoder (multiple GB/s per core), able to reach RAM speed in multi-core systems.
The decompressor allows to decompress data compressed with LZ4 fast as well as the LZ4 HC (high compression) algorithm.
Also the useful functions LZ4_decompress_safe_partial and LZ4_compress_destsize were added.
The latter reverses the logic by trying to compress as much data as possible from source to dest while the former aims to decompress partial blocks of data.
A bunch of streaming functions were also added which allow compressig/decompressing data in multiple steps (so called "streaming mode").
The methods lz4_compress and lz4_decompress_unknownoutputsize are now known as LZ4_compress_default respectivley LZ4_decompress_safe.
The old methods will be removed since there's
no callers left in the code.
--- include/linux/lz4.h | 762 ++++++++++++++++++++++++++++--- lib/lz4/lz4_compress.c | 1114 ++++++++++++++++++++++++++++++++-------------- lib/lz4/lz4_decompress.c | 696 ++++++++++++++++++----------- lib/lz4/lz4defs.h | 334 ++++++++------ lib/lz4/lz4hc_compress.c | 867 +++++++++++++++++++++++------------- 5 files changed, 2690 insertions(+), 1083 deletions(-)
include/linux/lz4.h +696 -66
@@ -1,87 +1,717 @@ -#ifndef __LZ4_H__ -#define __LZ4_H__ -/* -* LZ4 Kernel Interface +/* LZ4 Kernel Interface * * Copyright (C) 2013, LG Electronics, Kyungsik Lee <[email protected]> +* Copyright (C) 2016, Sven Schmidt <[email protected]> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. +* +* This file is based on the original header file +* for LZ4 - Fast LZ compression algorithm. +* +* LZ4 - Fast LZ compression algorithm +* Copyright (C) 2011-2016, Yann Collet. +* BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are +* met: +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above +* copyright notice, this list of conditions and the following disclaimer +* in the documentation and/or other materials provided with the +* distribution. +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* You can contact the author at : +* - LZ4 homepage : http://www.lz4.org +* - LZ4 source repository : https://github.com/lz4/lz4 */ -#define LZ4_MEM_COMPRESS (16384) -#define LZ4HC_MEM_COMPRESS (262144 + (2 * sizeof(unsigned char *))) +#ifndef __LZ4_H__ +#define __LZ4_H__ + +#include <linux/types.h> +#include <linux/string.h> /* memset, memcpy */ + +/*-************************************************************************ +* CONSTANTS +**************************************************************************/ /* -* lz4_compressbound() -* Provides the maximum size that LZ4 may output in a "worst case" scenario -* (input data not compressible) +* LZ4_MEMORY_USAGE : +* Memory usage formula : N->2^N Bytes +* (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.) +* Increasing memory usage improves compression ratio +* Reduced memory usage can improve speed, due to cache effect +* Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache */ -static inline size_t lz4_compressbound (size_t isize) +#define LZ4_MEMORY_USAGE 14 + +#define LZ4_MAX_INPUT_SIZE 0x7E000000/* 2 113 929 216 bytes */ +#define LZ4_COMPRESSBOUND (isize) (\ + (unsigned int)(isize) > (unsigned int)LZ4_MAX_INPUT_SIZE \ + ? 0 \ + : (isize) + ((isize)/255) + 16) + +#define LZ4_ACCELERATION_DEFAULT 1 +#define LZ4_HASHLOG (LZ4_MEMORY_USAGE-2) +#define LZ4_HASHTABLESIZE (1 << LZ4_MEMORY_USAGE) +#define LZ4_HASH_SIZE_U32 (1 << LZ4_HASHLOG) + +#define LZ4HC_MIN_CLEVEL 3 +#define LZ4HC_DEFAULT_CLEVEL 9 +#define LZ4HC_MAX_CLEVEL 16 + +#define LZ4HC_DICTIONARY_LOGSIZE 16 +#define LZ4HC_MAXD (1<<LZ4HC_DICTIONARY_LOGSIZE) +#define LZ4HC_MAXD_MASK (LZ4HC_MAXD - 1) +#define LZ4HC_HASH_LOG (LZ4HC_DICTIONARY_LOGSIZE - 1) +#define LZ4HC_HASHTABLESIZE (1 << LZ4HC_HASH_LOG) +#define LZ4HC_HASH_MASK (LZ4HC_HASHTABLESIZE - 1) + +/*-************************************************************************ +* STREAMING CONSTANTS AND STRUCTURES +**************************************************************************/ +#define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE-3)) + 4) +#define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U64 * sizeof(unsigned long long)) + +#define LZ4_STREAMHCSIZE 262192 +#define LZ4_STREAMHCSIZE_SIZET (262192 / sizeof(size_t)) + +#define LZ4_STREAMDECODESIZE_U64 4 +#define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * \ + sizeof(unsigned long long)) + +/* +* LZ4_stream_t - information structure to track an LZ4 stream. +*/ +typedef struct { +uint32_t hashTable[LZ4_HASH_SIZE_U32 ]; +uint32_t currentOffset; +uint32_t initCheck; +const uint8_t *dictionary; +uint8_t *bufferStart; +uint32_t dictSize; +}LZ4_stream_t_internal ; +typedef union { +unsigned long long table[LZ4_STREAMSIZE_U64 ]; +LZ4_stream_t_internal internal_donotuse; +}LZ4_stream_t ; + +/* +* LZ4_streamHC_t - information structure to track an LZ4HC stream. +*/ +typedef struct { +unsigned int hashTable[LZ4HC_HASHTABLESIZE ]; +unsigned short chainTable[LZ4HC_MAXD ]; +/* next block to continue on current prefix */ +const unsigned char *end; +/* All index relative to this position */ +const unsigned char *base; +/* alternate base for extDict */ +const unsigned char *dictBase; +/* below that point, need extDict */ +unsigned int dictLimit; +/* below that point, no more dict */ +unsigned int lowLimit; +/* index from which to continue dict update */ +unsigned int nextToUpdate; +unsigned int compressionLevel; +}LZ4HC_CCtx_internal ; +typedef union { +size_t table[LZ4_STREAMHCSIZE_SIZET ]; +LZ4HC_CCtx_internal internal_donotuse; +}LZ4_streamHC_t ; + +/* +* LZ4_streamDecode_t - information structure to track an +* LZ4 stream during decompression. +* +* init this structure using LZ4_setStreamDecode (or memset()) before first use +*/ +typedef struct { +const uint8_t *externalDict; +size_t extDictSize; +const uint8_t *prefixEnd; +size_t prefixSize; +}LZ4_streamDecode_t_internal ; +typedef union { +unsigned long long table[LZ4_STREAMDECODESIZE_U64 ]; +LZ4_streamDecode_t_internal internal_donotuse; +}LZ4_streamDecode_t ; + +/*-************************************************************************ +* SIZE OF STATE +**************************************************************************/ +#define LZ4_MEM_COMPRESS LZ4_STREAMSIZE +#define LZ4HC_MEM_COMPRESS LZ4_STREAMHCSIZE + +/*-************************************************************************ +* Compression Functions +**************************************************************************/ + +/** +* LZ4_compressBound() - Max. output size in worst case szenarios +* @isize: Size of the input data +* +* Return: Max. size LZ4 may output in a "worst case" szenario +* (data not compressible) +*/ +static inline int LZ4_compressBound (size_t isize) { -return isize + (isize /255 ) +16 ; +return LZ4_COMPRESSBOUND (isize); } -/* -* lz4_compress() -* src : source address of the original data -* src_len : size of the original data -* dst : output buffer address of the compressed data -* This requires 'dst' of size LZ4_COMPRESSBOUND. -* dst_len : is the output size, which is returned after compress done -* workmem : address of the working memory. -* This requires 'workmem' of size LZ4_MEM_COMPRESS. -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer and workmem must be already allocated with -* the defined size. -*/ -int lz4_compress (const unsigned char *src,size_t src_len, -unsigned char *dst,size_t *dst_len,void *wrkmem); - -/* -* lz4hc_compress() -* src : source address of the original data -* src_len : size of the original data -* dst : output buffer address of the compressed data -* This requires 'dst' of size LZ4_COMPRESSBOUND. -* dst_len : is the output size, which is returned after compress done -* workmem : address of the working memory. -* This requires 'workmem' of size LZ4HC_MEM_COMPRESS. -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer and workmem must be already allocated with -* the defined size. -*/ -int lz4hc_compress (const unsigned char *src,size_t src_len, -unsigned char *dst,size_t *dst_len,void *wrkmem); +/** +* lz4_compressbound() - For backwards compatibility; see LZ4_compressBound +* @isize: Size of the input data +* +* Return: Max. size LZ4 may output in a "worst case" szenario +* (data not compressible) +*/ +static inline int lz4_compressbound (size_t isize) +{ +return LZ4_COMPRESSBOUND (isize); +} + +/** +* LZ4_compress_default() - Compress data from source to dest +* @source: source address of the original data +* @dest: output buffer address of the compressed data +* @inputSize: size of the input data. Max supported value is LZ4_MAX_INPUT_SIZE +* @maxOutputSize: full or partial size of buffer 'dest' +* which must be already allocated +* @wrkmem: address of the working memory. +* This requires 'workmem' of LZ4_MEM_COMPRESS. +* +* Compresses 'sourceSize' bytes from buffer 'source' +* into already allocated 'dest' buffer of size 'maxOutputSize'. +* Compression is guaranteed to succeed if +* 'maxOutputSize' >= LZ4_compressBound(inputSize). +* It also runs faster, so it's a recommended setting. +* If the function cannot compress 'source' into a more limited 'dest' budget, +* compression stops *immediately*, and the function result is zero. +* As a consequence, 'dest' content is not valid. +* +* Return: Number of bytes written into buffer 'dest' +* (necessarily <= maxOutputSize) or 0 if compression fails +*/ +int LZ4_compress_default (const char *source,char *dest,int inputSize, +int maxOutputSize,void *wrkmem); + +/** +* LZ4_compress_fast() - As LZ4_compress_default providing an acceleration param +* @source: source address of the original data +* @dest: output buffer address of the compressed data +* @inputSize: size of the input data. Max supported value is LZ4_MAX_INPUT_SIZE +* @maxOutputSize: full or partial size of buffer 'dest' +* which must be already allocated +* @acceleration: acceleration factor +* @wrkmem: address of the working memory. +* This requires 'workmem' of LZ4_MEM_COMPRESS. +* +* Same as LZ4_compress_default(), but allows to select an "acceleration" +* factor. The larger the acceleration value, the faster the algorithm, +* but also the lesser the compression. It's a trade-off. It can be fine tuned, +* with each successive value providing roughly +~3% to speed. +* An acceleration value of "1" is the same as regular LZ4_compress_default() +* Values <= 0 will be replaced by LZ4_ACCELERATION_DEFAULT, which is 1. +* +* Return: Number of bytes written into buffer 'dest' +* (necessarily <= maxOutputSize) or 0 if compression fails +*/ +int LZ4_compress_fast (const char *source,char *dest,int inputSize, +int maxOutputSize,int acceleration,void *wrkmem); + +/** +* LZ4_compress_destSize() - Compress as much data as possible +* from source to dest +* @source: source address of the original data +* @dest: output buffer address of the compressed data +* @sourceSizePtr: will be modified to indicate how many bytes where read +* from 'source' to fill 'dest'. New value is necessarily <= old value. +* @targetDestSize: Size of buffer 'dest' which must be already allocated +* @wrkmem: address of the working memory. +* This requires 'workmem' of LZ4_MEM_COMPRESS. +* +* Reverse the logic, by compressing as much data as possible +* from 'source' buffer into already allocated buffer 'dest' +* of size 'targetDestSize'. +* This function either compresses the entire 'source' content into 'dest' +* if it's large enough, or fill 'dest' buffer completely with as much data as +* possible from 'source'. +* +* Return: Number of bytes written into 'dest' (necessarily <= targetDestSize) +* or 0 if compression fails +*/ +int LZ4_compress_destSize (const char *source,char *dest,int *sourceSizePtr, +int targetDestSize,void *wrkmem);/* -* lz4_decompress() -* src : source address of the compressed data -* src_len : is the input size, whcih is returned after decompress done -* dest : output buffer address of the decompressed data -* actual_dest_len: is the size of uncompressed data, supposing it's known -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer must be already allocated. -* slightly faster than lz4_decompress_unknownoutputsize() +* lz4_compress() - For backward compatibility, see LZ4_compress_default +* @src: source address of the original data +* @src_len: size of the original data +* @dst: output buffer address of the compressed data. This requires 'dst' +* of size LZ4_COMPRESSBOUND +* @dst_len: is the output size, which is returned after compress done +* @workmem: address of the working memory. +* +* Return: Success if return 0, Error if return < 0 */ -int lz4_decompress (const unsigned char *src,size_t *src_len, -unsigned char *dest,size_t actual_dest_len); +int lz4_compress (const unsigned char *src,size_t src_len,unsigned char *dst, +size_t *dst_len,void *wrkmem); + +/*-************************************************************************ +* Decompression Functions +**************************************************************************/ + +/** +* LZ4_decompress_fast() - Decompresses data from 'source' into 'dest' +* @source: source address of the compressed data +* @dest: output buffer address of the uncompressed data +* which must be already allocated with 'originalSize' bytes +* @originalSize: is the original and therefore uncompressed size +* +* Decompresses data from 'source' into 'dest'. +* This function fully respect memory boundaries for properly formed +* compressed data. +* It is a bit faster than LZ4_decompress_safe(). +* However, it does not provide any protection against intentionally +* modified data stream (malicious input). +* Use this function in trusted environment only +* (data to decode comes from a trusted source). +* +* Return: number of bytes read from the source buffer +* or a negative result if decompression fails. +*/ +int LZ4_decompress_fast (const char *source,char *dest,int originalSize); + +/** +* LZ4_decompress_safe() - Decompression protected against buffer overflow +* @source: source address of the compressed data +* @dest: output buffer address of the uncompressed data +* which must be already allocated +* @compressedSize: is the precise full size of the compressed block +* @maxDecompressedSize: is the size of 'dest' buffer +* +* Decompresses data fom 'source' into 'dest'. +* If the source stream is detected malformed, the function will +* stop decoding and return a negative result. +* This function is protected against buffer overflow exploits, +* including malicious data packets. It never writes outside output buffer, +* nor reads outside input buffer. +* +* Return: number of bytes decompressed into destination buffer +* (necessarily <= maxDecompressedSize) +* or a negative result in case of error +*/ +int LZ4_decompress_safe (const char *source,char *dest,int compressedSize, +int maxDecompressedSize); + +/** +* LZ4_decompress_safe_partial() - Decompress a block of size 'compressedSize' +* at position 'source' into buffer 'dest' +* @source: source address of the compressed data +* @dest: output buffer address of the decompressed data which must be +* already allocated +* @compressedSize: is the precise full size of the compressed block. +* @targetOutputSize: the decompression operation will try +* to stop as soon as 'targetOutputSize' has been reached +* @maxDecompressedSize: is the size of destination buffer +* +* This function decompresses a compressed block of size 'compressedSize' +* at position 'source' into destination buffer 'dest' +* of size 'maxDecompressedSize'. +* The function tries to stop decompressing operation as soon as +* 'targetOutputSize' has been reached, reducing decompression time. +* This function never writes outside of output buffer, +* and never reads outside of input buffer. +* It is therefore protected against malicious data packets. +* +* Return: the number of bytes decoded in the destination buffer +* (necessarily <= maxDecompressedSize) +* or a negative result in case of error +* +*/ +int LZ4_decompress_safe_partial (const char *source,char *dest, +int compressedSize,int targetOutputSize,int maxDecompressedSize);/* -* lz4_decompress_unknownoutputsize() -* src : source address of the compressed data -* src_len : is the input size, therefore the compressed size -* dest : output buffer address of the decompressed data -* dest_len: is the max size of the destination buffer, which is -* returned with actual size of decompressed data after -* decompress done -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer must be already allocated. +* lz4_decompress_unknownoutputsize() - For backwards compatibility, +* see LZ4_decompress_safe +* @src: source address of the compressed data +* @src_len: is the input size, therefore the compressed size +* @dest: output buffer address of the decompressed data +* which must be already allocated +* @dest_len: is the max size of the destination buffer, which is +* returned with actual size of decompressed data after decompress done +* +* Return: Success if return 0, Error if return (< 0) */ int lz4_decompress_unknownoutputsize (const unsigned char *src,size_t src_len, -unsigned char *dest,size_t *dest_len); +unsigned char *dest,size_t *dest_len); + +/** +* lz4_decompress() - For backwards cocmpatibility, see LZ4_decompress_fast +* @src: source address of the compressed data +* @src_len: is the input size, which is returned after decompress done +* @dest: output buffer address of the decompressed data, +* which must be already allocated +* @actual_dest_len: is the size of uncompressed data, supposing it's known +* +* Return: Success if return 0, Error if return (< 0) +*/ +int lz4_decompress (const unsigned char *src,size_t *src_len, +unsigned char *dest,size_t actual_dest_len); + +/*-************************************************************************ +* LZ4 HC Compression +**************************************************************************/ + +/** +* LZ4_compress_HC() - Compress data from `src` into `dst`, using HC algorithm +* @src: source address of the original data +* @dst: output buffer address of the compressed data +* @srcSize: size of the input data. Max supported value is LZ4_MAX_INPUT_SIZE +* @dstCapacity: full or partial size of buffer 'dst', +* which must be already allocated +* @compressionLevel: Recommended values are between 4 and 9, although any +* value between 1 and LZ4HC_MAX_CLEVEL will work. +* Values >LZ4HC_MAX_CLEVEL behave the same as 16. +* @wrkmem: address of the working memory. +* This requires 'wrkmem' of size LZ4HC_MEM_COMPRESS. +* +* Compress data from 'src' into 'dst', using the more powerful +* but slower "HC" algorithm. Compression is guaranteed to succeed if +* `dstCapacity >= LZ4_compressBound(srcSize) +* +* Return : the number of bytes written into 'dst' or 0 if compression fails. +*/ +int LZ4_compress_HC (const char *src,char *dst,int srcSize,int dstCapacity, +int compressionLevel,void *wrkmem); + +/** +* lz4hc_compress() - For backwards compatibility, see LZ4_compress_HC +* @src: source address of the original data +* @src_len: size of the original data +* @dst: output buffer address of the compressed data. This requires 'dst' +* of size LZ4_COMPRESSBOUND. +* @dst_len: is the output size, which is returned after compress done +* @wrkmem: address of the working memory. +* This requires 'workmem' of size LZ4HC_MEM_COMPRESS. +* +* Return : Success if return 0, Error if return (< 0) +*/ +int lz4hc_compress (const unsigned char *src,size_t src_len,unsigned char *dst, +size_t *dst_len,void *wrkmem); + +/** +* LZ4_resetStreamHC() - Init an allocated 'LZ4_streamHC_t' structure +* @streamHCPtr: pointer to the 'LZ4_streamHC_t' structure +* @compressionLevel: Recommended values are between 4 and 9, although any +* value between 1 and LZ4HC_MAX_CLEVEL will work. +* Values >LZ4HC_MAX_CLEVEL behave the same as 16. +* +* An LZ4_streamHC_t structure can be allocated once +* and re-used multiple times. +* Use this function to init an allocated `LZ4_streamHC_t` structure +* and start a new compression. +*/ +void LZ4_resetStreamHC (LZ4_streamHC_t *streamHCPtr,int compressionLevel); + +/** +* LZ4_loadDictHC() - Load a static dictionary into LZ4_streamHC +* @streamHCPtr: pointer to the LZ4HC_stream_t +* @dictionary: dictionary to load +* @dictSize: size of dictionary +* +* Use this function to load a static dictionary into LZ4HC_stream. +* Any previous data will be forgotten, only 'dictionary' +* will remain in memory. +* Loading a size of 0 is allowed. +* +* Return : dictionary size, in bytes (necessarily <= 64 KB) +*/ +int LZ4_loadDictHC (LZ4_streamHC_t *streamHCPtr,const char *dictionary, +int dictSize); + +/** +* LZ4_compress_HC_continue() - Compress 'src' using data from previously +* compressed blocks as a dictionary using the HC algorithm +* @streamHCPtr: Pointer to the previous 'LZ4_streamHC_t' structure +* @src: source address of the original data +* @dst: output buffer address of the compressed data, +* which must be already allocated +* @srcSize: size of the input data. Max supported value is LZ4_MAX_INPUT_SIZE +* @maxDstSize: full or partial size of buffer 'dest' +* which must be already allocated +* +* These functions compress data in successive blocks of any size, using +* previous blocks as dictionary. One key assumption is that previous +* blocks (up to 64 KB) remain read-accessible while +* compressing next blocks. There is an exception for ring buffers, +* which can be smaller than 64 KB. +* Ring buffers scenario is automatically detected and handled by +* LZ4_compress_HC_continue(). +* Before starting compression, state must be properly initialized, +* using LZ4_resetStreamHC(). +* A first "fictional block" can then be designated as +* initial dictionary, using LZ4_loadDictHC() (Optional). +* Then, use LZ4_compress_HC_continue() +* to compress each successive block. Previous memory blocks +* (including initial dictionary when present) must remain accessible +* and unmodified during compression. +* 'dst' buffer should be sized to handle worst case scenarios, using +* LZ4_compressBound(), to ensure operation success. +* If, for any reason, previous data blocks can't be preserved unmodified +* in memory during next compression block, +* you must save it to a safer memory space, using LZ4_saveDictHC(). +* Return value of LZ4_saveDictHC() is the size of dictionary +* effectively saved into 'safeBuffer'. +* +* Return: Number of bytes written into buffer 'dst' or 0 if compression fails +*/ +int LZ4_compress_HC_continue (LZ4_streamHC_t *streamHCPtr,const char *src, +char *dst,int srcSize,int maxDstSize); + +/** +* LZ4_saveDictHC() - Save static dictionary from LZ4HC_stream +* @streamHCPtr: pointer to the 'LZ4HC_stream_t' structure +* @safeBuffer: buffer to save dictionary to, must be already allocated +* @maxDictSize: size of 'safeBuffer' +* +* If previously compressed data block is not guaranteed +* to remain available at its memory location, +* save it into a safer place (char *safeBuffer). +* Note : you don't need to call LZ4_loadDictHC() afterwards, +* dictionary is immediately usable, you can therefore call +* LZ4_compress_HC_continue(). +* +* Return : saved dictionary size in bytes (necessarily <= maxDictSize), +* or 0 if error. +*/ +int LZ4_saveDictHC (LZ4_streamHC_t *streamHCPtr,char *safeBuffer, +int maxDictSize); + +/*-********************************************* +* Streaming Compression Functions +***********************************************/ + +/** +* LZ4_resetStream() - Init an allocated 'LZ4_stream_t' structure +* @LZ4_stream: pointer to the 'LZ4_stream_t' structure +* +* An LZ4_stream_t structure can be allocated once +* and re-used multiple times. +* Use this function to init an allocated `LZ4_stream_t` structure +* and start a new compression. +*/ +void LZ4_resetStream (LZ4_stream_t *LZ4_stream); + +/** +* LZ4_loadDict() - Load a static dictionary into LZ4_stream +* @streamPtr: pointer to the LZ4_stream_t +* @dictionary: dictionary to load +* @dictSize: size of dictionary +* +* Use this function to load a static dictionary into LZ4_stream. +* Any previous data will be forgotten, only 'dictionary' +* will remain in memory. +* Loading a size of 0 is allowed. +* +* Return : dictionary size, in bytes (necessarily <= 64 KB) +*/ +int LZ4_loadDict (LZ4_stream_t *streamPtr,const char *dictionary, +int dictSize); + +/** +* LZ4_saveDict() - Save static dictionary from LZ4_stream +* @streamPtr: pointer to the 'LZ4_stream_t' structure +* @safeBuffer: buffer to save dictionary to, must be already allocated +* @dictSize: size of 'safeBuffer' +* +* If previously compressed data block is not guaranteed +* to remain available at its memory location, +* save it into a safer place (char *safeBuffer). +* Note : you don't need to call LZ4_loadDict() afterwards, +* dictionary is immediately usable, you can therefore call +* LZ4_compress_fast_continue(). +* +* Return : saved dictionary size in bytes (necessarily <= dictSize), +* or 0 if error. +*/ +int LZ4_saveDict (LZ4_stream_t *streamPtr,char *safeBuffer,int dictSize); + +/** +* LZ4_compress_fast_continue() - Compress 'src' using data from previously +* compressed blocks as a dictionary +* @streamPtr: Pointer to the previous 'LZ4_stream_t' structure +* @src: source address of the original data +* @dst: output buffer address of the compressed data, +* which must be already allocated +* @srcSize: size of the input data. Max supported value is LZ4_MAX_INPUT_SIZE +* @maxDstSize: full or partial size of buffer 'dest' +* which must be already allocated +* @acceleration: acceleration factor +* +* Compress buffer content 'src', using data from previously compressed blocks +* as dictionary to improve compression ratio. +* Important : Previous data blocks are assumed to still +* be present and unmodified ! +* If maxDstSize >= LZ4_compressBound(srcSize), +* compression is guaranteed to succeed, and runs faster. +* +* Return: Number of bytes written into buffer 'dst' or 0 if compression fails +*/ +int LZ4_compress_fast_continue (LZ4_stream_t *streamPtr,const char *src, +char *dst,int srcSize,int maxDstSize,int acceleration); + +/** +* LZ4_setStreamDecode() - Instruct where to find dictionary +* @LZ4_streamDecode: the 'LZ4_streamDecode_t' structure +* @dictionary: dictionary to use +* @dictSize: size of dictionary +* +* Use this function to instruct where to find the dictionary. +* Setting a size of 0 is allowed (same effect as reset). +* +* Return: 1 if OK, 0 if error +*/ +int LZ4_setStreamDecode (LZ4_streamDecode_t *LZ4_streamDecode, +const char *dictionary,int dictSize); + +/** +* LZ4_decompress_fast_continue() - Decompress blocks in streaming mode +* @LZ4_streamDecode: the 'LZ4_streamDecode_t' structure +* @source: source address of the compressed data +* @dest: output buffer address of the uncompressed data +* which must be already allocated +* @compressedSize: is the precise full size of the compressed block +* @maxDecompressedSize: is the size of 'dest' buffer +* +* These decoding function allows decompression of multiple blocks +* in "streaming" mode. +* Previously decoded blocks *must* remain available at the memory position +* where they were decoded (up to 64 KB) +* In the case of a ring buffers, decoding buffer must be either : +* - Exactly same size as encoding buffer, with same update rule +* (block boundaries at same positions) In which case, +* the decoding & encoding ring buffer can have any size, +* including very small ones ( < 64 KB). +* - Larger than encoding buffer, by a minimum of maxBlockSize more bytes. +* maxBlockSize is implementation dependent. +* It's the maximum size you intend to compress into a single block. +* In which case, encoding and decoding buffers do not need +* to be synchronized, and encoding ring buffer can have any size, +* including small ones ( < 64 KB). +* - _At least_ 64 KB + 8 bytes + maxBlockSize. +* In which case, encoding and decoding buffers do not need to be +* synchronized, and encoding ring buffer can have any size, +* including larger than decoding buffer. W +* Whenever these conditions are not possible, save the last 64KB of decoded +* data into a safe buffer, and indicate where it is saved +* using LZ4_setStreamDecode() +* +* Return: number of bytes decompressed into destination buffer +* (necessarily <= maxDecompressedSize) +* or a negative result in case of error +*/ +int LZ4_decompress_safe_continue (LZ4_streamDecode_t *LZ4_streamDecode, +const char *source,char *dest,int compressedSize, +int maxDecompressedSize); + +/** +* LZ4_decompress_fast_continue() - Decompress blocks in streaming mode +* @LZ4_streamDecode: the 'LZ4_streamDecode_t' structure +* @source: source address of the compressed data +* @dest: output buffer address of the uncompressed data +* which must be already allocated with 'originalSize' bytes +* @originalSize: is the original and therefore uncompressed size +* +* These decoding function allows decompression of multiple blocks +* in "streaming" mode. +* Previously decoded blocks *must* remain available at the memory position +* where they were decoded (up to 64 KB) +* In the case of a ring buffers, decoding buffer must be either : +* - Exactly same size as encoding buffer, with same update rule +* (block boundaries at same positions) In which case, +* the decoding & encoding ring buffer can have any size, +* including very small ones ( < 64 KB). +* - Larger than encoding buffer, by a minimum of maxBlockSize more bytes. +* maxBlockSize is implementation dependent. +* It's the maximum size you intend to compress into a single block. +* In which case, encoding and decoding buffers do not need +* to be synchronized, and encoding ring buffer can have any size, +* including small ones ( < 64 KB). +* - _At least_ 64 KB + 8 bytes + maxBlockSize. +* In which case, encoding and decoding buffers do not need to be +* synchronized, and encoding ring buffer can have any size, +* including larger than decoding buffer. W +* Whenever these conditions are not possible, save the last 64KB of decoded +* data into a safe buffer, and indicate where it is saved +* using LZ4_setStreamDecode() +* +* Return: number of bytes decompressed into destination buffer +* (necessarily <= maxDecompressedSize) +* or a negative result in case of error +*/ +int LZ4_decompress_fast_continue (LZ4_streamDecode_t *LZ4_streamDecode, +const char *source,char *dest,int originalSize); + +/** +* LZ4_decompress_safe_usingDict() - Same as LZ4_setStreamDecode() +* followed by LZ4_decompress_safe_continue() +* @source: source address of the compressed data +* @dest: output buffer address of the uncompressed data +* which must be already allocated +* @compressedSize: is the precise full size of the compressed block +* @maxDecompressedSize: is the size of 'dest' buffer +* @dictStart: pointer to the start of the dictionary in memory +* @dictSize: size of dictionary +* +* These decoding function works the same as +* a combination of LZ4_setStreamDecode() followed by +* LZ4_decompress_safe_continue() +* It is stand-alone, and don'tn eed a LZ4_streamDecode_t structure. +* +* Return: number of bytes decompressed into destination buffer +* (necessarily <= maxDecompressedSize) +* or a negative result in case of error +*/ +int LZ4_decompress_safe_usingDict (const char *source,char *dest, +int compressedSize,int maxDecompressedSize,const char *dictStart, +int dictSize); + +/** +* LZ4_decompress_fast_usingDict() - Same as LZ4_setStreamDecode() +* followed by LZ4_decompress_fast_continue() +* @source: source address of the compressed data +* @dest: output buffer address of the uncompressed data +* which must be already allocated with 'originalSize' bytes +* @originalSize: is the original and therefore uncompressed size +* @dictStart: pointer to the start of the dictionary in memory +* @dictSize: size of dictionary +* +* These decoding function works the same as +* a combination of LZ4_setStreamDecode() followed by +* LZ4_decompress_safe_continue() +* It is stand-alone, and don'tn eed a LZ4_streamDecode_t structure. +* +* Return: number of bytes decompressed into destination buffer +* (necessarily <= maxDecompressedSize) +* or a negative result in case of error +*/ +int LZ4_decompress_fast_usingDict (const char *source,char *dest, +int originalSize,const char *dictStart,int dictSize); +#endif
lib/lz4/lz4_compress.c +790 -324
1,226 lines not on this page · raw message
lib/lz4/lz4_decompress.c +441 -255
779 lines not on this page · raw message
lib/lz4/lz4defs.h +203 -131
212 lines not on this page · raw message
lib/lz4/lz4hc_compress.c +560 -307
867 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.1.4
Part of the diff is not shown here. View the raw message.
PATCH v7 2/5 lib/decompress_unlz4: Change module to work with new LZ4 module version
In reply to Sven SchmidtThis patch updates the unlz4 wrapper to work with the updated LZ4 kernel module version.
--- lib/decompress_unlz4.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
lib/decompress_unlz4.c +8 -5
@@ -72,7 +72,7 @@ STATIC inline int INIT unlz4(u8 *input, long in_len,error ("NULL input pointer and missing fill function" ); goto exit_1; }else { - inp =large_malloc (lz4_compressbound (uncomp_chunksize)); + inp =large_malloc (LZ4_compressBound (uncomp_chunksize));if (!inp) {error ("Could not allocate input buffer" ); goto exit_1;
@@ -136,7 +136,7 @@ STATIC inline int INIT unlz4(u8 *input, long in_len, inp +=4 ; size -=4 ; }else { -if (chunksize >lz4_compressbound (uncomp_chunksize)) { +if (chunksize >LZ4_compressBound (uncomp_chunksize)) {error ("chunk length is longer than allocated" ); goto exit_2; }
@@ -152,11 +152,14 @@ STATIC inline int INIT unlz4(u8 *input, long in_len, out_len -= dest_len; }else dest_len = out_len; - ret =lz4_decompress (inp, &chunksize, outp, dest_len); + + ret =LZ4_decompress_fast (inp, outp, dest_len); + chunksize = ret;#else dest_len = uncomp_chunksize; - ret =lz4_decompress_unknownoutputsize (inp, chunksize, outp, - &dest_len); + + ret =LZ4_decompress_safe (inp, outp, chunksize, dest_len); + dest_len = ret;#endif if (ret <0 ) {error ("Decoding failed" );
Signature · 1 line
2.1.4
PATCH v7 3/5 crypto: Change LZ4 modules to work with new LZ4 module version
In reply to Sven SchmidtThis patch updates the crypto modules using LZ4 compression as well as the test cases in testmgr.h to work with the new LZ4 module version.
--- crypto/lz4.c | 23 ++++----- crypto/lz4hc.c | 23 ++++----- crypto/testmgr.h | 142 +++++++++++++++++++++++++++++++++++++++---------------- 3 files changed, 120 insertions(+), 68 deletions(-)
crypto/lz4.c +9 -14
@@ -66,15 +66,13 @@ static void lz4_exit(struct crypto_tfm *tfm)static int __lz4_compress_crypto (const u8 *src,unsigned int slen,u8 *dst,unsigned int *dlen,void *ctx) { -size_t tmp_len = *dlen; -int err; +int out_len =LZ4_compress_default (src, dst, + slen, *dlen, ctx); - err =lz4_compress (src, slen, dst, &tmp_len, ctx); - -if (err <0 ) +if (!out_len)return -EINVAL ; - *dlen = tmp_len; + *dlen = out_len;return 0 ; }
@@ -96,16 +94,13 @@ static int lz4_compress_crypto(struct crypto_tfm *tfm, const u8 *src,static int __lz4_decompress_crypto (const u8 *src,unsigned int slen,u8 *dst,unsigned int *dlen,void *ctx) { -int err; -size_t tmp_len = *dlen; -size_t __slen = slen; +int out_len =LZ4_decompress_safe (src, dst, slen, *dlen); - err =lz4_decompress_unknownoutputsize (src, __slen, dst, &tmp_len); -if (err <0 ) -return -EINVAL ; +if (out_len <0 ) +return out_len; - *dlen = tmp_len; -return err; + *dlen = out_len; +return 0 ; } static int lz4_sdecompress(struct crypto_scomp *tfm, const u8 *src,
crypto/lz4hc.c +9 -14
@@ -65,15 +65,13 @@ static void lz4hc_exit(struct crypto_tfm *tfm)static int __lz4hc_compress_crypto (const u8 *src,unsigned int slen,u8 *dst,unsigned int *dlen,void *ctx) { -size_t tmp_len = *dlen; -int err; +int out_len =LZ4_compress_HC (src, dst, slen, + *dlen,LZ4HC_DEFAULT_CLEVEL , ctx); - err =lz4hc_compress (src, slen, dst, &tmp_len, ctx); - -if (err <0 ) +if (!out_len)return -EINVAL ; - *dlen = tmp_len; + *dlen = out_len;return 0 ; }
@@ -97,16 +95,13 @@ static int lz4hc_compress_crypto(struct crypto_tfm *tfm, const u8 *src,static int __lz4hc_decompress_crypto (const u8 *src,unsigned int slen,u8 *dst,unsigned int *dlen,void *ctx) { -int err; -size_t tmp_len = *dlen; -size_t __slen = slen; +int out_len =LZ4_decompress_safe (src, dst, slen, *dlen); - err =lz4_decompress_unknownoutputsize (src, __slen, dst, &tmp_len); -if (err <0 ) -return -EINVAL ; +if (out_len <0 ) +return out_len; - *dlen = tmp_len; -return err; + *dlen = out_len; +return 0 ; } static int lz4hc_sdecompress(struct crypto_scomp *tfm, const u8 *src,
crypto/testmgr.h +102 -40
164 lines not on this page · raw message
Signature · 1 line
2.1.4
PATCH v7 4/5 fs/pstore: fs/squashfs: Change usage of LZ4 to work with new LZ4 version
In reply to Sven SchmidtThis patch updates fs/pstore and fs/squashfs to use the updated functions from the new LZ4 module.
--- fs/pstore/platform.c | 22 +++++++++++++--------- fs/squashfs/lz4_wrapper.c | 12 ++++++------ 2 files changed, 19 insertions(+), 15 deletions(-)
fs/pstore/platform.c +13 -9
@@ -342,31 +342,35 @@ static int compress_lz4(const void *in, void *out, size_t inlen, size_t outlen) {int ret; - ret =lz4_compress (in, inlen, out, &outlen, workspace); -if (ret) { -pr_err ("lz4_compress error, ret = %d!\n" , ret); + ret =LZ4_compress_default (in, out, inlen, outlen, workspace); +if (!ret) { +pr_err ("LZ4_compress_default error; compression failed!\n" );return -EIO ; } -return outlen; +return ret; }static int decompress_lz4 (void *in,void *out,size_t inlen,size_t outlen) {int ret; - ret =lz4_decompress_unknownoutputsize (in, inlen, out, &outlen); -if (ret) { -pr_err ("lz4_decompress error, ret = %d!\n" , ret); + ret =LZ4_decompress_safe (in, out, inlen, outlen); +if (ret <0 ) { +/* +* LZ4_decompress_safe will return an error code +* (< 0) if decompression failed +*/ +pr_err ("LZ4_decompress_safe error, ret = %d!\n" , ret);return -EIO ; } -return outlen; +return ret; }static void allocate_lz4 (void ) { - big_oops_buf_sz =lz4_compressbound (psinfo->bufsize); + big_oops_buf_sz =LZ4_compressBound (psinfo->bufsize); big_oops_buf =kmalloc (big_oops_buf_sz,GFP_KERNEL );if (big_oops_buf) { workspace =kmalloc (LZ4_MEM_COMPRESS ,GFP_KERNEL );
fs/squashfs/lz4_wrapper.c +6 -6
@@ -97,7 +97,6 @@ static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm,struct squashfs_lz4 *stream = strm;void *buff = stream->input, *data;int avail, i, bytes = length, res; -size_t dest_len = output->length;for (i =0 ; i < b; i++) { avail =min (bytes, msblk->devblksize - offset);
@@ -108,12 +107,13 @@ static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm,put_bh (bh[i]); } - res =lz4_decompress_unknownoutputsize (stream->input, length, - stream->output, &dest_len); -if (res) + res =LZ4_decompress_safe (stream->input, stream->output, + length, output->length); + +if (res <0 )return -EIO ; - bytes = dest_len; + bytes = res; data =squashfs_first_page (output); buff = stream->output;while (data) {
@@ -128,7 +128,7 @@ static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm, }squashfs_finish_page (output); -return dest_len; +return res; }const struct squashfs_decompressor squashfs_lz4_comp_ops = {
Signature · 1 line
2.1.4
PATCH v7 5/5 lib/lz4: Remove back-compat wrappers
In reply to Sven SchmidtThis patch removes the functions introduced as wrappers for providing backwards compatibility to the prior LZ4 version.
They're not needed anymore since there's no callers left.
--- include/linux/lz4.h | 69 ------------------------------------------------ lib/lz4/lz4_compress.c | 22 --------------- lib/lz4/lz4_decompress.c | 42 ----------------------------- lib/lz4/lz4hc_compress.c | 23 ---------------- 4 files changed, 156 deletions(-)
include/linux/lz4.h +0 -69
@@ -173,18 +173,6 @@ static inline int LZ4_compressBound(size_t isize) }/** -* lz4_compressbound() - For backwards compatibility; see LZ4_compressBound -* @isize: Size of the input data -* -* Return: Max. size LZ4 may output in a "worst case" szenario -* (data not compressible) -*/ -static inline int lz4_compressbound (size_t isize) -{ -return LZ4_COMPRESSBOUND (isize); -} - -/** * LZ4_compress_default() - Compress data from source to dest * @source: source address of the original data * @dest: output buffer address of the compressed data
@@ -257,20 +245,6 @@ int LZ4_compress_fast(const char *source, char *dest, int inputSize,int LZ4_compress_destSize (const char *source,char *dest,int *sourceSizePtr,int targetDestSize,void *wrkmem); -/* -* lz4_compress() - For backward compatibility, see LZ4_compress_default -* @src: source address of the original data -* @src_len: size of the original data -* @dst: output buffer address of the compressed data. This requires 'dst' -* of size LZ4_COMPRESSBOUND -* @dst_len: is the output size, which is returned after compress done -* @workmem: address of the working memory. -* -* Return: Success if return 0, Error if return < 0 -*/ -int lz4_compress (const unsigned char *src,size_t src_len,unsigned char *dst, -size_t *dst_len,void *wrkmem); -/*-************************************************************************ * Decompression Functions **************************************************************************/
@@ -346,34 +320,6 @@ int LZ4_decompress_safe(const char *source, char *dest, int compressedSize,int LZ4_decompress_safe_partial (const char *source,char *dest,int compressedSize,int targetOutputSize,int maxDecompressedSize); -/* -* lz4_decompress_unknownoutputsize() - For backwards compatibility, -* see LZ4_decompress_safe -* @src: source address of the compressed data -* @src_len: is the input size, therefore the compressed size -* @dest: output buffer address of the decompressed data -* which must be already allocated -* @dest_len: is the max size of the destination buffer, which is -* returned with actual size of decompressed data after decompress done -* -* Return: Success if return 0, Error if return (< 0) -*/ -int lz4_decompress_unknownoutputsize (const unsigned char *src,size_t src_len, -unsigned char *dest,size_t *dest_len); - -/** -* lz4_decompress() - For backwards cocmpatibility, see LZ4_decompress_fast -* @src: source address of the compressed data -* @src_len: is the input size, which is returned after decompress done -* @dest: output buffer address of the decompressed data, -* which must be already allocated -* @actual_dest_len: is the size of uncompressed data, supposing it's known -* -* Return: Success if return 0, Error if return (< 0) -*/ -int lz4_decompress (const unsigned char *src,size_t *src_len, -unsigned char *dest,size_t actual_dest_len); -/*-************************************************************************ * LZ4 HC Compression **************************************************************************/
@@ -401,21 +347,6 @@ int LZ4_compress_HC(const char *src, char *dst, int srcSize, int dstCapacity, int compressionLevel, void *wrkmem);/** -* lz4hc_compress() - For backwards compatibility, see LZ4_compress_HC -* @src: source address of the original data -* @src_len: size of the original data -* @dst: output buffer address of the compressed data. This requires 'dst' -* of size LZ4_COMPRESSBOUND. -* @dst_len: is the output size, which is returned after compress done -* @wrkmem: address of the working memory. -* This requires 'workmem' of size LZ4HC_MEM_COMPRESS. -* -* Return : Success if return 0, Error if return (< 0) -*/ -int lz4hc_compress (const unsigned char *src,size_t src_len,unsigned char *dst, -size_t *dst_len,void *wrkmem); - -/** * LZ4_resetStreamHC() - Init an allocated 'LZ4_streamHC_t' structure * @streamHCPtr: pointer to the 'LZ4_streamHC_t' structure * @compressionLevel: Recommended values are between 4 and 9, although any
lib/lz4/lz4_compress.c +0 -22
@@ -883,27 +883,5 @@ int LZ4_compress_fast_continue(LZ4_stream_t *LZ4_stream, const char *source, }EXPORT_SYMBOL (LZ4_compress_fast_continue); -/*-****************************** -* For backwards compatibility -********************************/ -int lz4_compress (const unsigned char *src,size_t src_len,unsigned char *dst, -size_t *dst_len,void *wrkmem) { - *dst_len =LZ4_compress_default (src, dst, src_len, - *dst_len, wrkmem); - -/* -* Prior lz4_compress will return -1 in case of error -* and 0 on success -* while new LZ4_compress_fast/default -* returns 0 in case of error -* and the output length on success -*/ -if (!*dst_len) -return -1 ; -else -return 0 ; -} -EXPORT_SYMBOL (lz4_compress); -MODULE_LICENSE ("Dual BSD/GPL" );MODULE_DESCRIPTION ("LZ4 compressor" );
lib/lz4/lz4_decompress.c +0 -42
47 lines not on this page · raw message
lib/lz4/lz4hc_compress.c +0 -23
@@ -765,28 +765,5 @@ int LZ4_saveDictHC( }EXPORT_SYMBOL (LZ4_saveDictHC); -/*-****************************** -* For backwards compatibility -********************************/ -int lz4hc_compress (const unsigned char *src,size_t src_len, -unsigned char *dst,size_t *dst_len,void *wrkmem) -{ - *dst_len =LZ4_compress_HC (src, dst, src_len, - *dst_len,LZ4HC_DEFAULT_CLEVEL , wrkmem); - -/* -* Prior lz4hc_compress will return -1 in case of error -* and 0 on success -* while new LZ4_compress_HC -* returns 0 in case of error -* and the output length on success -*/ -if (!*dst_len) -return -1 ; -else -return 0 ; -} -EXPORT_SYMBOL (lz4hc_compress); -MODULE_LICENSE ("Dual BSD/GPL" );MODULE_DESCRIPTION ("LZ4 HC compressor" );
Signature · 1 line
2.1.4
Hello Sven,
Quoted 30 lines · Sven Schmidt
This patchset is for updating the LZ4 compression module to a version based on LZ4 v1.7.3 allowing to use the fast compression algorithm aka LZ4 fast which provides an "acceleration" parameter as a tradeoff between high compression ratio and high compression speed.
We want to use LZ4 fast in order to support compression in lustre and (mostly, based on that) investigate data reduction techniques in behalf of storage systems.
Also, it will be useful for other users of LZ4 compression, as with LZ4 fast it is possible to enable applications to use fast and/or high compression depending on the usecase.
For instance, ZRAM is offering a LZ4 backend and could benefit from an updated LZ4 in the kernel.LZ4 homepage: http://www.lz4.org/
LZ4 source repository: https://github.com/lz4/lz4
Source version: 1.7.3Benchmark (taken from [1], Core i5-4300U @1.9GHz):
----------------|--------------|----------------|---------- Compressor | Compression | Decompression | Ratio ----------------|--------------|----------------|---------- memcpy | 4200 MB/s | 4200 MB/s | 1.000 LZ4 fast 50 | 1080 MB/s | 2650 MB/s | 1.375 LZ4 fast 17 | 680 MB/s | 2220 MB/s | 1.607 LZ4 fast 5 | 475 MB/s | 1920 MB/s | 1.886 LZ4 default | 385 MB/s | 1850 MB/s | 2.101
Quoted 7 lines
[1] http://fastcompression.blogspot.de/2015/04/sampling-or-faster-lz4.html
[PATCH 1/5] lib: Update LZ4 compressor module
[PATCH 2/5]lib/decompress_unlz4: Change module to work with new LZ4 module version [PATCH 3/5] crypto: Change LZ4 modules to work with new LZ4 module version [PATCH 4/5] fs/pstore: fs/squashfs: Change usage of LZ4 to work with new LZ4 version [PATCH 5/5] lib/lz4: Remove back-compat wrappers
Today, I did zram-lz4 performance test with fio in current mmotm and found it makes regression about 20%.
"lz4-update" means current mmots(git://git.cmpxchg.org/linux-mmots.git) so applied your 5 patches. (But now sure current mmots has recent uptodate patches)
"revert" means I reverted your 5 patches in current mmots.
revert lz4-update
seq-write 1547 1339 86.55%
rand-write 22775 19381 85.10%
seq-read 7035 5589 79.45%
rand-read 78556 68479 87.17%
mixed-seq(R) 1305 1066 81.69%
mixed-seq(W) 1205 984 81.66%
mixed-rand(R) 17421 14993 86.06%
mixed-rand(W) 17391 14968 86.07%My fio description file
[global]
bs=4k
ioengine=sync
size=100m
numjobs=1group_reporting
buffer_compress_percentage=30
scramble_buffers=0
filename=/dev/zram0
loops=10
fsync_on_close=1
[seq-write]
bs=64k
rw=write
stonewall
[rand-write]
rw=randwrite
stonewall
[seq-read]
bs=64k
rw=read
stonewall
[rand-read]
rw=randread
stonewall
[mixed-seq]
bs=64k
rw=rw
stonewall
[mixed-rand]
rw=randrw
stonewall
Quoted 4 lines · Minchan Kim
Today, I did zram-lz4 performance test with fio in current mmotm and found it makes regression about 20%.
This may or may not be the cause of the specific regression you're observing, but I just noticed that the proposed patch drops a lot of FORCEINLINE annotations from upstream LZ4. The FORCEINLINE's are there for a reason, especially for the main decompression and compression functions which are basically "templates" that take in different sets of constant parameters, and should be left in. We should #define FORCEINLINE to __always_inline somewhere, or just do a s/FORCEINLINE/__always_inline/g.
Note that the upstream LZ4 code is very carefully optimized, so we should not, in general, be changing things like when functions are force-inlined, what the hash table size is, etc.
[Also, for some reason linux-crypto is apparently still not receiving patch 1/5 in the series. It's missing from the linux-crypto archive at
http://www.spinics.net/lists/linux-crypto/, so it's not just me.]
Thanks!
Eric
Also I noticed another bug, this time in LZ4_count():
Quoted 5 lines
#if
defined(CONFIG_64BIT)
#defineLZ4_ARCH641
#else
#defineLZ4_ARCH640
#endif
...
Quoted 6 lines
#ifdef LZ4_ARCH64 if ((pIn < (pInLimit-3)) && (LZ4_read32(pMatch) == LZ4_read32(pIn))) { pIn += 4; pMatch += 4; }#endif
Because of how LZ4_ARCH64 is defined, it needs to be '#if LZ4_ARCH64'.
But I also think the way upstream LZ4 does 64-bit detection could have just been left as-is; it has a function which gets inlined:
static unsigned LZ4_64bits(void) { return sizeof(void*)==8; }Eric
Hey Eric,
Quoted 15 lines · Eric Biggers
Also I noticed another bug, this time in
LZ4_count():Quoted 5 lines
#if
defined(CONFIG_64BIT)
#defineLZ4_ARCH641
#else
#defineLZ4_ARCH640
#endif...
Quoted 6 lines
#ifdef LZ4_ARCH64 if ((pIn < (pInLimit-3)) && (LZ4_read32(pMatch) == LZ4_read32(pIn))) { pIn += 4; pMatch += 4; }#endif
Quoted 8 lines
Because of how
LZ4_ARCH64is defined, it needs to be '#ifLZ4_ARCH64'.But I also think the way upstream LZ4 does 64-bit detection could have just been left as-is; it has a function which gets inlined:
static unsigned LZ4_64bits(void) { return sizeof(void*)==8; }Eric
does this apply for LZ4_isLittleEndian() as well? As a reminder:
static unsigned LZ4_isLittleEndian(void)
{
/* don't use static : performance detrimental */
const union { U32 u; BYTE c[4]; } one = { 1 };
return one.c[0];
}It is surely easier to read and understand using these functions in favor of the macros.
Thanks,
Sven
Quoted 19 lines · Sven Schmidt
Quoted 8 lines
Because of how
LZ4_ARCH64is defined, it needs to be '#ifLZ4_ARCH64'.But I also think the way upstream LZ4 does 64-bit detection could have just been left as-is; it has a function which gets inlined:
static unsigned LZ4_64bits(void) { return sizeof(void*)==8; }Eric
does this apply for
LZ4_isLittleEndian()as well? As a reminder:static unsigned LZ4_isLittleEndian(void) { /* don't use static : performance detrimental */ const union { U32 u; BYTE c[4]; } one = { 1 }; return one.c[0]; }
Quoted 1 line
It is surely easier to read and understand using these functions in favor of the macros.
Yes, it makes sense to retain LZ4_isLittleEndian() too, mainly so that the call sites don't need to be changed and we have less chance of introducing bugs.
I also believe the *implementation* of LZ4_isLittleEndian() using the union "hack" to detecting endianness works fine and will get optimized correctly, though we could replace it with an #ifdef __LITTLE_ENDIAN__ if we wanted to. (I am sure that upstream LZ4 would do that if it was guaranteed to work, but upstream LZ4 needs to detect endianness reliably across many more different compilers, compiler versions, and platforms than the Linux kernel does, and there is no standard preprocessor macro for doing so.)
Eric
Hello Eric,
Quoted 15 lines · Eric Biggers
Also I noticed another bug, this time in
LZ4_count():Quoted 5 lines
#if
defined(CONFIG_64BIT)
#defineLZ4_ARCH641
#else
#defineLZ4_ARCH640
#endif...
Quoted 6 lines
#ifdef LZ4_ARCH64 if ((pIn < (pInLimit-3)) && (LZ4_read32(pMatch) == LZ4_read32(pIn))) { pIn += 4; pMatch += 4; }#endif
Quoted 7 lines
Because of how
LZ4_ARCH64is defined, it needs to be '#ifLZ4_ARCH64'.But I also think the way upstream LZ4 does 64-bit detection could have just been left as-is; it has a function which gets inlined:
static unsigned LZ4_64bits(void) { return sizeof(void*)==8; }
Thanks for looking this.
If you have a fix, could you send a patch? I'm happy to test it.
Hey Eric,
Quoted 6 lines · Eric Biggers
Quoted 4 lines · Minchan Kim
Today, I did zram-lz4 performance test with fio in current mmotm and found it makes regression about 20%.
Quoted 8 lines
This may or may not be the cause of the specific regression you're observing, but I just noticed that the proposed patch drops a lot of FORCEINLINE annotations from upstream LZ4. The FORCEINLINE's are there for a reason, especially for the main decompression and compression functions which are basically "templates" that take in different sets of constant parameters, and should be left in. We should #define FORCEINLINE to
__always_inlinesomewhere, or just do a s/FORCEINLINE/__always_inline/g.
I generally just replaced "FORCE_INLINE" by "static inline". At least I thought so. I rechecked and realised, I missed at least two of them (why did I not just use "search+replace"?). So I think it's maybe safer and easier to eventually just use "FORCE_INLINE"
with the definition you suggested. Will try that.
Quoted 8 lines
Note that the upstream LZ4 code is very carefully optimized, so we should not, in general, be changing things like when functions are force-inlined, what the hash table size is, etc.
[Also, for some reason linux-crypto is apparently still not receiving patch 1/5 in the series. It's missing from the linux-crypto archive at
http://www.spinics.net/lists/linux-crypto/, so it's not just me.]
I don't really know what to do about this. I think the matter is the size of the E-Mail. Are there filters or something like that? Since in linux-kernel the patch seems to get delivered. I could otherwise CC you if you wish.
Thanks,
Sven
Quoted 10 lines · Sven Schmidt
Quoted 5 lines
[Also, for some reason linux-crypto is apparently still not receiving patch 1/5 in the series. It's missing from the linux-crypto archive at
http://www.spinics.net/lists/linux-crypto/, so it's not just me.]I don't really know what to do about this. I think the matter is the size of the E-Mail. Are there filters or something like that? Since in linux-kernel the patch seems to get delivered. I could otherwise CC you if you wish.
If I'm not mistaken, David Miller is the admin of the mail server on vger.kernel.org, and he already happens to be Cc'ed on this thread, so maybe he can answer as to why linux-crypto may be configured differently?
Anyway, since the patch did make it to linux-kernel anyone can still download it from patchwork if they know where to look:
https://patchwork.kernel.org/patch/9556271/
Eric
David Miller
maintainerFrom: Eric Biggers <[email protected]>
Date: Thu, 9 Feb 2017 10:29:14 -0800
Quoted 12 lines · Sven Schmidt
Quoted 10 lines
Quoted 5 lines
[Also, for some reason linux-crypto is apparently still not receiving patch 1/5 in the series. It's missing from the linux-crypto archive at
http://www.spinics.net/lists/linux-crypto/, so it's not just me.]I don't really know what to do about this. I think the matter is the size of the E-Mail. Are there filters or something like that? Since in linux-kernel the patch seems to get delivered. I could otherwise CC you if you wish.
Quoted 7 lines
If I'm not mistaken, David Miller is the admin of the mail server on vger.kernel.org, and he already happens to be Cc'ed on this thread, so maybe he can answer as to why linux-crypto may be configured differently?
Anyway, since the patch did make it to linux-kernel anyone can still download it from patchwork if they know where to look:
https://patchwork.kernel.org/patch/9556271/
I've increased the maxlength parameter for linux-cryto so this doesn't happen again.
Hey Minchan,
Quoted 49 lines · Minchan Kim
Hello Sven,
Quoted 30 lines · Sven Schmidt
This patchset is for updating the LZ4 compression module to a version based on LZ4 v1.7.3 allowing to use the fast compression algorithm aka LZ4 fast which provides an "acceleration" parameter as a tradeoff between high compression ratio and high compression speed.
We want to use LZ4 fast in order to support compression in lustre and (mostly, based on that) investigate data reduction techniques in behalf of storage systems.
Also, it will be useful for other users of LZ4 compression, as with LZ4 fast it is possible to enable applications to use fast and/or high compression depending on the usecase.
For instance, ZRAM is offering a LZ4 backend and could benefit from an updated LZ4 in the kernel.LZ4 homepage: http://www.lz4.org/
LZ4 source repository: https://github.com/lz4/lz4
Source version: 1.7.3Benchmark (taken from [1], Core i5-4300U @1.9GHz):
----------------|--------------|----------------|---------- Compressor | Compression | Decompression | Ratio ----------------|--------------|----------------|---------- memcpy | 4200 MB/s | 4200 MB/s | 1.000 LZ4 fast 50 | 1080 MB/s | 2650 MB/s | 1.375 LZ4 fast 17 | 680 MB/s | 2220 MB/s | 1.607 LZ4 fast 5 | 475 MB/s | 1920 MB/s | 1.886 LZ4 default | 385 MB/s | 1850 MB/s | 2.101Quoted 7 lines
[1] http://fastcompression.blogspot.de/2015/04/sampling-or-faster-lz4.html
[PATCH 1/5] lib: Update LZ4 compressor module
[PATCH 2/5]lib/decompress_unlz4: Change module to work with new LZ4 module version [PATCH 3/5] crypto: Change LZ4 modules to work with new LZ4 module version [PATCH 4/5] fs/pstore: fs/squashfs: Change usage of LZ4 to work with new LZ4 version [PATCH 5/5] lib/lz4: Remove back-compat wrappersToday, I did zram-lz4 performance test with fio in current mmotm and found it makes regression about 20%.
"lz4-update" means current mmots(git://git.cmpxchg.org/linux-mmots.git) so applied your 5 patches. (But now sure current mmots has recent uptodate patches)
"revert" means I reverted your 5 patches in current mmots.
Quoted 10 lines
revert lz4-update
seq-write 1547 1339 86.55%
rand-write 22775 19381 85.10%
seq-read 7035 5589 79.45%
rand-read 78556 68479 87.17%
mixed-seq(R) 1305 1066 81.69%
mixed-seq(W) 1205 984 81.66%
mixed-rand(R) 17421 14993 86.06%
mixed-rand(W) 17391 14968 86.07%which parts of the output (as well as units) are these values exactly?
I did not work with fio until now, so I think I might ask before misinterpreting my results.
Quoted 32 lines
My fio description file
[global]
bs=4k
ioengine=sync
size=100m
numjobs=1group_reporting
buffer_compress_percentage=30
scramble_buffers=0
filename=/dev/zram0
loops=10
fsync_on_close=1[seq-write]
bs=64k
rw=write
stonewall[rand-write]
rw=randwrite
stonewall[seq-read]
bs=64k
rw=read
stonewall[rand-read]
rw=randread
stonewall
Quoted 9 lines
[mixed-seq]
bs=64k
rw=rw
stonewall[mixed-rand]
rw=randrw
stonewall
Great, this makes it easy for me to reproduce your test.
Thanks,
Sven
Hello Sven,
Quoted 63 lines · Sven Schmidt
Hey Minchan,
Quoted 49 lines · Minchan Kim
Hello Sven,
Quoted 30 lines · Sven Schmidt
This patchset is for updating the LZ4 compression module to a version based on LZ4 v1.7.3 allowing to use the fast compression algorithm aka LZ4 fast which provides an "acceleration" parameter as a tradeoff between high compression ratio and high compression speed.
We want to use LZ4 fast in order to support compression in lustre and (mostly, based on that) investigate data reduction techniques in behalf of storage systems.
Also, it will be useful for other users of LZ4 compression, as with LZ4 fast it is possible to enable applications to use fast and/or high compression depending on the usecase.
For instance, ZRAM is offering a LZ4 backend and could benefit from an updated LZ4 in the kernel.LZ4 homepage: http://www.lz4.org/
LZ4 source repository: https://github.com/lz4/lz4
Source version: 1.7.3Benchmark (taken from [1], Core i5-4300U @1.9GHz):
----------------|--------------|----------------|---------- Compressor | Compression | Decompression | Ratio ----------------|--------------|----------------|---------- memcpy | 4200 MB/s | 4200 MB/s | 1.000 LZ4 fast 50 | 1080 MB/s | 2650 MB/s | 1.375 LZ4 fast 17 | 680 MB/s | 2220 MB/s | 1.607 LZ4 fast 5 | 475 MB/s | 1920 MB/s | 1.886 LZ4 default | 385 MB/s | 1850 MB/s | 2.101Quoted 7 lines
[1] http://fastcompression.blogspot.de/2015/04/sampling-or-faster-lz4.html
[PATCH 1/5] lib: Update LZ4 compressor module
[PATCH 2/5]lib/decompress_unlz4: Change module to work with new LZ4 module version [PATCH 3/5] crypto: Change LZ4 modules to work with new LZ4 module version [PATCH 4/5] fs/pstore: fs/squashfs: Change usage of LZ4 to work with new LZ4 version [PATCH 5/5] lib/lz4: Remove back-compat wrappersToday, I did zram-lz4 performance test with fio in current mmotm and found it makes regression about 20%.
"lz4-update" means current mmots(git://git.cmpxchg.org/linux-mmots.git) so applied your 5 patches. (But now sure current mmots has recent uptodate patches)
"revert" means I reverted your 5 patches in current mmots.Quoted 10 lines
revert lz4-update seq-write 1547 1339 86.55% rand-write 22775 19381 85.10% seq-read 7035 5589 79.45% rand-read 78556 68479 87.17% mixed-seq(R) 1305 1066 81.69% mixed-seq(W) 1205 984 81.66% mixed-rand(R) 17421 14993 86.06% mixed-rand(W) 17391 14968 86.07%
Quoted 2 lines
which parts of the output (as well as units) are these values exactly?
I did not work with fio until now, so I think I might ask before misinterpreting my results.
It is IOPS.
Quoted 38 lines
Quoted 38 lines
My fio description file
[global]
bs=4k
ioengine=sync
size=100m
numjobs=1group_reporting
buffer_compress_percentage=30
scramble_buffers=0
filename=/dev/zram0
loops=10
fsync_on_close=1[seq-write]
bs=64k
rw=write
stonewall[rand-write]
rw=randwrite
stonewall[seq-read]
bs=64k
rw=read
stonewall[rand-read]
rw=randread
stonewall[mixed-seq]
bs=64k
rw=rw
stonewall
Quoted 6 lines
Quoted 4 lines
[mixed-rand]
rw=randrw
stonewallGreat, this makes it easy for me to reproduce your test.
If you have trouble to reproduce, feel free to ask me. I'm happy to test it. :)
Thanks!
Quoted 114 lines · Minchan Kim
Hello Sven,
Quoted 63 lines · Sven Schmidt
Hey Minchan,
Quoted 49 lines · Minchan Kim
Hello Sven,
Quoted 30 lines · Sven Schmidt
This patchset is for updating the LZ4 compression module to a version based on LZ4 v1.7.3 allowing to use the fast compression algorithm aka LZ4 fast which provides an "acceleration" parameter as a tradeoff between high compression ratio and high compression speed.
We want to use LZ4 fast in order to support compression in lustre and (mostly, based on that) investigate data reduction techniques in behalf of storage systems.
Also, it will be useful for other users of LZ4 compression, as with LZ4 fast it is possible to enable applications to use fast and/or high compression depending on the usecase.
For instance, ZRAM is offering a LZ4 backend and could benefit from an updated LZ4 in the kernel.LZ4 homepage: http://www.lz4.org/
LZ4 source repository: https://github.com/lz4/lz4
Source version: 1.7.3Benchmark (taken from [1], Core i5-4300U @1.9GHz):
----------------|--------------|----------------|---------- Compressor | Compression | Decompression | Ratio ----------------|--------------|----------------|---------- memcpy | 4200 MB/s | 4200 MB/s | 1.000 LZ4 fast 50 | 1080 MB/s | 2650 MB/s | 1.375 LZ4 fast 17 | 680 MB/s | 2220 MB/s | 1.607 LZ4 fast 5 | 475 MB/s | 1920 MB/s | 1.886 LZ4 default | 385 MB/s | 1850 MB/s | 2.101Quoted 7 lines
[1] http://fastcompression.blogspot.de/2015/04/sampling-or-faster-lz4.html
[PATCH 1/5] lib: Update LZ4 compressor module
[PATCH 2/5]lib/decompress_unlz4: Change module to work with new LZ4 module version [PATCH 3/5] crypto: Change LZ4 modules to work with new LZ4 module version [PATCH 4/5] fs/pstore: fs/squashfs: Change usage of LZ4 to work with new LZ4 version [PATCH 5/5] lib/lz4: Remove back-compat wrappersToday, I did zram-lz4 performance test with fio in current mmotm and found it makes regression about 20%.
"lz4-update" means current mmots(git://git.cmpxchg.org/linux-mmots.git) so applied your 5 patches. (But now sure current mmots has recent uptodate patches)
"revert" means I reverted your 5 patches in current mmots.Quoted 10 lines
revert lz4-update seq-write 1547 1339 86.55% rand-write 22775 19381 85.10% seq-read 7035 5589 79.45% rand-read 78556 68479 87.17% mixed-seq(R) 1305 1066 81.69% mixed-seq(W) 1205 984 81.66% mixed-rand(R) 17421 14993 86.06% mixed-rand(W) 17391 14968 86.07%Quoted 2 lines
which parts of the output (as well as units) are these values exactly?
I did not work with fio until now, so I think I might ask before misinterpreting my results.It is IOPS.
Quoted 43 lines
Quoted 42 lines
My fio description file
[global]
bs=4k
ioengine=sync
size=100m
numjobs=1group_reporting
buffer_compress_percentage=30
scramble_buffers=0
filename=/dev/zram0
loops=10
fsync_on_close=1[seq-write]
bs=64k
rw=write
stonewall[rand-write]
rw=randwrite
stonewall[seq-read]
bs=64k
rw=read
stonewall[rand-read]
rw=randread
stonewall[mixed-seq]
bs=64k
rw=rw
stonewall[mixed-rand]
rw=randrw
stonewall
Quoted 6 lines
Quoted 1 line
Great, this makes it easy for me to reproduce your test.
If you have trouble to reproduce, feel free to ask me. I'm happy to test it. :)
Thanks!
Hi Minchan,
I will send an updated patch as a reply to this E-Mail. Would be really grateful If you'd test it and provide feedback! The patch should be applied to the current mmots tree.
In fact, the updated LZ4 _is_ slower than the current one in kernel. But I was not able to reproduce such large regressions as you did. I now tried to define FORCE_INLINE as Eric suggested. I also inlined some functions which weren't in upstream LZ4, but are defined as macros in the current kernel LZ4. The approach to replace LZ4_ARCH64 with the function call _seemed_ to behave worse than the macro, so I withdrew the change.
The main difference is, that I replaced the read32/read16/write... etc. functions using memcpy with the other ones defined in upstream LZ4 (which can be switched using a macro).
The comment of the author stated, that they're as fast as the memcpy variants (or faster), but not as portable (which does not matter since we're not dependent for multiple compilers).
In my tests, this version is mostly as fast as the current kernel LZ4.
Thank you!
Sven
PATCH 1/1 lz4: fix performance regressions
Fix performance regressions compared to current kernel LZ4
--- include/linux/lz4.h | 2 +- lib/lz4/lz4_compress.c | 157 +++++++++++++++++++++++------------- lib/lz4/lz4_decompress.c | 50 ++++++++---- lib/lz4/lz4defs.h | 203 ++++++++++++++++++++++++++++++++--------------- lib/lz4/lz4hc_compress.c | 8 +- 5 files changed, 281 insertions(+), 139 deletions(-)
include/linux/lz4.h +1 -1
@@ -82,7 +82,7 @@/*-************************************************************************ * STREAMING CONSTANTS AND STRUCTURES **************************************************************************/ -#define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE-3)) + 4) +#define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE - 3)) + 4)#define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U64 * sizeof(unsigned long long))#define LZ4_STREAMHCSIZE 262192
lib/lz4/lz4_compress.c +101 -56
413 lines not on this page · raw message
lib/lz4/lz4_decompress.c +34 -16
136 lines not on this page · raw message
lib/lz4/lz4defs.h +141 -62
297 lines not on this page · raw message
lib/lz4/lz4hc_compress.c +4 -4
@@ -71,7 +71,7 @@ static void LZ4HC_init(LZ4HC_CCtx_internal *hc4, const BYTE *start) }/* Update chains up to ip (excluded) */ -static inline void LZ4HC_Insert (LZ4HC_CCtx_internal *hc4, +static FORCE_INLINE void LZ4HC_Insert(LZ4HC_CCtx_internal *hc4,const BYTE *ip) {U16 *const chainTable = hc4->chainTable;
@@ -96,7 +96,7 @@ static inline void LZ4HC_Insert(LZ4HC_CCtx_internal *hc4, hc4->nextToUpdate = target; } -static inline int LZ4HC_InsertAndFindBestMatch( +static FORCE_INLINE int LZ4HC_InsertAndFindBestMatch( LZ4HC_CCtx_internal *hc4, /* Index table will be updated */ const BYTE *ip, const BYTE * const iLimit,
@@ -165,7 +165,7 @@ static inline int LZ4HC_InsertAndFindBestMatch(return (int )ml; } -static inline int LZ4HC_InsertAndGetWiderMatch( +static FORCE_INLINE int LZ4HC_InsertAndGetWiderMatch( LZ4HC_CCtx_internal *hc4, const BYTE * const ip, const BYTE * const iLowLimit,
@@ -259,7 +259,7 @@ static inline int LZ4HC_InsertAndGetWiderMatch(return longest; } -static inline int LZ4HC_encodeSequence( +static FORCE_INLINE int LZ4HC_encodeSequence( const BYTE **ip, BYTE **op, const BYTE **anchor,
Re: [PATCH] lz4: fix performance regressions
Hi Sven,
Quoted 1 line · Sven Schmidt
Fix performance regressions compared to current kernel LZ4
Your patch contains mostly style cleanups which certainly are welcome but make the whole patch hard to review. These cleanups would have been better into a separate, preliminary patch IMHO.
Regards,
Willy
Re: [PATCH] lz4: fix performance regressions
Quoted 11 lines · Willy Tarreau
Hi Sven,
Quoted 1 line · Sven Schmidt
Fix performance regressions compared to current kernel LZ4
Your patch contains mostly style cleanups which certainly are welcome but make the whole patch hard to review. These cleanups would have been better into a separate, preliminary patch IMHO.
Regards,
Willy
Hi Willy,
the problem was, I wanted to compare my version to the upstream LZ4 to find bugs (as with my last patch version: wrong indentation in LZ4HC in two for loops). But since the LZ4 code is a pain to read, I made additional style cleanups "on the way".
Hope you can manage to review the patch though, because it is difficult to separate the cleanups now.
Please feel free to ask if you stumble upon something.
Greetings,
Sven
Re: [PATCH] lz4: fix performance regressions
Quoted 10 lines · Sven Schmidt
Quoted 9 lines · Willy Tarreau
Hi Sven,
Quoted 1 line · Sven Schmidt
Fix performance regressions compared to current kernel LZ4
Your patch contains mostly style cleanups which certainly are welcome but make the whole patch hard to review. These cleanups would have been better into a separate, preliminary patch IMHO.
Quoted 7 lines
Quoted 2 lines
Regards,
WillyHi Willy,
the problem was, I wanted to compare my version to the upstream LZ4 to find bugs (as with my last patch version: wrong indentation in LZ4HC in two for loops). But since the LZ4 code is a pain to read, I made additional style cleanups "on the way".
Oh I can easily understand!
Quoted 1 line
Hope you can manage to review the patch though, because it is difficult to separate the cleanups now.
When I need to split a patch into pieces, usually what I do is that I revert it, re-apply it without committing, then "git add -p", validate all the hunks to be taken as the first patch (ie here the cleanups), commit, then commit the rest as a separate one. It seems to me that the fix is in the last few hunks though I'm not sure yet.
Thanks,
Willy
Re: [PATCH] lz4: fix performance regressions
Quoted 23 lines · Willy Tarreau
Quoted 10 lines · Sven Schmidt
Quoted 9 lines · Willy Tarreau
Hi Sven,
Quoted 1 line · Sven Schmidt
Fix performance regressions compared to current kernel LZ4
Your patch contains mostly style cleanups which certainly are welcome but make the whole patch hard to review. These cleanups would have been better into a separate, preliminary patch IMHO.
Quoted 7 lines
Quoted 2 lines
Regards,
WillyHi Willy,
the problem was, I wanted to compare my version to the upstream LZ4 to find bugs (as with my last patch version: wrong indentation in LZ4HC in two for loops). But since the LZ4 code is a pain to read, I made additional style cleanups "on the way".
Oh I can easily understand!
Quoted 1 line
Hope you can manage to review the patch though, because it is difficult to separate the cleanups now.
Quoted 8 lines
When I need to split a patch into pieces, usually what I do is that I revert it, re-apply it without committing, then "git add -p", validate all the hunks to be taken as the first patch (ie here the cleanups), commit, then commit the rest as a separate one. It seems to me that the fix is in the last few hunks though I'm not sure yet.
Thanks,
Willy
Hi Willy,
I didn't know about this 'trick' until now. Thanks for sharing it! I gave it a short try recently, that's really cool!
Since the problem discussed in this branch of this thread seems to be solved (see Minchans E-Mail), I won't split the patches, though. Or is there an actual need for doing so? I will send an updated patchset (containing these patches + the other ones suggested by Eric) later.
Regards,
Sven
Re: [PATCH] lz4: fix performance regressions
Quoted 33 lines · Sven Schmidt
Quoted 23 lines · Willy Tarreau
Quoted 10 lines · Sven Schmidt
Quoted 9 lines · Willy Tarreau
Hi Sven,
Quoted 1 line · Sven Schmidt
Fix performance regressions compared to current kernel LZ4
Your patch contains mostly style cleanups which certainly are welcome but make the whole patch hard to review. These cleanups would have been better into a separate, preliminary patch IMHO.
Quoted 7 lines
Quoted 2 lines
Regards,
WillyHi Willy,
the problem was, I wanted to compare my version to the upstream LZ4 to find bugs (as with my last patch version: wrong indentation in LZ4HC in two for loops). But since the LZ4 code is a pain to read, I made additional style cleanups "on the way".
Oh I can easily understand!
Quoted 1 line
Hope you can manage to review the patch though, because it is difficult to separate the cleanups now.
Quoted 8 lines
When I need to split a patch into pieces, usually what I do is that I revert it, re-apply it without committing, then "git add -p", validate all the hunks to be taken as the first patch (ie here the cleanups), commit, then commit the rest as a separate one. It seems to me that the fix is in the last few hunks though I'm not sure yet.
Thanks,
Willy
Quoted 6 lines
Hi Willy,
I didn't know about this 'trick' until now. Thanks for sharing it! I gave it a short try recently, that's really cool!
Since the problem discussed in this branch of this thread seems to be solved (see Minchans E-Mail), I won't split the patches, though. Or is there an actual need for doing so? I will send an updated patchset (containing these patches + the other ones suggested by Eric) later.
It's probably too late for this time, but keep it in mind for next time :-)
willy
Re: [PATCH] lz4: fix performance regressions
In reply to Sven SchmidtHi Sven,
Quoted 65 lines · Sven Schmidt
/*-************************************
* Reading and writing into memory
**************************************/lib/lz4/lz4defs.h +17
-25+typedef union { +U16 u16; +U32 u32; +size_t uArch; +} __packed unalign; -static inline U16 LZ4_read16 (const void *memPtr) +static FORCE_INLINE __maybe_unused U16 LZ4_read16(const void *ptr) { -U16 val; - -memcpy (&val, memPtr,sizeof (val)); - -return val; +return ((const unalign *)ptr)->u16; } -static inline U32 LZ4_read32 (const void *memPtr) +static FORCE_INLINE __maybe_unused U32 LZ4_read32(const void *ptr) { -U32 val; - -memcpy (&val, memPtr,sizeof (val)); - -return val; +return ((const unalign *)ptr)->u32; } -static inline size_t LZ4_read_ARCH (const void *memPtr) +static FORCE_INLINE __maybe_unused size_t LZ4_read_ARCH(const void *ptr) { -size_t val; - -memcpy (&val, memPtr,sizeof (val)); - -return val; +return ((const unalign *)ptr)->uArch; } -static inline void LZ4_write16 (void *memPtr,U16 value) +static FORCE_INLINE __maybe_unused void LZ4_write16(void *memPtr, U16 value) { -memcpy (memPtr, &value,sizeof (value)); + ((unalign *)memPtr)->u16 = value; } -static inline void LZ4_write32 (void *memPtr,U32 value) -{ -memcpy (memPtr, &value,sizeof (value)); +static FORCE_INLINE __maybe_unused void LZ4_write32(void *memPtr, U32 value) { + ((unalign *)memPtr)->u32 = value; } -static inline U16 LZ4_readLE16 (const void *memPtr) +static FORCE_INLINE __maybe_unused U16 LZ4_readLE16(const void *memPtr) { -#ifdef __LITTLE_ENDIAN__ +#if LZ4_LITTLE_ENDIAN return LZ4_read16 (memPtr);#else const BYTE *p = (const BYTE *)memPtr;
lib/lz4/lz4defs.h static inline U16 LZ4_readLE16(const void *memPtr) · line 144
Quoted 2 lines
lib/lz4/lz4defs.h +0
-0@@ -137,19 +143,19 @@ static inline U16 LZ4_readLE16(const void *memPtr)#endif }
Since upstream LZ4 is intended to be compiled at -O3, this may allow it to get away with using memcpy() for unaligned memory accesses. The reason it uses memcpy() is that, other than a byte-by-byte copy, it is the only portable way to express unaligned memory accesses. But the Linux kernel is sometimes compiled optimized for size (-Os), and I wouldn't be *too* surprised if some of the memcpy()'s don't always get inlined then, which could be causing the performance regression being observed. (Of course, this could be verified by checking whether CONFIG_CC_OPTIMIZE_FOR_SIZE=y is set, then reading the assembly.)
But I don't think accessing a __packed structure directly is the right alternative. Instead, Linux already includes macros for unaligned memory accesses which have been optimized for every supported architecture. Those should just be used instead, e.g. like this:
static FORCE_INLINE U16 LZ4_read16(const void *ptr)
{
return get_unaligned((const u16 *)ptr);
}static FORCE_INLINE U32 LZ4_read32(const void *ptr)
{
return get_unaligned((const u32 *)ptr);
}static FORCE_INLINE size_t LZ4_read_ARCH(const void *ptr)
{
return get_unaligned((const size_t *)ptr);
}static FORCE_INLINE void LZ4_write16(void *memPtr, U16 value)
{
put_unaligned(value, (u16 *)memPtr);
}static FORCE_INLINE void LZ4_write32(void *memPtr, U32 value)
{
put_unaligned(value, (u32 *)memPtr);
}static FORCE_INLINE U16 LZ4_readLE16(const void *memPtr)
{
return get_unaligned_le16(memPtr);
}static FORCE_INLINE void LZ4_writeLE16(void *memPtr, U16 value)
{
return put_unaligned_le16(value, memPtr);
}static FORCE_INLINE void LZ4_copy8(void *dst, const void *src)
{
if (LZ4_64bits()) {
u64 a = get_unaligned((const u64 *)src);
put_unaligned(a, (u64 *)dst);
} else {
u32 a = get_unaligned((const u32 *)src);
u32 b = get_unaligned((const u32 *)src + 1);
put_unaligned(a, (u32 *)dst);
put_unaligned(b, (u32 *)dst + 1);
}
}Note that I dropped __maybe_unused as it's not needed on inline functions. That should be done everywhere else the patch proposes to add it too.
Quoted 76 lines
lib/lz4/lz4defs.h +63
-10-#if LZ4_ARCH64 -#ifdef __BIG_ENDIAN__ -#define LZ4_NBCOMMONBYTES (val) (__builtin_clzll(val) >> 3) +static FORCE_INLINE unsigned int LZ4_NbCommonBytes(register size_t val) +{ +#if LZ4_LITTLE_ENDIAN +#if LZ4_ARCH64 /* 64 Bits Little Endian */ +#if defined(LZ4_FORCE_SW_BITCOUNT ) +static const int DeBruijnBytePos[64 ] = { +0 ,0 ,0 ,0 ,0 ,1 ,1 ,2 ,0 ,3 ,1 ,3 ,1 ,4 ,2 ,7 , +0 ,2 ,3 ,6 ,1 ,5 ,3 ,5 ,1 ,3 ,4 ,4 ,2 ,5 ,6 ,7 , +7 ,0 ,1 ,2 ,3 ,3 ,4 ,6 ,2 ,6 ,5 ,5 ,3 ,4 ,5 ,6 , +7 ,1 ,2 ,4 ,6 ,4 ,4 ,5 ,7 ,2 ,6 ,5 ,7 ,6 ,7 ,7 + }; + +return DeBruijnBytePos[((U64 )((val & -(long long )val) + *0x0218A392CDABBD3FULL )) >>58 ];#else -#define LZ4_NBCOMMONBYTES (val) (__builtin_ctzll(val) >> 3) -#endif +return (__builtin_ctzll ((U64 )val) >>3 ); +#endif /* defined(LZ4_FORCE_SW_BITCOUNT) */ +#else /* 32 Bits Little Endian */ +#if defined(LZ4_FORCE_SW_BITCOUNT ) +static const int DeBruijnBytePos[32 ] = { +0 ,0 ,3 ,0 ,3 ,1 ,3 ,0 ,3 ,2 ,2 ,1 ,3 ,2 ,0 ,1 , +3 ,3 ,1 ,2 ,2 ,2 ,2 ,0 ,3 ,1 ,2 ,0 ,1 ,0 ,1 ,1 + }; + +return DeBruijnBytePos[((U32 )((val & -(S32 )val) + *0x077CB531U )) >>27 ];#else -#ifdef __BIG_ENDIAN__ -#define LZ4_NBCOMMONBYTES (val) (__builtin_clz(val) >> 3) +return (__builtin_ctz ((U32 )val) >>3 ); +#endif /* defined(LZ4_FORCE_SW_BITCOUNT) */ +#endif /* LZ4_ARCH64 */ +#else /* Big Endian */ +#if LZ4_ARCH64 /* 64 Bits Big Endian */ +#if defined(LZ4_FORCE_SW_BITCOUNT ) +unsigned int r; + +if (!(val >>32 )) { + r =4 ; + }else { + r =0 ; + val >>=32 ; + } + +if (!(val >>16 )) { + r +=2 ; + val >>=8 ; + }else { + val >>=24 ; + } + + r += (!val); + +return r;#else -#define LZ4_NBCOMMONBYTES (val) (__builtin_ctz(val) >> 3) -#endif -#endif +return (__builtin_clzll ((U64 )val) >>3 ); +#endif /* defined(LZ4_FORCE_SW_BITCOUNT) */ +#else /* 32 Bits Big Endian */ +#if defined(LZ4_FORCE_SW_BITCOUNT ) +unsigned int r; + +if (!(val >>16 )) { + r =2 ; + val >>=8 ; + }else { + r =0 ; + val >>=24 ; + }
lib/lz4/lz4defs.h static inline void LZ4_wildCopy(void *dstPtr, const void *srcPtr, void *dstEnd) · line 256
Quoted 10 lines
lib/lz4/lz4defs.h +10
-0+ + r += (!val); + +return r; +#else +return (__builtin_clz ((U32 )val) >>3 ); +#endif /* defined(LZ4_FORCE_SW_BITCOUNT) */ +#endif /* LZ4_ARCH64 */ +#endif /* LZ4_LITTLE_ENDIAN */ +}
The reason LZ4_NbCommonBytes() in upstream LZ4 is so complicated is that it needs to provide portable fallbacks that work on *any* platform and compiler. This isn't needed in the Linux kernel, and it should just call the functions already defined that do the right thing:
static FORCE_INLINE unsigned int LZ4_NbCommonBytes(register size_t val)
{
if (LZ4_isLittleEndian())
return __ffs(val) >> 3;
else
return (BITS_PER_LONG - 1 - __fls(val)) >> 3;
} To be clear, when I said that upstream LZ4 shouldn't generally be changed, I'm primarily talking about the core code, not the platform-specific parts. What we need to do is define platform-specific stuff, like LZ4_read*(), LZ4_write*(), LZ4_NbCommonBytes(), LZ4_64bits(), and FORCE_INLINE, in a way that makes sense for the Linux kernel and the environment it's compiled in. Also I think it's fine, and maybe even necessary for performance, to *add* inline or FORCE_INLINE in some places too, given that LZ4 in Linux may get compiled with a lower optimization level than that intended to be used for upstream LZ4 --- though it may be worth considering updating the Makefile to just always compile the LZ4 files with -O3 instead. What should be avoided is making unnecessary changes to the *users* of the platform-specific code or to the core (de)compression parameters or templates.
Eric
Re: [PATCH] lz4: fix performance regressions
Hey Eric,
Quoted 250 lines · Eric Biggers
Hi Sven,
Quoted 65 lines · Sven Schmidt
/*-************************************
* Reading and writing into memory
**************************************/lib/lz4/lz4defs.h +17
-25+typedef union { +U16 u16; +U32 u32; +size_t uArch; +} __packed unalign; -static inline U16 LZ4_read16 (const void *memPtr) +static FORCE_INLINE __maybe_unused U16 LZ4_read16(const void *ptr) { -U16 val; - -memcpy (&val, memPtr,sizeof (val)); - -return val; +return ((const unalign *)ptr)->u16; } -static inline U32 LZ4_read32 (const void *memPtr) +static FORCE_INLINE __maybe_unused U32 LZ4_read32(const void *ptr) { -U32 val; - -memcpy (&val, memPtr,sizeof (val)); - -return val; +return ((const unalign *)ptr)->u32; } -static inline size_t LZ4_read_ARCH (const void *memPtr) +static FORCE_INLINE __maybe_unused size_t LZ4_read_ARCH(const void *ptr) { -size_t val; - -memcpy (&val, memPtr,sizeof (val)); - -return val; +return ((const unalign *)ptr)->uArch; } -static inline void LZ4_write16 (void *memPtr,U16 value) +static FORCE_INLINE __maybe_unused void LZ4_write16(void *memPtr, U16 value) { -memcpy (memPtr, &value,sizeof (value)); + ((unalign *)memPtr)->u16 = value; } -static inline void LZ4_write32 (void *memPtr,U32 value) -{ -memcpy (memPtr, &value,sizeof (value)); +static FORCE_INLINE __maybe_unused void LZ4_write32(void *memPtr, U32 value) { + ((unalign *)memPtr)->u32 = value; } -static inline U16 LZ4_readLE16 (const void *memPtr) +static FORCE_INLINE __maybe_unused U16 LZ4_readLE16(const void *memPtr) { -#ifdef __LITTLE_ENDIAN__ +#if LZ4_LITTLE_ENDIAN return LZ4_read16 (memPtr);#else const BYTE *p = (const BYTE *)memPtr;lib/lz4/lz4defs.h static inline U16 LZ4_readLE16(const void *memPtr) · line 144
Quoted 2 lines
lib/lz4/lz4defs.h +0
-0@@ -137,19 +143,19 @@ static inline U16 LZ4_readLE16(const void *memPtr)#endif }Since upstream LZ4 is intended to be compiled at -O3, this may allow it to get away with using
memcpy()for unaligned memory accesses. The reason it usesmemcpy()is that, other than a byte-by-byte copy, it is the only portable way to express unaligned memory accesses. But the Linux kernel is sometimes compiled optimized for size (-Os), and I wouldn't be *too* surprised if some of the memcpy()'s don't always get inlined then, which could be causing the performance regression being observed. (Of course, this could be verified by checking whetherCONFIG_CC_OPTIMIZE_FOR_SIZE=yis set, then reading the assembly.)But I don't think accessing a
__packedstructure directly is the right alternative. Instead, Linux already includes macros for unaligned memory accesses which have been optimized for every supported architecture. Those should just be used instead, e.g. like this:static
FORCE_INLINEU16LZ4_read16(const void *ptr){ return get_unaligned((const u16 *)ptr); }static
FORCE_INLINEU32LZ4_read32(const void *ptr){ return get_unaligned((const u32 *)ptr); }static
FORCE_INLINEsize_tLZ4_read_ARCH(const void *ptr){ return get_unaligned((const size_t *)ptr); }static
FORCE_INLINEvoidLZ4_write16(void *memPtr, U16 value){ put_unaligned(value, (u16 *)memPtr); }static
FORCE_INLINEvoidLZ4_write32(void *memPtr, U32 value){ put_unaligned(value, (u32 *)memPtr); }static
FORCE_INLINEU16LZ4_readLE16(const void *memPtr){ return get_unaligned_le16(memPtr); }static
FORCE_INLINEvoidLZ4_writeLE16(void *memPtr, U16 value){ return put_unaligned_le16(value, memPtr); }static
FORCE_INLINEvoidLZ4_copy8(void *dst, const void *src){ if (LZ4_64bits()) { u64 a = get_unaligned((const u64 *)src); put_unaligned(a, (u64 *)dst); } else { u32 a = get_unaligned((const u32 *)src); u32 b = get_unaligned((const u32 *)src + 1); put_unaligned(a, (u32 *)dst); put_unaligned(b, (u32 *)dst + 1); } }Note that I dropped
__maybe_unusedas it's not needed on inline functions. That should be done everywhere else the patch proposes to add it too.Quoted 76 lines
lib/lz4/lz4defs.h +63
-10-#if LZ4_ARCH64 -#ifdef __BIG_ENDIAN__ -#define LZ4_NBCOMMONBYTES (val) (__builtin_clzll(val) >> 3) +static FORCE_INLINE unsigned int LZ4_NbCommonBytes(register size_t val) +{ +#if LZ4_LITTLE_ENDIAN +#if LZ4_ARCH64 /* 64 Bits Little Endian */ +#if defined(LZ4_FORCE_SW_BITCOUNT ) +static const int DeBruijnBytePos[64 ] = { +0 ,0 ,0 ,0 ,0 ,1 ,1 ,2 ,0 ,3 ,1 ,3 ,1 ,4 ,2 ,7 , +0 ,2 ,3 ,6 ,1 ,5 ,3 ,5 ,1 ,3 ,4 ,4 ,2 ,5 ,6 ,7 , +7 ,0 ,1 ,2 ,3 ,3 ,4 ,6 ,2 ,6 ,5 ,5 ,3 ,4 ,5 ,6 , +7 ,1 ,2 ,4 ,6 ,4 ,4 ,5 ,7 ,2 ,6 ,5 ,7 ,6 ,7 ,7 + }; + +return DeBruijnBytePos[((U64 )((val & -(long long )val) + *0x0218A392CDABBD3FULL )) >>58 ];#else -#define LZ4_NBCOMMONBYTES (val) (__builtin_ctzll(val) >> 3) -#endif +return (__builtin_ctzll ((U64 )val) >>3 ); +#endif /* defined(LZ4_FORCE_SW_BITCOUNT) */ +#else /* 32 Bits Little Endian */ +#if defined(LZ4_FORCE_SW_BITCOUNT ) +static const int DeBruijnBytePos[32 ] = { +0 ,0 ,3 ,0 ,3 ,1 ,3 ,0 ,3 ,2 ,2 ,1 ,3 ,2 ,0 ,1 , +3 ,3 ,1 ,2 ,2 ,2 ,2 ,0 ,3 ,1 ,2 ,0 ,1 ,0 ,1 ,1 + }; + +return DeBruijnBytePos[((U32 )((val & -(S32 )val) + *0x077CB531U )) >>27 ];#else -#ifdef __BIG_ENDIAN__ -#define LZ4_NBCOMMONBYTES (val) (__builtin_clz(val) >> 3) +return (__builtin_ctz ((U32 )val) >>3 ); +#endif /* defined(LZ4_FORCE_SW_BITCOUNT) */ +#endif /* LZ4_ARCH64 */ +#else /* Big Endian */ +#if LZ4_ARCH64 /* 64 Bits Big Endian */ +#if defined(LZ4_FORCE_SW_BITCOUNT ) +unsigned int r; + +if (!(val >>32 )) { + r =4 ; + }else { + r =0 ; + val >>=32 ; + } + +if (!(val >>16 )) { + r +=2 ; + val >>=8 ; + }else { + val >>=24 ; + } + + r += (!val); + +return r;#else -#define LZ4_NBCOMMONBYTES (val) (__builtin_ctz(val) >> 3) -#endif -#endif +return (__builtin_clzll ((U64 )val) >>3 ); +#endif /* defined(LZ4_FORCE_SW_BITCOUNT) */ +#else /* 32 Bits Big Endian */ +#if defined(LZ4_FORCE_SW_BITCOUNT ) +unsigned int r; + +if (!(val >>16 )) { + r =2 ; + val >>=8 ; + }else { + r =0 ; + val >>=24 ; + }lib/lz4/lz4defs.h static inline void LZ4_wildCopy(void *dstPtr, const void *srcPtr, void *dstEnd) · line 256
Quoted 10 lines
lib/lz4/lz4defs.h +10
-0+ + r += (!val); + +return r; +#else +return (__builtin_clz ((U32 )val) >>3 ); +#endif /* defined(LZ4_FORCE_SW_BITCOUNT) */ +#endif /* LZ4_ARCH64 */ +#endif /* LZ4_LITTLE_ENDIAN */ +}The reason
LZ4_NbCommonBytes()in upstream LZ4 is so complicated is that it needs to provide portable fallbacks that work on *any* platform and compiler. This isn't needed in the Linux kernel, and it should just call the functions already defined that do the right thing:static
FORCE_INLINEunsigned intLZ4_NbCommonBytes(register size_t val){ if (LZ4_isLittleEndian()) return __ffs(val) >> 3; else return (BITS_PER_LONG - 1 - __fls(val)) >> 3; }To be clear, when I said that upstream LZ4 shouldn't generally be changed, I'm primarily talking about the core code, not the platform-specific parts. What we need to do is define platform-specific stuff, like LZ4_read*(), LZ4_write*(),
LZ4_NbCommonBytes(),LZ4_64bits(), andFORCE_INLINE, in a way that makes sense for the Linux kernel and the environment it's compiled in. Also I think it's fine, and maybe even necessary for performance, to *add* inline orFORCE_INLINEin some places too, given that LZ4 in Linux may get compiled with a lower optimization level than that intended to be used for upstream LZ4 --- though it may be worth considering updating theMakefileto just always compile the LZ4 files with -O3 instead. What should be avoided is making unnecessary changes to the *users* of the platform-specific code or to the core (de)compression parameters or templates.
Quoted 1 line
Eric
I'm very thankful for your effort here! I included all your proposed changes (as well as -O3 as compiler flag in the Makefile) and noticed further improvements concerning the performance in ZRAM (compared to current LZ4 in 4.10 RC8 as well as the previous changes discussed here). I would never have thought that the NBcommonBytes function can be re-written in such an easy way, since the current LZ4 in the kernel uses similar approach checking for 64/32 bit and endianess.
Also, thanks for your very detailed remarks. That's really helpful to me.
Regards,
Sven
Hi Sven,
Quoted 135 lines · Sven Schmidt
Quoted 114 lines · Minchan Kim
Hello Sven,
Quoted 63 lines · Sven Schmidt
Hey Minchan,
Quoted 49 lines · Minchan Kim
Hello Sven,
Quoted 30 lines · Sven Schmidt
This patchset is for updating the LZ4 compression module to a version based on LZ4 v1.7.3 allowing to use the fast compression algorithm aka LZ4 fast which provides an "acceleration" parameter as a tradeoff between high compression ratio and high compression speed.
We want to use LZ4 fast in order to support compression in lustre and (mostly, based on that) investigate data reduction techniques in behalf of storage systems.
Also, it will be useful for other users of LZ4 compression, as with LZ4 fast it is possible to enable applications to use fast and/or high compression depending on the usecase.
For instance, ZRAM is offering a LZ4 backend and could benefit from an updated LZ4 in the kernel.LZ4 homepage: http://www.lz4.org/
LZ4 source repository: https://github.com/lz4/lz4
Source version: 1.7.3Benchmark (taken from [1], Core i5-4300U @1.9GHz):
----------------|--------------|----------------|---------- Compressor | Compression | Decompression | Ratio ----------------|--------------|----------------|---------- memcpy | 4200 MB/s | 4200 MB/s | 1.000 LZ4 fast 50 | 1080 MB/s | 2650 MB/s | 1.375 LZ4 fast 17 | 680 MB/s | 2220 MB/s | 1.607 LZ4 fast 5 | 475 MB/s | 1920 MB/s | 1.886 LZ4 default | 385 MB/s | 1850 MB/s | 2.101Quoted 7 lines
[1] http://fastcompression.blogspot.de/2015/04/sampling-or-faster-lz4.html
[PATCH 1/5] lib: Update LZ4 compressor module
[PATCH 2/5]lib/decompress_unlz4: Change module to work with new LZ4 module version [PATCH 3/5] crypto: Change LZ4 modules to work with new LZ4 module version [PATCH 4/5] fs/pstore: fs/squashfs: Change usage of LZ4 to work with new LZ4 version [PATCH 5/5] lib/lz4: Remove back-compat wrappersToday, I did zram-lz4 performance test with fio in current mmotm and found it makes regression about 20%.
"lz4-update" means current mmots(git://git.cmpxchg.org/linux-mmots.git) so applied your 5 patches. (But now sure current mmots has recent uptodate patches)
"revert" means I reverted your 5 patches in current mmots.Quoted 10 lines
revert lz4-update seq-write 1547 1339 86.55% rand-write 22775 19381 85.10% seq-read 7035 5589 79.45% rand-read 78556 68479 87.17% mixed-seq(R) 1305 1066 81.69% mixed-seq(W) 1205 984 81.66% mixed-rand(R) 17421 14993 86.06% mixed-rand(W) 17391 14968 86.07%Quoted 2 lines
which parts of the output (as well as units) are these values exactly?
I did not work with fio until now, so I think I might ask before misinterpreting my results.It is IOPS.
Quoted 43 lines
Quoted 42 lines
My fio description file
[global]
bs=4k
ioengine=sync
size=100m
numjobs=1group_reporting
buffer_compress_percentage=30
scramble_buffers=0
filename=/dev/zram0
loops=10
fsync_on_close=1[seq-write]
bs=64k
rw=write
stonewall[rand-write]
rw=randwrite
stonewall[seq-read]
bs=64k
rw=read
stonewall[rand-read]
rw=randread
stonewall[mixed-seq]
bs=64k
rw=rw
stonewall[mixed-rand]
rw=randrw
stonewallQuoted 6 lines
Quoted 1 line
Great, this makes it easy for me to reproduce your test.
If you have trouble to reproduce, feel free to ask me. I'm happy to test it. :)
Thanks!
Hi Minchan,
I will send an updated patch as a reply to this E-Mail. Would be really grateful If you'd test it and provide feedback! The patch should be applied to the current mmots tree.
In fact, the updated LZ4 _is_ slower than the current one in kernel. But I was not able to reproduce such large regressions as you did. I now tried to define
FORCE_INLINEas Eric suggested. I also inlined some functions which weren't in upstream LZ4, but are defined as macros in the current kernel LZ4. The approach to replaceLZ4_ARCH64with the function call _seemed_ to behave worse than the macro, so I withdrew the change.
Quoted 6 lines
The main difference is, that I replaced the read32/read16/write... etc. functions using memcpy with the other ones defined in upstream LZ4 (which can be switched using a macro).
The comment of the author stated, that they're as fast as the memcpy variants (or faster), but not as portable (which does not matter since we're not dependent for multiple compilers).In my tests, this version is mostly as fast as the current kernel LZ4.
With a patch you sent, I cannot see enhancement so I wanted to dig in and found how I was really careless.
I have tested both test with CONFIG_KASAN. OMG. With disabling it, I don't see any regression any more. So, I'm really really *sorry* about noise and wasting your time. However, I am curious why KASAN makes such difference.
The reason I tested new updated lz4 is description says lz4 fast and want to use it in zram. How can I do that? and How faster it is compared to old?
Thanks for you work!
Quoted 143 lines · Minchan Kim
Hi Sven,
Quoted 140 lines · Sven Schmidt
Quoted 114 lines · Minchan Kim
Hello Sven,
Quoted 63 lines · Sven Schmidt
Hey Minchan,
Quoted 49 lines · Minchan Kim
Hello Sven,
Quoted 30 lines · Sven Schmidt
This patchset is for updating the LZ4 compression module to a version based on LZ4 v1.7.3 allowing to use the fast compression algorithm aka LZ4 fast which provides an "acceleration" parameter as a tradeoff between high compression ratio and high compression speed.
We want to use LZ4 fast in order to support compression in lustre and (mostly, based on that) investigate data reduction techniques in behalf of storage systems.
Also, it will be useful for other users of LZ4 compression, as with LZ4 fast it is possible to enable applications to use fast and/or high compression depending on the usecase.
For instance, ZRAM is offering a LZ4 backend and could benefit from an updated LZ4 in the kernel.LZ4 homepage: http://www.lz4.org/
LZ4 source repository: https://github.com/lz4/lz4
Source version: 1.7.3Benchmark (taken from [1], Core i5-4300U @1.9GHz):
----------------|--------------|----------------|---------- Compressor | Compression | Decompression | Ratio ----------------|--------------|----------------|---------- memcpy | 4200 MB/s | 4200 MB/s | 1.000 LZ4 fast 50 | 1080 MB/s | 2650 MB/s | 1.375 LZ4 fast 17 | 680 MB/s | 2220 MB/s | 1.607 LZ4 fast 5 | 475 MB/s | 1920 MB/s | 1.886 LZ4 default | 385 MB/s | 1850 MB/s | 2.101Quoted 7 lines
[1] http://fastcompression.blogspot.de/2015/04/sampling-or-faster-lz4.html
[PATCH 1/5] lib: Update LZ4 compressor module
[PATCH 2/5]lib/decompress_unlz4: Change module to work with new LZ4 module version [PATCH 3/5] crypto: Change LZ4 modules to work with new LZ4 module version [PATCH 4/5] fs/pstore: fs/squashfs: Change usage of LZ4 to work with new LZ4 version [PATCH 5/5] lib/lz4: Remove back-compat wrappersToday, I did zram-lz4 performance test with fio in current mmotm and found it makes regression about 20%.
"lz4-update" means current mmots(git://git.cmpxchg.org/linux-mmots.git) so applied your 5 patches. (But now sure current mmots has recent uptodate patches)
"revert" means I reverted your 5 patches in current mmots.Quoted 10 lines
revert lz4-update seq-write 1547 1339 86.55% rand-write 22775 19381 85.10% seq-read 7035 5589 79.45% rand-read 78556 68479 87.17% mixed-seq(R) 1305 1066 81.69% mixed-seq(W) 1205 984 81.66% mixed-rand(R) 17421 14993 86.06% mixed-rand(W) 17391 14968 86.07%Quoted 2 lines
which parts of the output (as well as units) are these values exactly?
I did not work with fio until now, so I think I might ask before misinterpreting my results.It is IOPS.
Quoted 43 lines
Quoted 42 lines
My fio description file
[global]
bs=4k
ioengine=sync
size=100m
numjobs=1group_reporting
buffer_compress_percentage=30
scramble_buffers=0
filename=/dev/zram0
loops=10
fsync_on_close=1[seq-write]
bs=64k
rw=write
stonewall[rand-write]
rw=randwrite
stonewall[seq-read]
bs=64k
rw=read
stonewall[rand-read]
rw=randread
stonewall[mixed-seq]
bs=64k
rw=rw
stonewall[mixed-rand]
rw=randrw
stonewallQuoted 6 lines
Quoted 1 line
Great, this makes it easy for me to reproduce your test.
If you have trouble to reproduce, feel free to ask me. I'm happy to test it. :)
Thanks!
Hi Minchan,
I will send an updated patch as a reply to this E-Mail. Would be really grateful If you'd test it and provide feedback! The patch should be applied to the current mmots tree.
In fact, the updated LZ4 _is_ slower than the current one in kernel. But I was not able to reproduce such large regressions as you did. I now tried to define
FORCE_INLINEas Eric suggested. I also inlined some functions which weren't in upstream LZ4, but are defined as macros in the current kernel LZ4. The approach to replaceLZ4_ARCH64with the function call _seemed_ to behave worse than the macro, so I withdrew the change.The main difference is, that I replaced the read32/read16/write... etc. functions using memcpy with the other ones defined in upstream LZ4 (which can be switched using a macro).
The comment of the author stated, that they're as fast as the memcpy variants (or faster), but not as portable (which does not matter since we're not dependent for multiple compilers).
Quoted 9 lines
Quoted 1 line
In my tests, this version is mostly as fast as the current kernel LZ4.
With a patch you sent, I cannot see enhancement so I wanted to dig in and found how I was really careless.
I have tested both test with
CONFIG_KASAN. OMG. With disabling it, I don't see any regression any more. So, I'm really really *sorry* about noise and wasting your time. However, I am curious why KASAN makes such difference.
Hey Minchan,
I'm glad to hear that! Nevertheless, the changes discussed here made some differences in my own tests (I believe it got a bit faster now) and we have the functions properly inlined, where this makes sense. Also, I added the '-O3' C-flag as Eric suggested. So, this was not really a waste of time, I think.
Quoted 4 lines
The reason I tested new updated lz4 is description says lz4 fast and want to use it in zram. How can I do that? and How faster it is compared to old?
Unfortunately, in the current implementation (in crypto/lz4.c, which is used by zram) I'm setting the acceleration parameter (which is the paramer making the compression 'fast', see LZ4_compress_fast) to 1 (which is the default) since I did not know how this patchset is accepted and this equals the behaviour currently available in kernel.
Basically, the logic is 'higher acceleration = faster compression = lower compression ratio' and vice versa. I included some benchmarks in my patch 0/5 E-Mail taken from the official LZ4:
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
----------------|--------------|----------------|---------- Compressor | Compression | Decompression | Ratio ----------------|--------------|----------------|---------- memcpy | 4200 MB/s | 4200 MB/s | 1.000 LZ4 fast 50 | 1080 MB/s | 2650 MB/s | 1.375 LZ4 fast 17 | 680 MB/s | 2220 MB/s | 1.607 LZ4 fast 5 | 475 MB/s | 1920 MB/s | 1.886 LZ4 default | 385 MB/s | 1850 MB/s | 2.101
fast 50 means: acceleration=50, default: acceleration=1.
Besides the proposed patchset, I tried to implement a module parameter in crypto/lz4.c to set the acceleration factor. In my tests, the module parameter works out great.
But I think this is subject to a future, separate patch. Especially since I had to 'work around' the crypto/testmgr.c, which only tests acceleration=1 and there's no limit for acceleration.
Thanks for your help,
Sven
Hi Sven,
Quoted 160 lines · Sven Schmidt
Quoted 143 lines · Minchan Kim
Hi Sven,
Quoted 140 lines · Sven Schmidt
Quoted 114 lines · Minchan Kim
Hello Sven,
Quoted 63 lines · Sven Schmidt
Hey Minchan,
Quoted 49 lines · Minchan Kim
Hello Sven,
Quoted 30 lines · Sven Schmidt
This patchset is for updating the LZ4 compression module to a version based on LZ4 v1.7.3 allowing to use the fast compression algorithm aka LZ4 fast which provides an "acceleration" parameter as a tradeoff between high compression ratio and high compression speed.
We want to use LZ4 fast in order to support compression in lustre and (mostly, based on that) investigate data reduction techniques in behalf of storage systems.
Also, it will be useful for other users of LZ4 compression, as with LZ4 fast it is possible to enable applications to use fast and/or high compression depending on the usecase.
For instance, ZRAM is offering a LZ4 backend and could benefit from an updated LZ4 in the kernel.LZ4 homepage: http://www.lz4.org/
LZ4 source repository: https://github.com/lz4/lz4
Source version: 1.7.3Benchmark (taken from [1], Core i5-4300U @1.9GHz):
----------------|--------------|----------------|---------- Compressor | Compression | Decompression | Ratio ----------------|--------------|----------------|---------- memcpy | 4200 MB/s | 4200 MB/s | 1.000 LZ4 fast 50 | 1080 MB/s | 2650 MB/s | 1.375 LZ4 fast 17 | 680 MB/s | 2220 MB/s | 1.607 LZ4 fast 5 | 475 MB/s | 1920 MB/s | 1.886 LZ4 default | 385 MB/s | 1850 MB/s | 2.101Quoted 7 lines
[1] http://fastcompression.blogspot.de/2015/04/sampling-or-faster-lz4.html
[PATCH 1/5] lib: Update LZ4 compressor module
[PATCH 2/5]lib/decompress_unlz4: Change module to work with new LZ4 module version [PATCH 3/5] crypto: Change LZ4 modules to work with new LZ4 module version [PATCH 4/5] fs/pstore: fs/squashfs: Change usage of LZ4 to work with new LZ4 version [PATCH 5/5] lib/lz4: Remove back-compat wrappersToday, I did zram-lz4 performance test with fio in current mmotm and found it makes regression about 20%.
"lz4-update" means current mmots(git://git.cmpxchg.org/linux-mmots.git) so applied your 5 patches. (But now sure current mmots has recent uptodate patches)
"revert" means I reverted your 5 patches in current mmots.Quoted 10 lines
revert lz4-update seq-write 1547 1339 86.55% rand-write 22775 19381 85.10% seq-read 7035 5589 79.45% rand-read 78556 68479 87.17% mixed-seq(R) 1305 1066 81.69% mixed-seq(W) 1205 984 81.66% mixed-rand(R) 17421 14993 86.06% mixed-rand(W) 17391 14968 86.07%Quoted 2 lines
which parts of the output (as well as units) are these values exactly?
I did not work with fio until now, so I think I might ask before misinterpreting my results.It is IOPS.
Quoted 43 lines
Quoted 42 lines
My fio description file
[global]
bs=4k
ioengine=sync
size=100m
numjobs=1group_reporting
buffer_compress_percentage=30
scramble_buffers=0
filename=/dev/zram0
loops=10
fsync_on_close=1[seq-write]
bs=64k
rw=write
stonewall[rand-write]
rw=randwrite
stonewall[seq-read]
bs=64k
rw=read
stonewall[rand-read]
rw=randread
stonewall[mixed-seq]
bs=64k
rw=rw
stonewall[mixed-rand]
rw=randrw
stonewallQuoted 6 lines
Quoted 1 line
Great, this makes it easy for me to reproduce your test.
If you have trouble to reproduce, feel free to ask me. I'm happy to test it. :)
Thanks!
Hi Minchan,
I will send an updated patch as a reply to this E-Mail. Would be really grateful If you'd test it and provide feedback! The patch should be applied to the current mmots tree.
In fact, the updated LZ4 _is_ slower than the current one in kernel. But I was not able to reproduce such large regressions as you did. I now tried to define
FORCE_INLINEas Eric suggested. I also inlined some functions which weren't in upstream LZ4, but are defined as macros in the current kernel LZ4. The approach to replaceLZ4_ARCH64with the function call _seemed_ to behave worse than the macro, so I withdrew the change.The main difference is, that I replaced the read32/read16/write... etc. functions using memcpy with the other ones defined in upstream LZ4 (which can be switched using a macro).
The comment of the author stated, that they're as fast as the memcpy variants (or faster), but not as portable (which does not matter since we're not dependent for multiple compilers).Quoted 9 lines
Quoted 1 line
In my tests, this version is mostly as fast as the current kernel LZ4.
With a patch you sent, I cannot see enhancement so I wanted to dig in and found how I was really careless.
I have tested both test with
CONFIG_KASAN. OMG. With disabling it, I don't see any regression any more. So, I'm really really *sorry* about noise and wasting your time. However, I am curious why KASAN makes such difference.Hey Minchan,
I'm glad to hear that! Nevertheless, the changes discussed here made some differences in my own tests (I believe it got a bit faster now) and we have the functions properly inlined, where this makes sense. Also, I added the '-O3' C-flag as Eric suggested. So, this was not really a waste of time, I think.
Quoted 8 lines
Quoted 4 lines
The reason I tested new updated lz4 is description says lz4 fast and want to use it in zram. How can I do that? and How faster it is compared to old?
Unfortunately, in the current implementation (in
crypto/lz4.c, which is used by zram) I'm setting the acceleration parameter (which is the paramer making the compression 'fast', seeLZ4_compress_fast) to 1 (which is the default) since I did not know how this patchset is accepted and this equals the behaviour currently available in kernel.
Fair enough.
Quoted 14 lines
Basically, the logic is 'higher acceleration = faster compression = lower compression ratio' and vice versa. I included some benchmarks in my patch 0/5 E-Mail taken from the official LZ4:
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
----------------|--------------|----------------|---------- Compressor | Compression | Decompression | Ratio ----------------|--------------|----------------|---------- memcpy | 4200 MB/s | 4200 MB/s | 1.000 LZ4 fast 50 | 1080 MB/s | 2650 MB/s | 1.375 LZ4 fast 17 | 680 MB/s | 2220 MB/s | 1.607 LZ4 fast 5 | 475 MB/s | 1920 MB/s | 1.886 LZ4 default | 385 MB/s | 1850 MB/s | 2.101
Quoted 1 line
fast 50 means: acceleration=50, default: acceleration=1.
I understood now. Thanks for the explanation!
Quoted 3 lines
Besides the proposed patchset, I tried to implement a module parameter in
crypto/lz4.cto set the acceleration factor. In my tests, the module parameter works out great.
If it works with module parameter, it means every guest of lz4 via crypto should use same acceleration level? Hmm, some system might want different acceleration level among different subsystems.
Anyway, if it works, it would be great for user of zram to test/select right choice for their system workload.
Thanks for the great work!
Quoted 6 lines
But I think this is subject to a future, separate patch. Especially since I had to 'work around' the
crypto/testmgr.c, which only tests acceleration=1 and there's no limit for acceleration.Thanks for your help,
Sven
v8 cover Update LZ4 compressor module
In reply to Sven SchmidtThis patchset is for updating the LZ4 compression module to a version based on LZ4 v1.7.3 allowing to use the fast compression algorithm aka LZ4 fast which provides an "acceleration" parameter as a tradeoff between high compression ratio and high compression speed.
We want to use LZ4 fast in order to support compression in lustre and (mostly, based on that) investigate data reduction techniques in behalf of storage systems.
Also, it will be useful for other users of LZ4 compression, as with LZ4 fast it is possible to enable applications to use fast and/or high compression depending on the usecase.
For instance, ZRAM is offering a LZ4 backend and could benefit from an updated LZ4 in the kernel.
LZ4 homepage: http://www.lz4.org/
LZ4 source repository: https://github.com/lz4/lz4
Source version: 1.7.3
Benchmark (taken from [1], Core i5-4300U @1.9GHz):
----------------|--------------|----------------|---------- Compressor | Compression | Decompression | Ratio ----------------|--------------|----------------|---------- memcpy | 4200 MB/s | 4200 MB/s | 1.000 LZ4 fast 50 | 1080 MB/s | 2650 MB/s | 1.375 LZ4 fast 17 | 680 MB/s | 2220 MB/s | 1.607 LZ4 fast 5 | 475 MB/s | 1920 MB/s | 1.886 LZ4 default | 385 MB/s | 1850 MB/s | 2.101
[1] http://fastcompression.blogspot.de/2015/04/sampling-or-faster-lz4.html
[PATCH 1/5] lib: Update LZ4 compressor module
[PATCH 2/5] lib/decompress_unlz4: Change module to work with new LZ4 module version [PATCH 3/5] crypto: Change LZ4 modules to work with new LZ4 module version [PATCH 4/5] fs/pstore: fs/squashfs: Change usage of LZ4 to work with new LZ4 version [PATCH 5/5] lib/lz4: Remove back-compat wrappers
Changes:
v8:
- Rewrote the architecture-dependent definitions in lz4defs.h, such as LZ4_read*, LZ4_write* and LZ4_NbCommonBytes as proposed by Eric Biggers
- Added -O3 compiler flag to Makefile, also suggested by Eric
- Defined FORCE_INLINE, as used in upstream LZ4, as __always_inline and force-inlined most of the small functions in lz4defs.h
- lz4_decompress: Wrapped the EXPORT_SYMBOL and MODULE_* macros in a #ifdef STATIC the way suggested by Andrew Morton, fixing the breakage of CONFIG_KERNEL_LZ4 in x86 as reported by Arnd Bergman
v7:
- Fixed errors reported by the Smatch tool
- Changed function documentation comments in lz4.h to match kernel-doc style
- Fixed a misbehaviour of LZ4HC caused by the wrong level of indentation concerning two for loops introduced after I refactored the code style using checkpatch.pl (upstream LZ4 put dozens of stuff in just one line, gnah)
- Updated the crypto tests for LZ4 since they did fail for the new code and hence zram did fail to allocate memory for LZ4
v6:
- Fixed LZ4_NBCOMMONBYTES() for 64-bit little endian
- Reset LZ4_MEMORY_USAGE to 14 (which is the value used in upstream LZ4 as well as the previous kernel module)
- Fixed that weird double-indentation in lz4defs.h and lz4.h
- Adjusted general styling issues in lz4defs.h (e.g. lines consisting of more than one instruction)
- Removed the architecture-dependent typedef to reg_t since upstream LZ4 is just using size_t and that works fine
- Changed error messages in pstore/platform.c:
* LZ4_compress_default always returns 0 in case of an error (no need to print the return value)
* LZ4_decompress_safe returns a negative error message (return value _does_ matter)
v5:
- Added a fifth patch to remove the back-compat wrappers introduced to ensure bisectibility between the patches (the functions are no longer needed since there's no callers left)
v4:
- Fixed kbuild errors
- Re-added lz4_compressbound as alias for LZ4_compressBound to ensure backwards compatibility
- Wrapped LZ4_hash5 with check for LZ4_ARCH64 since it is only used there and triggers an unused function warning when false
v3:
- Adjusted the code to satisfy kernel coding style (checkpatch.pl)
- Made sure the changes to LZ4 in Kernel (overflow checks etc.) are included in the new module (they are)
- Removed the second LZ4_compressBound function with related name but different return type
- Corrected version number (was LZ4 1.7.3)
- Added missing LZ4 streaming functions
v2:
- Changed order of the patches since in the initial patchset the lz4.h was in the last patch but was referenced by the other ones
- Split lib/decompress_unlz4.c in an own patch
- Fixed errors reported by the buildbot
- Further refactorings
- Added more appropriate copyright note to include/linux/lz4.h
PATCH v8 1/5 lib: Update LZ4 compressor module
Update the LZ4 kernel module to LZ4 v1.7.3 by Yann Collet. The kernel module is inspired by the previous work by Chanho Min. The updated LZ4 module will not break existing code since the patchset contains appropriate changes.
API changes:
New method LZ4_compress_fast which differs from the variant available in kernel by the new acceleration parameter, allowing to trade compression ratio for more compression speed and vice versa.
LZ4_decompress_fast is the respective decompression method, featuring a very fast decoder (multiple GB/s per core), able to reach RAM speed in multi-core systems. The decompressor allows to decompress data compressed with LZ4 fast as well as the LZ4 HC (high compression) algorithm.
Also the useful functions LZ4_decompress_safe_partial and LZ4_compress_destsize were added. The latter reverses the logic by trying to compress as much data as possible from source to dest while the former aims to decompress partial blocks of data.
A bunch of streaming functions were also added which allow compressig/decompressing data in multiple steps (so called "streaming mode").
The methods lz4_compress and lz4_decompress_unknownoutputsize are now known as LZ4_compress_default respectivley LZ4_decompress_safe. The old methods will be removed since there's no callers left in the code.
--- include/linux/lz4.h | 762 +++++++++++++++++++++++++++--- lib/lz4/Makefile | 2 + lib/lz4/lz4_compress.c | 1161 +++++++++++++++++++++++++++++++++------------- lib/lz4/lz4_decompress.c | 705 ++++++++++++++++++---------- lib/lz4/lz4defs.h | 338 ++++++++------ lib/lz4/lz4hc_compress.c | 867 ++++++++++++++++++++++------------ 6 files changed, 2758 insertions(+), 1077 deletions(-)
include/linux/lz4.h +696 -66
@@ -1,87 +1,717 @@ -#ifndef __LZ4_H__ -#define __LZ4_H__ -/* -* LZ4 Kernel Interface +/* LZ4 Kernel Interface * * Copyright (C) 2013, LG Electronics, Kyungsik Lee <[email protected]> +* Copyright (C) 2016, Sven Schmidt <[email protected]> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. +* +* This file is based on the original header file +* for LZ4 - Fast LZ compression algorithm. +* +* LZ4 - Fast LZ compression algorithm +* Copyright (C) 2011-2016, Yann Collet. +* BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are +* met: +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above +* copyright notice, this list of conditions and the following disclaimer +* in the documentation and/or other materials provided with the +* distribution. +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* You can contact the author at : +* - LZ4 homepage : http://www.lz4.org +* - LZ4 source repository : https://github.com/lz4/lz4 */ -#define LZ4_MEM_COMPRESS (16384) -#define LZ4HC_MEM_COMPRESS (262144 + (2 * sizeof(unsigned char *))) +#ifndef __LZ4_H__ +#define __LZ4_H__ + +#include <linux/types.h> +#include <linux/string.h> /* memset, memcpy */ + +/*-************************************************************************ +* CONSTANTS +**************************************************************************/ /* -* lz4_compressbound() -* Provides the maximum size that LZ4 may output in a "worst case" scenario -* (input data not compressible) +* LZ4_MEMORY_USAGE : +* Memory usage formula : N->2^N Bytes +* (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.) +* Increasing memory usage improves compression ratio +* Reduced memory usage can improve speed, due to cache effect +* Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache */ -static inline size_t lz4_compressbound (size_t isize) +#define LZ4_MEMORY_USAGE 14 + +#define LZ4_MAX_INPUT_SIZE 0x7E000000/* 2 113 929 216 bytes */ +#define LZ4_COMPRESSBOUND (isize) (\ + (unsigned int)(isize) > (unsigned int)LZ4_MAX_INPUT_SIZE \ + ? 0 \ + : (isize) + ((isize)/255) + 16) + +#define LZ4_ACCELERATION_DEFAULT 1 +#define LZ4_HASHLOG (LZ4_MEMORY_USAGE-2) +#define LZ4_HASHTABLESIZE (1 << LZ4_MEMORY_USAGE) +#define LZ4_HASH_SIZE_U32 (1 << LZ4_HASHLOG) + +#define LZ4HC_MIN_CLEVEL 3 +#define LZ4HC_DEFAULT_CLEVEL 9 +#define LZ4HC_MAX_CLEVEL 16 + +#define LZ4HC_DICTIONARY_LOGSIZE 16 +#define LZ4HC_MAXD (1<<LZ4HC_DICTIONARY_LOGSIZE) +#define LZ4HC_MAXD_MASK (LZ4HC_MAXD - 1) +#define LZ4HC_HASH_LOG (LZ4HC_DICTIONARY_LOGSIZE - 1) +#define LZ4HC_HASHTABLESIZE (1 << LZ4HC_HASH_LOG) +#define LZ4HC_HASH_MASK (LZ4HC_HASHTABLESIZE - 1) + +/*-************************************************************************ +* STREAMING CONSTANTS AND STRUCTURES +**************************************************************************/ +#define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE - 3)) + 4) +#define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U64 * sizeof(unsigned long long)) + +#define LZ4_STREAMHCSIZE 262192 +#define LZ4_STREAMHCSIZE_SIZET (262192 / sizeof(size_t)) + +#define LZ4_STREAMDECODESIZE_U64 4 +#define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * \ + sizeof(unsigned long long)) + +/* +* LZ4_stream_t - information structure to track an LZ4 stream. +*/ +typedef struct { +uint32_t hashTable[LZ4_HASH_SIZE_U32 ]; +uint32_t currentOffset; +uint32_t initCheck; +const uint8_t *dictionary; +uint8_t *bufferStart; +uint32_t dictSize; +}LZ4_stream_t_internal ; +typedef union { +unsigned long long table[LZ4_STREAMSIZE_U64 ]; +LZ4_stream_t_internal internal_donotuse; +}LZ4_stream_t ; + +/* +* LZ4_streamHC_t - information structure to track an LZ4HC stream. +*/ +typedef struct { +unsigned int hashTable[LZ4HC_HASHTABLESIZE ]; +unsigned short chainTable[LZ4HC_MAXD ]; +/* next block to continue on current prefix */ +const unsigned char *end; +/* All index relative to this position */ +const unsigned char *base; +/* alternate base for extDict */ +const unsigned char *dictBase; +/* below that point, need extDict */ +unsigned int dictLimit; +/* below that point, no more dict */ +unsigned int lowLimit; +/* index from which to continue dict update */ +unsigned int nextToUpdate; +unsigned int compressionLevel; +}LZ4HC_CCtx_internal ; +typedef union { +size_t table[LZ4_STREAMHCSIZE_SIZET ]; +LZ4HC_CCtx_internal internal_donotuse; +}LZ4_streamHC_t ; + +/* +* LZ4_streamDecode_t - information structure to track an +* LZ4 stream during decompression. +* +* init this structure using LZ4_setStreamDecode (or memset()) before first use +*/ +typedef struct { +const uint8_t *externalDict; +size_t extDictSize; +const uint8_t *prefixEnd; +size_t prefixSize; +}LZ4_streamDecode_t_internal ; +typedef union { +unsigned long long table[LZ4_STREAMDECODESIZE_U64 ]; +LZ4_streamDecode_t_internal internal_donotuse; +}LZ4_streamDecode_t ; + +/*-************************************************************************ +* SIZE OF STATE +**************************************************************************/ +#define LZ4_MEM_COMPRESS LZ4_STREAMSIZE +#define LZ4HC_MEM_COMPRESS LZ4_STREAMHCSIZE + +/*-************************************************************************ +* Compression Functions +**************************************************************************/ + +/** +* LZ4_compressBound() - Max. output size in worst case szenarios +* @isize: Size of the input data +* +* Return: Max. size LZ4 may output in a "worst case" szenario +* (data not compressible) +*/ +static inline int LZ4_compressBound (size_t isize) { -return isize + (isize /255 ) +16 ; +return LZ4_COMPRESSBOUND (isize); } -/* -* lz4_compress() -* src : source address of the original data -* src_len : size of the original data -* dst : output buffer address of the compressed data -* This requires 'dst' of size LZ4_COMPRESSBOUND. -* dst_len : is the output size, which is returned after compress done -* workmem : address of the working memory. -* This requires 'workmem' of size LZ4_MEM_COMPRESS. -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer and workmem must be already allocated with -* the defined size. -*/ -int lz4_compress (const unsigned char *src,size_t src_len, -unsigned char *dst,size_t *dst_len,void *wrkmem); - -/* -* lz4hc_compress() -* src : source address of the original data -* src_len : size of the original data -* dst : output buffer address of the compressed data -* This requires 'dst' of size LZ4_COMPRESSBOUND. -* dst_len : is the output size, which is returned after compress done -* workmem : address of the working memory. -* This requires 'workmem' of size LZ4HC_MEM_COMPRESS. -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer and workmem must be already allocated with -* the defined size. -*/ -int lz4hc_compress (const unsigned char *src,size_t src_len, -unsigned char *dst,size_t *dst_len,void *wrkmem); +/** +* lz4_compressbound() - For backwards compatibility; see LZ4_compressBound +* @isize: Size of the input data +* +* Return: Max. size LZ4 may output in a "worst case" szenario +* (data not compressible) +*/ +static inline int lz4_compressbound (size_t isize) +{ +return LZ4_COMPRESSBOUND (isize); +} + +/** +* LZ4_compress_default() - Compress data from source to dest +* @source: source address of the original data +* @dest: output buffer address of the compressed data +* @inputSize: size of the input data. Max supported value is LZ4_MAX_INPUT_SIZE +* @maxOutputSize: full or partial size of buffer 'dest' +* which must be already allocated +* @wrkmem: address of the working memory. +* This requires 'workmem' of LZ4_MEM_COMPRESS. +* +* Compresses 'sourceSize' bytes from buffer 'source' +* into already allocated 'dest' buffer of size 'maxOutputSize'. +* Compression is guaranteed to succeed if +* 'maxOutputSize' >= LZ4_compressBound(inputSize). +* It also runs faster, so it's a recommended setting. +* If the function cannot compress 'source' into a more limited 'dest' budget, +* compression stops *immediately*, and the function result is zero. +* As a consequence, 'dest' content is not valid. +* +* Return: Number of bytes written into buffer 'dest' +* (necessarily <= maxOutputSize) or 0 if compression fails +*/ +int LZ4_compress_default (const char *source,char *dest,int inputSize, +int maxOutputSize,void *wrkmem); + +/** +* LZ4_compress_fast() - As LZ4_compress_default providing an acceleration param +* @source: source address of the original data +* @dest: output buffer address of the compressed data +* @inputSize: size of the input data. Max supported value is LZ4_MAX_INPUT_SIZE +* @maxOutputSize: full or partial size of buffer 'dest' +* which must be already allocated +* @acceleration: acceleration factor +* @wrkmem: address of the working memory. +* This requires 'workmem' of LZ4_MEM_COMPRESS. +* +* Same as LZ4_compress_default(), but allows to select an "acceleration" +* factor. The larger the acceleration value, the faster the algorithm, +* but also the lesser the compression. It's a trade-off. It can be fine tuned, +* with each successive value providing roughly +~3% to speed. +* An acceleration value of "1" is the same as regular LZ4_compress_default() +* Values <= 0 will be replaced by LZ4_ACCELERATION_DEFAULT, which is 1. +* +* Return: Number of bytes written into buffer 'dest' +* (necessarily <= maxOutputSize) or 0 if compression fails +*/ +int LZ4_compress_fast (const char *source,char *dest,int inputSize, +int maxOutputSize,int acceleration,void *wrkmem); + +/** +* LZ4_compress_destSize() - Compress as much data as possible +* from source to dest +* @source: source address of the original data +* @dest: output buffer address of the compressed data +* @sourceSizePtr: will be modified to indicate how many bytes where read +* from 'source' to fill 'dest'. New value is necessarily <= old value. +* @targetDestSize: Size of buffer 'dest' which must be already allocated +* @wrkmem: address of the working memory. +* This requires 'workmem' of LZ4_MEM_COMPRESS. +* +* Reverse the logic, by compressing as much data as possible +* from 'source' buffer into already allocated buffer 'dest' +* of size 'targetDestSize'. +* This function either compresses the entire 'source' content into 'dest' +* if it's large enough, or fill 'dest' buffer completely with as much data as +* possible from 'source'. +* +* Return: Number of bytes written into 'dest' (necessarily <= targetDestSize) +* or 0 if compression fails +*/ +int LZ4_compress_destSize (const char *source,char *dest,int *sourceSizePtr, +int targetDestSize,void *wrkmem);/* -* lz4_decompress() -* src : source address of the compressed data -* src_len : is the input size, whcih is returned after decompress done -* dest : output buffer address of the decompressed data -* actual_dest_len: is the size of uncompressed data, supposing it's known -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer must be already allocated. -* slightly faster than lz4_decompress_unknownoutputsize() +* lz4_compress() - For backward compatibility, see LZ4_compress_default +* @src: source address of the original data +* @src_len: size of the original data +* @dst: output buffer address of the compressed data. This requires 'dst' +* of size LZ4_COMPRESSBOUND +* @dst_len: is the output size, which is returned after compress done +* @workmem: address of the working memory. +* +* Return: Success if return 0, Error if return < 0 */ -int lz4_decompress (const unsigned char *src,size_t *src_len, -unsigned char *dest,size_t actual_dest_len); +int lz4_compress (const unsigned char *src,size_t src_len,unsigned char *dst, +size_t *dst_len,void *wrkmem); + +/*-************************************************************************ +* Decompression Functions +**************************************************************************/ + +/** +* LZ4_decompress_fast() - Decompresses data from 'source' into 'dest' +* @source: source address of the compressed data +* @dest: output buffer address of the uncompressed data +* which must be already allocated with 'originalSize' bytes +* @originalSize: is the original and therefore uncompressed size +* +* Decompresses data from 'source' into 'dest'. +* This function fully respect memory boundaries for properly formed +* compressed data. +* It is a bit faster than LZ4_decompress_safe(). +* However, it does not provide any protection against intentionally +* modified data stream (malicious input). +* Use this function in trusted environment only +* (data to decode comes from a trusted source). +* +* Return: number of bytes read from the source buffer +* or a negative result if decompression fails. +*/ +int LZ4_decompress_fast (const char *source,char *dest,int originalSize); + +/** +* LZ4_decompress_safe() - Decompression protected against buffer overflow +* @source: source address of the compressed data +* @dest: output buffer address of the uncompressed data +* which must be already allocated +* @compressedSize: is the precise full size of the compressed block +* @maxDecompressedSize: is the size of 'dest' buffer +* +* Decompresses data fom 'source' into 'dest'. +* If the source stream is detected malformed, the function will +* stop decoding and return a negative result. +* This function is protected against buffer overflow exploits, +* including malicious data packets. It never writes outside output buffer, +* nor reads outside input buffer. +* +* Return: number of bytes decompressed into destination buffer +* (necessarily <= maxDecompressedSize) +* or a negative result in case of error +*/ +int LZ4_decompress_safe (const char *source,char *dest,int compressedSize, +int maxDecompressedSize); + +/** +* LZ4_decompress_safe_partial() - Decompress a block of size 'compressedSize' +* at position 'source' into buffer 'dest' +* @source: source address of the compressed data +* @dest: output buffer address of the decompressed data which must be +* already allocated +* @compressedSize: is the precise full size of the compressed block. +* @targetOutputSize: the decompression operation will try +* to stop as soon as 'targetOutputSize' has been reached +* @maxDecompressedSize: is the size of destination buffer +* +* This function decompresses a compressed block of size 'compressedSize' +* at position 'source' into destination buffer 'dest' +* of size 'maxDecompressedSize'. +* The function tries to stop decompressing operation as soon as +* 'targetOutputSize' has been reached, reducing decompression time. +* This function never writes outside of output buffer, +* and never reads outside of input buffer. +* It is therefore protected against malicious data packets. +* +* Return: the number of bytes decoded in the destination buffer +* (necessarily <= maxDecompressedSize) +* or a negative result in case of error +* +*/ +int LZ4_decompress_safe_partial (const char *source,char *dest, +int compressedSize,int targetOutputSize,int maxDecompressedSize);/* -* lz4_decompress_unknownoutputsize() -* src : source address of the compressed data -* src_len : is the input size, therefore the compressed size -* dest : output buffer address of the decompressed data -* dest_len: is the max size of the destination buffer, which is -* returned with actual size of decompressed data after -* decompress done -* return : Success if return 0 -* Error if return (< 0) -* note : Destination buffer must be already allocated. +* lz4_decompress_unknownoutputsize() - For backwards compatibility, +* see LZ4_decompress_safe +* @src: source address of the compressed data +* @src_len: is the input size, therefore the compressed size +* @dest: output buffer address of the decompressed data +* which must be already allocated +* @dest_len: is the max size of the destination buffer, which is +* returned with actual size of decompressed data after decompress done +* +* Return: Success if return 0, Error if return (< 0) */ int lz4_decompress_unknownoutputsize (const unsigned char *src,size_t src_len, -unsigned char *dest,size_t *dest_len); +unsigned char *dest,size_t *dest_len); + +/** +* lz4_decompress() - For backwards cocmpatibility, see LZ4_decompress_fast +* @src: source address of the compressed data +* @src_len: is the input size, which is returned after decompress done +* @dest: output buffer address of the decompressed data, +* which must be already allocated +* @actual_dest_len: is the size of uncompressed data, supposing it's known +* +* Return: Success if return 0, Error if return (< 0) +*/ +int lz4_decompress (const unsigned char *src,size_t *src_len, +unsigned char *dest,size_t actual_dest_len); + +/*-************************************************************************ +* LZ4 HC Compression +**************************************************************************/ + +/** +* LZ4_compress_HC() - Compress data from `src` into `dst`, using HC algorithm +* @src: source address of the original data +* @dst: output buffer address of the compressed data +* @srcSize: size of the input data. Max supported value is LZ4_MAX_INPUT_SIZE +* @dstCapacity: full or partial size of buffer 'dst', +* which must be already allocated +* @compressionLevel: Recommended values are between 4 and 9, although any +* value between 1 and LZ4HC_MAX_CLEVEL will work. +* Values >LZ4HC_MAX_CLEVEL behave the same as 16. +* @wrkmem: address of the working memory. +* This requires 'wrkmem' of size LZ4HC_MEM_COMPRESS. +* +* Compress data from 'src' into 'dst', using the more powerful +* but slower "HC" algorithm. Compression is guaranteed to succeed if +* `dstCapacity >= LZ4_compressBound(srcSize) +* +* Return : the number of bytes written into 'dst' or 0 if compression fails. +*/ +int LZ4_compress_HC (const char *src,char *dst,int srcSize,int dstCapacity, +int compressionLevel,void *wrkmem); + +/** +* lz4hc_compress() - For backwards compatibility, see LZ4_compress_HC +* @src: source address of the original data +* @src_len: size of the original data +* @dst: output buffer address of the compressed data. This requires 'dst' +* of size LZ4_COMPRESSBOUND. +* @dst_len: is the output size, which is returned after compress done +* @wrkmem: address of the working memory. +* This requires 'workmem' of size LZ4HC_MEM_COMPRESS. +* +* Return : Success if return 0, Error if return (< 0) +*/ +int lz4hc_compress (const unsigned char *src,size_t src_len,unsigned char *dst, +size_t *dst_len,void *wrkmem); + +/** +* LZ4_resetStreamHC() - Init an allocated 'LZ4_streamHC_t' structure +* @streamHCPtr: pointer to the 'LZ4_streamHC_t' structure +* @compressionLevel: Recommended values are between 4 and 9, although any +* value between 1 and LZ4HC_MAX_CLEVEL will work. +* Values >LZ4HC_MAX_CLEVEL behave the same as 16. +* +* An LZ4_streamHC_t structure can be allocated once +* and re-used multiple times. +* Use this function to init an allocated `LZ4_streamHC_t` structure +* and start a new compression. +*/ +void LZ4_resetStreamHC (LZ4_streamHC_t *streamHCPtr,int compressionLevel); + +/** +* LZ4_loadDictHC() - Load a static dictionary into LZ4_streamHC +* @streamHCPtr: pointer to the LZ4HC_stream_t +* @dictionary: dictionary to load +* @dictSize: size of dictionary +* +* Use this function to load a static dictionary into LZ4HC_stream. +* Any previous data will be forgotten, only 'dictionary' +* will remain in memory. +* Loading a size of 0 is allowed. +* +* Return : dictionary size, in bytes (necessarily <= 64 KB) +*/ +int LZ4_loadDictHC (LZ4_streamHC_t *streamHCPtr,const char *dictionary, +int dictSize); + +/** +* LZ4_compress_HC_continue() - Compress 'src' using data from previously +* compressed blocks as a dictionary using the HC algorithm +* @streamHCPtr: Pointer to the previous 'LZ4_streamHC_t' structure +* @src: source address of the original data +* @dst: output buffer address of the compressed data, +* which must be already allocated +* @srcSize: size of the input data. Max supported value is LZ4_MAX_INPUT_SIZE +* @maxDstSize: full or partial size of buffer 'dest' +* which must be already allocated +* +* These functions compress data in successive blocks of any size, using +* previous blocks as dictionary. One key assumption is that previous +* blocks (up to 64 KB) remain read-accessible while +* compressing next blocks. There is an exception for ring buffers, +* which can be smaller than 64 KB. +* Ring buffers scenario is automatically detected and handled by +* LZ4_compress_HC_continue(). +* Before starting compression, state must be properly initialized, +* using LZ4_resetStreamHC(). +* A first "fictional block" can then be designated as +* initial dictionary, using LZ4_loadDictHC() (Optional). +* Then, use LZ4_compress_HC_continue() +* to compress each successive block. Previous memory blocks +* (including initial dictionary when present) must remain accessible +* and unmodified during compression. +* 'dst' buffer should be sized to handle worst case scenarios, using +* LZ4_compressBound(), to ensure operation success. +* If, for any reason, previous data blocks can't be preserved unmodified +* in memory during next compression block, +* you must save it to a safer memory space, using LZ4_saveDictHC(). +* Return value of LZ4_saveDictHC() is the size of dictionary +* effectively saved into 'safeBuffer'. +* +* Return: Number of bytes written into buffer 'dst' or 0 if compression fails +*/ +int LZ4_compress_HC_continue (LZ4_streamHC_t *streamHCPtr,const char *src, +char *dst,int srcSize,int maxDstSize); + +/** +* LZ4_saveDictHC() - Save static dictionary from LZ4HC_stream +* @streamHCPtr: pointer to the 'LZ4HC_stream_t' structure +* @safeBuffer: buffer to save dictionary to, must be already allocated +* @maxDictSize: size of 'safeBuffer' +* +* If previously compressed data block is not guaranteed +* to remain available at its memory location, +* save it into a safer place (char *safeBuffer). +* Note : you don't need to call LZ4_loadDictHC() afterwards, +* dictionary is immediately usable, you can therefore call +* LZ4_compress_HC_continue(). +* +* Return : saved dictionary size in bytes (necessarily <= maxDictSize), +* or 0 if error. +*/ +int LZ4_saveDictHC (LZ4_streamHC_t *streamHCPtr,char *safeBuffer, +int maxDictSize); + +/*-********************************************* +* Streaming Compression Functions +***********************************************/ + +/** +* LZ4_resetStream() - Init an allocated 'LZ4_stream_t' structure +* @LZ4_stream: pointer to the 'LZ4_stream_t' structure +* +* An LZ4_stream_t structure can be allocated once +* and re-used multiple times. +* Use this function to init an allocated `LZ4_stream_t` structure +* and start a new compression. +*/ +void LZ4_resetStream (LZ4_stream_t *LZ4_stream); + +/** +* LZ4_loadDict() - Load a static dictionary into LZ4_stream +* @streamPtr: pointer to the LZ4_stream_t +* @dictionary: dictionary to load +* @dictSize: size of dictionary +* +* Use this function to load a static dictionary into LZ4_stream. +* Any previous data will be forgotten, only 'dictionary' +* will remain in memory. +* Loading a size of 0 is allowed. +* +* Return : dictionary size, in bytes (necessarily <= 64 KB) +*/ +int LZ4_loadDict (LZ4_stream_t *streamPtr,const char *dictionary, +int dictSize); + +/** +* LZ4_saveDict() - Save static dictionary from LZ4_stream +* @streamPtr: pointer to the 'LZ4_stream_t' structure +* @safeBuffer: buffer to save dictionary to, must be already allocated +* @dictSize: size of 'safeBuffer' +* +* If previously compressed data block is not guaranteed +* to remain available at its memory location, +* save it into a safer place (char *safeBuffer). +* Note : you don't need to call LZ4_loadDict() afterwards, +* dictionary is immediately usable, you can therefore call +* LZ4_compress_fast_continue(). +* +* Return : saved dictionary size in bytes (necessarily <= dictSize), +* or 0 if error. +*/ +int LZ4_saveDict (LZ4_stream_t *streamPtr,char *safeBuffer,int dictSize); + +/** +* LZ4_compress_fast_continue() - Compress 'src' using data from previously +* compressed blocks as a dictionary +* @streamPtr: Pointer to the previous 'LZ4_stream_t' structure +* @src: source address of the original data +* @dst: output buffer address of the compressed data, +* which must be already allocated +* @srcSize: size of the input data. Max supported value is LZ4_MAX_INPUT_SIZE +* @maxDstSize: full or partial size of buffer 'dest' +* which must be already allocated +* @acceleration: acceleration factor +* +* Compress buffer content 'src', using data from previously compressed blocks +* as dictionary to improve compression ratio. +* Important : Previous data blocks are assumed to still +* be present and unmodified ! +* If maxDstSize >= LZ4_compressBound(srcSize), +* compression is guaranteed to succeed, and runs faster. +* +* Return: Number of bytes written into buffer 'dst' or 0 if compression fails +*/ +int LZ4_compress_fast_continue (LZ4_stream_t *streamPtr,const char *src, +char *dst,int srcSize,int maxDstSize,int acceleration); + +/** +* LZ4_setStreamDecode() - Instruct where to find dictionary +* @LZ4_streamDecode: the 'LZ4_streamDecode_t' structure +* @dictionary: dictionary to use +* @dictSize: size of dictionary +* +* Use this function to instruct where to find the dictionary. +* Setting a size of 0 is allowed (same effect as reset). +* +* Return: 1 if OK, 0 if error +*/ +int LZ4_setStreamDecode (LZ4_streamDecode_t *LZ4_streamDecode, +const char *dictionary,int dictSize); + +/** +* LZ4_decompress_fast_continue() - Decompress blocks in streaming mode +* @LZ4_streamDecode: the 'LZ4_streamDecode_t' structure +* @source: source address of the compressed data +* @dest: output buffer address of the uncompressed data +* which must be already allocated +* @compressedSize: is the precise full size of the compressed block +* @maxDecompressedSize: is the size of 'dest' buffer +* +* These decoding function allows decompression of multiple blocks +* in "streaming" mode. +* Previously decoded blocks *must* remain available at the memory position +* where they were decoded (up to 64 KB) +* In the case of a ring buffers, decoding buffer must be either : +* - Exactly same size as encoding buffer, with same update rule +* (block boundaries at same positions) In which case, +* the decoding & encoding ring buffer can have any size, +* including very small ones ( < 64 KB). +* - Larger than encoding buffer, by a minimum of maxBlockSize more bytes. +* maxBlockSize is implementation dependent. +* It's the maximum size you intend to compress into a single block. +* In which case, encoding and decoding buffers do not need +* to be synchronized, and encoding ring buffer can have any size, +* including small ones ( < 64 KB). +* - _At least_ 64 KB + 8 bytes + maxBlockSize. +* In which case, encoding and decoding buffers do not need to be +* synchronized, and encoding ring buffer can have any size, +* including larger than decoding buffer. W +* Whenever these conditions are not possible, save the last 64KB of decoded +* data into a safe buffer, and indicate where it is saved +* using LZ4_setStreamDecode() +* +* Return: number of bytes decompressed into destination buffer +* (necessarily <= maxDecompressedSize) +* or a negative result in case of error +*/ +int LZ4_decompress_safe_continue (LZ4_streamDecode_t *LZ4_streamDecode, +const char *source,char *dest,int compressedSize, +int maxDecompressedSize); + +/** +* LZ4_decompress_fast_continue() - Decompress blocks in streaming mode +* @LZ4_streamDecode: the 'LZ4_streamDecode_t' structure +* @source: source address of the compressed data +* @dest: output buffer address of the uncompressed data +* which must be already allocated with 'originalSize' bytes +* @originalSize: is the original and therefore uncompressed size +* +* These decoding function allows decompression of multiple blocks +* in "streaming" mode. +* Previously decoded blocks *must* remain available at the memory position +* where they were decoded (up to 64 KB) +* In the case of a ring buffers, decoding buffer must be either : +* - Exactly same size as encoding buffer, with same update rule +* (block boundaries at same positions) In which case, +* the decoding & encoding ring buffer can have any size, +* including very small ones ( < 64 KB). +* - Larger than encoding buffer, by a minimum of maxBlockSize more bytes. +* maxBlockSize is implementation dependent. +* It's the maximum size you intend to compress into a single block. +* In which case, encoding and decoding buffers do not need +* to be synchronized, and encoding ring buffer can have any size, +* including small ones ( < 64 KB). +* - _At least_ 64 KB + 8 bytes + maxBlockSize. +* In which case, encoding and decoding buffers do not need to be +* synchronized, and encoding ring buffer can have any size, +* including larger than decoding buffer. W +* Whenever these conditions are not possible, save the last 64KB of decoded +* data into a safe buffer, and indicate where it is saved +* using LZ4_setStreamDecode() +* +* Return: number of bytes decompressed into destination buffer +* (necessarily <= maxDecompressedSize) +* or a negative result in case of error +*/ +int LZ4_decompress_fast_continue (LZ4_streamDecode_t *LZ4_streamDecode, +const char *source,char *dest,int originalSize); + +/** +* LZ4_decompress_safe_usingDict() - Same as LZ4_setStreamDecode() +* followed by LZ4_decompress_safe_continue() +* @source: source address of the compressed data +* @dest: output buffer address of the uncompressed data +* which must be already allocated +* @compressedSize: is the precise full size of the compressed block +* @maxDecompressedSize: is the size of 'dest' buffer +* @dictStart: pointer to the start of the dictionary in memory +* @dictSize: size of dictionary +* +* These decoding function works the same as +* a combination of LZ4_setStreamDecode() followed by +* LZ4_decompress_safe_continue() +* It is stand-alone, and don'tn eed a LZ4_streamDecode_t structure. +* +* Return: number of bytes decompressed into destination buffer +* (necessarily <= maxDecompressedSize) +* or a negative result in case of error +*/ +int LZ4_decompress_safe_usingDict (const char *source,char *dest, +int compressedSize,int maxDecompressedSize,const char *dictStart, +int dictSize); + +/** +* LZ4_decompress_fast_usingDict() - Same as LZ4_setStreamDecode() +* followed by LZ4_decompress_fast_continue() +* @source: source address of the compressed data +* @dest: output buffer address of the uncompressed data +* which must be already allocated with 'originalSize' bytes +* @originalSize: is the original and therefore uncompressed size +* @dictStart: pointer to the start of the dictionary in memory +* @dictSize: size of dictionary +* +* These decoding function works the same as +* a combination of LZ4_setStreamDecode() followed by +* LZ4_decompress_safe_continue() +* It is stand-alone, and don'tn eed a LZ4_streamDecode_t structure. +* +* Return: number of bytes decompressed into destination buffer +* (necessarily <= maxDecompressedSize) +* or a negative result in case of error +*/ +int LZ4_decompress_fast_usingDict (const char *source,char *dest, +int originalSize,const char *dictStart,int dictSize); +#endif
lib/lz4/Makefile +2 -0
@@ -1,3 +1,5 @@ +ccflags-y += -O3 + obj-$(CONFIG_LZ4_COMPRESS) += lz4_compress.o obj-$(CONFIG_LZ4HC_COMPRESS) += lz4hc_compress.o obj-$(CONFIG_LZ4_DECOMPRESS) += lz4_decompress.o
lib/lz4/lz4_compress.c +840 -321
1,276 lines not on this page · raw message
lib/lz4/lz4_decompress.c +456 -249
794 lines not on this page · raw message
lib/lz4/lz4defs.h +204 -134
142 lines not on this page · raw message
lib/lz4/lz4hc_compress.c +560 -307
867 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.1.4
Part of the diff is not shown here. View the raw message.
PATCH v8 2/5 lib/decompress_unlz4: Change module to work with new LZ4 module version
In reply to Sven SchmidtUpdate the unlz4 wrapper to work with the updated LZ4 kernel module version.
--- lib/decompress_unlz4.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
lib/decompress_unlz4.c +8 -5
@@ -72,7 +72,7 @@ STATIC inline int INIT unlz4(u8 *input, long in_len,error ("NULL input pointer and missing fill function" ); goto exit_1; }else { - inp =large_malloc (lz4_compressbound (uncomp_chunksize)); + inp =large_malloc (LZ4_compressBound (uncomp_chunksize));if (!inp) {error ("Could not allocate input buffer" ); goto exit_1;
@@ -136,7 +136,7 @@ STATIC inline int INIT unlz4(u8 *input, long in_len, inp +=4 ; size -=4 ; }else { -if (chunksize >lz4_compressbound (uncomp_chunksize)) { +if (chunksize >LZ4_compressBound (uncomp_chunksize)) {error ("chunk length is longer than allocated" ); goto exit_2; }
@@ -152,11 +152,14 @@ STATIC inline int INIT unlz4(u8 *input, long in_len, out_len -= dest_len; }else dest_len = out_len; - ret =lz4_decompress (inp, &chunksize, outp, dest_len); + + ret =LZ4_decompress_fast (inp, outp, dest_len); + chunksize = ret;#else dest_len = uncomp_chunksize; - ret =lz4_decompress_unknownoutputsize (inp, chunksize, outp, - &dest_len); + + ret =LZ4_decompress_safe (inp, outp, chunksize, dest_len); + dest_len = ret;#endif if (ret <0 ) {error ("Decoding failed" );
Signature · 1 line
2.1.4
PATCH v8 3/5 crypto: Change LZ4 modules to work with new LZ4 module version
In reply to Sven SchmidtUpdate the crypto modules using LZ4 compression as well as the test cases in testmgr.h to work with the new LZ4 module version.
--- crypto/lz4.c | 23 ++++----- crypto/lz4hc.c | 23 ++++----- crypto/testmgr.h | 142 +++++++++++++++++++++++++++++++++++++++---------------- 3 files changed, 120 insertions(+), 68 deletions(-)
crypto/lz4.c +9 -14
@@ -66,15 +66,13 @@ static void lz4_exit(struct crypto_tfm *tfm)static int __lz4_compress_crypto (const u8 *src,unsigned int slen,u8 *dst,unsigned int *dlen,void *ctx) { -size_t tmp_len = *dlen; -int err; +int out_len =LZ4_compress_default (src, dst, + slen, *dlen, ctx); - err =lz4_compress (src, slen, dst, &tmp_len, ctx); - -if (err <0 ) +if (!out_len)return -EINVAL ; - *dlen = tmp_len; + *dlen = out_len;return 0 ; }
@@ -96,16 +94,13 @@ static int lz4_compress_crypto(struct crypto_tfm *tfm, const u8 *src,static int __lz4_decompress_crypto (const u8 *src,unsigned int slen,u8 *dst,unsigned int *dlen,void *ctx) { -int err; -size_t tmp_len = *dlen; -size_t __slen = slen; +int out_len =LZ4_decompress_safe (src, dst, slen, *dlen); - err =lz4_decompress_unknownoutputsize (src, __slen, dst, &tmp_len); -if (err <0 ) -return -EINVAL ; +if (out_len <0 ) +return out_len; - *dlen = tmp_len; -return err; + *dlen = out_len; +return 0 ; } static int lz4_sdecompress(struct crypto_scomp *tfm, const u8 *src,
crypto/lz4hc.c +9 -14
@@ -65,15 +65,13 @@ static void lz4hc_exit(struct crypto_tfm *tfm)static int __lz4hc_compress_crypto (const u8 *src,unsigned int slen,u8 *dst,unsigned int *dlen,void *ctx) { -size_t tmp_len = *dlen; -int err; +int out_len =LZ4_compress_HC (src, dst, slen, + *dlen,LZ4HC_DEFAULT_CLEVEL , ctx); - err =lz4hc_compress (src, slen, dst, &tmp_len, ctx); - -if (err <0 ) +if (!out_len)return -EINVAL ; - *dlen = tmp_len; + *dlen = out_len;return 0 ; }
@@ -97,16 +95,13 @@ static int lz4hc_compress_crypto(struct crypto_tfm *tfm, const u8 *src,static int __lz4hc_decompress_crypto (const u8 *src,unsigned int slen,u8 *dst,unsigned int *dlen,void *ctx) { -int err; -size_t tmp_len = *dlen; -size_t __slen = slen; +int out_len =LZ4_decompress_safe (src, dst, slen, *dlen); - err =lz4_decompress_unknownoutputsize (src, __slen, dst, &tmp_len); -if (err <0 ) -return -EINVAL ; +if (out_len <0 ) +return out_len; - *dlen = tmp_len; -return err; + *dlen = out_len; +return 0 ; } static int lz4hc_sdecompress(struct crypto_scomp *tfm, const u8 *src,
crypto/testmgr.h +102 -40
164 lines not on this page · raw message
Signature · 1 line
2.1.4
PATCH v8 4/5 fs/pstore: fs/squashfs: Change usage of LZ4 to work with new LZ4 version
In reply to Sven SchmidtUpdate fs/pstore and fs/squashfs to use the updated functions from the new LZ4 module.
--- fs/pstore/platform.c | 22 +++++++++++++--------- fs/squashfs/lz4_wrapper.c | 12 ++++++------ 2 files changed, 19 insertions(+), 15 deletions(-)
fs/pstore/platform.c +13 -9
@@ -342,31 +342,35 @@ static int compress_lz4(const void *in, void *out, size_t inlen, size_t outlen) {int ret; - ret =lz4_compress (in, inlen, out, &outlen, workspace); -if (ret) { -pr_err ("lz4_compress error, ret = %d!\n" , ret); + ret =LZ4_compress_default (in, out, inlen, outlen, workspace); +if (!ret) { +pr_err ("LZ4_compress_default error; compression failed!\n" );return -EIO ; } -return outlen; +return ret; }static int decompress_lz4 (void *in,void *out,size_t inlen,size_t outlen) {int ret; - ret =lz4_decompress_unknownoutputsize (in, inlen, out, &outlen); -if (ret) { -pr_err ("lz4_decompress error, ret = %d!\n" , ret); + ret =LZ4_decompress_safe (in, out, inlen, outlen); +if (ret <0 ) { +/* +* LZ4_decompress_safe will return an error code +* (< 0) if decompression failed +*/ +pr_err ("LZ4_decompress_safe error, ret = %d!\n" , ret);return -EIO ; } -return outlen; +return ret; }static void allocate_lz4 (void ) { - big_oops_buf_sz =lz4_compressbound (psinfo->bufsize); + big_oops_buf_sz =LZ4_compressBound (psinfo->bufsize); big_oops_buf =kmalloc (big_oops_buf_sz,GFP_KERNEL );if (big_oops_buf) { workspace =kmalloc (LZ4_MEM_COMPRESS ,GFP_KERNEL );
fs/squashfs/lz4_wrapper.c +6 -6
@@ -97,7 +97,6 @@ static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm,struct squashfs_lz4 *stream = strm;void *buff = stream->input, *data;int avail, i, bytes = length, res; -size_t dest_len = output->length;for (i =0 ; i < b; i++) { avail =min (bytes, msblk->devblksize - offset);
@@ -108,12 +107,13 @@ static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm,put_bh (bh[i]); } - res =lz4_decompress_unknownoutputsize (stream->input, length, - stream->output, &dest_len); -if (res) + res =LZ4_decompress_safe (stream->input, stream->output, + length, output->length); + +if (res <0 )return -EIO ; - bytes = dest_len; + bytes = res; data =squashfs_first_page (output); buff = stream->output;while (data) {
@@ -128,7 +128,7 @@ static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm, }squashfs_finish_page (output); -return dest_len; +return res; }const struct squashfs_decompressor squashfs_lz4_comp_ops = {
Signature · 1 line
2.1.4
PATCH v8 5/5 lib/lz4: Remove back-compat wrappers
In reply to Sven SchmidtRemove the functions introduced as wrappers for providing backwards compatibility to the prior LZ4 version. They're not needed anymore since there's no callers left.
--- include/linux/lz4.h | 69 ------------------------------------------------ lib/lz4/lz4_compress.c | 22 --------------- lib/lz4/lz4_decompress.c | 42 ----------------------------- lib/lz4/lz4hc_compress.c | 23 ---------------- 4 files changed, 156 deletions(-)
include/linux/lz4.h +0 -69
@@ -173,18 +173,6 @@ static inline int LZ4_compressBound(size_t isize) }/** -* lz4_compressbound() - For backwards compatibility; see LZ4_compressBound -* @isize: Size of the input data -* -* Return: Max. size LZ4 may output in a "worst case" szenario -* (data not compressible) -*/ -static inline int lz4_compressbound (size_t isize) -{ -return LZ4_COMPRESSBOUND (isize); -} - -/** * LZ4_compress_default() - Compress data from source to dest * @source: source address of the original data * @dest: output buffer address of the compressed data
@@ -257,20 +245,6 @@ int LZ4_compress_fast(const char *source, char *dest, int inputSize,int LZ4_compress_destSize (const char *source,char *dest,int *sourceSizePtr,int targetDestSize,void *wrkmem); -/* -* lz4_compress() - For backward compatibility, see LZ4_compress_default -* @src: source address of the original data -* @src_len: size of the original data -* @dst: output buffer address of the compressed data. This requires 'dst' -* of size LZ4_COMPRESSBOUND -* @dst_len: is the output size, which is returned after compress done -* @workmem: address of the working memory. -* -* Return: Success if return 0, Error if return < 0 -*/ -int lz4_compress (const unsigned char *src,size_t src_len,unsigned char *dst, -size_t *dst_len,void *wrkmem); -/*-************************************************************************ * Decompression Functions **************************************************************************/
@@ -346,34 +320,6 @@ int LZ4_decompress_safe(const char *source, char *dest, int compressedSize,int LZ4_decompress_safe_partial (const char *source,char *dest,int compressedSize,int targetOutputSize,int maxDecompressedSize); -/* -* lz4_decompress_unknownoutputsize() - For backwards compatibility, -* see LZ4_decompress_safe -* @src: source address of the compressed data -* @src_len: is the input size, therefore the compressed size -* @dest: output buffer address of the decompressed data -* which must be already allocated -* @dest_len: is the max size of the destination buffer, which is -* returned with actual size of decompressed data after decompress done -* -* Return: Success if return 0, Error if return (< 0) -*/ -int lz4_decompress_unknownoutputsize (const unsigned char *src,size_t src_len, -unsigned char *dest,size_t *dest_len); - -/** -* lz4_decompress() - For backwards cocmpatibility, see LZ4_decompress_fast -* @src: source address of the compressed data -* @src_len: is the input size, which is returned after decompress done -* @dest: output buffer address of the decompressed data, -* which must be already allocated -* @actual_dest_len: is the size of uncompressed data, supposing it's known -* -* Return: Success if return 0, Error if return (< 0) -*/ -int lz4_decompress (const unsigned char *src,size_t *src_len, -unsigned char *dest,size_t actual_dest_len); -/*-************************************************************************ * LZ4 HC Compression **************************************************************************/
@@ -401,21 +347,6 @@ int LZ4_compress_HC(const char *src, char *dst, int srcSize, int dstCapacity, int compressionLevel, void *wrkmem);/** -* lz4hc_compress() - For backwards compatibility, see LZ4_compress_HC -* @src: source address of the original data -* @src_len: size of the original data -* @dst: output buffer address of the compressed data. This requires 'dst' -* of size LZ4_COMPRESSBOUND. -* @dst_len: is the output size, which is returned after compress done -* @wrkmem: address of the working memory. -* This requires 'workmem' of size LZ4HC_MEM_COMPRESS. -* -* Return : Success if return 0, Error if return (< 0) -*/ -int lz4hc_compress (const unsigned char *src,size_t src_len,unsigned char *dst, -size_t *dst_len,void *wrkmem); - -/** * LZ4_resetStreamHC() - Init an allocated 'LZ4_streamHC_t' structure * @streamHCPtr: pointer to the 'LZ4_streamHC_t' structure * @compressionLevel: Recommended values are between 4 and 9, although any
lib/lz4/lz4_compress.c +0 -22
@@ -936,27 +936,5 @@ int LZ4_compress_fast_continue(LZ4_stream_t *LZ4_stream, const char *source, }EXPORT_SYMBOL (LZ4_compress_fast_continue); -/*-****************************** -* For backwards compatibility -********************************/ -int lz4_compress (const unsigned char *src,size_t src_len,unsigned char *dst, -size_t *dst_len,void *wrkmem) { - *dst_len =LZ4_compress_default (src, dst, src_len, - *dst_len, wrkmem); - -/* -* Prior lz4_compress will return -1 in case of error -* and 0 on success -* while new LZ4_compress_fast/default -* returns 0 in case of error -* and the output length on success -*/ -if (!*dst_len) -return -1 ; -else -return 0 ; -} -EXPORT_SYMBOL (lz4_compress); -MODULE_LICENSE ("Dual BSD/GPL" );MODULE_DESCRIPTION ("LZ4 compressor" );
lib/lz4/lz4_decompress.c +0 -42
54 lines not on this page · raw message
lib/lz4/lz4hc_compress.c +0 -23
@@ -765,28 +765,5 @@ int LZ4_saveDictHC( }EXPORT_SYMBOL (LZ4_saveDictHC); -/*-****************************** -* For backwards compatibility -********************************/ -int lz4hc_compress (const unsigned char *src,size_t src_len, -unsigned char *dst,size_t *dst_len,void *wrkmem) -{ - *dst_len =LZ4_compress_HC (src, dst, src_len, - *dst_len,LZ4HC_DEFAULT_CLEVEL , wrkmem); - -/* -* Prior lz4hc_compress will return -1 in case of error -* and 0 on success -* while new LZ4_compress_HC -* returns 0 in case of error -* and the output length on success -*/ -if (!*dst_len) -return -1 ; -else -return 0 ; -} -EXPORT_SYMBOL (lz4hc_compress); -MODULE_LICENSE ("Dual BSD/GPL" );MODULE_DESCRIPTION ("LZ4 HC compressor" );
Signature · 1 line
2.1.4