All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jeff Layton" <jlayton@kernel.org>
Cc: linux-scsi@vger.kernel.org
Subject: Re: [PATCH v3 10/11] scsi: mpt3sas: Don't use freed hostdata while polling devices
Date: Wed, 18 Nov 2026 23:23:06 +0000	[thread overview]
Message-ID: <sashiko-outbox-222174@kernel.org> (raw)
In-Reply-To: <20261118-sas-abba-v3-10-5562d690cf61@kernel.org>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] scsi: mpt3sas: unlocked hostdata dereference in device unblock
  loops
--

commit a3557f5a5117e28396670a7dd01d7199c96743db
Author: Jeff Layton <jlayton@kernel.org>

scsi: mpt3sas: Don't use freed hostdata while polling devices

This commit serializes device polling and unblocking against sdev hostdata
teardown using ioc->block_lock so that polling loops stop when hostdata is
freed.

> diff --git a/drivers/scsi/mpt3sas/mpt3sas_scsih.c b/drivers/scsi/mpt3sas/mpt3sas_scsih.c
> index 09dfa3124ebf9..814c18289cf1c 100644
> --- a/drivers/scsi/mpt3sas/mpt3sas_scsih.c
> +++ b/drivers/scsi/mpt3sas/mpt3sas_scsih.c
[ ... ]
> @@ -4098,10 +4191,11 @@ _scsih_ublock_io_all_device(struct MPT3SAS_ADAPTER *ioc, u8 no_turs)
>  	enum device_responsive_state rc;
>  	struct _sas_device *sas_device = NULL;
>  	struct _pcie_device *pcie_device = NULL;
> +	struct _scsih_target_ids ids;
>  	int count;
>  	u8 tr_method = 0;
>  	u8 tr_timeout = 30;
> -
> +	unsigned long flags;
>  
>  	shost_for_each_device(sdev, ioc->shost) {
>  		sas_device_priv_data = sdev->hostdata;
> @@ -4116,23 +4210,40 @@ _scsih_ublock_io_all_device(struct MPT3SAS_ADAPTER *ioc, u8 no_turs)
>  			continue;
>  
>  		if ((no_turs) || (!issue_scsi_cmd_to_bringup_drive)) {
> -			sdev_printk(KERN_WARNING, sdev, "device_unblocked handle(0x%04x)\n",
> -				sas_device_priv_data->sas_target->handle);
> -			_scsih_internal_device_unblock(sdev, sas_device_priv_data);
> +			spin_lock_irqsave(&ioc->block_lock, flags);
> +			if (sdev->hostdata != sas_device_priv_data)
> +				goto next;
> +			if (sas_device_priv_data->block) {
> +				sdev_printk(KERN_WARNING, sdev,
> +				    "device_unblocked handle(0x%04x)\n",
> +				    sas_target->handle);
> +				_scsih_internal_device_unblock(sdev,
> +				    sas_device_priv_data);
> +			}
> +next:
> +			spin_unlock_irqrestore(&ioc->block_lock, flags);
>  			continue;
>  		}
[ ... ]
> @@ -4207,27 +4315,32 @@ _scsih_ublock_io_device_wait(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
[ ... ]
>  	/* moving devices from SDEV_BLOCK to SDEV_RUNNING state */
>  	shost_for_each_device(sdev, ioc->shost) {
>  		sas_device_priv_data = sdev->hostdata;
>  		if (!sas_device_priv_data)
>  			continue;
>  		sas_target = sas_device_priv_data->sas_target;
>  		if (!sas_target)
>  			continue;
>  		if (sas_target->sas_address != sas_address ||
>  		    sas_target->port != port)
>  			continue;
>  		if (!sas_device_priv_data->block)
>  			continue;
>  
> +		/*
> +		 * Polling can take minutes, and sdev_destroy() and
> +		 * scsi_target_reap() can free the hostdata meanwhile.
> +		 */
> +		rc = DEVICE_ERROR;
>  		count = 0;
>  		do {
>  			host_reset_completion_count = 0;
> -			pcie_device = mpt3sas_get_pdev_by_handle(ioc, sas_target->handle);
> +			if (!_scsih_get_target_ids(ioc, sdev,
> +			    sas_device_priv_data, &ids))
> +				break;

[Severity: High]
Can the initial checks at the top of _scsih_ublock_io_all_device() and the
second loop of _scsih_ublock_io_device_wait() still read freed
sas_device_priv_data and sas_target before ioc->block_lock is taken?

Unlike the first loop in _scsih_ublock_io_device_wait(), both of these
loops read sdev->hostdata and dereference sas_device_priv_data->sas_target,
sas_target->deleted (or sas_address and port), and
sas_device_priv_data->block without holding ioc->block_lock:

CPU0                                    CPU1
_scsih_ublock_io_all_device()
  sas_device_priv_data = sdev->hostdata
                                        __scsi_remove_device(sdev)
                                          scsih_sdev_destroy(sdev)
                                            spin_lock(&ioc->block_lock)
                                            sdev->hostdata = NULL
                                            spin_unlock(&ioc->block_lock)
                                            kfree(sas_device_priv_data)
                                          scsi_target_reap()
                                            ...
                                              kfree(sas_target)
  sas_target =
    sas_device_priv_data->sas_target    <-- use-after-free
  if (sas_target->deleted)              <-- use-after-free

Taking ioc->block_lock at the start of each iteration to validate
sdev->hostdata, sas_target, and sas_device_priv_data->block before deciding
whether to unblock or poll the device would close this window.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261118-sas-abba-v3-0-5562d690cf61@kernel.org?part=10

  reply	other threads:[~2026-11-18 23:23 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 23:09 [PATCH v3 00/11] scsi: mpt3sas: Fix delayed not-responding block deadlock and related bugs Jeff Layton
2026-11-18 23:09 ` [PATCH v3 01/11] scsi: mpt3sas: Don't leave requeued firmware events pending at teardown Jeff Layton
2026-11-18 23:33   ` sashiko-bot
2026-11-18 23:09 ` [PATCH v3 02/11] scsi: mpt3sas: Hold event references while cleaning up the event queue Jeff Layton
2026-11-18 23:10 ` [PATCH v3 03/11] scsi: mpt3sas: Requeue topology events only for busy devices Jeff Layton
2026-11-18 23:10 ` [PATCH v3 04/11] scsi: mpt3sas: Fix pcie_device leak in _scsih_ublock_io_device_wait() Jeff Layton
2026-11-18 23:10 ` [PATCH v3 05/11] scsi: mpt3sas: Restore unblock retry " Jeff Layton
2026-11-18 23:10 ` [PATCH v3 06/11] scsi: mpt3sas: Stop polling devices when the unresponding scan is cancelled Jeff Layton
2026-11-18 23:27   ` sashiko-bot
2026-11-18 23:10 ` [PATCH v3 07/11] scsi: mpt3sas: Reset the TUR retry count for each device Jeff Layton
2026-11-18 23:10 ` [PATCH v3 08/11] scsi: mpt3sas: Cache the missing delays the IOC actually uses Jeff Layton
2026-11-18 23:10 ` [PATCH v3 09/11] scsi: mpt3sas: Serialize interrupt-time sdev block with hostdata teardown Jeff Layton
2026-11-18 23:10 ` [PATCH v3 10/11] scsi: mpt3sas: Don't use freed hostdata while polling devices Jeff Layton
2026-11-18 23:23   ` sashiko-bot [this message]
2026-11-18 23:10 ` [PATCH v3 11/11] scsi: mpt3sas: Time out delayed not-responding device blocks Jeff Layton
2026-11-18 23:33   ` sashiko-bot
2026-11-18 23:13 ` [PATCH v3 00/11] scsi: mpt3sas: Fix delayed not-responding block deadlock and related bugs Jeff Layton

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=sashiko-outbox-222174@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=jlayton@kernel.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.