2009-09-22 04:56:53 +00:00
|
|
|
/*
|
2010-06-01 08:01:25 +00:00
|
|
|
* Compressed RAM block device
|
2009-09-22 04:56:53 +00:00
|
|
|
*
|
2010-01-28 15:51:35 +00:00
|
|
|
* Copyright (C) 2008, 2009, 2010 Nitin Gupta
|
2014-01-30 23:45:55 +00:00
|
|
|
* 2012, 2013 Minchan Kim
|
2009-09-22 04:56:53 +00: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 08:01:25 +00:00
|
|
|
#define KMSG_COMPONENT "zram"
|
2009-09-22 04:56:53 +00:00
|
|
|
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
|
|
|
|
|
2011-01-28 14:59:26 +00:00
|
|
|
#ifdef CONFIG_ZRAM_DEBUG
|
|
|
|
#define DEBUG
|
|
|
|
#endif
|
|
|
|
|
2009-09-22 04:56:53 +00:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/kernel.h>
|
2010-06-24 03:27:09 +00:00
|
|
|
#include <linux/bio.h>
|
2009-09-22 04:56:53 +00: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 08:04:11 +00:00
|
|
|
#include <linux/slab.h>
|
2009-09-22 04:56:53 +00:00
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/vmalloc.h>
|
2014-04-07 22:38:20 +00:00
|
|
|
#include <linux/err.h>
|
2009-09-22 04:56:53 +00:00
|
|
|
|
2010-06-01 08:01:24 +00:00
|
|
|
#include "zram_drv.h"
|
2009-09-22 04:56:53 +00:00
|
|
|
|
|
|
|
/* Globals */
|
2010-06-01 08:01:25 +00:00
|
|
|
static int zram_major;
|
2013-06-06 16:07:29 +00:00
|
|
|
static struct zram *zram_devices;
|
2014-04-07 22:38:12 +00:00
|
|
|
static const char *default_compressor = "lzo";
|
2009-09-22 04:56:53 +00:00
|
|
|
|
|
|
|
/* Module params (documentation at end) */
|
2013-01-02 05:24:13 +00:00
|
|
|
static unsigned int num_devices = 1;
|
2010-08-09 17:26:47 +00:00
|
|
|
|
2014-04-07 22:38:04 +00:00
|
|
|
#define ZRAM_ATTR_RO(name) \
|
|
|
|
static ssize_t zram_attr_##name##_show(struct device *d, \
|
|
|
|
struct device_attribute *attr, char *b) \
|
|
|
|
{ \
|
|
|
|
struct zram *zram = dev_to_zram(d); \
|
|
|
|
return sprintf(b, "%llu\n", \
|
|
|
|
(u64)atomic64_read(&zram->stats.name)); \
|
|
|
|
} \
|
|
|
|
static struct device_attribute dev_attr_##name = \
|
|
|
|
__ATTR(name, S_IRUGO, zram_attr_##name##_show, NULL);
|
|
|
|
|
2014-04-07 22:38:00 +00:00
|
|
|
static inline int init_done(struct zram *zram)
|
|
|
|
{
|
|
|
|
return zram->meta != NULL;
|
|
|
|
}
|
|
|
|
|
2013-06-22 00:21:18 +00: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)
|
|
|
|
{
|
2014-04-07 22:38:04 +00:00
|
|
|
u32 val;
|
2013-06-22 00:21:18 +00:00
|
|
|
struct zram *zram = dev_to_zram(dev);
|
|
|
|
|
2014-04-07 22:38:04 +00:00
|
|
|
down_read(&zram->init_lock);
|
|
|
|
val = init_done(zram);
|
|
|
|
up_read(&zram->init_lock);
|
2013-06-22 00:21:18 +00:00
|
|
|
|
2014-04-07 22:38:04 +00:00
|
|
|
return sprintf(buf, "%u\n", val);
|
2013-06-22 00:21:18 +00: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-04-07 22:38:03 +00:00
|
|
|
(u64)(atomic64_read(&zram->stats.pages_stored)) << PAGE_SHIFT);
|
2013-06-22 00:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2014-04-07 22:38:00 +00:00
|
|
|
if (init_done(zram))
|
2013-06-22 00:21:18 +00:00
|
|
|
val = zs_get_total_size_bytes(meta->mem_pool);
|
|
|
|
up_read(&zram->init_lock);
|
|
|
|
|
|
|
|
return sprintf(buf, "%llu\n", val);
|
|
|
|
}
|
|
|
|
|
zram: add multi stream functionality
Existing zram (zcomp) implementation has only one compression stream
(buffer and algorithm private part), so in order to prevent data
corruption only one write (compress operation) can use this compression
stream, forcing all concurrent write operations to wait for stream lock
to be released. This patch changes zcomp to keep a compression streams
list of user-defined size (via sysfs device attr). Each write operation
still exclusively holds compression stream, the difference is that we
can have N write operations (depending on size of streams list)
executing in parallel. See TEST section later in commit message for
performance data.
Introduce struct zcomp_strm_multi and a set of functions to manage
zcomp_strm stream access. zcomp_strm_multi has a list of idle
zcomp_strm structs, spinlock to protect idle list and wait queue, making
it possible to perform parallel compressions.
The following set of functions added:
- zcomp_strm_multi_find()/zcomp_strm_multi_release()
find and release a compression stream, implement required locking
- zcomp_strm_multi_create()/zcomp_strm_multi_destroy()
create and destroy zcomp_strm_multi
zcomp ->strm_find() and ->strm_release() callbacks are set during
initialisation to zcomp_strm_multi_find()/zcomp_strm_multi_release()
correspondingly.
Each time zcomp issues a zcomp_strm_multi_find() call, the following set
of operations performed:
- spin lock strm_lock
- if idle list is not empty, remove zcomp_strm from idle list, spin
unlock and return zcomp stream pointer to caller
- if idle list is empty, current adds itself to wait queue. it will be
awaken by zcomp_strm_multi_release() caller.
zcomp_strm_multi_release():
- spin lock strm_lock
- add zcomp stream to idle list
- spin unlock, wake up sleeper
Minchan Kim reported that spinlock-based locking scheme has demonstrated
a severe perfomance regression for single compression stream case,
comparing to mutex-based (see https://lkml.org/lkml/2014/2/18/16)
base spinlock mutex
==Initial write ==Initial write ==Initial write
records: 5 records: 5 records: 5
avg: 1642424.35 avg: 699610.40 avg: 1655583.71
std: 39890.95(2.43%) std: 232014.19(33.16%) std: 52293.96
max: 1690170.94 max: 1163473.45 max: 1697164.75
min: 1568669.52 min: 573429.88 min: 1553410.23
==Rewrite ==Rewrite ==Rewrite
records: 5 records: 5 records: 5
avg: 1611775.39 avg: 501406.64 avg: 1684419.11
std: 17144.58(1.06%) std: 15354.41(3.06%) std: 18367.42
max: 1641800.95 max: 531356.78 max: 1706445.84
min: 1593515.27 min: 488817.78 min: 1655335.73
When only one compression stream available, mutex with spin on owner
tends to perform much better than frequent wait_event()/wake_up(). This
is why single stream implemented as a special case with mutex locking.
Introduce and document zram device attribute max_comp_streams. This
attr shows and stores current zcomp's max number of zcomp streams
(max_strm). Extend zcomp's zcomp_create() with `max_strm' parameter.
`max_strm' limits the number of zcomp_strm structs in compression
backend's idle list (max_comp_streams).
max_comp_streams used during initialisation as follows:
-- passing to zcomp_create() max_strm equals to 1 will initialise zcomp
using single compression stream zcomp_strm_single (mutex-based locking).
-- passing to zcomp_create() max_strm greater than 1 will initialise zcomp
using multi compression stream zcomp_strm_multi (spinlock-based locking).
default max_comp_streams value is 1, meaning that zram with single stream
will be initialised.
Later patch will introduce configuration knob to change max_comp_streams
on already initialised and used zcomp.
TEST
iozone -t 3 -R -r 16K -s 60M -I +Z
test base 1 strm (mutex) 3 strm (spinlock)
-----------------------------------------------------------------------
Initial write 589286.78 583518.39 718011.05
Rewrite 604837.97 596776.38 1515125.72
Random write 584120.11 595714.58 1388850.25
Pwrite 535731.17 541117.38 739295.27
Fwrite 1418083.88 1478612.72 1484927.06
Usage example:
set max_comp_streams to 4
echo 4 > /sys/block/zram0/max_comp_streams
show current max_comp_streams (default value is 1).
cat /sys/block/zram0/max_comp_streams
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>
2014-04-07 22:38:14 +00:00
|
|
|
static ssize_t max_comp_streams_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
int val;
|
|
|
|
struct zram *zram = dev_to_zram(dev);
|
|
|
|
|
|
|
|
down_read(&zram->init_lock);
|
|
|
|
val = zram->max_comp_streams;
|
|
|
|
up_read(&zram->init_lock);
|
|
|
|
|
|
|
|
return sprintf(buf, "%d\n", val);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t max_comp_streams_store(struct device *dev,
|
|
|
|
struct device_attribute *attr, const char *buf, size_t len)
|
|
|
|
{
|
|
|
|
int num;
|
|
|
|
struct zram *zram = dev_to_zram(dev);
|
2014-04-07 22:38:21 +00:00
|
|
|
int ret;
|
zram: add multi stream functionality
Existing zram (zcomp) implementation has only one compression stream
(buffer and algorithm private part), so in order to prevent data
corruption only one write (compress operation) can use this compression
stream, forcing all concurrent write operations to wait for stream lock
to be released. This patch changes zcomp to keep a compression streams
list of user-defined size (via sysfs device attr). Each write operation
still exclusively holds compression stream, the difference is that we
can have N write operations (depending on size of streams list)
executing in parallel. See TEST section later in commit message for
performance data.
Introduce struct zcomp_strm_multi and a set of functions to manage
zcomp_strm stream access. zcomp_strm_multi has a list of idle
zcomp_strm structs, spinlock to protect idle list and wait queue, making
it possible to perform parallel compressions.
The following set of functions added:
- zcomp_strm_multi_find()/zcomp_strm_multi_release()
find and release a compression stream, implement required locking
- zcomp_strm_multi_create()/zcomp_strm_multi_destroy()
create and destroy zcomp_strm_multi
zcomp ->strm_find() and ->strm_release() callbacks are set during
initialisation to zcomp_strm_multi_find()/zcomp_strm_multi_release()
correspondingly.
Each time zcomp issues a zcomp_strm_multi_find() call, the following set
of operations performed:
- spin lock strm_lock
- if idle list is not empty, remove zcomp_strm from idle list, spin
unlock and return zcomp stream pointer to caller
- if idle list is empty, current adds itself to wait queue. it will be
awaken by zcomp_strm_multi_release() caller.
zcomp_strm_multi_release():
- spin lock strm_lock
- add zcomp stream to idle list
- spin unlock, wake up sleeper
Minchan Kim reported that spinlock-based locking scheme has demonstrated
a severe perfomance regression for single compression stream case,
comparing to mutex-based (see https://lkml.org/lkml/2014/2/18/16)
base spinlock mutex
==Initial write ==Initial write ==Initial write
records: 5 records: 5 records: 5
avg: 1642424.35 avg: 699610.40 avg: 1655583.71
std: 39890.95(2.43%) std: 232014.19(33.16%) std: 52293.96
max: 1690170.94 max: 1163473.45 max: 1697164.75
min: 1568669.52 min: 573429.88 min: 1553410.23
==Rewrite ==Rewrite ==Rewrite
records: 5 records: 5 records: 5
avg: 1611775.39 avg: 501406.64 avg: 1684419.11
std: 17144.58(1.06%) std: 15354.41(3.06%) std: 18367.42
max: 1641800.95 max: 531356.78 max: 1706445.84
min: 1593515.27 min: 488817.78 min: 1655335.73
When only one compression stream available, mutex with spin on owner
tends to perform much better than frequent wait_event()/wake_up(). This
is why single stream implemented as a special case with mutex locking.
Introduce and document zram device attribute max_comp_streams. This
attr shows and stores current zcomp's max number of zcomp streams
(max_strm). Extend zcomp's zcomp_create() with `max_strm' parameter.
`max_strm' limits the number of zcomp_strm structs in compression
backend's idle list (max_comp_streams).
max_comp_streams used during initialisation as follows:
-- passing to zcomp_create() max_strm equals to 1 will initialise zcomp
using single compression stream zcomp_strm_single (mutex-based locking).
-- passing to zcomp_create() max_strm greater than 1 will initialise zcomp
using multi compression stream zcomp_strm_multi (spinlock-based locking).
default max_comp_streams value is 1, meaning that zram with single stream
will be initialised.
Later patch will introduce configuration knob to change max_comp_streams
on already initialised and used zcomp.
TEST
iozone -t 3 -R -r 16K -s 60M -I +Z
test base 1 strm (mutex) 3 strm (spinlock)
-----------------------------------------------------------------------
Initial write 589286.78 583518.39 718011.05
Rewrite 604837.97 596776.38 1515125.72
Random write 584120.11 595714.58 1388850.25
Pwrite 535731.17 541117.38 739295.27
Fwrite 1418083.88 1478612.72 1484927.06
Usage example:
set max_comp_streams to 4
echo 4 > /sys/block/zram0/max_comp_streams
show current max_comp_streams (default value is 1).
cat /sys/block/zram0/max_comp_streams
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>
2014-04-07 22:38:14 +00:00
|
|
|
|
2014-04-07 22:38:21 +00:00
|
|
|
ret = kstrtoint(buf, 0, &num);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
zram: add multi stream functionality
Existing zram (zcomp) implementation has only one compression stream
(buffer and algorithm private part), so in order to prevent data
corruption only one write (compress operation) can use this compression
stream, forcing all concurrent write operations to wait for stream lock
to be released. This patch changes zcomp to keep a compression streams
list of user-defined size (via sysfs device attr). Each write operation
still exclusively holds compression stream, the difference is that we
can have N write operations (depending on size of streams list)
executing in parallel. See TEST section later in commit message for
performance data.
Introduce struct zcomp_strm_multi and a set of functions to manage
zcomp_strm stream access. zcomp_strm_multi has a list of idle
zcomp_strm structs, spinlock to protect idle list and wait queue, making
it possible to perform parallel compressions.
The following set of functions added:
- zcomp_strm_multi_find()/zcomp_strm_multi_release()
find and release a compression stream, implement required locking
- zcomp_strm_multi_create()/zcomp_strm_multi_destroy()
create and destroy zcomp_strm_multi
zcomp ->strm_find() and ->strm_release() callbacks are set during
initialisation to zcomp_strm_multi_find()/zcomp_strm_multi_release()
correspondingly.
Each time zcomp issues a zcomp_strm_multi_find() call, the following set
of operations performed:
- spin lock strm_lock
- if idle list is not empty, remove zcomp_strm from idle list, spin
unlock and return zcomp stream pointer to caller
- if idle list is empty, current adds itself to wait queue. it will be
awaken by zcomp_strm_multi_release() caller.
zcomp_strm_multi_release():
- spin lock strm_lock
- add zcomp stream to idle list
- spin unlock, wake up sleeper
Minchan Kim reported that spinlock-based locking scheme has demonstrated
a severe perfomance regression for single compression stream case,
comparing to mutex-based (see https://lkml.org/lkml/2014/2/18/16)
base spinlock mutex
==Initial write ==Initial write ==Initial write
records: 5 records: 5 records: 5
avg: 1642424.35 avg: 699610.40 avg: 1655583.71
std: 39890.95(2.43%) std: 232014.19(33.16%) std: 52293.96
max: 1690170.94 max: 1163473.45 max: 1697164.75
min: 1568669.52 min: 573429.88 min: 1553410.23
==Rewrite ==Rewrite ==Rewrite
records: 5 records: 5 records: 5
avg: 1611775.39 avg: 501406.64 avg: 1684419.11
std: 17144.58(1.06%) std: 15354.41(3.06%) std: 18367.42
max: 1641800.95 max: 531356.78 max: 1706445.84
min: 1593515.27 min: 488817.78 min: 1655335.73
When only one compression stream available, mutex with spin on owner
tends to perform much better than frequent wait_event()/wake_up(). This
is why single stream implemented as a special case with mutex locking.
Introduce and document zram device attribute max_comp_streams. This
attr shows and stores current zcomp's max number of zcomp streams
(max_strm). Extend zcomp's zcomp_create() with `max_strm' parameter.
`max_strm' limits the number of zcomp_strm structs in compression
backend's idle list (max_comp_streams).
max_comp_streams used during initialisation as follows:
-- passing to zcomp_create() max_strm equals to 1 will initialise zcomp
using single compression stream zcomp_strm_single (mutex-based locking).
-- passing to zcomp_create() max_strm greater than 1 will initialise zcomp
using multi compression stream zcomp_strm_multi (spinlock-based locking).
default max_comp_streams value is 1, meaning that zram with single stream
will be initialised.
Later patch will introduce configuration knob to change max_comp_streams
on already initialised and used zcomp.
TEST
iozone -t 3 -R -r 16K -s 60M -I +Z
test base 1 strm (mutex) 3 strm (spinlock)
-----------------------------------------------------------------------
Initial write 589286.78 583518.39 718011.05
Rewrite 604837.97 596776.38 1515125.72
Random write 584120.11 595714.58 1388850.25
Pwrite 535731.17 541117.38 739295.27
Fwrite 1418083.88 1478612.72 1484927.06
Usage example:
set max_comp_streams to 4
echo 4 > /sys/block/zram0/max_comp_streams
show current max_comp_streams (default value is 1).
cat /sys/block/zram0/max_comp_streams
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>
2014-04-07 22:38:14 +00:00
|
|
|
if (num < 1)
|
|
|
|
return -EINVAL;
|
2014-04-07 22:38:21 +00:00
|
|
|
|
zram: add multi stream functionality
Existing zram (zcomp) implementation has only one compression stream
(buffer and algorithm private part), so in order to prevent data
corruption only one write (compress operation) can use this compression
stream, forcing all concurrent write operations to wait for stream lock
to be released. This patch changes zcomp to keep a compression streams
list of user-defined size (via sysfs device attr). Each write operation
still exclusively holds compression stream, the difference is that we
can have N write operations (depending on size of streams list)
executing in parallel. See TEST section later in commit message for
performance data.
Introduce struct zcomp_strm_multi and a set of functions to manage
zcomp_strm stream access. zcomp_strm_multi has a list of idle
zcomp_strm structs, spinlock to protect idle list and wait queue, making
it possible to perform parallel compressions.
The following set of functions added:
- zcomp_strm_multi_find()/zcomp_strm_multi_release()
find and release a compression stream, implement required locking
- zcomp_strm_multi_create()/zcomp_strm_multi_destroy()
create and destroy zcomp_strm_multi
zcomp ->strm_find() and ->strm_release() callbacks are set during
initialisation to zcomp_strm_multi_find()/zcomp_strm_multi_release()
correspondingly.
Each time zcomp issues a zcomp_strm_multi_find() call, the following set
of operations performed:
- spin lock strm_lock
- if idle list is not empty, remove zcomp_strm from idle list, spin
unlock and return zcomp stream pointer to caller
- if idle list is empty, current adds itself to wait queue. it will be
awaken by zcomp_strm_multi_release() caller.
zcomp_strm_multi_release():
- spin lock strm_lock
- add zcomp stream to idle list
- spin unlock, wake up sleeper
Minchan Kim reported that spinlock-based locking scheme has demonstrated
a severe perfomance regression for single compression stream case,
comparing to mutex-based (see https://lkml.org/lkml/2014/2/18/16)
base spinlock mutex
==Initial write ==Initial write ==Initial write
records: 5 records: 5 records: 5
avg: 1642424.35 avg: 699610.40 avg: 1655583.71
std: 39890.95(2.43%) std: 232014.19(33.16%) std: 52293.96
max: 1690170.94 max: 1163473.45 max: 1697164.75
min: 1568669.52 min: 573429.88 min: 1553410.23
==Rewrite ==Rewrite ==Rewrite
records: 5 records: 5 records: 5
avg: 1611775.39 avg: 501406.64 avg: 1684419.11
std: 17144.58(1.06%) std: 15354.41(3.06%) std: 18367.42
max: 1641800.95 max: 531356.78 max: 1706445.84
min: 1593515.27 min: 488817.78 min: 1655335.73
When only one compression stream available, mutex with spin on owner
tends to perform much better than frequent wait_event()/wake_up(). This
is why single stream implemented as a special case with mutex locking.
Introduce and document zram device attribute max_comp_streams. This
attr shows and stores current zcomp's max number of zcomp streams
(max_strm). Extend zcomp's zcomp_create() with `max_strm' parameter.
`max_strm' limits the number of zcomp_strm structs in compression
backend's idle list (max_comp_streams).
max_comp_streams used during initialisation as follows:
-- passing to zcomp_create() max_strm equals to 1 will initialise zcomp
using single compression stream zcomp_strm_single (mutex-based locking).
-- passing to zcomp_create() max_strm greater than 1 will initialise zcomp
using multi compression stream zcomp_strm_multi (spinlock-based locking).
default max_comp_streams value is 1, meaning that zram with single stream
will be initialised.
Later patch will introduce configuration knob to change max_comp_streams
on already initialised and used zcomp.
TEST
iozone -t 3 -R -r 16K -s 60M -I +Z
test base 1 strm (mutex) 3 strm (spinlock)
-----------------------------------------------------------------------
Initial write 589286.78 583518.39 718011.05
Rewrite 604837.97 596776.38 1515125.72
Random write 584120.11 595714.58 1388850.25
Pwrite 535731.17 541117.38 739295.27
Fwrite 1418083.88 1478612.72 1484927.06
Usage example:
set max_comp_streams to 4
echo 4 > /sys/block/zram0/max_comp_streams
show current max_comp_streams (default value is 1).
cat /sys/block/zram0/max_comp_streams
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>
2014-04-07 22:38:14 +00:00
|
|
|
down_write(&zram->init_lock);
|
|
|
|
if (init_done(zram)) {
|
2014-04-07 22:38:21 +00:00
|
|
|
if (!zcomp_set_max_streams(zram->comp, num)) {
|
2014-04-07 22:38:15 +00:00
|
|
|
pr_info("Cannot change max compression streams\n");
|
2014-04-07 22:38:21 +00:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
zram: add multi stream functionality
Existing zram (zcomp) implementation has only one compression stream
(buffer and algorithm private part), so in order to prevent data
corruption only one write (compress operation) can use this compression
stream, forcing all concurrent write operations to wait for stream lock
to be released. This patch changes zcomp to keep a compression streams
list of user-defined size (via sysfs device attr). Each write operation
still exclusively holds compression stream, the difference is that we
can have N write operations (depending on size of streams list)
executing in parallel. See TEST section later in commit message for
performance data.
Introduce struct zcomp_strm_multi and a set of functions to manage
zcomp_strm stream access. zcomp_strm_multi has a list of idle
zcomp_strm structs, spinlock to protect idle list and wait queue, making
it possible to perform parallel compressions.
The following set of functions added:
- zcomp_strm_multi_find()/zcomp_strm_multi_release()
find and release a compression stream, implement required locking
- zcomp_strm_multi_create()/zcomp_strm_multi_destroy()
create and destroy zcomp_strm_multi
zcomp ->strm_find() and ->strm_release() callbacks are set during
initialisation to zcomp_strm_multi_find()/zcomp_strm_multi_release()
correspondingly.
Each time zcomp issues a zcomp_strm_multi_find() call, the following set
of operations performed:
- spin lock strm_lock
- if idle list is not empty, remove zcomp_strm from idle list, spin
unlock and return zcomp stream pointer to caller
- if idle list is empty, current adds itself to wait queue. it will be
awaken by zcomp_strm_multi_release() caller.
zcomp_strm_multi_release():
- spin lock strm_lock
- add zcomp stream to idle list
- spin unlock, wake up sleeper
Minchan Kim reported that spinlock-based locking scheme has demonstrated
a severe perfomance regression for single compression stream case,
comparing to mutex-based (see https://lkml.org/lkml/2014/2/18/16)
base spinlock mutex
==Initial write ==Initial write ==Initial write
records: 5 records: 5 records: 5
avg: 1642424.35 avg: 699610.40 avg: 1655583.71
std: 39890.95(2.43%) std: 232014.19(33.16%) std: 52293.96
max: 1690170.94 max: 1163473.45 max: 1697164.75
min: 1568669.52 min: 573429.88 min: 1553410.23
==Rewrite ==Rewrite ==Rewrite
records: 5 records: 5 records: 5
avg: 1611775.39 avg: 501406.64 avg: 1684419.11
std: 17144.58(1.06%) std: 15354.41(3.06%) std: 18367.42
max: 1641800.95 max: 531356.78 max: 1706445.84
min: 1593515.27 min: 488817.78 min: 1655335.73
When only one compression stream available, mutex with spin on owner
tends to perform much better than frequent wait_event()/wake_up(). This
is why single stream implemented as a special case with mutex locking.
Introduce and document zram device attribute max_comp_streams. This
attr shows and stores current zcomp's max number of zcomp streams
(max_strm). Extend zcomp's zcomp_create() with `max_strm' parameter.
`max_strm' limits the number of zcomp_strm structs in compression
backend's idle list (max_comp_streams).
max_comp_streams used during initialisation as follows:
-- passing to zcomp_create() max_strm equals to 1 will initialise zcomp
using single compression stream zcomp_strm_single (mutex-based locking).
-- passing to zcomp_create() max_strm greater than 1 will initialise zcomp
using multi compression stream zcomp_strm_multi (spinlock-based locking).
default max_comp_streams value is 1, meaning that zram with single stream
will be initialised.
Later patch will introduce configuration knob to change max_comp_streams
on already initialised and used zcomp.
TEST
iozone -t 3 -R -r 16K -s 60M -I +Z
test base 1 strm (mutex) 3 strm (spinlock)
-----------------------------------------------------------------------
Initial write 589286.78 583518.39 718011.05
Rewrite 604837.97 596776.38 1515125.72
Random write 584120.11 595714.58 1388850.25
Pwrite 535731.17 541117.38 739295.27
Fwrite 1418083.88 1478612.72 1484927.06
Usage example:
set max_comp_streams to 4
echo 4 > /sys/block/zram0/max_comp_streams
show current max_comp_streams (default value is 1).
cat /sys/block/zram0/max_comp_streams
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>
2014-04-07 22:38:14 +00:00
|
|
|
}
|
2014-04-07 22:38:21 +00:00
|
|
|
|
zram: add multi stream functionality
Existing zram (zcomp) implementation has only one compression stream
(buffer and algorithm private part), so in order to prevent data
corruption only one write (compress operation) can use this compression
stream, forcing all concurrent write operations to wait for stream lock
to be released. This patch changes zcomp to keep a compression streams
list of user-defined size (via sysfs device attr). Each write operation
still exclusively holds compression stream, the difference is that we
can have N write operations (depending on size of streams list)
executing in parallel. See TEST section later in commit message for
performance data.
Introduce struct zcomp_strm_multi and a set of functions to manage
zcomp_strm stream access. zcomp_strm_multi has a list of idle
zcomp_strm structs, spinlock to protect idle list and wait queue, making
it possible to perform parallel compressions.
The following set of functions added:
- zcomp_strm_multi_find()/zcomp_strm_multi_release()
find and release a compression stream, implement required locking
- zcomp_strm_multi_create()/zcomp_strm_multi_destroy()
create and destroy zcomp_strm_multi
zcomp ->strm_find() and ->strm_release() callbacks are set during
initialisation to zcomp_strm_multi_find()/zcomp_strm_multi_release()
correspondingly.
Each time zcomp issues a zcomp_strm_multi_find() call, the following set
of operations performed:
- spin lock strm_lock
- if idle list is not empty, remove zcomp_strm from idle list, spin
unlock and return zcomp stream pointer to caller
- if idle list is empty, current adds itself to wait queue. it will be
awaken by zcomp_strm_multi_release() caller.
zcomp_strm_multi_release():
- spin lock strm_lock
- add zcomp stream to idle list
- spin unlock, wake up sleeper
Minchan Kim reported that spinlock-based locking scheme has demonstrated
a severe perfomance regression for single compression stream case,
comparing to mutex-based (see https://lkml.org/lkml/2014/2/18/16)
base spinlock mutex
==Initial write ==Initial write ==Initial write
records: 5 records: 5 records: 5
avg: 1642424.35 avg: 699610.40 avg: 1655583.71
std: 39890.95(2.43%) std: 232014.19(33.16%) std: 52293.96
max: 1690170.94 max: 1163473.45 max: 1697164.75
min: 1568669.52 min: 573429.88 min: 1553410.23
==Rewrite ==Rewrite ==Rewrite
records: 5 records: 5 records: 5
avg: 1611775.39 avg: 501406.64 avg: 1684419.11
std: 17144.58(1.06%) std: 15354.41(3.06%) std: 18367.42
max: 1641800.95 max: 531356.78 max: 1706445.84
min: 1593515.27 min: 488817.78 min: 1655335.73
When only one compression stream available, mutex with spin on owner
tends to perform much better than frequent wait_event()/wake_up(). This
is why single stream implemented as a special case with mutex locking.
Introduce and document zram device attribute max_comp_streams. This
attr shows and stores current zcomp's max number of zcomp streams
(max_strm). Extend zcomp's zcomp_create() with `max_strm' parameter.
`max_strm' limits the number of zcomp_strm structs in compression
backend's idle list (max_comp_streams).
max_comp_streams used during initialisation as follows:
-- passing to zcomp_create() max_strm equals to 1 will initialise zcomp
using single compression stream zcomp_strm_single (mutex-based locking).
-- passing to zcomp_create() max_strm greater than 1 will initialise zcomp
using multi compression stream zcomp_strm_multi (spinlock-based locking).
default max_comp_streams value is 1, meaning that zram with single stream
will be initialised.
Later patch will introduce configuration knob to change max_comp_streams
on already initialised and used zcomp.
TEST
iozone -t 3 -R -r 16K -s 60M -I +Z
test base 1 strm (mutex) 3 strm (spinlock)
-----------------------------------------------------------------------
Initial write 589286.78 583518.39 718011.05
Rewrite 604837.97 596776.38 1515125.72
Random write 584120.11 595714.58 1388850.25
Pwrite 535731.17 541117.38 739295.27
Fwrite 1418083.88 1478612.72 1484927.06
Usage example:
set max_comp_streams to 4
echo 4 > /sys/block/zram0/max_comp_streams
show current max_comp_streams (default value is 1).
cat /sys/block/zram0/max_comp_streams
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>
2014-04-07 22:38:14 +00:00
|
|
|
zram->max_comp_streams = num;
|
2014-04-07 22:38:21 +00:00
|
|
|
ret = len;
|
|
|
|
out:
|
zram: add multi stream functionality
Existing zram (zcomp) implementation has only one compression stream
(buffer and algorithm private part), so in order to prevent data
corruption only one write (compress operation) can use this compression
stream, forcing all concurrent write operations to wait for stream lock
to be released. This patch changes zcomp to keep a compression streams
list of user-defined size (via sysfs device attr). Each write operation
still exclusively holds compression stream, the difference is that we
can have N write operations (depending on size of streams list)
executing in parallel. See TEST section later in commit message for
performance data.
Introduce struct zcomp_strm_multi and a set of functions to manage
zcomp_strm stream access. zcomp_strm_multi has a list of idle
zcomp_strm structs, spinlock to protect idle list and wait queue, making
it possible to perform parallel compressions.
The following set of functions added:
- zcomp_strm_multi_find()/zcomp_strm_multi_release()
find and release a compression stream, implement required locking
- zcomp_strm_multi_create()/zcomp_strm_multi_destroy()
create and destroy zcomp_strm_multi
zcomp ->strm_find() and ->strm_release() callbacks are set during
initialisation to zcomp_strm_multi_find()/zcomp_strm_multi_release()
correspondingly.
Each time zcomp issues a zcomp_strm_multi_find() call, the following set
of operations performed:
- spin lock strm_lock
- if idle list is not empty, remove zcomp_strm from idle list, spin
unlock and return zcomp stream pointer to caller
- if idle list is empty, current adds itself to wait queue. it will be
awaken by zcomp_strm_multi_release() caller.
zcomp_strm_multi_release():
- spin lock strm_lock
- add zcomp stream to idle list
- spin unlock, wake up sleeper
Minchan Kim reported that spinlock-based locking scheme has demonstrated
a severe perfomance regression for single compression stream case,
comparing to mutex-based (see https://lkml.org/lkml/2014/2/18/16)
base spinlock mutex
==Initial write ==Initial write ==Initial write
records: 5 records: 5 records: 5
avg: 1642424.35 avg: 699610.40 avg: 1655583.71
std: 39890.95(2.43%) std: 232014.19(33.16%) std: 52293.96
max: 1690170.94 max: 1163473.45 max: 1697164.75
min: 1568669.52 min: 573429.88 min: 1553410.23
==Rewrite ==Rewrite ==Rewrite
records: 5 records: 5 records: 5
avg: 1611775.39 avg: 501406.64 avg: 1684419.11
std: 17144.58(1.06%) std: 15354.41(3.06%) std: 18367.42
max: 1641800.95 max: 531356.78 max: 1706445.84
min: 1593515.27 min: 488817.78 min: 1655335.73
When only one compression stream available, mutex with spin on owner
tends to perform much better than frequent wait_event()/wake_up(). This
is why single stream implemented as a special case with mutex locking.
Introduce and document zram device attribute max_comp_streams. This
attr shows and stores current zcomp's max number of zcomp streams
(max_strm). Extend zcomp's zcomp_create() with `max_strm' parameter.
`max_strm' limits the number of zcomp_strm structs in compression
backend's idle list (max_comp_streams).
max_comp_streams used during initialisation as follows:
-- passing to zcomp_create() max_strm equals to 1 will initialise zcomp
using single compression stream zcomp_strm_single (mutex-based locking).
-- passing to zcomp_create() max_strm greater than 1 will initialise zcomp
using multi compression stream zcomp_strm_multi (spinlock-based locking).
default max_comp_streams value is 1, meaning that zram with single stream
will be initialised.
Later patch will introduce configuration knob to change max_comp_streams
on already initialised and used zcomp.
TEST
iozone -t 3 -R -r 16K -s 60M -I +Z
test base 1 strm (mutex) 3 strm (spinlock)
-----------------------------------------------------------------------
Initial write 589286.78 583518.39 718011.05
Rewrite 604837.97 596776.38 1515125.72
Random write 584120.11 595714.58 1388850.25
Pwrite 535731.17 541117.38 739295.27
Fwrite 1418083.88 1478612.72 1484927.06
Usage example:
set max_comp_streams to 4
echo 4 > /sys/block/zram0/max_comp_streams
show current max_comp_streams (default value is 1).
cat /sys/block/zram0/max_comp_streams
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>
2014-04-07 22:38:14 +00:00
|
|
|
up_write(&zram->init_lock);
|
2014-04-07 22:38:21 +00:00
|
|
|
return ret;
|
zram: add multi stream functionality
Existing zram (zcomp) implementation has only one compression stream
(buffer and algorithm private part), so in order to prevent data
corruption only one write (compress operation) can use this compression
stream, forcing all concurrent write operations to wait for stream lock
to be released. This patch changes zcomp to keep a compression streams
list of user-defined size (via sysfs device attr). Each write operation
still exclusively holds compression stream, the difference is that we
can have N write operations (depending on size of streams list)
executing in parallel. See TEST section later in commit message for
performance data.
Introduce struct zcomp_strm_multi and a set of functions to manage
zcomp_strm stream access. zcomp_strm_multi has a list of idle
zcomp_strm structs, spinlock to protect idle list and wait queue, making
it possible to perform parallel compressions.
The following set of functions added:
- zcomp_strm_multi_find()/zcomp_strm_multi_release()
find and release a compression stream, implement required locking
- zcomp_strm_multi_create()/zcomp_strm_multi_destroy()
create and destroy zcomp_strm_multi
zcomp ->strm_find() and ->strm_release() callbacks are set during
initialisation to zcomp_strm_multi_find()/zcomp_strm_multi_release()
correspondingly.
Each time zcomp issues a zcomp_strm_multi_find() call, the following set
of operations performed:
- spin lock strm_lock
- if idle list is not empty, remove zcomp_strm from idle list, spin
unlock and return zcomp stream pointer to caller
- if idle list is empty, current adds itself to wait queue. it will be
awaken by zcomp_strm_multi_release() caller.
zcomp_strm_multi_release():
- spin lock strm_lock
- add zcomp stream to idle list
- spin unlock, wake up sleeper
Minchan Kim reported that spinlock-based locking scheme has demonstrated
a severe perfomance regression for single compression stream case,
comparing to mutex-based (see https://lkml.org/lkml/2014/2/18/16)
base spinlock mutex
==Initial write ==Initial write ==Initial write
records: 5 records: 5 records: 5
avg: 1642424.35 avg: 699610.40 avg: 1655583.71
std: 39890.95(2.43%) std: 232014.19(33.16%) std: 52293.96
max: 1690170.94 max: 1163473.45 max: 1697164.75
min: 1568669.52 min: 573429.88 min: 1553410.23
==Rewrite ==Rewrite ==Rewrite
records: 5 records: 5 records: 5
avg: 1611775.39 avg: 501406.64 avg: 1684419.11
std: 17144.58(1.06%) std: 15354.41(3.06%) std: 18367.42
max: 1641800.95 max: 531356.78 max: 1706445.84
min: 1593515.27 min: 488817.78 min: 1655335.73
When only one compression stream available, mutex with spin on owner
tends to perform much better than frequent wait_event()/wake_up(). This
is why single stream implemented as a special case with mutex locking.
Introduce and document zram device attribute max_comp_streams. This
attr shows and stores current zcomp's max number of zcomp streams
(max_strm). Extend zcomp's zcomp_create() with `max_strm' parameter.
`max_strm' limits the number of zcomp_strm structs in compression
backend's idle list (max_comp_streams).
max_comp_streams used during initialisation as follows:
-- passing to zcomp_create() max_strm equals to 1 will initialise zcomp
using single compression stream zcomp_strm_single (mutex-based locking).
-- passing to zcomp_create() max_strm greater than 1 will initialise zcomp
using multi compression stream zcomp_strm_multi (spinlock-based locking).
default max_comp_streams value is 1, meaning that zram with single stream
will be initialised.
Later patch will introduce configuration knob to change max_comp_streams
on already initialised and used zcomp.
TEST
iozone -t 3 -R -r 16K -s 60M -I +Z
test base 1 strm (mutex) 3 strm (spinlock)
-----------------------------------------------------------------------
Initial write 589286.78 583518.39 718011.05
Rewrite 604837.97 596776.38 1515125.72
Random write 584120.11 595714.58 1388850.25
Pwrite 535731.17 541117.38 739295.27
Fwrite 1418083.88 1478612.72 1484927.06
Usage example:
set max_comp_streams to 4
echo 4 > /sys/block/zram0/max_comp_streams
show current max_comp_streams (default value is 1).
cat /sys/block/zram0/max_comp_streams
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>
2014-04-07 22:38:14 +00:00
|
|
|
}
|
|
|
|
|
2014-04-07 22:38:17 +00:00
|
|
|
static ssize_t comp_algorithm_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
size_t sz;
|
|
|
|
struct zram *zram = dev_to_zram(dev);
|
|
|
|
|
|
|
|
down_read(&zram->init_lock);
|
|
|
|
sz = zcomp_available_show(zram->compressor, buf);
|
|
|
|
up_read(&zram->init_lock);
|
|
|
|
|
|
|
|
return sz;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t comp_algorithm_store(struct device *dev,
|
|
|
|
struct device_attribute *attr, const char *buf, size_t len)
|
|
|
|
{
|
|
|
|
struct zram *zram = dev_to_zram(dev);
|
|
|
|
down_write(&zram->init_lock);
|
|
|
|
if (init_done(zram)) {
|
|
|
|
up_write(&zram->init_lock);
|
|
|
|
pr_info("Can't change algorithm for initialized device\n");
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
strlcpy(zram->compressor, buf, sizeof(zram->compressor));
|
|
|
|
up_write(&zram->init_lock);
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2014-01-30 23:46:03 +00:00
|
|
|
/* flag operations needs meta->tb_lock */
|
2013-02-05 23:48:53 +00:00
|
|
|
static int zram_test_flag(struct zram_meta *meta, u32 index,
|
2010-06-01 08:01:25 +00:00
|
|
|
enum zram_pageflags flag)
|
2009-09-22 04:56:53 +00:00
|
|
|
{
|
2013-02-05 23:48:53 +00:00
|
|
|
return meta->table[index].flags & BIT(flag);
|
2009-09-22 04:56:53 +00:00
|
|
|
}
|
|
|
|
|
2013-02-05 23:48:53 +00:00
|
|
|
static void zram_set_flag(struct zram_meta *meta, u32 index,
|
2010-06-01 08:01:25 +00:00
|
|
|
enum zram_pageflags flag)
|
2009-09-22 04:56:53 +00:00
|
|
|
{
|
2013-02-05 23:48:53 +00:00
|
|
|
meta->table[index].flags |= BIT(flag);
|
2009-09-22 04:56:53 +00:00
|
|
|
}
|
|
|
|
|
2013-02-05 23:48:53 +00:00
|
|
|
static void zram_clear_flag(struct zram_meta *meta, u32 index,
|
2010-06-01 08:01:25 +00:00
|
|
|
enum zram_pageflags flag)
|
2009-09-22 04:56:53 +00:00
|
|
|
{
|
2013-02-05 23:48:53 +00:00
|
|
|
meta->table[index].flags &= ~BIT(flag);
|
2009-09-22 04:56:53 +00:00
|
|
|
}
|
|
|
|
|
2013-06-22 00:21:18 +00: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 18:23:24 +00:00
|
|
|
|
2013-06-22 00:21:18 +00:00
|
|
|
/* unaligned request */
|
2013-10-11 22:44:27 +00:00
|
|
|
if (unlikely(bio->bi_iter.bi_sector &
|
|
|
|
(ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1)))
|
2013-06-22 00:21:18 +00:00
|
|
|
return 0;
|
2013-10-11 22:44:27 +00:00
|
|
|
if (unlikely(bio->bi_iter.bi_size & (ZRAM_LOGICAL_BLOCK_SIZE - 1)))
|
2013-06-22 00:21:18 +00:00
|
|
|
return 0;
|
|
|
|
|
2013-10-11 22:44:27 +00:00
|
|
|
start = bio->bi_iter.bi_sector;
|
|
|
|
end = start + (bio->bi_iter.bi_size >> SECTOR_SHIFT);
|
2013-06-22 00:21:18 +00:00
|
|
|
bound = zram->disksize >> SECTOR_SHIFT;
|
|
|
|
/* out of range range */
|
2013-06-22 14:21:00 +00:00
|
|
|
if (unlikely(start >= bound || end > bound || start > end))
|
2013-06-22 00:21:18 +00: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);
|
|
|
|
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;
|
|
|
|
|
|
|
|
num_pages = disksize >> PAGE_SHIFT;
|
|
|
|
meta->table = vzalloc(num_pages * sizeof(*meta->table));
|
|
|
|
if (!meta->table) {
|
|
|
|
pr_err("Error allocating zram address table\n");
|
2014-04-07 22:38:12 +00:00
|
|
|
goto free_meta;
|
2013-06-22 00:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 23:46:03 +00:00
|
|
|
rwlock_init(&meta->tb_lock);
|
2013-06-22 00:21:18 +00:00
|
|
|
return meta;
|
|
|
|
|
|
|
|
free_table:
|
|
|
|
vfree(meta->table);
|
|
|
|
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-22 04:56:53 +00: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-22 00:21:18 +00: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 23:46:03 +00:00
|
|
|
/* NOTE: caller should hold meta->tb_lock with write-side */
|
2010-06-01 08:01:25 +00:00
|
|
|
static void zram_free_page(struct zram *zram, size_t index)
|
2009-09-22 04:56:53 +00:00
|
|
|
{
|
2013-02-05 23:48:53 +00:00
|
|
|
struct zram_meta *meta = zram->meta;
|
|
|
|
unsigned long handle = meta->table[index].handle;
|
2009-09-22 04:56:53 +00:00
|
|
|
|
2012-01-09 22:51:59 +00:00
|
|
|
if (unlikely(!handle)) {
|
2010-01-28 15:43:41 +00:00
|
|
|
/*
|
|
|
|
* No memory is allocated for zero filled pages.
|
|
|
|
* Simply clear zero page flag.
|
|
|
|
*/
|
2013-02-05 23:48:53 +00:00
|
|
|
if (zram_test_flag(meta, index, ZRAM_ZERO)) {
|
|
|
|
zram_clear_flag(meta, index, ZRAM_ZERO);
|
2014-04-07 22:38:03 +00:00
|
|
|
atomic64_dec(&zram->stats.zero_pages);
|
2009-09-22 04:56:53 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-05 23:48:53 +00:00
|
|
|
zs_free(meta->mem_pool, handle);
|
2009-09-22 04:56:53 +00:00
|
|
|
|
2014-04-07 22:38:03 +00:00
|
|
|
atomic64_sub(meta->table[index].size, &zram->stats.compr_data_size);
|
|
|
|
atomic64_dec(&zram->stats.pages_stored);
|
2009-09-22 04:56:53 +00:00
|
|
|
|
2013-02-05 23:48:53 +00:00
|
|
|
meta->table[index].handle = 0;
|
|
|
|
meta->table[index].size = 0;
|
2009-09-22 04:56:53 +00:00
|
|
|
}
|
|
|
|
|
2012-10-30 19:40:23 +00:00
|
|
|
static int zram_decompress_page(struct zram *zram, char *mem, u32 index)
|
2009-09-22 04:56:53 +00:00
|
|
|
{
|
2014-04-07 22:38:12 +00:00
|
|
|
int ret = 0;
|
2012-10-30 19:40:23 +00:00
|
|
|
unsigned char *cmem;
|
2013-02-05 23:48:53 +00:00
|
|
|
struct zram_meta *meta = zram->meta;
|
2014-01-30 23:46:03 +00:00
|
|
|
unsigned long handle;
|
|
|
|
u16 size;
|
|
|
|
|
|
|
|
read_lock(&meta->tb_lock);
|
|
|
|
handle = meta->table[index].handle;
|
|
|
|
size = meta->table[index].size;
|
2009-09-22 04:56:53 +00:00
|
|
|
|
2013-02-05 23:48:53 +00:00
|
|
|
if (!handle || zram_test_flag(meta, index, ZRAM_ZERO)) {
|
2014-01-30 23:46:03 +00:00
|
|
|
read_unlock(&meta->tb_lock);
|
2013-06-06 16:07:30 +00:00
|
|
|
clear_page(mem);
|
2011-06-10 13:28:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2009-09-22 04:56:53 +00:00
|
|
|
|
2013-02-05 23:48:53 +00:00
|
|
|
cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_RO);
|
2014-01-30 23:46:03 +00:00
|
|
|
if (size == PAGE_SIZE)
|
2013-06-06 16:07:30 +00:00
|
|
|
copy_page(mem, cmem);
|
2012-10-30 19:40:23 +00:00
|
|
|
else
|
2014-04-07 22:38:12 +00:00
|
|
|
ret = zcomp_decompress(zram->comp, cmem, size, mem);
|
2013-02-05 23:48:53 +00:00
|
|
|
zs_unmap_object(meta->mem_pool, handle);
|
2014-01-30 23:46:03 +00: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 08:01:23 +00:00
|
|
|
|
2011-06-10 13:28:47 +00:00
|
|
|
/* Should NEVER happen. Return bio error if it does. */
|
2014-04-07 22:38:12 +00:00
|
|
|
if (unlikely(ret)) {
|
2011-06-10 13:28:47 +00:00
|
|
|
pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
|
2013-06-06 16:07:31 +00:00
|
|
|
atomic64_inc(&zram->stats.failed_reads);
|
2011-06-10 13:28:47 +00: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 08:01:23 +00:00
|
|
|
}
|
2009-09-22 04:56:53 +00:00
|
|
|
|
2011-06-10 13:28:47 +00:00
|
|
|
return 0;
|
2009-09-22 04:56:53 +00:00
|
|
|
}
|
|
|
|
|
2012-10-30 19:40:23 +00:00
|
|
|
static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
|
|
|
|
u32 index, int offset, struct bio *bio)
|
2011-06-10 13:28:48 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2012-10-30 19:40:23 +00:00
|
|
|
struct page *page;
|
|
|
|
unsigned char *user_mem, *uncmem = NULL;
|
2013-02-05 23:48:53 +00:00
|
|
|
struct zram_meta *meta = zram->meta;
|
2012-10-30 19:40:23 +00:00
|
|
|
page = bvec->bv_page;
|
|
|
|
|
2014-01-30 23:46:03 +00:00
|
|
|
read_lock(&meta->tb_lock);
|
2013-02-05 23:48:53 +00:00
|
|
|
if (unlikely(!meta->table[index].handle) ||
|
|
|
|
zram_test_flag(meta, index, ZRAM_ZERO)) {
|
2014-01-30 23:46:03 +00:00
|
|
|
read_unlock(&meta->tb_lock);
|
2012-10-30 19:40:23 +00:00
|
|
|
handle_zero_page(bvec);
|
2011-06-10 13:28:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-01-30 23:46:03 +00:00
|
|
|
read_unlock(&meta->tb_lock);
|
2011-06-10 13:28:48 +00:00
|
|
|
|
2012-10-30 19:40:23 +00:00
|
|
|
if (is_partial_io(bvec))
|
|
|
|
/* Use a temporary buffer to decompress the page */
|
2013-01-30 02:41:39 +00:00
|
|
|
uncmem = kmalloc(PAGE_SIZE, GFP_NOIO);
|
|
|
|
|
|
|
|
user_mem = kmap_atomic(page);
|
|
|
|
if (!is_partial_io(bvec))
|
2012-10-30 19:40:23 +00:00
|
|
|
uncmem = user_mem;
|
|
|
|
|
|
|
|
if (!uncmem) {
|
|
|
|
pr_info("Unable to allocate temp memory\n");
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out_cleanup;
|
|
|
|
}
|
2011-06-10 13:28:48 +00:00
|
|
|
|
2012-10-30 19:40:23 +00:00
|
|
|
ret = zram_decompress_page(zram, uncmem, index);
|
2011-06-10 13:28:48 +00:00
|
|
|
/* Should NEVER happen. Return bio error if it does. */
|
2014-04-07 22:38:12 +00:00
|
|
|
if (unlikely(ret))
|
2012-10-30 19:40:23 +00:00
|
|
|
goto out_cleanup;
|
2011-06-10 13:28:48 +00:00
|
|
|
|
2012-10-30 19:40:23 +00: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 13:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
|
|
|
|
int offset)
|
2009-09-22 04:56:53 +00:00
|
|
|
{
|
2013-01-02 16:53:41 +00:00
|
|
|
int ret = 0;
|
2011-06-10 13:28:47 +00:00
|
|
|
size_t clen;
|
2012-06-08 06:39:25 +00:00
|
|
|
unsigned long handle;
|
2012-06-08 06:39:27 +00:00
|
|
|
struct page *page;
|
2011-06-10 13:28:48 +00:00
|
|
|
unsigned char *user_mem, *cmem, *src, *uncmem = NULL;
|
2013-02-05 23:48:53 +00:00
|
|
|
struct zram_meta *meta = zram->meta;
|
2014-04-07 22:38:12 +00:00
|
|
|
struct zcomp_strm *zstrm;
|
2014-01-30 23:46:06 +00:00
|
|
|
bool locked = false;
|
2009-09-22 04:56:53 +00:00
|
|
|
|
2011-06-10 13:28:47 +00:00
|
|
|
page = bvec->bv_page;
|
2011-06-10 13:28:48 +00: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-30 02:41:39 +00:00
|
|
|
uncmem = kmalloc(PAGE_SIZE, GFP_NOIO);
|
2011-06-10 13:28:48 +00:00
|
|
|
if (!uncmem) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
2012-10-30 19:40:23 +00:00
|
|
|
ret = zram_decompress_page(zram, uncmem, index);
|
2013-01-02 16:53:41 +00:00
|
|
|
if (ret)
|
2011-06-10 13:28:48 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2014-04-07 22:38:12 +00:00
|
|
|
zstrm = zcomp_strm_find(zram->comp);
|
2014-01-30 23:46:06 +00:00
|
|
|
locked = true;
|
2011-11-25 15:14:25 +00:00
|
|
|
user_mem = kmap_atomic(page);
|
2011-06-10 13:28:48 +00:00
|
|
|
|
2013-01-02 16:53:41 +00:00
|
|
|
if (is_partial_io(bvec)) {
|
2011-06-10 13:28:48 +00:00
|
|
|
memcpy(uncmem + offset, user_mem + bvec->bv_offset,
|
|
|
|
bvec->bv_len);
|
2013-01-02 16:53:41 +00:00
|
|
|
kunmap_atomic(user_mem);
|
|
|
|
user_mem = NULL;
|
|
|
|
} else {
|
2011-06-10 13:28:48 +00:00
|
|
|
uncmem = user_mem;
|
2013-01-02 16:53:41 +00:00
|
|
|
}
|
2011-06-10 13:28:48 +00:00
|
|
|
|
|
|
|
if (page_zero_filled(uncmem)) {
|
2011-11-25 15:14:25 +00:00
|
|
|
kunmap_atomic(user_mem);
|
2013-07-03 11:10:05 +00:00
|
|
|
/* Free memory associated with this sector now. */
|
2014-01-30 23:46:03 +00:00
|
|
|
write_lock(&zram->meta->tb_lock);
|
2013-07-03 11:10:05 +00:00
|
|
|
zram_free_page(zram, index);
|
2014-01-30 23:46:03 +00:00
|
|
|
zram_set_flag(meta, index, ZRAM_ZERO);
|
|
|
|
write_unlock(&zram->meta->tb_lock);
|
2013-07-03 11:10:05 +00:00
|
|
|
|
2014-04-07 22:38:03 +00:00
|
|
|
atomic64_inc(&zram->stats.zero_pages);
|
2011-06-10 13:28:48 +00:00
|
|
|
ret = 0;
|
|
|
|
goto out;
|
2011-06-10 13:28:47 +00:00
|
|
|
}
|
2009-09-22 04:56:53 +00:00
|
|
|
|
2014-04-07 22:38:12 +00:00
|
|
|
ret = zcomp_compress(zram->comp, zstrm, uncmem, &clen);
|
2013-01-02 16:53:41 +00:00
|
|
|
if (!is_partial_io(bvec)) {
|
|
|
|
kunmap_atomic(user_mem);
|
|
|
|
user_mem = NULL;
|
|
|
|
uncmem = NULL;
|
|
|
|
}
|
2009-09-22 04:56:53 +00:00
|
|
|
|
2014-04-07 22:38:12 +00:00
|
|
|
if (unlikely(ret)) {
|
2011-06-10 13:28:47 +00:00
|
|
|
pr_err("Compression failed! err=%d\n", ret);
|
2011-06-10 13:28:48 +00:00
|
|
|
goto out;
|
2011-06-10 13:28:47 +00:00
|
|
|
}
|
2014-04-07 22:38:12 +00:00
|
|
|
src = zstrm->buffer;
|
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-11 00:42:18 +00:00
|
|
|
if (unlikely(clen > max_zpage_size)) {
|
|
|
|
clen = PAGE_SIZE;
|
2013-01-02 16:53:41 +00:00
|
|
|
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-11 00:42:18 +00: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 08:01:23 +00:00
|
|
|
|
2013-02-05 23:48:53 +00:00
|
|
|
handle = zs_malloc(meta->mem_pool, clen);
|
2012-01-09 22:51:59 +00:00
|
|
|
if (!handle) {
|
2013-05-16 18:30:39 +00:00
|
|
|
pr_info("Error allocating memory for compressed page: %u, size=%zu\n",
|
|
|
|
index, clen);
|
2011-06-10 13:28:48 +00:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out;
|
2011-06-10 13:28:47 +00:00
|
|
|
}
|
2013-02-05 23:48:53 +00:00
|
|
|
cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_WO);
|
2009-09-22 04:56:53 +00:00
|
|
|
|
2013-06-06 16:07:30 +00:00
|
|
|
if ((clen == PAGE_SIZE) && !is_partial_io(bvec)) {
|
2013-01-02 16:53:41 +00:00
|
|
|
src = kmap_atomic(page);
|
2013-06-06 16:07:30 +00:00
|
|
|
copy_page(cmem, src);
|
2013-01-02 16:53:41 +00:00
|
|
|
kunmap_atomic(src);
|
2013-06-06 16:07:30 +00:00
|
|
|
} else {
|
|
|
|
memcpy(cmem, src, clen);
|
|
|
|
}
|
2009-09-22 04:56:53 +00:00
|
|
|
|
2014-04-07 22:38:12 +00:00
|
|
|
zcomp_strm_release(zram->comp, zstrm);
|
|
|
|
locked = false;
|
2013-02-05 23:48:53 +00:00
|
|
|
zs_unmap_object(meta->mem_pool, handle);
|
2012-01-09 22:51:59 +00:00
|
|
|
|
2013-07-03 11:10:05 +00:00
|
|
|
/*
|
|
|
|
* Free memory associated with this sector
|
|
|
|
* before overwriting unused sectors.
|
|
|
|
*/
|
2014-01-30 23:46:03 +00:00
|
|
|
write_lock(&zram->meta->tb_lock);
|
2013-07-03 11:10:05 +00:00
|
|
|
zram_free_page(zram, index);
|
|
|
|
|
2013-02-05 23:48:53 +00:00
|
|
|
meta->table[index].handle = handle;
|
|
|
|
meta->table[index].size = clen;
|
2014-01-30 23:46:03 +00:00
|
|
|
write_unlock(&zram->meta->tb_lock);
|
2009-09-22 04:56:53 +00:00
|
|
|
|
2011-06-10 13:28:47 +00:00
|
|
|
/* Update stats */
|
2014-04-07 22:38:03 +00:00
|
|
|
atomic64_add(clen, &zram->stats.compr_data_size);
|
|
|
|
atomic64_inc(&zram->stats.pages_stored);
|
2011-06-10 13:28:48 +00:00
|
|
|
out:
|
2014-01-30 23:46:06 +00:00
|
|
|
if (locked)
|
2014-04-07 22:38:12 +00:00
|
|
|
zcomp_strm_release(zram->comp, zstrm);
|
2013-01-02 16:53:41 +00:00
|
|
|
if (is_partial_io(bvec))
|
|
|
|
kfree(uncmem);
|
2011-06-10 13:28:48 +00:00
|
|
|
if (ret)
|
2013-06-06 16:07:31 +00:00
|
|
|
atomic64_inc(&zram->stats.failed_writes);
|
2011-06-10 13:28:48 +00:00
|
|
|
return ret;
|
2011-06-10 13:28:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,
|
2014-04-07 22:38:01 +00:00
|
|
|
int offset, struct bio *bio)
|
2011-06-10 13:28:47 +00:00
|
|
|
{
|
2011-06-10 13:28:49 +00:00
|
|
|
int ret;
|
2014-04-07 22:38:01 +00:00
|
|
|
int rw = bio_data_dir(bio);
|
2011-06-10 13:28:47 +00:00
|
|
|
|
2014-04-07 22:38:01 +00:00
|
|
|
if (rw == READ) {
|
|
|
|
atomic64_inc(&zram->stats.num_reads);
|
2011-06-10 13:28:49 +00:00
|
|
|
ret = zram_bvec_read(zram, bvec, index, offset, bio);
|
2014-04-07 22:38:01 +00:00
|
|
|
} else {
|
|
|
|
atomic64_inc(&zram->stats.num_writes);
|
2011-06-10 13:28:49 +00:00
|
|
|
ret = zram_bvec_write(zram, bvec, index, offset);
|
2014-04-07 22:38:01 +00:00
|
|
|
}
|
2011-06-10 13:28:49 +00:00
|
|
|
|
|
|
|
return ret;
|
2011-06-10 13:28:48 +00:00
|
|
|
}
|
|
|
|
|
2013-08-12 06:13:55 +00:00
|
|
|
static void zram_reset_device(struct zram *zram, bool reset_capacity)
|
2011-06-10 13:28:48 +00:00
|
|
|
{
|
2013-06-22 00:21:18 +00:00
|
|
|
size_t index;
|
|
|
|
struct zram_meta *meta;
|
|
|
|
|
2013-06-26 12:28:39 +00:00
|
|
|
down_write(&zram->init_lock);
|
2014-04-07 22:38:00 +00:00
|
|
|
if (!init_done(zram)) {
|
2013-06-26 12:28:39 +00:00
|
|
|
up_write(&zram->init_lock);
|
2013-06-22 00:21:18 +00:00
|
|
|
return;
|
2013-06-26 12:28:39 +00:00
|
|
|
}
|
2013-06-22 00:21:18 +00:00
|
|
|
|
|
|
|
meta = zram->meta;
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
2014-04-07 22:38:12 +00:00
|
|
|
zcomp_destroy(zram->comp);
|
zram: add multi stream functionality
Existing zram (zcomp) implementation has only one compression stream
(buffer and algorithm private part), so in order to prevent data
corruption only one write (compress operation) can use this compression
stream, forcing all concurrent write operations to wait for stream lock
to be released. This patch changes zcomp to keep a compression streams
list of user-defined size (via sysfs device attr). Each write operation
still exclusively holds compression stream, the difference is that we
can have N write operations (depending on size of streams list)
executing in parallel. See TEST section later in commit message for
performance data.
Introduce struct zcomp_strm_multi and a set of functions to manage
zcomp_strm stream access. zcomp_strm_multi has a list of idle
zcomp_strm structs, spinlock to protect idle list and wait queue, making
it possible to perform parallel compressions.
The following set of functions added:
- zcomp_strm_multi_find()/zcomp_strm_multi_release()
find and release a compression stream, implement required locking
- zcomp_strm_multi_create()/zcomp_strm_multi_destroy()
create and destroy zcomp_strm_multi
zcomp ->strm_find() and ->strm_release() callbacks are set during
initialisation to zcomp_strm_multi_find()/zcomp_strm_multi_release()
correspondingly.
Each time zcomp issues a zcomp_strm_multi_find() call, the following set
of operations performed:
- spin lock strm_lock
- if idle list is not empty, remove zcomp_strm from idle list, spin
unlock and return zcomp stream pointer to caller
- if idle list is empty, current adds itself to wait queue. it will be
awaken by zcomp_strm_multi_release() caller.
zcomp_strm_multi_release():
- spin lock strm_lock
- add zcomp stream to idle list
- spin unlock, wake up sleeper
Minchan Kim reported that spinlock-based locking scheme has demonstrated
a severe perfomance regression for single compression stream case,
comparing to mutex-based (see https://lkml.org/lkml/2014/2/18/16)
base spinlock mutex
==Initial write ==Initial write ==Initial write
records: 5 records: 5 records: 5
avg: 1642424.35 avg: 699610.40 avg: 1655583.71
std: 39890.95(2.43%) std: 232014.19(33.16%) std: 52293.96
max: 1690170.94 max: 1163473.45 max: 1697164.75
min: 1568669.52 min: 573429.88 min: 1553410.23
==Rewrite ==Rewrite ==Rewrite
records: 5 records: 5 records: 5
avg: 1611775.39 avg: 501406.64 avg: 1684419.11
std: 17144.58(1.06%) std: 15354.41(3.06%) std: 18367.42
max: 1641800.95 max: 531356.78 max: 1706445.84
min: 1593515.27 min: 488817.78 min: 1655335.73
When only one compression stream available, mutex with spin on owner
tends to perform much better than frequent wait_event()/wake_up(). This
is why single stream implemented as a special case with mutex locking.
Introduce and document zram device attribute max_comp_streams. This
attr shows and stores current zcomp's max number of zcomp streams
(max_strm). Extend zcomp's zcomp_create() with `max_strm' parameter.
`max_strm' limits the number of zcomp_strm structs in compression
backend's idle list (max_comp_streams).
max_comp_streams used during initialisation as follows:
-- passing to zcomp_create() max_strm equals to 1 will initialise zcomp
using single compression stream zcomp_strm_single (mutex-based locking).
-- passing to zcomp_create() max_strm greater than 1 will initialise zcomp
using multi compression stream zcomp_strm_multi (spinlock-based locking).
default max_comp_streams value is 1, meaning that zram with single stream
will be initialised.
Later patch will introduce configuration knob to change max_comp_streams
on already initialised and used zcomp.
TEST
iozone -t 3 -R -r 16K -s 60M -I +Z
test base 1 strm (mutex) 3 strm (spinlock)
-----------------------------------------------------------------------
Initial write 589286.78 583518.39 718011.05
Rewrite 604837.97 596776.38 1515125.72
Random write 584120.11 595714.58 1388850.25
Pwrite 535731.17 541117.38 739295.27
Fwrite 1418083.88 1478612.72 1484927.06
Usage example:
set max_comp_streams to 4
echo 4 > /sys/block/zram0/max_comp_streams
show current max_comp_streams (default value is 1).
cat /sys/block/zram0/max_comp_streams
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>
2014-04-07 22:38:14 +00:00
|
|
|
zram->max_comp_streams = 1;
|
|
|
|
|
2013-06-22 00:21:18 +00:00
|
|
|
zram_meta_free(zram->meta);
|
|
|
|
zram->meta = NULL;
|
|
|
|
/* Reset stats */
|
|
|
|
memset(&zram->stats, 0, sizeof(zram->stats));
|
|
|
|
|
|
|
|
zram->disksize = 0;
|
2013-08-12 06:13:55 +00:00
|
|
|
if (reset_capacity)
|
|
|
|
set_capacity(zram->disk, 0);
|
2013-06-26 12:28:39 +00:00
|
|
|
up_write(&zram->init_lock);
|
2013-06-22 00:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t disksize_store(struct device *dev,
|
|
|
|
struct device_attribute *attr, const char *buf, size_t len)
|
|
|
|
{
|
|
|
|
u64 disksize;
|
2014-04-07 22:38:19 +00:00
|
|
|
struct zcomp *comp;
|
2013-06-22 00:21:18 +00:00
|
|
|
struct zram_meta *meta;
|
|
|
|
struct zram *zram = dev_to_zram(dev);
|
2014-04-07 22:38:20 +00:00
|
|
|
int err;
|
2013-06-22 00:21:18 +00:00
|
|
|
|
|
|
|
disksize = memparse(buf, NULL);
|
|
|
|
if (!disksize)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
disksize = PAGE_ALIGN(disksize);
|
|
|
|
meta = zram_meta_alloc(disksize);
|
2014-03-03 23:38:34 +00:00
|
|
|
if (!meta)
|
|
|
|
return -ENOMEM;
|
2014-04-07 22:38:09 +00:00
|
|
|
|
2014-04-07 22:38:19 +00:00
|
|
|
comp = zcomp_create(zram->compressor, zram->max_comp_streams);
|
2014-04-07 22:38:20 +00:00
|
|
|
if (IS_ERR(comp)) {
|
2014-04-07 22:38:19 +00:00
|
|
|
pr_info("Cannot initialise %s compressing backend\n",
|
|
|
|
zram->compressor);
|
2014-04-07 22:38:20 +00:00
|
|
|
err = PTR_ERR(comp);
|
|
|
|
goto out_free_meta;
|
2014-04-07 22:38:19 +00:00
|
|
|
}
|
|
|
|
|
2013-06-22 00:21:18 +00:00
|
|
|
down_write(&zram->init_lock);
|
2014-04-07 22:38:00 +00:00
|
|
|
if (init_done(zram)) {
|
2013-06-22 00:21:18 +00:00
|
|
|
pr_info("Cannot change disksize for initialized device\n");
|
2014-04-07 22:38:12 +00:00
|
|
|
err = -EBUSY;
|
2014-04-07 22:38:20 +00:00
|
|
|
goto out_destroy_comp;
|
2013-06-22 00:21:18 +00:00
|
|
|
}
|
|
|
|
|
2014-04-07 22:38:09 +00:00
|
|
|
zram->meta = meta;
|
2014-04-07 22:38:19 +00:00
|
|
|
zram->comp = comp;
|
2013-06-22 00:21:18 +00:00
|
|
|
zram->disksize = disksize;
|
|
|
|
set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
|
|
|
|
up_write(&zram->init_lock);
|
|
|
|
return len;
|
2014-04-07 22:38:12 +00:00
|
|
|
|
2014-04-07 22:38:20 +00:00
|
|
|
out_destroy_comp:
|
|
|
|
up_write(&zram->init_lock);
|
|
|
|
zcomp_destroy(comp);
|
|
|
|
out_free_meta:
|
2014-04-07 22:38:12 +00:00
|
|
|
zram_meta_free(meta);
|
|
|
|
return err;
|
2013-06-22 00:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 13:06:32 +00:00
|
|
|
if (!bdev)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2013-06-22 00:21:18 +00:00
|
|
|
/* Do not reset an active device! */
|
2013-11-10 16:43:53 +00:00
|
|
|
if (bdev->bd_holders) {
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto out;
|
|
|
|
}
|
2013-06-22 00:21:18 +00:00
|
|
|
|
|
|
|
ret = kstrtou16(buf, 10, &do_reset);
|
|
|
|
if (ret)
|
2013-11-10 16:43:53 +00:00
|
|
|
goto out;
|
2013-06-22 00:21:18 +00:00
|
|
|
|
2013-11-10 16:43:53 +00:00
|
|
|
if (!do_reset) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2013-06-22 00:21:18 +00:00
|
|
|
|
|
|
|
/* Make sure all pending I/O is finished */
|
2013-10-30 13:06:32 +00:00
|
|
|
fsync_bdev(bdev);
|
2013-11-10 16:43:53 +00:00
|
|
|
bdput(bdev);
|
2013-06-22 00:21:18 +00:00
|
|
|
|
2013-08-12 06:13:55 +00:00
|
|
|
zram_reset_device(zram, true);
|
2013-06-22 00:21:18 +00:00
|
|
|
return len;
|
2013-11-10 16:43:53 +00:00
|
|
|
|
|
|
|
out:
|
|
|
|
bdput(bdev);
|
|
|
|
return ret;
|
2011-06-10 13:28:47 +00:00
|
|
|
}
|
|
|
|
|
2014-04-07 22:38:01 +00:00
|
|
|
static void __zram_make_request(struct zram *zram, struct bio *bio)
|
2011-06-10 13:28:47 +00:00
|
|
|
{
|
2013-11-24 01:19:00 +00:00
|
|
|
int offset;
|
2011-06-10 13:28:47 +00:00
|
|
|
u32 index;
|
2013-11-24 01:19:00 +00:00
|
|
|
struct bio_vec bvec;
|
|
|
|
struct bvec_iter iter;
|
2011-06-10 13:28:47 +00:00
|
|
|
|
2013-10-11 22:44:27 +00: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 13:28:47 +00:00
|
|
|
|
2013-11-24 01:19:00 +00:00
|
|
|
bio_for_each_segment(bvec, bio, iter) {
|
2011-06-10 13:28:48 +00:00
|
|
|
int max_transfer_size = PAGE_SIZE - offset;
|
|
|
|
|
2013-11-24 01:19:00 +00:00
|
|
|
if (bvec.bv_len > max_transfer_size) {
|
2011-06-10 13:28:48 +00:00
|
|
|
/*
|
|
|
|
* zram_bvec_rw() can only make operation on a single
|
|
|
|
* zram page. Split the bio vector.
|
|
|
|
*/
|
|
|
|
struct bio_vec bv;
|
|
|
|
|
2013-11-24 01:19:00 +00:00
|
|
|
bv.bv_page = bvec.bv_page;
|
2011-06-10 13:28:48 +00:00
|
|
|
bv.bv_len = max_transfer_size;
|
2013-11-24 01:19:00 +00:00
|
|
|
bv.bv_offset = bvec.bv_offset;
|
2011-06-10 13:28:48 +00:00
|
|
|
|
2014-04-07 22:38:01 +00:00
|
|
|
if (zram_bvec_rw(zram, &bv, index, offset, bio) < 0)
|
2011-06-10 13:28:48 +00:00
|
|
|
goto out;
|
|
|
|
|
2013-11-24 01:19:00 +00:00
|
|
|
bv.bv_len = bvec.bv_len - max_transfer_size;
|
2011-06-10 13:28:48 +00:00
|
|
|
bv.bv_offset += max_transfer_size;
|
2014-04-07 22:38:01 +00:00
|
|
|
if (zram_bvec_rw(zram, &bv, index + 1, 0, bio) < 0)
|
2011-06-10 13:28:48 +00:00
|
|
|
goto out;
|
|
|
|
} else
|
2014-04-07 22:38:01 +00:00
|
|
|
if (zram_bvec_rw(zram, &bvec, index, offset, bio) < 0)
|
2011-06-10 13:28:48 +00:00
|
|
|
goto out;
|
|
|
|
|
2013-11-24 01:19:00 +00: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 08:01:23 +00:00
|
|
|
}
|
2009-09-22 04:56:53 +00:00
|
|
|
|
|
|
|
set_bit(BIO_UPTODATE, &bio->bi_flags);
|
|
|
|
bio_endio(bio, 0);
|
2011-01-22 12:36:15 +00:00
|
|
|
return;
|
2009-09-22 04:56:53 +00:00
|
|
|
|
|
|
|
out:
|
|
|
|
bio_io_error(bio);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-06-01 08:01:25 +00:00
|
|
|
* Handler function for all zram I/O requests.
|
2009-09-22 04:56:53 +00:00
|
|
|
*/
|
2011-09-12 10:12:01 +00:00
|
|
|
static void zram_make_request(struct request_queue *queue, struct bio *bio)
|
2009-09-22 04:56:53 +00:00
|
|
|
{
|
2010-06-01 08:01:25 +00:00
|
|
|
struct zram *zram = queue->queuedata;
|
2009-09-22 04:56:53 +00:00
|
|
|
|
2011-09-06 13:02:11 +00:00
|
|
|
down_read(&zram->init_lock);
|
2014-04-07 22:38:00 +00:00
|
|
|
if (unlikely(!init_done(zram)))
|
2013-01-30 02:41:41 +00:00
|
|
|
goto error;
|
2011-09-06 13:02:11 +00:00
|
|
|
|
2010-06-01 08:01:25 +00:00
|
|
|
if (!valid_io_request(zram, bio)) {
|
2013-06-06 16:07:31 +00:00
|
|
|
atomic64_inc(&zram->stats.invalid_io);
|
2013-01-30 02:41:41 +00:00
|
|
|
goto error;
|
2011-02-17 16:11:49 +00:00
|
|
|
}
|
|
|
|
|
2014-04-07 22:38:01 +00:00
|
|
|
__zram_make_request(zram, bio);
|
2011-09-06 13:02:11 +00:00
|
|
|
up_read(&zram->init_lock);
|
2009-09-22 04:56:53 +00:00
|
|
|
|
2011-11-05 00:06:58 +00:00
|
|
|
return;
|
2011-09-06 13:02:11 +00:00
|
|
|
|
|
|
|
error:
|
2013-01-30 02:41:41 +00:00
|
|
|
up_read(&zram->init_lock);
|
2011-09-06 13:02:11 +00:00
|
|
|
bio_io_error(bio);
|
2009-09-22 04:56:53 +00:00
|
|
|
}
|
|
|
|
|
2011-09-09 23:01:00 +00:00
|
|
|
static void zram_slot_free_notify(struct block_device *bdev,
|
|
|
|
unsigned long index)
|
2010-05-17 05:32:44 +00:00
|
|
|
{
|
2010-06-01 08:01:25 +00:00
|
|
|
struct zram *zram;
|
2014-01-30 23:46:04 +00:00
|
|
|
struct zram_meta *meta;
|
2010-05-17 05:32:44 +00:00
|
|
|
|
2010-06-01 08:01:25 +00:00
|
|
|
zram = bdev->bd_disk->private_data;
|
2014-01-30 23:46:04 +00:00
|
|
|
meta = zram->meta;
|
2013-08-12 06:13:56 +00:00
|
|
|
|
2014-01-30 23:46:04 +00:00
|
|
|
write_lock(&meta->tb_lock);
|
|
|
|
zram_free_page(zram, index);
|
|
|
|
write_unlock(&meta->tb_lock);
|
|
|
|
atomic64_inc(&zram->stats.notify_free);
|
2010-05-17 05:32:44 +00:00
|
|
|
}
|
|
|
|
|
2010-06-01 08:01:25 +00:00
|
|
|
static const struct block_device_operations zram_devops = {
|
|
|
|
.swap_slot_free_notify = zram_slot_free_notify,
|
2010-05-17 05:32:44 +00:00
|
|
|
.owner = THIS_MODULE
|
2009-09-22 04:56:53 +00:00
|
|
|
};
|
|
|
|
|
2013-06-22 00:21:18 +00: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(orig_data_size, S_IRUGO, orig_data_size_show, NULL);
|
|
|
|
static DEVICE_ATTR(mem_used_total, S_IRUGO, mem_used_total_show, NULL);
|
zram: add multi stream functionality
Existing zram (zcomp) implementation has only one compression stream
(buffer and algorithm private part), so in order to prevent data
corruption only one write (compress operation) can use this compression
stream, forcing all concurrent write operations to wait for stream lock
to be released. This patch changes zcomp to keep a compression streams
list of user-defined size (via sysfs device attr). Each write operation
still exclusively holds compression stream, the difference is that we
can have N write operations (depending on size of streams list)
executing in parallel. See TEST section later in commit message for
performance data.
Introduce struct zcomp_strm_multi and a set of functions to manage
zcomp_strm stream access. zcomp_strm_multi has a list of idle
zcomp_strm structs, spinlock to protect idle list and wait queue, making
it possible to perform parallel compressions.
The following set of functions added:
- zcomp_strm_multi_find()/zcomp_strm_multi_release()
find and release a compression stream, implement required locking
- zcomp_strm_multi_create()/zcomp_strm_multi_destroy()
create and destroy zcomp_strm_multi
zcomp ->strm_find() and ->strm_release() callbacks are set during
initialisation to zcomp_strm_multi_find()/zcomp_strm_multi_release()
correspondingly.
Each time zcomp issues a zcomp_strm_multi_find() call, the following set
of operations performed:
- spin lock strm_lock
- if idle list is not empty, remove zcomp_strm from idle list, spin
unlock and return zcomp stream pointer to caller
- if idle list is empty, current adds itself to wait queue. it will be
awaken by zcomp_strm_multi_release() caller.
zcomp_strm_multi_release():
- spin lock strm_lock
- add zcomp stream to idle list
- spin unlock, wake up sleeper
Minchan Kim reported that spinlock-based locking scheme has demonstrated
a severe perfomance regression for single compression stream case,
comparing to mutex-based (see https://lkml.org/lkml/2014/2/18/16)
base spinlock mutex
==Initial write ==Initial write ==Initial write
records: 5 records: 5 records: 5
avg: 1642424.35 avg: 699610.40 avg: 1655583.71
std: 39890.95(2.43%) std: 232014.19(33.16%) std: 52293.96
max: 1690170.94 max: 1163473.45 max: 1697164.75
min: 1568669.52 min: 573429.88 min: 1553410.23
==Rewrite ==Rewrite ==Rewrite
records: 5 records: 5 records: 5
avg: 1611775.39 avg: 501406.64 avg: 1684419.11
std: 17144.58(1.06%) std: 15354.41(3.06%) std: 18367.42
max: 1641800.95 max: 531356.78 max: 1706445.84
min: 1593515.27 min: 488817.78 min: 1655335.73
When only one compression stream available, mutex with spin on owner
tends to perform much better than frequent wait_event()/wake_up(). This
is why single stream implemented as a special case with mutex locking.
Introduce and document zram device attribute max_comp_streams. This
attr shows and stores current zcomp's max number of zcomp streams
(max_strm). Extend zcomp's zcomp_create() with `max_strm' parameter.
`max_strm' limits the number of zcomp_strm structs in compression
backend's idle list (max_comp_streams).
max_comp_streams used during initialisation as follows:
-- passing to zcomp_create() max_strm equals to 1 will initialise zcomp
using single compression stream zcomp_strm_single (mutex-based locking).
-- passing to zcomp_create() max_strm greater than 1 will initialise zcomp
using multi compression stream zcomp_strm_multi (spinlock-based locking).
default max_comp_streams value is 1, meaning that zram with single stream
will be initialised.
Later patch will introduce configuration knob to change max_comp_streams
on already initialised and used zcomp.
TEST
iozone -t 3 -R -r 16K -s 60M -I +Z
test base 1 strm (mutex) 3 strm (spinlock)
-----------------------------------------------------------------------
Initial write 589286.78 583518.39 718011.05
Rewrite 604837.97 596776.38 1515125.72
Random write 584120.11 595714.58 1388850.25
Pwrite 535731.17 541117.38 739295.27
Fwrite 1418083.88 1478612.72 1484927.06
Usage example:
set max_comp_streams to 4
echo 4 > /sys/block/zram0/max_comp_streams
show current max_comp_streams (default value is 1).
cat /sys/block/zram0/max_comp_streams
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>
2014-04-07 22:38:14 +00:00
|
|
|
static DEVICE_ATTR(max_comp_streams, S_IRUGO | S_IWUSR,
|
|
|
|
max_comp_streams_show, max_comp_streams_store);
|
2014-04-07 22:38:17 +00:00
|
|
|
static DEVICE_ATTR(comp_algorithm, S_IRUGO | S_IWUSR,
|
|
|
|
comp_algorithm_show, comp_algorithm_store);
|
2013-06-22 00:21:18 +00:00
|
|
|
|
2014-04-07 22:38:04 +00:00
|
|
|
ZRAM_ATTR_RO(num_reads);
|
|
|
|
ZRAM_ATTR_RO(num_writes);
|
2014-04-07 22:38:05 +00:00
|
|
|
ZRAM_ATTR_RO(failed_reads);
|
|
|
|
ZRAM_ATTR_RO(failed_writes);
|
2014-04-07 22:38:04 +00:00
|
|
|
ZRAM_ATTR_RO(invalid_io);
|
|
|
|
ZRAM_ATTR_RO(notify_free);
|
|
|
|
ZRAM_ATTR_RO(zero_pages);
|
|
|
|
ZRAM_ATTR_RO(compr_data_size);
|
|
|
|
|
2013-06-22 00:21:18 +00:00
|
|
|
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,
|
2014-04-07 22:38:05 +00:00
|
|
|
&dev_attr_failed_reads.attr,
|
|
|
|
&dev_attr_failed_writes.attr,
|
2013-06-22 00:21:18 +00:00
|
|
|
&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,
|
zram: add multi stream functionality
Existing zram (zcomp) implementation has only one compression stream
(buffer and algorithm private part), so in order to prevent data
corruption only one write (compress operation) can use this compression
stream, forcing all concurrent write operations to wait for stream lock
to be released. This patch changes zcomp to keep a compression streams
list of user-defined size (via sysfs device attr). Each write operation
still exclusively holds compression stream, the difference is that we
can have N write operations (depending on size of streams list)
executing in parallel. See TEST section later in commit message for
performance data.
Introduce struct zcomp_strm_multi and a set of functions to manage
zcomp_strm stream access. zcomp_strm_multi has a list of idle
zcomp_strm structs, spinlock to protect idle list and wait queue, making
it possible to perform parallel compressions.
The following set of functions added:
- zcomp_strm_multi_find()/zcomp_strm_multi_release()
find and release a compression stream, implement required locking
- zcomp_strm_multi_create()/zcomp_strm_multi_destroy()
create and destroy zcomp_strm_multi
zcomp ->strm_find() and ->strm_release() callbacks are set during
initialisation to zcomp_strm_multi_find()/zcomp_strm_multi_release()
correspondingly.
Each time zcomp issues a zcomp_strm_multi_find() call, the following set
of operations performed:
- spin lock strm_lock
- if idle list is not empty, remove zcomp_strm from idle list, spin
unlock and return zcomp stream pointer to caller
- if idle list is empty, current adds itself to wait queue. it will be
awaken by zcomp_strm_multi_release() caller.
zcomp_strm_multi_release():
- spin lock strm_lock
- add zcomp stream to idle list
- spin unlock, wake up sleeper
Minchan Kim reported that spinlock-based locking scheme has demonstrated
a severe perfomance regression for single compression stream case,
comparing to mutex-based (see https://lkml.org/lkml/2014/2/18/16)
base spinlock mutex
==Initial write ==Initial write ==Initial write
records: 5 records: 5 records: 5
avg: 1642424.35 avg: 699610.40 avg: 1655583.71
std: 39890.95(2.43%) std: 232014.19(33.16%) std: 52293.96
max: 1690170.94 max: 1163473.45 max: 1697164.75
min: 1568669.52 min: 573429.88 min: 1553410.23
==Rewrite ==Rewrite ==Rewrite
records: 5 records: 5 records: 5
avg: 1611775.39 avg: 501406.64 avg: 1684419.11
std: 17144.58(1.06%) std: 15354.41(3.06%) std: 18367.42
max: 1641800.95 max: 531356.78 max: 1706445.84
min: 1593515.27 min: 488817.78 min: 1655335.73
When only one compression stream available, mutex with spin on owner
tends to perform much better than frequent wait_event()/wake_up(). This
is why single stream implemented as a special case with mutex locking.
Introduce and document zram device attribute max_comp_streams. This
attr shows and stores current zcomp's max number of zcomp streams
(max_strm). Extend zcomp's zcomp_create() with `max_strm' parameter.
`max_strm' limits the number of zcomp_strm structs in compression
backend's idle list (max_comp_streams).
max_comp_streams used during initialisation as follows:
-- passing to zcomp_create() max_strm equals to 1 will initialise zcomp
using single compression stream zcomp_strm_single (mutex-based locking).
-- passing to zcomp_create() max_strm greater than 1 will initialise zcomp
using multi compression stream zcomp_strm_multi (spinlock-based locking).
default max_comp_streams value is 1, meaning that zram with single stream
will be initialised.
Later patch will introduce configuration knob to change max_comp_streams
on already initialised and used zcomp.
TEST
iozone -t 3 -R -r 16K -s 60M -I +Z
test base 1 strm (mutex) 3 strm (spinlock)
-----------------------------------------------------------------------
Initial write 589286.78 583518.39 718011.05
Rewrite 604837.97 596776.38 1515125.72
Random write 584120.11 595714.58 1388850.25
Pwrite 535731.17 541117.38 739295.27
Fwrite 1418083.88 1478612.72 1484927.06
Usage example:
set max_comp_streams to 4
echo 4 > /sys/block/zram0/max_comp_streams
show current max_comp_streams (default value is 1).
cat /sys/block/zram0/max_comp_streams
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>
2014-04-07 22:38:14 +00:00
|
|
|
&dev_attr_max_comp_streams.attr,
|
2014-04-07 22:38:17 +00:00
|
|
|
&dev_attr_comp_algorithm.attr,
|
2013-06-22 00:21:18 +00:00
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct attribute_group zram_disk_attr_group = {
|
|
|
|
.attrs = zram_disk_attrs,
|
|
|
|
};
|
|
|
|
|
2010-06-01 08:01:25 +00:00
|
|
|
static int create_device(struct zram *zram, int device_id)
|
2009-09-22 04:56:53 +00:00
|
|
|
{
|
2013-06-06 16:07:24 +00:00
|
|
|
int ret = -ENOMEM;
|
2010-01-28 15:43:40 +00:00
|
|
|
|
2011-09-06 13:02:11 +00:00
|
|
|
init_rwsem(&zram->init_lock);
|
2009-09-22 04:56:53 +00:00
|
|
|
|
2010-06-01 08:01:25 +00:00
|
|
|
zram->queue = blk_alloc_queue(GFP_KERNEL);
|
|
|
|
if (!zram->queue) {
|
2009-09-22 04:56:53 +00:00
|
|
|
pr_err("Error allocating disk queue for device %d\n",
|
|
|
|
device_id);
|
2010-01-28 15:43:40 +00:00
|
|
|
goto out;
|
2009-09-22 04:56:53 +00:00
|
|
|
}
|
|
|
|
|
2010-06-01 08:01:25 +00:00
|
|
|
blk_queue_make_request(zram->queue, zram_make_request);
|
|
|
|
zram->queue->queuedata = zram;
|
2009-09-22 04:56:53 +00:00
|
|
|
|
|
|
|
/* gendisk structure */
|
2010-06-01 08:01:25 +00:00
|
|
|
zram->disk = alloc_disk(1);
|
|
|
|
if (!zram->disk) {
|
2012-06-07 23:03:47 +00:00
|
|
|
pr_warn("Error allocating disk structure for device %d\n",
|
2009-09-22 04:56:53 +00:00
|
|
|
device_id);
|
2013-06-06 16:07:24 +00:00
|
|
|
goto out_free_queue;
|
2009-09-22 04:56:53 +00:00
|
|
|
}
|
|
|
|
|
2010-06-01 08:01:25 +00: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-22 04:56:53 +00:00
|
|
|
|
2010-08-09 17:26:47 +00:00
|
|
|
/* Actual capacity set using syfs (/sys/block/zram<id>/disksize */
|
2010-06-01 08:01:25 +00:00
|
|
|
set_capacity(zram->disk, 0);
|
2014-04-07 22:38:09 +00:00
|
|
|
/* zram devices sort of resembles non-rotational disks */
|
|
|
|
queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zram->disk->queue);
|
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 08:01:23 +00:00
|
|
|
/*
|
|
|
|
* To ensure that we always get PAGE_SIZE aligned
|
|
|
|
* and n*PAGE_SIZED sized I/O requests.
|
|
|
|
*/
|
2010-06-01 08:01:25 +00:00
|
|
|
blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE);
|
2011-01-28 14:58:17 +00:00
|
|
|
blk_queue_logical_block_size(zram->disk->queue,
|
|
|
|
ZRAM_LOGICAL_BLOCK_SIZE);
|
2010-06-01 08:01:25 +00:00
|
|
|
blk_queue_io_min(zram->disk->queue, PAGE_SIZE);
|
|
|
|
blk_queue_io_opt(zram->disk->queue, PAGE_SIZE);
|
2010-01-28 15:43:39 +00:00
|
|
|
|
2010-06-01 08:01:25 +00:00
|
|
|
add_disk(zram->disk);
|
2009-09-22 04:56:53 +00:00
|
|
|
|
2010-08-09 17:26:47 +00:00
|
|
|
ret = sysfs_create_group(&disk_to_dev(zram->disk)->kobj,
|
|
|
|
&zram_disk_attr_group);
|
|
|
|
if (ret < 0) {
|
2012-06-07 23:03:47 +00:00
|
|
|
pr_warn("Error creating sysfs group");
|
2013-06-06 16:07:24 +00:00
|
|
|
goto out_free_disk;
|
2010-08-09 17:26:47 +00:00
|
|
|
}
|
2014-04-07 22:38:17 +00:00
|
|
|
strlcpy(zram->compressor, default_compressor, sizeof(zram->compressor));
|
2014-04-07 22:38:00 +00:00
|
|
|
zram->meta = NULL;
|
zram: add multi stream functionality
Existing zram (zcomp) implementation has only one compression stream
(buffer and algorithm private part), so in order to prevent data
corruption only one write (compress operation) can use this compression
stream, forcing all concurrent write operations to wait for stream lock
to be released. This patch changes zcomp to keep a compression streams
list of user-defined size (via sysfs device attr). Each write operation
still exclusively holds compression stream, the difference is that we
can have N write operations (depending on size of streams list)
executing in parallel. See TEST section later in commit message for
performance data.
Introduce struct zcomp_strm_multi and a set of functions to manage
zcomp_strm stream access. zcomp_strm_multi has a list of idle
zcomp_strm structs, spinlock to protect idle list and wait queue, making
it possible to perform parallel compressions.
The following set of functions added:
- zcomp_strm_multi_find()/zcomp_strm_multi_release()
find and release a compression stream, implement required locking
- zcomp_strm_multi_create()/zcomp_strm_multi_destroy()
create and destroy zcomp_strm_multi
zcomp ->strm_find() and ->strm_release() callbacks are set during
initialisation to zcomp_strm_multi_find()/zcomp_strm_multi_release()
correspondingly.
Each time zcomp issues a zcomp_strm_multi_find() call, the following set
of operations performed:
- spin lock strm_lock
- if idle list is not empty, remove zcomp_strm from idle list, spin
unlock and return zcomp stream pointer to caller
- if idle list is empty, current adds itself to wait queue. it will be
awaken by zcomp_strm_multi_release() caller.
zcomp_strm_multi_release():
- spin lock strm_lock
- add zcomp stream to idle list
- spin unlock, wake up sleeper
Minchan Kim reported that spinlock-based locking scheme has demonstrated
a severe perfomance regression for single compression stream case,
comparing to mutex-based (see https://lkml.org/lkml/2014/2/18/16)
base spinlock mutex
==Initial write ==Initial write ==Initial write
records: 5 records: 5 records: 5
avg: 1642424.35 avg: 699610.40 avg: 1655583.71
std: 39890.95(2.43%) std: 232014.19(33.16%) std: 52293.96
max: 1690170.94 max: 1163473.45 max: 1697164.75
min: 1568669.52 min: 573429.88 min: 1553410.23
==Rewrite ==Rewrite ==Rewrite
records: 5 records: 5 records: 5
avg: 1611775.39 avg: 501406.64 avg: 1684419.11
std: 17144.58(1.06%) std: 15354.41(3.06%) std: 18367.42
max: 1641800.95 max: 531356.78 max: 1706445.84
min: 1593515.27 min: 488817.78 min: 1655335.73
When only one compression stream available, mutex with spin on owner
tends to perform much better than frequent wait_event()/wake_up(). This
is why single stream implemented as a special case with mutex locking.
Introduce and document zram device attribute max_comp_streams. This
attr shows and stores current zcomp's max number of zcomp streams
(max_strm). Extend zcomp's zcomp_create() with `max_strm' parameter.
`max_strm' limits the number of zcomp_strm structs in compression
backend's idle list (max_comp_streams).
max_comp_streams used during initialisation as follows:
-- passing to zcomp_create() max_strm equals to 1 will initialise zcomp
using single compression stream zcomp_strm_single (mutex-based locking).
-- passing to zcomp_create() max_strm greater than 1 will initialise zcomp
using multi compression stream zcomp_strm_multi (spinlock-based locking).
default max_comp_streams value is 1, meaning that zram with single stream
will be initialised.
Later patch will introduce configuration knob to change max_comp_streams
on already initialised and used zcomp.
TEST
iozone -t 3 -R -r 16K -s 60M -I +Z
test base 1 strm (mutex) 3 strm (spinlock)
-----------------------------------------------------------------------
Initial write 589286.78 583518.39 718011.05
Rewrite 604837.97 596776.38 1515125.72
Random write 584120.11 595714.58 1388850.25
Pwrite 535731.17 541117.38 739295.27
Fwrite 1418083.88 1478612.72 1484927.06
Usage example:
set max_comp_streams to 4
echo 4 > /sys/block/zram0/max_comp_streams
show current max_comp_streams (default value is 1).
cat /sys/block/zram0/max_comp_streams
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>
2014-04-07 22:38:14 +00:00
|
|
|
zram->max_comp_streams = 1;
|
2013-06-06 16:07:24 +00:00
|
|
|
return 0;
|
2010-01-28 15:43:40 +00:00
|
|
|
|
2013-06-06 16:07:24 +00:00
|
|
|
out_free_disk:
|
|
|
|
del_gendisk(zram->disk);
|
|
|
|
put_disk(zram->disk);
|
|
|
|
out_free_queue:
|
|
|
|
blk_cleanup_queue(zram->queue);
|
2010-01-28 15:43:40 +00:00
|
|
|
out:
|
|
|
|
return ret;
|
2009-09-22 04:56:53 +00:00
|
|
|
}
|
|
|
|
|
2010-06-01 08:01:25 +00:00
|
|
|
static void destroy_device(struct zram *zram)
|
2009-09-22 04:56:53 +00:00
|
|
|
{
|
2010-08-09 17:26:47 +00:00
|
|
|
sysfs_remove_group(&disk_to_dev(zram->disk)->kobj,
|
|
|
|
&zram_disk_attr_group);
|
|
|
|
|
2013-10-30 13:13:32 +00:00
|
|
|
del_gendisk(zram->disk);
|
|
|
|
put_disk(zram->disk);
|
2009-09-22 04:56:53 +00:00
|
|
|
|
2013-10-30 13:13:32 +00:00
|
|
|
blk_cleanup_queue(zram->queue);
|
2009-09-22 04:56:53 +00:00
|
|
|
}
|
|
|
|
|
2010-06-01 08:01:25 +00:00
|
|
|
static int __init zram_init(void)
|
2009-09-22 04:56:53 +00:00
|
|
|
{
|
2010-01-28 15:43:40 +00:00
|
|
|
int ret, dev_id;
|
2009-09-22 04:56:53 +00:00
|
|
|
|
2012-02-13 04:04:45 +00:00
|
|
|
if (num_devices > max_num_devices) {
|
2012-06-07 23:03:47 +00:00
|
|
|
pr_warn("Invalid value for num_devices: %u\n",
|
2012-02-13 04:04:45 +00:00
|
|
|
num_devices);
|
2010-01-28 15:43:40 +00:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
2009-09-22 04:56:53 +00:00
|
|
|
}
|
|
|
|
|
2010-06-01 08:01:25 +00:00
|
|
|
zram_major = register_blkdev(0, "zram");
|
|
|
|
if (zram_major <= 0) {
|
2012-06-07 23:03:47 +00:00
|
|
|
pr_warn("Unable to get major number\n");
|
2010-01-28 15:43:40 +00:00
|
|
|
ret = -EBUSY;
|
|
|
|
goto out;
|
2009-09-22 04:56:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate the device array and initialize each one */
|
2012-02-13 04:04:45 +00:00
|
|
|
zram_devices = kzalloc(num_devices * sizeof(struct zram), GFP_KERNEL);
|
2011-07-20 23:05:57 +00:00
|
|
|
if (!zram_devices) {
|
2010-01-28 15:43:40 +00:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto unregister;
|
|
|
|
}
|
2009-09-22 04:56:53 +00:00
|
|
|
|
2012-02-13 04:04:45 +00:00
|
|
|
for (dev_id = 0; dev_id < num_devices; dev_id++) {
|
2011-07-20 23:05:57 +00:00
|
|
|
ret = create_device(&zram_devices[dev_id], dev_id);
|
2010-01-28 15:43:40 +00:00
|
|
|
if (ret)
|
2010-01-11 07:15:53 +00:00
|
|
|
goto free_devices;
|
2010-01-28 15:43:40 +00:00
|
|
|
}
|
|
|
|
|
2013-01-02 05:24:13 +00:00
|
|
|
pr_info("Created %u device(s) ...\n", num_devices);
|
|
|
|
|
2009-09-22 04:56:53 +00:00
|
|
|
return 0;
|
2010-01-28 15:43:40 +00:00
|
|
|
|
2010-01-11 07:15:53 +00:00
|
|
|
free_devices:
|
2010-01-28 15:43:40 +00:00
|
|
|
while (dev_id)
|
2011-07-20 23:05:57 +00:00
|
|
|
destroy_device(&zram_devices[--dev_id]);
|
|
|
|
kfree(zram_devices);
|
2010-01-28 15:43:40 +00:00
|
|
|
unregister:
|
2010-06-01 08:01:25 +00:00
|
|
|
unregister_blkdev(zram_major, "zram");
|
2010-01-28 15:43:40 +00:00
|
|
|
out:
|
2009-09-22 04:56:53 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-06-01 08:01:25 +00:00
|
|
|
static void __exit zram_exit(void)
|
2009-09-22 04:56:53 +00:00
|
|
|
{
|
|
|
|
int i;
|
2010-06-01 08:01:25 +00:00
|
|
|
struct zram *zram;
|
2009-09-22 04:56:53 +00:00
|
|
|
|
2012-02-13 04:04:45 +00:00
|
|
|
for (i = 0; i < num_devices; i++) {
|
2011-07-20 23:05:57 +00:00
|
|
|
zram = &zram_devices[i];
|
2009-09-22 04:56:53 +00:00
|
|
|
|
2010-06-01 08:01:25 +00:00
|
|
|
destroy_device(zram);
|
2013-08-12 06:13:55 +00:00
|
|
|
/*
|
|
|
|
* Shouldn't access zram->disk after destroy_device
|
|
|
|
* because destroy_device already released zram->disk.
|
|
|
|
*/
|
|
|
|
zram_reset_device(zram, false);
|
2009-09-22 04:56:53 +00:00
|
|
|
}
|
|
|
|
|
2010-06-01 08:01:25 +00:00
|
|
|
unregister_blkdev(zram_major, "zram");
|
2009-09-22 04:56:53 +00:00
|
|
|
|
2011-07-20 23:05:57 +00:00
|
|
|
kfree(zram_devices);
|
2009-09-22 04:56:53 +00:00
|
|
|
pr_debug("Cleanup done!\n");
|
|
|
|
}
|
|
|
|
|
2010-06-01 08:01:25 +00:00
|
|
|
module_init(zram_init);
|
|
|
|
module_exit(zram_exit);
|
2009-09-22 04:56:53 +00:00
|
|
|
|
2013-06-22 00:21:18 +00:00
|
|
|
module_param(num_devices, uint, 0);
|
|
|
|
MODULE_PARM_DESC(num_devices, "Number of zram devices");
|
|
|
|
|
2009-09-22 04:56:53 +00:00
|
|
|
MODULE_LICENSE("Dual BSD/GPL");
|
|
|
|
MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");
|
2010-06-01 08:01:25 +00:00
|
|
|
MODULE_DESCRIPTION("Compressed RAM Block Device");
|