Skip to contentSkip to conversation

net: introduce a function to check if a netdev name is in use

PATCH Applied Antoine Tenart 2 participants 5 messages Oct 7, 2021 – Oct 8, 2021

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover net: introduce a function to check if a netdev name is in use

Hello,

This was initially part of an RFC series[1] but has value on its own; hence the standalone report. (It will also help in not having a series too large).

From patch 1:

"""
__dev_get_by_name is currently used to either retrieve a net device reference using its name or to check if a name is already used by a registered net device (per ns). In the later case there is no need to return a reference to a net device.

Introduce a new helper, netdev_name_in_use, to check if a name is currently used by a registered net device without leaking a reference the corresponding net device. This helper uses netdev_name_node_lookup instead of __dev_get_by_name as we don't need the extra logic retrieving a reference to the corresponding net device.
"""

Two uses[2] of __dev_get_by_name weren't converted to this new function, as they are really looking for a net device, not only checking if a net device name is in use. While checking one or the other currently has the same result, that might change if the initial RFC series moves forward. I'll convert them later depending on the outcome of the initial series.

Thanks,
Antoine

[1] https://lore.kernel.org/all/[email protected]/
[2] drivers/net/Space.c:130 & drivers/nvme/host/tcp.c:2550

Antoine Tenart (3):
net: introduce a function to check if a netdev name is in use
bonding: use the correct function to check for netdev name collision
ppp: use the correct function to check if a netdev name is in use

 drivers/net/bonding/bond_sysfs.c |  4 ++--
 drivers/net/ppp/ppp_generic.c    |  2 +-
 include/linux/netdevice.h        |  1 +
 net/core/dev.c                   | 14 ++++++++++----
 4 files changed, 14 insertions(+), 7 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH 1/3 net: introduce a function to check if a netdev name is in use

__dev_get_by_name is currently used to either retrieve a net device reference using its name or to check if a name is already used by a registered net device (per ns). In the later case there is no need to return a reference to a net device.

Introduce a new helper, netdev_name_in_use, to check if a name is currently used by a registered net device without leaking a reference the corresponding net device. This helper uses netdev_name_node_lookup instead of __dev_get_by_name as we don't need the extra logic retrieving a reference to the corresponding net device.

---
 include/linux/netdevice.h |  1 +
 net/core/dev.c            | 14 ++++++++++----
 2 files changed, 11 insertions(+), 4 deletions(-)
