Linux PCI subsystem development
 help / color / mirror / Atom feed
* [PATCH] PCI: endpoint: check epf->epc and epf->sec_epc under locks in pci_epc_add_epf()
@ 2026-12-06  8:29 Ziheng Wei
  2026-12-06  8:44 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Ziheng Wei @ 2026-12-06  8:29 UTC (permalink / raw)
  To: Manivannan Sadhasivam, Krzysztof Wilczyński,
	Kishon Vijay Abraham I
  Cc: Frank Li, Bjorn Helgaas, linux-pci, linux-kernel, stable

pci_epc_add_epf() checks epf->epc and epf->sec_epc before acquiring
epc->list_lock or epf->lock.  Concurrent calls binding the same EPF via
configfs can both pass the unlocked check, allocate two function numbers,
and call list_add_tail() twice on the same list_head, corrupting
epc->pci_epf.

Acquire epc->list_lock followed by epf->lock (matching the lock order in
pci_epc_linkup() and the other EPC notification helpers) before checking
and updating epf->epc and epf->sec_epc, and hold epf->lock symmetrically
in pci_epc_remove_epf().

Found by LLM-assisted static analysis. Compile-tested on x86_64.

Fixes: 5e8cb4033807 ("PCI: endpoint: Add EP core layer to enable EP controller and EP functions")
Cc: stable@vger.kernel.org
Signed-off-by: Ziheng Wei <zihengw03@163.com>
---
 drivers/pci/endpoint/pci-epc-core.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/drivers/pci/endpoint/pci-epc-core.c b/drivers/pci/endpoint/pci-epc-core.c
index 831b40458..14deff2cb 100644
--- a/drivers/pci/endpoint/pci-epc-core.c
+++ b/drivers/pci/endpoint/pci-epc-core.c
@@ -797,13 +797,15 @@ int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf *epf,
 	if (IS_ERR_OR_NULL(epc) || epf->is_vf)
 		return -EINVAL;
 
-	if (type == PRIMARY_INTERFACE && epf->epc)
-		return -EBUSY;
+	mutex_lock(&epc->list_lock);
+	mutex_lock(&epf->lock);
 
-	if (type == SECONDARY_INTERFACE && epf->sec_epc)
-		return -EBUSY;
+	if ((type == PRIMARY_INTERFACE && epf->epc) ||
+	    (type == SECONDARY_INTERFACE && epf->sec_epc)) {
+		ret = -EBUSY;
+		goto ret;
+	}
 
-	mutex_lock(&epc->list_lock);
 	func_no = find_first_zero_bit(&epc->function_num_map,
 				      BITS_PER_LONG);
 	if (func_no >= BITS_PER_LONG) {
@@ -830,6 +832,7 @@ int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf *epf,
 
 	list_add_tail(list, &epc->pci_epf);
 ret:
+	mutex_unlock(&epf->lock);
 	mutex_unlock(&epc->list_lock);
 
 	return ret;
@@ -855,6 +858,7 @@ void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf,
 		return;
 
 	mutex_lock(&epc->list_lock);
+	mutex_lock(&epf->lock);
 	if (type == PRIMARY_INTERFACE) {
 		func_no = epf->func_no;
 		list = &epf->list;
@@ -866,6 +870,7 @@ void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf,
 	}
 	clear_bit(func_no, &epc->function_num_map);
 	list_del(list);
+	mutex_unlock(&epf->lock);
 	mutex_unlock(&epc->list_lock);
 }
 EXPORT_SYMBOL_GPL(pci_epc_remove_epf);

base-commit: 97d73c8251226cf27cc6656b4c542869bb8c69fa
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] PCI: endpoint: check epf->epc and epf->sec_epc under locks in pci_epc_add_epf()
  2026-12-06  8:29 [PATCH] PCI: endpoint: check epf->epc and epf->sec_epc under locks in pci_epc_add_epf() Ziheng Wei
@ 2026-12-06  8:44 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-06  8:44 UTC (permalink / raw)
  To: Ziheng Wei; +Cc: linux-pci

> pci_epc_add_epf() checks epf->epc and epf->sec_epc before acquiring
> epc->list_lock or epf->lock.  Concurrent calls binding the same EPF via
> configfs can both pass the unlocked check, allocate two function numbers,
> and call list_add_tail() twice on the same list_head, corrupting
> epc->pci_epf.
> 
> Acquire epc->list_lock followed by epf->lock (matching the lock order in
> pci_epc_linkup() and the other EPC notification helpers) before checking
> and updating epf->epc and epf->sec_epc, and hold epf->lock symmetrically
> in pci_epc_remove_epf().
> 
> Found by LLM-assisted static analysis. Compile-tested on x86_64.
> 
> Fixes: 5e8cb4033807 ("PCI: endpoint: Add EP core layer to enable EP controller and EP functions")
> Cc: stable@vger.kernel.org
> Signed-off-by: Ziheng Wei <zihengw03@163.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261206082942.1296-1-zihengw03@163.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-06  8:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-06  8:29 [PATCH] PCI: endpoint: check epf->epc and epf->sec_epc under locks in pci_epc_add_epf() Ziheng Wei
2026-12-06  8:44 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox