Skip to content

Commit c08cf31

Browse files
dhowellsgregkh
authored andcommitted
rxrpc: Fix data-race warning and potential load/store tearing
[ Upstream commit 5d5fe8b ] Fix the following: BUG: KCSAN: data-race in rxrpc_peer_keepalive_worker / rxrpc_send_data_packet which is reporting an issue with the reads and writes to ->last_tx_at in: conn->peer->last_tx_at = ktime_get_seconds(); and: keepalive_at = peer->last_tx_at + RXRPC_KEEPALIVE_TIME; The lockless accesses to these to values aren't actually a problem as the read only needs an approximate time of last transmission for the purposes of deciding whether or not the transmission of a keepalive packet is warranted yet. Also, as ->last_tx_at is a 64-bit value, tearing can occur on a 32-bit arch. Fix both of these by switching to an unsigned int for ->last_tx_at and only storing the LSW of the time64_t. It can then be reconstructed at need provided no more than 68 years has elapsed since the last transmission. Fixes: ace45be ("rxrpc: Fix firewall route keepalive") Reported-by: syzbot+6182afad5045e6703b3d@syzkaller.appspotmail.com Closes: https://lore.kernel.org/r/695e7cfb.050a0220.1c677c.036b.GAE@google.com/ Signed-off-by: David Howells <dhowells@redhat.com> cc: Marc Dionne <marc.dionne@auristor.com> cc: Simon Horman <horms@kernel.org> cc: linux-afs@lists.infradead.org cc: stable@kernel.org Link: https://patch.msgid.link/1107124.1768903985@warthog.procyon.org.uk Signed-off-by: Jakub Kicinski <kuba@kernel.org> [ different struct fields (peer->mtu, peer->srtt_us, peer->rto_us) and different output.c code structure ] Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 9930470 commit c08cf31

6 files changed

Lines changed: 34 additions & 12 deletions

File tree