include/linux/netdevice.h +1 -0
index d79163208dfd..15f4a658e436 100644
@@ -2955,6 +2955,7 @@ struct net_device *__dev_get_by_flags(struct net *net, unsigned short flags,
 struct net_device *dev_get_by_name(struct net *net, const char *name);
 struct net_device *dev_get_by_name_rcu(struct net *net, const char *name);
 struct net_device *__dev_get_by_name(struct net *net, const char *name);
+bool netdev_name_in_use(struct net *net, const char *name);
 int dev_alloc_name(struct net_device *dev, const char *name);
 int dev_open(struct net_device *dev, struct netlink_ext_ack *extack);
 void dev_close(struct net_device *dev);
net/core/dev.c +10 -4
index 16ab09b6a7f8..1594cd2955ba 100644
@@ -303,6 +303,12 @@ static struct netdev_name_node *netdev_name_node_lookup_rcu(struct net *net,
 	return NULL;
 }
 
+bool netdev_name_in_use(struct net *net, const char *name)
+{
+	return netdev_name_node_lookup(net, name);
+}
+EXPORT_SYMBOL(netdev_name_in_use);
+
 int netdev_name_node_alt_create(struct net_device *dev, const char *name)
 {
 	struct netdev_name_node *name_node;
@@ -1133,7 +1139,7 @@ static int __dev_alloc_name(struct net *net, const char *name, char *buf)
 	}
 
 	snprintf(buf, IFNAMSIZ, name, i);
-	if (!__dev_get_by_name(net, buf))
+	if (!netdev_name_in_use(net, buf))
 		return i;
 
 	/* It is possible to run out of possible slots
@@ -1187,7 +1193,7 @@ static int dev_get_valid_name(struct net *net, struct net_device *dev,
 
 	if (strchr(name, '%'))
 		return dev_alloc_name_ns(net, dev, name);
-	else if (__dev_get_by_name(net, name))
+	else if (netdev_name_in_use(net, name))
 		return -EEXIST;
 	else if (dev->name != name)
 		strlcpy(dev->name, name, IFNAMSIZ);
@@ -11153,7 +11159,7 @@ int __dev_change_net_namespace(struct net_device *dev, struct net *net,
 	 * we can use it in the destination network namespace.
 	 */
 	err = -EEXIST;
-	if (__dev_get_by_name(net, dev->name)) {
+	if (netdev_name_in_use(net, dev->name)) {
 		/* We get here if we can't use the current device name */
 		if (!pat)
 			goto out;
@@ -11506,7 +11512,7 @@ static void __net_exit default_device_exit(struct net *net)
 
 		/* Push remaining network devices to init_net */
 		snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
-		if (__dev_get_by_name(&init_net, fb_name))
+		if (netdev_name_in_use(&init_net, fb_name))
 			snprintf(fb_name, IFNAMSIZ, "dev%%d");
 		err = dev_change_net_namespace(dev, &init_net, fb_name);
 		if (err) {
Signature · 1 line
2.31.1
#2 lore raw

PATCH 2/3 bonding: use the correct function to check for netdev name collision

In reply to Antoine Tenart

A new helper to detect if a net device name is in use was added. Use it here as the return reference from __dev_get_by_name was discarded.

---
 drivers/net/bonding/bond_sysfs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/bonding/bond_sysfs.c +2 -2
index b9e9842fed94..c48b77167fab 100644
@@ -811,8 +811,8 @@ int bond_create_sysfs(struct bond_net *bn)
 	 */
 	if (ret == -EEXIST) {
 		/* Is someone being kinky and naming a device bonding_master? */
-		if (__dev_get_by_name(bn->net,
-				      class_attr_bonding_masters.attr.name))
+		if (netdev_name_in_use(bn->net,
+				       class_attr_bonding_masters.attr.name))
 			pr_err("network device named %s already exists in sysfs\n",
 			       class_attr_bonding_masters.attr.name);
 		ret = 0;
Signature · 1 line
2.31.1
#3 lore raw

PATCH 3/3 ppp: use the correct function to check if a netdev name is in use

In reply to Antoine Tenart

A new helper to detect if a net device name is in use was added. Use it here as the return reference from __dev_get_by_name was discarded.

---
 drivers/net/ppp/ppp_generic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ppp/ppp_generic.c +1 -1
index fb52cd175b45..1180a0e2445f 100644
@@ -1161,7 +1161,7 @@ static int ppp_unit_register(struct ppp *ppp, int unit, bool ifname_is_set)
 		if (!ifname_is_set) {
 			while (1) {
 				snprintf(ppp->dev->name, IFNAMSIZ, "ppp%i", ret);
-				if (!__dev_get_by_name(ppp->ppp_net, ppp->dev->name))
+				if (!netdev_name_in_use(ppp->ppp_net, ppp->dev->name))
 					break;
 				unit_put(&pn->units_idr, ret);
 				ret = unit_get(&pn->units_idr, ppp, ret + 1);
Signature · 1 line
2.31.1
#4 lore raw
In reply to Antoine Tenart
This series was applied to netdev/net-next.git (master) by David S. Miller <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (master) by David S. Miller <[email protected]>:

Quoted 9 lines · On Thu, 7 Oct 2021 18:16:49 +0200 you wrote:

Hello,

This was initially part of an RFC series[1] but has value on its own; hence the standalone report. (It will also help in not having a series too large).

From patch 1:

[...]

Here is the summary with links:
- [net-next,1/3] net: introduce a function to check if a netdev name is in use

    https://git.kernel.org/netdev/net-next/c/75ea27d0d622
  - [net-next,2/3] bonding: use the correct function to check for netdev name collision
    https://git.kernel.org/netdev/net-next/c/caa9b35fadff
  - [net-next,3/3] ppp: use the correct function to check if a netdev name is in use
    https://git.kernel.org/netdev/net-next/c/d03eb9787d3a

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html