ipmr: get rid of rwlocks
Subsystems (3)
cover ipmr: get rid of rwlocks
We need to get rid of rwlocks in networking stacks, if read_lock() is (ab)used from softirq context.
As discussed recently [1], rwlock are unfair by design in this case, and writers can starve and trigger soft lockups.
This series convert ipmr code (both IPv4 and IPv6 families) to RCU and spinlocks.
[1] https://lkml.org/lkml/2022/6/17/272
Eric Dumazet (19):
ip6mr: do not get a device reference in pim6_rcv()
ipmr: add rcu protection over (struct vif_device)->dev
ipmr: change igmpmsg_netlink_event() prototype
ipmr: ipmr_cache_report() changes
ipmr: do not acquire mrt_lock in __pim_rcv()
ipmr: do not acquire mrt_lock in ioctl(SIOCGETVIFCNT)
ipmr: do not acquire mrt_lock before calling ipmr_cache_unresolved()
ipmr: do not acquire mrt_lock while calling ip_mr_forward()
ipmr: do not acquire mrt_lock in ipmr_get_route()
ip6mr: ip6mr_cache_report() changes
ip6mr: do not acquire mrt_lock in pim6_rcv()
ip6mr: do not acquire mrt_lock in ioctl(SIOCGETMIFCNT_IN6)
ip6mr: do not acquire mrt_lock before calling ip6mr_cache_unresolved
ip6mr: do not acquire mrt_lock while calling ip6_mr_forward()
ip6mr: switch ip6mr_get_route() to rcu_read_lock()
ipmr: adopt rcu_read_lock() in mr_dump()
ipmr: conver /proc handlers to rcu_read_lock()
ipmr: convert mrt_lock to a spinlock
ip6mr: convert mrt_lock to a spinlock
include/linux/mroute_base.h | 15 ++- net/ipv4/ipmr.c | 215 +++++++++++++++++++----------------- net/ipv4/ipmr_base.c | 53 +++++---- net/ipv6/ip6mr.c | 202 ++++++++++++++++----------------- 4 files changed, 255 insertions(+), 230 deletions(-)
Signature · 1 line
2.37.0.rc0.104.g0611611a94-goog
PATCH 1/19 ip6mr: do not get a device reference in pim6_rcv()
pim6_rcv() is called under rcu_read_lock(), there is no need to use dev_hold()/dev_put() pair.
IPv4 side was handled in commit 55747a0a73ea ("ipmr: __pim_rcv() is called under rcu_read_lock")
--- net/ipv6/ip6mr.c | 2 -- 1 file changed, 2 deletions(-)
net/ipv6/ip6mr.c +0 -2
@@ -554,7 +554,6 @@ static int pim6_rcv(struct sk_buff *skb)read_lock (&mrt_lock);if (reg_vif_num >=0 ) reg_dev = mrt->vif_table[reg_vif_num].dev; -dev_hold (reg_dev);read_unlock (&mrt_lock);if (!reg_dev)
@@ -570,7 +569,6 @@ static int pim6_rcv(struct sk_buff *skb)netif_rx (skb); -dev_put (reg_dev);return 0 ; drop:kfree_skb (skb);
Signature · 1 line
2.37.0.rc0.104.g0611611a94-goog
PATCH 2/19 ipmr: add rcu protection over (struct vif_device)->dev
In reply to Eric DumazetWe will soon use RCU instead of rwlock in impr.
This preliminary patch adds proper rcu verbs to read/write (struct vif_device)->dev
--- include/linux/mroute_base.h | 11 ++++--- net/ipv4/ipmr.c | 65 ++++++++++++++++++++++--------------- net/ipv4/ipmr_base.c | 49 ++++++++++++++++++---------- net/ipv6/ip6mr.c | 63 +++++++++++++++++++---------------- 4 files changed, 111 insertions(+), 77 deletions(-)
include/linux/mroute_base.h +7 -4
@@ -26,7 +26,7 @@* @remote: Remote address for tunnels */ struct vif_device { -struct net_device *dev; +struct net_device __rcu *dev;netdevice_tracker dev_tracker;unsigned long bytes_in, bytes_out;unsigned long pkt_in, pkt_out;
@@ -52,6 +52,7 @@ static inline int mr_call_vif_notifier(struct notifier_block *nb,unsigned short family, enum fib_event_type event_type, struct vif_device *vif, + struct net_device *vif_dev, unsigned short vif_index, u32 tb_id, struct netlink_ext_ack *extack) {
@@ -60,7 +61,7 @@ static inline int mr_call_vif_notifier(struct notifier_block *nb, .family = family, .extack = extack, }, - .dev = vif->dev, + .dev = vif_dev, .vif_index = vif_index, .vif_flags = vif->flags, .tb_id = tb_id,
@@ -73,6 +74,7 @@ static inline int mr_call_vif_notifiers(struct net *net,unsigned short family, enum fib_event_type event_type, struct vif_device *vif, + struct net_device *vif_dev, unsigned short vif_index, u32 tb_id, unsigned int *ipmr_seq) {
@@ -80,7 +82,7 @@ static inline int mr_call_vif_notifiers(struct net *net, .info = { .family = family, }, - .dev = vif->dev, + .dev = vif_dev, .vif_index = vif_index, .vif_flags = vif->flags, .tb_id = tb_id,
@@ -98,7 +100,8 @@ static inline int mr_call_vif_notifiers(struct net *net,#define MAXVIFS 32#endif -#define VIF_EXISTS (_mrt, _idx) (!!((_mrt)->vif_table[_idx].dev)) +/* Note: This helper is deprecated. */ +#define VIF_EXISTS (_mrt, _idx) (!!rcu_access_pointer((_mrt)->vif_table[_idx].dev))/* mfc_flags: * MFC_STATIC - the entry was added statically (not by a routing daemon)
net/ipv4/ipmr.c +38 -27
168 lines not on this page · raw message
net/ipv4/ipmr_base.c +31 -18
108 lines not on this page · raw message
net/ipv6/ip6mr.c +35 -28
161 lines not on this page · raw message
Signature · 1 line
2.37.0.rc0.104.g0611611a94-goog
PATCH 3/19 ipmr: change igmpmsg_netlink_event() prototype
In reply to Eric Dumazetigmpmsg_netlink_event() first argument can be marked const.
igmpmsg_netlink_event() reads mrt->net and mrt->id, both being set once in mr_table_alloc().
--- net/ipv4/ipmr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/ipv4/ipmr.c +2 -2
@@ -110,7 +110,7 @@ static int ipmr_cache_report(struct mr_table *mrt, struct sk_buff *pkt, vifi_t vifi, int assert);static void mroute_netlink_event (struct mr_table *mrt,struct mfc_cache *mfc,int cmd); -static void igmpmsg_netlink_event (struct mr_table *mrt,struct sk_buff *pkt); +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 ipmr_expire_process (struct timer_list *t);
@@ -2410,7 +2410,7 @@ static size_t igmpmsg_netlink_msgsize(size_t payloadlen)return len; } -static void igmpmsg_netlink_event (struct mr_table *mrt,struct sk_buff *pkt) +static void igmpmsg_netlink_event (const struct mr_table *mrt,struct sk_buff *pkt) {struct net *net =read_pnet (&mrt->net);struct nlmsghdr *nlh;
Signature · 1 line
2.37.0.rc0.104.g0611611a94-goog
PATCH 4/19 ipmr: ipmr_cache_report() changes
In reply to Eric Dumazetipmr_cache_report() first argument can be marked const, and we change the caller convention about which lock needs to be held.
Instead of read_lock(&mrt_lock), we can use rcu_read_lock().
--- net/ipv4/ipmr.c | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-)
net/ipv4/ipmr.c +29 -16
@@ -106,7 +106,7 @@ static void ipmr_free_table(struct mr_table *mrt);static void ip_mr_forward (struct net *net,struct mr_table *mrt,struct net_device *dev,struct sk_buff *skb,struct mfc_cache *cache,int local); -static int ipmr_cache_report (struct mr_table *mrt, +static int ipmr_cache_report (const struct mr_table *mrt,struct sk_buff *pkt,vifi_t vifi,int assert);static void mroute_netlink_event (struct mr_table *mrt,struct mfc_cache *mfc,int cmd);
@@ -507,11 +507,15 @@ static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)return err; } -read_lock (&mrt_lock); dev->stats.tx_bytes += skb->len; dev->stats.tx_packets++; -ipmr_cache_report (mrt, skb, mrt->mroute_reg_vif_num,IGMPMSG_WHOLEPKT ); -read_unlock (&mrt_lock); +rcu_read_lock (); + +/* Pairs with WRITE_ONCE() in vif_add() and vif_delete() */ +ipmr_cache_report (mrt, skb,READ_ONCE (mrt->mroute_reg_vif_num), +IGMPMSG_WHOLEPKT ); + +rcu_read_unlock ();kfree_skb (skb);return NETDEV_TX_OK ; }
@@ -665,9 +669,10 @@ static int vif_delete(struct mr_table *mrt, int vifi, int notify, vifi, mrt->id);RCU_INIT_POINTER (v->dev,NULL ); -if (vifi == mrt->mroute_reg_vif_num) - mrt->mroute_reg_vif_num =-1 ; - +if (vifi == mrt->mroute_reg_vif_num) { +/* Pairs with READ_ONCE() in ipmr_cache_report() and reg_vif_xmit() */ +WRITE_ONCE (mrt->mroute_reg_vif_num,-1 ); + }if (vifi +1 == mrt->maxvif) {int tmp;
@@ -895,8 +900,10 @@ static int vif_add(struct net *net, struct mr_table *mrt,write_lock_bh (&mrt_lock);rcu_assign_pointer (v->dev, dev);netdev_tracker_alloc (dev, &v->dev_tracker,GFP_ATOMIC ); -if (v->flags &VIFF_REGISTER ) - mrt->mroute_reg_vif_num = vifi; +if (v->flags &VIFF_REGISTER ) { +/* Pairs with READ_ONCE() in ipmr_cache_report() and reg_vif_xmit() */ +WRITE_ONCE (mrt->mroute_reg_vif_num, vifi); + }if (vifi+1 > mrt->maxvif) mrt->maxvif = vifi+1 ;write_unlock_bh (&mrt_lock);
@@ -1005,9 +1012,9 @@ static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,/* Bounce a cache query up to mrouted and netlink. * -* Called under mrt_lock. +* Called under rcu_read_lock(). */ -static int ipmr_cache_report (struct mr_table *mrt, +static int ipmr_cache_report (const struct mr_table *mrt,struct sk_buff *pkt,vifi_t vifi,int assert) {const int ihl =ip_hdrlen (pkt);
@@ -1042,8 +1049,11 @@ static int ipmr_cache_report(struct mr_table *mrt, msg->im_vif = vifi; msg->im_vif_hi = vifi >>8 ; }else { - msg->im_vif = mrt->mroute_reg_vif_num; - msg->im_vif_hi = mrt->mroute_reg_vif_num >>8 ; +/* Pairs with WRITE_ONCE() in vif_add() and vif_delete() */ +int vif_num =READ_ONCE (mrt->mroute_reg_vif_num); + + msg->im_vif = vif_num; + msg->im_vif_hi = vif_num >>8 ; }ip_hdr (skb)->ihl =sizeof (struct iphdr ) >>2 ; ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
@@ -1068,10 +1078,8 @@ static int ipmr_cache_report(struct mr_table *mrt, skb->transport_header = skb->network_header; } -rcu_read_lock (); mroute_sk =rcu_dereference (mrt->mroute_sk);if (!mroute_sk) { -rcu_read_unlock ();kfree_skb (skb);return -EINVAL ; }
@@ -1080,7 +1088,7 @@ static int ipmr_cache_report(struct mr_table *mrt,/* Deliver to mrouted */ ret =sock_queue_rcv_skb (mroute_sk, skb); -rcu_read_unlock (); +if (ret <0 ) {net_warn_ratelimited ("mroute: pending queue full, dropping entries\n" );kfree_skb (skb);
@@ -1090,6 +1098,7 @@ static int ipmr_cache_report(struct mr_table *mrt, }/* Queue a packet for resolution. It gets locked cache entry! */ +/* Called under rcu_read_lock() */ static int ipmr_cache_unresolved (struct mr_table *mrt,vifi_t vifi,struct sk_buff *skb,struct net_device *dev) {
@@ -1830,7 +1839,9 @@ static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt, vif->bytes_out += skb->len; vif_dev->stats.tx_bytes += skb->len; vif_dev->stats.tx_packets++; +rcu_read_lock ();ipmr_cache_report (mrt, skb, vifi,IGMPMSG_WHOLEPKT ); +rcu_read_unlock (); goto out_free; }
@@ -1980,10 +1991,12 @@ static void ip_mr_forward(struct net *net, struct mr_table *mrt, c->_c.mfc_un.res.last_assert + MFC_ASSERT_THRESH)) { c->_c.mfc_un.res.last_assert = jiffies; +rcu_read_lock ();ipmr_cache_report (mrt, skb, true_vifi,IGMPMSG_WRONGVIF );if (mrt->mroute_do_wrvifwhole)ipmr_cache_report (mrt, skb, true_vifi,IGMPMSG_WRVIFWHOLE ); +rcu_read_unlock (); } goto dont_forward; }
Signature · 1 line
2.37.0.rc0.104.g0611611a94-goog
PATCH 5/19 ipmr: do not acquire mrt_lock in __pim_rcv()
In reply to Eric Dumazetrcu_read_lock() protection is more than enough.
vif_dev_read() supports either mrt_lock or rcu_read_lock().
--- net/ipv4/ipmr.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
net/ipv4/ipmr.c +5 -5
@@ -582,6 +582,7 @@ static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb, {struct net_device *reg_dev =NULL ;struct iphdr *encap; +int vif_num; encap = (struct iphdr *)(skb_transport_header (skb) + pimlen);/* Check that:
@@ -594,11 +595,10 @@ static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb, ntohs(encap->tot_len) + pimlen > skb->len)return 1 ; -read_lock (&mrt_lock); -if (mrt->mroute_reg_vif_num >=0 ) - reg_dev =vif_dev_read (&mrt->vif_table[mrt->mroute_reg_vif_num]); -read_unlock (&mrt_lock); - +/* Pairs with WRITE_ONCE() in vif_add()/vid_delete() */ + vif_num =READ_ONCE (mrt->mroute_reg_vif_num); +if (vif_num >=0 ) + reg_dev =vif_dev_read (&mrt->vif_table[vif_num]);if (!reg_dev)return 1 ;
Signature · 1 line
2.37.0.rc0.104.g0611611a94-goog
PATCH 6/19 ipmr: do not acquire mrt_lock in ioctl(SIOCGETVIFCNT)
In reply to Eric Dumazetrcu_read_lock() protection is good enough.
--- net/ipv4/ipmr.c | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-)
net/ipv4/ipmr.c +22 -20
@@ -1611,20 +1611,20 @@ int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)if (vr.vifi >= mrt->maxvif)return -EINVAL ; vr.vifi =array_index_nospec (vr.vifi, mrt->maxvif); -read_lock (&mrt_lock); +rcu_read_lock (); vif = &mrt->vif_table[vr.vifi];if (VIF_EXISTS (mrt, vr.vifi)) { - vr.icount = vif->pkt_in; - vr.ocount = vif->pkt_out; - vr.ibytes = vif->bytes_in; - vr.obytes = vif->bytes_out; -read_unlock (&mrt_lock); + vr.icount =READ_ONCE (vif->pkt_in); + vr.ocount =READ_ONCE (vif->pkt_out); + vr.ibytes =READ_ONCE (vif->bytes_in); + vr.obytes =READ_ONCE (vif->bytes_out); +rcu_read_unlock ();if (copy_to_user (arg, &vr,sizeof (vr)))return -EFAULT ;return 0 ; } -read_unlock (&mrt_lock); +rcu_read_unlock ();return -EADDRNOTAVAIL ;case SIOCGETSGCNT :if (copy_from_user (&sr, arg,sizeof (sr)))
@@ -1686,20 +1686,20 @@ int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)if (vr.vifi >= mrt->maxvif)return -EINVAL ; vr.vifi =array_index_nospec (vr.vifi, mrt->maxvif); -read_lock (&mrt_lock); +rcu_read_lock (); vif = &mrt->vif_table[vr.vifi];if (VIF_EXISTS (mrt, vr.vifi)) { - vr.icount = vif->pkt_in; - vr.ocount = vif->pkt_out; - vr.ibytes = vif->bytes_in; - vr.obytes = vif->bytes_out; -read_unlock (&mrt_lock); + vr.icount =READ_ONCE (vif->pkt_in); + vr.ocount =READ_ONCE (vif->pkt_out); + vr.ibytes =READ_ONCE (vif->bytes_in); + vr.obytes =READ_ONCE (vif->bytes_out); +rcu_read_unlock ();if (copy_to_user (arg, &vr,sizeof (vr)))return -EFAULT ;return 0 ; } -read_unlock (&mrt_lock); +rcu_read_unlock ();return -EADDRNOTAVAIL ;case SIOCGETSGCNT :if (copy_from_user (&sr, arg,sizeof (sr)))
@@ -1835,8 +1835,8 @@ static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt, goto out_free;if (vif->flags &VIFF_REGISTER ) { - vif->pkt_out++; - vif->bytes_out += skb->len; +WRITE_ONCE (vif->pkt_out, vif->pkt_out +1 ); +WRITE_ONCE (vif->bytes_out, vif->bytes_out + skb->len); vif_dev->stats.tx_bytes += skb->len; vif_dev->stats.tx_packets++;rcu_read_lock ();
@@ -1885,8 +1885,8 @@ static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt, goto out_free; } - vif->pkt_out++; - vif->bytes_out += skb->len; +WRITE_ONCE (vif->pkt_out, vif->pkt_out +1 ); +WRITE_ONCE (vif->bytes_out, vif->bytes_out + skb->len);skb_dst_drop (skb);skb_dst_set (skb, &rt->dst);
@@ -2002,8 +2002,10 @@ static void ip_mr_forward(struct net *net, struct mr_table *mrt, } forward: - mrt->vif_table[vif].pkt_in++; - mrt->vif_table[vif].bytes_in += skb->len; +WRITE_ONCE (mrt->vif_table[vif].pkt_in, + mrt->vif_table[vif].pkt_in +1 ); +WRITE_ONCE (mrt->vif_table[vif].bytes_in, + mrt->vif_table[vif].bytes_in + skb->len);/* Forward the frame */ if (c->mfc_origin == htonl(INADDR_ANY) &&
Signature · 1 line
2.37.0.rc0.104.g0611611a94-goog
PATCH 7/19 ipmr: do not acquire mrt_lock before calling ipmr_cache_unresolved()
In reply to Eric Dumazetrcu_read_lock() protection is good enough.
ipmr_cache_unresolved() uses a dedicated spinlock (mfc_unres_lock)
--- net/ipv4/ipmr.c | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-)
net/ipv4/ipmr.c +8 -17
@@ -680,7 +680,7 @@ static int vif_delete(struct mr_table *mrt, int vifi, int notify,if (VIF_EXISTS (mrt, tmp))break ; } - mrt->maxvif = tmp+1 ; +WRITE_ONCE (mrt->maxvif, tmp +1 ); }write_unlock_bh (&mrt_lock);
@@ -905,7 +905,7 @@ static int vif_add(struct net *net, struct mr_table *mrt,WRITE_ONCE (mrt->mroute_reg_vif_num, vifi); }if (vifi+1 > mrt->maxvif) - mrt->maxvif = vifi+1 ; +WRITE_ONCE (mrt->maxvif, vifi +1 );write_unlock_bh (&mrt_lock);call_ipmr_vif_entry_notifiers (net,FIB_EVENT_VIF_ADD , v, dev, vifi, mrt->id);
@@ -1923,11 +1923,12 @@ static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,kfree_skb (skb); } -static int ipmr_find_vif (struct mr_table *mrt,struct net_device *dev) +/* Called with mrt_lock or rcu_read_lock() */ +static int ipmr_find_vif (const struct mr_table *mrt,struct net_device *dev) {int ct; - -for (ct = mrt->maxvif-1 ; ct >=0 ; ct--) { +/* Pairs with WRITE_ONCE() in vif_delete()/vif_add() */ +for (ct =READ_ONCE (mrt->maxvif) -1 ; ct >=0 ; ct--) {if (rcu_access_pointer (mrt->vif_table[ct].dev) == dev)break ; }
@@ -2161,15 +2162,9 @@ int ip_mr_input(struct sk_buff *skb) skb = skb2; } -read_lock (&mrt_lock); vif =ipmr_find_vif (mrt, dev); -if (vif >=0 ) { -int err2 =ipmr_cache_unresolved (mrt, vif, skb, dev); -read_unlock (&mrt_lock); - -return err2; - } -read_unlock (&mrt_lock); +if (vif >=0 ) +return ipmr_cache_unresolved (mrt, vif, skb, dev);kfree_skb (skb);return -ENODEV ; }
@@ -2273,18 +2268,15 @@ int ipmr_get_route(struct net *net, struct sk_buff *skb,int vif =-1 ; dev = skb->dev; -read_lock (&mrt_lock);if (dev) vif =ipmr_find_vif (mrt, dev);if (vif <0 ) { -read_unlock (&mrt_lock);rcu_read_unlock ();return -ENODEV ; } skb2 =skb_realloc_headroom (skb,sizeof (struct iphdr ));if (!skb2) { -read_unlock (&mrt_lock);rcu_read_unlock ();return -ENOMEM ; }
@@ -2298,7 +2290,6 @@ int ipmr_get_route(struct net *net, struct sk_buff *skb, iph->daddr = daddr; iph->version =0 ; err =ipmr_cache_unresolved (mrt, vif, skb2, dev); -read_unlock (&mrt_lock);rcu_read_unlock ();return err; }
Signature · 1 line
2.37.0.rc0.104.g0611611a94-goog
PATCH 8/19 ipmr: do not acquire mrt_lock while calling ip_mr_forward()
In reply to Eric Dumazetip_mr_forward() uses standard RCU protection already.
--- net/ipv4/ipmr.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)
net/ipv4/ipmr.c +2 -7
@@ -1817,7 +1817,7 @@ static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt, }#endif -/* Processing handlers for ipmr_forward */ +/* Processing handlers for ipmr_forward, under rcu_read_lock() */ static void ipmr_queue_xmit (struct net *net,struct mr_table *mrt,int in_vifi,struct sk_buff *skb,int vifi)
@@ -1839,9 +1839,7 @@ static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,WRITE_ONCE (vif->bytes_out, vif->bytes_out + skb->len); vif_dev->stats.tx_bytes += skb->len; vif_dev->stats.tx_packets++; -rcu_read_lock ();ipmr_cache_report (mrt, skb, vifi,IGMPMSG_WHOLEPKT ); -rcu_read_unlock (); goto out_free; }
@@ -1936,6 +1934,7 @@ static int ipmr_find_vif(const struct mr_table *mrt, struct net_device *dev) }/* "local" means that we should preserve one skb (for local delivery) */ +/* Called uner rcu_read_lock() */ static void ip_mr_forward (struct net *net,struct mr_table *mrt,struct net_device *dev,struct sk_buff *skb,struct mfc_cache *c,int local)
@@ -1992,12 +1991,10 @@ static void ip_mr_forward(struct net *net, struct mr_table *mrt, c->_c.mfc_un.res.last_assert + MFC_ASSERT_THRESH)) { c->_c.mfc_un.res.last_assert = jiffies; -rcu_read_lock ();ipmr_cache_report (mrt, skb, true_vifi,IGMPMSG_WRONGVIF );if (mrt->mroute_do_wrvifwhole)ipmr_cache_report (mrt, skb, true_vifi,IGMPMSG_WRVIFWHOLE ); -rcu_read_unlock (); } goto dont_forward; }
@@ -2169,9 +2166,7 @@ int ip_mr_input(struct sk_buff *skb)return -ENODEV ; } -read_lock (&mrt_lock);ip_mr_forward (net, mrt, dev, skb, cache, local); -read_unlock (&mrt_lock);if (local)return ip_local_deliver (skb);
Signature · 1 line
2.37.0.rc0.104.g0611611a94-goog
PATCH 9/19 ipmr: do not acquire mrt_lock in ipmr_get_route()
In reply to Eric Dumazetmr_fill_mroute() uses standard rcu_read_unlock(),
no need to grab mrt_lock anymore.
--- net/ipv4/ipmr.c | 2 -- 1 file changed, 2 deletions(-)
net/ipv4/ipmr.c +0 -2
@@ -2289,9 +2289,7 @@ int ipmr_get_route(struct net *net, struct sk_buff *skb,return err; } -read_lock (&mrt_lock); err =mr_fill_mroute (mrt, skb, &cache->_c, rtm); -read_unlock (&mrt_lock);rcu_read_unlock ();return err; }
Signature · 1 line
2.37.0.rc0.104.g0611611a94-goog
PATCH 10/19 ip6mr: ip6mr_cache_report() changes
In reply to Eric Dumazetip6mr_cache_report() first argument can be marked const, and we change the caller convention about which lock needs to be held.
Instead of read_lock(&mrt_lock), we can use rcu_read_lock().
--- net/ipv6/ip6mr.c | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-)
net/ipv6/ip6mr.c +20 -15
@@ -91,11 +91,11 @@ static void ip6mr_free_table(struct mr_table *mrt);static void ip6_mr_forward (struct net *net,struct mr_table *mrt,struct net_device *dev,struct sk_buff *skb,struct mfc6_cache *cache); -static int ip6mr_cache_report (struct mr_table *mrt,struct sk_buff *pkt, +static int ip6mr_cache_report (const struct mr_table *mrt,struct sk_buff *pkt,mifi_t mifi,int assert);static void mr6_netlink_event (struct mr_table *mrt,struct mfc6_cache *mfc,int cmd); -static void mrt6msg_netlink_event (struct mr_table *mrt,struct sk_buff *pkt); +static void mrt6msg_netlink_event (const struct mr_table *mrt,struct sk_buff *pkt);static int ip6mr_rtm_dumproute (struct sk_buff *skb,struct netlink_callback *cb);static void mroute_clean_tables (struct mr_table *mrt,int flags);
@@ -608,11 +608,12 @@ static netdev_tx_t reg_vif_xmit(struct sk_buff *skb,if (ip6mr_fib_lookup (net, &fl6, &mrt) <0 ) goto tx_err; -read_lock (&mrt_lock); dev->stats.tx_bytes += skb->len; dev->stats.tx_packets++; -ip6mr_cache_report (mrt, skb, mrt->mroute_reg_vif_num,MRT6MSG_WHOLEPKT ); -read_unlock (&mrt_lock); +rcu_read_lock (); +ip6mr_cache_report (mrt, skb,READ_ONCE (mrt->mroute_reg_vif_num), +MRT6MSG_WHOLEPKT ); +rcu_read_unlock ();kfree_skb (skb);return NETDEV_TX_OK ;
@@ -718,8 +719,10 @@ static int mif6_delete(struct mr_table *mrt, int vifi, int notify,RCU_INIT_POINTER (v->dev,NULL );#ifdef CONFIG_IPV6_PIMSM_V2 -if (vifi == mrt->mroute_reg_vif_num) - mrt->mroute_reg_vif_num =-1 ; +if (vifi == mrt->mroute_reg_vif_num) { +/* Pairs with READ_ONCE() in ip6mr_cache_report() and reg_vif_xmit() */ +WRITE_ONCE (mrt->mroute_reg_vif_num,-1 ); + }#endif if (vifi +1 == mrt->maxvif) {
@@ -922,7 +925,7 @@ static int mif6_add(struct net *net, struct mr_table *mrt,netdev_tracker_alloc (dev, &v->dev_tracker,GFP_ATOMIC );#ifdef CONFIG_IPV6_PIMSM_V2 if (v->flags &MIFF_REGISTER ) - mrt->mroute_reg_vif_num = vifi; +WRITE_ONCE (mrt->mroute_reg_vif_num, vifi);#endif if (vifi +1 > mrt->maxvif) mrt->maxvif = vifi +1 ;
@@ -1033,10 +1036,10 @@ static void ip6mr_cache_resolve(struct net *net, struct mr_table *mrt,/* * Bounce a cache query up to pim6sd and netlink. * -* Called under mrt_lock. +* Called under rcu_read_lock() */ -static int ip6mr_cache_report (struct mr_table *mrt,struct sk_buff *pkt, +static int ip6mr_cache_report (const struct mr_table *mrt,struct sk_buff *pkt,mifi_t mifi,int assert) {struct sock *mroute6_sk;
@@ -1077,7 +1080,7 @@ static int ip6mr_cache_report(struct mr_table *mrt, struct sk_buff *pkt,if (assert ==MRT6MSG_WRMIFWHOLE ) msg->im6_mif = mifi;else - msg->im6_mif = mrt->mroute_reg_vif_num; + msg->im6_mif =READ_ONCE (mrt->mroute_reg_vif_num); msg->im6_pad =0 ; msg->im6_src =ipv6_hdr (pkt)->saddr; msg->im6_dst =ipv6_hdr (pkt)->daddr;
@@ -1112,10 +1115,8 @@ static int ip6mr_cache_report(struct mr_table *mrt, struct sk_buff *pkt, skb->ip_summed =CHECKSUM_UNNECESSARY ; } -rcu_read_lock (); mroute6_sk =rcu_dereference (mrt->mroute_sk);if (!mroute6_sk) { -rcu_read_unlock ();kfree_skb (skb);return -EINVAL ; }
@@ -1124,7 +1125,7 @@ static int ip6mr_cache_report(struct mr_table *mrt, struct sk_buff *pkt,/* Deliver to user space multicast routing algorithms */ ret =sock_queue_rcv_skb (mroute6_sk, skb); -rcu_read_unlock (); +if (ret <0 ) {net_warn_ratelimited ("mroute6: pending queue full, dropping entries\n" );kfree_skb (skb);
@@ -2042,7 +2043,9 @@ static int ip6mr_forward2(struct net *net, struct mr_table *mrt, vif->bytes_out += skb->len; vif_dev->stats.tx_bytes += skb->len; vif_dev->stats.tx_packets++; +rcu_read_lock ();ip6mr_cache_report (mrt, skb, vifi,MRT6MSG_WHOLEPKT ); +rcu_read_unlock (); goto out_free; }#endif
@@ -2155,10 +2158,12 @@ static void ip6_mr_forward(struct net *net, struct mr_table *mrt, c->_c.mfc_un.res.last_assert + MFC_ASSERT_THRESH)) { c->_c.mfc_un.res.last_assert = jiffies; +rcu_read_lock ();ip6mr_cache_report (mrt, skb, true_vifi,MRT6MSG_WRONGMIF );if (mrt->mroute_do_wrvifwhole)ip6mr_cache_report (mrt, skb, true_vifi,MRT6MSG_WRMIFWHOLE ); +rcu_read_unlock (); } goto dont_forward; }
@@ -2465,7 +2470,7 @@ static size_t mrt6msg_netlink_msgsize(size_t payloadlen)return len; } -static void mrt6msg_netlink_event (struct mr_table *mrt,struct sk_buff *pkt) +static void mrt6msg_netlink_event (const struct mr_table *mrt,struct sk_buff *pkt) {struct net *net =read_pnet (&mrt->net);struct nlmsghdr *nlh;
Signature · 1 line
2.37.0.rc0.104.g0611611a94-goog
PATCH 11/19 ip6mr: do not acquire mrt_lock in pim6_rcv()
In reply to Eric Dumazetrcu_read_lock() protection is more than enough.
vif_dev_read() supports either mrt_lock or rcu_read_lock().
--- net/ipv6/ip6mr.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
net/ipv6/ip6mr.c +2 -3
@@ -559,12 +559,11 @@ static int pim6_rcv(struct sk_buff *skb)if (ip6mr_fib_lookup (net, &fl6, &mrt) <0 ) goto drop; - reg_vif_num = mrt->mroute_reg_vif_num; -read_lock (&mrt_lock); +/* Pairs with WRITE_ONCE() in mif6_add()/mif6_delete() */ + reg_vif_num =READ_ONCE (mrt->mroute_reg_vif_num);if (reg_vif_num >=0 ) reg_dev =vif_dev_read (&mrt->vif_table[reg_vif_num]); -read_unlock (&mrt_lock);if (!reg_dev) goto drop;
Signature · 1 line
2.37.0.rc0.104.g0611611a94-goog
PATCH 12/19 ip6mr: do not acquire mrt_lock in ioctl(SIOCGETMIFCNT_IN6)
In reply to Eric Dumazetrcu_read_lock() protection is good enough.
--- net/ipv6/ip6mr.c | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-)
net/ipv6/ip6mr.c +22 -20
@@ -1896,20 +1896,20 @@ int ip6mr_ioctl(struct sock *sk, int cmd, void __user *arg)if (vr.mifi >= mrt->maxvif)return -EINVAL ; vr.mifi =array_index_nospec (vr.mifi, mrt->maxvif); -read_lock (&mrt_lock); +rcu_read_lock (); vif = &mrt->vif_table[vr.mifi];if (VIF_EXISTS (mrt, vr.mifi)) { - vr.icount = vif->pkt_in; - vr.ocount = vif->pkt_out; - vr.ibytes = vif->bytes_in; - vr.obytes = vif->bytes_out; -read_unlock (&mrt_lock); + vr.icount =READ_ONCE (vif->pkt_in); + vr.ocount =READ_ONCE (vif->pkt_out); + vr.ibytes =READ_ONCE (vif->bytes_in); + vr.obytes =READ_ONCE (vif->bytes_out); +rcu_read_unlock ();if (copy_to_user (arg, &vr,sizeof (vr)))return -EFAULT ;return 0 ; } -read_unlock (&mrt_lock); +rcu_read_unlock ();return -EADDRNOTAVAIL ;case SIOCGETSGCNT_IN6 :if (copy_from_user (&sr, arg,sizeof (sr)))
@@ -1971,20 +1971,20 @@ int ip6mr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)if (vr.mifi >= mrt->maxvif)return -EINVAL ; vr.mifi =array_index_nospec (vr.mifi, mrt->maxvif); -read_lock (&mrt_lock); +rcu_read_lock (); vif = &mrt->vif_table[vr.mifi];if (VIF_EXISTS (mrt, vr.mifi)) { - vr.icount = vif->pkt_in; - vr.ocount = vif->pkt_out; - vr.ibytes = vif->bytes_in; - vr.obytes = vif->bytes_out; -read_unlock (&mrt_lock); + vr.icount =READ_ONCE (vif->pkt_in); + vr.ocount =READ_ONCE (vif->pkt_out); + vr.ibytes =READ_ONCE (vif->bytes_in); + vr.obytes =READ_ONCE (vif->bytes_out); +rcu_read_unlock ();if (copy_to_user (arg, &vr,sizeof (vr)))return -EFAULT ;return 0 ; } -read_unlock (&mrt_lock); +rcu_read_unlock ();return -EADDRNOTAVAIL ;case SIOCGETSGCNT_IN6 :if (copy_from_user (&sr, arg,sizeof (sr)))
@@ -2038,8 +2038,8 @@ static int ip6mr_forward2(struct net *net, struct mr_table *mrt,#ifdef CONFIG_IPV6_PIMSM_V2 if (vif->flags &MIFF_REGISTER ) { - vif->pkt_out++; - vif->bytes_out += skb->len; +WRITE_ONCE (vif->pkt_out, vif->pkt_out +1 ); +WRITE_ONCE (vif->bytes_out, vif->bytes_out + skb->len); vif_dev->stats.tx_bytes += skb->len; vif_dev->stats.tx_packets++;rcu_read_lock ();
@@ -2077,8 +2077,8 @@ static int ip6mr_forward2(struct net *net, struct mr_table *mrt,* result in receiving multiple packets. */ skb->dev = vif_dev; - vif->pkt_out++; - vif->bytes_out += skb->len; +WRITE_ONCE (vif->pkt_out, vif->pkt_out +1 ); +WRITE_ONCE (vif->bytes_out, vif->bytes_out + skb->len);/* We are about to write */ /* XXX: extension headers? */
@@ -2168,8 +2168,10 @@ static void ip6_mr_forward(struct net *net, struct mr_table *mrt, } forward: - mrt->vif_table[vif].pkt_in++; - mrt->vif_table[vif].bytes_in += skb->len; +WRITE_ONCE (mrt->vif_table[vif].pkt_in, + mrt->vif_table[vif].pkt_in +1 ); +WRITE_ONCE (mrt->vif_table[vif].bytes_in, + mrt->vif_table[vif].bytes_in + skb->len);/* * Forward the frame
Signature · 1 line
2.37.0.rc0.104.g0611611a94-goog
PATCH 13/19 ip6mr: do not acquire mrt_lock before calling ip6mr_cache_unresolved
In reply to Eric Dumazetrcu_read_lock() protection is good enough.
ip6mr_cache_unresolved() uses a dedicated spinlock (mfc_unres_lock)
--- net/ipv6/ip6mr.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
net/ipv6/ip6mr.c +6 -6
@@ -730,7 +730,7 @@ static int mif6_delete(struct mr_table *mrt, int vifi, int notify,if (VIF_EXISTS (mrt, tmp))break ; } - mrt->maxvif = tmp +1 ; +WRITE_ONCE (mrt->maxvif, tmp +1 ); }write_unlock_bh (&mrt_lock);
@@ -927,7 +927,7 @@ static int mif6_add(struct net *net, struct mr_table *mrt,WRITE_ONCE (mrt->mroute_reg_vif_num, vifi);#endif if (vifi +1 > mrt->maxvif) - mrt->maxvif = vifi +1 ; +WRITE_ONCE (mrt->maxvif, vifi +1 );write_unlock_bh (&mrt_lock);call_ip6mr_vif_entry_notifiers (net,FIB_EVENT_VIF_ADD , v, dev, vifi, mrt->id);
@@ -2099,11 +2099,13 @@ static int ip6mr_forward2(struct net *net, struct mr_table *mrt,return 0 ; } +/* Called with mrt_lock or rcu_read_lock() */ static int ip6mr_find_vif (struct mr_table *mrt,struct net_device *dev) {int ct; -for (ct = mrt->maxvif -1 ; ct >=0 ; ct--) { +/* Pairs with WRITE_ONCE() in mif6_delete()/mif6_add() */ +for (ct =READ_ONCE (mrt->maxvif) -1 ; ct >=0 ; ct--) {if (rcu_access_pointer (mrt->vif_table[ct].dev) == dev)break ; }
@@ -2249,7 +2251,6 @@ int ip6_mr_input(struct sk_buff *skb)return err; } -read_lock (&mrt_lock); cache =ip6mr_cache_find (mrt, &ipv6_hdr (skb)->saddr, &ipv6_hdr (skb)->daddr);if (!cache) {
@@ -2270,15 +2271,14 @@ int ip6_mr_input(struct sk_buff *skb) vif =ip6mr_find_vif (mrt, dev);if (vif >=0 ) {int err =ip6mr_cache_unresolved (mrt, vif, skb, dev); -read_unlock (&mrt_lock);return err; } -read_unlock (&mrt_lock);kfree_skb (skb);return -ENODEV ; } +read_lock (&mrt_lock);ip6_mr_forward (net, mrt, dev, skb, cache);read_unlock (&mrt_lock);
Signature · 1 line
2.37.0.rc0.104.g0611611a94-goog
PATCH 14/19 ip6mr: do not acquire mrt_lock while calling ip6_mr_forward()
In reply to Eric Dumazetip6_mr_forward() uses standard RCU protection already.
--- net/ipv6/ip6mr.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-)
net/ipv6/ip6mr.c +1 -9
@@ -2042,9 +2042,7 @@ static int ip6mr_forward2(struct net *net, struct mr_table *mrt,WRITE_ONCE (vif->bytes_out, vif->bytes_out + skb->len); vif_dev->stats.tx_bytes += skb->len; vif_dev->stats.tx_packets++; -rcu_read_lock ();ip6mr_cache_report (mrt, skb, vifi,MRT6MSG_WHOLEPKT ); -rcu_read_unlock (); goto out_free; }#endif
@@ -2112,6 +2110,7 @@ static int ip6mr_find_vif(struct mr_table *mrt, struct net_device *dev)return ct; } +/* Called under rcu_read_lock() */ static void ip6_mr_forward (struct net *net,struct mr_table *mrt,struct net_device *dev,struct sk_buff *skb,struct mfc6_cache *c)
@@ -2131,14 +2130,12 @@ static void ip6_mr_forward(struct net *net, struct mr_table *mrt,/* For an (*,G) entry, we only check that the incoming * interface is part of the static tree. */ -rcu_read_lock (); cache_proxy =mr_mfc_find_any_parent (mrt, vif);if (cache_proxy && cache_proxy->_c.mfc_un.res.ttls[true_vifi] <255 ) {rcu_read_unlock (); goto forward; } -rcu_read_unlock (); }/*
@@ -2159,12 +2156,10 @@ static void ip6_mr_forward(struct net *net, struct mr_table *mrt, c->_c.mfc_un.res.last_assert + MFC_ASSERT_THRESH)) { c->_c.mfc_un.res.last_assert = jiffies; -rcu_read_lock ();ip6mr_cache_report (mrt, skb, true_vifi,MRT6MSG_WRONGMIF );if (mrt->mroute_do_wrvifwhole)ip6mr_cache_report (mrt, skb, true_vifi,MRT6MSG_WRMIFWHOLE ); -rcu_read_unlock (); } goto dont_forward; }
@@ -2278,11 +2273,8 @@ int ip6_mr_input(struct sk_buff *skb)return -ENODEV ; } -read_lock (&mrt_lock);ip6_mr_forward (net, mrt, dev, skb, cache); -read_unlock (&mrt_lock); -return 0 ; }
Signature · 1 line
2.37.0.rc0.104.g0611611a94-goog
PATCH 15/19 ip6mr: switch ip6mr_get_route() to rcu_read_lock()
In reply to Eric DumazetLike ipmr_get_route(), we can use standard RCU here.
--- net/ipv6/ip6mr.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
net/ipv6/ip6mr.c +5 -5
@@ -2290,7 +2290,7 @@ int ip6mr_get_route(struct net *net, struct sk_buff *skb, struct rtmsg *rtm,if (!mrt)return -ENOENT ; -read_lock (&mrt_lock); +rcu_read_lock (); cache =ip6mr_cache_find (mrt, &rt->rt6i_src.addr, &rt->rt6i_dst.addr);if (!cache && skb->dev) {int vif =ip6mr_find_vif (mrt, skb->dev);
@@ -2308,14 +2308,14 @@ int ip6mr_get_route(struct net *net, struct sk_buff *skb, struct rtmsg *rtm, dev = skb->dev;if (!dev || (vif =ip6mr_find_vif (mrt, dev)) <0 ) { -read_unlock (&mrt_lock); +rcu_read_unlock ();return -ENODEV ; }/* really correct? */ skb2 =alloc_skb (sizeof (struct ipv6hdr ),GFP_ATOMIC );if (!skb2) { -read_unlock (&mrt_lock); +rcu_read_unlock ();return -ENOMEM ; }
@@ -2338,13 +2338,13 @@ int ip6mr_get_route(struct net *net, struct sk_buff *skb, struct rtmsg *rtm, iph->daddr = rt->rt6i_dst.addr; err =ip6mr_cache_unresolved (mrt, vif, skb2, dev); -read_unlock (&mrt_lock); +rcu_read_unlock ();return err; } err =mr_fill_mroute (mrt, skb, &cache->_c, rtm); -read_unlock (&mrt_lock); +rcu_read_unlock ();return err; }
Signature · 1 line
2.37.0.rc0.104.g0611611a94-goog
PATCH 16/19 ipmr: adopt rcu_read_lock() in mr_dump()
In reply to Eric DumazetWe no longer need to acquire mrt_lock() in mr_dump, using rcu_read_lock() is enough.
--- include/linux/mroute_base.h | 4 ++-- net/ipv4/ipmr.c | 2 +- net/ipv4/ipmr_base.c | 8 +++----- net/ipv6/ip6mr.c | 2 +- 4 files changed, 7 insertions(+), 9 deletions(-)
include/linux/mroute_base.h +2 -2
@@ -308,7 +308,7 @@ int mr_dump(struct net *net, struct notifier_block *nb, unsigned short family, struct netlink_ext_ack *extack), struct mr_table *(*mr_iter)(struct net *net,struct mr_table *mrt), - rwlock_t *mrt_lock, struct netlink_ext_ack *extack); + struct netlink_ext_ack *extack);#else static inline void vif_device_init(struct vif_device *v, struct net_device *dev,
@@ -363,7 +363,7 @@ static inline int mr_dump(struct net *net, struct notifier_block *nb, struct netlink_ext_ack *extack), struct mr_table *(*mr_iter)(struct net *net,struct mr_table *mrt), - rwlock_t *mrt_lock, struct netlink_ext_ack *extack) + struct netlink_ext_ack *extack) { return -EINVAL; }
net/ipv4/ipmr.c +1 -1
@@ -3027,7 +3027,7 @@ static int ipmr_dump(struct net *net, struct notifier_block *nb, struct netlink_ext_ack *extack) {return mr_dump (net, nb,RTNL_FAMILY_IPMR , ipmr_rules_dump, - ipmr_mr_table_iter, &mrt_lock, extack); + ipmr_mr_table_iter, extack); }static const struct fib_notifier_ops ipmr_notifier_ops_template = {
net/ipv4/ipmr_base.c +3 -5
@@ -399,7 +399,6 @@ int mr_dump(struct net *net, struct notifier_block *nb, unsigned short family, struct netlink_ext_ack *extack), struct mr_table *(*mr_iter)(struct net *net,struct mr_table *mrt), - rwlock_t *mrt_lock, struct netlink_ext_ack *extack) { struct mr_table *mrt;
@@ -416,10 +415,9 @@ int mr_dump(struct net *net, struct notifier_block *nb, unsigned short family,int vifi;/* Notifiy on table VIF entries */ -read_lock (mrt_lock); +rcu_read_lock ();for (vifi =0 ; vifi < mrt->maxvif; vifi++, v++) { - vif_dev =rcu_dereference_check (v->dev, -lockdep_is_held (mrt_lock)); + vif_dev =rcu_dereference (v->dev);if (!vif_dev)continue ;
@@ -430,7 +428,7 @@ int mr_dump(struct net *net, struct notifier_block *nb, unsigned short family,if (err)break ; } -read_unlock (mrt_lock); +rcu_read_unlock ();if (err)return err;
net/ipv6/ip6mr.c +1 -1
@@ -1267,7 +1267,7 @@ static int ip6mr_dump(struct net *net, struct notifier_block *nb, struct netlink_ext_ack *extack) {return mr_dump (net, nb,RTNL_FAMILY_IP6MR , ip6mr_rules_dump, - ip6mr_mr_table_iter, &mrt_lock, extack); + ip6mr_mr_table_iter, extack); }static struct notifier_block ip6_mr_notifier = {
Signature · 1 line
2.37.0.rc0.104.g0611611a94-goog
PATCH 17/19 ipmr: conver /proc handlers to rcu_read_lock()
In reply to Eric DumazetWe can use standard rcu_read_lock(), to get rid
of last read_lock(&mrt_lock) call points.
--- net/ipv4/ipmr.c | 8 ++++---- net/ipv6/ip6mr.c | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-)
net/ipv4/ipmr.c +4 -4
@@ -2895,7 +2895,7 @@ static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb)*/ static void *ipmr_vif_seq_start (struct seq_file *seq,loff_t *pos) -__acquires (mrt_lock) +__acquires (RCU ) {struct mr_vif_iter *iter = seq->private;struct net *net =seq_file_net (seq);
@@ -2907,14 +2907,14 @@ static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos) iter->mrt = mrt; -read_lock (&mrt_lock); +rcu_read_lock ();return mr_vif_seq_start (seq, pos); }static void ipmr_vif_seq_stop (struct seq_file *seq,void *v) -__releases (mrt_lock) +__releases (RCU ) { -read_unlock (&mrt_lock); +rcu_read_unlock (); }static int ipmr_vif_seq_show (struct seq_file *seq,void *v)
net/ipv6/ip6mr.c +4 -4
@@ -404,7 +404,7 @@ static void ip6mr_free_table(struct mr_table *mrt)*/ static void *ip6mr_vif_seq_start (struct seq_file *seq,loff_t *pos) -__acquires (mrt_lock) +__acquires (RCU ) {struct mr_vif_iter *iter = seq->private;struct net *net =seq_file_net (seq);
@@ -416,14 +416,14 @@ static void *ip6mr_vif_seq_start(struct seq_file *seq, loff_t *pos) iter->mrt = mrt; -read_lock (&mrt_lock); +rcu_read_lock ();return mr_vif_seq_start (seq, pos); }static void ip6mr_vif_seq_stop (struct seq_file *seq,void *v) -__releases (mrt_lock) +__releases (RCU ) { -read_unlock (&mrt_lock); +rcu_read_unlock (); }static int ip6mr_vif_seq_show (struct seq_file *seq,void *v)
Signature · 1 line
2.37.0.rc0.104.g0611611a94-goog
PATCH 18/19 ipmr: convert mrt_lock to a spinlock
In reply to Eric Dumazetmrt_lock is only held in write mode, from process context only.
We can switch to a mere spinlock, and avoid blocking BH.
Also, vif_dev_read() is always called under standard rcu_read_lock().
--- net/ipv4/ipmr.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-)
net/ipv4/ipmr.c +9 -10
@@ -77,12 +77,11 @@ struct ipmr_result {* Note that the changes are semaphored via rtnl_lock. */ -static DEFINE_RWLOCK (mrt_lock); +static DEFINE_SPINLOCK (mrt_lock);static struct net_device *vif_dev_read (const struct vif_device *vif) { -return rcu_dereference_check (vif->dev, -lockdep_is_held (&mrt_lock)); +return rcu_dereference (vif->dev); }/* Multicast router control variables */
@@ -664,7 +663,7 @@ static int vif_delete(struct mr_table *mrt, int vifi, int notify,if (!dev)return -EADDRNOTAVAIL ; -write_lock_bh (&mrt_lock); +spin_lock (&mrt_lock);call_ipmr_vif_entry_notifiers (net,FIB_EVENT_VIF_DEL , v, dev, vifi, mrt->id);RCU_INIT_POINTER (v->dev,NULL );
@@ -683,7 +682,7 @@ static int vif_delete(struct mr_table *mrt, int vifi, int notify,WRITE_ONCE (mrt->maxvif, tmp +1 ); } -write_unlock_bh (&mrt_lock); +spin_unlock (&mrt_lock);dev_set_allmulti (dev,-1 );
@@ -785,7 +784,7 @@ static void ipmr_expire_process(struct timer_list *t)spin_unlock (&mfc_unres_lock); } -/* Fill oifs list. It is called under write locked mrt_lock. */ +/* Fill oifs list. It is called under locked mrt_lock. */ static void ipmr_update_thresholds (struct mr_table *mrt,struct mr_mfc *cache,unsigned char *ttls) {
@@ -897,7 +896,7 @@ static int vif_add(struct net *net, struct mr_table *mrt, v->remote = vifc->vifc_rmt_addr.s_addr;/* And finish update writing critical data */ -write_lock_bh (&mrt_lock); +spin_lock (&mrt_lock);rcu_assign_pointer (v->dev, dev);netdev_tracker_alloc (dev, &v->dev_tracker,GFP_ATOMIC );if (v->flags &VIFF_REGISTER ) {
@@ -906,7 +905,7 @@ static int vif_add(struct net *net, struct mr_table *mrt, }if (vifi+1 > mrt->maxvif)WRITE_ONCE (mrt->maxvif, vifi +1 ); -write_unlock_bh (&mrt_lock); +spin_unlock (&mrt_lock);call_ipmr_vif_entry_notifiers (net,FIB_EVENT_VIF_ADD , v, dev, vifi, mrt->id);return 0 ;
@@ -1211,12 +1210,12 @@ static int ipmr_mfc_add(struct net *net, struct mr_table *mrt, mfc->mfcc_mcastgrp.s_addr, parent);rcu_read_unlock ();if (c) { -write_lock_bh (&mrt_lock); +spin_lock (&mrt_lock); c->_c.mfc_parent = mfc->mfcc_parent;ipmr_update_thresholds (mrt, &c->_c, mfc->mfcc_ttls);if (!mrtsock) c->_c.mfc_flags |=MFC_STATIC ; -write_unlock_bh (&mrt_lock); +spin_unlock (&mrt_lock);call_ipmr_mfc_entry_notifiers (net,FIB_EVENT_ENTRY_REPLACE , c, mrt->id);mroute_netlink_event (mrt, c,RTM_NEWROUTE );
Signature · 1 line
2.37.0.rc0.104.g0611611a94-goog
Jakub Kicinski
maintainerQuoted 1 line · On Wed, 22 Jun 2022 05:12:36 +0000 Eric Dumazet wrote:
ipmr: conver /proc handlers to
rcu_read_lock()
convert
Quoted 2 lines
ipmr: convert
mrt_lockto a spinlock
ip6mr: convertmrt_lockto a spinlock
patch 19 did not make it :(