Skip to contentSkip to conversation

tg3: Fix peer device double put in tg3_find_peer()

PATCH In review Wentao Liang 2 participants 2 messages Sep 17 – Sep 20

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tg3: Fix peer device double put in tg3_find_peer()

pci_get_slot() returns a device with its reference count incremented. When the loop ends on tp->pdev (which happens when the adapter sits at PCI function 7 and no other function is present) that reference has already been dropped inside the loop, but the trailing pci_dev_put() drops it a second time, underflowing the refcount of tp->pdev.

Treat a loop end on tp->pdev as single-port mode, just like the existing !peer case.

Fixes 16fe9d74f14e ("[TG3]: Fix 5704 single-port mode")
---
 drivers/net/ethernet/broadcom/tg3.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/net/ethernet/broadcom/tg3.c +3 -2
index 73a4b569b03e..57669a22c036 100644
@@ -16122,9 +16122,10 @@ static struct pci_dev *tg3_find_peer(struct tg3 *tp)
 		pci_dev_put(peer);
 	}
 	/* 5704 can be configured in single-port mode, set peer to
-	 * tp->pdev in that case.
+	 * tp->pdev in that case. The loop can also end on tp->pdev
+	 * itself, whose reference was already dropped above.
 	 */
-	if (!peer) {
+	if (!peer || peer == tp->pdev) {
 		peer = tp->pdev;
 		return peer;
 	}
Signature · 1 line
2.34.1
reviewer
#1 lore raw
Quoted 6 lines · Wentao Liang

pci_get_slot() returns a device with its reference count incremented. When the loop ends on tp->pdev (which happens when the adapter sits at PCI function 7 and no other function is present) that reference has already been dropped inside the loop, but the trailing pci_dev_put() drops it a second time, underflowing the refcount of tp->pdev.

Quoted 6 lines

Treat a loop end on tp->pdev as single-port mode, just like the existing !peer case.

Fixes 16fe9d74f14e ("[TG3]: Fix 5704 single-port mode")

Hi,

I agree with your analysis of the logic.
But I wonder if it can occur in practice:
can such a device actually be presented to the Kernel?