Skip to content

Commit

Permalink
tcp: annotate data-races on tp->segs_in and tp->data_segs_in
Browse files Browse the repository at this point in the history
tcp_segs_in() can be called from BH, while socket spinlock
is held but socket owned by user, eventually reading these
fields from tcp_get_info()

Found by code inspection, no need to backport this patch
to older kernels.

Signed-off-by: Eric Dumazet <edumazet@google.com>
  • Loading branch information
Eric Dumazet authored and xanmod committed Jan 12, 2022
1 parent 14e47c2 commit 3c4bc0b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
8 changes: 6 additions & 2 deletions include/net/tcp.h
Expand Up @@ -2203,9 +2203,13 @@ static inline void tcp_segs_in(struct tcp_sock *tp, const struct sk_buff *skb)
u16 segs_in;

segs_in = max_t(u16, 1, skb_shinfo(skb)->gso_segs);
tp->segs_in += segs_in;

/* We update these fields while other threads might
* read them from tcp_get_info()
*/
WRITE_ONCE(tp->segs_in, tp->segs_in + segs_in);
if (skb->len > tcp_hdrlen(skb))
tp->data_segs_in += segs_in;
WRITE_ONCE(tp->data_segs_in, tp->data_segs_in + segs_in);
}

/*
Expand Down
6 changes: 4 additions & 2 deletions net/ipv4/tcp.c
Expand Up @@ -3773,10 +3773,12 @@ void tcp_get_info(struct sock *sk, struct tcp_info *info)
tcp_get_info_chrono_stats(tp, info);

info->tcpi_segs_out = tp->segs_out;
info->tcpi_segs_in = tp->segs_in;

/* segs_in and data_segs_in can be updated from tcp_segs_in() from BH */
info->tcpi_segs_in = READ_ONCE(tp->segs_in);
info->tcpi_data_segs_in = READ_ONCE(tp->data_segs_in);

info->tcpi_min_rtt = tcp_min_rtt(tp);
info->tcpi_data_segs_in = tp->data_segs_in;
info->tcpi_data_segs_out = tp->data_segs_out;

info->tcpi_delivery_rate_app_limited = tp->rate_app_limited ? 1 : 0;
Expand Down

0 comments on commit 3c4bc0b

Please sign in to comment.