From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.169 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796452396; b=devLccNHQ/r/z/zxWBIKdxpmGdW5eoXJ0H7yAVnlHl8Rhd7C2OhdXb6d3P6ddbXXdTpe FoI92pt8FOw2JSbulutmtv1pdRJcK6FchEH/m0dQkgqcetxsbQr/KdK4LDrSDX3SLOZbC w97JaiRDMZucmRfW71iBx2I8Gj1I9DySu00qqTFB8STbmbkZscQnuMoxb8Q1At8yyXJKq rgE/Af+ZEoflEXUeP0dirMdiCZbFhrZ0FbUH5M+JI0XhWecXwLbsY2Hq6HbbrynGuPK0y vhV/ynT5jF/tI7ZmOJD/5KfGmGcPfl6wQhxzCz8V6qLUBHcnVqNVS4efNG24n84/H7A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796452396; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-Id:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=RXaHg5gequXZnv6w8WU8o2pR0B/+2ycAEYltVIPhHd8=; b=yzq8RIxletm9j5Wvk2VlLX04PA61MBB2n075eaukYEDUKQTvWvWk3uzzYiKpqycCHYO/ LlrvB+E56IoA6pb4+/+CeLgTtTN8HTc0Z9+O5uq3tmxLHbgXvTxutNbHFi6ux7mq6E1y/ g/bo1H/CG5KiSTeRiGgEJhmO44rJOz15IcIZj1vqAwVuzZo9D/Y7UQibF4b5mVmxumkkJ IDC+n62WL4WkIFpC8mexSm5CtMHdsmUI+bZOdmqYkAMBeqDlxhi3E1DcT+OTZN+n0vLTb K45kKzpANC6b8ZBSpC233yapuqvLmbf00AkNQtAwYDzzqHPd33+UHkWvteKAz6lH3YA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=c5etL5+Z; arc=none smtp.remote-ip=74.125.82.169 Received: from mail-dy1-f169.google.com (mail-dy1-f169.google.com [74.125.82.169]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 96945606A68D for ; Sat, 5 Dec 2026 06:33:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 96945606A68D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=c5etL5+Z Received: by mail-dy1-f169.google.com with SMTP id 5a478bee46e88-387486ad5abso2648406eec.0 for ; Fri, 04 Dec 2026 22:33:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796452395; x=1797057195; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=RXaHg5gequXZnv6w8WU8o2pR0B/+2ycAEYltVIPhHd8=; b=c5etL5+ZDljSQdxog8tgbX6oSHh6MqYcmBrWdrTVoLu6NGk2E/52679d2eSWW2hHwW XJze8pVOujFXMv7ien69jBzz+Qw1hDbcc5YVk21+/l/rvbGfHb15+ScF6UGlkJ+XaYgc viJhMhJNG83tProGTp7Le7hAmDJy8tLcsGMIwnc3ZR2JVemy2Qso1ib/uwzQXALUV3CB EWq4kpRfbVVFDJ8YLGrqxMFaZvpjj6Y3KSjmiI4lQnx6ClTy9CcOrZ+K/MEUQstBaG1j oHR4ckA0Ir6/N/UwOXmV3puiVvtF/QFcvgjsRiYts4Pwv8i3brFL/kDs5u+t63woTsqM u8jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796452395; x=1797057195; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=RXaHg5gequXZnv6w8WU8o2pR0B/+2ycAEYltVIPhHd8=; b=jkvbEOunMlgMWbQosxxZqNfYs82M7Np4AuXaw0068qwYiog/ZJ6952F/Wyw1gioy/z lt9YV/0fpVz1KGYgnW10UPv/+GEdpIuJjmK4CGvqeZOwFcmsMrH1KGht1fHWIPw+JHbe yN5T+FiujiYvm4YtqcUW9crVnwTQS1Raxy+ttRkftjsIk5Cr36F0lEi4DDfMsmvcqxA2 4l5M5UlHfsWPS3r0TewuhLdwsPPGUWzpdhu2OfDK5bUOAgUCRLocWq2QaRjLnW33K+v4 KPbgJb0fGEbYJqASCTj/EPQLYheWBvqa2jD72KPsD2HRsQl+j4GaYZUa8xKSnE9zm03Z ubuw== X-Forwarded-Encrypted: i=1; AL+hErEfcI9b7Ntq1T9n57rsPerK2PQfPtyMxMXgaL7+Vp51Bhm5OBmIjbbJE759CaHBpNTRYTPnwpMTnF2d@vger.kernel.org X-Gm-Message-State: AB4q+uHLkFO1xUEHVsbwA3VDYgSu28g9j8GmVl3Ff/Tc0u+tX561eYR6 11Jlc8sXw4PWt1Rv/cu2KbnPH9/cGSvpgkryOocwkpPnrHh5/t6KBk8P X-Gm-Gg: AZFdTZdIXkUWkmhmLw3nFyybwZ8pq7ZXCRhFMl1U8XBzLCkOgLxQHDVrbu7J2CWacKx Qd+3cSEOZoPNhsL4VOgIvmTkxYBkJKmSl+hXJfAVWdqtNLEyQ3Rgb65cruNK5XNJ516pBUjxKuQ kXrQ4SiEkZPrnrU52m9vyYIeLPfVApezjb13WE0ODRYBoYrUn/X7yJuIY+3zIKCZ0E4OUQ/idEG 3S2d4hjdlhYJvu1Vb50XHBDlBS6+c8FbrKMyLw0GkI/gsA1B0ziGNdL6Y1O7I6jzDfRP0Ddz7Cf eH3X3lmjM/LjX8PFRpf/xz/ik4+YMZf4dlF48Oy/uPLbt6SxLcD4BF0qOujMKLZk49yUX8fKlAw sSUYciqmpMQoztfxOzm8P0N8KLM6jfccHfcSl0lB0V0z1EDAsf52MjtTWF09eoo1jXL2GVGbuMX w7ddJcZPF0PSAVQnzQLLsjPlWe6Vyt4wXowY7vzSrJxuxfRYWIiie13vGFwu6wdCQwY9pL3qVhR KumjiOeVIykarvrV3TluLuro9i2w66wZT2u4OtWj6apkxw02PStWnit3aCS1nd5Z08g9M0R2QAw q/k4c4Qrt5Tj7E6IA918kKrfOtAvjDbxxMSFgJaqvTrRGbFIkniVQeE1jaimwA8jsVg= X-Received: by 2002:a05:7301:5158:b0:386:f868:8caf with SMTP id 5a478bee46e88-3875216043dmr3457284eec.8.1796452394709; Fri, 04 Dec 2026 22:33:14 -0800 (PST) Received: from localhost.localdomain (wood.seclab.cs.ucsb.edu. [128.111.49.37]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-387521bb842sm14612341eec.15.2026.12.04.22.33.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 22:33:14 -0800 (PST) From: Praneeth Balasubramanian To: Sathya Prakash , Sreekanth Reddy , Suganath Prabu Subramani , Ranjan Kumar , "James E.J. Bottomley" , "Martin K. Petersen" Cc: Praneeth Balasubramanian , 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 Message-Id: <20261205063229.2518424-3-praneethb2002@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20261205063229.2518424-1-praneethb2002@gmail.com> References: <20261205063229.2518424-1-praneethb2002@gmail.com> X-Mailing-List: linux-scsi@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit _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 --- 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