zram: use zcomp compressing backends
Do not perform direct LZO compress/decompress calls, initialise and use zcomp LZO backend (single compression stream) instead. [akpm@linux-foundation.org: resolve conflicts with zram-delete-zram_init_device-fix.patch] Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com> Acked-by: Minchan Kim <minchan@kernel.org> Cc: Jerome Marchand <jmarchan@redhat.com> Cc: Nitin Gupta <ngupta@vflare.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
parent
e7e1ef439d
commit
b7ca232ee7
3 changed files with 36 additions and 43 deletions
|
@ -1,3 +1,3 @@
|
||||||
zram-y := zram_drv.o
|
zram-y := zcomp_lzo.o zcomp.o zram_drv.o
|
||||||
|
|
||||||
obj-$(CONFIG_ZRAM) += zram.o
|
obj-$(CONFIG_ZRAM) += zram.o
|
||||||
|
|
|
@ -29,7 +29,6 @@
|
||||||
#include <linux/genhd.h>
|
#include <linux/genhd.h>
|
||||||
#include <linux/highmem.h>
|
#include <linux/highmem.h>
|
||||||
#include <linux/slab.h>
|
#include <linux/slab.h>
|
||||||
#include <linux/lzo.h>
|
|
||||||
#include <linux/string.h>
|
#include <linux/string.h>
|
||||||
#include <linux/vmalloc.h>
|
#include <linux/vmalloc.h>
|
||||||
|
|
||||||
|
@ -38,6 +37,7 @@
|
||||||
/* Globals */
|
/* Globals */
|
||||||
static int zram_major;
|
static int zram_major;
|
||||||
static struct zram *zram_devices;
|
static struct zram *zram_devices;
|
||||||
|
static const char *default_compressor = "lzo";
|
||||||
|
|
||||||
/* Module params (documentation at end) */
|
/* Module params (documentation at end) */
|
||||||
static unsigned int num_devices = 1;
|
static unsigned int num_devices = 1;
|
||||||
|
@ -160,8 +160,6 @@ static inline int valid_io_request(struct zram *zram, struct bio *bio)
|
||||||
static void zram_meta_free(struct zram_meta *meta)
|
static void zram_meta_free(struct zram_meta *meta)
|
||||||
{
|
{
|
||||||
zs_destroy_pool(meta->mem_pool);
|
zs_destroy_pool(meta->mem_pool);
|
||||||
kfree(meta->compress_workmem);
|
|
||||||
free_pages((unsigned long)meta->compress_buffer, 1);
|
|
||||||
vfree(meta->table);
|
vfree(meta->table);
|
||||||
kfree(meta);
|
kfree(meta);
|
||||||
}
|
}
|
||||||
|
@ -173,22 +171,11 @@ static struct zram_meta *zram_meta_alloc(u64 disksize)
|
||||||
if (!meta)
|
if (!meta)
|
||||||
goto out;
|
goto out;
|
||||||
|
|
||||||
meta->compress_workmem = kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
|
|
||||||
if (!meta->compress_workmem)
|
|
||||||
goto free_meta;
|
|
||||||
|
|
||||||
meta->compress_buffer =
|
|
||||||
(void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
|
|
||||||
if (!meta->compress_buffer) {
|
|
||||||
pr_err("Error allocating compressor buffer space\n");
|
|
||||||
goto free_workmem;
|
|
||||||
}
|
|
||||||
|
|
||||||
num_pages = disksize >> PAGE_SHIFT;
|
num_pages = disksize >> PAGE_SHIFT;
|
||||||
meta->table = vzalloc(num_pages * sizeof(*meta->table));
|
meta->table = vzalloc(num_pages * sizeof(*meta->table));
|
||||||
if (!meta->table) {
|
if (!meta->table) {
|
||||||
pr_err("Error allocating zram address table\n");
|
pr_err("Error allocating zram address table\n");
|
||||||
goto free_buffer;
|
goto free_meta;
|
||||||
}
|
}
|
||||||
|
|
||||||
meta->mem_pool = zs_create_pool(GFP_NOIO | __GFP_HIGHMEM);
|
meta->mem_pool = zs_create_pool(GFP_NOIO | __GFP_HIGHMEM);
|
||||||
|
@ -198,15 +185,10 @@ static struct zram_meta *zram_meta_alloc(u64 disksize)
|
||||||
}
|
}
|
||||||
|
|
||||||
rwlock_init(&meta->tb_lock);
|
rwlock_init(&meta->tb_lock);
|
||||||
mutex_init(&meta->buffer_lock);
|
|
||||||
return meta;
|
return meta;
|
||||||
|
|
||||||
free_table:
|
free_table:
|
||||||
vfree(meta->table);
|
vfree(meta->table);
|
||||||
free_buffer:
|
|
||||||
free_pages((unsigned long)meta->compress_buffer, 1);
|
|
||||||
free_workmem:
|
|
||||||
kfree(meta->compress_workmem);
|
|
||||||
free_meta:
|
free_meta:
|
||||||
kfree(meta);
|
kfree(meta);
|
||||||
meta = NULL;
|
meta = NULL;
|
||||||
|
@ -280,8 +262,7 @@ static void zram_free_page(struct zram *zram, size_t index)
|
||||||
|
|
||||||
static int zram_decompress_page(struct zram *zram, char *mem, u32 index)
|
static int zram_decompress_page(struct zram *zram, char *mem, u32 index)
|
||||||
{
|
{
|
||||||
int ret = LZO_E_OK;
|
int ret = 0;
|
||||||
size_t clen = PAGE_SIZE;
|
|
||||||
unsigned char *cmem;
|
unsigned char *cmem;
|
||||||
struct zram_meta *meta = zram->meta;
|
struct zram_meta *meta = zram->meta;
|
||||||
unsigned long handle;
|
unsigned long handle;
|
||||||
|
@ -301,12 +282,12 @@ static int zram_decompress_page(struct zram *zram, char *mem, u32 index)
|
||||||
if (size == PAGE_SIZE)
|
if (size == PAGE_SIZE)
|
||||||
copy_page(mem, cmem);
|
copy_page(mem, cmem);
|
||||||
else
|
else
|
||||||
ret = lzo1x_decompress_safe(cmem, size, mem, &clen);
|
ret = zcomp_decompress(zram->comp, cmem, size, mem);
|
||||||
zs_unmap_object(meta->mem_pool, handle);
|
zs_unmap_object(meta->mem_pool, handle);
|
||||||
read_unlock(&meta->tb_lock);
|
read_unlock(&meta->tb_lock);
|
||||||
|
|
||||||
/* Should NEVER happen. Return bio error if it does. */
|
/* Should NEVER happen. Return bio error if it does. */
|
||||||
if (unlikely(ret != LZO_E_OK)) {
|
if (unlikely(ret)) {
|
||||||
pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
|
pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
|
||||||
atomic64_inc(&zram->stats.failed_reads);
|
atomic64_inc(&zram->stats.failed_reads);
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -349,7 +330,7 @@ static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
|
||||||
|
|
||||||
ret = zram_decompress_page(zram, uncmem, index);
|
ret = zram_decompress_page(zram, uncmem, index);
|
||||||
/* Should NEVER happen. Return bio error if it does. */
|
/* Should NEVER happen. Return bio error if it does. */
|
||||||
if (unlikely(ret != LZO_E_OK))
|
if (unlikely(ret))
|
||||||
goto out_cleanup;
|
goto out_cleanup;
|
||||||
|
|
||||||
if (is_partial_io(bvec))
|
if (is_partial_io(bvec))
|
||||||
|
@ -374,11 +355,10 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
|
||||||
struct page *page;
|
struct page *page;
|
||||||
unsigned char *user_mem, *cmem, *src, *uncmem = NULL;
|
unsigned char *user_mem, *cmem, *src, *uncmem = NULL;
|
||||||
struct zram_meta *meta = zram->meta;
|
struct zram_meta *meta = zram->meta;
|
||||||
|
struct zcomp_strm *zstrm;
|
||||||
bool locked = false;
|
bool locked = false;
|
||||||
|
|
||||||
page = bvec->bv_page;
|
page = bvec->bv_page;
|
||||||
src = meta->compress_buffer;
|
|
||||||
|
|
||||||
if (is_partial_io(bvec)) {
|
if (is_partial_io(bvec)) {
|
||||||
/*
|
/*
|
||||||
* This is a partial IO. We need to read the full page
|
* This is a partial IO. We need to read the full page
|
||||||
|
@ -394,7 +374,7 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
mutex_lock(&meta->buffer_lock);
|
zstrm = zcomp_strm_find(zram->comp);
|
||||||
locked = true;
|
locked = true;
|
||||||
user_mem = kmap_atomic(page);
|
user_mem = kmap_atomic(page);
|
||||||
|
|
||||||
|
@ -420,22 +400,20 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = lzo1x_1_compress(uncmem, PAGE_SIZE, src, &clen,
|
ret = zcomp_compress(zram->comp, zstrm, uncmem, &clen);
|
||||||
meta->compress_workmem);
|
|
||||||
if (!is_partial_io(bvec)) {
|
if (!is_partial_io(bvec)) {
|
||||||
kunmap_atomic(user_mem);
|
kunmap_atomic(user_mem);
|
||||||
user_mem = NULL;
|
user_mem = NULL;
|
||||||
uncmem = NULL;
|
uncmem = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (unlikely(ret != LZO_E_OK)) {
|
if (unlikely(ret)) {
|
||||||
pr_err("Compression failed! err=%d\n", ret);
|
pr_err("Compression failed! err=%d\n", ret);
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
src = zstrm->buffer;
|
||||||
if (unlikely(clen > max_zpage_size)) {
|
if (unlikely(clen > max_zpage_size)) {
|
||||||
clen = PAGE_SIZE;
|
clen = PAGE_SIZE;
|
||||||
src = NULL;
|
|
||||||
if (is_partial_io(bvec))
|
if (is_partial_io(bvec))
|
||||||
src = uncmem;
|
src = uncmem;
|
||||||
}
|
}
|
||||||
|
@ -457,6 +435,8 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
|
||||||
memcpy(cmem, src, clen);
|
memcpy(cmem, src, clen);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
zcomp_strm_release(zram->comp, zstrm);
|
||||||
|
locked = false;
|
||||||
zs_unmap_object(meta->mem_pool, handle);
|
zs_unmap_object(meta->mem_pool, handle);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -475,10 +455,9 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
|
||||||
atomic64_inc(&zram->stats.pages_stored);
|
atomic64_inc(&zram->stats.pages_stored);
|
||||||
out:
|
out:
|
||||||
if (locked)
|
if (locked)
|
||||||
mutex_unlock(&meta->buffer_lock);
|
zcomp_strm_release(zram->comp, zstrm);
|
||||||
if (is_partial_io(bvec))
|
if (is_partial_io(bvec))
|
||||||
kfree(uncmem);
|
kfree(uncmem);
|
||||||
|
|
||||||
if (ret)
|
if (ret)
|
||||||
atomic64_inc(&zram->stats.failed_writes);
|
atomic64_inc(&zram->stats.failed_writes);
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -522,6 +501,7 @@ static void zram_reset_device(struct zram *zram, bool reset_capacity)
|
||||||
zs_free(meta->mem_pool, handle);
|
zs_free(meta->mem_pool, handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
zcomp_destroy(zram->comp);
|
||||||
zram_meta_free(zram->meta);
|
zram_meta_free(zram->meta);
|
||||||
zram->meta = NULL;
|
zram->meta = NULL;
|
||||||
/* Reset stats */
|
/* Reset stats */
|
||||||
|
@ -539,6 +519,7 @@ static ssize_t disksize_store(struct device *dev,
|
||||||
u64 disksize;
|
u64 disksize;
|
||||||
struct zram_meta *meta;
|
struct zram_meta *meta;
|
||||||
struct zram *zram = dev_to_zram(dev);
|
struct zram *zram = dev_to_zram(dev);
|
||||||
|
int err;
|
||||||
|
|
||||||
disksize = memparse(buf, NULL);
|
disksize = memparse(buf, NULL);
|
||||||
if (!disksize)
|
if (!disksize)
|
||||||
|
@ -551,10 +532,17 @@ static ssize_t disksize_store(struct device *dev,
|
||||||
|
|
||||||
down_write(&zram->init_lock);
|
down_write(&zram->init_lock);
|
||||||
if (init_done(zram)) {
|
if (init_done(zram)) {
|
||||||
zram_meta_free(meta);
|
|
||||||
up_write(&zram->init_lock);
|
|
||||||
pr_info("Cannot change disksize for initialized device\n");
|
pr_info("Cannot change disksize for initialized device\n");
|
||||||
return -EBUSY;
|
err = -EBUSY;
|
||||||
|
goto out_free_meta;
|
||||||
|
}
|
||||||
|
|
||||||
|
zram->comp = zcomp_create(default_compressor);
|
||||||
|
if (!zram->comp) {
|
||||||
|
pr_info("Cannot initialise %s compressing backend\n",
|
||||||
|
default_compressor);
|
||||||
|
err = -EINVAL;
|
||||||
|
goto out_free_meta;
|
||||||
}
|
}
|
||||||
|
|
||||||
zram->meta = meta;
|
zram->meta = meta;
|
||||||
|
@ -563,6 +551,11 @@ static ssize_t disksize_store(struct device *dev,
|
||||||
up_write(&zram->init_lock);
|
up_write(&zram->init_lock);
|
||||||
|
|
||||||
return len;
|
return len;
|
||||||
|
|
||||||
|
out_free_meta:
|
||||||
|
up_write(&zram->init_lock);
|
||||||
|
zram_meta_free(meta);
|
||||||
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ssize_t reset_store(struct device *dev,
|
static ssize_t reset_store(struct device *dev,
|
||||||
|
|
|
@ -16,9 +16,10 @@
|
||||||
#define _ZRAM_DRV_H_
|
#define _ZRAM_DRV_H_
|
||||||
|
|
||||||
#include <linux/spinlock.h>
|
#include <linux/spinlock.h>
|
||||||
#include <linux/mutex.h>
|
|
||||||
#include <linux/zsmalloc.h>
|
#include <linux/zsmalloc.h>
|
||||||
|
|
||||||
|
#include "zcomp.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Some arbitrary value. This is just to catch
|
* Some arbitrary value. This is just to catch
|
||||||
* invalid value for num_devices module parameter.
|
* invalid value for num_devices module parameter.
|
||||||
|
@ -81,17 +82,16 @@ struct zram_stats {
|
||||||
|
|
||||||
struct zram_meta {
|
struct zram_meta {
|
||||||
rwlock_t tb_lock; /* protect table */
|
rwlock_t tb_lock; /* protect table */
|
||||||
void *compress_workmem;
|
|
||||||
void *compress_buffer;
|
|
||||||
struct table *table;
|
struct table *table;
|
||||||
struct zs_pool *mem_pool;
|
struct zs_pool *mem_pool;
|
||||||
struct mutex buffer_lock; /* protect compress buffers */
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct zram {
|
struct zram {
|
||||||
struct zram_meta *meta;
|
struct zram_meta *meta;
|
||||||
struct request_queue *queue;
|
struct request_queue *queue;
|
||||||
struct gendisk *disk;
|
struct gendisk *disk;
|
||||||
|
struct zcomp *comp;
|
||||||
|
|
||||||
/* Prevent concurrent execution of device init, reset and R/W request */
|
/* Prevent concurrent execution of device init, reset and R/W request */
|
||||||
struct rw_semaphore init_lock;
|
struct rw_semaphore init_lock;
|
||||||
/*
|
/*
|
||||||
|
|
Loading…
Reference in a new issue