Skip to content

Commit ddf251f

Browse files
edumazetkuba-moo
authored andcommitted
tcp_metrics: fix data-race in tcpm_suck_dst() vs fastopen
Whenever tcpm_new() reclaims an old entry, tcpm_suck_dst() would overwrite data that could be read from tcp_fastopen_cache_get() or tcp_metrics_fill_info(). We need to acquire fastopen_seqlock to maintain consistency. For newly allocated objects, tcpm_new() can switch to kzalloc() to avoid an extra fastopen_seqlock acquisition. Fixes: 1fe4c48 ("net-tcp: Fast Open client - cookie cache") Signed-off-by: Eric Dumazet <edumazet@google.com> Cc: Yuchung Cheng <ycheng@google.com> Reviewed-by: Kuniyuki Iwashima <kuniyu@amazon.com> Link: https://lore.kernel.org/r/20230802131500.1478140-7-edumazet@google.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent d5d986c commit ddf251f

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

net/ipv4/tcp_metrics.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ static struct tcpm_hash_bucket *tcp_metrics_hash __read_mostly;
9393
static unsigned int tcp_metrics_hash_log __read_mostly;
9494

9595
static DEFINE_SPINLOCK(tcp_metrics_lock);
96+
static DEFINE_SEQLOCK(fastopen_seqlock);
9697

9798
static void tcpm_suck_dst(struct tcp_metrics_block *tm,
9899
const struct dst_entry *dst,
@@ -129,11 +130,13 @@ static void tcpm_suck_dst(struct tcp_metrics_block *tm,
129130
tcp_metric_set(tm, TCP_METRIC_REORDERING,
130131
dst_metric_raw(dst, RTAX_REORDERING));
131132
if (fastopen_clear) {
133+
write_seqlock(&fastopen_seqlock);
132134
tm->tcpm_fastopen.mss = 0;
133135
tm->tcpm_fastopen.syn_loss = 0;
134136
tm->tcpm_fastopen.try_exp = 0;
135137
tm->tcpm_fastopen.cookie.exp = false;
136138
tm->tcpm_fastopen.cookie.len = 0;
139+
write_sequnlock(&fastopen_seqlock);
137140
}
138141
}
139142

@@ -194,7 +197,7 @@ static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst,
194197
}
195198
tm = oldest;
196199
} else {
197-
tm = kmalloc(sizeof(*tm), GFP_ATOMIC);
200+
tm = kzalloc(sizeof(*tm), GFP_ATOMIC);
198201
if (!tm)
199202
goto out_unlock;
200203
}
@@ -204,7 +207,7 @@ static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst,
204207
tm->tcpm_saddr = *saddr;
205208
tm->tcpm_daddr = *daddr;
206209

207-
tcpm_suck_dst(tm, dst, true);
210+
tcpm_suck_dst(tm, dst, reclaim);
208211

209212
if (likely(!reclaim)) {
210213
tm->tcpm_next = tcp_metrics_hash[hash].chain;
@@ -556,8 +559,6 @@ bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst)
556559
return ret;
557560
}
558561

559-
static DEFINE_SEQLOCK(fastopen_seqlock);
560-
561562
void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
562563
struct tcp_fastopen_cookie *cookie)
563564
{

0 commit comments

Comments
 (0)