diff options
author | Jerome Marchand <jmarchan@redhat.com> | 2012-05-14 11:21:07 -0500 |
---|---|---|
committer | Simon Shields <keepcalm444@gmail.com> | 2016-06-12 21:19:24 +1000 |
commit | f526a35a01423d3760e773604b763e8a7aeeb6c1 (patch) | |
tree | 0dd7f723b00b304d2c8746cf748886ea9c371f82 /drivers/staging | |
parent | dc3b0f18a7172f56a5ba1375c3e2f15fed6a9b25 (diff) | |
download | kernel_samsung_smdk4412-f526a35a01423d3760e773604b763e8a7aeeb6c1.tar.gz kernel_samsung_smdk4412-f526a35a01423d3760e773604b763e8a7aeeb6c1.tar.bz2 kernel_samsung_smdk4412-f526a35a01423d3760e773604b763e8a7aeeb6c1.zip |
Staging: zram: Replace mutex lock by a R/W semaphore
Currently, nothing protects zram table from concurrent access.
For instance, ZRAM_UNCOMPRESSED bit can be cleared by zram_free_page()
called from a concurrent write between the time ZRAM_UNCOMPRESSED has
been set and the time it is tested to unmap KM_USER0 in
zram_bvec_write(). This ultimately leads to kernel panic.
Also, a read request can occurs when the page has been freed by a
running write request and before it has been updated, leading to
zero filled block being incorrectly read and "Read before write"
error message.
This patch replace the current mutex by a rw_semaphore. It extends
the protection to zram table (currently, only compression buffers are
protected) and read requests (currently, only write requests are
protected).
Change-Id: Ie2b2608ff55dd655037b48a6f7370a819c9b581a
Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Signed-off-by: Jason Hrycay <jason.hrycay@motorola.com>
Reviewed-on: http://gerrit.pcs.mot.com/414992
Tested-by: Jira Key <JIRAKEY@motorola.com>
Reviewed-by: Christopher Fries <C.Fries@motorola.com>
Reviewed-by: Neil Patel <NPATEL@motorola.com>
Reviewed-by: David Ding <dding@motorola.com>
Diffstat (limited to 'drivers/staging')
-rw-r--r-- | drivers/staging/zram/zram_drv.c | 25 | ||||
-rw-r--r-- | drivers/staging/zram/zram_drv.h | 4 |
2 files changed, 15 insertions, 14 deletions
diff --git a/drivers/staging/zram/zram_drv.c b/drivers/staging/zram/zram_drv.c index 9b311638120..2095478b2e7 100644 --- a/drivers/staging/zram/zram_drv.c +++ b/drivers/staging/zram/zram_drv.c @@ -279,12 +279,9 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index) zram_test_flag(zram, index, ZRAM_ZERO)) zram_free_page(zram, index); - mutex_lock(&zram->lock); - user_mem = kmap_atomic(page, KM_USER0); if (page_zero_filled(user_mem)) { kunmap_atomic(user_mem, KM_USER0); - mutex_unlock(&zram->lock); zram_stat_inc(&zram->stats.pages_zero); zram_set_flag(zram, index, ZRAM_ZERO); return 0; @@ -296,7 +293,6 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index) kunmap_atomic(user_mem, KM_USER0); if (unlikely(ret != LZO_E_OK)) { - mutex_unlock(&zram->lock); pr_err("Compression failed! err=%d\n", ret); zram_stat64_inc(zram, &zram->stats.failed_writes); return ret; @@ -311,7 +307,6 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index) clen = PAGE_SIZE; page_store = alloc_page(GFP_NOIO | __GFP_HIGHMEM); if (unlikely(!page_store)) { - mutex_unlock(&zram->lock); pr_info("Error allocating memory for " "incompressible page: %u\n", index); zram_stat64_inc(zram, &zram->stats.failed_writes); @@ -329,7 +324,6 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index) if (xv_malloc(zram->mem_pool, clen + sizeof(*zheader), &zram->table[index].page, &offset, GFP_NOIO | __GFP_HIGHMEM)) { - mutex_unlock(&zram->lock); pr_info("Error allocating memory for compressed " "page: %u, size=%zu\n", index, clen); zram_stat64_inc(zram, &zram->stats.failed_writes); @@ -363,18 +357,25 @@ memstore: if (clen <= PAGE_SIZE / 2) zram_stat_inc(&zram->stats.good_compress); - mutex_unlock(&zram->lock); - return 0; } static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index, struct bio *bio, int rw) { - if (rw == READ) - return zram_bvec_read(zram, bvec, index, bio); + int ret; - return zram_bvec_write(zram, bvec, index); + if (rw == READ) { + down_read(&zram->lock); + ret = zram_bvec_read(zram, bvec, index, bio); + up_read(&zram->lock); + } else { + down_write(&zram->lock); + ret = zram_bvec_write(zram, bvec, index); + up_write(&zram->lock); + } + + return ret; } static void __zram_make_request(struct zram *zram, struct bio *bio, int rw) @@ -574,7 +575,7 @@ static int create_device(struct zram *zram, int device_id) { int ret = 0; - mutex_init(&zram->lock); + init_rwsem(&zram->lock); mutex_init(&zram->init_lock); spin_lock_init(&zram->stat64_lock); diff --git a/drivers/staging/zram/zram_drv.h b/drivers/staging/zram/zram_drv.h index 408b2c067fc..1db8f19a093 100644 --- a/drivers/staging/zram/zram_drv.h +++ b/drivers/staging/zram/zram_drv.h @@ -104,8 +104,8 @@ struct zram { void *compress_buffer; struct table *table; spinlock_t stat64_lock; /* protect 64-bit stats */ - struct mutex lock; /* protect compression buffers against - * concurrent writes */ + struct rw_semaphore lock; /* protect compression buffers against + * concurrent writes */ struct request_queue *queue; struct gendisk *disk; int init_done; |