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=1796395418; b=rNnEGfU0SctDmHAzeQm8TFh/hO3qG/1WZmBAnwPVvNy2yXpX0uK91AO5xndG7qhbrNrT gDGWEdGEbpyBLaVvZgMgOuQei6zs0rDgYXHvw5dyWikMewZouinZIk1ijYGxkycj7j2MJ mIuIj5GcjtWGpBpVtOUWnjADfbk/zU1QnbhSJA1GtgaD8H8iwgyw/8pifgKvD/bUvPPPV 9dBmoX6+LlCIdEjZiB3MW0si0PZowwTfETpzdiQR0yUEKJ4vd5+9LHdTBordkv33m/qFn ayz7LMpQFxXJjB4gClZJCjKYiA0X8DZRLf6y2mRluZ1XEDqYg87HtuAq10y1Ma5hbpw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796395418; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=pDzRBKROak+JWyjk81m0y27dzoVp01wNiAPXibFaG24=; b=KipqVV6Xtw6IZzsmNaaYkqI908O56FL/r6Sy1hbTGYCfqQM6IEuav4To5CxTJ3X4NSon k5CGPQ9IjVLXX5lal6WCvM1tXDyGjbwmIH4bSmE+7cVew6MCv8mZMEJulrfFQ047ogqXt HPD7/sxa4VPiLaVINuqzxnoRQQnHOEpViRsJ5wxJUYp5tRJHyrVcL6tK0SaYNCrGxGTDs Lo7/6LSpt7pzGC7u6GyKbMChbgvcgW3OpVEO8WIJrvRMeG8LMaFBB8yCYefCuJ39C30JL mDe8V6QEINRcZPgbFpRQ5tkqf/DouW1YsOW1vJDgRLlXrt0nZbVQWt6fuKQfiEXaIoQ== 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=c5B2LeYG; 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 38511606A322; Fri, 4 Dec 2026 14:43:38 +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=c5B2LeYG Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9E2E51F00897; Fri, 4 Dec 2026 14:43:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796395417; bh=pDzRBKROak+JWyjk81m0y27dzoVp01wNiAPXibFaG24=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=c5B2LeYGTkCZotMoI4euwzdTaxfH9f5wML8BoX+YFX10KAoa6cmRlIvRQEtfWlUEe QRCxtFKX3oBSh42FOxVLv8bQtXm3VXyjXYdso4DdrIdW4ut1i4/oeePoSI3u84HtEe WOX71mOhUpdNWSF/Bbda9dAZFBAy1yNqIufX+j9+RT2tz2adxO2sd8FwFUcOTNSvDQ Q/C1yjN1NsShsIIlEW3A+nHludCOPTIAeC3qy9lWQRaRPWjH8m/hKZqLfgmZRyUIM5 8olM7puhHc571BloKvy1s+36nzH76X6Xpe+kfHW/AIzDppxfv9ZoByDg6LX3gejAq6 ZZrU7yaxO/m/g== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH 2/2] PCI: endpoint: Add generic PCI Endpoint Controller driver Reply-To: sashiko-reviews@lists.linux.dev To: "Manivannan Sadhasivam" Cc: linux-pci@vger.kernel.org, devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org In-Reply-To: <20261204-pci-ep-generic-v1-2-30c0e44e170f@oss.qualcomm.com> References: <20261204-pci-ep-generic-v1-0-30c0e44e170f@oss.qualcomm.com> <20261204-pci-ep-generic-v1-2-30c0e44e170f@oss.qualcomm.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Fri, 04 Dec 2026 14:43:37 +0000 X-Mailing-List: linux-pci@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 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 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/contro= ller/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 [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 =3D 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 =3D epc_get_drvdata(epc); > + u64 msi_addr, aligned; > + > + msi_addr =3D ((u64)pci_ep_generic_read(epc_priv, fn, PCI_EPC_GEN_MSI_AD= DR_HI) << 32 | > + pci_ep_generic_read(epc_priv, fn, PCI_EPC_GEN_MSI_ADDR_LO)); > + > + aligned =3D ALIGN_DOWN(msi_addr, entry_size); > + *msi_addr_offset =3D (msi_addr - aligned) + (u32)irq_num * entry_size; > + *msi_data =3D 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] =3D 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 <=3D irq_num <=3D msi_count, nor that entry_size is a non-zero power of t= wo before ALIGN_DOWN(). > +static int pci_ep_generic_start(struct pci_epc *epc) > +{ > + struct pci_ep_generic *epc_priv =3D epc_get_drvdata(epc); > + u32 linkup; > + int ret; > + > + writel_relaxed(PCI_EPC_GEN_CMD_START, epc_priv->base + PCI_EPC_GEN_CMD); > + > + ret =3D 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 =3D 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 =3D !!(caps & PCI_EPC_GEN_CAP_DYN_INBOUND_MA= P); > + f->subrange_mapping =3D !!(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 =3D devm_platform_ioremap_resource_byname(pdev, "ctrl"); > + if (IS_ERR(epc_priv->base)) > + return PTR_ERR(epc_priv->base); > + > + res =3D platform_get_resource_byname(pdev, IORESOURCE_MEM, "outbound"); > + if (!res) > + return dev_err_probe(dev, -ENODEV, "missing \"outbound\" memory resour= ce\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 =3D 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 =3D devm_pci_epc_create(dev, &pci_ep_generic_ops); > + if (IS_ERR(epc)) > + return PTR_ERR(epc); > + > + epc->max_functions =3D readl_relaxed(epc_priv->base + PCI_EPC_GEN_MAX_F= UNC); > + epc_set_drvdata(epc, epc_priv); > + platform_set_drvdata(pdev, epc); > + > + ret =3D 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 =3D 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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261204-pci-ep-gen= eric-v1-0-30c0e44e170f@oss.qualcomm.com?part=3D2