Skip to content

Commit e3144ff

Browse files
committed
Merge branch 'rfs-lockless-annotate'
Eric Dumazet says: ==================== rfs: annotate lockless accesses rfs runs without locks held, so we should annotate read and writes to shared variables. It should prevent compilers forcing writes in the following situation: if (var != val) var = val; A compiler could indeed simply avoid the conditional: var = val; This matters if var is shared between many cpus. v2: aligns one closing bracket (Simon) adds Fixes: tags (Jakub) ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents ab39b11 + 5c3b74a commit e3144ff

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

include/linux/netdevice.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -768,8 +768,11 @@ static inline void rps_record_sock_flow(struct rps_sock_flow_table *table,
768768
/* We only give a hint, preemption can change CPU under us */
769769
val |= raw_smp_processor_id();
770770

771-
if (table->ents[index] != val)
772-
table->ents[index] = val;
771+
/* The following WRITE_ONCE() is paired with the READ_ONCE()
772+
* here, and another one in get_rps_cpu().
773+
*/
774+
if (READ_ONCE(table->ents[index]) != val)
775+
WRITE_ONCE(table->ents[index], val);
773776
}
774777
}
775778

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

net/core/dev.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4471,8 +4471,10 @@ static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
44714471
u32 next_cpu;
44724472
u32 ident;
44734473

4474-
/* First check into global flow table if there is a match */
4475-
ident = sock_flow_table->ents[hash & sock_flow_table->mask];
4474+
/* First check into global flow table if there is a match.
4475+
* This READ_ONCE() pairs with WRITE_ONCE() from rps_record_sock_flow().
4476+
*/
4477+
ident = READ_ONCE(sock_flow_table->ents[hash & sock_flow_table->mask]);
44764478
if ((ident ^ hash) & ~rps_cpu_mask)
44774479
goto try_rps;
44784480

0 commit comments

Comments
 (0)