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=1796446047; b=YMCwm+YfNxbv+VOdFFOE4fdvVIKSmyft/V/I4rBpeugDw57Klor176sxkMNQfPHkPR75 V9zlIuajEXno2i/kgN3xfZ97gEwSt1LTLdsbX+HN6MGrT+I3MvnEDYSEgtQkvlaRVlJd5 lCDIJqrCiM7H1i14kto6fxno9HkqgRGAVqbNjXuv+phU+sSnZOTjMT2/sCeqtWJGnCnfw tEfPD8MnnuH7S952wszXMJETn4HSAkDvV6CeKXdVbB7eL9va8uLJnazkr3k3moDRUxklI +ZIA//9PbrCi519yOorxjPB5LABn1ZH2ftTmL3hPZW21obdwuZI48lik2UamPNv04Xw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796446047; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=MWi+2cl9kwRcLm+v4i2XrJgZ6c/xkSzzNZ/HtlZss+Q=; b=mY7YY6ic8zzBp5IfiAGFhsY21hAC1sznB9WfNNuUAFQM9gGelLH8dBQ1NQEkoXtznxtT 2aV4vASd5WcxX9qJ96gt3RVhHzr0syVO28imugPwkoahqQkkKMXZAOBh82EIKmyaYSozx X+pKh3T+UjDoSdbF/pdl+19K0RxenfvsbOs2yfmGMn7F4HG4XSxzxE1hUqvBAn1ekOQeX XumtRG1nDhdelFIUJo2gQWCOkaZwTZEl/oHCrKu2VEJcQb48D9a9TWM3QlyeMvDf9roUz ZDmXjjXnPH0fgNZdTJeRLmrBSUVn1mD1kA5LHlYAoUkZLsAOYninYbIAj7t6lN6w8BA== 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=Rnp+jF7X; 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 BDC2C606A322; Sat, 5 Dec 2026 04:47:27 +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=Rnp+jF7X Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4CF341F0092B; Sat, 5 Dec 2026 04:47:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796446047; bh=MWi+2cl9kwRcLm+v4i2XrJgZ6c/xkSzzNZ/HtlZss+Q=; h=From:Subject:Reply-To:To:In-Reply-To:References:Date; b=Rnp+jF7XNf3HW+SmTOKID2qc5P7LStfJI8c5Fv0UZv6TGvje/yObmZCp/HmCQixNN Yj1+9jjAbw+rACAOIOzliZX5CpcjHzD7gakadm2FkHGMUSPxflGsT6Dd8AmoemRG2p ohvb1rMPPpeSqUJG91UJk0IU25ymW0+wD81SWE6685yD2unRxP+hnP18vUuUM9oO6o b563vRlqnIT7Ti6EjlM6rQ3pqVOQGg7Vq9ghFiQ0GoHndUv8dM9N07yaO4Wrd8EOTf 7cnhxxlYZ7fpFtMAria/Tnh4ZrFGczadMLKsak7hXWEPV7gk9pJz79DYW2z8q9Czoh IgJ94+txI87+w== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v18 13/19] cxl: Validate and synchronize HDM ranges around 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-14-smadhavan@nvidia.com> References: <20261205041453.3946529-1-smadhavan@nvidia.com> <20261205041453.3946529-14-smadhavan@nvidia.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Sat, 05 Dec 2026 04:47:26 +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 9 potential issues to consider: - [High] cxl: normalized-addressing decoders undetected without CXL driver - [High] PCI/CXL: cxl_reset_hdm_prepare() checks stale cached dvsec_ctrl - [Medium] cxl: devm_cxl_enable_hdm() omits global_ctrl cache update - [Medium] cxl: TOCTOU on regs_offline in cxl_hdm_refresh_decoder() - [Medium] cxl: cxl_port probe/unbind races with CXL reset regs_offline - [Medium] PCI/CXL: regs_offline misses BAR-disabled window during CXL reset - [Medium] PCI/CXL: cxl_reset_hdm_unmark() clears reset_pending too early - [Medium] cxl: cxl_hdm_quiesce_prepare() rejects reset with zero enabled HDM ranges - [Low] PCI/CXL: cxl_reset_wait_cache_wbi() misses final check after timeout -- commit fbf810078bf8100238ebf8edce3e20d5244b645e Author: Srirangan Madhavan cxl: Validate and synchronize HDM ranges around reset This commit reserves enabled system-physical HDM ranges exclusively and invalidates CPU caches around CXL Reset, while using per-device reset_pending and regs_offline marks to coordinate with concurrent decoder commits and teardowns. > diff --git a/drivers/cxl/core/hdm.c b/drivers/cxl/core/hdm.c > index f884181775824..ebc9995eec1be 100644 > --- a/drivers/cxl/core/hdm.c > +++ b/drivers/cxl/core/hdm.c > @@ -764,10 +764,31 @@ static void cxl_hdm_refresh_decoder(struct cxl_hdm = *cxlhdm, > cxl_port_get_uport_pci_dev(cxlhdm->port); > if (!pdev) > return; > - cxl_hdm_cache_refresh_decoder(pdev, hdm, > + /* Do not read the device while it is in reset. */ > + cxl_hdm_cache_refresh_decoder(pdev, > + cxl_hdm_cache_is_regs_offline(pdev) ? > + NULL : hdm, > to_cxl_endpoint_decoder(&cxld->dev)); > } [Severity: Medium] Can this race with cxl_reset_regs_offline() when called from devm_cxl_enumerate_decoders()? Unlike cxl_decoder_commit() and cxl_decoder_reset(), devm_cxl_enumerate_decoders() does not hold cxl_rwsem.region or device_lock(&pdev->dev). Because cxl_hdm_cache_is_regs_offline() releases cxl_rwsem.dpa before cxl_hdm_cache_refresh_decoder() reacquires it, the reset path can start in between: CPU0 CPU1 devm_cxl_enumerate_decoders() cxl_hdm_refresh_decoder() cxl_hdm_cache_is_regs_offline() -> false cxl_reset_regs_offline() regs_offline =3D true cxl_clear_memory(..., true) cxl_hdm_cache_refresh_decoder() readl(hdm + CTRL_OFFSET) <-- MMIO read during reset This violates CXL r4.0 sec 9.7.3 and can overwrite cached global_ctrl with a reset value such as 0 whenever the device does not return U32_MAX. Checking !info->regs_offline inside cxl_hdm_cache_refresh_decoder() under cxl_rwsem.dpa would close this window. [ ... ] > @@ -911,14 +945,16 @@ static void cxl_decoder_reset(struct cxl_decoder *c= xld) > "%s: out of order reset, expected decoder%d.%d\n", > dev_name(&cxld->dev), port->id, port->commit_end); > =20 > - ctrl =3D readl(hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id)); > - ctrl &=3D ~CXL_HDM_DECODER0_CTRL_COMMIT; > - writel(ctrl, hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id)); > + if (!cxl_decoder_regs_offline(cxld)) { > + ctrl =3D readl(hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id)); > + ctrl &=3D ~CXL_HDM_DECODER0_CTRL_COMMIT; > + writel(ctrl, hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id)); > =20 > - writel(0, hdm + CXL_HDM_DECODER0_SIZE_HIGH_OFFSET(id)); > - writel(0, hdm + CXL_HDM_DECODER0_SIZE_LOW_OFFSET(id)); > - writel(0, hdm + CXL_HDM_DECODER0_BASE_HIGH_OFFSET(id)); > - writel(0, hdm + CXL_HDM_DECODER0_BASE_LOW_OFFSET(id)); > + writel(0, hdm + CXL_HDM_DECODER0_SIZE_HIGH_OFFSET(id)); > + writel(0, hdm + CXL_HDM_DECODER0_SIZE_LOW_OFFSET(id)); > + writel(0, hdm + CXL_HDM_DECODER0_BASE_HIGH_OFFSET(id)); > + writel(0, hdm + CXL_HDM_DECODER0_BASE_LOW_OFFSET(id)); > + } [Severity: Medium] Does regs_offline also need to guard cxl_endpoint_port_probe() and cxl_port unbind while CXL Reset is in progress? PCI reset holds device_lock(&pdev->dev), but devm_cxl_add_endpoint() parents the endpoint cxl_port under the upstream port host rather than &pdev->dev, and neither probe nor devres teardown holds cxl_rwsem.region. Because cxl_rwsem.region is dropped across the up-to-100-second reset wait, a concurrent probe or unbind can access the resetting device: CPU0 CPU1 __cxl_reset_execute() cxl_reset_regs_offline() cxl_clear_memory(..., true) cxl_endpoint_port_probe() devm_cxl_enumerate_decoders() init_hdm_decoder() <-- reads 0 cxl_hdm_refresh_decoder() wipes pdev->hdm->settings[] On probe, this issues prohibited config and MMIO accesses during the reset quiet period and wipes cached decoder settings that post-reset restoration depends on. On unbind, clear_mem_enable() and disable_hdm() similarly access DVSEC config space and HDM MMIO without checking regs_offline, and disable_hdm() can write U32_MAX & ~CXL_HDM_DECODER_ENABLE to hardware while skipping the cache update. > diff --git a/drivers/cxl/core/hdm_state.c b/drivers/cxl/core/hdm_state.c > index ddb69411de1c7..497709bffff2b 100644 > --- a/drivers/cxl/core/hdm_state.c > +++ b/drivers/cxl/core/hdm_state.c [ ... ] > +bool cxl_hdm_snapshot_is_dvsec_decoded(const struct cxl_hdm_info *info) > +{ > + return (info->dvsec_ctrl & PCI_DVSEC_CXL_MEM_ENABLE) && > + !(info->global_ctrl & CXL_HDM_DECODER_ENABLE); > +} [Severity: Medium] Can this falsely classify an endpoint as DVSEC-decoded because devm_cxl_enable_hdm() does not update cached global_ctrl? In drivers/cxl/core/pci.c, devm_cxl_enable_hdm() sets CXL_HDM_DECODER_ENABLE in hardware without calling cxl_hdm_cache_set_global_ctrl(), unlike disable_hdm() right above it. In cxl_hdm_decode_init(), devm_cxl_enable_mem() runs immediately afterwards and updates pdev->hdm->dvsec_ctrl with PCI_DVSEC_CXL_MEM_ENABLE, before devm_cxl_enumerate_decoders() sleeps for 20 ms in cxl_settle_decoders(). During that window, a snapshot has MEM_ENABLE set and HDM_DECODER_ENABLE clear, causing cxl_reset_hdm_prepare() to refuse reset with -EOPNOTSUPP. [ ... ] > +static int cxl_hdm_ranges_collect(struct cxl_hdm_quiesce *q, > + const struct cxl_hdm_info *snapshot) > +{ > + int rc; > + > + for (int i =3D 0; i < snapshot->decoder_count; i++) { > + const struct cxl_decoder_config *config =3D > + &snapshot->settings[i].config; > + > + /* A committed zero-size decoder maps no HPA. */ > + if (!(config->flags & CXL_DECODER_F_ENABLE) || > + !range_len(&config->hpa_range)) > + continue; > + > + if (config->flags & CXL_DECODER_F_NORMALIZED_ADDRESSING) > + return -EOPNOTSUPP; > + > + rc =3D cxl_hdm_range_add(q, &config->hpa_range); > + if (rc) > + return rc; > + } > + > + return 0; > +} [Severity: High] As reported by Alison Schofield on v17 [1], what prevents a reset from using untranslated normalized-addressing ranges when no CXL driver is bound or before cxl_add_to_region() completes? Hardware HDM decoder registers do not indicate Normalized Addressing, and CXL_DECODER_F_NORMALIZED_ADDRESSING is only recorded into pdev->hdm by cxl_add_to_region() after PRM translation setup runs. When a device is bound to vfio-pci or reset before region discovery finishes, that flag is absent and cxl_hdm_ranges_request() calls request_mem_region() directly without checking that the range lies within a platform CXL window. If the untranslated address happens to be unclaimed in iomem_resource, the reset proceeds without reserving or invalidating the true CXL SPA range, risking data corruption or machine checks on active CXL memory. [ ... ] > +int cxl_hdm_quiesce_prepare(struct cxl_hdm_quiesce *q, > + const struct cxl_hdm_info *snapshot) > +{ > + int rc; > + > + lockdep_assert_held_write(&cxl_rwsem.region); > + INIT_LIST_HEAD(&q->ranges); > + > + if (!cpu_cache_has_invalidate_memregion() && > + !IS_ENABLED(CONFIG_CXL_REGION_INVALIDATION_TEST)) > + return -ENXIO; > + > + rc =3D cxl_hdm_ranges_collect(q, snapshot); > + if (rc) > + return rc; > + > + return cxl_hdm_ranges_request(q); > +} [Severity: Medium] Should this check cpu_cache_has_invalidate_memregion() only after cxl_hdm_ranges_collect() finds at least one active range? When all HDM decoders are disabled or zero-sized, q->ranges is empty and cxl_hdm_ranges_invalidate() performs no cache invalidation calls anyway. Returning -ENXIO upfront rejects reset on x86 VMs and architectures without a cache invalidation provider even when no HPA range is decoded, and because -ENXIO is not -ENOTTY, __pci_reset_function_locked() aborts without trying fallback reset methods. > diff --git a/drivers/pci/cxl.c b/drivers/pci/cxl.c > index 4d6cfabab2c0e..c90715c5a6d52 100644 > --- a/drivers/pci/cxl.c > +++ b/drivers/pci/cxl.c [ ... ] > + /* Judge the snapshot: the live cache can change after it was taken. */ > + if (cxl_hdm_snapshot_is_dvsec_decoded(snapshot)) { > + rc =3D -EOPNOTSUPP; > + goto free_snapshot; > + } > + /* > + * With CXL.cache enabled the device cache may hold dirty lines that on= ly > + * Cache Writeback and Invalidate can push back to memory. Reset would > + * discard them, so refuse when the device cannot do it. > + */ > + if ((snapshot->dvsec_ctrl & PCI_DVSEC_CXL_CACHE_ENABLE) && > + !(cap & PCI_DVSEC_CXL_CACHE_WBI_CAPABLE)) { > + pci_err(pdev, "CXL reset refused: CXL.cache is enabled but the device = cannot write back and invalidate its cache\n"); > + rc =3D -EOPNOTSUPP; > + goto free_snapshot; > + } [Severity: High] Can snapshot->dvsec_ctrl be stale here compared to the live PCI_DVSEC_CXL_CTRL register? pdev->hdm->dvsec_ctrl is populated once at PCI enumeration and is only updated by cxl_set_mem_enable(), never when CXL.cache is enabled or when a VFIO user writes PCI_DVSEC_CXL_CTRL directly. If PCI_DVSEC_CXL_CACHE_ENABLE was clear at enumeration and enabled later on a device without PCI_DVSEC_CXL_CACHE_WBI_CAPABLE, cxl_reset_function() reads live hardware and enters cxl_reset_execute(), while this check sees 0 in snapshot->dvsec_ctrl and allows CXL Reset to proceed without WBI, discarding dirty device-cache lines. Similarly, CXL.mem enabled outside cxl_set_mem_enable() with HDM globally disabled can bypass the DVSEC-decoded check. Reading live PCI_DVSEC_CXL_CTRL here and checking it alongside snapshot->dvsec_ctrl would close both gaps. > + > + rc =3D cxl_hdm_quiesce_prepare(&cache->quiesce, snapshot); > + if (rc) { > + pci_err(pdev, "failed to prepare CXL reset ranges: %d\n", rc); > + cxl_hdm_quiesce_destroy(&cache->quiesce); > + goto free_snapshot; > + } > + > + /* From here until restore, no decoder of this device can commit. */ > + cxl_hdm_cache_set_reset_pending(pdev, true); > + cxl_region_quiesce_unlock(); > + return snapshot; [Severity: Medium] Aren't the device's HDM BAR registers already offline at this point, before cxl_reset_regs_offline() is called? Standard PCI reset paths call pci_dev_save_and_disable() before cxl_reset_function(), which clears PCI_COMMAND_MEMORY in PCI_COMMAND until pci_dev_restore() runs. Because cxl_reset_hdm_prepare() drops cxl_rwsem.region with regs_offline still false, a concurrent region teardown can run before cxl_reset_regs_offline(): CPU0 CPU1 pci_dev_save_and_disable() PCI_COMMAND_MEMORY =3D 0 cxl_reset_hdm_prepare() reset_pending =3D true cxl_region_quiesce_unlock() cxl_decoder_reset() cxl_decoder_regs_offline() -> false readl/writel(hdm + ...) <-- BAR off clears ENABLE & cache settings cxl_reset_cache_prepare() -> fails The MMIO accesses hit a disabled BAR (returning 0xFFFFFFFF and dropping the uncommit writes), and if preparation then fails before CXL Reset starts, the hardware decoder remains committed while software and pdev->hdm record it as disabled. The same gap exists after cxl_reset_hdm_unmark() on error paths before pci_dev_restore() re-enables PCI_COMMAND_MEMORY. [ ... ] > +/* Shared host invalidation and device cache writeback, without the regi= on lock. */ > +static int cxl_reset_cache_prepare(struct pci_dev *pdev, int dvsec, u16 = cap, > + struct cxl_pci_cache *cache) > +{ > + int rc; > + > + rc =3D cxl_hdm_quiesce_flush(&cache->quiesce); > + if (rc) > + return rc; > + > + /* Track an attempted change so partial failures still restore policy. = */ > + cache->cache_disabled =3D true; > + return cxl_reset_disable_cache(pdev, dvsec, cap); > +} [Severity: Low] Can cxl_reset_wait_cache_wbi(), called from cxl_reset_disable_cache(), return a spurious -ETIMEDOUT after hardware has already finished WBI? drivers/pci/cxl.c:cxl_reset_wait_cache_wbi() { ... deadline =3D jiffies + usecs_to_jiffies(CXL_CACHE_WBI_TIMEOUT_US); while (time_before(jiffies, deadline)) { usleep_range(CXL_CACHE_WBI_POLL_US, CXL_CACHE_WBI_POLL_US * 2); ... } pci_warn(pdev, "timed out waiting for CXL cache writeback and invalidate\n= "); return -ETIMEDOUT; } If the task is preempted past deadline before the loop or after the last sleep, it exits without a final read of PCI_DVSEC_CXL_STATUS2 to check PCI_DVSEC_CXL_CACHE_INV, unlike cxl_reset_wait_done(). [ ... ] > + 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); > + > pci_dev_reset_iommu_done(pdev); > return rc; > } > =20 > +/* > + * Three short region-lock windows surround the reset: cxl_reset_hdm_pre= pare() > + * captures the state and reserves the ranges, cxl_reset_regs_offline() = keeps > + * region teardown off the registers just before the reset starts, and > + * cxl_reset_hdm_unmark() lets decoders commit again after the host flus= h. > + * The wait for Memory Clear, up to 100 s, and the flush run without the= lock. > + */ > static int cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap) > { > - u16 saved_ctrl2; > + struct cxl_pci_cache cache =3D {}; > int rc, rc2; > =20 > - rc =3D cxl_pci_read_config_word(pdev, dvsec + PCI_DVSEC_CXL_CTRL2, > - &saved_ctrl2); > - if (rc) > - return rc; > - > - rc =3D cxl_reset_disable_cache(pdev, dvsec, cap); > - if (!rc) > - rc =3D __cxl_reset_execute(pdev, dvsec, cap); > + struct cxl_hdm_info *snapshot __free(kfree) =3D > + cxl_reset_hdm_prepare(pdev, dvsec, cap, &cache); > + if (IS_ERR(snapshot)) > + return PTR_ERR(snapshot); > + > + rc =3D cxl_reset_cache_prepare(pdev, dvsec, cap, &cache); > + if (!rc) { > + rc =3D __cxl_reset_execute(pdev, dvsec, cap, &cache.quiesce); > + } else { > + /* Nothing was reset: clear the mark and drop the reservation. */ > + cxl_reset_hdm_unmark(pdev); > + if (!cache.cache_disabled) { > + pci_err(pdev, "failed to prepare CXL reset ranges: %d\n", rc); > + cxl_hdm_quiesce_destroy(&cache.quiesce); > + return rc; > + } > + } > /* Restore cache policy after any attempt to disable caching. */ > - rc2 =3D cxl_reset_restore_cache_policy(pdev, dvsec, saved_ctrl2); > + rc2 =3D cxl_reset_restore_cache_policy(pdev, dvsec, cache.saved_ctrl2); > + cxl_hdm_quiesce_destroy(&cache.quiesce); > return rc ?: rc2; > } [Severity: Medium] Does cxl_reset_hdm_unmark() unblock concurrent decoder commits too early? On both normal completion and error paths, cxl_reset_hdm_unmark() clears reset_pending and releases cxl_rwsem.region before pci_dev_reset_iommu_done(), cxl_reset_restore_cache_policy(), and cxl_hdm_quiesce_destroy() have finished. A concurrent commit_store() can commit an endpoint decoder while IOMMU exclusion and DISABLE_CACHING are still active and while the exclusive cxl_reset iomem_resource reservations are still held, causing the region's consumer driver probe to fail with -EBUSY. Moving cxl_reset_hdm_unmark() after those cleanup steps complete would avoid the race. [1] https://lore.kernel.org/r/avyPsvwV1_lc9va5@aschofie-mobl2.lan --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261205041453.3946= 529-1-smadhavan@nvidia.com?part=3D13