Re: [PATCH v4] pstore: add lz4hc and 842 compression support

2018-03-06 Thread Kees Cook
On Mon, Feb 12, 2018 at 10:40 PM, Geliang Tang  wrote:
> Currently, pstore has supported three compression algorithms: zlib,
> lzo and lz4. This patch added two more compression algorithms: lz4hc
> and 842.
>
> Signed-off-by: Geliang Tang 

Thanks for the updates, I've applied this. I'll send a follow-up patch
that I'm adding to avoid size_t cast to unsigned int.

-Kees

> ---
> Changes in v4:
>  -842 compress is not work in patch v3 since big_oops_buf_sz is too big.
>   change big_oops_buf_sz from psinfo->bufsize * 2 to psinfo->bufsize.
> Changes in v3:
>  -fix outlen in 842
> Changes in v2:
>  -fix checkpatch.pl WARNING:
>   please write a paragraph that describes the config symbol fully
> ---
>  fs/pstore/Kconfig|  25 +
>  fs/pstore/platform.c | 147 
> ---
>  2 files changed, 154 insertions(+), 18 deletions(-)
>
> diff --git a/fs/pstore/Kconfig b/fs/pstore/Kconfig
> index b42e5bd6d8ff..cf97a3ecdce0 100644
> --- a/fs/pstore/Kconfig
> +++ b/fs/pstore/Kconfig
> @@ -39,6 +39,31 @@ config PSTORE_LZ4_COMPRESS
>  select LZ4_DECOMPRESS
>  help
>This option enables LZ4 compression algorithm support.
> +
> +config PSTORE_LZ4HC_COMPRESS
> +   bool "LZ4HC"
> +   select LZ4HC_COMPRESS
> +   select LZ4_DECOMPRESS
> +   help
> + This option enables LZ4 high compression mode algorithm.
> +
> + Currently, pstore has supported five compression algorithms:
> + zlib, lzo, lz4, lz4hc and 842.
> +
> + The default compression algorithm is zlib.
> +
> +config PSTORE_842_COMPRESS
> +   bool "842"
> +   select 842_COMPRESS
> +   select 842_DECOMPRESS
> +   help
> + This option enables 842 compression algorithm support.
> +
> + Currently, pstore has supported five compression algorithms:
> + zlib, lzo, lz4, lz4hc and 842.
> +
> + The default compression algorithm is zlib.
> +
>  endchoice
>
>  config PSTORE_CONSOLE
> diff --git a/fs/pstore/platform.c b/fs/pstore/platform.c
> index c3129b131e4d..19aaefeb052f 100644
> --- a/fs/pstore/platform.c
> +++ b/fs/pstore/platform.c
> @@ -34,9 +34,12 @@
>  #ifdef CONFIG_PSTORE_LZO_COMPRESS
>  #include 
>  #endif
> -#ifdef CONFIG_PSTORE_LZ4_COMPRESS
> +#if defined(CONFIG_PSTORE_LZ4_COMPRESS) || 
> defined(CONFIG_PSTORE_LZ4HC_COMPRESS)
>  #include 
>  #endif
> +#ifdef CONFIG_PSTORE_842_COMPRESS
> +#include 
> +#endif
>  #include 
>  #include 
>  #include 
> @@ -336,6 +339,33 @@ static const struct pstore_zbackend backend_lzo = {
>  };
>  #endif
>
> +#if defined(CONFIG_PSTORE_LZ4_COMPRESS) || 
> defined(CONFIG_PSTORE_LZ4HC_COMPRESS)
> +static int decompress_lz4(void *in, void *out, size_t inlen, size_t outlen)
> +{
> +   int 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 ret;
> +}
> +
> +static void free_lz4(void)
> +{
> +   kfree(workspace);
> +   kfree(big_oops_buf);
> +   big_oops_buf = NULL;
> +   big_oops_buf_sz = 0;
> +}
> +#endif
> +
>  #ifdef CONFIG_PSTORE_LZ4_COMPRESS
>  static int compress_lz4(const void *in, void *out, size_t inlen, size_t 
> outlen)
>  {
> @@ -350,29 +380,54 @@ static int compress_lz4(const void *in, void *out, 
> size_t inlen, size_t outlen)
> return ret;
>  }
>
> -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 = kmalloc(big_oops_buf_sz, GFP_KERNEL);
> +   if (big_oops_buf) {
> +   workspace = kmalloc(LZ4_MEM_COMPRESS, GFP_KERNEL);
> +   if (!workspace) {
> +   pr_err("No memory for compression workspace; skipping 
> compression\n");
> +   kfree(big_oops_buf);
> +   big_oops_buf = NULL;
> +   }
> +   } else {
> +   pr_err("No memory for uncompressed data; skipping 
> compression\n");
> +   workspace = NULL;
> +   }
> +}
> +
> +static const struct pstore_zbackend backend_lz4 = {
> +   .compress   = compress_lz4,
> +   .decompress = decompress_lz4,
> +   .allocate   = allocate_lz4,
> +   .free   = free_lz4,
> +   .name   = "lz4",
> +};
> +#endif
> +
> +#ifdef CONFIG_PSTORE_LZ4HC_COMPRESS
> +static int compress_lz4hc(const void *in, void *out,
> + size_t inlen, size_t outlen)
>  {
> int ret;
>
> -   ret = LZ4_decompress_safe(in, out, inlen, outlen);
> -   if (ret < 0) {
> -   /*
> -  

Re: [PATCH v4] pstore: add lz4hc and 842 compression support

2018-03-06 Thread Kees Cook
On Mon, Feb 12, 2018 at 10:40 PM, Geliang Tang  wrote:
> Currently, pstore has supported three compression algorithms: zlib,
> lzo and lz4. This patch added two more compression algorithms: lz4hc
> and 842.
>
> Signed-off-by: Geliang Tang 

Thanks for the updates, I've applied this. I'll send a follow-up patch
that I'm adding to avoid size_t cast to unsigned int.

-Kees

> ---
> Changes in v4:
>  -842 compress is not work in patch v3 since big_oops_buf_sz is too big.
>   change big_oops_buf_sz from psinfo->bufsize * 2 to psinfo->bufsize.
> Changes in v3:
>  -fix outlen in 842
> Changes in v2:
>  -fix checkpatch.pl WARNING:
>   please write a paragraph that describes the config symbol fully
> ---
>  fs/pstore/Kconfig|  25 +
>  fs/pstore/platform.c | 147 
> ---
>  2 files changed, 154 insertions(+), 18 deletions(-)
>
> diff --git a/fs/pstore/Kconfig b/fs/pstore/Kconfig
> index b42e5bd6d8ff..cf97a3ecdce0 100644
> --- a/fs/pstore/Kconfig
> +++ b/fs/pstore/Kconfig
> @@ -39,6 +39,31 @@ config PSTORE_LZ4_COMPRESS
>  select LZ4_DECOMPRESS
>  help
>This option enables LZ4 compression algorithm support.
> +
> +config PSTORE_LZ4HC_COMPRESS
> +   bool "LZ4HC"
> +   select LZ4HC_COMPRESS
> +   select LZ4_DECOMPRESS
> +   help
> + This option enables LZ4 high compression mode algorithm.
> +
> + Currently, pstore has supported five compression algorithms:
> + zlib, lzo, lz4, lz4hc and 842.
> +
> + The default compression algorithm is zlib.
> +
> +config PSTORE_842_COMPRESS
> +   bool "842"
> +   select 842_COMPRESS
> +   select 842_DECOMPRESS
> +   help
> + This option enables 842 compression algorithm support.
> +
> + Currently, pstore has supported five compression algorithms:
> + zlib, lzo, lz4, lz4hc and 842.
> +
> + The default compression algorithm is zlib.
> +
>  endchoice
>
>  config PSTORE_CONSOLE
> diff --git a/fs/pstore/platform.c b/fs/pstore/platform.c
> index c3129b131e4d..19aaefeb052f 100644
> --- a/fs/pstore/platform.c
> +++ b/fs/pstore/platform.c
> @@ -34,9 +34,12 @@
>  #ifdef CONFIG_PSTORE_LZO_COMPRESS
>  #include 
>  #endif
> -#ifdef CONFIG_PSTORE_LZ4_COMPRESS
> +#if defined(CONFIG_PSTORE_LZ4_COMPRESS) || 
> defined(CONFIG_PSTORE_LZ4HC_COMPRESS)
>  #include 
>  #endif
> +#ifdef CONFIG_PSTORE_842_COMPRESS
> +#include 
> +#endif
>  #include 
>  #include 
>  #include 
> @@ -336,6 +339,33 @@ static const struct pstore_zbackend backend_lzo = {
>  };
>  #endif
>
> +#if defined(CONFIG_PSTORE_LZ4_COMPRESS) || 
> defined(CONFIG_PSTORE_LZ4HC_COMPRESS)
> +static int decompress_lz4(void *in, void *out, size_t inlen, size_t outlen)
> +{
> +   int 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 ret;
> +}
> +
> +static void free_lz4(void)
> +{
> +   kfree(workspace);
> +   kfree(big_oops_buf);
> +   big_oops_buf = NULL;
> +   big_oops_buf_sz = 0;
> +}
> +#endif
> +
>  #ifdef CONFIG_PSTORE_LZ4_COMPRESS
>  static int compress_lz4(const void *in, void *out, size_t inlen, size_t 
> outlen)
>  {
> @@ -350,29 +380,54 @@ static int compress_lz4(const void *in, void *out, 
> size_t inlen, size_t outlen)
> return ret;
>  }
>
> -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 = kmalloc(big_oops_buf_sz, GFP_KERNEL);
> +   if (big_oops_buf) {
> +   workspace = kmalloc(LZ4_MEM_COMPRESS, GFP_KERNEL);
> +   if (!workspace) {
> +   pr_err("No memory for compression workspace; skipping 
> compression\n");
> +   kfree(big_oops_buf);
> +   big_oops_buf = NULL;
> +   }
> +   } else {
> +   pr_err("No memory for uncompressed data; skipping 
> compression\n");
> +   workspace = NULL;
> +   }
> +}
> +
> +static const struct pstore_zbackend backend_lz4 = {
> +   .compress   = compress_lz4,
> +   .decompress = decompress_lz4,
> +   .allocate   = allocate_lz4,
> +   .free   = free_lz4,
> +   .name   = "lz4",
> +};
> +#endif
> +
> +#ifdef CONFIG_PSTORE_LZ4HC_COMPRESS
> +static int compress_lz4hc(const void *in, void *out,
> + size_t inlen, size_t outlen)
>  {
> int ret;
>
> -   ret = LZ4_decompress_safe(in, out, inlen, outlen);
> -   if (ret < 0) {
> -   /*
> -* LZ4_decompress_safe will return an error