zram: avoid race between zram_remove and disksize_store

After resetting device in zram_remove(), disksize_store still may come and
allocate resources again before deleting gendisk, fix the race by resetting
zram after del_gendisk() returns. At that time, disksize_store can't come
any more.

Reported-by: Luis Chamberlain <mcgrof@kernel.org>
Reviewed-by: Luis Chamberlain <mcgrof@kernel.org>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
Acked-by: Minchan Kim <minchan@kernel.org>
Link: https://lore.kernel.org/r/20211025025426.2815424-4-ming.lei@redhat.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
This commit is contained in:
Ming Lei 2021-10-25 10:54:25 +08:00 committed by Jens Axboe
parent 8c54499a59
commit 5a4b653655
1 changed files with 7 additions and 0 deletions

View File

@ -2006,6 +2006,13 @@ static int zram_remove(struct zram *zram)
/* del_gendisk drains pending reset_store */ /* del_gendisk drains pending reset_store */
WARN_ON_ONCE(claimed && zram->claim); WARN_ON_ONCE(claimed && zram->claim);
/*
* disksize_store() may be called in between zram_reset_device()
* and del_gendisk(), so run the last reset to avoid leaking
* anything allocated with disksize_store()
*/
zram_reset_device(zram);
blk_cleanup_disk(zram->disk); blk_cleanup_disk(zram->disk);
kfree(zram); kfree(zram);
return 0; return 0;