ipmr: No RTNL for RTNL_FAMILY_IPMR rtnetlink.
Subsystems (4)
cover ipmr: No RTNL for RTNL_FAMILY_IPMR rtnetlink.
This series removes RTNL from ipmr rtnetlink handlers.
After this series, there are a few RTNL left in net/ipv4/ipmr.c and such users will be converted to per-netns RTNL in another series.
Patch 1 adds a selftest to exercise most? of the RTNL paths
in net/ipv4/ipmr.c
Patch 2 - 6 converts RTM_GETLINK / RTM_GETROUTE handlers
to RCU.
Patch 7 - 9 converts ->exit_batch() to ->exit_rtnl() to
save one RTNL in cleanup_net().
Patch 10 - 11 removes unnecessary RTNL during setup_net()
failure.
Patch 12 is a random cleanup.
Patch 13 - 15 drops RTNL for RTM_NEWROUTE and RTM_DELROUTE.
Kuniyuki Iwashima (15):
selftest: net: Add basic functionality tests for ipmr.
ipmr: Annotate access to mrt->mroute_do_{pim,assert,wrvifwhole}.
ipmr: Convert ipmr_rtm_dumplink() to RCU.
ipmr: Use MAXVIFS in mroute_msgsize().
ipmr: Convert ipmr_rtm_getroute() to RCU.
ipmr: Convert ipmr_rtm_dumproute() to RCU.
ipmr: Move unregister_netdevice_many() out of mroute_clean_tables().
ipmr: Move unregister_netdevice_many() out of ipmr_free_table().
ipmr: Convert ipmr_net_exit_batch() to ->exit_rtnl().
ipmr: Remove RTNL in ipmr_rules_init() and ipmr_net_init().
ipmr: Call fib_rules_unregister() without RTNL.
ipmr: Define net->ipv4.{ipmr_notifier_ops,ipmr_seq} under
CONFIG_IP_MROUTE.
ipmr/ip6mr: Convert net->ipv[46].ipmr_seq to atomic_t.
ipmr: Add dedicated mutex for mrt->{mfc_hash,mfc_cache_list}.
ipmr: Don't hold RTNL for ipmr_rtm_route().
include/linux/mroute_base.h | 9 +- include/net/netns/ipv4.h | 6 +- include/net/netns/ipv6.h | 2 +- net/ipv4/ipmr.c | 257 ++++++---- net/ipv4/ipmr_base.c | 4 +- net/ipv6/ip6mr.c | 4 +- .../testing/selftests/net/forwarding/Makefile | 4 + tools/testing/selftests/net/forwarding/ipmr.c | 455 ++++++++++++++++++ 8 files changed, 630 insertions(+), 111 deletions(-) create mode 100644 tools/testing/selftests/net/forwarding/ipmr.c
Signature · 1 line
2.53.0.414.gf7e9f6c205-goog
PATCH 1/15 selftest: net: Add basic functionality tests for ipmr.
The new test exercise paths, where RTNL is needed, to catch lockdep splat:
setsockopt
MRT_INIT / MRT_DONE
MRT_ADD_VIF / MRT_DEL_VIF
MRT_ADD_MFC / MRT_DEL_MFC / MRT_ADD_MFC_PROXY / MRT_DEL_MFC_PROXY MRT_TABLE
MRT_FLUSH
rtnetlink
RTM_NEWROUTE
RTM_DELROUTE
NETDEV_UNREGISTER
I will extend this to cover IPv6 setsockopt() later.
--- .../testing/selftests/net/forwarding/Makefile | 4 + tools/testing/selftests/net/forwarding/ipmr.c | 455 ++++++++++++++++++ 2 files changed, 459 insertions(+) create mode 100644 tools/testing/selftests/net/forwarding/ipmr.c
tools/testing/selftests/net/forwarding/Makefile +4 -0
@@ -133,6 +133,10 @@ TEST_FILES := \ tc_common.sh \ # end of TEST_FILES +TEST_GEN_PROGS := \ + ipmr +# end of TEST_GEN_PROGS + TEST_INCLUDES := \ $(wildcard ../lib/sh/*.sh) \ ../lib.sh \
tools/testing/selftests/net/forwarding/ipmr.c new file +455 -0
455 lines not on this page · raw message
Signature · 1 line
2.53.0.414.gf7e9f6c205-goog
PATCH 2/15 ipmr: Annotate access to mrt->mroute_do_{pim,assert,wrvifwhole}.
In reply to Kuniyuki IwashimaThese fields in struct mr_table are updated in ip_mroute_setsockopt() under RTNL:
* mroute_do_pim
* mroute_do_assert
* mroute_do_wrvifwhole
However, ip_mroute_getsockopt() does not hold RTNL and read the first two fields locklessly, and ip_mr_forward() reads the latter two under RCU.
Let's use WRITE_ONCE() and READ_ONCE() for them.
--- net/ipv4/ipmr.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
net/ipv4/ipmr.c +8 -8
@@ -1506,7 +1506,7 @@ int ip_mroute_setsockopt(struct sock *sk, int optname, sockptr_t optval, ret = -EFAULT ;break ; } - mrt->mroute_do_assert = val; +WRITE_ONCE (mrt->mroute_do_assert, val);break ;case MRT_PIM :if (!ipmr_pimsm_enabled ()) {
@@ -1525,9 +1525,9 @@ int ip_mroute_setsockopt(struct sock *sk, int optname, sockptr_t optval, do_wrvifwhole = (val ==IGMPMSG_WRVIFWHOLE ); val = !!val;if (val != mrt->mroute_do_pim) { - mrt->mroute_do_pim = val; - mrt->mroute_do_assert = val; - mrt->mroute_do_wrvifwhole = do_wrvifwhole; +WRITE_ONCE (mrt->mroute_do_pim, val); +WRITE_ONCE (mrt->mroute_do_assert, val); +WRITE_ONCE (mrt->mroute_do_wrvifwhole, do_wrvifwhole); }break ;case MRT_TABLE :
@@ -1610,10 +1610,10 @@ int ip_mroute_getsockopt(struct sock *sk, int optname, sockptr_t optval,case MRT_PIM :if (!ipmr_pimsm_enabled ())return -ENOPROTOOPT ; - val = mrt->mroute_do_pim; + val =READ_ONCE (mrt->mroute_do_pim);break ;case MRT_ASSERT : - val = mrt->mroute_do_assert; + val =READ_ONCE (mrt->mroute_do_assert);break ;default :return -ENOPROTOOPT ;
@@ -2037,7 +2037,7 @@ static void ip_mr_forward(struct net *net, struct mr_table *mrt,atomic_long_inc (&c->_c.mfc_un.res.wrong_if); - if (true_vifi >= 0 && mrt->mroute_do_assert && + if (true_vifi >= 0 && READ_ONCE(mrt->mroute_do_assert) &&/* pimsm uses asserts, when switching from RPT to SPT, * so that we cannot check that packet arrived on an oif. * It is bad, but otherwise we would need to move pretty
@@ -2050,7 +2050,7 @@ static void ip_mr_forward(struct net *net, struct mr_table *mrt, MFC_ASSERT_THRESH)) { c->_c.mfc_un.res.last_assert = jiffies;ipmr_cache_report (mrt, skb, true_vifi,IGMPMSG_WRONGVIF ); -if (mrt->mroute_do_wrvifwhole) +if (READ_ONCE (mrt->mroute_do_wrvifwhole))ipmr_cache_report (mrt, skb, true_vifi,IGMPMSG_WRVIFWHOLE ); }
Signature · 1 line
2.53.0.414.gf7e9f6c205-goog
PATCH 3/15 ipmr: Convert ipmr_rtm_dumplink() to RCU.
In reply to Kuniyuki Iwashimanet->ipv4.mr_tables is updated under RTNL and can be read safely under RCU.
Once created, the multicast route tables are not removed until netns dismantle.
ipmr_rtm_dumplink() does not need RTNL protection for ipmr_for_each_table() and ipmr_fill_table() if RCU is held.
Let's convert it to RCU.
--- net/ipv4/ipmr.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-)
net/ipv4/ipmr.c +16 -11
@@ -2901,12 +2901,13 @@ static bool ipmr_fill_table(struct mr_table *mrt, struct sk_buff *skb)if (nla_put_u32 (skb,IPMRA_TABLE_ID , mrt->id) ||nla_put_u32 (skb,IPMRA_TABLE_CACHE_RES_QUEUE_LEN , queue_len) ||nla_put_s32 (skb,IPMRA_TABLE_MROUTE_REG_VIF_NUM , - mrt->mroute_reg_vif_num) || +READ_ONCE (mrt->mroute_reg_vif_num)) ||nla_put_u8 (skb,IPMRA_TABLE_MROUTE_DO_ASSERT , - mrt->mroute_do_assert) || -nla_put_u8 (skb,IPMRA_TABLE_MROUTE_DO_PIM , mrt->mroute_do_pim) || +READ_ONCE (mrt->mroute_do_assert)) || +nla_put_u8 (skb,IPMRA_TABLE_MROUTE_DO_PIM , +READ_ONCE (mrt->mroute_do_pim)) ||nla_put_u8 (skb,IPMRA_TABLE_MROUTE_DO_WRVIFWHOLE , - mrt->mroute_do_wrvifwhole)) +READ_ONCE (mrt->mroute_do_wrvifwhole)))return false;return true;
@@ -2919,7 +2920,7 @@ static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb)struct vif_device *vif; vif = &mrt->vif_table[vifid]; - vif_dev =rtnl_dereference (vif->dev); + vif_dev =vif_dev_read (vif);/* if the VIF doesn't exist just continue */ if (!vif_dev)return true;
@@ -2928,16 +2929,16 @@ static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb)if (!vif_nest)return false; -if (nla_put_u32 (skb,IPMRA_VIFA_IFINDEX , vif_dev->ifindex) || +if (nla_put_u32 (skb,IPMRA_VIFA_IFINDEX ,READ_ONCE (vif_dev->ifindex)) ||nla_put_u32 (skb,IPMRA_VIFA_VIF_ID , vifid) ||nla_put_u16 (skb,IPMRA_VIFA_FLAGS , vif->flags) || -nla_put_u64_64bit (skb,IPMRA_VIFA_BYTES_IN , vif->bytes_in, +nla_put_u64_64bit (skb,IPMRA_VIFA_BYTES_IN ,READ_ONCE (vif->bytes_in),IPMRA_VIFA_PAD ) || -nla_put_u64_64bit (skb,IPMRA_VIFA_BYTES_OUT , vif->bytes_out, +nla_put_u64_64bit (skb,IPMRA_VIFA_BYTES_OUT ,READ_ONCE (vif->bytes_out),IPMRA_VIFA_PAD ) || -nla_put_u64_64bit (skb,IPMRA_VIFA_PACKETS_IN , vif->pkt_in, +nla_put_u64_64bit (skb,IPMRA_VIFA_PACKETS_IN ,READ_ONCE (vif->pkt_in),IPMRA_VIFA_PAD ) || -nla_put_u64_64bit (skb,IPMRA_VIFA_PACKETS_OUT , vif->pkt_out, +nla_put_u64_64bit (skb,IPMRA_VIFA_PACKETS_OUT ,READ_ONCE (vif->pkt_out),IPMRA_VIFA_PAD ) ||nla_put_be32 (skb,IPMRA_VIFA_LOCAL_ADDR , vif->local) ||nla_put_be32 (skb,IPMRA_VIFA_REMOTE_ADDR , vif->remote)) {
@@ -2992,6 +2993,8 @@ static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb) s_t = cb->args[0 ]; s_e = cb->args[1 ]; +rcu_read_lock (); +ipmr_for_each_table (mrt, net) {struct nlattr *vifs, *af;struct ifinfomsg *hdr;
@@ -3048,6 +3051,8 @@ static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb) } out: +rcu_read_unlock (); + cb->args[1 ] = e; cb->args[0 ] = t;
@@ -3287,7 +3292,7 @@ static struct pernet_operations ipmr_net_ops = {static const struct rtnl_msg_handler ipmr_rtnl_msg_handlers[]__initconst = { {.protocol =RTNL_FAMILY_IPMR , .msgtype =RTM_GETLINK , - .dumpit = ipmr_rtm_dumplink}, + .dumpit = ipmr_rtm_dumplink, .flags =RTNL_FLAG_DUMP_UNLOCKED }, {.protocol =RTNL_FAMILY_IPMR , .msgtype =RTM_NEWROUTE , .doit = ipmr_rtm_route}, {.protocol =RTNL_FAMILY_IPMR , .msgtype =RTM_DELROUTE ,
Signature · 1 line
2.53.0.414.gf7e9f6c205-goog
PATCH 4/15 ipmr: Use MAXVIFS in mroute_msgsize().
In reply to Kuniyuki Iwashimamroute_msgsize() calculates skb size needed for ipmr_fill_mroute().
The size differs based on mrt->maxvif.
We will drop RTNL for ipmr_rtm_getroute() and mrt->maxvif may change under RCU.
To avoid -EMSGSIZE, let's calculate the size with the maximum value of mrt->maxvif, MAXVIFS.
struct rtnexthop is 8 bytes and MAXVIFS is 32, so the maximum delta is 256 bytes, which is small enough.
--- net/ipv4/ipmr.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
net/ipv4/ipmr.c +4 -5
@@ -2510,7 +2510,7 @@ static int _ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb, cmd, flags); } -static size_t mroute_msgsize (bool unresolved,int maxvif) +static size_t mroute_msgsize (bool unresolved) {size_t len =NLMSG_ALIGN (sizeof (struct rtmsg ))
@@ -2523,7 +2523,7 @@ static size_t mroute_msgsize(bool unresolved, int maxvif) len = len +nla_total_size (4 )/* RTA_IIF */ +nla_total_size (0 )/* RTA_MULTIPATH */ - + maxvif *NLA_ALIGN (sizeof (struct rtnexthop )) + +MAXVIFS *NLA_ALIGN (sizeof (struct rtnexthop ))/* RTA_MFC_STATS */ +nla_total_size_64bit (sizeof (struct rta_mfc_stats )) ;
@@ -2538,8 +2538,7 @@ static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,struct sk_buff *skb;int err = -ENOBUFS ; - skb =nlmsg_new (mroute_msgsize (mfc->_c.mfc_parent >=MAXVIFS , - mrt->maxvif), + skb =nlmsg_new (mroute_msgsize (mfc->_c.mfc_parent >=MAXVIFS ),GFP_ATOMIC );if (!skb) goto errout;
@@ -2711,7 +2710,7 @@ static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, goto errout_free; } - skb =nlmsg_new (mroute_msgsize (false, mrt->maxvif),GFP_KERNEL ); + skb =nlmsg_new (mroute_msgsize (false),GFP_KERNEL );if (!skb) { err = -ENOBUFS ; goto errout_free;
Signature · 1 line
2.53.0.414.gf7e9f6c205-goog
PATCH 5/15 ipmr: Convert ipmr_rtm_getroute() to RCU.
In reply to Kuniyuki Iwashimaipmr_rtm_getroute() calls __ipmr_get_table(), ipmr_cache_find(), and ipmr_fill_mroute().
The table is not removed until netns dismantle, and net->ipv4.mr_tables is managed with RCU list API, so __ipmr_get_table() is safe under RCU.
struct mfc_cache is freed by mr_cache_put() after RCU grace period, so we can use ipmr_cache_find() under RCU. rcu_read_lock() around it was just to avoid lockdep splat for rhl_for_each_entry_rcu().
ipmr_fill_mroute() calls mr_fill_mroute(), which properly uses RCU.
Let's drop RTNL for ipmr_rtm_getroute() and use RCU instead.
--- net/ipv4/ipmr.c | 34 ++++++++++++++++++---------------- net/ipv4/ipmr_base.c | 4 ++-- 2 files changed, 20 insertions(+), 18 deletions(-)
net/ipv4/ipmr.c +18 -16
@@ -2680,9 +2680,9 @@ static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, {struct net *net =sock_net (in_skb->sk);struct nlattr *tb[RTA_MAX +1 ]; -struct sk_buff *skb =NULL ;struct mfc_cache *cache;struct mr_table *mrt; +struct sk_buff *skb;__be32 src, grp;u32 tableid;int err;
@@ -2695,39 +2695,40 @@ static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, grp =nla_get_in_addr_default (tb[RTA_DST ],0 ); tableid =nla_get_u32_default (tb[RTA_TABLE ],0 ); + skb =nlmsg_new (mroute_msgsize (false),GFP_KERNEL ); +if (!skb) { + err = -ENOBUFS ; + goto errout; + } + +rcu_read_lock (); + mrt =__ipmr_get_table (net, tableid ? tableid :RT_TABLE_DEFAULT );if (!mrt) { err = -ENOENT ; - goto errout_free; + goto errout_unlock; } -/* entries are added/deleted only under RTNL */ -rcu_read_lock (); cache =ipmr_cache_find (mrt, src, grp); -rcu_read_unlock ();if (!cache) { err = -ENOENT ; - goto errout_free; - } - - skb =nlmsg_new (mroute_msgsize (false),GFP_KERNEL ); -if (!skb) { - err = -ENOBUFS ; - goto errout_free; + goto errout_unlock; } err =ipmr_fill_mroute (mrt, skb,NETLINK_CB (in_skb).portid, nlh->nlmsg_seq, cache,RTM_NEWROUTE ,0 );if (err <0 ) - goto errout_free; + goto errout_unlock; - err =rtnl_unicast (skb, net,NETLINK_CB (in_skb).portid); +rcu_read_unlock (); + err =rtnl_unicast (skb, net,NETLINK_CB (in_skb).portid); errout:return err; -errout_free: +errout_unlock: +rcu_read_unlock ();kfree_skb (skb); goto errout; }
@@ -3297,7 +3298,8 @@ static const struct rtnl_msg_handler ipmr_rtnl_msg_handlers[] __initconst = { {.protocol = RTNL_FAMILY_IPMR, .msgtype = RTM_DELROUTE, .doit = ipmr_rtm_route}, {.protocol = RTNL_FAMILY_IPMR, .msgtype = RTM_GETROUTE, - .doit = ipmr_rtm_getroute, .dumpit = ipmr_rtm_dumproute}, + .doit = ipmr_rtm_getroute, .dumpit = ipmr_rtm_dumproute, + .flags = RTNL_FLAG_DOIT_UNLOCKED}, };int __init ip_mr_init (void )
net/ipv4/ipmr_base.c +2 -2
@@ -223,7 +223,7 @@ int mr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,rcu_read_lock (); vif_dev =rcu_dereference (mrt->vif_table[c->mfc_parent].dev); -if (vif_dev &&nla_put_u32 (skb,RTA_IIF , vif_dev->ifindex) <0 ) { +if (vif_dev &&nla_put_u32 (skb,RTA_IIF ,READ_ONCE (vif_dev->ifindex)) <0 ) {rcu_read_unlock ();return -EMSGSIZE ; }
@@ -252,7 +252,7 @@ int mr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb, nhp->rtnh_flags =0 ; nhp->rtnh_hops = c->mfc_un.res.ttls[ct]; - nhp->rtnh_ifindex = vif_dev->ifindex; + nhp->rtnh_ifindex =READ_ONCE (vif_dev->ifindex); nhp->rtnh_len =sizeof (*nhp); } }
Signature · 1 line
2.53.0.414.gf7e9f6c205-goog
PATCH 6/15 ipmr: Convert ipmr_rtm_dumproute() to RCU.
In reply to Kuniyuki Iwashimaipmr_rtm_dumproute() calls mr_table_dump() or mr_rtm_dumproute(), and mr_rtm_dumproute() finally calls mr_table_dump().
mr_table_dump() calls the passed function, _ipmr_fill_mroute().
_ipmr_fill_mroute() is a wrapper of ipmr_fill_mroute() to cast struct mr_mfc * to struct mfc_cache *.
ipmr_fill_mroute() can be already called safely under RCU.
Let's convert ipmr_rtm_dumproute() to RCU.
--- net/ipv4/ipmr.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-)
net/ipv4/ipmr.c +20 -9
@@ -2736,15 +2736,17 @@ static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,static int ipmr_rtm_dumproute (struct sk_buff *skb,struct netlink_callback *cb) {struct fib_dump_filter filter = { - .rtnl_held = true, + .rtnl_held = false, };int err; +rcu_read_lock (); +if (cb->strict_check) { err =ip_valid_fib_dump_req (sock_net (skb->sk), cb->nlh, &filter, cb);if (err <0 ) -return err; + goto out; }if (filter.table_id) {
@@ -2752,19 +2754,28 @@ static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb) mrt =__ipmr_get_table (sock_net (skb->sk), filter.table_id);if (!mrt) { -if (rtnl_msg_family (cb->nlh) !=RTNL_FAMILY_IPMR ) -return skb->len; +if (rtnl_msg_family (cb->nlh) !=RTNL_FAMILY_IPMR ) { + err = skb->len; + goto out; + }NL_SET_ERR_MSG (cb->extack,"ipv4: MR table does not exist" ); -return -ENOENT ; + err = -ENOENT ; + goto out; } + err =mr_table_dump (mrt, skb, cb, _ipmr_fill_mroute, &mfc_unres_lock, &filter); -return skb->len ? : err; + err = skb->len ? : err; + goto out; } -return mr_rtm_dumproute (skb, cb, ipmr_mr_table_iter, - _ipmr_fill_mroute, &mfc_unres_lock, &filter); + err =mr_rtm_dumproute (skb, cb, ipmr_mr_table_iter, + _ipmr_fill_mroute, &mfc_unres_lock, &filter); +out: +rcu_read_unlock (); + +return err; }static const struct nla_policy rtm_ipmr_policy[RTA_MAX +1 ] = {
@@ -3299,7 +3310,7 @@ static const struct rtnl_msg_handler ipmr_rtnl_msg_handlers[] __initconst = { .doit = ipmr_rtm_route}, {.protocol = RTNL_FAMILY_IPMR, .msgtype = RTM_GETROUTE, .doit = ipmr_rtm_getroute, .dumpit = ipmr_rtm_dumproute, - .flags = RTNL_FLAG_DOIT_UNLOCKED}, + .flags = RTNL_FLAG_DOIT_UNLOCKED | RTNL_FLAG_DUMP_UNLOCKED}, };int __init ip_mr_init (void )
Signature · 1 line
2.53.0.414.gf7e9f6c205-goog
PATCH 7/15 ipmr: Move unregister_netdevice_many() out of mroute_clean_tables().
In reply to Kuniyuki IwashimaThis is a prep commit to convert ipmr_net_exit_batch() to ->exit_rtnl().
Let's move unregister_netdevice_many() in mroute_clean_tables() to its callers.
As a bonus, mrtsock_destruct() can do batching for all tables.
--- net/ipv4/ipmr.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-)
net/ipv4/ipmr.c +24 -10
@@ -112,7 +112,8 @@ static int ipmr_cache_report(const struct mr_table *mrt,static void mroute_netlink_event (struct mr_table *mrt,struct mfc_cache *mfc,int cmd);static void igmpmsg_netlink_event (const struct mr_table *mrt,struct sk_buff *pkt); -static void mroute_clean_tables (struct mr_table *mrt,int flags); +static void mroute_clean_tables (struct mr_table *mrt,int flags, +struct list_head *dev_kill_list);static void ipmr_expire_process (struct timer_list *t);#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
@@ -427,12 +428,15 @@ static struct mr_table *ipmr_new_table(struct net *net, u32 id)static void ipmr_free_table (struct mr_table *mrt) {struct net *net =read_pnet (&mrt->net); +LIST_HEAD (dev_kill_list);WARN_ON_ONCE (!mr_can_free_table (net));timer_shutdown_sync (&mrt->ipmr_expire_timer);mroute_clean_tables (mrt,MRT_FLUSH_VIFS |MRT_FLUSH_VIFS_STATIC | -MRT_FLUSH_MFC |MRT_FLUSH_MFC_STATIC ); +MRT_FLUSH_MFC |MRT_FLUSH_MFC_STATIC , + &dev_kill_list); +unregister_netdevice_many (&dev_kill_list);rhltable_destroy (&mrt->mfc_hash);kfree (mrt); }
@@ -1293,12 +1297,12 @@ static int ipmr_mfc_add(struct net *net, struct mr_table *mrt, }/* Close the multicast socket, and clear the vif tables etc */ -static void mroute_clean_tables (struct mr_table *mrt,int flags) +static void mroute_clean_tables (struct mr_table *mrt,int flags, +struct list_head *dev_kill_list) {struct net *net =read_pnet (&mrt->net); -struct mr_mfc *c, *tmp;struct mfc_cache *cache; -LIST_HEAD (list); +struct mr_mfc *c, *tmp;int i;/* Shut down all active vif entries */
@@ -1308,9 +1312,8 @@ static void mroute_clean_tables(struct mr_table *mrt, int flags) !(flags & MRT_FLUSH_VIFS_STATIC)) || (!(mrt->vif_table[i].flags & VIFF_STATIC) && !(flags & MRT_FLUSH_VIFS))) continue; -vif_delete (mrt, i,0 , &list); +vif_delete (mrt, i,0 , dev_kill_list); } -unregister_netdevice_many (&list); }/* Wipe the cache */
@@ -1349,9 +1352,11 @@ static void mroute_clean_tables(struct mr_table *mrt, int flags)static void mrtsock_destruct (struct sock *sk) {struct net *net =sock_net (sk); +LIST_HEAD (dev_kill_list);struct mr_table *mrt;rtnl_lock (); +ipmr_for_each_table (mrt, net) {if (sk ==rtnl_dereference (mrt->mroute_sk)) {IPV4_DEVCONF_ALL (net,MC_FORWARDING )--;
@@ -1360,9 +1365,13 @@ static void mrtsock_destruct(struct sock *sk)NETCONFA_IFINDEX_ALL , net->ipv4.devconf_all);RCU_INIT_POINTER (mrt->mroute_sk,NULL ); -mroute_clean_tables (mrt,MRT_FLUSH_VIFS |MRT_FLUSH_MFC ); +mroute_clean_tables (mrt,MRT_FLUSH_VIFS |MRT_FLUSH_MFC , + &dev_kill_list); } } + +unregister_netdevice_many (&dev_kill_list); +rtnl_unlock (); }
@@ -1485,7 +1494,9 @@ int ip_mroute_setsockopt(struct sock *sk, int optname, sockptr_t optval, sk ==rtnl_dereference (mrt->mroute_sk), parent);break ; -case MRT_FLUSH : +case MRT_FLUSH : { +LIST_HEAD (dev_kill_list); +if (optlen !=sizeof (val)) { ret = -EINVAL ;break ;
@@ -1494,8 +1505,11 @@ int ip_mroute_setsockopt(struct sock *sk, int optname, sockptr_t optval, ret = -EFAULT ;break ; } -mroute_clean_tables (mrt, val); + +mroute_clean_tables (mrt, val, &dev_kill_list); +unregister_netdevice_many (&dev_kill_list);break ; + }/* Control PIM assert. */ case MRT_ASSERT :if (optlen !=sizeof (val)) {
Signature · 1 line
2.53.0.414.gf7e9f6c205-goog
PATCH 8/15 ipmr: Move unregister_netdevice_many() out of ipmr_free_table().
In reply to Kuniyuki IwashimaThis is a prep commit to convert ipmr_net_exit_batch() to ->exit_rtnl().
Let's move unregister_netdevice_many() in ipmr_free_table() to its callers.
Now ipmr_rules_exit() can do batching all tables per netns.
Note that later we will remove RTNL and unregister_netdevice_many() in ipmr_rules_init().
--- net/ipv4/ipmr.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-)
net/ipv4/ipmr.c +17 -8
@@ -102,7 +102,8 @@ static DEFINE_SPINLOCK(mfc_unres_lock);static struct kmem_cache *mrt_cachep__ro_after_init ;static struct mr_table *ipmr_new_table (struct net *net,u32 id); -static void ipmr_free_table (struct mr_table *mrt); +static void ipmr_free_table (struct mr_table *mrt, +struct list_head *dev_kill_list); static void ip_mr_forward(struct net *net, struct mr_table *mrt, struct net_device *dev, struct sk_buff *skb,
@@ -251,6 +252,7 @@ static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {static int __net_init ipmr_rules_init (struct net *net) {struct fib_rules_ops *ops; +LIST_HEAD (dev_kill_list);struct mr_table *mrt;int err;
@@ -275,7 +277,8 @@ static int __net_init ipmr_rules_init(struct net *net) err2:rtnl_lock (); -ipmr_free_table (mrt); +ipmr_free_table (mrt, &dev_kill_list); +unregister_netdevice_many (&dev_kill_list);rtnl_unlock (); err1:fib_rules_unregister (ops);
@@ -285,12 +288,15 @@ static int __net_init ipmr_rules_init(struct net *net) static void __net_exit ipmr_rules_exit(struct net *net) {struct mr_table *mrt, *next; +LIST_HEAD (dev_kill_list);ASSERT_RTNL ();list_for_each_entry_safe (mrt, next, &net->ipv4.mr_tables, list) {list_del (&mrt->list); -ipmr_free_table (mrt); +ipmr_free_table (mrt, &dev_kill_list); } + +unregister_netdevice_many (&dev_kill_list);fib_rules_unregister (net->ipv4.mr_rules_ops); }
@@ -349,8 +355,13 @@ static int __net_init ipmr_rules_init(struct net *net) static void __net_exit ipmr_rules_exit(struct net *net) { +LIST_HEAD (dev_kill_list); +ASSERT_RTNL (); -ipmr_free_table (net->ipv4.mrt); + +ipmr_free_table (net->ipv4.mrt, &dev_kill_list); +unregister_netdevice_many (&dev_kill_list); + net->ipv4.mrt =NULL ; }
@@ -425,18 +436,16 @@ static struct mr_table *ipmr_new_table(struct net *net, u32 id) ipmr_expire_process, ipmr_new_table_set); } -static void ipmr_free_table (struct mr_table *mrt) +static void ipmr_free_table (struct mr_table *mrt,struct list_head *dev_kill_list) {struct net *net =read_pnet (&mrt->net); -LIST_HEAD (dev_kill_list);WARN_ON_ONCE (!mr_can_free_table (net));timer_shutdown_sync (&mrt->ipmr_expire_timer);mroute_clean_tables (mrt,MRT_FLUSH_VIFS |MRT_FLUSH_VIFS_STATIC |MRT_FLUSH_MFC |MRT_FLUSH_MFC_STATIC , - &dev_kill_list); -unregister_netdevice_many (&dev_kill_list); + dev_kill_list);rhltable_destroy (&mrt->mfc_hash);kfree (mrt); }
Signature · 1 line
2.53.0.414.gf7e9f6c205-goog
PATCH 9/15 ipmr: Convert ipmr_net_exit_batch() to ->exit_rtnl().
In reply to Kuniyuki Iwashimaipmr_net_ops uses ->exit_batch() to acquire RTNL only once for dying network namespaces.
ipmr does not depend on the ordering of ->exit_rtnl() and ->exit_batch() of other pernet_operations (unlike fib_net_ops).
Once ipmr_free_table() is called and all devices are queued for destruction in ->exit_rtnl(), later during NETDEV_UNREGISTER, ipmr_device_event() will not see anything in vif table and just do nothing.
Let's convert ipmr_net_exit_batch() to ->exit_rtnl().
Note that fib_rules_unregister() does not need RTNL and we will remove RTNL and unregister_netdevice_many() in ipmr_net_init().
--- net/ipv4/ipmr.c | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-)
net/ipv4/ipmr.c +13 -18
@@ -285,18 +285,17 @@ static int __net_init ipmr_rules_init(struct net *net)return err; } -static void __net_exit ipmr_rules_exit(struct net *net) +static void __net_exit ipmr_rules_exit_rtnl(struct net *net, +struct list_head *dev_kill_list) {struct mr_table *mrt, *next; -LIST_HEAD (dev_kill_list);ASSERT_RTNL ();list_for_each_entry_safe (mrt, next, &net->ipv4.mr_tables, list) {list_del (&mrt->list); -ipmr_free_table (mrt, &dev_kill_list); +ipmr_free_table (mrt, dev_kill_list); } -unregister_netdevice_many (&dev_kill_list);fib_rules_unregister (net->ipv4.mr_rules_ops); }
@@ -353,14 +352,12 @@ static int __net_init ipmr_rules_init(struct net *net)return 0 ; } -static void __net_exit ipmr_rules_exit(struct net *net) +static void __net_exit ipmr_rules_exit_rtnl(struct net *net, +struct list_head *dev_kill_list) { -LIST_HEAD (dev_kill_list); -ASSERT_RTNL (); -ipmr_free_table (net->ipv4.mrt, &dev_kill_list); -unregister_netdevice_many (&dev_kill_list); +ipmr_free_table (net->ipv4.mrt, dev_kill_list); net->ipv4.mrt =NULL ; }
@@ -3264,6 +3261,7 @@ static void __net_exit ipmr_notifier_exit(struct net *net)/* Setup for IP multicast routing */ static int __net_init ipmr_net_init (struct net *net) { +LIST_HEAD (dev_kill_list);int err; err =ipmr_notifier_init (net);
@@ -3290,7 +3288,8 @@ static int __net_init ipmr_net_init(struct net *net)remove_proc_entry ("ip_mr_vif" , net->proc_net); proc_vif_fail:rtnl_lock (); -ipmr_rules_exit (net); +ipmr_rules_exit_rtnl (net, &dev_kill_list); +unregister_netdevice_many (&dev_kill_list);rtnl_unlock ();#endif ipmr_rules_fail:
@@ -3308,20 +3307,16 @@ static void __net_exit ipmr_net_exit(struct net *net)ipmr_notifier_exit (net); } -static void __net_exit ipmr_net_exit_batch(struct list_head *net_list) +static void __net_exit ipmr_net_exit_rtnl(struct net *net, +struct list_head *dev_kill_list) { -struct net *net; - -rtnl_lock (); -list_for_each_entry (net, net_list, exit_list) -ipmr_rules_exit (net); -rtnl_unlock (); +ipmr_rules_exit_rtnl (net, dev_kill_list); }static struct pernet_operations ipmr_net_ops = { .init = ipmr_net_init, .exit = ipmr_net_exit, - .exit_batch = ipmr_net_exit_batch, + .exit_rtnl = ipmr_net_exit_rtnl, };static const struct rtnl_msg_handler ipmr_rtnl_msg_handlers[]__initconst = {
Signature · 1 line
2.53.0.414.gf7e9f6c205-goog
PATCH 10/15 ipmr: Remove RTNL in ipmr_rules_init() and ipmr_net_init().
In reply to Kuniyuki IwashimaWhen ipmr_free_table() is called from ipmr_rules_init() or ipmr_net_init(), the netns is not yet published.
Thus, no device should have been registered, and mroute_clean_tables() will not call vif_delete(), so unregister_netdevice_many() is unnecessary.
unregister_netdevice_many() does nothing if the list is empty, but it requires RTNL due to the unconditional ASSERT_RTNL() at the entry of unregister_netdevice_many_notify().
Let's remove unnecessary RTNL and ASSERT_RTNL() and instead add WARN_ON_ONCE() in ipmr_free_table().
--- net/ipv4/ipmr.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-)
net/ipv4/ipmr.c +2 -9
@@ -276,10 +276,7 @@ static int __net_init ipmr_rules_init(struct net *net)return 0 ; err2: -rtnl_lock ();ipmr_free_table (mrt, &dev_kill_list); -unregister_netdevice_many (&dev_kill_list); -rtnl_unlock (); err1:fib_rules_unregister (ops);return err;
@@ -290,7 +287,6 @@ static void __net_exit ipmr_rules_exit_rtnl(struct net *net, {struct mr_table *mrt, *next; -ASSERT_RTNL ();list_for_each_entry_safe (mrt, next, &net->ipv4.mr_tables, list) {list_del (&mrt->list);ipmr_free_table (mrt, dev_kill_list);
@@ -355,8 +351,6 @@ static int __net_init ipmr_rules_init(struct net *net) static void __net_exit ipmr_rules_exit_rtnl(struct net *net,struct list_head *dev_kill_list) { -ASSERT_RTNL (); -ipmr_free_table (net->ipv4.mrt, dev_kill_list); net->ipv4.mrt =NULL ;
@@ -445,6 +439,8 @@ static void ipmr_free_table(struct mr_table *mrt, struct list_head *dev_kill_lis dev_kill_list);rhltable_destroy (&mrt->mfc_hash);kfree (mrt); + +WARN_ON_ONCE (!net_initialized (net) && !list_empty (dev_kill_list)); }/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
@@ -3287,10 +3283,7 @@ static int __net_init ipmr_net_init(struct net *net) proc_cache_fail:remove_proc_entry ("ip_mr_vif" , net->proc_net); proc_vif_fail: -rtnl_lock ();ipmr_rules_exit_rtnl (net, &dev_kill_list); -unregister_netdevice_many (&dev_kill_list); -rtnl_unlock ();#endif ipmr_rules_fail:ipmr_notifier_exit (net);
Signature · 1 line
2.53.0.414.gf7e9f6c205-goog
PATCH 11/15 ipmr: Call fib_rules_unregister() without RTNL.
In reply to Kuniyuki Iwashimafib_rules_unregister() removes ops from net->rules_ops under spinlock, calls ops->delete() for each rule, and frees the ops.
ipmr_rules_ops_template does not have ->delete(), and any operation does not require RTNL there.
Let's move fib_rules_unregister() from ipmr_rules_exit_rtnl() to ipmr_net_exit().
--- net/ipv4/ipmr.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
net/ipv4/ipmr.c +11 -2
@@ -282,6 +282,11 @@ static int __net_init ipmr_rules_init(struct net *net)return err; } +static void __net_exit ipmr_rules_exit(struct net *net) +{ +fib_rules_unregister (net->ipv4.mr_rules_ops); +} + static void __net_exit ipmr_rules_exit_rtnl(struct net *net,struct list_head *dev_kill_list) {
@@ -291,8 +296,6 @@ static void __net_exit ipmr_rules_exit_rtnl(struct net *net,list_del (&mrt->list);ipmr_free_table (mrt, dev_kill_list); } - -fib_rules_unregister (net->ipv4.mr_rules_ops); } static int ipmr_rules_dump(struct net *net, struct notifier_block *nb,
@@ -348,6 +351,10 @@ static int __net_init ipmr_rules_init(struct net *net)return 0 ; } +static void __net_exit ipmr_rules_exit(struct net *net) +{ +} + static void __net_exit ipmr_rules_exit_rtnl(struct net *net,struct list_head *dev_kill_list) {
@@ -3284,6 +3291,7 @@ static int __net_init ipmr_net_init(struct net *net)remove_proc_entry ("ip_mr_vif" , net->proc_net); proc_vif_fail:ipmr_rules_exit_rtnl (net, &dev_kill_list); +ipmr_rules_exit (net);#endif ipmr_rules_fail:ipmr_notifier_exit (net);
@@ -3297,6 +3305,7 @@ static void __net_exit ipmr_net_exit(struct net *net)remove_proc_entry ("ip_mr_cache" , net->proc_net);remove_proc_entry ("ip_mr_vif" , net->proc_net);#endif +ipmr_rules_exit (net);ipmr_notifier_exit (net); }
Signature · 1 line
2.53.0.414.gf7e9f6c205-goog
PATCH 12/15 ipmr: Define net->ipv4.{ipmr_notifier_ops,ipmr_seq} under CONFIG_IP_MROUTE.
In reply to Kuniyuki Iwashimanet->ipv4.ipmr_notifier_ops and net->ipv4.ipmr_seq are used only in net/ipv4/ipmr.c.
Let's move these definitions under CONFIG_IP_MROUTE.
--- include/net/netns/ipv4.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
include/net/netns/ipv4.h +2 -3
@@ -279,6 +279,8 @@ struct netns_ipv4 {struct list_head mr_tables;struct fib_rules_ops *mr_rules_ops;#endif +struct fib_notifier_ops *ipmr_notifier_ops; +unsigned int ipmr_seq;/* protected by rtnl_mutex */ #endif #ifdef CONFIG_IP_ROUTE_MULTIPATH struct sysctl_fib_multipath_hash_seed sysctl_fib_multipath_hash_seed;
@@ -290,9 +292,6 @@ struct netns_ipv4 {struct fib_notifier_ops *notifier_ops;unsigned int fib_seq;/* writes protected by rtnl_mutex */ -struct fib_notifier_ops *ipmr_notifier_ops; -unsigned int ipmr_seq;/* protected by rtnl_mutex */ -atomic_t rt_genid;siphash_key_t ip_id_key;struct hlist_head *inet_addr_lst;
Signature · 1 line
2.53.0.414.gf7e9f6c205-goog
PATCH 13/15 ipmr/ip6mr: Convert net->ipv[46].ipmr_seq to atomic_t.
In reply to Kuniyuki IwashimaWe will no longer hold RTNL for ipmr_mfc_add() and ipmr_mfc_delete().
MFC entry can be loosely connected with VIF by its index for mrt->vif_table[] (stored in mfc_parent), but the two tables are not synchronised. i.e. Even if VIF 1 is removed, MFC for VIF 1 is not automatically removed.
The only field that the MFC/VIF interfaces share is net->ipv[46].ipmr_seq, which is protected by RTNL.
Adding a new mutex for both just to protect a single field is overkill.
Let's convert the field to atomic_t.
--- include/linux/mroute_base.h | 8 ++++---- include/net/netns/ipv4.h | 2 +- include/net/netns/ipv6.h | 2 +- net/ipv4/ipmr.c | 4 ++-- net/ipv6/ip6mr.c | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-)
include/linux/mroute_base.h +4 -4
@@ -76,7 +76,7 @@ static inline int mr_call_vif_notifiers(struct net *net, struct vif_device *vif, struct net_device *vif_dev, unsigned short vif_index, u32 tb_id, - unsigned int *ipmr_seq) + atomic_t *ipmr_seq) { struct vif_entry_notifier_info info = { .info = {
@@ -89,7 +89,7 @@ static inline int mr_call_vif_notifiers(struct net *net, };ASSERT_RTNL (); - (*ipmr_seq)++; +atomic_inc (ipmr_seq);return call_fib_notifiers (net, event_type, &info.info); }
@@ -198,7 +198,7 @@ static inline int mr_call_mfc_notifiers(struct net *net, unsigned short family, enum fib_event_type event_type, struct mr_mfc *mfc, u32 tb_id, - unsigned int *ipmr_seq) + atomic_t *ipmr_seq) { struct mfc_entry_notifier_info info = { .info = {
@@ -209,7 +209,7 @@ static inline int mr_call_mfc_notifiers(struct net *net, };ASSERT_RTNL (); - (*ipmr_seq)++; +atomic_inc (ipmr_seq);return call_fib_notifiers (net, event_type, &info.info); }
include/net/netns/ipv4.h +1 -1
@@ -280,7 +280,7 @@ struct netns_ipv4 {struct fib_rules_ops *mr_rules_ops;#endif struct fib_notifier_ops *ipmr_notifier_ops; -unsigned int ipmr_seq;/* protected by rtnl_mutex */ +atomic_t ipmr_seq;#endif #ifdef CONFIG_IP_ROUTE_MULTIPATH struct sysctl_fib_multipath_hash_seed sysctl_fib_multipath_hash_seed;
include/net/netns/ipv6.h +1 -1
@@ -118,7 +118,7 @@ struct netns_ipv6 {struct seg6_pernet_data *seg6_data;struct fib_notifier_ops *notifier_ops;struct fib_notifier_ops *ip6mr_notifier_ops; -unsigned int ipmr_seq;/* protected by rtnl_mutex */ +atomic_t ipmr_seq;struct {struct hlist_head head;spinlock_t lock;
net/ipv4/ipmr.c +2 -2
@@ -3224,7 +3224,7 @@ static const struct net_protocol pim_protocol = {static unsigned int ipmr_seq_read (const struct net *net) { -return READ_ONCE (net->ipv4.ipmr_seq) +ipmr_rules_seq_read (net); +return atomic_read (&net->ipv4.ipmr_seq) +ipmr_rules_seq_read (net); } static int ipmr_dump(struct net *net, struct notifier_block *nb,
@@ -3245,7 +3245,7 @@ static int __net_init ipmr_notifier_init(struct net *net) {struct fib_notifier_ops *ops; - net->ipv4.ipmr_seq =0 ; +atomic_set (&net->ipv4.ipmr_seq,0 ); ops =fib_notifier_ops_register (&ipmr_notifier_ops_template, net);if (IS_ERR (ops))
net/ipv6/ip6mr.c +2 -2
@@ -1280,7 +1280,7 @@ static int ip6mr_device_event(struct notifier_block *this,static unsigned int ip6mr_seq_read (const struct net *net) { -return READ_ONCE (net->ipv6.ipmr_seq) +ip6mr_rules_seq_read (net); +return atomic_read (&net->ipv6.ipmr_seq) +ip6mr_rules_seq_read (net); } static int ip6mr_dump(struct net *net, struct notifier_block *nb,
@@ -1305,7 +1305,7 @@ static int __net_init ip6mr_notifier_init(struct net *net) {struct fib_notifier_ops *ops; - net->ipv6.ipmr_seq =0 ; +atomic_set (&net->ipv6.ipmr_seq,0 ); ops =fib_notifier_ops_register (&ip6mr_notifier_ops_template, net);if (IS_ERR (ops))
Signature · 1 line
2.53.0.414.gf7e9f6c205-goog
PATCH 14/15 ipmr: Add dedicated mutex for mrt->{mfc_hash,mfc_cache_list}.
In reply to Kuniyuki IwashimaWe will no longer hold RTNL for ipmr_rtm_route() to modify the MFC hash table.
Only __dev_get_by_index() in rtm_to_ipmr_mfcc() is the RTNL dependant, otherwise, we just need protection for mrt->mfc_hash and mrt->mfc_cache_list.
Let's add a new mutex for ipmr_mfc_add(), ipmr_mfc_delete(), and mroute_clean_tables() (setsockopt(MRT_FLUSH or MRT_DONE)).
--- include/net/netns/ipv4.h | 1 + net/ipv4/ipmr.c | 28 ++++++++++++++++++++++------ 2 files changed, 23 insertions(+), 6 deletions(-)
include/net/netns/ipv4.h +1 -0
@@ -281,6 +281,7 @@ struct netns_ipv4 {#endif struct fib_notifier_ops *ipmr_notifier_ops;atomic_t ipmr_seq; +struct mutex mfc_mutex;#endif #ifdef CONFIG_IP_ROUTE_MULTIPATH struct sysctl_fib_multipath_hash_seed sysctl_fib_multipath_hash_seed;
net/ipv4/ipmr.c +22 -6
@@ -1327,6 +1327,8 @@ static void mroute_clean_tables(struct mr_table *mrt, int flags,/* Wipe the cache */ if (flags & (MRT_FLUSH_MFC |MRT_FLUSH_MFC_STATIC )) { +mutex_lock (&net->ipv4.mfc_mutex); +list_for_each_entry_safe (c, tmp, &mrt->mfc_cache_list, list) {if (((c->mfc_flags &MFC_STATIC ) && !(flags &MRT_FLUSH_MFC_STATIC )) || (!(c->mfc_flags &MFC_STATIC ) && !(flags &MRT_FLUSH_MFC )))
@@ -1339,6 +1341,8 @@ static void mroute_clean_tables(struct mr_table *mrt, int flags,mroute_netlink_event (mrt, cache,RTM_DELROUTE );mr_cache_put (c); } + +mutex_unlock (&net->ipv4.mfc_mutex); }if (flags &MRT_FLUSH_MFC ) {
@@ -1496,12 +1500,17 @@ int ip_mroute_setsockopt(struct sock *sk, int optname, sockptr_t optval, }if (parent ==0 ) parent = mfc.mfcc_parent; + +mutex_lock (&net->ipv4.mfc_mutex); +if (optname ==MRT_DEL_MFC || optname ==MRT_DEL_MFC_PROXY ) ret =ipmr_mfc_delete (mrt, &mfc, parent);else ret =ipmr_mfc_add (net, mrt, &mfc, sk ==rtnl_dereference (mrt->mroute_sk), parent); + +mutex_unlock (&net->ipv4.mfc_mutex);break ;case MRT_FLUSH : {LIST_HEAD (dev_kill_list);
@@ -2911,21 +2920,26 @@ static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh, struct netlink_ext_ack *extack) {struct net *net =sock_net (skb->sk); -int ret, mrtsock, parent; -struct mr_table *tbl; +int ret, mrtsock =0 , parent; +struct mr_table *tbl =NULL ;struct mfcctl mfcc; - mrtsock =0 ; - tbl =NULL ; ret =rtm_to_ipmr_mfcc (net, nlh, &mfcc, &mrtsock, &tbl, extack);if (ret <0 )return ret; parent = ret ? mfcc.mfcc_parent :-1 ; + +mutex_lock (&net->ipv4.mfc_mutex); +if (nlh->nlmsg_type ==RTM_NEWROUTE ) -return ipmr_mfc_add (net, tbl, &mfcc, mrtsock, parent); + ret =ipmr_mfc_add (net, tbl, &mfcc, mrtsock, parent);else -return ipmr_mfc_delete (tbl, &mfcc, parent); + ret =ipmr_mfc_delete (tbl, &mfcc, parent); + +mutex_unlock (&net->ipv4.mfc_mutex); + +return ret; }static bool ipmr_fill_table (struct mr_table *mrt,struct sk_buff *skb)
@@ -3267,6 +3281,8 @@ static int __net_init ipmr_net_init(struct net *net)LIST_HEAD (dev_kill_list);int err; +mutex_init (&net->ipv4.mfc_mutex); + err =ipmr_notifier_init (net);if (err) goto ipmr_notifier_fail;
Signature · 1 line
2.53.0.414.gf7e9f6c205-goog
PATCH 15/15 ipmr: Don't hold RTNL for ipmr_rtm_route().
In reply to Kuniyuki Iwashimaipmr_mfc_add() and ipmr_mfc_delete() are already protected by a dedicated mutex.
rtm_to_ipmr_mfcc() calls __ipmr_get_table(), __dev_get_by_index(), amd ipmr_find_vif().
Once __dev_get_by_index() is converted to dev_get_by_index_rcu(), we can move the other two functions under that same RCU section and drop RTNL for ipmr_rtm_route().
Let's do that conversion and drop ASSERT_RTNL() in mr_call_mfc_notifiers().
--- include/linux/mroute_base.h | 1 - net/ipv4/ipmr.c | 34 +++++++++++++++++++++------------- 2 files changed, 21 insertions(+), 14 deletions(-)
include/linux/mroute_base.h +0 -1
@@ -208,7 +208,6 @@ static inline int mr_call_mfc_notifiers(struct net *net, .tb_id = tb_id }; -ASSERT_RTNL ();atomic_inc (ipmr_seq);return call_fib_notifiers (net, event_type, &info.info); }
net/ipv4/ipmr.c +21 -13
@@ -1209,7 +1209,6 @@ static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)struct net *net =read_pnet (&mrt->net);struct mfc_cache *c; -/* The entries are added/deleted only under RTNL */ rcu_read_lock (); c =ipmr_cache_find_parent (mrt, mfc->mfcc_origin.s_addr, mfc->mfcc_mcastgrp.s_addr, parent);
@@ -1236,7 +1235,6 @@ static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,if (mfc->mfcc_parent >=MAXVIFS )return -ENFILE ; -/* The entries are added/deleted only under RTNL */ rcu_read_lock (); c =ipmr_cache_find_parent (mrt, mfc->mfcc_origin.s_addr, mfc->mfcc_mcastgrp.s_addr, parent);
@@ -2851,10 +2849,10 @@ static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh, {struct net_device *dev =NULL ;u32 tblid =RT_TABLE_DEFAULT ; +int ret, rem, iif =0 ;struct mr_table *mrt;struct nlattr *attr;struct rtmsg *rtm; -int ret, rem; ret =nlmsg_validate_deprecated (nlh,sizeof (*rtm),RTA_MAX , rtm_ipmr_policy, extack);
@@ -2881,11 +2879,7 @@ static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh, mfcc->mfcc_mcastgrp.s_addr =nla_get_be32 (attr);break ;case RTA_IIF : - dev =__dev_get_by_index (net,nla_get_u32 (attr)); -if (!dev) { - ret = -ENODEV ; - goto out; - } + iif =nla_get_u32 (attr);break ;case RTA_MULTIPATH :if (ipmr_nla_get_ttls (attr, mfcc) <0 ) {
@@ -2901,16 +2895,30 @@ static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,break ; } } + +rcu_read_lock (); + mrt =__ipmr_get_table (net, tblid);if (!mrt) { ret = -ENOENT ; - goto out; + goto unlock; } + +if (iif) { + dev =dev_get_by_index_rcu (net, iif); +if (!dev) { + ret = -ENODEV ; + goto unlock; + } + + mfcc->mfcc_parent =ipmr_find_vif (mrt, dev); + } + *mrtret = mrt; *mrtsock = rtm->rtm_protocol ==RTPROT_MROUTED ?1 :0 ; -if (dev) - mfcc->mfcc_parent =ipmr_find_vif (mrt, dev); +unlock: +rcu_read_unlock (); out:return ret; }
@@ -3341,9 +3349,9 @@ static const struct rtnl_msg_handler ipmr_rtnl_msg_handlers[] __initconst = { {.protocol = RTNL_FAMILY_IPMR, .msgtype = RTM_GETLINK, .dumpit = ipmr_rtm_dumplink, .flags = RTNL_FLAG_DUMP_UNLOCKED}, {.protocol = RTNL_FAMILY_IPMR, .msgtype = RTM_NEWROUTE, - .doit = ipmr_rtm_route}, + .doit = ipmr_rtm_route, .flags = RTNL_FLAG_DOIT_UNLOCKED}, {.protocol = RTNL_FAMILY_IPMR, .msgtype = RTM_DELROUTE, - .doit = ipmr_rtm_route}, + .doit = ipmr_rtm_route, .flags = RTNL_FLAG_DOIT_UNLOCKED}, {.protocol = RTNL_FAMILY_IPMR, .msgtype = RTM_GETROUTE, .doit = ipmr_rtm_getroute, .dumpit = ipmr_rtm_dumproute, .flags = RTNL_FLAG_DOIT_UNLOCKED | RTNL_FLAG_DUMP_UNLOCKED},
Signature · 1 line
2.53.0.414.gf7e9f6c205-goog
syzbot ci has tested the following series
syzbot ci has tested the following series
[v1] ipmr: No RTNL for RTNL_FAMILY_IPMR rtnetlink.
https://lore.kernel.org/all/[email protected]
* [PATCH v1 net-next 01/15] selftest: net: Add basic functionality tests for ipmr.
* [PATCH v1 net-next 02/15] ipmr: Annotate access to mrt->mroute_do_{pim,assert,wrvifwhole}.
* [PATCH v1 net-next 03/15] ipmr: Convert ipmr_rtm_dumplink() to RCU.
* [PATCH v1 net-next 04/15] ipmr: Use MAXVIFS in mroute_msgsize().
* [PATCH v1 net-next 05/15] ipmr: Convert ipmr_rtm_getroute() to RCU.
* [PATCH v1 net-next 06/15] ipmr: Convert ipmr_rtm_dumproute() to RCU.
* [PATCH v1 net-next 07/15] ipmr: Move unregister_netdevice_many() out of mroute_clean_tables().
* [PATCH v1 net-next 08/15] ipmr: Move unregister_netdevice_many() out of ipmr_free_table().
* [PATCH v1 net-next 09/15] ipmr: Convert ipmr_net_exit_batch() to ->exit_rtnl().
* [PATCH v1 net-next 10/15] ipmr: Remove RTNL in ipmr_rules_init() and ipmr_net_init().
* [PATCH v1 net-next 11/15] ipmr: Call fib_rules_unregister() without RTNL.
* [PATCH v1 net-next 12/15] ipmr: Define net->ipv4.{ipmr_notifier_ops,ipmr_seq} under CONFIG_IP_MROUTE.
* [PATCH v1 net-next 13/15] ipmr/ip6mr: Convert net->ipv[46].ipmr_seq to atomic_t.
* [PATCH v1 net-next 14/15] ipmr: Add dedicated mutex for mrt->{mfc_hash,mfc_cache_list}.
* [PATCH v1 net-next 15/15] ipmr: Don't hold RTNL for ipmr_rtm_route().
and found the following issue:
WARNING in ipmr_free_table
Full report is available here:
https://ci.syzbot.org/series/d7068eba-72b1-4110-aeb0-d4528e03fd24
***
WARNING in ipmr_free_table
tree: net-next URL: https://kernel.googlesource.com/pub/scm/linux/kernel/git/netdev/net-next.git base: c7f5c6fb0f2b1a44490a36582a251f0a304d6b0c arch: amd64 compiler: Debian clang version 21.1.8 (++20251221033036+2078da43e25a-1~exp1~20251221153213.50), Debian LLD 21.1.8 config: https://ci.syzbot.org/builds/0afa280b-14ff-4aec-8a20-7f8507025dd8/config C repro: https://ci.syzbot.org/findings/e38687c7-5a8b-4c68-909c-a0ffdd17a674/c_repro syz repro: https://ci.syzbot.org/findings/e38687c7-5a8b-4c68-909c-a0ffdd17a674/syz_repro
------------[ cut here ]------------ !net_initialized(net) && !list_empty(dev_kill_list) WARNING: net/ipv4/ipmr.c:450 at ipmr_free_table+0x150/0x180 net/ipv4/ipmr.c:450, CPU#1: syz.1.18/6004 Modules linked in: CPU: 1 UID: 0 PID: 6004 Comm: syz.1.18 Not tainted syzkaller #0 PREEMPT(full) Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.2-debian-1.16.2-1 04/01/2014 RIP: 0010:ipmr_free_table+0x150/0x180 net/ipv4/ipmr.c:450 Code: 23 e8 f4 5c 95 f7 5b 41 5c 41 5d 41 5e 41 5f 5d c3 cc cc cc cc cc e8 df 5c 95 f7 90 0f 0b 90 e9 64 ff ff ff e8 d1 5c 95 f7 90 <0f> 0b 90 eb d7 44 89 e1 80 e1 07 80 c1 03 38 c1 0f 8c 03 ff ff ff RSP: 0018:ffffc900037afa70 EFLAGS: 00010293 RAX: ffffffff8a302fbf RBX: ffffc900037afb60 RCX: ffff888172a70000 RDX: 0000000000000000 RSI: ffffffff8def033c RDI: ffffffff8c27a480 RBP: 0000000000000001 R08: ffffffff901166b7 R09: 1ffffffff2022cd6 R10: dffffc0000000000 R11: fffffbfff2022cd7 R12: ffff88816a392758 R13: dffffc0000000000 R14: ffff8881b8960000 R15: ffff88816a392758 FS: 00007f5a2d24e6c0(0000) GS:ffff8882a9469000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00007f7b7544e9d3 CR3: 0000000172316000 CR4: 00000000000006f0 Call Trace: <TASK> ipmr_rules_exit_rtnl net/ipv4/ipmr.c:297 [inline] ipmr_net_exit_rtnl+0x156/0x1a0 net/ipv4/ipmr.c:3339 ops_exit_rtnl_list net/core/net_namespace.c:181 [inline] ops_undo_list+0x347/0x940 net/core/net_namespace.c:248 setup_net+0x2f0/0x340 net/core/net_namespace.c:462 copy_net_ns+0x50e/0x730 net/core/net_namespace.c:581 create_new_namespaces+0x3e7/0x6a0 kernel/nsproxy.c:130 unshare_nsproxy_namespaces+0x11a/0x160 kernel/nsproxy.c:226 ksys_unshare+0x4f4/0x900 kernel/fork.c:3174 __do_sys_unshare kernel/fork.c:3245 [inline] __se_sys_unshare kernel/fork.c:3243 [inline] __x64_sys_unshare+0x38/0x50 kernel/fork.c:3243 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline] do_syscall_64+0x14d/0xf80 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7f RIP: 0033:0x7f5a2c39c629 Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 e8 ff ff ff f7 d8 64 89 01 48 RSP: 002b:00007f5a2d24e028 EFLAGS: 00000246 ORIG_RAX: 0000000000000110 RAX: ffffffffffffffda RBX: 00007f5a2c615fa0 RCX: 00007f5a2c39c629 RDX: 0000000000000000 RSI: 0000000000000000 RDI: 000000006a040000 RBP: 00007f5a2c432b39 R08: 0000000000000000 R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000 R13: 00007f5a2c616038 R14: 00007f5a2c615fa0 R15: 00007fffb0e41968 </TASK>
***
If these findings have caused you to resend the series or submit a separate fix, please add the following tag to your commit message:
---
This report is generated by a bot. It may contain errors. syzbot ci engineers can be reached at [email protected].
From: syzbot ci <[email protected]>
Date: Thu, 26 Feb 2026 09:00:11 -0800
Quoted 39 lines
syzbot ci has tested the following series
[v1] ipmr: No RTNL for
RTNL_FAMILY_IPMRrtnetlink.
https://lore.kernel.org/all/[email protected]
* [PATCH v1 net-next 01/15] selftest: net: Add basic functionality tests for ipmr.
* [PATCH v1 net-next 02/15] ipmr: Annotate access to mrt->mroute_do_{pim,assert,wrvifwhole}.
* [PATCH v1 net-next 03/15] ipmr: Convertipmr_rtm_dumplink()to RCU.
* [PATCH v1 net-next 04/15] ipmr: Use MAXVIFS inmroute_msgsize().
* [PATCH v1 net-next 05/15] ipmr: Convertipmr_rtm_getroute()to RCU.
* [PATCH v1 net-next 06/15] ipmr: Convertipmr_rtm_dumproute()to RCU.
* [PATCH v1 net-next 07/15] ipmr: Moveunregister_netdevice_many()out ofmroute_clean_tables().
* [PATCH v1 net-next 08/15] ipmr: Moveunregister_netdevice_many()out ofipmr_free_table().
* [PATCH v1 net-next 09/15] ipmr: Convertipmr_net_exit_batch()to ->exit_rtnl().
* [PATCH v1 net-next 10/15] ipmr: Remove RTNL inipmr_rules_init()andipmr_net_init().
* [PATCH v1 net-next 11/15] ipmr: Callfib_rules_unregister()without RTNL.
* [PATCH v1 net-next 12/15] ipmr: Define net->ipv4.{ipmr_notifier_ops,ipmr_seq} underCONFIG_IP_MROUTE.
* [PATCH v1 net-next 13/15] ipmr/ip6mr: Convert net->ipv[46].ipmr_seq toatomic_t.
* [PATCH v1 net-next 14/15] ipmr: Add dedicated mutex for mrt->{mfc_hash,mfc_cache_list}.
* [PATCH v1 net-next 15/15] ipmr: Don't hold RTNL foripmr_rtm_route().and found the following issue:
WARNING inipmr_free_tableFull report is available here:
https://ci.syzbot.org/series/d7068eba-72b1-4110-aeb0-d4528e03fd24***
WARNING in
ipmr_free_tabletree: net-next URL: https://kernel.googlesource.com/pub/scm/linux/kernel/git/netdev/net-next.git base: c7f5c6fb0f2b1a44490a36582a251f0a304d6b0c arch: amd64 compiler: Debian clang version 21.1.8 (++20251221033036+2078da43e25a-1~exp1~20251221153213.50), Debian LLD 21.1.8 config: https://ci.syzbot.org/builds/0afa280b-14ff-4aec-8a20-7f8507025dd8/config C repro: https://ci.syzbot.org/findings/e38687c7-5a8b-4c68-909c-a0ffdd17a674/c_repro syz repro: https://ci.syzbot.org/findings/e38687c7-5a8b-4c68-909c-a0ffdd17a674/syz_repro
Quoted 3 lines
------------[ cut here ]------------ !net_initialized(net) && !list_empty(dev_kill_list) WARNING: net/ipv4/ipmr.c:450 at ipmr_free_table+0x150/0x180 net/ipv4/ipmr.c:450, CPU#1: syz.1.18/6004
I forgot dev_kill_list could have some devices collected from _other_ ->rtnl_exit() _after_ successful ipmr init.
My assumption in patch 10 is still correct, and I'll squash this in v2.
net/ipv4/ipmr.c +4 -2
@@ -437,17 +437,19 @@ static struct mr_table *ipmr_new_table(struct net *net, u32 id)static void ipmr_free_table (struct mr_table *mrt,struct list_head *dev_kill_list) {struct net *net =read_pnet (&mrt->net); +LIST_HEAD (ipmr_dev_kill_list);WARN_ON_ONCE (!mr_can_free_table (net));timer_shutdown_sync (&mrt->ipmr_expire_timer);mroute_clean_tables (mrt,MRT_FLUSH_VIFS |MRT_FLUSH_VIFS_STATIC |MRT_FLUSH_MFC |MRT_FLUSH_MFC_STATIC , - dev_kill_list); + &ipmr_dev_kill_list);rhltable_destroy (&mrt->mfc_hash);kfree (mrt); -WARN_ON_ONCE (!net_initialized (net) && !list_empty (dev_kill_list)); +WARN_ON_ONCE (!net_initialized (net) && !list_empty (&ipmr_dev_kill_list)); +list_splice (&ipmr_dev_kill_list, dev_kill_list); }/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */