mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jeff Layton <jlayton@kernel.org>
To: Sathya Prakash <sathya.prakash@broadcom.com>,
	 Sreekanth Reddy <sreekanth.reddy@broadcom.com>,
	 Suganath Prabu Subramani <suganath-prabu.subramani@broadcom.com>,
	 Ranjan Kumar <ranjan.kumar@broadcom.com>,
	 "James E.J. Bottomley" <James.Bottomley@HansenPartnership.com>,
	 "Martin K. Petersen" <mkp@kernel.org>,
	 Nagalakshmi Nandigama <Nagalakshmi.Nandigama@lsi.com>,
	 James Bottomley <JBottomley@Parallels.com>
Cc: Keith Busch <kbusch@kernel.org>,
	 Sreekanth Reddy <Sreekanth.Reddy@lsi.com>,
	MPT-FusionLinux.pdl@broadcom.com,  linux-scsi@vger.kernel.org,
	linux-kernel@vger.kernel.org,  Jeff Layton <jlayton@kernel.org>
Subject: [PATCH v3 10/11] scsi: mpt3sas: Don't use freed hostdata while polling devices
Date: Wed, 18 Nov 2026 15:10:07 -0800	[thread overview]
Message-ID: <20261118-sas-abba-v3-10-5562d690cf61@kernel.org> (raw)
In-Reply-To: <20261118-sas-abba-v3-0-5562d690cf61@kernel.org>

_scsih_ublock_io_all_device() and _scsih_ublock_io_device_wait() send
TURs to each blocked sdev until it is ready, for up to
command_retry_count rounds. They sleep between retries. They read
sdev->hostdata and its sas_target once. Then they use both during
polling and after it.

The sdev reference pins only the scsi_device. __scsi_remove_device()
can call ->sdev_destroy() during this time. For example, this occurs
when a scan fails to probe a new LUN, or when the device is deleted
through sysfs. ->sdev_destroy() frees the hostdata. If the sdev was the
last LUN, scsi_target_reap() then frees the hostdata of the target.
Thus the worker reads and writes freed memory.

Before each TUR, read the handle and the target flags under block_lock.
If the hostdata is gone, stop polling. Do the final unblock under the
lock, and only if the hostdata is unchanged. Copy the target fields
that the offline logging needs. Take block_lock in the other
worker-side walkers that block or unblock sdevs too.

Fixes: aee682fad6cd ("scsi: mpt3sas: Improve device discovery and readiness handling for slow devices")
Signed-off-by: Jeff Layton <jlayton@kernel.org>
---
 drivers/scsi/mpt3sas/mpt3sas_base.h  |   3 +-
 drivers/scsi/mpt3sas/mpt3sas_scsih.c | 210 ++++++++++++++++++++++++++++-------
 2 files changed, 173 insertions(+), 40 deletions(-)

diff --git a/drivers/scsi/mpt3sas/mpt3sas_base.h b/drivers/scsi/mpt3sas/mpt3sas_base.h
index 419fdf164fc6..f5e1edf1a280 100644
--- a/drivers/scsi/mpt3sas/mpt3sas_base.h
+++ b/drivers/scsi/mpt3sas/mpt3sas_base.h
@@ -1249,7 +1249,8 @@ typedef void (*MPT3SAS_FLUSH_RUNNING_CMDS)(struct MPT3SAS_ADAPTER *ioc);
  * @pcie_device_list: pcie device object list
  * @pcie_device_init_list: pcie device object list (used only at init time)
  * @pcie_device_lock:
- * @block_lock: serializes sdev blocking against sdev hostdata teardown
+ * @block_lock: serializes sdev blocking and unblocking against sdev
+ *	hostdata teardown
  * @io_missing_delay: time for IO completed by fw when PDR enabled
  * @device_missing_delay: time for device missing by fw when PDR enabled
  * @sas_id : used for setting volume target IDs
