net: core: decouple ifalias get/set from rtnl lock
Subsystems (2)
PATCH 1/1 net: core: decouple ifalias get/set from rtnl lock
Device alias can be set by either rtnetlink (rtnl is held) or sysfs.
rtnetlink hold the rtnl mutex, sysfs acquires it for this purpose. Add an extra mutex for it plus a seqcount to get a consistent snapshot of the alias buffer.
This allows the sysfs path to not take rtnl and would later allow to not hold it when dumping ifalias.
--- include/linux/netdevice.h | 3 +- net/core/dev.c | 70 +++++++++++++++++++++++++++++++++++++++-------- net/core/net-sysfs.c | 14 ++++------ net/core/rtnetlink.c | 13 +++++++-- 4 files changed, 77 insertions(+), 23 deletions(-)
include/linux/netdevice.h +2 -1
@@ -1632,7 +1632,7 @@ enum netdev_priv_flags {struct net_device {char name[IFNAMSIZ ];struct hlist_node name_hlist; -char *ifalias; +char __rcu *ifalias;/* * I/O specific fields * FIXME: Merge these and struct ifmap into one
@@ -3275,6 +3275,7 @@ void __dev_notify_flags(struct net_device *, unsigned int old_flags, unsigned int gchanges);int dev_change_name (struct net_device *,const char *);int dev_set_alias (struct net_device *,const char *,size_t ); +int dev_get_alias (const struct net_device *,char *,size_t );int dev_change_net_namespace (struct net_device *,struct net *,const char *);int __dev_set_mtu (struct net_device *,int );int dev_set_mtu (struct net_device *,int );
net/core/dev.c +59 -11
100 lines not on this page · raw message
net/core/net-sysfs.c +5 -9
@@ -391,10 +391,7 @@ static ssize_t ifalias_store(struct device *dev, struct device_attribute *attr,if (len >0 && buf[len -1 ] =='\n' ) --count; -if (!rtnl_trylock ()) -return restart_syscall (); ret =dev_set_alias (netdev, buf, count); -rtnl_unlock ();return ret <0 ? ret : len; }
@@ -403,13 +400,12 @@ static ssize_t ifalias_show(struct device *dev, struct device_attribute *attr, char *buf) { const struct net_device *netdev = to_net_dev(dev); +char tmp[IFALIASZ ];ssize_t ret =0 ; -if (!rtnl_trylock ()) -return restart_syscall (); -if (netdev->ifalias) - ret =sprintf (buf,"%s\n" , netdev->ifalias); -rtnl_unlock (); + ret =dev_get_alias (netdev, tmp,sizeof (tmp)); +if (ret >0 ) + ret =sprintf (buf,"%s\n" , tmp);return ret; }static DEVICE_ATTR_RW (ifalias);
@@ -1488,7 +1484,7 @@ static void netdev_release(struct device *d)BUG_ON (dev->reg_state !=NETREG_RELEASED ); -kfree (dev->ifalias); +kfree (rcu_access_pointer (dev->ifalias));netdev_freemem (dev); }
net/core/rtnetlink.c +11 -2
@@ -1366,6 +1366,16 @@ static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev)return nla_put_u32 (skb,IFLA_LINK , ifindex); } +static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb, +struct net_device *dev) +{ +char buf[IFALIASZ ]; +int ret; + + ret =dev_get_alias (dev, buf,sizeof (buf)); +return ret >0 ?nla_put_string (skb,IFLA_IFALIAS , buf) :0 ; +} +static int rtnl_fill_link_netnsid (struct sk_buff *skb,const struct net_device *dev) {
@@ -1425,8 +1435,7 @@ static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,nla_put_u8 (skb,IFLA_CARRIER ,netif_carrier_ok (dev)) || (dev->qdisc &&nla_put_string (skb,IFLA_QDISC , dev->qdisc->ops->id)) || - (dev->ifalias && -nla_put_string (skb,IFLA_IFALIAS , dev->ifalias)) || +nla_put_ifalias (skb, dev) ||nla_put_u32 (skb,IFLA_CARRIER_CHANGES ,atomic_read (&dev->carrier_changes)) || nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
Signature · 1 line
2.13.5
David Miller
maintainerFrom: Florian Westphal <[email protected]>
Date: Fri, 29 Sep 2017 13:21:50 +0200
net/core/net-sysfs.c static void netdev_release(struct device *d) · line 1490
Quoted 9 lines
net/core/net-sysfs.c +1
-1@@ -1488,7 +1484,7 @@ static void netdev_release(struct device *d)BUG_ON (dev->reg_state !=NETREG_RELEASED ); -kfree (dev->ifalias); +kfree (rcu_access_pointer (dev->ifalias));netdev_freemem (dev); }
"kfree_rcu()" at least?
If the deal is that you don't need to do and RCU free because netdevice objects disappear synchronously, and you can therefore prove that no RCU based async access can occur to dev->ifalias, then you need to add a comment here.
Quoted 15 lines · David Miller
From: Florian Westphal <[email protected]>
Date: Fri, 29 Sep 2017 13:21:50 +0200net/core/net-sysfs.c static void netdev_release(struct device *d) · line 1490
Quoted 9 lines
net/core/net-sysfs.c +1
-1@@ -1488,7 +1484,7 @@ static void netdev_release(struct device *d)BUG_ON (dev->reg_state !=NETREG_RELEASED ); -kfree (dev->ifalias); +kfree (rcu_access_pointer (dev->ifalias));netdev_freemem (dev); }"
kfree_rcu()" at least?
Quoted 4 lines
If the deal is that you don't need to do and RCU free because netdevice objects disappear synchronously, and you can therefore prove that no RCU based async access can occur to
dev->ifalias, then you need to add a comment here.
Ok, I will add a comment.
netdev_release gets called via kobject_put() if this was last reference to the device. At that point the device has already been removed from all lists (and private destructor was invoked too).
Also, netdev_freemem frees the net_device memory immediately so no other cpu is allowed to use dev at this point.