Skip to content

Commit 6566d1a

Browse files
sergey-senozhatskytorvalds
authored andcommitted
zram: add dynamic device add/remove functionality
We currently don't support on-demand device creation. The one and only way to have N zram devices is to specify num_devices module parameter (default value: 1). IOW if, for some reason, at some point, user wants to have N + 1 devies he/she must umount all the existing devices, unload the module, load the module passing num_devices equals to N + 1. And do this again, if needed. This patch introduces zram control sysfs class, which has two sysfs attrs: - hot_add -- add a new zram device - hot_remove -- remove a specific (device_id) zram device hot_add sysfs attr is read-only and has only automatic device id assignment mode (as requested by Minchan Kim). read operation performed on this attr creates a new zram device and returns back its device_id or error status. Usage example: # add a new specific zram device cat /sys/class/zram-control/hot_add 2 # remove a specific zram device echo 4 > /sys/class/zram-control/hot_remove Returning zram_add() error code back to user (-ENOMEM in this case) cat /sys/class/zram-control/hot_add cat: /sys/class/zram-control/hot_add: Cannot allocate memory NOTE, there might be users who already depend on the fact that at least zram0 device gets always created by zram_init(). Preserve this behavior. [minchan@kernel.org: use zram->claim to avoid lockdep splat] Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com> Cc: Minchan Kim <minchan@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent f405c44 commit 6566d1a

3 files changed

Lines changed: 141 additions & 6 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
What: /sys/class/zram-control/
2+
Date: August 2015
3+
KernelVersion: 4.2
4+
Contact: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
5+
Description:
6+
The zram-control/ class sub-directory belongs to zram
7+
device class
8+
9+
What: /sys/class/zram-control/hot_add
10+
Date: August 2015
11+
KernelVersion: 4.2
12+
Contact: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
13+
Description:
14+
RO attribute. Read operation will cause zram to add a new
15+
device and return its device id back to user (so one can
16+
use /dev/zram<id>), or error code.
17+
18+
What: /sys/class/zram-control/hot_remove
19+
Date: August 2015
20+
KernelVersion: 4.2
21+
Contact: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
22+
Description:
23+
WO attribute. Remove a specific /dev/zramX device, where X
24+
is a device_id provided by user.

‎Documentation/blockdev/zram.txt‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,24 @@ size of the disk when not in use so a huge zram is wasteful.
9999
mkfs.ext4 /dev/zram1
100100
mount /dev/zram1 /tmp
101101

102-
7) Stats:
102+
7) Add/remove zram devices
103+
104+
zram provides a control interface, which enables dynamic (on-demand) device
105+
addition and removal.
106+
107+
In order to add a new /dev/zramX device, perform read operation on hot_add
108+
attribute. This will return either new device's device id (meaning that you
109+
can use /dev/zram<id>) or error code.
110+
111+
Example:
112+
cat /sys/class/zram-control/hot_add
113+
1
114+
115+
To remove the existing /dev/zramX device (where X is a device id)
116+
execute
117+
echo X > /sys/class/zram-control/hot_remove
118+
119+
8) Stats:
103120
Per-device statistics are exported as various nodes under /sys/block/zram<id>/
104121

105122
A brief description of exported device attritbutes. For more details please
@@ -174,11 +191,11 @@ line of text and contains the following stats separated by whitespace:
174191
zero_pages
175192
num_migrated
176193

177-
8) Deactivate:
194+
9) Deactivate:
178195
swapoff /dev/zram0
179196
umount /dev/zram1
180197

181-
9) Reset:
198+
10) Reset:
182199
Write any positive value to 'reset' sysfs node
183200
echo 1 > /sys/block/zram0/reset
184201
echo 1 > /sys/block/zram1/reset

‎drivers/block/zram/zram_drv.c‎

Lines changed: 97 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@
2929
#include <linux/vmalloc.h>
3030
#include <linux/err.h>
3131
#include <linux/idr.h>
32+
#include <linux/sysfs.h>
3233

3334
#include "zram_drv.h"
3435

3536
static DEFINE_IDR(zram_index_idr);
37+
/* idr index must be protected */
38+
static DEFINE_MUTEX(zram_index_mutex);
39+
3640
static int zram_major;
3741
static const char *default_compressor = "lzo";
3842

@@ -1273,24 +1277,104 @@ static int zram_add(void)
12731277
return ret;
12741278
}
12751279

