Skip to contentSkip to conversation

ipv6: remove RTNL protection from ip6addrlbl_dump()

PATCH Superseded Eric Dumazet 2 participants 5 messages Apr 3, 2024

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ipv6: remove RTNL protection from ip6addrlbl_dump()

No longer hold RTNL while calling ip6addrlbl_dump() ("ip addrlabel show")

ip6addrlbl_dump() was already mostly relying on RCU anyway.

Add READ_ONCE()/WRITE_ONCE() annotations around
net->ipv6.ip6addrlbl_table.seq

Also change return value for a completed dump, so that NLMSG_DONE can be appended to current skb, saving one recvmsg() system call.

---
 net/ipv6/addrlabel.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
net/ipv6/addrlabel.c +9 -7
index 17ac45aa7194ce9c148ed95e14dd575d17feeb98..961e853543b64cd2060ef693ae3ad32a44780aa1 100644
@@ -234,7 +234,8 @@ static int __ip6addrlbl_add(struct net *net, struct ip6addrlbl_entry *newp,
 		hlist_add_head_rcu(&newp->list, &net->ipv6.ip6addrlbl_table.head);
 out:
 	if (!ret)
-		net->ipv6.ip6addrlbl_table.seq++;
+		WRITE_ONCE(net->ipv6.ip6addrlbl_table.seq,
+			   net->ipv6.ip6addrlbl_table.seq + 1);
 	return ret;
 }
 
@@ -445,7 +446,7 @@ static void ip6addrlbl_putmsg(struct nlmsghdr *nlh,
 };
 
 static int ip6addrlbl_fill(struct sk_buff *skb,
-			   struct ip6addrlbl_entry *p,
+			   const struct ip6addrlbl_entry *p,
 			   u32 lseq,
 			   u32 portid, u32 seq, int event,
 			   unsigned int flags)
@@ -498,7 +499,7 @@ static int ip6addrlbl_dump(struct sk_buff *skb, struct netlink_callback *cb)
 	struct net *net = sock_net(skb->sk);
 	struct ip6addrlbl_entry *p;
 	int idx = 0, s_idx = cb->args[0];
-	int err;
+	int err = 0;
 
 	if (cb->strict_check) {
 		err = ip6addrlbl_valid_dump_req(nlh, cb->extack);
@@ -510,7 +511,7 @@ static int ip6addrlbl_dump(struct sk_buff *skb, struct netlink_callback *cb)
 	hlist_for_each_entry_rcu(p, &net->ipv6.ip6addrlbl_table.head, list) {
 		if (idx >= s_idx) {
 			err = ip6addrlbl_fill(skb, p,
-					      net->ipv6.ip6addrlbl_table.seq,
+					      READ_ONCE(net->ipv6.ip6addrlbl_table.seq),
 					      NETLINK_CB(cb->skb).portid,
 					      nlh->nlmsg_seq,
 					      RTM_NEWADDRLABEL,
@@ -522,7 +523,7 @@ static int ip6addrlbl_dump(struct sk_buff *skb, struct netlink_callback *cb)
 	}
 	rcu_read_unlock();
 	cb->args[0] = idx;
-	return skb->len;
+	return err;
 }
 
 static inline int ip6addrlbl_msgsize(void)
@@ -614,7 +615,7 @@ static int ip6addrlbl_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
 
 	rcu_read_lock();
 	p = __ipv6_addr_label(net, addr, ipv6_addr_type(addr), ifal->ifal_index);
-	lseq = net->ipv6.ip6addrlbl_table.seq;
+	lseq = READ_ONCE(net->ipv6.ip6addrlbl_table.seq);
 	if (p)
 		err = ip6addrlbl_fill(skb, p, lseq,
 				      NETLINK_CB(in_skb).portid,
@@ -647,6 +648,7 @@ int __init ipv6_addr_label_rtnl_register(void)
 		return ret;
 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETADDRLABEL,
 				   ip6addrlbl_get,
-				   ip6addrlbl_dump, RTNL_FLAG_DOIT_UNLOCKED);
+				   ip6addrlbl_dump, RTNL_FLAG_DOIT_UNLOCKED |
+						    RTNL_FLAG_DUMP_UNLOCKED);
 	return ret;
 }
Signature · 1 line
2.44.0.478.gd926399ef9-goog
maintainer
#1 lore raw
Quoted 33 lines · Eric Dumazet
net/ipv6/addrlabel.c +4 -3
index 17ac45aa7194ce9c148ed95e14dd575d17feeb98..961e853543b64cd2060ef693ae3ad32a44780aa1 100644
@@ -234,7 +234,8 @@ static int __ip6addrlbl_add(struct net *net, struct ip6addrlbl_entry *newp,
 		hlist_add_head_rcu(&newp->list, &net->ipv6.ip6addrlbl_table.head);
 out:
 	if (!ret)
-		net->ipv6.ip6addrlbl_table.seq++;
+		WRITE_ONCE(net->ipv6.ip6addrlbl_table.seq,
+			   net->ipv6.ip6addrlbl_table.seq + 1);
 	return ret;
 }
 
