Skip to contentSkip to conversation

PCI: hv: fix a couple of issues in hv_pci_onchannelcallback()

PATCH Applied Vitaly Kuznetsov 3 participants 10 messages May 30, 2016 – Jun 19, 2016 PCI

lore
Share & tools
lore
#0 lore raw

cover PCI: hv: fix a couple of issues in hv_pci_onchannelcallback()

kmemleak helped me to identify a memory leak in hv_pci_onchannelcallback() and while fixing it I stumbled upon an unrelated issue(s) there.

Vitaly Kuznetsov (2):
PCI: hv: don't leak buffer in hv_pci_onchannelcallback()
PCI: hv: handle all pending messages in hv_pci_onchannelcallback()

 drivers/pci/host/pci-hyperv.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
Signature · 1 line
2.5.5
#1 lore raw

PATCH 1/2 PCI: hv: don't leak buffer in hv_pci_onchannelcallback()

We don't free buffer on several code paths in hv_pci_onchannelcallback(), put kfree() to the end of the function to fix the issue. Direct { kfree(); return; } can now be replaced with a simple 'break';

---
 drivers/pci/host/pci-hyperv.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/pci/host/pci-hyperv.c +5 -6
index 7e9b2de..a68ec49 100644
@@ -1661,10 +1661,8 @@ static void hv_pci_onchannelcallback(void *context)
 		 * All incoming packets must be at least as large as a
 		 * response.
 		 */
-		if (bytes_recvd <= sizeof(struct pci_response)) {
-			kfree(buffer);
-			return;
-		}
+		if (bytes_recvd <= sizeof(struct pci_response))
+			break;
 		desc = (struct vmpacket_descriptor *)buffer;
 
 		switch (desc->type) {
@@ -1679,8 +1677,7 @@ static void hv_pci_onchannelcallback(void *context)
 			comp_packet->completion_func(comp_packet->compl_ctxt,
 						     response,
 						     bytes_recvd);
-			kfree(buffer);
-			return;
+			break;
 
 		case VM_PKT_DATA_INBAND:
 
@@ -1729,6 +1726,8 @@ static void hv_pci_onchannelcallback(void *context)
 		}
 		break;
 	}
+
+	kfree(buffer);
 }
 
 /**
Signature · 1 line
2.5.5
#3 lore raw

PATCH 2/2 PCI: hv: handle all pending messages in hv_pci_onchannelcallback()

In reply to Vitaly Kuznetsov

When we have an interrupt from host we have a bit set in event page indicating there are messages for the particular channel. We need to read them all as we won't get signaled for what was on the queue before we cleared the bit in vmbus_on_event(). This applies to all Hyper-V drivers and the pass-through driver should do the same.
I did non meet any bugs, the issue was found by code inspection. We don't have many events going through hv_pci_onchannelcallback(), this explains why nobody reported the issue before.

While on it, fix handling non-zero vmbus_recvpacket_raw() return values by dropping out. If the return value is not zero it is wrong to inspect buffer or bytes_recvd as these may contain invalid data.

---
 drivers/pci/host/pci-hyperv.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/pci/host/pci-hyperv.c +5 -2
index a68ec49..7de341d 100644
@@ -1657,12 +1657,16 @@ static void hv_pci_onchannelcallback(void *context)
 			continue;
 		}
 
+		/* Zero length indicates there are no more packets. */
+		if (ret || !bytes_recvd)
+			break;
+
 		/*
 		 * All incoming packets must be at least as large as a
 		 * response.
 		 */
 		if (bytes_recvd <= sizeof(struct pci_response))
-			break;
+			continue;
 		desc = (struct vmpacket_descriptor *)buffer;
 
 		switch (desc->type) {
@@ -1724,7 +1728,6 @@ static void hv_pci_onchannelcallback(void *context)
 				desc->type, req_id, bytes_recvd);
 			break;
 		}
-		break;
 	}
 
 	kfree(buffer);
Signature · 1 line
2.5.5
Acked-by on v1 2/2
#4 lore raw

RE: [PATCH 2/2] PCI: hv: handle all pending messages in hv_pci_onchannelcallback()

Quoted 20 lines

-----Original Message-----
From: Vitaly Kuznetsov [mailto:[email protected]]
Sent: Monday, May 30, 2016 7:18 AM
To: [email protected]
Cc: [email protected]; [email protected]; Bjorn Helgaas <[email protected]>; Haiyang Zhang <[email protected]>; KY Srinivasan <[email protected]>; Jake Oshins <[email protected]>
Subject: [PATCH 2/2] PCI: hv: handle all pending messages in hv_pci_onchannelcallback()

When we have an interrupt from host we have a bit set in event page indicating there are messages for the particular channel. We need to read them all as we won't get signaled for what was on the queue before we cleared the bit in vmbus_on_event(). This applies to all Hyper-V drivers and the pass-through driver should do the same.
I did non meet any bugs, the issue was found by code inspection. We don't have many events going through hv_pci_onchannelcallback(), this explains why nobody reported the issue before.

Quoted 5 lines

While on it, fix handling non-zero vmbus_recvpacket_raw() return values by dropping out. If the return value is not zero it is wrong to inspect buffer or bytes_recvd as these may contain invalid data.

Quoted 35 lines
---
 drivers/pci/host/pci-hyperv.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/pci/host/pci-hyperv.c +5 -1
index a68ec49..7de341d 100644
@@ -1657,12 +1657,16 @@ static void hv_pci_onchannelcallback(void
 *context)
 			continue;
 		}
 
+		/* Zero length indicates there are no more packets. */
+		if (ret || !bytes_recvd)
+			break;
+
 		/*
 		 * All incoming packets must be at least as large as a
 		 * response.
 		 */
 		if (bytes_recvd <= sizeof(struct pci_response))
-			break;
+			continue;
 		desc = (struct vmpacket_descriptor *)buffer;
 
drivers/pci/host/pci-hyperv.c +0 -1
 		switch (desc->type) {
@@ -1724,7 +1728,6 @@ static void hv_pci_onchannelcallback(void
 *context)
 				desc->type, req_id, bytes_recvd);
 			break;
 		}
-		break;
 	}
 
Quoted 3 lines

kfree(buffer);
--
2.5.5

This is good, too.

Thanks,
Jake Oshins

Acked-by on v1 1/2
#2 lore raw

RE: [PATCH 1/2] PCI: hv: don't leak buffer in hv_pci_onchannelcallback()

In reply to Vitaly Kuznetsov
Quoted 10 lines

-----Original Message-----
From: Vitaly Kuznetsov [mailto:[email protected]]
Sent: Monday, May 30, 2016 7:18 AM
To: [email protected]
Cc: [email protected]; [email protected]; Bjorn Helgaas <[email protected]>; Haiyang Zhang <[email protected]>; KY Srinivasan <[email protected]>; Jake Oshins <[email protected]>
Subject: [PATCH 1/2] PCI: hv: don't leak buffer in hv_pci_onchannelcallback()

Quoted 5 lines

We don't free buffer on several code paths in hv_pci_onchannelcallback(), put kfree() to the end of the function to fix the issue. Direct { kfree(); return; } can now be replaced with a simple 'break';

Quoted 43 lines
---
 drivers/pci/host/pci-hyperv.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/pci/host/pci-hyperv.c +2 -4
index 7e9b2de..a68ec49 100644
@@ -1661,10 +1661,8 @@ static void hv_pci_onchannelcallback(void
 *context)
 		 * All incoming packets must be at least as large as a
 		 * response.
 		 */
-		if (bytes_recvd <= sizeof(struct pci_response)) {
-			kfree(buffer);
-			return;
-		}
+		if (bytes_recvd <= sizeof(struct pci_response))
+			break;
 		desc = (struct vmpacket_descriptor *)buffer;
 
drivers/pci/host/pci-hyperv.c +1 -2
 		switch (desc->type) {
@@ -1679,8 +1677,7 @@ static void hv_pci_onchannelcallback(void
 *context)
 			comp_packet->completion_func(comp_packet-
 >compl_ctxt,
 						     response,
 						     bytes_recvd);
-			kfree(buffer);
-			return;
+			break;
 
drivers/pci/host/pci-hyperv.c +2 -0
 		case VM_PKT_DATA_INBAND:
@@ -1729,6 +1726,8 @@ static void hv_pci_onchannelcallback(void
 *context)
 		}
 		break;
 	}
