Skip to contentSkip to conversation

scsi: sd: do not call device_add() on scsi_disk with uninitialized gendisk ->queue

PATCH In review Ewan D. Milne 2 participants 4 messages Sep 2, 2021 – Sep 3, 2021 SCSI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scsi: sd: do not call device_add() on scsi_disk with uninitialized gendisk ->queue

Calling device_add() makes the scsi_disk visible in sysfs, the accessor routines reference sdkp->disk->queue which was not yet set properly. Fix this by initializing gendisk fields earlier in the function.

---
 drivers/scsi/sd.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/scsi/sd.c +7 -7
index 6d2d636..97ab18b 100644
@@ -3428,6 +3428,13 @@ static int sd_probe(struct device *dev)
 		goto out_free_index;
 	}
 
+	gd->major = sd_major((index & 0xf0) >> 4);
+	gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
+
+	gd->fops = &sd_fops;
+	gd->private_data = &sdkp->driver;
+	gd->queue = sdp->request_queue;
+
 	sdkp->device = sdp;
 	sdkp->driver = &sd_template;
 	sdkp->disk = gd;
@@ -3456,13 +3463,6 @@ static int sd_probe(struct device *dev)
 	get_device(dev);
 	dev_set_drvdata(dev, sdkp);
 
-	gd->major = sd_major((index & 0xf0) >> 4);
-	gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
-
-	gd->fops = &sd_fops;
-	gd->private_data = &sdkp->driver;
-	gd->queue = sdkp->device->request_queue;
-
 	/* defaults, until the device tells us otherwise */
 	sdp->sector_size = 512;
 	sdkp->capacity = 0;
Signature · 1 line
2.1.0
#1 lore raw
Quoted 3 lines · Ewan D. Milne

Calling device_add() makes the scsi_disk visible in sysfs, the accessor routines reference sdkp->disk->queue which was not yet set properly. Fix this by initializing gendisk fields earlier in the function.

What kernel is this against? This won't apply against linux-next as disk->queue is now always set up by __blk_alloc_disk.

author
#2 lore raw

It was against git://git.kernel.org/pub/scm/linux/kernel/git/mkp/scsi.git
branch "fixes".

Looks like your changes resolve ->queue not being set prior to add_device(), please disregard my patch. It's not that critical to fix in earlier versions. Sorry for the noise.

-Ewan

Quoted 9 lines · Christoph Hellwig
Quoted 3 lines · Ewan D. Milne

Calling device_add() makes the scsi_disk visible in sysfs, the accessor routines reference sdkp->disk->queue which was not yet set properly. Fix this by initializing gendisk fields earlier in the function.

What kernel is this against? This won't apply against linux-next as disk->queue is now always set up by __blk_alloc_disk.

#3 lore raw
Quoted 6 lines · Ewan Milne

It was against git://git.kernel.org/pub/scm/linux/kernel/git/mkp/scsi.git
branch "fixes".

Looks like your changes resolve ->queue not being set prior to add_device(), please disregard my patch. It's not that critical to fix in earlier versions. Sorry for the noise.

This might still be useful for -stable. It just needs to be clearly tagged as such.