Skip to contentSkip to conversation

vrf: A few cleanups

PATCH Superseded Ido Schimmel 3 participants 13 messages Mar 24 – Mar 25

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover vrf: A few cleanups

Perform a few cleanups in the VRF driver. Noticed these while reviewing a recent patch [1]. See individual patches for more details.

[1] https://lore.kernel.org/netdev/[email protected]/

Ido Schimmel (3):
vrf: Remove unnecessary NULL check
vrf: Use dst_dev_put() instead of using loopback device
vrf: Remove unnecessary synchronize_rcu() invocations

 drivers/net/vrf.c | 28 ++++------------------------
 1 file changed, 4 insertions(+), 24 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/3 vrf: Remove unnecessary NULL check

The VRF driver always allocates an IPv4 dst entry for a VRF device and prevents the device from being registered if the allocation fails.

Therefore, there is no need to check if the entry exists when tearing down a VRF device. Remove the check.

Note that the same is not true for the IPv6 dst entry. Its creation can be skipped if IPv6 is administratively disabled (i.e., 'ipv6.disable=1').

---
 drivers/net/vrf.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
drivers/net/vrf.c +5 -7
index 8c009bcaa8e7..0952ab6a2571 100644
@@ -1007,13 +1007,11 @@ static void vrf_rtable_release(struct net_device *dev, struct net_vrf *vrf)
 	/* move dev in dst's to loopback so this VRF device can be deleted
 	 * - based on dst_ifdown
 	 */
-	if (rth) {
-		dst = &rth->dst;
-		netdev_ref_replace(dst->dev, net->loopback_dev,
-				   &dst->dev_tracker, GFP_KERNEL);
-		dst->dev = net->loopback_dev;
-		dst_release(dst);
-	}
+	dst = &rth->dst;
+	netdev_ref_replace(dst->dev, net->loopback_dev,
+			   &dst->dev_tracker, GFP_KERNEL);
+	dst->dev = net->loopback_dev;
+	dst_release(dst);
 }
 
 static int vrf_rtable_create(struct net_device *dev)
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH net-next 1/3] vrf: Remove unnecessary NULL check

Quoted 10 lines · Ido Schimmel

The VRF driver always allocates an IPv4 dst entry for a VRF device and prevents the device from being registered if the allocation fails.

Therefore, there is no need to check if the entry exists when tearing down a VRF device. Remove the check.

Note that the same is not true for the IPv6 dst entry. Its creation can be skipped if IPv6 is administratively disabled (i.e., 'ipv6.disable=1').

Quoted 6 lines
---
 drivers/net/vrf.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
#3 lore raw

PATCH 2/3 vrf: Use dst_dev_put() instead of using loopback device

In reply to Ido Schimmel

Use dst_dev_put() to clean up the device referenced by the dst entry instead of partially open coding it. Internally, the helper uses the blackhole device instead of the loopback device.

---
 drivers/net/vrf.c | 24 ++++--------------------
 1 file changed, 4 insertions(+), 20 deletions(-)
drivers/net/vrf.c +4 -20
index 0952ab6a2571..bfc9ea91ac20 100644
@@ -751,21 +751,13 @@ static struct sk_buff *vrf_ip6_out(struct net_device *vrf_dev,
 static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf)
 {
 	struct rt6_info *rt6 = rtnl_dereference(vrf->rt6);
-	struct net *net = dev_net(dev);
-	struct dst_entry *dst;
 
 	RCU_INIT_POINTER(vrf->rt6, NULL);
 	synchronize_rcu();
 
-	/* move dev in dst's to loopback so this VRF device can be deleted
-	 * - based on dst_ifdown
-	 */
 	if (rt6) {
-		dst = &rt6->dst;
-		netdev_ref_replace(dst->dev, net->loopback_dev,
-				   &dst->dev_tracker, GFP_KERNEL);
-		dst->dev = net->loopback_dev;
-		dst_release(dst);
+		dst_dev_put(&rt6->dst);
+		dst_release(&rt6->dst);
 	}
 }
 
