summaryrefslogtreecommitdiffstats
path: root/block/genhd.c
diff options
context:
space:
mode:
authorChristoph Hellwig <hch@lst.de>2021-05-21 07:50:54 +0200
committerJens Axboe <axboe@kernel.dk>2021-06-01 15:42:23 +0200
commit958229a7c55f219b1cff99f939dabbc1b6ba7161 (patch)
tree95e14b94cb882656a3337f78f19102f305c1b515 /block/genhd.c
parentblock: automatically enable GENHD_FL_EXT_DEVT (diff)
downloadlinux-958229a7c55f219b1cff99f939dabbc1b6ba7161.tar.xz
linux-958229a7c55f219b1cff99f939dabbc1b6ba7161.zip
block: add a flag to make put_disk on partially initalized disks safer
Add a flag to indicate that __device_add_disk did grab a queue reference so that disk_release only drops it if we actually had it. This sort out one of the major pitfals with partially initialized gendisk that a lot of drivers did get wrong or still do. Signed-off-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Hannes Reinecke <hare@suse.de> Reviewed-by: Luis Chamberlain <mcgrof@kernel.org> Reviewed-by: Ulf Hansson <ulf.hansson@linaro.org> Link: https://lore.kernel.org/r/20210521055116.1053587-5-hch@lst.de Signed-off-by: Jens Axboe <axboe@kernel.dk>
Diffstat (limited to 'block/genhd.c')
-rw-r--r--block/genhd.c7
1 files changed, 5 insertions, 2 deletions
diff --git a/block/genhd.c b/block/genhd.c
index 9fa734cb9cbd..c826db33a73e 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -539,7 +539,10 @@ static void __device_add_disk(struct device *parent, struct gendisk *disk,
* Take an extra ref on queue which will be put on disk_release()
* so that it sticks around as long as @disk is there.
*/
- WARN_ON_ONCE(!blk_get_queue(disk->queue));
+ if (blk_get_queue(disk->queue))
+ set_bit(GD_QUEUE_REF, &disk->state);
+ else
+ WARN_ON_ONCE(1);
disk_add_events(disk);
blk_integrity_add(disk);
@@ -1107,7 +1110,7 @@ static void disk_release(struct device *dev)
kfree(disk->random);
xa_destroy(&disk->part_tbl);
bdput(disk->part0);
- if (disk->queue)
+ if (test_bit(GD_QUEUE_REF, &disk->state) && disk->queue)
blk_put_queue(disk->queue);
kfree(disk);
}