* [PATCH 1/2] staging: zram: fix zram locking
@ 2011-09-06 13:02 Jerome Marchand
2011-09-06 13:02 ` [PATCH 2/2] staging: zram: prevent accessing an unallocated table when init fails early Jerome Marchand
2011-09-08 1:35 ` [PATCH 1/2] staging: zram: fix zram locking Nitin Gupta
0 siblings, 2 replies; 4+ messages in thread
From: Jerome Marchand @ 2011-09-06 13:02 UTC (permalink / raw)
To: Greg Kroah-Hartman; +Cc: Linux Kernel List, Nitin Gupta, Jeff Moyer
Currently init_lock only prevents concurrent execution of zram_init_device()
and zram_reset_device() but not zram_make_request() nor sysfs store functions.
This patch changes init_lock into a rw_semaphore. A write lock is taken by
init, reset and store functions, a read lock is taken by zram_make_request().
Also, avoids to release the lock before calling __zram_reset_device() for
cleaning after a failed init, thus preventing any concurrent task to see an
inconsistent state of zram.
Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
---
drivers/staging/zram/zram_drv.c | 46 +++++++++++++++++++++++-------------
drivers/staging/zram/zram_drv.h | 6 ++--
drivers/staging/zram/zram_sysfs.c | 18 +++++++++-----
3 files changed, 44 insertions(+), 26 deletions(-)
diff --git a/drivers/staging/zram/zram_drv.c b/drivers/staging/zram/zram_drv.c
index d70ec1a..2d1c8bd 100644
--- a/drivers/staging/zram/zram_drv.c
+++ b/drivers/staging/zram/zram_drv.c
@@ -560,27 +560,34 @@ static int zram_make_request(struct request_queue *queue, struct bio *bio)
{
struct zram *zram = queue->queuedata;
+ if (unlikely(!zram->init_done) && zram_init_device(zram))
+ goto error;
+
+ down_read(&zram->init_lock);
+ if (unlikely(!zram->init_done))
+ goto error_unlock;
+
if (!valid_io_request(zram, bio)) {
zram_stat64_inc(zram, &zram->stats.invalid_io);
- bio_io_error(bio);
- return 0;
- }
-
- if (unlikely(!zram->init_done) && zram_init_device(zram)) {
- bio_io_error(bio);
- return 0;
+ goto error_unlock;
}
__zram_make_request(zram, bio, bio_data_dir(bio));
+ up_read(&zram->init_lock);
return 0;
+
+error_unlock:
+ up_read(&zram->init_lock);
+error:
+ bio_io_error(bio);
+ return 0;
}
-void zram_reset_device(struct zram *zram)
+void __zram_reset_device(struct zram *zram)
{
size_t index;
- mutex_lock(&zram->init_lock);
zram->init_done = 0;
/* Free various per-device buffers */
@@ -617,7 +624,13 @@ void zram_reset_device(struct zram *zram)
memset(&zram->stats, 0, sizeof(zram->stats));
zram->disksize = 0;
- mutex_unlock(&zram->init_lock);
+}
+
+void zram_reset_device(struct zram *zram)
+{
+ down_write(&zram->init_lock);
+ __zram_reset_device(zram);
+ up_write(&zram->init_lock);
}
int zram_init_device(struct zram *zram)
@@ -625,10 +638,10 @@ int zram_init_device(struct zram *zram)
int ret;
size_t num_pages;
- mutex_lock(&zram->init_lock);
+ down_write(&zram->init_lock);
if (zram->init_done) {
- mutex_unlock(&zram->init_lock);
+ up_write(&zram->init_lock);
return 0;
}
@@ -671,15 +684,14 @@ int zram_init_device(struct zram *zram)
}
zram->init_done = 1;
- mutex_unlock(&zram->init_lock);
+ up_write(&zram->init_lock);
pr_debug("Initialization done!\n");
return 0;
fail:
- mutex_unlock(&zram->init_lock);
- zram_reset_device(zram);
-
+ __zram_reset_device(zram);
+ up_write(&zram->init_lock);
pr_err("Initialization failed: err=%d\n", ret);
return ret;
}
@@ -703,7 +715,7 @@ static int create_device(struct zram *zram, int device_id)
int ret = 0;
init_rwsem(&zram->lock);
- mutex_init(&zram->init_lock);
+ init_rwsem(&zram->init_lock);
spin_lock_init(&zram->stat64_lock);
zram->queue = blk_alloc_queue(GFP_KERNEL);
diff --git a/drivers/staging/zram/zram_drv.h b/drivers/staging/zram/zram_drv.h
index abe5221..3bf134d 100644
--- a/drivers/staging/zram/zram_drv.h
+++ b/drivers/staging/zram/zram_drv.h
@@ -112,8 +112,8 @@ struct zram {
struct request_queue *queue;
struct gendisk *disk;
int init_done;
- /* Prevent concurrent execution of device init and reset */
- struct mutex init_lock;
+ /* Prevent concurrent execution of device init, reset and R/W request */
+ struct rw_semaphore init_lock;
/*
* This is the limit on amount of *uncompressed* worth of data
* we can store in a disk.
@@ -130,6 +130,6 @@ extern struct attribute_group zram_disk_attr_group;
#endif
extern int zram_init_device(struct zram *zram);
-extern void zram_reset_device(struct zram *zram);
+extern void __zram_reset_device(struct zram *zram);
#endif
diff --git a/drivers/staging/zram/zram_sysfs.c b/drivers/staging/zram/zram_sysfs.c
index a70cc01..43670a9 100644
--- a/drivers/staging/zram/zram_sysfs.c
+++ b/drivers/staging/zram/zram_sysfs.c
@@ -55,19 +55,23 @@ static ssize_t disksize_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t len)
{
int ret;
+ u64 disksize;
struct zram *zram = dev_to_zram(dev);
+ ret = strict_strtoull(buf, 10, &disksize);
+ if (ret)
+ return ret;
+
+ down_write(&zram->init_lock);
if (zram->init_done) {
+ up_write(&zram->init_lock);
pr_info("Cannot change disksize for initialized device\n");
return -EBUSY;
}
- ret = strict_strtoull(buf, 10, &zram->disksize);
- if (ret)
- return ret;
-
- zram->disksize = PAGE_ALIGN(zram->disksize);
+ zram->disksize = PAGE_ALIGN(disksize);
set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
+ up_write(&zram->init_lock);
return len;
}
@@ -106,8 +110,10 @@ static ssize_t reset_store(struct device *dev,
if (bdev)
fsync_bdev(bdev);
+ down_write(&zram->init_lock);
if (zram->init_done)
- zram_reset_device(zram);
+ __zram_reset_device(zram);
+ up_write(&zram->init_lock);
return len;
}
--
1.7.6
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH 2/2] staging: zram: prevent accessing an unallocated table when init fails early
2011-09-06 13:02 [PATCH 1/2] staging: zram: fix zram locking Jerome Marchand
@ 2011-09-06 13:02 ` Jerome Marchand
2011-09-08 1:35 ` [PATCH 1/2] staging: zram: fix zram locking Nitin Gupta
1 sibling, 0 replies; 4+ messages in thread
From: Jerome Marchand @ 2011-09-06 13:02 UTC (permalink / raw)
To: Greg Kroah-Hartman; +Cc: Linux Kernel List, Nitin Gupta, Jeff Moyer
When the allocation of zram->table fails, we set zram->disksize to zero
to prevent accessing the unallocated table entries during cleanup.
However, we currently don't take this precaution when the initialization
fails earlier.
Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
---
drivers/staging/zram/zram_drv.c | 11 ++++++-----
1 files changed, 6 insertions(+), 5 deletions(-)
diff --git a/drivers/staging/zram/zram_drv.c b/drivers/staging/zram/zram_drv.c
index 2d1c8bd..b99cf53 100644
--- a/drivers/staging/zram/zram_drv.c
+++ b/drivers/staging/zram/zram_drv.c
@@ -651,24 +651,22 @@ int zram_init_device(struct zram *zram)
if (!zram->compress_workmem) {
pr_err("Error allocating compressor working memory!\n");
ret = -ENOMEM;
- goto fail;
+ goto fail_no_table;
}
zram->compress_buffer = (void *)__get_free_pages(__GFP_ZERO, 1);
if (!zram->compress_buffer) {
pr_err("Error allocating compressor buffer space\n");
ret = -ENOMEM;
- goto fail;
+ goto fail_no_table;
}
num_pages = zram->disksize >> PAGE_SHIFT;
zram->table = vzalloc(num_pages * sizeof(*zram->table));
if (!zram->table) {
pr_err("Error allocating zram address table\n");
- /* To prevent accessing table entries during cleanup */
- zram->disksize = 0;
ret = -ENOMEM;
- goto fail;
+ goto fail_no_table;
}
set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
@@ -689,6 +687,9 @@ int zram_init_device(struct zram *zram)
pr_debug("Initialization done!\n");
return 0;
+fail_no_table:
+ /* To prevent accessing table entries during cleanup */
+ zram->disksize = 0;
fail:
__zram_reset_device(zram);
up_write(&zram->init_lock);
--
1.7.6
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH 1/2] staging: zram: fix zram locking
2011-09-06 13:02 [PATCH 1/2] staging: zram: fix zram locking Jerome Marchand
2011-09-06 13:02 ` [PATCH 2/2] staging: zram: prevent accessing an unallocated table when init fails early Jerome Marchand
@ 2011-09-08 1:35 ` Nitin Gupta
2011-09-08 8:05 ` Jerome Marchand
1 sibling, 1 reply; 4+ messages in thread
From: Nitin Gupta @ 2011-09-08 1:35 UTC (permalink / raw)
To: Jerome Marchand; +Cc: Greg Kroah-Hartman, Linux Kernel List, Jeff Moyer
On 09/06/2011 09:02 AM, Jerome Marchand wrote:
> Currently init_lock only prevents concurrent execution of zram_init_device()
> and zram_reset_device() but not zram_make_request() nor sysfs store functions.
>
zram_make_request() initializes the device first time it is used and
from then on no sysfs config writes are allowed till the device is reset
-- for example, you cannot change disksize while a disk is in
initialized state. So, I could not understand why we need to protect
zram_make_request vs sysfs stores.
Thanks,
Nitin
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH 1/2] staging: zram: fix zram locking
2011-09-08 1:35 ` [PATCH 1/2] staging: zram: fix zram locking Nitin Gupta
@ 2011-09-08 8:05 ` Jerome Marchand
0 siblings, 0 replies; 4+ messages in thread
From: Jerome Marchand @ 2011-09-08 8:05 UTC (permalink / raw)
To: Nitin Gupta; +Cc: Greg Kroah-Hartman, Linux Kernel List, Jeff Moyer
On 09/08/2011 03:35 AM, Nitin Gupta wrote:
> On 09/06/2011 09:02 AM, Jerome Marchand wrote:
>> Currently init_lock only prevents concurrent execution of zram_init_device()
>> and zram_reset_device() but not zram_make_request() nor sysfs store functions.
>>
>
> zram_make_request() initializes the device first time it is used and
> from then on no sysfs config writes are allowed till the device is reset
> -- for example, you cannot change disksize while a disk is in
> initialized state. So, I could not understand why we need to protect
> zram_make_request vs sysfs stores.
This is true for disksize_store() (which can race with zram_init_device(), thus
the write lock in it), not for reset_store(), which obviously can happen in
initialized state. I have actually hit those races with the following
reproducer:
---
#! /bin/sh
while true; do
for i in `seq 0 9`; do
echo 1 > /sys/block/zram0/reset&
echo $((1024*1024*500)) > /sys/block/zram0/disksize&
for i in `seq 1 10`; do
dd if=/dev/zero of=/dev/zram0 bs=4k count=1 2>/dev/null;
done
done;
wait;
done
>
> Thanks,
> Nitin
>
>
>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2011-09-08 8:05 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-09-06 13:02 [PATCH 1/2] staging: zram: fix zram locking Jerome Marchand
2011-09-06 13:02 ` [PATCH 2/2] staging: zram: prevent accessing an unallocated table when init fails early Jerome Marchand
2011-09-08 1:35 ` [PATCH 1/2] staging: zram: fix zram locking Nitin Gupta
2011-09-08 8:05 ` Jerome Marchand
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox