Commit c99b42c3 authored by Dan Streetman's avatar Dan Streetman Committed by Linus Torvalds

zswap: use charp for zswap param strings

Instead of using a fixed-length string for the zswap params, use charp.
This simplifies the code and uses less memory, as most zswap param strings
will be less than the current maximum length.
Signed-off-by: default avatarDan Streetman <ddstreet@ieee.org>
Cc: Rusty Russell <rusty@rustcorp.com.au>
Cc: Seth Jennings <sjennings@variantweb.net>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 3d9c637f
...@@ -82,33 +82,27 @@ module_param_named(enabled, zswap_enabled, bool, 0644); ...@@ -82,33 +82,27 @@ module_param_named(enabled, zswap_enabled, bool, 0644);
/* Crypto compressor to use */ /* Crypto compressor to use */
#define ZSWAP_COMPRESSOR_DEFAULT "lzo" #define ZSWAP_COMPRESSOR_DEFAULT "lzo"
static char zswap_compressor[CRYPTO_MAX_ALG_NAME] = ZSWAP_COMPRESSOR_DEFAULT; static char *zswap_compressor = ZSWAP_COMPRESSOR_DEFAULT;
static struct kparam_string zswap_compressor_kparam = {
.string = zswap_compressor,
.maxlen = sizeof(zswap_compressor),
};
static int zswap_compressor_param_set(const char *, static int zswap_compressor_param_set(const char *,
const struct kernel_param *); const struct kernel_param *);
static struct kernel_param_ops zswap_compressor_param_ops = { static struct kernel_param_ops zswap_compressor_param_ops = {
.set = zswap_compressor_param_set, .set = zswap_compressor_param_set,
.get = param_get_string, .get = param_get_charp,
.free = param_free_charp,
}; };
module_param_cb(compressor, &zswap_compressor_param_ops, module_param_cb(compressor, &zswap_compressor_param_ops,
&zswap_compressor_kparam, 0644); &zswap_compressor, 0644);
/* Compressed storage zpool to use */ /* Compressed storage zpool to use */
#define ZSWAP_ZPOOL_DEFAULT "zbud" #define ZSWAP_ZPOOL_DEFAULT "zbud"
static char zswap_zpool_type[32 /* arbitrary */] = ZSWAP_ZPOOL_DEFAULT; static char *zswap_zpool_type = ZSWAP_ZPOOL_DEFAULT;
static struct kparam_string zswap_zpool_kparam = {
.string = zswap_zpool_type,
.maxlen = sizeof(zswap_zpool_type),
};
static int zswap_zpool_param_set(const char *, const struct kernel_param *); static int zswap_zpool_param_set(const char *, const struct kernel_param *);
static struct kernel_param_ops zswap_zpool_param_ops = { static struct kernel_param_ops zswap_zpool_param_ops = {
.set = zswap_zpool_param_set, .set = zswap_zpool_param_set,
.get = param_get_string, .get = param_get_charp,
.free = param_free_charp,
}; };
module_param_cb(zpool, &zswap_zpool_param_ops, &zswap_zpool_kparam, 0644); module_param_cb(zpool, &zswap_zpool_param_ops, &zswap_zpool_type, 0644);
/* The maximum percentage of memory that the compressed pool can occupy */ /* The maximum percentage of memory that the compressed pool can occupy */
static unsigned int zswap_max_pool_percent = 20; static unsigned int zswap_max_pool_percent = 20;
...@@ -615,19 +609,29 @@ static struct zswap_pool *zswap_pool_create(char *type, char *compressor) ...@@ -615,19 +609,29 @@ static struct zswap_pool *zswap_pool_create(char *type, char *compressor)
return NULL; return NULL;
} }
static struct zswap_pool *__zswap_pool_create_fallback(void) static __init struct zswap_pool *__zswap_pool_create_fallback(void)
{ {
if (!crypto_has_comp(zswap_compressor, 0, 0)) { if (!crypto_has_comp(zswap_compressor, 0, 0)) {
if (!strcmp(zswap_compressor, ZSWAP_COMPRESSOR_DEFAULT)) {
pr_err("default compressor %s not available\n",
zswap_compressor);
return NULL;
}
pr_err("compressor %s not available, using default %s\n", pr_err("compressor %s not available, using default %s\n",
zswap_compressor, ZSWAP_COMPRESSOR_DEFAULT); zswap_compressor, ZSWAP_COMPRESSOR_DEFAULT);
strncpy(zswap_compressor, ZSWAP_COMPRESSOR_DEFAULT, param_free_charp(&zswap_compressor);
sizeof(zswap_compressor)); zswap_compressor = ZSWAP_COMPRESSOR_DEFAULT;
} }
if (!zpool_has_pool(zswap_zpool_type)) { if (!zpool_has_pool(zswap_zpool_type)) {
if (!strcmp(zswap_zpool_type, ZSWAP_ZPOOL_DEFAULT)) {
pr_err("default zpool %s not available\n",
zswap_zpool_type);
return NULL;
}
pr_err("zpool %s not available, using default %s\n", pr_err("zpool %s not available, using default %s\n",
zswap_zpool_type, ZSWAP_ZPOOL_DEFAULT); zswap_zpool_type, ZSWAP_ZPOOL_DEFAULT);
strncpy(zswap_zpool_type, ZSWAP_ZPOOL_DEFAULT, param_free_charp(&zswap_zpool_type);
sizeof(zswap_zpool_type)); zswap_zpool_type = ZSWAP_ZPOOL_DEFAULT;
} }
return zswap_pool_create(zswap_zpool_type, zswap_compressor); return zswap_pool_create(zswap_zpool_type, zswap_compressor);
...@@ -684,43 +688,39 @@ static void zswap_pool_put(struct zswap_pool *pool) ...@@ -684,43 +688,39 @@ static void zswap_pool_put(struct zswap_pool *pool)
* param callbacks * param callbacks
**********************************/ **********************************/
/* val must be a null-terminated string */
static int __zswap_param_set(const char *val, const struct kernel_param *kp, static int __zswap_param_set(const char *val, const struct kernel_param *kp,
char *type, char *compressor) char *type, char *compressor)
{ {
struct zswap_pool *pool, *put_pool = NULL; struct zswap_pool *pool, *put_pool = NULL;
char str[kp->str->maxlen], *s; char *s = strstrip((char *)val);
int ret; int ret;
/* /* no change required */
* kp is either zswap_zpool_kparam or zswap_compressor_kparam, defined if (!strcmp(s, *(char **)kp->arg))
* at the top of this file, so maxlen is CRYPTO_MAX_ALG_NAME (64) or return 0;
* 32 (arbitrary).
*/
strlcpy(str, val, kp->str->maxlen);
s = strim(str);
/* if this is load-time (pre-init) param setting, /* if this is load-time (pre-init) param setting,
* don't create a pool; that's done during init. * don't create a pool; that's done during init.
*/ */
if (!zswap_init_started) if (!zswap_init_started)
return param_set_copystring(s, kp); return param_set_charp(s, kp);
/* no change required */
if (!strncmp(kp->str->string, s, kp->str->maxlen))
return 0;
if (!type) { if (!type) {
type = s; if (!zpool_has_pool(s)) {
if (!zpool_has_pool(type)) { pr_err("zpool %s not available\n", s);
pr_err("zpool %s not available\n", type);
return -ENOENT; return -ENOENT;
} }
type = s;
} else if (!compressor) { } else if (!compressor) {
compressor = s; if (!crypto_has_comp(s, 0, 0)) {
if (!crypto_has_comp(compressor, 0, 0)) { pr_err("compressor %s not available\n", s);
pr_err("compressor %s not available\n", compressor);
return -ENOENT; return -ENOENT;
} }
compressor = s;
} else {
WARN_ON(1);
return -EINVAL;
} }
spin_lock(&zswap_pools_lock); spin_lock(&zswap_pools_lock);
...@@ -736,7 +736,7 @@ static int __zswap_param_set(const char *val, const struct kernel_param *kp, ...@@ -736,7 +736,7 @@ static int __zswap_param_set(const char *val, const struct kernel_param *kp,
} }
if (pool) if (pool)
ret = param_set_copystring(s, kp); ret = param_set_charp(s, kp);
else else
ret = -EINVAL; ret = -EINVAL;
......
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