@@ -998,20 +990,12 @@ static struct sk_buff *vrf_l3_out(struct net_device *vrf_dev,
 static void vrf_rtable_release(struct net_device *dev, struct net_vrf *vrf)
 {
 	struct rtable *rth = rtnl_dereference(vrf->rth);
-	struct net *net = dev_net(dev);
-	struct dst_entry *dst;
 
 	RCU_INIT_POINTER(vrf->rth, NULL);
 	synchronize_rcu();
 
-	/* move dev in dst's to loopback so this VRF device can be deleted
-	 * - based on dst_ifdown
-	 */
-	dst = &rth->dst;
-	netdev_ref_replace(dst->dev, net->loopback_dev,
-			   &dst->dev_tracker, GFP_KERNEL);
-	dst->dev = net->loopback_dev;
-	dst_release(dst);
+	dst_dev_put(&rth->dst);
+	dst_release(&rth->dst);
 }
 
 static int vrf_rtable_create(struct net_device *dev)
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH net-next 2/3] vrf: Use dst_dev_put() instead of using loopback device

Quoted 10 lines · Ido Schimmel

Use dst_dev_put() to clean up the device referenced by the dst entry instead of partially open coding it. Internally, the helper uses the blackhole device instead of the loopback device.

---
 drivers/net/vrf.c | 24 ++++--------------------
 1 file changed, 4 insertions(+), 20 deletions(-)
#5 lore raw

PATCH 3/3 vrf: Remove unnecessary synchronize_rcu() invocations

In reply to Ido Schimmel

vrf_rtable_release() and vrf_rt6_release() are called as part of the VRF device's ndo_uninit() callback. As such, an RCU grace period passes between the removal of the VRF slaves and the closing of the VRF device until these functions are invoked.

Therefore, it should not be possible for any concurrent RCU readers to try and acquire a reference on the dst entry while we are potentially releasing the last reference via dst_release().

Given the above, the synchronize_rcu() invocations in these functions seem unnecessary and only add unnecessary delay when deleting VRF devices. Remove them.

---
 drivers/net/vrf.c | 2 --
 1 file changed, 2 deletions(-)
drivers/net/vrf.c +0 -2
index bfc9ea91ac20..75edb8eba87f 100644
@@ -753,7 +753,6 @@ static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf)
 	struct rt6_info *rt6 = rtnl_dereference(vrf->rt6);
 
 	RCU_INIT_POINTER(vrf->rt6, NULL);
-	synchronize_rcu();
 
 	if (rt6) {
 		dst_dev_put(&rt6->dst);
@@ -992,7 +991,6 @@ static void vrf_rtable_release(struct net_device *dev, struct net_vrf *vrf)
 	struct rtable *rth = rtnl_dereference(vrf->rth);
 
 	RCU_INIT_POINTER(vrf->rth, NULL);
-	synchronize_rcu();
 
 	dst_dev_put(&rth->dst);
 	dst_release(&rth->dst);
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 3/3
#6 lore raw

Re: [PATCH net-next 3/3] vrf: Remove unnecessary synchronize_rcu() invocations

Quoted 13 lines · Ido Schimmel

vrf_rtable_release() and vrf_rt6_release() are called as part of the VRF device's ndo_uninit() callback. As such, an RCU grace period passes between the removal of the VRF slaves and the closing of the VRF device until these functions are invoked.

Therefore, it should not be possible for any concurrent RCU readers to try and acquire a reference on the dst entry while we are potentially releasing the last reference via dst_release().

Given the above, the synchronize_rcu() invocations in these functions seem unnecessary and only add unnecessary delay when deleting VRF devices. Remove them.

Quoted 6 lines
---
 drivers/net/vrf.c | 2 --
 1 file changed, 2 deletions(-)
#7 lore raw

Re: [PATCH net-next 3/3] vrf: Remove unnecessary synchronize_rcu() invocations

In reply to Ido Schimmel
Quoted 25 lines · Ido Schimmel

vrf_rtable_release() and vrf_rt6_release() are called as part of the VRF device's ndo_uninit() callback. As such, an RCU grace period passes between the removal of the VRF slaves and the closing of the VRF device until these functions are invoked.

Therefore, it should not be possible for any concurrent RCU readers to try and acquire a reference on the dst entry while we are potentially releasing the last reference via dst_release().

Given the above, the synchronize_rcu() invocations in these functions seem unnecessary and only add unnecessary delay when deleting VRF devices. Remove them.

---
 drivers/net/vrf.c | 2 --
 1 file changed, 2 deletions(-)
drivers/net/vrf.c +0 -0
index bfc9ea91ac20..75edb8eba87f 100644

drivers/net/vrf.c static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf) · line 755

