Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions drivers/block/zram/backend_842.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@ const struct zcomp_ops backend_842 = {
.destroy_ctx = destroy_842,
.setup_params = setup_params_842,
.release_params = release_params_842,
.caps = 0,
.name = "842",
};
3 changes: 3 additions & 0 deletions drivers/block/zram/backend_deflate.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,8 @@ const struct zcomp_ops backend_deflate = {
.destroy_ctx = deflate_destroy,
.setup_params = deflate_setup_params,
.release_params = deflate_release_params,
.caps = ZCOMP_CAP_LEVEL,
.level_min = Z_DEFAULT_COMPRESSION,
.level_max = Z_BEST_COMPRESSION,
.name = "deflate",
};
3 changes: 3 additions & 0 deletions drivers/block/zram/backend_lz4.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,8 @@ const struct zcomp_ops backend_lz4 = {
.destroy_ctx = lz4_destroy,
.setup_params = lz4_setup_params,
.release_params = lz4_release_params,
.caps = ZCOMP_CAP_DICT | ZCOMP_CAP_LEVEL,
.level_min = LZ4_ACCELERATION_DEFAULT,
.level_max = 65535,
.name = "lz4",
};
3 changes: 3 additions & 0 deletions drivers/block/zram/backend_lz4hc.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,8 @@ const struct zcomp_ops backend_lz4hc = {
.destroy_ctx = lz4hc_destroy,
.setup_params = lz4hc_setup_params,
.release_params = lz4hc_release_params,
.caps = ZCOMP_CAP_DICT | ZCOMP_CAP_LEVEL,
.level_min = LZ4HC_MIN_CLEVEL,
.level_max = LZ4HC_MAX_CLEVEL,
.name = "lz4hc",
};
1 change: 1 addition & 0 deletions drivers/block/zram/backend_lzo.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ const struct zcomp_ops backend_lzo = {
.destroy_ctx = lzo_destroy,
.setup_params = lzo_setup_params,
.release_params = lzo_release_params,
.caps = 0,
.name = "lzo",
};
1 change: 1 addition & 0 deletions drivers/block/zram/backend_lzorle.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ const struct zcomp_ops backend_lzorle = {
.destroy_ctx = lzorle_destroy,
.setup_params = lzorle_setup_params,
.release_params = lzorle_release_params,
.caps = 0,
.name = "lzo-rle",
};
4 changes: 3 additions & 1 deletion drivers/block/zram/backend_zstd.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ static int zstd_create(struct zcomp_params *params, struct zcomp_ctx *ctx)
return 0;

error:
zstd_release_params(params);
zstd_destroy(ctx);
return -EINVAL;
}
Expand Down Expand Up @@ -213,5 +212,8 @@ const struct zcomp_ops backend_zstd = {
.destroy_ctx = zstd_destroy,
.setup_params = zstd_setup_params,
.release_params = zstd_release_params,
.caps = ZCOMP_CAP_DICT | ZCOMP_CAP_LEVEL,
.level_min = (int)-ZSTD_TARGETLENGTH_MAX,
.level_max = ZSTD_MAX_CLEVEL,
.name = "zstd",
};
23 changes: 23 additions & 0 deletions drivers/block/zram/zcomp.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#include <linux/cpuhotplug.h>
#include <linux/vmalloc.h>
#include <linux/sysfs.h>
#include <linux/lz4.h>
#include <linux/zlib.h>
#include <linux/zstd.h>

#include "zcomp.h"

Expand Down Expand Up @@ -94,6 +97,26 @@ const char *zcomp_lookup_backend_name(const char *comp)
return NULL;
}

unsigned int zcomp_get_caps(const char *comp)
{
const struct zcomp_ops *backend = lookup_backend_ops(comp);

return backend ? backend->caps : 0;
}

int zcomp_validate_level(const char *comp, s32 level)
{
const struct zcomp_ops *backend = lookup_backend_ops(comp);

if (!backend)
return -EINVAL;
if (!(backend->caps & ZCOMP_CAP_LEVEL))
return -EOPNOTSUPP;
if (level < backend->level_min || level > backend->level_max)
return -EINVAL;
return 0;
}

