Skip to contentSkip to conversation

ipv4: Convert RTM_{NEW,DEL}ADDR and more to per-netns RTNL.

PATCH In review Kuniyuki Iwashima 3 participants 27 messages Oct 18, 2024 – Oct 20, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover ipv4: Convert RTM_{NEW,DEL}ADDR and more to per-netns RTNL.

The IPv4 address hash table and GC are already namespacified.

This series converts RTM_NEWADDR/RTM_DELADDR and some more RTNL users to per-netns RTNL.

Kuniyuki Iwashima (11):
rtnetlink: Define RTNL_FLAG_DOIT_PERNET for per-netns RTNL doit().
ipv4: Factorise RTM_NEWADDR validation to inet_validate_rtm().
ipv4: Don't allocate ifa for 0.0.0.0 in inet_rtm_newaddr().
ipv4: Convert RTM_NEWADDR to per-netns RTNL.
ipv4: Use per-netns RTNL helpers in inet_rtm_newaddr().
ipv4: Convert RTM_DELADDR to per-netns RTNL.
ipv4: Convert check_lifetime() to per-netns RTNL.
rtnetlink: Define rtnl_net_lock().
ipv4: Convert devinet_sysctl_forward() to per-netns RTNL.
ipv4: Convert devinet_ioctl() to per-netns RTNL except for
SIOCSIFFLAGS.
ipv4: Convert devinet_ioctl to per-netns RTNL.

 include/linux/inetdevice.h |   9 ++
 include/linux/rtnetlink.h  |   6 ++
 include/net/rtnetlink.h    |   1 +
 net/core/dev_ioctl.c       |   6 +-
 net/core/rtnetlink.c       |  11 +++
 net/ipv4/devinet.c         | 190 +++++++++++++++++++++----------------
 6 files changed, 138 insertions(+), 85 deletions(-)
Signature · 1 line
2.39.5 (Apple Git-154)
#1 lore raw

PATCH 1/11 rtnetlink: Define RTNL_FLAG_DOIT_PERNET for per-netns RTNL doit().

We will push RTNL down to each doit() as rtnl_net_lock().

We can use RTNL_FLAG_DOIT_UNLOCKED to call doit() without RTNL, but doit() will still hold RTNL.

Let's define RTNL_FLAG_DOIT_PERNET as an alias of RTNL_FLAG_DOIT_UNLOCKED.

---
 include/net/rtnetlink.h | 1 +
 1 file changed, 1 insertion(+)
