Commit 39eb8cd1 authored by David Ahern's avatar David Ahern Committed by David S. Miller

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: default avatarDavid Ahern <dsa@cumulusnetworks.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 3d8417d7
...@@ -189,10 +189,15 @@ static u32 mpls_multipath_hash(struct mpls_route *rt, struct sk_buff *skb) ...@@ -189,10 +189,15 @@ static u32 mpls_multipath_hash(struct mpls_route *rt, struct sk_buff *skb)
return hash; 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, static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
struct sk_buff *skb) struct sk_buff *skb)
{ {
int alive = ACCESS_ONCE(rt->rt_nhn_alive); unsigned int alive;
u32 hash = 0; u32 hash = 0;
int nh_index = 0; int nh_index = 0;
int n = 0; int n = 0;
...@@ -203,7 +208,8 @@ static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt, ...@@ -203,7 +208,8 @@ static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
if (rt->rt_nhn == 1) if (rt->rt_nhn == 1)
goto out; goto out;
if (alive <= 0) alive = READ_ONCE(rt->rt_nhn_alive);
if (alive == 0)
return NULL; return NULL;
hash = mpls_multipath_hash(rt, skb); hash = mpls_multipath_hash(rt, skb);
...@@ -211,7 +217,9 @@ static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt, ...@@ -211,7 +217,9 @@ static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
if (alive == rt->rt_nhn) if (alive == rt->rt_nhn)
goto out; goto out;
for_nexthops(rt) { 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; continue;
if (n == nh_index) if (n == nh_index)
return nh; return nh;
...@@ -1302,7 +1310,6 @@ static void mpls_ifdown(struct net_device *dev, int event) ...@@ -1302,7 +1310,6 @@ static void mpls_ifdown(struct net_device *dev, int event)
{ {
struct mpls_route __rcu **platform_label; struct mpls_route __rcu **platform_label;
struct net *net = dev_net(dev); struct net *net = dev_net(dev);
unsigned int nh_flags = RTNH_F_DEAD | RTNH_F_LINKDOWN;
unsigned int alive, deleted; unsigned int alive, deleted;
unsigned index; unsigned index;
...@@ -1316,22 +1323,27 @@ static void mpls_ifdown(struct net_device *dev, int event) ...@@ -1316,22 +1323,27 @@ static void mpls_ifdown(struct net_device *dev, int event)
alive = 0; alive = 0;
deleted = 0; deleted = 0;
change_nexthops(rt) { change_nexthops(rt) {
unsigned int nh_flags = nh->nh_flags;
if (rtnl_dereference(nh->nh_dev) != dev) if (rtnl_dereference(nh->nh_dev) != dev)
goto next; goto next;
switch (event) { switch (event) {
case NETDEV_DOWN: case NETDEV_DOWN:
case NETDEV_UNREGISTER: case NETDEV_UNREGISTER:
nh->nh_flags |= RTNH_F_DEAD; nh_flags |= RTNH_F_DEAD;
/* fall through */ /* fall through */
case NETDEV_CHANGE: case NETDEV_CHANGE:
nh->nh_flags |= RTNH_F_LINKDOWN; nh_flags |= RTNH_F_LINKDOWN;
break; break;
} }
if (event == NETDEV_UNREGISTER) if (event == NETDEV_UNREGISTER)
RCU_INIT_POINTER(nh->nh_dev, NULL); RCU_INIT_POINTER(nh->nh_dev, NULL);
if (nh->nh_flags != nh_flags)
WRITE_ONCE(nh->nh_flags, nh_flags);
next: next:
if (!(nh->nh_flags & nh_flags)) if (!(nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)))
alive++; alive++;
if (!rtnl_dereference(nh->nh_dev)) if (!rtnl_dereference(nh->nh_dev))
deleted++; deleted++;
...@@ -1345,7 +1357,7 @@ static void mpls_ifdown(struct net_device *dev, int event) ...@@ -1345,7 +1357,7 @@ static void mpls_ifdown(struct net_device *dev, int event)
} }
} }
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 mpls_route __rcu **platform_label;
struct net *net = dev_net(dev); struct net *net = dev_net(dev);
...@@ -1361,20 +1373,22 @@ static void mpls_ifup(struct net_device *dev, unsigned int nh_flags) ...@@ -1361,20 +1373,22 @@ static void mpls_ifup(struct net_device *dev, unsigned int nh_flags)
alive = 0; alive = 0;
change_nexthops(rt) { change_nexthops(rt) {
unsigned int nh_flags = nh->nh_flags;
struct net_device *nh_dev = struct net_device *nh_dev =
rtnl_dereference(nh->nh_dev); rtnl_dereference(nh->nh_dev);
if (!(nh->nh_flags & nh_flags)) { if (!(nh_flags & flags)) {
alive++; alive++;
continue; continue;
} }
if (nh_dev != dev) if (nh_dev != dev)
continue; continue;
alive++; alive++;
nh->nh_flags &= ~nh_flags; nh_flags &= ~flags;
WRITE_ONCE(nh->nh_flags, flags);
} endfor_nexthops(rt); } endfor_nexthops(rt);
ACCESS_ONCE(rt->rt_nhn_alive) = alive; WRITE_ONCE(rt->rt_nhn_alive, alive);
} }
} }
......
...@@ -83,6 +83,10 @@ enum mpls_payload_type { ...@@ -83,6 +83,10 @@ enum mpls_payload_type {
struct mpls_nh { /* next hop label forwarding entry */ struct mpls_nh { /* next hop label forwarding entry */
struct net_device __rcu *nh_dev; 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; unsigned int nh_flags;
u32 nh_label[MAX_NEW_LABELS]; u32 nh_label[MAX_NEW_LABELS];
u8 nh_labels; u8 nh_labels;
...@@ -124,6 +128,10 @@ struct mpls_route { /* next hop label forwarding entry */ ...@@ -124,6 +128,10 @@ struct mpls_route { /* next hop label forwarding entry */
u8 rt_max_alen; u8 rt_max_alen;
u8 rt_ttl_propagate; u8 rt_ttl_propagate;
unsigned int rt_nhn; 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; unsigned int rt_nhn_alive;
struct mpls_nh rt_nh[0]; struct mpls_nh rt_nh[0];
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment