From: sashiko-bot@kernel.org
To: "Manivannan Sadhasivam" <manivannan.sadhasivam@oss.qualcomm.com>
Cc: linux-pci@vger.kernel.org, devicetree@vger.kernel.org,
robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH 2/2] PCI: endpoint: Add generic PCI Endpoint Controller driver
Date: Fri, 04 Dec 2026 14:43:37 +0000 [thread overview]
Message-ID: <sashiko-outbox-250004@kernel.org> (raw)
In-Reply-To: <20261204-pci-ep-generic-v1-2-30c0e44e170f@oss.qualcomm.com>
Thank you for your contribution! Sashiko AI review found 12 potential
issues to consider:
- [High] PCI: endpoint: deadlock calling pci_epc_linkup() under
epc->lock in start
- [High] PCI: endpoint: broken multi-slot mapping and offsets in
map_msi_irq()
- [High] PCI: endpoint: advertised subrange_mapping ignored in set_bar()
- [Medium] PCI: endpoint: unvalidated max_functions and ctrl size in
probe()
- [Medium] PCI: endpoint: relaxed CMD writes omit write barrier in start
and stop
- [Medium] PCI: endpoint: hardcoded 32-bit DMA mask despite 64-bit
address registers
- [Medium] PCI: endpoint: pci_ep_generic_remove() omits stopping the
endpoint link
- [Medium] PCI: endpoint: premature pci_epc_mem_exit() UAF in
pci_ep_generic_remove()
- [Medium] PCI: endpoint: EPC exposed before epc_set_drvdata() in
probe()
- [Low] PCI: endpoint: raw Linux PCI_IRQ flags written unvalidated in
raise_irq()
- [Low] PCI: endpoint: driver includes internal linux/dma-map-ops.h
header
- [Low] PCI: endpoint: include linux/dma-mapping.h instead of internal
dma-map-ops.h
--
commit a1bc1c2d472582e2deeb1171bc1e667a9ff9e083
Author: Manivannan Sadhasivam <manivannan.sadhasivam@oss.qualcomm.com>
PCI: endpoint: Add generic PCI Endpoint Controller driver
This commit adds a platform driver for a generic firmware-initialised PCI
Endpoint Controller using a simple MMIO register interface and an outbound
memory window.
> diff --git a/drivers/pci/controller/pci-ep-generic.c b/drivers/pci/controller/pci-ep-generic.c
> new file mode 100644
> index 0000000000000..8ebed7cb8370b
> --- /dev/null
> +++ b/drivers/pci/controller/pci-ep-generic.c
> @@ -0,0 +1,387 @@
[ ... ]
> +#include <linux/dma-map-ops.h>
[Severity: Low]
Should this include linux/dma-mapping.h instead of linux/dma-map-ops.h?
include/linux/dma-map-ops.h is reserved for dma_map_ops implementations and
states that drivers just using the DMA API should not include it.
[ ... ]
> +static int pci_ep_generic_raise_irq(struct pci_epc *epc, u8 fn, u8 vfn,
> + unsigned int type, u16 irq_num)
> +{
> + struct pci_ep_generic *epc_priv = epc_get_drvdata(epc);
> +
> + pci_ep_generic_write(epc_priv, fn, type, PCI_EPC_GEN_IRQ_TYPE);
> + pci_ep_generic_write(epc_priv, fn, irq_num, PCI_EPC_GEN_IRQ_NUM);
> + pci_ep_generic_write_cmd(epc_priv, fn, PCI_EPC_GEN_CMD_RAISE_IRQ);
> +
> + return 0;
> +}
[Severity: Low]
Should pci_ep_generic_raise_irq() translate type into controller-defined
constants and validate it against the controller's capabilities instead of
writing raw Linux PCI_IRQ_* flags directly to PCI_EPC_GEN_IRQ_TYPE? This
ties the MMIO register ABI to internal kernel allocation bitmasks and
silently returns 0 instead of -EINVAL when an unsupported or invalid IRQ
type is requested.
> +static int pci_ep_generic_map_msi_irq(struct pci_epc *epc, u8 fn, u8 vfn,
> + phys_addr_t phys_addr, u8 irq_num,
> + u32 entry_size, u32 *msi_data,
> + u32 *msi_addr_offset)
> +{
> + struct pci_ep_generic *epc_priv = epc_get_drvdata(epc);
> + u64 msi_addr, aligned;
> +
> + msi_addr = ((u64)pci_ep_generic_read(epc_priv, fn, PCI_EPC_GEN_MSI_ADDR_HI) << 32 |
> + pci_ep_generic_read(epc_priv, fn, PCI_EPC_GEN_MSI_ADDR_LO));
> +
> + aligned = ALIGN_DOWN(msi_addr, entry_size);
> + *msi_addr_offset = (msi_addr - aligned) + (u32)irq_num * entry_size;
> + *msi_data = pci_ep_generic_read(epc_priv, fn, PCI_EPC_GEN_MSI_DATA) + irq_num;
> +
> + return pci_ep_generic_map_addr(epc, fn, vfn, phys_addr, aligned, entry_size);
> +}
[Severity: High]
Does pci_ep_generic_map_msi_irq() break the pci_epc_map_msi_irq() contract
for multi-doorbell mapping?
Its caller epf_ntb_configure_msi() passes the doorbell count as irq_num,
sets peer_ctrl->db_data[i] = db_data | i, and the host driver
ntb_epf_peer_db_set() writes to ndev->db_reg + (db_entry_size *
interrupt_num) + db_offset.
Here only a single entry_size window is mapped instead of looping irq_num
times across consecutive entry_size slots (compare
cdns_pcie_ep_map_msi_irq()), leaving slots 1..irq_num-1 unmapped. Adding
(u32)irq_num * entry_size to *msi_addr_offset also pushes every host
doorbell write past the mapped doorbell window, and adding irq_num to
*msi_data instead of masking the lower vector bits corrupts the MSI vector
numbers.
It also does not check PCI_EPC_GEN_MSI to confirm MSI is enabled with
1 <= irq_num <= msi_count, nor that entry_size is a non-zero power of two
before ALIGN_DOWN().
> +static int pci_ep_generic_start(struct pci_epc *epc)
> +{
> + struct pci_ep_generic *epc_priv = epc_get_drvdata(epc);
> + u32 linkup;
> + int ret;
> +
> + writel_relaxed(PCI_EPC_GEN_CMD_START, epc_priv->base + PCI_EPC_GEN_CMD);
> +
> + ret = readl_relaxed_poll_timeout(epc_priv->base + PCI_EPC_GEN_LINKUP,
> + linkup, linkup, 1000, 1000000);
> + if (ret) {
> + writel_relaxed(PCI_EPC_GEN_CMD_STOP, epc_priv->base + PCI_EPC_GEN_CMD);
> + return ret;
> + }
> +
> + pci_epc_linkup(epc);
> +
> + return 0;
> +}
[Severity: High]
Can calling pci_epc_linkup() from pci_ep_generic_start() cause an ABBA
deadlock?
pci_epc_start() holds epc->lock across epc->ops->start(), while
pci_epc_linkup() acquires epc->list_lock and then epf->lock. The rest of
the PCI endpoint subsystem acquires these locks in the reverse order:
CPU0 CPU1
---- ----
lock(&epc->lock)
lock(&epc->list_lock)
lock(&epf->lock)
lock(&epc->list_lock)
lock(&epc->lock)
*** DEADLOCK ***
CPU0: pci_epc_start() -> pci_ep_generic_start() -> pci_epc_linkup()
CPU1: pci_ep_generic_remove() -> pci_epc_deinit_notify() ->
pci_epf_test_epc_deinit() -> pci_epc_clear_bar()
Any EPF link_up() callback that calls a pci_epc_*() helper will also
self-deadlock on epc->lock, and polling PCI_EPC_GEN_LINKUP for up to 1
second holds epc->lock across the entire controller. In addition,
pci_epc_linkup() is called even when features.linkup_notifier is not set,
and pci_ep_generic_stop() never notifies pci_epc_linkdown(). Deferring
link polling and linkup notification to a work item outside epc->lock
(gated on features.linkup_notifier and canceled on stop/remove) would avoid
this.
> +static void pci_ep_generic_stop(struct pci_epc *epc)
> +{
> + struct pci_ep_generic *epc_priv = epc_get_drvdata(epc);
> +
> + writel_relaxed(PCI_EPC_GEN_CMD_STOP, epc_priv->base + PCI_EPC_GEN_CMD);
> +}
[Severity: Medium]
Should pci_ep_generic_start() and pci_ep_generic_stop() use
pci_ep_generic_write_cmd() (ordered writel()) and readl_poll_timeout()
instead of relaxed MMIO accesses? Using writel_relaxed() for
PCI_EPC_GEN_CMD_START omits the write barrier (__io_bw()) needed on
weakly-ordered architectures to ensure prior CPU writes to DMA-coherent BAR
buffers are visible before the link comes up.
[ ... ]
> + f->dynamic_inbound_mapping = !!(caps & PCI_EPC_GEN_CAP_DYN_INBOUND_MAP);
> + f->subrange_mapping = !!(caps & PCI_EPC_GEN_CAP_SUBRANGE);
[Severity: High]
Should f->subrange_mapping be left unset while pci_ep_generic_set_bar()
does not implement subrange mapping? When both dynamic_inbound_mapping and
subrange_mapping are advertised, pci_epc_set_bar() allows EPF drivers to
pass epf_bar->num_submap > 0, which pci_ep_generic_set_bar() silently
ignores while programming only the top-level epf_bar->phys_addr and
returning 0.
[ ... ]
> + epc_priv->base = devm_platform_ioremap_resource_byname(pdev, "ctrl");
> + if (IS_ERR(epc_priv->base))
> + return PTR_ERR(epc_priv->base);
> +
> + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "outbound");
> + if (!res)
> + return dev_err_probe(dev, -ENODEV, "missing \"outbound\" memory resource\n");
> +
> + if (!devm_request_mem_region(dev, res->start, resource_size(res),
> + dev_name(dev)))
> + return dev_err_probe(dev, -EBUSY, "outbound window already in use\n");
> +
> + ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
> + if (ret)
> + return dev_err_probe(dev, ret, "failed to set DMA mask\n");
[Severity: Medium]
Why is the DMA mask hardcoded to 32 bits when the register interface has
64-bit physical address registers (PCI_EPC_GEN_BAR_PHYS_LO/HI and
PCI_EPC_GEN_MAP_PHYS_LO/HI)? EPF drivers allocate BAR buffers with
dma_alloc_coherent(epc->dev.parent, ...) and map doorbells with
dma_map_resource(epc->dev.parent, ...), so a 32-bit mask breaks allocations
and mappings on 64-bit platforms with RAM or doorbells above 4 GiB and
forces SWIOTLB bounce buffering.
[ ... ]
> + epc = devm_pci_epc_create(dev, &pci_ep_generic_ops);
> + if (IS_ERR(epc))
> + return PTR_ERR(epc);
> +
> + epc->max_functions = readl_relaxed(epc_priv->base + PCI_EPC_GEN_MAX_FUNC);
> + epc_set_drvdata(epc, epc_priv);
> + platform_set_drvdata(pdev, epc);
> +
> + ret = pci_epc_mem_init(epc, res->start, resource_size(res), PAGE_SIZE);
[Severity: Medium]
Should pci_ep_generic_probe() validate PCI_EPC_GEN_MAX_FUNC and the "ctrl"
resource size?
If PCI_EPC_GEN_MAX_FUNC exceeds the number of 0x1000-byte strides in the
"ctrl" resource (or reads 0xffffffff on a fault, truncating to 255),
per-function MMIO accesses at fn * PCI_EPC_GEN_REG_STRIDE will access past
the end of the ioremapped region. Conversely, if it reads 0 (or truncates
to u8 0), the check func_no > epc->max_functions - 1 in pci_epc_add_epf()
underflows to 0xffffffffU and allows invalid EPF binding.
[Severity: Medium]
Can a concurrent configfs access hit a NULL pointer dereference between
devm_pci_epc_create() and epc_set_drvdata()?
devm_pci_epc_create() immediately publishes the controller in configfs via
pci_ep_cfs_add_epc_group() while epc_get_drvdata(epc) is still NULL and
outbound memory is uninitialized:
CPU0 CPU1
pci_ep_generic_probe()
devm_pci_epc_create()
pci_ep_cfs_add_epc_group()
pci_epc_start_store()
pci_epc_start()
pci_ep_generic_start()
epc_priv->base <-- NULL deref
epc_set_drvdata(epc, epc_priv)
Setting epc->max_functions before epc_set_drvdata() and pci_epc_mem_init()
also lets a concurrent EPF bind call pci_ep_generic_get_features() on a
NULL epc_priv (returning an invalid pointer at offset 0x8) or fail outbound
allocations.
[ ... ]
> +static void pci_ep_generic_remove(struct platform_device *pdev)
> +{
> + struct pci_epc *epc = platform_get_drvdata(pdev);
> +
> + pci_epc_deinit_notify(epc);
> + pci_epc_mem_exit(epc);
> +}
[Severity: Medium]
Should pci_ep_generic_remove() stop the endpoint link (via pci_epc_stop()
or PCI_EPC_GEN_CMD_STOP) before tearing down EPF notifications and outbound
memory? Because the driver is tristate and unbindable, removing it while
the controller is started leaves the link active while BARs and DMA buffers
are torn down.
[Severity: Medium]
Can calling pci_epc_mem_exit() in pci_ep_generic_remove() cause a
use-after-free while the EPC is still registered?
Because epc is created with devm_pci_epc_create(), pci_epc_destroy() only
removes the configfs group and unregisters the device in
devres_release_all() after pci_ep_generic_remove() returns.
pci_epc_mem_exit() frees epc->windows and each mem without locking:
CPU0 CPU1
pci_ep_generic_remove()
pci_epc_mem_exit(epc)
kfree(mem)
pci_epc_mem_alloc_addr()
mutex_lock(&mem->lock) <-- UAF
devres_release_all()
pci_epc_destroy()
In addition, if an EPF without an epc_deinit callback calls
pci_epc_mem_free_addr() after pci_epc_mem_exit() zeroes epc->num_windows,
pci_epc_get_matching_window() returns NULL and leaks the ioremap mapping.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204-pci-ep-generic-v1-0-30c0e44e170f@oss.qualcomm.com?part=2
prev parent reply other threads:[~2026-12-04 14:43 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-04 14:00 [PATCH 0/2] PCI: endpoint: Add a generic PCI Endpoint Controller driver Manivannan Sadhasivam via B4 Relay
2026-12-04 14:00 ` [PATCH 1/2] dt-bindings: PCI: Add generic PCI Endpoint Controller Manivannan Sadhasivam via B4 Relay
2026-12-04 14:14 ` sashiko-bot
2026-12-04 14:00 ` [PATCH 2/2] PCI: endpoint: Add generic PCI Endpoint Controller driver Manivannan Sadhasivam via B4 Relay
2026-12-04 14:43 ` sashiko-bot [this message]
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-250004@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=manivannan.sadhasivam@oss.qualcomm.com \
--cc=robh@kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox