PCI: hv: Fix the PCI HyperV probe failure path to release resource properly
Subsystems (4)
Wei Hu
maintainerPATCH v2 1/2 PCI: hv: Fix the PCI HyperV probe failure path to release resource properly
Some error cases in hv_pci_probe() were not handled. Fix these error paths to release the resourses and clean up the state properly.
--- drivers/pci/controller/pci-hyperv.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)
drivers/pci/controller/pci-hyperv.c +16 -4
@@ -480,6 +480,9 @@ struct hv_pcibus_device {struct workqueue_struct *wq; +/* Highest slot of child device with resources allocated */ +int wslot_res_allocated; +/* hypercall arg, must not cross page boundary */ struct hv_retarget_device_interrupt retarget_msi_interrupt_params;
@@ -2847,7 +2850,7 @@ static int hv_send_resources_allocated(struct hv_device *hdev)struct hv_pci_dev *hpdev;struct pci_packet *pkt;size_t size_res; -u32 wslot; +int wslot;int ret; size_res = (hbus->protocol_version <PCI_PROTOCOL_VERSION_1_2 )
@@ -2900,6 +2903,8 @@ static int hv_send_resources_allocated(struct hv_device *hdev) comp_pkt.completion_status);break ; } + + hbus->wslot_res_allocated = wslot; }kfree (pkt);
@@ -2918,10 +2923,10 @@ static int hv_send_resources_released(struct hv_device *hdev)struct hv_pcibus_device *hbus =hv_get_drvdata (hdev);struct pci_child_message pkt;struct hv_pci_dev *hpdev; -u32 wslot; +int wslot;int ret; -for (wslot =0 ; wslot <256 ; wslot++) { +for (wslot = hbus->wslot_res_allocated; wslot >=0 ; wslot--) { hpdev =get_pcichild_wslot (hbus, wslot);if (!hpdev)continue ;
@@ -2936,8 +2941,12 @@ static int hv_send_resources_released(struct hv_device *hdev) VM_PKT_DATA_INBAND, 0);if (ret)return ret; + + hbus->wslot_res_allocated = wslot -1 ; } + hbus->wslot_res_allocated =-1 ; +return 0 ; }
@@ -3037,6 +3046,7 @@ static int hv_pci_probe(struct hv_device *hdev,if (!hbus)return -ENOMEM ; hbus->state = hv_pcibus_init; + hbus->wslot_res_allocated =-1 ;/* * The PCI bus "domain" is what is called "segment" in ACPI and other
@@ -3136,7 +3146,7 @@ static int hv_pci_probe(struct hv_device *hdev, ret =hv_pci_allocate_bridge_windows (hbus);if (ret) - goto free_irq_domain; + goto exit_d0; ret =hv_send_resources_allocated (hdev);if (ret)
@@ -3154,6 +3164,8 @@ static int hv_pci_probe(struct hv_device *hdev, free_windows:hv_pci_free_bridge_windows (hbus); +exit_d0: + (void )hv_pci_bus_exit (hdev, true); free_irq_domain:irq_domain_remove (hbus->irq_domain); free_fwnode:
Signature · 1 line
2.20.1
From: Wei Hu <[email protected]> Sent: Thursday, April 30, 2020 10:36 PM
Quoted 94 lines
Some error cases in
hv_pci_probe()were not handled. Fix these error paths to release the resourses and clean up the state properly.Signed-off-by Wei Hu <[email protected]>--- drivers/pci/controller/pci-hyperv.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)drivers/pci/controller/pci-hyperv.c +16
-5@@ -480,6 +480,9 @@ struct hv_pcibus_device {struct workqueue_struct *wq; +/* Highest slot of child device with resources allocated */ +int wslot_res_allocated; +/* hypercall arg, must not cross page boundary */ struct hv_retarget_device_interrupt retarget_msi_interrupt_params;@@ -2847,7 +2850,7 @@ static int hv_send_resources_allocated(struct hv_device *hdev)struct hv_pci_dev *hpdev;struct pci_packet *pkt;size_t size_res; -u32 wslot; +int wslot;int ret; size_res = (hbus->protocol_version <PCI_PROTOCOL_VERSION_1_2 )@@ -2900,6 +2903,8 @@ static int hv_send_resources_allocated(struct hv_device *hdev) comp_pkt.completion_status);break ; } + + hbus->wslot_res_allocated = wslot; }kfree (pkt);@@ -2918,10 +2923,10 @@ static int hv_send_resources_released(struct hv_device *hdev)struct hv_pcibus_device *hbus =hv_get_drvdata (hdev);struct pci_child_message pkt;struct hv_pci_dev *hpdev; -u32 wslot; +int wslot;int ret; -for (wslot =0 ; wslot <256 ; wslot++) { +for (wslot = hbus->wslot_res_allocated; wslot >=0 ; wslot--) { hpdev =get_pcichild_wslot (hbus, wslot);if (!hpdev)continue ;@@ -2936,8 +2941,12 @@ static int hv_send_resources_released(struct hv_device *hdev) VM_PKT_DATA_INBAND, 0);if (ret)return ret; + + hbus->wslot_res_allocated = wslot -1 ; } + hbus->wslot_res_allocated =-1 ; +return 0 ; }@@ -3037,6 +3046,7 @@ static int hv_pci_probe(struct hv_device *hdev,if (!hbus)return -ENOMEM ; hbus->state = hv_pcibus_init; + hbus->wslot_res_allocated =-1 ;/* * The PCI bus "domain" is what is called "segment" in ACPI and other @@ -3136,7 +3146,7 @@ static int hv_pci_probe(struct hv_device *hdev, ret =hv_pci_allocate_bridge_windows (hbus);if (ret) - goto free_irq_domain; + goto exit_d0; ret =hv_send_resources_allocated (hdev);if (ret)@@ -3154,6 +3164,8 @@ static int hv_pci_probe(struct hv_device *hdev, free_windows:hv_pci_free_bridge_windows (hbus); +exit_d0: + (void )hv_pci_bus_exit (hdev, true); free_irq_domain:irq_domain_remove (hbus->irq_domain); free_fwnode: --
Quoted 1 line
2.20.1
Quoted 2 lines · Wei Hu
Some error cases in
hv_pci_probe()were not handled. Fix these error paths to release the resourses and clean up the state properly.
This patch does more than that. It adds a variable to store the number of slots actually allocated - I presume to free only allocated on slots on the exit path.
Two patches required I am afraid.
Quoted 10 lines
Signed-off-by Wei Hu <[email protected]>--- drivers/pci/controller/pci-hyperv.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)drivers/pci/controller/pci-hyperv.c +0
-0
drivers/pci/controller/pci-hyperv.c struct hv_pcibus_device { · line 484
Quoted 5 lines
drivers/pci/controller/pci-hyperv.c +2
-0@@ -480,6 +480,9 @@ struct hv_pcibus_device {struct workqueue_struct *wq; +/* Highest slot of child device with resources allocated */ +int wslot_res_allocated;
I don't understand why you need an int rather than a u32.
Furthermore, I think a bitmap is more appropriate for what this variable is used for.
Quoted 36 lines
drivers/pci/controller/pci-hyperv.c +6
-3+/* hypercall arg, must not cross page boundary */ struct hv_retarget_device_interrupt retarget_msi_interrupt_params;@@ -2847,7 +2850,7 @@ static int hv_send_resources_allocated(struct hv_device *hdev)struct hv_pci_dev *hpdev;struct pci_packet *pkt;size_t size_res; -u32 wslot; +int wslot;int ret; size_res = (hbus->protocol_version <PCI_PROTOCOL_VERSION_1_2 )@@ -2900,6 +2903,8 @@ static int hv_send_resources_allocated(struct hv_device *hdev) comp_pkt.completion_status);break ; } + + hbus->wslot_res_allocated = wslot; }kfree (pkt);@@ -2918,10 +2923,10 @@ static int hv_send_resources_released(struct hv_device *hdev)struct hv_pcibus_device *hbus =hv_get_drvdata (hdev);struct pci_child_message pkt;struct hv_pci_dev *hpdev; -u32 wslot; +int wslot;int ret; -for (wslot =0 ; wslot <256 ; wslot++) { +for (wslot = hbus->wslot_res_allocated; wslot >=0 ; wslot--) { hpdev =get_pcichild_wslot (hbus, wslot);if (!hpdev)continue ;
drivers/pci/controller/pci-hyperv.c static int hv_send_resources_released(struct hv_device *hdev) · line 2945
Quoted 5 lines
drivers/pci/controller/pci-hyperv.c +2
-0@@ -2936,8 +2941,12 @@ static int hv_send_resources_released(struct hv_device *hdev) VM_PKT_DATA_INBAND, 0);if (ret)return ret; + + hbus->wslot_res_allocated = wslot -1 ;
Do you really need to set it at every loop iteration ?
Moreover, I think a bitmap is better suited for what you are doing, given that you may skip some loop indexes on !hpdev.
Quoted 25 lines
drivers/pci/controller/pci-hyperv.c +4
-1} + hbus->wslot_res_allocated =-1 ; +return 0 ; }@@ -3037,6 +3046,7 @@ static int hv_pci_probe(struct hv_device *hdev,if (!hbus)return -ENOMEM ; hbus->state = hv_pcibus_init; + hbus->wslot_res_allocated =-1 ;/* * The PCI bus "domain" is what is called "segment" in ACPI and other @@ -3136,7 +3146,7 @@ static int hv_pci_probe(struct hv_device *hdev, ret =hv_pci_allocate_bridge_windows (hbus);if (ret) - goto free_irq_domain; + goto exit_d0; ret =hv_send_resources_allocated (hdev);if (ret)
drivers/pci/controller/pci-hyperv.c static int hv_pci_probe(struct hv_device *hdev, · line 3168
Quoted 5 lines
drivers/pci/controller/pci-hyperv.c +2
-0@@ -3154,6 +3164,8 @@ static int hv_pci_probe(struct hv_device *hdev, free_windows:hv_pci_free_bridge_windows (hbus); +exit_d0: + (void )hv_pci_bus_exit (hdev, true);
Remove the (void) cast.
Lorenzo
Quoted 6 lines
drivers/pci/controller/pci-hyperv.c +0
-0free_irq_domain:irq_domain_remove (hbus->irq_domain); free_fwnode:Signature · 1 line
2.20.1
Wei Hu
maintainerHi Lorenzo,
Thanks for your review. Please see my comments inline.
Quoted 13 lines
-----Original Message-----
From: Lorenzo Pieralisi <[email protected]>
Sent: Tuesday, May 5, 2020 11:03 PM
To: Wei Hu <[email protected]>
Cc: KY Srinivasan <[email protected]>; Haiyang Zhang <[email protected]>; Stephen Hemminger <[email protected]>; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]; linux- [email protected]; Dexuan Cui <[email protected]>; Michael Kelley <[email protected]>
Subject: Re: [PATCH v2 1/2] PCI: hv: Fix the PCI HyperV probe failure path to release resource properly
Quoted 8 lines
Quoted 2 lines · Wei Hu
Some error cases in
hv_pci_probe()were not handled. Fix these error paths to release the resourses and clean up the state properly.This patch does more than that. It adds a variable to store the number of slots actually allocated - I presume to free only allocated on slots on the exit path.
Two patches required I am afraid.
Well, adding this variable is needed to make the call of "(void) hv_pci_bus_exit(hdev, true)" at the end to work and clean up the PCI state in the failure path. Just adding this variable would not make any changes. So I think it may be better to put them in single patch?
Quoted 15 lines
Quoted 15 lines
Signed-off-by Wei Hu <[email protected]>--- drivers/pci/controller/pci-hyperv.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)drivers/pci/controller/pci-hyperv.c +0
-0@@ -480,6 +480,9 @@ struct hv_pcibus_device {struct workqueue_struct *wq;
Quoted 7 lines
drivers/pci/controller/pci-hyperv.c struct hv_pcibus_device { · line 484
Quoted 2 lines
drivers/pci/controller/pci-hyperv.c +2
-0+/* Highest slot of child device with resources allocated */ +int wslot_res_allocated;I don't understand why you need an int rather than a u32.
Furthermore, I think a bitmap is more appropriate for what this variable is used for.
So it can use a negative value (-1 in this case) to indicated none of any resources has been allocated. Currently value between 0-255 indicating some resources have been allocated. Of course I can use 0xffffffff to indicate that if it were u32. But it wouldn't make much difference, would it?
It would take 32 bytes for total 256 child slots in bitmap, while it only takes 4 bytes using int. It is not in critical path so scanning from the location one by one is not a big deal.
Quoted 50 lines
drivers/pci/controller/pci-hyperv.c static int hv_send_resources_allocated(struct
Quoted 6 lines
drivers/pci/controller/pci-hyperv.c +1
-0+/* hypercall arg, must not cross page boundary */ struct hv_retarget_device_interrupt retarget_msi_interrupt_params;@@ -2847,7 +2850,7 @@ static int hv_send_resources_allocated(struct
hv_device*hdev)drivers/pci/controller/pci-hyperv.c static int hv_send_resources_allocated(struct hv_device *hdev) · line 2856
Quoted 8 lines
drivers/pci/controller/pci-hyperv.c +1
-1struct hv_pci_dev *hpdev;struct pci_packet *pkt;size_t size_res; -u32 wslot; +int wslot;int ret; size_res = (hbus->protocol_version <PCI_PROTOCOL_VERSION_1_2 )@@
Quoted 1 line
-2900,6 +2903,8 @@ static int hv_send_resources_allocated(struct
hv_device*hdev)
drivers/pci/controller/pci-hyperv.c static int hv_send_resources_released(struct
Quoted 9 lines
drivers/pci/controller/pci-hyperv.c +2
-0comp_pkt.completion_status);break ; } + + hbus->wslot_res_allocated = wslot; }kfree (pkt);@@ -2918,10 +2923,10 @@ static int hv_send_resources_released(struct
hv_device*hdev)drivers/pci/controller/pci-hyperv.c static int hv_send_resources_released(struct
Quoted 13 lines
drivers/pci/controller/pci-hyperv.c +2
-2struct hv_pcibus_device *hbus =hv_get_drvdata (hdev);struct pci_child_message pkt;struct hv_pci_dev *hpdev; -u32 wslot; +int wslot;int ret; -for (wslot =0 ; wslot <256 ; wslot++) { +for (wslot = hbus->wslot_res_allocated; wslot >=0 ; wslot--) { hpdev =get_pcichild_wslot (hbus, wslot);if (!hpdev)continue ;@@ -2936,8 +2941,12 @@ static int hv_send_resources_released(struct
hv_device*hdev)drivers/pci/controller/pci-hyperv.c static int hv_send_resources_released(struct hv_device *hdev) · line 2945
Quoted 5 lines
drivers/pci/controller/pci-hyperv.c +2
-0VM_PKT_DATA_INBAND, 0);if (ret)return ret; + + hbus->wslot_res_allocated = wslot -1 ;Do you really need to set it at every loop iteration ?
Quoted 3 lines
Moreover, I think a bitmap is better suited for what you are doing, given that you may skip some loop indexes on !hpdev.
The value is set in the loop whenever a resource was successfully released. It could happen that it failed in the next iteration so the last one which had succeeded would be recorded in this variable. It is needed at the end of loop when this iteration succeeds.
Once again since it is not in the critical path, just using an signed integer is straightforward, less error prone and a bit easier to maintain than bitmap in my opinion. 😊
Quoted 27 lines
Quoted 27 lines
drivers/pci/controller/pci-hyperv.c +3
-0} + hbus->wslot_res_allocated =-1 ; +return 0 ; }@@ -3037,6 +3046,7 @@ static int hv_pci_probe(struct hv_device *hdev,if (!hbus)return -ENOMEM ; hbus->state = hv_pcibus_init; + hbus->wslot_res_allocated =-1 ;/* * The PCI bus "domain" is what is called "segment" in ACPI and other @@ -3136,7 +3146,7 @@ static int hv_pci_probe(struct
hv_device*hdev,drivers/pci/controller/pci-hyperv.c +1
-1ret =hv_pci_allocate_bridge_windows (hbus);if (ret) - goto free_irq_domain; + goto exit_d0; ret =hv_send_resources_allocated (hdev);if (ret)@@ -3154,6 +3164,8 @@ static int hv_pci_probe(struct hv_device *hdev,
Quoted 7 lines
drivers/pci/controller/pci-hyperv.c static int hv_pci_probe(struct hv_device *hdev, · line 3168
Quoted 4 lines
drivers/pci/controller/pci-hyperv.c +2
-0free_windows:hv_pci_free_bridge_windows (hbus); +exit_d0: + (void )hv_pci_bus_exit (hdev, true);Remove the (void) cast.
Some tools (maybe lint?) could generate error/warning messages assuming the code fails to check the return value without such cast. Leaving the cast in place just indicates that the return value is deliberately ignored.
Thanks,
Wei
Quoted 21 lines · Wei Hu
Hi Lorenzo,
Thanks for your review. Please see my comments inline.
Quoted 17 lines
-----Original Message-----
From: Lorenzo Pieralisi <[email protected]>
Sent: Tuesday, May 5, 2020 11:03 PM
To: Wei Hu <[email protected]>
Cc: KY Srinivasan <[email protected]>; Haiyang Zhang <[email protected]>; Stephen Hemminger <[email protected]>; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]; linux- [email protected]; Dexuan Cui <[email protected]>; Michael Kelley <[email protected]>
Subject: Re: [PATCH v2 1/2] PCI: hv: Fix the PCI HyperV probe failure path to release resource properlyQuoted 2 lines · Wei Hu
Some error cases in
hv_pci_probe()were not handled. Fix these error paths to release the resourses and clean up the state properly.
Quoted 6 lines
Quoted 4 lines
This patch does more than that. It adds a variable to store the number of slots actually allocated - I presume to free only allocated on slots on the exit path.
Two patches required I am afraid.
Well, adding this variable is needed to make the call of "(void)
hv_pci_bus_exit(hdev, true)"
I don't understand why - it is not clear from the commit log and the code, please explain it since it is not obvious.
Quoted 23 lines
at the end to work and clean up the PCI state in the failure path. Just adding this variable would not make any changes. So I think it may be better to put them in single patch?
Quoted 20 lines
Quoted 12 lines
Signed-off-by Wei Hu <[email protected]>--- drivers/pci/controller/pci-hyperv.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)drivers/pci/controller/pci-hyperv.c +0
-0drivers/pci/controller/pci-hyperv.c struct hv_pcibus_device { · line 484
Quoted 5 lines
drivers/pci/controller/pci-hyperv.c +2
-0@@ -480,6 +480,9 @@ struct hv_pcibus_device {struct workqueue_struct *wq; +/* Highest slot of child device with resources allocated */ +int wslot_res_allocated;I don't understand why you need an int rather than a u32.
Quoted 7 lines
Quoted 2 lines
Furthermore, I think a bitmap is more appropriate for what this variable is used for.
So it can use a negative value (-1 in this case) to indicated none of any resources has been allocated. Currently value between 0-255 indicating some resources have been allocated. Of course I can use
0xffffffffto indicate that if it were u32. But it wouldn't make much difference, would it?
It is fine by me - I would not have written it this way but it does not matter.
Quoted 3 lines
It would take 32 bytes for total 256 child slots in bitmap, while it only takes 4 bytes using int. It is not in critical path so scanning from the location one by one is not a big deal.
I suggested a bitmap for correctness - a slot number may include slots that as far as I can read the code failed get_pcichild_slot().
It is not clear what this patch is doing in this loop, that's certain.
Quoted 50 lines
Quoted 50 lines
drivers/pci/controller/pci-hyperv.c static int hv_send_resources_allocated(struct
Quoted 6 lines
drivers/pci/controller/pci-hyperv.c +1
-0+/* hypercall arg, must not cross page boundary */ struct hv_retarget_device_interrupt retarget_msi_interrupt_params;@@ -2847,7 +2850,7 @@ static int hv_send_resources_allocated(struct
hv_device*hdev)drivers/pci/controller/pci-hyperv.c static int hv_send_resources_allocated(struct hv_device *hdev) · line 2856
Quoted 8 lines
drivers/pci/controller/pci-hyperv.c +1
-1struct hv_pci_dev *hpdev;struct pci_packet *pkt;size_t size_res; -u32 wslot; +int wslot;int ret; size_res = (hbus->protocol_version <PCI_PROTOCOL_VERSION_1_2 )@@
Quoted 1 line
-2900,6 +2903,8 @@ static int hv_send_resources_allocated(struct
hv_device*hdev)
drivers/pci/controller/pci-hyperv.c static int hv_send_resources_released(struct
Quoted 9 lines
drivers/pci/controller/pci-hyperv.c +2
-0comp_pkt.completion_status);break ; } + + hbus->wslot_res_allocated = wslot; }kfree (pkt);@@ -2918,10 +2923,10 @@ static int hv_send_resources_released(struct
hv_device*hdev)drivers/pci/controller/pci-hyperv.c static int hv_send_resources_released(struct
Quoted 13 lines
drivers/pci/controller/pci-hyperv.c +2
-2struct hv_pcibus_device *hbus =hv_get_drvdata (hdev);struct pci_child_message pkt;struct hv_pci_dev *hpdev; -u32 wslot; +int wslot;int ret; -for (wslot =0 ; wslot <256 ; wslot++) { +for (wslot = hbus->wslot_res_allocated; wslot >=0 ; wslot--) { hpdev =get_pcichild_wslot (hbus, wslot);if (!hpdev)continue ;@@ -2936,8 +2941,12 @@ static int hv_send_resources_released(struct
hv_device*hdev)drivers/pci/controller/pci-hyperv.c static int hv_send_resources_released(struct hv_device *hdev) · line 2945
Quoted 5 lines
drivers/pci/controller/pci-hyperv.c +2
-0VM_PKT_DATA_INBAND, 0);if (ret)return ret; + + hbus->wslot_res_allocated = wslot -1 ;Do you really need to set it at every loop iteration ?
Quoted 7 lines
Quoted 3 lines
Moreover, I think a bitmap is better suited for what you are doing, given that you may skip some loop indexes on !hpdev.
The value is set in the loop whenever a resource was successfully released. It could happen that it failed in the next iteration so the last one which had succeeded would be recorded in this variable. It is needed at the end of loop when this iteration succeeds.
Ok understood.
Quoted 3 lines
Once again since it is not in the critical path, just using an signed integer is straightforward, less error prone and a bit easier to maintain than bitmap in my opinion. 😊
Less error prone, not sure, see above - it is your code so you choose but please explain this change better, it is not obvious.
Quoted 35 lines
Quoted 27 lines
Quoted 27 lines
drivers/pci/controller/pci-hyperv.c +3
-0} + hbus->wslot_res_allocated =-1 ; +return 0 ; }@@ -3037,6 +3046,7 @@ static int hv_pci_probe(struct hv_device *hdev,if (!hbus)return -ENOMEM ; hbus->state = hv_pcibus_init; + hbus->wslot_res_allocated =-1 ;/* * The PCI bus "domain" is what is called "segment" in ACPI and other @@ -3136,7 +3146,7 @@ static int hv_pci_probe(struct
hv_device*hdev,drivers/pci/controller/pci-hyperv.c +1
-1ret =hv_pci_allocate_bridge_windows (hbus);if (ret) - goto free_irq_domain; + goto exit_d0; ret =hv_send_resources_allocated (hdev);if (ret)@@ -3154,6 +3164,8 @@ static int hv_pci_probe(struct hv_device *hdev,Quoted 7 lines
drivers/pci/controller/pci-hyperv.c static int hv_pci_probe(struct hv_device *hdev, · line 3168
Quoted 4 lines
drivers/pci/controller/pci-hyperv.c +2
-0free_windows:hv_pci_free_bridge_windows (hbus); +exit_d0: + (void )hv_pci_bus_exit (hdev, true);Remove the (void) cast.
Quoted 3 lines
Some tools (maybe lint?) could generate error/warning messages assuming the code fails to check the return value without such cast. Leaving the cast in place just indicates that the return value is deliberately ignored.
I understand that - the question is why it is OK to ignore it in this specific case.
Maybe adding a wrapper around hv_pci_bus_exit() can help, I am fine with it, just trying to help.
Lorenzo
Wei Hu
maintainerThanks for your email. See my comments inline.
Quoted 39 lines
-----Original Message-----
From: Lorenzo Pieralisi <[email protected]>
Sent: Wednesday, May 6, 2020 7:10 PM
To: Wei Hu <[email protected]>
Cc: KY Srinivasan <[email protected]>; Haiyang Zhang <[email protected]>; Stephen Hemminger <[email protected]>; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]; linux- [email protected]; Dexuan Cui <[email protected]>; Michael Kelley <[email protected]>
Subject: Re: [PATCH v2 1/2] PCI: hv: Fix the PCI HyperV probe failure path to release resource properlyQuoted 23 lines · Wei Hu
Hi Lorenzo,
Thanks for your review. Please see my comments inline.
Quoted 19 lines
-----Original Message-----
From: Lorenzo Pieralisi <[email protected]>
Sent: Tuesday, May 5, 2020 11:03 PM
To: Wei Hu <[email protected]>
Cc: KY Srinivasan <[email protected]>; Haiyang Zhang <[email protected]>; Stephen Hemminger <[email protected]>; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]; linux- [email protected]; Dexuan Cui <[email protected]>; Michael Kelley <[email protected]>
Subject: Re: [PATCH v2 1/2] PCI: hv: Fix the PCI HyperV probe failure path to release resource properlyQuoted 2 lines · Wei Hu
Some error cases in
hv_pci_probe()were not handled. Fix these error paths to release the resourses and clean up the state properly.This patch does more than that. It adds a variable to store the number of slots actually allocated - I presume to free only allocated on slots
on the exit path.
Quoted 1 line
Quoted 1 line
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Two patches required I am afraid.
Well, adding this variable is needed to make the call of "(void)
hv_pci_bus_exit(hdev, true)"I don't understand why - it is not clear from the commit log and the code, please explain it since it is not obvious.
Hv_pci_bus_exit() calls hv_send_resources_released() to release all child resources. These child resources were allocated in hv_send_resources_allocated(). Hv_send_resources_allocated() could fail in the middle, leaving some child resources allocated and rest not. Without adding this variable to record the highest slot number that resource has been successfully allocated, calling hv_send_resources_released() could cause spurious resource release requests being sent to hypervisor.
This had been fine since hv_pci_bus_exit() was never called in error path before this patch was introduced. To add this call to clean the pci state in the error path, we need to know the starting point in child device that resource has not been allocated. Hence this variable
is used in hv_send_resources_allocated() to record this point and in hv_send_resource_released() to start deallocating child resources.
I can add to the commit log if you are fine with this explanation.
Quoted 35 lines
Quoted 2 lines
at the end to work and clean up the PCI state in the failure path. Just adding this variable would not make any changes. So I think it may be
better to put them in single patch?
Quoted 21 lines
Quoted 21 lines
Quoted 13 lines
Signed-off-by Wei Hu <[email protected]>--- drivers/pci/controller/pci-hyperv.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)drivers/pci/controller/pci-hyperv.c +0
-0drivers/pci/controller/pci-hyperv.c struct hv_pcibus_device { · line 484
Quoted 5 lines
drivers/pci/controller/pci-hyperv.c +2
-0@@ -480,6 +480,9 @@ struct hv_pcibus_device {struct workqueue_struct *wq; +/* Highest slot of child device with resources allocated */ +int wslot_res_allocated;I don't understand why you need an int rather than a u32.
Quoted 7 lines
Quoted 2 lines
Furthermore, I think a bitmap is more appropriate for what this variable is used for.
So it can use a negative value (-1 in this case) to indicated none of any resources has been allocated. Currently value between 0-255 indicating some resources have been allocated. Of course I can use
0xffffffffto indicate that if it were u32. But it wouldn't make much difference,would it?
It is fine by me - I would not have written it this way but it does not matter.
Quoted 9 lines
Quoted 3 lines
It would take 32 bytes for total 256 child slots in bitmap, while it only takes 4 bytes using int. It is not in critical path so scanning from the location one by one is not a big deal.
I suggested a bitmap for correctness - a slot number may include slots that as far as I can read the code failed
get_pcichild_slot().It is not clear what this patch is doing in this loop, that's certain.
Get_pcichild_wslot() just tells if a pci child device exists under this slot number. If there is no child device, it just continue to look on the next slot number. If it does exists, the code sends request to hypervisor for resource allocation. If it succeeds, we update the wslot_res_allocated accordingly.
I hope this along with the explanation earlier makes it clear.
Quoted 65 lines
Quoted 55 lines
Quoted 55 lines
Quoted 8 lines
drivers/pci/controller/pci-hyperv.c +1
-0+/* hypercall arg, must not cross page boundary */ struct hv_retarget_device_interrupt retarget_msi_interrupt_params;@@ -2847,7 +2850,7 @@ static inthv_send_resources_allocated(struct
hv_device*hdev)drivers/pci/controller/pci-hyperv.c static int hv_send_resources_allocated(struct hv_device *hdev) · line 2856
Quoted 8 lines
drivers/pci/controller/pci-hyperv.c +1
-1struct hv_pci_dev *hpdev;struct pci_packet *pkt;size_t size_res; -u32 wslot; +int wslot;int ret; size_res = (hbus->protocol_version <PCI_PROTOCOL_VERSION_1_2 )@@
Quoted 2 lines
-2900,6 +2903,8 @@ static int hv_send_resources_allocated(struct
hv_device*hdev)
Quoted 10 lines
drivers/pci/controller/pci-hyperv.c +2
-0comp_pkt.completion_status);break ; } + + hbus->wslot_res_allocated = wslot; }kfree (pkt);@@ -2918,10 +2923,10 @@ static inthv_send_resources_released(struct
hv_device*hdev)Quoted 13 lines
drivers/pci/controller/pci-hyperv.c +2
-2struct hv_pcibus_device *hbus =hv_get_drvdata (hdev);struct pci_child_message pkt;struct hv_pci_dev *hpdev; -u32 wslot; +int wslot;int ret; -for (wslot =0 ; wslot <256 ; wslot++) { +for (wslot = hbus->wslot_res_allocated; wslot >=0 ; wslot--) { hpdev =get_pcichild_wslot (hbus, wslot);if (!hpdev)continue ;@@ -2936,8 +2941,12 @@ static intQuoted 1 line
hv_send_resources_released(struct
hv_device*hdev)drivers/pci/controller/pci-hyperv.c static int hv_send_resources_released(struct hv_device *hdev) · line 2945
Quoted 5 lines
drivers/pci/controller/pci-hyperv.c +2
-0VM_PKT_DATA_INBAND, 0);if (ret)return ret; + + hbus->wslot_res_allocated = wslot -1 ;Do you really need to set it at every loop iteration ?
Quoted 7 lines
Quoted 3 lines
Moreover, I think a bitmap is better suited for what you are doing, given that you may skip some loop indexes on !hpdev.
The value is set in the loop whenever a resource was successfully released. It could happen that it failed in the next iteration so the last one which had succeeded would be recorded in this variable. It is needed at the end of loop when this iteration succeeds.
Ok understood.
Quoted 7 lines
Quoted 4 lines
Once again since it is not in the critical path, just using an signed integer is straightforward, less error prone and a bit easier to maintain than bitmap in my opinion. 😊
Less error prone, not sure, see above - it is your code so you choose but please explain this change better, it is not obvious.
I hope the explanations earlier make it a little better to understand.
Quoted 37 lines
Quoted 37 lines
Quoted 36 lines
Quoted 27 lines
drivers/pci/controller/pci-hyperv.c +3
-0} + hbus->wslot_res_allocated =-1 ; +return 0 ; }@@ -3037,6 +3046,7 @@ static int hv_pci_probe(struct hv_device *hdev,if (!hbus)return -ENOMEM ; hbus->state = hv_pcibus_init; + hbus->wslot_res_allocated =-1 ;/* * The PCI bus "domain" is what is called "segment" in ACPI and other @@ -3136,7 +3146,7 @@ static int hv_pci_probe(struct
hv_device*hdev,drivers/pci/controller/pci-hyperv.c +1
-1ret =hv_pci_allocate_bridge_windows (hbus);if (ret) - goto free_irq_domain; + goto exit_d0; ret =hv_send_resources_allocated (hdev);if (ret)drivers/pci/controller/pci-hyperv.c static int hv_pci_probe(struct hv_device · line 3169
Quoted 6 lines
drivers/pci/controller/pci-hyperv.c +2
-0@@ -3154,6 +3164,8 @@ static int hv_pci_probe(struct hv_device *hdev, free_windows:hv_pci_free_bridge_windows (hbus); +exit_d0: + (void )hv_pci_bus_exit (hdev, true);Remove the (void) cast.
Quoted 7 lines
Quoted 3 lines
Some tools (maybe lint?) could generate error/warning messages assuming the code fails to check the return value without such cast. Leaving the cast in place just indicates that the return value is deliberately
ignored.
I understand that - the question is why it is OK to ignore it in this specific case.
Since it is already in the error path, checking the return value is not necessary. The earlier failure point is more important to be returned to the caller.
Quoted 2 lines
Maybe adding a wrapper around
hv_pci_bus_exit()can help, I am fine with it, just trying to help.
Do you mean making the (void) cast in wrapper function? Or checking the return value in the wrapper function and ignore it? Either way I think it might not be necessary.
Thanks,
Wei
From: Wei Hu <[email protected]> Sent: Wednesday, May 6, 2020 6:22 AM
Quoted 62 lines
Quoted 39 lines
-----Original Message-----
From: Lorenzo Pieralisi <[email protected]>
Sent: Wednesday, May 6, 2020 7:10 PM
To: Wei Hu <[email protected]>
Cc: KY Srinivasan <[email protected]>; Haiyang Zhang <[email protected]>; Stephen Hemminger <[email protected]>; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]; linux- [email protected]; Dexuan Cui <[email protected]>; Michael Kelley <[email protected]>
Subject: Re: [PATCH v2 1/2] PCI: hv: Fix the PCI HyperV probe failure path to release resource properlyQuoted 23 lines · Wei Hu
Hi Lorenzo,
Thanks for your review. Please see my comments inline.
Quoted 19 lines
-----Original Message-----
From: Lorenzo Pieralisi <[email protected]>
Sent: Tuesday, May 5, 2020 11:03 PM
To: Wei Hu <[email protected]>
Cc: KY Srinivasan <[email protected]>; Haiyang Zhang <[email protected]>; Stephen Hemminger <[email protected]>; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]; linux- [email protected]; Dexuan Cui <[email protected]>; Michael Kelley <[email protected]>
Subject: Re: [PATCH v2 1/2] PCI: hv: Fix the PCI HyperV probe failure path to release resource properlyQuoted 2 lines · Wei Hu
Some error cases in
hv_pci_probe()were not handled. Fix these error paths to release the resourses and clean up the state properly.This patch does more than that. It adds a variable to store the number of slots actually allocated - I presume to free only allocated on slots
on the exit path.
Quoted 1 line
Quoted 1 line
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Two patches required I am afraid.
Well, adding this variable is needed to make the call of "(void)
hv_pci_bus_exit(hdev, true)"I don't understand why - it is not clear from the commit log and the code, please explain it since it is not obvious.
Hv_pci_bus_exit()callshv_send_resources_released()to release all child resources. These child resources were allocated inhv_send_resources_allocated().Hv_send_resources_allocated()could fail in the middle, leaving some child resources allocated and rest not. Without adding this variable to record the highest slot number that resource has been successfully allocated, callinghv_send_resources_released()could cause spurious resource release requests being sent to hypervisor.This had been fine since
hv_pci_bus_exit()was never called in error path before this patch was
introduced. To add this call to clean the pci state in the error path, we need to know the starting
point in child device that resource has not been allocated. Hence this variable is used inhv_send_resources_allocated()to record this point and inhv_send_resource_released()to start deallocating child resources.
Quoted 2 lines
I can add to the commit log if you are fine with this explanation.
FWIW, I think of this patch as follows:
In some error cases in hv_pci_probe(), allocated resources are not freed. Fix this by adding a field to keep track of the high water mark for slots that have resources allocated to them. In case of an error, this high water mark is used to know which slots have resources that must be released. Since slots are numbered starting with zero, a value of -1 indicates no slots have been allocated resources. There may be unused slots in the range between slot 0 and the high water mark slot, but these slots are already ignored by the existing code in the allocate and release loops with the call to get_pcichild_wslot().
Michael
On Wed, May 06, 2020 at 02:55:17PM +0000, Michael Kelley wrote:
[...]
Quoted 20 lines
Quoted 15 lines
Hv_pci_bus_exit()callshv_send_resources_released()to release all child resources. These child resources were allocated inhv_send_resources_allocated().Hv_send_resources_allocated()could fail in the middle, leaving some child resources allocated and rest not. Without adding this variable to record the highest slot number that resource has been successfully allocated, callinghv_send_resources_released()could cause spurious resource release requests being sent to hypervisor.This had been fine since
hv_pci_bus_exit()was never called in error path before this patch was
introduced. To add this call to clean the pci state in the error path, we need to know the starting
point in child device that resource has not been allocated. Hence this variable is used inhv_send_resources_allocated()to record this point and inhv_send_resource_released()to start deallocating child resources.Quoted 2 lines
I can add to the commit log if you are fine with this explanation.
FWIW, I think of this patch as follows:
Quoted 9 lines
In some error cases in
hv_pci_probe(), allocated resources are not freed. Fix this by adding a field to keep track of the high water mark for slots that have resources allocated to them. In case of an error, this high water mark is used to know which slots have resources that must be released. Since slots are numbered starting with zero, a value of -1 indicates no slots have been allocated resources. There may be unused slots in the range between slot 0 and the high water mark slot, but these slots are already ignored by the existing code in the allocate and release loops with the call toget_pcichild_wslot().
That's much clearer now - please add these bits of info to the commit log, it is essential that developers can find an explanation for a change like this one there IMO.
Overall the code changes are fine, I am not a big fan of the (void) cast (I don't think error codes should be ignored) but it is acceptable, in this context.
Thank you for taking some time to review the code together.
Lorenzo