[PATCH v2] spmi: hisi-spmi-controller: manage the OF node reference in device initialization and cleanup

Joe Hattori posted 1 patch 1 year, 1 month ago
There is a newer version of this series
drivers/spmi/hisi-spmi-controller.c | 1 -
drivers/spmi/spmi-pmic-arb.c        | 1 -
drivers/spmi/spmi.c                 | 3 ++-
3 files changed, 2 insertions(+), 3 deletions(-)
[PATCH v2] spmi: hisi-spmi-controller: manage the OF node reference in device initialization and cleanup
Posted by Joe Hattori 1 year, 1 month ago
spmi_controller_probe() increments the refcount of an OF node, but does
not release it. Instead, call of_node_get() in spmi_controller_alloc()
and release it in spmi_ctrl_release() to avoid the reference leak. Also
remove the lines in spmi_pmic_arb_bus_init() and spmi_controller_probe()
where a pdev's of_node is stored after spmi_controller_alloc() is
called.

This bug was found by an experimental static analysis tool that I am
developing.

Fixes: e562cf3aea3e ("spmi: hisi-spmi-controller: move driver from staging")
Signed-off-by: Joe Hattori <joe@pf.is.s.u-tokyo.ac.jp>
---
Changes in v2:
- Remove the store to of_node in spmi_pmic_arb_bus_init().
---
 drivers/spmi/hisi-spmi-controller.c | 1 -
 drivers/spmi/spmi-pmic-arb.c        | 1 -
 drivers/spmi/spmi.c                 | 3 ++-
 3 files changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/spmi/hisi-spmi-controller.c b/drivers/spmi/hisi-spmi-controller.c
index 3cafdf22c909..dd21c5d1ca83 100644
--- a/drivers/spmi/hisi-spmi-controller.c
+++ b/drivers/spmi/hisi-spmi-controller.c
@@ -301,7 +301,6 @@ static int spmi_controller_probe(struct platform_device *pdev)
 	spin_lock_init(&spmi_controller->lock);
 
 	ctrl->dev.parent = pdev->dev.parent;
-	ctrl->dev.of_node = of_node_get(pdev->dev.of_node);
 
 	/* Callbacks */
 	ctrl->read_cmd = spmi_read_cmd;
diff --git a/drivers/spmi/spmi-pmic-arb.c b/drivers/spmi/spmi-pmic-arb.c
index 5c058db21821..73f2f19737f8 100644
--- a/drivers/spmi/spmi-pmic-arb.c
+++ b/drivers/spmi/spmi-pmic-arb.c
@@ -1746,7 +1746,6 @@ static int spmi_pmic_arb_bus_init(struct platform_device *pdev,
 	irq_set_chained_handler_and_data(bus->irq,
 					 pmic_arb_chained_irq, bus);
 
-	ctrl->dev.of_node = node;
 	dev_set_name(&ctrl->dev, "spmi-%d", bus_index);
 
 	ret = devm_spmi_controller_add(dev, ctrl);
diff --git a/drivers/spmi/spmi.c b/drivers/spmi/spmi.c
index fb0101da1485..e662a7a78df2 100644
--- a/drivers/spmi/spmi.c
+++ b/drivers/spmi/spmi.c
@@ -36,6 +36,7 @@ static void spmi_ctrl_release(struct device *dev)
 	struct spmi_controller *ctrl = to_spmi_controller(dev);
 
 	ida_free(&ctrl_ida, ctrl->nr);
+	of_node_put(dev->of_node);
 	kfree(ctrl);
 }
 
@@ -458,7 +459,7 @@ struct spmi_controller *spmi_controller_alloc(struct device *parent,
 	ctrl->dev.type = &spmi_ctrl_type;
 	ctrl->dev.bus = &spmi_bus_type;
 	ctrl->dev.parent = parent;
-	ctrl->dev.of_node = parent->of_node;
+	ctrl->dev.of_node = of_node_get(parent->of_node);
 	spmi_controller_set_drvdata(ctrl, &ctrl[1]);
 
 	id = ida_alloc(&ctrl_ida, GFP_KERNEL);
-- 
2.34.1
Re: [PATCH v2] spmi: hisi-spmi-controller: manage the OF node reference in device initialization and cleanup
Posted by Stephen Boyd 1 year, 1 month ago
Quoting Joe Hattori (2024-12-17 19:39:54)
> spmi_controller_probe() increments the refcount of an OF node, but does
> not release it. Instead, call of_node_get() in spmi_controller_alloc()
> and release it in spmi_ctrl_release() to avoid the reference leak. Also
> remove the lines in spmi_pmic_arb_bus_init() and spmi_controller_probe()
> where a pdev's of_node is stored after spmi_controller_alloc() is
> called.

That last sentence says what is done but doesn't explain why. Please
explain why stashing the of_node in the two drivers are removed.

> 
> This bug was found by an experimental static analysis tool that I am
> developing.
> 
> Fixes: e562cf3aea3e ("spmi: hisi-spmi-controller: move driver from staging")
> Signed-off-by: Joe Hattori <joe@pf.is.s.u-tokyo.ac.jp>
[...]
> diff --git a/drivers/spmi/spmi.c b/drivers/spmi/spmi.c
> index fb0101da1485..e662a7a78df2 100644
> --- a/drivers/spmi/spmi.c
> +++ b/drivers/spmi/spmi.c
> @@ -36,6 +36,7 @@ static void spmi_ctrl_release(struct device *dev)
>         struct spmi_controller *ctrl = to_spmi_controller(dev);
>  
>         ida_free(&ctrl_ida, ctrl->nr);
> +       of_node_put(dev->of_node);
>         kfree(ctrl);
>  }
>  
> @@ -458,7 +459,7 @@ struct spmi_controller *spmi_controller_alloc(struct device *parent,
>         ctrl->dev.type = &spmi_ctrl_type;
>         ctrl->dev.bus = &spmi_bus_type;
>         ctrl->dev.parent = parent;
> -       ctrl->dev.of_node = parent->of_node;
> +       ctrl->dev.of_node = of_node_get(parent->of_node);

Do we need to use device_set_node() here?

>         spmi_controller_set_drvdata(ctrl, &ctrl[1]);
>
Re: [PATCH v2] spmi: hisi-spmi-controller: manage the OF node reference in device initialization and cleanup
Posted by Joe Hattori 1 year, 1 month ago
Thank you for your review.

On 12/19/24 04:49, Stephen Boyd wrote:
> Quoting Joe Hattori (2024-12-17 19:39:54)
>> spmi_controller_probe() increments the refcount of an OF node, but does
>> not release it. Instead, call of_node_get() in spmi_controller_alloc()
>> and release it in spmi_ctrl_release() to avoid the reference leak. Also
>> remove the lines in spmi_pmic_arb_bus_init() and spmi_controller_probe()
>> where a pdev's of_node is stored after spmi_controller_alloc() is
>> called.
> 
> That last sentence says what is done but doesn't explain why. Please
> explain why stashing the of_node in the two drivers are removed.

Addressed in the v3 patch.

> 
>>
>> This bug was found by an experimental static analysis tool that I am
>> developing.
>>
>> Fixes: e562cf3aea3e ("spmi: hisi-spmi-controller: move driver from staging")
>> Signed-off-by: Joe Hattori <joe@pf.is.s.u-tokyo.ac.jp>
> [...]
>> diff --git a/drivers/spmi/spmi.c b/drivers/spmi/spmi.c
>> index fb0101da1485..e662a7a78df2 100644
>> --- a/drivers/spmi/spmi.c
>> +++ b/drivers/spmi/spmi.c
>> @@ -36,6 +36,7 @@ static void spmi_ctrl_release(struct device *dev)
>>          struct spmi_controller *ctrl = to_spmi_controller(dev);
>>   
>>          ida_free(&ctrl_ida, ctrl->nr);
>> +       of_node_put(dev->of_node);
>>          kfree(ctrl);
>>   }
>>   
>> @@ -458,7 +459,7 @@ struct spmi_controller *spmi_controller_alloc(struct device *parent,
>>          ctrl->dev.type = &spmi_ctrl_type;
>>          ctrl->dev.bus = &spmi_bus_type;
>>          ctrl->dev.parent = parent;
>> -       ctrl->dev.of_node = parent->of_node;
>> +       ctrl->dev.of_node = of_node_get(parent->of_node);
> 
> Do we need to use device_set_node() here?

Yes, setting also the fwnode sounds cleaner. Applied in the v3 patch.

> 
>>          spmi_controller_set_drvdata(ctrl, &ctrl[1]);
>>

Best,
Joe