Quoted 3 lines
drivers/net/vrf.c +0 -0
@@ -753,7 +753,6 @@ static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf)
        struct rt6_info *rt6 = rtnl_dereference(vrf->rt6);
 
        RCU_INIT_POINTER(vrf->rt6, NULL);

We could argue that the vrf->rt6 clearing is not needed, or should be done before the RCU grace period mentioned in the changelog started ?

Quoted 15 lines
drivers/net/vrf.c +0 -3
-       synchronize_rcu();
 
        if (rt6) {
                dst_dev_put(&rt6->dst);
@@ -992,7 +991,6 @@ static void vrf_rtable_release(struct net_device *dev, struct net_vrf *vrf)
        struct rtable *rth = rtnl_dereference(vrf->rth);
 
        RCU_INIT_POINTER(vrf->rth, NULL);
-       synchronize_rcu();
 
        dst_dev_put(&rth->dst);
        dst_release(&rth->dst);
--

2.53.0

maintainer
#8 lore raw

Re: [PATCH net-next 3/3] vrf: Remove unnecessary synchronize_rcu() invocations

Quoted 28 lines · Eric Dumazet
Quoted 27 lines · Ido Schimmel

vrf_rtable_release() and vrf_rt6_release() are called as part of the VRF device's ndo_uninit() callback. As such, an RCU grace period passes between the removal of the VRF slaves and the closing of the VRF device until these functions are invoked.

Therefore, it should not be possible for any concurrent RCU readers to try and acquire a reference on the dst entry while we are potentially releasing the last reference via dst_release().

Given the above, the synchronize_rcu() invocations in these functions seem unnecessary and only add unnecessary delay when deleting VRF devices. Remove them.

---
 drivers/net/vrf.c | 2 --
 1 file changed, 2 deletions(-)
drivers/net/vrf.c +0 -0
index bfc9ea91ac20..75edb8eba87f 100644
@@ -753,7 +753,6 @@ static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf)
        struct rt6_info *rt6 = rtnl_dereference(vrf->rt6);
 
Quoted 6 lines

drivers/net/vrf.c static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf) · line 755

Quoted 1 line
drivers/net/vrf.c +0 -0
        RCU_INIT_POINTER(vrf->rt6, NULL);

We could argue that the vrf->rt6 clearing is not needed, or should be done before the RCU grace period mentioned in the changelog started ?

AIUI Ido's point is that the synchronize_net() in unregister_netdevice_many_notify() occurs before the ndo_uninit call making the synchronize_rcu here unnecessary. There is also a second synchronize_net after it.

I get your point about skipping the clearing the poiniters.

#9 lore raw

Re: [PATCH net-next 3/3] vrf: Remove unnecessary synchronize_rcu() invocations

Quoted 36 lines · David Ahern
Quoted 28 lines · Eric Dumazet
Quoted 27 lines · Ido Schimmel

vrf_rtable_release() and vrf_rt6_release() are called as part of the VRF device's ndo_uninit() callback. As such, an RCU grace period passes between the removal of the VRF slaves and the closing of the VRF device until these functions are invoked.

Therefore, it should not be possible for any concurrent RCU readers to try and acquire a reference on the dst entry while we are potentially releasing the last reference via dst_release().

Given the above, the synchronize_rcu() invocations in these functions seem unnecessary and only add unnecessary delay when deleting VRF devices. Remove them.

---
 drivers/net/vrf.c | 2 --
 1 file changed, 2 deletions(-)
drivers/net/vrf.c +0 -0
index bfc9ea91ac20..75edb8eba87f 100644
@@ -753,7 +753,6 @@ static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf)
        struct rt6_info *rt6 = rtnl_dereference(vrf->rt6);
 
Quoted 6 lines

drivers/net/vrf.c static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf) · line 755

Quoted 1 line
drivers/net/vrf.c +0 -0
        RCU_INIT_POINTER(vrf->rt6, NULL);

We could argue that the vrf->rt6 clearing is not needed, or should be done before the RCU grace period mentioned in the changelog started ?

Quoted 6 lines

AIUI Ido's point is that the synchronize_net() in unregister_netdevice_many_notify() occurs before the ndo_uninit call making the synchronize_rcu here unnecessary. There is also a second synchronize_net after it.

