Commit 7ac07a26 authored by Sergey Senozhatsky's avatar Sergey Senozhatsky Committed by Andrew Morton

zram: preparation for multi-zcomp support

Patch series "zram: Support multiple compression streams", v5.

This series adds support for multiple compression streams.  The main idea
is that different compression algorithms have different characteristics
and zram may benefit when it uses a combination of algorithms: a default
algorithm that is faster but have lower compression rate and a secondary
algorithm that can use higher compression rate at a price of slower
compression/decompression.

There are several use-case for this functionality:

- huge pages re-compression: zstd or deflate can successfully compress
  huge pages (~50% of huge pages on my synthetic ChromeOS tests), IOW
  pages that lzo was not able to compress.

- idle pages re-compression: idle/cold pages sit in the memory and we
  may reduce zsmalloc memory usage if we recompress those idle pages.

Userspace has a number of ways to control the behavior and impact of zram
recompression: what type of pages should be recompressed, size watermarks,
etc.  Please refer to documentation patch.


This patch (of 13):
			
The patch turns compression streams and compressor algorithm name struct
zram members into arrays, so that we can have multiple compression streams
support (in the next patches).

The patch uses a rather explicit API for compressor selection:

- Get primary (default) compression stream
	zcomp_stream_get(zram->comps[ZRAM_PRIMARY_COMP])
- Get secondary compression stream
	zcomp_stream_get(zram->comps[ZRAM_SECONDARY_COMP])

We use similar API for compression streams put().

At this point we always have just one compression stream,
since CONFIG_ZRAM_MULTI_COMP is not yet defined.