@@ -445,7 +446,7 @@ static void ip6addrlbl_putmsg(struct nlmsghdr *nlh,
 };
 
 static int ip6addrlbl_fill(struct sk_buff *skb,
-			   struct ip6addrlbl_entry *p,
+			   const struct ip6addrlbl_entry *p,
 			   u32 lseq,
 			   u32 portid, u32 seq, int event,
 			   unsigned int flags)
@@ -498,7 +499,7 @@ static int ip6addrlbl_dump(struct sk_buff *skb, struct netlink_callback *cb)
 	struct net *net = sock_net(skb->sk);
 	struct ip6addrlbl_entry *p;
 	int idx = 0, s_idx = cb->args[0];
-	int err;
+	int err = 0;
 
 	if (cb->strict_check) {
 		err = ip6addrlbl_valid_dump_req(nlh, cb->extack);

net/ipv6/addrlabel.c static int ip6addrlbl_dump(struct sk_buff *skb, struct netlink_callback *cb) · line 514

Quoted 5 lines
net/ipv6/addrlabel.c +1 -1
@@ -510,7 +511,7 @@ static int ip6addrlbl_dump(struct sk_buff *skb, struct netlink_callback *cb)
 	hlist_for_each_entry_rcu(p, &net->ipv6.ip6addrlbl_table.head, list) {
 		if (idx >= s_idx) {
 			err = ip6addrlbl_fill(skb, p,
-					      net->ipv6.ip6addrlbl_table.seq,
+					      READ_ONCE(net->ipv6.ip6addrlbl_table.seq),

seems like this should be read once on entry, and the same value used for all iterations.

Quoted 30 lines
net/ipv6/addrlabel.c +4 -3
 					      NETLINK_CB(cb->skb).portid,
 					      nlh->nlmsg_seq,
 					      RTM_NEWADDRLABEL,
@@ -522,7 +523,7 @@ static int ip6addrlbl_dump(struct sk_buff *skb, struct netlink_callback *cb)
 	}
 	rcu_read_unlock();
 	cb->args[0] = idx;
-	return skb->len;
+	return err;
 }
 
 static inline int ip6addrlbl_msgsize(void)
@@ -614,7 +615,7 @@ static int ip6addrlbl_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
 
 	rcu_read_lock();
 	p = __ipv6_addr_label(net, addr, ipv6_addr_type(addr), ifal->ifal_index);
-	lseq = net->ipv6.ip6addrlbl_table.seq;
+	lseq = READ_ONCE(net->ipv6.ip6addrlbl_table.seq);
 	if (p)
 		err = ip6addrlbl_fill(skb, p, lseq,
 				      NETLINK_CB(in_skb).portid,
@@ -647,6 +648,7 @@ int __init ipv6_addr_label_rtnl_register(void)
 		return ret;
 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETADDRLABEL,
 				   ip6addrlbl_get,
-				   ip6addrlbl_dump, RTNL_FLAG_DOIT_UNLOCKED);
+				   ip6addrlbl_dump, RTNL_FLAG_DOIT_UNLOCKED |
+						    RTNL_FLAG_DUMP_UNLOCKED);
 	return ret;
 }
#2 lore raw
Quoted 32 lines · David Ahern
Quoted 30 lines · Eric Dumazet
net/ipv6/addrlabel.c +4 -3
index 17ac45aa7194ce9c148ed95e14dd575d17feeb98..961e853543b64cd2060ef693ae3ad32a44780aa1 100644
@@ -234,7 +234,8 @@ static int __ip6addrlbl_add(struct net *net, struct ip6addrlbl_entry *newp,
              hlist_add_head_rcu(&newp->list, &net->ipv6.ip6addrlbl_table.head);
 out:
      if (!ret)
-             net->ipv6.ip6addrlbl_table.seq++;
+             WRITE_ONCE(net->ipv6.ip6addrlbl_table.seq,
+                        net->ipv6.ip6addrlbl_table.seq + 1);
      return ret;
 }
 
@@ -445,7 +446,7 @@ static void ip6addrlbl_putmsg(struct nlmsghdr *nlh,
 };
 
 static int ip6addrlbl_fill(struct sk_buff *skb,
-                        struct ip6addrlbl_entry *p,
+                        const struct ip6addrlbl_entry *p,
                         u32 lseq,
                         u32 portid, u32 seq, int event,
                         unsigned int flags)
