mpls: Remove RTNL dependency.
Subsystems (3)
v2 cover mpls: Remove RTNL dependency.
MPLS uses RTNL
1) to guarantee the lifetime of struct mpls_nh.nh_dev
2) to protect net->mpls.platform_label
, but neither actually requires RTNL.
If struct mpls_nh holds a refcnt for nh_dev, we do not need RTNL, and it can be replaced with a dedicated mutex.
The series removes RTNL from net/mpls/.
Overview:
Patch 1 is misc cleanup.
Patch 2 - 9 are prep to drop RTNL for RTM_{NEW,DEL,GET}ROUTE handlers.
Patch 10 & 11 converts mpls_dump_routes() and RTM_GETNETCONF to RCU.
Patch 12 replaces RTNL with a new per-netns mutex.
Patch 13 drops RTNL from RTM_{NEW,DEL,GET}ROUTE.
Changes:
v2:
* Patch 10 : Removed dup entry of RTM_GETROUTE in mpls_rtnl_msg_handlers[] (Guillaume Nault)
v1: https://lore.kernel.org/netdev/[email protected]/
Kuniyuki Iwashima (13):
mpls: Return early in mpls_label_ok().
mpls: Hold dev refcnt for mpls_nh.
mpls: Unify return paths in mpls_dev_notify().
ipv6: Add in6_dev_rcu().
mpls: Use in6_dev_rcu() and dev_net_rcu() in mpls_forward() and
mpls_xmit().
mpls: Add mpls_dev_rcu().
mpls: Pass net to mpls_dev_get().
mpls: Add mpls_route_input().
mpls: Use mpls_route_input() where appropriate.
mpls: Convert mpls_dump_routes() to RCU.
mpls: Convert RTM_GETNETCONF to RCU.
mpls: Protect net->mpls.platform_label with a per-netns mutex.
mpls: Drop RTNL for RTM_NEWROUTE, RTM_DELROUTE, and RTM_GETROUTE.
include/net/addrconf.h | 5 + include/net/netns/mpls.h | 1 + net/mpls/af_mpls.c | 321 ++++++++++++++++++++++++--------------- net/mpls/internal.h | 19 ++- net/mpls/mpls_iptunnel.c | 6 +- 5 files changed, 224 insertions(+), 128 deletions(-)
Signature · 1 line
2.51.1.851.g4ebd6896fd-goog
PATCH v2 1/13 mpls: Return early in mpls_label_ok().
When mpls_label_ok() returns false, it does not need to update *index.
Let's remove is_ok and return early.
--- net/mpls/af_mpls.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
net/mpls/af_mpls.c +5 -6
@@ -940,24 +940,23 @@ static int mpls_nh_build_multi(struct mpls_route_config *cfg,static bool mpls_label_ok (struct net *net,unsigned int *index,struct netlink_ext_ack *extack) { -bool is_ok = true; -/* Reserved labels may not be set */ if (*index <MPLS_LABEL_FIRST_UNRESERVED ) {NL_SET_ERR_MSG (extack,"Invalid label - must be MPLS_LABEL_FIRST_UNRESERVED or higher" ); - is_ok = false; +return false; }/* The full 20 bit range may not be supported. */ -if (is_ok && *index >= net->mpls.platform_labels) { +if (*index >= net->mpls.platform_labels) {NL_SET_ERR_MSG (extack,"Label >= configured maximum in platform_labels" ); - is_ok = false; +return false; } *index =array_index_nospec (*index, net->mpls.platform_labels); -return is_ok; + +return true; } static int mpls_route_add(struct mpls_route_config *cfg,
Signature · 1 line
2.51.1.851.g4ebd6896fd-goog
PATCH v2 2/13 mpls: Hold dev refcnt for mpls_nh.
In reply to Kuniyuki IwashimaMPLS uses RTNL
1) to guarantee the lifetime of struct mpls_nh.nh_dev
2) to protect net->mpls.platform_label
, but neither actually requires RTNL.
If we do not call dev_put() in find_outdev() and call it just before freeing struct mpls_route, we can drop RTNL for 1).
Let's hold the refcnt of mpls_nh.nh_dev and track it with netdevice_tracker.
Two notable changes:
If mpls_nh_build_multi() fails to set up a neighbour, we need to call netdev_put() for successfully created neighbours in mpls_rt_free_rcu(), so the number of neighbours (rt->rt_nhn) is now updated in each iteration.
When a dev is unregistered, mpls_ifdown() clones mpls_route and replaces it with the clone, so the clone requires extra netdev_hold().
--- net/mpls/af_mpls.c | 63 +++++++++++++++++++++++++++++++-------------- net/mpls/internal.h | 1 + 2 files changed, 45 insertions(+), 19 deletions(-)
net/mpls/af_mpls.c +44 -19
@@ -530,10 +530,23 @@ static struct mpls_route *mpls_rt_alloc(u8 num_nh, u8 max_alen, u8 max_labels)return rt; } +static void mpls_rt_free_rcu (struct rcu_head *head) +{ +struct mpls_route *rt; + + rt = container_of(head, struct mpls_route, rt_rcu); + +change_nexthops (rt) { +netdev_put (nh->nh_dev, &nh->nh_dev_tracker); + }endfor_nexthops (rt); + +kfree (rt); +} +static void mpls_rt_free (struct mpls_route *rt) {if (rt) -kfree_rcu (rt, rt_rcu); +call_rcu (&rt->rt_rcu, mpls_rt_free_rcu); } static void mpls_notify_route(struct net *net, unsigned index,
@@ -587,6 +600,7 @@ static unsigned find_free_label(struct net *net)#if IS_ENABLED (CONFIG_INET )static struct net_device *inet_fib_lookup_dev (struct net *net, +struct mpls_nh *nh,const void *addr) {struct net_device *dev;
@@ -599,14 +613,14 @@ static struct net_device *inet_fib_lookup_dev(struct net *net,return ERR_CAST (rt); dev = rt->dst.dev; -dev_hold (dev); - +netdev_hold (dev, &nh->nh_dev_tracker,GFP_KERNEL );ip_rt_put (rt); return dev; }#else static struct net_device *inet_fib_lookup_dev (struct net *net, +struct mpls_nh *nh,const void *addr) {return ERR_PTR (-EAFNOSUPPORT );
@@ -615,6 +629,7 @@ static struct net_device *inet_fib_lookup_dev(struct net *net,#if IS_ENABLED (CONFIG_IPV6 )static struct net_device *inet6_fib_lookup_dev (struct net *net, +struct mpls_nh *nh,const void *addr) {struct net_device *dev;
@@ -631,13 +646,14 @@ static struct net_device *inet6_fib_lookup_dev(struct net *net,return ERR_CAST (dst); dev = dst->dev; -dev_hold (dev); +netdev_hold (dev, &nh->nh_dev_tracker,GFP_KERNEL );dst_release (dst); return dev; }#else static struct net_device *inet6_fib_lookup_dev (struct net *net, +struct mpls_nh *nh,const void *addr) {return ERR_PTR (-EAFNOSUPPORT );
@@ -653,16 +669,17 @@ static struct net_device *find_outdev(struct net *net,if (!oif) {switch (nh->nh_via_table) {case NEIGH_ARP_TABLE : - dev =inet_fib_lookup_dev (net,mpls_nh_via (rt, nh)); + dev =inet_fib_lookup_dev (net, nh,mpls_nh_via (rt, nh));break ;case NEIGH_ND_TABLE : - dev =inet6_fib_lookup_dev (net,mpls_nh_via (rt, nh)); + dev =inet6_fib_lookup_dev (net, nh,mpls_nh_via (rt, nh));break ;case NEIGH_LINK_TABLE :break ; } }else { - dev =dev_get_by_index (net, oif); + dev =netdev_get_by_index (net, oif, + &nh->nh_dev_tracker,GFP_KERNEL ); }if (!dev)
@@ -671,8 +688,7 @@ static struct net_device *find_outdev(struct net *net,if (IS_ERR (dev))return dev; -/* The caller is holding rtnl anyways, so release the dev reference */ -dev_put (dev); + nh->nh_dev = dev;return dev; }
@@ -686,20 +702,17 @@ static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt, dev =find_outdev (net, rt, nh, oif);if (IS_ERR (dev)) { err =PTR_ERR (dev); - dev =NULL ; goto errout; }/* Ensure this is a supported device */ err = -EINVAL ;if (!mpls_dev_get (dev)) - goto errout; + goto errout_put;if ((nh->nh_via_table ==NEIGH_LINK_TABLE ) && (dev->addr_len != nh->nh_via_alen)) - goto errout; - - nh->nh_dev = dev; + goto errout_put;if (!(dev->flags &IFF_UP )) { nh->nh_flags |=RTNH_F_DEAD ;
@@ -713,6 +726,9 @@ static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,return 0 ; +errout_put: +netdev_put (nh->nh_dev, &nh->nh_dev_tracker); + nh->nh_dev =NULL ; errout:return err; }
@@ -890,7 +906,8 @@ static int mpls_nh_build_multi(struct mpls_route_config *cfg,struct nlattr *nla_via, *nla_newdst;int remaining = cfg->rc_mp_len;int err =0 ; -u8 nhs =0 ; + + rt->rt_nhn =0 ;change_nexthops (rt) {int attrlen;
@@ -926,11 +943,9 @@ static int mpls_nh_build_multi(struct mpls_route_config *cfg, rt->rt_nhn_alive--; rtnh =rtnh_next (rtnh, &remaining); - nhs++; + rt->rt_nhn++; }endfor_nexthops (rt); - rt->rt_nhn = nhs; -return 0 ; errout:
@@ -1523,8 +1538,12 @@ static int mpls_ifdown(struct net_device *dev, int event)change_nexthops (rt) {unsigned int nh_flags = nh->nh_flags; -if (nh->nh_dev != dev) +if (nh->nh_dev != dev) { +if (nh_del) +netdev_hold (nh->nh_dev, &nh->nh_dev_tracker, +GFP_KERNEL ); goto next; + }switch (event) {case NETDEV_DOWN :
@@ -2518,10 +2537,13 @@ static int resize_platform_label_table(struct net *net, size_t limit)/* In case the predefined labels need to be populated */ if (limit >MPLS_LABEL_IPV4NULL ) {struct net_device *lo = net->loopback_dev; + rt0 =mpls_rt_alloc (1 , lo->addr_len,0 );if (IS_ERR (rt0)) goto nort0; + rt0->rt_nh->nh_dev = lo; +netdev_hold (lo, &rt0->rt_nh->nh_dev_tracker,GFP_KERNEL ); rt0->rt_protocol =RTPROT_KERNEL ; rt0->rt_payload_type =MPT_IPV4 ; rt0->rt_ttl_propagate =MPLS_TTL_PROP_DEFAULT ;
@@ -2532,10 +2554,13 @@ static int resize_platform_label_table(struct net *net, size_t limit) }if (limit >MPLS_LABEL_IPV6NULL ) {struct net_device *lo = net->loopback_dev; + rt2 =mpls_rt_alloc (1 , lo->addr_len,0 );if (IS_ERR (rt2)) goto nort2; + rt2->rt_nh->nh_dev = lo; +netdev_hold (lo, &rt2->rt_nh->nh_dev_tracker,GFP_KERNEL ); rt2->rt_protocol =RTPROT_KERNEL ; rt2->rt_payload_type =MPT_IPV6 ; rt2->rt_ttl_propagate =MPLS_TTL_PROP_DEFAULT ;
net/mpls/internal.h +1 -0
@@ -88,6 +88,7 @@ enum mpls_payload_type {struct mpls_nh {/* next hop label forwarding entry */ struct net_device *nh_dev; +netdevice_tracker nh_dev_tracker;/* nh_flags is accessed under RCU in the packet path; it is * modified handling netdev events with rtnl lock held
Signature · 1 line
2.51.1.851.g4ebd6896fd-goog
PATCH v2 3/13 mpls: Unify return paths in mpls_dev_notify().
In reply to Kuniyuki IwashimaWe will protect net->mpls.platform_label by a dedicated mutex.
Then, we need to wrap functions called from mpls_dev_notify() with the mutex.
As a prep, let's unify the return paths.
--- net/mpls/af_mpls.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-)
net/mpls/af_mpls.c +16 -8
@@ -1616,22 +1616,24 @@ static int mpls_dev_notify(struct notifier_block *this, unsigned long event,if (event ==NETDEV_REGISTER ) { mdev =mpls_add_dev (dev); -if (IS_ERR (mdev)) -return notifier_from_errno (PTR_ERR (mdev)); +if (IS_ERR (mdev)) { + err =PTR_ERR (mdev); + goto err; + } -return NOTIFY_OK ; + goto out; } mdev =mpls_dev_get (dev);if (!mdev) -return NOTIFY_OK ; + goto out;switch (event) {case NETDEV_DOWN : err =mpls_ifdown (dev, event);if (err) -return notifier_from_errno (err); + goto err;break ;case NETDEV_UP : flags =netif_get_flags (dev);
@@ -1647,13 +1649,14 @@ static int mpls_dev_notify(struct notifier_block *this, unsigned long event, }else { err =mpls_ifdown (dev, event);if (err) -return notifier_from_errno (err); + goto err; }break ;case NETDEV_UNREGISTER : err =mpls_ifdown (dev, event);if (err) -return notifier_from_errno (err); + goto err; + mdev =mpls_dev_get (dev);if (mdev) {mpls_dev_sysctl_unregister (dev, mdev);
@@ -1667,11 +1670,16 @@ static int mpls_dev_notify(struct notifier_block *this, unsigned long event,mpls_dev_sysctl_unregister (dev, mdev); err =mpls_dev_sysctl_register (dev, mdev);if (err) -return notifier_from_errno (err); + goto err; }break ; } + +out:return NOTIFY_OK ; + +err: +return notifier_from_errno (err); }static struct notifier_block mpls_dev_notifier = {
Signature · 1 line
2.51.1.851.g4ebd6896fd-goog
PATCH v2 4/13 ipv6: Add in6_dev_rcu().
In reply to Kuniyuki Iwashimarcu_dereference_rtnl() does not clearly tell whether the caller is under RCU or RTNL.
Let's add in6_dev_rcu() to make it easy to remove __in6_dev_get() in the future.
--- include/net/addrconf.h | 5 +++++ 1 file changed, 5 insertions(+)
include/net/addrconf.h +5 -0
@@ -347,6 +347,11 @@ static inline struct inet6_dev *__in6_dev_get(const struct net_device *dev)return rcu_dereference_rtnl (dev->ip6_ptr); } +static inline struct inet6_dev *in6_dev_rcu (const struct net_device *dev) +{ +return rcu_dereference (dev->ip6_ptr); +} +static inline struct inet6_dev *__in6_dev_get_rtnl_net (const struct net_device *dev) {return rtnl_net_dereference (dev_net (dev), dev->ip6_ptr);
Signature · 1 line
2.51.1.851.g4ebd6896fd-goog
PATCH v2 5/13 mpls: Use in6_dev_rcu() and dev_net_rcu() in mpls_forward() and mpls_xmit().
In reply to Kuniyuki Iwashimampls_forward() and mpls_xmit() are called under RCU.
Let's use in6_dev_rcu() and dev_net_rcu() there to annotate as such.
Now we pass net to mpls_stats_inc_outucastpkts() not to read dev_net_rcu() twice.
--- net/mpls/af_mpls.c | 15 ++++++++------- net/mpls/internal.h | 3 ++- net/mpls/mpls_iptunnel.c | 4 ++-- 3 files changed, 12 insertions(+), 10 deletions(-)
net/mpls/af_mpls.c +8 -7
@@ -129,7 +129,8 @@ bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu) }EXPORT_SYMBOL_GPL (mpls_pkt_too_big); -void mpls_stats_inc_outucastpkts (struct net_device *dev, +void mpls_stats_inc_outucastpkts (struct net *net, +struct net_device *dev,const struct sk_buff *skb) {struct mpls_dev *mdev;
@@ -141,13 +142,13 @@ void mpls_stats_inc_outucastpkts(struct net_device *dev, tx_packets, tx_bytes); }else if (skb->protocol ==htons (ETH_P_IP )) { -IP_UPD_PO_STATS (dev_net (dev),IPSTATS_MIB_OUT , skb->len); +IP_UPD_PO_STATS (net,IPSTATS_MIB_OUT , skb->len);#if IS_ENABLED (CONFIG_IPV6 ) } else if (skb->protocol == htons(ETH_P_IPV6)) { -struct inet6_dev *in6dev =__in6_dev_get (dev); +struct inet6_dev *in6dev =in6_dev_rcu (dev);if (in6dev) -IP6_UPD_PO_STATS (dev_net (dev), in6dev, +IP6_UPD_PO_STATS (net, in6dev,IPSTATS_MIB_OUT , skb->len);#endif }
@@ -342,7 +343,7 @@ static bool mpls_egress(struct net *net, struct mpls_route *rt,static int mpls_forward (struct sk_buff *skb,struct net_device *dev,struct packet_type *pt,struct net_device *orig_dev) { -struct net *net =dev_net (dev); +struct net *net =dev_net_rcu (dev);struct mpls_shim_hdr *hdr;const struct mpls_nh *nh;struct mpls_route *rt;
@@ -434,7 +435,7 @@ static int mpls_forward(struct sk_buff *skb, struct net_device *dev, dec.ttl -=1 ;if (unlikely (!new_header_size && dec.bos)) {/* Penultimate hop popping */ -if (!mpls_egress (dev_net (out_dev), rt, skb, dec)) +if (!mpls_egress (net, rt, skb, dec)) goto err; }else {bool bos;
@@ -451,7 +452,7 @@ static int mpls_forward(struct sk_buff *skb, struct net_device *dev, } } -mpls_stats_inc_outucastpkts (out_dev, skb); +mpls_stats_inc_outucastpkts (net, out_dev, skb);/* If via wasn't specified then send out using device address */ if (nh->nh_via_table ==MPLS_NEIGH_TABLE_UNSPEC )
net/mpls/internal.h +2 -1
@@ -197,7 +197,8 @@ int nla_get_labels(const struct nlattr *nla, u8 max_labels, u8 *labels,bool mpls_output_possible (const struct net_device *dev);unsigned int mpls_dev_mtu (const struct net_device *dev);bool mpls_pkt_too_big (const struct sk_buff *skb,unsigned int mtu); -void mpls_stats_inc_outucastpkts (struct net_device *dev, +void mpls_stats_inc_outucastpkts (struct net *net, +struct net_device *dev,const struct sk_buff *skb);#endif /* MPLS_INTERNAL_H */
net/mpls/mpls_iptunnel.c +2 -2
@@ -53,7 +53,7 @@ static int mpls_xmit(struct sk_buff *skb)/* Find the output device */ out_dev = dst->dev; - net =dev_net (out_dev); + net =dev_net_rcu (out_dev);if (!mpls_output_possible (out_dev) || !dst->lwtstate ||skb_warn_if_lro (skb))
@@ -128,7 +128,7 @@ static int mpls_xmit(struct sk_buff *skb) bos = false; } -mpls_stats_inc_outucastpkts (out_dev, skb); +mpls_stats_inc_outucastpkts (net, out_dev, skb);if (rt) {if (rt->rt_gw_family ==AF_INET6 )
Signature · 1 line
2.51.1.851.g4ebd6896fd-goog
PATCH v2 6/13 mpls: Add mpls_dev_rcu().
In reply to Kuniyuki Iwashimampls_dev_get() uses rcu_dereference_rtnl() to fetch dev->mpls_ptr.
We will replace RTNL with a dedicated mutex to protect the field.
Then, we will use rcu_dereference_protected() for clarity.
Let's add mpls_dev_rcu() for the RCU reader.
--- net/mpls/af_mpls.c | 12 ++++++------ net/mpls/internal.h | 5 +++++ net/mpls/mpls_iptunnel.c | 2 +- 3 files changed, 12 insertions(+), 7 deletions(-)
net/mpls/af_mpls.c +6 -6
@@ -136,7 +136,7 @@ void mpls_stats_inc_outucastpkts(struct net *net,struct mpls_dev *mdev;if (skb->protocol ==htons (ETH_P_MPLS_UC )) { - mdev =mpls_dev_get (dev); + mdev =mpls_dev_rcu (dev);if (mdev) MPLS_INC_STATS_LEN(mdev, skb->len, tx_packets,
@@ -358,7 +358,7 @@ static int mpls_forward(struct sk_buff *skb, struct net_device *dev,/* Careful this entire function runs inside of an rcu critical section */ - mdev =mpls_dev_get (dev); + mdev =mpls_dev_rcu (dev);if (!mdev) goto drop;
@@ -467,7 +467,7 @@ static int mpls_forward(struct sk_buff *skb, struct net_device *dev,return 0 ; tx_err: - out_mdev = out_dev ?mpls_dev_get (out_dev) :NULL ; + out_mdev = out_dev ?mpls_dev_rcu (out_dev) :NULL ;if (out_mdev)MPLS_INC_STATS (out_mdev, tx_errors); goto drop;
@@ -1118,7 +1118,7 @@ static int mpls_fill_stats_af(struct sk_buff *skb,struct mpls_dev *mdev;struct nlattr *nla; - mdev =mpls_dev_get (dev); + mdev =mpls_dev_rcu (dev);if (!mdev)return -ENODATA ;
@@ -1138,7 +1138,7 @@ static size_t mpls_get_stats_af_size(const struct net_device *dev) {struct mpls_dev *mdev; - mdev =mpls_dev_get (dev); + mdev =mpls_dev_rcu (dev);if (!mdev)return 0 ;
@@ -1341,7 +1341,7 @@ static int mpls_netconf_dump_devconf(struct sk_buff *skb,rcu_read_lock ();for_each_netdev_dump (net, dev, ctx->ifindex) { - mdev =mpls_dev_get (dev); + mdev =mpls_dev_rcu (dev);if (!mdev)continue ; err = mpls_netconf_fill_devconf(skb, mdev,
net/mpls/internal.h +5 -0
@@ -185,6 +185,11 @@ static inline struct mpls_entry_decoded mpls_entry_decode(struct mpls_shim_hdr *return result; } +static inline struct mpls_dev *mpls_dev_rcu (const struct net_device *dev) +{ +return rcu_dereference (dev->mpls_ptr); +} +static inline struct mpls_dev *mpls_dev_get (const struct net_device *dev) {return rcu_dereference_rtnl (dev->mpls_ptr);
net/mpls/mpls_iptunnel.c +1 -1
@@ -153,7 +153,7 @@ static int mpls_xmit(struct sk_buff *skb)return LWTUNNEL_XMIT_DONE ; drop: - out_mdev = out_dev ?mpls_dev_get (out_dev) :NULL ; + out_mdev = out_dev ?mpls_dev_rcu (out_dev) :NULL ;if (out_mdev)MPLS_INC_STATS (out_mdev, tx_errors);kfree_skb (skb);
Signature · 1 line
2.51.1.851.g4ebd6896fd-goog
PATCH v2 7/13 mpls: Pass net to mpls_dev_get().
In reply to Kuniyuki IwashimaWe will replace RTNL with a per-netns mutex to protect dev->mpls_ptr.
Then, we will use rcu_dereference_protected() with the lockdep_is_held() annotation, which requires net to access the per-netns mutex.
However, dev_net(dev) is not safe without RTNL.
Let's pass net to mpls_dev_get().
--- net/mpls/af_mpls.c | 11 ++++++----- net/mpls/internal.h | 3 ++- 2 files changed, 8 insertions(+), 6 deletions(-)
net/mpls/af_mpls.c +6 -5
@@ -708,7 +708,7 @@ static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,/* Ensure this is a supported device */ err = -EINVAL ; -if (!mpls_dev_get (dev)) +if (!mpls_dev_get (net, dev)) goto errout_put; if ((nh->nh_via_table == NEIGH_LINK_TABLE) &&
@@ -1288,7 +1288,7 @@ static int mpls_netconf_get_devconf(struct sk_buff *in_skb,if (!dev) goto errout; - mdev =mpls_dev_get (dev); + mdev =mpls_dev_get (net, dev);if (!mdev) goto errout;
@@ -1611,6 +1611,7 @@ static int mpls_dev_notify(struct notifier_block *this, unsigned long event, void *ptr) {struct net_device *dev =netdev_notifier_info_to_dev (ptr); +struct net *net =dev_net (dev);struct mpls_dev *mdev;unsigned int flags;int err;
@@ -1625,7 +1626,7 @@ static int mpls_dev_notify(struct notifier_block *this, unsigned long event, goto out; } - mdev =mpls_dev_get (dev); + mdev =mpls_dev_get (net, dev);if (!mdev) goto out;
@@ -1658,7 +1659,7 @@ static int mpls_dev_notify(struct notifier_block *this, unsigned long event,if (err) goto err; - mdev =mpls_dev_get (dev); + mdev =mpls_dev_get (net, dev);if (mdev) {mpls_dev_sysctl_unregister (dev, mdev);RCU_INIT_POINTER (dev->mpls_ptr,NULL );
@@ -1666,7 +1667,7 @@ static int mpls_dev_notify(struct notifier_block *this, unsigned long event, }break ;case NETDEV_CHANGENAME : - mdev =mpls_dev_get (dev); + mdev =mpls_dev_get (net, dev);if (mdev) {mpls_dev_sysctl_unregister (dev, mdev); err =mpls_dev_sysctl_register (dev, mdev);
net/mpls/internal.h +2 -1
@@ -190,7 +190,8 @@ static inline struct mpls_dev *mpls_dev_rcu(const struct net_device *dev)return rcu_dereference (dev->mpls_ptr); } -static inline struct mpls_dev *mpls_dev_get (const struct net_device *dev) +static inline struct mpls_dev *mpls_dev_get (const struct net *net, +const struct net_device *dev) {return rcu_dereference_rtnl (dev->mpls_ptr); }
Signature · 1 line
2.51.1.851.g4ebd6896fd-goog
PATCH v2 8/13 mpls: Add mpls_route_input().
In reply to Kuniyuki Iwashimampls_route_input_rcu() is called from mpls_forward() and mpls_getroute().
The former is under RCU, and the latter is under RTNL, so mpls_route_input_rcu() uses rcu_dereference_rtnl().
Let's use rcu_dereference() in mpls_route_input_rcu() and add an RTNL variant for mpls_getroute().
Later, we will remove rtnl_dereference() there.
--- net/mpls/af_mpls.c | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-)
net/mpls/af_mpls.c +18 -10
@@ -75,16 +75,23 @@ static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt, struct nlmsghdr *nlh, struct net *net, u32 portid, unsigned int nlm_flags); -static struct mpls_route *mpls_route_input_rcu (struct net *net,unsigned index) +static struct mpls_route *mpls_route_input (struct net *net,unsigned int index) { -struct mpls_route *rt =NULL ; +struct mpls_route __rcu **platform_label; -if (index < net->mpls.platform_labels) { -struct mpls_route __rcu **platform_label = -rcu_dereference_rtnl (net->mpls.platform_label); - rt =rcu_dereference_rtnl (platform_label[index]); - } -return rt; + platform_label =rtnl_dereference (net->mpls.platform_label); +return rtnl_dereference (platform_label[index]); +} + +static struct mpls_route *mpls_route_input_rcu (struct net *net,unsigned int index) +{ +struct mpls_route __rcu **platform_label; + +if (index >= net->mpls.platform_labels) +return NULL ; + + platform_label =rcu_dereference (net->mpls.platform_label); +return rcu_dereference (platform_label[index]); }bool mpls_output_possible (const struct net_device *dev)
@@ -2373,12 +2380,12 @@ static int mpls_getroute(struct sk_buff *in_skb, struct nlmsghdr *in_nlh,u32 portid =NETLINK_CB (in_skb).portid;u32 in_label =LABEL_NOT_SPECIFIED ;struct nlattr *tb[RTA_MAX +1 ]; +struct mpls_route *rt =NULL ;u32 labels[MAX_NEW_LABELS ];struct mpls_shim_hdr *hdr;unsigned int hdr_size =0 ;const struct mpls_nh *nh;struct net_device *dev; -struct mpls_route *rt;struct rtmsg *rtm, *r;struct nlmsghdr *nlh;struct sk_buff *skb;
@@ -2406,7 +2413,8 @@ static int mpls_getroute(struct sk_buff *in_skb, struct nlmsghdr *in_nlh, } } - rt =mpls_route_input_rcu (net, in_label); +if (in_label < net->mpls.platform_labels) + rt =mpls_route_input (net, in_label);if (!rt) { err = -ENETUNREACH ; goto errout;
Signature · 1 line
2.51.1.851.g4ebd6896fd-goog
PATCH v2 9/13 mpls: Use mpls_route_input() where appropriate.
In reply to Kuniyuki IwashimaIn many places, we uses rtnl_dereference() twice for net->mpls.platform_label and net->mpls.platform_label[index].
Let's replace the code with mpls_route_input().
We do not use mpls_route_input() in mpls_dump_routes() since we will rely on RCU there.
--- net/mpls/af_mpls.c | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-)
net/mpls/af_mpls.c +13 -19
@@ -590,19 +590,17 @@ static void mpls_route_update(struct net *net, unsigned index,mpls_rt_free (rt); } -static unsigned find_free_label (struct net *net) +static unsigned int find_free_label (struct net *net) { -struct mpls_route __rcu **platform_label; -size_t platform_labels; -unsigned index; +unsigned int index; - platform_label =rtnl_dereference (net->mpls.platform_label); - platform_labels = net->mpls.platform_labels; -for (index =MPLS_LABEL_FIRST_UNRESERVED ; index < platform_labels; +for (index =MPLS_LABEL_FIRST_UNRESERVED ; + index < net->mpls.platform_labels; index++) { -if (!rtnl_dereference (platform_label[index])) +if (!mpls_route_input (net, index))return index; } +return LABEL_NOT_SPECIFIED ; }
@@ -985,7 +983,6 @@ static bool mpls_label_ok(struct net *net, unsigned int *index,static int mpls_route_add (struct mpls_route_config *cfg,struct netlink_ext_ack *extack) { -struct mpls_route __rcu **platform_label;struct net *net = cfg->rc_nlinfo.nl_net;struct mpls_route *rt, *old;int err = -EINVAL ;
@@ -1013,8 +1010,7 @@ static int mpls_route_add(struct mpls_route_config *cfg, } err = -EEXIST ; - platform_label =rtnl_dereference (net->mpls.platform_label); - old =rtnl_dereference (platform_label[index]); + old =mpls_route_input (net, index);if ((cfg->rc_nlflags &NLM_F_EXCL ) && old) goto errout;
@@ -1503,16 +1499,15 @@ static void mpls_dev_destroy_rcu(struct rcu_head *head)static int mpls_ifdown (struct net_device *dev,int event) { -struct mpls_route __rcu **platform_label;struct net *net =dev_net (dev); -unsigned index; +unsigned int index; - platform_label =rtnl_dereference (net->mpls.platform_label);for (index =0 ; index < net->mpls.platform_labels; index++) { -struct mpls_route *rt =rtnl_dereference (platform_label[index]); +struct mpls_route *rt;bool nh_del = false;u8 alive =0 ; + rt =mpls_route_input (net, index);if (!rt)continue ;
@@ -1583,15 +1578,14 @@ static int mpls_ifdown(struct net_device *dev, int event)static void mpls_ifup (struct net_device *dev,unsigned int flags) { -struct mpls_route __rcu **platform_label;struct net *net =dev_net (dev); -unsigned index; +unsigned int index;u8 alive; - platform_label =rtnl_dereference (net->mpls.platform_label);for (index =0 ; index < net->mpls.platform_labels; index++) { -struct mpls_route *rt =rtnl_dereference (platform_label[index]); +struct mpls_route *rt; + rt =mpls_route_input (net, index);if (!rt)continue ;
Signature · 1 line
2.51.1.851.g4ebd6896fd-goog
PATCH v2 10/13 mpls: Convert mpls_dump_routes() to RCU.
In reply to Kuniyuki Iwashimampls_dump_routes() sets fib_dump_filter.rtnl_held to true and calls __dev_get_by_index() in mpls_valid_fib_dump_req().
This is the only RTNL dependant in mpls_dump_routes().
Also, synchronize_rcu() in resize_platform_label_table() guarantees that net->mpls.platform_label is alive under RCU.
Let's convert mpls_dump_routes() to RCU and use dev_get_by_index_rcu().
---
v2: Removed dup entry of RTM_GETROUTE in mpls_rtnl_msg_handlers[]
--- net/mpls/af_mpls.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-)
net/mpls/af_mpls.c +16 -10
@@ -2153,7 +2153,7 @@ static int mpls_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,if (i ==RTA_OIF ) { ifindex =nla_get_u32 (tb[i]); - filter->dev =__dev_get_by_index (net, ifindex); + filter->dev =dev_get_by_index_rcu (net, ifindex);if (!filter->dev)return -ENODEV ; filter->filter_set =1 ;
@@ -2191,20 +2191,19 @@ static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)struct net *net =sock_net (skb->sk);struct mpls_route __rcu **platform_label;struct fib_dump_filter filter = { - .rtnl_held = true, + .rtnl_held = false, };unsigned int flags =NLM_F_MULTI ;size_t platform_labels;unsigned int index; +int err; -ASSERT_RTNL (); +rcu_read_lock ();if (cb->strict_check) { -int err; - err =mpls_valid_fib_dump_req (net, nlh, &filter, cb);if (err <0 ) -return err; + goto err;/* for MPLS, there is only 1 table with fixed type and flags. * If either are set in the filter then return nothing.
@@ -2212,14 +2211,14 @@ static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)if ((filter.table_id && filter.table_id !=RT_TABLE_MAIN ) || (filter.rt_type && filter.rt_type !=RTN_UNICAST ) || filter.flags) -return skb->len; + goto unlock; } index = cb->args[0 ];if (index <MPLS_LABEL_FIRST_UNRESERVED ) index =MPLS_LABEL_FIRST_UNRESERVED ; - platform_label =rtnl_dereference (net->mpls.platform_label); + platform_label =rcu_dereference (net->mpls.platform_label); platform_labels = net->mpls.platform_labels;if (filter.filter_set)
@@ -2228,7 +2227,7 @@ static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)for (; index < platform_labels; index++) {struct mpls_route *rt; - rt =rtnl_dereference (platform_label[index]); + rt =rcu_dereference (platform_label[index]);if (!rt)continue ;
@@ -2243,7 +2242,13 @@ static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb) } cb->args[0 ] = index; +unlock: +rcu_read_unlock ();return skb->len; + +err: +rcu_read_unlock (); +return err; }static inline size_t lfib_nlmsg_size (struct mpls_route *rt)
@@ -2767,7 +2772,8 @@ static struct rtnl_af_ops mpls_af_ops __read_mostly = { static const struct rtnl_msg_handler mpls_rtnl_msg_handlers[] __initdata_or_module = { {THIS_MODULE ,PF_MPLS ,RTM_NEWROUTE , mpls_rtm_newroute,NULL ,0 }, {THIS_MODULE ,PF_MPLS ,RTM_DELROUTE , mpls_rtm_delroute,NULL ,0 }, - {THIS_MODULE ,PF_MPLS ,RTM_GETROUTE , mpls_getroute, mpls_dump_routes,0 }, + {THIS_MODULE ,PF_MPLS ,RTM_GETROUTE , mpls_getroute, mpls_dump_routes, +RTNL_FLAG_DUMP_UNLOCKED }, {THIS_MODULE ,PF_MPLS ,RTM_GETNETCONF , mpls_netconf_get_devconf, mpls_netconf_dump_devconf,RTNL_FLAG_DUMP_UNLOCKED },
Signature · 1 line
2.51.1.851.g4ebd6896fd-goog
PATCH v2 11/13 mpls: Convert RTM_GETNETCONF to RCU.
In reply to Kuniyuki Iwashimampls_netconf_get_devconf() calls __dev_get_by_index(), and this only depends on RTNL.
Let's convert mpls_netconf_get_devconf() to RCU and use dev_get_by_index_rcu().
Note that nlmsg_new() is moved ahead to use GFP_KERNEL.
--- net/mpls/af_mpls.c | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-)
net/mpls/af_mpls.c +30 -14
@@ -1282,23 +1282,32 @@ static int mpls_netconf_get_devconf(struct sk_buff *in_skb,if (err <0 ) goto errout; - err = -EINVAL ; -if (!tb[NETCONFA_IFINDEX ]) +if (!tb[NETCONFA_IFINDEX ]) { + err = -EINVAL ; goto errout; + } ifindex =nla_get_s32 (tb[NETCONFA_IFINDEX ]); - dev =__dev_get_by_index (net, ifindex); -if (!dev) - goto errout; - - mdev =mpls_dev_get (net, dev); -if (!mdev) - goto errout; - err = -ENOBUFS ; skb =nlmsg_new (mpls_netconf_msgsize_devconf (NETCONFA_ALL ),GFP_KERNEL ); -if (!skb) +if (!skb) { + err = -ENOBUFS ; goto errout; + } + +rcu_read_lock (); + + dev =dev_get_by_index_rcu (net, ifindex); +if (!dev) { + err = -EINVAL ; + goto errout_unlock; + } + + mdev =mpls_dev_rcu (dev); +if (!mdev) { + err = -EINVAL ; + goto errout_unlock; + } err = mpls_netconf_fill_devconf(skb, mdev, NETLINK_CB(in_skb).portid,
@@ -1307,12 +1316,19 @@ static int mpls_netconf_get_devconf(struct sk_buff *in_skb,if (err <0 ) {/* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */ WARN_ON (err == -EMSGSIZE ); -kfree_skb (skb); - goto errout; + goto errout_unlock; } + err =rtnl_unicast (skb, net,NETLINK_CB (in_skb).portid); + +rcu_read_unlock (); errout:return err; + +errout_unlock: +rcu_read_unlock (); +kfree_skb (skb); + goto errout; } static int mpls_netconf_dump_devconf(struct sk_buff *skb,
@@ -2776,7 +2792,7 @@ static const struct rtnl_msg_handler mpls_rtnl_msg_handlers[] __initdata_or_modu RTNL_FLAG_DUMP_UNLOCKED}, {THIS_MODULE ,PF_MPLS ,RTM_GETNETCONF , mpls_netconf_get_devconf, mpls_netconf_dump_devconf, - RTNL_FLAG_DUMP_UNLOCKED}, + RTNL_FLAG_DOIT_UNLOCKED | RTNL_FLAG_DUMP_UNLOCKED}, };static int __init mpls_init (void )
Signature · 1 line
2.51.1.851.g4ebd6896fd-goog
PATCH v2 12/13 mpls: Protect net->mpls.platform_label with a per-netns mutex.
In reply to Kuniyuki IwashimaMPLS (re)uses RTNL to protect net->mpls.platform_label, but the lock does not need to be RTNL at all.
Let's protect net->mpls.platform_label with a dedicated per-netns mutex.
--- include/net/netns/mpls.h | 1 + net/mpls/af_mpls.c | 55 ++++++++++++++++++++++++++-------------- net/mpls/internal.h | 7 ++++- 3 files changed, 43 insertions(+), 20 deletions(-)
include/net/netns/mpls.h +1 -0
@@ -16,6 +16,7 @@ struct netns_mpls {int default_ttl;size_t platform_labels;struct mpls_route __rcu *__rcu *platform_label; +struct mutex platform_mutex;struct ctl_table_header *ctl; };
net/mpls/af_mpls.c +36 -19
179 lines not on this page · raw message
net/mpls/internal.h +6 -1
@@ -185,6 +185,11 @@ static inline struct mpls_entry_decoded mpls_entry_decode(struct mpls_shim_hdr *return result; } +#define mpls_dereference (net, p) \ + rcu_dereference_protected( \ + (p), \ + lockdep_is_held(&(net)->mpls.platform_mutex)) +static inline struct mpls_dev *mpls_dev_rcu (const struct net_device *dev) {return rcu_dereference (dev->mpls_ptr);
@@ -193,7 +198,7 @@ static inline struct mpls_dev *mpls_dev_rcu(const struct net_device *dev)static inline struct mpls_dev *mpls_dev_get (const struct net *net,const struct net_device *dev) { -return rcu_dereference_rtnl (dev->mpls_ptr); +return mpls_dereference (net, dev->mpls_ptr); } int nla_put_labels(struct sk_buff *skb, int attrtype, u8 labels,
Signature · 1 line
2.51.1.851.g4ebd6896fd-goog
PATCH v2 13/13 mpls: Drop RTNL for RTM_NEWROUTE, RTM_DELROUTE, and RTM_GETROUTE.
In reply to Kuniyuki IwashimaRTM_NEWROUTE looks up dev under RCU (ip_route_output(), ipv6_stub->ipv6_dst_lookup_flow(), netdev_get_by_index()), and each neighbour holds the refcnt of its dev.
Also, net->mpls.platform_label is protected by a dedicated per-netns mutex.
Now, no MPLS code depends on RTNL.
Let's drop RTNL for RTM_NEWROUTE, RTM_DELROUTE, and RTM_GETROUTE.
--- net/mpls/af_mpls.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
net/mpls/af_mpls.c +5 -3
@@ -2803,10 +2803,12 @@ static struct rtnl_af_ops mpls_af_ops __read_mostly = { }; static const struct rtnl_msg_handler mpls_rtnl_msg_handlers[] __initdata_or_module = { - {THIS_MODULE ,PF_MPLS ,RTM_NEWROUTE , mpls_rtm_newroute,NULL ,0 }, - {THIS_MODULE ,PF_MPLS ,RTM_DELROUTE , mpls_rtm_delroute,NULL ,0 }, + {THIS_MODULE ,PF_MPLS ,RTM_NEWROUTE , mpls_rtm_newroute,NULL , +RTNL_FLAG_DOIT_UNLOCKED }, + {THIS_MODULE ,PF_MPLS ,RTM_DELROUTE , mpls_rtm_delroute,NULL , +RTNL_FLAG_DOIT_UNLOCKED }, {THIS_MODULE ,PF_MPLS ,RTM_GETROUTE , mpls_getroute, mpls_dump_routes, -RTNL_FLAG_DUMP_UNLOCKED }, +RTNL_FLAG_DOIT_UNLOCKED |RTNL_FLAG_DUMP_UNLOCKED }, {THIS_MODULE ,PF_MPLS ,RTM_GETNETCONF , mpls_netconf_get_devconf, mpls_netconf_dump_devconf,RTNL_FLAG_DOIT_UNLOCKED |RTNL_FLAG_DUMP_UNLOCKED },
Signature · 1 line
2.51.1.851.g4ebd6896fd-goog
Quoted 3 lines · Kuniyuki Iwashima
The series removes RTNL from
net/mpls/.
For the series,
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 8 lines · On Wed, 29 Oct 2025 17:32:52 +0000 you wrote:
MPLS uses RTNL
1) to guarantee the lifetime of struct mpls_nh.nh_dev
2) to protectnet->mpls.platform_label, but neither actually requires RTNL.
[...]
Here is the summary with links:
- [v2,net-next,01/13] mpls: Return early in mpls_label_ok().
https://git.kernel.org/netdev/net-next/c/2214ca1ff6df - [v2,net-next,02/13] mpls: Hold dev refcnt for mpls_nh. https://git.kernel.org/netdev/net-next/c/f0914b8436c5 - [v2,net-next,03/13] mpls: Unify return paths in mpls_dev_notify(). https://git.kernel.org/netdev/net-next/c/451c538ec067 - [v2,net-next,04/13] ipv6: Add in6_dev_rcu(). https://git.kernel.org/netdev/net-next/c/d8f9581e1b7f - [v2,net-next,05/13] mpls: Use in6_dev_rcu() and dev_net_rcu() in mpls_forward() and mpls_xmit(). https://git.kernel.org/netdev/net-next/c/bc7ebc569e8c - [v2,net-next,06/13] mpls: Add mpls_dev_rcu(). https://git.kernel.org/netdev/net-next/c/ab061f334792 - [v2,net-next,07/13] mpls: Pass net to mpls_dev_get(). https://git.kernel.org/netdev/net-next/c/1fb462de9329 - [v2,net-next,08/13] mpls: Add mpls_route_input(). https://git.kernel.org/netdev/net-next/c/73e405393991 - [v2,net-next,09/13] mpls: Use mpls_route_input() where appropriate. https://git.kernel.org/netdev/net-next/c/3a49629335a5 - [v2,net-next,10/13] mpls: Convert mpls_dump_routes() to RCU. https://git.kernel.org/netdev/net-next/c/dde1b38e873c - [v2,net-next,11/13] mpls: Convert RTM_GETNETCONF to RCU. https://git.kernel.org/netdev/net-next/c/fb2b77b9b1db - [v2,net-next,12/13] mpls: Protect net->mpls.platform_label with a per-netns mutex. https://git.kernel.org/netdev/net-next/c/e833eb25161a - [v2,net-next,13/13] mpls: Drop RTNL for RTM_NEWROUTE, RTM_DELROUTE, and RTM_GETROUTE. https://git.kernel.org/netdev/net-next/c/7d99a7c6c6a3
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html