2009-09-21 22:56:53 -06:00
|
|
|
/*
|
2010-06-01 02:01:25 -06:00
|
|
|
* Compressed RAM block device
|
2009-09-21 22:56:53 -06:00
|
|
|
*
|
2010-01-28 08:51:35 -07:00
|
|
|
* Copyright (C) 2008, 2009, 2010 Nitin Gupta
|
2014-01-30 16:45:55 -07:00
|
|
|
* 2012, 2013 Minchan Kim
|
2009-09-21 22:56:53 -06:00
|
|
|
*
|
|
|
|
* This code is released using a dual license strategy: BSD/GPL
|
|
|
|
* You can choose the licence that better fits your requirements.
|
|
|
|
*
|
|
|
|
* Released under the terms of 3-clause BSD License
|
|
|
|
* Released under the terms of GNU General Public License Version 2.0
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-06-01 02:01:25 -06:00
|
|
|
#define KMSG_COMPONENT "zram"
|
2009-09-21 22:56:53 -06:00
|
|
|
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
|
|
|
|
|
2011-01-28 07:59:26 -07:00
|
|
|
#ifdef CONFIG_ZRAM_DEBUG
|
|
|
|
#define DEBUG
|
|
|
|
#endif
|
|
|
|
|
2009-09-21 22:56:53 -06:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/kernel.h>
|
2010-06-23 21:27:09 -06:00
|
|
|
#include <linux/bio.h>
|
2009-09-21 22:56:53 -06:00
|
|
|
#include <linux/bitops.h>
|
|
|
|
#include <linux/blkdev.h>
|
|
|
|
#include <linux/buffer_head.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/genhd.h>
|
|
|
|
#include <linux/highmem.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 02:04:11 -06:00
|
|
|
#include <linux/slab.h>
|
2009-09-21 22:56:53 -06:00
|
|
|
#include <linux/lzo.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/vmalloc.h>
|
|
|
|
|
2010-06-01 02:01:24 -06:00
|
|
|
#include "zram_drv.h"
|
2009-09-21 22:56:53 -06:00
|
|
|
|
|
|
|
/* Globals */
|
2010-06-01 02:01:25 -06:00
|
|
|
static int zram_major;
|
2013-06-06 10:07:29 -06:00
|
|
|
static struct zram *zram_devices;
|
2009-09-21 22:56:53 -06:00
|
|
|
|
|
|
|
/* Module params (documentation at end) */
|
2013-01-01 22:24:13 -07:00
|
|
|
static unsigned int num_devices = 1;
|
2010-08-09 11:26:47 -06:00
|
|
|
|
2013-06-21 18:21:18 -06:00
|
|
|
static inline struct zram *dev_to_zram(struct device *dev)
|
|
|
|
{
|
|
|
|
return (struct zram *)dev_to_disk(dev)->private_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t disksize_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct zram *zram = dev_to_zram(dev);
|
|
|
|
|
|
|
|
return sprintf(buf, "%llu\n", zram->disksize);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t initstate_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct zram *zram = dev_to_zram(dev);
|
|
|
|
|
|
|
|
return sprintf(buf, "%u\n", zram->init_done);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t num_reads_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct zram *zram = dev_to_zram(dev);
|
|
|
|
|
|
|
|
return sprintf(buf, "%llu\n",
|
|
|
|
(u64)atomic64_read(&zram->stats.num_reads));
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t num_writes_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct zram *zram = dev_to_zram(dev);
|
|
|
|
|
|
|
|
return sprintf(buf, "%llu\n",
|
|
|
|
(u64)atomic64_read(&zram->stats.num_writes));
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t invalid_io_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct zram *zram = dev_to_zram(dev);
|
|
|
|
|
|
|
|
return sprintf(buf, "%llu\n",
|
|
|
|
(u64)atomic64_read(&zram->stats.invalid_io));
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t notify_free_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct zram *zram = dev_to_zram(dev);
|
|
|
|
|
|
|
|
return sprintf(buf, "%llu\n",
|
|
|
|
(u64)atomic64_read(&zram->stats.notify_free));
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t zero_pages_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct zram *zram = dev_to_zram(dev);
|
|
|
|
|
2014-01-30 16:46:02 -07:00
|
|
|
return sprintf(buf, "%u\n", atomic_read(&zram->stats.pages_zero));
|
2013-06-21 18:21:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t orig_data_size_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct zram *zram = dev_to_zram(dev);
|
|
|
|
|
|
|
|
return sprintf(buf, "%llu\n",
|
2014-01-30 16:46:02 -07:00
|
|
|
(u64)(atomic_read(&zram->stats.pages_stored)) << PAGE_SHIFT);
|
2013-06-21 18:21:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t compr_data_size_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct zram *zram = dev_to_zram(dev);
|
|
|
|
|
|
|
|
return sprintf(buf, "%llu\n",
|
|
|
|
(u64)atomic64_read(&zram->stats.compr_size));
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t mem_used_total_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
u64 val = 0;
|
|
|
|
struct zram *zram = dev_to_zram(dev);
|
|
|
|
struct zram_meta *meta = zram->meta;
|
|
|
|
|
|
|
|
down_read(&zram->init_lock);
|
|
|
|
if (zram->init_done)
|
|
|
|
val = zs_get_total_size_bytes(meta->mem_pool);
|
|
|
|
up_read(&zram->init_lock);
|
|
|
|
|
|
|
|
return sprintf(buf, "%llu\n", val);
|
|
|
|
}
|
|
|
|
|
2014-01-30 16:46:03 -07:00
|
|
|
/* flag operations needs meta->tb_lock */
|
2013-02-05 16:48:53 -07:00
|
|
|
static int zram_test_flag(struct zram_meta *meta, u32 index,
|
2010-06-01 02:01:25 -06:00
|
|
|
enum zram_pageflags flag)
|
2009-09-21 22:56:53 -06:00
|
|
|
{
|
2013-02-05 16:48:53 -07:00
|
|
|
return meta->table[index].flags & BIT(flag);
|
2009-09-21 22:56:53 -06:00
|
|
|
}
|
|
|
|
|
2013-02-05 16:48:53 -07:00
|
|
|
static void zram_set_flag(struct zram_meta *meta, u32 index,
|
2010-06-01 02:01:25 -06:00
|
|
|
enum zram_pageflags flag)
|
2009-09-21 22:56:53 -06:00
|
|
|
{
|
2013-02-05 16:48:53 -07:00
|
|
|
meta->table[index].flags |= BIT(flag);
|
2009-09-21 22:56:53 -06:00
|
|
|
}
|
|
|
|
|
2013-02-05 16:48:53 -07:00
|
|
|
static void zram_clear_flag(struct zram_meta *meta, u32 index,
|
2010-06-01 02:01:25 -06:00
|
|
|
enum zram_pageflags flag)
|
2009-09-21 22:56:53 -06:00
|
|
|
{
|
2013-02-05 16:48:53 -07:00
|
|
|
meta->table[index].flags &= ~BIT(flag);
|
2009-09-21 22:56:53 -06:00
|
|
|
}
|
|
|
|
|
2013-06-21 18:21:18 -06:00
|
|
|
static inline int is_partial_io(struct bio_vec *bvec)
|
|
|
|
{
|
|
|
|
return bvec->bv_len != PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if request is within bounds and aligned on zram logical blocks.
|
|
|
|
*/
|
|
|
|
static inline int valid_io_request(struct zram *zram, struct bio *bio)
|
|
|
|
{
|
|
|
|
u64 start, end, bound;
|
2013-08-08 12:23:24 -06:00
|
|
|
|
2013-06-21 18:21:18 -06:00
|
|
|
/* unaligned request */
|
2013-10-11 16:44:27 -06:00
|
|
|
if (unlikely(bio->bi_iter.bi_sector &
|
|
|
|
(ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1)))
|
2013-06-21 18:21:18 -06:00
|
|
|
return 0;
|
2013-10-11 16:44:27 -06:00
|
|
|
if (unlikely(bio->bi_iter.bi_size & (ZRAM_LOGICAL_BLOCK_SIZE - 1)))
|
2013-06-21 18:21:18 -06:00
|
|
|
return 0;
|
|
|
|
|
2013-10-11 16:44:27 -06:00
|
|
|
start = bio->bi_iter.bi_sector;
|
|
|
|
end = start + (bio->bi_iter.bi_size >> SECTOR_SHIFT);
|
2013-06-21 18:21:18 -06:00
|
|
|
bound = zram->disksize >> SECTOR_SHIFT;
|
|
|
|
/* out of range range */
|
2013-06-22 08:21:00 -06:00
|
|
|
if (unlikely(start >= bound || end > bound || start > end))
|
2013-06-21 18:21:18 -06:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* I/O request is valid */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void zram_meta_free(struct zram_meta *meta)
|
|
|
|
{
|
|
|
|
zs_destroy_pool(meta->mem_pool);
|
|
|
|
kfree(meta->compress_workmem);
|
|
|
|
free_pages((unsigned long)meta->compress_buffer, 1);
|
|
|
|
vfree(meta->table);
|
|
|
|
kfree(meta);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct zram_meta *zram_meta_alloc(u64 disksize)
|
|
|
|
{
|
|
|
|
size_t num_pages;
|
|
|
|
struct zram_meta *meta = kmalloc(sizeof(*meta), GFP_KERNEL);
|
|
|
|
if (!meta)
|
|
|
|
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;
|
|
|
|
meta->table = vzalloc(num_pages * sizeof(*meta->table));
|
|
|
|
if (!meta->table) {
|
|
|
|
pr_err("Error allocating zram address table\n");
|
|
|
|
goto free_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
meta->mem_pool = zs_create_pool(GFP_NOIO | __GFP_HIGHMEM);
|
|
|
|
if (!meta->mem_pool) {
|
|
|
|
pr_err("Error creating memory pool\n");
|
|
|
|
goto free_table;
|
|
|
|
}
|
|
|
|
|
2014-01-30 16:46:03 -07:00
|
|
|
rwlock_init(&meta->tb_lock);
|
2014-01-30 16:46:06 -07:00
|
|
|
mutex_init(&meta->buffer_lock);
|
2013-06-21 18:21:18 -06:00
|
|
|
return meta;
|
|
|
|
|
|
|
|
free_table:
|
|
|
|
vfree(meta->table);
|
|
|
|
free_buffer:
|
|
|
|
free_pages((unsigned long)meta->compress_buffer, 1);
|
|
|
|
free_workmem:
|
|
|
|
kfree(meta->compress_workmem);
|
|
|
|
free_meta:
|
|
|
|
kfree(meta);
|
|
|
|
meta = NULL;
|
|
|
|
out:
|
|
|
|
return meta;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void update_position(u32 *index, int *offset, struct bio_vec *bvec)
|
|
|
|
{
|
|
|
|
if (*offset + bvec->bv_len >= PAGE_SIZE)
|
|
|
|
(*index)++;
|
|
|
|
*offset = (*offset + bvec->bv_len) % PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
2009-09-21 22:56:53 -06:00
|
|
|
static int page_zero_filled(void *ptr)
|
|
|
|
{
|
|
|
|
unsigned int pos;
|
|
|
|
unsigned long *page;
|
|
|
|
|
|
|
|
page = (unsigned long *)ptr;
|
|
|
|
|
|
|
|
for (pos = 0; pos != PAGE_SIZE / sizeof(*page); pos++) {
|
|
|
|
if (page[pos])
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-06-21 18:21:18 -06:00
|
|
|
static void handle_zero_page(struct bio_vec *bvec)
|
|
|
|
{
|
|
|
|
struct page *page = bvec->bv_page;
|
|
|
|
void *user_mem;
|
|
|
|
|
|
|
|
user_mem = kmap_atomic(page);
|
|
|
|
if (is_partial_io(bvec))
|
|
|
|
memset(user_mem + bvec->bv_offset, 0, bvec->bv_len);
|
|
|
|
else
|
|
|
|
clear_page(user_mem);
|
|
|
|
kunmap_atomic(user_mem);
|
|
|
|
|
|
|
|
flush_dcache_page(page);
|
|
|
|
}
|
|
|
|
|
2014-01-30 16:46:03 -07:00
|
|
|
/* NOTE: caller should hold meta->tb_lock with write-side */
|
2010-06-01 02:01:25 -06:00
|
|
|
static void zram_free_page(struct zram *zram, size_t index)
|
2009-09-21 22:56:53 -06:00
|
|
|
{
|
2013-02-05 16:48:53 -07:00
|
|
|
struct zram_meta *meta = zram->meta;
|
|
|
|
unsigned long handle = meta->table[index].handle;
|
|
|
|
u16 size = meta->table[index].size;
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2012-01-09 15:51:59 -07:00
|
|
|
if (unlikely(!handle)) {
|
2010-01-28 08:43:41 -07:00
|
|
|
/*
|
|
|
|
* No memory is allocated for zero filled pages.
|
|
|
|
* Simply clear zero page flag.
|
|
|
|
*/
|
2013-02-05 16:48:53 -07:00
|
|
|
if (zram_test_flag(meta, index, ZRAM_ZERO)) {
|
|
|
|
zram_clear_flag(meta, index, ZRAM_ZERO);
|
2014-01-30 16:46:02 -07:00
|
|
|
atomic_dec(&zram->stats.pages_zero);
|
2009-09-21 22:56:53 -06:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-08 00:39:27 -06:00
|
|
|
if (unlikely(size > max_zpage_size))
|
2014-01-30 16:46:02 -07:00
|
|
|
atomic_dec(&zram->stats.bad_compress);
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2013-02-05 16:48:53 -07:00
|
|
|
zs_free(meta->mem_pool, handle);
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2012-06-08 00:39:27 -06:00
|
|
|
if (size <= PAGE_SIZE / 2)
|
2014-01-30 16:46:02 -07:00
|
|
|
atomic_dec(&zram->stats.good_compress);
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2013-06-06 10:07:31 -06:00
|
|
|
atomic64_sub(meta->table[index].size, &zram->stats.compr_size);
|
2014-01-30 16:46:02 -07:00
|
|
|
atomic_dec(&zram->stats.pages_stored);
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2013-02-05 16:48:53 -07:00
|
|
|
meta->table[index].handle = 0;
|
|
|
|
meta->table[index].size = 0;
|
2009-09-21 22:56:53 -06:00
|
|
|
}
|
|
|
|
|
2012-10-30 13:40:23 -06:00
|
|
|
static int zram_decompress_page(struct zram *zram, char *mem, u32 index)
|
2009-09-21 22:56:53 -06:00
|
|
|
{
|
2012-10-30 13:40:23 -06:00
|
|
|
int ret = LZO_E_OK;
|
|
|
|
size_t clen = PAGE_SIZE;
|
|
|
|
unsigned char *cmem;
|
2013-02-05 16:48:53 -07:00
|
|
|
struct zram_meta *meta = zram->meta;
|
2014-01-30 16:46:03 -07:00
|
|
|
unsigned long handle;
|
|
|
|
u16 size;
|
|
|
|
|
|
|
|
read_lock(&meta->tb_lock);
|
|
|
|
handle = meta->table[index].handle;
|
|
|
|
size = meta->table[index].size;
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2013-02-05 16:48:53 -07:00
|
|
|
if (!handle || zram_test_flag(meta, index, ZRAM_ZERO)) {
|
2014-01-30 16:46:03 -07:00
|
|
|
read_unlock(&meta->tb_lock);
|
2013-06-06 10:07:30 -06:00
|
|
|
clear_page(mem);
|
2011-06-10 07:28:47 -06:00
|
|
|
return 0;
|
|
|
|
}
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2013-02-05 16:48:53 -07:00
|
|
|
cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_RO);
|
2014-01-30 16:46:03 -07:00
|
|
|
if (size == PAGE_SIZE)
|
2013-06-06 10:07:30 -06:00
|
|
|
copy_page(mem, cmem);
|
2012-10-30 13:40:23 -06:00
|
|
|
else
|
2014-01-30 16:46:03 -07:00
|
|
|
ret = lzo1x_decompress_safe(cmem, size, mem, &clen);
|
2013-02-05 16:48:53 -07:00
|
|
|
zs_unmap_object(meta->mem_pool, handle);
|
2014-01-30 16:46:03 -07:00
|
|
|
read_unlock(&meta->tb_lock);
|
Staging: ramzswap: Support generic I/O requests
Currently, ramzwap devices (/dev/ramzswapX) can only
be used as swap disks since it was hard-coded to consider
only the first request in bio vector.
Now, we iterate over all the segments in an incoming
bio which allows us to handle all kinds of I/O requests.
ramzswap devices can still handle PAGE_SIZE aligned and
multiple of PAGE_SIZE sized I/O requests only. To ensure
that we get always get such requests only, we set following
request_queue attributes to PAGE_SIZE:
- physical_block_size
- logical_block_size
- io_min
- io_opt
Note: physical and logical block sizes were already set
equal to PAGE_SIZE and that seems to be sufficient to get
PAGE_SIZE aligned I/O.
Since we are no longer limited to handling swap requests
only, the next few patches rename ramzswap to zram. So,
the devices will then be called /dev/zram{0, 1, 2, ...}
Usage/Examples:
1) Use as /tmp storage
- mkfs.ext4 /dev/zram0
- mount /dev/zram0 /tmp
2) Use as swap:
- mkswap /dev/zram0
- swapon /dev/zram0 -p 10 # give highest priority to zram0
Performance:
- I/O benchamark done with 'dd' command. Details can be
found here:
http://code.google.com/p/compcache/wiki/zramperf
Summary:
- Maximum read speed (approx):
- ram disk: 1200 MB/sec
- zram disk: 600 MB/sec
- Maximum write speed (approx):
- ram disk: 500 MB/sec
- zram disk: 160 MB/sec
Issues:
- Double caching: We can potentially waste memory by having
two copies of a page -- one in page cache (uncompress) and
second in the device memory (compressed). However, during
reclaim, clean page cache pages are quickly freed, so this
does not seem to be a big problem.
- Stale data: Not all filesystems support issuing 'discard'
requests to underlying block devices. So, if such filesystems
are used over zram devices, we can accumulate lot of stale
data in memory. Even for filesystems to do support discard
(example, ext4), we need to see how effective it is.
- Scalability: There is only one (per-device) de/compression
buffer stats. This can lead to significant contention, especially
when used for generic (non-swap) purposes.
Signed-off-by: Nitin Gupta <ngupta@vflare.org>
Acked-by: Pekka Enberg <penberg@cs.helsinki.fi>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
2010-06-01 02:01:23 -06:00
|
|
|
|
2011-06-10 07:28:47 -06:00
|
|
|
/* Should NEVER happen. Return bio error if it does. */
|
|
|
|
if (unlikely(ret != LZO_E_OK)) {
|
|
|
|
pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
|
2013-06-06 10:07:31 -06:00
|
|
|
atomic64_inc(&zram->stats.failed_reads);
|
2011-06-10 07:28:47 -06:00
|
|
|
return ret;
|
Staging: ramzswap: Support generic I/O requests
Currently, ramzwap devices (/dev/ramzswapX) can only
be used as swap disks since it was hard-coded to consider
only the first request in bio vector.
Now, we iterate over all the segments in an incoming
bio which allows us to handle all kinds of I/O requests.
ramzswap devices can still handle PAGE_SIZE aligned and
multiple of PAGE_SIZE sized I/O requests only. To ensure
that we get always get such requests only, we set following
request_queue attributes to PAGE_SIZE:
- physical_block_size
- logical_block_size
- io_min
- io_opt
Note: physical and logical block sizes were already set
equal to PAGE_SIZE and that seems to be sufficient to get
PAGE_SIZE aligned I/O.
Since we are no longer limited to handling swap requests
only, the next few patches rename ramzswap to zram. So,
the devices will then be called /dev/zram{0, 1, 2, ...}
Usage/Examples:
1) Use as /tmp storage
- mkfs.ext4 /dev/zram0
- mount /dev/zram0 /tmp
2) Use as swap:
- mkswap /dev/zram0
- swapon /dev/zram0 -p 10 # give highest priority to zram0
Performance:
- I/O benchamark done with 'dd' command. Details can be
found here:
http://code.google.com/p/compcache/wiki/zramperf
Summary:
- Maximum read speed (approx):
- ram disk: 1200 MB/sec
- zram disk: 600 MB/sec
- Maximum write speed (approx):
- ram disk: 500 MB/sec
- zram disk: 160 MB/sec
Issues:
- Double caching: We can potentially waste memory by having
two copies of a page -- one in page cache (uncompress) and
second in the device memory (compressed). However, during
reclaim, clean page cache pages are quickly freed, so this
does not seem to be a big problem.
- Stale data: Not all filesystems support issuing 'discard'
requests to underlying block devices. So, if such filesystems
are used over zram devices, we can accumulate lot of stale
data in memory. Even for filesystems to do support discard
(example, ext4), we need to see how effective it is.
- Scalability: There is only one (per-device) de/compression
buffer stats. This can lead to significant contention, especially
when used for generic (non-swap) purposes.
Signed-off-by: Nitin Gupta <ngupta@vflare.org>
Acked-by: Pekka Enberg <penberg@cs.helsinki.fi>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
2010-06-01 02:01:23 -06:00
|
|
|
}
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2011-06-10 07:28:47 -06:00
|
|
|
return 0;
|
2009-09-21 22:56:53 -06:00
|
|
|
}
|
|
|
|
|
2012-10-30 13:40:23 -06:00
|
|
|
static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
|
|
|
|
u32 index, int offset, struct bio *bio)
|
2011-06-10 07:28:48 -06:00
|
|
|
{
|
|
|
|
int ret;
|
2012-10-30 13:40:23 -06:00
|
|
|
struct page *page;
|
|
|
|
unsigned char *user_mem, *uncmem = NULL;
|
2013-02-05 16:48:53 -07:00
|
|
|
struct zram_meta *meta = zram->meta;
|
2012-10-30 13:40:23 -06:00
|
|
|
page = bvec->bv_page;
|
|
|
|
|
2014-01-30 16:46:03 -07:00
|
|
|
read_lock(&meta->tb_lock);
|
2013-02-05 16:48:53 -07:00
|
|
|
if (unlikely(!meta->table[index].handle) ||
|
|
|
|
zram_test_flag(meta, index, ZRAM_ZERO)) {
|
2014-01-30 16:46:03 -07:00
|
|
|
read_unlock(&meta->tb_lock);
|
2012-10-30 13:40:23 -06:00
|
|
|
handle_zero_page(bvec);
|
2011-06-10 07:28:48 -06:00
|
|
|
return 0;
|
|
|
|
}
|
2014-01-30 16:46:03 -07:00
|
|
|
read_unlock(&meta->tb_lock);
|
2011-06-10 07:28:48 -06:00
|
|
|
|
2012-10-30 13:40:23 -06:00
|
|
|
if (is_partial_io(bvec))
|
|
|
|
/* Use a temporary buffer to decompress the page */
|
2013-01-29 19:41:39 -07:00
|
|
|
uncmem = kmalloc(PAGE_SIZE, GFP_NOIO);
|
|
|
|
|
|
|
|
user_mem = kmap_atomic(page);
|
|
|
|
if (!is_partial_io(bvec))
|
2012-10-30 13:40:23 -06:00
|
|
|
uncmem = user_mem;
|
|
|
|
|
|
|
|
if (!uncmem) {
|
|
|
|
pr_info("Unable to allocate temp memory\n");
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out_cleanup;
|
|
|
|
}
|
2011-06-10 07:28:48 -06:00
|
|
|
|
2012-10-30 13:40:23 -06:00
|
|
|
ret = zram_decompress_page(zram, uncmem, index);
|
2011-06-10 07:28:48 -06:00
|
|
|
/* Should NEVER happen. Return bio error if it does. */
|
2013-03-13 01:06:16 -06:00
|
|
|
if (unlikely(ret != LZO_E_OK))
|
2012-10-30 13:40:23 -06:00
|
|
|
goto out_cleanup;
|
2011-06-10 07:28:48 -06:00
|
|
|
|
2012-10-30 13:40:23 -06:00
|
|
|
if (is_partial_io(bvec))
|
|
|
|
memcpy(user_mem + bvec->bv_offset, uncmem + offset,
|
|
|
|
bvec->bv_len);
|
|
|
|
|
|
|
|
flush_dcache_page(page);
|
|
|
|
ret = 0;
|
|
|
|
out_cleanup:
|
|
|
|
kunmap_atomic(user_mem);
|
|
|
|
if (is_partial_io(bvec))
|
|
|
|
kfree(uncmem);
|
|
|
|
return ret;
|
2011-06-10 07:28:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
|
|
|
|
int offset)
|
2009-09-21 22:56:53 -06:00
|
|
|
{
|
2013-01-02 09:53:41 -07:00
|
|
|
int ret = 0;
|
2011-06-10 07:28:47 -06:00
|
|
|
size_t clen;
|
2012-06-08 00:39:25 -06:00
|
|
|
unsigned long handle;
|
2012-06-08 00:39:27 -06:00
|
|
|
struct page *page;
|
2011-06-10 07:28:48 -06:00
|
|
|
unsigned char *user_mem, *cmem, *src, *uncmem = NULL;
|
2013-02-05 16:48:53 -07:00
|
|
|
struct zram_meta *meta = zram->meta;
|
2014-01-30 16:46:06 -07:00
|
|
|
bool locked = false;
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2011-06-10 07:28:47 -06:00
|
|
|
page = bvec->bv_page;
|
2013-02-05 16:48:53 -07:00
|
|
|
src = meta->compress_buffer;
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2011-06-10 07:28:48 -06:00
|
|
|
if (is_partial_io(bvec)) {
|
|
|
|
/*
|
|
|
|
* This is a partial IO. We need to read the full page
|
|
|
|
* before to write the changes.
|
|
|
|
*/
|
2013-01-29 19:41:39 -07:00
|
|
|
uncmem = kmalloc(PAGE_SIZE, GFP_NOIO);
|
2011-06-10 07:28:48 -06:00
|
|
|
if (!uncmem) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
2012-10-30 13:40:23 -06:00
|
|
|
ret = zram_decompress_page(zram, uncmem, index);
|
2013-01-02 09:53:41 -07:00
|
|
|
if (ret)
|
2011-06-10 07:28:48 -06:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2014-01-30 16:46:06 -07:00
|
|
|
mutex_lock(&meta->buffer_lock);
|
|
|
|
locked = true;
|
2011-11-25 08:14:25 -07:00
|
|
|
user_mem = kmap_atomic(page);
|
2011-06-10 07:28:48 -06:00
|
|
|
|
2013-01-02 09:53:41 -07:00
|
|
|
if (is_partial_io(bvec)) {
|
2011-06-10 07:28:48 -06:00
|
|
|
memcpy(uncmem + offset, user_mem + bvec->bv_offset,
|
|
|
|
bvec->bv_len);
|
2013-01-02 09:53:41 -07:00
|
|
|
kunmap_atomic(user_mem);
|
|
|
|
user_mem = NULL;
|
|
|
|
} else {
|
2011-06-10 07:28:48 -06:00
|
|
|
uncmem = user_mem;
|
2013-01-02 09:53:41 -07:00
|
|
|
}
|
2011-06-10 07:28:48 -06:00
|
|
|
|
|
|
|
if (page_zero_filled(uncmem)) {
|
2011-11-25 08:14:25 -07:00
|
|
|
kunmap_atomic(user_mem);
|
2013-07-03 05:10:05 -06:00
|
|
|
/* Free memory associated with this sector now. */
|
2014-01-30 16:46:03 -07:00
|
|
|
write_lock(&zram->meta->tb_lock);
|
2013-07-03 05:10:05 -06:00
|
|
|
zram_free_page(zram, index);
|
2014-01-30 16:46:03 -07:00
|
|
|
zram_set_flag(meta, index, ZRAM_ZERO);
|
|
|
|
write_unlock(&zram->meta->tb_lock);
|
2013-07-03 05:10:05 -06:00
|
|
|
|
2014-01-30 16:46:02 -07:00
|
|
|
atomic_inc(&zram->stats.pages_zero);
|
2011-06-10 07:28:48 -06:00
|
|
|
ret = 0;
|
|
|
|
goto out;
|
2011-06-10 07:28:47 -06:00
|
|
|
}
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2011-06-10 07:28:48 -06:00
|
|
|
ret = lzo1x_1_compress(uncmem, PAGE_SIZE, src, &clen,
|
2013-02-05 16:48:53 -07:00
|
|
|
meta->compress_workmem);
|
2013-01-02 09:53:41 -07:00
|
|
|
if (!is_partial_io(bvec)) {
|
|
|
|
kunmap_atomic(user_mem);
|
|
|
|
user_mem = NULL;
|
|
|
|
uncmem = NULL;
|
|
|
|
}
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2011-06-10 07:28:47 -06:00
|
|
|
if (unlikely(ret != LZO_E_OK)) {
|
|
|
|
pr_err("Compression failed! err=%d\n", ret);
|
2011-06-10 07:28:48 -06:00
|
|
|
goto out;
|
2011-06-10 07:28:47 -06:00
|
|
|
}
|
2009-09-21 22:56:53 -06:00
|
|
|
|
staging: zram: Fix handling of incompressible pages
Change 130f315a (staging: zram: remove special handle of uncompressed page)
introduced a bug in the handling of incompressible pages which resulted in
memory allocation failure for such pages.
When a page expands on compression, say from 4K to 4K+30, we were trying to
do zsmalloc(pool, 4K+30). However, the maximum size which zsmalloc can
allocate is PAGE_SIZE (for obvious reasons), so such allocation requests
always return failure (0).
For a page that has compressed size larger than the original size (this may
happen with already compressed or random data), there is no point storing
the compressed version as that would take more space and would also require
time for decompression when needed again. So, the fix is to store any page,
whose compressed size exceeds a threshold (max_zpage_size), as-it-is i.e.
without compression. Memory required for storing this uncompressed page can
then be requested from zsmalloc which supports PAGE_SIZE sized allocations.
Lastly, the fix checks that we do not attempt to "decompress" the page which
we stored in the uncompressed form -- we just memcpy() out such pages.
Signed-off-by: Nitin Gupta <ngupta@vflare.org>
Reported-by: viechweg@gmail.com
Reported-by: paerley@gmail.com
Reported-by: wu.tommy@gmail.com
Acked-by: Minchan Kim <minchan@kernel.org>
Cc: stable <stable@vger.kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2012-10-10 18:42:18 -06:00
|
|
|
if (unlikely(clen > max_zpage_size)) {
|
2014-01-30 16:46:02 -07:00
|
|
|
atomic_inc(&zram->stats.bad_compress);
|
staging: zram: Fix handling of incompressible pages
Change 130f315a (staging: zram: remove special handle of uncompressed page)
introduced a bug in the handling of incompressible pages which resulted in
memory allocation failure for such pages.
When a page expands on compression, say from 4K to 4K+30, we were trying to
do zsmalloc(pool, 4K+30). However, the maximum size which zsmalloc can
allocate is PAGE_SIZE (for obvious reasons), so such allocation requests
always return failure (0).
For a page that has compressed size larger than the original size (this may
happen with already compressed or random data), there is no point storing
the compressed version as that would take more space and would also require
time for decompression when needed again. So, the fix is to store any page,
whose compressed size exceeds a threshold (max_zpage_size), as-it-is i.e.
without compression. Memory required for storing this uncompressed page can
then be requested from zsmalloc which supports PAGE_SIZE sized allocations.
Lastly, the fix checks that we do not attempt to "decompress" the page which
we stored in the uncompressed form -- we just memcpy() out such pages.
Signed-off-by: Nitin Gupta <ngupta@vflare.org>
Reported-by: viechweg@gmail.com
Reported-by: paerley@gmail.com
Reported-by: wu.tommy@gmail.com
Acked-by: Minchan Kim <minchan@kernel.org>
Cc: stable <stable@vger.kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2012-10-10 18:42:18 -06:00
|
|
|
clen = PAGE_SIZE;
|
2013-01-02 09:53:41 -07:00
|
|
|
src = NULL;
|
|
|
|
if (is_partial_io(bvec))
|
|
|
|
src = uncmem;
|
staging: zram: Fix handling of incompressible pages
Change 130f315a (staging: zram: remove special handle of uncompressed page)
introduced a bug in the handling of incompressible pages which resulted in
memory allocation failure for such pages.
When a page expands on compression, say from 4K to 4K+30, we were trying to
do zsmalloc(pool, 4K+30). However, the maximum size which zsmalloc can
allocate is PAGE_SIZE (for obvious reasons), so such allocation requests
always return failure (0).
For a page that has compressed size larger than the original size (this may
happen with already compressed or random data), there is no point storing
the compressed version as that would take more space and would also require
time for decompression when needed again. So, the fix is to store any page,
whose compressed size exceeds a threshold (max_zpage_size), as-it-is i.e.
without compression. Memory required for storing this uncompressed page can
then be requested from zsmalloc which supports PAGE_SIZE sized allocations.
Lastly, the fix checks that we do not attempt to "decompress" the page which
we stored in the uncompressed form -- we just memcpy() out such pages.
Signed-off-by: Nitin Gupta <ngupta@vflare.org>
Reported-by: viechweg@gmail.com
Reported-by: paerley@gmail.com
Reported-by: wu.tommy@gmail.com
Acked-by: Minchan Kim <minchan@kernel.org>
Cc: stable <stable@vger.kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2012-10-10 18:42:18 -06:00
|
|
|
}
|
Staging: ramzswap: Support generic I/O requests
Currently, ramzwap devices (/dev/ramzswapX) can only
be used as swap disks since it was hard-coded to consider
only the first request in bio vector.
Now, we iterate over all the segments in an incoming
bio which allows us to handle all kinds of I/O requests.
ramzswap devices can still handle PAGE_SIZE aligned and
multiple of PAGE_SIZE sized I/O requests only. To ensure
that we get always get such requests only, we set following
request_queue attributes to PAGE_SIZE:
- physical_block_size
- logical_block_size
- io_min
- io_opt
Note: physical and logical block sizes were already set
equal to PAGE_SIZE and that seems to be sufficient to get
PAGE_SIZE aligned I/O.
Since we are no longer limited to handling swap requests
only, the next few patches rename ramzswap to zram. So,
the devices will then be called /dev/zram{0, 1, 2, ...}
Usage/Examples:
1) Use as /tmp storage
- mkfs.ext4 /dev/zram0
- mount /dev/zram0 /tmp
2) Use as swap:
- mkswap /dev/zram0
- swapon /dev/zram0 -p 10 # give highest priority to zram0
Performance:
- I/O benchamark done with 'dd' command. Details can be
found here:
http://code.google.com/p/compcache/wiki/zramperf
Summary:
- Maximum read speed (approx):
- ram disk: 1200 MB/sec
- zram disk: 600 MB/sec
- Maximum write speed (approx):
- ram disk: 500 MB/sec
- zram disk: 160 MB/sec
Issues:
- Double caching: We can potentially waste memory by having
two copies of a page -- one in page cache (uncompress) and
second in the device memory (compressed). However, during
reclaim, clean page cache pages are quickly freed, so this
does not seem to be a big problem.
- Stale data: Not all filesystems support issuing 'discard'
requests to underlying block devices. So, if such filesystems
are used over zram devices, we can accumulate lot of stale
data in memory. Even for filesystems to do support discard
(example, ext4), we need to see how effective it is.
- Scalability: There is only one (per-device) de/compression
buffer stats. This can lead to significant contention, especially
when used for generic (non-swap) purposes.
Signed-off-by: Nitin Gupta <ngupta@vflare.org>
Acked-by: Pekka Enberg <penberg@cs.helsinki.fi>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
2010-06-01 02:01:23 -06:00
|
|
|
|
2013-02-05 16:48:53 -07:00
|
|
|
handle = zs_malloc(meta->mem_pool, clen);
|
2012-01-09 15:51:59 -07:00
|
|
|
if (!handle) {
|
2013-05-16 12:30:39 -06:00
|
|
|
pr_info("Error allocating memory for compressed page: %u, size=%zu\n",
|
|
|
|
index, clen);
|
2011-06-10 07:28:48 -06:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out;
|
2011-06-10 07:28:47 -06:00
|
|
|
}
|
2013-02-05 16:48:53 -07:00
|
|
|
cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_WO);
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2013-06-06 10:07:30 -06:00
|
|
|
if ((clen == PAGE_SIZE) && !is_partial_io(bvec)) {
|
2013-01-02 09:53:41 -07:00
|
|
|
src = kmap_atomic(page);
|
2013-06-06 10:07:30 -06:00
|
|
|
copy_page(cmem, src);
|
2013-01-02 09:53:41 -07:00
|
|
|
kunmap_atomic(src);
|
2013-06-06 10:07:30 -06:00
|
|
|
} else {
|
|
|
|
memcpy(cmem, src, clen);
|
|
|
|
}
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2013-02-05 16:48:53 -07:00
|
|
|
zs_unmap_object(meta->mem_pool, handle);
|
2012-01-09 15:51:59 -07:00
|
|
|
|
2013-07-03 05:10:05 -06:00
|
|
|
/*
|
|
|
|
* Free memory associated with this sector
|
|
|
|
* before overwriting unused sectors.
|
|
|
|
*/
|
2014-01-30 16:46:03 -07:00
|
|
|
write_lock(&zram->meta->tb_lock);
|
2013-07-03 05:10:05 -06:00
|
|
|
zram_free_page(zram, index);
|
|
|
|
|
2013-02-05 16:48:53 -07:00
|
|
|
meta->table[index].handle = handle;
|
|
|
|
meta->table[index].size = clen;
|
2014-01-30 16:46:03 -07:00
|
|
|
write_unlock(&zram->meta->tb_lock);
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2011-06-10 07:28:47 -06:00
|
|
|
/* Update stats */
|
2013-06-06 10:07:31 -06:00
|
|
|
atomic64_add(clen, &zram->stats.compr_size);
|
2014-01-30 16:46:02 -07:00
|
|
|
atomic_inc(&zram->stats.pages_stored);
|
2011-06-10 07:28:47 -06:00
|
|
|
if (clen <= PAGE_SIZE / 2)
|
2014-01-30 16:46:02 -07:00
|
|
|
atomic_inc(&zram->stats.good_compress);
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2011-06-10 07:28:48 -06:00
|
|
|
out:
|
2014-01-30 16:46:06 -07:00
|
|
|
if (locked)
|
|
|
|
mutex_unlock(&meta->buffer_lock);
|
2013-01-02 09:53:41 -07:00
|
|
|
if (is_partial_io(bvec))
|
|
|
|
kfree(uncmem);
|
|
|
|
|
2011-06-10 07:28:48 -06:00
|
|
|
if (ret)
|
2013-06-06 10:07:31 -06:00
|
|
|
atomic64_inc(&zram->stats.failed_writes);
|
2011-06-10 07:28:48 -06:00
|
|
|
return ret;
|
2011-06-10 07:28:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,
|
2011-06-10 07:28:48 -06:00
|
|
|
int offset, struct bio *bio, int rw)
|
2011-06-10 07:28:47 -06:00
|
|
|
{
|
2011-06-10 07:28:49 -06:00
|
|
|
int ret;
|
2011-06-10 07:28:47 -06:00
|
|
|
|
2014-01-30 16:46:06 -07:00
|
|
|
if (rw == READ)
|
2011-06-10 07:28:49 -06:00
|
|
|
ret = zram_bvec_read(zram, bvec, index, offset, bio);
|
2014-01-30 16:46:06 -07:00
|
|
|
else
|
2011-06-10 07:28:49 -06:00
|
|
|
ret = zram_bvec_write(zram, bvec, index, offset);
|
|
|
|
|
|
|
|
return ret;
|
2011-06-10 07:28:48 -06:00
|
|
|
}
|
|
|
|
|
2013-08-12 00:13:55 -06:00
|
|
|
static void zram_reset_device(struct zram *zram, bool reset_capacity)
|
2011-06-10 07:28:48 -06:00
|
|
|
{
|
2013-06-21 18:21:18 -06:00
|
|
|
size_t index;
|
|
|
|
struct zram_meta *meta;
|
|
|
|
|
2013-06-26 06:28:39 -06:00
|
|
|
down_write(&zram->init_lock);
|
|
|
|
if (!zram->init_done) {
|
|
|
|
up_write(&zram->init_lock);
|
2013-06-21 18:21:18 -06:00
|
|
|
return;
|
2013-06-26 06:28:39 -06:00
|
|
|
}
|
2013-06-21 18:21:18 -06:00
|
|
|
|
|
|
|
meta = zram->meta;
|
|
|
|
zram->init_done = 0;
|
|
|
|
|
|
|
|
/* Free all pages that are still in this zram device */
|
|
|
|
for (index = 0; index < zram->disksize >> PAGE_SHIFT; index++) {
|
|
|
|
unsigned long handle = meta->table[index].handle;
|
|
|
|
if (!handle)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
zs_free(meta->mem_pool, handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
zram_meta_free(zram->meta);
|
|
|
|
zram->meta = NULL;
|
|
|
|
/* Reset stats */
|
|
|
|
memset(&zram->stats, 0, sizeof(zram->stats));
|
|
|
|
|
|
|
|
zram->disksize = 0;
|
2013-08-12 00:13:55 -06:00
|
|
|
if (reset_capacity)
|
|
|
|
set_capacity(zram->disk, 0);
|
2013-06-26 06:28:39 -06:00
|
|
|
up_write(&zram->init_lock);
|
2013-06-21 18:21:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void zram_init_device(struct zram *zram, struct zram_meta *meta)
|
|
|
|
{
|
|
|
|
if (zram->disksize > 2 * (totalram_pages << PAGE_SHIFT)) {
|
|
|
|
pr_info(
|
|
|
|
"There is little point creating a zram of greater than "
|
|
|
|
"twice the size of memory since we expect a 2:1 compression "
|
|
|
|
"ratio. Note that zram uses about 0.1%% of the size of "
|
|
|
|
"the disk when not in use so a huge zram is "
|
|
|
|
"wasteful.\n"
|
|
|
|
"\tMemory Size: %lu kB\n"
|
|
|
|
"\tSize you selected: %llu kB\n"
|
|
|
|
"Continuing anyway ...\n",
|
|
|
|
(totalram_pages << PAGE_SHIFT) >> 10, zram->disksize >> 10
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* zram devices sort of resembles non-rotational disks */
|
|
|
|
queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zram->disk->queue);
|
|
|
|
|
|
|
|
zram->meta = meta;
|
|
|
|
zram->init_done = 1;
|
|
|
|
|
|
|
|
pr_debug("Initialization done!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t disksize_store(struct device *dev,
|
|
|
|
struct device_attribute *attr, const char *buf, size_t len)
|
|
|
|
{
|
|
|
|
u64 disksize;
|
|
|
|
struct zram_meta *meta;
|
|
|
|
struct zram *zram = dev_to_zram(dev);
|
|
|
|
|
|
|
|
disksize = memparse(buf, NULL);
|
|
|
|
if (!disksize)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
disksize = PAGE_ALIGN(disksize);
|
|
|
|
meta = zram_meta_alloc(disksize);
|
|
|
|
down_write(&zram->init_lock);
|
|
|
|
if (zram->init_done) {
|
|
|
|
up_write(&zram->init_lock);
|
|
|
|
zram_meta_free(meta);
|
|
|
|
pr_info("Cannot change disksize for initialized device\n");
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
zram->disksize = disksize;
|
|
|
|
set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
|
|
|
|
zram_init_device(zram, meta);
|
|
|
|
up_write(&zram->init_lock);
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t reset_store(struct device *dev,
|
|
|
|
struct device_attribute *attr, const char *buf, size_t len)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned short do_reset;
|
|
|
|
struct zram *zram;
|
|
|
|
struct block_device *bdev;
|
|
|
|
|
|
|
|
zram = dev_to_zram(dev);
|
|
|
|
bdev = bdget_disk(zram->disk, 0);
|
|
|
|
|
2013-10-30 07:06:32 -06:00
|
|
|
if (!bdev)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2013-06-21 18:21:18 -06:00
|
|
|
/* Do not reset an active device! */
|
2013-11-10 09:43:53 -07:00
|
|
|
if (bdev->bd_holders) {
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto out;
|
|
|
|
}
|
2013-06-21 18:21:18 -06:00
|
|
|
|
|
|
|
ret = kstrtou16(buf, 10, &do_reset);
|
|
|
|
if (ret)
|
2013-11-10 09:43:53 -07:00
|
|
|
goto out;
|
2013-06-21 18:21:18 -06:00
|
|
|
|
2013-11-10 09:43:53 -07:00
|
|
|
if (!do_reset) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2013-06-21 18:21:18 -06:00
|
|
|
|
|
|
|
/* Make sure all pending I/O is finished */
|
2013-10-30 07:06:32 -06:00
|
|
|
fsync_bdev(bdev);
|
2013-11-10 09:43:53 -07:00
|
|
|
bdput(bdev);
|
2013-06-21 18:21:18 -06:00
|
|
|
|
2013-08-12 00:13:55 -06:00
|
|
|
zram_reset_device(zram, true);
|
2013-06-21 18:21:18 -06:00
|
|
|
return len;
|
2013-11-10 09:43:53 -07:00
|
|
|
|
|
|
|
out:
|
|
|
|
bdput(bdev);
|
|
|
|
return ret;
|
2011-06-10 07:28:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __zram_make_request(struct zram *zram, struct bio *bio, int rw)
|
|
|
|
{
|
2013-11-23 18:19:00 -07:00
|
|
|
int offset;
|
2011-06-10 07:28:47 -06:00
|
|
|
u32 index;
|
2013-11-23 18:19:00 -07:00
|
|
|
struct bio_vec bvec;
|
|
|
|
struct bvec_iter iter;
|
2011-06-10 07:28:47 -06:00
|
|
|
|
|
|
|
switch (rw) {
|
|
|
|
case READ:
|
2013-06-06 10:07:31 -06:00
|
|
|
atomic64_inc(&zram->stats.num_reads);
|
2011-06-10 07:28:47 -06:00
|
|
|
break;
|
|
|
|
case WRITE:
|
2013-06-06 10:07:31 -06:00
|
|
|
atomic64_inc(&zram->stats.num_writes);
|
2011-06-10 07:28:47 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-10-11 16:44:27 -06:00
|
|
|
index = bio->bi_iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
|
|
|
|
offset = (bio->bi_iter.bi_sector &
|
|
|
|
(SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT;
|
2011-06-10 07:28:47 -06:00
|
|
|
|
2013-11-23 18:19:00 -07:00
|
|
|
bio_for_each_segment(bvec, bio, iter) {
|
2011-06-10 07:28:48 -06:00
|
|
|
int max_transfer_size = PAGE_SIZE - offset;
|
|
|
|
|
2013-11-23 18:19:00 -07:00
|
|
|
if (bvec.bv_len > max_transfer_size) {
|
2011-06-10 07:28:48 -06:00
|
|
|
/*
|
|
|
|
* zram_bvec_rw() can only make operation on a single
|
|
|
|
* zram page. Split the bio vector.
|
|
|
|
*/
|
|
|
|
struct bio_vec bv;
|
|
|
|
|
2013-11-23 18:19:00 -07:00
|
|
|
bv.bv_page = bvec.bv_page;
|
2011-06-10 07:28:48 -06:00
|
|
|
bv.bv_len = max_transfer_size;
|
2013-11-23 18:19:00 -07:00
|
|
|
bv.bv_offset = bvec.bv_offset;
|
2011-06-10 07:28:48 -06:00
|
|
|
|
|
|
|
if (zram_bvec_rw(zram, &bv, index, offset, bio, rw) < 0)
|
|
|
|
goto out;
|
|
|
|
|
2013-11-23 18:19:00 -07:00
|
|
|
bv.bv_len = bvec.bv_len - max_transfer_size;
|
2011-06-10 07:28:48 -06:00
|
|
|
bv.bv_offset += max_transfer_size;
|
|
|
|
if (zram_bvec_rw(zram, &bv, index+1, 0, bio, rw) < 0)
|
|
|
|
goto out;
|
|
|
|
} else
|
2013-11-23 18:19:00 -07:00
|
|
|
if (zram_bvec_rw(zram, &bvec, index, offset, bio, rw)
|
2011-06-10 07:28:48 -06:00
|
|
|
< 0)
|
|
|
|
goto out;
|
|
|
|
|
2013-11-23 18:19:00 -07:00
|
|
|
update_position(&index, &offset, &bvec);
|
Staging: ramzswap: Support generic I/O requests
Currently, ramzwap devices (/dev/ramzswapX) can only
be used as swap disks since it was hard-coded to consider
only the first request in bio vector.
Now, we iterate over all the segments in an incoming
bio which allows us to handle all kinds of I/O requests.
ramzswap devices can still handle PAGE_SIZE aligned and
multiple of PAGE_SIZE sized I/O requests only. To ensure
that we get always get such requests only, we set following
request_queue attributes to PAGE_SIZE:
- physical_block_size
- logical_block_size
- io_min
- io_opt
Note: physical and logical block sizes were already set
equal to PAGE_SIZE and that seems to be sufficient to get
PAGE_SIZE aligned I/O.
Since we are no longer limited to handling swap requests
only, the next few patches rename ramzswap to zram. So,
the devices will then be called /dev/zram{0, 1, 2, ...}
Usage/Examples:
1) Use as /tmp storage
- mkfs.ext4 /dev/zram0
- mount /dev/zram0 /tmp
2) Use as swap:
- mkswap /dev/zram0
- swapon /dev/zram0 -p 10 # give highest priority to zram0
Performance:
- I/O benchamark done with 'dd' command. Details can be
found here:
http://code.google.com/p/compcache/wiki/zramperf
Summary:
- Maximum read speed (approx):
- ram disk: 1200 MB/sec
- zram disk: 600 MB/sec
- Maximum write speed (approx):
- ram disk: 500 MB/sec
- zram disk: 160 MB/sec
Issues:
- Double caching: We can potentially waste memory by having
two copies of a page -- one in page cache (uncompress) and
second in the device memory (compressed). However, during
reclaim, clean page cache pages are quickly freed, so this
does not seem to be a big problem.
- Stale data: Not all filesystems support issuing 'discard'
requests to underlying block devices. So, if such filesystems
are used over zram devices, we can accumulate lot of stale
data in memory. Even for filesystems to do support discard
(example, ext4), we need to see how effective it is.
- Scalability: There is only one (per-device) de/compression
buffer stats. This can lead to significant contention, especially
when used for generic (non-swap) purposes.
Signed-off-by: Nitin Gupta <ngupta@vflare.org>
Acked-by: Pekka Enberg <penberg@cs.helsinki.fi>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
2010-06-01 02:01:23 -06:00
|
|
|
}
|
2009-09-21 22:56:53 -06:00
|
|
|
|
|
|
|
set_bit(BIO_UPTODATE, &bio->bi_flags);
|
|
|
|
bio_endio(bio, 0);
|
2011-01-22 05:36:15 -07:00
|
|
|
return;
|
2009-09-21 22:56:53 -06:00
|
|
|
|
|
|
|
out:
|
|
|
|
bio_io_error(bio);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-06-01 02:01:25 -06:00
|
|
|
* Handler function for all zram I/O requests.
|
2009-09-21 22:56:53 -06:00
|
|
|
*/
|
2011-09-12 04:12:01 -06:00
|
|
|
static void zram_make_request(struct request_queue *queue, struct bio *bio)
|
2009-09-21 22:56:53 -06:00
|
|
|
{
|
2010-06-01 02:01:25 -06:00
|
|
|
struct zram *zram = queue->queuedata;
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2011-09-06 07:02:11 -06:00
|
|
|
down_read(&zram->init_lock);
|
|
|
|
if (unlikely(!zram->init_done))
|
2013-01-29 19:41:41 -07:00
|
|
|
goto error;
|
2011-09-06 07:02:11 -06:00
|
|
|
|
2010-06-01 02:01:25 -06:00
|
|
|
if (!valid_io_request(zram, bio)) {
|
2013-06-06 10:07:31 -06:00
|
|
|
atomic64_inc(&zram->stats.invalid_io);
|
2013-01-29 19:41:41 -07:00
|
|
|
goto error;
|
2011-02-17 09:11:49 -07:00
|
|
|
}
|
|
|
|
|
2011-06-10 07:28:47 -06:00
|
|
|
__zram_make_request(zram, bio, bio_data_dir(bio));
|
2011-09-06 07:02:11 -06:00
|
|
|
up_read(&zram->init_lock);
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2011-11-04 18:06:58 -06:00
|
|
|
return;
|
2011-09-06 07:02:11 -06:00
|
|
|
|
|
|
|
error:
|
2013-01-29 19:41:41 -07:00
|
|
|
up_read(&zram->init_lock);
|
2011-09-06 07:02:11 -06:00
|
|
|
bio_io_error(bio);
|
2009-09-21 22:56:53 -06:00
|
|
|
}
|
|
|
|
|
2011-09-09 17:01:00 -06:00
|
|
|
static void zram_slot_free_notify(struct block_device *bdev,
|
|
|
|
unsigned long index)
|
2010-05-16 23:32:44 -06:00
|
|
|
{
|
2010-06-01 02:01:25 -06:00
|
|
|
struct zram *zram;
|
2014-01-30 16:46:04 -07:00
|
|
|
struct zram_meta *meta;
|
2010-05-16 23:32:44 -06:00
|
|
|
|
2010-06-01 02:01:25 -06:00
|
|
|
zram = bdev->bd_disk->private_data;
|
2014-01-30 16:46:04 -07:00
|
|
|
meta = zram->meta;
|
2013-08-12 00:13:56 -06:00
|
|
|
|
2014-01-30 16:46:04 -07:00
|
|
|
write_lock(&meta->tb_lock);
|
|
|
|
zram_free_page(zram, index);
|
|
|
|
write_unlock(&meta->tb_lock);
|
|
|
|
atomic64_inc(&zram->stats.notify_free);
|
2010-05-16 23:32:44 -06:00
|
|
|
}
|
|
|
|
|
2010-06-01 02:01:25 -06:00
|
|
|
static const struct block_device_operations zram_devops = {
|
|
|
|
.swap_slot_free_notify = zram_slot_free_notify,
|
2010-05-16 23:32:44 -06:00
|
|
|
.owner = THIS_MODULE
|
2009-09-21 22:56:53 -06:00
|
|
|
};
|
|
|
|
|
2013-06-21 18:21:18 -06:00
|
|
|
static DEVICE_ATTR(disksize, S_IRUGO | S_IWUSR,
|
|
|
|
disksize_show, disksize_store);
|
|
|
|
static DEVICE_ATTR(initstate, S_IRUGO, initstate_show, NULL);
|
|
|
|
static DEVICE_ATTR(reset, S_IWUSR, NULL, reset_store);
|
|
|
|
static DEVICE_ATTR(num_reads, S_IRUGO, num_reads_show, NULL);
|
|
|
|
static DEVICE_ATTR(num_writes, S_IRUGO, num_writes_show, NULL);
|
|
|
|
static DEVICE_ATTR(invalid_io, S_IRUGO, invalid_io_show, NULL);
|
|
|
|
static DEVICE_ATTR(notify_free, S_IRUGO, notify_free_show, NULL);
|
|
|
|
static DEVICE_ATTR(zero_pages, S_IRUGO, zero_pages_show, NULL);
|
|
|
|
static DEVICE_ATTR(orig_data_size, S_IRUGO, orig_data_size_show, NULL);
|
|
|
|
static DEVICE_ATTR(compr_data_size, S_IRUGO, compr_data_size_show, NULL);
|
|
|
|
static DEVICE_ATTR(mem_used_total, S_IRUGO, mem_used_total_show, NULL);
|
|
|
|
|
|
|
|
static struct attribute *zram_disk_attrs[] = {
|
|
|
|
&dev_attr_disksize.attr,
|
|
|
|
&dev_attr_initstate.attr,
|
|
|
|
&dev_attr_reset.attr,
|
|
|
|
&dev_attr_num_reads.attr,
|
|
|
|
&dev_attr_num_writes.attr,
|
|
|
|
&dev_attr_invalid_io.attr,
|
|
|
|
&dev_attr_notify_free.attr,
|
|
|
|
&dev_attr_zero_pages.attr,
|
|
|
|
&dev_attr_orig_data_size.attr,
|
|
|
|
&dev_attr_compr_data_size.attr,
|
|
|
|
&dev_attr_mem_used_total.attr,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct attribute_group zram_disk_attr_group = {
|
|
|
|
.attrs = zram_disk_attrs,
|
|
|
|
};
|
|
|
|
|
2010-06-01 02:01:25 -06:00
|
|
|
static int create_device(struct zram *zram, int device_id)
|
2009-09-21 22:56:53 -06:00
|
|
|
{
|
2013-06-06 10:07:24 -06:00
|
|
|
int ret = -ENOMEM;
|
2010-01-28 08:43:40 -07:00
|
|
|
|
2011-09-06 07:02:11 -06:00
|
|
|
init_rwsem(&zram->init_lock);
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2010-06-01 02:01:25 -06:00
|
|
|
zram->queue = blk_alloc_queue(GFP_KERNEL);
|
|
|
|
if (!zram->queue) {
|
2009-09-21 22:56:53 -06:00
|
|
|
pr_err("Error allocating disk queue for device %d\n",
|
|
|
|
device_id);
|
2010-01-28 08:43:40 -07:00
|
|
|
goto out;
|
2009-09-21 22:56:53 -06:00
|
|
|
}
|
|
|
|
|
2010-06-01 02:01:25 -06:00
|
|
|
blk_queue_make_request(zram->queue, zram_make_request);
|
|
|
|
zram->queue->queuedata = zram;
|
2009-09-21 22:56:53 -06:00
|
|
|
|
|
|
|
/* gendisk structure */
|
2010-06-01 02:01:25 -06:00
|
|
|
zram->disk = alloc_disk(1);
|
|
|
|
if (!zram->disk) {
|
2012-06-07 17:03:47 -06:00
|
|
|
pr_warn("Error allocating disk structure for device %d\n",
|
2009-09-21 22:56:53 -06:00
|
|
|
device_id);
|
2013-06-06 10:07:24 -06:00
|
|
|
goto out_free_queue;
|
2009-09-21 22:56:53 -06:00
|
|
|
}
|
|
|
|
|
2010-06-01 02:01:25 -06:00
|
|
|
zram->disk->major = zram_major;
|
|
|
|
zram->disk->first_minor = device_id;
|
|
|
|
zram->disk->fops = &zram_devops;
|
|
|
|
zram->disk->queue = zram->queue;
|
|
|
|
zram->disk->private_data = zram;
|
|
|
|
snprintf(zram->disk->disk_name, 16, "zram%d", device_id);
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2010-08-09 11:26:47 -06:00
|
|
|
/* Actual capacity set using syfs (/sys/block/zram<id>/disksize */
|
2010-06-01 02:01:25 -06:00
|
|
|
set_capacity(zram->disk, 0);
|
2010-01-28 08:43:39 -07:00
|
|
|
|
Staging: ramzswap: Support generic I/O requests
Currently, ramzwap devices (/dev/ramzswapX) can only
be used as swap disks since it was hard-coded to consider
only the first request in bio vector.
Now, we iterate over all the segments in an incoming
bio which allows us to handle all kinds of I/O requests.
ramzswap devices can still handle PAGE_SIZE aligned and
multiple of PAGE_SIZE sized I/O requests only. To ensure
that we get always get such requests only, we set following
request_queue attributes to PAGE_SIZE:
- physical_block_size
- logical_block_size
- io_min
- io_opt
Note: physical and logical block sizes were already set
equal to PAGE_SIZE and that seems to be sufficient to get
PAGE_SIZE aligned I/O.
Since we are no longer limited to handling swap requests
only, the next few patches rename ramzswap to zram. So,
the devices will then be called /dev/zram{0, 1, 2, ...}
Usage/Examples:
1) Use as /tmp storage
- mkfs.ext4 /dev/zram0
- mount /dev/zram0 /tmp
2) Use as swap:
- mkswap /dev/zram0
- swapon /dev/zram0 -p 10 # give highest priority to zram0
Performance:
- I/O benchamark done with 'dd' command. Details can be
found here:
http://code.google.com/p/compcache/wiki/zramperf
Summary:
- Maximum read speed (approx):
- ram disk: 1200 MB/sec
- zram disk: 600 MB/sec
- Maximum write speed (approx):
- ram disk: 500 MB/sec
- zram disk: 160 MB/sec
Issues:
- Double caching: We can potentially waste memory by having
two copies of a page -- one in page cache (uncompress) and
second in the device memory (compressed). However, during
reclaim, clean page cache pages are quickly freed, so this
does not seem to be a big problem.
- Stale data: Not all filesystems support issuing 'discard'
requests to underlying block devices. So, if such filesystems
are used over zram devices, we can accumulate lot of stale
data in memory. Even for filesystems to do support discard
(example, ext4), we need to see how effective it is.
- Scalability: There is only one (per-device) de/compression
buffer stats. This can lead to significant contention, especially
when used for generic (non-swap) purposes.
Signed-off-by: Nitin Gupta <ngupta@vflare.org>
Acked-by: Pekka Enberg <penberg@cs.helsinki.fi>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
2010-06-01 02:01:23 -06:00
|
|
|
/*
|
|
|
|
* To ensure that we always get PAGE_SIZE aligned
|
|
|
|
* and n*PAGE_SIZED sized I/O requests.
|
|
|
|
*/
|
2010-06-01 02:01:25 -06:00
|
|
|
blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE);
|
2011-01-28 07:58:17 -07:00
|
|
|
blk_queue_logical_block_size(zram->disk->queue,
|
|
|
|
ZRAM_LOGICAL_BLOCK_SIZE);
|
2010-06-01 02:01:25 -06:00
|
|
|
blk_queue_io_min(zram->disk->queue, PAGE_SIZE);
|
|
|
|
blk_queue_io_opt(zram->disk->queue, PAGE_SIZE);
|
2010-01-28 08:43:39 -07:00
|
|
|
|
2010-06-01 02:01:25 -06:00
|
|
|
add_disk(zram->disk);
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2010-08-09 11:26:47 -06:00
|
|
|
ret = sysfs_create_group(&disk_to_dev(zram->disk)->kobj,
|
|
|
|
&zram_disk_attr_group);
|
|
|
|
if (ret < 0) {
|
2012-06-07 17:03:47 -06:00
|
|
|
pr_warn("Error creating sysfs group");
|
2013-06-06 10:07:24 -06:00
|
|
|
goto out_free_disk;
|
2010-08-09 11:26:47 -06:00
|
|
|
}
|
|
|
|
|
2010-06-01 02:01:25 -06:00
|
|
|
zram->init_done = 0;
|
2013-06-06 10:07:24 -06:00
|
|
|
return 0;
|
2010-01-28 08:43:40 -07:00
|
|
|
|
2013-06-06 10:07:24 -06:00
|
|
|
out_free_disk:
|
|
|
|
del_gendisk(zram->disk);
|
|
|
|
put_disk(zram->disk);
|
|
|
|
out_free_queue:
|
|
|
|
blk_cleanup_queue(zram->queue);
|
2010-01-28 08:43:40 -07:00
|
|
|
out:
|
|
|
|
return ret;
|
2009-09-21 22:56:53 -06:00
|
|
|
}
|
|
|
|
|
2010-06-01 02:01:25 -06:00
|
|
|
static void destroy_device(struct zram *zram)
|
2009-09-21 22:56:53 -06:00
|
|
|
{
|
2010-08-09 11:26:47 -06:00
|
|
|
sysfs_remove_group(&disk_to_dev(zram->disk)->kobj,
|
|
|
|
&zram_disk_attr_group);
|
|
|
|
|
2013-10-30 07:13:32 -06:00
|
|
|
del_gendisk(zram->disk);
|
|
|
|
put_disk(zram->disk);
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2013-10-30 07:13:32 -06:00
|
|
|
blk_cleanup_queue(zram->queue);
|
2009-09-21 22:56:53 -06:00
|
|
|
}
|
|
|
|
|
2010-06-01 02:01:25 -06:00
|
|
|
static int __init zram_init(void)
|
2009-09-21 22:56:53 -06:00
|
|
|
{
|
2010-01-28 08:43:40 -07:00
|
|
|
int ret, dev_id;
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2012-02-12 21:04:45 -07:00
|
|
|
if (num_devices > max_num_devices) {
|
2012-06-07 17:03:47 -06:00
|
|
|
pr_warn("Invalid value for num_devices: %u\n",
|
2012-02-12 21:04:45 -07:00
|
|
|
num_devices);
|
2010-01-28 08:43:40 -07:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
2009-09-21 22:56:53 -06:00
|
|
|
}
|
|
|
|
|
2010-06-01 02:01:25 -06:00
|
|
|
zram_major = register_blkdev(0, "zram");
|
|
|
|
if (zram_major <= 0) {
|
2012-06-07 17:03:47 -06:00
|
|
|
pr_warn("Unable to get major number\n");
|
2010-01-28 08:43:40 -07:00
|
|
|
ret = -EBUSY;
|
|
|
|
goto out;
|
2009-09-21 22:56:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate the device array and initialize each one */
|
2012-02-12 21:04:45 -07:00
|
|
|
zram_devices = kzalloc(num_devices * sizeof(struct zram), GFP_KERNEL);
|
2011-07-20 17:05:57 -06:00
|
|
|
if (!zram_devices) {
|
2010-01-28 08:43:40 -07:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto unregister;
|
|
|
|
}
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2012-02-12 21:04:45 -07:00
|
|
|
for (dev_id = 0; dev_id < num_devices; dev_id++) {
|
2011-07-20 17:05:57 -06:00
|
|
|
ret = create_device(&zram_devices[dev_id], dev_id);
|
2010-01-28 08:43:40 -07:00
|
|
|
if (ret)
|
2010-01-11 00:15:53 -07:00
|
|
|
goto free_devices;
|
2010-01-28 08:43:40 -07:00
|
|
|
}
|
|
|
|
|
2013-01-01 22:24:13 -07:00
|
|
|
pr_info("Created %u device(s) ...\n", num_devices);
|
|
|
|
|
2009-09-21 22:56:53 -06:00
|
|
|
return 0;
|
2010-01-28 08:43:40 -07:00
|
|
|
|
2010-01-11 00:15:53 -07:00
|
|
|
free_devices:
|
2010-01-28 08:43:40 -07:00
|
|
|
while (dev_id)
|
2011-07-20 17:05:57 -06:00
|
|
|
destroy_device(&zram_devices[--dev_id]);
|
|
|
|
kfree(zram_devices);
|
2010-01-28 08:43:40 -07:00
|
|
|
unregister:
|
2010-06-01 02:01:25 -06:00
|
|
|
unregister_blkdev(zram_major, "zram");
|
2010-01-28 08:43:40 -07:00
|
|
|
out:
|
2009-09-21 22:56:53 -06:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-06-01 02:01:25 -06:00
|
|
|
static void __exit zram_exit(void)
|
2009-09-21 22:56:53 -06:00
|
|
|
{
|
|
|
|
int i;
|
2010-06-01 02:01:25 -06:00
|
|
|
struct zram *zram;
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2012-02-12 21:04:45 -07:00
|
|
|
for (i = 0; i < num_devices; i++) {
|
2011-07-20 17:05:57 -06:00
|
|
|
zram = &zram_devices[i];
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2010-06-01 02:01:25 -06:00
|
|
|
destroy_device(zram);
|
2013-08-12 00:13:55 -06:00
|
|
|
/*
|
|
|
|
* Shouldn't access zram->disk after destroy_device
|
|
|
|
* because destroy_device already released zram->disk.
|
|
|
|
*/
|
|
|
|
zram_reset_device(zram, false);
|
2009-09-21 22:56:53 -06:00
|
|
|
}
|
|
|
|
|
2010-06-01 02:01:25 -06:00
|
|
|
unregister_blkdev(zram_major, "zram");
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2011-07-20 17:05:57 -06:00
|
|
|
kfree(zram_devices);
|
2009-09-21 22:56:53 -06:00
|
|
|
pr_debug("Cleanup done!\n");
|
|
|
|
}
|
|
|
|
|
2010-06-01 02:01:25 -06:00
|
|
|
module_init(zram_init);
|
|
|
|
module_exit(zram_exit);
|
2009-09-21 22:56:53 -06:00
|
|
|
|
2013-06-21 18:21:18 -06:00
|
|
|
module_param(num_devices, uint, 0);
|
|
|
|
MODULE_PARM_DESC(num_devices, "Number of zram devices");
|
|
|
|
|
2009-09-21 22:56:53 -06:00
|
|
|
MODULE_LICENSE("Dual BSD/GPL");
|
|
|
|
MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");
|
2010-06-01 02:01:25 -06:00
|
|
|
MODULE_DESCRIPTION("Compressed RAM Block Device");
|