net: provide a devres variant of register_netdev()
Subsystems (6)
cover net: provide a devres variant of register_netdev()
From: Bartosz Golaszewski <[email protected]>
This series applies on top of my mtk-eth-mac series[1].
Using devres helpers allows to shrink the probing code, avoid memory leaks in error paths make sure the order in which resources are freed is the exact opposite of their allocation. This series proposes to add a devres variant of register_netdev() that will only work with net_device structures whose memory is also managed.
First we add the missing documentation entry for the only other networking devres helper: devm_alloc_etherdev().
Next we move devm_alloc_etherdev() into a separate source file.
We then use a proxy structure in devm_alloc_etherdev() to improve readability.
Last: we implement devm_register_netdev() and use it in mtk-eth-mac driver.
[1] https://lkml.org/lkml/2020/5/20/507
Bartosz Golaszewski (5):
Documentation: devres: add a missing section for networking helpers
net: move devres helpers into a separate source file
net: devres: define a separate devres structure for
devm_alloc_etherdev()
net: devres: provide devm_register_netdev()
net: ethernet: mtk_eth_mac: use devm_register_netdev()
.../driver-api/driver-model/devres.rst | 5 + drivers/net/ethernet/mediatek/mtk_eth_mac.c | 17 +--- include/linux/netdevice.h | 2 + net/Makefile | 2 +- net/devres.c | 95 +++++++++++++++++++ net/ethernet/eth.c | 28 ------ 6 files changed, 104 insertions(+), 45 deletions(-) create mode 100644 net/devres.c
Signature · 1 line
2.25.0
PATCH 1/5 Documentation: devres: add a missing section for networking helpers
From: Bartosz Golaszewski <[email protected]>
Add a new section for networking devres helpers to devres.rst and list the two existing devm functions.
--- Documentation/driver-api/driver-model/devres.rst | 4 ++++ 1 file changed, 4 insertions(+)
Documentation/driver-api/driver-model/devres.rst +4 -0
@@ -372,6 +372,10 @@ MUX devm_mux_chip_register() devm_mux_control_get() +NET + devm_alloc_etherdev() + devm_alloc_etherdev_mqs() + PER-CPU MEM devm_alloc_percpu() devm_free_percpu()
Signature · 1 line
2.25.0
PATCH 2/5 net: move devres helpers into a separate source file
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
There's currently only a single devres helper in net/ - devm variant of alloc_etherdev. Let's move it to net/devres.c with the intention of assing a second one: devm_register_netdev(). This new routine will need to know the address of the release function of devm_alloc_etherdev() so that it can verify (using devres_find()) that the struct net_device that's being passed to it is also resource managed.
--- net/Makefile | 2 +- net/devres.c | 36 ++++++++++++++++++++++++++++++++++++ net/ethernet/eth.c | 28 ---------------------------- 3 files changed, 37 insertions(+), 29 deletions(-) create mode 100644 net/devres.c
net/Makefile +1 -1
@@ -6,7 +6,7 @@ # Rewritten to use lists instead of if-statements. # -obj-$(CONFIG_NET) := socket.o core/ +obj-$(CONFIG_NET) := devres.o socket.o core/ tmp-$(CONFIG_COMPAT) := compat.o obj-$(CONFIG_NET) += $(tmp-y)
net/devres.c new file +36 -0
@@ -0,0 +1,36 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* This file contains all networking devres helpers. +*/ + +#include <linux/device.h> +#include <linux/etherdevice.h> +#include <linux/netdevice.h> + +static void devm_free_netdev (struct device *dev,void *res) +{ +free_netdev (*(struct net_device **)res); +} + +struct net_device *devm_alloc_etherdev_mqs (struct device *dev,int sizeof_priv, +unsigned int txqs,unsigned int rxqs) +{ +struct net_device **dr; +struct net_device *netdev; + + dr =devres_alloc (devm_free_netdev,sizeof (*dr),GFP_KERNEL ); +if (!dr) +return NULL ; + + netdev =alloc_etherdev_mqs (sizeof_priv, txqs, rxqs); +if (!netdev) { +devres_free (dr); +return NULL ; + } + + *dr = netdev; +devres_add (dev, dr); + +return netdev; +} +EXPORT_SYMBOL (devm_alloc_etherdev_mqs);
net/ethernet/eth.c +0 -28
@@ -400,34 +400,6 @@ struct net_device *alloc_etherdev_mqs(int sizeof_priv, unsigned int txqs, }EXPORT_SYMBOL (alloc_etherdev_mqs); -static void devm_free_netdev (struct device *dev,void *res) -{ -free_netdev (*(struct net_device **)res); -} - -struct net_device *devm_alloc_etherdev_mqs (struct device *dev,int sizeof_priv, -unsigned int txqs,unsigned int rxqs) -{ -struct net_device **dr; -struct net_device *netdev; - - dr =devres_alloc (devm_free_netdev,sizeof (*dr),GFP_KERNEL ); -if (!dr) -return NULL ; - - netdev =alloc_etherdev_mqs (sizeof_priv, txqs, rxqs); -if (!netdev) { -devres_free (dr); -return NULL ; - } - - *dr = netdev; -devres_add (dev, dr); - -return netdev; -} -EXPORT_SYMBOL (devm_alloc_etherdev_mqs); -ssize_t sysfs_format_mac (char *buf,const unsigned char *addr,int len) {return scnprintf (buf,PAGE_SIZE ,"%*phC\n" , len, addr);
Signature · 1 line
2.25.0
PATCH 3/5 net: devres: define a separate devres structure for devm_alloc_etherdev()
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
Not using a proxy structure to store struct net_device doesn't save anything in terms of compiled code size or memory usage but significantly decreases the readability of the code with all the pointer casting.
Define struct net_device_devres and use it in devm_alloc_etherdev_mqs().
--- net/devres.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-)
net/devres.c +12 -8
@@ -7,30 +7,34 @@#include <linux/etherdevice.h> #include <linux/netdevice.h> -static void devm_free_netdev (struct device *dev,void *res) +struct net_device_devres { +struct net_device *ndev; +}; + +static void devm_free_netdev (struct device *dev,void *this) { -free_netdev (*(struct net_device **)res); +struct net_device_devres *res = this; + +free_netdev (res->ndev); }struct net_device *devm_alloc_etherdev_mqs (struct device *dev,int sizeof_priv,unsigned int txqs,unsigned int rxqs) { -struct net_device **dr; -struct net_device *netdev; +struct net_device_devres *dr; dr =devres_alloc (devm_free_netdev,sizeof (*dr),GFP_KERNEL );if (!dr)return NULL ; - netdev =alloc_etherdev_mqs (sizeof_priv, txqs, rxqs); -if (!netdev) { + dr->ndev =alloc_etherdev_mqs (sizeof_priv, txqs, rxqs); +if (!dr->ndev) {devres_free (dr);return NULL ; } - *dr = netdev;devres_add (dev, dr); -return netdev; +return dr->ndev; }EXPORT_SYMBOL (devm_alloc_etherdev_mqs);
Signature · 1 line
2.25.0
PATCH 4/5 net: devres: provide devm_register_netdev()
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
Provide devm_register_netdev() - a device resource managed variant of register_netdev(). This new helper will only work for net_device structs that are also already managed by devres.
--- .../driver-api/driver-model/devres.rst | 1 + include/linux/netdevice.h | 2 + net/devres.c | 55 +++++++++++++++++++ 3 files changed, 58 insertions(+)
Documentation/driver-api/driver-model/devres.rst +1 -0
@@ -375,6 +375,7 @@ MUX NET devm_alloc_etherdev() devm_alloc_etherdev_mqs() + devm_register_netdev() PER-CPU MEM devm_alloc_percpu()
include/linux/netdevice.h +2 -0
@@ -4208,6 +4208,8 @@ struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,int register_netdev (struct net_device *dev);void unregister_netdev (struct net_device *dev); +int devm_register_netdev (struct device *dev,struct net_device *ndev); +/* General hardware address lists handling functions */ int __hw_addr_sync (struct netdev_hw_addr_list *to_list,struct netdev_hw_addr_list *from_list,int addr_len);
net/devres.c +55 -0
58 lines not on this page · raw message
Signature · 1 line
2.25.0
PATCH 5/5 net: ethernet: mtk_eth_mac: use devm_register_netdev()
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
Use the new devres variant of register_netdev() in the mtk-eth-mac driver and shrink the code by a couple lines.
--- drivers/net/ethernet/mediatek/mtk_eth_mac.c | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-)
drivers/net/ethernet/mediatek/mtk_eth_mac.c +1 -16
@@ -1513,13 +1513,6 @@ static void mtk_mac_clk_disable_unprepare(void *data)clk_bulk_disable_unprepare (MTK_MAC_NCLKS , priv->clks); } -static void mtk_mac_unregister_netdev (void *data) -{ -struct net_device *ndev = data; - -unregister_netdev (ndev); -} -static int mtk_mac_probe (struct platform_device *pdev) {struct device_node *of_node;
@@ -1631,15 +1624,7 @@ static int mtk_mac_probe(struct platform_device *pdev)netif_napi_add (ndev, &priv->napi, mtk_mac_poll,MTK_MAC_NAPI_WEIGHT ); - ret =register_netdev (ndev); -if (ret) -return ret; - - ret =devm_add_action_or_reset (dev, mtk_mac_unregister_netdev, ndev); -if (ret) -return ret; - -return 0 ; +return devm_register_netdev (dev, ndev); }static const struct of_device_id mtk_mac_of_match[] = {
Signature · 1 line
2.25.0
David Miller
maintainerFrom: Bartosz Golaszewski <[email protected]>
Date: Wed, 20 May 2020 13:44:10 +0200
Quoted 1 line
net: ethernet:
mtk_eth_mac: usedevm_register_netdev()
This patch doesn't apply to net-next.
Neither the source file drivers/net/ethernet/mediatek/mtk_eth_mac.c, nor the function mtk_mac_probe() even exist in the net-next GIT tree.
sob., 23 maj 2020 o 00:36 David Miller <[email protected]> napisał(a):
Quoted 11 lines
From: Bartosz Golaszewski <[email protected]>
Date: Wed, 20 May 2020 13:44:10 +0200Quoted 1 line
net: ethernet:
mtk_eth_mac: usedevm_register_netdev()This patch doesn't apply to net-next.
Neither the source file
drivers/net/ethernet/mediatek/mtk_eth_mac.c, nor the functionmtk_mac_probe()even exist in the net-next GIT tree.
Indeed. The driver got renamed in v5. I didn't resend a new version of this series because I thought there would be a discussion about its usefulness. I'll send a rebased version then.
Bartosz