git: f1fae67afbb1 - main - ifnet: vnet_if_return(): Avoid unnecessary recursive acquisition of ifnet_detach_sxlock

From: Zhenlei Huang <zlei_at_FreeBSD.org>
Date: Mon, 13 Apr 2026 04:39:46 UTC
The branch main has been updated by zlei:

URL: https://cgit.FreeBSD.org/src/commit/?id=f1fae67afbb13a41d488d0e0ec66b1805925019c

commit f1fae67afbb13a41d488d0e0ec66b1805925019c
Author:     Zhenlei Huang <zlei@FreeBSD.org>
AuthorDate: 2026-04-13 04:38:43 +0000
Commit:     Zhenlei Huang <zlei@FreeBSD.org>
CommitDate: 2026-04-13 04:38:43 +0000

    ifnet: vnet_if_return(): Avoid unnecessary recursive acquisition of ifnet_detach_sxlock
    
    vnet_if_return() will be invocked by vnet_sysuninit() on vnet destructing,
    while the lock ifnet_detach_sxlock has been acquired in vnet_destroy()
    already.
    
    With this change the order of locking is more clear. There should be no
    functional change.
    
    Reviewed by:    pouria
    Fixes:          868bf82153e8 if: avoid interface destroy race
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D56288
---
 sys/net/if.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/sys/net/if.c b/sys/net/if.c
index 519b23750b52..73138716cf46 100644
--- a/sys/net/if.c
+++ b/sys/net/if.c
@@ -479,6 +479,8 @@ vnet_if_return(const void *unused __unused)
 
 	i = 0;
 
+	/* The lock has already been aquired in vnet_destroy() */
+	sx_assert(&ifnet_detach_sxlock, SX_XLOCKED);
 	/*
 	 * We need to protect our access to the V_ifnet tailq. Ordinarily we'd
 	 * enter NET_EPOCH, but that's not possible, because if_vmove() calls
@@ -507,9 +509,7 @@ vnet_if_return(const void *unused __unused)
 	IFNET_WUNLOCK();
 
 	for (int j = 0; j < i; j++) {
-		sx_xlock(&ifnet_detach_sxlock);
 		if_vmove(pending[j], pending[j]->if_home_vnet);
-		sx_xunlock(&ifnet_detach_sxlock);
 	}
 
 	free(pending, M_IFNET);