git: 381601cfcb56 - stable/13 - ifnet: make if_alloc_domain() never fail

From: Zhenlei Huang <zlei_at_FreeBSD.org>
Date: Tue, 08 Oct 2024 04:01:56 UTC
The branch stable/13 has been updated by zlei:

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

commit 381601cfcb56a98a14dcbfddb1b2be0ea7a5192c
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2021-11-23 03:49:57 +0000
Commit:     Zhenlei Huang <zlei@FreeBSD.org>
CommitDate: 2024-10-08 04:00:59 +0000

    ifnet: make if_alloc_domain() never fail
    
    The last consumer of if_com_alloc() is firewire.  It never fails
    to allocate.  Most likely the if_com_alloc() KPI will go away
    together with if_fwip(), less likely new consumers of if_com_alloc()
    will be added, but they would need to follow the no fail KPI.
    
    MFC note: As for stable/13, there is one additional consumer sppp,
    which also never fails to allocate. This MFCing is mainly to keep
    behavioral compatibility of if_alloc_domain() and its wrappers
    if_alloc(), if_alloc_dev(), and if_gethandle() with stable/14 and
    onward branches. 3rd party drivers should be ready for this for years
    as this behavioral change was done in stable/14 at November 22 2021.
    As a good effect new drivers to be MFCed to stable/13 do not have to
    conditionally check failure from if_alloc() for stable/13.
    
    (cherry picked from commit 4787572d0580c6fdf818fd64efa3089de88720f0)
---
 sys/net/if.c | 22 +++++-----------------
 1 file changed, 5 insertions(+), 17 deletions(-)

diff --git a/sys/net/if.c b/sys/net/if.c
index a7aed084143c..78da7cf02bfb 100644
--- a/sys/net/if.c
+++ b/sys/net/if.c
@@ -399,7 +399,7 @@ ifindex_alloc(void **old)
 }
 
 static void
-ifindex_free_locked(u_short idx)
+ifindex_free(u_short idx)
 {
 
 	IFNET_WLOCK_ASSERT();
@@ -410,15 +410,6 @@ ifindex_free_locked(u_short idx)
 		V_if_index--;
 }
 
-static void
-ifindex_free(u_short idx)
-{
-
-	IFNET_WLOCK();
-	ifindex_free_locked(idx);
-	IFNET_WUNLOCK();
-}
-
 static void
 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
 {
@@ -631,11 +622,8 @@ if_alloc_domain(u_char type, int numa_domain)
 #endif
 	if (if_com_alloc[type] != NULL) {
 		ifp->if_l2com = if_com_alloc[type](type, ifp);
-		if (ifp->if_l2com == NULL) {
-			free(ifp, M_IFNET);
-			ifindex_free(idx);
-			return (NULL);
-		}
+		KASSERT(ifp->if_l2com, ("%s: if_com_alloc[%u] failed", __func__,
+		    type));
 	}
 
 	IF_ADDR_LOCK_INIT(ifp);
@@ -730,7 +718,7 @@ if_free(struct ifnet *ifp)
 	KASSERT(ifp == ifnet_byindex(ifp->if_index),
 	    ("%s: freeing unallocated ifnet", ifp->if_xname));
 
-	ifindex_free_locked(ifp->if_index);
+	ifindex_free(ifp->if_index);
 	IFNET_WUNLOCK();
 
 	if (refcount_release(&ifp->if_refcount))
@@ -1334,7 +1322,7 @@ if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
 	 * or we'd lock on one vnet and unlock on another.
 	 */
 	IFNET_WLOCK();
-	ifindex_free_locked(ifp->if_index);
+	ifindex_free(ifp->if_index);
 	IFNET_WUNLOCK();
 
 	/*