net: mpls: rt_nhn_alive and nh_flags should be accessed using READ_ONCE

The number of alive nexthops for a route (rt->rt_nhn_alive) and the
flags for a next hop (nh->nh_flags) are modified by netdev event
handlers. The event handlers run with rtnl_lock held so updates are
always done with the lock held. The packet path accesses the fields
under the rcu lock. Since those fields can change at any moment in
the packet path, both fields should be accessed using READ_ONCE. Updates
to both fields should use WRITE_ONCE.

Update mpls_select_multipath (packet path) and mpls_ifdown and mpls_ifup
(event handlers) accordingly.

Signed-off-by: David Ahern <dsa@cumulusnetworks.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
David Ahern 2017-03-31 07:13:59 -07:00 committed by David S. Miller
parent 3d8417d79e
commit 39eb8cd175
2 changed files with 33 additions and 11 deletions

View File

@ -189,10 +189,15 @@ static u32 mpls_multipath_hash(struct mpls_route *rt, struct sk_buff *skb)
return hash;
}
/* number of alive nexthops (rt->rt_nhn_alive) and the flags for
* a next hop (nh->nh_flags) are modified by netdev event handlers.
* Since those fields can change at any moment, use READ_ONCE to
* access both.
*/
static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
struct sk_buff *skb)
{
int alive = ACCESS_ONCE(rt->rt_nhn_alive);
unsigned int alive;
u32 hash = 0;
int nh_index = 0;
int n = 0;
@ -203,7 +208,8 @@ static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
if (rt->rt_nhn == 1)
goto out;
if (alive <= 0)
alive = READ_ONCE(rt->rt_nhn_alive);
if (alive == 0)
return NULL;
hash = mpls_multipath_hash(rt, skb);
@ -211,7 +217,9 @@ static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
if (alive == rt->rt_nhn)
goto out;
for_nexthops(rt) {
if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
unsigned int nh_flags = READ_ONCE(nh->nh_flags);
if (nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
continue;
if (n == nh_index)
return nh;
@ -1302,7 +1310,6 @@ static void mpls_ifdown(struct net_device *dev, int event)
{
struct mpls_route __rcu **platform_label;
struct net *net = dev_net(dev);
unsigned int nh_flags = RTNH_F_DEAD | RTNH_F_LINKDOWN;
unsigned int alive, deleted;
unsigned index;
@ -1316,22 +1323,27 @@ static void mpls_ifdown(struct net_device *dev, int event)
alive = 0;
deleted = 0;
change_nexthops(rt) {
unsigned int nh_flags = nh->nh_flags;
if (rtnl_dereference(nh->nh_dev) != dev)
goto next;
switch (event) {
case NETDEV_DOWN:
case NETDEV_UNREGISTER:
nh->nh_flags |= RTNH_F_DEAD;
nh_flags |= RTNH_F_DEAD;
/* fall through */
case NETDEV_CHANGE:
nh->nh_flags |= RTNH_F_LINKDOWN;
nh_flags |= RTNH_F_LINKDOWN;
break;
}
if (event == NETDEV_UNREGISTER)
RCU_INIT_POINTER(nh->nh_dev, NULL);
if (nh->nh_flags != nh_flags)
WRITE_ONCE(nh->nh_flags, nh_flags);
next:
if (!(nh->nh_flags & nh_flags))
if (!(nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)))
alive++;
if (!rtnl_dereference(nh->nh_dev))
deleted++;
@ -1345,7 +1357,7 @@ next:
}
}
static void mpls_ifup(struct net_device *dev, unsigned int nh_flags)
static void mpls_ifup(struct net_device *dev, unsigned int flags)
{
struct mpls_route __rcu **platform_label;
struct net *net = dev_net(dev);
@ -1361,20 +1373,22 @@ static void mpls_ifup(struct net_device *dev, unsigned int nh_flags)
alive = 0;
change_nexthops(rt) {
unsigned int nh_flags = nh->nh_flags;
struct net_device *nh_dev =
rtnl_dereference(nh->nh_dev);
if (!(nh->nh_flags & nh_flags)) {
if (!(nh_flags & flags)) {
alive++;
continue;
}
if (nh_dev != dev)
continue;
alive++;
nh->nh_flags &= ~nh_flags;
nh_flags &= ~flags;
WRITE_ONCE(nh->nh_flags, flags);
} endfor_nexthops(rt);
ACCESS_ONCE(rt->rt_nhn_alive) = alive;
WRITE_ONCE(rt->rt_nhn_alive, alive);
}
}

View File

@ -83,6 +83,10 @@ enum mpls_payload_type {
struct mpls_nh { /* next hop label forwarding entry */
struct net_device __rcu *nh_dev;
/* nh_flags is accessed under RCU in the packet path; it is
* modified handling netdev events with rtnl lock held
*/
unsigned int nh_flags;
u32 nh_label[MAX_NEW_LABELS];
u8 nh_labels;
@ -124,6 +128,10 @@ struct mpls_route { /* next hop label forwarding entry */
u8 rt_max_alen;
u8 rt_ttl_propagate;
unsigned int rt_nhn;
/* rt_nhn_alive is accessed under RCU in the packet path; it
* is modified handling netdev events with rtnl lock held
*/
unsigned int rt_nhn_alive;
struct mpls_nh rt_nh[0];
};