Skip to content

Commit

Permalink
net: avoid potential false sharing in neighbor related code
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
Eric Dumazet authored and davem330 committed Nov 7, 2019
1 parent 3828a93 commit 25c7a6d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions include/net/arp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
8 changes: 4 additions & 4 deletions include/net/ndisc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down
12 changes: 6 additions & 6 deletions include/net/sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}
}

Expand Down

0 comments on commit 25c7a6d

Please sign in to comment.