Skip to content

Commit 1e5c647

Browse files
edumazetdavem330
authored andcommitted
rfs: annotate lockless accesses to sk->sk_rxhash
Add READ_ONCE()/WRITE_ONCE() on accesses to sk->sk_rxhash. This also prevents a (smart ?) compiler to remove the condition in: if (sk->sk_rxhash != newval) sk->sk_rxhash = newval; We need the condition to avoid dirtying a shared cache line. Fixes: fec5e65 ("rfs: Receive Flow Steering") Signed-off-by: Eric Dumazet <[email protected]> Reviewed-by: Simon Horman <[email protected]> Reviewed-by: Kuniyuki Iwashima <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent ab39b11 commit 1e5c647

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

include/net/sock.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,8 +1152,12 @@ static inline void sock_rps_record_flow(const struct sock *sk)
11521152
* OR an additional socket flag
11531153
* [1] : sk_state and sk_prot are in the same cache line.
11541154
*/
1155-
if (sk->sk_state == TCP_ESTABLISHED)
1156-
sock_rps_record_flow_hash(sk->sk_rxhash);
1155+
if (sk->sk_state == TCP_ESTABLISHED) {
1156+
/* This READ_ONCE() is paired with the WRITE_ONCE()
1157+
* from sock_rps_save_rxhash() and sock_rps_reset_rxhash().
1158+
*/
1159+
sock_rps_record_flow_hash(READ_ONCE(sk->sk_rxhash));
1160+
}
11571161
}
11581162
#endif
11591163
}
@@ -1162,15 +1166,19 @@ static inline void sock_rps_save_rxhash(struct sock *sk,
11621166
const struct sk_buff *skb)
11631167
{
11641168
#ifdef CONFIG_RPS
1165-
if (unlikely(sk->sk_rxhash != skb->hash))
1166-
sk->sk_rxhash = skb->hash;
1169+
/* The following WRITE_ONCE() is paired with the READ_ONCE()
1170+
* here, and another one in sock_rps_record_flow().
1171+
*/
1172+
if (unlikely(READ_ONCE(sk->sk_rxhash) != skb->hash))
1173+
WRITE_ONCE(sk->sk_rxhash, skb->hash);
11671174
#endif
11681175
}
11691176

11701177
static inline void sock_rps_reset_rxhash(struct sock *sk)
11711178
{
11721179
#ifdef CONFIG_RPS
1173-
sk->sk_rxhash = 0;
1180+
/* Paired with READ_ONCE() in sock_rps_record_flow() */
1181+
WRITE_ONCE(sk->sk_rxhash, 0);
11741182
#endif
11751183
}
11761184

0 commit comments

Comments
 (0)