From: "Russell King (Oracle)" <linux@armlinux.org.uk>
To: Serge Semin <fancer.lancer@gmail.com>
Cc: Jose Abreu <Jose.Abreu@synopsys.com>,
Andrew Lunn <andrew@lunn.ch>, Conor Dooley <conor+dt@kernel.org>,
Tomer Maimon <tmaimon77@gmail.com>,
devicetree@vger.kernel.org, netdev@vger.kernel.org,
openbmc@lists.ozlabs.org,
Alexandre Torgue <alexandre.torgue@foss.st.com>,
Rob Herring <robh+dt@kernel.org>,
Maxime Chevallier <maxime.chevallier@bootlin.com>,
Eric Dumazet <edumazet@google.com>,
Jose Abreu <joabreu@synopsys.com>,
Krzysztof Kozlowski <krzysztof.kozlowski+dt@linaro.org>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
"David S. Miller" <davem@davemloft.net>,
linux-kernel@vger.kernel.org,
Heiner Kallweit <hkallweit1@gmail.com>
Subject: Re: [PATCH net-next 06/16] net: pcs: xpcs: Avoid creating dummy XPCS MDIO device
Date: Tue, 5 Dec 2023 13:46:44 +0000 [thread overview]
Message-ID: <ZW8pxM3RvyHJTwqH@shell.armlinux.org.uk> (raw)
In-Reply-To: <20231205103559.9605-7-fancer.lancer@gmail.com>
On Tue, Dec 05, 2023 at 01:35:27PM +0300, Serge Semin wrote:
> If the DW XPCS MDIO devices are either left unmasked for being auto-probed
> or explicitly registered in the MDIO subsystem by means of the
> mdiobus_register_board_info() method there is no point in creating the
> dummy MDIO device instance in order to get the DW XPCS handler since the
> MDIO core subsystem will create the device during the MDIO bus
> registration procedure. All what needs to be done is to just reuse the
> MDIO-device instance available in the mii_bus.mdio_map array (using some
> getter for it would look better though). It shall prevent the XPCS devices
> been accessed over several MDIO-device instances.
>
> Note since the MDIO-device instance might be retrieved from the MDIO-bus
> map array its reference counter shall be increased. If the MDIO-device
> instance is created in the xpcs_create_mdiodev() method its reference
> counter will be already increased. So there is no point in toggling the
> reference counter in the xpcs_create() function. Just drop it from there.
>
> Signed-off-by: Serge Semin <fancer.lancer@gmail.com>
> ---
> drivers/net/pcs/pcs-xpcs.c | 26 +++++++++++++-------------
> 1 file changed, 13 insertions(+), 13 deletions(-)
>
> diff --git a/drivers/net/pcs/pcs-xpcs.c b/drivers/net/pcs/pcs-xpcs.c
> index 2850122f354a..a53376472394 100644
> --- a/drivers/net/pcs/pcs-xpcs.c
> +++ b/drivers/net/pcs/pcs-xpcs.c
> @@ -1376,7 +1376,6 @@ static struct dw_xpcs *xpcs_create(struct mdio_device *mdiodev,
> if (!xpcs)
> return ERR_PTR(-ENOMEM);
>
> - mdio_device_get(mdiodev);
> xpcs->mdiodev = mdiodev;
>
> xpcs_id = xpcs_get_id(xpcs);
> @@ -1417,7 +1416,6 @@ static struct dw_xpcs *xpcs_create(struct mdio_device *mdiodev,
> ret = -ENODEV;
>
> out:
> - mdio_device_put(mdiodev);
> kfree(xpcs);
>
> return ERR_PTR(ret);
The above two hunks are a completely Unnecessary change.
> @@ -1437,19 +1435,21 @@ struct dw_xpcs *xpcs_create_mdiodev(struct mii_bus *bus, int addr,
> struct mdio_device *mdiodev;
> struct dw_xpcs *xpcs;
>
> - mdiodev = mdio_device_create(bus, addr);
> - if (IS_ERR(mdiodev))
> - return ERR_CAST(mdiodev);
> + if (addr >= PHY_MAX_ADDR)
> + return ERR_PTR(-EINVAL);
>
> - xpcs = xpcs_create(mdiodev, interface);
> + if (mdiobus_is_registered_device(bus, addr)) {
> + mdiodev = bus->mdio_map[addr];
> + mdio_device_get(mdiodev);
This is fine - taking a reference on the mdiodev you've got from
somewhere else is the right thing to do.
> + } else {
> + mdiodev = mdio_device_create(bus, addr);
> + if (IS_ERR(mdiodev))
> + return ERR_CAST(mdiodev);
> + }
>
> - /* xpcs_create() has taken a refcount on the mdiodev if it was
> - * successful. If xpcs_create() fails, this will free the mdio
> - * device here. In any case, we don't need to hold our reference
> - * anymore, and putting it here will allow mdio_device_put() in
> - * xpcs_destroy() to automatically free the mdio device.
> - */
> - mdio_device_put(mdiodev);
> + xpcs = xpcs_create(mdiodev, interface);
> + if (IS_ERR(xpcs))
> + mdio_device_put(mdiodev);
Without the change to xpcs_create() you don't need this change - and
this is why I say you don't understand refcounting.
The point here is that the refcounting management is in each function
where references are gained or lost.
xpcs_create() creates a new reference to the mdiodev by storing it in
the dw_xpcs structure. Therefore, it takes a reference to the mdiodev.
If something fails, it drops that reference to restore the refcount
as it was on function entry.
xpcs_create_mdiodev() as it originally stood creates the mdiodev from
the bus/address, and then passes that to xpcs_create(). Once
xpcs_create() has finished its work (irrespective of whether it was
successful or not) we're done with the mdiodev in this function, so
the reference is _always_ put.
For your use case, it would be:
mdiodev = bus->mdio_map[addr];
mdio_device_get(mdiodev);
xpcs = xpcs_create(mdiodev, interface);
mdio_device_put(mdiodev);
return xpcs;
which illustrates this point - we get a reference to the mdiodev by
reading it from the array. We do something (calling xpcs_create)
with it. If that something was successful, it takes its own refcount
otherwise leaves it as-is. We're then done with the mdiodev so we
drop the refcount we took.
There is no need to make the code more complicated by changing this,
so I regard the refcount changes in this patch to be wrong.
--
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
WARNING: multiple messages have this Message-ID (diff)
From: "Russell King (Oracle)" <linux@armlinux.org.uk>
To: Serge Semin <fancer.lancer@gmail.com>
Cc: Andrew Lunn <andrew@lunn.ch>,
Heiner Kallweit <hkallweit1@gmail.com>,
Alexandre Torgue <alexandre.torgue@foss.st.com>,
Jose Abreu <joabreu@synopsys.com>,
Jose Abreu <Jose.Abreu@synopsys.com>,
Maxime Chevallier <maxime.chevallier@bootlin.com>,
Tomer Maimon <tmaimon77@gmail.com>,
Rob Herring <robh+dt@kernel.org>,
Krzysztof Kozlowski <krzysztof.kozlowski+dt@linaro.org>,
Conor Dooley <conor+dt@kernel.org>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@google.com>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
openbmc@lists.ozlabs.org, netdev@vger.kernel.org,
devicetree@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH net-next 06/16] net: pcs: xpcs: Avoid creating dummy XPCS MDIO device
Date: Tue, 5 Dec 2023 13:46:44 +0000 [thread overview]
Message-ID: <ZW8pxM3RvyHJTwqH@shell.armlinux.org.uk> (raw)
In-Reply-To: <20231205103559.9605-7-fancer.lancer@gmail.com>
On Tue, Dec 05, 2023 at 01:35:27PM +0300, Serge Semin wrote:
> If the DW XPCS MDIO devices are either left unmasked for being auto-probed
> or explicitly registered in the MDIO subsystem by means of the
> mdiobus_register_board_info() method there is no point in creating the
> dummy MDIO device instance in order to get the DW XPCS handler since the
> MDIO core subsystem will create the device during the MDIO bus
> registration procedure. All what needs to be done is to just reuse the
> MDIO-device instance available in the mii_bus.mdio_map array (using some
> getter for it would look better though). It shall prevent the XPCS devices
> been accessed over several MDIO-device instances.
>
> Note since the MDIO-device instance might be retrieved from the MDIO-bus
> map array its reference counter shall be increased. If the MDIO-device
> instance is created in the xpcs_create_mdiodev() method its reference
> counter will be already increased. So there is no point in toggling the
> reference counter in the xpcs_create() function. Just drop it from there.
>
> Signed-off-by: Serge Semin <fancer.lancer@gmail.com>
> ---
> drivers/net/pcs/pcs-xpcs.c | 26 +++++++++++++-------------
> 1 file changed, 13 insertions(+), 13 deletions(-)
>
> diff --git a/drivers/net/pcs/pcs-xpcs.c b/drivers/net/pcs/pcs-xpcs.c
> index 2850122f354a..a53376472394 100644
> --- a/drivers/net/pcs/pcs-xpcs.c
> +++ b/drivers/net/pcs/pcs-xpcs.c
> @@ -1376,7 +1376,6 @@ static struct dw_xpcs *xpcs_create(struct mdio_device *mdiodev,
> if (!xpcs)
> return ERR_PTR(-ENOMEM);
>
> - mdio_device_get(mdiodev);
> xpcs->mdiodev = mdiodev;
>
> xpcs_id = xpcs_get_id(xpcs);
> @@ -1417,7 +1416,6 @@ static struct dw_xpcs *xpcs_create(struct mdio_device *mdiodev,
> ret = -ENODEV;
>
> out:
> - mdio_device_put(mdiodev);
> kfree(xpcs);
>
> return ERR_PTR(ret);
The above two hunks are a completely Unnecessary change.
> @@ -1437,19 +1435,21 @@ struct dw_xpcs *xpcs_create_mdiodev(struct mii_bus *bus, int addr,
> struct mdio_device *mdiodev;
> struct dw_xpcs *xpcs;
>
> - mdiodev = mdio_device_create(bus, addr);
> - if (IS_ERR(mdiodev))
> - return ERR_CAST(mdiodev);
> + if (addr >= PHY_MAX_ADDR)
> + return ERR_PTR(-EINVAL);
>
> - xpcs = xpcs_create(mdiodev, interface);
> + if (mdiobus_is_registered_device(bus, addr)) {
> + mdiodev = bus->mdio_map[addr];
> + mdio_device_get(mdiodev);
This is fine - taking a reference on the mdiodev you've got from
somewhere else is the right thing to do.
> + } else {
> + mdiodev = mdio_device_create(bus, addr);
> + if (IS_ERR(mdiodev))
> + return ERR_CAST(mdiodev);
> + }
>
> - /* xpcs_create() has taken a refcount on the mdiodev if it was
> - * successful. If xpcs_create() fails, this will free the mdio
> - * device here. In any case, we don't need to hold our reference
> - * anymore, and putting it here will allow mdio_device_put() in
> - * xpcs_destroy() to automatically free the mdio device.
> - */
> - mdio_device_put(mdiodev);
> + xpcs = xpcs_create(mdiodev, interface);
> + if (IS_ERR(xpcs))
> + mdio_device_put(mdiodev);
Without the change to xpcs_create() you don't need this change - and
this is why I say you don't understand refcounting.
The point here is that the refcounting management is in each function
where references are gained or lost.
xpcs_create() creates a new reference to the mdiodev by storing it in
the dw_xpcs structure. Therefore, it takes a reference to the mdiodev.
If something fails, it drops that reference to restore the refcount
as it was on function entry.
xpcs_create_mdiodev() as it originally stood creates the mdiodev from
the bus/address, and then passes that to xpcs_create(). Once
xpcs_create() has finished its work (irrespective of whether it was
successful or not) we're done with the mdiodev in this function, so
the reference is _always_ put.
For your use case, it would be:
mdiodev = bus->mdio_map[addr];
mdio_device_get(mdiodev);
xpcs = xpcs_create(mdiodev, interface);
mdio_device_put(mdiodev);
return xpcs;
which illustrates this point - we get a reference to the mdiodev by
reading it from the array. We do something (calling xpcs_create)
with it. If that something was successful, it takes its own refcount
otherwise leaves it as-is. We're then done with the mdiodev so we
drop the refcount we took.
There is no need to make the code more complicated by changing this,
so I regard the refcount changes in this patch to be wrong.
--
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
next prev parent reply other threads:[~2023-12-05 13:47 UTC|newest]
Thread overview: 146+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-12-05 10:35 [PATCH net-next 00/16] net: pcs: xpcs: Add memory-based management iface support Serge Semin
2023-12-05 10:35 ` Serge Semin
2023-12-05 10:35 ` [PATCH net-next 01/16] net: pcs: xpcs: Drop sentinel entry from 2500basex ifaces list Serge Semin
2023-12-05 10:35 ` Serge Semin
2023-12-05 11:24 ` Vladimir Oltean
2023-12-05 11:24 ` Vladimir Oltean
2023-12-05 11:39 ` Serge Semin
2023-12-05 11:39 ` Serge Semin
2023-12-05 10:35 ` [PATCH net-next 02/16] net: pcs: xpcs: Drop redundant workqueue.h include directive Serge Semin
2023-12-05 10:35 ` Serge Semin
2023-12-05 13:34 ` Andrew Lunn
2023-12-05 13:34 ` Andrew Lunn
2023-12-05 10:35 ` [PATCH net-next 03/16] net: pcs: xpcs: Return EINVAL in the internal methods Serge Semin
2023-12-05 10:35 ` Serge Semin
2023-12-05 13:34 ` Andrew Lunn
2023-12-05 13:34 ` Andrew Lunn
2023-12-05 10:35 ` [PATCH net-next 04/16] net: pcs: xpcs: Explicitly return error on caps validation Serge Semin
2023-12-05 10:35 ` Serge Semin
2023-12-05 10:35 ` [PATCH net-next 05/16] net: pcs: xpcs: Move native device ID macro to linux/pcs/pcs-xpcs.h Serge Semin
2023-12-05 10:35 ` Serge Semin
2023-12-05 10:45 ` Russell King (Oracle)
2023-12-05 10:45 ` Russell King (Oracle)
2023-12-05 11:14 ` Serge Semin
2023-12-05 11:14 ` Serge Semin
2023-12-05 11:22 ` Russell King (Oracle)
2023-12-05 11:22 ` Russell King (Oracle)
2023-12-05 11:48 ` Serge Semin
2023-12-05 11:48 ` Serge Semin
2023-12-05 11:27 ` Vladimir Oltean
2023-12-05 11:27 ` Vladimir Oltean
2023-12-05 11:49 ` Serge Semin
2023-12-05 11:49 ` Serge Semin
2023-12-05 10:35 ` [PATCH net-next 06/16] net: pcs: xpcs: Avoid creating dummy XPCS MDIO device Serge Semin
2023-12-05 10:35 ` Serge Semin
2023-12-05 10:49 ` Russell King (Oracle)
2023-12-05 10:49 ` Russell King (Oracle)
2023-12-05 11:31 ` Serge Semin
2023-12-05 11:31 ` Serge Semin
2023-12-05 13:31 ` Russell King (Oracle)
2023-12-05 13:31 ` Russell King (Oracle)
2023-12-05 13:52 ` Andrew Lunn
2023-12-05 13:52 ` Andrew Lunn
2023-12-05 14:50 ` Russell King (Oracle)
2023-12-05 14:50 ` Russell King (Oracle)
2023-12-12 13:52 ` Serge Semin
2023-12-12 13:52 ` Serge Semin
2023-12-05 11:52 ` Vladimir Oltean
2023-12-05 11:52 ` Vladimir Oltean
2023-12-13 15:27 ` Serge Semin
2023-12-13 15:27 ` Serge Semin
2023-12-19 15:48 ` Serge Semin
2023-12-19 15:48 ` Serge Semin
2023-12-19 16:28 ` Vladimir Oltean
2023-12-19 16:28 ` Vladimir Oltean
2023-12-19 21:48 ` Serge Semin
2023-12-19 21:48 ` Serge Semin
2023-12-05 13:46 ` Russell King (Oracle) [this message]
2023-12-05 13:46 ` Russell King (Oracle)
2023-12-05 14:54 ` Russell King (Oracle)
2023-12-05 14:54 ` Russell King (Oracle)
2023-12-12 15:26 ` Serge Semin
2023-12-12 15:26 ` Serge Semin
2023-12-12 19:06 ` Russell King (Oracle)
2023-12-12 19:06 ` Russell King (Oracle)
2023-12-13 15:47 ` Serge Semin
2023-12-13 15:47 ` Serge Semin
2023-12-13 0:01 ` Serge Semin
2023-12-13 0:01 ` Serge Semin
2023-12-13 16:32 ` Russell King (Oracle)
2023-12-13 16:32 ` Russell King (Oracle)
2023-12-14 14:19 ` Serge Semin
2023-12-14 14:19 ` Serge Semin
2023-12-05 10:35 ` [PATCH net-next 07/16] net: pcs: xpcs: Split up xpcs_create() content to sub-functions Serge Semin
2023-12-05 10:35 ` Serge Semin
2023-12-05 10:35 ` [PATCH net-next 08/16] dt-bindings: net: Add Synopsys DW xPCS bindings Serge Semin
2023-12-05 10:35 ` Serge Semin
2023-12-14 17:40 ` Rob Herring
2023-12-14 17:40 ` Rob Herring
2023-12-14 21:27 ` Serge Semin
2023-12-14 21:27 ` Serge Semin
2023-12-05 10:35 ` [PATCH net-next 09/16] net: mdio: Add Synopsys DW XPCS management interface support Serge Semin
2023-12-05 10:35 ` Serge Semin
2023-12-05 12:32 ` Maxime Chevallier
2023-12-05 12:32 ` Maxime Chevallier
2023-12-06 16:48 ` Serge Semin
2023-12-06 16:48 ` Serge Semin
2023-12-06 17:01 ` Andrew Lunn
2023-12-06 17:01 ` Andrew Lunn
2023-12-07 13:35 ` Serge Semin
2023-12-07 13:35 ` Serge Semin
2023-12-07 14:02 ` Russell King (Oracle)
2023-12-07 14:02 ` Russell King (Oracle)
2023-12-07 14:54 ` Andrew Lunn
2023-12-07 14:54 ` Andrew Lunn
2023-12-08 16:07 ` Serge Semin
2023-12-08 16:07 ` Serge Semin
2023-12-05 10:35 ` [PATCH net-next 10/16] net: pcs: xpcs: Add generic DW XPCS MDIO-device support Serge Semin
2023-12-05 10:35 ` Serge Semin
2023-12-05 11:13 ` Vladimir Oltean
2023-12-05 11:13 ` Vladimir Oltean
2023-12-05 11:35 ` Serge Semin
2023-12-05 11:35 ` Serge Semin
2023-12-05 12:23 ` Vladimir Oltean
2023-12-05 12:23 ` Vladimir Oltean
2023-12-08 14:11 ` Serge Semin
2023-12-08 14:11 ` Serge Semin
2023-12-08 16:33 ` Vladimir Oltean
2023-12-08 16:33 ` Vladimir Oltean
2023-12-14 11:54 ` Serge Semin
2023-12-14 11:54 ` Serge Semin
2023-12-14 12:00 ` Vladimir Oltean
2023-12-14 12:00 ` Vladimir Oltean
2023-12-14 12:28 ` Serge Semin
2023-12-14 12:28 ` Serge Semin
2023-12-05 10:35 ` [PATCH net-next 11/16] net: pcs: xpcs: Change xpcs_create_mdiodev() suffix to "byaddr" Serge Semin
2023-12-05 10:35 ` Serge Semin
2023-12-05 10:35 ` Serge Semin
2023-12-05 23:03 ` kernel test robot
2023-12-05 23:03 ` kernel test robot
2023-12-05 23:03 ` kernel test robot
2023-12-07 14:37 ` Serge Semin
2023-12-07 14:37 ` Serge Semin
2023-12-07 14:37 ` Serge Semin
2023-12-06 0:29 ` kernel test robot
2023-12-06 0:29 ` kernel test robot
2023-12-06 0:29 ` kernel test robot
2023-12-05 10:35 ` [PATCH net-next 12/16] net: pcs: xpcs: Add xpcs_create_bynode() method Serge Semin
2023-12-05 10:35 ` Serge Semin
2023-12-05 10:35 ` [PATCH net-next 13/16] net: stmmac: intel: Register generic MDIO device Serge Semin
2023-12-05 10:35 ` Serge Semin
2023-12-05 10:35 ` Serge Semin
2023-12-06 0:19 ` kernel test robot
2023-12-06 0:19 ` kernel test robot
2023-12-06 0:19 ` kernel test robot
2023-12-07 14:47 ` Serge Semin
2023-12-07 14:47 ` Serge Semin
2023-12-07 14:47 ` Serge Semin
2023-12-05 10:35 ` [PATCH net-next 14/16] net: stmmac: Pass netdev to XPCS setup function Serge Semin
2023-12-05 10:35 ` Serge Semin
2023-12-05 10:35 ` Serge Semin
2023-12-05 10:35 ` [PATCH net-next 15/16] net: stmmac: Add dedicated XPCS cleanup method Serge Semin
2023-12-05 10:35 ` Serge Semin
2023-12-05 10:35 ` Serge Semin
2023-12-05 10:35 ` [PATCH net-next 16/16] net: stmmac: Add externally detected DW XPCS support Serge Semin
2023-12-05 10:35 ` Serge Semin
2023-12-05 10:35 ` Serge Semin
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=ZW8pxM3RvyHJTwqH@shell.armlinux.org.uk \
--to=linux@armlinux.org.uk \
--cc=Jose.Abreu@synopsys.com \
--cc=alexandre.torgue@foss.st.com \
--cc=andrew@lunn.ch \
--cc=conor+dt@kernel.org \
--cc=davem@davemloft.net \
--cc=devicetree@vger.kernel.org \
--cc=edumazet@google.com \
--cc=fancer.lancer@gmail.com \
--cc=hkallweit1@gmail.com \
--cc=joabreu@synopsys.com \
--cc=krzysztof.kozlowski+dt@linaro.org \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=maxime.chevallier@bootlin.com \
--cc=netdev@vger.kernel.org \
--cc=openbmc@lists.ozlabs.org \
--cc=pabeni@redhat.com \
--cc=robh+dt@kernel.org \
--cc=tmaimon77@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.