diff --git a/drivers/scsi/mpt3sas/mpt3sas_scsih.c b/drivers/scsi/mpt3sas/mpt3sas_scsih.c
index 09dfa3124ebf..814c18289cf1 100644
--- a/drivers/scsi/mpt3sas/mpt3sas_scsih.c
+++ b/drivers/scsi/mpt3sas/mpt3sas_scsih.c
@@ -4066,6 +4066,99 @@ _scsih_internal_device_unblock(struct scsi_device *sdev,
 	}
 }
 
+/*
+ * Target fields the firmware event worker uses while it polls a device
+ * without holding block_lock.
+ */
+struct _scsih_target_ids {
+	u64 sas_address;
+	struct hba_port *port;
+	u16 handle;
+	u8 raid_component;
+};
+
+/*
+ * Copy the target fields of @sdev into @ids, if @sas_device_priv_data is
+ * still its hostdata.  The caller holds block_lock.
+ */
+static bool
+__scsih_get_target_ids(struct scsi_device *sdev,
+		       struct MPT3SAS_DEVICE *sas_device_priv_data,
+		       struct _scsih_target_ids *ids)
+{
+	struct MPT3SAS_TARGET *sas_target;
+
+	if (sdev->hostdata != sas_device_priv_data)
+		return false;
+	sas_target = sas_device_priv_data->sas_target;
+	ids->sas_address = sas_target->sas_address;
+	ids->port = sas_target->port;
+	ids->handle = sas_target->handle;
+	ids->raid_component =
+	    !!(sas_target->flags & MPT_TARGET_FLAGS_RAID_COMPONENT);
+	return true;
+}
+
+/**
+ * _scsih_get_target_ids - read the target fields of a polled device
+ * @ioc: per adapter object
+ * @sdev: scsi device struct
+ * @sas_device_priv_data: hostdata of @sdev when polling began
+ * @ids: filled in with the target fields
+ *
+ * A host reset can change the handle while the device is polled, so
+ * the handle is read again before each command.
+ *
+ * Return: false if @sdev lost its hostdata, true otherwise.
+ */
+static bool
+_scsih_get_target_ids(struct MPT3SAS_ADAPTER *ioc, struct scsi_device *sdev,
+		      struct MPT3SAS_DEVICE *sas_device_priv_data,
+		      struct _scsih_target_ids *ids)
+{
+	unsigned long flags;
+	bool live;
+
+	spin_lock_irqsave(&ioc->block_lock, flags);
+	live = __scsih_get_target_ids(sdev, sas_device_priv_data, ids);
+	spin_unlock_irqrestore(&ioc->block_lock, flags);
+	return live;
+}
+
+/**
+ * _scsih_end_poll - unblock a device after polling it
+ * @ioc: per adapter object
+ * @sdev: scsi device struct
+ * @sas_device_priv_data: hostdata of @sdev when polling began
+ * @ready: the device passed the readiness check
+ * @ids: filled in with the target fields, if true is returned
+ *
+ * Unblock @sdev, and mark it deleted if it is not @ready.  Nothing is
+ * done if @sdev lost its hostdata while it was polled.
+ *
+ * Return: true if the caller should act on @ready, false otherwise.
+ */
+static bool
+_scsih_end_poll(struct MPT3SAS_ADAPTER *ioc, struct scsi_device *sdev,
+		struct MPT3SAS_DEVICE *sas_device_priv_data,
+		bool ready, struct _scsih_target_ids *ids)
+{
+	unsigned long flags;
+	bool ret = false;
+
+	spin_lock_irqsave(&ioc->block_lock, flags);
+	if (!__scsih_get_target_ids(sdev, sas_device_priv_data, ids))
+		goto out;
+	if (!ready)
+		sas_device_priv_data->deleted = 1;
+	if (sas_device_priv_data->block)
+		_scsih_internal_device_unblock(sdev, sas_device_priv_data);
+	ret = true;
+out:
+	spin_unlock_irqrestore(&ioc->block_lock, flags);
+	return ret;
+}
+
 /**
  * _scsih_unresponding_scan_cancelled - should polling devices stop
  * @ioc: per adapter object
@@ -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;
 		}
 
+		/*
+		 * Polling can take minutes, and sdev_destroy() and
+		 * scsi_target_reap() can free the hostdata meanwhile.
+		 */
+		rc = DEVICE_ERROR;
 		count = 0;
 		do {
-			pcie_device = mpt3sas_get_pdev_by_handle(ioc, sas_target->handle);
+			if (!_scsih_get_target_ids(ioc, sdev,
+			    sas_device_priv_data, &ids))
+				break;
+			pcie_device = mpt3sas_get_pdev_by_handle(ioc, ids.handle);
 			if (pcie_device && (!ioc->tm_custom_handling) &&
 				(!(mpt3sas_scsih_is_pcie_scsi_device(pcie_device->device_info)))) {
 				tr_timeout = pcie_device->reset_timeout;
 				tr_method = MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE;
 			}
 			rc = _scsih_wait_for_device_to_become_ready(ioc,
-			    sas_target->handle, 0, (sas_target->flags &
-			    MPT_TARGET_FLAGS_RAID_COMPONENT), sdev->lun, tr_timeout, tr_method);
+			    ids.handle, 0, ids.raid_component, sdev->lun,
+			    tr_timeout, tr_method);
 			if (rc == DEVICE_RETRY || rc == DEVICE_START_UNIT ||
 			    rc == DEVICE_STOP_UNIT || rc == DEVICE_RETRY_UA)
 				ssleep(1);
