igmp: convert struct ip_sf_list to RCU
Subsystems (3)
PATCH v2 1/1 igmp: convert struct ip_sf_list to RCU
Commit 23d2b94043ca ("igmp: Add ip_mc_list lock in ip_check_mc_rcu") added spin_lock_bh(&im->lock) to ip_check_mc_rcu() to prevent a use-after-free while iterating im->sources during concurrent deletions.
However, ip_check_mc_rcu() is called from RCU read-side critical sections in packet receive and route lookup fast paths (e.g. __mkroute_output(), ip_route_input_rcu(), and __udp4_lib_rcv()).
When igmpv3_send_cr() or igmpv3_send_report() holds &pmc->lock and calls add_grec() -> igmpv3_newpack() -> ip_route_output_ports(), an XFRM policy matching a multicast destination triggers xfrm_tmpl_resolve_one() -> xfrm4_get_saddr() -> __mkroute_output() -> ip_check_mc_rcu(). This attempts to acquire &im->lock while &pmc->lock is already held on the same CPU, triggering a lockdep recursive locking warning / deadlock.
Fix this by converting IPv4 struct ip_sf_list to RCU, mirroring the IPv6 implementation in net/ipv6/mcast.c:
1. Add struct rcu_head to struct ip_sf_list and annotate sf_next, sources, and tomb as __rcu pointers.
2. Use rcu_assign_pointer() and kfree_rcu() for list updates and deletions.
3. Remove spin_lock_bh(&im->lock) from ip_check_mc_rcu() and traverse im->sources locklessly with for_each_psf_rcu(), reading and writing counter fields with READ_ONCE() and WRITE_ONCE().
Note: RCU conversion of /proc/net/mcfilter will be done in a separate patch.
---
v2: addressed kernel test robot feedback.
v1: https://lore.kernel.org/netdev/[email protected]/
include/linux/igmp.h | 7 +- net/ipv4/igmp.c | 210 +++++++++++++++++++++++++++---------------- 2 files changed, 135 insertions(+), 82 deletions(-)
include/linux/igmp.h +4 -3
@@ -57,20 +57,21 @@ struct ip_mc_socklist { };struct ip_sf_list { -struct ip_sf_list *sf_next; +struct ip_sf_list __rcu *sf_next;unsigned long sf_count[2 ];/* include/exclude counts */ __be32 sf_inaddr;unsigned char sf_gsresp;/* include in g & s response? */ unsigned char sf_oldin;/* change state */ unsigned char sf_crcount;/* retrans. left to send */ +struct rcu_head rcu; };struct ip_mc_list {struct in_device *interface;__be32 multiaddr;unsigned int sfmode; -struct ip_sf_list *sources; -struct ip_sf_list *tomb; +struct ip_sf_list __rcu *sources; +struct ip_sf_list __rcu *tomb;unsigned long sfcount[2 ];union {struct ip_mc_list *next;
net/ipv4/igmp.c +131 -79
466 lines not on this page · raw message
Signature · 1 line
2.55.0.887.g758fc8c411-goog
Quoted 27 lines · Eric Dumazet
Commit
23d2b94043ca("igmp: Addip_mc_listlock inip_check_mc_rcu") addedspin_lock_bh(&im->lock)toip_check_mc_rcu()to prevent a use-after-free while iteratingim->sourcesduring concurrent deletions.However,
ip_check_mc_rcu()is called from RCU read-side critical sections in packet receive and route lookup fast paths (e.g.__mkroute_output(),ip_route_input_rcu(), and__udp4_lib_rcv()).When
igmpv3_send_cr()origmpv3_send_report()holds&pmc->lockand callsadd_grec()->igmpv3_newpack()->ip_route_output_ports(), an XFRM policy matching a multicast destination triggersxfrm_tmpl_resolve_one()->xfrm4_get_saddr()->__mkroute_output()->ip_check_mc_rcu(). This attempts to acquire&im->lockwhile&pmc->lockis already held on the same CPU, triggering a lockdep recursive locking warning / deadlock.Fix this by converting IPv4 struct
ip_sf_listto RCU, mirroring the IPv6 implementation innet/ipv6/mcast.c:1. Add struct
rcu_headto structip_sf_listand annotatesf_next, sources, and tomb as__rcupointers.
2. Usercu_assign_pointer()andkfree_rcu()for list updates and deletions.
3. Removespin_lock_bh(&im->lock)fromip_check_mc_rcu()and traverseim->sourceslocklessly withfor_each_psf_rcu(), reading and writing counter fields withREAD_ONCE()andWRITE_ONCE().
Quoted 7 lines
Note: RCU conversion of
/proc/net/mcfilterwill be done in a separate patch.Fixes 23d2b94043ca ("igmp: Add ip_mc_list lock in ip_check_mc_rcu")Reported-by [email protected]Signed-off-by Eric Dumazet <[email protected]>
[...]
net/ipv4/igmp.c static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im, · line 1260
Quoted 8 lines
net/ipv4/igmp.c +2
-0@@ -1228,11 +1254,12 @@ static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im,if (pmc->sfmode ==MCAST_INCLUDE ) {struct ip_sf_list *psf; +for_each_psf_mclock (im, psf) + psf->sf_crcount = pmc->crcount; pmc->tomb = im->tomb; pmc->sources = im->sources;
In case you need another version:
RCU_INIT_POINTER(pmc->tomb, pmc_dereference(im->tomb, im)); RCU_INIT_POINTER(pmc->sources,
pmc_dereference(im->sources, im));
Quoted 7 lines
net/ipv4/igmp.c +2
-3- im->tomb = im->sources =NULL ; -for (psf = pmc->sources; psf; psf = psf->sf_next) - psf->sf_crcount = pmc->crcount; +RCU_INIT_POINTER (im->tomb,NULL ); +RCU_INIT_POINTER (im->sources,NULL ); }spin_unlock_bh (&im->lock);
[...]
Quoted 27 lines
net/ipv4/igmp.c +12
-7@@ -2011,19 +2047,28 @@ static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,/* no more filters for this source */ if (psf_prev) - psf_prev->sf_next = psf->sf_next; +rcu_assign_pointer (psf_prev->sf_next, +pmc_dereference (psf->sf_next, pmc));else - pmc->sources = psf->sf_next; +rcu_assign_pointer (pmc->sources, +pmc_dereference (psf->sf_next, pmc));#ifdef CONFIG_IP_MULTICAST if (psf->sf_oldin && !IGMP_V1_SEEN (in_dev) && !IGMP_V2_SEEN (in_dev)) { - psf->sf_crcount = in_dev->mr_qrv ?:READ_ONCE (net->ipv4.sysctl_igmp_qrv); - psf->sf_next = pmc->tomb; - pmc->tomb = psf; - rv =1 ; - }else +struct ip_sf_list *dpsf =kmalloc_obj (*dpsf,GFP_ATOMIC ); + +if (dpsf) { + *dpsf = *psf; + dpsf->sf_crcount = in_dev->mr_qrv ?: +READ_ONCE (net->ipv4.sysctl_igmp_qrv); +rcu_assign_pointer (dpsf->sf_next, +pmc_dereference (pmc->tomb, pmc));
net/ipv4/igmp.c static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode, · line 2074
Quoted 10 lines
net/ipv4/igmp.c +5
-1+rcu_assign_pointer (pmc->tomb, dpsf); + rv = 1; + } + }#endif -kfree (psf); +kfree_rcu (psf, rcu); }return rv; }
The allocation can fail, but I don't see a better fix. I assume it's done in that way for the reason mentioned in [1].
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 9 lines · On Thu, 27 Aug 2026 16:06:56 +0000 you wrote:
Commit
23d2b94043ca("igmp: Addip_mc_listlock inip_check_mc_rcu") addedspin_lock_bh(&im->lock)toip_check_mc_rcu()to prevent a use-after-free while iteratingim->sourcesduring concurrent deletions.However,
ip_check_mc_rcu()is called from RCU read-side critical sections in packet receive and route lookup fast paths (e.g.__mkroute_output(),ip_route_input_rcu(), and__udp4_lib_rcv()).[...]
Here is the summary with links:
- [v2,net] igmp: convert struct ip_sf_list to RCU
https://git.kernel.org/netdev/net/c/2987ee196c88
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html