include/net/rtnetlink.h +1 -0
index bb49c5708ce7..3fa9da93364b 100644
@@ -11,6 +11,7 @@ typedef int (*rtnl_dumpit_func)(struct sk_buff *, struct netlink_callback *);
 
 enum rtnl_link_flags {
 	RTNL_FLAG_DOIT_UNLOCKED		= BIT(0),
+#define RTNL_FLAG_DOIT_PERNET		RTNL_FLAG_DOIT_UNLOCKED
 	RTNL_FLAG_BULK_DEL_SUPPORTED	= BIT(1),
 	RTNL_FLAG_DUMP_UNLOCKED		= BIT(2),
 	RTNL_FLAG_DUMP_SPLIT_NLM_DONE	= BIT(3),	/* legacy behavior */
Signature · 1 line
2.39.5 (Apple Git-154)
Reviewed-by on v1 1/11
#2 lore raw

Re: [PATCH v1 net-next 01/11] rtnetlink: Define RTNL_FLAG_DOIT_PERNET for per-netns RTNL doit().

Quoted 9 lines · Kuniyuki Iwashima

We will push RTNL down to each doit() as rtnl_net_lock().

We can use RTNL_FLAG_DOIT_UNLOCKED to call doit() without RTNL, but doit() will still hold RTNL.

Let's define RTNL_FLAG_DOIT_PERNET as an alias of RTNL_FLAG_DOIT_UNLOCKED.

#3 lore raw

PATCH 2/11 ipv4: Factorise RTM_NEWADDR validation to inet_validate_rtm().

In reply to Kuniyuki Iwashima

rtm_to_ifaddr() validates some attributes, looks up a netdev, allocates struct in_ifaddr, and validates IFA_CACHEINFO.

There is no reason to delay IFA_CACHEINFO validation.

We will push RTNL down to inet_rtm_newaddr(), and then we want to complete rtnetlink validation before rtnl_net_lock().

Let's factorise the validation parts.

---
 net/ipv4/devinet.c | 79 ++++++++++++++++++++++++++--------------------
 1 file changed, 44 insertions(+), 35 deletions(-)
net/ipv4/devinet.c +44 -35
index 89b892eaeb95..64994ece27c0 100644
@@ -831,35 +831,54 @@ static void set_ifa_lifetime(struct in_ifaddr *ifa, __u32 valid_lft,
 		WRITE_ONCE(ifa->ifa_cstamp, ifa->ifa_tstamp);
 }
 
-static struct in_ifaddr *rtm_to_ifaddr(struct net *net, struct nlmsghdr *nlh,
-				       __u32 *pvalid_lft, __u32 *pprefered_lft,
-				       struct netlink_ext_ack *extack)
+static int inet_validate_rtm(struct nlmsghdr *nlh, struct nlattr **tb,
+			     struct netlink_ext_ack *extack,
+			     __u32 *valid_lft, __u32 *prefered_lft)
 {
-	struct nlattr *tb[IFA_MAX+1];
-	struct in_ifaddr *ifa;
-	struct ifaddrmsg *ifm;
-	struct net_device *dev;
-	struct in_device *in_dev;
+	struct ifaddrmsg *ifm = nlmsg_data(nlh);
 	int err;
 
 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
 				     ifa_ipv4_policy, extack);
 	if (err < 0)
-		goto errout;
-
-	ifm = nlmsg_data(nlh);
-	err = -EINVAL;
+		return err;
 
 	if (ifm->ifa_prefixlen > 32) {
 		NL_SET_ERR_MSG(extack, "ipv4: Invalid prefix length");
-		goto errout;
+		return -EINVAL;
 	}
 
 	if (!tb[IFA_LOCAL]) {
 		NL_SET_ERR_MSG(extack, "ipv4: Local address is not supplied");
-		goto errout;
+		return -EINVAL;
 	}
 
+	if (tb[IFA_CACHEINFO]) {
+		struct ifa_cacheinfo *ci;
+
+		ci = nla_data(tb[IFA_CACHEINFO]);
+		if (!ci->ifa_valid || ci->ifa_prefered > ci->ifa_valid) {
+			NL_SET_ERR_MSG(extack, "ipv4: address lifetime invalid");
+			return -EINVAL;
+		}
+
+		*valid_lft = ci->ifa_valid;
+		*prefered_lft = ci->ifa_prefered;
+	}
+
+	return 0;
+}
+
+static struct in_ifaddr *inet_rtm_to_ifa(struct net *net, struct nlmsghdr *nlh,
+					 struct nlattr **tb,
+					 struct netlink_ext_ack *extack)
+{
+	struct ifaddrmsg *ifm = nlmsg_data(nlh);
+	struct in_device *in_dev;
+	struct net_device *dev;
+	struct in_ifaddr *ifa;
+	int err;
+
 	dev = __dev_get_by_index(net, ifm->ifa_index);
 	err = -ENODEV;
 	if (!dev) {
@@ -908,23 +927,8 @@ static struct in_ifaddr *rtm_to_ifaddr(struct net *net, struct nlmsghdr *nlh,
 	if (tb[IFA_PROTO])
 		ifa->ifa_proto = nla_get_u8(tb[IFA_PROTO]);
 
-	if (tb[IFA_CACHEINFO]) {
-		struct ifa_cacheinfo *ci;
-
-		ci = nla_data(tb[IFA_CACHEINFO]);
-		if (!ci->ifa_valid || ci->ifa_prefered > ci->ifa_valid) {
-			NL_SET_ERR_MSG(extack, "ipv4: address lifetime invalid");
-			err = -EINVAL;
-			goto errout_free;
-		}
-		*pvalid_lft = ci->ifa_valid;
-		*pprefered_lft = ci->ifa_prefered;
-	}
-
 	return ifa;
 
-errout_free:
-	inet_free_ifa(ifa);
 errout:
 	return ERR_PTR(err);
 }
@@ -949,15 +953,21 @@ static struct in_ifaddr *find_matching_ifa(struct in_ifaddr *ifa)
 static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
 			    struct netlink_ext_ack *extack)
 {
+	__u32 prefered_lft = INFINITY_LIFE_TIME;
+	__u32 valid_lft = INFINITY_LIFE_TIME;
 	struct net *net = sock_net(skb->sk);
-	struct in_ifaddr *ifa;
 	struct in_ifaddr *ifa_existing;
-	__u32 valid_lft = INFINITY_LIFE_TIME;
-	__u32 prefered_lft = INFINITY_LIFE_TIME;
+	struct nlattr *tb[IFA_MAX + 1];
+	struct in_ifaddr *ifa;
+	int ret;
 
 	ASSERT_RTNL();
 
-	ifa = rtm_to_ifaddr(net, nlh, &valid_lft, &prefered_lft, extack);
+	ret = inet_validate_rtm(nlh, tb, extack, &valid_lft, &prefered_lft);
+	if (ret < 0)
+		return ret;
+
+	ifa = inet_rtm_to_ifa(net, nlh, tb, extack);
 	if (IS_ERR(ifa))
 		return PTR_ERR(ifa);
 
@@ -968,8 +978,7 @@ static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
 		 */
 		set_ifa_lifetime(ifa, valid_lft, prefered_lft);
 		if (ifa->ifa_flags & IFA_F_MCAUTOJOIN) {
-			int ret = ip_mc_autojoin_config(net, true, ifa);
-
+			ret = ip_mc_autojoin_config(net, true, ifa);
 			if (ret < 0) {
 				NL_SET_ERR_MSG(extack, "ipv4: Multicast auto join failed");
 				inet_free_ifa(ifa);
Signature · 1 line
2.39.5 (Apple Git-154)
Reviewed-by on v1 2/11
#4 lore raw

Re: [PATCH v1 net-next 02/11] ipv4: Factorise RTM_NEWADDR validation to inet_validate_rtm().

Quoted 4 lines · Kuniyuki Iwashima

rtm_to_ifaddr() validates some attributes, looks up a netdev, allocates struct in_ifaddr, and validates IFA_CACHEINFO.

Quoted 8 lines

There is no reason to delay IFA_CACHEINFO validation.

We will push RTNL down to inet_rtm_newaddr(), and then we want to complete rtnetlink validation before rtnl_net_lock().

Let's factorise the validation parts.

#5 lore raw

PATCH 3/11 ipv4: Don't allocate ifa for 0.0.0.0 in inet_rtm_newaddr().

In reply to Kuniyuki Iwashima

When we pass 0.0.0.0 to __inet_insert_ifa(), it frees ifa and returns 0.

We can do this check much earlier for RTM_NEWADDR even before allocating struct in_ifaddr.

Let's move the validation to

1. inet_insert_ifa() for ioctl()
2. inet_rtm_newaddr() for RTM_NEWADDR

Now, we can remove the same check in find_matching_ifa().

---
 net/ipv4/devinet.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
net/ipv4/devinet.c +9 -8
index 64994ece27c0..636df3661963 100644
@@ -493,11 +493,6 @@ static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh,
 
 	ASSERT_RTNL();
 
-	if (!ifa->ifa_local) {
-		inet_free_ifa(ifa);
-		return 0;
-	}
-
 	ifa->ifa_flags &= ~IFA_F_SECONDARY;
 	last_primary = &in_dev->ifa_list;
 
@@ -569,6 +564,11 @@ static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh,
 
 static int inet_insert_ifa(struct in_ifaddr *ifa)
 {
+	if (!ifa->ifa_local) {
+		inet_free_ifa(ifa);
+		return 0;
+	}
+
 	return __inet_insert_ifa(ifa, NULL, 0, NULL);
 }
 
@@ -938,15 +938,13 @@ static struct in_ifaddr *find_matching_ifa(struct in_ifaddr *ifa)
 	struct in_device *in_dev = ifa->ifa_dev;
 	struct in_ifaddr *ifa1;
 
-	if (!ifa->ifa_local)
-		return NULL;
-
 	in_dev_for_each_ifa_rtnl(ifa1, in_dev) {
 		if (ifa1->ifa_mask == ifa->ifa_mask &&
 		    inet_ifa_match(ifa1->ifa_address, ifa) &&
 		    ifa1->ifa_local == ifa->ifa_local)
 			return ifa1;
 	}
+
 	return NULL;
 }
 
@@ -967,6 +965,9 @@ static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
 	if (ret < 0)
 		return ret;
 
+	if (!nla_get_in_addr(tb[IFA_LOCAL]))
+		return 0;
+
 	ifa = inet_rtm_to_ifa(net, nlh, tb, extack);
 	if (IS_ERR(ifa))
 		return PTR_ERR(ifa);
Signature · 1 line
2.39.5 (Apple Git-154)
Reviewed-by on v1 3/11
#6 lore raw

Re: [PATCH v1 net-next 03/11] ipv4: Don't allocate ifa for 0.0.0.0 in inet_rtm_newaddr().

Quoted 6 lines · Kuniyuki Iwashima

When we pass 0.0.0.0 to __inet_insert_ifa(), it frees ifa and returns 0.

We can do this check much earlier for RTM_NEWADDR even before allocating struct in_ifaddr.

Quoted 8 lines

Let's move the validation to

1. inet_insert_ifa() for ioctl()
2. inet_rtm_newaddr() for RTM_NEWADDR

Now, we can remove the same check in find_matching_ifa().

#7 lore raw

PATCH 4/11 ipv4: Convert RTM_NEWADDR to per-netns RTNL.

In reply to Kuniyuki Iwashima

The address hash table and GC are already namespacified.

Let's push down RTNL into inet_rtm_newaddr() as rtnl_net_lock().

---
 net/ipv4/devinet.c | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)
net/ipv4/devinet.c +18 -10
index 636df3661963..6abafdd20b3c 100644
@@ -959,8 +959,6 @@ static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
 	struct in_ifaddr *ifa;
 	int ret;
 
-	ASSERT_RTNL();
-
 	ret = inet_validate_rtm(nlh, tb, extack, &valid_lft, &prefered_lft);
 	if (ret < 0)
 		return ret;
@@ -968,9 +966,13 @@ static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
 	if (!nla_get_in_addr(tb[IFA_LOCAL]))
 		return 0;
 
+	rtnl_net_lock(net);
+
 	ifa = inet_rtm_to_ifa(net, nlh, tb, extack);
-	if (IS_ERR(ifa))
-		return PTR_ERR(ifa);
+	if (IS_ERR(ifa)) {
+		ret = PTR_ERR(ifa);
+		goto unlock;
+	}
 
 	ifa_existing = find_matching_ifa(ifa);
 	if (!ifa_existing) {
@@ -983,11 +985,11 @@ static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
 			if (ret < 0) {
 				NL_SET_ERR_MSG(extack, "ipv4: Multicast auto join failed");
 				inet_free_ifa(ifa);
-				return ret;
+				goto unlock;
 			}
 		}
-		return __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).portid,
-					 extack);
+
+		ret = __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).portid, extack);
 	} else {
 		u32 new_metric = ifa->ifa_rt_priority;
 		u8 new_proto = ifa->ifa_proto;
@@ -997,7 +999,8 @@ static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
 		if (nlh->nlmsg_flags & NLM_F_EXCL ||
 		    !(nlh->nlmsg_flags & NLM_F_REPLACE)) {
 			NL_SET_ERR_MSG(extack, "ipv4: Address already assigned");
-			return -EEXIST;
+			ret = -EEXIST;
+			goto unlock;
 		}
 		ifa = ifa_existing;
 
@@ -1014,7 +1017,11 @@ static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
 				   &net->ipv4.addr_chk_work, 0);
 		rtmsg_ifa(RTM_NEWADDR, ifa, nlh, NETLINK_CB(skb).portid);
 	}