@@ -498,7 +499,7 @@ static int ip6addrlbl_dump(struct sk_buff *skb, struct netlink_callback *cb)
      struct net *net = sock_net(skb->sk);
      struct ip6addrlbl_entry *p;
      int idx = 0, s_idx = cb->args[0];
-     int err;
+     int err = 0;
 
Quoted 11 lines

net/ipv6/addrlabel.c static int ip6addrlbl_dump(struct sk_buff *skb, struct netlink_callback *cb) · line 514

Quoted 8 lines
net/ipv6/addrlabel.c +1 -1
      if (cb->strict_check) {
              err = ip6addrlbl_valid_dump_req(nlh, cb->extack);
@@ -510,7 +511,7 @@ static int ip6addrlbl_dump(struct sk_buff *skb, struct netlink_callback *cb)
      hlist_for_each_entry_rcu(p, &net->ipv6.ip6addrlbl_table.head, list) {
              if (idx >= s_idx) {
                      err = ip6addrlbl_fill(skb, p,
-                                           net->ipv6.ip6addrlbl_table.seq,
+                                           READ_ONCE(net->ipv6.ip6addrlbl_table.seq),

seems like this should be read once on entry, and the same value used for all iterations.

I thought of that, but this will miss any update done concurrently (if all entries fit in a single skb)

It is unclear to me what user space can do with ifal_seq

There is no clear signal like :

Initial seq _before_ the dump, and seq seen _after_ the dump.

maintainer
#3 lore raw
Quoted 45 lines · Eric Dumazet
Quoted 32 lines · David Ahern
Quoted 30 lines · Eric Dumazet
net/ipv6/addrlabel.c +4 -3
index 17ac45aa7194ce9c148ed95e14dd575d17feeb98..961e853543b64cd2060ef693ae3ad32a44780aa1 100644
@@ -234,7 +234,8 @@ static int __ip6addrlbl_add(struct net *net, struct ip6addrlbl_entry *newp,
              hlist_add_head_rcu(&newp->list, &net->ipv6.ip6addrlbl_table.head);
 out:
      if (!ret)
-             net->ipv6.ip6addrlbl_table.seq++;
+             WRITE_ONCE(net->ipv6.ip6addrlbl_table.seq,
+                        net->ipv6.ip6addrlbl_table.seq + 1);
      return ret;
 }
 
@@ -445,7 +446,7 @@ static void ip6addrlbl_putmsg(struct nlmsghdr *nlh,
 };
 
 static int ip6addrlbl_fill(struct sk_buff *skb,
-                        struct ip6addrlbl_entry *p,
+                        const struct ip6addrlbl_entry *p,
                         u32 lseq,
                         u32 portid, u32 seq, int event,
                         unsigned int flags)
@@ -498,7 +499,7 @@ static int ip6addrlbl_dump(struct sk_buff *skb, struct netlink_callback *cb)
      struct net *net = sock_net(skb->sk);
      struct ip6addrlbl_entry *p;
      int idx = 0, s_idx = cb->args[0];
-     int err;
+     int err = 0;
 
Quoted 11 lines

net/ipv6/addrlabel.c static int ip6addrlbl_dump(struct sk_buff *skb, struct netlink_callback *cb) · line 514

Quoted 8 lines
net/ipv6/addrlabel.c +1 -1
      if (cb->strict_check) {
              err = ip6addrlbl_valid_dump_req(nlh, cb->extack);
@@ -510,7 +511,7 @@ static int ip6addrlbl_dump(struct sk_buff *skb, struct netlink_callback *cb)
      hlist_for_each_entry_rcu(p, &net->ipv6.ip6addrlbl_table.head, list) {
              if (idx >= s_idx) {
                      err = ip6addrlbl_fill(skb, p,
-                                           net->ipv6.ip6addrlbl_table.seq,
+                                           READ_ONCE(net->ipv6.ip6addrlbl_table.seq),

seems like this should be read once on entry, and the same value used for all iterations.

Quoted 8 lines

I thought of that, but this will miss any update done concurrently (if all entries fit in a single skb)

It is unclear to me what user space can do with ifal_seq

There is no clear signal like :

Initial seq _before_ the dump, and seq seen _after_ the dump.

Since this a known change in behavior (however slight), it would be best to add something in the commit message or a code comment so if someone hits a problem and does a git bisect the problem and solution are clear.

#4 lore raw
Quoted 3 lines · David Ahern

Since this a known change in behavior (however slight), it would be best to add something in the commit message or a code comment so if someone hits a problem and does a git bisect the problem and solution are clear.

Ok.

The cb->seq / nl_dump_check_consistent() protocol could be implemented later, if necessary.