linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net-next] net: thunderx: Unembed netdev structure
@ 2024-06-24 10:29 Breno Leitao
  2024-06-26  0:54 ` Jakub Kicinski
  0 siblings, 1 reply; 3+ messages in thread
From: Breno Leitao @ 2024-06-24 10:29 UTC (permalink / raw)
  To: Sunil Goutham, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni
  Cc: horms, moderated list:ARM/CAVIUM THUNDER NETWORK DRIVER,
	open list:NETWORKING DRIVERS, open list

Embedding net_device into structures prohibits the usage of flexible
arrays in the net_device structure. For more details, see the discussion
at [1].

Un-embed the net_devices from struct lmac by converting them
into pointers, and allocating them dynamically. Use the leverage
alloc_netdev() to allocate the net_device object at
bgx_lmac_enable().

The free of the device occurs at bgx_lmac_disable().

 Do not free_netdevice() if bgx_lmac_enable() fails after lmac->netdev
is allocated, since bgx_lmac_disable() is called if bgx_lmac_enable()
fails, and lmac->netdev will be freed there (similarly to lmac->dmacs).

Link: https://lore.kernel.org/all/20240229225910.79e224cf@kernel.org/ [1]
Signed-off-by: Breno Leitao <leitao@debian.org>
---
PS: Unfortunately due to lack of hardware, this patch is compiled-test
only.

 .../net/ethernet/cavium/thunder/thunder_bgx.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/drivers/net/ethernet/cavium/thunder/thunder_bgx.c b/drivers/net/ethernet/cavium/thunder/thunder_bgx.c
index a317feb8decb..d097d606577b 100644
--- a/drivers/net/ethernet/cavium/thunder/thunder_bgx.c
+++ b/drivers/net/ethernet/cavium/thunder/thunder_bgx.c
@@ -54,7 +54,7 @@ struct lmac {
 	bool			link_up;
 	int			lmacid; /* ID within BGX */
 	int			lmacid_bd; /* ID on board */
-	struct net_device       netdev;
+	struct net_device       *netdev;
 	struct phy_device       *phydev;
 	unsigned int            last_duplex;
 	unsigned int            last_link;
@@ -590,7 +590,7 @@ static void bgx_sgmii_change_link_state(struct lmac *lmac)
 
 static void bgx_lmac_handler(struct net_device *netdev)
 {
-	struct lmac *lmac = container_of(netdev, struct lmac, netdev);
+	struct lmac *lmac = netdev_priv(netdev);
 	struct phy_device *phydev;
 	int link_changed = 0;
 
@@ -1052,12 +1052,18 @@ static int phy_interface_mode(u8 lmac_type)
 
 static int bgx_lmac_enable(struct bgx *bgx, u8 lmacid)
 {
-	struct lmac *lmac;
+	struct lmac *lmac, **priv;
 	u64 cfg;
 
 	lmac = &bgx->lmac[lmacid];
 	lmac->bgx = bgx;
 
+	lmac->netdev = alloc_netdev_dummy(sizeof(struct lmac *));
+	if (!lmac->netdev)
+		return -ENOMEM;
+	priv = netdev_priv(lmac->netdev);
+	*priv = lmac;
+
 	if ((lmac->lmac_type == BGX_MODE_SGMII) ||
 	    (lmac->lmac_type == BGX_MODE_QSGMII) ||
 	    (lmac->lmac_type == BGX_MODE_RGMII)) {
@@ -1116,7 +1122,7 @@ static int bgx_lmac_enable(struct bgx *bgx, u8 lmacid)
 		}
 		lmac->phydev->dev_flags = 0;
 
-		if (phy_connect_direct(&lmac->netdev, lmac->phydev,
+		if (phy_connect_direct(lmac->netdev, lmac->phydev,
 				       bgx_lmac_handler,
 				       phy_interface_mode(lmac->lmac_type)))
 			return -ENODEV;
@@ -1183,6 +1189,7 @@ static void bgx_lmac_disable(struct bgx *bgx, u8 lmacid)
 	    (lmac->lmac_type != BGX_MODE_10G_KR) && lmac->phydev)
 		phy_disconnect(lmac->phydev);
 
+	free_netdev(lmac->netdev);
 	lmac->phydev = NULL;
 }
 
@@ -1414,7 +1421,7 @@ static acpi_status bgx_acpi_register_phy(acpi_handle handle,
 
 	acpi_get_mac_address(dev, adev, bgx->lmac[bgx->acpi_lmac_idx].mac);
 
-	SET_NETDEV_DEV(&bgx->lmac[bgx->acpi_lmac_idx].netdev, dev);
+	SET_NETDEV_DEV(bgx->lmac[bgx->acpi_lmac_idx].netdev, dev);
 
 	bgx->lmac[bgx->acpi_lmac_idx].lmacid = bgx->acpi_lmac_idx;
 	bgx->acpi_lmac_idx++; /* move to next LMAC */
@@ -1483,7 +1490,7 @@ static int bgx_init_of_phy(struct bgx *bgx)
 
 		of_get_mac_address(node, bgx->lmac[lmac].mac);
 
-		SET_NETDEV_DEV(&bgx->lmac[lmac].netdev, &bgx->pdev->dev);
+		SET_NETDEV_DEV(bgx->lmac[lmac].netdev, &bgx->pdev->dev);
 		bgx->lmac[lmac].lmacid = lmac;
 
 		phy_np = of_parse_phandle(node, "phy-handle", 0);
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH net-next] net: thunderx: Unembed netdev structure
  2024-06-24 10:29 [PATCH net-next] net: thunderx: Unembed netdev structure Breno Leitao
@ 2024-06-26  0:54 ` Jakub Kicinski
  2024-06-26 15:12   ` Breno Leitao
  0 siblings, 1 reply; 3+ messages in thread
From: Jakub Kicinski @ 2024-06-26  0:54 UTC (permalink / raw)
  To: Breno Leitao
  Cc: Sunil Goutham, David S. Miller, Eric Dumazet, Paolo Abeni, horms,
	moderated list:ARM/CAVIUM THUNDER NETWORK DRIVER,
	open list:NETWORKING DRIVERS, open list

On Mon, 24 Jun 2024 03:29:18 -0700 Breno Leitao wrote:
>  static void bgx_lmac_handler(struct net_device *netdev)
>  {
> -	struct lmac *lmac = container_of(netdev, struct lmac, netdev);
> +	struct lmac *lmac = netdev_priv(netdev);

I think you are storing a pointer to lmac, so:

	struct lmac **priv = netdev_priv(netdev);
	struct lmac *lmac = *priv;
-- 
pw-bot: cr


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH net-next] net: thunderx: Unembed netdev structure
  2024-06-26  0:54 ` Jakub Kicinski
