diff mbox series

[v1,net-next,5/5] ipv4: Trigger check_lifetime() only when necessary.

Message ID 20241001024837.96425-6-kuniyu@amazon.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series ipv4: Namespacify IPv4 address hash table. | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 46 this patch: 46
netdev/build_tools success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 1 maintainers not CCed: lixiaoyan@google.com
netdev/build_clang success Errors and warnings before: 95 this patch: 95
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn fail Errors and warnings before: 12 this patch: 12
netdev/checkpatch warning WARNING: line length of 81 exceeds 80 columns WARNING: line length of 93 exceeds 80 columns
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 1 this patch: 1
netdev/source_inline success Was 0 now: 0

Commit Message

Kuniyuki Iwashima Oct. 1, 2024, 2:48 a.m. UTC
DHCP is unlikely to be used in non-root network namespaces, and GC is
unnecessary in such cases.

Let's count the number of non-permanent IPv4 addresses and schedule GC
only when the count is not zero.

Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
 include/net/netns/ipv4.h |  1 +
 net/ipv4/devinet.c       | 45 +++++++++++++++++++++++++++++++++-------
 2 files changed, 39 insertions(+), 7 deletions(-)

Comments

Eric Dumazet Oct. 1, 2024, 9:27 a.m. UTC | #1
On Tue, Oct 1, 2024 at 4:51 AM Kuniyuki Iwashima <kuniyu@amazon.com> wrote:
>
> DHCP is unlikely to be used in non-root network namespaces, and GC is
> unnecessary in such cases.
>
> Let's count the number of non-permanent IPv4 addresses and schedule GC
> only when the count is not zero.
>
> Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>

I find this a bit distracting honestly.

Lets focus on RTNL first, there are already a lot of reviews.
Kuniyuki Iwashima Oct. 1, 2024, 8:55 p.m. UTC | #2
From: Eric Dumazet <edumazet@google.com>
Date: Tue, 1 Oct 2024 11:27:18 +0200
> On Tue, Oct 1, 2024 at 4:51 AM Kuniyuki Iwashima <kuniyu@amazon.com> wrote:
> >
> > DHCP is unlikely to be used in non-root network namespaces, and GC is
> > unnecessary in such cases.
> >
> > Let's count the number of non-permanent IPv4 addresses and schedule GC
> > only when the count is not zero.
> >
> > Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
> 
> I find this a bit distracting honestly.
> 
> Lets focus on RTNL first, there are already a lot of reviews.

Sure, will keep this in a local post-conversion branch.

I'll post v2 with patch 1-4 + tags and Sparse warning (__force u32) fixed.

Thanks!
diff mbox series

Patch

diff --git a/include/net/netns/ipv4.h b/include/net/netns/ipv4.h
index 66a4cffc44ee..e37001572111 100644
--- a/include/net/netns/ipv4.h
+++ b/include/net/netns/ipv4.h
@@ -272,5 +272,6 @@  struct netns_ipv4 {
 	siphash_key_t	ip_id_key;
 	struct hlist_head	*inet_addr_lst;
 	struct delayed_work	addr_chk_work;
+	unsigned int		addr_non_perm;
 };
 #endif
diff --git a/net/ipv4/devinet.c b/net/ipv4/devinet.c
index 96fc4aacc539..2510d3ef3291 100644
--- a/net/ipv4/devinet.c
+++ b/net/ipv4/devinet.c
@@ -124,18 +124,38 @@  static u32 inet_addr_hash(const struct net *net, __be32 addr)
 	return hash_32(addr, IN4_ADDR_HSIZE_SHIFT);
 }
 
+static bool inet_ifa_has_lifetime(struct in_ifaddr *ifa)
+{
+	return !(ifa->ifa_flags & IFA_F_PERMANENT);
+}
+
 static void inet_hash_insert(struct net *net, struct in_ifaddr *ifa)
 {
 	u32 hash = inet_addr_hash(net, ifa->ifa_local);
 
 	ASSERT_RTNL();
 	hlist_add_head_rcu(&ifa->addr_lst, &net->ipv4.inet_addr_lst[hash]);
+
+	if (inet_ifa_has_lifetime(ifa)) {
+		WRITE_ONCE(net->ipv4.addr_non_perm, net->ipv4.addr_non_perm + 1);
+		cancel_delayed_work(&net->ipv4.addr_chk_work);
+		queue_delayed_work(system_power_efficient_wq,
+				   &net->ipv4.addr_chk_work, 0);
+	}
 }
 
 static void inet_hash_remove(struct in_ifaddr *ifa)
 {
 	ASSERT_RTNL();
 	hlist_del_init_rcu(&ifa->addr_lst);
+
+	if (inet_ifa_has_lifetime(ifa)) {
+		struct net *net = dev_net(ifa->ifa_dev->dev);
+
+		WRITE_ONCE(net->ipv4.addr_non_perm, net->ipv4.addr_non_perm - 1);
+		if (!net->ipv4.addr_non_perm)
+			cancel_delayed_work(&net->ipv4.addr_chk_work);
+	}
 }
 
 /**
@@ -484,7 +504,6 @@  static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh,
 {
 	struct in_ifaddr __rcu **last_primary, **ifap;
 	struct in_device *in_dev = ifa->ifa_dev;
-	struct net *net = dev_net(in_dev->dev);
 	struct in_validator_info ivi;
 	struct in_ifaddr *ifa1;
 	int ret;
@@ -553,9 +572,6 @@  static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh,
 
 	inet_hash_insert(dev_net(in_dev->dev), ifa);
 
-	cancel_delayed_work(&net->ipv4.addr_chk_work);
-	queue_delayed_work(system_power_efficient_wq, &net->ipv4.addr_chk_work, 0);
-
 	/* Send message first, then call notifier.
 	   Notifier will trigger FIB update, so that
 	   listeners of netlink will know about new ifaddr */
@@ -787,6 +803,9 @@  static void check_lifetime(struct work_struct *work)
 		rtnl_unlock();
 	}
 
+	if (!READ_ONCE(net->ipv4.addr_non_perm))
+		return;
+
 	next_sec = round_jiffies_up(next);
 	next_sched = next;
 
@@ -979,6 +998,7 @@  static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
 	} else {
 		u32 new_metric = ifa->ifa_rt_priority;
 		u8 new_proto = ifa->ifa_proto;
+		int delta = 0;
 
 		inet_free_ifa(ifa);
 
@@ -996,10 +1016,21 @@  static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
 
 		ifa->ifa_proto = new_proto;
 
+		if (inet_ifa_has_lifetime(ifa))
+			delta -= 1;
+
 		set_ifa_lifetime(ifa, valid_lft, prefered_lft);
-		cancel_delayed_work(&net->ipv4.addr_chk_work);
-		queue_delayed_work(system_power_efficient_wq,
-				   &net->ipv4.addr_chk_work, 0);
+
+		if (inet_ifa_has_lifetime(ifa))
+			delta += 1;
+
+		if (delta) {
+			WRITE_ONCE(net->ipv4.addr_non_perm, net->ipv4.addr_non_perm + delta);
+			cancel_delayed_work(&net->ipv4.addr_chk_work);
+			queue_delayed_work(system_power_efficient_wq,
+					   &net->ipv4.addr_chk_work, 0);
+		}
+
 		rtmsg_ifa(RTM_NEWADDR, ifa, nlh, NETLINK_CB(skb).portid);
 	}
 	return 0;