1276-
static void zram_remove(struct zram *zram)
1280+
static int zram_remove(struct zram *zram)
12771281
{
1278-
pr_info("Removed device: %s\n", zram->disk->disk_name);
1282+
struct block_device *bdev;
1283+
1284+
bdev = bdget_disk(zram->disk, 0);
1285+
if (!bdev)
1286+
return -ENOMEM;
1287+
1288+
mutex_lock(&bdev->bd_mutex);
1289+
if (bdev->bd_openers || zram->claim) {
1290+
mutex_unlock(&bdev->bd_mutex);
1291+
bdput(bdev);
1292+
return -EBUSY;
1293+
}
1294+
1295+
zram->claim = true;
1296+
mutex_unlock(&bdev->bd_mutex);
1297+
12791298
/*
12801299
* Remove sysfs first, so no one will perform a disksize
1281-
* store while we destroy the devices
1300+
* store while we destroy the devices. This also helps during
1301+
* hot_remove -- zram_reset_device() is the last holder of
1302+
* ->init_lock, no later/concurrent disksize_store() or any
1303+
* other sysfs handlers are possible.
12821304
*/
12831305
sysfs_remove_group(&disk_to_dev(zram->disk)->kobj,
12841306
&zram_disk_attr_group);
12851307

1308+
/* Make sure all the pending I/O are finished */
1309+
fsync_bdev(bdev);
12861310
zram_reset_device(zram);
1311+
bdput(bdev);
1312+
1313+
pr_info("Removed device: %s\n", zram->disk->disk_name);
1314+
12871315
idr_remove(&zram_index_idr, zram->disk->first_minor);
12881316
blk_cleanup_queue(zram->disk->queue);
12891317
del_gendisk(zram->disk);
12901318
put_disk(zram->disk);
12911319
kfree(zram);
1320+
return 0;
1321+
}
1322+
1323+
/* zram-control sysfs attributes */
1324+
static ssize_t hot_add_show(struct class *class,
1325+
struct class_attribute *attr,
1326+
char *buf)
1327+
{
1328+
int ret;
1329+
1330+
mutex_lock(&zram_index_mutex);
1331+
ret = zram_add();
1332+
mutex_unlock(&zram_index_mutex);
1333+
1334+
if (ret < 0)
1335+
return ret;
1336+
return scnprintf(buf, PAGE_SIZE, "%d\n", ret);
1337+
}
1338+
1339+
static ssize_t hot_remove_store(struct class *class,
1340+
struct class_attribute *attr,
1341+
const char *buf,
1342+
size_t count)
1343+
{
1344+
struct zram *zram;
1345+
int ret, dev_id;
1346+
1347+
/* dev_id is gendisk->first_minor, which is `int' */
1348+
ret = kstrtoint(buf, 10, &dev_id);
1349+
if (ret)
1350+
return ret;
1351+
if (dev_id < 0)
1352+
return -EINVAL;
1353+
1354+
mutex_lock(&zram_index_mutex);
1355+
1356+
zram = idr_find(&zram_index_idr, dev_id);
1357+
if (zram)
1358+
ret = zram_remove(zram);
1359+
else
1360+
ret = -ENODEV;
1361+
1362+
mutex_unlock(&zram_index_mutex);
1363+
return ret ? ret : count;
12921364
}
12931365

1366+
static struct class_attribute zram_control_class_attrs[] = {
1367+
__ATTR_RO(hot_add),
1368+
__ATTR_WO(hot_remove),
1369+
__ATTR_NULL,
1370+
};
1371+
1372+
static struct class zram_control_class = {
1373+
.name = "zram-control",
1374+
.owner = THIS_MODULE,
1375+
.class_attrs = zram_control_class_attrs,
1376+
};
1377+
12941378
static int zram_remove_cb(int id, void *ptr, void *data)
12951379
{
12961380
zram_remove(ptr);
@@ -1299,6 +1383,7 @@ static int zram_remove_cb(int id, void *ptr, void *data)
12991383

13001384
static void destroy_devices(void)
13011385
{
1386+
class_unregister(&zram_control_class);
13021387
idr_for_each(&zram_index_idr, &zram_remove_cb, NULL);
13031388
idr_destroy(&zram_index_idr);
13041389
unregister_blkdev(zram_major, "zram");
@@ -1308,14 +1393,23 @@ static int __init zram_init(void)
13081393
{
13091394
int ret;
13101395

1396+
ret = class_register(&zram_control_class);
1397+
if (ret) {
1398+
pr_warn("Unable to register zram-control class\n");
1399+
return ret;
1400+
}
1401+
13111402
zram_major = register_blkdev(0, "zram");
13121403
if (zram_major <= 0) {
13131404
pr_warn("Unable to get major number\n");
1405+
class_unregister(&zram_control_class);
13141406
return -EBUSY;
13151407
}
13161408

13171409
while (num_devices != 0) {
1410+
mutex_lock(&zram_index_mutex);
13181411
ret = zram_add();
1412+
mutex_unlock(&zram_index_mutex);
13191413
if (ret < 0)
13201414
goto out_error;
13211415
num_devices--;

0 commit comments

Comments
 (0)