提交 a6ddf000 编写于 作者: E Eric Dumazet 提交者: Cheng Jian

tcp: annotate tp->copied_seq lockless reads

stable inclusion
from linux-4.19.181
commit 7209e120966c5557106caed20cb4bb08ab26d5d3

--------------------------------

[ Upstream commit 7db48e98 ]

There are few places where we fetch tp->copied_seq while
this field can change from IRQ or other cpu.

We need to add READ_ONCE() annotations, and also make
sure write sides use corresponding WRITE_ONCE() to avoid
store-tearing.

Note that tcp_inq_hint() was already using READ_ONCE(tp->copied_seq)
Signed-off-by: NEric Dumazet <edumazet@google.com>
Signed-off-by: NDavid S. Miller <davem@davemloft.net>
Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: NYang Yingliang <yangyingliang@huawei.com>
Signed-off-by: NCheng Jian <cj.chengjian@huawei.com>
上级 699675df
...@@ -567,7 +567,7 @@ __poll_t tcp_poll(struct file *file, struct socket *sock, poll_table *wait) ...@@ -567,7 +567,7 @@ __poll_t tcp_poll(struct file *file, struct socket *sock, poll_table *wait)
(state != TCP_SYN_RECV || tp->fastopen_rsk)) { (state != TCP_SYN_RECV || tp->fastopen_rsk)) {
int target = sock_rcvlowat(sk, 0, INT_MAX); int target = sock_rcvlowat(sk, 0, INT_MAX);
if (tp->urg_seq == tp->copied_seq && if (tp->urg_seq == READ_ONCE(tp->copied_seq) &&
!sock_flag(sk, SOCK_URGINLINE) && !sock_flag(sk, SOCK_URGINLINE) &&
tp->urg_data) tp->urg_data)
target++; target++;
...@@ -628,7 +628,7 @@ int tcp_ioctl(struct sock *sk, int cmd, unsigned long arg) ...@@ -628,7 +628,7 @@ int tcp_ioctl(struct sock *sk, int cmd, unsigned long arg)
unlock_sock_fast(sk, slow); unlock_sock_fast(sk, slow);
break; break;
case SIOCATMARK: case SIOCATMARK:
answ = tp->urg_data && tp->urg_seq == tp->copied_seq; answ = tp->urg_data && tp->urg_seq == READ_ONCE(tp->copied_seq);
break; break;
case SIOCOUTQ: case SIOCOUTQ:
if (sk->sk_state == TCP_LISTEN) if (sk->sk_state == TCP_LISTEN)
...@@ -1696,9 +1696,9 @@ int tcp_read_sock(struct sock *sk, read_descriptor_t *desc, ...@@ -1696,9 +1696,9 @@ int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
sk_eat_skb(sk, skb); sk_eat_skb(sk, skb);
if (!desc->count) if (!desc->count)
break; break;
tp->copied_seq = seq; WRITE_ONCE(tp->copied_seq, seq);
} }
tp->copied_seq = seq; WRITE_ONCE(tp->copied_seq, seq);
tcp_rcv_space_adjust(sk); tcp_rcv_space_adjust(sk);
...@@ -1835,7 +1835,7 @@ static int tcp_zerocopy_receive(struct sock *sk, ...@@ -1835,7 +1835,7 @@ static int tcp_zerocopy_receive(struct sock *sk,
out: out:
up_read(&current->mm->mmap_sem); up_read(&current->mm->mmap_sem);
if (length) { if (length) {
tp->copied_seq = seq; WRITE_ONCE(tp->copied_seq, seq);
tcp_rcv_space_adjust(sk); tcp_rcv_space_adjust(sk);
/* Clean up data we have read: This will do ACK frames. */ /* Clean up data we have read: This will do ACK frames. */
...@@ -2112,7 +2112,7 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock, ...@@ -2112,7 +2112,7 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
if (urg_offset < used) { if (urg_offset < used) {
if (!urg_offset) { if (!urg_offset) {
if (!sock_flag(sk, SOCK_URGINLINE)) { if (!sock_flag(sk, SOCK_URGINLINE)) {
++*seq; WRITE_ONCE(*seq, *seq + 1);
urg_hole++; urg_hole++;
offset++; offset++;
used--; used--;
...@@ -2134,7 +2134,7 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock, ...@@ -2134,7 +2134,7 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
} }
} }
*seq += used; WRITE_ONCE(*seq, *seq + used);
copied += used; copied += used;
len -= used; len -= used;
...@@ -2163,7 +2163,7 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock, ...@@ -2163,7 +2163,7 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
found_fin_ok: found_fin_ok:
/* Process the FIN. */ /* Process the FIN. */
++*seq; WRITE_ONCE(*seq, *seq + 1);
if (!(flags & MSG_PEEK)) if (!(flags & MSG_PEEK))
sk_eat_skb(sk, skb); sk_eat_skb(sk, skb);
break; break;
...@@ -2578,7 +2578,7 @@ int tcp_disconnect(struct sock *sk, int flags) ...@@ -2578,7 +2578,7 @@ int tcp_disconnect(struct sock *sk, int flags)
tcp_clear_xmit_timers(sk); tcp_clear_xmit_timers(sk);
__skb_queue_purge(&sk->sk_receive_queue); __skb_queue_purge(&sk->sk_receive_queue);
tp->copied_seq = tp->rcv_nxt; WRITE_ONCE(tp->copied_seq, tp->rcv_nxt);
tp->urg_data = 0; tp->urg_data = 0;
tcp_write_queue_purge(sk); tcp_write_queue_purge(sk);
tcp_fastopen_active_disable_ofo_check(sk); tcp_fastopen_active_disable_ofo_check(sk);
......
...@@ -30,7 +30,8 @@ static void tcp_diag_get_info(struct sock *sk, struct inet_diag_msg *r, ...@@ -30,7 +30,8 @@ static void tcp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
} else if (sk->sk_type == SOCK_STREAM) { } else if (sk->sk_type == SOCK_STREAM) {
const struct tcp_sock *tp = tcp_sk(sk); const struct tcp_sock *tp = tcp_sk(sk);
r->idiag_rqueue = max_t(int, READ_ONCE(tp->rcv_nxt) - tp->copied_seq, 0); r->idiag_rqueue = max_t(int, READ_ONCE(tp->rcv_nxt) -
READ_ONCE(tp->copied_seq), 0);
r->idiag_wqueue = tp->write_seq - tp->snd_una; r->idiag_wqueue = tp->write_seq - tp->snd_una;
} }
if (info) if (info)
......
...@@ -5897,7 +5897,7 @@ static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb, ...@@ -5897,7 +5897,7 @@ static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb,
/* Remember, tcp_poll() does not lock socket! /* Remember, tcp_poll() does not lock socket!
* Change state from SYN-SENT only after copied_seq * Change state from SYN-SENT only after copied_seq
* is initialized. */ * is initialized. */
tp->copied_seq = tp->rcv_nxt; WRITE_ONCE(tp->copied_seq, tp->rcv_nxt);
smc_check_reset_syn(tp); smc_check_reset_syn(tp);
...@@ -5972,7 +5972,7 @@ static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb, ...@@ -5972,7 +5972,7 @@ static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb,
} }
WRITE_ONCE(tp->rcv_nxt, TCP_SKB_CB(skb)->seq + 1); WRITE_ONCE(tp->rcv_nxt, TCP_SKB_CB(skb)->seq + 1);
tp->copied_seq = tp->rcv_nxt; WRITE_ONCE(tp->copied_seq, tp->rcv_nxt);
tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1; tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1;
/* RFC1323: The window in SYN & SYN/ACK segments is /* RFC1323: The window in SYN & SYN/ACK segments is
...@@ -6134,7 +6134,7 @@ int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb) ...@@ -6134,7 +6134,7 @@ int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb)
tcp_rearm_rto(sk); tcp_rearm_rto(sk);
} else { } else {
tcp_init_transfer(sk, BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB); tcp_init_transfer(sk, BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB);
tp->copied_seq = tp->rcv_nxt; WRITE_ONCE(tp->copied_seq, tp->rcv_nxt);
} }
smp_mb(); smp_mb();
tcp_set_state(sk, TCP_ESTABLISHED); tcp_set_state(sk, TCP_ESTABLISHED);
......
...@@ -2431,7 +2431,7 @@ static void get_tcp4_sock(struct sock *sk, struct seq_file *f, int i) ...@@ -2431,7 +2431,7 @@ static void get_tcp4_sock(struct sock *sk, struct seq_file *f, int i)
* we might find a transient negative value. * we might find a transient negative value.
*/ */
rx_queue = max_t(int, READ_ONCE(tp->rcv_nxt) - rx_queue = max_t(int, READ_ONCE(tp->rcv_nxt) -
tp->copied_seq, 0); READ_ONCE(tp->copied_seq), 0);
seq_printf(f, "%4d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX " seq_printf(f, "%4d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX "
"%08X %5u %8d %lu %d %pK %lu %lu %u %u %d", "%08X %5u %8d %lu %d %pK %lu %lu %u %u %d",
......
...@@ -470,7 +470,7 @@ struct sock *tcp_create_openreq_child(const struct sock *sk, ...@@ -470,7 +470,7 @@ struct sock *tcp_create_openreq_child(const struct sock *sk,
seq = treq->rcv_isn + 1; seq = treq->rcv_isn + 1;
newtp->rcv_wup = seq; newtp->rcv_wup = seq;
newtp->copied_seq = seq; WRITE_ONCE(newtp->copied_seq, seq);
WRITE_ONCE(newtp->rcv_nxt, seq); WRITE_ONCE(newtp->rcv_nxt, seq);
newtp->segs_in = 1; newtp->segs_in = 1;
......
...@@ -3381,7 +3381,7 @@ static void tcp_connect_init(struct sock *sk) ...@@ -3381,7 +3381,7 @@ static void tcp_connect_init(struct sock *sk)
else else
tp->rcv_tstamp = tcp_jiffies32; tp->rcv_tstamp = tcp_jiffies32;
tp->rcv_wup = tp->rcv_nxt; tp->rcv_wup = tp->rcv_nxt;
tp->copied_seq = tp->rcv_nxt; WRITE_ONCE(tp->copied_seq, tp->rcv_nxt);
inet_csk(sk)->icsk_rto = tcp_timeout_init(sk); inet_csk(sk)->icsk_rto = tcp_timeout_init(sk);
inet_csk(sk)->icsk_retransmits = 0; inet_csk(sk)->icsk_retransmits = 0;
......
...@@ -1839,7 +1839,7 @@ static void get_tcp6_sock(struct seq_file *seq, struct sock *sp, int i) ...@@ -1839,7 +1839,7 @@ static void get_tcp6_sock(struct seq_file *seq, struct sock *sp, int i)
* we might find a transient negative value. * we might find a transient negative value.
*/ */
rx_queue = max_t(int, READ_ONCE(tp->rcv_nxt) - rx_queue = max_t(int, READ_ONCE(tp->rcv_nxt) -
tp->copied_seq, 0); READ_ONCE(tp->copied_seq), 0);
seq_printf(seq, seq_printf(seq,
"%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X " "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册