aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/block
diff options
context:
space:
mode:
authorMinchan Kim <minchan@kernel.org>2015-02-12 18:00:45 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2015-02-12 21:54:12 -0500
commit08eee69fcf6baea543a2b4d2a2fcba0e61aa3160 (patch)
treeeda2eb79414a825fea6d87f9d0c466b51ea42286 /drivers/block
parent2b269ce6fcbfafc6cae37254cab4bf2309bfed0e (diff)
zram: remove init_lock in zram_make_request
Admin could reset zram during I/O operation going on so we have used zram->init_lock as read-side lock in I/O path to prevent sudden zram meta freeing. However, the init_lock is really troublesome. We can't do call zram_meta_alloc under init_lock due to lockdep splat because zram_rw_page is one of the function under reclaim path and hold it as read_lock while other places in process context hold it as write_lock. So, we have used allocation out of the lock to avoid lockdep warn but it's not good for readability and fainally, I met another lockdep splat between init_lock and cpu_hotplug from kmem_cache_destroy during working zsmalloc compaction. :( Yes, the ideal is to remove horrible init_lock of zram in rw path. This patch removes it in rw path and instead, add atomic refcount for meta lifetime management and completion to free meta in process context. It's important to free meta in process context because some of resource destruction needs mutex lock, which could be held if we releases the resource in reclaim context so it's deadlock, again. As a bonus, we could remove init_done check in rw path because zram_meta_get will do a role for it, instead. Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com> Signed-off-by: Minchan Kim <minchan@kernel.org> Cc: Nitin Gupta <ngupta@vflare.org> Cc: Jerome Marchand <jmarchan@redhat.com> Cc: Ganesh Mahendran <opensource.ganesh@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'drivers/block')
-rw-r--r--drivers/block/zram/zram_drv.c76
-rw-r--r--drivers/block/zram/zram_drv.h20
2 files changed, 64 insertions, 32 deletions
diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
index 0abcf4a31c78..db94572b35c4 100644
--- a/drivers/block/zram/zram_drv.c
+++ b/drivers/block/zram/zram_drv.c
@@ -53,9 +53,9 @@ static ssize_t name##_show(struct device *d, \
53} \ 53} \
54static DEVICE_ATTR_RO(name); 54static DEVICE_ATTR_RO(name);
55 55
56static inline int init_done(struct zram *zram) 56static inline bool init_done(struct zram *zram)
57{ 57{
58 return zram->meta != NULL; 58 return zram->disksize;
59} 59}
60 60
61static inline struct zram *dev_to_zram(struct device *dev) 61static inline struct zram *dev_to_zram(struct device *dev)
@@ -356,6 +356,18 @@ out_error:
356 return NULL; 356 return NULL;
357} 357}
358 358
359static inline bool zram_meta_get(struct zram *zram)
360{
361 if (atomic_inc_not_zero(&zram->refcount))
362 return true;
363 return false;
364}
365
366static inline void zram_meta_put(struct zram *zram)
367{
368 atomic_dec(&zram->refcount);
369}
370
359static void update_position(u32 *index, int *offset, struct bio_vec *bvec) 371static void update_position(u32 *index, int *offset, struct bio_vec *bvec)
360{ 372{
361 if (*offset + bvec->bv_len >= PAGE_SIZE) 373 if (*offset + bvec->bv_len >= PAGE_SIZE)
@@ -717,6 +729,10 @@ static void zram_bio_discard(struct zram *zram, u32 index,
717 729
718static void zram_reset_device(struct zram *zram) 730static void zram_reset_device(struct zram *zram)
719{ 731{
732 struct zram_meta *meta;
733 struct zcomp *comp;
734 u64 disksize;
735
720 down_write(&zram->init_lock); 736 down_write(&zram->init_lock);
721 737
722 zram->limit_pages = 0; 738 zram->limit_pages = 0;
@@ -726,16 +742,31 @@ static void zram_reset_device(struct zram *zram)
726 return; 742 return;
727 } 743 }
728 744
729 zcomp_destroy(zram->comp); 745 meta = zram->meta;
730 zram->max_comp_streams = 1; 746 comp = zram->comp;
731 zram_meta_free(zram->meta, zram->disksize); 747 disksize = zram->disksize;
732 zram->meta = NULL; 748 /*
749 * Refcount will go down to 0 eventually and r/w handler
750 * cannot handle further I/O so it will bail out by
751 * check zram_meta_get.
752 */
753 zram_meta_put(zram);
754 /*
755 * We want to free zram_meta in process context to avoid
756 * deadlock between reclaim path and any other locks.
757 */
758 wait_event(zram->io_done, atomic_read(&zram->refcount) == 0);
759
733 /* Reset stats */ 760 /* Reset stats */
734 memset(&zram->stats, 0, sizeof(zram->stats)); 761 memset(&zram->stats, 0, sizeof(zram->stats));
735 zram->disksize = 0; 762 zram->disksize = 0;
763 zram->max_comp_streams = 1;
736 set_capacity(zram->disk, 0); 764 set_capacity(zram->disk, 0);
737 765
738 up_write(&zram->init_lock); 766 up_write(&zram->init_lock);
767 /* I/O operation under all of CPU are done so let's free */
768 zram_meta_free(meta, disksize);
769 zcomp_destroy(comp);
739} 770}
740 771
741static ssize_t disksize_store(struct device *dev, 772static ssize_t disksize_store(struct device *dev,
@@ -771,6 +802,8 @@ static ssize_t disksize_store(struct device *dev,
771 goto out_destroy_comp; 802 goto out_destroy_comp;
772 } 803 }
773 804
805 init_waitqueue_head(&zram->io_done);
806 atomic_set(&zram->refcount, 1);
774 zram->meta = meta; 807 zram->meta = meta;
775 zram->comp = comp; 808 zram->comp = comp;
776 zram->disksize = disksize; 809 zram->disksize = disksize;
@@ -901,23 +934,21 @@ static void zram_make_request(struct request_queue *queue, struct bio *bio)
901{ 934{
902 struct zram *zram = queue->queuedata; 935 struct zram *zram = queue->queuedata;
903 936
904 down_read(&zram->init_lock); 937 if (unlikely(!zram_meta_get(zram)))
905 if (unlikely(!init_done(zram)))
906 goto error; 938 goto error;
907 939
908 if (!valid_io_request(zram, bio->bi_iter.bi_sector, 940 if (!valid_io_request(zram, bio->bi_iter.bi_sector,
909 bio->bi_iter.bi_size)) { 941 bio->bi_iter.bi_size)) {
910 atomic64_inc(&zram->stats.invalid_io); 942 atomic64_inc(&zram->stats.invalid_io);
911 goto error; 943 goto put_zram;
912 } 944 }
913 945
914 __zram_make_request(zram, bio); 946 __zram_make_request(zram, bio);
915 up_read(&zram->init_lock); 947 zram_meta_put(zram);
916
917 return; 948 return;
918 949put_zram:
950 zram_meta_put(zram);
919error: 951error:
920 up_read(&zram->init_lock);
921 bio_io_error(bio); 952 bio_io_error(bio);
922} 953}
923 954
@@ -939,21 +970,19 @@ static void zram_slot_free_notify(struct block_device *bdev,
939static int zram_rw_page(struct block_device *bdev, sector_t sector, 970static int zram_rw_page(struct block_device *bdev, sector_t sector,
940 struct page *page, int rw) 971 struct page *page, int rw)
941{ 972{
942 int offset, err; 973 int offset, err = -EIO;
943 u32 index; 974 u32 index;
944 struct zram *zram; 975 struct zram *zram;
945 struct bio_vec bv; 976 struct bio_vec bv;
946 977
947 zram = bdev->bd_disk->private_data; 978 zram = bdev->bd_disk->private_data;
979 if (unlikely(!zram_meta_get(zram)))
980 goto out;
981
948 if (!valid_io_request(zram, sector, PAGE_SIZE)) { 982 if (!valid_io_request(zram, sector, PAGE_SIZE)) {
949 atomic64_inc(&zram->stats.invalid_io); 983 atomic64_inc(&zram->stats.invalid_io);
950 return -EINVAL; 984 err = -EINVAL;
951 } 985 goto put_zram;
952
953 down_read(&zram->init_lock);
954 if (unlikely(!init_done(zram))) {
955 err = -EIO;
956 goto out_unlock;
957 } 986 }
958 987
959 index = sector >> SECTORS_PER_PAGE_SHIFT; 988 index = sector >> SECTORS_PER_PAGE_SHIFT;
@@ -964,8 +993,9 @@ static int zram_rw_page(struct block_device *bdev, sector_t sector,
964 bv.bv_offset = 0; 993 bv.bv_offset = 0;
965 994
966 err = zram_bvec_rw(zram, &bv, index, offset, rw); 995 err = zram_bvec_rw(zram, &bv, index, offset, rw);
967out_unlock: 996put_zram:
968 up_read(&zram->init_lock); 997 zram_meta_put(zram);
998out:
969 /* 999 /*
970 * If I/O fails, just return error(ie, non-zero) without 1000 * If I/O fails, just return error(ie, non-zero) without
971 * calling page_endio. 1001 * calling page_endio.
diff --git a/drivers/block/zram/zram_drv.h b/drivers/block/zram/zram_drv.h
index b05a816b09ac..5249f51ccdb3 100644
--- a/drivers/block/zram/zram_drv.h
+++ b/drivers/block/zram/zram_drv.h
@@ -100,24 +100,26 @@ struct zram_meta {
100 100
101struct zram { 101struct zram {
102 struct zram_meta *meta; 102 struct zram_meta *meta;
103 struct zcomp *comp;
103 struct request_queue *queue; 104 struct request_queue *queue;
104 struct gendisk *disk; 105 struct gendisk *disk;
105 struct zcomp *comp; 106 /* Prevent concurrent execution of device init */
106
107 /* Prevent concurrent execution of device init, reset and R/W request */
108 struct rw_semaphore init_lock; 107 struct rw_semaphore init_lock;
109 /* 108 /*
110 * This is the limit on amount of *uncompressed* worth of data 109 * the number of pages zram can consume for storing compressed data
111 * we can store in a disk.
112 */ 110 */
113 u64 disksize; /* bytes */ 111 unsigned long limit_pages;
114 int max_comp_streams; 112 int max_comp_streams;
113
115 struct zram_stats stats; 114 struct zram_stats stats;
115 atomic_t refcount; /* refcount for zram_meta */
116 /* wait all IO under all of cpu are done */
117 wait_queue_head_t io_done;
116 /* 118 /*
117 * the number of pages zram can consume for storing compressed data 119 * This is the limit on amount of *uncompressed* worth of data
120 * we can store in a disk.
118 */ 121 */
119 unsigned long limit_pages; 122 u64 disksize; /* bytes */
120
121 char compressor[10]; 123 char compressor[10];
122}; 124};
123#endif 125#endif