From: Praneeth Balasubramanian <praneethb2002@gmail.com>
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>
Cc: Praneeth Balasubramanian <praneethb2002@gmail.com>,
stable@vger.kernel.org, MPT-FusionLinux.pdl@broadcom.com,
linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 2/9] scsi: mpt3sas: do not keep an enclosure pointer across a host reset
Date: Fri, 4 Dec 2026 22:32:22 -0800 [thread overview]
Message-ID: <20261205063229.2518424-3-praneethb2002@gmail.com> (raw)
In-Reply-To: <20261205063229.2518424-1-praneethb2002@gmail.com>
_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@vger.kernel.org
Signed-off-by: Praneeth Balasubramanian <praneethb2002@gmail.com>
---
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
next prev parent reply other threads:[~2026-12-05 6:33 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-05 6:32 [PATCH 0/9] scsi: fix missing locking and dump length checks in several drivers Praneeth Balasubramanian
2026-12-05 6:32 ` [PATCH 1/9] scsi: bnx2i: Hold the session back_lock around iscsi_itt_to_task() Praneeth Balasubramanian
2026-12-05 6:32 ` Praneeth Balasubramanian [this message]
2026-12-05 6:32 ` [PATCH 3/9] scsi: pmcraid: Take host_lock around scsi_report_bus_reset() Praneeth Balasubramanian
2026-12-05 6:32 ` [PATCH 4/9] scsi: ipr: Skip dump entries whose length is not a 4 byte multiple Praneeth Balasubramanian
2026-12-05 6:32 ` [PATCH 5/9] scsi: arm: Fix eh_abort race against the interrupt handler Praneeth Balasubramanian
2026-12-05 6:32 ` [PATCH 6/9] scsi: csiostor: hold hw lock when issuing port stats mailbox command Praneeth Balasubramanian
2026-12-05 6:32 ` [PATCH 7/9] scsi: fnic: Hold fnic_lock for the oxid calls in the FIP timer work Praneeth Balasubramanian
2026-12-05 6:32 ` [PATCH 8/9] scsi: qla2xxx: Hold qp_lock when completing command in abort timeout Praneeth Balasubramanian
2026-12-05 6:32 ` [PATCH 9/9] scsi: target: core: Fix PRgeneration race in REGISTER AND MOVE Praneeth Balasubramanian
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=20261205063229.2518424-3-praneethb2002@gmail.com \
--to=praneethb2002@gmail.com \
--cc=James.Bottomley@HansenPartnership.com \
--cc=MPT-FusionLinux.pdl@broadcom.com \
--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=stable@vger.kernel.org \
--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