Skip to content

Commit 17ec4cd

Browse files
jeromemarchandtorvalds
authored andcommitted
zram: don't call idr_remove() from zram_remove()
The use of idr_remove() is forbidden in the callback functions of idr_for_each(). It is therefore unsafe to call idr_remove in zram_remove(). This patch moves the call to idr_remove() from zram_remove() to hot_remove_store(). In the detroy_devices() path, idrs are removed by idr_destroy(). This solves an use-after-free detected by KASan. [[email protected]: fix coding stype, per Sergey] Signed-off-by: Jerome Marchand <[email protected]> Acked-by: Sergey Senozhatsky <[email protected]> Cc: Minchan Kim <[email protected]> Cc: <[email protected]> [4.2+] Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 8749cfe commit 17ec4cd

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

drivers/block/zram/zram_drv.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,7 +1325,6 @@ static int zram_remove(struct zram *zram)
13251325

13261326
pr_info("Removed device: %s\n", zram->disk->disk_name);
13271327

1328-
idr_remove(&zram_index_idr, zram->disk->first_minor);
13291328
blk_cleanup_queue(zram->disk->queue);
13301329
del_gendisk(zram->disk);
13311330
put_disk(zram->disk);
@@ -1367,10 +1366,12 @@ static ssize_t hot_remove_store(struct class *class,
13671366
mutex_lock(&zram_index_mutex);
13681367

13691368
zram = idr_find(&zram_index_idr, dev_id);
1370-
if (zram)
1369+
if (zram) {
13711370
ret = zram_remove(zram);
1372-
else
1371+
idr_remove(&zram_index_idr, dev_id);
1372+
} else {
13731373
ret = -ENODEV;
1374+
}
13741375

13751376
mutex_unlock(&zram_index_mutex);
13761377
return ret ? ret : count;

0 commit comments

Comments
 (0)