From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.182 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796452395; b=B2SujYOGTlybD/hyEFqD+q0n+MPVwxrbjSOWGNRNoKH0n+vi+yiir1v8IGn+VFeEqKSj InCHXPUmxsuyK853hc6XQLrHBKTFZ7L0ePfRwmkaQKNxB4dXuUM95Sp64KGeR1FQiI/7L eQn3Shu3FJK7zjy3R+Ew0VPFfOZiI4TxHmSA94dktUOx/uoAqmguAIzQC/hUwt96w1w6R 6wu1Ye9Ep9G7xod7fvLY3vwiTyk7KxD356sj5ldsOkjHKqOEvDMFPoxejToriXpuMNYL8 Jj7J6tSoeVKz44EsDvAgiiTze7UwNlNY9hD/7MWk4vVOuyQG1O6+hH92aszPv1GUHDg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796452395; 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=WJo1tWdXjO/qbxx776aocsL5e22E9biUIWlonuKmSCfERTteRpDkb/14CQIiUfvQwPB4 e1oEfnzL7oCzRW+03SFL1EyhA/M1aBsbJZV68U/ll9dFbZ7sG1J5UgTCO2u0rJRMIsatf aIgn9pml2puKMi7Eiqkgg9E4orzlwNOX4Xb9wKxoHqbZxYH/AVmG8Uw1SnhWU5CFYxr1H ZbWM5lSvI7E0xsLWCrpzlTxxTMWi1QFmREVUSmRt9Hxk4KdGC4NmnHj+S2goaHmU7ZVaE OJp8bl/sWFZvAq1RxhYlpIefRSlp7fAgV9bR0di1KdEA8FST0GLorxVnLKguvHyWPMw== 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.182 Received: from mail-dy1-f182.google.com (mail-dy1-f182.google.com [74.125.82.182]) (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 854FE606A689 for ; Sat, 5 Dec 2026 06:33:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 854FE606A689 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-f182.google.com with SMTP id 5a478bee46e88-387486ad5abso2648405eec.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=M5FxsprZAVUCUEKPVAFPZxe+AsLTLITCScGq5xNTov1a4moKoalFeiV19XTlgHgRNl Y4SED7Y9CCLLBrsYBzVmmn9QAn1aiPV3KLEiZgtD10bymjNafgtNX1rkXC29RwraTtf6 eILDtiXK6qsTB5Eoar2ZBAR3LePLv1btk9y/YLq9+9s8IxhQhkGPcD26ebR2Vtmo8dgW nXa8ds9Cb/csDi3kXMWN4dKEcn51UTVIgb9gsBm3XD8HYv1mHEio/oS/TN7bWw1optIo pL8F43Mno/czwPTz39q2LypxfO/vVoL3GSzDmOVRQPveh6tSm7lxgM0TD/wuutBv444c f4Kw== X-Forwarded-Encrypted: i=1; AL+hErEhqXUDvzvpN1bi6RR5RBBDpwePKd+XBuKJtjXlf0yJZic0WJfaKHGAvvZDyG6oB652tcnS+/w=@vger.kernel.org X-Gm-Message-State: AB4q+uFXC/OaaF5Hm4KGkzeNy4mHMIMWzvS9a28RFq2TAAgJggUsxA6J f9lJP9al3FspFIOc98Ubh7tuMbUaX/xNsYX6/BmjQ9hWi59YCDDrMluqUSJbQMj3 X-Gm-Gg: AZFdTZdVjBfKqubbm38an6Ox0c//GDQdTfFM5zyEfcv7F6tebsq7VK2KZ+vqDOSPjf8 qn3BtZchp7O/GwwZ69Qed7R44DlKF9jIxgRl9oIwFWEXyxUeMQbYVzqGpiJuUI6r7Q2GTh0Qmcl 62x7zaOcX0VAakcrCvDF3JrN8VPAXvHMhHVSbxCxD+XUUlkHyLsLgc5ymjDd+MqYCLNq/qpzzS3 15H4NQCKr9tXhisoIGvo4R1DcRw6e1XqsR00vZCRtm7w3kFqmcL2ggcvmaN//wyG5CXh6rWRAuU PhcTkuz4nVBaag4uoAmZVhv7X/WbTWmwaTiTu0+4Mfr7Py6suGMlx1eMLmCnBNgQ2y2pDstnVTR E+lcZe1/chWhMXYytjCP4GKR3LJbi6iJ9IoKjVUcZv1Dx7Rgv8QjdlD6tNorIJ1HOhq1Y9ZKzyt 65EwD9zIt3uQUmBhWfHKuNYHmmNWHRKLsv51dMLNYeTzSUJ37rnhl4P2NlwsqgIBrUrWhe04HYk 9VxvnTLmhiKUEYNVXSfnrhNPrFszRNhav+MsXHOdWGwqsdj0QvkA5HvgnYOthOMgD1YyJO7og66 MwOtM33CdXCwoSRGH9yprRsG4kpQEfDRgNmNFa/trcBCXf8YfD7PjK8xhoc11OLg76U= 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: stable@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