@ 2024-06-26 15:12   ` Breno Leitao
  0 siblings, 0 replies; 3+ messages in thread
From: Breno Leitao @ 2024-06-26 15:12 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: Sunil Goutham, David S. Miller, Eric Dumazet, Paolo Abeni, horms,
	moderated list:ARM/CAVIUM THUNDER NETWORK DRIVER,
	open list:NETWORKING DRIVERS, open list

On Tue, Jun 25, 2024 at 05:54:34PM -0700, Jakub Kicinski wrote:
> On Mon, 24 Jun 2024 03:29:18 -0700 Breno Leitao wrote:
> >  static void bgx_lmac_handler(struct net_device *netdev)
> >  {
> > -	struct lmac *lmac = container_of(netdev, struct lmac, netdev);
> > +	struct lmac *lmac = netdev_priv(netdev);
> 
> I think you are storing a pointer to lmac, so:
> 
> 	struct lmac **priv = netdev_priv(netdev);
> 	struct lmac *lmac = *priv;

Good catch. you are absolutely correct. I will update.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-06-26 15:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-24 10:29 [PATCH net-next] net: thunderx: Unembed netdev structure Breno Leitao
2024-06-26  0:54 ` Jakub Kicinski
2024-06-26 15:12   ` Breno Leitao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).