Skip to content

Commit f2bac7a

Browse files
sergey-senozhatskyakpm00
authored andcommitted
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/[email protected] Signed-off-by: Sergey Senozhatsky <[email protected]> Cc: Minchan Kim <[email protected]> Cc: Nick Terrell <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 1a78390 commit f2bac7a

File tree

11 files changed

+67
-24
lines changed

11 files changed

+67
-24
lines changed

drivers/block/zram/backend_842.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ static void destroy_842(void *ctx)
1919
kfree(zctx);
2020
}
2121

22-
static void *create_842(void)
22+
static void *create_842(struct zcomp_params *params)
2323
{
2424
struct sw842_ctx *ctx;
2525

drivers/block/zram/backend_deflate.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static void deflate_destroy(void *ctx)
3232
kfree(zctx);
3333
}
3434

35-
static void *deflate_create(void)
35+
static void *deflate_create(struct zcomp_params *params)
3636
{
3737
struct deflate_ctx *ctx;
3838
size_t sz;
@@ -42,8 +42,11 @@ static void *deflate_create(void)
4242
if (!ctx)
4343
return NULL;
4444

45-
/* @FIXME: using a hardcoded Z_DEFAULT_COMPRESSION for now */
46-
ctx->level = Z_DEFAULT_COMPRESSION;
45+
if (params->level != ZCOMP_PARAM_NO_LEVEL)
46+
ctx->level = params->level;
47+
else
48+
ctx->level = Z_DEFAULT_COMPRESSION;
49+
4750
sz = zlib_deflate_workspacesize(-DEFLATE_DEF_WINBITS, MAX_MEM_LEVEL);
4851
ctx->cctx.workspace = vzalloc(sz);
4952
if (!ctx->cctx.workspace)

drivers/block/zram/backend_lz4.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,19 @@ static void lz4_destroy(void *ctx)
1818
kfree(zctx);
1919
}
2020

21-
static void *lz4_create(void)
21+
static void *lz4_create(struct zcomp_params *params)
2222
{
2323
struct lz4_ctx *ctx;
2424

2525
ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2626
if (!ctx)
2727
return NULL;
2828

29-
/* @FIXME: using a hardcoded LZ4_ACCELERATION_DEFAULT for now */
30-
ctx->level = LZ4_ACCELERATION_DEFAULT;
29+
if (params->level != ZCOMP_PARAM_NO_LEVEL)
30+
ctx->level = params->level;
31+
else
32+
ctx->level = LZ4_ACCELERATION_DEFAULT;
33+
3134
ctx->mem = vmalloc(LZ4_MEM_COMPRESS);
3235
if (!ctx->mem)
3336
goto error;

drivers/block/zram/backend_lz4hc.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,19 @@ static void lz4hc_destroy(void *ctx)
1818
kfree(zctx);
1919
}
2020

21-
static void *lz4hc_create(void)
21+
static void *lz4hc_create(struct zcomp_params *params)
2222
{
2323
struct lz4hc_ctx *ctx;
2424

2525
ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2626
if (!ctx)
2727
return NULL;
2828

29-
/* @FIXME: using a hardcoded LZ4HC_DEFAULT_CLEVEL for now */
30-
ctx->level = LZ4HC_DEFAULT_CLEVEL;
29+
if (params->level != ZCOMP_PARAM_NO_LEVEL)
30+
ctx->level = params->level;
31+
else
32+
ctx->level = LZ4HC_DEFAULT_CLEVEL;
33+
3134
ctx->mem = vmalloc(LZ4HC_MEM_COMPRESS);
3235
if (!ctx->mem)
3336
goto error;

drivers/block/zram/backend_lzo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include "backend_lzo.h"
88

9-
static void *lzo_create(void)
9+
static void *lzo_create(struct zcomp_params *params)
1010
{
1111
return kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
1212
}

drivers/block/zram/backend_lzorle.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include "backend_lzorle.h"
88

9-
static void *lzorle_create(void)
9+
static void *lzorle_create(struct zcomp_params *params)
1010
{
1111
return kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
1212
}

drivers/block/zram/backend_zstd.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,23 @@ static void zstd_destroy(void *ctx)
2424
kfree(zctx);
2525
}
2626