+
+	kfree(buffer);
 }
 
Quoted 3 lines

/**
--
2.5.5

This is a good fix. Thanks.

-- Jake Oshins

#5 lore raw
In reply to Vitaly Kuznetsov
Quoted 7 lines · Vitaly Kuznetsov

kmemleak helped me to identify a memory leak in hv_pci_onchannelcallback() and while fixing it I stumbled upon an unrelated issue(s) there.

Vitaly Kuznetsov (2):
PCI: hv: don't leak buffer in hv_pci_onchannelcallback()
PCI: hv: handle all pending messages in hv_pci_onchannelcallback()

Bjorn,

sorry for the ping but with both patches acked by Jake is there anything else required for this series to get merged? It would be nice to have these fixes in 4.7 but even knowing that they're queued for 4.8 is OK.

Thanks,

Signature · 1 line
  Vitaly
#6 lore raw
Quoted 10 lines · Vitaly Kuznetsov
Quoted 7 lines · Vitaly Kuznetsov

kmemleak helped me to identify a memory leak in hv_pci_onchannelcallback() and while fixing it I stumbled upon an unrelated issue(s) there.

Vitaly Kuznetsov (2):
PCI: hv: don't leak buffer in hv_pci_onchannelcallback()
PCI: hv: handle all pending messages in hv_pci_onchannelcallback()

Quoted 5 lines

Bjorn,

sorry for the ping but with both patches acked by Jake is there anything else required for this series to get merged? It would be nice to have these fixes in 4.7 but even knowing that they're queued for 4.8 is OK.

Nothing else required, but I'm glad you mentioned that these should go in v4.7. By default I merge things to -next, which would be for v4.8 right now.

Bjorn

#7 lore raw
In reply to Vitaly Kuznetsov
Quoted 6 lines · Vitaly Kuznetsov

kmemleak helped me to identify a memory leak in hv_pci_onchannelcallback() and while fixing it I stumbled upon an unrelated issue(s) there.

Vitaly Kuznetsov (2):
PCI: hv: don't leak buffer in hv_pci_onchannelcallback()
PCI: hv: handle all pending messages in hv_pci_onchannelcallback()

I applied both to for-linus for v4.7 with Jake's acks, thanks, Vitaly.

#8 lore raw
Quoted 9 lines · Bjorn Helgaas
Quoted 6 lines · Vitaly Kuznetsov

kmemleak helped me to identify a memory leak in hv_pci_onchannelcallback() and while fixing it I stumbled upon an unrelated issue(s) there.

Vitaly Kuznetsov (2):
PCI: hv: don't leak buffer in hv_pci_onchannelcallback()
PCI: hv: handle all pending messages in hv_pci_onchannelcallback()

I applied both to for-linus for v4.7 with Jake's acks, thanks, Vitaly.

Somehow I must have been thinking these were fixes for things we merged or broke during the v4.7 merge window, but that doesn't look like the case. So I'm going to merge these for v4.8 instead, on the theory that the v4.7-rc cycles are primarily for stabilization.

Bjorn

#9 lore raw
Quoted 9 lines · Bjorn Helgaas
Quoted 8 lines · Bjorn Helgaas
Quoted 6 lines · Vitaly Kuznetsov

kmemleak helped me to identify a memory leak in hv_pci_onchannelcallback() and while fixing it I stumbled upon an unrelated issue(s) there.

Vitaly Kuznetsov (2):
PCI: hv: don't leak buffer in hv_pci_onchannelcallback()
PCI: hv: handle all pending messages in hv_pci_onchannelcallback()

Quoted 6 lines
Quoted 1 line

I applied both to for-linus for v4.7 with Jake's acks, thanks, Vitaly.

Somehow I must have been thinking these were fixes for things we merged or broke during the v4.7 merge window, but that doesn't look like the case. So I'm going to merge these for v4.8 instead, on the theory that the v4.7-rc cycles are primarily for stabilization.

Right, this is not a 4.7 regression.

Thanks!

Signature · 1 line
  Vitaly