net: thunderx: Unembed netdev structure
authorBreno Leitao <leitao@debian.org>
Wed, 26 Jun 2024 17:35:02 +0000 (10:35 -0700)
committerJakub Kicinski <kuba@kernel.org>
Thu, 27 Jun 2024 23:55:34 +0000 (16:55 -0700)
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/
Signed-off-by: Breno Leitao <leitao@debian.org>
Link: https://patch.msgid.link/20240626173503.87636-1-leitao@debian.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
drivers/net/ethernet/cavium/thunder/thunder_bgx.c

index a317feb8decb64c9263f64245b8dfeeb4efea40f..a40c266c37f207c1966375058b8117c81f2629fe 100644 (file)
@@ -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,10 +590,12 @@ 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 phy_device *phydev;
+       struct lmac *lmac, **priv;
        int link_changed = 0;
 
+       priv = netdev_priv(netdev);
+       lmac = *priv;
        phydev = lmac->phydev;
 
        if (!phydev->link && lmac->last_link)
@@ -1052,12 +1054,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 +1124,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 +1191,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 +1423,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 +1492,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);