batman-adv: convert ifmcaddr6 to RCU
RFC PATCH 1/1 batman-adv: convert ifmcaddr6 to RCU
From: Taehee Yoo <[email protected]>
The ifmcaddr6 has been protected by inet6_dev->lock(rwlock) so that the critical section is atomic context. In order to switch this context, changing locking is needed. The ifmcaddr6 actually already protected by RTNL So if it's converted to use RCU, its control path context can be switched to sleepable.
---
@Sven: maybe something like this?
Seems to compile fine for me, without warnings and seems to run fine and use the compat code for me.
I tried to create and add it to compat-include/net/if_inet6.h, as that would fit better for the "struct ifmcaddr6", but couldn't get that to work, lots of errors. And when adding it to compat.h the compat code does not seem to be used at all.
Also let me know if the kernel version is correct or if it should be 5.14 instead?
compat-include/net/addrconf.h | 85 +++++++++++++++++++++++++++++++++++ net/batman-adv/multicast.c | 6 +-- 2 files changed, 88 insertions(+), 3 deletions(-)
compat-include/net/addrconf.h +85 -0
@@ -25,4 +25,89 @@ static inline int batadv_ipv6_mc_check_mld(struct sk_buff *skb)#endif /* LINUX_VERSION_IS_LESS(5, 1, 0) */ +#if LINUX_VERSION_IS_LESS (5 ,13 ,0 ) + +static bool batadv_mcast_mla_is_duplicate (u8 *mcast_addr, +struct hlist_head *mcast_list); + +static inline int +compat_batadv_mcast_mla_softif_get_ipv6 (struct net_device *dev, +struct hlist_head *mcast_list, +struct batadv_mcast_mla_flags *flags, +u8 *mcast_addr, +struct batadv_hw_addr *new, +struct inet6_dev *in6_dev) + +{ +struct ifmcaddr6 *pmc6; +int ret =0 ; + +if (flags->tvlv_flags &BATADV_MCAST_WANT_ALL_IPV6 ) +return 0 ; + +rcu_read_lock (); + + in6_dev =__in6_dev_get (dev); +if (!in6_dev) { +rcu_read_unlock (); +return 0 ; + } + +read_lock_bh (&in6_dev->lock); +for (pmc6 = in6_dev->mc_list; pmc6; pmc6 = pmc6->next) { +if (IPV6_ADDR_MC_SCOPE (&pmc6->mca_addr) < +IPV6_ADDR_SCOPE_LINKLOCAL ) +continue ; + +if (flags->tvlv_flags &BATADV_MCAST_WANT_ALL_UNSNOOPABLES && +ipv6_addr_is_ll_all_nodes (&pmc6->mca_addr)) +continue ; + +if (!(flags->tvlv_flags &BATADV_MCAST_WANT_NO_RTR6 ) && +IPV6_ADDR_MC_SCOPE (&pmc6->mca_addr) > +IPV6_ADDR_SCOPE_LINKLOCAL ) +continue ; + +ipv6_eth_mc_map (&pmc6->mca_addr, mcast_addr); + +if (batadv_mcast_mla_is_duplicate (mcast_addr, mcast_list)) +continue ; + + new =kmalloc (sizeof (*new),GFP_ATOMIC ); +if (!new) { + ret = -ENOMEM ; +break ; + } + +ether_addr_copy (new->addr, mcast_addr); +hlist_add_head (&new->list, mcast_list); + ret++; + } +read_unlock_bh (&in6_dev->lock); +rcu_read_unlock (); + +return ret; +} + +#define ifmcaddr6 \ + net_device *orig_dev = dev; \ + return compat_batadv_mcast_mla_softif_get_ipv6(orig_dev, \ + mcast_list, \ + flags, \ + mcast_addr, \ + new = NULL, \ + in6_dev = NULL); \ + } \ + static inline int \ + __unused_batadv_mcast_mla_softif_get_ipv6(struct net_device *dev, \ + struct hlist_head *mcast_list, \ + struct batadv_mcast_mla_flags *flags) \ + { \ + struct batadv_hw_addr *new; \ + struct inet6_dev *in6_dev; \ + u8 mcast_addr[ETH_ALEN]; \ + struct ifmcaddr6 + +#endif /* LINUX_VERSION_IS_LESS(5, 13, 0) */ +#endif /* _NET_BATMAN_ADV_COMPAT_NET_ADDRCONF_H_ */
net/batman-adv/multicast.c +3 -3
@@ -454,8 +454,9 @@ batadv_mcast_mla_softif_get_ipv6(struct net_device *dev,return 0 ; } -read_lock_bh (&in6_dev->lock); -for (pmc6 = in6_dev->mc_list; pmc6; pmc6 = pmc6->next) { +for (pmc6 =rcu_dereference (in6_dev->mc_list); + pmc6; + pmc6 =rcu_dereference (pmc6->next)) {if (IPV6_ADDR_MC_SCOPE (&pmc6->mca_addr) <IPV6_ADDR_SCOPE_LINKLOCAL )continue ;
@@ -484,7 +485,6 @@ batadv_mcast_mla_softif_get_ipv6(struct net_device *dev,hlist_add_head (&new->list, mcast_list); ret++; } -read_unlock_bh (&in6_dev->lock);rcu_read_unlock ();return ret;
Signature · 1 line
2.31.0
Quoted 19 lines · Linus Lüssing
[...]
net/batman-adv/multicast.c +3
-2@@ -454,8 +454,9 @@ batadv_mcast_mla_softif_get_ipv6(struct net_device *dev,return 0 ; } -read_lock_bh (&in6_dev->lock); -for (pmc6 = in6_dev->mc_list; pmc6; pmc6 = pmc6->next) { +for (pmc6 =rcu_dereference (in6_dev->mc_list); + pmc6; + pmc6 =rcu_dereference (pmc6->next)) {if (IPV6_ADDR_MC_SCOPE (&pmc6->mca_addr) <IPV6_ADDR_SCOPE_LINKLOCAL )continue ;
net/batman-adv/multicast.c batadv_mcast_mla_softif_get_ipv6(struct net_device *dev, · line 490
Quoted 7 lines
net/batman-adv/multicast.c +0
-1@@ -484,7 +485,6 @@ batadv_mcast_mla_softif_get_ipv6(struct net_device *dev,hlist_add_head (&new->list, mcast_list); ret++; } -read_unlock_bh (&in6_dev->lock);rcu_read_unlock ();return ret;
Ups, accidentally added this. Will remove that in the next version.
Quoted 25 lines · Linus Lüssing
Quoted 24 lines · Linus Lüssing
[...]
net/batman-adv/multicast.c +3
-3@@ -454,8 +454,9 @@ batadv_mcast_mla_softif_get_ipv6(struct net_device *dev,return 0 ; } -read_lock_bh (&in6_dev->lock); -for (pmc6 = in6_dev->mc_list; pmc6; pmc6 = pmc6->next) { +for (pmc6 =rcu_dereference (in6_dev->mc_list); + pmc6; + pmc6 =rcu_dereference (pmc6->next)) {if (IPV6_ADDR_MC_SCOPE (&pmc6->mca_addr) <IPV6_ADDR_SCOPE_LINKLOCAL )continue ;@@ -484,7 +485,6 @@ batadv_mcast_mla_softif_get_ipv6(struct net_device *dev,hlist_add_head (&new->list, mcast_list); ret++; } -read_unlock_bh (&in6_dev->lock);rcu_read_unlock ();
Quoted 5 lines
net/batman-adv/multicast.c batadv_mcast_mla_softif_get_ipv6(struct net_device *dev, · line 490
Quoted 2 lines
net/batman-adv/multicast.c +0
-0return ret;Ups, accidentally added this. Will remove that in the next version.
Ah, no, sorry, should be in there, changed my mind :D.
Sorry for the noise.