net: provide a devres variant of register_netdev()
Subsystems (6)
v2 cover net: provide a devres variant of register_netdev()
From: Bartosz Golaszewski <[email protected]>
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.
v1 -> v2:
- rebase on top of net-next after driver rename, no functional changes
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_star_emac: use devm_register_netdev()
.../driver-api/driver-model/devres.rst | 5 + drivers/net/ethernet/mediatek/mtk_star_emac.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 v2 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 v2 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 v2 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
@@ -4280,6 +4280,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 v2 5/5 net: ethernet: mtk_star_emac: use devm_register_netdev()
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
Use the new devres variant of register_netdev() in the mtk-star-emac driver and shrink the code by a couple lines.
--- drivers/net/ethernet/mediatek/mtk_star_emac.c | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-)
drivers/net/ethernet/mediatek/mtk_star_emac.c +1 -16
@@ -1519,13 +1519,6 @@ static void mtk_star_mdiobus_unregister(void *data)mdiobus_unregister (priv->mii); } -static void mtk_star_unregister_netdev (void *data) -{ -struct net_device *ndev = data; - -unregister_netdev (ndev); -} -static int mtk_star_probe (struct platform_device *pdev) {struct device_node *of_node;
@@ -1641,15 +1634,7 @@ static int mtk_star_probe(struct platform_device *pdev)netif_napi_add (ndev, &priv->napi, mtk_star_poll,MTK_STAR_NAPI_WEIGHT ); - ret =register_netdev (ndev); -if (ret) -return ret; - - ret =devm_add_action_or_reset (dev, mtk_star_unregister_netdev, ndev); -if (ret) -return ret; - -return 0 ; +return devm_register_netdev (dev, ndev); }static const struct of_device_id mtk_star_of_match[] = {
Signature · 1 line
2.25.0
PATCH v2 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
David Miller
maintainerFrom: Bartosz Golaszewski <[email protected]>
Date: Sat, 23 May 2020 15:27:06 +0200
Quoted 11 lines
From: Bartosz Golaszewski <[email protected]>
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 withnet_devicestructures whose memory is also managed.First we add the missing documentation entry for the only other networking devres helper:
devm_alloc_etherdev().
Quoted 8 lines
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.v1 -> v2:
- rebase on top of net-next after driver rename, no functional changes
Series applied, thank you.