Skip to contentSkip to conversation

ipv4: igmp: annotate data-races around im->users

PATCH Applied Yuyang Huang 5 participants 7 messages May 22 – May 25

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

PATCH v2 1/1 ipv4: igmp: annotate data-races around im->users

/proc/net/igmp walks IPv4 multicast memberships under RCU and prints im->users without holding RTNL, while multicast join and leave paths update the field while holding RTNL. Annotate this intentional lockless snapshot with READ_ONCE() and the matching writers with WRITE_ONCE().

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")

---
Changes in v2:
- Added Fixes: tag

 net/ipv4/igmp.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
net/ipv4/igmp.c +7 -4
index 27d120183779..f2aca659b29c 100644
@@ -1541,7 +1541,7 @@ static void ____ip_mc_inc_group(struct in_device *in_dev, __be32 addr,
 	}
 
 	if  (im) {
-		im->users++;
+		WRITE_ONCE(im->users, im->users + 1);
 		ip_mc_add_src(in_dev, &addr, mode, 0, NULL, 0);
 		goto out;
 	}
@@ -1550,7 +1550,7 @@ static void ____ip_mc_inc_group(struct in_device *in_dev, __be32 addr,
 	if (!im)
 		goto out;
 
-	im->users = 1;
+	WRITE_ONCE(im->users, 1);
 	im->interface = in_dev;
 	in_dev_hold(in_dev);
 	im->multiaddr = addr;
@@ -1784,7 +1784,10 @@ void __ip_mc_dec_group(struct in_device *in_dev, __be32 addr, gfp_t gfp)
 	     (i = rtnl_dereference(*ip)) != NULL;
 	     ip = &i->next_rcu) {
 		if (i->multiaddr == addr) {
-			if (--i->users == 0) {
+			int new_users = i->users - 1;
+
+			WRITE_ONCE(i->users, new_users);
+			if (new_users == 0) {
 				ip_mc_hash_remove(in_dev, i);
 				*ip = i->next_rcu;
 				in_dev->mc_count--;
@@ -2977,7 +2980,7 @@ static int igmp_mc_seq_show(struct seq_file *seq, void *v)
 		delta = im->timer.expires - jiffies;
 		seq_printf(seq,
 			   "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
-			   im->multiaddr, im->users,
+			   im->multiaddr, READ_ONCE(im->users),
 			   im->tm_running,
 			   im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
 			   im->reporter);
Signature · 1 line
2.43.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 8 lines · Yuyang Huang

/proc/net/igmp walks IPv4 multicast memberships under RCU and prints im->users without holding RTNL, while multicast join and leave paths update the field while holding RTNL. Annotate this intentional lockless snapshot with READ_ONCE() and the matching writers with WRITE_ONCE().

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Reviewed-by
#2 lore raw
Quoted 9 lines · Jiayuan Chen
Quoted 6 lines · Yuyang Huang

/proc/net/igmp walks IPv4 multicast memberships under RCU and prints im->users without holding RTNL, while multicast join and leave paths update the field while holding RTNL. Annotate this intentional lockless snapshot with READ_ONCE() and the matching writers with WRITE_ONCE().

Quoted 5 lines
Quoted 2 lines
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
maintainer Reviewed-by on v2 1/1
#3 lore raw
In reply to Yuyang Huang
Quoted 8 lines · Yuyang Huang

/proc/net/igmp walks IPv4 multicast memberships under RCU and prints im->users without holding RTNL, while multicast join and leave paths update the field while holding RTNL. Annotate this intentional lockless snapshot with READ_ONCE() and the matching writers with WRITE_ONCE().

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Quoted 21 lines

---
Changes in v2:
- Added Fixes: tag

 net/ipv4/igmp.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
net/ipv4/igmp.c +1 -1
index 27d120183779..f2aca659b29c 100644
@@ -1541,7 +1541,7 @@ static void ____ip_mc_inc_group(struct in_device *in_dev, __be32 addr,
 	}
 
 	if  (im) {
-		im->users++;
+		WRITE_ONCE(im->users, im->users + 1);
 		ip_mc_add_src(in_dev, &addr, mode, 0, NULL, 0);
 		goto out;
 	}

net/ipv4/igmp.c static void ____ip_mc_inc_group(struct in_device *in_dev, __be32 addr, · line 1553

Quoted 5 lines
net/ipv4/igmp.c +1 -1
@@ -1550,7 +1550,7 @@ static void ____ip_mc_inc_group(struct in_device *in_dev, __be32 addr,
 	if (!im)
 		goto out;
 
-	im->users = 1;
+	WRITE_ONCE(im->users, 1);

This one isn't necessary since the object was just allocated and not yet published

#4 lore raw
Quoted 2 lines

This one isn't necessary since the object was just allocated and not yet published

Thanks for the comment. I did consider not modifying it when writing the patch, but decided to keep it for consistency, it makes the code easier to reason about later. If you feel strongly about removing it, I'm happy to send a v3.

Quoted 42 lines · Ido Schimmel
Quoted 8 lines · Yuyang Huang

/proc/net/igmp walks IPv4 multicast memberships under RCU and prints im->users without holding RTNL, while multicast join and leave paths update the field while holding RTNL. Annotate this intentional lockless snapshot with READ_ONCE() and the matching writers with WRITE_ONCE().

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Quoted 21 lines

---
Changes in v2:
- Added Fixes: tag

 net/ipv4/igmp.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
net/ipv4/igmp.c +1 -1
index 27d120183779..f2aca659b29c 100644
@@ -1541,7 +1541,7 @@ static void ____ip_mc_inc_group(struct in_device *in_dev, __be32 addr,
      }
 
      if  (im) {
-             im->users++;
+             WRITE_ONCE(im->users, im->users + 1);
              ip_mc_add_src(in_dev, &addr, mode, 0, NULL, 0);
              goto out;
      }

net/ipv4/igmp.c static void ____ip_mc_inc_group(struct in_device *in_dev, __be32 addr, · line 1553

Quoted 5 lines
net/ipv4/igmp.c +1 -1
@@ -1550,7 +1550,7 @@ static void ____ip_mc_inc_group(struct in_device *in_dev, __be32 addr,
      if (!im)
              goto out;
 
-     im->users = 1;
+     WRITE_ONCE(im->users, 1);

This one isn't necessary since the object was just allocated and not yet published

maintainer
#5 lore raw
Quoted 7 lines · Yuyang Huang
Quoted 2 lines

This one isn't necessary since the object was just allocated and not yet published

Thanks for the comment. I did consider not modifying it when writing the patch, but decided to keep it for consistency, it makes the code easier to reason about later. If you feel strongly about removing it, I'm happy to send a v3.

I don't think it's worth another version. Personally I wouldn't add WRITE_ONCE() where it's not necessary.

P.S. Please don't top post.

#6 lore raw
In reply to Yuyang Huang
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 10 lines · On Fri, 22 May 2026 18:39:06 +0900 you wrote:

/proc/net/igmp walks IPv4 multicast memberships under RCU and prints im->users without holding RTNL, while multicast join and leave paths update the field while holding RTNL. Annotate this intentional lockless snapshot with READ_ONCE() and the matching writers with WRITE_ONCE().

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")

[...]

Here is the summary with links:
- [net-next,v2] ipv4: igmp: annotate data-races around im->users

    https://git.kernel.org/netdev/net-next/c/061c0aa740d5

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html