Linux PCI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Koichiro Den" <den@valinux.co.jp>
Cc: linux-pci@vger.kernel.org
Subject: Re: [PATCH v5 4/6] PCI: dwc: Expose endpoint DMA resources
Date: Fri, 17 Jul 2026 05:19:49 +0000	[thread overview]
Message-ID: <20260717051950.2CC741F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260717050635.2145014-5-den@valinux.co.jp>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] TOCTOU race condition and lockless access of `edma` state leads to kernel heap buffer overflow.
- [High] Missing implementation of `delegate_dma_chan` and `reclaim_dma_chan` in `pci_epc_ops` makes exposed DMA channels unusable.
--

--- Patch [4]: [PATCH v5 4/6] PCI: dwc: Expose endpoint DMA resources ---
commit 0cf8b17e421eb425ac8f9d3f97c02af0eb190199
Author: Koichiro Den <den@valinux.co.jp>

PCI: dwc: Expose endpoint DMA resources

Expose the DesignWare endpoint-integrated eDMA register window, logical
DMA channels, and linked-list descriptor memories through the EPC
auxiliary resource API. Expose DMA controller and channel resources only
after the local DW eDMA provider has been registered.

> diff --git a/drivers/pci/controller/dwc/pcie-designware-ep.c b/drivers/pci/controller/dwc/pcie-designware-ep.c
> index 7d2794945704e..ef1e2b6d85cf3 100644
> --- a/drivers/pci/controller/dwc/pcie-designware-ep.c
> +++ b/drivers/pci/controller/dwc/pcie-designware-ep.c

[ ... ]

> @@ -865,14 +895,23 @@ dw_pcie_ep_get_aux_resources_count(struct pci_epc *epc, u8 func_no,
>  	struct dw_pcie_ep *ep = epc_get_drvdata(epc);
>  	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
>  	struct dw_edma_chip *edma = &pci->edma;
> +	int ret;
> +	int count = 0;
>  
>  	if (!pci->edma_reg_size)
>  		return 0;
>  
> -	if (edma->db_offset == ~0)
> -		return 0;
> +	ret = dw_pcie_ep_check_edma_vfunc(vfunc_no);
> +	if (ret)
> +		return ret;
>  
> -	return 1;
> +	if (dw_pcie_ep_has_edma_ll_resources(edma))
> +		count += 1 + 2 * (edma->ll_wr_cnt + edma->ll_rd_cnt);
> +
> +	if (edma->dw && edma->db_offset != ~0)
> +		count++;

[Severity: Critical]
Is it possible for edma->dw to change concurrently here (e.g. if the link
state changes and dw_edma_probe() completes), leading to an inconsistent
count and a buffer overflow in dw_pcie_ep_get_aux_resources() below?

If edma->dw is NULL during dw_pcie_ep_has_edma_ll_resources(), but becomes
non-NULL before the second check, count will be evaluated as 1.

> +
> +	return count;
>  }
>  
>  static int
> @@ -888,6 +927,8 @@ dw_pcie_ep_get_aux_resources(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
>  	resource_size_t db_offset = edma->db_offset;
>  	resource_size_t dma_ctrl_bar_offset = 0;
>  	resource_size_t dma_reg_size;
> +	bool has_edma_ll_resources;
> +	unsigned int i;
>  	int count;
>  
>  	count = dw_pcie_ep_get_aux_resources_count(epc, func_no, vfunc_no);
> @@ -901,6 +942,7 @@ dw_pcie_ep_get_aux_resources(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
>  		return 0;
>  
>  	dma_reg_size = pci->edma_reg_size;
> +	has_edma_ll_resources = dw_pcie_ep_has_edma_ll_resources(edma);

[Severity: Critical]
Since count could be calculated as 1 earlier, the bounds check
(num_resources < count) passes. However, has_edma_ll_resources is
re-evaluated here. 

If edma->dw is now non-NULL, this evaluates to true, and the function
will write 1 + 2 * (wr_cnt + rd_cnt) elements into the resources array which
may only be sized for 1 element, causing an out-of-bounds write.

[ ... ]

> @@ -917,7 +1029,7 @@ dw_pcie_ep_get_aux_resources(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
>  				  sizeof(u32), dma_reg_size))
>  		return -EINVAL;
>  
> -	resources[0] = (struct pci_epc_aux_resource) {
> +	resources[count] = (struct pci_epc_aux_resource) {
>  		.type = PCI_EPC_AUX_DOORBELL_MMIO,
>  		.phys_addr = pci->edma_reg_phys + db_offset,
>  		.size = sizeof(u32),

[Severity: High]
This patch exposes DMA channels via dw_pcie_ep_get_aux_resources(), but does
epc_ops also need to implement the delegate_dma_chan and reclaim_dma_chan
callbacks?

Without these callbacks in epc_ops, if an EPF driver attempts to reserve one
of these newly exposed DMA channels by calling pci_epc_delegate_dma_chan(),
will it immediately fail with -EOPNOTSUPP?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260717050635.2145014-1-den@valinux.co.jp?part=4

  reply	other threads:[~2026-07-17  5:19 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-17  5:06 [PATCH v5 0/6] PCI: endpoint: Expose endpoint DMA resources (part 2/3) Koichiro Den
2026-07-17  5:06 ` [PATCH v5 1/6] PCI: endpoint: Define endpoint DMA BAR metadata format Koichiro Den
2026-07-17  5:08   ` sashiko-bot
2026-07-17  5:06 ` [PATCH v5 2/6] PCI: endpoint: Add DMA auxiliary resource metadata Koichiro Den
2026-07-17  5:15   ` sashiko-bot
2026-07-17  5:06 ` [PATCH v5 3/6] PCI: endpoint: Add API to delegate EPC DMA channels to the host Koichiro Den
2026-07-17  5:16   ` sashiko-bot
2026-07-17  5:06 ` [PATCH v5 4/6] PCI: dwc: Expose endpoint DMA resources Koichiro Den
2026-07-17  5:19   ` sashiko-bot [this message]
2026-07-17  5:06 ` [PATCH v5 5/6] dmaengine: dw-edma: Add delegated channel request helpers Koichiro Den
2026-07-17  5:21   ` sashiko-bot
2026-07-17  5:06 ` [PATCH v5 6/6] PCI: dwc: Implement endpoint DMA channel delegation Koichiro Den
2026-07-17  5:12   ` 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=20260717051950.2CC741F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=den@valinux.co.jp \
    --cc=linux-pci@vger.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