I get your point about skipping the clearing the poiniters.

We can probably remove more code than just the synchronize_rcu() calls. Looking into that right now.

#10 lore raw

Re: [PATCH net-next 3/3] vrf: Remove unnecessary synchronize_rcu() invocations

Quoted 43 lines · Ido Schimmel
Quoted 41 lines · David Ahern
Quoted 28 lines · Eric Dumazet
Quoted 27 lines · Ido Schimmel

vrf_rtable_release() and vrf_rt6_release() are called as part of the VRF device's ndo_uninit() callback. As such, an RCU grace period passes between the removal of the VRF slaves and the closing of the VRF device until these functions are invoked.

Therefore, it should not be possible for any concurrent RCU readers to try and acquire a reference on the dst entry while we are potentially releasing the last reference via dst_release().

Given the above, the synchronize_rcu() invocations in these functions seem unnecessary and only add unnecessary delay when deleting VRF devices. Remove them.

---
 drivers/net/vrf.c | 2 --
 1 file changed, 2 deletions(-)
drivers/net/vrf.c +0 -0
index bfc9ea91ac20..75edb8eba87f 100644
@@ -753,7 +753,6 @@ static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf)
        struct rt6_info *rt6 = rtnl_dereference(vrf->rt6);
 
Quoted 6 lines

drivers/net/vrf.c static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf) · line 755

Quoted 1 line
drivers/net/vrf.c +0 -0
        RCU_INIT_POINTER(vrf->rt6, NULL);

We could argue that the vrf->rt6 clearing is not needed, or should be done before the RCU grace period mentioned in the changelog started ?

AIUI Ido's point is that the synchronize_net() in unregister_netdevice_many_notify() occurs before the ndo_uninit call making the synchronize_rcu here unnecessary. There is also a second synchronize_net after it.

Quoted 4 lines
Quoted 1 line

I get your point about skipping the clearing the poiniters.

We can probably remove more code than just the synchronize_rcu() calls. Looking into that right now.

This was a minor point, the synchronize_rcu() is far more interesting of course.

Thanks.

#11 lore raw

Re: [PATCH net-next 3/3] vrf: Remove unnecessary synchronize_rcu() invocations

Quoted 44 lines · Eric Dumazet
Quoted 43 lines · Ido Schimmel
Quoted 41 lines · David Ahern
Quoted 28 lines · Eric Dumazet
Quoted 27 lines · Ido Schimmel

vrf_rtable_release() and vrf_rt6_release() are called as part of the VRF device's ndo_uninit() callback. As such, an RCU grace period passes between the removal of the VRF slaves and the closing of the VRF device until these functions are invoked.

Therefore, it should not be possible for any concurrent RCU readers to try and acquire a reference on the dst entry while we are potentially releasing the last reference via dst_release().

Given the above, the synchronize_rcu() invocations in these functions seem unnecessary and only add unnecessary delay when deleting VRF devices. Remove them.

---
 drivers/net/vrf.c | 2 --
 1 file changed, 2 deletions(-)
drivers/net/vrf.c +0 -0
index bfc9ea91ac20..75edb8eba87f 100644
@@ -753,7 +753,6 @@ static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf)
        struct rt6_info *rt6 = rtnl_dereference(vrf->rt6);
 
Quoted 6 lines

drivers/net/vrf.c static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf) · line 755

Quoted 1 line
drivers/net/vrf.c +0 -0
        RCU_INIT_POINTER(vrf->rt6, NULL);

We could argue that the vrf->rt6 clearing is not needed, or should be done before the RCU grace period mentioned in the changelog started ?

AIUI Ido's point is that the synchronize_net() in unregister_netdevice_many_notify() occurs before the ndo_uninit call making the synchronize_rcu here unnecessary. There is also a second synchronize_net after it.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

I get your point about skipping the clearing the poiniters.

We can probably remove more code than just the synchronize_rcu() calls. Looking into that right now.

This was a minor point, the synchronize_rcu() is far more interesting of course.

Did some more digging. I don't see a reason for keeping the dst pointers RCU protected. I can replace patch #3 with the following one in v2:

https://github.com/idosch/linux/commit/3fbc1cad610df95cfcde117394c2c3d9e9a04e65.patch