Skip to content

Commit e46b8a0

Browse files
sergey-senozhatskytorvalds
authored andcommitted
zram: make compression algorithm selection possible
Add and document `comp_algorithm' device attribute. This attribute allows to show supported compression and currently selected compression algorithms: cat /sys/block/zram0/comp_algorithm [lzo] lz4 and change selected compression algorithm: echo lzo > /sys/block/zram0/comp_algorithm 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>
1 parent fe8eb12 commit e46b8a0

6 files changed

Lines changed: 93 additions & 11 deletions

File tree

‎Documentation/ABI/testing/sysfs-block-zram‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ Description:
6565
number of backend's zcomp_strm compression streams (number of
6666
concurrent compress operations).
6767

68+
What: /sys/block/zram<id>/comp_algorithm
69+
Date: February 2014
70+
Contact: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
71+
Description:
72+
The comp_algorithm file is read-write and lets to show
73+
available and selected compression algorithms, change
74+
compression algorithm selection.
75+
6876
What: /sys/block/zram<id>/notify_free
6977
Date: August 2010
7078
Contact: Nitin Gupta <ngupta@vflare.org>

‎Documentation/blockdev/zram.txt‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,21 @@ will not take any effect, because single stream compression backend implemented
4242
as a special case and does not support dynamic max_comp_streams. Only multi
4343
stream backend supports dynamic max_comp_streams adjustment.
4444

45-
3) Set Disksize
45+
3) Select compression algorithm
46+
Using comp_algorithm device attribute one can see available and
47+
currently selected (shown in square brackets) compression algortithms,
48+
change selected compression algorithm (once the device is initialised
49+
there is no way to change compression algorithm).
50+
51+
Examples:
52+
#show supported compression algorithms
53+
cat /sys/block/zram0/comp_algorithm
54+
lzo [lz4]
55+
56+
#select lzo compression algorithm
57+
echo lzo > /sys/block/zram0/comp_algorithm
58+
59+
4) Set Disksize
4660
Set disk size by writing the value to sysfs node 'disksize'.
4761
The value can be either in bytes or you can use mem suffixes.
4862
Examples:
@@ -59,14 +73,14 @@ There is little point creating a zram of greater than twice the size of memory
5973
since we expect a 2:1 compression ratio. Note that zram uses about 0.1% of the
6074
size of the disk when not in use so a huge zram is wasteful.
6175

62-
4) Activate:
76+
5) Activate:
6377
mkswap /dev/zram0
6478
swapon /dev/zram0
6579

6680
mkfs.ext4 /dev/zram1
6781
mount /dev/zram1 /tmp
6882

69-
5) Stats:
83+
6) Stats:
7084
Per-device statistics are exported as various nodes under
7185
/sys/block/zram<id>/
7286
disksize
@@ -81,11 +95,11 @@ size of the disk when not in use so a huge zram is wasteful.
8195
compr_data_size
8296
mem_used_total
8397

84-
6) Deactivate:
98+
7) Deactivate:
8599
swapoff /dev/zram0
86100
umount /dev/zram1
87101

88-
7) Reset:
102+
8) Reset:
89103
Write any positive value to 'reset' sysfs node
90104
echo 1 > /sys/block/zram0/reset
91105
echo 1 > /sys/block/zram1/reset

‎drivers/block/zram/zcomp.c‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,20 @@ struct zcomp_strm_multi {
3939
wait_queue_head_t strm_wait;
4040
};
4141

42+
static struct zcomp_backend *backends[] = {
43+
&zcomp_lzo,
44+
NULL
45+
};
46+
4247
static struct zcomp_backend *find_backend(const char *compress)
4348
{
44-
if (strncmp(compress, "lzo", 3) == 0)
45-
return &zcomp_lzo;
46-
return NULL;
49+
int i = 0;
50+
while (backends[i]) {
51+
if (sysfs_streq(compress, backends[i]->name))
52+
break;
53+
i++;
54+
}
55+
return backends[i];
4756
}
4857

4958
static void zcomp_strm_free(struct zcomp *comp, struct zcomp_strm *zstrm)
@@ -251,6 +260,23 @@ static int zcomp_strm_single_create(struct zcomp *comp)
251260
return 0;
252261
}
253262

263+
/* show available compressors */
264+
ssize_t zcomp_available_show(const char *comp, char *buf)
265+
{
266+
ssize_t sz = 0;
267+
int i = 0;
268+
269+
while (backends[i]) {
270+
if (sysfs_streq(comp, backends[i]->name))
271+
sz += sprintf(buf + sz, "[%s] ", backends[i]->name);
272+
else
273+
sz += sprintf(buf + sz, "%s ", backends[i]->name);
274+
i++;
275+
}
276+
sz += sprintf(buf + sz, "\n");
277+
return sz;
278+
}
279+
254280
int zcomp_set_max_streams(struct zcomp *comp, int num_strm)
255281
{
256282
return comp->set_max_streams(comp, num_strm);

‎drivers/block/zram/zcomp.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ struct zcomp {
5050
void (*destroy)(struct zcomp *comp);
5151
};
5252

53+
ssize_t zcomp_available_show(const char *comp, char *buf);
54+
5355
struct zcomp *zcomp_create(const char *comp, int max_strm);
5456
void zcomp_destroy(struct zcomp *comp);
5557

‎drivers/block/zram/zram_drv.c‎

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,34 @@ static ssize_t max_comp_streams_store(struct device *dev,
141141
return len;
142142
}
143143

144+
static ssize_t comp_algorithm_show(struct device *dev,
145+
struct device_attribute *attr, char *buf)
146+
{
147+
size_t sz;
148+
struct zram *zram = dev_to_zram(dev);
149+
150+
down_read(&zram->init_lock);
151+
sz = zcomp_available_show(zram->compressor, buf);
152+
up_read(&zram->init_lock);
153+
154+
return sz;
155+
}
156+
157+
static ssize_t comp_algorithm_store(struct device *dev,
158+
struct device_attribute *attr, const char *buf, size_t len)
159+
{
160+
struct zram *zram = dev_to_zram(dev);
161+
down_write(&zram->init_lock);
162+
if (init_done(zram)) {
163+
up_write(&zram->init_lock);
164+
pr_info("Can't change algorithm for initialized device\n");
165+
return -EBUSY;
166+
}
167+
strlcpy(zram->compressor, buf, sizeof(zram->compressor));
168+
up_write(&zram->init_lock);
169+
return len;
170+
}
171+
144172
/* flag operations needs meta->tb_lock */
145173
static int zram_test_flag(struct zram_meta *meta, u32 index,
146174
enum zram_pageflags flag)
@@ -572,10 +600,10 @@ static ssize_t disksize_store(struct device *dev,
572600
goto out_free_meta;
573601
}
574602

575-
zram->comp = zcomp_create(default_compressor, zram->max_comp_streams);
603+
zram->comp = zcomp_create(zram->compressor, zram->max_comp_streams);
576604
if (!zram->comp) {
577605
pr_info("Cannot initialise %s compressing backend\n",
578-
default_compressor);
606+
zram->compressor);
579607
err = -EINVAL;
580608
goto out_free_meta;
581609
}
@@ -735,6 +763,8 @@ static DEVICE_ATTR(orig_data_size, S_IRUGO, orig_data_size_show, NULL);
735763
static DEVICE_ATTR(mem_used_total, S_IRUGO, mem_used_total_show, NULL);
736764
static DEVICE_ATTR(max_comp_streams, S_IRUGO | S_IWUSR,
737765
max_comp_streams_show, max_comp_streams_store);
766+
static DEVICE_ATTR(comp_algorithm, S_IRUGO | S_IWUSR,
767+
comp_algorithm_show, comp_algorithm_store);
738768

739769
ZRAM_ATTR_RO(num_reads);
740770
ZRAM_ATTR_RO(num_writes);
@@ -760,6 +790,7 @@ static struct attribute *zram_disk_attrs[] = {
760790
&dev_attr_compr_data_size.attr,
761791
&dev_attr_mem_used_total.attr,
762792
&dev_attr_max_comp_streams.attr,
793+
&dev_attr_comp_algorithm.attr,
763794
NULL,
764795
};
765796

@@ -820,7 +851,7 @@ static int create_device(struct zram *zram, int device_id)
820851
pr_warn("Error creating sysfs group");
821852
goto out_free_disk;
822853
}
823-
854+
strlcpy(zram->compressor, default_compressor, sizeof(zram->compressor));
824855
zram->meta = NULL;
825856
zram->max_comp_streams = 1;
826857
return 0;

‎drivers/block/zram/zram_drv.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,6 @@ struct zram {
101101
u64 disksize; /* bytes */
102102
int max_comp_streams;
103103
struct zram_stats stats;
104+
char compressor[10];
104105
};
105106
#endif

0 commit comments

Comments
 (0)