From nobody Thu Oct 10 10:03:22 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4XPQLf5Df9z5Ympt; Thu, 10 Oct 2024 10:03:22 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4XPQLf4Y5mz4sTg; Thu, 10 Oct 2024 10:03:22 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1728554602; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=LuHvfl5opN5E9N5BbEXavgmnes8iGhd5iS9OMyr73Rc=; b=iUJwlkKCtJj4NMDfrYX2QM+P5QdL99A0gQUoxvSU1h6UpU8bxbfjfOW4bAKoxT+GaGPR9i 5UChqqOsChMLeJZXo+LLwjyHCo8WdGhczLStfguZmCKD1HCTK4/N3lU2+VvMBKRo8/JRNT BpHfzjhk2cPSqVXdMRt69uoS6GscUkHL8VQTeH32uIMWgZrI9hv2LeS3O1wzIPC92mcFA0 tGUVI9DvZljv59a5rKCpnQbydqArSrSECxi9VtwCq+hUcXjppRuAfL1N1iUoBdBrH94mJz lg7M0xjbUm9Pd+M3bltwwYKLjDA+jMJs5N/3CiYQI3+JVgv4rtBm41OB7Vlb6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1728554602; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=LuHvfl5opN5E9N5BbEXavgmnes8iGhd5iS9OMyr73Rc=; b=kZon/edl+uxPc6dCpJyshNTI5+xLtwzBwjsajV1fao0WIv4UsnHtZIh4v1HYH/OHKa7hHo 1+0PLXmHU6waIGaW8GwfryyHlqbzon0rzYnvuusmk89o/sz/4A67wDY3XR4l4YAQWDNKuS wDL+iy2bBlPC/lhgAkTCa32YgXhBMndoMUUK1tBjQpcHuJKD3iJ3Iy7mnYSeFfMt5GjRgk pjVPrD943/Pt1cVGrb36dCD97yV3/f/uH/i3fRGMh98l+G/7M80cNWTL+rUNhMI5n6hxOD QYSBtyYBu8WV0CqlqLLbg6XFUz3iU9rbcpPxK6Ux9Uez7KFE5dPkibtxl4rpug== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1728554602; a=rsa-sha256; cv=none; b=w2hA7/XacwNCjMLczf3Acehet1sseG4Kv6fM3rI/bra7z9YZtrFFc70pnzvq4/TKa6n6dL BplEbQ6xyKTAMcV/mOTMXmUXsyFDLEs9Xtr2rDHICebAWjNZOdtNrrTdE1IL8GSc55olD1 ugzuAUp7kZkgnTD0yLrRczOS1LjCoFKPqQHoSd2dTMdnAM8J7SL0Q+LLgXayQk7DQx15Th XmFrWei8Az+oFw1BzszxtLTlLbH1W4DgaePZmYFb47oSg4OFIO/jxEbEKGyH90hVeC+e1Y DyuG/LHmAcerOWD0JtmLfauHra2uclqhiWdU5xTu2O7w1SYbi4x81uvMFydnFA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4XPQLf3rlYzt25; Thu, 10 Oct 2024 10:03:22 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 49AA3MbF052649; Thu, 10 Oct 2024 10:03:22 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 49AA3M70052646; Thu, 10 Oct 2024 10:03:22 GMT (envelope-from git) Date: Thu, 10 Oct 2024 10:03:22 GMT Message-Id: <202410101003.49AA3M70052646@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Zhenlei Huang Subject: git: 7afcdd13a49c - stable/13 - ifnet: allocate index at the end of if_alloc_domain() List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: zlei X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 7afcdd13a49c50c5c2396543ddeba1c283202eb7 Auto-Submitted: auto-generated The branch stable/13 has been updated by zlei: URL: https://cgit.FreeBSD.org/src/commit/?id=7afcdd13a49c50c5c2396543ddeba1c283202eb7 commit 7afcdd13a49c50c5c2396543ddeba1c283202eb7 Author: Gleb Smirnoff AuthorDate: 2021-12-04 17:49:35 +0000 Commit: Zhenlei Huang CommitDate: 2024-10-10 10:00:47 +0000 ifnet: allocate index at the end of if_alloc_domain() Now that if_alloc_domain() never fails and actually doesn't expose ifnet to outside we can eliminate IFNET_HOLD and two step index allocation. Reviewed by: kp Differential revision: https://reviews.freebsd.org/D33259 (cherry picked from commit 8062e5759cb4886ad4630d52c212d8ca77ef9c95) --- sys/net/if.c | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/sys/net/if.c b/sys/net/if.c index 78da7cf02bfb..4182f22c848e 100644 --- a/sys/net/if.c +++ b/sys/net/if.c @@ -323,13 +323,6 @@ struct sx ifnet_detach_sxlock; SX_SYSINIT_FLAGS(ifnet_detach, &ifnet_detach_sxlock, "ifnet_detach_sx", SX_RECURSE); -/* - * The allocation of network interfaces is a rather non-atomic affair; we - * need to select an index before we are ready to expose the interface for - * use, so will use this pointer value to indicate reservation. - */ -#define IFNET_HOLD (void *)(uintptr_t)(-1) - #ifdef VIMAGE #define VNET_IS_SHUTTING_DOWN(_vnet) \ ((_vnet)->vnet_shutdown && (_vnet)->vnet_state < SI_SUB_VNET_DONE) @@ -345,13 +338,11 @@ MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address"); struct ifnet * ifnet_byindex(u_short idx) { - struct ifnet *ifp; if (__predict_false(idx > V_if_index)) return (NULL); - ifp = *(struct ifnet * const volatile *)(V_ifindex_table + idx); - return (__predict_false(ifp == IFNET_HOLD) ? NULL : ifp); + return (V_ifindex_table[idx]); } struct ifnet * @@ -414,6 +405,7 @@ static void ifnet_setbyindex(u_short idx, struct ifnet *ifp) { + ifp->if_index = idx; V_ifindex_table[idx] = ifp; } @@ -602,18 +594,6 @@ if_alloc_domain(u_char type, int numa_domain) else ifp = malloc_domainset(sizeof(struct ifnet), M_IFNET, DOMAINSET_PREF(numa_domain), M_WAITOK | M_ZERO); - restart: - IFNET_WLOCK(); - idx = ifindex_alloc(&old); - if (__predict_false(idx == USHRT_MAX)) { - IFNET_WUNLOCK(); - epoch_wait_preempt(net_epoch_preempt); - free(old, M_IFNET); - goto restart; - } - ifnet_setbyindex(idx, IFNET_HOLD); - IFNET_WUNLOCK(); - ifp->if_index = idx; ifp->if_type = type; ifp->if_alloctype = type; ifp->if_numa_domain = numa_domain; @@ -644,7 +624,19 @@ if_alloc_domain(u_char type, int numa_domain) ifp->if_counters[i] = counter_u64_alloc(M_WAITOK); ifp->if_get_counter = if_get_counter_default; ifp->if_pcp = IFNET_PCP_NONE; - ifnet_setbyindex(ifp->if_index, ifp); + +restart: + IFNET_WLOCK(); + idx = ifindex_alloc(&old); + if (__predict_false(idx == USHRT_MAX)) { + IFNET_WUNLOCK(); + epoch_wait_preempt(net_epoch_preempt); + free(old, M_IFNET); + goto restart; + } + ifnet_setbyindex(idx, ifp); + IFNET_WUNLOCK(); + return (ifp); }