All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Srirangan Madhavan" <smadhavan@nvidia.com>,
	linux-cxl@vger.kernel.org, linux-pci@vger.kernel.org
Subject: Re: [PATCH v18 11/19] PCI/CXL: Cache endpoint HDM state during PCI enumeration
Date: Sat, 05 Dec 2026 04:44:08 +0000	[thread overview]
Message-ID: <sashiko-outbox-251096@kernel.org> (raw)
In-Reply-To: <20261205041453.3946529-12-smadhavan@nvidia.com>

Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [High] PCI/CXL: pci_cxl_hdm_cache_init() runs before x86 BAR survey
  and assignment
- [High] PCI/CXL: cxl_pci_hdm_read_info() skips upstream bridge memory
  enablement
- [High] PCI/CXL: HDM cache enables reset before Normalized Addressing
  discovery
- [Medium] PCI/CXL: out-of-bounds MMIO read in cxl_pci_hdm_map_regs()
- [Medium] cxl: devm_cxl_enable_hdm() omits
  cxl_hdm_cache_set_global_ctrl() update
- [Medium] cxl/pci: disable_hdm() leaves stale HDM enable in cache on
  U32_MAX read
- [Medium] PCI/CXL: __pci_cxl_hdm_cache_init() excludes CXL.mem-only
  devices
--

commit cd94cb85d3b6b7e41ad48b2c419445f63d976837
Author: Srirangan Madhavan <smadhavan@nvidia.com>

PCI/CXL: Cache endpoint HDM state during PCI enumeration

This commit caches endpoint HDM decoder and DVSEC state in pci_dev->hdm
during PCI enumeration and updates the cache when CXL enables or disables
memory and HDM decode.

> diff --git a/drivers/cxl/core/pci.c b/drivers/cxl/core/pci.c
> index d0da38763b4bc..a9d33357d095a 100644
> --- a/drivers/cxl/core/pci.c
> +++ b/drivers/cxl/core/pci.c

[ ... ]

> @@ -266,13 +275,20 @@ static int dvsec_range_allowed(struct device *dev, const void *arg)
>  
>  static void disable_hdm(void *_cxlhdm)
>  {
> -	u32 global_ctrl;
>  	struct cxl_hdm *cxlhdm = _cxlhdm;
>  	void __iomem *hdm = cxlhdm->regs.hdm_decoder;
> +	struct pci_dev *pdev __free(pci_dev_put) =
> +		cxl_port_get_uport_pci_dev(cxlhdm->port);
> +	u32 global_ctrl;
>  
>  	global_ctrl = readl(hdm + CXL_HDM_DECODER_CTRL_OFFSET);
>  	writel(global_ctrl & ~CXL_HDM_DECODER_ENABLE,
>  	       hdm + CXL_HDM_DECODER_CTRL_OFFSET);
> +
> +	/* Keep the reset cache up to date. */
> +	if (pdev && global_ctrl != U32_MAX)
> +		cxl_hdm_cache_set_global_ctrl(pdev,
> +					      global_ctrl & ~CXL_HDM_DECODER_ENABLE);

[Severity: Medium]
Does skipping cxl_hdm_cache_set_global_ctrl() when readl() returns U32_MAX
leave a stale CXL_HDM_DECODER_ENABLE bit in pdev->hdm->global_ctrl?

When cxl_error_detected() unbinds the memdev on a frozen PCIe channel,
disable_hdm() runs while MMIO reads return U32_MAX.  Leaving the enable bit
set in the cache causes a later reset to restore HDM global enable on
hardware even though the driver unbound and disabled it.  Clearing
CXL_HDM_DECODER_ENABLE in the cached global_ctrl on a U32_MAX read would
keep the cache consistent.

>  }
>  
>  static int devm_cxl_enable_hdm(struct device *host, struct cxl_hdm *cxlhdm)

