Skip to content

Commit 8c54499

Browse files
Ming Leiaxboe
authored andcommitted
zram: don't fail to remove zram during unloading module
When the zram module is being unloaded, no one should be using the zram disks. However even while being unloaded the zram module's sysfs attributes might be poked at to re-configure zram devices. This is expected, and kernfs ensures that these operations complete before device_del() completes. But reset_store() may set ->claim which will fail zram_remove(), when this happens, zram_reset_device() is bypassed, and zram->comp can't be destroyed, so the warning of 'Error: Removing state 63 which has instances left.' is triggered during unloading module, together with memory leak and sort of thing. Fixes the issue by not failing zram_remove() if ->claim is set, and we actually need to do nothing in case that zram_reset() is running since del_gendisk() will wait until zram_reset() is done. Reported-by: Luis Chamberlain <[email protected]> Reviewed-by: Luis Chamberlain <[email protected]> Signed-off-by: Ming Lei <[email protected]> Acked-by: Minchan Kim <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 6f16377 commit 8c54499

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

drivers/block/zram/zram_drv.c

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1972,25 +1972,40 @@ static int zram_add(void)
19721972
static int zram_remove(struct zram *zram)
19731973
{
19741974
struct block_device *bdev = zram->disk->part0;
1975+
bool claimed;
19751976

19761977
mutex_lock(&bdev->bd_disk->open_mutex);
1977-
if (bdev->bd_openers || zram->claim) {
1978+
if (bdev->bd_openers) {
19781979
mutex_unlock(&bdev->bd_disk->open_mutex);
19791980
return -EBUSY;
19801981
}
19811982

1982-
zram->claim = true;
1983+
claimed = zram->claim;
1984+
if (!claimed)
1985+
zram->claim = true;
19831986
mutex_unlock(&bdev->bd_disk->open_mutex);
19841987

19851988
zram_debugfs_unregister(zram);
19861989

1987-
/* Make sure all the pending I/O are finished */
1988-
fsync_bdev(bdev);
1989-
zram_reset_device(zram);
1990+
if (claimed) {
1991+
/*
1992+
* If we were claimed by reset_store(), del_gendisk() will
1993+
* wait until reset_store() is done, so nothing need to do.
1994+
*/
1995+
;
1996+
} else {
1997+
/* Make sure all the pending I/O are finished */
1998+
fsync_bdev(bdev);
1999+
zram_reset_device(zram);
2000+
}
19902001

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

19932004
del_gendisk(zram->disk);
2005+
2006+
/* del_gendisk drains pending reset_store */
2007+
WARN_ON_ONCE(claimed && zram->claim);
2008+
19942009
blk_cleanup_disk(zram->disk);
19952010
kfree(zram);
19962011
return 0;
@@ -2067,7 +2082,7 @@ static struct class zram_control_class = {
20672082

20682083
static int zram_remove_cb(int id, void *ptr, void *data)
20692084
{
2070-
zram_remove(ptr);
2085+
WARN_ON_ONCE(zram_remove(ptr));
20712086
return 0;
20722087
}
20732088

0 commit comments

Comments
 (0)