diff options
author | Sergey Senozhatsky <sergey.senozhatsky@gmail.com> | 2015-02-12 18:00:36 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2015-02-12 21:54:11 -0500 |
commit | ba6b17d68c8e3aa8d55d0474299cb931965c5ea5 (patch) | |
tree | 3130712f638e8158e7b5f985cec46189564fc935 | |
parent | 1fec117281d9f5349c35279c9521f4096fa33357 (diff) |
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>
-rw-r--r-- | drivers/block/zram/zram_drv.c | 23 |
1 files changed, 9 insertions, 14 deletions
diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 0e07652cf7c1..2607bd9f4955 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c | |||
@@ -715,7 +715,7 @@ static void zram_bio_discard(struct zram *zram, u32 index, | |||
715 | } | 715 | } |
716 | } | 716 | } |
717 | 717 | ||
718 | static void zram_reset_device(struct zram *zram, bool reset_capacity) | 718 | static void zram_reset_device(struct zram *zram) |
719 | { | 719 | { |
720 | down_write(&zram->init_lock); | 720 | down_write(&zram->init_lock); |
721 | 721 | ||
@@ -734,18 +734,7 @@ static void zram_reset_device(struct zram *zram, bool reset_capacity) | |||
734 | memset(&zram->stats, 0, sizeof(zram->stats)); | 734 | memset(&zram->stats, 0, sizeof(zram->stats)); |
735 | 735 | ||
736 | zram->disksize = 0; | 736 | zram->disksize = 0; |
737 | if (reset_capacity) | ||
738 | set_capacity(zram->disk, 0); | ||
739 | |||
740 | up_write(&zram->init_lock); | 737 | 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); | ||
749 | } | 738 | } |
750 | 739 | ||
751 | static ssize_t disksize_store(struct device *dev, | 740 | static ssize_t disksize_store(struct device *dev, |
@@ -818,6 +807,7 @@ static ssize_t reset_store(struct device *dev, | |||
818 | if (!bdev) | 807 | if (!bdev) |
819 | return -ENOMEM; | 808 | return -ENOMEM; |
820 | 809 | ||
810 | mutex_lock(&bdev->bd_mutex); | ||
821 | /* Do not reset an active device! */ | 811 | /* Do not reset an active device! */ |
822 | if (bdev->bd_holders) { | 812 | if (bdev->bd_holders) { |
823 | ret = -EBUSY; | 813 | ret = -EBUSY; |
@@ -835,12 +825,17 @@ static ssize_t reset_store(struct device *dev, | |||
835 | 825 | ||
836 | /* Make sure all pending I/O is finished */ | 826 | /* Make sure all pending I/O is finished */ |
837 | fsync_bdev(bdev); | 827 | 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); | ||
838 | bdput(bdev); | 833 | bdput(bdev); |
839 | 834 | ||
840 | zram_reset_device(zram, true); | ||
841 | return len; | 835 | return len; |
842 | 836 | ||
843 | out: | 837 | out: |
838 | mutex_unlock(&bdev->bd_mutex); | ||
844 | bdput(bdev); | 839 | bdput(bdev); |
845 | return ret; | 840 | return ret; |
846 | } | 841 | } |
@@ -1186,7 +1181,7 @@ static void __exit zram_exit(void) | |||
1186 | * Shouldn't access zram->disk after destroy_device | 1181 | * Shouldn't access zram->disk after destroy_device |
1187 | * because destroy_device already released zram->disk. | 1182 | * because destroy_device already released zram->disk. |
1188 | */ | 1183 | */ |
1189 | zram_reset_device(zram, false); | 1184 | zram_reset_device(zram); |
1190 | } | 1185 | } |
1191 | 1186 | ||
1192 | unregister_blkdev(zram_major, "zram"); | 1187 | unregister_blkdev(zram_major, "zram"); |