[Severity: Medium]
Should devm_cxl_enable_hdm() also call cxl_hdm_cache_set_global_ctrl()
after enabling CXL_HDM_DECODER_ENABLE?

The kerneldoc for cxl_hdm_cache_set_global_ctrl() notes that it tracks
enabling HDM on bind as well as disabling it on unbind, but
devm_cxl_enable_hdm() does not update pdev->hdm->global_ctrl.  During
cxl_endpoint_port_probe(), devm_cxl_enable_mem() updates cached dvsec_ctrl
with PCI_DVSEC_CXL_MEM_ENABLE set, and devm_cxl_enumerate_decoders() then
sleeps 20 ms in cxl_settle_decoders() before refreshing global_ctrl:

CPU0                                CPU1
cxl_endpoint_port_probe()
  devm_cxl_enable_hdm()
    writel(... | ENABLE)
  devm_cxl_enable_mem()
    cache dvsec_ctrl (MEM_ENABLE=1)
  cxl_settle_decoders()
    msleep(20)
                                    pci_reset_function()
                                      cxl_reset_hdm_prepare()
                                        snapshot sees MEM_ENABLE=1,
                                        HDM_ENABLE=0  <-- -EOPNOTSUPP

A concurrent reset during that window either fails with -EOPNOTSUPP or, if
it lands before devm_cxl_enable_mem(), restores CXL_HDM_DECODER_ENABLE
cleared to hardware.

[ ... ]

> diff --git a/drivers/pci/bus.c b/drivers/pci/bus.c
> index 655ed53436d3e..aad567fb7c560 100644
> --- a/drivers/pci/bus.c
> +++ b/drivers/pci/bus.c
> @@ -359,6 +359,7 @@ void pci_bus_add_device(struct pci_dev *dev)
>  
>  	/* Save config space for error recoverability */
>  	pci_save_state(dev);
> +	pci_cxl_hdm_cache_init(dev);

[Severity: High]
As reported by Richard Cheng on v8 [1], can pci_cxl_hdm_cache_init() run
safely here before x86 BAR resource survey and assignment?

During x86 ACPI boot, acpi_pci_root_add() calls pci_bus_add_devices()
during subsys_initcall(acpi_init), before pcibios_resource_survey() and
fs_initcall(pcibios_assign_resources).  If firmware left the component BAR
unassigned or requiring relocation, cxl_pci_bar_assigned() fails and cache
initialization is never retried, leaving pdev->hdm permanently NULL and
breaking post-reset HDM restoration.

In addition, cxl_pci_bar_assigned() does not check res->parent, so a
conflicting firmware BAR that has not yet been surveyed can be temporarily
enabled with PCI_COMMAND_MEMORY and ioremapped before resource survey
clears the conflict.

> diff --git a/drivers/pci/cxl.c b/drivers/pci/cxl.c
> new file mode 100644
> index 0000000000000..56119e3fd03a6
> --- /dev/null
> +++ b/drivers/pci/cxl.c

[ ... ]

