From patchwork Wed Feb 26 19:25:45 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13993064 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-fw-80007.amazon.com (smtp-fw-80007.amazon.com [99.78.197.218]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3347623906A for ; Wed, 26 Feb 2025 19:27:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=99.78.197.218 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740598038; cv=none; b=bEzj1SBzvrg5bYPY9QDQ0ZuTeyDAAUuEX81CMheG9y3SNqUIvnuSY8FMdXW+TdjPtnu6H78HQ461QNgbSKR2b215hbePtKIQ9naOQRTHI1fCJn43A16Pye/Y7Fek6DI18a3c/SwPlUaQOaugMem6X6l1nuefj3xbg/ha8BgMf4U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740598038; c=relaxed/simple; bh=Mh4yX1AeJRGXFO8labW/8wNKYxfPPvvpiVMNOWdFQbQ=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=EC2Z4Q1jR0Xax+KD9AVDIk5BfbGa5oKAtIS7PI9Hh6FcpSqQJWLcrhrx2+EfLu/XA2GT1YwO9bOAbVzv+y1CmV7C3EiaPyizBVS2K+R+bxN4f3GQaI+IbCxA9qlT7BRUwRKLzrtteNYgxRO4/0nepLIMckZUWJ/FoNz0o9LWfx8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.co.jp; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=UDQvKoF3; arc=none smtp.client-ip=99.78.197.218 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.jp Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="UDQvKoF3" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1740598034; x=1772134034; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=LaF7zTDJJc+Ngr1OO7w76VZM5DKJu+dtBChFc+/axy0=; b=UDQvKoF3W+FsU9d3Xbb3t9568KJ/B3SJFzAmGXPR5K9BBUKMFytndEIh T2oThQIfmo6bhyiIgWaiybTO9b2BKNu9iekZ3j32W7t0rRXfZlDoSP1Ae Jhz/CzXYklEXkGjNPsaBhDJkNqq3gXOsL8PQ9ikK9Vxko12SkVWPIC+ah U=; X-IronPort-AV: E=Sophos;i="6.13,318,1732579200"; d="scan'208";a="381126428" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.25.36.210]) by smtp-border-fw-80007.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2025 19:26:33 +0000 Received: from EX19MTAUWA001.ant.amazon.com [10.0.7.35:38450] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.35.58:2525] with esmtp (Farcaster) id 634e7c24-431f-4165-b3c9-73be919ba7c7; Wed, 26 Feb 2025 19:26:33 +0000 (UTC) X-Farcaster-Flow-ID: 634e7c24-431f-4165-b3c9-73be919ba7c7 Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX19MTAUWA001.ant.amazon.com (10.250.64.204) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Wed, 26 Feb 2025 19:26:33 +0000 Received: from 6c7e67bfbae3.amazon.com (10.187.170.36) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Wed, 26 Feb 2025 19:26:30 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , David Ahern , Eric Dumazet , Jakub Kicinski , "Paolo Abeni" , Simon Horman CC: Kuniyuki Iwashima , Kuniyuki Iwashima , Subject: [PATCH v2 net-next 01/12] ipv4: fib: Use cached net in fib_inetaddr_event(). Date: Wed, 26 Feb 2025 11:25:45 -0800 Message-ID: <20250226192556.21633-2-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250226192556.21633-1-kuniyu@amazon.com> References: <20250226192556.21633-1-kuniyu@amazon.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D032UWA002.ant.amazon.com (10.13.139.81) To EX19D004ANA001.ant.amazon.com (10.37.240.138) X-Patchwork-Delegate: kuba@kernel.org net is available in fib_inetaddr_event(), let's use it. Signed-off-by: Kuniyuki Iwashima Reviewed-by: Eric Dumazet --- net/ipv4/fib_frontend.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/net/ipv4/fib_frontend.c b/net/ipv4/fib_frontend.c index 272e42d81323..6730e2034cf8 100644 --- a/net/ipv4/fib_frontend.c +++ b/net/ipv4/fib_frontend.c @@ -1450,7 +1450,7 @@ static int fib_inetaddr_event(struct notifier_block *this, unsigned long event, fib_sync_up(dev, RTNH_F_DEAD); #endif atomic_inc(&net->ipv4.dev_addr_genid); - rt_cache_flush(dev_net(dev)); + rt_cache_flush(net); break; case NETDEV_DOWN: fib_del_ifaddr(ifa, NULL); @@ -1461,7 +1461,7 @@ static int fib_inetaddr_event(struct notifier_block *this, unsigned long event, */ fib_disable_ip(dev, event, true); } else { - rt_cache_flush(dev_net(dev)); + rt_cache_flush(net); } break; } From patchwork Wed Feb 26 19:25:46 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13993065 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-fw-80006.amazon.com (smtp-fw-80006.amazon.com [99.78.197.217]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2F740238D5A for ; Wed, 26 Feb 2025 19:28:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=99.78.197.217 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740598093; cv=none; b=Ba8Z30PC8/kRszEsZk78FX1qW0PxlyUV3CwUxPC+Ic3l2GZwQ0vRQ8807xfm5COrFaGvQqguPqWGORLnz1GZElwPmOzKn79jfEwBENdmtjuhnvE94Voe6pqPb3W0CQVz129tf0KnkdS7/jB9w+1oH4ly0SsobeRGbI3QuLu00HA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740598093; c=relaxed/simple; bh=8PloxJJ2yYUhVRhdsTM0QNP3l0+hYHSdQydXTCoBo1Y=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ns/FqAoDjlaIxpfU2d30GPbOklxF9AsjLtXR/s7MuXda2Yh2Y/dSTjyhEyweYOXyEI5uT3cAS9OnaxTfyfE7vAHehn/XKysM5KqrursPTxE9R9gVDrnBIHqJ22JGd188hsI9c5u2UGFOCUM+Hl0MDjQMmuitInUjKmOLPQflC/Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.co.jp; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=TnGDzAef; arc=none smtp.client-ip=99.78.197.217 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.jp Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="TnGDzAef" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1740598092; x=1772134092; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=IU2uLDZXZ1whHt75f5UbrdE5+EvlvFkI84Hl+q6Qp9w=; b=TnGDzAefQsrxXECVOl3UXHsub1PClZOGE6hRJBf+IOSZwwc8wyDoqipW 5fnZeX2CUboAVvBma8N4G0oMvnjQQeJrEkUCJ3sP/uuyuqoEnCN1EM8g2 /3E/K+fk19BtPhSpA+nuDBYUYrRi+dl1Vj7xDvL18KKJ65M7EoXBAiBWF U=; X-IronPort-AV: E=Sophos;i="6.13,318,1732579200"; d="scan'208";a="26358298" Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.25.36.214]) by smtp-border-fw-80006.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2025 19:27:07 +0000 Received: from EX19MTAUWC001.ant.amazon.com [10.0.7.35:42357] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.11.69:2525] with esmtp (Farcaster) id df194479-fe90-40a5-89f3-a072ac933237; Wed, 26 Feb 2025 19:27:06 +0000 (UTC) X-Farcaster-Flow-ID: df194479-fe90-40a5-89f3-a072ac933237 Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX19MTAUWC001.ant.amazon.com (10.250.64.174) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Wed, 26 Feb 2025 19:26:58 +0000 Received: from 6c7e67bfbae3.amazon.com (10.187.170.36) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Wed, 26 Feb 2025 19:26:55 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , David Ahern , Eric Dumazet , Jakub Kicinski , "Paolo Abeni" , Simon Horman CC: Kuniyuki Iwashima , Kuniyuki Iwashima , Subject: [PATCH v2 net-next 02/12] ipv4: fib: Allocate fib_info_hash[] and fib_info_laddrhash[] by kvmalloc_array(). Date: Wed, 26 Feb 2025 11:25:46 -0800 Message-ID: <20250226192556.21633-3-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250226192556.21633-1-kuniyu@amazon.com> References: <20250226192556.21633-1-kuniyu@amazon.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D036UWB001.ant.amazon.com (10.13.139.133) To EX19D004ANA001.ant.amazon.com (10.37.240.138) X-Patchwork-Delegate: kuba@kernel.org Both fib_info_hash[] and fib_info_laddrhash[] are hash tables for struct fib_info and are allocated by kvzmalloc() separately. Let's replace the two kvzmalloc() calls with kvmalloc_array() to remove the fib_info_laddrhash pointer later. Note that fib_info_hash_alloc() allocates a new hash table based on fib_info_hash_bits because we will remove fib_info_hash_size later. Signed-off-by: Kuniyuki Iwashima Reviewed-by: Eric Dumazet --- net/ipv4/fib_semantics.c | 44 ++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/net/ipv4/fib_semantics.c b/net/ipv4/fib_semantics.c index d2cee5c314f5..a68a4eb5e0d1 100644 --- a/net/ipv4/fib_semantics.c +++ b/net/ipv4/fib_semantics.c @@ -357,6 +357,19 @@ static inline unsigned int fib_info_hashfn(struct fib_info *fi) return fib_info_hashfn_result(fi->fib_net, val); } +static struct hlist_head *fib_info_hash_alloc(unsigned int hash_bits) +{ + /* The second half is used for prefsrc */ + return kvmalloc_array((1 << hash_bits) * 2, + sizeof(struct hlist_head *), + GFP_KERNEL | __GFP_ZERO); +} + +static void fib_info_hash_free(struct hlist_head *head) +{ + kvfree(head); +} + /* no metrics, only nexthop id */ static struct fib_info *fib_find_info_nh(struct net *net, const struct fib_config *cfg) @@ -1249,9 +1262,9 @@ fib_info_laddrhash_bucket(const struct net *net, __be32 val) } static void fib_info_hash_move(struct hlist_head *new_info_hash, - struct hlist_head *new_laddrhash, unsigned int new_size) { + struct hlist_head *new_laddrhash = new_info_hash + new_size; struct hlist_head *old_info_hash, *old_laddrhash; unsigned int old_size = fib_info_hash_size; unsigned int i; @@ -1293,8 +1306,7 @@ static void fib_info_hash_move(struct hlist_head *new_info_hash, } } - kvfree(old_info_hash); - kvfree(old_laddrhash); + fib_info_hash_free(old_info_hash); } __be32 fib_info_update_nhc_saddr(struct net *net, struct fib_nh_common *nhc, @@ -1412,22 +1424,18 @@ struct fib_info *fib_create_info(struct fib_config *cfg, err = -ENOBUFS; if (fib_info_cnt >= fib_info_hash_size) { - unsigned int new_size = fib_info_hash_size << 1; struct hlist_head *new_info_hash; - struct hlist_head *new_laddrhash; - size_t bytes; - - if (!new_size) - new_size = 16; - bytes = (size_t)new_size * sizeof(struct hlist_head *); - new_info_hash = kvzalloc(bytes, GFP_KERNEL); - new_laddrhash = kvzalloc(bytes, GFP_KERNEL); - if (!new_info_hash || !new_laddrhash) { - kvfree(new_info_hash); - kvfree(new_laddrhash); - } else { - fib_info_hash_move(new_info_hash, new_laddrhash, new_size); - } + unsigned int new_hash_bits; + + if (!fib_info_hash_bits) + new_hash_bits = 4; + else + new_hash_bits = fib_info_hash_bits + 1; + + new_info_hash = fib_info_hash_alloc(new_hash_bits); + if (new_info_hash) + fib_info_hash_move(new_info_hash, 1 << new_hash_bits); + if (!fib_info_hash_size) goto failure; } From patchwork Wed Feb 26 19:25:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13993080 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-fw-80007.amazon.com (smtp-fw-80007.amazon.com [99.78.197.218]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A4AF524394B for ; Wed, 26 Feb 2025 19:30:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=99.78.197.218 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740598246; cv=none; b=haYJElpAPtO6sUXl7b7TkM0kYVe3aTDE2i++NarimaGeE8Nwc2IXYWCGnrLYVsUjY75joLP5SKO5aQW60ROHuHOyaLrwYozUuqY+YD4ljO+iWiX5YyMdZdfILWNm0Wvu4zbEh39z8hQFYDs6L/PZC5p2suqkBmNn0JWqjyeiQG0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740598246; c=relaxed/simple; bh=X7HgU2SooqwsdZJaiSDCFA8LRp4qnUVmYOCsQPycZx4=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=kSzuGw1r/U04kihZNziQ57YJ7WJKC9EcyvBTE5ZjH37DO+/mTL4JcSbZIo9WIDGApxUiUtmALl1Mn/Sm/zX9LfrRaUnCHcYw6aKCwf9GUO91A+S7HufUwNXFMRZZCEOFWNKuaupVaZIIcS2OfrvDBtfRUQ6ywbThA/Qkt9Fwoto= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.co.jp; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=I4HZbpbx; arc=none smtp.client-ip=99.78.197.218 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.jp Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="I4HZbpbx" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1740598244; x=1772134244; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=10vq9HcFvL/h4/5UOZz3xjreJ3PbRno1kcDII/cclaU=; b=I4HZbpbxGG/L2xk6yyXrnAJ0VrxJDSfTayIYld8e2U04m2xvnOKXjH7z SPCNdThotQmlysdb8TJtl69Gw4Jbo//N22wdpCfQ1KiJaAklgL+hA4lE9 t6GRBJVVKZYCWMV90w4guqrQ9e/MqHlApknJacLu/mJaquEtOLuATXZOU o=; X-IronPort-AV: E=Sophos;i="6.13,318,1732579200"; d="scan'208";a="381129376" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev) ([10.25.36.210]) by smtp-border-fw-80007.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2025 19:29:11 +0000 Received: from EX19MTAUWB002.ant.amazon.com [10.0.38.20:53171] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.55.141:2525] with esmtp (Farcaster) id ed6e0e2c-73c4-437e-8849-113864842a5c; Wed, 26 Feb 2025 19:29:10 +0000 (UTC) X-Farcaster-Flow-ID: ed6e0e2c-73c4-437e-8849-113864842a5c Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX19MTAUWB002.ant.amazon.com (10.250.64.231) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Wed, 26 Feb 2025 19:27:23 +0000 Received: from 6c7e67bfbae3.amazon.com (10.187.170.36) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Wed, 26 Feb 2025 19:27:20 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , David Ahern , Eric Dumazet , Jakub Kicinski , "Paolo Abeni" , Simon Horman CC: Kuniyuki Iwashima , Kuniyuki Iwashima , Subject: [PATCH v2 net-next 03/12] ipv4: fib: Allocate fib_info_hash[] during netns initialisation. Date: Wed, 26 Feb 2025 11:25:47 -0800 Message-ID: <20250226192556.21633-4-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250226192556.21633-1-kuniyu@amazon.com> References: <20250226192556.21633-1-kuniyu@amazon.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D045UWA003.ant.amazon.com (10.13.139.46) To EX19D004ANA001.ant.amazon.com (10.37.240.138) X-Patchwork-Delegate: kuba@kernel.org We will allocate fib_info_hash[] and fib_info_laddrhash[] for each netns. Currently, fib_info_hash[] is allocated when the first route is added. Let's move the first allocation to a new __net_init function. Note that we must call fib4_semantics_exit() in fib_net_exit_batch() because ->exit() is called earlier than ->exit_batch(). Signed-off-by: Kuniyuki Iwashima --- v2: * Fix memleak by calling fib4_semantics_exit() properly * Move fib4_semantics_exit() to fib_net_exit_batch() --- include/net/ip_fib.h | 2 ++ net/ipv4/fib_frontend.c | 11 ++++++++++ net/ipv4/fib_semantics.c | 45 ++++++++++++++++++++++++++++------------ 3 files changed, 45 insertions(+), 13 deletions(-) diff --git a/include/net/ip_fib.h b/include/net/ip_fib.h index a113c11ab56b..e3864b74e92a 100644 --- a/include/net/ip_fib.h +++ b/include/net/ip_fib.h @@ -162,6 +162,8 @@ struct fib_info { struct fib_nh fib_nh[] __counted_by(fib_nhs); }; +int __net_init fib4_semantics_init(struct net *net); +void __net_exit fib4_semantics_exit(struct net *net); #ifdef CONFIG_IP_MULTIPLE_TABLES struct fib_rule; diff --git a/net/ipv4/fib_frontend.c b/net/ipv4/fib_frontend.c index 6730e2034cf8..40c062f820f2 100644 --- a/net/ipv4/fib_frontend.c +++ b/net/ipv4/fib_frontend.c @@ -1615,9 +1615,15 @@ static int __net_init fib_net_init(struct net *net) error = ip_fib_net_init(net); if (error < 0) goto out; + + error = fib4_semantics_init(net); + if (error) + goto out_semantics; + error = nl_fib_lookup_init(net); if (error < 0) goto out_nlfl; + error = fib_proc_init(net); if (error < 0) goto out_proc; @@ -1627,6 +1633,8 @@ static int __net_init fib_net_init(struct net *net) out_proc: nl_fib_lookup_exit(net); out_nlfl: + fib4_semantics_exit(net); +out_semantics: rtnl_lock(); ip_fib_net_exit(net); rtnl_unlock(); @@ -1648,6 +1656,9 @@ static void __net_exit fib_net_exit_batch(struct list_head *net_list) ip_fib_net_exit(net); rtnl_unlock(); + + list_for_each_entry(net, net_list, exit_list) + fib4_semantics_exit(net); } static struct pernet_operations fib_net_ops = { diff --git a/net/ipv4/fib_semantics.c b/net/ipv4/fib_semantics.c index a68a4eb5e0d1..f00ac983861a 100644 --- a/net/ipv4/fib_semantics.c +++ b/net/ipv4/fib_semantics.c @@ -1421,28 +1421,21 @@ struct fib_info *fib_create_info(struct fib_config *cfg, } #endif - err = -ENOBUFS; - if (fib_info_cnt >= fib_info_hash_size) { + unsigned int new_hash_bits = fib_info_hash_bits + 1; struct hlist_head *new_info_hash; - unsigned int new_hash_bits; - - if (!fib_info_hash_bits) - new_hash_bits = 4; - else - new_hash_bits = fib_info_hash_bits + 1; new_info_hash = fib_info_hash_alloc(new_hash_bits); if (new_info_hash) fib_info_hash_move(new_info_hash, 1 << new_hash_bits); - - if (!fib_info_hash_size) - goto failure; } fi = kzalloc(struct_size(fi, fib_nh, nhs), GFP_KERNEL); - if (!fi) + if (!fi) { + err = -ENOBUFS; goto failure; + } + fi->fib_metrics = ip_fib_metrics_init(cfg->fc_mx, cfg->fc_mx_len, extack); if (IS_ERR(fi->fib_metrics)) { err = PTR_ERR(fi->fib_metrics); @@ -1863,7 +1856,7 @@ int fib_sync_down_addr(struct net_device *dev, __be32 local) struct fib_info *fi; int ret = 0; - if (!fib_info_laddrhash || local == 0) + if (!local) return 0; head = fib_info_laddrhash_bucket(net, local); @@ -2265,3 +2258,29 @@ void fib_select_path(struct net *net, struct fib_result *res, fl4->saddr = inet_select_addr(l3mdev, 0, RT_SCOPE_LINK); } } + +int __net_init fib4_semantics_init(struct net *net) +{ + unsigned int hash_bits = 4; + + if (!net_eq(net, &init_net)) + return 0; + + fib_info_hash = fib_info_hash_alloc(hash_bits); + if (!fib_info_hash) + return -ENOMEM; + + fib_info_hash_bits = hash_bits; + fib_info_hash_size = 1 << hash_bits; + fib_info_laddrhash = fib_info_hash + fib_info_hash_size; + + return 0; +} + +void __net_exit fib4_semantics_exit(struct net *net) +{ + if (!net_eq(net, &init_net)) + return; + + fib_info_hash_free(fib_info_hash); +} From patchwork Wed Feb 26 19:25:48 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13993070 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-fw-52004.amazon.com (smtp-fw-52004.amazon.com [52.119.213.154]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C3CA422D4F6 for ; Wed, 26 Feb 2025 19:29:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=52.119.213.154 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740598181; cv=none; b=oBWrKhNpozwcJvVsKNthIE0PiOxMDTQXHGNdyGnF2wtjkQiTOzWGPeaQ83NF3axORbe+++rkHftU/gZUrzlPzEZZm+x7g4Rl4LNCL+hi6ZcL2blve+vqV27kmRhk0OMBtc6quDxH6ONgG1k3FT9b077uZM/vOI5spASrxEQ3DVM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740598181; c=relaxed/simple; bh=QsuaelTi6apo+1WarSwxky4X5mzuqathxRJb+if0W+c=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Q6YWFdbY7BM7OvzsHn2N5YtftrnL7z8OAFpD3d+H0QLBy2yeC1XHl5RX1U7zNBWFievhtCsC1lBSra31ym89TCpt1s0+/OBTwQg/g9vyHqbL7PUAY/v4dDJwTUou2TPF1zLIfjFXeUK/TRtf8YfEboDxnjwfaq5/+SjIedNXyj8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.co.jp; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=MaxKViEo; arc=none smtp.client-ip=52.119.213.154 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.jp Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="MaxKViEo" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1740598181; x=1772134181; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=B4NsKfpJfOYEBEj9uG/UYLm0zCQ2G+UK26RsVYWLcvg=; b=MaxKViEoctrSsm7b50EkE/LlsuQ9U+eAGmsSZ8dUZOjQB7MDaeQw5HM2 Joq/c6VnOtPhYytlWsnUg+8MK5PoPgc8oQHiJ/HzbeGbnin1TvVtZvLRw +LXWVJUdgHfl5L97UyiNCBDxLvxNJppQHoBk+jNSBdJcGQkFUZGNSDgf1 A=; X-IronPort-AV: E=Sophos;i="6.13,318,1732579200"; d="scan'208";a="274609765" Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.43.8.2]) by smtp-border-fw-52004.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2025 19:29:15 +0000 Received: from EX19MTAUWB002.ant.amazon.com [10.0.38.20:10884] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.18.70:2525] with esmtp (Farcaster) id 14ebf105-9450-4112-8d34-bed423e40584; Wed, 26 Feb 2025 19:29:13 +0000 (UTC) X-Farcaster-Flow-ID: 14ebf105-9450-4112-8d34-bed423e40584 Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX19MTAUWB002.ant.amazon.com (10.250.64.231) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Wed, 26 Feb 2025 19:28:06 +0000 Received: from 6c7e67bfbae3.amazon.com (10.187.170.36) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Wed, 26 Feb 2025 19:27:45 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , David Ahern , Eric Dumazet , Jakub Kicinski , "Paolo Abeni" , Simon Horman CC: Kuniyuki Iwashima , Kuniyuki Iwashima , Subject: [PATCH v2 net-next 04/12] ipv4: fib: Make fib_info_hashfn() return struct hlist_head. Date: Wed, 26 Feb 2025 11:25:48 -0800 Message-ID: <20250226192556.21633-5-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250226192556.21633-1-kuniyu@amazon.com> References: <20250226192556.21633-1-kuniyu@amazon.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D041UWB001.ant.amazon.com (10.13.139.132) To EX19D004ANA001.ant.amazon.com (10.37.240.138) X-Patchwork-Delegate: kuba@kernel.org Every time fib_info_hashfn() returns a hash value, we fetch &fib_info_hash[hash]. Let's return the hlist_head pointer from fib_info_hashfn() and rename it to fib_info_hash_bucket() to match a similar function, fib_info_laddrhash_bucket(). Note that we need to move the fib_info_hash assignment earlier in fib_info_hash_move() to use fib_info_hash_bucket() in the for loop. Signed-off-by: Kuniyuki Iwashima Reviewed-by: Eric Dumazet --- net/ipv4/fib_semantics.c | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/net/ipv4/fib_semantics.c b/net/ipv4/fib_semantics.c index f00ac983861a..18bec34645ec 100644 --- a/net/ipv4/fib_semantics.c +++ b/net/ipv4/fib_semantics.c @@ -338,7 +338,7 @@ static unsigned int fib_info_hashfn_result(const struct net *net, return hash_32(val ^ net_hash_mix(net), fib_info_hash_bits); } -static inline unsigned int fib_info_hashfn(struct fib_info *fi) +static struct hlist_head *fib_info_hash_bucket(struct fib_info *fi) { unsigned int val; @@ -354,7 +354,7 @@ static inline unsigned int fib_info_hashfn(struct fib_info *fi) } endfor_nexthops(fi) } - return fib_info_hashfn_result(fi->fib_net, val); + return &fib_info_hash[fib_info_hashfn_result(fi->fib_net, val)]; } static struct hlist_head *fib_info_hash_alloc(unsigned int hash_bits) @@ -405,12 +405,8 @@ static struct fib_info *fib_find_info_nh(struct net *net, static struct fib_info *fib_find_info(struct fib_info *nfi) { - struct hlist_head *head; + struct hlist_head *head = fib_info_hash_bucket(nfi); struct fib_info *fi; - unsigned int hash; - - hash = fib_info_hashfn(nfi); - head = &fib_info_hash[hash]; hlist_for_each_entry(fi, head, fib_hash) { if (!net_eq(fi->fib_net, nfi->fib_net)) @@ -1274,22 +1270,16 @@ static void fib_info_hash_move(struct hlist_head *new_info_hash, old_laddrhash = fib_info_laddrhash; fib_info_hash_size = new_size; fib_info_hash_bits = ilog2(new_size); + fib_info_hash = new_info_hash; for (i = 0; i < old_size; i++) { - struct hlist_head *head = &fib_info_hash[i]; + struct hlist_head *head = &old_info_hash[i]; struct hlist_node *n; struct fib_info *fi; - hlist_for_each_entry_safe(fi, n, head, fib_hash) { - struct hlist_head *dest; - unsigned int new_hash; - - new_hash = fib_info_hashfn(fi); - dest = &new_info_hash[new_hash]; - hlist_add_head(&fi->fib_hash, dest); - } + hlist_for_each_entry_safe(fi, n, head, fib_hash) + hlist_add_head(&fi->fib_hash, fib_info_hash_bucket(fi)); } - fib_info_hash = new_info_hash; fib_info_laddrhash = new_laddrhash; for (i = 0; i < old_size; i++) { @@ -1573,8 +1563,8 @@ struct fib_info *fib_create_info(struct fib_config *cfg, refcount_set(&fi->fib_clntref, 1); fib_info_cnt++; - hlist_add_head(&fi->fib_hash, - &fib_info_hash[fib_info_hashfn(fi)]); + hlist_add_head(&fi->fib_hash, fib_info_hash_bucket(fi)); + if (fi->fib_prefsrc) { struct hlist_head *head; From patchwork Wed Feb 26 19:25:49 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13993072 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-fw-80007.amazon.com (smtp-fw-80007.amazon.com [99.78.197.218]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8CB1124E4B7 for ; Wed, 26 Feb 2025 19:30:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=99.78.197.218 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740598230; cv=none; b=mdRQWIYYNmHqdata8JNdw8q3mpxtnSFDeHLY+U+UddNXKNnJQ62FnABlgqpdOKni9Ywv+RW27PdHjlUMI09ZGldq/Ppf3yKNAc9ROqut2BspK8T98rHpRUZQU7l9iO8BoQi7MoXwsO23O5Td/mC3G+Z0pTlN3wmq4zwI/wc1juk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740598230; c=relaxed/simple; bh=gXIjcEMQjL/7U6C+Ii6bEK5az8lTgYR0uFC6mBYvhxs=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=rESWrlLINp5nLG8tHJmJ8tUqeB56Taz5iGFbJka0dPzGJ5HunccyQQz2wpCDggOWNxceVI4nLquil3FpaIiD6vWLIYLKF8DHc3bfkW+iiKVqCfbJmjUnJKbndlJcP7F0fMXQmdjGlQoCxQLgnPJk25sdv0uDI7EkRlCWzSXrnPg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.co.jp; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=ADGl7vBL; arc=none smtp.client-ip=99.78.197.218 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.jp Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="ADGl7vBL" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1740598229; x=1772134229; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Az94S5piMt41cKJvWs9HRLlFBGQgV+64F7sM2Iuu8nc=; b=ADGl7vBLjWNeZ3TWSAXdIl9FLqsctJgEuDAQ/BaeuJXcnXIj2vhH61Vl 4h0DnKKTc6zbUXveU8sUxG1vlVm83BB6LekDnOajGisowDojwlNdWmlbb yqgVjDJzb0F10nIy9kVAm2gxSH7eammniz5fn/2WSnXu9U3wz7Gyww6Yh E=; X-IronPort-AV: E=Sophos;i="6.13,318,1732579200"; d="scan'208";a="381129103" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.25.36.210]) by smtp-border-fw-80007.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2025 19:28:48 +0000 Received: from EX19MTAUWA002.ant.amazon.com [10.0.38.20:57872] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.55.141:2525] with esmtp (Farcaster) id 1622f49b-3cd2-4d2a-abd9-483365a2f2a3; Wed, 26 Feb 2025 19:28:47 +0000 (UTC) X-Farcaster-Flow-ID: 1622f49b-3cd2-4d2a-abd9-483365a2f2a3 Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX19MTAUWA002.ant.amazon.com (10.250.64.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Wed, 26 Feb 2025 19:28:47 +0000 Received: from 6c7e67bfbae3.amazon.com (10.187.170.36) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Wed, 26 Feb 2025 19:28:09 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , David Ahern , Eric Dumazet , Jakub Kicinski , "Paolo Abeni" , Simon Horman CC: Kuniyuki Iwashima , Kuniyuki Iwashima , Subject: [PATCH v2 net-next 05/12] ipv4: fib: Remove fib_info_laddrhash pointer. Date: Wed, 26 Feb 2025 11:25:49 -0800 Message-ID: <20250226192556.21633-6-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250226192556.21633-1-kuniyu@amazon.com> References: <20250226192556.21633-1-kuniyu@amazon.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D046UWB003.ant.amazon.com (10.13.139.174) To EX19D004ANA001.ant.amazon.com (10.37.240.138) X-Patchwork-Delegate: kuba@kernel.org We will allocate the fib_info hash tables per netns. There are 5 global variables for fib_info hash tables: fib_info_hash, fib_info_laddrhash, fib_info_hash_size, fib_info_hash_bits, fib_info_cnt. However, fib_info_laddrhash and fib_info_hash_size can be easily calculated from fib_info_hash and fib_info_hash_bits. Let's remove the fib_info_laddrhash pointer and instead use fib_info_hash + (1 << fib_info_hash_bits). While at it, fib_info_laddrhash_bucket() is moved near other hash-table-specific functions. Signed-off-by: Kuniyuki Iwashima Reviewed-by: Eric Dumazet --- net/ipv4/fib_semantics.c | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/net/ipv4/fib_semantics.c b/net/ipv4/fib_semantics.c index 18bec34645ec..c57173516de5 100644 --- a/net/ipv4/fib_semantics.c +++ b/net/ipv4/fib_semantics.c @@ -51,7 +51,6 @@ #include "fib_lookup.h" static struct hlist_head *fib_info_hash; -static struct hlist_head *fib_info_laddrhash; static unsigned int fib_info_hash_size; static unsigned int fib_info_hash_bits; static unsigned int fib_info_cnt; @@ -357,6 +356,15 @@ static struct hlist_head *fib_info_hash_bucket(struct fib_info *fi) return &fib_info_hash[fib_info_hashfn_result(fi->fib_net, val)]; } +static struct hlist_head *fib_info_laddrhash_bucket(const struct net *net, + __be32 val) +{ + u32 slot = hash_32(net_hash_mix(net) ^ (__force u32)val, + fib_info_hash_bits); + + return &fib_info_hash[(1 << fib_info_hash_bits) + slot]; +} + static struct hlist_head *fib_info_hash_alloc(unsigned int hash_bits) { /* The second half is used for prefsrc */ @@ -1248,26 +1256,15 @@ int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope, return err; } -static struct hlist_head * -fib_info_laddrhash_bucket(const struct net *net, __be32 val) -{ - u32 slot = hash_32(net_hash_mix(net) ^ (__force u32)val, - fib_info_hash_bits); - - return &fib_info_laddrhash[slot]; -} - static void fib_info_hash_move(struct hlist_head *new_info_hash, unsigned int new_size) { - struct hlist_head *new_laddrhash = new_info_hash + new_size; - struct hlist_head *old_info_hash, *old_laddrhash; unsigned int old_size = fib_info_hash_size; + struct hlist_head *old_info_hash; unsigned int i; ASSERT_RTNL(); old_info_hash = fib_info_hash; - old_laddrhash = fib_info_laddrhash; fib_info_hash_size = new_size; fib_info_hash_bits = ilog2(new_size); fib_info_hash = new_info_hash; @@ -1281,9 +1278,8 @@ static void fib_info_hash_move(struct hlist_head *new_info_hash, hlist_add_head(&fi->fib_hash, fib_info_hash_bucket(fi)); } - fib_info_laddrhash = new_laddrhash; for (i = 0; i < old_size; i++) { - struct hlist_head *lhead = &old_laddrhash[i]; + struct hlist_head *lhead = &old_info_hash[old_size + i]; struct hlist_node *n; struct fib_info *fi; @@ -2262,7 +2258,6 @@ int __net_init fib4_semantics_init(struct net *net) fib_info_hash_bits = hash_bits; fib_info_hash_size = 1 << hash_bits; - fib_info_laddrhash = fib_info_hash + fib_info_hash_size; return 0; } From patchwork Wed Feb 26 19:25:50 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13993071 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-fw-2101.amazon.com (smtp-fw-2101.amazon.com [72.21.196.25]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BC72120D51E for ; Wed, 26 Feb 2025 19:30:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=72.21.196.25 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740598206; cv=none; b=hn0gju5j80B7lcSowGuJhpyY/yKIOVPMRrQfFSIJ0woJHvp2Gd8yBxw/xlHvSNVXGAwF22nryo5IYkIm+xAY58sQnoPY/28xalfrV2NY//fl4HoGSn0dAixuQZHoW2w75xwPTS+ScHRDnfpVK5840PA39ieJIFF0RxKne7FhM60= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740598206; c=relaxed/simple; bh=Vf9ObaJi0bvyMGdyhJN76bpdH4+bjU5Lsqunk9dO3w4=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=g24ZsrpDjoQvDDWYKgnkBLDqTR4zCHmk7ppCAb8jbFehv3I7D2Yk7RSu6+IJGg2e42ro4sbpai30JDJCBAJY8D5+1ih0LJSfMDcWg8bx+As1+sM5MkigR/5SEYR36UiZR99wupqg0W/s6/PAsvvYl3KjwmxkGbOnB2uUeNnBiY8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.co.jp; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=PaB4P/x1; arc=none smtp.client-ip=72.21.196.25 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.jp Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="PaB4P/x1" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1740598205; x=1772134205; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=8uS19WUys1JguXofnckrQy5gu+SDxt+BerKdCWPhzQc=; b=PaB4P/x1FuACiJbfdlQ2l4d8gR3x7izNOAQBdcmx1AvJlPjw6u7tGzt/ XIxsSk/9kDSgzrvNU+50mqgU7vcZhW+ZmsxMhPRMRiwOcHwDS5aFD6GeV gUFQ/HVzSFENlJZDiKXGROUJ6Q5su6OryUiVkUOXpx+Xv+s879k+nVvK1 I=; X-IronPort-AV: E=Sophos;i="6.13,318,1732579200"; d="scan'208";a="470255911" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.43.8.6]) by smtp-border-fw-2101.iad2.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2025 19:29:11 +0000 Received: from EX19MTAUWA002.ant.amazon.com [10.0.38.20:4272] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.55.141:2525] with esmtp (Farcaster) id 15c215e5-c8df-4ea5-8bc1-c9ca6fce7a29; Wed, 26 Feb 2025 19:29:10 +0000 (UTC) X-Farcaster-Flow-ID: 15c215e5-c8df-4ea5-8bc1-c9ca6fce7a29 Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX19MTAUWA002.ant.amazon.com (10.250.64.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Wed, 26 Feb 2025 19:28:47 +0000 Received: from 6c7e67bfbae3.amazon.com (10.187.170.36) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Wed, 26 Feb 2025 19:28:34 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , David Ahern , Eric Dumazet , Jakub Kicinski , "Paolo Abeni" , Simon Horman CC: Kuniyuki Iwashima , Kuniyuki Iwashima , Subject: [PATCH v2 net-next 06/12] ipv4: fib: Remove fib_info_hash_size. Date: Wed, 26 Feb 2025 11:25:50 -0800 Message-ID: <20250226192556.21633-7-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250226192556.21633-1-kuniyu@amazon.com> References: <20250226192556.21633-1-kuniyu@amazon.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D043UWA004.ant.amazon.com (10.13.139.41) To EX19D004ANA001.ant.amazon.com (10.37.240.138) X-Patchwork-Delegate: kuba@kernel.org We will allocate the fib_info hash tables per netns. There are 5 global variables for fib_info hash tables: fib_info_hash, fib_info_laddrhash, fib_info_hash_size, fib_info_hash_bits, fib_info_cnt. However, fib_info_laddrhash and fib_info_hash_size can be easily calculated from fib_info_hash and fib_info_hash_bits. Let's remove fib_info_hash_size and use (1 << fib_info_hash_bits) instead. Now we need not pass the new hash table size to fib_info_hash_move(). Signed-off-by: Kuniyuki Iwashima Reviewed-by: Eric Dumazet --- net/ipv4/fib_semantics.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/net/ipv4/fib_semantics.c b/net/ipv4/fib_semantics.c index c57173516de5..cf45e35a603f 100644 --- a/net/ipv4/fib_semantics.c +++ b/net/ipv4/fib_semantics.c @@ -51,7 +51,6 @@ #include "fib_lookup.h" static struct hlist_head *fib_info_hash; -static unsigned int fib_info_hash_size; static unsigned int fib_info_hash_bits; static unsigned int fib_info_cnt; @@ -1256,17 +1255,15 @@ int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope, return err; } -static void fib_info_hash_move(struct hlist_head *new_info_hash, - unsigned int new_size) +static void fib_info_hash_move(struct hlist_head *new_info_hash) { - unsigned int old_size = fib_info_hash_size; + unsigned int old_size = 1 << fib_info_hash_bits; struct hlist_head *old_info_hash; unsigned int i; ASSERT_RTNL(); old_info_hash = fib_info_hash; - fib_info_hash_size = new_size; - fib_info_hash_bits = ilog2(new_size); + fib_info_hash_bits += 1; fib_info_hash = new_info_hash; for (i = 0; i < old_size; i++) { @@ -1407,13 +1404,12 @@ struct fib_info *fib_create_info(struct fib_config *cfg, } #endif - if (fib_info_cnt >= fib_info_hash_size) { - unsigned int new_hash_bits = fib_info_hash_bits + 1; + if (fib_info_cnt >= (1 << fib_info_hash_bits)) { struct hlist_head *new_info_hash; - new_info_hash = fib_info_hash_alloc(new_hash_bits); + new_info_hash = fib_info_hash_alloc(fib_info_hash_bits + 1); if (new_info_hash) - fib_info_hash_move(new_info_hash, 1 << new_hash_bits); + fib_info_hash_move(new_info_hash); } fi = kzalloc(struct_size(fi, fib_nh, nhs), GFP_KERNEL); @@ -2257,7 +2253,6 @@ int __net_init fib4_semantics_init(struct net *net) return -ENOMEM; fib_info_hash_bits = hash_bits; - fib_info_hash_size = 1 << hash_bits; return 0; } From patchwork Wed Feb 26 19:25:51 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13993081 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-fw-80007.amazon.com (smtp-fw-80007.amazon.com [99.78.197.218]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3C86324394B for ; Wed, 26 Feb 2025 19:30:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=99.78.197.218 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740598251; cv=none; b=gC6zWWrGJZ2z18/6nXlaCGDSP6LFkObQrPXneGNhafGAknHRB515HtiE53LpkyOUlKFT1wL7zaKwd6XfgMbETZ+PR1bwnxbFe5gHXOKzBKd+3IqzG7OkRWYAHyn3w1hcUpAOTbntBgf83E8uqlr8K3rFuAjIzAv2HVThQroRGLA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740598251; c=relaxed/simple; bh=g+QnOLrpWFBl4yoBCC21S+5vcTS+HbdM2Cl2Kbszdf8=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=BJ2rWz8DLesnHIHvqejMOxaf77Ifa62mCx5gfYuOaKiC7vdw6oCWXOR6Rl1iJYCU4g6S/EkjVsluCCJ45yMv/0dGO5xMFRUWchM+DOYK3rKAxSfncoZjC12VncpXbSbGm0ZfJEHWwiZYQ2bJN2PXe9q4zKxapos3XjsC/giec4g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.co.jp; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=osYnQHql; arc=none smtp.client-ip=99.78.197.218 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.jp Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="osYnQHql" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1740598250; x=1772134250; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=HTvaMzZJBvSRbIkQJDi+h987CWMdmBU2WYN5tb03jMY=; b=osYnQHqlXQwLDWzYTrxXWhXtLLwDMBiJwfuxp08Lzj4RjgjxrVEy1FdG MeCGy/CuV3UWNgu0rPRp4KrcmqA8L1o+r0JFCKtMOo4RU7rB7uLjzsmim 7p13nX+AKNJnM3IX+j9ooUTHPRHWn3GdFRCEEz4hVmd7vAJkjMeDkG7bC w=; X-IronPort-AV: E=Sophos;i="6.13,318,1732579200"; d="scan'208";a="381129481" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.25.36.210]) by smtp-border-fw-80007.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2025 19:29:13 +0000 Received: from EX19MTAUWA002.ant.amazon.com [10.0.38.20:29566] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.11.69:2525] with esmtp (Farcaster) id bdeaafb1-41a0-422e-aa74-ed479a9e8199; Wed, 26 Feb 2025 19:29:13 +0000 (UTC) X-Farcaster-Flow-ID: bdeaafb1-41a0-422e-aa74-ed479a9e8199 Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX19MTAUWA002.ant.amazon.com (10.250.64.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Wed, 26 Feb 2025 19:29:01 +0000 Received: from 6c7e67bfbae3.amazon.com (10.187.170.36) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Wed, 26 Feb 2025 19:28:59 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , David Ahern , Eric Dumazet , Jakub Kicinski , "Paolo Abeni" , Simon Horman CC: Kuniyuki Iwashima , Kuniyuki Iwashima , Subject: [PATCH v2 net-next 07/12] ipv4: fib: Add fib_info_hash_grow(). Date: Wed, 26 Feb 2025 11:25:51 -0800 Message-ID: <20250226192556.21633-8-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250226192556.21633-1-kuniyu@amazon.com> References: <20250226192556.21633-1-kuniyu@amazon.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D041UWB001.ant.amazon.com (10.13.139.132) To EX19D004ANA001.ant.amazon.com (10.37.240.138) X-Patchwork-Delegate: kuba@kernel.org When the number of struct fib_info exceeds the hash table size in fib_create_info(), we try to allocate a new hash table with the doubled size. The allocation is done in fib_create_info(), and if successful, each struct fib_info is moved to the new hash table by fib_info_hash_move(). Let's integrate the allocation and fib_info_hash_move() as fib_info_hash_grow() to make the following change cleaner. While at it, fib_info_hash_grow() is placed near other hash-table-specific functions. Signed-off-by: Kuniyuki Iwashima Reviewed-by: Eric Dumazet --- net/ipv4/fib_semantics.c | 85 +++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 44 deletions(-) diff --git a/net/ipv4/fib_semantics.c b/net/ipv4/fib_semantics.c index cf45e35a603f..0eb583a7d772 100644 --- a/net/ipv4/fib_semantics.c +++ b/net/ipv4/fib_semantics.c @@ -377,6 +377,46 @@ static void fib_info_hash_free(struct hlist_head *head) kvfree(head); } +static void fib_info_hash_grow(void) +{ + struct hlist_head *new_info_hash, *old_info_hash; + unsigned int old_size = 1 << fib_info_hash_bits; + unsigned int i; + + if (fib_info_cnt < old_size) + return; + + new_info_hash = fib_info_hash_alloc(fib_info_hash_bits + 1); + if (!new_info_hash) + return; + + old_info_hash = fib_info_hash; + fib_info_hash = new_info_hash; + fib_info_hash_bits += 1; + + for (i = 0; i < old_size; i++) { + struct hlist_head *head = &old_info_hash[i]; + struct hlist_node *n; + struct fib_info *fi; + + hlist_for_each_entry_safe(fi, n, head, fib_hash) + hlist_add_head(&fi->fib_hash, fib_info_hash_bucket(fi)); + } + + for (i = 0; i < old_size; i++) { + struct hlist_head *lhead = &old_info_hash[old_size + i]; + struct hlist_node *n; + struct fib_info *fi; + + hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) + hlist_add_head(&fi->fib_lhash, + fib_info_laddrhash_bucket(fi->fib_net, + fi->fib_prefsrc)); + } + + fib_info_hash_free(old_info_hash); +} + /* no metrics, only nexthop id */ static struct fib_info *fib_find_info_nh(struct net *net, const struct fib_config *cfg) @@ -1255,43 +1295,6 @@ int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope, return err; } -static void fib_info_hash_move(struct hlist_head *new_info_hash) -{ - unsigned int old_size = 1 << fib_info_hash_bits; - struct hlist_head *old_info_hash; - unsigned int i; - - ASSERT_RTNL(); - old_info_hash = fib_info_hash; - fib_info_hash_bits += 1; - fib_info_hash = new_info_hash; - - for (i = 0; i < old_size; i++) { - struct hlist_head *head = &old_info_hash[i]; - struct hlist_node *n; - struct fib_info *fi; - - hlist_for_each_entry_safe(fi, n, head, fib_hash) - hlist_add_head(&fi->fib_hash, fib_info_hash_bucket(fi)); - } - - for (i = 0; i < old_size; i++) { - struct hlist_head *lhead = &old_info_hash[old_size + i]; - struct hlist_node *n; - struct fib_info *fi; - - hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) { - struct hlist_head *ldest; - - ldest = fib_info_laddrhash_bucket(fi->fib_net, - fi->fib_prefsrc); - hlist_add_head(&fi->fib_lhash, ldest); - } - } - - fib_info_hash_free(old_info_hash); -} - __be32 fib_info_update_nhc_saddr(struct net *net, struct fib_nh_common *nhc, unsigned char scope) { @@ -1404,13 +1407,7 @@ struct fib_info *fib_create_info(struct fib_config *cfg, } #endif - if (fib_info_cnt >= (1 << fib_info_hash_bits)) { - struct hlist_head *new_info_hash; - - new_info_hash = fib_info_hash_alloc(fib_info_hash_bits + 1); - if (new_info_hash) - fib_info_hash_move(new_info_hash); - } + fib_info_hash_grow(); fi = kzalloc(struct_size(fi, fib_nh, nhs), GFP_KERNEL); if (!fi) { From patchwork Wed Feb 26 19:25:52 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13993079 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-fw-52004.amazon.com (smtp-fw-52004.amazon.com [52.119.213.154]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9C64F245037 for ; Wed, 26 Feb 2025 19:30:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=52.119.213.154 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740598244; cv=none; b=c5TqtHmJLUYCXWELkgJR1ElbElaB9k5/EhD0D+m4/5lQuoAdmzaXzcNpV2eaXu/O7xrzhewxYHkfLJHqhrdoDtyXnzfVpvAPwf8ZTEOQSLbmp4AXECZROZ2VjpKDmz++HbdYN3tu3V1kySHmJvRYN8qP2gL2oKzDC6Ot4pgozTo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740598244; c=relaxed/simple; bh=3+4tBKQIgDp2CfSijmTYZRBaVPvUrwU6C6SjvDCcAC0=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Cq3uY1MrHOFBntZgAWhRL8L2onxmVrO1CqrtpKvBV4WBudxIoP6A3C7viaHIOx+Rjx77rV3sCDFA+ljjTQXJMyZeHgKb0g49ARg15I+bWwouKvggmRoAmvMaHcGNaawTZMDNJcS0ZzyhGcQmljOFREP1rXGZTlEw9lA+4PI7cnQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.co.jp; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=j1NvwUW2; arc=none smtp.client-ip=52.119.213.154 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.jp Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="j1NvwUW2" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1740598244; x=1772134244; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=leMoVXLRbTSPwZNJqv7YnV/xC5jg77d6zH3rp3Y2GVY=; b=j1NvwUW29UQluL9DYRvYMzNavQ0oGWtIC7kV/elvKSlnGIA8i47wYOYz Kgi2CRfWNdbKGD5ImQymnpvy0l5LHTvXMjPjZGvdpPI7rxmON/xyHv0og kF70uAjetwKbuP0Q5LkgSIBKJ64EsPXPslQyvzOwRgId+dRkmdSnQTXss o=; X-IronPort-AV: E=Sophos;i="6.13,318,1732579200"; d="scan'208";a="274610759" Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.43.8.2]) by smtp-border-fw-52004.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2025 19:29:52 +0000 Received: from EX19MTAUWC002.ant.amazon.com [10.0.21.151:59126] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.48.97:2525] with esmtp (Farcaster) id 411bd7cc-9abf-4385-8882-d35dd0966230; Wed, 26 Feb 2025 19:29:51 +0000 (UTC) X-Farcaster-Flow-ID: 411bd7cc-9abf-4385-8882-d35dd0966230 Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX19MTAUWC002.ant.amazon.com (10.250.64.143) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Wed, 26 Feb 2025 19:29:26 +0000 Received: from 6c7e67bfbae3.amazon.com (10.187.170.36) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Wed, 26 Feb 2025 19:29:23 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , David Ahern , Eric Dumazet , Jakub Kicinski , "Paolo Abeni" , Simon Horman CC: Kuniyuki Iwashima , Kuniyuki Iwashima , Subject: [PATCH v2 net-next 08/12] ipv4: fib: Namespacify fib_info hash tables. Date: Wed, 26 Feb 2025 11:25:52 -0800 Message-ID: <20250226192556.21633-9-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250226192556.21633-1-kuniyu@amazon.com> References: <20250226192556.21633-1-kuniyu@amazon.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D043UWA001.ant.amazon.com (10.13.139.45) To EX19D004ANA001.ant.amazon.com (10.37.240.138) X-Patchwork-Delegate: kuba@kernel.org We will convert RTM_NEWROUTE and RTM_DELROUTE to per-netns RTNL. Then, we need to have per-netns hash tables for struct fib_info. Let's allocate the hash tables per netns. fib_info_hash, fib_info_hash_bits, and fib_info_cnt are now moved to struct netns_ipv4 and accessed with net->ipv4.fib_XXX. Also, the netns checks are removed from fib_find_info_nh() and fib_find_info(). Signed-off-by: Kuniyuki Iwashima Reviewed-by: Eric Dumazet --- include/net/netns/ipv4.h | 3 ++ net/ipv4/fib_semantics.c | 61 +++++++++++++++++----------------------- 2 files changed, 29 insertions(+), 35 deletions(-) diff --git a/include/net/netns/ipv4.h b/include/net/netns/ipv4.h index 45ac125e8aeb..650b2dc9199f 100644 --- a/include/net/netns/ipv4.h +++ b/include/net/netns/ipv4.h @@ -111,6 +111,9 @@ struct netns_ipv4 { #endif struct hlist_head *fib_table_hash; struct sock *fibnl; + struct hlist_head *fib_info_hash; + unsigned int fib_info_hash_bits; + unsigned int fib_info_cnt; struct sock *mc_autojoin_sk; diff --git a/net/ipv4/fib_semantics.c b/net/ipv4/fib_semantics.c index 0eb583a7d772..dd80d2e291e4 100644 --- a/net/ipv4/fib_semantics.c +++ b/net/ipv4/fib_semantics.c @@ -50,10 +50,6 @@ #include "fib_lookup.h" -static struct hlist_head *fib_info_hash; -static unsigned int fib_info_hash_bits; -static unsigned int fib_info_cnt; - /* for_nexthops and change_nexthops only used when nexthop object * is not set in a fib_info. The logic within can reference fib_nh. */ @@ -256,8 +252,7 @@ void fib_release_info(struct fib_info *fi) ASSERT_RTNL(); if (fi && refcount_dec_and_test(&fi->fib_treeref)) { hlist_del(&fi->fib_hash); - - fib_info_cnt--; + fi->fib_net->ipv4.fib_info_cnt--; if (fi->fib_prefsrc) hlist_del(&fi->fib_lhash); @@ -333,11 +328,12 @@ static unsigned int fib_info_hashfn_1(int init_val, u8 protocol, u8 scope, static unsigned int fib_info_hashfn_result(const struct net *net, unsigned int val) { - return hash_32(val ^ net_hash_mix(net), fib_info_hash_bits); + return hash_32(val ^ net_hash_mix(net), net->ipv4.fib_info_hash_bits); } static struct hlist_head *fib_info_hash_bucket(struct fib_info *fi) { + struct net *net = fi->fib_net; unsigned int val; val = fib_info_hashfn_1(fi->fib_nhs, fi->fib_protocol, @@ -352,16 +348,18 @@ static struct hlist_head *fib_info_hash_bucket(struct fib_info *fi) } endfor_nexthops(fi) } - return &fib_info_hash[fib_info_hashfn_result(fi->fib_net, val)]; + return &net->ipv4.fib_info_hash[fib_info_hashfn_result(net, val)]; } static struct hlist_head *fib_info_laddrhash_bucket(const struct net *net, __be32 val) { - u32 slot = hash_32(net_hash_mix(net) ^ (__force u32)val, - fib_info_hash_bits); + unsigned int hash_bits = net->ipv4.fib_info_hash_bits; + u32 slot; - return &fib_info_hash[(1 << fib_info_hash_bits) + slot]; + slot = hash_32(net_hash_mix(net) ^ (__force u32)val, hash_bits); + + return &net->ipv4.fib_info_hash[(1 << hash_bits) + slot]; } static struct hlist_head *fib_info_hash_alloc(unsigned int hash_bits) @@ -377,22 +375,22 @@ static void fib_info_hash_free(struct hlist_head *head) kvfree(head); } -static void fib_info_hash_grow(void) +static void fib_info_hash_grow(struct net *net) { + unsigned int old_size = 1 << net->ipv4.fib_info_hash_bits; struct hlist_head *new_info_hash, *old_info_hash; - unsigned int old_size = 1 << fib_info_hash_bits; unsigned int i; - if (fib_info_cnt < old_size) + if (net->ipv4.fib_info_cnt < old_size) return; - new_info_hash = fib_info_hash_alloc(fib_info_hash_bits + 1); + new_info_hash = fib_info_hash_alloc(net->ipv4.fib_info_hash_bits + 1); if (!new_info_hash) return; - old_info_hash = fib_info_hash; - fib_info_hash = new_info_hash; - fib_info_hash_bits += 1; + old_info_hash = net->ipv4.fib_info_hash; + net->ipv4.fib_info_hash = new_info_hash; + net->ipv4.fib_info_hash_bits += 1; for (i = 0; i < old_size; i++) { struct hlist_head *head = &old_info_hash[i]; @@ -430,13 +428,12 @@ static struct fib_info *fib_find_info_nh(struct net *net, (__force u32)cfg->fc_prefsrc, cfg->fc_priority); hash = fib_info_hashfn_result(net, hash); - head = &fib_info_hash[hash]; + head = &net->ipv4.fib_info_hash[hash]; hlist_for_each_entry(fi, head, fib_hash) { - if (!net_eq(fi->fib_net, net)) - continue; if (!fi->nh || fi->nh->id != cfg->fc_nh_id) continue; + if (cfg->fc_protocol == fi->fib_protocol && cfg->fc_scope == fi->fib_scope && cfg->fc_prefsrc == fi->fib_prefsrc && @@ -456,10 +453,9 @@ static struct fib_info *fib_find_info(struct fib_info *nfi) struct fib_info *fi; hlist_for_each_entry(fi, head, fib_hash) { - if (!net_eq(fi->fib_net, nfi->fib_net)) - continue; if (fi->fib_nhs != nfi->fib_nhs) continue; + if (nfi->fib_protocol == fi->fib_protocol && nfi->fib_scope == fi->fib_scope && nfi->fib_prefsrc == fi->fib_prefsrc && @@ -1407,7 +1403,7 @@ struct fib_info *fib_create_info(struct fib_config *cfg, } #endif - fib_info_hash_grow(); + fib_info_hash_grow(net); fi = kzalloc(struct_size(fi, fib_nh, nhs), GFP_KERNEL); if (!fi) { @@ -1551,7 +1547,7 @@ struct fib_info *fib_create_info(struct fib_config *cfg, refcount_set(&fi->fib_treeref, 1); refcount_set(&fi->fib_clntref, 1); - fib_info_cnt++; + net->ipv4.fib_info_cnt++; hlist_add_head(&fi->fib_hash, fib_info_hash_bucket(fi)); if (fi->fib_prefsrc) { @@ -2242,22 +2238,17 @@ int __net_init fib4_semantics_init(struct net *net) { unsigned int hash_bits = 4; - if (!net_eq(net, &init_net)) - return 0; - - fib_info_hash = fib_info_hash_alloc(hash_bits); - if (!fib_info_hash) + net->ipv4.fib_info_hash = fib_info_hash_alloc(hash_bits); + if (!net->ipv4.fib_info_hash) return -ENOMEM; - fib_info_hash_bits = hash_bits; + net->ipv4.fib_info_hash_bits = hash_bits; + net->ipv4.fib_info_cnt = 0; return 0; } void __net_exit fib4_semantics_exit(struct net *net) { - if (!net_eq(net, &init_net)) - return; - - fib_info_hash_free(fib_info_hash); + fib_info_hash_free(net->ipv4.fib_info_hash); } From patchwork Wed Feb 26 19:25:53 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13993084 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-fw-80009.amazon.com (smtp-fw-80009.amazon.com [99.78.197.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9D1CB24A06C for ; Wed, 26 Feb 2025 19:32:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=99.78.197.220 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740598356; cv=none; b=rGPNBqmo0ofnb+1ZCaGAK7L4AcYT7Mo4Z1Py0krWt7ckYDw5ZI/Rv93YvZiD1KzyB5MwNtuo6jupzxYG6wSKiD3gngo7a0qcKnuLvKpbOkr77KcQ2zeatvIk3O3UT02E8agfKe1KEYQrNEg77MVWguPcmXx215oI6BPoMqjwjEI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740598356; c=relaxed/simple; bh=8DbAhIql40RzAZFOKS9rwyPdrPIXxIDQ0/SYk7/K5TM=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Ymf9I0d2G3PCBFbG6CZU6ZRcdM13q5lwZVx2m2BApVgkRRl65vfemRoyz4MNqe1NJut0QyhAxO6+TClPdswgWp7H0ME9HIw7zEA6FyJK+S4LQNakTvoigEQXtEK5QtR49KwcXhmMfamLLtkd+zKjJgzC6y+KuJc/ZDto0vEgr4Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.co.jp; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=rhwV6afH; arc=none smtp.client-ip=99.78.197.220 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.jp Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="rhwV6afH" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1740598354; x=1772134354; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=K3u6PhPeHRherC5lcTCBSrJ6b+PgVB/Tq7iB2T8rPGk=; b=rhwV6afHj/YPas8CWCXQ/BIZRU7/VNpLlVrVHdbulJn8argVpLEzr46J F8H+7kFjD9q4DjQejzAPOGdDVwC2kcUN6BT+F1UqcCn6/TYru0aFdklpS aI/iULfkHWH9MAbmERoJElk59IL5uvB6IO6jag/MmKMotp0hjkgG6VBYT U=; X-IronPort-AV: E=Sophos;i="6.13,318,1732579200"; d="scan'208";a="176287234" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.25.36.210]) by smtp-border-fw-80009.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2025 19:30:07 +0000 Received: from EX19MTAUWB002.ant.amazon.com [10.0.38.20:33677] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.48.97:2525] with esmtp (Farcaster) id 7087981e-9911-46f5-9697-5288957e397a; Wed, 26 Feb 2025 19:30:07 +0000 (UTC) X-Farcaster-Flow-ID: 7087981e-9911-46f5-9697-5288957e397a Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX19MTAUWB002.ant.amazon.com (10.250.64.231) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Wed, 26 Feb 2025 19:29:50 +0000 Received: from 6c7e67bfbae3.amazon.com (10.187.170.36) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Wed, 26 Feb 2025 19:29:47 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , David Ahern , Eric Dumazet , Jakub Kicinski , "Paolo Abeni" , Simon Horman CC: Kuniyuki Iwashima , Kuniyuki Iwashima , Subject: [PATCH v2 net-next 09/12] ipv4: fib: Hold rtnl_net_lock() for ip_fib_net_exit(). Date: Wed, 26 Feb 2025 11:25:53 -0800 Message-ID: <20250226192556.21633-10-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250226192556.21633-1-kuniyu@amazon.com> References: <20250226192556.21633-1-kuniyu@amazon.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D038UWC004.ant.amazon.com (10.13.139.229) To EX19D004ANA001.ant.amazon.com (10.37.240.138) X-Patchwork-Delegate: kuba@kernel.org ip_fib_net_exit() requires RTNL and is called from fib_net_init() and fib_net_exit_batch(). Let's hold rtnl_net_lock() before ip_fib_net_exit(). Signed-off-by: Kuniyuki Iwashima Reviewed-by: Eric Dumazet --- net/ipv4/fib_frontend.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/net/ipv4/fib_frontend.c b/net/ipv4/fib_frontend.c index 40c062f820f2..c48ed369b179 100644 --- a/net/ipv4/fib_frontend.c +++ b/net/ipv4/fib_frontend.c @@ -1575,7 +1575,7 @@ static void ip_fib_net_exit(struct net *net) { int i; - ASSERT_RTNL(); + ASSERT_RTNL_NET(net); #ifdef CONFIG_IP_MULTIPLE_TABLES RCU_INIT_POINTER(net->ipv4.fib_main, NULL); RCU_INIT_POINTER(net->ipv4.fib_default, NULL); @@ -1635,9 +1635,9 @@ static int __net_init fib_net_init(struct net *net) out_nlfl: fib4_semantics_exit(net); out_semantics: - rtnl_lock(); + rtnl_net_lock(net); ip_fib_net_exit(net); - rtnl_unlock(); + rtnl_net_unlock(net); goto out; } @@ -1652,9 +1652,11 @@ static void __net_exit fib_net_exit_batch(struct list_head *net_list) struct net *net; rtnl_lock(); - list_for_each_entry(net, net_list, exit_list) + list_for_each_entry(net, net_list, exit_list) { + __rtnl_net_lock(net); ip_fib_net_exit(net); - + __rtnl_net_unlock(net); + } rtnl_unlock(); list_for_each_entry(net, net_list, exit_list) From patchwork Wed Feb 26 19:25:54 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13993082 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-fw-6001.amazon.com (smtp-fw-6001.amazon.com [52.95.48.154]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1417621ABA9 for ; Wed, 26 Feb 2025 19:31:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=52.95.48.154 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740598297; cv=none; b=rJM7GgK80OIXPno+icBZx1VWCX/wWb+WYM59SW0ggAG9TQ5TAp6WjKrqsb3Gd+yzIiiJhw6JENPJATZdE1TuKx6Wy2eEcv+4ki2g/uLYBiTyY7z0kgsW1o2IZk8Ij3O9vq2NcQ/e6FqQGWq7IQWpQrMVpvInFuqf/wLSTh+QmYQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740598297; c=relaxed/simple; bh=WqIeWs5TOBaXtLnowolkiUPXc9jeoxpqC7RapXgPTic=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=JBDB97h3cFzITjG8y0i59idv0QYm0CZJPB64/K5IrTi2QjrHU+6XU89CgZnWw89TUuNgRn2TcitY5EHFxeh334GSx+wXFTsGdxiYsn8Mx+IadWyJxPW7GAO8Vv0n1jRPg1bi5W1cl3zYZGEQQgcCX/+W3Jz3o7t6LGVAdM8CHFg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.co.jp; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=T5fKp2ol; arc=none smtp.client-ip=52.95.48.154 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.jp Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="T5fKp2ol" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1740598293; x=1772134293; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=cfK8FEFW5RMVO5+IYO4ybU2+lGS8HhROHjUl+Xt4IZM=; b=T5fKp2olW8tygvMvie6FeumfMN2pGMVgHUzi1E5ILBr2/cGYmNS4kzxK g9FUNNIF5c70QU66FygFyUx7PPVXD0fKC/80dZmrLhp+pf+9F8pMX8Cke 1Mh4Og+gjFQBgsLhoUj7OL8O0bdIUFhb5nRqZk+7AQpstYtLqQj1EIS19 Y=; X-IronPort-AV: E=Sophos;i="6.13,318,1732579200"; d="scan'208";a="466073849" Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.43.8.2]) by smtp-border-fw-6001.iad6.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2025 19:30:16 +0000 Received: from EX19MTAUWA002.ant.amazon.com [10.0.38.20:51063] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.6.186:2525] with esmtp (Farcaster) id 37dd9a89-a21a-4ea0-904c-92f6e6448853; Wed, 26 Feb 2025 19:30:15 +0000 (UTC) X-Farcaster-Flow-ID: 37dd9a89-a21a-4ea0-904c-92f6e6448853 Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX19MTAUWA002.ant.amazon.com (10.250.64.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Wed, 26 Feb 2025 19:30:15 +0000 Received: from 6c7e67bfbae3.amazon.com (10.187.170.36) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Wed, 26 Feb 2025 19:30:12 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , David Ahern , Eric Dumazet , Jakub Kicinski , "Paolo Abeni" , Simon Horman CC: Kuniyuki Iwashima , Kuniyuki Iwashima , Subject: [PATCH v2 net-next 10/12] ipv4: fib: Hold rtnl_net_lock() in ip_rt_ioctl(). Date: Wed, 26 Feb 2025 11:25:54 -0800 Message-ID: <20250226192556.21633-11-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250226192556.21633-1-kuniyu@amazon.com> References: <20250226192556.21633-1-kuniyu@amazon.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D037UWB002.ant.amazon.com (10.13.138.121) To EX19D004ANA001.ant.amazon.com (10.37.240.138) X-Patchwork-Delegate: kuba@kernel.org ioctl(SIOCADDRT/SIOCDELRT) calls ip_rt_ioctl() to add/remove a route in the netns of the specified socket. Let's hold rtnl_net_lock() there. Note that rtentry_to_fib_config() can be called without rtnl_net_lock() if we convert rtentry.dev handling to RCU later. Signed-off-by: Kuniyuki Iwashima Reviewed-by: Eric Dumazet --- net/ipv4/fib_frontend.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/net/ipv4/fib_frontend.c b/net/ipv4/fib_frontend.c index c48ed369b179..a76dacc3e577 100644 --- a/net/ipv4/fib_frontend.c +++ b/net/ipv4/fib_frontend.c @@ -553,18 +553,16 @@ static int rtentry_to_fib_config(struct net *net, int cmd, struct rtentry *rt, const struct in_ifaddr *ifa; struct in_device *in_dev; - in_dev = __in_dev_get_rtnl(dev); + in_dev = __in_dev_get_rtnl_net(dev); if (!in_dev) return -ENODEV; *colon = ':'; - rcu_read_lock(); - in_dev_for_each_ifa_rcu(ifa, in_dev) { + in_dev_for_each_ifa_rtnl_net(net, ifa, in_dev) { if (strcmp(ifa->ifa_label, devname) == 0) break; } - rcu_read_unlock(); if (!ifa) return -ENODEV; @@ -635,7 +633,7 @@ int ip_rt_ioctl(struct net *net, unsigned int cmd, struct rtentry *rt) if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) return -EPERM; - rtnl_lock(); + rtnl_net_lock(net); err = rtentry_to_fib_config(net, cmd, rt, &cfg); if (err == 0) { struct fib_table *tb; @@ -659,7 +657,7 @@ int ip_rt_ioctl(struct net *net, unsigned int cmd, struct rtentry *rt) /* allocated by rtentry_to_fib_config() */ kfree(cfg.fc_mx); } - rtnl_unlock(); + rtnl_net_unlock(net); return err; } return -EINVAL; From patchwork Wed Feb 26 19:25:55 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13993085 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-fw-80007.amazon.com (smtp-fw-80007.amazon.com [99.78.197.218]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8801B24290B for ; Wed, 26 Feb 2025 19:33:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=99.78.197.218 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740598436; cv=none; b=Fac1GVuy5cODG37NOqrenENCg/FfigDk5TBuGiKlA1edggcCLXtgpyyQg2KABLPwqTw5ib2UdJOZuihFUf1O3WpLSFjQA1xnW+WsubRGDONtlCoiqHdsK7joKKVPRs/aXRlfpVrlZ4qTXA5z8Z418cenlBr2Jomef/YieazywL8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740598436; c=relaxed/simple; bh=eXzVXvsZGTjSjD2FcXbb1MdW26hn8UOjMeYiU0kWIeM=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=asjj/rlRfsspxrMP0ncaBDyOYAaiTm3YOzrwbJ90WM0nw7hjtcGMd5iovl15Z/rRAZrUGJz7Q046hBWA+uM6i7E7X/OK3JKI8EcxB6S91dPGbELJwXK4jLJLJvTYQpBJAas3UOdUR3zD1lBefmohWYsKXzY99iI+tTeet0sGAPY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.co.jp; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=vxbN2aiG; arc=none smtp.client-ip=99.78.197.218 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.jp Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="vxbN2aiG" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1740598434; x=1772134434; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=decV62Ut2mz6a8LfXOCRwGRSwph0h6C8pZzRkkfVpqA=; b=vxbN2aiGspd3HvgpIoIW9H0WQ/oUTPUegBuNwHGfdeR3bx5i9NYM2HDo UqG2YihfNrimNhlwK7d9hU1ZuGl3z97lbEF3D/nK7DVZHW+WDWF6UhSnq GvVSqsmOQlypCtfTULMT2muX5IZpBPFhCbQv0zuvEeoEPcKtycl77h939 U=; X-IronPort-AV: E=Sophos;i="6.13,318,1732579200"; d="scan'208";a="381132149" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.25.36.210]) by smtp-border-fw-80007.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2025 19:30:40 +0000 Received: from EX19MTAUWB001.ant.amazon.com [10.0.7.35:24906] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.7.176:2525] with esmtp (Farcaster) id 06b735ad-c2d9-4d62-a1c5-841ffe920779; Wed, 26 Feb 2025 19:30:39 +0000 (UTC) X-Farcaster-Flow-ID: 06b735ad-c2d9-4d62-a1c5-841ffe920779 Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX19MTAUWB001.ant.amazon.com (10.250.64.248) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Wed, 26 Feb 2025 19:30:39 +0000 Received: from 6c7e67bfbae3.amazon.com (10.187.170.36) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Wed, 26 Feb 2025 19:30:36 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , David Ahern , Eric Dumazet , Jakub Kicinski , "Paolo Abeni" , Simon Horman CC: Kuniyuki Iwashima , Kuniyuki Iwashima , Subject: [PATCH v2 net-next 11/12] ipv4: fib: Move fib_valid_key_len() to rtm_to_fib_config(). Date: Wed, 26 Feb 2025 11:25:55 -0800 Message-ID: <20250226192556.21633-12-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250226192556.21633-1-kuniyu@amazon.com> References: <20250226192556.21633-1-kuniyu@amazon.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D046UWB004.ant.amazon.com (10.13.139.164) To EX19D004ANA001.ant.amazon.com (10.37.240.138) X-Patchwork-Delegate: kuba@kernel.org fib_valid_key_len() is called in the beginning of fib_table_insert() or fib_table_delete() to check if the prefix length is valid. fib_table_insert() and fib_table_delete() are called from 3 paths - ip_rt_ioctl() - inet_rtm_newroute() / inet_rtm_delroute() - fib_magic() In the first ioctl() path, rtentry_to_fib_config() checks the prefix length with bad_mask(). Also, fib_magic() always passes the correct prefix: 32 or ifa->ifa_prefixlen, which is already validated. Let's move fib_valid_key_len() to the rtnetlink path, rtm_to_fib_config(). While at it, 2 direct returns in rtm_to_fib_config() are changed to goto to match other places in the same function Signed-off-by: Kuniyuki Iwashima Reviewed-by: Eric Dumazet --- net/ipv4/fib_frontend.c | 18 ++++++++++++++++-- net/ipv4/fib_trie.c | 22 ---------------------- 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/net/ipv4/fib_frontend.c b/net/ipv4/fib_frontend.c index a76dacc3e577..a6372d934e45 100644 --- a/net/ipv4/fib_frontend.c +++ b/net/ipv4/fib_frontend.c @@ -835,19 +835,33 @@ static int rtm_to_fib_config(struct net *net, struct sk_buff *skb, } } + if (cfg->fc_dst_len > 32) { + NL_SET_ERR_MSG(extack, "Invalid prefix length"); + err = -EINVAL; + goto errout; + } + + if (cfg->fc_dst_len < 32 && (ntohl(cfg->fc_dst) << cfg->fc_dst_len)) { + NL_SET_ERR_MSG(extack, "Invalid prefix for given prefix length"); + err = -EINVAL; + goto errout; + } + if (cfg->fc_nh_id) { if (cfg->fc_oif || cfg->fc_gw_family || cfg->fc_encap || cfg->fc_mp) { NL_SET_ERR_MSG(extack, "Nexthop specification and nexthop id are mutually exclusive"); - return -EINVAL; + err = -EINVAL; + goto errout; } } if (has_gw && has_via) { NL_SET_ERR_MSG(extack, "Nexthop configuration can not contain both GATEWAY and VIA"); - return -EINVAL; + err = -EINVAL; + goto errout; } if (!cfg->fc_table) diff --git a/net/ipv4/fib_trie.c b/net/ipv4/fib_trie.c index d6411ac81096..59a6f0a9638f 100644 --- a/net/ipv4/fib_trie.c +++ b/net/ipv4/fib_trie.c @@ -1187,22 +1187,6 @@ static int fib_insert_alias(struct trie *t, struct key_vector *tp, return 0; } -static bool fib_valid_key_len(u32 key, u8 plen, struct netlink_ext_ack *extack) -{ - if (plen > KEYLENGTH) { - NL_SET_ERR_MSG(extack, "Invalid prefix length"); - return false; - } - - if ((plen < KEYLENGTH) && (key << plen)) { - NL_SET_ERR_MSG(extack, - "Invalid prefix for given prefix length"); - return false; - } - - return true; -} - static void fib_remove_alias(struct trie *t, struct key_vector *tp, struct key_vector *l, struct fib_alias *old); @@ -1223,9 +1207,6 @@ int fib_table_insert(struct net *net, struct fib_table *tb, key = ntohl(cfg->fc_dst); - if (!fib_valid_key_len(key, plen, extack)) - return -EINVAL; - pr_debug("Insert table=%u %08x/%d\n", tb->tb_id, key, plen); fi = fib_create_info(cfg, extack); @@ -1717,9 +1698,6 @@ int fib_table_delete(struct net *net, struct fib_table *tb, key = ntohl(cfg->fc_dst); - if (!fib_valid_key_len(key, plen, extack)) - return -EINVAL; - l = fib_find_node(t, &tp, key); if (!l) return -ESRCH; From patchwork Wed Feb 26 19:25:56 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13993083 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-fw-6002.amazon.com (smtp-fw-6002.amazon.com [52.95.49.90]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 311C221ABA9 for ; Wed, 26 Feb 2025 19:32:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=52.95.49.90 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740598353; cv=none; b=Wd0HiTyiwn2Rwz3WmbOd67vViYwd46poSMlNXrs8aDIFqwtdZwOa4Exq4Yfh1EeR+RsWiMJitb1tQ1Kbj2iS4XXG3DmLg5HtY/yKfTboYGTxGnH9zNDe+CLikXQ0SoCJyKLu9K9NFY32euFZkPYa7GZg7D9QU+rPHPsrqFPkknU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740598353; c=relaxed/simple; bh=bMBsBRjw/lRUtGuUMc71/I/S/lp0ZTwauL7I5alfvFI=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=tHDAerf5gAIfIQe2dtRj6DnV099MnqnG0jSiulgd4AQaYkFS4NzA6veE3phBAh8kR3EOH3K6uxCFJl0u8zftGgVBNuXtKX21JSjB5yK4guk8VsFdLgtKzzrnBs85olLRaxwjL+yEvdF+bUUf0DETsDj6lZ5dB/Xg1HncC9UVYxM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.co.jp; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=rsvAAeBj; arc=none smtp.client-ip=52.95.49.90 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.jp Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="rsvAAeBj" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1740598349; x=1772134349; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=RdhpugZ0ovpSSzgmTLWYFM6J4IE8hkBH8r6QF5K7iSw=; b=rsvAAeBjBG31eZOc9zSv/oAMFN/8wIU4MpwaEUPtH/eIGGYdwwE9vKxt Qr3hvDoZnAys66PSCqMd2/QZtdENGWGk7gNGUlNyhcz00F7QDePloDiV4 LvSWU/wGRGMKih9qW1w9mpwBRV6kA2wkyv3wlYl6umy8OGfTQ9wAbwvDc I=; X-IronPort-AV: E=Sophos;i="6.13,318,1732579200"; d="scan'208";a="475576113" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.43.8.6]) by smtp-border-fw-6002.iad6.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2025 19:31:20 +0000 Received: from EX19MTAUWC001.ant.amazon.com [10.0.38.20:18015] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.48.97:2525] with esmtp (Farcaster) id 7f691583-ec7b-4917-9905-2b6c92de0b8e; Wed, 26 Feb 2025 19:31:20 +0000 (UTC) X-Farcaster-Flow-ID: 7f691583-ec7b-4917-9905-2b6c92de0b8e Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX19MTAUWC001.ant.amazon.com (10.250.64.174) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Wed, 26 Feb 2025 19:31:04 +0000 Received: from 6c7e67bfbae3.amazon.com (10.187.170.36) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Wed, 26 Feb 2025 19:31:01 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , David Ahern , Eric Dumazet , Jakub Kicinski , "Paolo Abeni" , Simon Horman CC: Kuniyuki Iwashima , Kuniyuki Iwashima , Subject: [PATCH v2 net-next 12/12] ipv4: fib: Convert RTM_NEWROUTE and RTM_DELROUTE to per-netns RTNL. Date: Wed, 26 Feb 2025 11:25:56 -0800 Message-ID: <20250226192556.21633-13-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250226192556.21633-1-kuniyu@amazon.com> References: <20250226192556.21633-1-kuniyu@amazon.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D041UWA004.ant.amazon.com (10.13.139.9) To EX19D004ANA001.ant.amazon.com (10.37.240.138) X-Patchwork-Delegate: kuba@kernel.org We converted fib_info hash tables to per-netns one and now ready to convert RTM_NEWROUTE and RTM_DELROUTE to per-netns RTNL. Let's hold rtnl_net_lock() in inet_rtm_newroute() and inet_rtm_delroute(). Signed-off-by: Kuniyuki Iwashima Reviewed-by: Eric Dumazet --- net/ipv4/fib_frontend.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/net/ipv4/fib_frontend.c b/net/ipv4/fib_frontend.c index a6372d934e45..6de77415b5b3 100644 --- a/net/ipv4/fib_frontend.c +++ b/net/ipv4/fib_frontend.c @@ -884,20 +884,24 @@ static int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, if (err < 0) goto errout; + rtnl_net_lock(net); + if (cfg.fc_nh_id && !nexthop_find_by_id(net, cfg.fc_nh_id)) { NL_SET_ERR_MSG(extack, "Nexthop id does not exist"); err = -EINVAL; - goto errout; + goto unlock; } tb = fib_get_table(net, cfg.fc_table); if (!tb) { NL_SET_ERR_MSG(extack, "FIB table does not exist"); err = -ESRCH; - goto errout; + goto unlock; } err = fib_table_delete(net, tb, &cfg, extack); +unlock: + rtnl_net_unlock(net); errout: return err; } @@ -914,15 +918,20 @@ static int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, if (err < 0) goto errout; + rtnl_net_lock(net); + tb = fib_new_table(net, cfg.fc_table); if (!tb) { err = -ENOBUFS; - goto errout; + goto unlock; } err = fib_table_insert(net, tb, &cfg, extack); if (!err && cfg.fc_type == RTN_LOCAL) net->ipv4.fib_has_custom_local_routes = true; + +unlock: + rtnl_net_unlock(net); errout: return err; } @@ -1683,9 +1692,9 @@ static struct pernet_operations fib_net_ops = { static const struct rtnl_msg_handler fib_rtnl_msg_handlers[] __initconst = { {.protocol = PF_INET, .msgtype = RTM_NEWROUTE, - .doit = inet_rtm_newroute}, + .doit = inet_rtm_newroute, .flags = RTNL_FLAG_DOIT_PERNET}, {.protocol = PF_INET, .msgtype = RTM_DELROUTE, - .doit = inet_rtm_delroute}, + .doit = inet_rtm_delroute, .flags = RTNL_FLAG_DOIT_PERNET}, {.protocol = PF_INET, .msgtype = RTM_GETROUTE, .dumpit = inet_dump_fib, .flags = RTNL_FLAG_DUMP_UNLOCKED | RTNL_FLAG_DUMP_SPLIT_NLM_DONE}, };