-	return 0;
+
+unlock:
+	rtnl_net_unlock(net);
+
+	return ret;
 }
 
 /*
@@ -2808,7 +2815,8 @@ static struct rtnl_af_ops inet_af_ops __read_mostly = {
 };
 
 static const struct rtnl_msg_handler devinet_rtnl_msg_handlers[] __initconst = {
-	{.protocol = PF_INET, .msgtype = RTM_NEWADDR, .doit = inet_rtm_newaddr},
+	{.protocol = PF_INET, .msgtype = RTM_NEWADDR, .doit = inet_rtm_newaddr,
+	 .flags = RTNL_FLAG_DOIT_PERNET},
 	{.protocol = PF_INET, .msgtype = RTM_DELADDR, .doit = inet_rtm_deladdr},
 	{.protocol = PF_INET, .msgtype = RTM_GETADDR, .dumpit = inet_dump_ifaddr,
 	 .flags = RTNL_FLAG_DUMP_UNLOCKED | RTNL_FLAG_DUMP_SPLIT_NLM_DONE},
Signature · 1 line
2.39.5 (Apple Git-154)
#9 lore raw

PATCH 5/11 ipv4: Use per-netns RTNL helpers in inet_rtm_newaddr().

In reply to Kuniyuki Iwashima

inet_rtm_to_ifa() and find_matching_ifa() are called under rtnl_net_lock().

__in_dev_get_rtnl() and in_dev_for_each_ifa_rtnl() there can use per-netns RTNL helpers.

Let's define and use __in_dev_get_rtnl_net() and
in_dev_for_each_ifa_rtnl_net().

---
 include/linux/inetdevice.h | 9 +++++++++
 net/ipv4/devinet.c         | 8 ++++----
 2 files changed, 13 insertions(+), 4 deletions(-)
include/linux/inetdevice.h +9 -0
index d9c690c8c80b..5730ba6b1cfa 100644
@@ -226,6 +226,10 @@ static __inline__ bool bad_mask(__be32 mask, __be32 addr)
 	for (ifa = rtnl_dereference((in_dev)->ifa_list); ifa;	\
 	     ifa = rtnl_dereference(ifa->ifa_next))
 
+#define in_dev_for_each_ifa_rtnl_net(net, ifa, in_dev)			\
+	for (ifa = rtnl_net_dereference(net, (in_dev)->ifa_list); ifa;	\
+	     ifa = rtnl_net_dereference(net, ifa->ifa_next))
+
 #define in_dev_for_each_ifa_rcu(ifa, in_dev)			\
 	for (ifa = rcu_dereference((in_dev)->ifa_list); ifa;	\
 	     ifa = rcu_dereference(ifa->ifa_next))
@@ -252,6 +256,11 @@ static inline struct in_device *__in_dev_get_rtnl(const struct net_device *dev)
 	return rtnl_dereference(dev->ip_ptr);
 }
 
+static inline struct in_device *__in_dev_get_rtnl_net(const struct net_device *dev)
+{
+	return rtnl_net_dereference(dev_net(dev), dev->ip_ptr);
+}
+
 /* called with rcu_read_lock or rtnl held */
 static inline bool ip_ignore_linkdown(const struct net_device *dev)
 {
net/ipv4/devinet.c +4 -4
index 6abafdd20b3c..4f94fc8f552d 100644
@@ -886,7 +886,7 @@ static struct in_ifaddr *inet_rtm_to_ifa(struct net *net, struct nlmsghdr *nlh,
 		goto errout;
 	}
 
-	in_dev = __in_dev_get_rtnl(dev);
+	in_dev = __in_dev_get_rtnl_net(dev);
 	err = -ENOBUFS;
 	if (!in_dev)
 		goto errout;
@@ -933,12 +933,12 @@ static struct in_ifaddr *inet_rtm_to_ifa(struct net *net, struct nlmsghdr *nlh,
 	return ERR_PTR(err);
 }
 
-static struct in_ifaddr *find_matching_ifa(struct in_ifaddr *ifa)
+static struct in_ifaddr *find_matching_ifa(struct net *net, struct in_ifaddr *ifa)
 {
 	struct in_device *in_dev = ifa->ifa_dev;
 	struct in_ifaddr *ifa1;
 
-	in_dev_for_each_ifa_rtnl(ifa1, in_dev) {
+	in_dev_for_each_ifa_rtnl_net(net, ifa1, in_dev) {
 		if (ifa1->ifa_mask == ifa->ifa_mask &&
 		    inet_ifa_match(ifa1->ifa_address, ifa) &&
 		    ifa1->ifa_local == ifa->ifa_local)
@@ -974,7 +974,7 @@ static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
 		goto unlock;
 	}
 
-	ifa_existing = find_matching_ifa(ifa);
+	ifa_existing = find_matching_ifa(net, ifa);
 	if (!ifa_existing) {
 		/* It would be best to check for !NLM_F_CREATE here but
 		 * userspace already relies on not having to provide this.
Signature · 1 line
2.39.5 (Apple Git-154)
Reviewed-by on v1 5/11
#10 lore raw

Re: [PATCH v1 net-next 05/11] ipv4: Use per-netns RTNL helpers in inet_rtm_newaddr().

Quoted 11 lines · Kuniyuki Iwashima

inet_rtm_to_ifa() and find_matching_ifa() are called under rtnl_net_lock().

__in_dev_get_rtnl() and in_dev_for_each_ifa_rtnl() there can use per-netns RTNL helpers.

Let's define and use __in_dev_get_rtnl_net() and
in_dev_for_each_ifa_rtnl_net().

#11 lore raw

Re: [PATCH v1 net-next 05/11] ipv4: Use per-netns RTNL helpers in inet_rtm_newaddr().

In reply to Kuniyuki Iwashima
kernel test robot noticed the following build warnings:

Hi Kuniyuki,

kernel test robot noticed the following build warnings:

[auto build test WARNING on net-next/main]

url:    https://github.com/intel-lab-lkp/linux/commits/Kuniyuki-Iwashima/rtnetlink-Define-RTNL_FLAG_DOIT_PERNET-for-per-netns-RTNL-doit/20241018-092802
base:   net-next/main
patch link:    https://lore.kernel.org/r/20241018012225.90409-6-kuniyu%40amazon.com
patch subject: [PATCH v1 net-next 05/11] ipv4: Use per-netns RTNL helpers in inet_rtm_newaddr().
config: x86_64-randconfig-122-20241019 (https://download.01.org/0day-ci/archive/20241020/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241020/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

sparse warnings: (new ones prefixed by >>)

>> net/ipv4/devinet.c:941:9: sparse: sparse: incorrect type in argument 2 (different address spaces) @@     expected void *p @@     got struct in_ifaddr [noderef] __rcu *ifa_list @@
   net/ipv4/devinet.c:941:9: sparse:     expected void *p
   net/ipv4/devinet.c:941:9: sparse:     got struct in_ifaddr [noderef] __rcu *ifa_list
>> net/ipv4/devinet.c:941:9: sparse: sparse: incorrect type in argument 2 (different address spaces) @@     expected void *p @@     got struct in_ifaddr [noderef] __rcu *ifa_next @@
   net/ipv4/devinet.c:941:9: sparse:     expected void *p
   net/ipv4/devinet.c:941:9: sparse:     got struct in_ifaddr [noderef] __rcu *ifa_next
   net/ipv4/devinet.c: note: in included file:
>> include/linux/inetdevice.h:261:54: sparse: sparse: incorrect type in argument 2 (different address spaces) @@     expected void *p @@     got struct in_device [noderef] __rcu *const ip_ptr @@
   include/linux/inetdevice.h:261:54: sparse:     expected void *p
   include/linux/inetdevice.h:261:54: sparse:     got struct in_device [noderef] __rcu *const ip_ptr
   net/ipv4/devinet.c: note: in included file (through include/linux/inetdevice.h):
>> include/linux/rtnetlink.h:147:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
   include/linux/rtnetlink.h:147:16: sparse:    void [noderef] __rcu *
   include/linux/rtnetlink.h:147:16: sparse:    void *
>> include/linux/rtnetlink.h:147:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
   include/linux/rtnetlink.h:147:16: sparse:    void [noderef] __rcu *
   include/linux/rtnetlink.h:147:16: sparse:    void *
>> include/linux/rtnetlink.h:147:16: sparse: sparse: incompatible types in comparison expression (different address spaces):

include/linux/rtnetlink.h:147:16: sparse: void [noderef] __rcu *
include/linux/rtnetlink.h:147:16: sparse: void *

vim +941 net/ipv4/devinet.c

935
936 static struct in_ifaddr *find_matching_ifa(struct net *net, struct in_ifaddr *ifa)
937 {
938 struct in_device *in_dev = ifa->ifa_dev;
939 struct in_ifaddr *ifa1;
940

 > 941		in_dev_for_each_ifa_rtnl_net(net, ifa1, in_dev) {

942 if (ifa1->ifa_mask == ifa->ifa_mask &&
943 inet_ifa_match(ifa1->ifa_address, ifa) &&
944 ifa1->ifa_local == ifa->ifa_local)
945 return ifa1;
946 }
947
948 return NULL;
949 }
950

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#12 lore raw

Re: [PATCH v1 net-next 05/11] ipv4: Use per-netns RTNL helpers in inet_rtm_newaddr().

From: kernel test robot <[email protected]>
Date: Sun, 20 Oct 2024 04:03:55 +0800

Quoted 19 lines

Hi Kuniyuki,

kernel test robot noticed the following build warnings:

[auto build test WARNING on net-next/main]

url:    https://github.com/intel-lab-lkp/linux/commits/Kuniyuki-Iwashima/rtnetlink-Define-RTNL_FLAG_DOIT_PERNET-for-per-netns-RTNL-doit/20241018-092802
base:   net-next/main
patch link:    https://lore.kernel.org/r/20241018012225.90409-6-kuniyu%40amazon.com
patch subject: [PATCH v1 net-next 05/11] ipv4: Use per-netns RTNL helpers in inet_rtm_newaddr().
config: x86_64-randconfig-122-20241019 (https://download.01.org/0day-ci/archive/20241020/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241020/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

Quoted 4 lines

sparse warnings: (new ones prefixed by >>)

Quoted 1 line
Quoted 1 line

net/ipv4/devinet.c:941:9: sparse: sparse: incorrect type in argument 2 (different address spaces) @@ expected void *p @@ got struct in_ifaddr [noderef] __rcu *ifa_list @@

net/ipv4/devinet.c:941:9: sparse: expected void *p
net/ipv4/devinet.c:941:9: sparse: got struct in_ifaddr [noderef] __rcu *ifa_list

Hmm.. when DEBUG_NET_SMALL_RTNL is off, we use static inline helper returning (void *) to make sure net is always evaluated as requested in this thread.

https://lore.kernel.org/netdev/[email protected]/

But it seems we can't do that when the pointer has __rcu annotation.

Also, this (void *)net evaluation in turn makes build fail due to -Werror=unused-value.

  #define rtnl_net_dereference(net, p)			\
  	({						\
  		(void *)net;				\
  		rtnl_dereference(p);			\
  	})

net/ipv4/devinet.c: In function ‘inet_rtm_deladdr’:
./include/linux/rtnetlink.h:154:17: error: statement with no effect [-Werror=unused-value]

    154 |                 (void *)net;                            \
  net/ipv4/devinet.c:674:21: note: in expansion of macro ‘rtnl_net_dereference’
    674 |              (ifa = rtnl_net_dereference(net, *ifap)) != NULL;
        |                     ^~~~~~~~~~~~~~~~~~~~

So, we need to go back to the simplest macro.

  #define rtnl_net_dereference(net, p)			\
  	rtnl_dereference(p);

I'll include the fix in v2.

Quoted 36 lines
Quoted 1 line
Quoted 1 line

net/ipv4/devinet.c:941:9: sparse: sparse: incorrect type in argument 2 (different address spaces) @@ expected void *p @@ got struct in_ifaddr [noderef] __rcu *ifa_next @@

net/ipv4/devinet.c:941:9: sparse: expected void *p
net/ipv4/devinet.c:941:9: sparse: got struct in_ifaddr [noderef] __rcu *ifa_next
net/ipv4/devinet.c: note: in included file:

Quoted 1 line
Quoted 1 line

include/linux/inetdevice.h:261:54: sparse: sparse: incorrect type in argument 2 (different address spaces) @@ expected void *p @@ got struct in_device [noderef] __rcu *const ip_ptr @@

include/linux/inetdevice.h:261:54: sparse: expected void *p
include/linux/inetdevice.h:261:54: sparse: got struct in_device [noderef] __rcu *const ip_ptr
net/ipv4/devinet.c: note: in included file (through include/linux/inetdevice.h):

Quoted 1 line
Quoted 1 line

include/linux/rtnetlink.h:147:16: sparse: sparse: incompatible types in comparison expression (different address spaces):

   include/linux/rtnetlink.h:147:16: sparse:    void [noderef] __rcu *
   include/linux/rtnetlink.h:147:16: sparse:    void *
Quoted 1 line
Quoted 1 line

include/linux/rtnetlink.h:147:16: sparse: sparse: incompatible types in comparison expression (different address spaces):

   include/linux/rtnetlink.h:147:16: sparse:    void [noderef] __rcu *
   include/linux/rtnetlink.h:147:16: sparse:    void *
Quoted 1 line
Quoted 1 line

include/linux/rtnetlink.h:147:16: sparse: sparse: incompatible types in comparison expression (different address spaces):

   include/linux/rtnetlink.h:147:16: sparse:    void [noderef] __rcu *
   include/linux/rtnetlink.h:147:16: sparse:    void *

vim +941 net/ipv4/devinet.c

   935	
   936	static struct in_ifaddr *find_matching_ifa(struct net *net, struct in_ifaddr *ifa)
   937	{
   938		struct in_device *in_dev = ifa->ifa_dev;
   939		struct in_ifaddr *ifa1;
   940	
Quoted 1 line

941 in_dev_for_each_ifa_rtnl_net(net, ifa1, in_dev) {

942 if (ifa1->ifa_mask == ifa->ifa_mask &&
943 inet_ifa_match(ifa1->ifa_address, ifa) &&
944 ifa1->ifa_local == ifa->ifa_local)

   945				return ifa1;
   946		}
   947	
   948		return NULL;
   949	}
   950	
#13 lore raw

PATCH 6/11 ipv4: Convert RTM_DELADDR to per-netns RTNL.

In reply to Kuniyuki Iwashima

Let's push down RTNL into inet_rtm_deladdr() as rtnl_net_lock().

Now, ip_mc_autojoin_config() is always called under per-netns RTNL, so ASSERT_RTNL() can be replaced with ASSERT_RTNL_NET().

---
 net/ipv4/devinet.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)
net/ipv4/devinet.c +15 -9
index 4f94fc8f552d..cbda22eb8d06 100644
@@ -630,7 +630,7 @@ static int ip_mc_autojoin_config(struct net *net, bool join,
 	struct sock *sk = net->ipv4.mc_autojoin_sk;
 	int ret;
 
-	ASSERT_RTNL();
+	ASSERT_RTNL_NET(net);
 
 	lock_sock(sk);
 	if (join)
@@ -656,22 +656,24 @@ static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
 	struct in_ifaddr *ifa;
 	int err;
 
-	ASSERT_RTNL();
-
 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
 				     ifa_ipv4_policy, extack);
 	if (err < 0)
-		goto errout;
+		goto out;
 
 	ifm = nlmsg_data(nlh);
+
+	rtnl_net_lock(net);
+
 	in_dev = inetdev_by_index(net, ifm->ifa_index);
 	if (!in_dev) {
 		NL_SET_ERR_MSG(extack, "ipv4: Device not found");
 		err = -ENODEV;
-		goto errout;
+		goto unlock;
 	}
 
-	for (ifap = &in_dev->ifa_list; (ifa = rtnl_dereference(*ifap)) != NULL;
+	for (ifap = &in_dev->ifa_list;
+	     (ifa = rtnl_net_dereference(net, *ifap)) != NULL;
 	     ifap = &ifa->ifa_next) {
 		if (tb[IFA_LOCAL] &&
 		    ifa->ifa_local != nla_get_in_addr(tb[IFA_LOCAL]))
@@ -687,13 +689,16 @@ static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
 
 		if (ipv4_is_multicast(ifa->ifa_address))
 			ip_mc_autojoin_config(net, false, ifa);
+
 		__inet_del_ifa(in_dev, ifap, 1, nlh, NETLINK_CB(skb).portid);
-		return 0;
+		goto unlock;
 	}
 
 	NL_SET_ERR_MSG(extack, "ipv4: Address not found");
 	err = -EADDRNOTAVAIL;
-errout:
+unlock:
+	rtnl_net_unlock(net);
+out:
 	return err;
 }
 
@@ -2817,7 +2822,8 @@ static struct rtnl_af_ops inet_af_ops __read_mostly = {
 static const struct rtnl_msg_handler devinet_rtnl_msg_handlers[] __initconst = {
 	{.protocol = PF_INET, .msgtype = RTM_NEWADDR, .doit = inet_rtm_newaddr,
 	 .flags = RTNL_FLAG_DOIT_PERNET},
-	{.protocol = PF_INET, .msgtype = RTM_DELADDR, .doit = inet_rtm_deladdr},
+	{.protocol = PF_INET, .msgtype = RTM_DELADDR, .doit = inet_rtm_deladdr,
+	 .flags = RTNL_FLAG_DOIT_PERNET},
 	{.protocol = PF_INET, .msgtype = RTM_GETADDR, .dumpit = inet_dump_ifaddr,
 	 .flags = RTNL_FLAG_DUMP_UNLOCKED | RTNL_FLAG_DUMP_SPLIT_NLM_DONE},
 	{.protocol = PF_INET, .msgtype = RTM_GETNETCONF,
Signature · 1 line
2.39.5 (Apple Git-154)
Reviewed-by on v1 6/11
#14 lore raw

Re: [PATCH v1 net-next 06/11] ipv4: Convert RTM_DELADDR to per-netns RTNL.

Quoted 8 lines · Kuniyuki Iwashima

Let's push down RTNL into inet_rtm_deladdr() as rtnl_net_lock().

Now, ip_mc_autojoin_config() is always called under per-netns RTNL, so ASSERT_RTNL() can be replaced with ASSERT_RTNL_NET().

---

#15 lore raw

Re: [PATCH v1 net-next 06/11] ipv4: Convert RTM_DELADDR to per-netns RTNL.

In reply to Kuniyuki Iwashima
kernel test robot noticed the following build warnings:

Hi Kuniyuki,

kernel test robot noticed the following build warnings:

[auto build test WARNING on net-next/main]

url:    https://github.com/intel-lab-lkp/linux/commits/Kuniyuki-Iwashima/rtnetlink-Define-RTNL_FLAG_DOIT_PERNET-for-per-netns-RTNL-doit/20241018-092802
base:   net-next/main
patch link:    https://lore.kernel.org/r/20241018012225.90409-7-kuniyu%40amazon.com
patch subject: [PATCH v1 net-next 06/11] ipv4: Convert RTM_DELADDR to per-netns RTNL.
config: x86_64-randconfig-122-20241019 (https://download.01.org/0day-ci/archive/20241020/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241020/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

sparse warnings: (new ones prefixed by >>)

>> net/ipv4/devinet.c:676:47: sparse: sparse: incorrect type in argument 2 (different address spaces) @@     expected void *p @@     got struct in_ifaddr [noderef] __rcu * @@

net/ipv4/devinet.c:676:47: sparse: expected void *p
net/ipv4/devinet.c:676:47: sparse: got struct in_ifaddr [noderef] __rcu *
net/ipv4/devinet.c:946:9: sparse: sparse: incorrect type in argument 2 (different address spaces) @@ expected void *p @@ got struct in_ifaddr [noderef] __rcu *ifa_list @@
net/ipv4/devinet.c:946:9: sparse: expected void *p
net/ipv4/devinet.c:946:9: sparse: got struct in_ifaddr [noderef] __rcu *ifa_list
net/ipv4/devinet.c:946:9: sparse: sparse: incorrect type in argument 2 (different address spaces) @@ expected void *p @@ got struct in_ifaddr [noderef] __rcu *ifa_next @@
net/ipv4/devinet.c:946:9: sparse: expected void *p
net/ipv4/devinet.c:946:9: sparse: got struct in_ifaddr [noderef] __rcu *ifa_next
net/ipv4/devinet.c: note: in included file (through include/linux/inetdevice.h):
include/linux/rtnetlink.h:147:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
include/linux/rtnetlink.h:147:16: sparse: void [noderef] __rcu *
include/linux/rtnetlink.h:147:16: sparse: void *
include/linux/rtnetlink.h:147:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
include/linux/rtnetlink.h:147:16: sparse: void [noderef] __rcu *
include/linux/rtnetlink.h:147:16: sparse: void *
include/linux/rtnetlink.h:147:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
include/linux/rtnetlink.h:147:16: sparse: void [noderef] __rcu *
include/linux/rtnetlink.h:147:16: sparse: void *
include/linux/rtnetlink.h:147:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
include/linux/rtnetlink.h:147:16: sparse: void [noderef] __rcu *
include/linux/rtnetlink.h:147:16: sparse: void *

vim +676 net/ipv4/devinet.c

647
648 static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
649 struct netlink_ext_ack *extack)
650 {
651 struct net *net = sock_net(skb->sk);
652 struct in_ifaddr __rcu **ifap;
653 struct nlattr *tb[IFA_MAX+1];
654 struct in_device *in_dev;
655 struct ifaddrmsg *ifm;
656 struct in_ifaddr *ifa;
657 int err;
658
659 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
660 ifa_ipv4_policy, extack);
661 if (err < 0)
662 goto out;
663
664 ifm = nlmsg_data(nlh);
665
666 rtnl_net_lock(net);
667
668 in_dev = inetdev_by_index(net, ifm->ifa_index);
669 if (!in_dev) {
670 NL_SET_ERR_MSG(extack, "ipv4: Device not found");
671 err = -ENODEV;
672 goto unlock;
673 }
674
675 for (ifap = &in_dev->ifa_list;

 > 676		     (ifa = rtnl_net_dereference(net, *ifap)) != NULL;

677 ifap = &ifa->ifa_next) {
678 if (tb[IFA_LOCAL] &&
679 ifa->ifa_local != nla_get_in_addr(tb[IFA_LOCAL]))
680 continue;
681
682 if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label))
683 continue;
684
685 if (tb[IFA_ADDRESS] &&
686 (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
687 !inet_ifa_match(nla_get_in_addr(tb[IFA_ADDRESS]), ifa)))
688 continue;
689
690 if (ipv4_is_multicast(ifa->ifa_address))
691 ip_mc_autojoin_config(net, false, ifa);
692
693 __inet_del_ifa(in_dev, ifap, 1, nlh, NETLINK_CB(skb).portid);
694 goto unlock;
695 }
696
697 NL_SET_ERR_MSG(extack, "ipv4: Address not found");
698 err = -EADDRNOTAVAIL;
699 unlock:
700 rtnl_net_unlock(net);
701 out:
702 return err;
703 }
704

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#16 lore raw

PATCH 7/11 ipv4: Convert check_lifetime() to per-netns RTNL.

In reply to Kuniyuki Iwashima

Since commit 1675f385213e ("ipv4: Namespacify IPv4 address GC."), check_lifetime() works on a per-netns basis.

Let's use rtnl_net_lock() and rtnl_net_dereference().

---
 net/ipv4/devinet.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
net/ipv4/devinet.c +5 -4
index cbda22eb8d06..f8e232dbb96a 100644
@@ -756,7 +756,8 @@ static void check_lifetime(struct work_struct *work)
 		rcu_read_unlock();
 		if (!change_needed)
 			continue;
-		rtnl_lock();
+
+		rtnl_net_lock(net);
 		hlist_for_each_entry_safe(ifa, n, head, addr_lst) {
 			unsigned long age;
 
@@ -773,7 +774,7 @@ static void check_lifetime(struct work_struct *work)
 				struct in_ifaddr *tmp;
 
 				ifap = &ifa->ifa_dev->ifa_list;
-				tmp = rtnl_dereference(*ifap);
+				tmp = rtnl_net_dereference(net, *ifap);
 				while (tmp) {
 					if (tmp == ifa) {
 						inet_del_ifa(ifa->ifa_dev,
@@ -781,7 +782,7 @@ static void check_lifetime(struct work_struct *work)
 						break;
 					}
 					ifap = &tmp->ifa_next;
-					tmp = rtnl_dereference(*ifap);
+					tmp = rtnl_net_dereference(net, *ifap);
 				}
 			} else if (ifa->ifa_preferred_lft !=
 				   INFINITY_LIFE_TIME &&
@@ -791,7 +792,7 @@ static void check_lifetime(struct work_struct *work)
 				rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0);
 			}
 		}
-		rtnl_unlock();
+		rtnl_net_unlock(net);
 	}
 
 	next_sec = round_jiffies_up(next);
Signature · 1 line
2.39.5 (Apple Git-154)
#18 lore raw

PATCH 8/11 rtnetlink: Define rtnl_net_lock().

In reply to Kuniyuki Iwashima

We will need the per-netns version of rtnl_trylock().

rtnl_net_trylock() calls __rtnl_net_lock() only when rtnl_trylock() successfully holds RTNL.

When RTNL is removed, we will use mutex_trylock() for per-netns RTNL.

---
 include/linux/rtnetlink.h |  6 ++++++
 net/core/rtnetlink.c      | 11 +++++++++++
 2 files changed, 17 insertions(+)
include/linux/rtnetlink.h +6 -0
index 8468a4ce8510..b34d610b1249 100644
@@ -101,6 +101,7 @@ void __rtnl_net_lock(struct net *net);
 void __rtnl_net_unlock(struct net *net);
 void rtnl_net_lock(struct net *net);
 void rtnl_net_unlock(struct net *net);
+int rtnl_net_trylock(struct net *net);
 int rtnl_net_lock_cmp_fn(const struct lockdep_map *a, const struct lockdep_map *b);
 
 bool rtnl_net_is_locked(struct net *net);
@@ -132,6 +133,11 @@ static inline void rtnl_net_unlock(struct net *net)
 	rtnl_unlock();
 }
 
+static inline int rtnl_net_trylock(struct net *net)
+{
+	return rtnl_trylock();
+}
+
 static inline void ASSERT_RTNL_NET(struct net *net)
 {
 	ASSERT_RTNL();
net/core/rtnetlink.c +11 -0
index a9c92392fb1d..bb4927da0275 100644
@@ -210,6 +210,17 @@ void rtnl_net_unlock(struct net *net)
 }
 EXPORT_SYMBOL(rtnl_net_unlock);
 
+int rtnl_net_trylock(struct net *net)
+{
+	int ret = rtnl_trylock();
+
+	if (ret)
+		__rtnl_net_lock(net);
+
+	return ret;
+}
+EXPORT_SYMBOL(rtnl_net_trylock);
+
 static int rtnl_net_cmp_locks(const struct net *net_a, const struct net *net_b)
 {
 	if (net_eq(net_a, net_b))
Signature · 1 line
2.39.5 (Apple Git-154)
Reviewed-by on v1 8/11
#19 lore raw

Re: [PATCH v1 net-next 08/11] rtnetlink: Define rtnl_net_lock().

Quoted 10 lines · Kuniyuki Iwashima

We will need the per-netns version of rtnl_trylock().

rtnl_net_trylock() calls __rtnl_net_lock() only when rtnl_trylock() successfully holds RTNL.

When RTNL is removed, we will use mutex_trylock() for per-netns RTNL.

---

#20 lore raw

PATCH 9/11 ipv4: Convert devinet_sysctl_forward() to per-netns RTNL.

In reply to Kuniyuki Iwashima

devinet_sysctl_forward() touches only a single netns.

Let's use rtnl_trylock() and __in_dev_get_rtnl_net().

---
 net/ipv4/devinet.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
net/ipv4/devinet.c +3 -3
index f8e232dbb96a..1cc2c2b4a10a 100644
@@ -2380,7 +2380,7 @@ static void inet_forward_change(struct net *net)
 		if (on)
 			dev_disable_lro(dev);
 
-		in_dev = __in_dev_get_rtnl(dev);
+		in_dev = __in_dev_get_rtnl_net(dev);
 		if (in_dev) {
 			IN_DEV_CONF_SET(in_dev, FORWARDING, on);
 			inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
@@ -2471,7 +2471,7 @@ static int devinet_sysctl_forward(const struct ctl_table *ctl, int write,
 
 	if (write && *valp != val) {
 		if (valp != &IPV4_DEVCONF_DFLT(net, FORWARDING)) {
-			if (!rtnl_trylock()) {
+			if (!rtnl_net_trylock(net)) {
 				/* Restore the original values before restarting */
 				*valp = val;
 				*ppos = pos;
@@ -2490,7 +2490,7 @@ static int devinet_sysctl_forward(const struct ctl_table *ctl, int write,
 							    idev->dev->ifindex,
 							    cnf);
 			}
-			rtnl_unlock();
+			rtnl_net_unlock(net);
 			rt_cache_flush(net);
 		} else
 			inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
Signature · 1 line
2.39.5 (Apple Git-154)
#22 lore raw

PATCH 10/11 ipv4: Convert devinet_ioctl() to per-netns RTNL except for SIOCSIFFLAGS.

In reply to Kuniyuki Iwashima

Basically, devinet_ioctl() operates on a single netns.

However, ioctl(SIOCSIFFLAGS) will trigger the netdev notifier that could touch another netdev in different netns.

Let's use per-netns RTNL helper in devinet_ioctl() and place ASSERT_RTNL() for SIOCSIFFLAGS.

We will remove ASSERT_RTNL() once RTM_SETLINK and RTM_DELLINK are converted.

---
 net/ipv4/devinet.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
net/ipv4/devinet.c +9 -8
index 1cc2c2b4a10a..f4790859ea69 100644
@@ -574,9 +574,7 @@ static int inet_insert_ifa(struct in_ifaddr *ifa)
 
 static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
 {
-	struct in_device *in_dev = __in_dev_get_rtnl(dev);
-
-	ASSERT_RTNL();
+	struct in_device *in_dev = __in_dev_get_rtnl_net(dev);
 
 	ipv4_devconf_setall(in_dev);
 	neigh_parms_data_state_setall(in_dev->arp_parms);
@@ -1114,7 +1112,7 @@ int devinet_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr)
 		goto out;
 	}
 
-	rtnl_lock();
+	rtnl_net_lock(net);
 
 	ret = -ENODEV;
 	dev = __dev_get_by_name(net, ifr->ifr_name);
@@ -1124,7 +1122,7 @@ int devinet_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr)
 	if (colon)
 		*colon = ':';
 