> +static void __iomem *cxl_pci_hdm_map_regs(struct pci_dev *pdev,
> +					  struct cxl_register_map *map,
> +					  resource_size_t *hdm_offset,
> +					  int *decoder_count)
> +{
> +	struct cxl_reg_map *hdm_map = &map->component_map.hdm_decoder;
> +	void __iomem *hdm;
> +	int rc;
> +
> +	/* cxl_setup_regs() populates hdm_map, including its valid bit. */
> +	rc = cxl_setup_regs(map);
> +	if (rc)
> +		return ERR_PTR(rc);
> +	if (!hdm_map->valid)
> +		return ERR_PTR(-ENODEV);
> +	if (hdm_map->offset > map->max_size ||
> +	    hdm_map->size > map->max_size - hdm_map->offset)
> +		return ERR_PTR(-ENXIO);

[Severity: Medium]
Can cxl_setup_regs() read past the end of the ioremap() window if
map->max_size is smaller than the component register block?

cxl_decode_regblock() allows map->max_size down to 0, and
cxl_map_regblock() maps only map->max_size bytes.  Before the bounds check
here runs, cxl_probe_component_regs() unconditionally advances base by
CXL_CM_OFFSET (0x1000) and reads the capability header and capability
offsets without checking map->max_size, which will fault on an unmapped
vmalloc guard page for a non-conforming device with a small BAR.

[ ... ]

> +	if (!(command & PCI_COMMAND_MEMORY)) {
> +		rc = pci_write_config_word(pdev, PCI_COMMAND,
> +					   command | PCI_COMMAND_MEMORY);
> +		if (rc)
> +			return ERR_PTR(pcibios_err_to_errno(rc));
> +	}
> +
> +	info = __cxl_pci_hdm_read_info(pdev, map, dvsec);

[Severity: High]
Does setting PCI_COMMAND_MEMORY only on pdev work when upstream PCIe
bridges do not yet have memory decoding enabled?

During boot enumeration in subsys_initcall(acpi_init), pcieport has not
bound yet (pcie_portdrv_init() is a device_initcall) and
__pci_setup_bridge() does not set PCI_COMMAND_MEMORY on bridges.  If any
upstream Root Port or Switch Port has memory forwarding disabled, the MMIO
reads in __cxl_pci_hdm_read_info() receive Unsupported Request responses
(0xffffffff) and cache initialization fails silently, leaving pdev->hdm
permanently NULL.

[ ... ]

> +	if (!(cap & PCI_DVSEC_CXL_CACHE_CAPABLE) ||
> +	    !(cap & PCI_DVSEC_CXL_MEM_CAPABLE) ||
> +	    !(cap & PCI_DVSEC_CXL_RST_CAPABLE) ||
> +	    !(cap & PCI_DVSEC_CXL_RST_MEM_CLR_CAPABLE) ||
> +	    !cxl_dvsec_hdm_count_valid(FIELD_GET(PCI_DVSEC_CXL_HDM_COUNT, cap)))
> +		return 0;

[Severity: Medium]
As reported by Jonathan Cameron on v12 [2], should this require
PCI_DVSEC_CXL_CACHE_CAPABLE?

Gating cache initialization on PCI_DVSEC_CXL_CACHE_CAPABLE excludes
CXL.mem-only endpoints (such as non-class-code-matching Type 3 devices)
from ever populating pdev->hdm, so they miss HDM range synchronization and
post-reset HDM restoration on CXL bus reset even though the reset helpers
in commit "PCI/CXL: Flush host and device caches for CXL bus reset" check
PCI_DVSEC_CXL_CACHE_CAPABLE dynamically.

[ ... ]

> +	read_info = cxl_pci_hdm_read_info(pdev, &map, dvsec);
> +	if (IS_ERR(read_info))
> +		return PTR_ERR(read_info);
> +
> +	pdev->hdm = read_info;

[Severity: High]
As reported by Alison Schofield on v17 [3], does publishing pdev->hdm here
allow CXL reset to run before Normalized Addressing has been discovered?

Normalized Addressing cannot be determined from endpoint HDM registers
alone; CXL_DECODER_F_NORMALIZED_ADDRESSING is only recorded later during
cxl_add_to_region() when CXL endpoint discovery runs.  Without a gate
checking whether discovery has verified address translation, resetting a
device without a bound CXL driver (or before region discovery completes)
causes request_mem_region() and cpu_cache_invalidate_memregion() to operate
on raw normalized endpoint addresses rather than the translated System
Physical Addresses, which can bypass region exclusivity and wipe actively
used CXL memory or flush the wrong host cache range.

[1] https://lore.kernel.org/r/akyNQjte2TmeLhE8@MWDK4CY14F
[2] https://lore.kernel.org/r/20260912022617.47860862@jic23-hlaptop
[3] https://lore.kernel.org/r/avyPB4YCEKUDtT19@aschofie-mobl2.lan

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205041453.3946529-1-smadhavan@nvidia.com?part=11

  reply	other threads:[~2026-12-05  4:44 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05  4:14 [PATCH v18 00/19] PCI/CXL: Add CXL reset support for Type 2 devices Srirangan Madhavan
2026-12-05  4:14 ` [PATCH v18 01/19] cxl: Clarify decoder interleave limit Srirangan Madhavan
2026-12-05  4:14 ` [PATCH v18 02/19] cxl: Share CXL port upstream PCI device lookup Srirangan Madhavan
2026-12-05  4:14 ` [PATCH v18 03/19] cxl: Move decoder declarations to shared header Srirangan Madhavan
2026-12-05  4:14 ` [PATCH v18 04/19] cxl: Embed decoder configuration in a standalone structure Srirangan Madhavan
2026-12-05  4:14 ` [PATCH v18 05/19] cxl: Introduce endpoint HDM decoder settings Srirangan Madhavan
2026-12-05  4:30   ` sashiko-bot
2026-12-05  4:14 ` [PATCH v18 06/19] cxl: Move HDM decoder helpers to built-in code Srirangan Madhavan
2026-12-05  4:14 ` [PATCH v18 07/19] cxl: Share HDM decoder register unpacking Srirangan Madhavan
2026-12-05  4:30   ` sashiko-bot
2026-12-05  4:14 ` [PATCH v18 08/19] cxl: Move cxl_rwsem into built-in code Srirangan Madhavan
2026-12-05  4:14 ` [PATCH v18 09/19] cxl: Refresh cached PCI HDM decoder settings Srirangan Madhavan
2026-12-05  4:38   ` sashiko-bot
2026-12-05  4:14 ` [PATCH v18 10/19] cxl: Build register block helpers into the kernel Srirangan Madhavan
2026-12-05  4:14 ` [PATCH v18 11/19] PCI/CXL: Cache endpoint HDM state during PCI enumeration Srirangan Madhavan
2026-12-05  4:44   ` sashiko-bot [this message]
2026-12-05  4:14 ` [PATCH v18 12/19] PCI/CXL: Add CXL Device Reset sequencing Srirangan Madhavan
2026-12-05  4:38   ` sashiko-bot
2026-12-05  4:14 ` [PATCH v18 13/19] cxl: Validate and synchronize HDM ranges around reset Srirangan Madhavan
2026-12-05  4:47   ` sashiko-bot
2026-12-05  4:14 ` [PATCH v18 14/19] PCI/CXL: Reject reset with unsafe function scope Srirangan Madhavan
2026-12-05  4:43   ` sashiko-bot
2026-12-05  4:14 ` [PATCH v18 15/19] PCI/CXL: Restore CXL state after PCI reset Srirangan Madhavan
2026-12-05  4:50   ` sashiko-bot
2026-12-05  4:14 ` [PATCH v18 16/19] PCI/CXL: Expose CXL Reset as a PCI reset method Srirangan Madhavan
2026-12-05  4:48   ` sashiko-bot
2026-12-05  4:14 ` [PATCH v18 17/19] PCI/CXL: Restore CXL state after CXL bus reset Srirangan Madhavan
2026-12-05  4:47   ` sashiko-bot
2026-12-05  4:14 ` [PATCH v18 18/19] PCI/CXL: Flush host and device caches for " Srirangan Madhavan
2026-12-05  4:54   ` sashiko-bot
2026-12-05  4:14 ` [PATCH v18 19/19] cxl: Add KUnit checks for HDM decoder unpacking Srirangan Madhavan
2026-12-05  4:38   ` sashiko-bot
2026-12-05  4:54 ` [PATCH v18 00/19] PCI/CXL: Add CXL reset support for Type 2 devices sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-251096@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-cxl@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=smadhavan@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.