27-
static void *zstd_create(void)
27+
static void *zstd_create(struct zcomp_params *params)
2828
{
29-
zstd_parameters params;
29+
zstd_parameters prm;
3030
struct zstd_ctx *ctx;
3131
size_t sz;
3232

3333
ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
3434
if (!ctx)
3535
return NULL;
3636

37-
ctx->level = zstd_default_clevel();
38-
params = zstd_get_params(ctx->level, PAGE_SIZE);
39-
sz = zstd_cctx_workspace_bound(&params.cParams);
37+
if (params->level != ZCOMP_PARAM_NO_LEVEL)
38+
ctx->level = params->level;
39+
else
40+
ctx->level = zstd_default_clevel();
41+
42+
prm = zstd_get_params(ctx->level, PAGE_SIZE);
43+
sz = zstd_cctx_workspace_bound(&prm.cParams);
4044
ctx->cctx_mem = vzalloc(sz);
4145
if (!ctx->cctx_mem)
4246
goto error;
@@ -65,11 +69,11 @@ static int zstd_compress(void *ctx, const unsigned char *src, size_t src_len,
6569
unsigned char *dst, size_t *dst_len)
6670
{
6771
struct zstd_ctx *zctx = ctx;
68-
const zstd_parameters params = zstd_get_params(zctx->level, PAGE_SIZE);
72+
const zstd_parameters prm = zstd_get_params(zctx->level, PAGE_SIZE);
6973
size_t ret;
7074

7175
ret = zstd_compress_cctx(zctx->cctx, dst, *dst_len,
72-
src, src_len, &params);
76+
src, src_len, &prm);
7377
if (zstd_is_error(ret))
7478
return -EINVAL;
7579
*dst_len = ret;

drivers/block/zram/zcomp.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static void zcomp_strm_free(struct zcomp *comp, struct zcomp_strm *zstrm)
5454

5555
static int zcomp_strm_init(struct zcomp *comp, struct zcomp_strm *zstrm)
5656
{
57-
zstrm->ctx = comp->ops->create_ctx();
57+
zstrm->ctx = comp->ops->create_ctx(comp->params);
5858

5959
/*
6060
* allocate 2 pages. 1 for compressed data, plus 1 extra for the
@@ -187,7 +187,7 @@ void zcomp_destroy(struct zcomp *comp)
187187
kfree(comp);
188188
}
189189

190-
struct zcomp *zcomp_create(const char *alg)
190+
struct zcomp *zcomp_create(const char *alg, struct zcomp_params *params)
191191
{
192192
struct zcomp *comp;
193193
int error;
@@ -204,6 +204,7 @@ struct zcomp *zcomp_create(const char *alg)
204204
if (!comp)
205205
return ERR_PTR(-ENOMEM);
206206

207+
comp->params = params;
207208
comp->ops = lookup_backend_ops(alg);
208209
if (!comp->ops) {
209210
kfree(comp);

drivers/block/zram/zcomp.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@
22

33
#ifndef _ZCOMP_H_
44
#define _ZCOMP_H_
5+
56
#include <linux/local_lock.h>
67

8+
#define ZCOMP_PARAM_NO_LEVEL INT_MIN
9+
10+
struct zcomp_params {
11+
void *dict;
12+
size_t dict_sz;
13+
s32 level;
14+
};
15+
716
struct zcomp_strm {
817
/* The members ->buffer and ->tfm are protected by ->lock. */
918
local_lock_t lock;
@@ -19,7 +28,7 @@ struct zcomp_ops {
1928
int (*decompress)(void *ctx, const unsigned char *src, size_t src_len,
2029
unsigned char *dst, size_t dst_len);
2130

22-
void *(*create_ctx)(void);
31+
void *(*create_ctx)(struct zcomp_params *params);
2332
void (*destroy_ctx)(void *ctx);
2433

2534
const char *name;
@@ -29,6 +38,7 @@ struct zcomp_ops {
2938
struct zcomp {
3039
struct zcomp_strm __percpu *stream;
3140
const struct zcomp_ops *ops;
41+
struct zcomp_params *params;
3242
struct hlist_node node;
3343
};
3444

@@ -37,7 +47,7 @@ int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node);
3747
ssize_t zcomp_available_show(const char *comp, char *buf);
3848
bool zcomp_available_algorithm(const char *comp);
3949

40-
struct zcomp *zcomp_create(const char *alg);
50+
struct zcomp *zcomp_create(const char *alg, struct zcomp_params *params);
4151
void zcomp_destroy(struct zcomp *comp);
4252

4353
struct zcomp_strm *zcomp_stream_get(struct zcomp *comp);

drivers/block/zram/zram_drv.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1979,6 +1979,20 @@ static void zram_slot_free_notify(struct block_device *bdev,
19791979
zram_slot_unlock(zram, index);
19801980
}
19811981

1982+
static void zram_comp_params_reset(struct zram *zram)
1983+
{
1984+
u32 prio;
1985+
1986+
for (prio = ZRAM_PRIMARY_COMP; prio < ZRAM_MAX_COMPS; prio++) {
1987+
struct zcomp_params *params = &zram->params[prio];
1988+
1989+
vfree(params->dict);
1990+
params->level = ZCOMP_PARAM_NO_LEVEL;
1991+
params->dict_sz = 0;
1992+
params->dict = NULL;
1993+
}
1994+
}
1995+
19821996
static void zram_destroy_comps(struct zram *zram)
19831997
{
19841998
u32 prio;
@@ -1992,6 +2006,8 @@ static void zram_destroy_comps(struct zram *zram)
19922006
zcomp_destroy(comp);
19932007
zram->num_active_comps--;
19942008
}
2009+
2010+
zram_comp_params_reset(zram);
19952011
}
19962012

19972013
static void zram_reset_device(struct zram *zram)
@@ -2049,7 +2065,8 @@ static ssize_t disksize_store(struct device *dev,
20492065
if (!zram->comp_algs[prio])
20502066
continue;
20512067

2052-
comp = zcomp_create(zram->comp_algs[prio]);
2068+
comp = zcomp_create(zram->comp_algs[prio],
2069+
&zram->params[prio]);
20532070
if (IS_ERR(comp)) {
20542071
pr_err("Cannot initialise %s compressing backend\n",
20552072
zram->comp_algs[prio]);
@@ -2254,6 +2271,7 @@ static int zram_add(void)
22542271
if (ret)
22552272
goto out_cleanup_disk;
22562273

2274+
zram_comp_params_reset(zram);
22572275
comp_algorithm_set(zram, ZRAM_PRIMARY_COMP, default_compressor);
22582276

22592277
zram_debugfs_register(zram);

drivers/block/zram/zram_drv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ struct zram {
107107
struct zram_table_entry *table;
108108
struct zs_pool *mem_pool;
109109
struct zcomp *comps[ZRAM_MAX_COMPS];
110+
struct zcomp_params params[ZRAM_MAX_COMPS];
110111
struct gendisk *disk;
111112
/* Prevent concurrent execution of device init */
112113
struct rw_semaphore init_lock;

0 commit comments

Comments
 (0)