-	in_dev = __in_dev_get_rtnl(dev);
+	in_dev = __in_dev_get_rtnl_net(dev);
 	if (in_dev) {
 		if (tryaddrmatch) {
 			/* Matthias Andree */
@@ -1134,7 +1132,7 @@ int devinet_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr)
 			   This is checked above. */
 
 			for (ifap = &in_dev->ifa_list;
-			     (ifa = rtnl_dereference(*ifap)) != NULL;
+			     (ifa = rtnl_net_dereference(net, *ifap)) != NULL;
 			     ifap = &ifa->ifa_next) {
 				if (!strcmp(ifr->ifr_name, ifa->ifa_label) &&
 				    sin_orig.sin_addr.s_addr ==
@@ -1148,7 +1146,7 @@ int devinet_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr)
 		   comparing just the label */
 		if (!ifa) {
 			for (ifap = &in_dev->ifa_list;
-			     (ifa = rtnl_dereference(*ifap)) != NULL;
+			     (ifa = rtnl_net_dereference(net, *ifap)) != NULL;
 			     ifap = &ifa->ifa_next)
 				if (!strcmp(ifr->ifr_name, ifa->ifa_label))
 					break;
@@ -1190,6 +1188,9 @@ int devinet_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr)
 				inet_del_ifa(in_dev, ifap, 1);
 			break;
 		}
+
+		/* NETDEV_UP/DOWN/CHANGE could touch a peer dev */
+		ASSERT_RTNL();
 		ret = dev_change_flags(dev, ifr->ifr_flags, NULL);
 		break;
 
@@ -1291,7 +1292,7 @@ int devinet_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr)
 		break;
 	}
 done:
