From patchwork Wed Jun 26 17:35:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Breno Leitao X-Patchwork-Id: 13713207 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 37EB0C3064D for ; Wed, 26 Jun 2024 17:35:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=ow1/zkrwYlXL87F3Jhh935fE1AcOFBtgfTMUkpcve0g=; b=EPrBL36gvBmgA7T/OjCDofNNuW wgXJSp+t8Vp2Ovd+hhVBThm7aCietFOVEICVtWTJBD19uKY/17/wxkVxb/hmFRMWM3XGn0t9F7s1K ZApL9tDCnZ/GuxpN/B+QNEUc0y/izMtlum2EAET7+iEpmCzOq75A6mITqqXRqjOz226EZVopm3I7e YQdJFZLdrn+IuMGHtusMwiDVrHWqtGN3qd/VB9TGbu2tQ1oxL5ILmnYZ/AytOO7CIxK/VdR51skGx AR3llb56040ZjJKR0i7IWaUZEIjSNaH9fcRxlGC440dayLrGSe4nolfppyTQ/UJ+lqAI9zhG4AmTF OAtR0hWw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMWYF-00000007mlE-1Qcr; Wed, 26 Jun 2024 17:35:23 +0000 Received: from mail-ej1-f53.google.com ([209.85.218.53]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMWY7-00000007mie-0XsC for linux-arm-kernel@lists.infradead.org; Wed, 26 Jun 2024 17:35:17 +0000 Received: by mail-ej1-f53.google.com with SMTP id a640c23a62f3a-a6cb130027aso472562466b.2 for ; Wed, 26 Jun 2024 10:35:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719423313; x=1720028113; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ow1/zkrwYlXL87F3Jhh935fE1AcOFBtgfTMUkpcve0g=; b=FSjKP+rpCk4W07GbU02AhEZ+g88QE4Cj1+tLkyE5r9XYGuD4lNzUJ6nvGOfziVtErB Ths3TZqfRQbBjTkjR8OQjgnKPkp26NgHdoaIEPJmCr9CDekDntbvl6m0UvqXHXTicTRz hqHC14EH91ZO7fdVRWF7TmGkZL3nyXKE2tDDMSzwnLVguiNM1QLKTH1jbOe6MKinn9XZ YhEJtn8bi78AzL3hF4YxeUMe7muBQRvO+ibdbd+PFr8iFIFVGOb/mP5r6ZJu9qCTQOBa JyGZ4npEdyo7H/W7tnSlvd7J6Gw/y422MpKUYSuFq3ClnTztxgMJSBo3XUCg4fLeVB59 /+sw== X-Forwarded-Encrypted: i=1; AJvYcCXlz94y5KINF3oZPceD7ZjRStdx/JRXkTZq1gCLAD21tk3JwKoZBmz9auMVV3au9vQIltrIt9CxHVoNda2oO8Ki+tM1upUN23op9Jx5D/lzgYRBa1o= X-Gm-Message-State: AOJu0YyQidmGwHTa9SHYwCrrG2jaLaxPn1iI9BM8K6jYRmfJFlNXGHP7 tW2hFO18TwBccWyiZdxt7gkAps5Kwz6jNq+0FGPcVHwHxrhEvU14 X-Google-Smtp-Source: AGHT+IGd5w8E3YSQlN+Wteim20OJPCRnGzSrcDQQ4ewgWBwEl7pRbve5ElLzoCZ3gjCNKsuDtUIfug== X-Received: by 2002:a50:99dd:0:b0:57c:5874:4f5c with SMTP id 4fb4d7f45d1cf-57d4bdcad82mr10024152a12.32.1719423313055; Wed, 26 Jun 2024 10:35:13 -0700 (PDT) Received: from localhost (fwdproxy-lla-112.fbsv.net. [2a03:2880:30ff:70::face:b00c]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-57d30bd5f1asm7471524a12.37.2024.06.26.10.35.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jun 2024 10:35:12 -0700 (PDT) From: Breno Leitao To: kuba@kernel.org, Sunil Goutham , "David S. Miller" , Eric Dumazet , Paolo Abeni Cc: horms@kernel.org, linux-arm-kernel@lists.infradead.org (moderated list:ARM/CAVIUM THUNDER NETWORK DRIVER), netdev@vger.kernel.org (open list:NETWORKING DRIVERS), linux-kernel@vger.kernel.org (open list) Subject: [PATCH net-next v2] net: thunderx: Unembed netdev structure Date: Wed, 26 Jun 2024 10:35:02 -0700 Message-ID: <20240626173503.87636-1-leitao@debian.org> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240626_103515_211365_962F2C41 X-CRM114-Status: GOOD ( 14.72 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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 --- Changelog: v2: * Fixed a wrong dereference in netdev_priv (Jakub) .../net/ethernet/cavium/thunder/thunder_bgx.c | 21 +++++++++++++------ 1 file changed, 15 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..a40c266c37f2 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,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);