From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796446212; b=UoyoPhuFoAbKjDRe4NybTA+7A9klQwGg1bYxcNGefx95/9UsnosQfJwAiFCQC5ryVL0s v3wEZYQnqEXlkXT3QdfbYveuS05ccTx6PkaZKtclmHRVreSdXzScHKSYReStpUBr45W+i 9OZjHDumOofwqvZ2mNA3uXvTXruA37Scz/Uuk8gyNsXjrlJYVJR92TvJwfIURrSUCQN/n 6Gj8G8joLHw9Strqd8pp5pXleWYFvGW0j/mpOFfj5a1huvpRVP9Zpq07c92YOH26pXTdq 36Y9utTzrwqzzdbJC9IuRNwIjm8ryS+DQTzbSYhrr0UFjxDa+g9Uv7c+57udpjT3NSA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796446212; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=3LbNn6+UIrPzzlFjPHd5gZ1/sC7vL00URVDELI1OfOQ=; b=K+GdX9Q3r9uqUVeHYExP6M86Mm/DtyEnYhbgvpGr04nChoQ4/cErrbaBVKdF8UBkaHLA PdiJPC/0VGalXqtCLUPa/qN1oLBYEusCEoOaEXRZHFGLTCGx0DygvdkJ2F7J1kX3nzZAf tv1/V7j3XZc04rssKg8uCcuCJs1ohqX2DrjXd9YEdn3ZKAKZqyUebfmyN+2vbRrvwxyjS Oi+ry9hxqgmxh9zM/f/0kp7XIRQUkIYOl0JoqF8VmFXsSzlUXUHJmqYQHGnZ0m//EZThG feMyeaWdnCqGWCsYHGvJJQhuRIzS9A42JO67NxCn1gfJPq1Na6E5f/itov4bfAxKFVg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=OcKnE8vU; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 72D6C606A322; Sat, 5 Dec 2026 04:50:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=OcKnE8vU Received: by smtp.kernel.org (Postfix) with ESMTPSA id E1EDB1F00936; Sat, 5 Dec 2026 04:50:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796446212; bh=3LbNn6+UIrPzzlFjPHd5gZ1/sC7vL00URVDELI1OfOQ=; h=From:Subject:Reply-To:To:In-Reply-To:References:Date; b=OcKnE8vUT7tBJoYEUB5P0CFc6Vy2g2+E4Pi7IUnGrZVd7vZI6QyIsuSu32WBiz55+ XE7hKEv5TM6+6xX/l65ZjyXEnbpmnoMoIc6JYnZzRed4NFEa0+Ahsjhb4AgEvJ0X9S Ll9o2WZSh+NMB2yc3/ZXCCj+9H2Lry6t8MSuYYDOFWKP7N8+aANbSFG04geJOXSTif cR4Wc5mPlS9YPjeBIq8b9l1SHXLfg+RFltnaX/YZKQpCiHo04kuatQ4dyHj70y+PuN wJ4kwPFc1bHapMZB22Pq8I6jwLI+97Mh7/iZ06+YIZ1r57Mx4kvWSKTdWu2tDbsEDo kfpjTTP/QW67Q== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v18 15/19] PCI/CXL: Restore CXL state after PCI reset Reply-To: sashiko-reviews@lists.linux.dev To: "Srirangan Madhavan" , linux-cxl@vger.kernel.org, linux-pci@vger.kernel.org In-Reply-To: <20261205041453.3946529-16-smadhavan@nvidia.com> References: <20261205041453.3946529-1-smadhavan@nvidia.com> <20261205041453.3946529-16-smadhavan@nvidia.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Sat, 05 Dec 2026 04:50:11 +0000 X-Mailing-List: linux-cxl@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 7 potential issues to consider: - [Medium] cxl: cxl_hdm_restore_state() uncommits decoders in ascending order - [Medium] PCI/CXL: regs_offline and reset_pending cleared before reset cleanup finishes - [Medium] PCI/CXL: cxl_reset_save_disabled_state() clobbers saved PCI state on error - [Medium] PCI/CXL: cxl_reset_hdm_restore() writes stale dvsec_ctrl and global_ctrl - [Medium] PCI/CXL: cxl_reset_save_disabled_state() leaves CXL.mem and CXL.cache enabled - [Medium] PCI/CXL: race between cxl_reset_execute() and cxl_port probe/unbind - [Medium] PCI/CXL: cxl_reset_execute() enables MSE/BME before outer pci_restore_state() -- commit 46128862bc7d9a15961d5d75eb89380f613d4b4f Author: Srirangan Madhavan PCI/CXL: Restore CXL state after PCI reset This commit adds built-in HDM decoder restoration helpers in drivers/cxl/core/hdm_regs.c and hooks them into the CXL Device Reset path in drivers/pci/cxl.c to restore PCI state, HDM decoders, and CXL Device DVSEC control after reset. > diff --git a/drivers/cxl/core/hdm_regs.c b/drivers/cxl/core/hdm_regs.c > index cc99ae83d1cd1..358e2ba7495de 100644 > --- a/drivers/cxl/core/hdm_regs.c > +++ b/drivers/cxl/core/hdm_regs.c [ ... ] > +static int > +cxl_hdm_restore_decoder(void __iomem *hdm, > + const struct cxl_endpoint_decoder_settings *settings) > +{ > + const struct cxl_decoder_config *config =3D &settings->config; > + bool retained; > + u32 ctrl; > + int rc; > + > + rc =3D cxl_hdm_decoder_uncommit(hdm, config->id, &retained); [ ... ] > +int cxl_hdm_restore_state(void __iomem *hdm, > + const struct cxl_hdm_info *snapshot) > +{ > + int count; > + int rc; > + > + count =3D cxl_hdm_decoder_count(readl(hdm + CXL_HDM_DECODER_CAP_OFFSET)= ); > + if (count !=3D snapshot->decoder_count) > + return -ENXIO; > + > + /* Restore global HDM control before committing individual decoders. */ > + writel(snapshot->global_ctrl, hdm + CXL_HDM_DECODER_CTRL_OFFSET); > + > + for (int i =3D 0; i < snapshot->decoder_count; i++) { > + rc =3D cxl_hdm_restore_decoder(hdm, &snapshot->settings[i]); > + if (rc) > + return rc; > + } [Severity: Medium] Can this ascending loop uncommit HDM decoders out of order when multiple decoders survive reset? CXL requires HDM decoders to be uncommitted in descending order and committed in ascending order. If decoder 0 and decoder 1 remain committed in hardware, iteration 0 clears Commit on decoder 0 while decoder 1 is still committed. Spec-compliant hardware rejects out-of-order uncommit with Commit Error (-EIO) or never clears Committed (-ETIMEDOUT), aborting restoration and leaving the PCI device disabled. Performing a descending uncommit pass down to any locked committed decoder before the ascending commit pass would preserve hardware ordering. > diff --git a/drivers/pci/cxl.c b/drivers/pci/cxl.c > index 0c60f6d7bb4fa..1053aa2f66071 100644 > --- a/drivers/pci/cxl.c > +++ b/drivers/pci/cxl.c [ ... ] > +static int cxl_restore_dvsec_ctrl(struct pci_dev *pdev, > + const struct cxl_hdm_info *snapshot) > +{ > + int rc; > + > + if (!snapshot->dvsec_ctrl_valid) { > + pci_err(pdev, "CXL DVSEC control snapshot is invalid\n"); > + return -ENXIO; > + } > + > + /* Leave CXL.cache off until the host caches are flushed. */ > + rc =3D pci_write_config_word(pdev, snapshot->dvsec + PCI_DVSEC_CXL_CTRL, > + snapshot->dvsec_ctrl & > + ~PCI_DVSEC_CXL_CACHE_ENABLE); > + if (rc) { > + rc =3D pcibios_err_to_errno(rc); > + pci_err(pdev, "failed to restore CXL DVSEC control: %d\n", rc); > + } > + > + return rc; > +} > + > +/* Turn CXL.cache back on after the host caches are flushed. */ > +static int cxl_reset_enable_cache(struct pci_dev *pdev, > + const struct cxl_hdm_info *snapshot) > +{ > + int rc; > + > + if (!(snapshot->dvsec_ctrl & PCI_DVSEC_CXL_CACHE_ENABLE)) > + return 0; > + > + rc =3D pci_write_config_word(pdev, snapshot->dvsec + PCI_DVSEC_CXL_CTRL, > + snapshot->dvsec_ctrl); [Severity: Medium] Can cxl_restore_dvsec_ctrl(), cxl_reset_enable_cache(), and cxl_hdm_restore_state() write stale pre-reset dvsec_ctrl and global_ctrl values? Because cxl_rwsem.region is dropped after cxl_reset_hdm_prepare() and again after cxl_reset_hdm_restore(), a concurrent cxl_port unbind can run clear_mem_enable() and disable_hdm(), clearing PCI_DVSEC_CXL_MEM_ENABLE and CXL_HDM_DECODER_ENABLE in both hardware and pdev->hdm. The restore path still writes the pre-reset snapshot->global_ctrl and the full snapshot->dvsec_ctrl word, re-enabling CXL.mem or global HDM decoding while cxl_port is unbound. If unbind runs between cxl_reset_hdm_restore() and cxl_reset_enable_cache(), writing the full snapshot->dvsec_ctrl word turns PCI_DVSEC_CXL_MEM_ENABLE back on while CXL_HDM_DECODER_ENABLE remains cleared. Later commit "PCI/CXL: Restore CXL state after CXL bus reset" refreshes per-decoder settings from pdev->hdm, but leaves global_ctrl and dvsec_ctrl stale. Refreshing global_ctrl and dvsec_ctrl from pdev->hdm and using read-modify-write for PCI_DVSEC_CXL_CACHE_ENABLE would avoid restoring stale enable bits. [ ... ] > +static void cxl_reset_save_disabled_state(struct pci_dev *pdev) > +{ > + u32 *saved_command =3D &pdev->saved_config_space[PCI_COMMAND / 4]; > + int rc; > + > + rc =3D pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABL= E); > + if (rc) > + pci_err(pdev, > + "failed to disable device after CXL reset failure: %d\n", > + pcibios_err_to_errno(rc)); > + > + /* Preserve the pre-reset snapshot if PCI state has not been restored. = */ > + if (!pdev->state_saved) { > + rc =3D pci_save_state(pdev); > + if (rc) > + pci_err(pdev, > + "failed to save disabled state after CXL reset failure: %d\n", > + rc); > + } > + > + /* Ensure the outer PCI restore cannot re-enable MSE or BME. */ > + *saved_command &=3D GENMASK(31, 16); > + *saved_command |=3D PCI_COMMAND_INTX_DISABLE; > +} [Severity: Medium] Can calling pci_save_state() here overwrite the intact pre-reset saved PCI state with 0xffffffff when the device is unresponsive? cxl_restore_pci_state_for_hdm_restore() calls pci_restore_state(), which clears pdev->state_saved while leaving pdev->saved_config_space[] and saved capability buffers intact. If a later step fails because the device stopped responding, !pdev->state_saved is true and pci_save_state() reads all 16 header dwords and saved capabilities without checking for read errors or 0xffffffff. That overwrites the saved BARs and capability buffers with all-ones, which the outer pci_dev_restore() then applies. Preserving the existing saved buffers instead of re-reading from a failed device would avoid clobbering them. > +static int cxl_reset_save_restored_state(struct pci_dev *pdev, u16 comma= nd) > +{ > + int rc; > + > + rc =3D pci_write_config_word(pdev, PCI_COMMAND, command); > + if (rc) { > + rc =3D pcibios_err_to_errno(rc); > + pci_err(pdev, "failed to restore PCI command state: %d\n", rc); > + cxl_reset_save_disabled_state(pdev); > + return rc; > + } > + > + rc =3D pci_save_state(pdev); [Severity: Medium] Does enabling MSE and BME here before returning cause the outer pci_dev_restore() -> pci_restore_state() call to reprogram PCI capabilities on an active device? cxl_restore_pci_state_for_hdm_restore() already runs a full pci_restore_state(), and cxl_reset_save_restored_state() then writes PCI_COMMAND (enabling MSE and BME) and re-saves state. When __pci_reset_function_locked() returns, pci_dev_restore() unconditionally calls pci_restore_state() a second time while MSE, BME, and all PCI capabilities are already enabled. On that second pass, pci_restore_rebar_state() writes PCI_REBAR_CTRL while PCI_COMMAND_MEMORY is set (violating the PCIe Resizable BAR requirement), pci_restore_vc_state() reloads VC arbitration tables with extended VCs active, pci_restore_pri_state() writes PCI_PRI_ALLOC_REQ while PRI is enabled, and MSI/MSI-X are masked and rewritten while BME is active. [ ... ] > +static int cxl_reset_hdm_restore(struct pci_dev *pdev, > + struct cxl_hdm_info *snapshot, u16 *command) > +{ > + int rc; > + > + cxl_region_quiesce_lock(); > + rc =3D pci_cxl_restore_state(pdev, snapshot, command); > + /* The registers are usable again; later teardowns write them. */ > + cxl_hdm_cache_set_regs_offline(pdev, false); > + cxl_region_quiesce_unlock(); > + > + return rc; > +} [ ... ] > rc2 =3D cxl_hdm_quiesce_flush(q); > if (rc2) > pci_err(pdev, "failed to invalidate CPU cache after CXL reset: %d\n", = rc2); > rc =3D rc ?: rc2; > cxl_reset_hdm_unmark(pdev); > + if (rc) > + cxl_reset_save_disabled_state(pdev); > =20 > pci_dev_reset_iommu_done(pdev); > return rc; [Severity: Medium] Can clearing regs_offline and reset_pending before reset cleanup finishes let concurrent decoder operations access HDM MMIO prematurely? In cxl_reset_hdm_restore(), regs_offline is cleared even when pci_cxl_restore_state() fails, allowing a concurrent cxl_decoder_reset() to write HDM registers on an unrestored device before cxl_reset_save_disabled_state() clears PCI_COMMAND_MEMORY. Similarly, __cxl_reset_execute() calls cxl_reset_hdm_unmark() before cxl_reset_save_disabled_state(), pci_dev_reset_iommu_done(), cache policy and CXL.cache restoration, and cxl_reset_save_restored_state(). Because cxl_decoder_commit() synchronizes on cxl_rwsem.region rather than device_lock(&pdev->dev), it can pass cxl_hdm_cache_is_reset_pending() and program HDM registers while PCI_COMMAND_MEMORY is being cleared on error or while reset teardown is still incomplete. [ ... ] > + keep_caching_off =3D attempted && rc; > + if (cache.cache_disabled && !keep_caching_off) { > + rc2 =3D cxl_reset_restore_cache_policy(pdev, dvsec, cache.saved_ctrl2); > + if (!rc2 && attempted) > + rc2 =3D cxl_reset_enable_cache(pdev, snapshot); > + if (rc2) > + cxl_reset_save_disabled_state(pdev); > + } [Severity: Medium] Can cxl_reset_execute() race with concurrent cxl_port probe or unbind on PCI_DVSEC_CXL_CTRL and CXL_HDM_DECODER_CTRL_OFFSET? cxl_port probe and unbind hold only device_lock(&port->dev), never device_lock(&pdev->dev) or cxl_rwsem.region, and kernel PCI config accesses bypass pci_cfg_access_lock(). That allows an interleaving such as: CPU0 (cxl_reset_execute) CPU1 (cxl_port unbind) cxl_reset_hdm_restore() cxl_restore_dvsec_ctrl() write PCI_DVSEC_CXL_CTRL (CACHE_ENABLE=3D0) cxl_region_quiesce_unlock() clear_mem_enable() cxl_set_mem_enable(cxlds, 0) read PCI_DVSEC_CXL_CTRL (CACHE=3D0) write 0; cache dvsec_ctrl =3D 0 disable_hdm() clear CXL_HDM_DECODER_ENABLE cxl_reset_enable_cache() write snapshot->dvsec_ctrl <-- re-enables MEM_ENABLE with HDM off This caches the transient CACHE_ENABLE=3D0 value into pdev->hdm->dvsec_ctrl and then re-enables PCI_DVSEC_CXL_MEM_ENABLE in hardware while CXL_HDM_DECODER_ENABLE is cleared. In addition, during cxl_clear_memory() and cxl_reset_wait_done(), cxl_set_mem_enable(), disable_hdm(), and devm_cxl_enable_hdm() do not check regs_offline or reset_pending and can issue PCI config and HDM MMIO accesses while the device is in reset. > cxl_hdm_quiesce_destroy(&cache.quiesce); > - return rc ?: rc2; > + > + if (rc) > + return rc; > + if (rc2) > + return rc2; > + > + return cxl_reset_save_restored_state(pdev, command); [Severity: Medium] Can post-restore failures leave CXL.mem or CXL.cache enabled in hardware after cxl_reset_save_disabled_state() runs? cxl_reset_save_disabled_state() only clears PCI_COMMAND (MSE and BME), which gates CXL.io and PCIe DMA rather than CXL.mem or CXL.cache. If post-reset cxl_hdm_quiesce_flush() fails after cxl_reset_hdm_restore() succeeded, PCI_DVSEC_CXL_MEM_ENABLE and the committed HDM decoders remain active even though CPU cache invalidation failed and HPA reservations are released. In addition, cxl_reset_execute() restores cache policy, re-enables PCI_DVSEC_CXL_CACHE_ENABLE, and calls cxl_hdm_quiesce_destroy() before cxl_reset_save_restored_state(). If cxl_reset_save_restored_state() fails, both CXL.mem and CXL.cache remain enabled in hardware. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261205041453.3946= 529-1-smadhavan@nvidia.com?part=3D15