-	rtnl_unlock();
+	rtnl_net_unlock(net);
 out:
 	return ret;
 }
Signature · 1 line
2.39.5 (Apple Git-154)
Reviewed-by on v1 10/11
#23 lore raw

Re: [PATCH v1 net-next 10/11] ipv4: Convert devinet_ioctl() to per-netns RTNL except for SIOCSIFFLAGS.

Quoted 6 lines · Kuniyuki Iwashima

Basically, devinet_ioctl() operates on a single netns.

However, ioctl(SIOCSIFFLAGS) will trigger the netdev notifier that could touch another netdev in different netns.

Quoted 7 lines

Let's use per-netns RTNL helper in devinet_ioctl() and place ASSERT_RTNL() for SIOCSIFFLAGS.

We will remove ASSERT_RTNL() once RTM_SETLINK and RTM_DELLINK are converted.

#24 lore raw

PATCH 11/11 ipv4: Convert devinet_ioctl to per-netns RTNL.

In reply to Kuniyuki Iwashima

ioctl(SIOCGIFCONF) calls dev_ifconf() that operates on the current netns.

Let's use per-netns RTNL helpers in dev_ifconf() and inet_gifconf().

---
 net/core/dev_ioctl.c | 6 +++---
 net/ipv4/devinet.c   | 4 ++--
 2 files changed, 5 insertions(+), 5 deletions(-)