@@ -4147,26 +4258,23 @@ _scsih_ublock_io_all_device(struct MPT3SAS_ADAPTER *ioc, u8 no_turs)
 			scsi_device_put(sdev);
 			return;
 		}
-		sas_device_priv_data->block = 0;
-		if (rc != DEVICE_READY)
-			sas_device_priv_data->deleted = 1;
-
-		_scsih_internal_device_unblock(sdev, sas_device_priv_data);
+		if (!_scsih_end_poll(ioc, sdev, sas_device_priv_data,
+		    rc == DEVICE_READY, &ids))
+			continue;
 
 		if (rc != DEVICE_READY) {
 			sdev_printk(KERN_WARNING, sdev, "%s: device_offlined,\n"
 			    "handle(0x%04x)\n",
-			    __func__, sas_device_priv_data->sas_target->handle);
+			    __func__, ids.handle);
 			scsi_device_set_state(sdev, SDEV_OFFLINE);
 			sas_device = mpt3sas_get_sdev_by_addr(ioc,
-					sas_device_priv_data->sas_target->sas_address,
-					sas_device_priv_data->sas_target->port);
+					ids.sas_address, ids.port);
 			if (sas_device) {
 				_scsih_display_enclosure_chassis_info(NULL, sas_device, sdev, NULL);
 				sas_device_put(sas_device);
 			} else {
 				pcie_device = mpt3sas_get_pdev_by_wwid(ioc,
-						    sas_device_priv_data->sas_target->sas_address);
+						    ids.sas_address);
 				if (pcie_device) {
 					if (pcie_device->enclosure_handle != 0)
 						sdev_printk(KERN_INFO, sdev, "enclosure logical id\n"
@@ -4184,7 +4292,7 @@ _scsih_ublock_io_all_device(struct MPT3SAS_ADAPTER *ioc, u8 no_turs)
 		} else
 			sdev_printk(KERN_WARNING, sdev, "device_unblocked,\n"
 			    "handle(0x%04x)\n",
-			    sas_device_priv_data->sas_target->handle);
+			    ids.handle);
 	}
 }
 
