Subject: Re: [PATCH 1/3] net: dsa: Use devm_ prefixed allocations

From: Neil Armstrong <[email-masked]>
Date: Sat, 3 Oct 2015 15:55:14 +0200

On 10/03/2015 02:39 PM, Felix Fietkau wrote:
> On 2015-10-02 15:30, Neil Armstrong wrote:
>> On 10/02/2015 03:29 PM, Sergei Shtylyov wrote:
>>> On 10/2/2015 1:48 PM, Neil Armstrong wrote:
>>>
>>>> To simplify and prevent memory leakage when unbinding, use
>>>> the devm_ memory allocation calls.
>>>>
>>>> Tested-by: Andrew Lunn <[email-masked]>
>>>> Tested-by: Florian Fainelli <[email-masked]>
>>>> Signed-off-by: Neil Armstrong <[email-masked]>
>>>> ---
>>>>   net/dsa/dsa.c | 6 +++---
>>>>   1 file changed, 3 insertions(+), 3 deletions(-)
>>>>
>>>> diff --git a/net/dsa/dsa.c b/net/dsa/dsa.c
>>>> index c59fa5d..98f94c2 100644
>>>> --- a/net/dsa/dsa.c
>>>> +++ b/net/dsa/dsa.c
>>>> @@ -305,7 +305,7 @@ static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
>>>>       if (ret < 0)
>>>>           goto out;
>>>>
>>>> -    ds->slave_mii_bus = mdiobus_alloc();
>>>> +    ds->slave_mii_bus = devm_mdiobus_alloc(parent);
>>>>       if (ds->slave_mii_bus == NULL) {
>>>>           ret = -ENOMEM;
>>>>           goto out;
>>>> @@ -400,7 +400,7 @@ dsa_switch_setup(struct dsa_switch_tree *dst, int index,
>>>>       /*
>>>>        * Allocate and initialise switch state.
>>>>        */
>>>> -    ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL);
>>>> +    ds = devm_kzalloc(parent, sizeof(*ds) + drv->priv_size, GFP_KERNEL);
>>>>       if (ds == NULL)
>>>>           return ERR_PTR(-ENOMEM);
>>>>
>>>> @@ -883,7 +883,7 @@ static int dsa_probe(struct platform_device *pdev)
>>>>           goto out;
>>>>       }
>>>>
>>>> -    dst = kzalloc(sizeof(*dst), GFP_KERNEL);
>>>> +    dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL);
>>>>       if (dst == NULL) {
>>>>           dev_put(dev);
>>>>           ret = -ENOMEM;
>>>>
>>>
>>>    Shouldn't you remove the correspoding kfree(), etc. calls?
>>>
>>> MBR, Sergei
>>>
>> The corresponding kfree() calls were all missing.
> Not in the error handling path. mdiobus_alloc has a corresponding
> mdiobus_free in the same function.
> The ds kzalloc in dsa_switch_setup has a kfree in dsa_switch_setup_one.
> 
> - Felix
> 
Thanks Felix & Sergei,

I will repost based on net with an intermediate commit adding the missing kfree calls.

Neil