net/core/dev_ioctl.c +3 -3
index 473c437b6b53..46d43b950471 100644
@@ -64,7 +64,7 @@ int dev_ifconf(struct net *net, struct ifconf __user *uifc)
 	}
 
 	/* Loop over the interfaces, and write an info block for each. */
-	rtnl_lock();
+	rtnl_net_lock(net);
 	for_each_netdev(net, dev) {
 		if (!pos)
 			done = inet_gifconf(dev, NULL, 0, size);
@@ -72,12 +72,12 @@ int dev_ifconf(struct net *net, struct ifconf __user *uifc)
 			done = inet_gifconf(dev, pos + total,
 					    len - total, size);
 		if (done < 0) {
-			rtnl_unlock();
+			rtnl_net_unlock(net);
 			return -EFAULT;
 		}
 		total += done;
 	}
-	rtnl_unlock();
+	rtnl_net_unlock(net);
 
 	return put_user(total, &uifc->ifc_len);
 }
net/ipv4/devinet.c +2 -2
index f4790859ea69..6089d9255d31 100644
@@ -1299,7 +1299,7 @@ int devinet_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr)
 
 int inet_gifconf(struct net_device *dev, char __user *buf, int len, int size)
 {
-	struct in_device *in_dev = __in_dev_get_rtnl(dev);
+	struct in_device *in_dev = __in_dev_get_rtnl_net(dev);
 	const struct in_ifaddr *ifa;
 	struct ifreq ifr;
 	int done = 0;
@@ -1310,7 +1310,7 @@ int inet_gifconf(struct net_device *dev, char __user *buf, int len, int size)
 	if (!in_dev)
 		goto out;
 
-	in_dev_for_each_ifa_rtnl(ifa, in_dev) {
+	in_dev_for_each_ifa_rtnl_net(dev_net(dev), ifa, in_dev) {
 		if (!buf) {
 			done += size;
 			continue;
Signature · 1 line
2.39.5 (Apple Git-154)
#26 lore raw

Re: [PATCH v1 net-next 11/11] ipv4: Convert devinet_ioctl to per-netns RTNL.

In reply to Kuniyuki Iwashima
kernel test robot noticed the following build warnings:

Hi Kuniyuki,

kernel test robot noticed the following build warnings:

[auto build test WARNING on net-next/main]

url:    https://github.com/intel-lab-lkp/linux/commits/Kuniyuki-Iwashima/rtnetlink-Define-RTNL_FLAG_DOIT_PERNET-for-per-netns-RTNL-doit/20241018-092802
base:   net-next/main
patch link:    https://lore.kernel.org/r/20241018012225.90409-12-kuniyu%40amazon.com
patch subject: [PATCH v1 net-next 11/11] ipv4: Convert devinet_ioctl to per-netns RTNL.
config: x86_64-randconfig-122-20241019 (https://download.01.org/0day-ci/archive/20241020/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241020/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

sparse warnings: (new ones prefixed by >>)
net/ipv4/devinet.c:674:47: sparse: sparse: incorrect type in argument 2 (different address spaces) @@ expected void *p @@ got struct in_ifaddr [noderef] __rcu * @@
net/ipv4/devinet.c:674:47: sparse: expected void *p
net/ipv4/devinet.c:674:47: sparse: got struct in_ifaddr [noderef] __rcu *
net/ipv4/devinet.c:775:65: sparse: sparse: incorrect type in argument 2 (different address spaces) @@ expected void *p @@ got struct in_ifaddr [noderef] __rcu * @@
net/ipv4/devinet.c:775:65: sparse: expected void *p
net/ipv4/devinet.c:775:65: sparse: got struct in_ifaddr [noderef] __rcu *
net/ipv4/devinet.c:783:73: sparse: sparse: incorrect type in argument 2 (different address spaces) @@ expected void *p @@ got struct in_ifaddr [noderef] __rcu * @@
net/ipv4/devinet.c:783:73: sparse: expected void *p
net/ipv4/devinet.c:783:73: sparse: got struct in_ifaddr [noderef] __rcu *
net/ipv4/devinet.c:945:9: sparse: sparse: incorrect type in argument 2 (different address spaces) @@ expected void *p @@ got struct in_ifaddr [noderef] __rcu *ifa_list @@
net/ipv4/devinet.c:945:9: sparse: expected void *p
net/ipv4/devinet.c:945:9: sparse: got struct in_ifaddr [noderef] __rcu *ifa_list
net/ipv4/devinet.c:945:9: sparse: sparse: incorrect type in argument 2 (different address spaces) @@ expected void *p @@ got struct in_ifaddr [noderef] __rcu *ifa_next @@
net/ipv4/devinet.c:945:9: sparse: expected void *p
net/ipv4/devinet.c:945:9: sparse: got struct in_ifaddr [noderef] __rcu *ifa_next
net/ipv4/devinet.c:1135:63: sparse: sparse: incorrect type in argument 2 (different address spaces) @@ expected void *p @@ got struct in_ifaddr [noderef] __rcu * @@
net/ipv4/devinet.c:1135:63: sparse: expected void *p
net/ipv4/devinet.c:1135:63: sparse: got struct in_ifaddr [noderef] __rcu *
net/ipv4/devinet.c:1149:63: sparse: sparse: incorrect type in argument 2 (different address spaces) @@ expected void *p @@ got struct in_ifaddr [noderef] __rcu * @@
net/ipv4/devinet.c:1149:63: sparse: expected void *p
net/ipv4/devinet.c:1149:63: sparse: got struct in_ifaddr [noderef] __rcu *
net/ipv4/devinet.c:1313:9: sparse: sparse: incorrect type in argument 2 (different address spaces) @@ expected void *p @@ got struct in_ifaddr [noderef] __rcu *ifa_list @@
net/ipv4/devinet.c:1313:9: sparse: expected void *p
net/ipv4/devinet.c:1313:9: sparse: got struct in_ifaddr [noderef] __rcu *ifa_list

>> net/ipv4/devinet.c:1313:9: sparse: sparse: incorrect type in argument 2 (different address spaces) @@     expected void *p @@     got struct in_ifaddr [noderef] __rcu *const ifa_next @@

net/ipv4/devinet.c:1313:9: sparse: expected void *p
net/ipv4/devinet.c:1313:9: sparse: got struct in_ifaddr [noderef] __rcu *const ifa_next
net/ipv4/devinet.c: note: in included file:
include/linux/inetdevice.h:261:54: sparse: sparse: incorrect type in argument 2 (different address spaces) @@ expected void *p @@ got struct in_device [noderef] __rcu *const ip_ptr @@
include/linux/inetdevice.h:261:54: sparse: expected void *p
include/linux/inetdevice.h:261:54: sparse: got struct in_device [noderef] __rcu *const ip_ptr
net/ipv4/devinet.c: note: in included file (through include/linux/inetdevice.h):
include/linux/rtnetlink.h:153:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
include/linux/rtnetlink.h:153:16: sparse: void [noderef] __rcu *
include/linux/rtnetlink.h:153:16: sparse: void *
include/linux/rtnetlink.h:153:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
include/linux/rtnetlink.h:153:16: sparse: void [noderef] __rcu *
include/linux/rtnetlink.h:153:16: sparse: void *
include/linux/rtnetlink.h:153:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
include/linux/rtnetlink.h:153:16: sparse: void [noderef] __rcu *
include/linux/rtnetlink.h:153:16: sparse: void *
include/linux/rtnetlink.h:153:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
include/linux/rtnetlink.h:153:16: sparse: void [noderef] __rcu *
include/linux/rtnetlink.h:153:16: sparse: void *
include/linux/rtnetlink.h:153:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
include/linux/rtnetlink.h:153:16: sparse: void [noderef] __rcu *
include/linux/rtnetlink.h:153:16: sparse: void *
include/linux/rtnetlink.h:153:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
include/linux/rtnetlink.h:153:16: sparse: void [noderef] __rcu *
include/linux/rtnetlink.h:153:16: sparse: void *
include/linux/rtnetlink.h:153:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
include/linux/rtnetlink.h:153:16: sparse: void [noderef] __rcu *
include/linux/rtnetlink.h:153:16: sparse: void *
include/linux/rtnetlink.h:153:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
include/linux/rtnetlink.h:153:16: sparse: void [noderef] __rcu *
include/linux/rtnetlink.h:153:16: sparse: void *
include/linux/rtnetlink.h:153:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
include/linux/rtnetlink.h:153:16: sparse: void [noderef] __rcu *
include/linux/rtnetlink.h:153:16: sparse: void *
include/linux/rtnetlink.h:153:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
include/linux/rtnetlink.h:153:16: sparse: void [noderef] __rcu *
include/linux/rtnetlink.h:153:16: sparse: void *
include/linux/rtnetlink.h:153:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
include/linux/rtnetlink.h:153:16: sparse: void [noderef] __rcu *
include/linux/rtnetlink.h:153:16: sparse: void *
include/linux/rtnetlink.h:153:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
include/linux/rtnetlink.h:153:16: sparse: void [noderef] __rcu *
include/linux/rtnetlink.h:153:16: sparse: void *
include/linux/rtnetlink.h:153:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
include/linux/rtnetlink.h:153:16: sparse: void [noderef] __rcu *
include/linux/rtnetlink.h:153:16: sparse: void *
include/linux/rtnetlink.h:153:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
include/linux/rtnetlink.h:153:16: sparse: void [noderef] __rcu *
include/linux/rtnetlink.h:153:16: sparse: void *

vim +1313 net/ipv4/devinet.c

1299
1300 int inet_gifconf(struct net_device *dev, char __user *buf, int len, int size)
1301 {
1302 struct in_device *in_dev = __in_dev_get_rtnl_net(dev);
1303 const struct in_ifaddr *ifa;
1304 struct ifreq ifr;
1305 int done = 0;
1306
1307 if (WARN_ON(size > sizeof(struct ifreq)))
1308 goto out;
1309
1310 if (!in_dev)
1311 goto out;
1312

> 1313		in_dev_for_each_ifa_rtnl_net(dev_net(dev), ifa, in_dev) {

1314 if (!buf) {
1315 done += size;
1316 continue;
1317 }
1318 if (len < size)
1319 break;
1320 memset(&ifr, 0, sizeof(struct ifreq));
1321 strcpy(ifr.ifr_name, ifa->ifa_label);
1322
1323 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
1324 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
1325 ifa->ifa_local;
1326
1327 if (copy_to_user(buf + done, &ifr, size)) {
1328 done = -EFAULT;
1329 break;
1330 }
1331 len -= size;
1332 done += size;
1333 }
1334 out:
1335 return done;
1336 }
1337

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki