Commit 535d039a authored by Christoph Hellwig's avatar Christoph Hellwig Committed by Li Nan
Browse files

block: add a flag to make put_disk on partially initalized disks safer

mainline inclusion
from mainline-v5.14-rc1
commit 958229a7
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/I81XCK

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=958229a7c55f219b1cff99f939dabbc1b6ba7161



----------------------------------------

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: default avatarChristoph Hellwig <hch@lst.de>
Reviewed-by: default avatarHannes Reinecke <hare@suse.de>
Reviewed-by: default avatarLuis Chamberlain <mcgrof@kernel.org>
Reviewed-by: default avatarUlf Hansson <ulf.hansson@linaro.org>
Link: https://lore.kernel.org/r/20210521055116.1053587-5-hch@lst.de


Signed-off-by: default avatarJens Axboe <axboe@kernel.dk>
conflicts:
  block/genhd.c
  include/linux/genhd.h
Signed-off-by: default avatarZhong Jinghua <zhongjinghua@huawei.com>
Signed-off-by: default avatarLi Nan <linan122@huawei.com>
parent 85b52a38
Loading
Loading
Loading
Loading
+5 −2
Original line number Diff line number Diff line
@@ -781,7 +781,10 @@ static int __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);
	/*
	 * The disk queue should now be all set with enough information about
	 * the device for the elevator code to pick an adequate default
@@ -1725,7 +1728,7 @@ static void disk_release(struct device *dev)
	kfree(disk->random);
	disk_replace_part_tbl(disk, NULL);
	hd_free_part(&disk->part0);
	if (disk->queue)
	if (test_bit(GD_QUEUE_REF, &disk->state) && disk->queue)
		blk_put_queue(disk->queue);
	kfree(disk);
}
+1 −0
Original line number Diff line number Diff line
@@ -204,6 +204,7 @@ struct gendisk {
	int flags;
	unsigned long state;
#define GD_NEED_PART_SCAN		0
#define GD_QUEUE_REF			1
	struct rw_semaphore lookup_sem;
	struct kobject *slave_dir;