From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.175 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796452395; b=G+eQc8CClnkFb5N3j6eybz7++2R4/aTdIuuT0B50UEFuefoO7T6Q6htvpUEJ69g4dp1K GvARihGDy91xC1n6hpPUQOl4tllS8sFIqn7M2KqjJW68CHmrrkepMuuC86clJhS6jwrgr Kljv+JNVFKI7VMVWCXrxvZSgBkhV3H3hyUwxTxiXMjhxE63e0Q20Mum8VnivMIKs5V5CM du5CMNdVjFNoUhfA5NF26gdXl9n/K5iMGvLzi8XgsYUr5JaevyQUgZA5VkJ7ufHi8KemI OQcff9nWRCcaJlnUqduDgXu1byrvzvYLOeMzRiLTNkqY28YMlSK22sHwtZyrcHhmOPw== 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=aMILsR3lT16/Tsekj18Gay3didlxOm4waNc1dXiToWeSWJqBJHeS1qF2x8Yv3QDzHIWr EOq2REyW4extpnQPgpSJVBsszg/S28B4aAglTZ38sMxH8Ux0NilCAqLIEf8wiLpTym5y6 iPNVuf6jk36ZsbRnLOMyLl/s8gOk2fB+HrKZGNg4zV+zRunUQTj5pVjmm79yl2h0R5bCj r7LiCNoe2OTutTTN3/KfTsTxbLlscrJiuuhhK8yx5XugxSyxPTgF5MavvnuAOHB0UmJ85 he7UQpC42OVFSFXNL/FCWVALXoL8cKLWe6ynnjANWceVymvPBQXtUe0RubvbU501ECQ== 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.175 Received: from mail-dy1-f175.google.com (mail-dy1-f175.google.com [74.125.82.175]) (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 8FD20606A68B for ; Sat, 5 Dec 2026 06:33:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8FD20606A68B 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-f175.google.com with SMTP id 5a478bee46e88-38190551625so2012063eec.1 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=yq1n6xDrk6UREf7gA9vvw2pVZrGfXgKQpdtvJpuRNmSPICwWngyiId+N+GN0PCWrox OAX4fsW+a6mOPSze6iTEQI6b5V3sLVeBGYPE/t+r9WqsznZf0v2iKHefHScai7NkfDoN kCRb5mUfNIhMStT6Hdq9bvAwz7mUBpazdaooXuF32OWO8p9qw1tAW8lCuzmqeEGFcySm Od9KlCYOwbYQqtWETdeTGb2G1KfNZEw7QoPaA73FDawx6g47f8K8xdmVxSb6686Ij6IV WqgJn2JEh3IGzaIFAt33EZ2/nWDUL6vx2ctxyrP3PsSLfd+gDDtvoLioJ/9ZDAY3ShnU BlsA== X-Forwarded-Encrypted: i=1; AL+hErGEFGXhkc3l80eH062ibs4kop/5nHLDZXLRdEmwKWkq6Eu//We8qVZrhbKFFtIfw11UDKvpubE1ekaoPrM=@vger.kernel.org X-Gm-Message-State: AB4q+uHW6f5U/cVn22pjJewkXqtkztM0b4gGb3PuyE91XpEbhp3TVBx2 dWOzXXdNQopfk5WlwUweTPwh77s5NucEcUpHPDYd40bGURzPxF/G4gzL X-Gm-Gg: AZFdTZfpSDbpwYScp/vJo9RyG1ArzCX7pnZz4FFkkFs5lCptc3OC8O/J/ozfvx0EjeV bfKOfyKECtA5ze5HYQChOveC1ByjyvJ82zvfcaH4xSRxnw+N9Ld3/jXlvkPOgG0dyzRO+Qh9Tn0 zXiCiJ4q+QWWAYDzr3HzZ4Tkkj9sU4HYzADudSc1/jTIeyy+cHc+DLX/TcBUlCRtIZVJlFf41oB XuaUKoJa0LMpM+1hOTKqZPy5MNHCrwzWLMFKiuxMceczb4+B5yKVHg5gte27QDekw1hTXVIf7uU wMT4yBhdtxmys20w8NhnCVasrW9A6l0zs+fsSnY3JD+b4r6HJ2zghGO8SGn6C8D81jvu6Xpv/w5 n8hpLLfGKARX2TzREhTlGbUluwyvwoqdsgq9ieSGfbtbKhC+a+lVZ8QOBhNcdhq2Urepo4sfOP0 QxyxccqP0fN68RRRvUxVIU0RS7k+TelCp9tjDAewb1gy6t/EyMOPKCfMGVZCfgAsJpYD8E2NBHJ bZdTC4gT7WF7uMltOeeR+EuRtlA+CyYYLAkLhw859JiNpAo20m+65V63AKSGfdXb1rp09G/PMvx 0UR16JCX8WIcFs+7LmRYZX9a4tEEveBGZFoClNcYITvysQP7dnUG4XyLZNXywzp1dpk= 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-kernel@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