Skip to content

Commit ba6b17d

Browse files
sergey-senozhatskytorvalds
authored andcommitted
zram: fix umount-reset_store-mount race condition
Ganesh Mahendran was the first one who proposed to use bdev->bd_mutex to avoid ->bd_holders race condition: CPU0 CPU1 umount /* zram->init_done is true */ reset_store() bdev->bd_holders == 0 mount ... zram_make_request() zram_reset_device() However, his solution required some considerable amount of code movement, which we can avoid. Apart from using bdev->bd_mutex in reset_store(), this patch also simplifies zram_reset_device(). zram_reset_device() has a bool parameter reset_capacity which tells it whether disk capacity and itself disk should be reset. There are two zram_reset_device() callers: -- zram_exit() passes reset_capacity=false -- reset_store() passes reset_capacity=true So we can move reset_capacity-sensitive work out of zram_reset_device() and perform it unconditionally in reset_store(). This also lets us drop reset_capacity parameter from zram_reset_device() and pass zram pointer only. Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com> Reported-by: Ganesh Mahendran <opensource.ganesh@gmail.com> Cc: Minchan Kim <minchan@kernel.org> Cc: Vlastimil Babka <vbabka@suse.cz> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 1fec117 commit ba6b17d

1 file changed

Lines changed: 9 additions & 14 deletions

File tree

‎drivers/block/zram/zram_drv.c‎

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ static void zram_bio_discard(struct zram *zram, u32 index,
715715
}
716716
}
717717

718-
static void zram_reset_device(struct zram *zram, bool reset_capacity)
718+
static void zram_reset_device(struct zram *zram)
719719
{
720720
down_write(&zram->init_lock);
721721

@@ -734,18 +734,7 @@ static void zram_reset_device(struct zram *zram, bool reset_capacity)
734734
memset(&zram->stats, 0, sizeof(zram->stats));
735735

736736
zram->disksize = 0;
737-
if (reset_capacity)
738-
set_capacity(zram->disk, 0);
739-
740737
up_write(&zram->init_lock);
741-
742-
/*
743-
* Revalidate disk out of the init_lock to avoid lockdep splat.
744-
* It's okay because disk's capacity is protected by init_lock
745-
* so that revalidate_disk always sees up-to-date capacity.
746-
*/
747-
if (reset_capacity)
748-
revalidate_disk(zram->disk);
749738
}
750739

751740
static ssize_t disksize_store(struct device *dev,
@@ -818,6 +807,7 @@ static ssize_t reset_store(struct device *dev,
818807
if (!bdev)
819808
return -ENOMEM;
820809

810+
mutex_lock(&bdev->bd_mutex);
821811
/* Do not reset an active device! */
822812
if (bdev->bd_holders) {
823813
ret = -EBUSY;
@@ -835,12 +825,17 @@ static ssize_t reset_store(struct device *dev,
835825

836826
/* Make sure all pending I/O is finished */
837827
fsync_bdev(bdev);
828+
zram_reset_device(zram);
829+
set_capacity(zram->disk, 0);
830+
831+
mutex_unlock(&bdev->bd_mutex);
832+
revalidate_disk(zram->disk);
838833
bdput(bdev);
839834

840-
zram_reset_device(zram, true);
841835
return len;
842836

843837
out:
838+
mutex_unlock(&bdev->bd_mutex);
844839
bdput(bdev);
845840
return ret;
846841
}
@@ -1186,7 +1181,7 @@ static void __exit zram_exit(void)
11861181
* Shouldn't access zram->disk after destroy_device
11871182
* because destroy_device already released zram->disk.
11881183
*/
1189-
zram_reset_device(zram, false);
1184+
zram_reset_device(zram);
11901185
}
11911186

11921187
unregister_blkdev(zram_major, "zram");

0 commit comments

Comments
 (0)