/* show available compressors */
ssize_t zcomp_available_show(const char *comp, char *buf, ssize_t at)
{
Expand Down
8 changes: 8 additions & 0 deletions drivers/block/zram/zcomp.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

#define ZCOMP_PARAM_NOT_SET INT_MIN

#define ZCOMP_CAP_DICT BIT(0) /* dictionary support */
#define ZCOMP_CAP_LEVEL BIT(1) /* adjustable compression level */

struct deflate_params {
s32 winbits;
};
Expand Down Expand Up @@ -66,6 +69,9 @@ struct zcomp_ops {
int (*setup_params)(struct zcomp_params *params);
void (*release_params)(struct zcomp_params *params);

unsigned int caps;
s32 level_min;
s32 level_max;
const char *name;
};

Expand All @@ -81,6 +87,8 @@ int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node);
int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node);
ssize_t zcomp_available_show(const char *comp, char *buf, ssize_t at);
const char *zcomp_lookup_backend_name(const char *comp);
unsigned int zcomp_get_caps(const char *comp);
int zcomp_validate_level(const char *comp, s32 level);

struct zcomp *zcomp_create(const char *alg, struct zcomp_params *params);
void zcomp_destroy(struct zcomp *comp);
Expand Down
34 changes: 26 additions & 8 deletions drivers/block/zram/zram_drv.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ static size_t huge_class_size;
static const struct block_device_operations zram_devops;

static void slot_free(struct zram *zram, u32 index);
static void comp_params_reset(struct zram *zram, u32 prio);
#define slot_dep_map(zram, index) (&(zram)->table[(index)].dep_map)

static void slot_lock_init(struct zram *zram, u32 index)
Expand Down Expand Up @@ -1681,6 +1682,7 @@ static int __comp_algorithm_store(struct zram *zram, u32 prio, const char *buf)
}

comp_algorithm_set(zram, prio, alg);
comp_params_reset(zram, prio);
return 0;
}

Expand All @@ -1699,21 +1701,24 @@ static int comp_params_store(struct zram *zram, u32 prio, s32 level,
const char *dict_path,
struct deflate_params *deflate_params)
{
void *new_dict = NULL;
ssize_t sz = 0;

comp_params_reset(zram, prio);

if (dict_path) {
sz = kernel_read_file_from_path(dict_path, 0,
&zram->params[prio].dict,
INT_MAX,
NULL,
READING_POLICY);
if (sz < 0)
sz = kernel_read_file_from_path(dict_path, 0, &new_dict,
INT_MAX, NULL, READING_POLICY);
if (sz < 0) {
vfree(new_dict);
return sz;
} else if (sz == 0) {
vfree(new_dict);
return -EINVAL;
}
}

comp_params_reset(zram, prio);
zram->params[prio].dict_sz = sz;
zram->params[prio].dict = new_dict;
zram->params[prio].level = level;
zram->params[prio].deflate.winbits = deflate_params->winbits;
return 0;
Expand Down Expand Up @@ -1794,6 +1799,19 @@ static ssize_t algorithm_params_store(struct device *dev,
return -EINVAL;
}

if (zram->comp_algs[prio]) {
unsigned int caps = zcomp_get_caps(zram->comp_algs[prio]);

if (dict_path && !(caps & ZCOMP_CAP_DICT))
return -EOPNOTSUPP;

if (level != ZCOMP_PARAM_NOT_SET) {
ret = zcomp_validate_level(zram->comp_algs[prio], level);
if (ret)
return ret;
}
}

ret = comp_params_store(zram, prio, level, dict_path, &deflate_params);
return ret ? ret : len;
}
Expand Down
1 change: 1 addition & 0 deletions include/linux/zstd_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,7 @@ ZSTDLIB_API size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict);
#define ZSTD_SEARCHLOG_MIN 1
#define ZSTD_MINMATCH_MAX 7 /* only for ZSTD_fast, other strategies are limited to 6 */
#define ZSTD_MINMATCH_MIN 3 /* only for ZSTD_btopt+, faster strategies are limited to 4 */
#define ZSTD_MAX_CLEVEL 22
#define ZSTD_TARGETLENGTH_MAX ZSTD_BLOCKSIZE_MAX
#define ZSTD_TARGETLENGTH_MIN 0 /* note : comparing this constant to an unsigned results in a tautological test */
#define ZSTD_STRATEGY_MIN ZSTD_fast
Expand Down
1 change: 0 additions & 1 deletion lib/zstd/compress/clevels.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

/*-===== Pre-defined compression levels =====-*/

#define ZSTD_MAX_CLEVEL 22

__attribute__((__unused__))

Expand Down