net/rxrpc/ar-internal.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ struct rxrpc_peer {
335335
struct hlist_head error_targets; /* targets for net error distribution */
336336
struct rb_root service_conns; /* Service connections */
337337
struct list_head keepalive_link; /* Link in net->peer_keepalive[] */
338-
time64_t last_tx_at; /* Last time packet sent here */
338+
unsigned int last_tx_at; /* Last time packet sent here (time64_t LSW) */
339339
seqlock_t service_conn_lock;
340340
spinlock_t lock; /* access lock */
341341
unsigned int if_mtu; /* interface MTU for this peer */
@@ -1161,6 +1161,13 @@ void rxrpc_transmit_one(struct rxrpc_call *call, struct rxrpc_txbuf *txb);
11611161
void rxrpc_input_error(struct rxrpc_local *, struct sk_buff *);
11621162
void rxrpc_peer_keepalive_worker(struct work_struct *);
11631163

1164+
/* Update the last transmission time on a peer for keepalive purposes. */
1165+
static inline void rxrpc_peer_mark_tx(struct rxrpc_peer *peer)
1166+
{
1167+
/* To avoid tearing on 32-bit systems, we only keep the LSW. */
1168+
WRITE_ONCE(peer->last_tx_at, ktime_get_seconds());
1169+
}
1170+
11641171
/*
11651172
* peer_object.c
11661173
*/

net/rxrpc/conn_event.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ void rxrpc_conn_retransmit_call(struct rxrpc_connection *conn,
180180
}
181181

182182
ret = kernel_sendmsg(conn->local->socket, &msg, iov, ioc, len);
183-
conn->peer->last_tx_at = ktime_get_seconds();
183+
rxrpc_peer_mark_tx(conn->peer);
184184
if (ret < 0)
185185
trace_rxrpc_tx_fail(chan->call_debug_id, serial, ret,
186186
rxrpc_tx_point_call_final_resend);

net/rxrpc/output.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ static void rxrpc_send_ack_packet(struct rxrpc_call *call, struct rxrpc_txbuf *t
209209
iov_iter_kvec(&msg.msg_iter, WRITE, txb->kvec, txb->nr_kvec, txb->len);
210210
rxrpc_local_dont_fragment(conn->local, false);
211211
ret = do_udp_sendmsg(conn->local->socket, &msg, txb->len);
212-
call->peer->last_tx_at = ktime_get_seconds();
212+
rxrpc_peer_mark_tx(call->peer);
213213
if (ret < 0) {
214214
trace_rxrpc_tx_fail(call->debug_id, txb->serial, ret,
215215
rxrpc_tx_point_call_ack);
@@ -310,7 +310,7 @@ int rxrpc_send_abort_packet(struct rxrpc_call *call)
310310

311311
iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, sizeof(pkt));
312312
ret = do_udp_sendmsg(conn->local->socket, &msg, sizeof(pkt));
313-
conn->peer->last_tx_at = ktime_get_seconds();
313+
rxrpc_peer_mark_tx(conn->peer);
314314
if (ret < 0)
315315
trace_rxrpc_tx_fail(call->debug_id, serial, ret,
316316
rxrpc_tx_point_call_abort);
@@ -486,7 +486,7 @@ static int rxrpc_send_data_packet(struct rxrpc_call *call, struct rxrpc_txbuf *t
486486
*/
487487
rxrpc_inc_stat(call->rxnet, stat_tx_data_send);
488488
ret = do_udp_sendmsg(conn->local->socket, &msg, len);
489-
conn->peer->last_tx_at = ktime_get_seconds();
489+
rxrpc_peer_mark_tx(conn->peer);
490490

491491
if (ret < 0) {
492492
rxrpc_inc_stat(call->rxnet, stat_tx_data_send_fail);
@@ -573,7 +573,7 @@ void rxrpc_send_conn_abort(struct rxrpc_connection *conn)
573573

574574
trace_rxrpc_tx_packet(conn->debug_id, &whdr, rxrpc_tx_point_conn_abort);
575575

576-
conn->peer->last_tx_at = ktime_get_seconds();
576+
rxrpc_peer_mark_tx(conn->peer);
577577
}
578578

579579
/*
@@ -692,7 +692,7 @@ void rxrpc_send_keepalive(struct rxrpc_peer *peer)
692692
trace_rxrpc_tx_packet(peer->debug_id, &whdr,
693693
rxrpc_tx_point_version_keepalive);
694694

695-
peer->last_tx_at = ktime_get_seconds();
695+
rxrpc_peer_mark_tx(peer);
696696
_leave("");
697697
}
698698

net/rxrpc/peer_event.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,21 @@ static void rxrpc_distribute_error(struct rxrpc_peer *peer, struct sk_buff *skb,
224224
spin_unlock(&peer->lock);
225225
}
226226

227+
/*
228+
* Reconstruct the last transmission time. The difference calculated should be
229+
* valid provided no more than ~68 years elapsed since the last transmission.
230+
*/
231+
static time64_t rxrpc_peer_get_tx_mark(const struct rxrpc_peer *peer, time64_t base)
232+
{
233+
s32 last_tx_at = READ_ONCE(peer->last_tx_at);
234+
s32 base_lsw = base;
235+
s32 diff = last_tx_at - base_lsw;
236+
237+
diff = clamp(diff, -RXRPC_KEEPALIVE_TIME, RXRPC_KEEPALIVE_TIME);
238+
239+
return diff + base;
240+
}
241+
227242
/*
228243
* Perform keep-alive pings.
229244
*/
@@ -252,7 +267,7 @@ static void rxrpc_peer_keepalive_dispatch(struct rxrpc_net *rxnet,
252267
spin_unlock_bh(&rxnet->peer_hash_lock);
253268

254269
if (use) {
255-
keepalive_at = peer->last_tx_at + RXRPC_KEEPALIVE_TIME;
270+
keepalive_at = rxrpc_peer_get_tx_mark(peer, base) + RXRPC_KEEPALIVE_TIME;
256271
slot = keepalive_at - base;
257272
_debug("%02x peer %u t=%d {%pISp}",
258273
cursor, peer->debug_id, slot, &peer->srx.transport);

net/rxrpc/proc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,13 @@ static int rxrpc_peer_seq_show(struct seq_file *seq, void *v)
299299
now = ktime_get_seconds();
300300
seq_printf(seq,
301301
"UDP %-47.47s %-47.47s %3u"
302-
" %3u %5u %6llus %8u %8u\n",
302+
" %3u %5u %6ds %8u %8u\n",
303303
lbuff,
304304
rbuff,
305305
refcount_read(&peer->ref),
306306
peer->cong_ssthresh,
307307
peer->mtu,
308-
now - peer->last_tx_at,
308+
(s32)now - (s32)READ_ONCE(peer->last_tx_at),
309309
peer->srtt_us >> 3,
310310
peer->rto_us);
311311

net/rxrpc/rxkad.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ static int rxkad_issue_challenge(struct rxrpc_connection *conn)
676676
return -EAGAIN;
677677
}
678678

679-
conn->peer->last_tx_at = ktime_get_seconds();
679+
rxrpc_peer_mark_tx(conn->peer);
680680
trace_rxrpc_tx_packet(conn->debug_id, &whdr,
681681
rxrpc_tx_point_rxkad_challenge);
682682
_leave(" = 0");
@@ -734,7 +734,7 @@ static int rxkad_send_response(struct rxrpc_connection *conn,
734734
return -EAGAIN;
735735
}
736736

737-
conn->peer->last_tx_at = ktime_get_seconds();
737+
rxrpc_peer_mark_tx(conn->peer);
738738
_leave(" = 0");
739739
return 0;
740740
}

0 commit comments

Comments
 (0)