From patchwork Tue May 7 10:28:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxime Chevallier X-Patchwork-Id: 13656597 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 8CA38C25B76 for ; Tue, 7 May 2024 10:28:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=0TPpNgab0xkl6wRq+kBaGK8/rOgZ7wSwKLZIO7Y0WBw=; b=YwhiOAHD9lD370 iJmxWFWLQHtwgm4eQIGQz/K5VqXNMTShRrpvVDXcpeuvaWqPkkwXVwzsI/TEmnioXVAYeHDMEWEsJ EoZuSdi7jjpyesGhq57vAerybY45p7vAO/C9ZZKXQj+PlWCdp7QMxEpxPNPFEpte520vPj0jUj5kI 5QzDjDsos3buesRj4GJy8nPFJqi0jntjI2qf5Hhwx+POB0hpZObSRsAWqFnXwAtWdAcmUKF7ZuCxQ 9YBH6ZFe+IS7T1nPRI0Gfr7FdYXZTz3E2ZrvrjArN6A0d1MyXkwnJb3eCUHQYkiOdMzAkmjLgtYyU 1E/la6tffDztnEjV5/Gg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1s4I3o-0000000AXDa-22f5; Tue, 07 May 2024 10:28:36 +0000 Received: from relay4-d.mail.gandi.net ([217.70.183.196]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1s4I3k-0000000AX9u-19lD for linux-arm-kernel@lists.infradead.org; Tue, 07 May 2024 10:28:34 +0000 Received: by mail.gandi.net (Postfix) with ESMTPSA id 8B99FE0004; Tue, 7 May 2024 10:28:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1715077708; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CUadJuWaqhi+XpEWHDh3AN7U2iQoAUeidf5WQOmYOpA=; b=mAfcNcwrAZRpbUmnaGm2ZWNEFMXcdCee0YfFjU6W8xu/Exp+ggJzi20dNqMt7Evgd06mj3 baEHbfAoNkQK8mxvgTmq0K7eFYGGDz4JWW2n9KYKGrWVhF90is/Iaib04gUAZfFfJIJCwf I1KIF7xUou9i3pMTJuSOKtrc6YYXYbh4E3yap5RU8lk0hII/+prnIiebvoG6gTifDrYSzL 8IntoFByLa0syCQkmkAZZyZ0XIiVtg4xayRWDOkU5YrIDn6gVSVzlyfveD4g70OaPevL31 +YlxiI9RSDe18/eiLxHkkJM/rGhpBw7Ah1gSapSQHndvUqOhfFiGUl3BuBE+Cw== From: Maxime Chevallier To: davem@davemloft.net Cc: Maxime Chevallier , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, thomas.petazzoni@bootlin.com, Andrew Lunn , Jakub Kicinski , Eric Dumazet , Paolo Abeni , Russell King , linux-arm-kernel@lists.infradead.org, Christophe Leroy , Herve Codina , Florian Fainelli , Heiner Kallweit , Vladimir Oltean , =?utf-8?q?K=C3=B6ry_Maincent?= , Jesse Brandeburg , =?utf-8?q?Marek_Beh=C3=BAn?= , Piergiorgio Beruto , Oleksij Rempel , =?utf-8?q?Nicol=C3=B2_Veronese?= , Simon Horman , mwojtas@chromium.org, Nathan Chancellor , Antoine Tenart Subject: [PATCH net-next 2/2] net: phy: phy_link_topology: Lazy-initialize the link topology Date: Tue, 7 May 2024 12:28:21 +0200 Message-ID: <20240507102822.2023826-3-maxime.chevallier@bootlin.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240507102822.2023826-1-maxime.chevallier@bootlin.com> References: <20240507102822.2023826-1-maxime.chevallier@bootlin.com> MIME-Version: 1.0 X-GND-Sasl: maxime.chevallier@bootlin.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240507_032832_735105_A6F7BFEE X-CRM114-Status: GOOD ( 17.20 ) 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 Having the net_device's init path for the link_topology depend on IS_REACHABLE(PHYLIB)-protected helpers triggers errors when modules are being built with phylib as a module as-well, as they expect netdev->link_topo to be initialized. Move the link_topo initialization at the first PHY insertion, which will both improve the memory usage, and make the behaviour more predicatble and robust. Signed-off-by: Maxime Chevallier Fixes: 6916e461e793 ("net: phy: Introduce ethernet link topology representation") Closes: https://lore.kernel.org/netdev/2e11b89d-100f-49e7-9c9a-834cc0b82f97@gmail.com/ Closes: https://lore.kernel.org/netdev/20240409201553.GA4124869@dev-arch.thelio-3990X/ --- drivers/net/phy/phy_link_topology.c | 31 ++++++--------------- include/linux/netdevice.h | 2 ++ include/linux/phy_link_topology.h | 23 ++++++++-------- include/linux/phy_link_topology_core.h | 23 +++------------- net/core/dev.c | 38 ++++++++++++++++++++++---- 5 files changed, 58 insertions(+), 59 deletions(-) diff --git a/drivers/net/phy/phy_link_topology.c b/drivers/net/phy/phy_link_topology.c index 0e36bd7c15dc..b1aba9313e73 100644 --- a/drivers/net/phy/phy_link_topology.c +++ b/drivers/net/phy/phy_link_topology.c @@ -12,29 +12,6 @@ #include #include -struct phy_link_topology *phy_link_topo_create(struct net_device *dev) -{ - struct phy_link_topology *topo; - - topo = kzalloc(sizeof(*topo), GFP_KERNEL); - if (!topo) - return ERR_PTR(-ENOMEM); - - xa_init_flags(&topo->phys, XA_FLAGS_ALLOC1); - topo->next_phy_index = 1; - - return topo; -} - -void phy_link_topo_destroy(struct phy_link_topology *topo) -{ - if (!topo) - return; - - xa_destroy(&topo->phys); - kfree(topo); -} - int phy_link_topo_add_phy(struct net_device *dev, struct phy_device *phy, enum phy_upstream upt, void *upstream) @@ -43,6 +20,14 @@ int phy_link_topo_add_phy(struct net_device *dev, struct phy_device_node *pdn; int ret; + if (!topo) { + ret = netdev_alloc_phy_link_topology(dev); + if (ret) + return ret; + + topo = dev->link_topo; + } + pdn = kzalloc(sizeof(*pdn), GFP_KERNEL); if (!pdn) return -ENOMEM; diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h index cf261fb89d73..25a0a77cfadc 100644 --- a/include/linux/netdevice.h +++ b/include/linux/netdevice.h @@ -4569,6 +4569,8 @@ void __hw_addr_unsync_dev(struct netdev_hw_addr_list *list, const unsigned char *)); void __hw_addr_init(struct netdev_hw_addr_list *list); +int netdev_alloc_phy_link_topology(struct net_device *dev); + /* Functions used for device addresses handling */ void dev_addr_mod(struct net_device *dev, unsigned int offset, const void *addr, size_t len); diff --git a/include/linux/phy_link_topology.h b/include/linux/phy_link_topology.h index 166a01710aa2..3501f9a9e932 100644 --- a/include/linux/phy_link_topology.h +++ b/include/linux/phy_link_topology.h @@ -32,10 +32,12 @@ struct phy_device_node { struct phy_device *phy; }; -struct phy_link_topology { - struct xarray phys; - u32 next_phy_index; -}; +#if IS_ENABLED(CONFIG_PHYLIB) +int phy_link_topo_add_phy(struct net_device *dev, + struct phy_device *phy, + enum phy_upstream upt, void *upstream); + +void phy_link_topo_del_phy(struct net_device *dev, struct phy_device *phy); static inline struct phy_device *phy_link_topo_get_phy(struct net_device *dev, u32 phyindex) @@ -53,13 +55,6 @@ static inline struct phy_device return NULL; } -#if IS_REACHABLE(CONFIG_PHYLIB) -int phy_link_topo_add_phy(struct net_device *dev, - struct phy_device *phy, - enum phy_upstream upt, void *upstream); - -void phy_link_topo_del_phy(struct net_device *dev, struct phy_device *phy); - #else static inline int phy_link_topo_add_phy(struct net_device *dev, struct phy_device *phy, @@ -72,6 +67,12 @@ static inline void phy_link_topo_del_phy(struct net_device *dev, struct phy_device *phy) { } + +static inline struct phy_device * +phy_link_topo_get_phy(struct net_device *dev, u32 phyindex) +{ + return NULL; +} #endif #endif /* __PHY_LINK_TOPOLOGY_H */ diff --git a/include/linux/phy_link_topology_core.h b/include/linux/phy_link_topology_core.h index 0a6479055745..f9c0520806fb 100644 --- a/include/linux/phy_link_topology_core.h +++ b/include/linux/phy_link_topology_core.h @@ -2,24 +2,9 @@ #ifndef __PHY_LINK_TOPOLOGY_CORE_H #define __PHY_LINK_TOPOLOGY_CORE_H -struct phy_link_topology; - -#if IS_REACHABLE(CONFIG_PHYLIB) - -struct phy_link_topology *phy_link_topo_create(struct net_device *dev); -void phy_link_topo_destroy(struct phy_link_topology *topo); - -#else - -static inline struct phy_link_topology *phy_link_topo_create(struct net_device *dev) -{ - return NULL; -} - -static inline void phy_link_topo_destroy(struct phy_link_topology *topo) -{ -} - -#endif +struct phy_link_topology { + struct xarray phys; + u32 next_phy_index; +}; #endif /* __PHY_LINK_TOPOLOGY_CORE_H */ diff --git a/net/core/dev.c b/net/core/dev.c index d2ce91a334c1..1b4ffc273a04 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -10256,6 +10256,35 @@ static void netdev_do_free_pcpu_stats(struct net_device *dev) } } +int netdev_alloc_phy_link_topology(struct net_device *dev) +{ + struct phy_link_topology *topo; + + topo = kzalloc(sizeof(*topo), GFP_KERNEL); + if (!topo) + return -ENOMEM; + + xa_init_flags(&topo->phys, XA_FLAGS_ALLOC1); + topo->next_phy_index = 1; + + dev->link_topo = topo; + + return 0; +} +EXPORT_SYMBOL_GPL(netdev_alloc_phy_link_topology); + +static void netdev_free_phy_link_topology(struct net_device *dev) +{ + struct phy_link_topology *topo = dev->link_topo; + + if (!topo) + return; + + xa_destroy(&topo->phys); + kfree(topo); + dev->link_topo = NULL; +} + /** * register_netdevice() - register a network device * @dev: device to register @@ -10998,11 +11027,6 @@ struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name, #ifdef CONFIG_NET_SCHED hash_init(dev->qdisc_hash); #endif - dev->link_topo = phy_link_topo_create(dev); - if (IS_ERR(dev->link_topo)) { - dev->link_topo = NULL; - goto free_all; - } dev->priv_flags = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM; setup(dev); @@ -11092,7 +11116,9 @@ void free_netdev(struct net_device *dev) free_percpu(dev->xdp_bulkq); dev->xdp_bulkq = NULL; - phy_link_topo_destroy(dev->link_topo); +#if IS_ENABLED(CONFIG_PHYLIB) + netdev_free_phy_link_topology(dev); +#endif /* Compatibility with error handling in drivers */ if (dev->reg_state == NETREG_UNINITIALIZED ||