Link: https://lkml.kernel.org/r/20221109115047.2921851-1-senozhatsky@chromium.org
Link: https://lkml.kernel.org/r/20221109115047.2921851-2-senozhatsky@chromium.orgSigned-off-by: default avatarSergey Senozhatsky <senozhatsky@chromium.org>
Acked-by: default avatarMinchan Kim <minchan@kernel.org>
Cc: Minchan Kim <minchan@kernel.org>
Cc: Nitin Gupta <ngupta@vflare.org>
Cc: Suleiman Souhlal <suleiman@google.com>
Cc: Nhat Pham <nphamcs@gmail.com>
Cc: Alexey Romanov <avromanov@sberdevices.ru>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent f036c818
...@@ -206,7 +206,7 @@ void zcomp_destroy(struct zcomp *comp) ...@@ -206,7 +206,7 @@ void zcomp_destroy(struct zcomp *comp)
* case of allocation error, or any other error potentially * case of allocation error, or any other error potentially
* returned by zcomp_init(). * returned by zcomp_init().
*/ */
struct zcomp *zcomp_create(const char *compress) struct zcomp *zcomp_create(const char *alg)
{ {
struct zcomp *comp; struct zcomp *comp;
int error; int error;
...@@ -216,14 +216,14 @@ struct zcomp *zcomp_create(const char *compress) ...@@ -216,14 +216,14 @@ struct zcomp *zcomp_create(const char *compress)
* is not loaded yet. We must do it here, otherwise we are about to * is not loaded yet. We must do it here, otherwise we are about to
* call /sbin/modprobe under CPU hot-plug lock. * call /sbin/modprobe under CPU hot-plug lock.
*/ */
if (!zcomp_available_algorithm(compress)) if (!zcomp_available_algorithm(alg))
return ERR_PTR(-EINVAL); return ERR_PTR(-EINVAL);
comp = kzalloc(sizeof(struct zcomp), GFP_KERNEL); comp = kzalloc(sizeof(struct zcomp), GFP_KERNEL);
if (!comp) if (!comp)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
comp->name = compress; comp->name = alg;
error = zcomp_init(comp); error = zcomp_init(comp);
if (error) { if (error) {
kfree(comp); kfree(comp);
......
...@@ -27,7 +27,7 @@ int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node); ...@@ -27,7 +27,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 *comp); struct zcomp *zcomp_create(const char *alg);
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);
......
...@@ -1004,36 +1004,53 @@ static ssize_t comp_algorithm_show(struct device *dev, ...@@ -1004,36 +1004,53 @@ static ssize_t comp_algorithm_show(struct device *dev,
struct zram *zram = dev_to_zram(dev); struct zram *zram = dev_to_zram(dev);
down_read(&zram->init_lock); down_read(&zram->init_lock);
sz = zcomp_available_show(zram->compressor, buf); sz = zcomp_available_show(zram->comp_algs[ZRAM_PRIMARY_COMP], buf);
up_read(&zram->init_lock); up_read(&zram->init_lock);
return sz; return sz;
} }
static void comp_algorithm_set(struct zram *zram, u32 prio, const char *alg)
{
/* Do not kfree() algs that we didn't allocate, IOW the default ones */
if (zram->comp_algs[prio] != default_compressor)
kfree(zram->comp_algs[prio]);
zram->comp_algs[prio] = alg;
}
static ssize_t comp_algorithm_store(struct device *dev, static ssize_t comp_algorithm_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t len) struct device_attribute *attr, const char *buf, size_t len)
{ {
struct zram *zram = dev_to_zram(dev); struct zram *zram = dev_to_zram(dev);
char compressor[ARRAY_SIZE(zram->compressor)]; char *compressor;
size_t sz; size_t sz;
strscpy(compressor, buf, sizeof(compressor)); sz = strlen(buf);
if (sz >= CRYPTO_MAX_ALG_NAME)
return -E2BIG;
compressor = kstrdup(buf, GFP_KERNEL);
if (!compressor)
return -ENOMEM;
/* ignore trailing newline */ /* ignore trailing newline */
sz = strlen(compressor);
if (sz > 0 && compressor[sz - 1] == '\n') if (sz > 0 && compressor[sz - 1] == '\n')
compressor[sz - 1] = 0x00; compressor[sz - 1] = 0x00;
if (!zcomp_available_algorithm(compressor)) if (!zcomp_available_algorithm(compressor)) {
kfree(compressor);
return -EINVAL; return -EINVAL;
}
down_write(&zram->init_lock); down_write(&zram->init_lock);
if (init_done(zram)) { if (init_done(zram)) {
up_write(&zram->init_lock); up_write(&zram->init_lock);
kfree(compressor);
pr_info("Can't change algorithm for initialized device\n"); pr_info("Can't change algorithm for initialized device\n");
return -EBUSY; return -EBUSY;
} }
strcpy(zram->compressor, compressor); comp_algorithm_set(zram, ZRAM_PRIMARY_COMP, compressor);
up_write(&zram->init_lock); up_write(&zram->init_lock);
return len; return len;
} }
...@@ -1281,7 +1298,7 @@ static int __zram_bvec_read(struct zram *zram, struct page *page, u32 index, ...@@ -1281,7 +1298,7 @@ static int __zram_bvec_read(struct zram *zram, struct page *page, u32 index,
size = zram_get_obj_size(zram, index); size = zram_get_obj_size(zram, index);
if (size != PAGE_SIZE) if (size != PAGE_SIZE)
zstrm = zcomp_stream_get(zram->comp); zstrm = zcomp_stream_get(zram->comps[ZRAM_PRIMARY_COMP]);
src = zs_map_object(zram->mem_pool, handle, ZS_MM_RO); src = zs_map_object(zram->mem_pool, handle, ZS_MM_RO);
if (size == PAGE_SIZE) { if (size == PAGE_SIZE) {
...@@ -1293,7 +1310,7 @@ static int __zram_bvec_read(struct zram *zram, struct page *page, u32 index, ...@@ -1293,7 +1310,7 @@ static int __zram_bvec_read(struct zram *zram, struct page *page, u32 index,
dst = kmap_atomic(page); dst = kmap_atomic(page);
ret = zcomp_decompress(zstrm, src, size, dst); ret = zcomp_decompress(zstrm, src, size, dst);
kunmap_atomic(dst); kunmap_atomic(dst);
zcomp_stream_put(zram->comp); zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP]);
} }
zs_unmap_object(zram->mem_pool, handle); zs_unmap_object(zram->mem_pool, handle);
zram_slot_unlock(zram, index); zram_slot_unlock(zram, index);
...@@ -1360,13 +1377,13 @@ static int __zram_bvec_write(struct zram *zram, struct bio_vec *bvec, ...@@ -1360,13 +1377,13 @@ static int __zram_bvec_write(struct zram *zram, struct bio_vec *bvec,
kunmap_atomic(mem); kunmap_atomic(mem);
compress_again: compress_again:
zstrm = zcomp_stream_get(zram->comp); zstrm = zcomp_stream_get(zram->comps[ZRAM_PRIMARY_COMP]);
src = kmap_atomic(page); src = kmap_atomic(page);
ret = zcomp_compress(zstrm, src, &comp_len); ret = zcomp_compress(zstrm, src, &comp_len);
kunmap_atomic(src); kunmap_atomic(src);
if (unlikely(ret)) { if (unlikely(ret)) {
zcomp_stream_put(zram->comp); zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP]);
pr_err("Compression failed! err=%d\n", ret); pr_err("Compression failed! err=%d\n", ret);
zs_free(zram->mem_pool, handle); zs_free(zram->mem_pool, handle);
return ret; return ret;
...@@ -1394,7 +1411,7 @@ static int __zram_bvec_write(struct zram *zram, struct bio_vec *bvec, ...@@ -1394,7 +1411,7 @@ static int __zram_bvec_write(struct zram *zram, struct bio_vec *bvec,
__GFP_HIGHMEM | __GFP_HIGHMEM |
__GFP_MOVABLE); __GFP_MOVABLE);
if (IS_ERR((void *)handle)) { if (IS_ERR((void *)handle)) {
zcomp_stream_put(zram->comp); zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP]);
atomic64_inc(&zram->stats.writestall); atomic64_inc(&zram->stats.writestall);
handle = zs_malloc(zram->mem_pool, comp_len, handle = zs_malloc(zram->mem_pool, comp_len,
GFP_NOIO | __GFP_HIGHMEM | GFP_NOIO | __GFP_HIGHMEM |
...@@ -1411,14 +1428,14 @@ static int __zram_bvec_write(struct zram *zram, struct bio_vec *bvec, ...@@ -1411,14 +1428,14 @@ static int __zram_bvec_write(struct zram *zram, struct bio_vec *bvec,
* zstrm buffer back. It is necessary that the dereferencing * zstrm buffer back. It is necessary that the dereferencing
* of the zstrm variable below occurs correctly. * of the zstrm variable below occurs correctly.
*/ */
zstrm = zcomp_stream_get(zram->comp); zstrm = zcomp_stream_get(zram->comps[ZRAM_PRIMARY_COMP]);
} }
alloced_pages = zs_get_total_pages(zram->mem_pool); alloced_pages = zs_get_total_pages(zram->mem_pool);
update_used_max(zram, alloced_pages); update_used_max(zram, alloced_pages);
if (zram->limit_pages && alloced_pages > zram->limit_pages) { if (zram->limit_pages && alloced_pages > zram->limit_pages) {
zcomp_stream_put(zram->comp); zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP]);
zs_free(zram->mem_pool, handle); zs_free(zram->mem_pool, handle);
return -ENOMEM; return -ENOMEM;
} }
...@@ -1432,7 +1449,7 @@ static int __zram_bvec_write(struct zram *zram, struct bio_vec *bvec, ...@@ -1432,7 +1449,7 @@ static int __zram_bvec_write(struct zram *zram, struct bio_vec *bvec,
if (comp_len == PAGE_SIZE) if (comp_len == PAGE_SIZE)
kunmap_atomic(src); kunmap_atomic(src);
zcomp_stream_put(zram->comp); zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP]);
zs_unmap_object(zram->mem_pool, handle); zs_unmap_object(zram->mem_pool, handle);
atomic64_add(comp_len, &zram->stats.compr_data_size); atomic64_add(comp_len, &zram->stats.compr_data_size);
out: out:
...@@ -1707,6 +1724,20 @@ static int zram_rw_page(struct block_device *bdev, sector_t sector, ...@@ -1707,6 +1724,20 @@ static int zram_rw_page(struct block_device *bdev, sector_t sector,
return ret; return ret;
} }
static void zram_destroy_comps(struct zram *zram)
{
u32 prio;
for (prio = 0; prio < ZRAM_MAX_COMPS; prio++) {
struct zcomp *comp = zram->comps[prio];
zram->comps[prio] = NULL;
if (!comp)
continue;
zcomp_destroy(comp);
}
}
static void zram_reset_device(struct zram *zram) static void zram_reset_device(struct zram *zram)
{ {
down_write(&zram->init_lock); down_write(&zram->init_lock);
...@@ -1724,11 +1755,11 @@ static void zram_reset_device(struct zram *zram) ...@@ -1724,11 +1755,11 @@ static void zram_reset_device(struct zram *zram)
/* I/O operation under all of CPU are done so let's free */ /* I/O operation under all of CPU are done so let's free */
zram_meta_free(zram, zram->disksize); zram_meta_free(zram, zram->disksize);
zram->disksize = 0; zram->disksize = 0;
zram_destroy_comps(zram);
memset(&zram->stats, 0, sizeof(zram->stats)); memset(&zram->stats, 0, sizeof(zram->stats));
zcomp_destroy(zram->comp);
zram->comp = NULL;
reset_bdev(zram); reset_bdev(zram);
comp_algorithm_set(zram, ZRAM_PRIMARY_COMP, default_compressor);
up_write(&zram->init_lock); up_write(&zram->init_lock);
} }
...@@ -1739,6 +1770,7 @@ static ssize_t disksize_store(struct device *dev, ...@@ -1739,6 +1770,7 @@ static ssize_t disksize_store(struct device *dev,
struct zcomp *comp; struct zcomp *comp;
struct zram *zram = dev_to_zram(dev); struct zram *zram = dev_to_zram(dev);
int err; int err;
u32 prio;
disksize = memparse(buf, NULL); disksize = memparse(buf, NULL);
if (!disksize) if (!disksize)
...@@ -1757,22 +1789,28 @@ static ssize_t disksize_store(struct device *dev, ...@@ -1757,22 +1789,28 @@ static ssize_t disksize_store(struct device *dev,
goto out_unlock; goto out_unlock;
} }
comp = zcomp_create(zram->compressor); for (prio = 0; prio < ZRAM_MAX_COMPS; prio++) {
if (IS_ERR(comp)) { if (!zram->comp_algs[prio])
pr_err("Cannot initialise %s compressing backend\n", continue;
zram->compressor);
err = PTR_ERR(comp); comp = zcomp_create(zram->comp_algs[prio]);
goto out_free_meta; if (IS_ERR(comp)) {
} pr_err("Cannot initialise %s compressing backend\n",
zram->comp_algs[prio]);
err = PTR_ERR(comp);
goto out_free_comps;
}
zram->comp = comp; zram->comps[prio] = comp;
}
zram->disksize = disksize; zram->disksize = disksize;
set_capacity_and_notify(zram->disk, zram->disksize >> SECTOR_SHIFT); set_capacity_and_notify(zram->disk, zram->disksize >> SECTOR_SHIFT);
up_write(&zram->init_lock); up_write(&zram->init_lock);
return len; return len;
out_free_meta: out_free_comps:
zram_destroy_comps(zram);
zram_meta_free(zram, disksize); zram_meta_free(zram, disksize);
out_unlock: out_unlock:
up_write(&zram->init_lock); up_write(&zram->init_lock);
...@@ -1959,7 +1997,7 @@ static int zram_add(void) ...@@ -1959,7 +1997,7 @@ static int zram_add(void)
if (ret) if (ret)
goto out_cleanup_disk; goto out_cleanup_disk;
strscpy(zram->compressor, default_compressor, sizeof(zram->compressor)); zram->comp_algs[ZRAM_PRIMARY_COMP] = default_compressor;
zram_debugfs_register(zram); zram_debugfs_register(zram);
pr_info("Added device: %s\n", zram->disk->disk_name); pr_info("Added device: %s\n", zram->disk->disk_name);
......
...@@ -89,10 +89,20 @@ struct zram_stats { ...@@ -89,10 +89,20 @@ struct zram_stats {
#endif #endif
}; };
#ifdef CONFIG_ZRAM_MULTI_COMP
#define ZRAM_PRIMARY_COMP 0U
#define ZRAM_SECONDARY_COMP 1U
#define ZRAM_MAX_COMPS 4U
#else
#define ZRAM_PRIMARY_COMP 0U
#define ZRAM_SECONDARY_COMP 0U
#define ZRAM_MAX_COMPS 1U
#endif
struct zram { struct zram {
struct zram_table_entry *table; struct zram_table_entry *table;
struct zs_pool *mem_pool; struct zs_pool *mem_pool;
struct zcomp *comp; struct zcomp *comps[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;
...@@ -107,7 +117,7 @@ struct zram { ...@@ -107,7 +117,7 @@ struct zram {
* we can store in a disk. * we can store in a disk.
*/ */
u64 disksize; /* bytes */ u64 disksize; /* bytes */
char compressor[CRYPTO_MAX_ALG_NAME]; const char *comp_algs[ZRAM_MAX_COMPS];
/* /*
* zram is claimed so open request will be failed * zram is claimed so open request will be failed
*/ */
......
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