[PATCH 2/9] scsi: mpt3sas: do not keep an enclosure pointer across a host reset
From: Praneeth Balasubramanian
Date: Sat Dec 05 2026 - 01:33:33 EST
_scsih_add_device() looks up the enclosure node of the new device and
keeps the pointer. Later it reads the node again: in the "device not
ready" message after _scsih_wait_for_target_to_become_ready(), and when
it fills in the enclosure logical ID and chassis slot of the new
sas_device. In between, it sleeps in the readiness wait, in kzalloc()
and in the config page request of _scsih_get_sas_address().
A host reset frees every enclosure node and builds the list again in
_scsih_create_enclosure_list_after_reset(). The reset can be issued by
the readiness wait itself, when a SCSI command and then the target
reset for it time out. It can also come from another context, such as
the fault watchdog. _scsih_fw_event_cleanup_queue() does not wait for
the running firmware event, so _scsih_add_device() keeps running and
then reads freed memory.
Copy the enclosure logical ID and chassis slot right after the lookup
and use the copies later, as _scsih_pcie_add_device() does by reading
them at lookup time.
This was found by an LLM-based audit that checks code against the rules
stated in kernel comments.
Fixes: aee682fad6cd ("scsi: mpt3sas: Improve device discovery and readiness handling for slow devices")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Praneeth Balasubramanian <praneethb2002@xxxxxxxxx>
---
Notes:
This was found by an LLM-based audit, part of research at UCSB SecLab.
An LLM turned a kernel comment into a rule, and three LLM votes checked
the code against it. The fix and changelog were drafted with GLM-5.3,
then checked against the source and partly rewritten with Claude Opus
5.5. I reviewed the patch and take responsibility for it.
Testing: builds without new warnings with W=1 (x86_64 allmodconfig),
clang 23. checkpatch.pl is clean. Not runtime-tested, and there is no
reproducer. I do not have this hardware.
Jeff Layton's mpt3sas series (v5, 2026-11-30) rewrites the firmware
event handling in mpt3sas_scsih.c but not the enclosure lookup. If it
lands first, this patch may need a rebase.
Before aee682fad6cd the pointer was also kept across kzalloc() and the
config page request in _scsih_get_sas_address(), but no readiness wait.
So the window existed since 22a923c315ba ("scsi: mpt3sas: Cache
enclosure pages during enclosure add.") and became much wider with the
readiness wait. The patch applies only on top of aee682fad6cd.
The enclosure list has no lock. The kernel-doc of
mpt3sas_scsih_enclosure_find_by_handle() says the caller should hold
ioc->sas_device_lock, but neither the reset path that frees the list nor
the event that adds to it takes that lock. Other lookups use the node
right away without sleeping, so this patch only fixes the one pointer
that is kept across sleeps.
drivers/scsi/mpt3sas/mpt3sas_scsih.c | 38 +++++++++++++++++-----------
1 file changed, 23 insertions(+), 15 deletions(-)
diff --git a/drivers/scsi/mpt3sas/mpt3sas_scsih.c b/drivers/scsi/mpt3sas/mpt3sas_scsih.c
index 04255286a2a6..6b1d05385bb1 100644
--- a/drivers/scsi/mpt3sas/mpt3sas_scsih.c
+++ b/drivers/scsi/mpt3sas/mpt3sas_scsih.c
@@ -8409,6 +8409,9 @@ _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 retry_count,
struct _sas_device *sas_device;
struct _enclosure_node *enclosure_dev = NULL;
enum device_responsive_state rc;
+ u64 enclosure_logical_id = 0;
+ u8 is_chassis_slot_valid = 0;
+ u8 chassis_slot = 0;
u32 ioc_status;
u64 sas_address;
u32 device_info;
@@ -8463,9 +8466,23 @@ _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 retry_count,
enclosure_dev =
mpt3sas_scsih_enclosure_find_by_handle(ioc,
le16_to_cpu(sas_device_pg0.EnclosureHandle));
- if (enclosure_dev == NULL)
+ if (enclosure_dev == NULL) {
ioc_info(ioc, "Enclosure handle(0x%04x) doesn't match with enclosure device!\n",
sas_device_pg0.EnclosureHandle);
+ } else {
+ /*
+ * A host reset frees the enclosure list and can run
+ * while this function sleeps below. Copy the fields
+ * instead of keeping a pointer to the node.
+ */
+ enclosure_logical_id =
+ le64_to_cpu(enclosure_dev->pg0.EnclosureLogicalID);
+ if (le16_to_cpu(enclosure_dev->pg0.Flags) &
+ MPI2_SAS_ENCLS0_FLAGS_CHASSIS_SLOT_VALID) {
+ is_chassis_slot_valid = 1;
+ chassis_slot = enclosure_dev->pg0.ChassisSlot;
+ }
+ }
}
/*
@@ -8497,10 +8514,9 @@ _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 retry_count,
sas_device_pg0.EnclosureLevel, connector_name));
}
- if ((enclosure_dev) && (le16_to_cpu(enclosure_dev->pg0.Flags) &
- MPI2_SAS_ENCLS0_FLAGS_CHASSIS_SLOT_VALID))
+ if (is_chassis_slot_valid)
ioc_info(ioc, "chassis slot(0x%04x)\n",
- enclosure_dev->pg0.ChassisSlot);
+ chassis_slot);
if (rc == DEVICE_RETRY || rc == DEVICE_START_UNIT ||
rc == DEVICE_STOP_UNIT || rc == DEVICE_RETRY_UA)
@@ -8553,17 +8569,9 @@ _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 retry_count,
sas_device->connector_name[0] = '\0';
}
/* get enclosure_logical_id & chassis_slot*/
- sas_device->is_chassis_slot_valid = 0;
- if (enclosure_dev) {
- sas_device->enclosure_logical_id =
- le64_to_cpu(enclosure_dev->pg0.EnclosureLogicalID);
- if (le16_to_cpu(enclosure_dev->pg0.Flags) &
- MPI2_SAS_ENCLS0_FLAGS_CHASSIS_SLOT_VALID) {
- sas_device->is_chassis_slot_valid = 1;
- sas_device->chassis_slot =
- enclosure_dev->pg0.ChassisSlot;
- }
- }
+ sas_device->enclosure_logical_id = enclosure_logical_id;
+ sas_device->is_chassis_slot_valid = is_chassis_slot_valid;
+ sas_device->chassis_slot = chassis_slot;
/* get device name */
sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
--
2.25.1