@@ -4207,27 +4315,32 @@ _scsih_ublock_io_device_wait(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
 	int host_reset_completion_count;
 	struct _sas_device *sas_device;
 	struct _pcie_device *pcie_device;
+	struct _scsih_target_ids ids;
 	u8 tr_timeout = 30;
 	u8 tr_method = 0;
 	int count;
+	unsigned long flags;
 
 	/* moving devices from SDEV_OFFLINE to SDEV_BLOCK */
 	shost_for_each_device(sdev, ioc->shost) {
+		spin_lock_irqsave(&ioc->block_lock, flags);
 		sas_device_priv_data = sdev->hostdata;
 		if (!sas_device_priv_data)
-			continue;
+			goto next;
 		sas_target = sas_device_priv_data->sas_target;
 		if (!sas_target)
-			continue;
+			goto next;
 		if (sas_target->sas_address != sas_address ||
 		    sas_target->port != port)
-			continue;
+			goto next;
 		if (sdev->sdev_state == SDEV_OFFLINE) {
 			sas_device_priv_data->block = 1;
 			sas_device_priv_data->deleted = 0;
 			scsi_device_set_state(sdev, SDEV_RUNNING);
 			scsi_internal_device_block_nowait(sdev);
 		}
+next:
+		spin_unlock_irqrestore(&ioc->block_lock, flags);
 	}
 
 	/* moving devices from SDEV_BLOCK to SDEV_RUNNING state */
@@ -4244,10 +4357,18 @@ _scsih_ublock_io_device_wait(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
 		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;
+			pcie_device = mpt3sas_get_pdev_by_handle(ioc, ids.handle);
 			if (pcie_device && (!ioc->tm_custom_handling) &&
 				(!(mpt3sas_scsih_is_pcie_scsi_device(pcie_device->device_info)))) {
 				tr_timeout = pcie_device->reset_timeout;
@@ -4256,8 +4377,8 @@ _scsih_ublock_io_device_wait(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
 			if (pcie_device)
 				pcie_device_put(pcie_device);
 			rc = _scsih_wait_for_device_to_become_ready(ioc,
-			      sas_target->handle, 0, (sas_target->flags &
-			      MPT_TARGET_FLAGS_RAID_COMPONENT), sdev->lun, tr_timeout, tr_method);
+			      ids.handle, 0, ids.raid_component, sdev->lun,
+			      tr_timeout, tr_method);
 			if (rc == DEVICE_RETRY || rc == DEVICE_START_UNIT ||
 			    rc == DEVICE_STOP_UNIT || rc == DEVICE_RETRY_UA) {
 				do {
@@ -4266,10 +4387,13 @@ _scsih_ublock_io_device_wait(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
 				} while (rc == DEVICE_RETRY &&
 							ioc->shost_recovery);
 				if (host_reset_completion_count > 1) {
+					/* the reset may have changed the handle */
+					if (!_scsih_get_target_ids(ioc, sdev,
+					    sas_device_priv_data, &ids))
+						break;
 					rc = _scsih_wait_for_device_to_become_ready(ioc,
-						sas_target->handle, 0, (sas_target->flags &
-						MPT_TARGET_FLAGS_RAID_COMPONENT), sdev->lun,
-						tr_timeout, tr_method);
+						ids.handle, 0, ids.raid_component,
+						sdev->lun, tr_timeout, tr_method);
 					if (rc == DEVICE_RETRY || rc == DEVICE_START_UNIT ||
 					    rc == DEVICE_STOP_UNIT || rc == DEVICE_RETRY_UA)
 						msleep(500);
@@ -4280,23 +4404,23 @@ _scsih_ublock_io_device_wait(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
 		    rc == DEVICE_STOP_UNIT || rc == DEVICE_RETRY_UA)
 			&& count++ <= command_retry_count);
 
-		if (rc != DEVICE_READY)
-			sas_device_priv_data->deleted = 1;
-		_scsih_internal_device_unblock(sdev, sas_device_priv_data);
+		if (!_scsih_end_poll(ioc, sdev, sas_device_priv_data,
+		    rc == DEVICE_READY, &ids))
+			continue;
 
 		if (rc != DEVICE_READY) {
 			sdev_printk(KERN_WARNING, sdev,
 			    "%s: device_offlined, handle(0x%04x)\n",
-			    __func__, sas_device_priv_data->sas_target->handle);
+			    __func__, ids.handle);
 
 			sas_device = mpt3sas_get_sdev_by_handle(ioc,
-				sas_device_priv_data->sas_target->handle);
+				ids.handle);
 			if (sas_device) {
 				_scsih_display_enclosure_chassis_info(NULL, sas_device, sdev, NULL);
 				sas_device_put(sas_device);
 			} else {
 				pcie_device = mpt3sas_get_pdev_by_handle(ioc,
-							sas_device_priv_data->sas_target->handle);
+							ids.handle);
 				if (pcie_device) {
 					if (pcie_device->enclosure_handle != 0)
 						sdev_printk(KERN_INFO, sdev,
@@ -4317,7 +4441,7 @@ _scsih_ublock_io_device_wait(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
 		} else {
 			sdev_printk(KERN_WARNING, sdev,
 				"device_unblocked, handle(0x%04x)\n",
-				sas_device_priv_data->sas_target->handle);
+				ids.handle);
 		}
 	}
 }
@@ -4336,19 +4460,23 @@ _scsih_ublock_io_device(struct MPT3SAS_ADAPTER *ioc,
 {
 	struct MPT3SAS_DEVICE *sas_device_priv_data;
 	struct scsi_device *sdev;
+	unsigned long flags;
 
 	shost_for_each_device(sdev, ioc->shost) {
+		spin_lock_irqsave(&ioc->block_lock, flags);
 		sas_device_priv_data = sdev->hostdata;
 		if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
-			continue;
+			goto next;
 		if (sas_device_priv_data->sas_target->sas_address
 		    != sas_address)
-			continue;
+			goto next;
 		if (sas_device_priv_data->sas_target->port != port)
-			continue;
+			goto next;
 		if (sas_device_priv_data->block)
 			_scsih_internal_device_unblock(sdev,
 				sas_device_priv_data);
+next:
+		spin_unlock_irqrestore(&ioc->block_lock, flags);
 	}
 }
 
@@ -4363,20 +4491,24 @@ _scsih_block_io_all_device(struct MPT3SAS_ADAPTER *ioc)
 {
 	struct MPT3SAS_DEVICE *sas_device_priv_data;
 	struct scsi_device *sdev;
+	unsigned long flags;
 
 	shost_for_each_device(sdev, ioc->shost) {
+		spin_lock_irqsave(&ioc->block_lock, flags);
 		sas_device_priv_data = sdev->hostdata;
 		if (!sas_device_priv_data)
-			continue;
+			goto next;
 		if (sas_device_priv_data->block)
-			continue;
+			goto next;
 		if (sas_device_priv_data->ignore_delay_remove) {
 			sdev_printk(KERN_INFO, sdev,
 			"%s skip device_block for SES handle(0x%04x)\n",
 			__func__, sas_device_priv_data->sas_target->handle);
-			continue;
+			goto next;
 		}
 		_scsih_internal_device_block(sdev, sas_device_priv_data);
+next:
+		spin_unlock_irqrestore(&ioc->block_lock, flags);
 	}
 }
 

-- 
2.56.1


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

Thread overview: 13+ 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: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: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 ` Jeff Layton [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: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=20261118-sas-abba-v3-10-5562d690cf61@kernel.org \
    --to=jlayton@kernel.org \
    --cc=JBottomley@Parallels.com \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=MPT-FusionLinux.pdl@broadcom.com \
    --cc=Nagalakshmi.Nandigama@lsi.com \
    --cc=Sreekanth.Reddy@lsi.com \
    --cc=kbusch@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=mkp@kernel.org \
    --cc=ranjan.kumar@broadcom.com \
    --cc=sathya.prakash@broadcom.com \
    --cc=sreekanth.reddy@broadcom.com \
    --cc=suganath-prabu.subramani@broadcom.com \
    /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

all inboxes | Powered by JetHome®