Linux SCSI subsystem development
 help / color / mirror / Atom feed
From: John Garry <john.garry@linux.dev>
To: sashiko-reviews@lists.linux.dev, John Garry <john.g.garry@oracle.com>
Cc: linux-scsi@vger.kernel.org
Subject: Re: [PATCH v8 24/27] scsi: sd: support multipath disk
Date: Mon, 7 Sep 2026 13:34:51 +0100	[thread overview]
Message-ID: <a7be8330-1678-44e2-8c1b-eb48cb205eeb@linux.dev> (raw)
In-Reply-To: <20260907122525.940BF1F00A3A@smtp.kernel.org>

On 9/7/26 13:25, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
> 
> New issues:
> - [High] Pending delayed removal work is not cancelled when `sd_mpath_fail_probe()` tears down a multipath disk, leading to use-after-free of `mpath_head`.
> 
> Pre-existing issues:
> - [Critical] Preexisting double put of `sdkp->disk_dev` when `sd_large_pool_create()` or `device_add_disk()` fails.
> --
> 
> commit 9972be7d4ae335cb741d2858144500af92bf6dc3
> Author: John Garry <john.garry@linux.dev>
> 
> scsi: sd: support multipath disk
> 
> This commit adds support to attach a multipath disk by allocating a hidden
> gendisk per path. A global list of multipath disks is kept for matching
> SCSI devices, and the multipath gendisk mimics a standard SCSI disk.
> 
>> diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
>> index 60e071d23ddd0..27230ecc2337e 100644
>> --- a/drivers/scsi/sd.c
>> +++ b/drivers/scsi/sd.c
> [ ... ]
>> @@ -4048,13 +4151,260 @@ static const struct attribute_group sd_mpath_disk_attr_group = {
> [ ... ]
>> +static void sd_mpath_fail_probe(struct scsi_disk *sdkp)
>> +{
>> +	struct sd_mpath_disk *sd_mpath_disk = sdkp->sd_mpath_disk;
>> +	struct scsi_mpath_device *scsi_mpath_dev;
>> +	struct mpath_device *mpath_device;
>> +	struct scsi_device *sdp = sdkp->device;
>> +	struct scsi_mpath_head *scsi_mpath_head;
>> +	struct mpath_head *mpath_head;
>> +	bool remove = false;
>> +
>> +	if (!sd_mpath_disk)
>> +		return;
>> +
>> +	scsi_mpath_dev = sdp->scsi_mpath_dev;
>> +	mpath_device = &scsi_mpath_dev->mpath_device;
>> +	scsi_mpath_head = sd_mpath_disk->scsi_mpath_head;
>> +	mpath_head = &scsi_mpath_head->mpath_head;
>> +
>> +	mutex_lock(&sd_mpath_disks_lock);
>> +	sd_mpath_disk->disk_count--;
>> +	if (!sd_mpath_disk->disk_count) {
>> +		list_del_init(&sd_mpath_disk->entry);
>> +		remove = true;
>> +	}
>> +	mutex_unlock(&sd_mpath_disks_lock);
>> +	mpath_device->disk = NULL;
>> +
>> +	if (remove) {
>> +		device_del(&sd_mpath_disk->dev);
>> +		mpath_remove_disk(mpath_head);
>> +	}
>> +	sd_mpath_put_disk(sd_mpath_disk);
> 
> [Severity: High]
> Can this sequence leave pending delayed removal work uncancelled when tearing
> down a multipath disk?
> 
> If a multipath disk has its last path removed, it can enter delayed removal
> where disk_count drops to 0 and remove_work is scheduled via
> mpath_can_remove_head().
> 
> If a path is then re-probed, disk_count becomes 1.

The re-probe should fail early as mpath_head->disk is still set

> If that probe subsequently
> fails and sd_mpath_fail_probe() is called, the disk_count drops back to 0,
> which triggers the device deletion and frees the head.
> 
> However, since this path bypasses mpath_can_remove_head(), could the pending
> remove_work scheduled earlier go uncancelled, potentially leading to a
> use-after-free of mpath_head when the delayed work timer expires?
> 
>> +}
> [ ... ]
>> @@ -4171,6 +4529,7 @@ static int sd_probe(struct scsi_device *sdp)
>>   	sd_revalidate_disk(gd);
>>   	if (sdp->sector_size > PAGE_SIZE) {
>>   		if (sd_large_pool_create()) {
>> +			sd_mpath_fail_probe(sdkp);
>>   			error = -ENOMEM;
>>   			device_unregister(&sdkp->disk_dev);
>>   			put_disk(gd);
> 
> [Severity: Critical]
> This is a pre-existing issue, but could this error path result in a
> double-free of sdkp->disk_dev?
> 
> When sd_large_pool_create() fails, device_unregister() is called on
> sdkp->disk_dev, which drops the refcount to 0 and frees sdkp.
> 
> Immediately following this, put_disk(gd) is called. Because gd->fops is
> already set to &sd_fops, dropping the gendisk refcount invokes
> scsi_disk_free_disk(), which reads the already freed sdkp and attempts a
> second put_device() on sdkp->disk_dev.

I'll check it, but I doubt that there is a problem

> 
>>   			goto out;
>>   		}
>>   	}
>>   
>> +	if (sdp->scsi_mpath_dev) {
>> +		error = sd_mpath_revalidate_head(sdkp);
>> +		if (error)
>> +			sdev_printk(KERN_WARNING, sdp, "could not revalidate multipath limits\n");
>> +	}
>> +
>>   	if (sdp->removable) {
>>   		gd->flags |= GENHD_FL_REMOVABLE;
>>   		gd->events |= DISK_EVENT_MEDIA_CHANGE;
>> @@ -4192,6 +4557,7 @@ static int sd_probe(struct scsi_device *sdp)
>>   
>>   	error = device_add_disk(dev, gd, NULL);
>>   	if (error) {
>> +		sd_mpath_fail_probe(sdkp);
>>   		device_unregister(&sdkp->disk_dev);
>>   		put_disk(gd);
> 
> [Severity: Critical]
> This is a pre-existing issue, but might this error path also suffer from the
> same double-free of sdkp->disk_dev?
> 
> Just like the memory allocation failure path above, if device_add_disk()
> fails, device_unregister() frees sdkp, and the subsequent put_disk(gd)
> accesses the freed sdkp and performs a second put_device() on
> sdkp->disk_dev.

As with v7, this is fine

> 
>>   		if (sdp->sector_size > PAGE_SIZE)
> 


  reply	other threads:[~2026-09-07 12:35 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-07 10:49 [PATCH v8 00/27] Native SCSI Multipath support John Garry
2026-09-07 10:49 ` [PATCH v8 01/27] libmultipath: Add initial framework John Garry
2026-09-07 10:49 ` [PATCH v8 02/27] libmultipath: Add basic gendisk support John Garry
2026-09-07 10:49 ` [PATCH v8 03/27] libmultipath: Add path selection support John Garry
2026-09-07 11:09   ` sashiko-bot
2026-09-07 12:00     ` John Garry
2026-09-07 10:49 ` [PATCH v8 04/27] libmultipath: Add bio handling John Garry
2026-09-07 10:49 ` [PATCH v8 05/27] libmultipath: Add support for mpath_device management John Garry
2026-09-07 10:50 ` [PATCH v8 06/27] libmultipath: Add delayed removal support John Garry
2026-09-07 11:23   ` sashiko-bot
2026-09-07 12:01     ` John Garry
2026-09-07 10:50 ` [PATCH v8 07/27] libmultipath: Add sysfs helpers John Garry
2026-09-07 10:50 ` [PATCH v8 08/27] libmultipath: Add support for block device IOCTL John Garry
2026-09-07 10:50 ` [PATCH v8 09/27] libmultipath: Add mpath_bdev_getgeo() John Garry
2026-09-07 10:50 ` [PATCH v8 10/27] libmultipath: Add mpath_bdev_get_unique_id() John Garry
2026-09-07 10:50 ` [PATCH v8 11/27] scsi-multipath: introduce basic SCSI device support John Garry
2026-09-07 11:33   ` sashiko-bot
2026-09-07 12:06     ` John Garry
2026-09-07 10:50 ` [PATCH v8 12/27] scsi-multipath: introduce scsi_device head structure John Garry
2026-09-07 10:50 ` [PATCH v8 13/27] scsi-multipath: provide sysfs link from to scsi_device John Garry
2026-09-07 10:50 ` [PATCH v8 14/27] scsi-multipath: support iopolicy John Garry
2026-09-07 10:50 ` [PATCH v8 15/27] scsi-multipath: clone each bio John Garry
2026-09-07 11:45   ` sashiko-bot
2026-09-07 12:08     ` John Garry
2026-09-07 10:50 ` [PATCH v8 16/27] scsi-multipath: clear path when device is blocked John Garry
2026-09-07 11:49   ` sashiko-bot
2026-09-07 12:08     ` John Garry
2026-09-07 10:50 ` [PATCH v8 17/27] scsi-multipath: revalidate paths upon device unblock John Garry
2026-09-07 11:56   ` sashiko-bot
2026-09-07 12:10     ` John Garry
2026-09-07 10:50 ` [PATCH v8 18/27] scsi-multipath: failover handling John Garry
2026-09-07 12:02   ` sashiko-bot
2026-09-07 12:13     ` John Garry
2026-09-07 10:50 ` [PATCH v8 19/27] scsi-multipath: provide callbacks for path state John Garry
2026-09-07 12:03   ` sashiko-bot
2026-09-07 12:14     ` John Garry
2026-09-07 10:50 ` [PATCH v8 20/27] scsi-multipath: add scsi_mpath_{start,end}_request() John Garry
2026-09-07 12:09   ` sashiko-bot
2026-09-07 12:22     ` John Garry
2026-09-07 10:50 ` [PATCH v8 21/27] scsi-multipath: add delayed disk removal support John Garry
2026-09-07 10:50 ` [PATCH v8 22/27] scsi: sd: add multipath disk class John Garry
2026-09-07 10:50 ` [PATCH v8 23/27] scsi: sd: add multipath disk attr groups John Garry
2026-09-07 10:50 ` [PATCH v8 24/27] scsi: sd: support multipath disk John Garry
2026-09-07 12:25   ` sashiko-bot
2026-09-07 12:34     ` John Garry [this message]
2026-09-07 10:50 ` [PATCH v8 25/27] scsi: sd: add mpath_dev file John Garry
2026-09-07 10:50 ` [PATCH v8 26/27] scsi: sd: add mpath_numa_nodes dev attribute John Garry
2026-09-07 10:50 ` [PATCH v8 27/27] scsi: sd: add mpath_queue_depth " John Garry

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a7be8330-1678-44e2-8c1b-eb48cb205eeb@linux.dev \
    --to=john.garry@linux.dev \
    --cc=john.g.garry@oracle.com \
    --cc=linux-scsi@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox