Skip to contentSkip to conversation

rxrpc: Fix data-race warning and potential load/store tearing

PATCH Superseded David Howells 2 participants 4 messages Jan 13 – Jan 14

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

David Howells

author maintainer
#0 lore raw

PATCH 1/1 rxrpc: Fix data-race warning and potential load/store tearing

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.

---
 net/rxrpc/ar-internal.h |    9 ++++++++-
 net/rxrpc/conn_event.c  |    2 +-
 net/rxrpc/output.c      |   14 +++++++-------
 net/rxrpc/peer_event.c  |   17 ++++++++++++++++-
 net/rxrpc/proc.c        |    2 +-
 net/rxrpc/rxgk.c        |    2 +-
 net/rxrpc/rxkad.c       |    2 +-
 7 files changed, 35 insertions(+), 13 deletions(-)
net/rxrpc/ar-internal.h +8 -1
index 5b7342d43486..36d6ca0d1089 100644
@@ -387,7 +387,7 @@ struct rxrpc_peer {
 	struct rb_root		service_conns;	/* Service connections */
 	struct list_head	keepalive_link;	/* Link in net->peer_keepalive[] */
 	unsigned long		app_data;	/* Application data (e.g. afs_server) */
-	time64_t		last_tx_at;	/* Last time packet sent here */
+	unsigned int		last_tx_at;	/* Last time packet sent here (time64_t LSW) */
 	seqlock_t		service_conn_lock;
 	spinlock_t		lock;		/* access lock */
 	int			debug_id;	/* debug ID for printks */
@@ -1379,6 +1379,13 @@ void rxrpc_peer_keepalive_worker(struct work_struct *);
 void rxrpc_input_probe_for_pmtud(struct rxrpc_connection *conn, rxrpc_serial_t acked_serial,
 				 bool sendmsg_fail);
 
+/* Update the last transmission time on a peer for keepalive purposes. */
+static inline void rxrpc_peer_mark_tx(struct rxrpc_peer *peer)
+{
+	/* To avoid tearing on 32-bit systems, we only keep the LSW. */
+	WRITE_ONCE(peer->last_tx_at, ktime_get_seconds());
+}
+
 /*
  * peer_object.c
  */
net/rxrpc/conn_event.c +1 -1
index 232b6986da83..98ad9b51ca2c 100644
@@ -194,7 +194,7 @@ void rxrpc_conn_retransmit_call(struct rxrpc_connection *conn,
 	}
 
 	ret = kernel_sendmsg(conn->local->socket, &msg, iov, ioc, len);
-	conn->peer->last_tx_at = ktime_get_seconds();
+	rxrpc_peer_mark_tx(conn->peer);
 	if (ret < 0)
 		trace_rxrpc_tx_fail(chan->call_debug_id, serial, ret,
 				    rxrpc_tx_point_call_final_resend);
net/rxrpc/output.c +7 -7
index 8b5903b6e481..d70db367e358 100644
@@ -275,7 +275,7 @@ static void rxrpc_send_ack_packet(struct rxrpc_call *call, int nr_kv, size_t len
 	rxrpc_local_dont_fragment(conn->local, why == rxrpc_propose_ack_ping_for_mtu_probe);
 
 	ret = do_udp_sendmsg(conn->local->socket, &msg, len);
-	call->peer->last_tx_at = ktime_get_seconds();
+	rxrpc_peer_mark_tx(call->peer);
 	if (ret < 0) {
 		trace_rxrpc_tx_fail(call->debug_id, serial, ret,
 				    rxrpc_tx_point_call_ack);
@@ -411,7 +411,7 @@ int rxrpc_send_abort_packet(struct rxrpc_call *call)
 
 	iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, sizeof(pkt));
 	ret = do_udp_sendmsg(conn->local->socket, &msg, sizeof(pkt));
-	conn->peer->last_tx_at = ktime_get_seconds();
+	rxrpc_peer_mark_tx(conn->peer);
 	if (ret < 0)
 		trace_rxrpc_tx_fail(call->debug_id, serial, ret,
 				    rxrpc_tx_point_call_abort);
@@ -698,7 +698,7 @@ void rxrpc_send_data_packet(struct rxrpc_call *call, struct rxrpc_send_data_req
 			ret = 0;
 			trace_rxrpc_tx_data(call, txb->seq, txb->serial, txb->flags,
 					    rxrpc_txdata_inject_loss);
-			conn->peer->last_tx_at = ktime_get_seconds();
+			rxrpc_peer_mark_tx(conn->peer);
 			goto done;
 		}
 	}
@@ -711,7 +711,7 @@ void rxrpc_send_data_packet(struct rxrpc_call *call, struct rxrpc_send_data_req
 	 */
 	rxrpc_inc_stat(call->rxnet, stat_tx_data_send);
 	ret = do_udp_sendmsg(conn->local->socket, &msg, len);
-	conn->peer->last_tx_at = ktime_get_seconds();
+	rxrpc_peer_mark_tx(conn->peer);
 
 	if (ret == -EMSGSIZE) {
 		rxrpc_inc_stat(call->rxnet, stat_tx_data_send_msgsize);
@@ -797,7 +797,7 @@ void rxrpc_send_conn_abort(struct rxrpc_connection *conn)
 
 	trace_rxrpc_tx_packet(conn->debug_id, &whdr, rxrpc_tx_point_conn_abort);
 
-	conn->peer->last_tx_at = ktime_get_seconds();
+	rxrpc_peer_mark_tx(conn->peer);
 }
 
 /*
@@ -917,7 +917,7 @@ void rxrpc_send_keepalive(struct rxrpc_peer *peer)
 		trace_rxrpc_tx_packet(peer->debug_id, &whdr,
 				      rxrpc_tx_point_version_keepalive);
 
-	peer->last_tx_at = ktime_get_seconds();
+	rxrpc_peer_mark_tx(peer);
 	_leave("");
 }
 
@@ -973,7 +973,7 @@ void rxrpc_send_response(struct rxrpc_connection *conn, struct sk_buff *response
 	if (ret < 0)
 		goto fail;
 
-	conn->peer->last_tx_at = ktime_get_seconds();
+	rxrpc_peer_mark_tx(conn->peer);
 	return;
 
 fail:
net/rxrpc/peer_event.c +16 -1
index 7f4729234957..9d02448ac062 100644
@@ -237,6 +237,21 @@ static void rxrpc_distribute_error(struct rxrpc_peer *peer, struct sk_buff *skb,
 	spin_unlock_irq(&peer->lock);
 }
 
+/*
+ * Reconstruct the last transmission time.  The difference calculated should be
+ * valid provided no more than ~68 years elapsed since the last transmission.
+ */
+static time64_t rxrpc_peer_get_tx_mark(const struct rxrpc_peer *peer, time64_t base)
+{
+	s32 last_tx_at = READ_ONCE(peer->last_tx_at);
+	s32 base_lsw = base;
+	s32 diff = last_tx_at - base_lsw;
+
+	diff = clamp(diff, -RXRPC_KEEPALIVE_TIME, RXRPC_KEEPALIVE_TIME);
+
+	return diff + base;
+}
+
 /*
  * Perform keep-alive pings.
  */
@@ -265,7 +280,7 @@ static void rxrpc_peer_keepalive_dispatch(struct rxrpc_net *rxnet,
 		spin_unlock_bh(&rxnet->peer_hash_lock);
 
 		if (use) {
-			keepalive_at = peer->last_tx_at + RXRPC_KEEPALIVE_TIME;
+			keepalive_at = rxrpc_peer_get_tx_mark(peer, base) + RXRPC_KEEPALIVE_TIME;
 			slot = keepalive_at - base;
 			_debug("%02x peer %u t=%d {%pISp}",
 			       cursor, peer->debug_id, slot, &peer->srx.transport);
net/rxrpc/proc.c +1 -1
index d803562ca0ac..8c9f9d510fe2 100644
@@ -302,7 +302,7 @@ static int rxrpc_peer_seq_show(struct seq_file *seq, void *v)
 		   refcount_read(&peer->ref),
 		   peer->cong_ssthresh,
 		   peer->max_data,
-		   now - peer->last_tx_at,
+		   (s32)now - (s32)peer->last_tx_at,
 		   READ_ONCE(peer->recent_srtt_us),
 		   READ_ONCE(peer->recent_rto_us));
 
net/rxrpc/rxgk.c +1 -1
index dce5a3d8a964..43cbf9efd89f 100644
@@ -678,7 +678,7 @@ static int rxgk_issue_challenge(struct rxrpc_connection *conn)
 
 	ret = do_udp_sendmsg(conn->local->socket, &msg, len);
 	if (ret > 0)
-		conn->peer->last_tx_at = ktime_get_seconds();
+		rxrpc_peer_mark_tx(conn->peer);
 	__free_page(page);
 
 	if (ret < 0) {
net/rxrpc/rxkad.c +1 -1
index 3657c0661cdc..a756855a0a62 100644
@@ -694,7 +694,7 @@ static int rxkad_issue_challenge(struct rxrpc_connection *conn)
 		return -EAGAIN;
 	}
 
-	conn->peer->last_tx_at = ktime_get_seconds();
+	rxrpc_peer_mark_tx(conn->peer);
 	trace_rxrpc_tx_packet(conn->debug_id, &whdr,
 			      rxrpc_tx_point_rxkad_challenge);
 	_leave(" = 0");
author maintainer
#1 lore raw
Fixes ace45bec6d77 ("rxrpc: Fix firewall route keepalive")
#2 lore raw
In reply to David Howells
kernel test robot noticed the following build warnings:

Hi David,

kernel test robot noticed the following build warnings:

[auto build test WARNING on net/main]
[also build test WARNING on net-next/main linus/master v6.19-rc5 next-20260113] [cannot apply to horms-ipvs/master]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/David-Howells/rxrpc-Fix-data-race-warning-and-potential-load-store-tearing/20260114-005726
base:   net/main
patch link:    https://lore.kernel.org/r/3535584.1768322992%40warthog.procyon.org.uk
patch subject: [PATCH] rxrpc: Fix data-race warning and potential load/store tearing
config: x86_64-rhel-9.4-ltp (https://download.01.org/0day-ci/archive/20260114/[email protected]/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260114/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

net/rxrpc/proc.c: In function 'rxrpc_peer_seq_show':

>> net/rxrpc/proc.c:299:61: warning: format '%llu' expects argument of type 'long long unsigned int', but argument 8 has type 'int' [-Wformat=]
     299 |                    "UDP   %-47.47s %-47.47s %3u %4u %5u %6llus %8d %8d\n",
         |                                                         ~~~~^
         |                                                             |
         |                                                             long long unsigned int
         |                                                         %6u
   ......
     305 |                    (s32)now - (s32)peer->last_tx_at,
         |                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~          
         |                             |
         |                             int

vim +299 net/rxrpc/proc.c

d2ce4a84c21f803 David Howells 2023-10-26  274  
bc0e7cf43370a8e David Howells 2018-10-15  275  /*
bc0e7cf43370a8e David Howells 2018-10-15  276   * generate a list of extant virtual peers in /proc/net/rxrpc/peers
bc0e7cf43370a8e David Howells 2018-10-15  277   */
bc0e7cf43370a8e David Howells 2018-10-15  278  static int rxrpc_peer_seq_show(struct seq_file *seq, void *v)
bc0e7cf43370a8e David Howells 2018-10-15  279  {
bc0e7cf43370a8e David Howells 2018-10-15  280  	struct rxrpc_peer *peer;
bc0e7cf43370a8e David Howells 2018-10-15  281  	time64_t now;
bc0e7cf43370a8e David Howells 2018-10-15  282  	char lbuff[50], rbuff[50];
bc0e7cf43370a8e David Howells 2018-10-15  283  
bc0e7cf43370a8e David Howells 2018-10-15  284  	if (v == SEQ_START_TOKEN) {
bc0e7cf43370a8e David Howells 2018-10-15  285  		seq_puts(seq,
eeaedc5449d9fcc David Howells 2024-12-04  286  			 "Proto Local                                           Remote                                          Use SST   Maxd LastUse      RTT      RTO\n"
bc0e7cf43370a8e David Howells 2018-10-15  287  			 );
bc0e7cf43370a8e David Howells 2018-10-15  288  		return 0;
bc0e7cf43370a8e David Howells 2018-10-15  289  	}
bc0e7cf43370a8e David Howells 2018-10-15  290  
bc0e7cf43370a8e David Howells 2018-10-15  291  	peer = list_entry(v, struct rxrpc_peer, hash_link);
bc0e7cf43370a8e David Howells 2018-10-15  292  
bc0e7cf43370a8e David Howells 2018-10-15  293  	sprintf(lbuff, "%pISpc", &peer->local->srx.transport);
bc0e7cf43370a8e David Howells 2018-10-15  294  
bc0e7cf43370a8e David Howells 2018-10-15  295  	sprintf(rbuff, "%pISpc", &peer->srx.transport);
bc0e7cf43370a8e David Howells 2018-10-15  296  
bc0e7cf43370a8e David Howells 2018-10-15  297  	now = ktime_get_seconds();
bc0e7cf43370a8e David Howells 2018-10-15  298  	seq_printf(seq,
b40ef2b85a7d117 David Howells 2024-12-04 @299  		   "UDP   %-47.47s %-47.47s %3u %4u %5u %6llus %8d %8d\n",
bc0e7cf43370a8e David Howells 2018-10-15  300  		   lbuff,
bc0e7cf43370a8e David Howells 2018-10-15  301  		   rbuff,
a05754295e01f00 David Howells 2022-05-21  302  		   refcount_read(&peer->ref),
1fc4fa2ac93dcf3 David Howells 2022-10-03  303  		   peer->cong_ssthresh,
eeaedc5449d9fcc David Howells 2024-12-04  304  		   peer->max_data,
db6f1fa7f67e415 David Howells 2026-01-13  305  		   (s32)now - (s32)peer->last_tx_at,
b40ef2b85a7d117 David Howells 2024-12-04  306  		   READ_ONCE(peer->recent_srtt_us),
b40ef2b85a7d117 David Howells 2024-12-04  307  		   READ_ONCE(peer->recent_rto_us));
bc0e7cf43370a8e David Howells 2018-10-15  308  
bc0e7cf43370a8e David Howells 2018-10-15  309  	return 0;
bc0e7cf43370a8e David Howells 2018-10-15  310  }
bc0e7cf43370a8e David Howells 2018-10-15  311  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#3 lore raw
In reply to David Howells
kernel test robot noticed the following build warnings:

Hi David,

kernel test robot noticed the following build warnings:

[auto build test WARNING on net/main]
[also build test WARNING on net-next/main linus/master v6.19-rc5 next-20260113] [cannot apply to horms-ipvs/master]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/David-Howells/rxrpc-Fix-data-race-warning-and-potential-load-store-tearing/20260114-005726
base:   net/main
patch link:    https://lore.kernel.org/r/3535584.1768322992%40warthog.procyon.org.uk
patch subject: [PATCH] rxrpc: Fix data-race warning and potential load/store tearing
config: x86_64-buildonly-randconfig-006-20260114 (https://download.01.org/0day-ci/archive/20260114/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260114/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> net/rxrpc/proc.c:305:6: warning: format specifies type 'unsigned long long' but the argument has type 's32' (aka 'int') [-Wformat]
     299 |                    "UDP   %-47.47s %-47.47s %3u %4u %5u %6llus %8d %8d\n",
         |                                                         ~~~~~
         |                                                         %6d
     300 |                    lbuff,
     301 |                    rbuff,
     302 |                    refcount_read(&peer->ref),
     303 |                    peer->cong_ssthresh,
     304 |                    peer->max_data,
     305 |                    (s32)now - (s32)peer->last_tx_at,
         |                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1 warning generated.

vim +305 net/rxrpc/proc.c

274
275 /*
276 * generate a list of extant virtual peers in /proc/net/rxrpc/peers
277 */
278 static int rxrpc_peer_seq_show(struct seq_file *seq, void *v)
279 {
280 struct rxrpc_peer *peer;
281 time64_t now;
282 char lbuff[50], rbuff[50];
283
284 if (v == SEQ_START_TOKEN) {
285 seq_puts(seq,
286 "Proto Local Remote Use SST Maxd LastUse RTT RTO\n"
287 );
288 return 0;
289 }
290
291 peer = list_entry(v, struct rxrpc_peer, hash_link);
292
293 sprintf(lbuff, "%pISpc", &peer->local->srx.transport);
294
295 sprintf(rbuff, "%pISpc", &peer->srx.transport);
296
297 now = ktime_get_seconds();
298 seq_printf(seq,

   299			   "UDP   %-47.47s %-47.47s %3u %4u %5u %6llus %8d %8d\n",
   300			   lbuff,
   301			   rbuff,
   302			   refcount_read(&peer->ref),
   303			   peer->cong_ssthresh,
   304			   peer->max_data,
 > 305			   (s32)now - (s32)peer->last_tx_at,
   306			   READ_ONCE(peer->recent_srtt_us),
   307			   READ_ONCE(peer->recent_rto_us));

308
309 return 0;
310 }
311

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki