Skip to content

Commit 9864f4c

Browse files
Charan Teja ReddyDhineshCool
authored andcommitted
zram: fix race condition while returning zram_entry refcount
With deduplication enabled, the duplicated zram objects are tracked using the zram_entry backed by a refcount. The race condition while decrementing the refcount through zram_dedup_put() is as follows: Say Task A and task B share the same object and thus the zram_entry->refcount = 2. Task A Task B zram_dedup_put zram_dedup_put spin_lock(&hash->lock); entry->refcount--; (Now it is 1) spin_unlock(&hash->lock); spin_lock(&hash->lock); entry->refcount--; (Now it is 0) spin_unlock(&hash->lock); return entry->refcount return entry->refcount We return 0 in above steps thus leading to double free of the handle, which is a slab object. Change-Id: I8dd9bad27140a6e3a295905bf4411050d8eac931 Signed-off-by: Charan Teja Reddy <charante@codeaurora.org> Signed-off-by: Marco Zanin <mrczn.bb@gmail.com> Signed-off-by: snnbyyds <snnbyyds@gmail.com>
1 parent 222effc commit 9864f4c

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

drivers/block/zram/zram_dedup.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,22 @@ static unsigned long zram_dedup_put(struct zram *zram,
9292
{
9393
struct zram_hash *hash;
9494
u32 checksum;
95+
unsigned long val;
9596

9697
checksum = entry->checksum;
9798
hash = &zram->hash[checksum % zram->hash_size];
9899

99100
spin_lock(&hash->lock);
100101

101-
entry->refcount--;
102+
val = --entry->refcount;
102103
if (!entry->refcount)
103104
rb_erase(&entry->rb_node, &hash->rb_root);
104105
else
105106
atomic64_sub(entry->len, &zram->stats.dup_data_size);
106107

107108
spin_unlock(&hash->lock);
108109

109-
return entry->refcount;
110+
return val;
110111
}
111112

112113
static struct zram_entry *__zram_dedup_get(struct zram *zram,

0 commit comments

Comments
 (0)