Commit f2bac7ad authored by Sergey Senozhatsky's avatar Sergey Senozhatsky Committed by Andrew Morton

zram: introduce zcomp_params structure

We will store a per-algorithm parameters there (compression level,
dictionary, dictionary size, etc.).

Link: https://lkml.kernel.org/r/20240902105656.1383858-14-senozhatsky@chromium.orgSigned-off-by: default avatarSergey Senozhatsky <senozhatsky@chromium.org>
Cc: Minchan Kim <minchan@kernel.org>
Cc: Nick Terrell <terrelln@fb.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent 1a78390d
...@@ -19,7 +19,7 @@ static void destroy_842(void *ctx) ...@@ -19,7 +19,7 @@ static void destroy_842(void *ctx)
kfree(zctx); kfree(zctx);
} }
static void *create_842(void) static void *create_842(struct zcomp_params *params)
{ {
struct sw842_ctx *ctx; struct sw842_ctx *ctx;
......
...@@ -32,7 +32,7 @@ static void deflate_destroy(void *ctx) ...@@ -32,7 +32,7 @@ static void deflate_destroy(void *ctx)
kfree(zctx); kfree(zctx);
} }
static void *deflate_create(void) static void *deflate_create(struct zcomp_params *params)
{ {
struct deflate_ctx *ctx; struct deflate_ctx *ctx;
size_t sz; size_t sz;
...@@ -42,8 +42,11 @@ static void *deflate_create(void) ...@@ -42,8 +42,11 @@ static void *deflate_create(void)
if (!ctx) if (!ctx)
return NULL; return NULL;
/* @FIXME: using a hardcoded Z_DEFAULT_COMPRESSION for now */ if (params->level != ZCOMP_PARAM_NO_LEVEL)
ctx->level = Z_DEFAULT_COMPRESSION; ctx->level = params->level;
else
ctx->level = Z_DEFAULT_COMPRESSION;
sz = zlib_deflate_workspacesize(-DEFLATE_DEF_WINBITS, MAX_MEM_LEVEL); sz = zlib_deflate_workspacesize(-DEFLATE_DEF_WINBITS, MAX_MEM_LEVEL);
ctx->cctx.workspace = vzalloc(sz); ctx->cctx.workspace = vzalloc(sz);
if (!ctx->cctx.workspace) if (!ctx->cctx.workspace)
......
...@@ -18,7 +18,7 @@ static void lz4_destroy(void *ctx) ...@@ -18,7 +18,7 @@ static void lz4_destroy(void *ctx)
kfree(zctx); kfree(zctx);
} }
static void *lz4_create(void) static void *lz4_create(struct zcomp_params *params)
{ {
struct lz4_ctx *ctx; struct lz4_ctx *ctx;
...@@ -26,8 +26,11 @@ static void *lz4_create(void) ...@@ -26,8 +26,11 @@ static void *lz4_create(void)
if (!ctx) if (!ctx)
return NULL; return NULL;
/* @FIXME: using a hardcoded LZ4_ACCELERATION_DEFAULT for now */ if (params->level != ZCOMP_PARAM_NO_LEVEL)
ctx->level = LZ4_ACCELERATION_DEFAULT; ctx->level = params->level;
else
ctx->level = LZ4_ACCELERATION_DEFAULT;
ctx->mem = vmalloc(LZ4_MEM_COMPRESS); ctx->mem = vmalloc(LZ4_MEM_COMPRESS);
if (!ctx->mem) if (!ctx->mem)
goto error; goto error;
......
...@@ -18,7 +18,7 @@ static void lz4hc_destroy(void *ctx) ...@@ -18,7 +18,7 @@ static void lz4hc_destroy(void *ctx)
kfree(zctx); kfree(zctx);
} }
static void *lz4hc_create(void) static void *lz4hc_create(struct zcomp_params *params)
{ {
struct lz4hc_ctx *ctx; struct lz4hc_ctx *ctx;
...@@ -26,8 +26,11 @@ static void *lz4hc_create(void) ...@@ -26,8 +26,11 @@ static void *lz4hc_create(void)
if (!ctx) if (!ctx)
return NULL; return NULL;
/* @FIXME: using a hardcoded LZ4HC_DEFAULT_CLEVEL for now */ if (params->level != ZCOMP_PARAM_NO_LEVEL)
ctx->level = LZ4HC_DEFAULT_CLEVEL; ctx->level = params->level;
else
ctx->level = LZ4HC_DEFAULT_CLEVEL;
ctx->mem = vmalloc(LZ4HC_MEM_COMPRESS); ctx->mem = vmalloc(LZ4HC_MEM_COMPRESS);
if (!ctx->mem) if (!ctx->mem)
goto error; goto error;
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
#include "backend_lzo.h" #include "backend_lzo.h"
static void *lzo_create(void) static void *lzo_create(struct zcomp_params *params)
{ {
return kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); return kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
} }
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
#include "backend_lzorle.h" #include "backend_lzorle.h"
static void *lzorle_create(void) static void *lzorle_create(struct zcomp_params *params)
{ {
return kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); return kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
} }
......
...@@ -24,9 +24,9 @@ static void zstd_destroy(void *ctx) ...@@ -24,9 +24,9 @@ static void zstd_destroy(void *ctx)
kfree(zctx); kfree(zctx);
} }
static void *zstd_create(void) static void *zstd_create(struct zcomp_params *params)
{ {
zstd_parameters params; zstd_parameters prm;
struct zstd_ctx *ctx; struct zstd_ctx *ctx;
size_t sz; size_t sz;
...@@ -34,9 +34,13 @@ static void *zstd_create(void) ...@@ -34,9 +34,13 @@ static void *zstd_create(void)
if (!ctx) if (!ctx)
return NULL; return NULL;
ctx->level = zstd_default_clevel(); if (params->level != ZCOMP_PARAM_NO_LEVEL)
params = zstd_get_params(ctx->level, PAGE_SIZE); ctx->level = params->level;
sz = zstd_cctx_workspace_bound(&params.cParams); else
ctx->level = zstd_default_clevel();
prm = zstd_get_params(ctx->level, PAGE_SIZE);
sz = zstd_cctx_workspace_bound(&prm.cParams);
ctx->cctx_mem = vzalloc(sz); ctx->cctx_mem = vzalloc(sz);
if (!ctx->cctx_mem) if (!ctx->cctx_mem)
goto error; goto error;
...@@ -65,11 +69,11 @@ static int zstd_compress(void *ctx, const unsigned char *src, size_t src_len, ...@@ -65,11 +69,11 @@ static int zstd_compress(void *ctx, const unsigned char *src, size_t src_len,
unsigned char *dst, size_t *dst_len) unsigned char *dst, size_t *dst_len)
{ {
struct zstd_ctx *zctx = ctx; struct zstd_ctx *zctx = ctx;
const zstd_parameters params = zstd_get_params(zctx->level, PAGE_SIZE); const zstd_parameters prm = zstd_get_params(zctx->level, PAGE_SIZE);
size_t ret; size_t ret;
ret = zstd_compress_cctx(zctx->cctx, dst, *dst_len, ret = zstd_compress_cctx(zctx->cctx, dst, *dst_len,
src, src_len, &params); src, src_len, &prm);
if (zstd_is_error(ret)) if (zstd_is_error(ret))
return -EINVAL; return -EINVAL;
*dst_len = ret; *dst_len = ret;
......
...@@ -54,7 +54,7 @@ static void zcomp_strm_free(struct zcomp *comp, struct zcomp_strm *zstrm) ...@@ -54,7 +54,7 @@ static void zcomp_strm_free(struct zcomp *comp, struct zcomp_strm *zstrm)
static int zcomp_strm_init(struct zcomp *comp, struct zcomp_strm *zstrm) static int zcomp_strm_init(struct zcomp *comp, struct zcomp_strm *zstrm)
{ {
zstrm->ctx = comp->ops->create_ctx(); zstrm->ctx = comp->ops->create_ctx(comp->params);
/* /*
* allocate 2 pages. 1 for compressed data, plus 1 extra for the * allocate 2 pages. 1 for compressed data, plus 1 extra for the
...@@ -187,7 +187,7 @@ void zcomp_destroy(struct zcomp *comp) ...@@ -187,7 +187,7 @@ void zcomp_destroy(struct zcomp *comp)
kfree(comp); kfree(comp);
} }
struct zcomp *zcomp_create(const char *alg) struct zcomp *zcomp_create(const char *alg, struct zcomp_params *params)
{ {
struct zcomp *comp; struct zcomp *comp;
int error; int error;
...@@ -204,6 +204,7 @@ struct zcomp *zcomp_create(const char *alg) ...@@ -204,6 +204,7 @@ struct zcomp *zcomp_create(const char *alg)
if (!comp) if (!comp)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
comp->params = params;
comp->ops = lookup_backend_ops(alg); comp->ops = lookup_backend_ops(alg);
if (!comp->ops) { if (!comp->ops) {
kfree(comp); kfree(comp);
......
...@@ -2,8 +2,17 @@ ...@@ -2,8 +2,17 @@
#ifndef _ZCOMP_H_ #ifndef _ZCOMP_H_
#define _ZCOMP_H_ #define _ZCOMP_H_
#include <linux/local_lock.h> #include <linux/local_lock.h>
#define ZCOMP_PARAM_NO_LEVEL INT_MIN
struct zcomp_params {
void *dict;
size_t dict_sz;
s32 level;
};
struct zcomp_strm { struct zcomp_strm {
/* The members ->buffer and ->tfm are protected by ->lock. */ /* The members ->buffer and ->tfm are protected by ->lock. */
local_lock_t lock; local_lock_t lock;
...@@ -19,7 +28,7 @@ struct zcomp_ops { ...@@ -19,7 +28,7 @@ struct zcomp_ops {
int (*decompress)(void *ctx, const unsigned char *src, size_t src_len, int (*decompress)(void *ctx, const unsigned char *src, size_t src_len,
unsigned char *dst, size_t dst_len); unsigned char *dst, size_t dst_len);
void *(*create_ctx)(void); void *(*create_ctx)(struct zcomp_params *params);
void (*destroy_ctx)(void *ctx); void (*destroy_ctx)(void *ctx);
const char *name; const char *name;
...@@ -29,6 +38,7 @@ struct zcomp_ops { ...@@ -29,6 +38,7 @@ struct zcomp_ops {
struct zcomp { struct zcomp {
struct zcomp_strm __percpu *stream; struct zcomp_strm __percpu *stream;
const struct zcomp_ops *ops; const struct zcomp_ops *ops;
struct zcomp_params *params;
struct hlist_node node; struct hlist_node node;
}; };
...@@ -37,7 +47,7 @@ int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node); ...@@ -37,7 +47,7 @@ int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node);
ssize_t zcomp_available_show(const char *comp, char *buf); ssize_t zcomp_available_show(const char *comp, char *buf);
bool zcomp_available_algorithm(const char *comp); bool zcomp_available_algorithm(const char *comp);
struct zcomp *zcomp_create(const char *alg); struct zcomp *zcomp_create(const char *alg, struct zcomp_params *params);
void zcomp_destroy(struct zcomp *comp); void zcomp_destroy(struct zcomp *comp);
struct zcomp_strm *zcomp_stream_get(struct zcomp *comp); struct zcomp_strm *zcomp_stream_get(struct zcomp *comp);
......
...@@ -1979,6 +1979,20 @@ static void zram_slot_free_notify(struct block_device *bdev, ...@@ -1979,6 +1979,20 @@ static void zram_slot_free_notify(struct block_device *bdev,
zram_slot_unlock(zram, index); zram_slot_unlock(zram, index);
} }
static void zram_comp_params_reset(struct zram *zram)
{
u32 prio;
for (prio = ZRAM_PRIMARY_COMP; prio < ZRAM_MAX_COMPS; prio++) {
struct zcomp_params *params = &zram->params[prio];
vfree(params->dict);
params->level = ZCOMP_PARAM_NO_LEVEL;
params->dict_sz = 0;
params->dict = NULL;
}
}
static void zram_destroy_comps(struct zram *zram) static void zram_destroy_comps(struct zram *zram)
{ {
u32 prio; u32 prio;
...@@ -1992,6 +2006,8 @@ static void zram_destroy_comps(struct zram *zram) ...@@ -1992,6 +2006,8 @@ static void zram_destroy_comps(struct zram *zram)
zcomp_destroy(comp); zcomp_destroy(comp);
zram->num_active_comps--; zram->num_active_comps--;
} }
zram_comp_params_reset(zram);
} }
static void zram_reset_device(struct zram *zram) static void zram_reset_device(struct zram *zram)
...@@ -2049,7 +2065,8 @@ static ssize_t disksize_store(struct device *dev, ...@@ -2049,7 +2065,8 @@ static ssize_t disksize_store(struct device *dev,
if (!zram->comp_algs[prio]) if (!zram->comp_algs[prio])
continue; continue;
comp = zcomp_create(zram->comp_algs[prio]); comp = zcomp_create(zram->comp_algs[prio],
&zram->params[prio]);
if (IS_ERR(comp)) { if (IS_ERR(comp)) {
pr_err("Cannot initialise %s compressing backend\n", pr_err("Cannot initialise %s compressing backend\n",
zram->comp_algs[prio]); zram->comp_algs[prio]);
...@@ -2254,6 +2271,7 @@ static int zram_add(void) ...@@ -2254,6 +2271,7 @@ static int zram_add(void)
if (ret) if (ret)
goto out_cleanup_disk; goto out_cleanup_disk;
zram_comp_params_reset(zram);
comp_algorithm_set(zram, ZRAM_PRIMARY_COMP, default_compressor); comp_algorithm_set(zram, ZRAM_PRIMARY_COMP, default_compressor);
zram_debugfs_register(zram); zram_debugfs_register(zram);
......
...@@ -107,6 +107,7 @@ struct zram { ...@@ -107,6 +107,7 @@ struct zram {
struct zram_table_entry *table; struct zram_table_entry *table;
struct zs_pool *mem_pool; struct zs_pool *mem_pool;
struct zcomp *comps[ZRAM_MAX_COMPS]; struct zcomp *comps[ZRAM_MAX_COMPS];
struct zcomp_params params[ZRAM_MAX_COMPS];
struct gendisk *disk; struct gendisk *disk;
/* Prevent concurrent execution of device init */ /* Prevent concurrent execution of device init */
struct rw_semaphore init_lock; struct rw_semaphore init_lock;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment