From: Florian Fainelli <florian.fainelli@broadcom.com>
To: Simon Horman <horms@kernel.org>, Jinjie Ruan <ruanjinjie@huawei.com>
Cc: netdev@vger.kernel.org, Andrew Lunn <andrew@lunn.ch>,
Vladimir Oltean <olteanv@gmail.com>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@google.com>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Vivien Didelot <vivien.didelot@gmail.com>
Subject: Re: [PATCH v2] net: dsa: bcm_sf2: Fix possible memory leak in bcm_sf2_mdio_register()
Date: Tue, 10 Oct 2023 11:18:22 -0700 [thread overview]
Message-ID: <a5a79bb4-4d0c-437d-aee4-27953089defb@broadcom.com> (raw)
In-Reply-To: <20231010180522.GB1003866@kernel.org>
[-- Attachment #1: Type: text/plain, Size: 2716 bytes --]
On 10/10/23 11:05, Simon Horman wrote:
> On Mon, Oct 09, 2023 at 04:39:06PM +0800, Jinjie Ruan wrote:
>> In bcm_sf2_mdio_register(), the class_find_device() will call get_device()
>> to increment reference count for priv->master_mii_bus->dev if
>> of_mdio_find_bus() succeeds. If mdiobus_alloc() or mdiobus_register()
>> fails, it will call get_device() twice without decrement reference count
>> for the device. And it is the same if bcm_sf2_mdio_register() succeeds but
>> fails in bcm_sf2_sw_probe(), or if bcm_sf2_sw_probe() succeeds. If the
>> reference count has not decremented to zero, the dev related resource will
>> not be freed.
>>
>> So remove the get_device() in bcm_sf2_mdio_register(), and call
>> put_device() if mdiobus_alloc() or mdiobus_register() fails and in
>> bcm_sf2_mdio_unregister() to solve the issue.
>>
>> Fixes: 461cd1b03e32 ("net: dsa: bcm_sf2: Register our slave MDIO bus")
>> Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
>
> Hi Jinjie Ruan,
>
> I agree with your analysis here, but I wonder if it would be nicer
> to move bcm_sf2_mdio_register() to a more idiomatic way of unwinding
> from errors.
That would appear a tad cleaner, yes!
>
> Something like this (compile tested only!)
>
> diff --git a/drivers/net/dsa/bcm_sf2.c b/drivers/net/dsa/bcm_sf2.c
> index 0b62bd78ac50..037ce118ee00 100644
> --- a/drivers/net/dsa/bcm_sf2.c
> +++ b/drivers/net/dsa/bcm_sf2.c
> @@ -617,8 +617,8 @@ static int bcm_sf2_mdio_register(struct dsa_switch *ds)
> dn = of_find_compatible_node(NULL, NULL, "brcm,unimac-mdio");
> priv->master_mii_bus = of_mdio_find_bus(dn);
> if (!priv->master_mii_bus) {
> - of_node_put(dn);
> - return -EPROBE_DEFER;
> + err = -EPROBE_DEFER;
> + goto err_of_node_put;
> }
>
> get_device(&priv->master_mii_bus->dev);
> @@ -626,8 +626,8 @@ static int bcm_sf2_mdio_register(struct dsa_switch *ds)
>
> priv->slave_mii_bus = mdiobus_alloc();
> if (!priv->slave_mii_bus) {
> - of_node_put(dn);
> - return -ENOMEM;
> + err = -ENOMEM;
> + goto err_put_master_mii_bus_device;
> }
>
> priv->slave_mii_bus->priv = priv;
> @@ -684,12 +684,18 @@ static int bcm_sf2_mdio_register(struct dsa_switch *ds)
> }
>
> err = mdiobus_register(priv->slave_mii_bus);
> - if (err && dn) {
> - mdiobus_free(priv->slave_mii_bus);
> - of_node_put(dn);
> - }
> + if (err && dn)
> + goto err_free_slave_mii_bus;
>
> return err;
> +
> +err_free_slave_mii_bus:
> + mdiobus_free(priv->slave_mii_bus);
> +err_put_master_mii_bus_device:
> + put_device(&priv->master_mii_bus->dev);
> +err_of_node_put:
> + of_node_put(dn);
> + return err;
> }
>
> static void bcm_sf2_mdio_unregister(struct bcm_sf2_priv *priv)
--
Florian
[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 4221 bytes --]
prev parent reply other threads:[~2023-10-10 18:18 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-09 8:39 [PATCH v2] net: dsa: bcm_sf2: Fix possible memory leak in bcm_sf2_mdio_register() Jinjie Ruan
2023-10-10 18:05 ` Simon Horman
2023-10-10 18:18 ` Florian Fainelli [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=a5a79bb4-4d0c-437d-aee4-27953089defb@broadcom.com \
--to=florian.fainelli@broadcom.com \
--cc=andrew@lunn.ch \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=olteanv@gmail.com \
--cc=pabeni@redhat.com \
--cc=ruanjinjie@huawei.com \
--cc=vivien.didelot@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox