net: avoid potential false sharing in neighbor related code
authorEric Dumazet <edumazet@google.com>
Tue, 5 Nov 2019 22:11:51 +0000 (14:11 -0800)
committerDavid S. Miller <davem@davemloft.net>
Thu, 7 Nov 2019 00:14:48 +0000 (16:14 -0800)
There are common instances of the following construct :

if (n->confirmed != now)
n->confirmed = now;

A C compiler could legally remove the conditional.

Use READ_ONCE()/WRITE_ONCE() to avoid this problem.

Signed-off-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/net/arp.h
include/net/ndisc.h
include/net/sock.h

index c8f580a0e6b1f5c0853cda1605336fa8eb90917c..4950191f6b2bf424519c7ecf3483336739fea143 100644 (file)
@@ -57,8 +57,8 @@ static inline void __ipv4_confirm_neigh(struct net_device *dev, u32 key)
                unsigned long now = jiffies;
 
                /* avoid dirtying neighbour */
-               if (n->confirmed != now)
-                       n->confirmed = now;
+               if (READ_ONCE(n->confirmed) != now)
+                       WRITE_ONCE(n->confirmed, now);
        }
        rcu_read_unlock_bh();
 }
index b2f715ca056726d75033083d1bccee693cea9672..b5ebeb3b0de0e9de3fa495148ab19830d9767e89 100644 (file)
@@ -414,8 +414,8 @@ static inline void __ipv6_confirm_neigh(struct net_device *dev,
                unsigned long now = jiffies;
 
                /* avoid dirtying neighbour */
-               if (n->confirmed != now)
-                       n->confirmed = now;
+               if (READ_ONCE(n->confirmed) != now)
+                       WRITE_ONCE(n->confirmed, now);
        }
        rcu_read_unlock_bh();
 }
@@ -431,8 +431,8 @@ static inline void __ipv6_confirm_neigh_stub(struct net_device *dev,
                unsigned long now = jiffies;
 
                /* avoid dirtying neighbour */
-               if (n->confirmed != now)
-                       n->confirmed = now;
+               if (READ_ONCE(n->confirmed) != now)
+                       WRITE_ONCE(n->confirmed, now);
        }
        rcu_read_unlock_bh();
 }
index ac6042d0af328a7f6e18f8882d2ee0679a0e59c5..f2f853439b6576925e39f6db010964762e39ccf2 100644 (file)
@@ -1939,8 +1939,8 @@ struct dst_entry *sk_dst_check(struct sock *sk, u32 cookie);
 
 static inline void sk_dst_confirm(struct sock *sk)
 {
-       if (!sk->sk_dst_pending_confirm)
-               sk->sk_dst_pending_confirm = 1;
+       if (!READ_ONCE(sk->sk_dst_pending_confirm))
+               WRITE_ONCE(sk->sk_dst_pending_confirm, 1);
 }
 
 static inline void sock_confirm_neigh(struct sk_buff *skb, struct neighbour *n)
@@ -1950,10 +1950,10 @@ static inline void sock_confirm_neigh(struct sk_buff *skb, struct neighbour *n)
                unsigned long now = jiffies;
 
                /* avoid dirtying neighbour */
-               if (n->confirmed != now)
-                       n->confirmed = now;
-               if (sk && sk->sk_dst_pending_confirm)
-                       sk->sk_dst_pending_confirm = 0;
+               if (READ_ONCE(n->confirmed) != now)
+                       WRITE_ONCE(n->confirmed, now);
+               if (sk && READ_ONCE(sk->sk_dst_pending_confirm))
+                       WRITE_ONCE(sk->sk_dst_pending_confirm, 0);
        }
 }